xfs_utils.c 8.0 KB

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