1 /*
2 * Copyright (C) 2001-2003 Sistina Software (UK) Limited.
3 *
4 * This file is released under the GPL.
5 */
6
7 #include "dm.h"
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/blkdev.h>
11 #include <linux/bio.h>
12 #include <linux/slab.h>
13 #include <linux/device-mapper.h>
14
15 #define DM_MSG_PREFIX "linear"
16
17 /*
18 * Linear: maps a linear range of a device.
19 */
20 struct linear_c {
21 struct dm_dev *dev;
22 sector_t start;
23 };
24
25 /*
26 * Construct a linear mapping: <dev_path> <offset>
27 */
dm_linear_ctr(struct dm_target * ti,unsigned int argc,char ** argv)28 int dm_linear_ctr(struct dm_target *ti, unsigned int argc, char **argv)
29 {
30 struct linear_c *lc;
31 unsigned long long tmp;
32 char dummy;
33
34 if (argc != 2) {
35 ti->error = "Invalid argument count";
36 return -EINVAL;
37 }
38
39 lc = kmalloc(sizeof(*lc), GFP_KERNEL);
40 if (lc == NULL) {
41 ti->error = "dm-linear: Cannot allocate linear context";
42 return -ENOMEM;
43 }
44
45 if (sscanf(argv[1], "%llu%c", &tmp, &dummy) != 1) {
46 ti->error = "dm-linear: Invalid device sector";
47 goto bad;
48 }
49 lc->start = tmp;
50
51 if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &lc->dev)) {
52 ti->error = "dm-linear: Device lookup failed";
53 goto bad;
54 }
55
56 ti->num_flush_bios = 1;
57 ti->num_discard_bios = 1;
58 ti->num_write_same_bios = 1;
59 ti->private = lc;
60 return 0;
61
62 bad:
63 kfree(lc);
64 return -EINVAL;
65 }
66 EXPORT_SYMBOL_GPL(dm_linear_ctr);
67
dm_linear_dtr(struct dm_target * ti)68 void dm_linear_dtr(struct dm_target *ti)
69 {
70 struct linear_c *lc = (struct linear_c *) ti->private;
71
72 dm_put_device(ti, lc->dev);
73 kfree(lc);
74 }
75 EXPORT_SYMBOL_GPL(dm_linear_dtr);
76
linear_map_sector(struct dm_target * ti,sector_t bi_sector)77 static sector_t linear_map_sector(struct dm_target *ti, sector_t bi_sector)
78 {
79 struct linear_c *lc = ti->private;
80
81 return lc->start + dm_target_offset(ti, bi_sector);
82 }
83
linear_map_bio(struct dm_target * ti,struct bio * bio)84 static void linear_map_bio(struct dm_target *ti, struct bio *bio)
85 {
86 struct linear_c *lc = ti->private;
87
88 bio->bi_bdev = lc->dev->bdev;
89 if (bio_sectors(bio))
90 bio->bi_iter.bi_sector =
91 linear_map_sector(ti, bio->bi_iter.bi_sector);
92 }
93
dm_linear_map(struct dm_target * ti,struct bio * bio)94 int dm_linear_map(struct dm_target *ti, struct bio *bio)
95 {
96 linear_map_bio(ti, bio);
97
98 return DM_MAPIO_REMAPPED;
99 }
100 EXPORT_SYMBOL_GPL(dm_linear_map);
101
dm_linear_status(struct dm_target * ti,status_type_t type,unsigned status_flags,char * result,unsigned maxlen)102 void dm_linear_status(struct dm_target *ti, status_type_t type,
103 unsigned status_flags, char *result, unsigned maxlen)
104 {
105 struct linear_c *lc = (struct linear_c *) ti->private;
106
107 switch (type) {
108 case STATUSTYPE_INFO:
109 result[0] = '\0';
110 break;
111
112 case STATUSTYPE_TABLE:
113 snprintf(result, maxlen, "%s %llu", lc->dev->name,
114 (unsigned long long)lc->start);
115 break;
116 }
117 }
118 EXPORT_SYMBOL_GPL(dm_linear_status);
119
dm_linear_ioctl(struct dm_target * ti,unsigned int cmd,unsigned long arg)120 int dm_linear_ioctl(struct dm_target *ti, unsigned int cmd,
121 unsigned long arg)
122 {
123 struct linear_c *lc = (struct linear_c *) ti->private;
124 struct dm_dev *dev = lc->dev;
125 int r = 0;
126
127 /*
128 * Only pass ioctls through if the device sizes match exactly.
129 */
130 if (lc->start ||
131 ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
132 r = scsi_verify_blk_ioctl(NULL, cmd);
133
134 return r ? : __blkdev_driver_ioctl(dev->bdev, dev->mode, cmd, arg);
135 }
136 EXPORT_SYMBOL_GPL(dm_linear_ioctl);
137
dm_linear_merge(struct dm_target * ti,struct bvec_merge_data * bvm,struct bio_vec * biovec,int max_size)138 int dm_linear_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
139 struct bio_vec *biovec, int max_size)
140 {
141 struct linear_c *lc = ti->private;
142 struct request_queue *q = bdev_get_queue(lc->dev->bdev);
143
144 if (!q->merge_bvec_fn)
145 return max_size;
146
147 bvm->bi_bdev = lc->dev->bdev;
148 bvm->bi_sector = linear_map_sector(ti, bvm->bi_sector);
149
150 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
151 }
152 EXPORT_SYMBOL_GPL(dm_linear_merge);
153
dm_linear_iterate_devices(struct dm_target * ti,iterate_devices_callout_fn fn,void * data)154 int dm_linear_iterate_devices(struct dm_target *ti,
155 iterate_devices_callout_fn fn, void *data)
156 {
157 struct linear_c *lc = ti->private;
158
159 return fn(ti, lc->dev, lc->start, ti->len, data);
160 }
161 EXPORT_SYMBOL_GPL(dm_linear_iterate_devices);
162
163 static struct target_type linear_target = {
164 .name = "linear",
165 .version = {1, 2, 1},
166 .module = THIS_MODULE,
167 .ctr = dm_linear_ctr,
168 .dtr = dm_linear_dtr,
169 .map = dm_linear_map,
170 .status = dm_linear_status,
171 .ioctl = dm_linear_ioctl,
172 .merge = dm_linear_merge,
173 .iterate_devices = dm_linear_iterate_devices,
174 };
175
dm_linear_init(void)176 int __init dm_linear_init(void)
177 {
178 int r = dm_register_target(&linear_target);
179
180 if (r < 0)
181 DMERR("register failed %d", r);
182
183 return r;
184 }
185
dm_linear_exit(void)186 void dm_linear_exit(void)
187 {
188 dm_unregister_target(&linear_target);
189 }
190