Blame view

drivers/md/dm-unstripe.c 4.57 KB
18a5bf270   Scott Bauer   dm: add unstriped...
1
2
3
4
5
6
7
8
9
  /*
   * Copyright (C) 2017 Intel Corporation.
   *
   * This file is released under the GPL.
   */
  
  #include "dm.h"
  
  #include <linux/module.h>
18a5bf270   Scott Bauer   dm: add unstriped...
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  
  struct unstripe_c {
  	struct dm_dev *dev;
  	sector_t physical_start;
  
  	uint32_t stripes;
  
  	uint32_t unstripe;
  	sector_t unstripe_width;
  	sector_t unstripe_offset;
  
  	uint32_t chunk_size;
  	u8 chunk_shift;
  };
  
  #define DM_MSG_PREFIX "unstriped"
  
  static void cleanup_unstripe(struct unstripe_c *uc, struct dm_target *ti)
  {
  	if (uc->dev)
  		dm_put_device(ti, uc->dev);
  	kfree(uc);
  }
  
  /*
   * Contruct an unstriped mapping.
   * <number of stripes> <chunk size> <stripe #> <dev_path> <offset>
   */
  static int unstripe_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  {
  	struct unstripe_c *uc;
cc6566198   Scott Bauer   dm unstripe: fix ...
41
  	sector_t tmp_len;
18a5bf270   Scott Bauer   dm: add unstriped...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  	unsigned long long start;
  	char dummy;
  
  	if (argc != 5) {
  		ti->error = "Invalid number of arguments";
  		return -EINVAL;
  	}
  
  	uc = kzalloc(sizeof(*uc), GFP_KERNEL);
  	if (!uc) {
  		ti->error = "Memory allocation for unstriped context failed";
  		return -ENOMEM;
  	}
  
  	if (kstrtouint(argv[0], 10, &uc->stripes) || !uc->stripes) {
  		ti->error = "Invalid stripe count";
  		goto err;
  	}
  
  	if (kstrtouint(argv[1], 10, &uc->chunk_size) || !uc->chunk_size) {
  		ti->error = "Invalid chunk_size";
  		goto err;
  	}
18a5bf270   Scott Bauer   dm: add unstriped...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  	if (kstrtouint(argv[2], 10, &uc->unstripe)) {
  		ti->error = "Invalid stripe number";
  		goto err;
  	}
  
  	if (uc->unstripe > uc->stripes && uc->stripes > 1) {
  		ti->error = "Please provide stripe between [0, # of stripes]";
  		goto err;
  	}
  
  	if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &uc->dev)) {
  		ti->error = "Couldn't get striped device";
  		goto err;
  	}
ef87bfc24   Milan Broz   dm: Check for dev...
79
  	if (sscanf(argv[4], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) {
18a5bf270   Scott Bauer   dm: add unstriped...
80
81
82
83
84
85
86
  		ti->error = "Invalid striped device offset";
  		goto err;
  	}
  	uc->physical_start = start;
  
  	uc->unstripe_offset = uc->unstripe * uc->chunk_size;
  	uc->unstripe_width = (uc->stripes - 1) * uc->chunk_size;
2ae600cd1   Heinz Mauelshagen   dm unstripe: supp...
87
  	uc->chunk_shift = is_power_of_2(uc->chunk_size) ? fls(uc->chunk_size) - 1 : 0;
18a5bf270   Scott Bauer   dm: add unstriped...
88

cc6566198   Scott Bauer   dm unstripe: fix ...
89
  	tmp_len = ti->len;
18a5bf270   Scott Bauer   dm: add unstriped...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  	if (sector_div(tmp_len, uc->chunk_size)) {
  		ti->error = "Target length not divisible by chunk size";
  		goto err;
  	}
  
  	if (dm_set_target_max_io_len(ti, uc->chunk_size)) {
  		ti->error = "Failed to set max io len";
  		goto err;
  	}
  
  	ti->private = uc;
  	return 0;
  err:
  	cleanup_unstripe(uc, ti);
  	return -EINVAL;
  }
  
  static void unstripe_dtr(struct dm_target *ti)
  {
  	struct unstripe_c *uc = ti->private;
  
  	cleanup_unstripe(uc, ti);
  }
  
  static sector_t map_to_core(struct dm_target *ti, struct bio *bio)
  {
  	struct unstripe_c *uc = ti->private;
  	sector_t sector = bio->bi_iter.bi_sector;
2ae600cd1   Heinz Mauelshagen   dm unstripe: supp...
118
  	sector_t tmp_sector = sector;
18a5bf270   Scott Bauer   dm: add unstriped...
119
120
  
  	/* Shift us up to the right "row" on the stripe */
2ae600cd1   Heinz Mauelshagen   dm unstripe: supp...
121
122
123
124
  	if (uc->chunk_shift)
  		tmp_sector >>= uc->chunk_shift;
  	else
  		sector_div(tmp_sector, uc->chunk_size);
18a5bf270   Scott Bauer   dm: add unstriped...
125

2ae600cd1   Heinz Mauelshagen   dm unstripe: supp...
126
  	sector += uc->unstripe_width * tmp_sector;
18a5bf270   Scott Bauer   dm: add unstriped...
127

2ae600cd1   Heinz Mauelshagen   dm unstripe: supp...
128
129
  	/* Account for what stripe we're operating on */
  	return sector + uc->unstripe_offset;
18a5bf270   Scott Bauer   dm: add unstriped...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  }
  
  static int unstripe_map(struct dm_target *ti, struct bio *bio)
  {
  	struct unstripe_c *uc = ti->private;
  
  	bio_set_dev(bio, uc->dev->bdev);
  	bio->bi_iter.bi_sector = map_to_core(ti, bio) + uc->physical_start;
  
  	return DM_MAPIO_REMAPPED;
  }
  
  static void unstripe_status(struct dm_target *ti, status_type_t type,
  			    unsigned int status_flags, char *result, unsigned int maxlen)
  {
  	struct unstripe_c *uc = ti->private;
  	unsigned int sz = 0;
  
  	switch (type) {
  	case STATUSTYPE_INFO:
  		break;
  
  	case STATUSTYPE_TABLE:
  		DMEMIT("%d %llu %d %s %llu",
  		       uc->stripes, (unsigned long long)uc->chunk_size, uc->unstripe,
  		       uc->dev->name, (unsigned long long)uc->physical_start);
  		break;
  	}
  }
  
  static int unstripe_iterate_devices(struct dm_target *ti,
  				    iterate_devices_callout_fn fn, void *data)
  {
  	struct unstripe_c *uc = ti->private;
  
  	return fn(ti, uc->dev, uc->physical_start, ti->len, data);
  }
  
  static void unstripe_io_hints(struct dm_target *ti,
  			       struct queue_limits *limits)
  {
  	struct unstripe_c *uc = ti->private;
  
  	limits->chunk_sectors = uc->chunk_size;
  }
  
  static struct target_type unstripe_target = {
  	.name = "unstriped",
2ae600cd1   Heinz Mauelshagen   dm unstripe: supp...
178
  	.version = {1, 1, 0},
18a5bf270   Scott Bauer   dm: add unstriped...
179
180
181
182
183
184
185
186
187
188
189
  	.module = THIS_MODULE,
  	.ctr = unstripe_ctr,
  	.dtr = unstripe_dtr,
  	.map = unstripe_map,
  	.status = unstripe_status,
  	.iterate_devices = unstripe_iterate_devices,
  	.io_hints = unstripe_io_hints,
  };
  
  static int __init dm_unstripe_init(void)
  {
91e065d8f   Heinz Mauelshagen   dm unstripe: remo...
190
  	return dm_register_target(&unstripe_target);
18a5bf270   Scott Bauer   dm: add unstriped...
191
192
193
194
195
196
197
198
199
200
201
  }
  
  static void __exit dm_unstripe_exit(void)
  {
  	dm_unregister_target(&unstripe_target);
  }
  
  module_init(dm_unstripe_init);
  module_exit(dm_unstripe_exit);
  
  MODULE_DESCRIPTION(DM_NAME " unstriped target");
ba5dfbb71   Heinz Mauelshagen   dm unstripe: add ...
202
  MODULE_ALIAS("dm-unstriped");
18a5bf270   Scott Bauer   dm: add unstriped...
203
204
  MODULE_AUTHOR("Scott Bauer <scott.bauer@intel.com>");
  MODULE_LICENSE("GPL");