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