blocklayout.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2014-2016 Christoph Hellwig.
  4. */
  5. #include <linux/exportfs.h>
  6. #include <linux/iomap.h>
  7. #include <linux/slab.h>
  8. #include <linux/pr.h>
  9. #include <linux/nfsd/debug.h>
  10. #include "blocklayoutxdr.h"
  11. #include "pnfs.h"
  12. #include "filecache.h"
  13. #include "vfs.h"
  14. #define NFSDDBG_FACILITY NFSDDBG_PNFS
  15. static __be32
  16. nfsd4_block_proc_layoutget(struct inode *inode, const struct svc_fh *fhp,
  17. struct nfsd4_layoutget *args)
  18. {
  19. struct nfsd4_layout_seg *seg = &args->lg_seg;
  20. struct super_block *sb = inode->i_sb;
  21. u32 block_size = i_blocksize(inode);
  22. struct pnfs_block_extent *bex;
  23. struct iomap iomap;
  24. u32 device_generation = 0;
  25. int error;
  26. if (seg->offset & (block_size - 1)) {
  27. dprintk("pnfsd: I/O misaligned\n");
  28. goto out_layoutunavailable;
  29. }
  30. /*
  31. * Some clients barf on non-zero block numbers for NONE or INVALID
  32. * layouts, so make sure to zero the whole structure.
  33. */
  34. error = -ENOMEM;
  35. bex = kzalloc(sizeof(*bex), GFP_KERNEL);
  36. if (!bex)
  37. goto out_error;
  38. args->lg_content = bex;
  39. error = sb->s_export_op->map_blocks(inode, seg->offset, seg->length,
  40. &iomap, seg->iomode != IOMODE_READ,
  41. &device_generation);
  42. if (error) {
  43. if (error == -ENXIO)
  44. goto out_layoutunavailable;
  45. goto out_error;
  46. }
  47. if (iomap.length < args->lg_minlength) {
  48. dprintk("pnfsd: extent smaller than minlength\n");
  49. goto out_layoutunavailable;
  50. }
  51. switch (iomap.type) {
  52. case IOMAP_MAPPED:
  53. if (seg->iomode == IOMODE_READ)
  54. bex->es = PNFS_BLOCK_READ_DATA;
  55. else
  56. bex->es = PNFS_BLOCK_READWRITE_DATA;
  57. bex->soff = iomap.addr;
  58. break;
  59. case IOMAP_UNWRITTEN:
  60. if (seg->iomode & IOMODE_RW) {
  61. /*
  62. * Crack monkey special case from section 2.3.1.
  63. */
  64. if (args->lg_minlength == 0) {
  65. dprintk("pnfsd: no soup for you!\n");
  66. goto out_layoutunavailable;
  67. }
  68. bex->es = PNFS_BLOCK_INVALID_DATA;
  69. bex->soff = iomap.addr;
  70. break;
  71. }
  72. fallthrough;
  73. case IOMAP_HOLE:
  74. if (seg->iomode == IOMODE_READ) {
  75. bex->es = PNFS_BLOCK_NONE_DATA;
  76. break;
  77. }
  78. fallthrough;
  79. case IOMAP_DELALLOC:
  80. default:
  81. WARN(1, "pnfsd: filesystem returned %d extent\n", iomap.type);
  82. goto out_layoutunavailable;
  83. }
  84. error = nfsd4_set_deviceid(&bex->vol_id, fhp, device_generation);
  85. if (error)
  86. goto out_error;
  87. bex->foff = iomap.offset;
  88. bex->len = iomap.length;
  89. seg->offset = iomap.offset;
  90. seg->length = iomap.length;
  91. dprintk("GET: 0x%llx:0x%llx %d\n", bex->foff, bex->len, bex->es);
  92. return 0;
  93. out_error:
  94. seg->length = 0;
  95. return nfserrno(error);
  96. out_layoutunavailable:
  97. seg->length = 0;
  98. return nfserr_layoutunavailable;
  99. }
  100. static __be32
  101. nfsd4_block_commit_blocks(struct inode *inode, struct nfsd4_layoutcommit *lcp,
  102. struct iomap *iomaps, int nr_iomaps)
  103. {
  104. struct timespec64 mtime = inode_get_mtime(inode);
  105. struct iattr iattr = { .ia_valid = 0 };
  106. int error;
  107. if (lcp->lc_mtime.tv_nsec == UTIME_NOW ||
  108. timespec64_compare(&lcp->lc_mtime, &mtime) < 0)
  109. lcp->lc_mtime = current_time(inode);
  110. iattr.ia_valid |= ATTR_ATIME | ATTR_CTIME | ATTR_MTIME;
  111. iattr.ia_atime = iattr.ia_ctime = iattr.ia_mtime = lcp->lc_mtime;
  112. if (lcp->lc_size_chg) {
  113. iattr.ia_valid |= ATTR_SIZE;
  114. iattr.ia_size = lcp->lc_newsize;
  115. }
  116. error = inode->i_sb->s_export_op->commit_blocks(inode, iomaps,
  117. nr_iomaps, &iattr);
  118. kfree(iomaps);
  119. return nfserrno(error);
  120. }
  121. #ifdef CONFIG_NFSD_BLOCKLAYOUT
  122. static int
  123. nfsd4_block_get_device_info_simple(struct super_block *sb,
  124. struct nfsd4_getdeviceinfo *gdp)
  125. {
  126. struct pnfs_block_deviceaddr *dev;
  127. struct pnfs_block_volume *b;
  128. dev = kzalloc(struct_size(dev, volumes, 1), GFP_KERNEL);
  129. if (!dev)
  130. return -ENOMEM;
  131. gdp->gd_device = dev;
  132. dev->nr_volumes = 1;
  133. b = &dev->volumes[0];
  134. b->type = PNFS_BLOCK_VOLUME_SIMPLE;
  135. b->simple.sig_len = PNFS_BLOCK_UUID_LEN;
  136. return sb->s_export_op->get_uuid(sb, b->simple.sig, &b->simple.sig_len,
  137. &b->simple.offset);
  138. }
  139. static __be32
  140. nfsd4_block_proc_getdeviceinfo(struct super_block *sb,
  141. struct svc_rqst *rqstp,
  142. struct nfs4_client *clp,
  143. struct nfsd4_getdeviceinfo *gdp)
  144. {
  145. if (bdev_is_partition(sb->s_bdev))
  146. return nfserr_inval;
  147. return nfserrno(nfsd4_block_get_device_info_simple(sb, gdp));
  148. }
  149. static __be32
  150. nfsd4_block_proc_layoutcommit(struct inode *inode, struct svc_rqst *rqstp,
  151. struct nfsd4_layoutcommit *lcp)
  152. {
  153. struct iomap *iomaps;
  154. int nr_iomaps;
  155. __be32 nfserr;
  156. rqstp->rq_arg = lcp->lc_up_layout;
  157. svcxdr_init_decode(rqstp);
  158. nfserr = nfsd4_block_decode_layoutupdate(&rqstp->rq_arg_stream,
  159. &iomaps, &nr_iomaps, i_blocksize(inode));
  160. if (nfserr != nfs_ok)
  161. return nfserr;
  162. return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
  163. }
  164. const struct nfsd4_layout_ops bl_layout_ops = {
  165. /*
  166. * Pretend that we send notification to the client. This is a blatant
  167. * lie to force recent Linux clients to cache our device IDs.
  168. * We rarely ever change the device ID, so the harm of leaking deviceids
  169. * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
  170. * in this regard, but I filed errata 4119 for this a while ago, and
  171. * hopefully the Linux client will eventually start caching deviceids
  172. * without this again.
  173. */
  174. .notify_types =
  175. NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
  176. .proc_getdeviceinfo = nfsd4_block_proc_getdeviceinfo,
  177. .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
  178. .proc_layoutget = nfsd4_block_proc_layoutget,
  179. .encode_layoutget = nfsd4_block_encode_layoutget,
  180. .proc_layoutcommit = nfsd4_block_proc_layoutcommit,
  181. };
  182. #endif /* CONFIG_NFSD_BLOCKLAYOUT */
  183. #ifdef CONFIG_NFSD_SCSILAYOUT
  184. #define NFSD_MDS_PR_KEY 0x0100000000000000ULL
  185. /*
  186. * We use the client ID as a unique key for the reservations.
  187. * This allows us to easily fence a client when recalls fail.
  188. */
  189. static u64 nfsd4_scsi_pr_key(struct nfs4_client *clp)
  190. {
  191. return ((u64)clp->cl_clientid.cl_boot << 32) | clp->cl_clientid.cl_id;
  192. }
  193. static const u8 designator_types[] = {
  194. PS_DESIGNATOR_EUI64,
  195. PS_DESIGNATOR_NAA,
  196. };
  197. static int
  198. nfsd4_block_get_unique_id(struct gendisk *disk, struct pnfs_block_volume *b)
  199. {
  200. int ret, i;
  201. for (i = 0; i < ARRAY_SIZE(designator_types); i++) {
  202. u8 type = designator_types[i];
  203. ret = disk->fops->get_unique_id(disk, b->scsi.designator, type);
  204. if (ret > 0) {
  205. b->scsi.code_set = PS_CODE_SET_BINARY;
  206. b->scsi.designator_type = type;
  207. b->scsi.designator_len = ret;
  208. return 0;
  209. }
  210. }
  211. return -EINVAL;
  212. }
  213. static int
  214. nfsd4_block_get_device_info_scsi(struct super_block *sb,
  215. struct nfs4_client *clp,
  216. struct nfsd4_getdeviceinfo *gdp)
  217. {
  218. struct pnfs_block_deviceaddr *dev;
  219. struct pnfs_block_volume *b;
  220. const struct pr_ops *ops;
  221. int ret;
  222. dev = kzalloc(struct_size(dev, volumes, 1), GFP_KERNEL);
  223. if (!dev)
  224. return -ENOMEM;
  225. gdp->gd_device = dev;
  226. dev->nr_volumes = 1;
  227. b = &dev->volumes[0];
  228. b->type = PNFS_BLOCK_VOLUME_SCSI;
  229. b->scsi.pr_key = nfsd4_scsi_pr_key(clp);
  230. ret = nfsd4_block_get_unique_id(sb->s_bdev->bd_disk, b);
  231. if (ret < 0)
  232. goto out_free_dev;
  233. ret = -EINVAL;
  234. ops = sb->s_bdev->bd_disk->fops->pr_ops;
  235. if (!ops) {
  236. pr_err("pNFS: device %s does not support PRs.\n",
  237. sb->s_id);
  238. goto out_free_dev;
  239. }
  240. ret = ops->pr_register(sb->s_bdev, 0, NFSD_MDS_PR_KEY, true);
  241. if (ret) {
  242. pr_err("pNFS: failed to register key for device %s.\n",
  243. sb->s_id);
  244. goto out_free_dev;
  245. }
  246. ret = ops->pr_reserve(sb->s_bdev, NFSD_MDS_PR_KEY,
  247. PR_EXCLUSIVE_ACCESS_REG_ONLY, 0);
  248. if (ret) {
  249. pr_err("pNFS: failed to reserve device %s.\n",
  250. sb->s_id);
  251. goto out_free_dev;
  252. }
  253. return 0;
  254. out_free_dev:
  255. kfree(dev);
  256. gdp->gd_device = NULL;
  257. return ret;
  258. }
  259. static __be32
  260. nfsd4_scsi_proc_getdeviceinfo(struct super_block *sb,
  261. struct svc_rqst *rqstp,
  262. struct nfs4_client *clp,
  263. struct nfsd4_getdeviceinfo *gdp)
  264. {
  265. if (bdev_is_partition(sb->s_bdev))
  266. return nfserr_inval;
  267. return nfserrno(nfsd4_block_get_device_info_scsi(sb, clp, gdp));
  268. }
  269. static __be32
  270. nfsd4_scsi_proc_layoutcommit(struct inode *inode, struct svc_rqst *rqstp,
  271. struct nfsd4_layoutcommit *lcp)
  272. {
  273. struct iomap *iomaps;
  274. int nr_iomaps;
  275. __be32 nfserr;
  276. rqstp->rq_arg = lcp->lc_up_layout;
  277. svcxdr_init_decode(rqstp);
  278. nfserr = nfsd4_scsi_decode_layoutupdate(&rqstp->rq_arg_stream,
  279. &iomaps, &nr_iomaps, i_blocksize(inode));
  280. if (nfserr != nfs_ok)
  281. return nfserr;
  282. return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
  283. }
  284. static void
  285. nfsd4_scsi_fence_client(struct nfs4_layout_stateid *ls, struct nfsd_file *file)
  286. {
  287. struct nfs4_client *clp = ls->ls_stid.sc_client;
  288. struct block_device *bdev = file->nf_file->f_path.mnt->mnt_sb->s_bdev;
  289. bdev->bd_disk->fops->pr_ops->pr_preempt(bdev, NFSD_MDS_PR_KEY,
  290. nfsd4_scsi_pr_key(clp), 0, true);
  291. }
  292. const struct nfsd4_layout_ops scsi_layout_ops = {
  293. /*
  294. * Pretend that we send notification to the client. This is a blatant
  295. * lie to force recent Linux clients to cache our device IDs.
  296. * We rarely ever change the device ID, so the harm of leaking deviceids
  297. * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
  298. * in this regard, but I filed errata 4119 for this a while ago, and
  299. * hopefully the Linux client will eventually start caching deviceids
  300. * without this again.
  301. */
  302. .notify_types =
  303. NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
  304. .proc_getdeviceinfo = nfsd4_scsi_proc_getdeviceinfo,
  305. .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
  306. .proc_layoutget = nfsd4_block_proc_layoutget,
  307. .encode_layoutget = nfsd4_block_encode_layoutget,
  308. .proc_layoutcommit = nfsd4_scsi_proc_layoutcommit,
  309. .fence_client = nfsd4_scsi_fence_client,
  310. };
  311. #endif /* CONFIG_NFSD_SCSILAYOUT */