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__BROKEN__
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 unsigned long flags)
218{
219 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(&ndns->dev);
220 struct nd_blk_region *ndbr = to_ndbr(nsblk);
221 resource_size_t dev_offset;
222
223 dev_offset = to_dev_offset(nsblk, offset, n);
224
225 if (unlikely(offset + n > nsblk->size)) {
226 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
227 return -EFAULT;
228 }
229
230 if (dev_offset == SIZE_MAX)
231 return -EIO;
232
233 return ndbr->do_io(ndbr, dev_offset, iobuf, n, rw);
234}
235
236static const struct block_device_operations nd_blk_fops = {
237 .owner = THIS_MODULE,
238 .revalidate_disk = nvdimm_revalidate_disk,
239};
240
241static void nd_blk_release_queue(void *q)
242{
243 blk_cleanup_queue(q);
244}
245
246static void nd_blk_release_disk(void *disk)
247{
248 del_gendisk(disk);
249 put_disk(disk);
250}
251
252static int nsblk_attach_disk(struct nd_namespace_blk *nsblk)
253{
254 struct device *dev = &nsblk->common.dev;
255 resource_size_t available_disk_size;
256 struct request_queue *q;
257 struct gendisk *disk;
258 u64 internal_nlba;
259
260 internal_nlba = div_u64(nsblk->size, nsblk_internal_lbasize(nsblk));
261 available_disk_size = internal_nlba * nsblk_sector_size(nsblk);
262
263 q = blk_alloc_queue(GFP_KERNEL);
264 if (!q)
265 return -ENOMEM;
266 if (devm_add_action_or_reset(dev, nd_blk_release_queue, q))
267 return -ENOMEM;
268
269 blk_queue_make_request(q, nd_blk_make_request);
270 blk_queue_max_hw_sectors(q, UINT_MAX);
271 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
272 blk_queue_logical_block_size(q, nsblk_sector_size(nsblk));
273 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
274 q->queuedata = nsblk;
275
276 disk = alloc_disk(0);
277 if (!disk)
278 return -ENOMEM;
279
280 disk->driverfs_dev = dev;
281 disk->first_minor = 0;
282 disk->fops = &nd_blk_fops;
283 disk->queue = q;
284 disk->flags = GENHD_FL_EXT_DEVT;
285 nvdimm_namespace_disk_name(&nsblk->common, disk->disk_name);
286 set_capacity(disk, available_disk_size >> SECTOR_SHIFT);
287 add_disk(disk);
288
289 if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk))
290 return -ENOMEM;
291
292 return 0;
293}
294
295static int nd_blk_probe(struct device *dev)
296{
297 struct nd_namespace_common *ndns;
298 struct nd_namespace_blk *nsblk;
299
300 ndns = nvdimm_namespace_common_probe(dev);
301 if (IS_ERR(ndns))
302 return PTR_ERR(ndns);
303
304 nsblk = to_nd_namespace_blk(&ndns->dev);
305 nsblk->size = nvdimm_namespace_capacity(ndns);
306 dev_set_drvdata(dev, nsblk);
307
308 ndns->rw_bytes = nsblk_rw_bytes;
309 if (is_nd_btt(dev))
310 return nvdimm_namespace_attach_btt(ndns);
311 else if (nd_btt_probe(dev, ndns) == 0) {
312
313 return -ENXIO;
314 } else
315 return nsblk_attach_disk(nsblk);
316}
317
318static int nd_blk_remove(struct device *dev)
319{
320 if (is_nd_btt(dev))
321 nvdimm_namespace_detach_btt(to_nd_btt(dev));
322 return 0;
323}
324
325static struct nd_device_driver nd_blk_driver = {
326 .probe = nd_blk_probe,
327 .remove = nd_blk_remove,
328 .drv = {
329 .name = "nd_blk",
330 },
331 .type = ND_DRIVER_NAMESPACE_BLK,
332};
333
334static int __init nd_blk_init(void)
335{
336 return nd_driver_register(&nd_blk_driver);
337}
338
339static void __exit nd_blk_exit(void)
340{
341 driver_unregister(&nd_blk_driver.drv);
342}
343
344MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
345MODULE_LICENSE("GPL v2");
346MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_BLK);
347module_init(nd_blk_init);
348module_exit(nd_blk_exit);
349