blocklayout.c 11 KB

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