blob: c6459514fadbcc9d96f0d29de80d0f1e3d9b9911 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
3 *
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.
7 *
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.
11 *
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.
18 *
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.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_trans.h"
39#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110040#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_dir.h"
42#include "xfs_dir2.h"
43#include "xfs_dmapi.h"
44#include "xfs_mount.h"
45#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_dir_sf.h"
47#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110048#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110051#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "xfs_bmap.h"
53#include "xfs_error.h"
54#include "xfs_quota.h"
55#include "xfs_rw.h"
56#include "xfs_itable.h"
57#include "xfs_utils.h"
58
59/*
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.
65 */
66int
67xfs_get_dir_entry(
68 vname_t *dentry,
69 xfs_inode_t **ipp)
70{
71 vnode_t *vp;
72 bhv_desc_t *bdp;
73
74 vp = VNAME_TO_VNODE(dentry);
75 bdp = vn_bhv_lookup_unlocked(VN_BHV_HEAD(vp), &xfs_vnodeops);
76 if (!bdp) {
77 *ipp = NULL;
78 return XFS_ERROR(ENOENT);
79 }
80 VN_HOLD(vp);
81 *ipp = XFS_BHVTOI(bdp);
82 return 0;
83}
84
85int
86xfs_dir_lookup_int(
87 bhv_desc_t *dir_bdp,
88 uint lock_mode,
89 vname_t *dentry,
90 xfs_ino_t *inum,
91 xfs_inode_t **ipp)
92{
93 vnode_t *dir_vp;
94 xfs_inode_t *dp;
95 int error;
96
97 dir_vp = BHV_TO_VNODE(dir_bdp);
98 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
99
100 dp = XFS_BHVTOI(dir_bdp);
101
102 error = XFS_DIR_LOOKUP(dp->i_mount, NULL, dp,
103 VNAME(dentry), VNAMELEN(dentry), inum);
104 if (!error) {
105 /*
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.
112 */
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);
116
117 if (error) {
118 *ipp = NULL;
119 } else if ((*ipp)->i_d.di_mode == 0) {
120 /*
121 * The inode has been freed. Something is
122 * wrong so just get out of here.
123 */
124 xfs_iunlock(dp, lock_mode);
125 xfs_iput_new(*ipp, 0);
126 *ipp = NULL;
127 xfs_ilock(dp, lock_mode);
128 error = XFS_ERROR(ENOENT);
129 }
130 }
131 return error;
132}
133
134/*
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.
139 *
140 * This routine is designed to be called from xfs_create and
141 * xfs_create_dir.
142 *
143 */
144int
145xfs_dir_ialloc(
146 xfs_trans_t **tpp, /* input: current transaction;
147 output: may be a new transaction. */
148 xfs_inode_t *dp, /* directory within whose allocate
149 the inode. */
150 mode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -0700151 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 xfs_dev_t rdev,
153 cred_t *credp,
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
157 locked. */
158 int *committed)
159
160{
161 xfs_trans_t *tp;
162 xfs_trans_t *ntp;
163 xfs_inode_t *ip;
164 xfs_buf_t *ialloc_context = NULL;
165 boolean_t call_again = B_FALSE;
166 int code;
167 uint log_res;
168 uint log_count;
169 void *dqinfo;
170 uint tflags;
171
172 tp = *tpp;
173 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
174
175 /*
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.
183 *
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.
189 */
190 code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid, okalloc,
191 &ialloc_context, &call_again, &ip);
192
193 /*
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.
197 */
198 if (code) {
199 *ipp = NULL;
200 return code;
201 }
202 if (!call_again && (ip == NULL)) {
203 *ipp = NULL;
204 return XFS_ERROR(ENOSPC);
205 }
206
207 /*
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.
212 */
213 if (call_again) {
214
215 /*
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
220 * allocation group.
221 */
222 xfs_trans_bhold(tp, ialloc_context);
223 /*
224 * Save the log reservation so we can use
225 * them in the next transaction.
226 */
227 log_res = xfs_trans_get_log_res(tp);
228 log_count = xfs_trans_get_log_count(tp);
229
230 /*
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.
234 */
235 dqinfo = NULL;
236 tflags = 0;
237 if (tp->t_dqinfo) {
238 dqinfo = (void *)tp->t_dqinfo;
239 tp->t_dqinfo = NULL;
240 tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
241 tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
242 }
243
244 ntp = xfs_trans_dup(tp);
245 code = xfs_trans_commit(tp, 0, NULL);
246 tp = ntp;
247 if (committed != NULL) {
248 *committed = 1;
249 }
250 /*
251 * If we get an error during the commit processing,
252 * release the buffer that is still held and return
253 * to the caller.
254 */
255 if (code) {
256 xfs_buf_relse(ialloc_context);
257 if (dqinfo) {
258 tp->t_dqinfo = dqinfo;
259 XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp);
260 }
261 *tpp = ntp;
262 *ipp = NULL;
263 return code;
264 }
265 code = xfs_trans_reserve(tp, 0, log_res, 0,
266 XFS_TRANS_PERM_LOG_RES, log_count);
267 /*
268 * Re-attach the quota info that we detached from prev trx.
269 */
270 if (dqinfo) {
271 tp->t_dqinfo = dqinfo;
272 tp->t_flags |= tflags;
273 }
274
275 if (code) {
276 xfs_buf_relse(ialloc_context);
277 *tpp = ntp;
278 *ipp = NULL;
279 return code;
280 }
281 xfs_trans_bjoin(tp, ialloc_context);
282
283 /*
284 * Call ialloc again. Since we've locked out all
285 * other allocations in this allocation group,
286 * this call should always succeed.
287 */
288 code = xfs_ialloc(tp, dp, mode, nlink, rdev, credp, prid,
289 okalloc, &ialloc_context, &call_again, &ip);
290
291 /*
292 * If we get an error at this point, return to the caller
293 * so that the current transaction can be aborted.
294 */
295 if (code) {
296 *tpp = tp;
297 *ipp = NULL;
298 return code;
299 }
300 ASSERT ((!call_again) && (ip != NULL));
301
302 } else {
303 if (committed != NULL) {
304 *committed = 0;
305 }
306 }
307
308 *ipp = ip;
309 *tpp = tp;
310
311 return 0;
312}
313
314/*
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.
318 */
319int /* error */
320xfs_droplink(
321 xfs_trans_t *tp,
322 xfs_inode_t *ip)
323{
324 int error;
325
326 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
327
328 ASSERT (ip->i_d.di_nlink > 0);
329 ip->i_d.di_nlink--;
330 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
331
332 error = 0;
333 if (ip->i_d.di_nlink == 0) {
334 /*
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.
339 */
340 error = xfs_iunlink(tp, ip);
341 }
342 return error;
343}
344
345/*
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.
351 */
352void
353xfs_bump_ino_vers2(
354 xfs_trans_t *tp,
355 xfs_inode_t *ip)
356{
357 xfs_mount_t *mp;
358 unsigned long s;
359
360 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
361 ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1);
362
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));
366 mp = tp->t_mountp;
367 if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
368 s = XFS_SB_LOCK(mp);
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);
373 } else {
374 XFS_SB_UNLOCK(mp, s);
375 }
376 }
377 /* Caller must log the inode */
378}
379
380/*
381 * Increment the link count on an inode & log the change.
382 */
383int
384xfs_bumplink(
385 xfs_trans_t *tp,
386 xfs_inode_t *ip)
387{
388 if (ip->i_d.di_nlink >= XFS_MAXLINK)
389 return XFS_ERROR(EMLINK);
390 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
391
392 ASSERT(ip->i_d.di_nlink > 0);
393 ip->i_d.di_nlink++;
394 if ((ip->i_d.di_version == XFS_DINODE_VERSION_1) &&
395 (ip->i_d.di_nlink > XFS_MAXLINK_1)) {
396 /*
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.
403 */
404 xfs_bump_ino_vers2(tp, ip);
405 }
406
407 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
408 return 0;
409}
410
411/*
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.
415 */
416int
417xfs_truncate_file(
418 xfs_mount_t *mp,
419 xfs_inode_t *ip)
420{
421 xfs_trans_t *tp;
422 int error;
423
424#ifdef QUOTADEBUG
425 /*
426 * This is called to truncate the quotainodes too.
427 */
428 if (XFS_IS_UQUOTA_ON(mp)) {
429 if (ip->i_ino != mp->m_sb.sb_uquotino)
430 ASSERT(ip->i_udquot);
431 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000432 if (XFS_IS_OQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (ip->i_ino != mp->m_sb.sb_gquotino)
434 ASSERT(ip->i_gdquot);
435 }
436#endif
437 /*
438 * Make the call to xfs_itruncate_start before starting the
439 * transaction, because we cannot make the call while we're
440 * in a transaction.
441 */
442 xfs_ilock(ip, XFS_IOLOCK_EXCL);
443 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, (xfs_fsize_t)0);
444
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);
451 return error;
452 }
453
454 /*
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.
458 */
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);
462 /*
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
471 * changes -- rcc.
472 */
473 error = xfs_itruncate_finish(&tp, ip, (xfs_fsize_t)0,
474 XFS_DATA_FORK,
475 ((ip->i_d.di_nlink != 0 ||
476 !(mp->m_flags & XFS_MOUNT_WSYNC))
477 ? 1 : 0));
478 if (error) {
479 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
480 XFS_TRANS_ABORT);
481 } else {
482 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
483 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES,
484 NULL);
485 }
486 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
487
488 return error;
489}