net/netlabel/netlabel_kapi.c: add missing cleanup code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_utils.c
blob8b32d1a4c5a1c168043eaeced7949872bf19536b
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
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"
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
47 * xfs_create_dir.
50 int
51 xfs_dir_ialloc(
52 xfs_trans_t **tpp, /* input: current transaction;
53 output: may be a new transaction. */
54 xfs_inode_t *dp, /* directory within whose allocate
55 the inode. */
56 mode_t mode,
57 xfs_nlink_t nlink,
58 xfs_dev_t rdev,
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
62 locked. */
63 int *committed)
66 xfs_trans_t *tp;
67 xfs_trans_t *ntp;
68 xfs_inode_t *ip;
69 xfs_buf_t *ialloc_context = NULL;
70 boolean_t call_again = B_FALSE;
71 int code;
72 uint log_res;
73 uint log_count;
74 void *dqinfo;
75 uint tflags;
77 tp = *tpp;
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.
103 if (code) {
104 *ipp = NULL;
105 return code;
107 if (!call_again && (ip == NULL)) {
108 *ipp = 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.
118 if (call_again) {
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
125 * allocation group.
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.
140 dqinfo = NULL;
141 tflags = 0;
142 if (tp->t_dqinfo) {
143 dqinfo = (void *)tp->t_dqinfo;
144 tp->t_dqinfo = NULL;
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);
151 tp = ntp;
152 if (committed != NULL) {
153 *committed = 1;
156 * If we get an error during the commit processing,
157 * release the buffer that is still held and return
158 * to the caller.
160 if (code) {
161 xfs_buf_relse(ialloc_context);
162 if (dqinfo) {
163 tp->t_dqinfo = dqinfo;
164 xfs_trans_free_dqinfo(tp);
166 *tpp = ntp;
167 *ipp = NULL;
168 return code;
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.
181 if (dqinfo) {
182 tp->t_dqinfo = dqinfo;
183 tp->t_flags |= tflags;
186 if (code) {
187 xfs_buf_relse(ialloc_context);
188 *tpp = ntp;
189 *ipp = NULL;
190 return code;
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.
206 if (code) {
207 *tpp = tp;
208 *ipp = NULL;
209 return code;
211 ASSERT ((!call_again) && (ip != NULL));
213 } else {
214 if (committed != NULL) {
215 *committed = 0;
219 *ipp = ip;
220 *tpp = tp;
222 return 0;
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.
230 int /* error */
231 xfs_droplink(
232 xfs_trans_t *tp,
233 xfs_inode_t *ip)
235 int error;
237 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
239 ASSERT (ip->i_d.di_nlink > 0);
240 ip->i_d.di_nlink--;
241 drop_nlink(VFS_I(ip));
242 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
244 error = 0;
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);
254 return error;
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.
264 void
265 xfs_bump_ino_vers2(
266 xfs_trans_t *tp,
267 xfs_inode_t *ip)
269 xfs_mount_t *mp;
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));
277 mp = tp->t_mountp;
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);
284 } else {
285 spin_unlock(&mp->m_sb_lock);
288 /* Caller must log the inode */
292 * Increment the link count on an inode & log the change.
295 xfs_bumplink(
296 xfs_trans_t *tp,
297 xfs_inode_t *ip)
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);
304 ip->i_d.di_nlink++;
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);
320 return 0;