xfs_dfrag.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. /*
  47. * Syssgi interface for swapext
  48. */
  49. int
  50. xfs_swapext(
  51. xfs_swapext_t *sxp)
  52. {
  53. xfs_inode_t *ip, *tip;
  54. struct file *file, *target_file;
  55. int error = 0;
  56. /* Pull information for the target fd */
  57. file = fget((int)sxp->sx_fdtarget);
  58. if (!file) {
  59. error = XFS_ERROR(EINVAL);
  60. goto out;
  61. }
  62. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) {
  63. error = XFS_ERROR(EBADF);
  64. goto out_put_file;
  65. }
  66. target_file = fget((int)sxp->sx_fdtmp);
  67. if (!target_file) {
  68. error = XFS_ERROR(EINVAL);
  69. goto out_put_file;
  70. }
  71. if (!(target_file->f_mode & FMODE_WRITE) ||
  72. (target_file->f_flags & O_APPEND)) {
  73. error = XFS_ERROR(EBADF);
  74. goto out_put_target_file;
  75. }
  76. if (IS_SWAPFILE(file->f_path.dentry->d_inode) ||
  77. IS_SWAPFILE(target_file->f_path.dentry->d_inode)) {
  78. error = XFS_ERROR(EINVAL);
  79. goto out_put_target_file;
  80. }
  81. ip = XFS_I(file->f_path.dentry->d_inode);
  82. tip = XFS_I(target_file->f_path.dentry->d_inode);
  83. if (ip->i_mount != tip->i_mount) {
  84. error = XFS_ERROR(EINVAL);
  85. goto out_put_target_file;
  86. }
  87. if (ip->i_ino == tip->i_ino) {
  88. error = XFS_ERROR(EINVAL);
  89. goto out_put_target_file;
  90. }
  91. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  92. error = XFS_ERROR(EIO);
  93. goto out_put_target_file;
  94. }
  95. error = xfs_swap_extents(ip, tip, sxp);
  96. out_put_target_file:
  97. fput(target_file);
  98. out_put_file:
  99. fput(file);
  100. out:
  101. return error;
  102. }
  103. int
  104. xfs_swap_extents(
  105. xfs_inode_t *ip,
  106. xfs_inode_t *tip,
  107. xfs_swapext_t *sxp)
  108. {
  109. xfs_mount_t *mp;
  110. xfs_trans_t *tp;
  111. xfs_bstat_t *sbp = &sxp->sx_stat;
  112. xfs_ifork_t *tempifp, *ifp, *tifp;
  113. int ilf_fields, tilf_fields;
  114. int error = 0;
  115. int aforkblks = 0;
  116. int taforkblks = 0;
  117. __uint64_t tmp;
  118. mp = ip->i_mount;
  119. tempifp = kmem_alloc(sizeof(xfs_ifork_t), KM_MAYFAIL);
  120. if (!tempifp) {
  121. error = XFS_ERROR(ENOMEM);
  122. goto out;
  123. }
  124. sbp = &sxp->sx_stat;
  125. /*
  126. * we have to do two separate lock calls here to keep lockdep
  127. * happy. If we try to get all the locks in one call, lock will
  128. * report false positives when we drop the ILOCK and regain them
  129. * below.
  130. */
  131. xfs_lock_two_inodes(ip, tip, XFS_IOLOCK_EXCL);
  132. xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
  133. /* Verify that both files have the same format */
  134. if ((ip->i_d.di_mode & S_IFMT) != (tip->i_d.di_mode & S_IFMT)) {
  135. error = XFS_ERROR(EINVAL);
  136. goto out_unlock;
  137. }
  138. /* Verify both files are either real-time or non-realtime */
  139. if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
  140. error = XFS_ERROR(EINVAL);
  141. goto out_unlock;
  142. }
  143. /* Should never get a local format */
  144. if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
  145. tip->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
  146. error = XFS_ERROR(EINVAL);
  147. goto out_unlock;
  148. }
  149. if (VN_CACHED(VFS_I(tip)) != 0) {
  150. xfs_inval_cached_trace(tip, 0, -1, 0, -1);
  151. error = xfs_flushinval_pages(tip, 0, -1,
  152. FI_REMAPF_LOCKED);
  153. if (error)
  154. goto out_unlock;
  155. }
  156. /* Verify O_DIRECT for ftmp */
  157. if (VN_CACHED(VFS_I(tip)) != 0) {
  158. error = XFS_ERROR(EINVAL);
  159. goto out_unlock;
  160. }
  161. /* Verify all data are being swapped */
  162. if (sxp->sx_offset != 0 ||
  163. sxp->sx_length != ip->i_d.di_size ||
  164. sxp->sx_length != tip->i_d.di_size) {
  165. error = XFS_ERROR(EFAULT);
  166. goto out_unlock;
  167. }
  168. /*
  169. * If the target has extended attributes, the tmp file
  170. * must also in order to ensure the correct data fork
  171. * format.
  172. */
  173. if ( XFS_IFORK_Q(ip) != XFS_IFORK_Q(tip) ) {
  174. error = XFS_ERROR(EINVAL);
  175. goto out_unlock;
  176. }
  177. /*
  178. * Compare the current change & modify times with that
  179. * passed in. If they differ, we abort this swap.
  180. * This is the mechanism used to ensure the calling
  181. * process that the file was not changed out from
  182. * under it.
  183. */
  184. if ((sbp->bs_ctime.tv_sec != ip->i_d.di_ctime.t_sec) ||
  185. (sbp->bs_ctime.tv_nsec != ip->i_d.di_ctime.t_nsec) ||
  186. (sbp->bs_mtime.tv_sec != ip->i_d.di_mtime.t_sec) ||
  187. (sbp->bs_mtime.tv_nsec != ip->i_d.di_mtime.t_nsec)) {
  188. error = XFS_ERROR(EBUSY);
  189. goto out_unlock;
  190. }
  191. /* We need to fail if the file is memory mapped. Once we have tossed
  192. * all existing pages, the page fault will have no option
  193. * but to go to the filesystem for pages. By making the page fault call
  194. * vop_read (or write in the case of autogrow) they block on the iolock
  195. * until we have switched the extents.
  196. */
  197. if (VN_MAPPED(VFS_I(ip))) {
  198. error = XFS_ERROR(EBUSY);
  199. goto out_unlock;
  200. }
  201. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  202. xfs_iunlock(tip, XFS_ILOCK_EXCL);
  203. /*
  204. * There is a race condition here since we gave up the
  205. * ilock. However, the data fork will not change since
  206. * we have the iolock (locked for truncation too) so we
  207. * are safe. We don't really care if non-io related
  208. * fields change.
  209. */
  210. xfs_tosspages(ip, 0, -1, FI_REMAPF);
  211. tp = xfs_trans_alloc(mp, XFS_TRANS_SWAPEXT);
  212. if ((error = xfs_trans_reserve(tp, 0,
  213. XFS_ICHANGE_LOG_RES(mp), 0,
  214. 0, 0))) {
  215. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  216. xfs_iunlock(tip, XFS_IOLOCK_EXCL);
  217. xfs_trans_cancel(tp, 0);
  218. goto out;
  219. }
  220. xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
  221. /*
  222. * Count the number of extended attribute blocks
  223. */
  224. if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
  225. (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
  226. error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &aforkblks);
  227. if (error)
  228. goto out_trans_cancel;
  229. }
  230. if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
  231. (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
  232. error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK,
  233. &taforkblks);
  234. if (error)
  235. goto out_trans_cancel;
  236. }
  237. /*
  238. * Swap the data forks of the inodes
  239. */
  240. ifp = &ip->i_df;
  241. tifp = &tip->i_df;
  242. *tempifp = *ifp; /* struct copy */
  243. *ifp = *tifp; /* struct copy */
  244. *tifp = *tempifp; /* struct copy */
  245. /*
  246. * Fix the on-disk inode values
  247. */
  248. tmp = (__uint64_t)ip->i_d.di_nblocks;
  249. ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
  250. tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
  251. tmp = (__uint64_t) ip->i_d.di_nextents;
  252. ip->i_d.di_nextents = tip->i_d.di_nextents;
  253. tip->i_d.di_nextents = tmp;
  254. tmp = (__uint64_t) ip->i_d.di_format;
  255. ip->i_d.di_format = tip->i_d.di_format;
  256. tip->i_d.di_format = tmp;
  257. ilf_fields = XFS_ILOG_CORE;
  258. switch(ip->i_d.di_format) {
  259. case XFS_DINODE_FMT_EXTENTS:
  260. /* If the extents fit in the inode, fix the
  261. * pointer. Otherwise it's already NULL or
  262. * pointing to the extent.
  263. */
  264. if (ip->i_d.di_nextents <= XFS_INLINE_EXTS) {
  265. ifp->if_u1.if_extents =
  266. ifp->if_u2.if_inline_ext;
  267. }
  268. ilf_fields |= XFS_ILOG_DEXT;
  269. break;
  270. case XFS_DINODE_FMT_BTREE:
  271. ilf_fields |= XFS_ILOG_DBROOT;
  272. break;
  273. }
  274. tilf_fields = XFS_ILOG_CORE;
  275. switch(tip->i_d.di_format) {
  276. case XFS_DINODE_FMT_EXTENTS:
  277. /* If the extents fit in the inode, fix the
  278. * pointer. Otherwise it's already NULL or
  279. * pointing to the extent.
  280. */
  281. if (tip->i_d.di_nextents <= XFS_INLINE_EXTS) {
  282. tifp->if_u1.if_extents =
  283. tifp->if_u2.if_inline_ext;
  284. }
  285. tilf_fields |= XFS_ILOG_DEXT;
  286. break;
  287. case XFS_DINODE_FMT_BTREE:
  288. tilf_fields |= XFS_ILOG_DBROOT;
  289. break;
  290. }
  291. IHOLD(ip);
  292. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  293. IHOLD(tip);
  294. xfs_trans_ijoin(tp, tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  295. xfs_trans_log_inode(tp, ip, ilf_fields);
  296. xfs_trans_log_inode(tp, tip, tilf_fields);
  297. /*
  298. * If this is a synchronous mount, make sure that the
  299. * transaction goes to disk before returning to the user.
  300. */
  301. if (mp->m_flags & XFS_MOUNT_WSYNC)
  302. xfs_trans_set_sync(tp);
  303. error = xfs_trans_commit(tp, XFS_TRANS_SWAPEXT);
  304. out:
  305. kmem_free(tempifp);
  306. return error;
  307. out_unlock:
  308. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  309. xfs_iunlock(tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  310. goto out;
  311. out_trans_cancel:
  312. xfs_trans_cancel(tp, 0);
  313. goto out_unlock;
  314. }