Linux v2.6.13
[linux-2.6/next.git] / fs / xfs / xfs_trans_inode.c
blob7e7631ca49793b6ad8b2cad24d3a0e9ab530f442
1 /*
2 * Copyright (c) 2000 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:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include "xfs.h"
34 #include "xfs_macros.h"
35 #include "xfs_types.h"
36 #include "xfs_inum.h"
37 #include "xfs_log.h"
38 #include "xfs_trans.h"
39 #include "xfs_sb.h"
40 #include "xfs_ag.h"
41 #include "xfs_dir.h"
42 #include "xfs_dir2.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_trans_priv.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode_item.h"
56 #include "xfs_inode.h"
58 #ifdef XFS_TRANS_DEBUG
59 STATIC void
60 xfs_trans_inode_broot_debug(
61 xfs_inode_t *ip);
62 #else
63 #define xfs_trans_inode_broot_debug(ip)
64 #endif
68 * Get and lock the inode for the caller if it is not already
69 * locked within the given transaction. If it is already locked
70 * within the transaction, just increment its lock recursion count
71 * and return a pointer to it.
73 * For an inode to be locked in a transaction, the inode lock, as
74 * opposed to the io lock, must be taken exclusively. This ensures
75 * that the inode can be involved in only 1 transaction at a time.
76 * Lock recursion is handled on the io lock, but only for lock modes
77 * of equal or lesser strength. That is, you can recur on the io lock
78 * held EXCL with a SHARED request but not vice versa. Also, if
79 * the inode is already a part of the transaction then you cannot
80 * go from not holding the io lock to having it EXCL or SHARED.
82 * Use the inode cache routine xfs_inode_incore() to find the inode
83 * if it is already owned by this transaction.
85 * If we don't already own the inode, use xfs_iget() to get it.
86 * Since the inode log item structure is embedded in the incore
87 * inode structure and is initialized when the inode is brought
88 * into memory, there is nothing to do with it here.
90 * If the given transaction pointer is NULL, just call xfs_iget().
91 * This simplifies code which must handle both cases.
93 int
94 xfs_trans_iget(
95 xfs_mount_t *mp,
96 xfs_trans_t *tp,
97 xfs_ino_t ino,
98 uint flags,
99 uint lock_flags,
100 xfs_inode_t **ipp)
102 int error;
103 xfs_inode_t *ip;
104 xfs_inode_log_item_t *iip;
107 * If the transaction pointer is NULL, just call the normal
108 * xfs_iget().
110 if (tp == NULL)
111 return xfs_iget(mp, NULL, ino, flags, lock_flags, ipp, 0);
114 * If we find the inode in core with this transaction
115 * pointer in its i_transp field, then we know we already
116 * have it locked. In this case we just increment the lock
117 * recursion count and return the inode to the caller.
118 * Assert that the inode is already locked in the mode requested
119 * by the caller. We cannot do lock promotions yet, so
120 * die if someone gets this wrong.
122 if ((ip = xfs_inode_incore(tp->t_mountp, ino, tp)) != NULL) {
124 * Make sure that the inode lock is held EXCL and
125 * that the io lock is never upgraded when the inode
126 * is already a part of the transaction.
128 ASSERT(ip->i_itemp != NULL);
129 ASSERT(lock_flags & XFS_ILOCK_EXCL);
130 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
131 ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) ||
132 ismrlocked(&ip->i_iolock, MR_UPDATE));
133 ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) ||
134 (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_EXCL));
135 ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) ||
136 ismrlocked(&ip->i_iolock, (MR_UPDATE | MR_ACCESS)));
137 ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) ||
138 (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_ANY));
140 if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
141 ip->i_itemp->ili_iolock_recur++;
143 if (lock_flags & XFS_ILOCK_EXCL) {
144 ip->i_itemp->ili_ilock_recur++;
146 *ipp = ip;
147 return 0;
150 ASSERT(lock_flags & XFS_ILOCK_EXCL);
151 error = xfs_iget(tp->t_mountp, tp, ino, flags, lock_flags, &ip, 0);
152 if (error) {
153 return error;
155 ASSERT(ip != NULL);
158 * Get a log_item_desc to point at the new item.
160 if (ip->i_itemp == NULL)
161 xfs_inode_item_init(ip, mp);
162 iip = ip->i_itemp;
163 (void) xfs_trans_add_item(tp, (xfs_log_item_t *)(iip));
165 xfs_trans_inode_broot_debug(ip);
168 * If the IO lock has been acquired, mark that in
169 * the inode log item so we'll know to unlock it
170 * when the transaction commits.
172 ASSERT(iip->ili_flags == 0);
173 if (lock_flags & XFS_IOLOCK_EXCL) {
174 iip->ili_flags |= XFS_ILI_IOLOCKED_EXCL;
175 } else if (lock_flags & XFS_IOLOCK_SHARED) {
176 iip->ili_flags |= XFS_ILI_IOLOCKED_SHARED;
180 * Initialize i_transp so we can find it with xfs_inode_incore()
181 * above.
183 ip->i_transp = tp;
185 *ipp = ip;
186 return 0;
190 * Add the locked inode to the transaction.
191 * The inode must be locked, and it cannot be associated with any
192 * transaction. The caller must specify the locks already held
193 * on the inode.
195 void
196 xfs_trans_ijoin(
197 xfs_trans_t *tp,
198 xfs_inode_t *ip,
199 uint lock_flags)
201 xfs_inode_log_item_t *iip;
203 ASSERT(ip->i_transp == NULL);
204 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
205 ASSERT(lock_flags & XFS_ILOCK_EXCL);
206 if (ip->i_itemp == NULL)
207 xfs_inode_item_init(ip, ip->i_mount);
208 iip = ip->i_itemp;
209 ASSERT(iip->ili_flags == 0);
210 ASSERT(iip->ili_ilock_recur == 0);
211 ASSERT(iip->ili_iolock_recur == 0);
214 * Get a log_item_desc to point at the new item.
216 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)(iip));
218 xfs_trans_inode_broot_debug(ip);
221 * If the IO lock is already held, mark that in the inode log item.
223 if (lock_flags & XFS_IOLOCK_EXCL) {
224 iip->ili_flags |= XFS_ILI_IOLOCKED_EXCL;
225 } else if (lock_flags & XFS_IOLOCK_SHARED) {
226 iip->ili_flags |= XFS_ILI_IOLOCKED_SHARED;
230 * Initialize i_transp so we can find it with xfs_inode_incore()
231 * in xfs_trans_iget() above.
233 ip->i_transp = tp;
239 * Mark the inode as not needing to be unlocked when the inode item's
240 * IOP_UNLOCK() routine is called. The inode must already be locked
241 * and associated with the given transaction.
243 /*ARGSUSED*/
244 void
245 xfs_trans_ihold(
246 xfs_trans_t *tp,
247 xfs_inode_t *ip)
249 ASSERT(ip->i_transp == tp);
250 ASSERT(ip->i_itemp != NULL);
251 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
253 ip->i_itemp->ili_flags |= XFS_ILI_HOLD;
258 * This is called to mark the fields indicated in fieldmask as needing
259 * to be logged when the transaction is committed. The inode must
260 * already be associated with the given transaction.
262 * The values for fieldmask are defined in xfs_inode_item.h. We always
263 * log all of the core inode if any of it has changed, and we always log
264 * all of the inline data/extents/b-tree root if any of them has changed.
266 void
267 xfs_trans_log_inode(
268 xfs_trans_t *tp,
269 xfs_inode_t *ip,
270 uint flags)
272 xfs_log_item_desc_t *lidp;
274 ASSERT(ip->i_transp == tp);
275 ASSERT(ip->i_itemp != NULL);
276 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
278 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)(ip->i_itemp));
279 ASSERT(lidp != NULL);
281 tp->t_flags |= XFS_TRANS_DIRTY;
282 lidp->lid_flags |= XFS_LID_DIRTY;
285 * Always OR in the bits from the ili_last_fields field.
286 * This is to coordinate with the xfs_iflush() and xfs_iflush_done()
287 * routines in the eventual clearing of the ilf_fields bits.
288 * See the big comment in xfs_iflush() for an explanation of
289 * this coorination mechanism.
291 flags |= ip->i_itemp->ili_last_fields;
292 ip->i_itemp->ili_format.ilf_fields |= flags;
295 #ifdef XFS_TRANS_DEBUG
297 * Keep track of the state of the inode btree root to make sure we
298 * log it properly.
300 STATIC void
301 xfs_trans_inode_broot_debug(
302 xfs_inode_t *ip)
304 xfs_inode_log_item_t *iip;
306 ASSERT(ip->i_itemp != NULL);
307 iip = ip->i_itemp;
308 if (iip->ili_root_size != 0) {
309 ASSERT(iip->ili_orig_root != NULL);
310 kmem_free(iip->ili_orig_root, iip->ili_root_size);
311 iip->ili_root_size = 0;
312 iip->ili_orig_root = NULL;
314 if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
315 ASSERT((ip->i_df.if_broot != NULL) &&
316 (ip->i_df.if_broot_bytes > 0));
317 iip->ili_root_size = ip->i_df.if_broot_bytes;
318 iip->ili_orig_root =
319 (char*)kmem_alloc(iip->ili_root_size, KM_SLEEP);
320 memcpy(iip->ili_orig_root, (char*)(ip->i_df.if_broot),
321 iip->ili_root_size);
324 #endif