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.
103 xfs_inode_log_item_t
*iip
;
106 * If the transaction pointer is NULL, just call the normal
110 return (xfs_iget(mp
, NULL
, ino
, 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
, 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()
191 * Add the locked inode to the transaction.
192 * The inode must be locked, and it cannot be associated with any
193 * transaction. The caller must specify the locks already held
202 xfs_inode_log_item_t
*iip
;
204 ASSERT(ip
->i_transp
== NULL
);
205 ASSERT(ismrlocked(&ip
->i_lock
, MR_UPDATE
));
206 ASSERT(lock_flags
& XFS_ILOCK_EXCL
);
207 if (ip
->i_itemp
== NULL
)
208 xfs_inode_item_init(ip
, ip
->i_mount
);
210 ASSERT(iip
->ili_flags
== 0);
211 ASSERT(iip
->ili_ilock_recur
== 0);
212 ASSERT(iip
->ili_iolock_recur
== 0);
215 * Get a log_item_desc to point at the new item.
217 (void) xfs_trans_add_item(tp
, (xfs_log_item_t
*)(iip
));
219 xfs_trans_inode_broot_debug(ip
);
222 * If the IO lock is already held, mark that in the inode log item.
224 if (lock_flags
& XFS_IOLOCK_EXCL
) {
225 iip
->ili_flags
|= XFS_ILI_IOLOCKED_EXCL
;
226 } else if (lock_flags
& XFS_IOLOCK_SHARED
) {
227 iip
->ili_flags
|= XFS_ILI_IOLOCKED_SHARED
;
231 * Initialize i_transp so we can find it with xfs_inode_incore()
232 * in xfs_trans_iget() above.
240 * Mark the inode as not needing to be unlocked when the inode item's
241 * IOP_UNLOCK() routine is called. The inode must already be locked
242 * and associated with the given transaction.
250 ASSERT(ip
->i_transp
== tp
);
251 ASSERT(ip
->i_itemp
!= NULL
);
252 ASSERT(ismrlocked(&ip
->i_lock
, MR_UPDATE
));
254 ip
->i_itemp
->ili_flags
|= XFS_ILI_HOLD
;
258 * Cancel the previous inode hold request made on this inode
259 * for this transaction.
263 xfs_trans_ihold_release(
267 ASSERT(ip
->i_transp
== tp
);
268 ASSERT(ip
->i_itemp
!= NULL
);
269 ASSERT(ismrlocked(&ip
->i_lock
, MR_UPDATE
));
270 ASSERT(ip
->i_itemp
->ili_flags
& XFS_ILI_HOLD
);
272 ip
->i_itemp
->ili_flags
&= ~XFS_ILI_HOLD
;
277 * This is called to mark the fields indicated in fieldmask as needing
278 * to be logged when the transaction is committed. The inode must
279 * already be associated with the given transaction.
281 * The values for fieldmask are defined in xfs_inode_item.h. We always
282 * log all of the core inode if any of it has changed, and we always log
283 * all of the inline data/extents/b-tree root if any of them has changed.
291 xfs_log_item_desc_t
*lidp
;
293 ASSERT(ip
->i_transp
== tp
);
294 ASSERT(ip
->i_itemp
!= NULL
);
295 ASSERT(ismrlocked(&ip
->i_lock
, MR_UPDATE
));
297 lidp
= xfs_trans_find_item(tp
, (xfs_log_item_t
*)(ip
->i_itemp
));
298 ASSERT(lidp
!= NULL
);
300 tp
->t_flags
|= XFS_TRANS_DIRTY
;
301 lidp
->lid_flags
|= XFS_LID_DIRTY
;
304 * Always OR in the bits from the ili_last_fields field.
305 * This is to coordinate with the xfs_iflush() and xfs_iflush_done()
306 * routines in the eventual clearing of the ilf_fields bits.
307 * See the big comment in xfs_iflush() for an explanation of
308 * this coorination mechanism.
310 flags
|= ip
->i_itemp
->ili_last_fields
;
311 ip
->i_itemp
->ili_format
.ilf_fields
|= flags
;
314 #ifdef XFS_TRANS_DEBUG
316 * Keep track of the state of the inode btree root to make sure we
320 xfs_trans_inode_broot_debug(
323 xfs_inode_log_item_t
*iip
;
325 ASSERT(ip
->i_itemp
!= NULL
);
327 if (iip
->ili_root_size
!= 0) {
328 ASSERT(iip
->ili_orig_root
!= NULL
);
329 kmem_free(iip
->ili_orig_root
, iip
->ili_root_size
);
330 iip
->ili_root_size
= 0;
331 iip
->ili_orig_root
= NULL
;
333 if (ip
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
) {
334 ASSERT((ip
->i_df
.if_broot
!= NULL
) &&
335 (ip
->i_df
.if_broot_bytes
> 0));
336 iip
->ili_root_size
= ip
->i_df
.if_broot_bytes
;
338 (char*)kmem_alloc(iip
->ili_root_size
, KM_SLEEP
);
339 memcpy(iip
->ili_orig_root
, (char*)(ip
->i_df
.if_broot
),