xfs_dfrag.c 9.8 KB

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