xfs_dfrag.c 9.3 KB

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