1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2014-2016 Christoph Hellwig.
4 */
5#include <linux/sunrpc/svc.h>
6#include <linux/blkdev.h>
7#include <linux/nfs4.h>
8#include <linux/nfs_fs.h>
9#include <linux/nfs_xdr.h>
10#include <linux/pr.h>
11
12#include "blocklayout.h"
13
14#define NFSDBG_FACILITY NFSDBG_PNFS_LD
15
16static void
17bl_free_device(struct pnfs_block_dev *dev)
18{
19 if (dev->nr_children) {
20 int i;
21
22 for (i = 0; i < dev->nr_children; i++)
23 bl_free_device(dev: &dev->children[i]);
24 kfree(objp: dev->children);
25 } else {
26 if (dev->pr_registered) {
27 const struct pr_ops *ops =
28 file_bdev(bdev_file: dev->bdev_file)->bd_disk->fops->pr_ops;
29 int error;
30
31 error = ops->pr_register(file_bdev(bdev_file: dev->bdev_file),
32 dev->pr_key, 0, false);
33 if (error)
34 pr_err("failed to unregister PR key.\n");
35 }
36
37 if (dev->bdev_file)
38 fput(dev->bdev_file);
39 }
40}
41
42void
43bl_free_deviceid_node(struct nfs4_deviceid_node *d)
44{
45 struct pnfs_block_dev *dev =
46 container_of(d, struct pnfs_block_dev, node);
47
48 bl_free_device(dev);
49 kfree_rcu(dev, node.rcu);
50}
51
52static int
53nfs4_block_decode_volume(struct xdr_stream *xdr, struct pnfs_block_volume *b)
54{
55 __be32 *p;
56 int i;
57
58 p = xdr_inline_decode(xdr, nbytes: 4);
59 if (!p)
60 return -EIO;
61 b->type = be32_to_cpup(p: p++);
62
63 switch (b->type) {
64 case PNFS_BLOCK_VOLUME_SIMPLE:
65 p = xdr_inline_decode(xdr, nbytes: 4);
66 if (!p)
67 return -EIO;
68 b->simple.nr_sigs = be32_to_cpup(p: p++);
69 if (!b->simple.nr_sigs || b->simple.nr_sigs > PNFS_BLOCK_MAX_UUIDS) {
70 dprintk("Bad signature count: %d\n", b->simple.nr_sigs);
71 return -EIO;
72 }
73
74 b->simple.len = 4 + 4;
75 for (i = 0; i < b->simple.nr_sigs; i++) {
76 p = xdr_inline_decode(xdr, nbytes: 8 + 4);
77 if (!p)
78 return -EIO;
79 p = xdr_decode_hyper(p, valp: &b->simple.sigs[i].offset);
80 b->simple.sigs[i].sig_len = be32_to_cpup(p: p++);
81 if (b->simple.sigs[i].sig_len > PNFS_BLOCK_UUID_LEN) {
82 pr_info("signature too long: %d\n",
83 b->simple.sigs[i].sig_len);
84 return -EIO;
85 }
86
87 p = xdr_inline_decode(xdr, nbytes: b->simple.sigs[i].sig_len);
88 if (!p)
89 return -EIO;
90 memcpy(&b->simple.sigs[i].sig, p,
91 b->simple.sigs[i].sig_len);
92
93 b->simple.len += 8 + 4 + \
94 (XDR_QUADLEN(b->simple.sigs[i].sig_len) << 2);
95 }
96 break;
97 case PNFS_BLOCK_VOLUME_SLICE:
98 p = xdr_inline_decode(xdr, nbytes: 8 + 8 + 4);
99 if (!p)
100 return -EIO;
101 p = xdr_decode_hyper(p, valp: &b->slice.start);
102 p = xdr_decode_hyper(p, valp: &b->slice.len);
103 b->slice.volume = be32_to_cpup(p: p++);
104 break;
105 case PNFS_BLOCK_VOLUME_CONCAT:
106 p = xdr_inline_decode(xdr, nbytes: 4);
107 if (!p)
108 return -EIO;
109
110 b->concat.volumes_count = be32_to_cpup(p: p++);
111 if (b->concat.volumes_count > PNFS_BLOCK_MAX_DEVICES) {
112 dprintk("Too many volumes: %d\n", b->concat.volumes_count);
113 return -EIO;
114 }
115
116 p = xdr_inline_decode(xdr, nbytes: b->concat.volumes_count * 4);
117 if (!p)
118 return -EIO;
119 for (i = 0; i < b->concat.volumes_count; i++)
120 b->concat.volumes[i] = be32_to_cpup(p: p++);
121 break;
122 case PNFS_BLOCK_VOLUME_STRIPE:
123 p = xdr_inline_decode(xdr, nbytes: 8 + 4);
124 if (!p)
125 return -EIO;
126
127 p = xdr_decode_hyper(p, valp: &b->stripe.chunk_size);
128 b->stripe.volumes_count = be32_to_cpup(p: p++);
129 if (b->stripe.volumes_count > PNFS_BLOCK_MAX_DEVICES) {
130 dprintk("Too many volumes: %d\n", b->stripe.volumes_count);
131 return -EIO;
132 }
133
134 p = xdr_inline_decode(xdr, nbytes: b->stripe.volumes_count * 4);
135 if (!p)
136 return -EIO;
137 for (i = 0; i < b->stripe.volumes_count; i++)
138 b->stripe.volumes[i] = be32_to_cpup(p: p++);
139 break;
140 case PNFS_BLOCK_VOLUME_SCSI:
141 p = xdr_inline_decode(xdr, nbytes: 4 + 4 + 4);
142 if (!p)
143 return -EIO;
144 b->scsi.code_set = be32_to_cpup(p: p++);
145 b->scsi.designator_type = be32_to_cpup(p: p++);
146 b->scsi.designator_len = be32_to_cpup(p: p++);
147 p = xdr_inline_decode(xdr, nbytes: b->scsi.designator_len);
148 if (!p)
149 return -EIO;
150 if (b->scsi.designator_len > 256)
151 return -EIO;
152 memcpy(&b->scsi.designator, p, b->scsi.designator_len);
153 p = xdr_inline_decode(xdr, nbytes: 8);
154 if (!p)
155 return -EIO;
156 p = xdr_decode_hyper(p, valp: &b->scsi.pr_key);
157 break;
158 default:
159 dprintk("unknown volume type!\n");
160 return -EIO;
161 }
162
163 return 0;
164}
165
166static bool bl_map_simple(struct pnfs_block_dev *dev, u64 offset,
167 struct pnfs_block_dev_map *map)
168{
169 map->start = dev->start;
170 map->len = dev->len;
171 map->disk_offset = dev->disk_offset;
172 map->bdev = file_bdev(bdev_file: dev->bdev_file);
173 return true;
174}
175
176static bool bl_map_concat(struct pnfs_block_dev *dev, u64 offset,
177 struct pnfs_block_dev_map *map)
178{
179 int i;
180
181 for (i = 0; i < dev->nr_children; i++) {
182 struct pnfs_block_dev *child = &dev->children[i];
183
184 if (child->start > offset ||
185 child->start + child->len <= offset)
186 continue;
187
188 child->map(child, offset - child->start, map);
189 return true;
190 }
191
192 dprintk("%s: ran off loop!\n", __func__);
193 return false;
194}
195
196static bool bl_map_stripe(struct pnfs_block_dev *dev, u64 offset,
197 struct pnfs_block_dev_map *map)
198{
199 struct pnfs_block_dev *child;
200 u64 chunk;
201 u32 chunk_idx;
202 u64 disk_offset;
203
204 chunk = div_u64(dividend: offset, divisor: dev->chunk_size);
205 div_u64_rem(dividend: chunk, divisor: dev->nr_children, remainder: &chunk_idx);
206
207 if (chunk_idx >= dev->nr_children) {
208 dprintk("%s: invalid chunk idx %d (%lld/%lld)\n",
209 __func__, chunk_idx, offset, dev->chunk_size);
210 /* error, should not happen */
211 return false;
212 }
213
214 /* truncate offset to the beginning of the stripe */
215 offset = chunk * dev->chunk_size;
216
217 /* disk offset of the stripe */
218 disk_offset = div_u64(dividend: offset, divisor: dev->nr_children);
219
220 child = &dev->children[chunk_idx];
221 child->map(child, disk_offset, map);
222
223 map->start += offset;
224 map->disk_offset += disk_offset;
225 map->len = dev->chunk_size;
226 return true;
227}
228
229static int
230bl_parse_deviceid(struct nfs_server *server, struct pnfs_block_dev *d,
231 struct pnfs_block_volume *volumes, int idx, gfp_t gfp_mask);
232
233
234static int
235bl_parse_simple(struct nfs_server *server, struct pnfs_block_dev *d,
236 struct pnfs_block_volume *volumes, int idx, gfp_t gfp_mask)
237{
238 struct pnfs_block_volume *v = &volumes[idx];
239 struct file *bdev_file;
240 dev_t dev;
241
242 dev = bl_resolve_deviceid(server, b: v, gfp_mask);
243 if (!dev)
244 return -EIO;
245
246 bdev_file = bdev_file_open_by_dev(dev, BLK_OPEN_READ | BLK_OPEN_WRITE,
247 NULL, NULL);
248 if (IS_ERR(ptr: bdev_file)) {
249 printk(KERN_WARNING "pNFS: failed to open device %d:%d (%ld)\n",
250 MAJOR(dev), MINOR(dev), PTR_ERR(bdev_file));
251 return PTR_ERR(ptr: bdev_file);
252 }
253 d->bdev_file = bdev_file;
254 d->len = bdev_nr_bytes(bdev: file_bdev(bdev_file));
255 d->map = bl_map_simple;
256
257 printk(KERN_INFO "pNFS: using block device %s\n",
258 file_bdev(bdev_file)->bd_disk->disk_name);
259 return 0;
260}
261
262static bool
263bl_validate_designator(struct pnfs_block_volume *v)
264{
265 switch (v->scsi.designator_type) {
266 case PS_DESIGNATOR_EUI64:
267 if (v->scsi.code_set != PS_CODE_SET_BINARY)
268 return false;
269
270 if (v->scsi.designator_len != 8 &&
271 v->scsi.designator_len != 10 &&
272 v->scsi.designator_len != 16)
273 return false;
274
275 return true;
276 case PS_DESIGNATOR_NAA:
277 if (v->scsi.code_set != PS_CODE_SET_BINARY)
278 return false;
279
280 if (v->scsi.designator_len != 8 &&
281 v->scsi.designator_len != 16)
282 return false;
283
284 return true;
285 case PS_DESIGNATOR_T10:
286 case PS_DESIGNATOR_NAME:
287 pr_err("pNFS: unsupported designator "
288 "(code set %d, type %d, len %d.\n",
289 v->scsi.code_set,
290 v->scsi.designator_type,
291 v->scsi.designator_len);
292 return false;
293 default:
294 pr_err("pNFS: invalid designator "
295 "(code set %d, type %d, len %d.\n",
296 v->scsi.code_set,
297 v->scsi.designator_type,
298 v->scsi.designator_len);
299 return false;
300 }
301}
302
303static struct file *
304bl_open_path(struct pnfs_block_volume *v, const char *prefix)
305{
306 struct file *bdev_file;
307 const char *devname;
308
309 devname = kasprintf(GFP_KERNEL, fmt: "/dev/disk/by-id/%s%*phN",
310 prefix, v->scsi.designator_len, v->scsi.designator);
311 if (!devname)
312 return ERR_PTR(error: -ENOMEM);
313
314 bdev_file = bdev_file_open_by_path(path: devname, BLK_OPEN_READ | BLK_OPEN_WRITE,
315 NULL, NULL);
316 if (IS_ERR(ptr: bdev_file)) {
317 pr_warn("pNFS: failed to open device %s (%ld)\n",
318 devname, PTR_ERR(bdev_file));
319 }
320
321 kfree(objp: devname);
322 return bdev_file;
323}
324
325static int
326bl_parse_scsi(struct nfs_server *server, struct pnfs_block_dev *d,
327 struct pnfs_block_volume *volumes, int idx, gfp_t gfp_mask)
328{
329 struct pnfs_block_volume *v = &volumes[idx];
330 struct file *bdev_file;
331 const struct pr_ops *ops;
332 int error;
333
334 if (!bl_validate_designator(v))
335 return -EINVAL;
336
337 /*
338 * Try to open the RH/Fedora specific dm-mpath udev path first, as the
339 * wwn- links will only point to the first discovered SCSI device there.
340 * On other distributions like Debian, the default SCSI by-id path will
341 * point to the dm-multipath device if one exists.
342 */
343 bdev_file = bl_open_path(v, prefix: "dm-uuid-mpath-0x");
344 if (IS_ERR(ptr: bdev_file))
345 bdev_file = bl_open_path(v, prefix: "wwn-0x");
346 if (IS_ERR(ptr: bdev_file))
347 return PTR_ERR(ptr: bdev_file);
348 d->bdev_file = bdev_file;
349
350 d->len = bdev_nr_bytes(bdev: file_bdev(bdev_file: d->bdev_file));
351 d->map = bl_map_simple;
352 d->pr_key = v->scsi.pr_key;
353
354 if (d->len == 0)
355 return -ENODEV;
356
357 pr_info("pNFS: using block device %s (reservation key 0x%llx)\n",
358 file_bdev(d->bdev_file)->bd_disk->disk_name, d->pr_key);
359
360 ops = file_bdev(bdev_file: d->bdev_file)->bd_disk->fops->pr_ops;
361 if (!ops) {
362 pr_err("pNFS: block device %s does not support reservations.",
363 file_bdev(d->bdev_file)->bd_disk->disk_name);
364 error = -EINVAL;
365 goto out_blkdev_put;
366 }
367
368 error = ops->pr_register(file_bdev(bdev_file: d->bdev_file), 0, d->pr_key, true);
369 if (error) {
370 pr_err("pNFS: failed to register key for block device %s.",
371 file_bdev(d->bdev_file)->bd_disk->disk_name);
372 goto out_blkdev_put;
373 }
374
375 d->pr_registered = true;
376 return 0;
377
378out_blkdev_put:
379 fput(d->bdev_file);
380 return error;
381}
382
383static int
384bl_parse_slice(struct nfs_server *server, struct pnfs_block_dev *d,
385 struct pnfs_block_volume *volumes, int idx, gfp_t gfp_mask)
386{
387 struct pnfs_block_volume *v = &volumes[idx];
388 int ret;
389
390 ret = bl_parse_deviceid(server, d, volumes, idx: v->slice.volume, gfp_mask);
391 if (ret)
392 return ret;
393
394 d->disk_offset = v->slice.start;
395 d->len = v->slice.len;
396 return 0;
397}
398
399static int
400bl_parse_concat(struct nfs_server *server, struct pnfs_block_dev *d,
401 struct pnfs_block_volume *volumes, int idx, gfp_t gfp_mask)
402{
403 struct pnfs_block_volume *v = &volumes[idx];
404 u64 len = 0;
405 int ret, i;
406
407 d->children = kcalloc(n: v->concat.volumes_count,
408 size: sizeof(struct pnfs_block_dev), flags: gfp_mask);
409 if (!d->children)
410 return -ENOMEM;
411
412 for (i = 0; i < v->concat.volumes_count; i++) {
413 ret = bl_parse_deviceid(server, d: &d->children[i],
414 volumes, idx: v->concat.volumes[i], gfp_mask);
415 if (ret)
416 return ret;
417
418 d->nr_children++;
419 d->children[i].start += len;
420 len += d->children[i].len;
421 }
422
423 d->len = len;
424 d->map = bl_map_concat;
425 return 0;
426}
427
428static int
429bl_parse_stripe(struct nfs_server *server, struct pnfs_block_dev *d,
430 struct pnfs_block_volume *volumes, int idx, gfp_t gfp_mask)
431{
432 struct pnfs_block_volume *v = &volumes[idx];
433 u64 len = 0;
434 int ret, i;
435
436 d->children = kcalloc(n: v->stripe.volumes_count,
437 size: sizeof(struct pnfs_block_dev), flags: gfp_mask);
438 if (!d->children)
439 return -ENOMEM;
440
441 for (i = 0; i < v->stripe.volumes_count; i++) {
442 ret = bl_parse_deviceid(server, d: &d->children[i],
443 volumes, idx: v->stripe.volumes[i], gfp_mask);
444 if (ret)
445 return ret;
446
447 d->nr_children++;
448 len += d->children[i].len;
449 }
450
451 d->len = len;
452 d->chunk_size = v->stripe.chunk_size;
453 d->map = bl_map_stripe;
454 return 0;
455}
456
457static int
458bl_parse_deviceid(struct nfs_server *server, struct pnfs_block_dev *d,
459 struct pnfs_block_volume *volumes, int idx, gfp_t gfp_mask)
460{
461 switch (volumes[idx].type) {
462 case PNFS_BLOCK_VOLUME_SIMPLE:
463 return bl_parse_simple(server, d, volumes, idx, gfp_mask);
464 case PNFS_BLOCK_VOLUME_SLICE:
465 return bl_parse_slice(server, d, volumes, idx, gfp_mask);
466 case PNFS_BLOCK_VOLUME_CONCAT:
467 return bl_parse_concat(server, d, volumes, idx, gfp_mask);
468 case PNFS_BLOCK_VOLUME_STRIPE:
469 return bl_parse_stripe(server, d, volumes, idx, gfp_mask);
470 case PNFS_BLOCK_VOLUME_SCSI:
471 return bl_parse_scsi(server, d, volumes, idx, gfp_mask);
472 default:
473 dprintk("unsupported volume type: %d\n", volumes[idx].type);
474 return -EIO;
475 }
476}
477
478struct nfs4_deviceid_node *
479bl_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev,
480 gfp_t gfp_mask)
481{
482 struct nfs4_deviceid_node *node = NULL;
483 struct pnfs_block_volume *volumes;
484 struct pnfs_block_dev *top;
485 struct xdr_stream xdr;
486 struct xdr_buf buf;
487 struct page *scratch;
488 int nr_volumes, ret, i;
489 __be32 *p;
490
491 scratch = alloc_page(gfp_mask);
492 if (!scratch)
493 goto out;
494
495 xdr_init_decode_pages(xdr: &xdr, buf: &buf, pages: pdev->pages, len: pdev->pglen);
496 xdr_set_scratch_page(xdr: &xdr, page: scratch);
497
498 p = xdr_inline_decode(xdr: &xdr, nbytes: sizeof(__be32));
499 if (!p)
500 goto out_free_scratch;
501 nr_volumes = be32_to_cpup(p: p++);
502
503 volumes = kcalloc(n: nr_volumes, size: sizeof(struct pnfs_block_volume),
504 flags: gfp_mask);
505 if (!volumes)
506 goto out_free_scratch;
507
508 for (i = 0; i < nr_volumes; i++) {
509 ret = nfs4_block_decode_volume(xdr: &xdr, b: &volumes[i]);
510 if (ret < 0)
511 goto out_free_volumes;
512 }
513
514 top = kzalloc(size: sizeof(*top), flags: gfp_mask);
515 if (!top)
516 goto out_free_volumes;
517
518 ret = bl_parse_deviceid(server, d: top, volumes, idx: nr_volumes - 1, gfp_mask);
519
520 node = &top->node;
521 nfs4_init_deviceid_node(node, server, &pdev->dev_id);
522 if (ret)
523 nfs4_mark_deviceid_unavailable(node);
524
525out_free_volumes:
526 kfree(objp: volumes);
527out_free_scratch:
528 __free_page(scratch);
529out:
530 return node;
531}
532

source code of linux/fs/nfs/blocklayout/dev.c