xfs_utils.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_mount.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_dinode.h"
  31. #include "xfs_inode.h"
  32. #include "xfs_inode_item.h"
  33. #include "xfs_bmap.h"
  34. #include "xfs_error.h"
  35. #include "xfs_quota.h"
  36. #include "xfs_itable.h"
  37. #include "xfs_utils.h"
  38. /*
  39. * Allocates a new inode from disk and return a pointer to the
  40. * incore copy. This routine will internally commit the current
  41. * transaction and allocate a new one if the Space Manager needed
  42. * to do an allocation to replenish the inode free-list.
  43. *
  44. * This routine is designed to be called from xfs_create and
  45. * xfs_create_dir.
  46. *
  47. */
  48. int
  49. xfs_dir_ialloc(
  50. xfs_trans_t **tpp, /* input: current transaction;
  51. output: may be a new transaction. */
  52. xfs_inode_t *dp, /* directory within whose allocate
  53. the inode. */
  54. mode_t mode,
  55. xfs_nlink_t nlink,
  56. xfs_dev_t rdev,
  57. cred_t *credp,
  58. prid_t prid, /* project id */
  59. int okalloc, /* ok to allocate new space */
  60. xfs_inode_t **ipp, /* pointer to inode; it will be
  61. locked. */
  62. int *committed)
  63. {
  64. xfs_trans_t *tp;
  65. xfs_trans_t *ntp;
  66. xfs_inode_t *ip;
  67. xfs_buf_t *ialloc_context = NULL;
  68. boolean_t call_again = B_FALSE;
  69. int code;
  70. uint log_res;
  71. uint log_count;
  72. void *dqinfo;
  73. uint tflags;
  74. tp = *tpp;
  75. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  76. /*
  77. * xfs_ialloc will return a pointer to an incore inode if
  78. * the Space Manager has an available inode on the free
  79. * list. Otherwise, it will do an allocation and replenish
  80. * the freelist. Since we can only do one allocation per
  81. * transaction without deadlocks, we will need to commit the
  82. * current transaction and start a new one. We will then
  83. * need to call xfs_ialloc again to get the inode.
  84. *
  85. * If xfs_ialloc did an allocation to replenish the freelist,
  86. * it returns the bp containing the head of the freelist as
  87. * ialloc_context. We will hold a lock on it across the
  88. * transaction commit so that no other process can steal
  89. * the inode(s) that we've just allocated.
  90. */
  91. code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid, okalloc,
  92. &ialloc_context, &call_again, &ip);
  93. /*
  94. * Return an error if we were unable to allocate a new inode.
  95. * This should only happen if we run out of space on disk or
  96. * encounter a disk error.
  97. */
  98. if (code) {
  99. *ipp = NULL;
  100. return code;
  101. }
  102. if (!call_again && (ip == NULL)) {
  103. *ipp = NULL;
  104. return XFS_ERROR(ENOSPC);
  105. }
  106. /*
  107. * If call_again is set, then we were unable to get an
  108. * inode in one operation. We need to commit the current
  109. * transaction and call xfs_ialloc() again. It is guaranteed
  110. * to succeed the second time.
  111. */
  112. if (call_again) {
  113. /*
  114. * Normally, xfs_trans_commit releases all the locks.
  115. * We call bhold to hang on to the ialloc_context across
  116. * the commit. Holding this buffer prevents any other
  117. * processes from doing any allocations in this
  118. * allocation group.
  119. */
  120. xfs_trans_bhold(tp, ialloc_context);
  121. /*
  122. * Save the log reservation so we can use
  123. * them in the next transaction.
  124. */
  125. log_res = xfs_trans_get_log_res(tp);
  126. log_count = xfs_trans_get_log_count(tp);
  127. /*
  128. * We want the quota changes to be associated with the next
  129. * transaction, NOT this one. So, detach the dqinfo from this
  130. * and attach it to the next transaction.
  131. */
  132. dqinfo = NULL;
  133. tflags = 0;
  134. if (tp->t_dqinfo) {
  135. dqinfo = (void *)tp->t_dqinfo;
  136. tp->t_dqinfo = NULL;
  137. tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
  138. tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
  139. }
  140. ntp = xfs_trans_dup(tp);
  141. code = xfs_trans_commit(tp, 0);
  142. tp = ntp;
  143. if (committed != NULL) {
  144. *committed = 1;
  145. }
  146. /*
  147. * If we get an error during the commit processing,
  148. * release the buffer that is still held and return
  149. * to the caller.
  150. */
  151. if (code) {
  152. xfs_buf_relse(ialloc_context);
  153. if (dqinfo) {
  154. tp->t_dqinfo = dqinfo;
  155. xfs_trans_free_dqinfo(tp);
  156. }
  157. *tpp = ntp;
  158. *ipp = NULL;
  159. return code;
  160. }
  161. /*
  162. * transaction commit worked ok so we can drop the extra ticket
  163. * reference that we gained in xfs_trans_dup()
  164. */
  165. xfs_log_ticket_put(tp->t_ticket);
  166. code = xfs_trans_reserve(tp, 0, log_res, 0,
  167. XFS_TRANS_PERM_LOG_RES, log_count);
  168. /*
  169. * Re-attach the quota info that we detached from prev trx.
  170. */
  171. if (dqinfo) {
  172. tp->t_dqinfo = dqinfo;
  173. tp->t_flags |= tflags;
  174. }
  175. if (code) {
  176. xfs_buf_relse(ialloc_context);
  177. *tpp = ntp;
  178. *ipp = NULL;
  179. return code;
  180. }
  181. xfs_trans_bjoin(tp, ialloc_context);
  182. /*
  183. * Call ialloc again. Since we've locked out all
  184. * other allocations in this allocation group,
  185. * this call should always succeed.
  186. */
  187. code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid,
  188. okalloc, &ialloc_context, &call_again, &ip);
  189. /*
  190. * If we get an error at this point, return to the caller
  191. * so that the current transaction can be aborted.
  192. */
  193. if (code) {
  194. *tpp = tp;
  195. *ipp = NULL;
  196. return code;
  197. }
  198. ASSERT ((!call_again) && (ip != NULL));
  199. } else {
  200. if (committed != NULL) {
  201. *committed = 0;
  202. }
  203. }
  204. *ipp = ip;
  205. *tpp = tp;
  206. return 0;
  207. }
  208. /*
  209. * Decrement the link count on an inode & log the change.
  210. * If this causes the link count to go to zero, initiate the
  211. * logging activity required to truncate a file.
  212. */
  213. int /* error */
  214. xfs_droplink(
  215. xfs_trans_t *tp,
  216. xfs_inode_t *ip)
  217. {
  218. int error;
  219. xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
  220. ASSERT (ip->i_d.di_nlink > 0);
  221. ip->i_d.di_nlink--;
  222. drop_nlink(VFS_I(ip));
  223. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  224. error = 0;
  225. if (ip->i_d.di_nlink == 0) {
  226. /*
  227. * We're dropping the last link to this file.
  228. * Move the on-disk inode to the AGI unlinked list.
  229. * From xfs_inactive() we will pull the inode from
  230. * the list and free it.
  231. */
  232. error = xfs_iunlink(tp, ip);
  233. }
  234. return error;
  235. }
  236. /*
  237. * This gets called when the inode's version needs to be changed from 1 to 2.
  238. * Currently this happens when the nlink field overflows the old 16-bit value
  239. * or when chproj is called to change the project for the first time.
  240. * As a side effect the superblock version will also get rev'd
  241. * to contain the NLINK bit.
  242. */
  243. void
  244. xfs_bump_ino_vers2(
  245. xfs_trans_t *tp,
  246. xfs_inode_t *ip)
  247. {
  248. xfs_mount_t *mp;
  249. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  250. ASSERT(ip->i_d.di_version == 1);
  251. ip->i_d.di_version = 2;
  252. ip->i_d.di_onlink = 0;
  253. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  254. mp = tp->t_mountp;
  255. if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
  256. spin_lock(&mp->m_sb_lock);
  257. if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
  258. xfs_sb_version_addnlink(&mp->m_sb);
  259. spin_unlock(&mp->m_sb_lock);
  260. xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
  261. } else {
  262. spin_unlock(&mp->m_sb_lock);
  263. }
  264. }
  265. /* Caller must log the inode */
  266. }
  267. /*
  268. * Increment the link count on an inode & log the change.
  269. */
  270. int
  271. xfs_bumplink(
  272. xfs_trans_t *tp,
  273. xfs_inode_t *ip)
  274. {
  275. if (ip->i_d.di_nlink >= XFS_MAXLINK)
  276. return XFS_ERROR(EMLINK);
  277. xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
  278. ASSERT(ip->i_d.di_nlink > 0);
  279. ip->i_d.di_nlink++;
  280. inc_nlink(VFS_I(ip));
  281. if ((ip->i_d.di_version == 1) &&
  282. (ip->i_d.di_nlink > XFS_MAXLINK_1)) {
  283. /*
  284. * The inode has increased its number of links beyond
  285. * what can fit in an old format inode. It now needs
  286. * to be converted to a version 2 inode with a 32 bit
  287. * link count. If this is the first inode in the file
  288. * system to do this, then we need to bump the superblock
  289. * version number as well.
  290. */
  291. xfs_bump_ino_vers2(tp, ip);
  292. }
  293. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  294. return 0;
  295. }