xfs_pnfs.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. * The pNFS block layout spec actually supports reflink like
  106. * functionality, but the Linux pNFS server doesn't implement it yet.
  107. */
  108. if (xfs_is_reflink_inode(ip))
  109. return -ENXIO;
  110. /*
  111. * Lock out any other I/O before we flush and invalidate the pagecache,
  112. * and then hand out a layout to the remote system. This is very
  113. * similar to direct I/O, except that the synchronization is much more
  114. * complicated. See the comment near xfs_break_layouts for a detailed
  115. * explanation.
  116. */
  117. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  118. error = -EINVAL;
  119. limit = mp->m_super->s_maxbytes;
  120. if (!write)
  121. limit = max(limit, round_up(i_size_read(inode),
  122. inode->i_sb->s_blocksize));
  123. if (offset > limit)
  124. goto out_unlock;
  125. if (offset > limit - length)
  126. length = limit - offset;
  127. error = filemap_write_and_wait(inode->i_mapping);
  128. if (error)
  129. goto out_unlock;
  130. error = invalidate_inode_pages2(inode->i_mapping);
  131. if (WARN_ON_ONCE(error))
  132. return error;
  133. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + length);
  134. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  135. lock_flags = xfs_ilock_data_map_shared(ip);
  136. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
  137. &imap, &nimaps, bmapi_flags);
  138. xfs_iunlock(ip, lock_flags);
  139. if (error)
  140. goto out_unlock;
  141. if (write) {
  142. enum xfs_prealloc_flags flags = 0;
  143. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  144. if (!nimaps || imap.br_startblock == HOLESTARTBLOCK) {
  145. /*
  146. * xfs_iomap_write_direct() expects to take ownership of
  147. * the shared ilock.
  148. */
  149. xfs_ilock(ip, XFS_ILOCK_SHARED);
  150. error = xfs_iomap_write_direct(ip, offset, length,
  151. &imap, nimaps);
  152. if (error)
  153. goto out_unlock;
  154. /*
  155. * Ensure the next transaction is committed
  156. * synchronously so that the blocks allocated and
  157. * handed out to the client are guaranteed to be
  158. * present even after a server crash.
  159. */
  160. flags |= XFS_PREALLOC_SET | XFS_PREALLOC_SYNC;
  161. }
  162. error = xfs_update_prealloc_flags(ip, flags);
  163. if (error)
  164. goto out_unlock;
  165. }
  166. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  167. xfs_bmbt_to_iomap(ip, iomap, &imap);
  168. *device_generation = mp->m_generation;
  169. return error;
  170. out_unlock:
  171. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  172. return error;
  173. }
  174. /*
  175. * Ensure the size update falls into a valid allocated block.
  176. */
  177. static int
  178. xfs_pnfs_validate_isize(
  179. struct xfs_inode *ip,
  180. xfs_off_t isize)
  181. {
  182. struct xfs_bmbt_irec imap;
  183. int nimaps = 1;
  184. int error = 0;
  185. xfs_ilock(ip, XFS_ILOCK_SHARED);
  186. error = xfs_bmapi_read(ip, XFS_B_TO_FSBT(ip->i_mount, isize - 1), 1,
  187. &imap, &nimaps, 0);
  188. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  189. if (error)
  190. return error;
  191. if (imap.br_startblock == HOLESTARTBLOCK ||
  192. imap.br_startblock == DELAYSTARTBLOCK ||
  193. imap.br_state == XFS_EXT_UNWRITTEN)
  194. return -EIO;
  195. return 0;
  196. }
  197. /*
  198. * Make sure the blocks described by maps are stable on disk. This includes
  199. * converting any unwritten extents, flushing the disk cache and updating the
  200. * time stamps.
  201. *
  202. * Note that we rely on the caller to always send us a timestamp update so that
  203. * we always commit a transaction here. If that stops being true we will have
  204. * to manually flush the cache here similar to what the fsync code path does
  205. * for datasyncs on files that have no dirty metadata.
  206. */
  207. int
  208. xfs_fs_commit_blocks(
  209. struct inode *inode,
  210. struct iomap *maps,
  211. int nr_maps,
  212. struct iattr *iattr)
  213. {
  214. struct xfs_inode *ip = XFS_I(inode);
  215. struct xfs_mount *mp = ip->i_mount;
  216. struct xfs_trans *tp;
  217. bool update_isize = false;
  218. int error, i;
  219. loff_t size;
  220. ASSERT(iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME));
  221. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  222. size = i_size_read(inode);
  223. if ((iattr->ia_valid & ATTR_SIZE) && iattr->ia_size > size) {
  224. update_isize = true;
  225. size = iattr->ia_size;
  226. }
  227. for (i = 0; i < nr_maps; i++) {
  228. u64 start, length, end;
  229. start = maps[i].offset;
  230. if (start > size)
  231. continue;
  232. end = start + maps[i].length;
  233. if (end > size)
  234. end = size;
  235. length = end - start;
  236. if (!length)
  237. continue;
  238. /*
  239. * Make sure reads through the pagecache see the new data.
  240. */
  241. error = invalidate_inode_pages2_range(inode->i_mapping,
  242. start >> PAGE_SHIFT,
  243. (end - 1) >> PAGE_SHIFT);
  244. WARN_ON_ONCE(error);
  245. error = xfs_iomap_write_unwritten(ip, start, length, false);
  246. if (error)
  247. goto out_drop_iolock;
  248. }
  249. if (update_isize) {
  250. error = xfs_pnfs_validate_isize(ip, size);
  251. if (error)
  252. goto out_drop_iolock;
  253. }
  254. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  255. if (error)
  256. goto out_drop_iolock;
  257. xfs_ilock(ip, XFS_ILOCK_EXCL);
  258. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  259. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  260. xfs_setattr_time(ip, iattr);
  261. if (update_isize) {
  262. i_size_write(inode, iattr->ia_size);
  263. ip->i_d.di_size = iattr->ia_size;
  264. }
  265. xfs_trans_set_sync(tp);
  266. error = xfs_trans_commit(tp);
  267. out_drop_iolock:
  268. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  269. return error;
  270. }