2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #include "xfs_types.h"
38 #include "xfs_trans.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_bmap_btree.h"
46 #include "xfs_dir_sf.h"
47 #include "xfs_dir2_sf.h"
48 #include "xfs_attr_sf.h"
49 #include "xfs_dinode.h"
50 #include "xfs_inode.h"
51 #include "xfs_inode_item.h"
53 #include "xfs_error.h"
54 #include "xfs_quota.h"
56 #include "xfs_itable.h"
57 #include "xfs_utils.h"
60 * xfs_get_dir_entry is used to get a reference to an inode given
61 * its parent directory inode and the name of the file. It does
62 * not lock the child inode, and it unlocks the directory before
63 * returning. The directory's generation number is returned for
64 * use by a later call to xfs_lock_dir_and_entry.
74 vp = VNAME_TO_VNODE(dentry);
75 bdp = vn_bhv_lookup_unlocked(VN_BHV_HEAD(vp), &xfs_vnodeops);
78 return XFS_ERROR(ENOENT);
81 *ipp = XFS_BHVTOI(bdp);
97 dir_vp = BHV_TO_VNODE(dir_bdp);
98 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
100 dp = XFS_BHVTOI(dir_bdp);
102 error = XFS_DIR_LOOKUP(dp->i_mount, NULL, dp,
103 VNAME(dentry), VNAMELEN(dentry), inum);
106 * Unlock the directory. We do this because we can't
107 * hold the directory lock while doing the vn_get()
108 * in xfs_iget(). Doing so could cause us to hold
109 * a lock while waiting for the inode to finish
110 * being inactive while it's waiting for a log
111 * reservation in the inactive routine.
113 xfs_iunlock(dp, lock_mode);
114 error = xfs_iget(dp->i_mount, NULL, *inum, 0, 0, ipp, 0);
115 xfs_ilock(dp, lock_mode);
119 } else if ((*ipp)->i_d.di_mode == 0) {
121 * The inode has been freed. Something is
122 * wrong so just get out of here.
124 xfs_iunlock(dp, lock_mode);
125 xfs_iput_new(*ipp, 0);
127 xfs_ilock(dp, lock_mode);
128 error = XFS_ERROR(ENOENT);
135 * Allocates a new inode from disk and return a pointer to the
136 * incore copy. This routine will internally commit the current
137 * transaction and allocate a new one if the Space Manager needed
138 * to do an allocation to replenish the inode free-list.
140 * This routine is designed to be called from xfs_create and
146 xfs_trans_t **tpp, /* input: current transaction;
147 output: may be a new transaction. */
148 xfs_inode_t *dp, /* directory within whose allocate
154 prid_t prid, /* project id */
155 int okalloc, /* ok to allocate new space */
156 xfs_inode_t **ipp, /* pointer to inode; it will be
164 xfs_buf_t *ialloc_context = NULL;
165 boolean_t call_again = B_FALSE;
173 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
176 * xfs_ialloc will return a pointer to an incore inode if
177 * the Space Manager has an available inode on the free
178 * list. Otherwise, it will do an allocation and replenish
179 * the freelist. Since we can only do one allocation per
180 * transaction without deadlocks, we will need to commit the
181 * current transaction and start a new one. We will then
182 * need to call xfs_ialloc again to get the inode.
184 * If xfs_ialloc did an allocation to replenish the freelist,
185 * it returns the bp containing the head of the freelist as
186 * ialloc_context. We will hold a lock on it across the
187 * transaction commit so that no other process can steal
188 * the inode(s) that we've just allocated.
190 code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid, okalloc,
191 &ialloc_context, &call_again, &ip);
194 * Return an error if we were unable to allocate a new inode.
195 * This should only happen if we run out of space on disk or
196 * encounter a disk error.
202 if (!call_again && (ip == NULL)) {
204 return XFS_ERROR(ENOSPC);
208 * If call_again is set, then we were unable to get an
209 * inode in one operation. We need to commit the current
210 * transaction and call xfs_ialloc() again. It is guaranteed
211 * to succeed the second time.
216 * Normally, xfs_trans_commit releases all the locks.
217 * We call bhold to hang on to the ialloc_context across
218 * the commit. Holding this buffer prevents any other
219 * processes from doing any allocations in this
222 xfs_trans_bhold(tp, ialloc_context);
224 * Save the log reservation so we can use
225 * them in the next transaction.
227 log_res = xfs_trans_get_log_res(tp);
228 log_count = xfs_trans_get_log_count(tp);
231 * We want the quota changes to be associated with the next
232 * transaction, NOT this one. So, detach the dqinfo from this
233 * and attach it to the next transaction.
238 dqinfo = (void *)tp->t_dqinfo;
240 tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
241 tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
244 ntp = xfs_trans_dup(tp);
245 code = xfs_trans_commit(tp, 0, NULL);
247 if (committed != NULL) {
251 * If we get an error during the commit processing,
252 * release the buffer that is still held and return
256 xfs_buf_relse(ialloc_context);
258 tp->t_dqinfo = dqinfo;
259 XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp);
265 code = xfs_trans_reserve(tp, 0, log_res, 0,
266 XFS_TRANS_PERM_LOG_RES, log_count);
268 * Re-attach the quota info that we detached from prev trx.
271 tp->t_dqinfo = dqinfo;
272 tp->t_flags |= tflags;
276 xfs_buf_relse(ialloc_context);
281 xfs_trans_bjoin(tp, ialloc_context);
284 * Call ialloc again. Since we've locked out all
285 * other allocations in this allocation group,
286 * this call should always succeed.
288 code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid,
289 okalloc, &ialloc_context, &call_again, &ip);
292 * If we get an error at this point, return to the caller
293 * so that the current transaction can be aborted.
300 ASSERT ((!call_again) && (ip != NULL));
303 if (committed != NULL) {
315 * Decrement the link count on an inode & log the change.
316 * If this causes the link count to go to zero, initiate the
317 * logging activity required to truncate a file.
326 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
328 ASSERT (ip->i_d.di_nlink > 0);
330 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
333 if (ip->i_d.di_nlink == 0) {
335 * We're dropping the last link to this file.
336 * Move the on-disk inode to the AGI unlinked list.
337 * From xfs_inactive() we will pull the inode from
338 * the list and free it.
340 error = xfs_iunlink(tp, ip);
346 * This gets called when the inode's version needs to be changed from 1 to 2.
347 * Currently this happens when the nlink field overflows the old 16-bit value
348 * or when chproj is called to change the project for the first time.
349 * As a side effect the superblock version will also get rev'd
350 * to contain the NLINK bit.
360 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
361 ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1);
363 ip->i_d.di_version = XFS_DINODE_VERSION_2;
364 ip->i_d.di_onlink = 0;
365 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
367 if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
369 if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
370 XFS_SB_VERSION_ADDNLINK(&mp->m_sb);
371 XFS_SB_UNLOCK(mp, s);
372 xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
374 XFS_SB_UNLOCK(mp, s);
377 /* Caller must log the inode */
381 * Increment the link count on an inode & log the change.
388 if (ip->i_d.di_nlink >= XFS_MAXLINK)
389 return XFS_ERROR(EMLINK);
390 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
392 ASSERT(ip->i_d.di_nlink > 0);
394 if ((ip->i_d.di_version == XFS_DINODE_VERSION_1) &&
395 (ip->i_d.di_nlink > XFS_MAXLINK_1)) {
397 * The inode has increased its number of links beyond
398 * what can fit in an old format inode. It now needs
399 * to be converted to a version 2 inode with a 32 bit
400 * link count. If this is the first inode in the file
401 * system to do this, then we need to bump the superblock
402 * version number as well.
404 xfs_bump_ino_vers2(tp, ip);
407 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
412 * Try to truncate the given file to 0 length. Currently called
413 * only out of xfs_remove when it has to truncate a file to free
414 * up space for the remove to proceed.
426 * This is called to truncate the quotainodes too.
428 if (XFS_IS_UQUOTA_ON(mp)) {
429 if (ip->i_ino != mp->m_sb.sb_uquotino)
430 ASSERT(ip->i_udquot);
432 if (XFS_IS_OQUOTA_ON(mp)) {
433 if (ip->i_ino != mp->m_sb.sb_gquotino)
434 ASSERT(ip->i_gdquot);
438 * Make the call to xfs_itruncate_start before starting the
439 * transaction, because we cannot make the call while we're
442 xfs_ilock(ip, XFS_IOLOCK_EXCL);
443 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, (xfs_fsize_t)0);
445 tp = xfs_trans_alloc(mp, XFS_TRANS_TRUNCATE_FILE);
446 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
447 XFS_TRANS_PERM_LOG_RES,
448 XFS_ITRUNCATE_LOG_COUNT))) {
449 xfs_trans_cancel(tp, 0);
450 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
455 * Follow the normal truncate locking protocol. Since we
456 * hold the inode in the transaction, we know that it's number
457 * of references will stay constant.
459 xfs_ilock(ip, XFS_ILOCK_EXCL);
460 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
461 xfs_trans_ihold(tp, ip);
463 * Signal a sync xaction. The only case where that isn't
464 * the case is if we're truncating an already unlinked file
465 * on a wsync fs. In that case, we know the blocks can't
466 * reappear in the file because the links to file are
467 * permanently toast. Currently, we're always going to
468 * want a sync transaction because this code is being
469 * called from places where nlink is guaranteed to be 1
470 * but I'm leaving the tests in to protect against future
473 error = xfs_itruncate_finish(&tp, ip, (xfs_fsize_t)0,
475 ((ip->i_d.di_nlink != 0 ||
476 !(mp->m_flags & XFS_MOUNT_WSYNC))
479 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
482 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
483 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES,
486 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);