📄 dm-snap.c
字号:
s->valid = 0; flush = __flush_bios(pe); up_write(&s->lock); error_bios(bio_list_get(&pe->snapshot_bios)); goto out; } *e = pe->e; /* * Add a proper exception, and remove the * in-flight exception from the list. */ down_write(&s->lock); insert_exception(&s->complete, e); remove_exception(&pe->e); flush = __flush_bios(pe); /* Submit any pending write bios */ up_write(&s->lock); flush_bios(bio_list_get(&pe->snapshot_bios)); } else { /* Read/write error - snapshot is unusable */ down_write(&s->lock); if (s->valid) DMERR("Error reading/writing snapshot"); s->store.drop_snapshot(&s->store); s->valid = 0; remove_exception(&pe->e); flush = __flush_bios(pe); up_write(&s->lock); error_bios(bio_list_get(&pe->snapshot_bios)); dm_table_event(s->table); } out: free_pending_exception(pe); if (flush) flush_bios(flush);}static void commit_callback(void *context, int success){ struct pending_exception *pe = (struct pending_exception *) context; pending_complete(pe, success);}/* * Called when the copy I/O has finished. kcopyd actually runs * this code so don't block. */static void copy_callback(int read_err, unsigned int write_err, void *context){ struct pending_exception *pe = (struct pending_exception *) context; struct dm_snapshot *s = pe->snap; if (read_err || write_err) pending_complete(pe, 0); else /* Update the metadata if we are persistent */ s->store.commit_exception(&s->store, &pe->e, commit_callback, pe);}/* * Dispatches the copy operation to kcopyd. */static inline void start_copy(struct pending_exception *pe){ struct dm_snapshot *s = pe->snap; struct io_region src, dest; struct block_device *bdev = s->origin->bdev; sector_t dev_size; dev_size = get_dev_size(bdev); src.bdev = bdev; src.sector = chunk_to_sector(s, pe->e.old_chunk); src.count = min(s->chunk_size, dev_size - src.sector); dest.bdev = s->cow->bdev; dest.sector = chunk_to_sector(s, pe->e.new_chunk); dest.count = src.count; /* Hand over to kcopyd */ kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, copy_callback, pe);}/* * Looks to see if this snapshot already has a pending exception * for this chunk, otherwise it allocates a new one and inserts * it into the pending table. * * NOTE: a write lock must be held on snap->lock before calling * this. */static struct pending_exception *__find_pending_exception(struct dm_snapshot *s, struct bio *bio){ struct exception *e; struct pending_exception *pe; chunk_t chunk = sector_to_chunk(s, bio->bi_sector); /* * Is there a pending exception for this already ? */ e = lookup_exception(&s->pending, chunk); if (e) { /* cast the exception to a pending exception */ pe = container_of(e, struct pending_exception, e); } else { /* * Create a new pending exception, we don't want * to hold the lock while we do this. */ up_write(&s->lock); pe = alloc_pending_exception(); down_write(&s->lock); e = lookup_exception(&s->pending, chunk); if (e) { free_pending_exception(pe); pe = container_of(e, struct pending_exception, e); } else { pe->e.old_chunk = chunk; bio_list_init(&pe->origin_bios); bio_list_init(&pe->snapshot_bios); INIT_LIST_HEAD(&pe->siblings); pe->snap = s; pe->started = 0; if (s->store.prepare_exception(&s->store, &pe->e)) { free_pending_exception(pe); s->valid = 0; return NULL; } insert_exception(&s->pending, &pe->e); } } return pe;}static inline void remap_exception(struct dm_snapshot *s, struct exception *e, struct bio *bio){ bio->bi_bdev = s->cow->bdev; bio->bi_sector = chunk_to_sector(s, e->new_chunk) + (bio->bi_sector & s->chunk_mask);}static int snapshot_map(struct dm_target *ti, struct bio *bio, union map_info *map_context){ struct exception *e; struct dm_snapshot *s = (struct dm_snapshot *) ti->private; int r = 1; chunk_t chunk; struct pending_exception *pe; chunk = sector_to_chunk(s, bio->bi_sector); /* Full snapshots are not usable */ if (!s->valid) return -1; /* * Write to snapshot - higher level takes care of RW/RO * flags so we should only get this if we are * writeable. */ if (bio_rw(bio) == WRITE) { /* FIXME: should only take write lock if we need * to copy an exception */ down_write(&s->lock); /* If the block is already remapped - use that, else remap it */ e = lookup_exception(&s->complete, chunk); if (e) { remap_exception(s, e, bio); up_write(&s->lock); } else { pe = __find_pending_exception(s, bio); if (!pe) { if (s->store.drop_snapshot) s->store.drop_snapshot(&s->store); s->valid = 0; r = -EIO; up_write(&s->lock); } else { remap_exception(s, &pe->e, bio); bio_list_add(&pe->snapshot_bios, bio); if (!pe->started) { /* this is protected by snap->lock */ pe->started = 1; up_write(&s->lock); start_copy(pe); } else up_write(&s->lock); r = 0; } } } else { /* * FIXME: this read path scares me because we * always use the origin when we have a pending * exception. However I can't think of a * situation where this is wrong - ejt. */ /* Do reads */ down_read(&s->lock); /* See if it it has been remapped */ e = lookup_exception(&s->complete, chunk); if (e) remap_exception(s, e, bio); else bio->bi_bdev = s->origin->bdev; up_read(&s->lock); } return r;}static void snapshot_resume(struct dm_target *ti){ struct dm_snapshot *s = (struct dm_snapshot *) ti->private; if (s->have_metadata) return; if (s->store.read_metadata(&s->store)) { down_write(&s->lock); s->valid = 0; up_write(&s->lock); } s->have_metadata = 1;}static int snapshot_status(struct dm_target *ti, status_type_t type, char *result, unsigned int maxlen){ struct dm_snapshot *snap = (struct dm_snapshot *) ti->private; char cow[32]; char org[32]; switch (type) { case STATUSTYPE_INFO: if (!snap->valid) snprintf(result, maxlen, "Invalid"); else { if (snap->store.fraction_full) { sector_t numerator, denominator; snap->store.fraction_full(&snap->store, &numerator, &denominator); snprintf(result, maxlen, SECTOR_FORMAT "/" SECTOR_FORMAT, numerator, denominator); } else snprintf(result, maxlen, "Unknown"); } break; case STATUSTYPE_TABLE: /* * kdevname returns a static pointer so we need * to make private copies if the output is to * make sense. */ format_dev_t(cow, snap->cow->bdev->bd_dev); format_dev_t(org, snap->origin->bdev->bd_dev); snprintf(result, maxlen, "%s %s %c " SECTOR_FORMAT, org, cow, snap->type, snap->chunk_size); break; } return 0;}/*----------------------------------------------------------------- * Origin methods *---------------------------------------------------------------*/static void list_merge(struct list_head *l1, struct list_head *l2){ struct list_head *l1_n, *l2_p; l1_n = l1->next; l2_p = l2->prev; l1->next = l2; l2->prev = l1; l2_p->next = l1_n; l1_n->prev = l2_p;}static int __origin_write(struct list_head *snapshots, struct bio *bio){ int r = 1, first = 1; struct dm_snapshot *snap; struct exception *e; struct pending_exception *pe, *last = NULL; chunk_t chunk; /* Do all the snapshots on this origin */ list_for_each_entry (snap, snapshots, list) { /* Only deal with valid snapshots */ if (!snap->valid) continue; down_write(&snap->lock); /* * Remember, different snapshots can have * different chunk sizes. */ chunk = sector_to_chunk(snap, bio->bi_sector); /* * Check exception table to see if block * is already remapped in this snapshot * and trigger an exception if not. */ e = lookup_exception(&snap->complete, chunk); if (!e) { pe = __find_pending_exception(snap, bio); if (!pe) { snap->store.drop_snapshot(&snap->store); snap->valid = 0; } else { if (last) list_merge(&pe->siblings, &last->siblings); last = pe; r = 0; } } up_write(&snap->lock); } /* * Now that we have a complete pe list we can start the copying. */ if (last) { pe = last; do { down_write(&pe->snap->lock); if (first) bio_list_add(&pe->origin_bios, bio); if (!pe->started) { pe->started = 1; up_write(&pe->snap->lock); start_copy(pe); } else up_write(&pe->snap->lock); first = 0; pe = list_entry(pe->siblings.next, struct pending_exception, siblings); } while (pe != last); } return r;}/* * Called on a write from the origin driver. */static int do_origin(struct dm_dev *origin, struct bio *bio){ struct origin *o; int r = 1; down_read(&_origins_lock); o = __lookup_origin(origin->bdev); if (o) r = __origin_write(&o->snapshots, bio); up_read(&_origins_lock); return r;}/* * Origin: maps a linear range of a device, with hooks for snapshotting. *//* * Construct an origin mapping: <dev_path> * The context for an origin is merely a 'struct dm_dev *' * pointing to the real device. */static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv){ int r; struct dm_dev *dev; if (argc != 1) { ti->error = "dm-origin: incorrect number of arguments"; return -EINVAL; } r = dm_get_device(ti, argv[0], 0, ti->len, dm_table_get_mode(ti->table), &dev); if (r) { ti->error = "Cannot get target device"; return r; } ti->private = dev; return 0;}static void origin_dtr(struct dm_target *ti){ struct dm_dev *dev = (struct dm_dev *) ti->private; dm_put_device(ti, dev);}static int origin_map(struct dm_target *ti, struct bio *bio, union map_info *map_context){ struct dm_dev *dev = (struct dm_dev *) ti->private; bio->bi_bdev = dev->bdev; /* Only tell snapshots if this is a write */ return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : 1;}#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))/* * Set the target "split_io" field to the minimum of all the snapshots' * chunk sizes. */static void origin_resume(struct dm_target *ti){ struct dm_dev *dev = (struct dm_dev *) ti->private; struct dm_snapshot *snap; struct origin *o; chunk_t chunk_size = 0; down_read(&_origins_lock); o = __lookup_origin(dev->bdev); if (o) list_for_each_entry (snap, &o->snapshots, list) chunk_size = min_not_zero(chunk_size, snap->chunk_size); up_read(&_origins_lock); ti->split_io = chunk_size;}static int origin_status(struct dm_target *ti, status_type_t type, char *result, unsigned int maxlen){ struct dm_dev *dev = (struct dm_dev *) ti->private; char buffer[32]; switch (type) { case STATUSTYPE_INFO: result[0] = '\0'; break; case STATUSTYPE_TABLE: format_dev_t(buffer, dev->bdev->bd_dev); snprintf(result, maxlen, "%s", buffer); break; } return 0;}static struct target_type origin_target = { .name = "snapshot-origin", .version = {1, 0, 1}, .module = THIS_MODULE, .ctr = origin_ctr, .dtr = origin_dtr, .map = origin_map, .resume = origin_resume, .status = origin_status,};static struct target_type snapshot_target = { .name = "snapshot", .version = {1, 0, 1}, .module = THIS_MODULE, .ctr = snapshot_ctr, .dtr = snapshot_dtr, .map = snapshot_map, .resume = snapshot_resume, .status = snapshot_status,};static int __init dm_snapshot_init(void){ int r; r = dm_register_target(&snapshot_target); if (r) { DMERR("snapshot target register failed %d", r); return r; } r = dm_register_target(&origin_target); if (r < 0) { DMERR("Device mapper: Origin: register failed %d\n", r); goto bad1; } r = init_origin_hash(); if (r) { DMERR("init_origin_hash failed."); goto bad2; } exception_cache = kmem_cache_create("dm-snapshot-ex", sizeof(struct exception), __alignof__(struct exception), 0, NULL, NULL); if (!exception_cache) { DMERR("Couldn't create exception cache."); r = -ENOMEM; goto bad3; } pending_cache = kmem_cache_create("dm-snapshot-in", sizeof(struct pending_exception), __alignof__(struct pending_exception), 0, NULL, NULL); if (!pending_cache) { DMERR("Couldn't create pending cache."); r = -ENOMEM; goto bad4; } pending_pool = mempool_create(128, mempool_alloc_slab, mempool_free_slab, pending_cache); if (!pending_pool) { DMERR("Couldn't create pending pool."); r = -ENOMEM; goto bad5; } return 0; bad5: kmem_cache_destroy(pending_cache); bad4: kmem_cache_destroy(exception_cache); bad3: exit_origin_hash(); bad2: dm_unregister_target(&origin_target); bad1: dm_unregister_target(&snapshot_target); return r;}static void __exit dm_snapshot_exit(void){ int r; r = dm_unregister_target(&snapshot_target); if (r) DMERR("snapshot unregister failed %d", r); r = dm_unregister_target(&origin_target); if (r) DMERR("origin unregister failed %d", r); exit_origin_hash(); mempool_destroy(pending_pool); kmem_cache_destroy(pending_cache); kmem_cache_destroy(exception_cache);}/* Module hooks */module_init(dm_snapshot_init);module_exit(dm_snapshot_exit);MODULE_DESCRIPTION(DM_NAME " snapshot target");MODULE_AUTHOR("Joe Thornber");MODULE_LICENSE("GPL");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -