xfs_dfrag.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_log.h"
  22. #include "xfs_trans.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_mount.h"
  26. #include "xfs_bmap_btree.h"
  27. #include "xfs_alloc_btree.h"
  28. #include "xfs_ialloc_btree.h"
  29. #include "xfs_btree.h"
  30. #include "xfs_dinode.h"
  31. #include "xfs_inode.h"
  32. #include "xfs_inode_item.h"
  33. #include "xfs_bmap.h"
  34. #include "xfs_itable.h"
  35. #include "xfs_dfrag.h"
  36. #include "xfs_error.h"
  37. #include "xfs_vnodeops.h"
  38. #include "xfs_trace.h"
  39. static int xfs_swap_extents(
  40. xfs_inode_t *ip, /* target inode */
  41. xfs_inode_t *tip, /* tmp inode */
  42. xfs_swapext_t *sxp);
  43. /*
  44. * ioctl interface for swapext
  45. */
  46. int
  47. xfs_swapext(
  48. xfs_swapext_t *sxp)
  49. {
  50. xfs_inode_t *ip, *tip;
  51. struct fd f, tmp;
  52. int error = 0;
  53. /* Pull information for the target fd */
  54. f = fdget((int)sxp->sx_fdtarget);
  55. if (!f.file) {
  56. error = XFS_ERROR(EINVAL);
  57. goto out;
  58. }
  59. if (!(f.file->f_mode & FMODE_WRITE) ||
  60. !(f.file->f_mode & FMODE_READ) ||
  61. (f.file->f_flags & O_APPEND)) {
  62. error = XFS_ERROR(EBADF);
  63. goto out_put_file;
  64. }
  65. tmp = fdget((int)sxp->sx_fdtmp);
  66. if (!tmp.file) {
  67. error = XFS_ERROR(EINVAL);
  68. goto out_put_file;
  69. }
  70. if (!(tmp.file->f_mode & FMODE_WRITE) ||
  71. !(tmp.file->f_mode & FMODE_READ) ||
  72. (tmp.file->f_flags & O_APPEND)) {
  73. error = XFS_ERROR(EBADF);
  74. goto out_put_tmp_file;
  75. }
  76. if (IS_SWAPFILE(file_inode(f.file)) ||
  77. IS_SWAPFILE(file_inode(tmp.file))) {
  78. error = XFS_ERROR(EINVAL);
  79. goto out_put_tmp_file;
  80. }
  81. ip = XFS_I(file_inode(f.file));
  82. tip = XFS_I(file_inode(tmp.file));
  83. if (ip->i_mount != tip->i_mount) {
  84. error = XFS_ERROR(EINVAL);
  85. goto out_put_tmp_file;
  86. }
  87. if (ip->i_ino == tip->i_ino) {
  88. error = XFS_ERROR(EINVAL);
  89. goto out_put_tmp_file;
  90. }
  91. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  92. error = XFS_ERROR(EIO);
  93. goto out_put_tmp_file;
  94. }
  95. error = xfs_swap_extents(ip, tip, sxp);
  96. out_put_tmp_file:
  97. fdput(tmp);
  98. out_put_file:
  99. fdput(f);
  100. out:
  101. return error;
  102. }
  103. /*
  104. * We need to check that the format of the data fork in the temporary inode is
  105. * valid for the target inode before doing the swap. This is not a problem with
  106. * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
  107. * data fork depending on the space the attribute fork is taking so we can get
  108. * invalid formats on the target inode.
  109. *
  110. * E.g. target has space for 7 extents in extent format, temp inode only has
  111. * space for 6. If we defragment down to 7 extents, then the tmp format is a
  112. * btree, but when swapped it needs to be in extent format. Hence we can't just
  113. * blindly swap data forks on attr2 filesystems.
  114. *
  115. * Note that we check the swap in both directions so that we don't end up with
  116. * a corrupt temporary inode, either.
  117. *
  118. * Note that fixing the way xfs_fsr sets up the attribute fork in the source
  119. * inode will prevent this situation from occurring, so all we do here is
  120. * reject and log the attempt. basically we are putting the responsibility on
  121. * userspace to get this right.
  122. */
  123. static int
  124. xfs_swap_extents_check_format(
  125. xfs_inode_t *ip, /* target inode */
  126. xfs_inode_t *tip) /* tmp inode */
  127. {
  128. /* Should never get a local format */
  129. if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
  130. tip->i_d.di_format == XFS_DINODE_FMT_LOCAL)
  131. return EINVAL;
  132. /*
  133. * if the target inode has less extents that then temporary inode then
  134. * why did userspace call us?
  135. */
  136. if (ip->i_d.di_nextents < tip->i_d.di_nextents)
  137. return EINVAL;
  138. /*
  139. * if the target inode is in extent form and the temp inode is in btree
  140. * form then we will end up with the target inode in the wrong format
  141. * as we already know there are less extents in the temp inode.
  142. */
  143. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  144. tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
  145. return EINVAL;
  146. /* Check temp in extent form to max in target */
  147. if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  148. XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) >
  149. XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
  150. return EINVAL;
  151. /* Check target in extent form to max in temp */
  152. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  153. XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) >
  154. XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
  155. return EINVAL;
  156. /*
  157. * If we are in a btree format, check that the temp root block will fit
  158. * in the target and that it has enough extents to be in btree format
  159. * in the target.
  160. *
  161. * Note that we have to be careful to allow btree->extent conversions
  162. * (a common defrag case) which will occur when the temp inode is in
  163. * extent format...
  164. */
  165. if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  166. if (XFS_IFORK_BOFF(ip) &&
  167. XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip))
  168. return EINVAL;
  169. if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <=
  170. XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
  171. return EINVAL;
  172. }
  173. /* Reciprocal target->temp btree format checks */
  174. if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  175. if (XFS_IFORK_BOFF(tip) &&
  176. XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
  177. return EINVAL;
  178. if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <=
  179. XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
  180. return EINVAL;
  181. }
  182. return 0;
  183. }
  184. static int
  185. xfs_swap_extents(
  186. xfs_inode_t *ip, /* target inode */
  187. xfs_inode_t *tip, /* tmp inode */
  188. xfs_swapext_t *sxp)
  189. {
  190. xfs_mount_t *mp = ip->i_mount;
  191. xfs_trans_t *tp;
  192. xfs_bstat_t *sbp = &sxp->sx_stat;
  193. xfs_ifork_t *tempifp, *ifp, *tifp;
  194. int src_log_flags, target_log_flags;
  195. int error = 0;
  196. int aforkblks = 0;
  197. int taforkblks = 0;
  198. __uint64_t tmp;
  199. /*
  200. * We have no way of updating owner information in the BMBT blocks for
  201. * each inode on CRC enabled filesystems, so to avoid corrupting the
  202. * this metadata we simply don't allow extent swaps to occur.
  203. */
  204. if (xfs_sb_version_hascrc(&mp->m_sb))
  205. return XFS_ERROR(EINVAL);
  206. tempifp = kmem_alloc(sizeof(xfs_ifork_t), KM_MAYFAIL);
  207. if (!tempifp) {
  208. error = XFS_ERROR(ENOMEM);
  209. goto out;
  210. }
  211. /*
  212. * we have to do two separate lock calls here to keep lockdep
  213. * happy. If we try to get all the locks in one call, lock will
  214. * report false positives when we drop the ILOCK and regain them
  215. * below.
  216. */
  217. xfs_lock_two_inodes(ip, tip, XFS_IOLOCK_EXCL);
  218. xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
  219. /* Verify that both files have the same format */
  220. if ((ip->i_d.di_mode & S_IFMT) != (tip->i_d.di_mode & S_IFMT)) {
  221. error = XFS_ERROR(EINVAL);
  222. goto out_unlock;
  223. }
  224. /* Verify both files are either real-time or non-realtime */
  225. if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
  226. error = XFS_ERROR(EINVAL);
  227. goto out_unlock;
  228. }
  229. error = -filemap_write_and_wait(VFS_I(tip)->i_mapping);
  230. if (error)
  231. goto out_unlock;
  232. truncate_pagecache_range(VFS_I(tip), 0, -1);
  233. /* Verify O_DIRECT for ftmp */
  234. if (VN_CACHED(VFS_I(tip)) != 0) {
  235. error = XFS_ERROR(EINVAL);
  236. goto out_unlock;
  237. }
  238. /* Verify all data are being swapped */
  239. if (sxp->sx_offset != 0 ||
  240. sxp->sx_length != ip->i_d.di_size ||
  241. sxp->sx_length != tip->i_d.di_size) {
  242. error = XFS_ERROR(EFAULT);
  243. goto out_unlock;
  244. }
  245. trace_xfs_swap_extent_before(ip, 0);
  246. trace_xfs_swap_extent_before(tip, 1);
  247. /* check inode formats now that data is flushed */
  248. error = xfs_swap_extents_check_format(ip, tip);
  249. if (error) {
  250. xfs_notice(mp,
  251. "%s: inode 0x%llx format is incompatible for exchanging.",
  252. __func__, ip->i_ino);
  253. goto out_unlock;
  254. }
  255. /*
  256. * Compare the current change & modify times with that
  257. * passed in. If they differ, we abort this swap.
  258. * This is the mechanism used to ensure the calling
  259. * process that the file was not changed out from
  260. * under it.
  261. */
  262. if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
  263. (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
  264. (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
  265. (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
  266. error = XFS_ERROR(EBUSY);
  267. goto out_unlock;
  268. }
  269. /* We need to fail if the file is memory mapped. Once we have tossed
  270. * all existing pages, the page fault will have no option
  271. * but to go to the filesystem for pages. By making the page fault call
  272. * vop_read (or write in the case of autogrow) they block on the iolock
  273. * until we have switched the extents.
  274. */
  275. if (VN_MAPPED(VFS_I(ip))) {
  276. error = XFS_ERROR(EBUSY);
  277. goto out_unlock;
  278. }
  279. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  280. xfs_iunlock(tip, XFS_ILOCK_EXCL);
  281. /*
  282. * There is a race condition here since we gave up the
  283. * ilock. However, the data fork will not change since
  284. * we have the iolock (locked for truncation too) so we
  285. * are safe. We don't really care if non-io related
  286. * fields change.
  287. */
  288. truncate_pagecache_range(VFS_I(ip), 0, -1);
  289. tp = xfs_trans_alloc(mp, XFS_TRANS_SWAPEXT);
  290. if ((error = xfs_trans_reserve(tp, 0,
  291. XFS_ICHANGE_LOG_RES(mp), 0,
  292. 0, 0))) {
  293. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  294. xfs_iunlock(tip, XFS_IOLOCK_EXCL);
  295. xfs_trans_cancel(tp, 0);
  296. goto out;
  297. }
  298. xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
  299. /*
  300. * Count the number of extended attribute blocks
  301. */
  302. if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
  303. (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
  304. error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &aforkblks);
  305. if (error)
  306. goto out_trans_cancel;
  307. }
  308. if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
  309. (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
  310. error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK,
  311. &taforkblks);
  312. if (error)
  313. goto out_trans_cancel;
  314. }
  315. /*
  316. * Swap the data forks of the inodes
  317. */
  318. ifp = &ip->i_df;
  319. tifp = &tip->i_df;
  320. *tempifp = *ifp; /* struct copy */
  321. *ifp = *tifp; /* struct copy */
  322. *tifp = *tempifp; /* struct copy */
  323. /*
  324. * Fix the on-disk inode values
  325. */
  326. tmp = (__uint64_t)ip->i_d.di_nblocks;
  327. ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
  328. tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
  329. tmp = (__uint64_t) ip->i_d.di_nextents;
  330. ip->i_d.di_nextents = tip->i_d.di_nextents;
  331. tip->i_d.di_nextents = tmp;
  332. tmp = (__uint64_t) ip->i_d.di_format;
  333. ip->i_d.di_format = tip->i_d.di_format;
  334. tip->i_d.di_format = tmp;
  335. /*
  336. * The extents in the source inode could still contain speculative
  337. * preallocation beyond EOF (e.g. the file is open but not modified
  338. * while defrag is in progress). In that case, we need to copy over the
  339. * number of delalloc blocks the data fork in the source inode is
  340. * tracking beyond EOF so that when the fork is truncated away when the
  341. * temporary inode is unlinked we don't underrun the i_delayed_blks
  342. * counter on that inode.
  343. */
  344. ASSERT(tip->i_delayed_blks == 0);
  345. tip->i_delayed_blks = ip->i_delayed_blks;
  346. ip->i_delayed_blks = 0;
  347. src_log_flags = XFS_ILOG_CORE;
  348. switch (ip->i_d.di_format) {
  349. case XFS_DINODE_FMT_EXTENTS:
  350. /* If the extents fit in the inode, fix the
  351. * pointer. Otherwise it's already NULL or
  352. * pointing to the extent.
  353. */
  354. if (ip->i_d.di_nextents <= XFS_INLINE_EXTS) {
  355. ifp->if_u1.if_extents =
  356. ifp->if_u2.if_inline_ext;
  357. }
  358. src_log_flags |= XFS_ILOG_DEXT;
  359. break;
  360. case XFS_DINODE_FMT_BTREE:
  361. src_log_flags |= XFS_ILOG_DBROOT;
  362. break;
  363. }
  364. target_log_flags = XFS_ILOG_CORE;
  365. switch (tip->i_d.di_format) {
  366. case XFS_DINODE_FMT_EXTENTS:
  367. /* If the extents fit in the inode, fix the
  368. * pointer. Otherwise it's already NULL or
  369. * pointing to the extent.
  370. */
  371. if (tip->i_d.di_nextents <= XFS_INLINE_EXTS) {
  372. tifp->if_u1.if_extents =
  373. tifp->if_u2.if_inline_ext;
  374. }
  375. target_log_flags |= XFS_ILOG_DEXT;
  376. break;
  377. case XFS_DINODE_FMT_BTREE:
  378. target_log_flags |= XFS_ILOG_DBROOT;
  379. break;
  380. }
  381. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  382. xfs_trans_ijoin(tp, tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  383. xfs_trans_log_inode(tp, ip, src_log_flags);
  384. xfs_trans_log_inode(tp, tip, target_log_flags);
  385. /*
  386. * If this is a synchronous mount, make sure that the
  387. * transaction goes to disk before returning to the user.
  388. */
  389. if (mp->m_flags & XFS_MOUNT_WSYNC)
  390. xfs_trans_set_sync(tp);
  391. error = xfs_trans_commit(tp, 0);
  392. trace_xfs_swap_extent_after(ip, 0);
  393. trace_xfs_swap_extent_after(tip, 1);
  394. out:
  395. kmem_free(tempifp);
  396. return error;
  397. out_unlock:
  398. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  399. xfs_iunlock(tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  400. goto out;
  401. out_trans_cancel:
  402. xfs_trans_cancel(tp, 0);
  403. goto out_unlock;
  404. }