xfs_utils.c 12 KB

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