xfs_dfrag.c 9.2 KB

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