blocklayout.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2014 Christoph Hellwig.
  3. */
  4. #include <linux/exportfs.h>
  5. #include <linux/genhd.h>
  6. #include <linux/slab.h>
  7. #include <linux/nfsd/debug.h>
  8. #include "blocklayoutxdr.h"
  9. #include "pnfs.h"
  10. #define NFSDDBG_FACILITY NFSDDBG_PNFS
  11. static int
  12. nfsd4_block_get_device_info_simple(struct super_block *sb,
  13. struct nfsd4_getdeviceinfo *gdp)
  14. {
  15. struct pnfs_block_deviceaddr *dev;
  16. struct pnfs_block_volume *b;
  17. dev = kzalloc(sizeof(struct pnfs_block_deviceaddr) +
  18. sizeof(struct pnfs_block_volume), GFP_KERNEL);
  19. if (!dev)
  20. return -ENOMEM;
  21. gdp->gd_device = dev;
  22. dev->nr_volumes = 1;
  23. b = &dev->volumes[0];
  24. b->type = PNFS_BLOCK_VOLUME_SIMPLE;
  25. b->simple.sig_len = PNFS_BLOCK_UUID_LEN;
  26. return sb->s_export_op->get_uuid(sb, b->simple.sig, &b->simple.sig_len,
  27. &b->simple.offset);
  28. }
  29. static __be32
  30. nfsd4_block_proc_getdeviceinfo(struct super_block *sb,
  31. struct nfsd4_getdeviceinfo *gdp)
  32. {
  33. if (sb->s_bdev != sb->s_bdev->bd_contains)
  34. return nfserr_inval;
  35. return nfserrno(nfsd4_block_get_device_info_simple(sb, gdp));
  36. }
  37. static __be32
  38. nfsd4_block_proc_layoutget(struct inode *inode, const struct svc_fh *fhp,
  39. struct nfsd4_layoutget *args)
  40. {
  41. struct nfsd4_layout_seg *seg = &args->lg_seg;
  42. struct super_block *sb = inode->i_sb;
  43. u32 block_size = i_blocksize(inode);
  44. struct pnfs_block_extent *bex;
  45. struct iomap iomap;
  46. u32 device_generation = 0;
  47. int error;
  48. if (seg->offset & (block_size - 1)) {
  49. dprintk("pnfsd: I/O misaligned\n");
  50. goto out_layoutunavailable;
  51. }
  52. /*
  53. * Some clients barf on non-zero block numbers for NONE or INVALID
  54. * layouts, so make sure to zero the whole structure.
  55. */
  56. error = -ENOMEM;
  57. bex = kzalloc(sizeof(*bex), GFP_KERNEL);
  58. if (!bex)
  59. goto out_error;
  60. args->lg_content = bex;
  61. error = sb->s_export_op->map_blocks(inode, seg->offset, seg->length,
  62. &iomap, seg->iomode != IOMODE_READ,
  63. &device_generation);
  64. if (error) {
  65. if (error == -ENXIO)
  66. goto out_layoutunavailable;
  67. goto out_error;
  68. }
  69. if (iomap.length < args->lg_minlength) {
  70. dprintk("pnfsd: extent smaller than minlength\n");
  71. goto out_layoutunavailable;
  72. }
  73. switch (iomap.type) {
  74. case IOMAP_MAPPED:
  75. if (seg->iomode == IOMODE_READ)
  76. bex->es = PNFS_BLOCK_READ_DATA;
  77. else
  78. bex->es = PNFS_BLOCK_READWRITE_DATA;
  79. bex->soff = (iomap.blkno << 9);
  80. break;
  81. case IOMAP_UNWRITTEN:
  82. if (seg->iomode & IOMODE_RW) {
  83. /*
  84. * Crack monkey special case from section 2.3.1.
  85. */
  86. if (args->lg_minlength == 0) {
  87. dprintk("pnfsd: no soup for you!\n");
  88. goto out_layoutunavailable;
  89. }
  90. bex->es = PNFS_BLOCK_INVALID_DATA;
  91. bex->soff = (iomap.blkno << 9);
  92. break;
  93. }
  94. /*FALLTHRU*/
  95. case IOMAP_HOLE:
  96. if (seg->iomode == IOMODE_READ) {
  97. bex->es = PNFS_BLOCK_NONE_DATA;
  98. break;
  99. }
  100. /*FALLTHRU*/
  101. case IOMAP_DELALLOC:
  102. default:
  103. WARN(1, "pnfsd: filesystem returned %d extent\n", iomap.type);
  104. goto out_layoutunavailable;
  105. }
  106. error = nfsd4_set_deviceid(&bex->vol_id, fhp, device_generation);
  107. if (error)
  108. goto out_error;
  109. bex->foff = iomap.offset;
  110. bex->len = iomap.length;
  111. seg->offset = iomap.offset;
  112. seg->length = iomap.length;
  113. dprintk("GET: 0x%llx:0x%llx %d\n", bex->foff, bex->len, bex->es);
  114. return 0;
  115. out_error:
  116. seg->length = 0;
  117. return nfserrno(error);
  118. out_layoutunavailable:
  119. seg->length = 0;
  120. return nfserr_layoutunavailable;
  121. }
  122. static __be32
  123. nfsd4_block_proc_layoutcommit(struct inode *inode,
  124. struct nfsd4_layoutcommit *lcp)
  125. {
  126. loff_t new_size = lcp->lc_last_wr + 1;
  127. struct iattr iattr = { .ia_valid = 0 };
  128. struct iomap *iomaps;
  129. int nr_iomaps;
  130. int error;
  131. nr_iomaps = nfsd4_block_decode_layoutupdate(lcp->lc_up_layout,
  132. lcp->lc_up_len, &iomaps, i_blocksize(inode));
  133. if (nr_iomaps < 0)
  134. return nfserrno(nr_iomaps);
  135. if (lcp->lc_mtime.tv_nsec == UTIME_NOW ||
  136. timespec_compare(&lcp->lc_mtime, &inode->i_mtime) < 0)
  137. lcp->lc_mtime = current_fs_time(inode->i_sb);
  138. iattr.ia_valid |= ATTR_ATIME | ATTR_CTIME | ATTR_MTIME;
  139. iattr.ia_atime = iattr.ia_ctime = iattr.ia_mtime = lcp->lc_mtime;
  140. if (new_size > i_size_read(inode)) {
  141. iattr.ia_valid |= ATTR_SIZE;
  142. iattr.ia_size = new_size;
  143. }
  144. error = inode->i_sb->s_export_op->commit_blocks(inode, iomaps,
  145. nr_iomaps, &iattr);
  146. kfree(iomaps);
  147. return nfserrno(error);
  148. }
  149. const struct nfsd4_layout_ops bl_layout_ops = {
  150. /*
  151. * Pretend that we send notification to the client. This is a blatant
  152. * lie to force recent Linux clients to cache our device IDs.
  153. * We rarely ever change the device ID, so the harm of leaking deviceids
  154. * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
  155. * in this regard, but I filed errata 4119 for this a while ago, and
  156. * hopefully the Linux client will eventually start caching deviceids
  157. * without this again.
  158. */
  159. .notify_types =
  160. NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
  161. .proc_getdeviceinfo = nfsd4_block_proc_getdeviceinfo,
  162. .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
  163. .proc_layoutget = nfsd4_block_proc_layoutget,
  164. .encode_layoutget = nfsd4_block_encode_layoutget,
  165. .proc_layoutcommit = nfsd4_block_proc_layoutcommit,
  166. };