blocklayout.c 9.2 KB

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