xfs_dfrag.c 12 KB

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