blocklayout.c 11 KB

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