xfs_dfrag.c 9.6 KB

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