xfs_pnfs.c 8.1 KB

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