blocklayout.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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/genhd.h>
  8. #include <linux/slab.h>
  9. #include <linux/pr.h>
  10. #include <linux/nfsd/debug.h>
  11. #include <scsi/scsi_proto.h>
  12. #include <scsi/scsi_common.h>
  13. #include <scsi/scsi_request.h>
  14. #include "blocklayoutxdr.h"
  15. #include "pnfs.h"
  16. #include "filecache.h"
  17. #define NFSDDBG_FACILITY NFSDDBG_PNFS
  18. static __be32
  19. nfsd4_block_proc_layoutget(struct inode *inode, const struct svc_fh *fhp,
  20. struct nfsd4_layoutget *args)
  21. {
  22. struct nfsd4_layout_seg *seg = &args->lg_seg;
  23. struct super_block *sb = inode->i_sb;
  24. u32 block_size = i_blocksize(inode);
  25. struct pnfs_block_extent *bex;
  26. struct iomap iomap;
  27. u32 device_generation = 0;
  28. int error;
  29. if (seg->offset & (block_size - 1)) {
  30. dprintk("pnfsd: I/O misaligned\n");
  31. goto out_layoutunavailable;
  32. }
  33. /*
  34. * Some clients barf on non-zero block numbers for NONE or INVALID
  35. * layouts, so make sure to zero the whole structure.
  36. */
  37. error = -ENOMEM;
  38. bex = kzalloc(sizeof(*bex), GFP_KERNEL);
  39. if (!bex)
  40. goto out_error;
  41. args->lg_content = bex;
  42. error = sb->s_export_op->map_blocks(inode, seg->offset, seg->length,
  43. &iomap, seg->iomode != IOMODE_READ,
  44. &device_generation);
  45. if (error) {
  46. if (error == -ENXIO)
  47. goto out_layoutunavailable;
  48. goto out_error;
  49. }
  50. if (iomap.length < args->lg_minlength) {
  51. dprintk("pnfsd: extent smaller than minlength\n");
  52. goto out_layoutunavailable;
  53. }
  54. switch (iomap.type) {
  55. case IOMAP_MAPPED:
  56. if (seg->iomode == IOMODE_READ)
  57. bex->es = PNFS_BLOCK_READ_DATA;
  58. else
  59. bex->es = PNFS_BLOCK_READWRITE_DATA;
  60. bex->soff = iomap.addr;
  61. break;
  62. case IOMAP_UNWRITTEN:
  63. if (seg->iomode & IOMODE_RW) {
  64. /*
  65. * Crack monkey special case from section 2.3.1.
  66. */
  67. if (args->lg_minlength == 0) {
  68. dprintk("pnfsd: no soup for you!\n");
  69. goto out_layoutunavailable;
  70. }
  71. bex->es = PNFS_BLOCK_INVALID_DATA;
  72. bex->soff = iomap.addr;
  73. break;
  74. }
  75. fallthrough;
  76. case IOMAP_HOLE:
  77. if (seg->iomode == IOMODE_READ) {
  78. bex->es = PNFS_BLOCK_NONE_DATA;
  79. break;
  80. }
  81. fallthrough;
  82. case IOMAP_DELALLOC:
  83. default:
  84. WARN(1, "pnfsd: filesystem returned %d extent\n", iomap.type);
  85. goto out_layoutunavailable;
  86. }
  87. error = nfsd4_set_deviceid(&bex->vol_id, fhp, device_generation);
  88. if (error)
  89. goto out_error;
  90. bex->foff = iomap.offset;
  91. bex->len = iomap.length;
  92. seg->offset = iomap.offset;
  93. seg->length = iomap.length;
  94. dprintk("GET: 0x%llx:0x%llx %d\n", bex->foff, bex->len, bex->es);
  95. return 0;
  96. out_error:
  97. seg->length = 0;
  98. return nfserrno(error);
  99. out_layoutunavailable:
  100. seg->length = 0;
  101. return nfserr_layoutunavailable;
  102. }
  103. static __be32
  104. nfsd4_block_commit_blocks(struct inode *inode, struct nfsd4_layoutcommit *lcp,
  105. struct iomap *iomaps, int nr_iomaps)
  106. {
  107. loff_t new_size = lcp->lc_last_wr + 1;
  108. struct iattr iattr = { .ia_valid = 0 };
  109. int error;
  110. if (lcp->lc_mtime.tv_nsec == UTIME_NOW ||
  111. timespec64_compare(&lcp->lc_mtime, &inode->i_mtime) < 0)
  112. lcp->lc_mtime = current_time(inode);
  113. iattr.ia_valid |= ATTR_ATIME | ATTR_CTIME | ATTR_MTIME;
  114. iattr.ia_atime = iattr.ia_ctime = iattr.ia_mtime = lcp->lc_mtime;
  115. if (new_size > i_size_read(inode)) {
  116. iattr.ia_valid |= ATTR_SIZE;
  117. iattr.ia_size = new_size;
  118. }
  119. error = inode->i_sb->s_export_op->commit_blocks(inode, iomaps,
  120. nr_iomaps, &iattr);
  121. kfree(iomaps);
  122. return nfserrno(error);
  123. }
  124. #ifdef CONFIG_NFSD_BLOCKLAYOUT
  125. static int
  126. nfsd4_block_get_device_info_simple(struct super_block *sb,
  127. struct nfsd4_getdeviceinfo *gdp)
  128. {
  129. struct pnfs_block_deviceaddr *dev;
  130. struct pnfs_block_volume *b;
  131. dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
  132. sizeof(struct pnfs_block_volume), GFP_KERNEL);
  133. if (!dev)
  134. return -ENOMEM;
  135. gdp->gd_device = dev;
  136. dev->nr_volumes = 1;
  137. b = &dev->volumes[0];
  138. b->type = PNFS_BLOCK_VOLUME_SIMPLE;
  139. b->simple.sig_len = PNFS_BLOCK_UUID_LEN;
  140. return sb->s_export_op->get_uuid(sb, b->simple.sig, &b->simple.sig_len,
  141. &b->simple.offset);
  142. }
  143. static __be32
  144. nfsd4_block_proc_getdeviceinfo(struct super_block *sb,
  145. struct svc_rqst *rqstp,
  146. struct nfs4_client *clp,
  147. struct nfsd4_getdeviceinfo *gdp)
  148. {
  149. if (bdev_is_partition(sb->s_bdev))
  150. return nfserr_inval;
  151. return nfserrno(nfsd4_block_get_device_info_simple(sb, gdp));
  152. }
  153. static __be32
  154. nfsd4_block_proc_layoutcommit(struct inode *inode,
  155. struct nfsd4_layoutcommit *lcp)
  156. {
  157. struct iomap *iomaps;
  158. int nr_iomaps;
  159. nr_iomaps = nfsd4_block_decode_layoutupdate(lcp->lc_up_layout,
  160. lcp->lc_up_len, &iomaps, i_blocksize(inode));
  161. if (nr_iomaps < 0)
  162. return nfserrno(nr_iomaps);
  163. return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
  164. }
  165. const struct nfsd4_layout_ops bl_layout_ops = {
  166. /*
  167. * Pretend that we send notification to the client. This is a blatant
  168. * lie to force recent Linux clients to cache our device IDs.
  169. * We rarely ever change the device ID, so the harm of leaking deviceids
  170. * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
  171. * in this regard, but I filed errata 4119 for this a while ago, and
  172. * hopefully the Linux client will eventually start caching deviceids
  173. * without this again.
  174. */
  175. .notify_types =
  176. NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
  177. .proc_getdeviceinfo = nfsd4_block_proc_getdeviceinfo,
  178. .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
  179. .proc_layoutget = nfsd4_block_proc_layoutget,
  180. .encode_layoutget = nfsd4_block_encode_layoutget,
  181. .proc_layoutcommit = nfsd4_block_proc_layoutcommit,
  182. };
  183. #endif /* CONFIG_NFSD_BLOCKLAYOUT */
  184. #ifdef CONFIG_NFSD_SCSILAYOUT
  185. static int nfsd4_scsi_identify_device(struct block_device *bdev,
  186. struct pnfs_block_volume *b)
  187. {
  188. struct request_queue *q = bdev->bd_disk->queue;
  189. struct request *rq;
  190. struct scsi_request *req;
  191. /*
  192. * The allocation length (passed in bytes 3 and 4 of the INQUIRY
  193. * command descriptor block) specifies the number of bytes that have
  194. * been allocated for the data-in buffer.
  195. * 252 is the highest one-byte value that is a multiple of 4.
  196. * 65532 is the highest two-byte value that is a multiple of 4.
  197. */
  198. size_t bufflen = 252, maxlen = 65532, len, id_len;
  199. u8 *buf, *d, type, assoc;
  200. int retries = 1, error;
  201. if (WARN_ON_ONCE(!blk_queue_scsi_passthrough(q)))
  202. return -EINVAL;
  203. again:
  204. buf = kzalloc(bufflen, GFP_KERNEL);
  205. if (!buf)
  206. return -ENOMEM;
  207. rq = blk_get_request(q, REQ_OP_SCSI_IN, 0);
  208. if (IS_ERR(rq)) {
  209. error = -ENOMEM;
  210. goto out_free_buf;
  211. }
  212. req = scsi_req(rq);
  213. error = blk_rq_map_kern(q, rq, buf, bufflen, GFP_KERNEL);
  214. if (error)
  215. goto out_put_request;
  216. req->cmd[0] = INQUIRY;
  217. req->cmd[1] = 1;
  218. req->cmd[2] = 0x83;
  219. req->cmd[3] = bufflen >> 8;
  220. req->cmd[4] = bufflen & 0xff;
  221. req->cmd_len = COMMAND_SIZE(INQUIRY);
  222. blk_execute_rq(rq->q, NULL, rq, 1);
  223. if (req->result) {
  224. pr_err("pNFS: INQUIRY 0x83 failed with: %x\n",
  225. req->result);
  226. error = -EIO;
  227. goto out_put_request;
  228. }
  229. len = (buf[2] << 8) + buf[3] + 4;
  230. if (len > bufflen) {
  231. if (len <= maxlen && retries--) {
  232. blk_put_request(rq);
  233. kfree(buf);
  234. bufflen = len;
  235. goto again;
  236. }
  237. pr_err("pNFS: INQUIRY 0x83 response invalid (len = %zd)\n",
  238. len);
  239. goto out_put_request;
  240. }
  241. d = buf + 4;
  242. for (d = buf + 4; d < buf + len; d += id_len + 4) {
  243. id_len = d[3];
  244. type = d[1] & 0xf;
  245. assoc = (d[1] >> 4) & 0x3;
  246. /*
  247. * We only care about a EUI-64 and NAA designator types
  248. * with LU association.
  249. */
  250. if (assoc != 0x00)
  251. continue;
  252. if (type != 0x02 && type != 0x03)
  253. continue;
  254. if (id_len != 8 && id_len != 12 && id_len != 16)
  255. continue;
  256. b->scsi.code_set = PS_CODE_SET_BINARY;
  257. b->scsi.designator_type = type == 0x02 ?
  258. PS_DESIGNATOR_EUI64 : PS_DESIGNATOR_NAA;
  259. b->scsi.designator_len = id_len;
  260. memcpy(b->scsi.designator, d + 4, id_len);
  261. /*
  262. * If we found a 8 or 12 byte descriptor continue on to
  263. * see if a 16 byte one is available. If we find a
  264. * 16 byte descriptor we're done.
  265. */
  266. if (id_len == 16)
  267. break;
  268. }
  269. out_put_request:
  270. blk_put_request(rq);
  271. out_free_buf:
  272. kfree(buf);
  273. return error;
  274. }
  275. #define NFSD_MDS_PR_KEY 0x0100000000000000ULL
  276. /*
  277. * We use the client ID as a unique key for the reservations.
  278. * This allows us to easily fence a client when recalls fail.
  279. */
  280. static u64 nfsd4_scsi_pr_key(struct nfs4_client *clp)
  281. {
  282. return ((u64)clp->cl_clientid.cl_boot << 32) | clp->cl_clientid.cl_id;
  283. }
  284. static int
  285. nfsd4_block_get_device_info_scsi(struct super_block *sb,
  286. struct nfs4_client *clp,
  287. struct nfsd4_getdeviceinfo *gdp)
  288. {
  289. struct pnfs_block_deviceaddr *dev;
  290. struct pnfs_block_volume *b;
  291. const struct pr_ops *ops;
  292. int error;
  293. dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
  294. sizeof(struct pnfs_block_volume), GFP_KERNEL);
  295. if (!dev)
  296. return -ENOMEM;
  297. gdp->gd_device = dev;
  298. dev->nr_volumes = 1;
  299. b = &dev->volumes[0];
  300. b->type = PNFS_BLOCK_VOLUME_SCSI;
  301. b->scsi.pr_key = nfsd4_scsi_pr_key(clp);
  302. error = nfsd4_scsi_identify_device(sb->s_bdev, b);
  303. if (error)
  304. return error;
  305. ops = sb->s_bdev->bd_disk->fops->pr_ops;
  306. if (!ops) {
  307. pr_err("pNFS: device %s does not support PRs.\n",
  308. sb->s_id);
  309. return -EINVAL;
  310. }
  311. error = ops->pr_register(sb->s_bdev, 0, NFSD_MDS_PR_KEY, true);
  312. if (error) {
  313. pr_err("pNFS: failed to register key for device %s.\n",
  314. sb->s_id);
  315. return -EINVAL;
  316. }
  317. error = ops->pr_reserve(sb->s_bdev, NFSD_MDS_PR_KEY,
  318. PR_EXCLUSIVE_ACCESS_REG_ONLY, 0);
  319. if (error) {
  320. pr_err("pNFS: failed to reserve device %s.\n",
  321. sb->s_id);
  322. return -EINVAL;
  323. }
  324. return 0;
  325. }
  326. static __be32
  327. nfsd4_scsi_proc_getdeviceinfo(struct super_block *sb,
  328. struct svc_rqst *rqstp,
  329. struct nfs4_client *clp,
  330. struct nfsd4_getdeviceinfo *gdp)
  331. {
  332. if (bdev_is_partition(sb->s_bdev))
  333. return nfserr_inval;
  334. return nfserrno(nfsd4_block_get_device_info_scsi(sb, clp, gdp));
  335. }
  336. static __be32
  337. nfsd4_scsi_proc_layoutcommit(struct inode *inode,
  338. struct nfsd4_layoutcommit *lcp)
  339. {
  340. struct iomap *iomaps;
  341. int nr_iomaps;
  342. nr_iomaps = nfsd4_scsi_decode_layoutupdate(lcp->lc_up_layout,
  343. lcp->lc_up_len, &iomaps, i_blocksize(inode));
  344. if (nr_iomaps < 0)
  345. return nfserrno(nr_iomaps);
  346. return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
  347. }
  348. static void
  349. nfsd4_scsi_fence_client(struct nfs4_layout_stateid *ls)
  350. {
  351. struct nfs4_client *clp = ls->ls_stid.sc_client;
  352. struct block_device *bdev = ls->ls_file->nf_file->f_path.mnt->mnt_sb->s_bdev;
  353. bdev->bd_disk->fops->pr_ops->pr_preempt(bdev, NFSD_MDS_PR_KEY,
  354. nfsd4_scsi_pr_key(clp), 0, true);
  355. }
  356. const struct nfsd4_layout_ops scsi_layout_ops = {
  357. /*
  358. * Pretend that we send notification to the client. This is a blatant
  359. * lie to force recent Linux clients to cache our device IDs.
  360. * We rarely ever change the device ID, so the harm of leaking deviceids
  361. * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
  362. * in this regard, but I filed errata 4119 for this a while ago, and
  363. * hopefully the Linux client will eventually start caching deviceids
  364. * without this again.
  365. */
  366. .notify_types =
  367. NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
  368. .proc_getdeviceinfo = nfsd4_scsi_proc_getdeviceinfo,
  369. .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
  370. .proc_layoutget = nfsd4_block_proc_layoutget,
  371. .encode_layoutget = nfsd4_block_encode_layoutget,
  372. .proc_layoutcommit = nfsd4_scsi_proc_layoutcommit,
  373. .fence_client = nfsd4_scsi_fence_client,
  374. };
  375. #endif /* CONFIG_NFSD_SCSILAYOUT */