xfs_pnfs.c 7.3 KB

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