xfs_dfrag.c 12 KB

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