blocklayout.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (c) 2014-2016 Christoph Hellwig.
  3. */
  4. #include <linux/exportfs.h>
  5. #include <linux/genhd.h>
  6. #include <linux/slab.h>
  7. #include <linux/pr.h>
  8. #include <linux/nfsd/debug.h>
  9. #include <scsi/scsi_proto.h>
  10. #include <scsi/scsi_common.h>
  11. #include "blocklayoutxdr.h"
  12. #include "pnfs.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 = (1 << inode->i_blkbits);
  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.blkno << 9);
  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.blkno << 9);
  69. break;
  70. }
  71. /*FALLTHRU*/
  72. case IOMAP_HOLE:
  73. if (seg->iomode == IOMODE_READ) {
  74. bex->es = PNFS_BLOCK_NONE_DATA;
  75. break;
  76. }
  77. /*FALLTHRU*/
  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. timespec_compare(&lcp->lc_mtime, &inode->i_mtime) < 0)
  108. lcp->lc_mtime = current_fs_time(inode->i_sb);
  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 nfs4_client *clp,
  142. struct nfsd4_getdeviceinfo *gdp)
  143. {
  144. if (sb->s_bdev != sb->s_bdev->bd_contains)
  145. return nfserr_inval;
  146. return nfserrno(nfsd4_block_get_device_info_simple(sb, gdp));
  147. }
  148. static __be32
  149. nfsd4_block_proc_layoutcommit(struct inode *inode,
  150. struct nfsd4_layoutcommit *lcp)
  151. {
  152. struct iomap *iomaps;
  153. int nr_iomaps;
  154. nr_iomaps = nfsd4_block_decode_layoutupdate(lcp->lc_up_layout,
  155. lcp->lc_up_len, &iomaps, 1 << inode->i_blkbits);
  156. if (nr_iomaps < 0)
  157. return nfserrno(nr_iomaps);
  158. return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
  159. }
  160. const struct nfsd4_layout_ops bl_layout_ops = {
  161. /*
  162. * Pretend that we send notification to the client. This is a blatant
  163. * lie to force recent Linux clients to cache our device IDs.
  164. * We rarely ever change the device ID, so the harm of leaking deviceids
  165. * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
  166. * in this regard, but I filed errata 4119 for this a while ago, and
  167. * hopefully the Linux client will eventually start caching deviceids
  168. * without this again.
  169. */
  170. .notify_types =
  171. NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
  172. .proc_getdeviceinfo = nfsd4_block_proc_getdeviceinfo,
  173. .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
  174. .proc_layoutget = nfsd4_block_proc_layoutget,
  175. .encode_layoutget = nfsd4_block_encode_layoutget,
  176. .proc_layoutcommit = nfsd4_block_proc_layoutcommit,
  177. };
  178. #endif /* CONFIG_NFSD_BLOCKLAYOUT */
  179. #ifdef CONFIG_NFSD_SCSILAYOUT
  180. static int nfsd4_scsi_identify_device(struct block_device *bdev,
  181. struct pnfs_block_volume *b)
  182. {
  183. struct request_queue *q = bdev->bd_disk->queue;
  184. struct request *rq;
  185. size_t bufflen = 252, len, id_len;
  186. u8 *buf, *d, type, assoc;
  187. int error;
  188. buf = kzalloc(bufflen, GFP_KERNEL);
  189. if (!buf)
  190. return -ENOMEM;
  191. rq = blk_get_request(q, READ, GFP_KERNEL);
  192. if (IS_ERR(rq)) {
  193. error = -ENOMEM;
  194. goto out_free_buf;
  195. }
  196. blk_rq_set_block_pc(rq);
  197. error = blk_rq_map_kern(q, rq, buf, bufflen, GFP_KERNEL);
  198. if (error)
  199. goto out_put_request;
  200. rq->cmd[0] = INQUIRY;
  201. rq->cmd[1] = 1;
  202. rq->cmd[2] = 0x83;
  203. rq->cmd[3] = bufflen >> 8;
  204. rq->cmd[4] = bufflen & 0xff;
  205. rq->cmd_len = COMMAND_SIZE(INQUIRY);
  206. error = blk_execute_rq(rq->q, NULL, rq, 1);
  207. if (error) {
  208. pr_err("pNFS: INQUIRY 0x83 failed with: %x\n",
  209. rq->errors);
  210. goto out_put_request;
  211. }
  212. len = (buf[2] << 8) + buf[3] + 4;
  213. if (len > bufflen) {
  214. pr_err("pNFS: INQUIRY 0x83 response invalid (len = %zd)\n",
  215. len);
  216. goto out_put_request;
  217. }
  218. d = buf + 4;
  219. for (d = buf + 4; d < buf + len; d += id_len + 4) {
  220. id_len = d[3];
  221. type = d[1] & 0xf;
  222. assoc = (d[1] >> 4) & 0x3;
  223. /*
  224. * We only care about a EUI-64 and NAA designator types
  225. * with LU association.
  226. */
  227. if (assoc != 0x00)
  228. continue;
  229. if (type != 0x02 && type != 0x03)
  230. continue;
  231. if (id_len != 8 && id_len != 12 && id_len != 16)
  232. continue;
  233. b->scsi.code_set = PS_CODE_SET_BINARY;
  234. b->scsi.designator_type = type == 0x02 ?
  235. PS_DESIGNATOR_EUI64 : PS_DESIGNATOR_NAA;
  236. b->scsi.designator_len = id_len;
  237. memcpy(b->scsi.designator, d + 4, id_len);
  238. /*
  239. * If we found a 8 or 12 byte descriptor continue on to
  240. * see if a 16 byte one is available. If we find a
  241. * 16 byte descriptor we're done.
  242. */
  243. if (id_len == 16)
  244. break;
  245. }
  246. out_put_request:
  247. blk_put_request(rq);
  248. out_free_buf:
  249. kfree(buf);
  250. return error;
  251. }
  252. #define NFSD_MDS_PR_KEY 0x0100000000000000
  253. /*
  254. * We use the client ID as a unique key for the reservations.
  255. * This allows us to easily fence a client when recalls fail.
  256. */
  257. static u64 nfsd4_scsi_pr_key(struct nfs4_client *clp)
  258. {
  259. return ((u64)clp->cl_clientid.cl_boot << 32) | clp->cl_clientid.cl_id;
  260. }
  261. static int
  262. nfsd4_block_get_device_info_scsi(struct super_block *sb,
  263. struct nfs4_client *clp,
  264. struct nfsd4_getdeviceinfo *gdp)
  265. {
  266. struct pnfs_block_deviceaddr *dev;
  267. struct pnfs_block_volume *b;
  268. const struct pr_ops *ops;
  269. int error;
  270. dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
  271. sizeof(struct pnfs_block_volume), GFP_KERNEL);
  272. if (!dev)
  273. return -ENOMEM;
  274. gdp->gd_device = dev;
  275. dev->nr_volumes = 1;
  276. b = &dev->volumes[0];
  277. b->type = PNFS_BLOCK_VOLUME_SCSI;
  278. b->scsi.pr_key = nfsd4_scsi_pr_key(clp);
  279. error = nfsd4_scsi_identify_device(sb->s_bdev, b);
  280. if (error)
  281. return error;
  282. ops = sb->s_bdev->bd_disk->fops->pr_ops;
  283. if (!ops) {
  284. pr_err("pNFS: device %s does not support PRs.\n",
  285. sb->s_id);
  286. return -EINVAL;
  287. }
  288. error = ops->pr_register(sb->s_bdev, 0, NFSD_MDS_PR_KEY, true);
  289. if (error) {
  290. pr_err("pNFS: failed to register key for device %s.\n",
  291. sb->s_id);
  292. return -EINVAL;
  293. }
  294. error = ops->pr_reserve(sb->s_bdev, NFSD_MDS_PR_KEY,
  295. PR_EXCLUSIVE_ACCESS_REG_ONLY, 0);
  296. if (error) {
  297. pr_err("pNFS: failed to reserve device %s.\n",
  298. sb->s_id);
  299. return -EINVAL;
  300. }
  301. return 0;
  302. }
  303. static __be32
  304. nfsd4_scsi_proc_getdeviceinfo(struct super_block *sb,
  305. struct nfs4_client *clp,
  306. struct nfsd4_getdeviceinfo *gdp)
  307. {
  308. if (sb->s_bdev != sb->s_bdev->bd_contains)
  309. return nfserr_inval;
  310. return nfserrno(nfsd4_block_get_device_info_scsi(sb, clp, gdp));
  311. }
  312. static __be32
  313. nfsd4_scsi_proc_layoutcommit(struct inode *inode,
  314. struct nfsd4_layoutcommit *lcp)
  315. {
  316. struct iomap *iomaps;
  317. int nr_iomaps;
  318. nr_iomaps = nfsd4_scsi_decode_layoutupdate(lcp->lc_up_layout,
  319. lcp->lc_up_len, &iomaps, 1 << inode->i_blkbits);
  320. if (nr_iomaps < 0)
  321. return nfserrno(nr_iomaps);
  322. return nfsd4_block_commit_blocks(inode, lcp, iomaps, nr_iomaps);
  323. }
  324. static void
  325. nfsd4_scsi_fence_client(struct nfs4_layout_stateid *ls)
  326. {
  327. struct nfs4_client *clp = ls->ls_stid.sc_client;
  328. struct block_device *bdev = ls->ls_file->f_path.mnt->mnt_sb->s_bdev;
  329. bdev->bd_disk->fops->pr_ops->pr_preempt(bdev, NFSD_MDS_PR_KEY,
  330. nfsd4_scsi_pr_key(clp), 0, true);
  331. }
  332. const struct nfsd4_layout_ops scsi_layout_ops = {
  333. /*
  334. * Pretend that we send notification to the client. This is a blatant
  335. * lie to force recent Linux clients to cache our device IDs.
  336. * We rarely ever change the device ID, so the harm of leaking deviceids
  337. * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
  338. * in this regard, but I filed errata 4119 for this a while ago, and
  339. * hopefully the Linux client will eventually start caching deviceids
  340. * without this again.
  341. */
  342. .notify_types =
  343. NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
  344. .proc_getdeviceinfo = nfsd4_scsi_proc_getdeviceinfo,
  345. .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
  346. .proc_layoutget = nfsd4_block_proc_layoutget,
  347. .encode_layoutget = nfsd4_block_encode_layoutget,
  348. .proc_layoutcommit = nfsd4_scsi_proc_layoutcommit,
  349. .fence_client = nfsd4_scsi_fence_client,
  350. };
  351. #endif /* CONFIG_NFSD_SCSILAYOUT */