xref: /openbmc/linux/fs/nfs/blocklayout/dev.c (revision 08b45fcb)
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 
16 static void
bl_free_device(struct pnfs_block_dev * dev)17 bl_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->children[i]);
24 		kfree(dev->children);
25 	} else {
26 		if (dev->pr_registered) {
27 			const struct pr_ops *ops =
28 				dev->bdev->bd_disk->fops->pr_ops;
29 			int error;
30 
31 			error = ops->pr_register(dev->bdev, dev->pr_key, 0,
32 				false);
33 			if (error)
34 				pr_err("failed to unregister PR key.\n");
35 		}
36 
37 		if (dev->bdev)
38 			blkdev_put(dev->bdev, NULL);
39 	}
40 }
41 
42 void
bl_free_deviceid_node(struct nfs4_deviceid_node * d)43 bl_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 
52 static int
nfs4_block_decode_volume(struct xdr_stream * xdr,struct pnfs_block_volume * b)53 nfs4_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, 4);
59 	if (!p)
60 		return -EIO;
61 	b->type = be32_to_cpup(p++);
62 
63 	switch (b->type) {
64 	case PNFS_BLOCK_VOLUME_SIMPLE:
65 		p = xdr_inline_decode(xdr, 4);
66 		if (!p)
67 			return -EIO;
68 		b->simple.nr_sigs = be32_to_cpup(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, 8 + 4);
77 			if (!p)
78 				return -EIO;
79 			p = xdr_decode_hyper(p, &b->simple.sigs[i].offset);
80 			b->simple.sigs[i].sig_len = be32_to_cpup(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, 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, 8 + 8 + 4);
99 		if (!p)
100 			return -EIO;
101 		p = xdr_decode_hyper(p, &b->slice.start);
102 		p = xdr_decode_hyper(p, &b->slice.len);
103 		b->slice.volume = be32_to_cpup(p++);
104 		break;
105 	case PNFS_BLOCK_VOLUME_CONCAT:
106 		p = xdr_inline_decode(xdr, 4);
107 		if (!p)
108 			return -EIO;
109 
110 		b->concat.volumes_count = be32_to_cpup(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, 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++);
121 		break;
122 	case PNFS_BLOCK_VOLUME_STRIPE:
123 		p = xdr_inline_decode(xdr, 8 + 4);
124 		if (!p)
125 			return -EIO;
126 
127 		p = xdr_decode_hyper(p, &b->stripe.chunk_size);
128 		b->stripe.volumes_count = be32_to_cpup(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, 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++);
139 		break;
140 	case PNFS_BLOCK_VOLUME_SCSI:
141 		p = xdr_inline_decode(xdr, 4 + 4 + 4);
142 		if (!p)
143 			return -EIO;
144 		b->scsi.code_set = be32_to_cpup(p++);
145 		b->scsi.designator_type = be32_to_cpup(p++);
146 		b->scsi.designator_len = be32_to_cpup(p++);
147 		p = xdr_inline_decode(xdr, 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, 8);
154 		if (!p)
155 			return -EIO;
156 		p = xdr_decode_hyper(p, &b->scsi.pr_key);
157 		break;
158 	default:
159 		dprintk("unknown volume type!\n");
160 		return -EIO;
161 	}
162 
163 	return 0;
164 }
165 
bl_map_simple(struct pnfs_block_dev * dev,u64 offset,struct pnfs_block_dev_map * map)166 static 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 = dev->bdev;
173 	return true;
174 }
175 
bl_map_concat(struct pnfs_block_dev * dev,u64 offset,struct pnfs_block_dev_map * map)176 static 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 
bl_map_stripe(struct pnfs_block_dev * dev,u64 offset,struct pnfs_block_dev_map * map)196 static 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(offset, dev->chunk_size);
205 	div_u64_rem(chunk, dev->nr_children, &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(offset, 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 
229 static int
230 bl_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 
234 static int
bl_parse_simple(struct nfs_server * server,struct pnfs_block_dev * d,struct pnfs_block_volume * volumes,int idx,gfp_t gfp_mask)235 bl_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 block_device *bdev;
240 	dev_t dev;
241 
242 	dev = bl_resolve_deviceid(server, v, gfp_mask);
243 	if (!dev)
244 		return -EIO;
245 
246 	bdev = blkdev_get_by_dev(dev, BLK_OPEN_READ | BLK_OPEN_WRITE, NULL,
247 				 NULL);
248 	if (IS_ERR(bdev)) {
249 		printk(KERN_WARNING "pNFS: failed to open device %d:%d (%ld)\n",
250 			MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
251 		return PTR_ERR(bdev);
252 	}
253 	d->bdev = bdev;
254 
255 
256 	d->len = bdev_nr_bytes(d->bdev);
257 	d->map = bl_map_simple;
258 
259 	printk(KERN_INFO "pNFS: using block device %s\n",
260 		d->bdev->bd_disk->disk_name);
261 	return 0;
262 }
263 
264 static bool
bl_validate_designator(struct pnfs_block_volume * v)265 bl_validate_designator(struct pnfs_block_volume *v)
266 {
267 	switch (v->scsi.designator_type) {
268 	case PS_DESIGNATOR_EUI64:
269 		if (v->scsi.code_set != PS_CODE_SET_BINARY)
270 			return false;
271 
272 		if (v->scsi.designator_len != 8 &&
273 		    v->scsi.designator_len != 10 &&
274 		    v->scsi.designator_len != 16)
275 			return false;
276 
277 		return true;
278 	case PS_DESIGNATOR_NAA:
279 		if (v->scsi.code_set != PS_CODE_SET_BINARY)
280 			return false;
281 
282 		if (v->scsi.designator_len != 8 &&
283 		    v->scsi.designator_len != 16)
284 			return false;
285 
286 		return true;
287 	case PS_DESIGNATOR_T10:
288 	case PS_DESIGNATOR_NAME:
289 		pr_err("pNFS: unsupported designator "
290 			"(code set %d, type %d, len %d.\n",
291 			v->scsi.code_set,
292 			v->scsi.designator_type,
293 			v->scsi.designator_len);
294 		return false;
295 	default:
296 		pr_err("pNFS: invalid designator "
297 			"(code set %d, type %d, len %d.\n",
298 			v->scsi.code_set,
299 			v->scsi.designator_type,
300 			v->scsi.designator_len);
301 		return false;
302 	}
303 }
304 
305 static struct block_device *
bl_open_path(struct pnfs_block_volume * v,const char * prefix)306 bl_open_path(struct pnfs_block_volume *v, const char *prefix)
307 {
308 	struct block_device *bdev;
309 	const char *devname;
310 
311 	devname = kasprintf(GFP_KERNEL, "/dev/disk/by-id/%s%*phN",
312 			prefix, v->scsi.designator_len, v->scsi.designator);
313 	if (!devname)
314 		return ERR_PTR(-ENOMEM);
315 
316 	bdev = blkdev_get_by_path(devname, BLK_OPEN_READ | BLK_OPEN_WRITE, NULL,
317 				  NULL);
318 	if (IS_ERR(bdev)) {
319 		pr_warn("pNFS: failed to open device %s (%ld)\n",
320 			devname, PTR_ERR(bdev));
321 	}
322 
323 	kfree(devname);
324 	return bdev;
325 }
326 
327 static int
bl_parse_scsi(struct nfs_server * server,struct pnfs_block_dev * d,struct pnfs_block_volume * volumes,int idx,gfp_t gfp_mask)328 bl_parse_scsi(struct nfs_server *server, struct pnfs_block_dev *d,
329 		struct pnfs_block_volume *volumes, int idx, gfp_t gfp_mask)
330 {
331 	struct pnfs_block_volume *v = &volumes[idx];
332 	struct block_device *bdev;
333 	const struct pr_ops *ops;
334 	int error;
335 
336 	if (!bl_validate_designator(v))
337 		return -EINVAL;
338 
339 	/*
340 	 * Try to open the RH/Fedora specific dm-mpath udev path first, as the
341 	 * wwn- links will only point to the first discovered SCSI device there.
342 	 * On other distributions like Debian, the default SCSI by-id path will
343 	 * point to the dm-multipath device if one exists.
344 	 */
345 	bdev = bl_open_path(v, "dm-uuid-mpath-0x");
346 	if (IS_ERR(bdev))
347 		bdev = bl_open_path(v, "wwn-0x");
348 	if (IS_ERR(bdev))
349 		return PTR_ERR(bdev);
350 	d->bdev = bdev;
351 
352 	d->len = bdev_nr_bytes(d->bdev);
353 	d->map = bl_map_simple;
354 	d->pr_key = v->scsi.pr_key;
355 
356 	pr_info("pNFS: using block device %s (reservation key 0x%llx)\n",
357 		d->bdev->bd_disk->disk_name, d->pr_key);
358 
359 	ops = d->bdev->bd_disk->fops->pr_ops;
360 	if (!ops) {
361 		pr_err("pNFS: block device %s does not support reservations.",
362 				d->bdev->bd_disk->disk_name);
363 		error = -EINVAL;
364 		goto out_blkdev_put;
365 	}
366 
367 	error = ops->pr_register(d->bdev, 0, d->pr_key, true);
368 	if (error) {
369 		pr_err("pNFS: failed to register key for block device %s.",
370 				d->bdev->bd_disk->disk_name);
371 		goto out_blkdev_put;
372 	}
373 
374 	d->pr_registered = true;
375 	return 0;
376 
377 out_blkdev_put:
378 	blkdev_put(d->bdev, NULL);
379 	return error;
380 }
381 
382 static int
bl_parse_slice(struct nfs_server * server,struct pnfs_block_dev * d,struct pnfs_block_volume * volumes,int idx,gfp_t gfp_mask)383 bl_parse_slice(struct nfs_server *server, struct pnfs_block_dev *d,
384 		struct pnfs_block_volume *volumes, int idx, gfp_t gfp_mask)
385 {
386 	struct pnfs_block_volume *v = &volumes[idx];
387 	int ret;
388 
389 	ret = bl_parse_deviceid(server, d, volumes, v->slice.volume, gfp_mask);
390 	if (ret)
391 		return ret;
392 
393 	d->disk_offset = v->slice.start;
394 	d->len = v->slice.len;
395 	return 0;
396 }
397 
398 static int
bl_parse_concat(struct nfs_server * server,struct pnfs_block_dev * d,struct pnfs_block_volume * volumes,int idx,gfp_t gfp_mask)399 bl_parse_concat(struct nfs_server *server, struct pnfs_block_dev *d,
400 		struct pnfs_block_volume *volumes, int idx, gfp_t gfp_mask)
401 {
402 	struct pnfs_block_volume *v = &volumes[idx];
403 	u64 len = 0;
404 	int ret, i;
405 
406 	d->children = kcalloc(v->concat.volumes_count,
407 			sizeof(struct pnfs_block_dev), gfp_mask);
408 	if (!d->children)
409 		return -ENOMEM;
410 
411 	for (i = 0; i < v->concat.volumes_count; i++) {
412 		ret = bl_parse_deviceid(server, &d->children[i],
413 				volumes, v->concat.volumes[i], gfp_mask);
414 		if (ret)
415 			return ret;
416 
417 		d->nr_children++;
418 		d->children[i].start += len;
419 		len += d->children[i].len;
420 	}
421 
422 	d->len = len;
423 	d->map = bl_map_concat;
424 	return 0;
425 }
426 
427 static int
bl_parse_stripe(struct nfs_server * server,struct pnfs_block_dev * d,struct pnfs_block_volume * volumes,int idx,gfp_t gfp_mask)428 bl_parse_stripe(struct nfs_server *server, struct pnfs_block_dev *d,
429 		struct pnfs_block_volume *volumes, int idx, gfp_t gfp_mask)
430 {
431 	struct pnfs_block_volume *v = &volumes[idx];
432 	u64 len = 0;
433 	int ret, i;
434 
435 	d->children = kcalloc(v->stripe.volumes_count,
436 			sizeof(struct pnfs_block_dev), gfp_mask);
437 	if (!d->children)
438 		return -ENOMEM;
439 
440 	for (i = 0; i < v->stripe.volumes_count; i++) {
441 		ret = bl_parse_deviceid(server, &d->children[i],
442 				volumes, v->stripe.volumes[i], gfp_mask);
443 		if (ret)
444 			return ret;
445 
446 		d->nr_children++;
447 		len += d->children[i].len;
448 	}
449 
450 	d->len = len;
451 	d->chunk_size = v->stripe.chunk_size;
452 	d->map = bl_map_stripe;
453 	return 0;
454 }
455 
456 static int
bl_parse_deviceid(struct nfs_server * server,struct pnfs_block_dev * d,struct pnfs_block_volume * volumes,int idx,gfp_t gfp_mask)457 bl_parse_deviceid(struct nfs_server *server, struct pnfs_block_dev *d,
458 		struct pnfs_block_volume *volumes, int idx, gfp_t gfp_mask)
459 {
460 	switch (volumes[idx].type) {
461 	case PNFS_BLOCK_VOLUME_SIMPLE:
462 		return bl_parse_simple(server, d, volumes, idx, gfp_mask);
463 	case PNFS_BLOCK_VOLUME_SLICE:
464 		return bl_parse_slice(server, d, volumes, idx, gfp_mask);
465 	case PNFS_BLOCK_VOLUME_CONCAT:
466 		return bl_parse_concat(server, d, volumes, idx, gfp_mask);
467 	case PNFS_BLOCK_VOLUME_STRIPE:
468 		return bl_parse_stripe(server, d, volumes, idx, gfp_mask);
469 	case PNFS_BLOCK_VOLUME_SCSI:
470 		return bl_parse_scsi(server, d, volumes, idx, gfp_mask);
471 	default:
472 		dprintk("unsupported volume type: %d\n", volumes[idx].type);
473 		return -EIO;
474 	}
475 }
476 
477 struct nfs4_deviceid_node *
bl_alloc_deviceid_node(struct nfs_server * server,struct pnfs_device * pdev,gfp_t gfp_mask)478 bl_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev,
479 		gfp_t gfp_mask)
480 {
481 	struct nfs4_deviceid_node *node = NULL;
482 	struct pnfs_block_volume *volumes;
483 	struct pnfs_block_dev *top;
484 	struct xdr_stream xdr;
485 	struct xdr_buf buf;
486 	struct page *scratch;
487 	int nr_volumes, ret, i;
488 	__be32 *p;
489 
490 	scratch = alloc_page(gfp_mask);
491 	if (!scratch)
492 		goto out;
493 
494 	xdr_init_decode_pages(&xdr, &buf, pdev->pages, pdev->pglen);
495 	xdr_set_scratch_page(&xdr, scratch);
496 
497 	p = xdr_inline_decode(&xdr, sizeof(__be32));
498 	if (!p)
499 		goto out_free_scratch;
500 	nr_volumes = be32_to_cpup(p++);
501 
502 	volumes = kcalloc(nr_volumes, sizeof(struct pnfs_block_volume),
503 			  gfp_mask);
504 	if (!volumes)
505 		goto out_free_scratch;
506 
507 	for (i = 0; i < nr_volumes; i++) {
508 		ret = nfs4_block_decode_volume(&xdr, &volumes[i]);
509 		if (ret < 0)
510 			goto out_free_volumes;
511 	}
512 
513 	top = kzalloc(sizeof(*top), gfp_mask);
514 	if (!top)
515 		goto out_free_volumes;
516 
517 	ret = bl_parse_deviceid(server, top, volumes, nr_volumes - 1, gfp_mask);
518 
519 	node = &top->node;
520 	nfs4_init_deviceid_node(node, server, &pdev->dev_id);
521 	if (ret)
522 		nfs4_mark_deviceid_unavailable(node);
523 
524 out_free_volumes:
525 	kfree(volumes);
526 out_free_scratch:
527 	__free_page(scratch);
528 out:
529 	return node;
530 }
531