xfs_dfrag.c 9.2 KB

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