m68k: Atari SCSI revival
[linux/fpc-iii.git] / fs / xfs / xfs_utils.c
blob9014d7e444885faa447c2a7a987c1f4bdcd1bf8e
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"
44 * xfs_get_dir_entry is used to get a reference to an inode given
45 * its parent directory inode and the name of the file. It does
46 * not lock the child inode, and it unlocks the directory before
47 * returning. The directory's generation number is returned for
48 * use by a later call to xfs_lock_dir_and_entry.
50 int
51 xfs_get_dir_entry(
52 bhv_vname_t *dentry,
53 xfs_inode_t **ipp)
55 bhv_vnode_t *vp;
57 vp = VNAME_TO_VNODE(dentry);
59 *ipp = xfs_vtoi(vp);
60 if (!*ipp)
61 return XFS_ERROR(ENOENT);
62 VN_HOLD(vp);
63 return 0;
66 int
67 xfs_dir_lookup_int(
68 bhv_desc_t *dir_bdp,
69 uint lock_mode,
70 bhv_vname_t *dentry,
71 xfs_ino_t *inum,
72 xfs_inode_t **ipp)
74 bhv_vnode_t *dir_vp;
75 xfs_inode_t *dp;
76 int error;
78 dir_vp = BHV_TO_VNODE(dir_bdp);
79 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
81 dp = XFS_BHVTOI(dir_bdp);
83 error = xfs_dir_lookup(NULL, dp, VNAME(dentry), VNAMELEN(dentry), inum);
84 if (!error) {
86 * Unlock the directory. We do this because we can't
87 * hold the directory lock while doing the vn_get()
88 * in xfs_iget(). Doing so could cause us to hold
89 * a lock while waiting for the inode to finish
90 * being inactive while it's waiting for a log
91 * reservation in the inactive routine.
93 xfs_iunlock(dp, lock_mode);
94 error = xfs_iget(dp->i_mount, NULL, *inum, 0, 0, ipp, 0);
95 xfs_ilock(dp, lock_mode);
97 if (error) {
98 *ipp = NULL;
99 } else if ((*ipp)->i_d.di_mode == 0) {
101 * The inode has been freed. Something is
102 * wrong so just get out of here.
104 xfs_iunlock(dp, lock_mode);
105 xfs_iput_new(*ipp, 0);
106 *ipp = NULL;
107 xfs_ilock(dp, lock_mode);
108 error = XFS_ERROR(ENOENT);
111 return error;
115 * Allocates a new inode from disk and return a pointer to the
116 * incore copy. This routine will internally commit the current
117 * transaction and allocate a new one if the Space Manager needed
118 * to do an allocation to replenish the inode free-list.
120 * This routine is designed to be called from xfs_create and
121 * xfs_create_dir.
125 xfs_dir_ialloc(
126 xfs_trans_t **tpp, /* input: current transaction;
127 output: may be a new transaction. */
128 xfs_inode_t *dp, /* directory within whose allocate
129 the inode. */
130 mode_t mode,
131 xfs_nlink_t nlink,
132 xfs_dev_t rdev,
133 cred_t *credp,
134 prid_t prid, /* project id */
135 int okalloc, /* ok to allocate new space */
136 xfs_inode_t **ipp, /* pointer to inode; it will be
137 locked. */
138 int *committed)
141 xfs_trans_t *tp;
142 xfs_trans_t *ntp;
143 xfs_inode_t *ip;
144 xfs_buf_t *ialloc_context = NULL;
145 boolean_t call_again = B_FALSE;
146 int code;
147 uint log_res;
148 uint log_count;
149 void *dqinfo;
150 uint tflags;
152 tp = *tpp;
153 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
156 * xfs_ialloc will return a pointer to an incore inode if
157 * the Space Manager has an available inode on the free
158 * list. Otherwise, it will do an allocation and replenish
159 * the freelist. Since we can only do one allocation per
160 * transaction without deadlocks, we will need to commit the
161 * current transaction and start a new one. We will then
162 * need to call xfs_ialloc again to get the inode.
164 * If xfs_ialloc did an allocation to replenish the freelist,
165 * it returns the bp containing the head of the freelist as
166 * ialloc_context. We will hold a lock on it across the
167 * transaction commit so that no other process can steal
168 * the inode(s) that we've just allocated.
170 code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid, okalloc,
171 &ialloc_context, &call_again, &ip);
174 * Return an error if we were unable to allocate a new inode.
175 * This should only happen if we run out of space on disk or
176 * encounter a disk error.
178 if (code) {
179 *ipp = NULL;
180 return code;
182 if (!call_again && (ip == NULL)) {
183 *ipp = NULL;
184 return XFS_ERROR(ENOSPC);
188 * If call_again is set, then we were unable to get an
189 * inode in one operation. We need to commit the current
190 * transaction and call xfs_ialloc() again. It is guaranteed
191 * to succeed the second time.
193 if (call_again) {
196 * Normally, xfs_trans_commit releases all the locks.
197 * We call bhold to hang on to the ialloc_context across
198 * the commit. Holding this buffer prevents any other
199 * processes from doing any allocations in this
200 * allocation group.
202 xfs_trans_bhold(tp, ialloc_context);
204 * Save the log reservation so we can use
205 * them in the next transaction.
207 log_res = xfs_trans_get_log_res(tp);
208 log_count = xfs_trans_get_log_count(tp);
211 * We want the quota changes to be associated with the next
212 * transaction, NOT this one. So, detach the dqinfo from this
213 * and attach it to the next transaction.
215 dqinfo = NULL;
216 tflags = 0;
217 if (tp->t_dqinfo) {
218 dqinfo = (void *)tp->t_dqinfo;
219 tp->t_dqinfo = NULL;
220 tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
221 tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
224 ntp = xfs_trans_dup(tp);
225 code = xfs_trans_commit(tp, 0, NULL);
226 tp = ntp;
227 if (committed != NULL) {
228 *committed = 1;
231 * If we get an error during the commit processing,
232 * release the buffer that is still held and return
233 * to the caller.
235 if (code) {
236 xfs_buf_relse(ialloc_context);
237 if (dqinfo) {
238 tp->t_dqinfo = dqinfo;
239 XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp);
241 *tpp = ntp;
242 *ipp = NULL;
243 return code;
245 code = xfs_trans_reserve(tp, 0, log_res, 0,
246 XFS_TRANS_PERM_LOG_RES, log_count);
248 * Re-attach the quota info that we detached from prev trx.
250 if (dqinfo) {
251 tp->t_dqinfo = dqinfo;
252 tp->t_flags |= tflags;
255 if (code) {
256 xfs_buf_relse(ialloc_context);
257 *tpp = ntp;
258 *ipp = NULL;
259 return code;
261 xfs_trans_bjoin(tp, ialloc_context);
264 * Call ialloc again. Since we've locked out all
265 * other allocations in this allocation group,
266 * this call should always succeed.
268 code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid,
269 okalloc, &ialloc_context, &call_again, &ip);
272 * If we get an error at this point, return to the caller
273 * so that the current transaction can be aborted.
275 if (code) {
276 *tpp = tp;
277 *ipp = NULL;
278 return code;
280 ASSERT ((!call_again) && (ip != NULL));
282 } else {
283 if (committed != NULL) {
284 *committed = 0;
288 *ipp = ip;
289 *tpp = tp;
291 return 0;
295 * Decrement the link count on an inode & log the change.
296 * If this causes the link count to go to zero, initiate the
297 * logging activity required to truncate a file.
299 int /* error */
300 xfs_droplink(
301 xfs_trans_t *tp,
302 xfs_inode_t *ip)
304 int error;
306 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
308 ASSERT (ip->i_d.di_nlink > 0);
309 ip->i_d.di_nlink--;
310 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
312 error = 0;
313 if (ip->i_d.di_nlink == 0) {
315 * We're dropping the last link to this file.
316 * Move the on-disk inode to the AGI unlinked list.
317 * From xfs_inactive() we will pull the inode from
318 * the list and free it.
320 error = xfs_iunlink(tp, ip);
322 return error;
326 * This gets called when the inode's version needs to be changed from 1 to 2.
327 * Currently this happens when the nlink field overflows the old 16-bit value
328 * or when chproj is called to change the project for the first time.
329 * As a side effect the superblock version will also get rev'd
330 * to contain the NLINK bit.
332 void
333 xfs_bump_ino_vers2(
334 xfs_trans_t *tp,
335 xfs_inode_t *ip)
337 xfs_mount_t *mp;
338 unsigned long s;
340 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
341 ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1);
343 ip->i_d.di_version = XFS_DINODE_VERSION_2;
344 ip->i_d.di_onlink = 0;
345 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
346 mp = tp->t_mountp;
347 if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
348 s = XFS_SB_LOCK(mp);
349 if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
350 XFS_SB_VERSION_ADDNLINK(&mp->m_sb);
351 XFS_SB_UNLOCK(mp, s);
352 xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
353 } else {
354 XFS_SB_UNLOCK(mp, s);
357 /* Caller must log the inode */
361 * Increment the link count on an inode & log the change.
364 xfs_bumplink(
365 xfs_trans_t *tp,
366 xfs_inode_t *ip)
368 if (ip->i_d.di_nlink >= XFS_MAXLINK)
369 return XFS_ERROR(EMLINK);
370 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
372 ASSERT(ip->i_d.di_nlink > 0);
373 ip->i_d.di_nlink++;
374 if ((ip->i_d.di_version == XFS_DINODE_VERSION_1) &&
375 (ip->i_d.di_nlink > XFS_MAXLINK_1)) {
377 * The inode has increased its number of links beyond
378 * what can fit in an old format inode. It now needs
379 * to be converted to a version 2 inode with a 32 bit
380 * link count. If this is the first inode in the file
381 * system to do this, then we need to bump the superblock
382 * version number as well.
384 xfs_bump_ino_vers2(tp, ip);
387 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
388 return 0;
392 * Try to truncate the given file to 0 length. Currently called
393 * only out of xfs_remove when it has to truncate a file to free
394 * up space for the remove to proceed.
397 xfs_truncate_file(
398 xfs_mount_t *mp,
399 xfs_inode_t *ip)
401 xfs_trans_t *tp;
402 int error;
404 #ifdef QUOTADEBUG
406 * This is called to truncate the quotainodes too.
408 if (XFS_IS_UQUOTA_ON(mp)) {
409 if (ip->i_ino != mp->m_sb.sb_uquotino)
410 ASSERT(ip->i_udquot);
412 if (XFS_IS_OQUOTA_ON(mp)) {
413 if (ip->i_ino != mp->m_sb.sb_gquotino)
414 ASSERT(ip->i_gdquot);
416 #endif
418 * Make the call to xfs_itruncate_start before starting the
419 * transaction, because we cannot make the call while we're
420 * in a transaction.
422 xfs_ilock(ip, XFS_IOLOCK_EXCL);
423 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, (xfs_fsize_t)0);
425 tp = xfs_trans_alloc(mp, XFS_TRANS_TRUNCATE_FILE);
426 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
427 XFS_TRANS_PERM_LOG_RES,
428 XFS_ITRUNCATE_LOG_COUNT))) {
429 xfs_trans_cancel(tp, 0);
430 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
431 return error;
435 * Follow the normal truncate locking protocol. Since we
436 * hold the inode in the transaction, we know that it's number
437 * of references will stay constant.
439 xfs_ilock(ip, XFS_ILOCK_EXCL);
440 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
441 xfs_trans_ihold(tp, ip);
443 * Signal a sync xaction. The only case where that isn't
444 * the case is if we're truncating an already unlinked file
445 * on a wsync fs. In that case, we know the blocks can't
446 * reappear in the file because the links to file are
447 * permanently toast. Currently, we're always going to
448 * want a sync transaction because this code is being
449 * called from places where nlink is guaranteed to be 1
450 * but I'm leaving the tests in to protect against future
451 * changes -- rcc.
453 error = xfs_itruncate_finish(&tp, ip, (xfs_fsize_t)0,
454 XFS_DATA_FORK,
455 ((ip->i_d.di_nlink != 0 ||
456 !(mp->m_flags & XFS_MOUNT_WSYNC))
457 ? 1 : 0));
458 if (error) {
459 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
460 XFS_TRANS_ABORT);
461 } else {
462 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
463 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES,
464 NULL);
466 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
468 return error;