blocklayout.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 = (1 << inode->i_blkbits);
  44. struct pnfs_block_extent *bex;
  45. struct iomap iomap;
  46. u32 device_generation = 0;
  47. int error;
  48. /*
  49. * We do not attempt to support I/O smaller than the fs block size,
  50. * or not aligned to it.
  51. */
  52. if (args->lg_minlength < block_size) {
  53. dprintk("pnfsd: I/O too small\n");
  54. goto out_layoutunavailable;
  55. }
  56. if (seg->offset & (block_size - 1)) {
  57. dprintk("pnfsd: I/O misaligned\n");
  58. goto out_layoutunavailable;
  59. }
  60. /*
  61. * Some clients barf on non-zero block numbers for NONE or INVALID
  62. * layouts, so make sure to zero the whole structure.
  63. */
  64. error = -ENOMEM;
  65. bex = kzalloc(sizeof(*bex), GFP_KERNEL);
  66. if (!bex)
  67. goto out_error;
  68. args->lg_content = bex;
  69. error = sb->s_export_op->map_blocks(inode, seg->offset, seg->length,
  70. &iomap, seg->iomode != IOMODE_READ,
  71. &device_generation);
  72. if (error) {
  73. if (error == -ENXIO)
  74. goto out_layoutunavailable;
  75. goto out_error;
  76. }
  77. if (iomap.length < args->lg_minlength) {
  78. dprintk("pnfsd: extent smaller than minlength\n");
  79. goto out_layoutunavailable;
  80. }
  81. switch (iomap.type) {
  82. case IOMAP_MAPPED:
  83. if (seg->iomode == IOMODE_READ)
  84. bex->es = PNFS_BLOCK_READ_DATA;
  85. else
  86. bex->es = PNFS_BLOCK_READWRITE_DATA;
  87. bex->soff = (iomap.blkno << 9);
  88. break;
  89. case IOMAP_UNWRITTEN:
  90. if (seg->iomode & IOMODE_RW) {
  91. /*
  92. * Crack monkey special case from section 2.3.1.
  93. */
  94. if (args->lg_minlength == 0) {
  95. dprintk("pnfsd: no soup for you!\n");
  96. goto out_layoutunavailable;
  97. }
  98. bex->es = PNFS_BLOCK_INVALID_DATA;
  99. bex->soff = (iomap.blkno << 9);
  100. break;
  101. }
  102. /*FALLTHRU*/
  103. case IOMAP_HOLE:
  104. if (seg->iomode == IOMODE_READ) {
  105. bex->es = PNFS_BLOCK_NONE_DATA;
  106. break;
  107. }
  108. /*FALLTHRU*/
  109. case IOMAP_DELALLOC:
  110. default:
  111. WARN(1, "pnfsd: filesystem returned %d extent\n", iomap.type);
  112. goto out_layoutunavailable;
  113. }
  114. error = nfsd4_set_deviceid(&bex->vol_id, fhp, device_generation);
  115. if (error)
  116. goto out_error;
  117. bex->foff = iomap.offset;
  118. bex->len = iomap.length;
  119. seg->offset = iomap.offset;
  120. seg->length = iomap.length;
  121. dprintk("GET: 0x%llx:0x%llx %d\n", bex->foff, bex->len, bex->es);
  122. return 0;
  123. out_error:
  124. seg->length = 0;
  125. return nfserrno(error);
  126. out_layoutunavailable:
  127. seg->length = 0;
  128. return nfserr_layoutunavailable;
  129. }
  130. static __be32
  131. nfsd4_block_proc_layoutcommit(struct inode *inode,
  132. struct nfsd4_layoutcommit *lcp)
  133. {
  134. loff_t new_size = lcp->lc_last_wr + 1;
  135. struct iattr iattr = { .ia_valid = 0 };
  136. struct iomap *iomaps;
  137. int nr_iomaps;
  138. int error;
  139. nr_iomaps = nfsd4_block_decode_layoutupdate(lcp->lc_up_layout,
  140. lcp->lc_up_len, &iomaps, 1 << inode->i_blkbits);
  141. if (nr_iomaps < 0)
  142. return nfserrno(nr_iomaps);
  143. if (lcp->lc_mtime.tv_nsec == UTIME_NOW ||
  144. timespec_compare(&lcp->lc_mtime, &inode->i_mtime) < 0)
  145. lcp->lc_mtime = current_fs_time(inode->i_sb);
  146. iattr.ia_valid |= ATTR_ATIME | ATTR_CTIME | ATTR_MTIME;
  147. iattr.ia_atime = iattr.ia_ctime = iattr.ia_mtime = lcp->lc_mtime;
  148. if (new_size > i_size_read(inode)) {
  149. iattr.ia_valid |= ATTR_SIZE;
  150. iattr.ia_size = new_size;
  151. }
  152. error = inode->i_sb->s_export_op->commit_blocks(inode, iomaps,
  153. nr_iomaps, &iattr);
  154. kfree(iomaps);
  155. return nfserrno(error);
  156. }
  157. const struct nfsd4_layout_ops bl_layout_ops = {
  158. /*
  159. * Pretend that we send notification to the client. This is a blatant
  160. * lie to force recent Linux clients to cache our device IDs.
  161. * We rarely ever change the device ID, so the harm of leaking deviceids
  162. * for a while isn't too bad. Unfortunately RFC5661 is a complete mess
  163. * in this regard, but I filed errata 4119 for this a while ago, and
  164. * hopefully the Linux client will eventually start caching deviceids
  165. * without this again.
  166. */
  167. .notify_types =
  168. NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
  169. .proc_getdeviceinfo = nfsd4_block_proc_getdeviceinfo,
  170. .encode_getdeviceinfo = nfsd4_block_encode_getdeviceinfo,
  171. .proc_layoutget = nfsd4_block_proc_layoutget,
  172. .encode_layoutget = nfsd4_block_encode_layoutget,
  173. .proc_layoutcommit = nfsd4_block_proc_layoutcommit,
  174. };