xfs_utils.c 11 KB

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