Merge master.kernel.org:/home/rmk/linux-2.6-mmc
[linux-2.6/mini2440.git] / fs / xfs / xfs_trans_inode.c
blobe341409172d20c3de97f599c25652df1071ddbf3
1 /*
2 * Copyright (c) 2000,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir.h"
28 #include "xfs_dir2.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir_sf.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_btree.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_trans_priv.h"
42 #include "xfs_inode_item.h"
44 #ifdef XFS_TRANS_DEBUG
45 STATIC void
46 xfs_trans_inode_broot_debug(
47 xfs_inode_t *ip);
48 #else
49 #define xfs_trans_inode_broot_debug(ip)
50 #endif
54 * Get and lock the inode for the caller if it is not already
55 * locked within the given transaction. If it is already locked
56 * within the transaction, just increment its lock recursion count
57 * and return a pointer to it.
59 * For an inode to be locked in a transaction, the inode lock, as
60 * opposed to the io lock, must be taken exclusively. This ensures
61 * that the inode can be involved in only 1 transaction at a time.
62 * Lock recursion is handled on the io lock, but only for lock modes
63 * of equal or lesser strength. That is, you can recur on the io lock
64 * held EXCL with a SHARED request but not vice versa. Also, if
65 * the inode is already a part of the transaction then you cannot
66 * go from not holding the io lock to having it EXCL or SHARED.
68 * Use the inode cache routine xfs_inode_incore() to find the inode
69 * if it is already owned by this transaction.
71 * If we don't already own the inode, use xfs_iget() to get it.
72 * Since the inode log item structure is embedded in the incore
73 * inode structure and is initialized when the inode is brought
74 * into memory, there is nothing to do with it here.
76 * If the given transaction pointer is NULL, just call xfs_iget().
77 * This simplifies code which must handle both cases.
79 int
80 xfs_trans_iget(
81 xfs_mount_t *mp,
82 xfs_trans_t *tp,
83 xfs_ino_t ino,
84 uint flags,
85 uint lock_flags,
86 xfs_inode_t **ipp)
88 int error;
89 xfs_inode_t *ip;
90 xfs_inode_log_item_t *iip;
93 * If the transaction pointer is NULL, just call the normal
94 * xfs_iget().
96 if (tp == NULL)
97 return xfs_iget(mp, NULL, ino, flags, lock_flags, ipp, 0);
100 * If we find the inode in core with this transaction
101 * pointer in its i_transp field, then we know we already
102 * have it locked. In this case we just increment the lock
103 * recursion count and return the inode to the caller.
104 * Assert that the inode is already locked in the mode requested
105 * by the caller. We cannot do lock promotions yet, so
106 * die if someone gets this wrong.
108 if ((ip = xfs_inode_incore(tp->t_mountp, ino, tp)) != NULL) {
110 * Make sure that the inode lock is held EXCL and
111 * that the io lock is never upgraded when the inode
112 * is already a part of the transaction.
114 ASSERT(ip->i_itemp != NULL);
115 ASSERT(lock_flags & XFS_ILOCK_EXCL);
116 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
117 ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) ||
118 ismrlocked(&ip->i_iolock, MR_UPDATE));
119 ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) ||
120 (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_EXCL));
121 ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) ||
122 ismrlocked(&ip->i_iolock, (MR_UPDATE | MR_ACCESS)));
123 ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) ||
124 (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_ANY));
126 if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
127 ip->i_itemp->ili_iolock_recur++;
129 if (lock_flags & XFS_ILOCK_EXCL) {
130 ip->i_itemp->ili_ilock_recur++;
132 *ipp = ip;
133 return 0;
136 ASSERT(lock_flags & XFS_ILOCK_EXCL);
137 error = xfs_iget(tp->t_mountp, tp, ino, flags, lock_flags, &ip, 0);
138 if (error) {
139 return error;
141 ASSERT(ip != NULL);
144 * Get a log_item_desc to point at the new item.
146 if (ip->i_itemp == NULL)
147 xfs_inode_item_init(ip, mp);
148 iip = ip->i_itemp;
149 (void) xfs_trans_add_item(tp, (xfs_log_item_t *)(iip));
151 xfs_trans_inode_broot_debug(ip);
154 * If the IO lock has been acquired, mark that in
155 * the inode log item so we'll know to unlock it
156 * when the transaction commits.
158 ASSERT(iip->ili_flags == 0);
159 if (lock_flags & XFS_IOLOCK_EXCL) {
160 iip->ili_flags |= XFS_ILI_IOLOCKED_EXCL;
161 } else if (lock_flags & XFS_IOLOCK_SHARED) {
162 iip->ili_flags |= XFS_ILI_IOLOCKED_SHARED;
166 * Initialize i_transp so we can find it with xfs_inode_incore()
167 * above.
169 ip->i_transp = tp;
171 *ipp = ip;
172 return 0;
176 * Add the locked inode to the transaction.
177 * The inode must be locked, and it cannot be associated with any
178 * transaction. The caller must specify the locks already held
179 * on the inode.
181 void
182 xfs_trans_ijoin(
183 xfs_trans_t *tp,
184 xfs_inode_t *ip,
185 uint lock_flags)
187 xfs_inode_log_item_t *iip;
189 ASSERT(ip->i_transp == NULL);
190 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
191 ASSERT(lock_flags & XFS_ILOCK_EXCL);
192 if (ip->i_itemp == NULL)
193 xfs_inode_item_init(ip, ip->i_mount);
194 iip = ip->i_itemp;
195 ASSERT(iip->ili_flags == 0);
196 ASSERT(iip->ili_ilock_recur == 0);
197 ASSERT(iip->ili_iolock_recur == 0);
200 * Get a log_item_desc to point at the new item.
202 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)(iip));
204 xfs_trans_inode_broot_debug(ip);
207 * If the IO lock is already held, mark that in the inode log item.
209 if (lock_flags & XFS_IOLOCK_EXCL) {
210 iip->ili_flags |= XFS_ILI_IOLOCKED_EXCL;
211 } else if (lock_flags & XFS_IOLOCK_SHARED) {
212 iip->ili_flags |= XFS_ILI_IOLOCKED_SHARED;
216 * Initialize i_transp so we can find it with xfs_inode_incore()
217 * in xfs_trans_iget() above.
219 ip->i_transp = tp;
225 * Mark the inode as not needing to be unlocked when the inode item's
226 * IOP_UNLOCK() routine is called. The inode must already be locked
227 * and associated with the given transaction.
229 /*ARGSUSED*/
230 void
231 xfs_trans_ihold(
232 xfs_trans_t *tp,
233 xfs_inode_t *ip)
235 ASSERT(ip->i_transp == tp);
236 ASSERT(ip->i_itemp != NULL);
237 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
239 ip->i_itemp->ili_flags |= XFS_ILI_HOLD;
244 * This is called to mark the fields indicated in fieldmask as needing
245 * to be logged when the transaction is committed. The inode must
246 * already be associated with the given transaction.
248 * The values for fieldmask are defined in xfs_inode_item.h. We always
249 * log all of the core inode if any of it has changed, and we always log
250 * all of the inline data/extents/b-tree root if any of them has changed.
252 void
253 xfs_trans_log_inode(
254 xfs_trans_t *tp,
255 xfs_inode_t *ip,
256 uint flags)
258 xfs_log_item_desc_t *lidp;
260 ASSERT(ip->i_transp == tp);
261 ASSERT(ip->i_itemp != NULL);
262 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
264 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)(ip->i_itemp));
265 ASSERT(lidp != NULL);
267 tp->t_flags |= XFS_TRANS_DIRTY;
268 lidp->lid_flags |= XFS_LID_DIRTY;
271 * Always OR in the bits from the ili_last_fields field.
272 * This is to coordinate with the xfs_iflush() and xfs_iflush_done()
273 * routines in the eventual clearing of the ilf_fields bits.
274 * See the big comment in xfs_iflush() for an explanation of
275 * this coorination mechanism.
277 flags |= ip->i_itemp->ili_last_fields;
278 ip->i_itemp->ili_format.ilf_fields |= flags;
281 #ifdef XFS_TRANS_DEBUG
283 * Keep track of the state of the inode btree root to make sure we
284 * log it properly.
286 STATIC void
287 xfs_trans_inode_broot_debug(
288 xfs_inode_t *ip)
290 xfs_inode_log_item_t *iip;
292 ASSERT(ip->i_itemp != NULL);
293 iip = ip->i_itemp;
294 if (iip->ili_root_size != 0) {
295 ASSERT(iip->ili_orig_root != NULL);
296 kmem_free(iip->ili_orig_root, iip->ili_root_size);
297 iip->ili_root_size = 0;
298 iip->ili_orig_root = NULL;
300 if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
301 ASSERT((ip->i_df.if_broot != NULL) &&
302 (ip->i_df.if_broot_bytes > 0));
303 iip->ili_root_size = ip->i_df.if_broot_bytes;
304 iip->ili_orig_root =
305 (char*)kmem_alloc(iip->ili_root_size, KM_SLEEP);
306 memcpy(iip->ili_orig_root, (char*)(ip->i_df.if_broot),
307 iip->ili_root_size);
310 #endif