xfs_utils.c 13 KB

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