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_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"
37 #include "xfs_error.h"
38 #include "xfs_quota.h"
40 #include "xfs_itable.h"
41 #include "xfs_utils.h"
48 struct xfs_name *name,
56 error = xfs_dir_lookup(NULL, dp, name, inum);
59 * Unlock the directory. We do this because we can't
60 * hold the directory lock while doing the vn_get()
61 * in xfs_iget(). Doing so could cause us to hold
62 * a lock while waiting for the inode to finish
63 * being inactive while it's waiting for a log
64 * reservation in the inactive routine.
66 xfs_iunlock(dp, lock_mode);
67 error = xfs_iget(dp->i_mount, NULL, *inum, 0, 0, ipp, 0);
68 xfs_ilock(dp, lock_mode);
72 } else if ((*ipp)->i_d.di_mode == 0) {
74 * The inode has been freed. Something is
75 * wrong so just get out of here.
77 xfs_iunlock(dp, lock_mode);
78 xfs_iput_new(*ipp, 0);
80 xfs_ilock(dp, lock_mode);
81 error = XFS_ERROR(ENOENT);
88 * Allocates a new inode from disk and return a pointer to the
89 * incore copy. This routine will internally commit the current
90 * transaction and allocate a new one if the Space Manager needed
91 * to do an allocation to replenish the inode free-list.
93 * This routine is designed to be called from xfs_create and
99 xfs_trans_t **tpp, /* input: current transaction;
100 output: may be a new transaction. */
101 xfs_inode_t *dp, /* directory within whose allocate
107 prid_t prid, /* project id */
108 int okalloc, /* ok to allocate new space */
109 xfs_inode_t **ipp, /* pointer to inode; it will be
117 xfs_buf_t *ialloc_context = NULL;
118 boolean_t call_again = B_FALSE;
126 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
129 * xfs_ialloc will return a pointer to an incore inode if
130 * the Space Manager has an available inode on the free
131 * list. Otherwise, it will do an allocation and replenish
132 * the freelist. Since we can only do one allocation per
133 * transaction without deadlocks, we will need to commit the
134 * current transaction and start a new one. We will then
135 * need to call xfs_ialloc again to get the inode.
137 * If xfs_ialloc did an allocation to replenish the freelist,
138 * it returns the bp containing the head of the freelist as
139 * ialloc_context. We will hold a lock on it across the
140 * transaction commit so that no other process can steal
141 * the inode(s) that we've just allocated.
143 code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid, okalloc,
144 &ialloc_context, &call_again, &ip);
147 * Return an error if we were unable to allocate a new inode.
148 * This should only happen if we run out of space on disk or
149 * encounter a disk error.
155 if (!call_again && (ip == NULL)) {
157 return XFS_ERROR(ENOSPC);
161 * If call_again is set, then we were unable to get an
162 * inode in one operation. We need to commit the current
163 * transaction and call xfs_ialloc() again. It is guaranteed
164 * to succeed the second time.
169 * Normally, xfs_trans_commit releases all the locks.
170 * We call bhold to hang on to the ialloc_context across
171 * the commit. Holding this buffer prevents any other
172 * processes from doing any allocations in this
175 xfs_trans_bhold(tp, ialloc_context);
177 * Save the log reservation so we can use
178 * them in the next transaction.
180 log_res = xfs_trans_get_log_res(tp);
181 log_count = xfs_trans_get_log_count(tp);
184 * We want the quota changes to be associated with the next
185 * transaction, NOT this one. So, detach the dqinfo from this
186 * and attach it to the next transaction.
191 dqinfo = (void *)tp->t_dqinfo;
193 tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
194 tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
197 ntp = xfs_trans_dup(tp);
198 code = xfs_trans_commit(tp, 0);
200 if (committed != NULL) {
204 * If we get an error during the commit processing,
205 * release the buffer that is still held and return
209 xfs_buf_relse(ialloc_context);
211 tp->t_dqinfo = dqinfo;
212 XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp);
218 code = xfs_trans_reserve(tp, 0, log_res, 0,
219 XFS_TRANS_PERM_LOG_RES, log_count);
221 * Re-attach the quota info that we detached from prev trx.
224 tp->t_dqinfo = dqinfo;
225 tp->t_flags |= tflags;
229 xfs_buf_relse(ialloc_context);
234 xfs_trans_bjoin(tp, ialloc_context);
237 * Call ialloc again. Since we've locked out all
238 * other allocations in this allocation group,
239 * this call should always succeed.
241 code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid,
242 okalloc, &ialloc_context, &call_again, &ip);
245 * If we get an error at this point, return to the caller
246 * so that the current transaction can be aborted.
253 ASSERT ((!call_again) && (ip != NULL));
256 if (committed != NULL) {
268 * Decrement the link count on an inode & log the change.
269 * If this causes the link count to go to zero, initiate the
270 * logging activity required to truncate a file.
279 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
281 ASSERT (ip->i_d.di_nlink > 0);
283 drop_nlink(ip->i_vnode);
284 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
287 if (ip->i_d.di_nlink == 0) {
289 * We're dropping the last link to this file.
290 * Move the on-disk inode to the AGI unlinked list.
291 * From xfs_inactive() we will pull the inode from
292 * the list and free it.
294 error = xfs_iunlink(tp, ip);
300 * This gets called when the inode's version needs to be changed from 1 to 2.
301 * Currently this happens when the nlink field overflows the old 16-bit value
302 * or when chproj is called to change the project for the first time.
303 * As a side effect the superblock version will also get rev'd
304 * to contain the NLINK bit.
313 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
314 ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1);
316 ip->i_d.di_version = XFS_DINODE_VERSION_2;
317 ip->i_d.di_onlink = 0;
318 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
320 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
321 spin_lock(&mp->m_sb_lock);
322 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
323 xfs_sb_version_addnlink(&mp->m_sb);
324 spin_unlock(&mp->m_sb_lock);
325 xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
327 spin_unlock(&mp->m_sb_lock);
330 /* Caller must log the inode */
334 * Increment the link count on an inode & log the change.
341 if (ip->i_d.di_nlink >= XFS_MAXLINK)
342 return XFS_ERROR(EMLINK);
343 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
345 ASSERT(ip->i_d.di_nlink > 0);
347 inc_nlink(ip->i_vnode);
348 if ((ip->i_d.di_version == XFS_DINODE_VERSION_1) &&
349 (ip->i_d.di_nlink > XFS_MAXLINK_1)) {
351 * The inode has increased its number of links beyond
352 * what can fit in an old format inode. It now needs
353 * to be converted to a version 2 inode with a 32 bit
354 * link count. If this is the first inode in the file
355 * system to do this, then we need to bump the superblock
356 * version number as well.
358 xfs_bump_ino_vers2(tp, ip);
361 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
366 * Try to truncate the given file to 0 length. Currently called
367 * only out of xfs_remove when it has to truncate a file to free
368 * up space for the remove to proceed.
380 * This is called to truncate the quotainodes too.
382 if (XFS_IS_UQUOTA_ON(mp)) {
383 if (ip->i_ino != mp->m_sb.sb_uquotino)
384 ASSERT(ip->i_udquot);
386 if (XFS_IS_OQUOTA_ON(mp)) {
387 if (ip->i_ino != mp->m_sb.sb_gquotino)
388 ASSERT(ip->i_gdquot);
392 * Make the call to xfs_itruncate_start before starting the
393 * transaction, because we cannot make the call while we're
396 xfs_ilock(ip, XFS_IOLOCK_EXCL);
397 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, (xfs_fsize_t)0);
399 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
403 tp = xfs_trans_alloc(mp, XFS_TRANS_TRUNCATE_FILE);
404 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
405 XFS_TRANS_PERM_LOG_RES,
406 XFS_ITRUNCATE_LOG_COUNT))) {
407 xfs_trans_cancel(tp, 0);
408 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
413 * Follow the normal truncate locking protocol. Since we
414 * hold the inode in the transaction, we know that it's number
415 * of references will stay constant.
417 xfs_ilock(ip, XFS_ILOCK_EXCL);
418 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
419 xfs_trans_ihold(tp, ip);
421 * Signal a sync xaction. The only case where that isn't
422 * the case is if we're truncating an already unlinked file
423 * on a wsync fs. In that case, we know the blocks can't
424 * reappear in the file because the links to file are
425 * permanently toast. Currently, we're always going to
426 * want a sync transaction because this code is being
427 * called from places where nlink is guaranteed to be 1
428 * but I'm leaving the tests in to protect against future
431 error = xfs_itruncate_finish(&tp, ip, (xfs_fsize_t)0,
433 ((ip->i_d.di_nlink != 0 ||
434 !(mp->m_flags & XFS_MOUNT_WSYNC))
437 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
440 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
441 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
443 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);