blocklayout.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. int error;
  109. if (lcp->lc_mtime.tv_nsec == UTIME_NOW ||
  110. timespec_compare(&lcp->lc_mtime, &inode->i_mtime) < 0)
  111. lcp->lc_mtime = current_time(inode);
  112. iattr.ia_valid |= ATTR_ATIME | ATTR_CTIME | ATTR_MTIME;
  113. iattr.ia_atime = iattr.ia_ctime = iattr.ia_mtime = lcp->lc_mtime;
  114. if (new_size > i_size_read(inode)) {
  115. iattr.ia_valid |= ATTR_SIZE;
  116. iattr.ia_size = new_size;
  117. }
  118. error = inode->i_sb->s_export_op->commit_blocks(inode, iomaps,
  119. nr_iomaps, &iattr);
  120. kfree(iomaps);
  121. return nfserrno(error);
  122. }
  123. #ifdef CONFIG_NFSD_BLOCKLAYOUT
  124. static int
  125. nfsd4_block_get_device_info_simple(struct super_block *sb,
  126. struct nfsd4_getdeviceinfo *gdp)
  127. {
  128. struct pnfs_block_deviceaddr *dev;
  129. struct pnfs_block_volume *b;
  130. dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
  131. sizeof(struct pnfs_block_volume), GFP_KERNEL);
  132. if (!dev)
  133. return -ENOMEM;
  134. gdp->gd_device = dev;
  135. dev->nr_volumes = 1;
  136. b = &dev->volumes[0];
  137. b->type = PNFS_BLOCK_VOLUME_SIMPLE;
  138. b->simple.sig_len = PNFS_BLOCK_UUID_LEN;
  139. return sb->s_export_op->get_uuid(sb, b->simple.sig, &b->simple.sig_len,
  140. &b->simple.offset);
  141. }
  142. static __be32
  143. nfsd4_block_proc_getdeviceinfo(struct super_block *sb,
  144. struct svc_rqst *rqstp,
  145. struct nfs4_client *clp,
  146. struct nfsd4_getdeviceinfo *gdp)
  147. {
  148. if (sb->s_bdev != sb->s_bdev->bd_contains)
  149. return nfserr_inval;
  150. return nfserrno(nfsd4_block_get_device_info_simple(sb, gdp));
  151. }
  152. static __be32
  153. nfsd4_block_proc_layoutcommit(struct inode *inode,
  154. struct nfsd4_layoutcommit *lcp)
  155. {
  156. struct iomap *iomaps;
  157. int nr_iomaps;
  158. nr_iomaps = nfsd4_block_decode_layoutupdate(lcp->lc_up_layout,
  159. lcp->lc_up_len, &iomaps, i_blocksize(inode));
  160. if (nr_iomaps < 0)
  161. return nfserrno(nr_iomaps);
  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. static int nfsd4_scsi_identify_device(struct block_device *bdev,
  185. struct pnfs_block_volume *b)
  186. {
  187. struct request_queue *q = bdev->bd_disk->queue;
  188. struct request *rq;
  189. struct scsi_request *req;
  190. size_t bufflen = 252, len, id_len;
  191. u8 *buf, *d, type, assoc;
  192. int error;
  193. if (WARN_ON_ONCE(!blk_queue_scsi_passthrough(q)))
  194. return -EINVAL;
  195. buf = kzalloc(bufflen, GFP_KERNEL);
  196. if (!buf)
  197. return -ENOMEM;
  198. rq = blk_get_request(q, REQ_OP_SCSI_IN, 0);
  199. if (IS_ERR(rq)) {
  200. error = -ENOMEM;
  201. goto out_free_buf;
  202. }
  203. req = scsi_req(rq);
  204. error = blk_rq_map_kern(q, rq, buf, bufflen, GFP_KERNEL);
  205. if (error)
  206. goto out_put_request;
  207. req->cmd[0] = INQUIRY;
  208. req->cmd[1] = 1;
  209. req->cmd[2] = 0x83;
  210. req->cmd[3] = bufflen >> 8;
  211. req->cmd[4] = bufflen & 0xff;
  212. req->cmd_len = COMMAND_SIZE(INQUIRY);
  213. blk_execute_rq(rq->q, NULL, rq, 1);
  214. if (req->result) {
  215. pr_err("pNFS: INQUIRY 0x83 failed with: %x\n",
  216. req->result);
  217. error = -EIO;
  218. goto out_put_request;
  219. }
  220. len = (buf[2] << 8) + buf[3] + 4;
  221. if (len > bufflen) {
  222. pr_err("pNFS: INQUIRY 0x83 response invalid (len = %zd)\n",
  223. len);
  224. goto out_put_request;
  225. }
  226. d = buf + 4;
  227. for (d = buf + 4; d < buf + len; d += id_len + 4) {
  228. id_len = d[3];
  229. type = d[1] & 0xf;
  230. assoc = (d[1] >> 4) & 0x3;
  231. /*
  232. * We only care about a EUI-64 and NAA designator types
  233. * with LU association.
  234. */
  235. if (assoc != 0x00)
  236. continue;
  237. if (type != 0x02 && type != 0x03)
  238. continue;
  239. if (id_len != 8 && id_len != 12 && id_len != 16)
  240. continue;
  241. b->scsi.code_set = PS_CODE_SET_BINARY;
  242. b->scsi.designator_type = type == 0x02 ?
  243. PS_DESIGNATOR_EUI64 : PS_DESIGNATOR_NAA;
  244. b->scsi.designator_len = id_len;
  245. memcpy(b->scsi.designator, d + 4, id_len);
  246. /*
  247. * If we found a 8 or 12 byte descriptor continue on to
  248. * see if a 16 byte one is available. If we find a
  249. * 16 byte descriptor we're done.
  250. */
  251. if (id_len == 16)
  252. break;
  253. }
  254. out_put_request:
  255. blk_put_request(rq);
  256. out_free_buf:
  257. kfree(buf);
  258. return error;
  259. }
  260. #define NFSD_MDS_PR_KEY 0x0100000000000000ULL
  261. /*
  262. * We use the client ID as a unique key for the reservations.
  263. * This allows us to easily fence a client when recalls fail.
  264. */
  265. static u64 nfsd4_scsi_pr_key(struct nfs4_client *clp)
  266. {
  267. return ((u64)clp->cl_clientid.cl_boot << 32) | clp->cl_clientid.cl_id;
  268. }
  269. static int
  270. nfsd4_block_get_device_info_scsi(struct super_block *sb,
  271. struct nfs4_client *clp,
  272. struct nfsd4_getdeviceinfo *gdp)
  273. {
  274. struct pnfs_block_deviceaddr *dev;
  275. struct pnfs_block_volume *b;
  276. const struct pr_ops *ops;
  277. int error;
  278. dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
  279. sizeof(struct pnfs_block_volume), GFP_KERNEL);
  280. if (!dev)
  281. return -ENOMEM;
  282. gdp->gd_device = dev;
  283. dev->nr_volumes = 1;
  284. b = &dev->volumes[0];
  285. b->type = PNFS_BLOCK_VOLUME_SCSI;
  286. b->scsi.pr_key = nfsd4_scsi_pr_key(clp);
  287. error = nfsd4_scsi_identify_device(sb->s_bdev, b);
  288. if (error)
  289. return error;
  290. ops = sb->s_bdev->bd_disk->fops->pr_ops;
  291. if (!ops) {
  292. pr_err("pNFS: device %s does not support PRs.\n",
  293. sb->s_id);
  294. return -EINVAL;
  295. }
  296. error = ops->pr_register(sb->s_bdev, 0, NFSD_MDS_PR_KEY, true);
  297. if (error) {
  298. pr_err("pNFS: failed to register key for device %s.\n",
  299. sb->s_id);
  300. return -EINVAL;
  301. }
  302. error = ops->pr_reserve(sb->s_bdev, NFSD_MDS_PR_KEY,
  303. PR_EXCLUSIVE_ACCESS_REG_ONLY, 0);
  304. if (error) {
  305. pr_err("pNFS: failed to reserve device %s.\n",
  306. sb->s_id);
  307. return -EINVAL;
  308. }
  309. return 0;
  310. }
  311. static __be32
  312. nfsd4_scsi_proc_getdeviceinfo(struct super_block *sb,
  313. struct svc_rqst *rqstp,
  314. struct nfs4_client *clp,
  315. struct nfsd4_getdeviceinfo *gdp)
  316. {
  317. if (sb->s_bdev != sb->s_bdev->bd_contains)
  318. return nfserr_inval;
  319. return nfserrno(nfsd4_block_get_device_info_scsi(sb, clp, gdp));
  320. }
  321. static __be32
  322. nfsd4_scsi_proc_layoutcommit(struct inode *inode,
  323. struct nfsd4_layoutcommit *lcp)
  324. {
  325. struct iomap *iomaps;
  326. int nr_iomaps;
  327. nr_iomaps = nfsd4_scsi_decode_layoutupdate(lcp->lc_up_layout,
  328. lcp->lc_up_len, &iomaps, i_blocksize(inode));
  329. if (nr_iomaps < 0)
  330. return nfserrno(nr_iomaps);
  331. return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
  332. }
  333. static void
  334. nfsd4_scsi_fence_client(struct nfs4_layout_stateid *ls)
  335. {
  336. struct nfs4_client *clp = ls->ls_stid.sc_client;
  337. struct block_device *bdev = ls->ls_file->f_path.mnt->mnt_sb->s_bdev;
  338. bdev->bd_disk->fops->pr_ops->pr_preempt(bdev, NFSD_MDS_PR_KEY,
  339. nfsd4_scsi_pr_key(clp), 0, true);
  340. }
  341. const struct nfsd4_layout_ops scsi_layout_ops = {
  342. /*
  343. * Pretend that we send notification to the client. This is a blatant
  344. * lie to force recent Linux clients to cache our device IDs.
  345. * We rarely ever change the device ID, so the harm of leaking deviceids
  346. * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
  347. * in this regard, but I filed errata 4119 for this a while ago, and
  348. * hopefully the Linux client will eventually start caching deviceids
  349. * without this again.
  350. */
  351. .notify_types =
  352. NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
  353. .proc_getdeviceinfo = nfsd4_scsi_proc_getdeviceinfo,
  354. .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
  355. .proc_layoutget = nfsd4_block_proc_layoutget,
  356. .encode_layoutget = nfsd4_block_encode_layoutget,
  357. .proc_layoutcommit = nfsd4_scsi_proc_layoutcommit,
  358. .fence_client = nfsd4_scsi_fence_client,
  359. };
  360. #endif /* CONFIG_NFSD_SCSILAYOUT */