xfs_pnfs.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. * We cannot use file based VFS helpers such as file_modified() to update
  67. * inode state as we modify the data/metadata in the inode here. Hence we have
  68. * to open code the timestamp updates and SUID/SGID stripping. We also need
  69. * to set the inode prealloc flag to ensure that the extents we allocate are not
  70. * removed if the inode is reclaimed from memory before xfs_fs_block_commit()
  71. * is from the client to indicate that data has been written and the file size
  72. * can be extended.
  73. */
  74. static int
  75. xfs_fs_map_update_inode(
  76. struct xfs_inode *ip)
  77. {
  78. struct xfs_trans *tp;
  79. int error;
  80. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_writeid,
  81. 0, 0, 0, &tp);
  82. if (error)
  83. return error;
  84. xfs_ilock(ip, XFS_ILOCK_EXCL);
  85. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  86. VFS_I(ip)->i_mode &= ~S_ISUID;
  87. if (VFS_I(ip)->i_mode & S_IXGRP)
  88. VFS_I(ip)->i_mode &= ~S_ISGID;
  89. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  90. ip->i_diflags |= XFS_DIFLAG_PREALLOC;
  91. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  92. return xfs_trans_commit(tp);
  93. }
  94. /*
  95. * Get a layout for the pNFS client.
  96. */
  97. int
  98. xfs_fs_map_blocks(
  99. struct inode *inode,
  100. loff_t offset,
  101. u64 length,
  102. struct iomap *iomap,
  103. bool write,
  104. u32 *device_generation)
  105. {
  106. struct xfs_inode *ip = XFS_I(inode);
  107. struct xfs_mount *mp = ip->i_mount;
  108. struct xfs_bmbt_irec imap;
  109. xfs_fileoff_t offset_fsb, end_fsb;
  110. loff_t limit;
  111. int bmapi_flags = XFS_BMAPI_ENTIRE;
  112. int nimaps = 1;
  113. uint lock_flags;
  114. int error = 0;
  115. if (xfs_is_shutdown(mp))
  116. return -EIO;
  117. /*
  118. * We can't export inodes residing on the realtime device. The realtime
  119. * device doesn't have a UUID to identify it, so the client has no way
  120. * to find it.
  121. */
  122. if (XFS_IS_REALTIME_INODE(ip))
  123. return -ENXIO;
  124. /*
  125. * The pNFS block layout spec actually supports reflink like
  126. * functionality, but the Linux pNFS server doesn't implement it yet.
  127. */
  128. if (xfs_is_reflink_inode(ip))
  129. return -ENXIO;
  130. /*
  131. * Lock out any other I/O before we flush and invalidate the pagecache,
  132. * and then hand out a layout to the remote system. This is very
  133. * similar to direct I/O, except that the synchronization is much more
  134. * complicated. See the comment near xfs_break_leased_layouts
  135. * for a detailed explanation.
  136. */
  137. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  138. error = -EINVAL;
  139. limit = mp->m_super->s_maxbytes;
  140. if (!write)
  141. limit = max(limit, round_up(i_size_read(inode),
  142. inode->i_sb->s_blocksize));
  143. if (offset > limit)
  144. goto out_unlock;
  145. if (offset > limit - length)
  146. length = limit - offset;
  147. error = filemap_write_and_wait(inode->i_mapping);
  148. if (error)
  149. goto out_unlock;
  150. error = invalidate_inode_pages2(inode->i_mapping);
  151. if (WARN_ON_ONCE(error))
  152. goto out_unlock;
  153. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + length);
  154. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  155. lock_flags = xfs_ilock_data_map_shared(ip);
  156. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
  157. &imap, &nimaps, bmapi_flags);
  158. ASSERT(!nimaps || imap.br_startblock != DELAYSTARTBLOCK);
  159. if (!error && write &&
  160. (!nimaps || imap.br_startblock == HOLESTARTBLOCK)) {
  161. if (offset + length > XFS_ISIZE(ip))
  162. end_fsb = xfs_iomap_eof_align_last_fsb(ip, end_fsb);
  163. else if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
  164. end_fsb = min(end_fsb, imap.br_startoff +
  165. imap.br_blockcount);
  166. xfs_iunlock(ip, lock_flags);
  167. error = xfs_iomap_write_direct(ip, offset_fsb,
  168. end_fsb - offset_fsb, 0, &imap);
  169. if (error)
  170. goto out_unlock;
  171. /*
  172. * Ensure the next transaction is committed synchronously so
  173. * that the blocks allocated and handed out to the client are
  174. * guaranteed to be present even after a server crash.
  175. */
  176. error = xfs_fs_map_update_inode(ip);
  177. if (!error)
  178. error = xfs_log_force_inode(ip);
  179. if (error)
  180. goto out_unlock;
  181. } else {
  182. xfs_iunlock(ip, lock_flags);
  183. }
  184. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  185. error = xfs_bmbt_to_iomap(ip, iomap, &imap, 0, 0);
  186. *device_generation = mp->m_generation;
  187. return error;
  188. out_unlock:
  189. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  190. return error;
  191. }
  192. /*
  193. * Ensure the size update falls into a valid allocated block.
  194. */
  195. static int
  196. xfs_pnfs_validate_isize(
  197. struct xfs_inode *ip,
  198. xfs_off_t isize)
  199. {
  200. struct xfs_bmbt_irec imap;
  201. int nimaps = 1;
  202. int error = 0;
  203. xfs_ilock(ip, XFS_ILOCK_SHARED);
  204. error = xfs_bmapi_read(ip, XFS_B_TO_FSBT(ip->i_mount, isize - 1), 1,
  205. &imap, &nimaps, 0);
  206. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  207. if (error)
  208. return error;
  209. if (imap.br_startblock == HOLESTARTBLOCK ||
  210. imap.br_startblock == DELAYSTARTBLOCK ||
  211. imap.br_state == XFS_EXT_UNWRITTEN)
  212. return -EIO;
  213. return 0;
  214. }
  215. /*
  216. * Make sure the blocks described by maps are stable on disk. This includes
  217. * converting any unwritten extents, flushing the disk cache and updating the
  218. * time stamps.
  219. *
  220. * Note that we rely on the caller to always send us a timestamp update so that
  221. * we always commit a transaction here. If that stops being true we will have
  222. * to manually flush the cache here similar to what the fsync code path does
  223. * for datasyncs on files that have no dirty metadata.
  224. */
  225. int
  226. xfs_fs_commit_blocks(
  227. struct inode *inode,
  228. struct iomap *maps,
  229. int nr_maps,
  230. struct iattr *iattr)
  231. {
  232. struct xfs_inode *ip = XFS_I(inode);
  233. struct xfs_mount *mp = ip->i_mount;
  234. struct xfs_trans *tp;
  235. bool update_isize = false;
  236. int error, i;
  237. loff_t size;
  238. ASSERT(iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME));
  239. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  240. size = i_size_read(inode);
  241. if ((iattr->ia_valid & ATTR_SIZE) && iattr->ia_size > size) {
  242. update_isize = true;
  243. size = iattr->ia_size;
  244. }
  245. for (i = 0; i < nr_maps; i++) {
  246. u64 start, length, end;
  247. start = maps[i].offset;
  248. if (start > size)
  249. continue;
  250. end = start + maps[i].length;
  251. if (end > size)
  252. end = size;
  253. length = end - start;
  254. if (!length)
  255. continue;
  256. /*
  257. * Make sure reads through the pagecache see the new data.
  258. */
  259. error = invalidate_inode_pages2_range(inode->i_mapping,
  260. start >> PAGE_SHIFT,
  261. (end - 1) >> PAGE_SHIFT);
  262. WARN_ON_ONCE(error);
  263. error = xfs_iomap_write_unwritten(ip, start, length, false);
  264. if (error)
  265. goto out_drop_iolock;
  266. }
  267. if (update_isize) {
  268. error = xfs_pnfs_validate_isize(ip, size);
  269. if (error)
  270. goto out_drop_iolock;
  271. }
  272. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  273. if (error)
  274. goto out_drop_iolock;
  275. xfs_ilock(ip, XFS_ILOCK_EXCL);
  276. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  277. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  278. ASSERT(!(iattr->ia_valid & (ATTR_UID | ATTR_GID)));
  279. setattr_copy(&init_user_ns, inode, iattr);
  280. if (update_isize) {
  281. i_size_write(inode, iattr->ia_size);
  282. ip->i_disk_size = iattr->ia_size;
  283. }
  284. xfs_trans_set_sync(tp);
  285. error = xfs_trans_commit(tp);
  286. out_drop_iolock:
  287. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  288. return error;
  289. }