dev.c 13 KB

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