xfs_utils.c 13 KB

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