blob: 822198a75e96a599a7c9c55e14f61164bf35ba77 [file] [log] [blame]
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001/*
2 * NVDIMM Block Window Driver
3 * Copyright (c) 2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
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
Dan Williams9d90725d2016-03-18 11:27:36 -070024static u32 nsblk_meta_size(struct nd_namespace_blk *nsblk)
Vishal Vermafcae6952015-06-25 04:22:39 -040025{
Dan Williams9d90725d2016-03-18 11:27:36 -070026 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);
Vishal Vermafcae6952015-06-25 04:22:39 -040037}
38
Ross Zwisler047fc8a2015-06-25 04:21:02 -040039static 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
Dan Williams9d90725d2016-03-18 11:27:36 -070060static struct nd_blk_region *to_ndbr(struct nd_namespace_blk *nsblk)
Vishal Vermafcae6952015-06-25 04:22:39 -040061{
Dan Williams9d90725d2016-03-18 11:27:36 -070062 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);
Vishal Vermafcae6952015-06-25 04:22:39 -040076 resource_size_t dev_offset, ns_offset;
Dan Williams9d90725d2016-03-18 11:27:36 -070077 u32 internal_lbasize, sector_size;
Vishal Vermafcae6952015-06-25 04:22:39 -040078 int err = 0;
79
Dan Williams9d90725d2016-03-18 11:27:36 -070080 internal_lbasize = nsblk_internal_lbasize(nsblk);
81 sector_size = nsblk_sector_size(nsblk);
82 ns_offset = lba * internal_lbasize + sector_size;
Vishal Vermafcae6952015-06-25 04:22:39 -040083 dev_offset = to_dev_offset(nsblk, ns_offset, len);
84 if (dev_offset == SIZE_MAX)
85 return -EIO;
86
87 while (len) {
88 unsigned int cur_len;
89 struct bio_vec bv;
90 void *iobuf;
91
92 bv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
93 /*
94 * The 'bv' obtained from bvec_iter_bvec has its .bv_len and
95 * .bv_offset already adjusted for iter->bi_bvec_done, and we
96 * can use those directly
97 */
98
99 cur_len = min(len, bv.bv_len);
100 iobuf = kmap_atomic(bv.bv_page);
101 err = ndbr->do_io(ndbr, dev_offset, iobuf + bv.bv_offset,
102 cur_len, rw);
103 kunmap_atomic(iobuf);
104 if (err)
105 return err;
106
107 len -= cur_len;
108 dev_offset += cur_len;
109 bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len);
110 }
111
112 return err;
113}
114
115#else /* CONFIG_BLK_DEV_INTEGRITY */
Dan Williams9d90725d2016-03-18 11:27:36 -0700116static int nd_blk_rw_integrity(struct nd_namespace_blk *nsblk,
117 struct bio_integrity_payload *bip, u64 lba, int rw)
Vishal Vermafcae6952015-06-25 04:22:39 -0400118{
119 return 0;
120}
121#endif
122
Dan Williams9d90725d2016-03-18 11:27:36 -0700123static int nsblk_do_bvec(struct nd_namespace_blk *nsblk,
124 struct bio_integrity_payload *bip, struct page *page,
125 unsigned int len, unsigned int off, int rw, sector_t sector)
Vishal Vermafcae6952015-06-25 04:22:39 -0400126{
Dan Williams9d90725d2016-03-18 11:27:36 -0700127 struct nd_blk_region *ndbr = to_ndbr(nsblk);
Vishal Vermafcae6952015-06-25 04:22:39 -0400128 resource_size_t dev_offset, ns_offset;
Dan Williams9d90725d2016-03-18 11:27:36 -0700129 u32 internal_lbasize, sector_size;
Vishal Vermafcae6952015-06-25 04:22:39 -0400130 int err = 0;
131 void *iobuf;
132 u64 lba;
133
Dan Williams9d90725d2016-03-18 11:27:36 -0700134 internal_lbasize = nsblk_internal_lbasize(nsblk);
135 sector_size = nsblk_sector_size(nsblk);
Vishal Vermafcae6952015-06-25 04:22:39 -0400136 while (len) {
137 unsigned int cur_len;
138
139 /*
140 * If we don't have an integrity payload, we don't have to
141 * split the bvec into sectors, as this would cause unnecessary
142 * Block Window setup/move steps. the do_io routine is capable
143 * of handling len <= PAGE_SIZE.
144 */
Dan Williams9d90725d2016-03-18 11:27:36 -0700145 cur_len = bip ? min(len, sector_size) : len;
Vishal Vermafcae6952015-06-25 04:22:39 -0400146
Dan Williams9d90725d2016-03-18 11:27:36 -0700147 lba = div_u64(sector << SECTOR_SHIFT, sector_size);
148 ns_offset = lba * internal_lbasize;
149 dev_offset = to_dev_offset(nsblk, ns_offset, cur_len);
Vishal Vermafcae6952015-06-25 04:22:39 -0400150 if (dev_offset == SIZE_MAX)
151 return -EIO;
152
153 iobuf = kmap_atomic(page);
154 err = ndbr->do_io(ndbr, dev_offset, iobuf + off, cur_len, rw);
155 kunmap_atomic(iobuf);
156 if (err)
157 return err;
158
159 if (bip) {
Dan Williams9d90725d2016-03-18 11:27:36 -0700160 err = nd_blk_rw_integrity(nsblk, bip, lba, rw);
Vishal Vermafcae6952015-06-25 04:22:39 -0400161 if (err)
162 return err;
163 }
164 len -= cur_len;
165 off += cur_len;
Dan Williams9d90725d2016-03-18 11:27:36 -0700166 sector += sector_size >> SECTOR_SHIFT;
Vishal Vermafcae6952015-06-25 04:22:39 -0400167 }
168
169 return err;
170}
171
Jens Axboedece1632015-11-05 10:41:16 -0700172static blk_qc_t nd_blk_make_request(struct request_queue *q, struct bio *bio)
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400173{
Vishal Vermafcae6952015-06-25 04:22:39 -0400174 struct bio_integrity_payload *bip;
Dan Williams9d90725d2016-03-18 11:27:36 -0700175 struct nd_namespace_blk *nsblk;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400176 struct bvec_iter iter;
Dan Williamsf0dc0892015-05-16 12:28:53 -0400177 unsigned long start;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400178 struct bio_vec bvec;
179 int err = 0, rw;
Dan Williamsf0dc0892015-05-16 12:28:53 -0400180 bool do_acct;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400181
Vishal Vermafcae6952015-06-25 04:22:39 -0400182 /*
183 * bio_integrity_enabled also checks if the bio already has an
184 * integrity payload attached. If it does, we *don't* do a
185 * bio_integrity_prep here - the payload has been generated by
186 * another kernel subsystem, and we just pass it through.
187 */
188 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200189 bio->bi_error = -EIO;
Vishal Vermafcae6952015-06-25 04:22:39 -0400190 goto out;
191 }
192
193 bip = bio_integrity(bio);
Dan Williams9d90725d2016-03-18 11:27:36 -0700194 nsblk = q->queuedata;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400195 rw = bio_data_dir(bio);
Dan Williamsf0dc0892015-05-16 12:28:53 -0400196 do_acct = nd_iostat_start(bio, &start);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400197 bio_for_each_segment(bvec, bio, iter) {
198 unsigned int len = bvec.bv_len;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400199
200 BUG_ON(len > PAGE_SIZE);
Dan Williams9d90725d2016-03-18 11:27:36 -0700201 err = nsblk_do_bvec(nsblk, bip, bvec.bv_page, len,
202 bvec.bv_offset, rw, iter.bi_sector);
Vishal Vermafcae6952015-06-25 04:22:39 -0400203 if (err) {
Dan Williams9d90725d2016-03-18 11:27:36 -0700204 dev_dbg(&nsblk->common.dev,
Vishal Vermafcae6952015-06-25 04:22:39 -0400205 "io error in %s sector %lld, len %d,\n",
206 (rw == READ) ? "READ" : "WRITE",
207 (unsigned long long) iter.bi_sector, len);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200208 bio->bi_error = err;
Dan Williamsf0dc0892015-05-16 12:28:53 -0400209 break;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400210 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400211 }
Dan Williamsf0dc0892015-05-16 12:28:53 -0400212 if (do_acct)
213 nd_iostat_end(bio, start);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400214
215 out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200216 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700217 return BLK_QC_T_NONE;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400218}
219
Dan Williams9d90725d2016-03-18 11:27:36 -0700220static int nsblk_rw_bytes(struct nd_namespace_common *ndns,
Vishal Verma3ae3d672017-05-10 15:01:30 -0600221 resource_size_t offset, void *iobuf, size_t n, int rw,
222 unsigned long flags)
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400223{
Dan Williams9d90725d2016-03-18 11:27:36 -0700224 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(&ndns->dev);
225 struct nd_blk_region *ndbr = to_ndbr(nsblk);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400226 resource_size_t dev_offset;
227
228 dev_offset = to_dev_offset(nsblk, offset, n);
229
Dan Williams9d90725d2016-03-18 11:27:36 -0700230 if (unlikely(offset + n > nsblk->size)) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400231 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
232 return -EFAULT;
233 }
234
235 if (dev_offset == SIZE_MAX)
236 return -EIO;
237
238 return ndbr->do_io(ndbr, dev_offset, iobuf, n, rw);
239}
240
241static const struct block_device_operations nd_blk_fops = {
242 .owner = THIS_MODULE,
Dan Williams58138822015-06-23 20:08:34 -0400243 .revalidate_disk = nvdimm_revalidate_disk,
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400244};
245
Dan Williamsd29cee12016-03-17 20:08:28 -0700246static void nd_blk_release_queue(void *q)
247{
248 blk_cleanup_queue(q);
249}
250
251static void nd_blk_release_disk(void *disk)
252{
253 del_gendisk(disk);
254 put_disk(disk);
255}
256
Dan Williams9d90725d2016-03-18 11:27:36 -0700257static int nsblk_attach_disk(struct nd_namespace_blk *nsblk)
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400258{
Dan Williams9d90725d2016-03-18 11:27:36 -0700259 struct device *dev = &nsblk->common.dev;
Vishal Vermafcae6952015-06-25 04:22:39 -0400260 resource_size_t available_disk_size;
Dan Williamsd29cee12016-03-17 20:08:28 -0700261 struct request_queue *q;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400262 struct gendisk *disk;
Vishal Vermafcae6952015-06-25 04:22:39 -0400263 u64 internal_nlba;
264
Dan Williams9d90725d2016-03-18 11:27:36 -0700265 internal_nlba = div_u64(nsblk->size, nsblk_internal_lbasize(nsblk));
266 available_disk_size = internal_nlba * nsblk_sector_size(nsblk);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400267
Dan Williamsd29cee12016-03-17 20:08:28 -0700268 q = blk_alloc_queue(GFP_KERNEL);
269 if (!q)
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400270 return -ENOMEM;
Dan Williamsf02716d2016-06-15 14:59:17 -0700271 if (devm_add_action_or_reset(dev, nd_blk_release_queue, q))
Dan Williamsd29cee12016-03-17 20:08:28 -0700272 return -ENOMEM;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400273
Dan Williamsd29cee12016-03-17 20:08:28 -0700274 blk_queue_make_request(q, nd_blk_make_request);
275 blk_queue_max_hw_sectors(q, UINT_MAX);
276 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
Dan Williams9d90725d2016-03-18 11:27:36 -0700277 blk_queue_logical_block_size(q, nsblk_sector_size(nsblk));
Dan Williamsd29cee12016-03-17 20:08:28 -0700278 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
Dan Williams9d90725d2016-03-18 11:27:36 -0700279 q->queuedata = nsblk;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400280
Dan Williamsd29cee12016-03-17 20:08:28 -0700281 disk = alloc_disk(0);
282 if (!disk)
283 return -ENOMEM;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400284
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400285 disk->first_minor = 0;
286 disk->fops = &nd_blk_fops;
Dan Williamsd29cee12016-03-17 20:08:28 -0700287 disk->queue = q;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400288 disk->flags = GENHD_FL_EXT_DEVT;
Dan Williams9d90725d2016-03-18 11:27:36 -0700289 nvdimm_namespace_disk_name(&nsblk->common, disk->disk_name);
Vishal Vermafcae6952015-06-25 04:22:39 -0400290 set_capacity(disk, 0);
Dan Williams0d52c7562016-06-15 19:44:20 -0700291 device_add_disk(dev, disk);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400292
Dan Williamsf02716d2016-06-15 14:59:17 -0700293 if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk))
294 return -ENOMEM;
295
Dan Williams9d90725d2016-03-18 11:27:36 -0700296 if (nsblk_meta_size(nsblk)) {
297 int rc = nd_integrity_init(disk, nsblk_meta_size(nsblk));
Vishal Vermafcae6952015-06-25 04:22:39 -0400298
Dan Williamsd29cee12016-03-17 20:08:28 -0700299 if (rc)
Vishal Vermafcae6952015-06-25 04:22:39 -0400300 return rc;
Vishal Vermafcae6952015-06-25 04:22:39 -0400301 }
302
303 set_capacity(disk, available_disk_size >> SECTOR_SHIFT);
Dan Williams58138822015-06-23 20:08:34 -0400304 revalidate_disk(disk);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400305 return 0;
306}
307
308static int nd_blk_probe(struct device *dev)
309{
310 struct nd_namespace_common *ndns;
Vishal Vermafcae6952015-06-25 04:22:39 -0400311 struct nd_namespace_blk *nsblk;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400312
313 ndns = nvdimm_namespace_common_probe(dev);
314 if (IS_ERR(ndns))
315 return PTR_ERR(ndns);
316
Vishal Vermafcae6952015-06-25 04:22:39 -0400317 nsblk = to_nd_namespace_blk(&ndns->dev);
Dan Williams9d90725d2016-03-18 11:27:36 -0700318 nsblk->size = nvdimm_namespace_capacity(ndns);
319 dev_set_drvdata(dev, nsblk);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400320
Dan Williams9d90725d2016-03-18 11:27:36 -0700321 ndns->rw_bytes = nsblk_rw_bytes;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400322 if (is_nd_btt(dev))
Dan Williamsd29cee12016-03-17 20:08:28 -0700323 return nvdimm_namespace_attach_btt(ndns);
Dan Williams200c79d2016-03-22 00:22:16 -0700324 else if (nd_btt_probe(dev, ndns) == 0) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400325 /* we'll come back as btt-blk */
Dan Williamsd29cee12016-03-17 20:08:28 -0700326 return -ENXIO;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400327 } else
Dan Williams9d90725d2016-03-18 11:27:36 -0700328 return nsblk_attach_disk(nsblk);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400329}
330
331static int nd_blk_remove(struct device *dev)
332{
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400333 if (is_nd_btt(dev))
Dan Williams298f2bc2016-03-15 16:41:04 -0700334 nvdimm_namespace_detach_btt(to_nd_btt(dev));
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400335 return 0;
336}
337
338static struct nd_device_driver nd_blk_driver = {
339 .probe = nd_blk_probe,
340 .remove = nd_blk_remove,
341 .drv = {
342 .name = "nd_blk",
343 },
344 .type = ND_DRIVER_NAMESPACE_BLK,
345};
346
347static int __init nd_blk_init(void)
348{
NeilBrownec561512016-03-10 08:59:28 +1100349 return nd_driver_register(&nd_blk_driver);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400350}
351
352static void __exit nd_blk_exit(void)
353{
354 driver_unregister(&nd_blk_driver.drv);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400355}
356
357MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
358MODULE_LICENSE("GPL v2");
359MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_BLK);
360module_init(nd_blk_init);
361module_exit(nd_blk_exit);