xfs_utils.c 12 KB

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