2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_types.h"
24 #include "xfs_trans.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"
34 #include "xfs_error.h"
35 #include "xfs_quota.h"
36 #include "xfs_itable.h"
37 #include "xfs_utils.h"
41 * Allocates a new inode from disk and return a pointer to the
42 * incore copy. This routine will internally commit the current
43 * transaction and allocate a new one if the Space Manager needed
44 * to do an allocation to replenish the inode free-list.
46 * This routine is designed to be called from xfs_create and
52 xfs_trans_t
**tpp
, /* input: current transaction;
53 output: may be a new transaction. */
54 xfs_inode_t
*dp
, /* directory within whose allocate
59 prid_t prid
, /* project id */
60 int okalloc
, /* ok to allocate new space */
61 xfs_inode_t
**ipp
, /* pointer to inode; it will be
69 xfs_buf_t
*ialloc_context
= NULL
;
70 boolean_t call_again
= B_FALSE
;
78 ASSERT(tp
->t_flags
& XFS_TRANS_PERM_LOG_RES
);
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.
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.
95 code
= xfs_ialloc(tp
, dp
, mode
, nlink
, rdev
, prid
, okalloc
,
96 &ialloc_context
, &call_again
, &ip
);
99 * Return an error if we were unable to allocate a new inode.
100 * This should only happen if we run out of space on disk or
101 * encounter a disk error.
107 if (!call_again
&& (ip
== NULL
)) {
109 return XFS_ERROR(ENOSPC
);
113 * If call_again is set, then we were unable to get an
114 * inode in one operation. We need to commit the current
115 * transaction and call xfs_ialloc() again. It is guaranteed
116 * to succeed the second time.
121 * Normally, xfs_trans_commit releases all the locks.
122 * We call bhold to hang on to the ialloc_context across
123 * the commit. Holding this buffer prevents any other
124 * processes from doing any allocations in this
127 xfs_trans_bhold(tp
, ialloc_context
);
129 * Save the log reservation so we can use
130 * them in the next transaction.
132 log_res
= xfs_trans_get_log_res(tp
);
133 log_count
= xfs_trans_get_log_count(tp
);
136 * We want the quota changes to be associated with the next
137 * transaction, NOT this one. So, detach the dqinfo from this
138 * and attach it to the next transaction.
143 dqinfo
= (void *)tp
->t_dqinfo
;
145 tflags
= tp
->t_flags
& XFS_TRANS_DQ_DIRTY
;
146 tp
->t_flags
&= ~(XFS_TRANS_DQ_DIRTY
);
149 ntp
= xfs_trans_dup(tp
);
150 code
= xfs_trans_commit(tp
, 0);
152 if (committed
!= NULL
) {
156 * If we get an error during the commit processing,
157 * release the buffer that is still held and return
161 xfs_buf_relse(ialloc_context
);
163 tp
->t_dqinfo
= dqinfo
;
164 xfs_trans_free_dqinfo(tp
);
172 * transaction commit worked ok so we can drop the extra ticket
173 * reference that we gained in xfs_trans_dup()
175 xfs_log_ticket_put(tp
->t_ticket
);
176 code
= xfs_trans_reserve(tp
, 0, log_res
, 0,
177 XFS_TRANS_PERM_LOG_RES
, log_count
);
179 * Re-attach the quota info that we detached from prev trx.
182 tp
->t_dqinfo
= dqinfo
;
183 tp
->t_flags
|= tflags
;
187 xfs_buf_relse(ialloc_context
);
192 xfs_trans_bjoin(tp
, ialloc_context
);
195 * Call ialloc again. Since we've locked out all
196 * other allocations in this allocation group,
197 * this call should always succeed.
199 code
= xfs_ialloc(tp
, dp
, mode
, nlink
, rdev
, prid
,
200 okalloc
, &ialloc_context
, &call_again
, &ip
);
203 * If we get an error at this point, return to the caller
204 * so that the current transaction can be aborted.
211 ASSERT ((!call_again
) && (ip
!= NULL
));
214 if (committed
!= NULL
) {
226 * Decrement the link count on an inode & log the change.
227 * If this causes the link count to go to zero, initiate the
228 * logging activity required to truncate a file.
237 xfs_trans_ichgtime(tp
, ip
, XFS_ICHGTIME_CHG
);
239 ASSERT (ip
->i_d
.di_nlink
> 0);
241 drop_nlink(VFS_I(ip
));
242 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
245 if (ip
->i_d
.di_nlink
== 0) {
247 * We're dropping the last link to this file.
248 * Move the on-disk inode to the AGI unlinked list.
249 * From xfs_inactive() we will pull the inode from
250 * the list and free it.
252 error
= xfs_iunlink(tp
, ip
);
258 * This gets called when the inode's version needs to be changed from 1 to 2.
259 * Currently this happens when the nlink field overflows the old 16-bit value
260 * or when chproj is called to change the project for the first time.
261 * As a side effect the superblock version will also get rev'd
262 * to contain the NLINK bit.
271 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
272 ASSERT(ip
->i_d
.di_version
== 1);
274 ip
->i_d
.di_version
= 2;
275 ip
->i_d
.di_onlink
= 0;
276 memset(&(ip
->i_d
.di_pad
[0]), 0, sizeof(ip
->i_d
.di_pad
));
278 if (!xfs_sb_version_hasnlink(&mp
->m_sb
)) {
279 spin_lock(&mp
->m_sb_lock
);
280 if (!xfs_sb_version_hasnlink(&mp
->m_sb
)) {
281 xfs_sb_version_addnlink(&mp
->m_sb
);
282 spin_unlock(&mp
->m_sb_lock
);
283 xfs_mod_sb(tp
, XFS_SB_VERSIONNUM
);
285 spin_unlock(&mp
->m_sb_lock
);
288 /* Caller must log the inode */
292 * Increment the link count on an inode & log the change.
299 if (ip
->i_d
.di_nlink
>= XFS_MAXLINK
)
300 return XFS_ERROR(EMLINK
);
301 xfs_trans_ichgtime(tp
, ip
, XFS_ICHGTIME_CHG
);
303 ASSERT(ip
->i_d
.di_nlink
> 0);
305 inc_nlink(VFS_I(ip
));
306 if ((ip
->i_d
.di_version
== 1) &&
307 (ip
->i_d
.di_nlink
> XFS_MAXLINK_1
)) {
309 * The inode has increased its number of links beyond
310 * what can fit in an old format inode. It now needs
311 * to be converted to a version 2 inode with a 32 bit
312 * link count. If this is the first inode in the file
313 * system to do this, then we need to bump the superblock
314 * version number as well.
316 xfs_bump_ino_vers2(tp
, ip
);
319 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);