xfs_pnfs.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2014 Christoph Hellwig.
  3. */
  4. #include "xfs.h"
  5. #include "xfs_format.h"
  6. #include "xfs_log_format.h"
  7. #include "xfs_trans_resv.h"
  8. #include "xfs_sb.h"
  9. #include "xfs_mount.h"
  10. #include "xfs_inode.h"
  11. #include "xfs_trans.h"
  12. #include "xfs_log.h"
  13. #include "xfs_bmap.h"
  14. #include "xfs_bmap_util.h"
  15. #include "xfs_error.h"
  16. #include "xfs_iomap.h"
  17. #include "xfs_shared.h"
  18. #include "xfs_bit.h"
  19. #include "xfs_pnfs.h"
  20. /*
  21. * Ensure that we do not have any outstanding pNFS layouts that can be used by
  22. * clients to directly read from or write to this inode. This must be called
  23. * before every operation that can remove blocks from the extent map.
  24. * Additionally we call it during the write operation, where aren't concerned
  25. * about exposing unallocated blocks but just want to provide basic
  26. * synchronization between a local writer and pNFS clients. mmap writes would
  27. * also benefit from this sort of synchronization, but due to the tricky locking
  28. * rules in the page fault path we don't bother.
  29. */
  30. int
  31. xfs_break_layouts(
  32. struct inode *inode,
  33. uint *iolock)
  34. {
  35. struct xfs_inode *ip = XFS_I(inode);
  36. int error;
  37. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL));
  38. while ((error = break_layout(inode, false) == -EWOULDBLOCK)) {
  39. xfs_iunlock(ip, *iolock);
  40. error = break_layout(inode, true);
  41. *iolock = XFS_IOLOCK_EXCL;
  42. xfs_ilock(ip, *iolock);
  43. }
  44. return error;
  45. }
  46. /*
  47. * Get a unique ID including its location so that the client can identify
  48. * the exported device.
  49. */
  50. int
  51. xfs_fs_get_uuid(
  52. struct super_block *sb,
  53. u8 *buf,
  54. u32 *len,
  55. u64 *offset)
  56. {
  57. struct xfs_mount *mp = XFS_M(sb);
  58. printk_once(KERN_NOTICE
  59. "XFS (%s): using experimental pNFS feature, use at your own risk!\n",
  60. mp->m_fsname);
  61. if (*len < sizeof(uuid_t))
  62. return -EINVAL;
  63. memcpy(buf, &mp->m_sb.sb_uuid, sizeof(uuid_t));
  64. *len = sizeof(uuid_t);
  65. *offset = offsetof(struct xfs_dsb, sb_uuid);
  66. return 0;
  67. }
  68. static void
  69. xfs_bmbt_to_iomap(
  70. struct xfs_inode *ip,
  71. struct iomap *iomap,
  72. struct xfs_bmbt_irec *imap)
  73. {
  74. struct xfs_mount *mp = ip->i_mount;
  75. if (imap->br_startblock == HOLESTARTBLOCK) {
  76. iomap->blkno = IOMAP_NULL_BLOCK;
  77. iomap->type = IOMAP_HOLE;
  78. } else if (imap->br_startblock == DELAYSTARTBLOCK) {
  79. iomap->blkno = IOMAP_NULL_BLOCK;
  80. iomap->type = IOMAP_DELALLOC;
  81. } else {
  82. iomap->blkno =
  83. XFS_FSB_TO_DADDR(ip->i_mount, imap->br_startblock);
  84. if (imap->br_state == XFS_EXT_UNWRITTEN)
  85. iomap->type = IOMAP_UNWRITTEN;
  86. else
  87. iomap->type = IOMAP_MAPPED;
  88. }
  89. iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
  90. iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
  91. }
  92. /*
  93. * Get a layout for the pNFS client.
  94. */
  95. int
  96. xfs_fs_map_blocks(
  97. struct inode *inode,
  98. loff_t offset,
  99. u64 length,
  100. struct iomap *iomap,
  101. bool write,
  102. u32 *device_generation)
  103. {
  104. struct xfs_inode *ip = XFS_I(inode);
  105. struct xfs_mount *mp = ip->i_mount;
  106. struct xfs_bmbt_irec imap;
  107. xfs_fileoff_t offset_fsb, end_fsb;
  108. loff_t limit;
  109. int bmapi_flags = XFS_BMAPI_ENTIRE;
  110. int nimaps = 1;
  111. uint lock_flags;
  112. int error = 0;
  113. if (XFS_FORCED_SHUTDOWN(mp))
  114. return -EIO;
  115. /*
  116. * We can't export inodes residing on the realtime device. The realtime
  117. * device doesn't have a UUID to identify it, so the client has no way
  118. * to find it.
  119. */
  120. if (XFS_IS_REALTIME_INODE(ip))
  121. return -ENXIO;
  122. /*
  123. * Lock out any other I/O before we flush and invalidate the pagecache,
  124. * and then hand out a layout to the remote system. This is very
  125. * similar to direct I/O, except that the synchronization is much more
  126. * complicated. See the comment near xfs_break_layouts for a detailed
  127. * explanation.
  128. */
  129. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  130. error = -EINVAL;
  131. limit = mp->m_super->s_maxbytes;
  132. if (!write)
  133. limit = max(limit, round_up(i_size_read(inode),
  134. inode->i_sb->s_blocksize));
  135. if (offset > limit)
  136. goto out_unlock;
  137. if (offset > limit - length)
  138. length = limit - offset;
  139. error = filemap_write_and_wait(inode->i_mapping);
  140. if (error)
  141. goto out_unlock;
  142. error = invalidate_inode_pages2(inode->i_mapping);
  143. if (WARN_ON_ONCE(error))
  144. return error;
  145. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + length);
  146. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  147. lock_flags = xfs_ilock_data_map_shared(ip);
  148. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
  149. &imap, &nimaps, bmapi_flags);
  150. xfs_iunlock(ip, lock_flags);
  151. if (error)
  152. goto out_unlock;
  153. if (write) {
  154. enum xfs_prealloc_flags flags = 0;
  155. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  156. if (!nimaps || imap.br_startblock == HOLESTARTBLOCK) {
  157. error = xfs_iomap_write_direct(ip, offset, length,
  158. &imap, nimaps);
  159. if (error)
  160. goto out_unlock;
  161. /*
  162. * Ensure the next transaction is committed
  163. * synchronously so that the blocks allocated and
  164. * handed out to the client are guaranteed to be
  165. * present even after a server crash.
  166. */
  167. flags |= XFS_PREALLOC_SET | XFS_PREALLOC_SYNC;
  168. }
  169. error = xfs_update_prealloc_flags(ip, flags);
  170. if (error)
  171. goto out_unlock;
  172. }
  173. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  174. xfs_bmbt_to_iomap(ip, iomap, &imap);
  175. *device_generation = mp->m_generation;
  176. return error;
  177. out_unlock:
  178. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  179. return error;
  180. }
  181. /*
  182. * Ensure the size update falls into a valid allocated block.
  183. */
  184. static int
  185. xfs_pnfs_validate_isize(
  186. struct xfs_inode *ip,
  187. xfs_off_t isize)
  188. {
  189. struct xfs_bmbt_irec imap;
  190. int nimaps = 1;
  191. int error = 0;
  192. xfs_ilock(ip, XFS_ILOCK_SHARED);
  193. error = xfs_bmapi_read(ip, XFS_B_TO_FSBT(ip->i_mount, isize - 1), 1,
  194. &imap, &nimaps, 0);
  195. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  196. if (error)
  197. return error;
  198. if (imap.br_startblock == HOLESTARTBLOCK ||
  199. imap.br_startblock == DELAYSTARTBLOCK ||
  200. imap.br_state == XFS_EXT_UNWRITTEN)
  201. return -EIO;
  202. return 0;
  203. }
  204. /*
  205. * Make sure the blocks described by maps are stable on disk. This includes
  206. * converting any unwritten extents, flushing the disk cache and updating the
  207. * time stamps.
  208. *
  209. * Note that we rely on the caller to always send us a timestamp update so that
  210. * we always commit a transaction here. If that stops being true we will have
  211. * to manually flush the cache here similar to what the fsync code path does
  212. * for datasyncs on files that have no dirty metadata.
  213. */
  214. int
  215. xfs_fs_commit_blocks(
  216. struct inode *inode,
  217. struct iomap *maps,
  218. int nr_maps,
  219. struct iattr *iattr)
  220. {
  221. struct xfs_inode *ip = XFS_I(inode);
  222. struct xfs_mount *mp = ip->i_mount;
  223. struct xfs_trans *tp;
  224. bool update_isize = false;
  225. int error, i;
  226. loff_t size;
  227. ASSERT(iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME));
  228. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  229. size = i_size_read(inode);
  230. if ((iattr->ia_valid & ATTR_SIZE) && iattr->ia_size > size) {
  231. update_isize = true;
  232. size = iattr->ia_size;
  233. }
  234. for (i = 0; i < nr_maps; i++) {
  235. u64 start, length, end;
  236. start = maps[i].offset;
  237. if (start > size)
  238. continue;
  239. end = start + maps[i].length;
  240. if (end > size)
  241. end = size;
  242. length = end - start;
  243. if (!length)
  244. continue;
  245. /*
  246. * Make sure reads through the pagecache see the new data.
  247. */
  248. error = invalidate_inode_pages2_range(inode->i_mapping,
  249. start >> PAGE_CACHE_SHIFT,
  250. (end - 1) >> PAGE_CACHE_SHIFT);
  251. WARN_ON_ONCE(error);
  252. error = xfs_iomap_write_unwritten(ip, start, length);
  253. if (error)
  254. goto out_drop_iolock;
  255. }
  256. if (update_isize) {
  257. error = xfs_pnfs_validate_isize(ip, size);
  258. if (error)
  259. goto out_drop_iolock;
  260. }
  261. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  262. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
  263. if (error) {
  264. xfs_trans_cancel(tp, 0);
  265. goto out_drop_iolock;
  266. }
  267. xfs_ilock(ip, XFS_ILOCK_EXCL);
  268. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  269. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  270. xfs_setattr_time(ip, iattr);
  271. if (update_isize) {
  272. i_size_write(inode, iattr->ia_size);
  273. ip->i_d.di_size = iattr->ia_size;
  274. }
  275. xfs_trans_set_sync(tp);
  276. error = xfs_trans_commit(tp, 0);
  277. out_drop_iolock:
  278. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  279. return error;
  280. }