xfs_pnfs.c 7.4 KB

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