xfs_dfrag.c 9.0 KB

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