xfs_utils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_dir2_sf.h"
  32. #include "xfs_attr_sf.h"
  33. #include "xfs_dinode.h"
  34. #include "xfs_inode.h"
  35. #include "xfs_inode_item.h"
  36. #include "xfs_bmap.h"
  37. #include "xfs_error.h"
  38. #include "xfs_quota.h"
  39. #include "xfs_rw.h"
  40. #include "xfs_itable.h"
  41. #include "xfs_utils.h"
  42. /*
  43. * xfs_get_dir_entry is used to get a reference to an inode given
  44. * its parent directory inode and the name of the file. It does
  45. * not lock the child inode, and it unlocks the directory before
  46. * returning. The directory's generation number is returned for
  47. * use by a later call to xfs_lock_dir_and_entry.
  48. */
  49. int
  50. xfs_get_dir_entry(
  51. bhv_vname_t *dentry,
  52. xfs_inode_t **ipp)
  53. {
  54. bhv_vnode_t *vp;
  55. vp = VNAME_TO_VNODE(dentry);
  56. *ipp = xfs_vtoi(vp);
  57. if (!*ipp)
  58. return XFS_ERROR(ENOENT);
  59. VN_HOLD(vp);
  60. return 0;
  61. }
  62. int
  63. xfs_dir_lookup_int(
  64. bhv_desc_t *dir_bdp,
  65. uint lock_mode,
  66. bhv_vname_t *dentry,
  67. xfs_ino_t *inum,
  68. xfs_inode_t **ipp)
  69. {
  70. bhv_vnode_t *dir_vp;
  71. xfs_inode_t *dp;
  72. int error;
  73. dir_vp = BHV_TO_VNODE(dir_bdp);
  74. vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
  75. dp = XFS_BHVTOI(dir_bdp);
  76. error = xfs_dir_lookup(NULL, dp, VNAME(dentry), VNAMELEN(dentry), inum);
  77. if (!error) {
  78. /*
  79. * Unlock the directory. We do this because we can't
  80. * hold the directory lock while doing the vn_get()
  81. * in xfs_iget(). Doing so could cause us to hold
  82. * a lock while waiting for the inode to finish
  83. * being inactive while it's waiting for a log
  84. * reservation in the inactive routine.
  85. */
  86. xfs_iunlock(dp, lock_mode);
  87. error = xfs_iget(dp->i_mount, NULL, *inum, 0, 0, ipp, 0);
  88. xfs_ilock(dp, lock_mode);
  89. if (error) {
  90. *ipp = NULL;
  91. } else if ((*ipp)->i_d.di_mode == 0) {
  92. /*
  93. * The inode has been freed. Something is
  94. * wrong so just get out of here.
  95. */
  96. xfs_iunlock(dp, lock_mode);
  97. xfs_iput_new(*ipp, 0);
  98. *ipp = NULL;
  99. xfs_ilock(dp, lock_mode);
  100. error = XFS_ERROR(ENOENT);
  101. }
  102. }
  103. return error;
  104. }
  105. /*
  106. * Allocates a new inode from disk and return a pointer to the
  107. * incore copy. This routine will internally commit the current
  108. * transaction and allocate a new one if the Space Manager needed
  109. * to do an allocation to replenish the inode free-list.
  110. *
  111. * This routine is designed to be called from xfs_create and
  112. * xfs_create_dir.
  113. *
  114. */
  115. int
  116. xfs_dir_ialloc(
  117. xfs_trans_t **tpp, /* input: current transaction;
  118. output: may be a new transaction. */
  119. xfs_inode_t *dp, /* directory within whose allocate
  120. the inode. */
  121. mode_t mode,
  122. xfs_nlink_t nlink,
  123. xfs_dev_t rdev,
  124. cred_t *credp,
  125. prid_t prid, /* project id */
  126. int okalloc, /* ok to allocate new space */
  127. xfs_inode_t **ipp, /* pointer to inode; it will be
  128. locked. */
  129. int *committed)
  130. {
  131. xfs_trans_t *tp;
  132. xfs_trans_t *ntp;
  133. xfs_inode_t *ip;
  134. xfs_buf_t *ialloc_context = NULL;
  135. boolean_t call_again = B_FALSE;
  136. int code;
  137. uint log_res;
  138. uint log_count;
  139. void *dqinfo;
  140. uint tflags;
  141. tp = *tpp;
  142. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  143. /*
  144. * xfs_ialloc will return a pointer to an incore inode if
  145. * the Space Manager has an available inode on the free
  146. * list. Otherwise, it will do an allocation and replenish
  147. * the freelist. Since we can only do one allocation per
  148. * transaction without deadlocks, we will need to commit the
  149. * current transaction and start a new one. We will then
  150. * need to call xfs_ialloc again to get the inode.
  151. *
  152. * If xfs_ialloc did an allocation to replenish the freelist,
  153. * it returns the bp containing the head of the freelist as
  154. * ialloc_context. We will hold a lock on it across the
  155. * transaction commit so that no other process can steal
  156. * the inode(s) that we've just allocated.
  157. */
  158. code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid, okalloc,
  159. &ialloc_context, &call_again, &ip);
  160. /*
  161. * Return an error if we were unable to allocate a new inode.
  162. * This should only happen if we run out of space on disk or
  163. * encounter a disk error.
  164. */
  165. if (code) {
  166. *ipp = NULL;
  167. return code;
  168. }
  169. if (!call_again && (ip == NULL)) {
  170. *ipp = NULL;
  171. return XFS_ERROR(ENOSPC);
  172. }
  173. /*
  174. * If call_again is set, then we were unable to get an
  175. * inode in one operation. We need to commit the current
  176. * transaction and call xfs_ialloc() again. It is guaranteed
  177. * to succeed the second time.
  178. */
  179. if (call_again) {
  180. /*
  181. * Normally, xfs_trans_commit releases all the locks.
  182. * We call bhold to hang on to the ialloc_context across
  183. * the commit. Holding this buffer prevents any other
  184. * processes from doing any allocations in this
  185. * allocation group.
  186. */
  187. xfs_trans_bhold(tp, ialloc_context);
  188. /*
  189. * Save the log reservation so we can use
  190. * them in the next transaction.
  191. */
  192. log_res = xfs_trans_get_log_res(tp);
  193. log_count = xfs_trans_get_log_count(tp);
  194. /*
  195. * We want the quota changes to be associated with the next
  196. * transaction, NOT this one. So, detach the dqinfo from this
  197. * and attach it to the next transaction.
  198. */
  199. dqinfo = NULL;
  200. tflags = 0;
  201. if (tp->t_dqinfo) {
  202. dqinfo = (void *)tp->t_dqinfo;
  203. tp->t_dqinfo = NULL;
  204. tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
  205. tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
  206. }
  207. ntp = xfs_trans_dup(tp);
  208. code = xfs_trans_commit(tp, 0, NULL);
  209. tp = ntp;
  210. if (committed != NULL) {
  211. *committed = 1;
  212. }
  213. /*
  214. * If we get an error during the commit processing,
  215. * release the buffer that is still held and return
  216. * to the caller.
  217. */
  218. if (code) {
  219. xfs_buf_relse(ialloc_context);
  220. if (dqinfo) {
  221. tp->t_dqinfo = dqinfo;
  222. XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp);
  223. }
  224. *tpp = ntp;
  225. *ipp = NULL;
  226. return code;
  227. }
  228. code = xfs_trans_reserve(tp, 0, log_res, 0,
  229. XFS_TRANS_PERM_LOG_RES, log_count);
  230. /*
  231. * Re-attach the quota info that we detached from prev trx.
  232. */
  233. if (dqinfo) {
  234. tp->t_dqinfo = dqinfo;
  235. tp->t_flags |= tflags;
  236. }
  237. if (code) {
  238. xfs_buf_relse(ialloc_context);
  239. *tpp = ntp;
  240. *ipp = NULL;
  241. return code;
  242. }
  243. xfs_trans_bjoin(tp, ialloc_context);
  244. /*
  245. * Call ialloc again. Since we've locked out all
  246. * other allocations in this allocation group,
  247. * this call should always succeed.
  248. */
  249. code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid,
  250. okalloc, &ialloc_context, &call_again, &ip);
  251. /*
  252. * If we get an error at this point, return to the caller
  253. * so that the current transaction can be aborted.
  254. */
  255. if (code) {
  256. *tpp = tp;
  257. *ipp = NULL;
  258. return code;
  259. }
  260. ASSERT ((!call_again) && (ip != NULL));
  261. } else {
  262. if (committed != NULL) {
  263. *committed = 0;
  264. }
  265. }
  266. *ipp = ip;
  267. *tpp = tp;
  268. return 0;
  269. }
  270. /*
  271. * Decrement the link count on an inode & log the change.
  272. * If this causes the link count to go to zero, initiate the
  273. * logging activity required to truncate a file.
  274. */
  275. int /* error */
  276. xfs_droplink(
  277. xfs_trans_t *tp,
  278. xfs_inode_t *ip)
  279. {
  280. int error;
  281. xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
  282. ASSERT (ip->i_d.di_nlink > 0);
  283. ip->i_d.di_nlink--;
  284. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  285. error = 0;
  286. if (ip->i_d.di_nlink == 0) {
  287. /*
  288. * We're dropping the last link to this file.
  289. * Move the on-disk inode to the AGI unlinked list.
  290. * From xfs_inactive() we will pull the inode from
  291. * the list and free it.
  292. */
  293. error = xfs_iunlink(tp, ip);
  294. }
  295. return error;
  296. }
  297. /*
  298. * This gets called when the inode's version needs to be changed from 1 to 2.
  299. * Currently this happens when the nlink field overflows the old 16-bit value
  300. * or when chproj is called to change the project for the first time.
  301. * As a side effect the superblock version will also get rev'd
  302. * to contain the NLINK bit.
  303. */
  304. void
  305. xfs_bump_ino_vers2(
  306. xfs_trans_t *tp,
  307. xfs_inode_t *ip)
  308. {
  309. xfs_mount_t *mp;
  310. unsigned long s;
  311. ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
  312. ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1);
  313. ip->i_d.di_version = XFS_DINODE_VERSION_2;
  314. ip->i_d.di_onlink = 0;
  315. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  316. mp = tp->t_mountp;
  317. if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
  318. s = XFS_SB_LOCK(mp);
  319. if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
  320. XFS_SB_VERSION_ADDNLINK(&mp->m_sb);
  321. XFS_SB_UNLOCK(mp, s);
  322. xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
  323. } else {
  324. XFS_SB_UNLOCK(mp, s);
  325. }
  326. }
  327. /* Caller must log the inode */
  328. }
  329. /*
  330. * Increment the link count on an inode & log the change.
  331. */
  332. int
  333. xfs_bumplink(
  334. xfs_trans_t *tp,
  335. xfs_inode_t *ip)
  336. {
  337. if (ip->i_d.di_nlink >= XFS_MAXLINK)
  338. return XFS_ERROR(EMLINK);
  339. xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
  340. ASSERT(ip->i_d.di_nlink > 0);
  341. ip->i_d.di_nlink++;
  342. if ((ip->i_d.di_version == XFS_DINODE_VERSION_1) &&
  343. (ip->i_d.di_nlink > XFS_MAXLINK_1)) {
  344. /*
  345. * The inode has increased its number of links beyond
  346. * what can fit in an old format inode. It now needs
  347. * to be converted to a version 2 inode with a 32 bit
  348. * link count. If this is the first inode in the file
  349. * system to do this, then we need to bump the superblock
  350. * version number as well.
  351. */
  352. xfs_bump_ino_vers2(tp, ip);
  353. }
  354. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  355. return 0;
  356. }
  357. /*
  358. * Try to truncate the given file to 0 length. Currently called
  359. * only out of xfs_remove when it has to truncate a file to free
  360. * up space for the remove to proceed.
  361. */
  362. int
  363. xfs_truncate_file(
  364. xfs_mount_t *mp,
  365. xfs_inode_t *ip)
  366. {
  367. xfs_trans_t *tp;
  368. int error;
  369. #ifdef QUOTADEBUG
  370. /*
  371. * This is called to truncate the quotainodes too.
  372. */
  373. if (XFS_IS_UQUOTA_ON(mp)) {
  374. if (ip->i_ino != mp->m_sb.sb_uquotino)
  375. ASSERT(ip->i_udquot);
  376. }
  377. if (XFS_IS_OQUOTA_ON(mp)) {
  378. if (ip->i_ino != mp->m_sb.sb_gquotino)
  379. ASSERT(ip->i_gdquot);
  380. }
  381. #endif
  382. /*
  383. * Make the call to xfs_itruncate_start before starting the
  384. * transaction, because we cannot make the call while we're
  385. * in a transaction.
  386. */
  387. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  388. xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, (xfs_fsize_t)0);
  389. tp = xfs_trans_alloc(mp, XFS_TRANS_TRUNCATE_FILE);
  390. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  391. XFS_TRANS_PERM_LOG_RES,
  392. XFS_ITRUNCATE_LOG_COUNT))) {
  393. xfs_trans_cancel(tp, 0);
  394. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  395. return error;
  396. }
  397. /*
  398. * Follow the normal truncate locking protocol. Since we
  399. * hold the inode in the transaction, we know that it's number
  400. * of references will stay constant.
  401. */
  402. xfs_ilock(ip, XFS_ILOCK_EXCL);
  403. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  404. xfs_trans_ihold(tp, ip);
  405. /*
  406. * Signal a sync xaction. The only case where that isn't
  407. * the case is if we're truncating an already unlinked file
  408. * on a wsync fs. In that case, we know the blocks can't
  409. * reappear in the file because the links to file are
  410. * permanently toast. Currently, we're always going to
  411. * want a sync transaction because this code is being
  412. * called from places where nlink is guaranteed to be 1
  413. * but I'm leaving the tests in to protect against future
  414. * changes -- rcc.
  415. */
  416. error = xfs_itruncate_finish(&tp, ip, (xfs_fsize_t)0,
  417. XFS_DATA_FORK,
  418. ((ip->i_d.di_nlink != 0 ||
  419. !(mp->m_flags & XFS_MOUNT_WSYNC))
  420. ? 1 : 0));
  421. if (error) {
  422. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
  423. XFS_TRANS_ABORT);
  424. } else {
  425. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  426. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES,
  427. NULL);
  428. }
  429. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  430. return error;
  431. }