xfs_pnfs.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. error = xfs_iomap_write_direct(ip, offset, length,
  163. &imap, nimaps);
  164. if (error)
  165. goto out_unlock;
  166. /*
  167. * Ensure the next transaction is committed
  168. * synchronously so that the blocks allocated and
  169. * handed out to the client are guaranteed to be
  170. * present even after a server crash.
  171. */
  172. flags |= XFS_PREALLOC_SET | XFS_PREALLOC_SYNC;
  173. }
  174. error = xfs_update_prealloc_flags(ip, flags);
  175. if (error)
  176. goto out_unlock;
  177. }
  178. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  179. xfs_bmbt_to_iomap(ip, iomap, &imap);
  180. *device_generation = mp->m_generation;
  181. return error;
  182. out_unlock:
  183. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  184. return error;
  185. }
  186. /*
  187. * Ensure the size update falls into a valid allocated block.
  188. */
  189. static int
  190. xfs_pnfs_validate_isize(
  191. struct xfs_inode *ip,
  192. xfs_off_t isize)
  193. {
  194. struct xfs_bmbt_irec imap;
  195. int nimaps = 1;
  196. int error = 0;
  197. xfs_ilock(ip, XFS_ILOCK_SHARED);
  198. error = xfs_bmapi_read(ip, XFS_B_TO_FSBT(ip->i_mount, isize - 1), 1,
  199. &imap, &nimaps, 0);
  200. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  201. if (error)
  202. return error;
  203. if (imap.br_startblock == HOLESTARTBLOCK ||
  204. imap.br_startblock == DELAYSTARTBLOCK ||
  205. imap.br_state == XFS_EXT_UNWRITTEN)
  206. return -EIO;
  207. return 0;
  208. }
  209. /*
  210. * Make sure the blocks described by maps are stable on disk. This includes
  211. * converting any unwritten extents, flushing the disk cache and updating the
  212. * time stamps.
  213. *
  214. * Note that we rely on the caller to always send us a timestamp update so that
  215. * we always commit a transaction here. If that stops being true we will have
  216. * to manually flush the cache here similar to what the fsync code path does
  217. * for datasyncs on files that have no dirty metadata.
  218. */
  219. int
  220. xfs_fs_commit_blocks(
  221. struct inode *inode,
  222. struct iomap *maps,
  223. int nr_maps,
  224. struct iattr *iattr)
  225. {
  226. struct xfs_inode *ip = XFS_I(inode);
  227. struct xfs_mount *mp = ip->i_mount;
  228. struct xfs_trans *tp;
  229. bool update_isize = false;
  230. int error, i;
  231. loff_t size;
  232. ASSERT(iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME));
  233. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  234. size = i_size_read(inode);
  235. if ((iattr->ia_valid & ATTR_SIZE) && iattr->ia_size > size) {
  236. update_isize = true;
  237. size = iattr->ia_size;
  238. }
  239. for (i = 0; i < nr_maps; i++) {
  240. u64 start, length, end;
  241. start = maps[i].offset;
  242. if (start > size)
  243. continue;
  244. end = start + maps[i].length;
  245. if (end > size)
  246. end = size;
  247. length = end - start;
  248. if (!length)
  249. continue;
  250. /*
  251. * Make sure reads through the pagecache see the new data.
  252. */
  253. error = invalidate_inode_pages2_range(inode->i_mapping,
  254. start >> PAGE_CACHE_SHIFT,
  255. (end - 1) >> PAGE_CACHE_SHIFT);
  256. WARN_ON_ONCE(error);
  257. error = xfs_iomap_write_unwritten(ip, start, length);
  258. if (error)
  259. goto out_drop_iolock;
  260. }
  261. if (update_isize) {
  262. error = xfs_pnfs_validate_isize(ip, size);
  263. if (error)
  264. goto out_drop_iolock;
  265. }
  266. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  267. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
  268. if (error) {
  269. xfs_trans_cancel(tp);
  270. goto out_drop_iolock;
  271. }
  272. xfs_ilock(ip, XFS_ILOCK_EXCL);
  273. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  274. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  275. xfs_setattr_time(ip, iattr);
  276. if (update_isize) {
  277. i_size_write(inode, iattr->ia_size);
  278. ip->i_d.di_size = iattr->ia_size;
  279. }
  280. xfs_trans_set_sync(tp);
  281. error = xfs_trans_commit(tp);
  282. out_drop_iolock:
  283. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  284. return error;
  285. }