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:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #include "xfs_macros.h"
35 #include "xfs_types.h"
38 #include "xfs_trans.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
60 xfs_trans_inode_broot_debug(
63 #define xfs_trans_inode_broot_debug(ip)
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.
104 xfs_inode_log_item_t
*iip
;
107 * If the transaction pointer is NULL, just call the normal
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
++;
150 ASSERT(lock_flags
& XFS_ILOCK_EXCL
);
151 error
= xfs_iget(tp
->t_mountp
, tp
, ino
, flags
, lock_flags
, &ip
, 0);
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
);
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()
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
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
);
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.
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.
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
;
257 * Cancel the previous inode hold request made on this inode
258 * for this transaction.
262 xfs_trans_ihold_release(
266 ASSERT(ip
->i_transp
== tp
);
267 ASSERT(ip
->i_itemp
!= NULL
);
268 ASSERT(ismrlocked(&ip
->i_lock
, MR_UPDATE
));
269 ASSERT(ip
->i_itemp
->ili_flags
& XFS_ILI_HOLD
);
271 ip
->i_itemp
->ili_flags
&= ~XFS_ILI_HOLD
;
276 * This is called to mark the fields indicated in fieldmask as needing
277 * to be logged when the transaction is committed. The inode must
278 * already be associated with the given transaction.
280 * The values for fieldmask are defined in xfs_inode_item.h. We always
281 * log all of the core inode if any of it has changed, and we always log
282 * all of the inline data/extents/b-tree root if any of them has changed.
290 xfs_log_item_desc_t
*lidp
;
292 ASSERT(ip
->i_transp
== tp
);
293 ASSERT(ip
->i_itemp
!= NULL
);
294 ASSERT(ismrlocked(&ip
->i_lock
, MR_UPDATE
));
296 lidp
= xfs_trans_find_item(tp
, (xfs_log_item_t
*)(ip
->i_itemp
));
297 ASSERT(lidp
!= NULL
);
299 tp
->t_flags
|= XFS_TRANS_DIRTY
;
300 lidp
->lid_flags
|= XFS_LID_DIRTY
;
303 * Always OR in the bits from the ili_last_fields field.
304 * This is to coordinate with the xfs_iflush() and xfs_iflush_done()
305 * routines in the eventual clearing of the ilf_fields bits.
306 * See the big comment in xfs_iflush() for an explanation of
307 * this coorination mechanism.
309 flags
|= ip
->i_itemp
->ili_last_fields
;
310 ip
->i_itemp
->ili_format
.ilf_fields
|= flags
;
313 #ifdef XFS_TRANS_DEBUG
315 * Keep track of the state of the inode btree root to make sure we
319 xfs_trans_inode_broot_debug(
322 xfs_inode_log_item_t
*iip
;
324 ASSERT(ip
->i_itemp
!= NULL
);
326 if (iip
->ili_root_size
!= 0) {
327 ASSERT(iip
->ili_orig_root
!= NULL
);
328 kmem_free(iip
->ili_orig_root
, iip
->ili_root_size
);
329 iip
->ili_root_size
= 0;
330 iip
->ili_orig_root
= NULL
;
332 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
) {
333 ASSERT((ip
->i_df
.if_broot
!= NULL
) &&
334 (ip
->i_df
.if_broot_bytes
> 0));
335 iip
->ili_root_size
= ip
->i_df
.if_broot_bytes
;
337 (char*)kmem_alloc(iip
->ili_root_size
, KM_SLEEP
);
338 memcpy(iip
->ili_orig_root
, (char*)(ip
->i_df
.if_broot
),