xfs_dfrag.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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);
  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_trans_t *tp;
  118. xfs_bstat_t *sbp = &sxp->sx_stat;
  119. xfs_ifork_t *tempifp, *ifp, *tifp;
  120. int ilf_fields, tilf_fields;
  121. static uint lock_flags = XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL;
  122. int error = 0;
  123. int aforkblks = 0;
  124. int taforkblks = 0;
  125. __uint64_t tmp;
  126. char locked = 0;
  127. mp = ip->i_mount;
  128. tempifp = kmem_alloc(sizeof(xfs_ifork_t), KM_MAYFAIL);
  129. if (!tempifp) {
  130. error = XFS_ERROR(ENOMEM);
  131. goto error0;
  132. }
  133. sbp = &sxp->sx_stat;
  134. xfs_lock_two_inodes(ip, tip, lock_flags);
  135. locked = 1;
  136. /* Verify that both files have the same format */
  137. if ((ip->i_d.di_mode & S_IFMT) != (tip->i_d.di_mode & S_IFMT)) {
  138. error = XFS_ERROR(EINVAL);
  139. goto error0;
  140. }
  141. /* Verify both files are either real-time or non-realtime */
  142. if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
  143. error = XFS_ERROR(EINVAL);
  144. goto error0;
  145. }
  146. /* Should never get a local format */
  147. if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
  148. tip->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
  149. error = XFS_ERROR(EINVAL);
  150. goto error0;
  151. }
  152. if (VN_CACHED(VFS_I(tip)) != 0) {
  153. xfs_inval_cached_trace(tip, 0, -1, 0, -1);
  154. error = xfs_flushinval_pages(tip, 0, -1,
  155. FI_REMAPF_LOCKED);
  156. if (error)
  157. goto error0;
  158. }
  159. /* Verify O_DIRECT for ftmp */
  160. if (VN_CACHED(VFS_I(tip)) != 0) {
  161. error = XFS_ERROR(EINVAL);
  162. goto error0;
  163. }
  164. /* Verify all data are being swapped */
  165. if (sxp->sx_offset != 0 ||
  166. sxp->sx_length != ip->i_d.di_size ||
  167. sxp->sx_length != tip->i_d.di_size) {
  168. error = XFS_ERROR(EFAULT);
  169. goto error0;
  170. }
  171. /*
  172. * If the target has extended attributes, the tmp file
  173. * must also in order to ensure the correct data fork
  174. * format.
  175. */
  176. if ( XFS_IFORK_Q(ip) != XFS_IFORK_Q(tip) ) {
  177. error = XFS_ERROR(EINVAL);
  178. goto error0;
  179. }
  180. /*
  181. * Compare the current change & modify times with that
  182. * passed in. If they differ, we abort this swap.
  183. * This is the mechanism used to ensure the calling
  184. * process that the file was not changed out from
  185. * under it.
  186. */
  187. if ((sbp->bs_ctime.tv_sec != ip->i_d.di_ctime.t_sec) ||
  188. (sbp->bs_ctime.tv_nsec != ip->i_d.di_ctime.t_nsec) ||
  189. (sbp->bs_mtime.tv_sec != ip->i_d.di_mtime.t_sec) ||
  190. (sbp->bs_mtime.tv_nsec != ip->i_d.di_mtime.t_nsec)) {
  191. error = XFS_ERROR(EBUSY);
  192. goto error0;
  193. }
  194. /* We need to fail if the file is memory mapped. Once we have tossed
  195. * all existing pages, the page fault will have no option
  196. * but to go to the filesystem for pages. By making the page fault call
  197. * vop_read (or write in the case of autogrow) they block on the iolock
  198. * until we have switched the extents.
  199. */
  200. if (VN_MAPPED(VFS_I(ip))) {
  201. error = XFS_ERROR(EBUSY);
  202. goto error0;
  203. }
  204. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  205. xfs_iunlock(tip, XFS_ILOCK_EXCL);
  206. /*
  207. * There is a race condition here since we gave up the
  208. * ilock. However, the data fork will not change since
  209. * we have the iolock (locked for truncation too) so we
  210. * are safe. We don't really care if non-io related
  211. * fields change.
  212. */
  213. xfs_tosspages(ip, 0, -1, FI_REMAPF);
  214. tp = xfs_trans_alloc(mp, XFS_TRANS_SWAPEXT);
  215. if ((error = xfs_trans_reserve(tp, 0,
  216. XFS_ICHANGE_LOG_RES(mp), 0,
  217. 0, 0))) {
  218. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  219. xfs_iunlock(tip, XFS_IOLOCK_EXCL);
  220. xfs_trans_cancel(tp, 0);
  221. locked = 0;
  222. goto error0;
  223. }
  224. xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
  225. /*
  226. * Count the number of extended attribute blocks
  227. */
  228. if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
  229. (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
  230. error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &aforkblks);
  231. if (error) {
  232. xfs_trans_cancel(tp, 0);
  233. goto error0;
  234. }
  235. }
  236. if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
  237. (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
  238. error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK,
  239. &taforkblks);
  240. if (error) {
  241. xfs_trans_cancel(tp, 0);
  242. goto error0;
  243. }
  244. }
  245. /*
  246. * Swap the data forks of the inodes
  247. */
  248. ifp = &ip->i_df;
  249. tifp = &tip->i_df;
  250. *tempifp = *ifp; /* struct copy */
  251. *ifp = *tifp; /* struct copy */
  252. *tifp = *tempifp; /* struct copy */
  253. /*
  254. * Fix the on-disk inode values
  255. */
  256. tmp = (__uint64_t)ip->i_d.di_nblocks;
  257. ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
  258. tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
  259. tmp = (__uint64_t) ip->i_d.di_nextents;
  260. ip->i_d.di_nextents = tip->i_d.di_nextents;
  261. tip->i_d.di_nextents = tmp;
  262. tmp = (__uint64_t) ip->i_d.di_format;
  263. ip->i_d.di_format = tip->i_d.di_format;
  264. tip->i_d.di_format = tmp;
  265. ilf_fields = XFS_ILOG_CORE;
  266. switch(ip->i_d.di_format) {
  267. case XFS_DINODE_FMT_EXTENTS:
  268. /* If the extents fit in the inode, fix the
  269. * pointer. Otherwise it's already NULL or
  270. * pointing to the extent.
  271. */
  272. if (ip->i_d.di_nextents <= XFS_INLINE_EXTS) {
  273. ifp->if_u1.if_extents =
  274. ifp->if_u2.if_inline_ext;
  275. }
  276. ilf_fields |= XFS_ILOG_DEXT;
  277. break;
  278. case XFS_DINODE_FMT_BTREE:
  279. ilf_fields |= XFS_ILOG_DBROOT;
  280. break;
  281. }
  282. tilf_fields = XFS_ILOG_CORE;
  283. switch(tip->i_d.di_format) {
  284. case XFS_DINODE_FMT_EXTENTS:
  285. /* If the extents fit in the inode, fix the
  286. * pointer. Otherwise it's already NULL or
  287. * pointing to the extent.
  288. */
  289. if (tip->i_d.di_nextents <= XFS_INLINE_EXTS) {
  290. tifp->if_u1.if_extents =
  291. tifp->if_u2.if_inline_ext;
  292. }
  293. tilf_fields |= XFS_ILOG_DEXT;
  294. break;
  295. case XFS_DINODE_FMT_BTREE:
  296. tilf_fields |= XFS_ILOG_DBROOT;
  297. break;
  298. }
  299. IHOLD(ip);
  300. xfs_trans_ijoin(tp, ip, lock_flags);
  301. IHOLD(tip);
  302. xfs_trans_ijoin(tp, tip, lock_flags);
  303. xfs_trans_log_inode(tp, ip, ilf_fields);
  304. xfs_trans_log_inode(tp, tip, tilf_fields);
  305. /*
  306. * If this is a synchronous mount, make sure that the
  307. * transaction goes to disk before returning to the user.
  308. */
  309. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  310. xfs_trans_set_sync(tp);
  311. }
  312. error = xfs_trans_commit(tp, XFS_TRANS_SWAPEXT);
  313. locked = 0;
  314. error0:
  315. if (locked) {
  316. xfs_iunlock(ip, lock_flags);
  317. xfs_iunlock(tip, lock_flags);
  318. }
  319. if (tempifp != NULL)
  320. kmem_free(tempifp);
  321. return error;
  322. }