xfs_pnfs.c 7.5 KB

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