1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/blkdev.h>
16#include <linux/fs.h>
17#include <linux/genhd.h>
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20#include <linux/nd.h>
21#include <linux/sizes.h>
22#include "nd.h"
23
24static u32 nsblk_meta_size(struct nd_namespace_blk *nsblk)
25{
26 return nsblk->lbasize - ((nsblk->lbasize >= 4096) ? 4096 : 512);
27}
28
29static u32 nsblk_internal_lbasize(struct nd_namespace_blk *nsblk)
30{
31 return roundup(nsblk->lbasize, INT_LBASIZE_ALIGNMENT);
32}
33
34static u32 nsblk_sector_size(struct nd_namespace_blk *nsblk)
35{
36 return nsblk->lbasize - nsblk_meta_size(nsblk);
37}
38
39static resource_size_t to_dev_offset(struct nd_namespace_blk *nsblk,
40 resource_size_t ns_offset, unsigned int len)
41{
42 int i;
43
44 for (i = 0; i < nsblk->num_resources; i++) {
45 if (ns_offset < resource_size(nsblk->res[i])) {
46 if (ns_offset + len > resource_size(nsblk->res[i])) {
47 dev_WARN_ONCE(&nsblk->common.dev, 1,
48 "illegal request\n");
49 return SIZE_MAX;
50 }
51 return nsblk->res[i]->start + ns_offset;
52 }
53 ns_offset -= resource_size(nsblk->res[i]);
54 }
55
56 dev_WARN_ONCE(&nsblk->common.dev, 1, "request out of range\n");
57 return SIZE_MAX;
58}
59
60static struct nd_blk_region *to_ndbr(struct nd_namespace_blk *nsblk)
61{
62 struct nd_region *nd_region;
63 struct device *parent;
64
65 parent = nsblk->common.dev.parent;
66 nd_region = container_of(parent, struct nd_region, dev);
67 return container_of(nd_region, struct nd_blk_region, nd_region);
68}
69
70#ifdef CONFIG_BLK_DEV_INTEGRITY
71static int nd_blk_rw_integrity(struct nd_namespace_blk *nsblk,
72 struct bio_integrity_payload *bip, u64 lba, int rw)
73{
74 struct nd_blk_region *ndbr = to_ndbr(nsblk);
75 unsigned int len = nsblk_meta_size(nsblk);
76 resource_size_t dev_offset, ns_offset;
77 u32 internal_lbasize, sector_size;
78 struct bio_vec *bv;
79 int err = 0, i;
80
81 internal_lbasize = nsblk_internal_lbasize(nsblk);
82 sector_size = nsblk_sector_size(nsblk);
83 ns_offset = lba * internal_lbasize + sector_size;
84 dev_offset = to_dev_offset(nsblk, ns_offset, len);
85 if (dev_offset == SIZE_MAX)
86 return -EIO;
87
88 bip_for_each_vec(bv, bip, i) {
89 unsigned int cur_len;
90 void *iobuf;
91
92 if (!len)
93 break;
94
95 cur_len = min(len, bv->bv_len);
96 iobuf = kmap_atomic(bv->bv_page);
97 err = ndbr->do_io(ndbr, dev_offset, iobuf + bv->bv_offset,
98 cur_len, rw);
99 kunmap_atomic(iobuf);
100 if (err)
101 return err;
102
103 len -= cur_len;
104 dev_offset += cur_len;
105 }
106 BUG_ON(len);
107
108 return err;
109}
110
111#else
112static int nd_blk_rw_integrity(struct nd_namespace_blk *nsblk,
113 struct bio_integrity_payload *bip, u64 lba, int rw)
114{
115 return 0;
116}
117#endif
118
119static int nsblk_do_bvec(struct nd_namespace_blk *nsblk,
120 struct bio_integrity_payload *bip, struct page *page,
121 unsigned int len, unsigned int off, int rw, sector_t sector)
122{
123 struct nd_blk_region *ndbr = to_ndbr(nsblk);
124 resource_size_t dev_offset, ns_offset;
125 u32 internal_lbasize, sector_size;
126 int err = 0;
127 void *iobuf;
128 u64 lba;
129
130 internal_lbasize = nsblk_internal_lbasize(nsblk);
131 sector_size = nsblk_sector_size(nsblk);
132 while (len) {
133 unsigned int cur_len;
134
135
136
137
138
139
140
141 cur_len = bip ? min(len, sector_size) : len;
142
143 lba = div_u64(sector << SECTOR_SHIFT, sector_size);
144 ns_offset = lba * internal_lbasize;
145 dev_offset = to_dev_offset(nsblk, ns_offset, cur_len);
146 if (dev_offset == SIZE_MAX)
147 return -EIO;
148
149 iobuf = kmap_atomic(page);
150 err = ndbr->do_io(ndbr, dev_offset, iobuf + off, cur_len, rw);
151 kunmap_atomic(iobuf);
152 if (err)
153 return err;
154
155 if (bip) {
156 err = nd_blk_rw_integrity(nsblk, bip, lba, rw);
157 if (err)
158 return err;
159 }
160 len -= cur_len;
161 off += cur_len;
162 sector += sector_size >> SECTOR_SHIFT;
163 }
164
165 return err;
166}
167
168static void nd_blk_make_request(struct request_queue *q, struct bio *bio)
169{
170 struct bio_integrity_payload *bip;
171 struct nd_namespace_blk *nsblk;
172 struct bio_vec *bvec;
173 unsigned long start;
174 sector_t sector = bio->bi_sector;
175 int err = 0, rw, i;
176 bool do_acct;
177
178
179
180
181
182
183
184 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
185 err = -EIO;
186 goto out;
187 }
188
189 bip = bio->bi_integrity;
190 nsblk = q->queuedata;
191 rw = bio_data_dir(bio);
192 do_acct = nd_iostat_start(bio, &start);
193 bio_for_each_segment(bvec, bio, i) {
194 unsigned int len = bvec->bv_len;
195
196 BUG_ON(len > PAGE_SIZE);
197 err = nsblk_do_bvec(nsblk, bip, bvec->bv_page, len,
198 bvec->bv_offset, rw, sector);
199 if (err) {
200 dev_dbg(&nsblk->common.dev,
201 "io error in %s sector %lld, len %d,\n",
202 (rw == READ) ? "READ" : "WRITE",
203 (unsigned long long) sector, len);
204 break;
205 }
206 sector += len >> SECTOR_SHIFT;
207 }
208 if (do_acct)
209 nd_iostat_end(bio, start);
210
211 out:
212 bio_endio(bio, err);
213}
214
215static int nsblk_rw_bytes(struct nd_namespace_common *ndns,
216 resource_size_t offset, void *iobuf, size_t n, int rw)
217{
218 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(&ndns->dev);
219 struct nd_blk_region *ndbr = to_ndbr(nsblk);
220 resource_size_t dev_offset;
221
222 dev_offset = to_dev_offset(nsblk, offset, n);
223
224 if (unlikely(offset + n > nsblk->size)) {
225 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
226 return -EFAULT;
227 }
228
229 if (dev_offset == SIZE_MAX)
230 return -EIO;
231
232 return ndbr->do_io(ndbr, dev_offset, iobuf, n, rw);
233}
234
235static const struct block_device_operations nd_blk_fops = {
236 .owner = THIS_MODULE,
237 .revalidate_disk = nvdimm_revalidate_disk,
238};
239
240static void nd_blk_release_queue(void *q)
241{
242 blk_cleanup_queue(q);
243}
244
245static void nd_blk_release_disk(void *disk)
246{
247 del_gendisk(disk);
248 put_disk(disk);
249}
250
251static int nsblk_attach_disk(struct nd_namespace_blk *nsblk)
252{
253 struct device *dev = &nsblk->common.dev;
254 resource_size_t available_disk_size;
255 struct request_queue *q;
256 struct gendisk *disk;
257 u64 internal_nlba;
258
259 internal_nlba = div_u64(nsblk->size, nsblk_internal_lbasize(nsblk));
260 available_disk_size = internal_nlba * nsblk_sector_size(nsblk);
261
262 q = blk_alloc_queue(GFP_KERNEL);
263 if (!q)
264 return -ENOMEM;
265 if (devm_add_action(dev, nd_blk_release_queue, q)) {
266 blk_cleanup_queue(q);
267 return -ENOMEM;
268 }
269
270 blk_queue_make_request(q, nd_blk_make_request);
271 blk_queue_max_hw_sectors(q, UINT_MAX);
272 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
273 blk_queue_logical_block_size(q, nsblk_sector_size(nsblk));
274 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
275 q->queuedata = nsblk;
276
277 disk = alloc_disk(0);
278 if (!disk)
279 return -ENOMEM;
280 if (devm_add_action(dev, nd_blk_release_disk, disk)) {
281 put_disk(disk);
282 return -ENOMEM;
283 }
284
285 disk->driverfs_dev = dev;
286 disk->first_minor = 0;
287 disk->fops = &nd_blk_fops;
288 disk->queue = q;
289 disk->flags = GENHD_FL_EXT_DEVT;
290 nvdimm_namespace_disk_name(&nsblk->common, disk->disk_name);
291 set_capacity(disk, 0);
292 add_disk(disk);
293
294 if (nsblk_meta_size(nsblk)) {
295 int rc = nd_integrity_init(disk, nsblk_meta_size(nsblk));
296
297 if (rc)
298 return rc;
299 }
300
301 set_capacity(disk, available_disk_size >> SECTOR_SHIFT);
302 revalidate_disk(disk);
303 return 0;
304}
305
306static int nd_blk_probe(struct device *dev)
307{
308 struct nd_namespace_common *ndns;
309 struct nd_namespace_blk *nsblk;
310
311 ndns = nvdimm_namespace_common_probe(dev);
312 if (IS_ERR(ndns))
313 return PTR_ERR(ndns);
314
315 nsblk = to_nd_namespace_blk(&ndns->dev);
316 nsblk->size = nvdimm_namespace_capacity(ndns);
317 dev_set_drvdata(dev, nsblk);
318
319 ndns->rw_bytes = nsblk_rw_bytes;
320 if (is_nd_btt(dev))
321 return nvdimm_namespace_attach_btt(ndns);
322 else if (nd_btt_probe(dev, ndns) == 0) {
323
324 return -ENXIO;
325 } else
326 return nsblk_attach_disk(nsblk);
327}
328
329static int nd_blk_remove(struct device *dev)
330{
331 if (is_nd_btt(dev))
332 nvdimm_namespace_detach_btt(to_nd_btt(dev));
333 return 0;
334}
335
336static struct nd_device_driver nd_blk_driver = {
337 .probe = nd_blk_probe,
338 .remove = nd_blk_remove,
339 .drv = {
340 .name = "nd_blk",
341 },
342 .type = ND_DRIVER_NAMESPACE_BLK,
343};
344
345static int __init nd_blk_init(void)
346{
347 return nd_driver_register(&nd_blk_driver);
348}
349
350static void __exit nd_blk_exit(void)
351{
352 driver_unregister(&nd_blk_driver.drv);
353}
354
355MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
356MODULE_LICENSE("GPL v2");
357MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_BLK);
358module_init(nd_blk_init);
359module_exit(nd_blk_exit);
360