xfs_pnfs.c 7.4 KB

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