blob: bf4dc5eb4cfc77c673c6843ec1ad1c12fb808182 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dinode.h"
36#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_btree.h"
38#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_quota.h"
40#include "xfs_utils.h"
David Chinner783a2f62008-10-30 17:39:58 +110041#include "xfs_trans_priv.h"
42#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
David Chinner6441e542008-10-30 17:21:19 +110045 * Check the validity of the inode we just found it the cache
46 */
47static int
48xfs_iget_cache_hit(
David Chinner6441e542008-10-30 17:21:19 +110049 struct xfs_perag *pag,
50 struct xfs_inode *ip,
51 int flags,
52 int lock_flags) __releases(pag->pag_ici_lock)
53{
54 struct xfs_mount *mp = ip->i_mount;
David Chinner6bfb3d02008-10-30 18:32:43 +110055 int error = EAGAIN;
David Chinner6441e542008-10-30 17:21:19 +110056
57 /*
58 * If INEW is set this inode is being set up
David Chinnerbf904242008-10-30 17:36:14 +110059 * If IRECLAIM is set this inode is being torn down
David Chinner6441e542008-10-30 17:21:19 +110060 * Pause and try again.
61 */
David Chinnerbf904242008-10-30 17:36:14 +110062 if (xfs_iflags_test(ip, (XFS_INEW|XFS_IRECLAIM))) {
David Chinner6441e542008-10-30 17:21:19 +110063 XFS_STATS_INC(xs_ig_frecycle);
64 goto out_error;
65 }
66
David Chinnerbf904242008-10-30 17:36:14 +110067 /* If IRECLAIMABLE is set, we've torn down the vfs inode part */
68 if (xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
David Chinner6441e542008-10-30 17:21:19 +110069
70 /*
David Chinnerbf904242008-10-30 17:36:14 +110071 * If lookup is racing with unlink, then we should return an
72 * error immediately so we don't remove it from the reclaim
73 * list and potentially leak the inode.
David Chinner6441e542008-10-30 17:21:19 +110074 */
David Chinnerbf904242008-10-30 17:36:14 +110075 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
David Chinner6441e542008-10-30 17:21:19 +110076 error = ENOENT;
77 goto out_error;
78 }
David Chinnerbf904242008-10-30 17:36:14 +110079
David Chinner6441e542008-10-30 17:21:19 +110080 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
81
David Chinnerbf904242008-10-30 17:36:14 +110082 /*
83 * We need to re-initialise the VFS inode as it has been
84 * 'freed' by the VFS. Do this here so we can deal with
85 * errors cleanly, then tag it so it can be set up correctly
86 * later.
87 */
88 if (!inode_init_always(mp->m_super, VFS_I(ip))) {
89 error = ENOMEM;
90 goto out_error;
91 }
David Chinner6bfb3d02008-10-30 18:32:43 +110092
93 /*
94 * We must set the XFS_INEW flag before clearing the
95 * XFS_IRECLAIMABLE flag so that if a racing lookup does
96 * not find the XFS_IRECLAIMABLE above but has the igrab()
97 * below succeed we can safely check XFS_INEW to detect
98 * that this inode is still being initialised.
99 */
David Chinnerbf904242008-10-30 17:36:14 +1100100 xfs_iflags_set(ip, XFS_INEW);
David Chinner6441e542008-10-30 17:21:19 +1100101 xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100102
103 /* clear the radix tree reclaim flag as well. */
104 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
David Chinnerbf904242008-10-30 17:36:14 +1100105 } else if (!igrab(VFS_I(ip))) {
106 /* If the VFS inode is being torn down, pause and try again. */
David Chinnerbf904242008-10-30 17:36:14 +1100107 XFS_STATS_INC(xs_ig_frecycle);
108 goto out_error;
David Chinner6bfb3d02008-10-30 18:32:43 +1100109 } else if (xfs_iflags_test(ip, XFS_INEW)) {
110 /*
111 * We are racing with another cache hit that is
112 * currently recycling this inode out of the XFS_IRECLAIMABLE
113 * state. Wait for the initialisation to complete before
114 * continuing.
115 */
116 wait_on_inode(VFS_I(ip));
David Chinner6441e542008-10-30 17:21:19 +1100117 }
118
119 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
120 error = ENOENT;
David Chinner6bfb3d02008-10-30 18:32:43 +1100121 iput(VFS_I(ip));
122 goto out_error;
David Chinner6441e542008-10-30 17:21:19 +1100123 }
124
David Chinner6bfb3d02008-10-30 18:32:43 +1100125 /* We've got a live one. */
126 read_unlock(&pag->pag_ici_lock);
127
David Chinner6441e542008-10-30 17:21:19 +1100128 if (lock_flags != 0)
129 xfs_ilock(ip, lock_flags);
130
131 xfs_iflags_clear(ip, XFS_ISTALE);
132 xfs_itrace_exit_tag(ip, "xfs_iget.found");
133 XFS_STATS_INC(xs_ig_found);
134 return 0;
135
136out_error:
137 read_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100138 return error;
139}
140
141
142static int
143xfs_iget_cache_miss(
144 struct xfs_mount *mp,
145 struct xfs_perag *pag,
146 xfs_trans_t *tp,
147 xfs_ino_t ino,
148 struct xfs_inode **ipp,
149 xfs_daddr_t bno,
150 int flags,
151 int lock_flags) __releases(pag->pag_ici_lock)
152{
153 struct xfs_inode *ip;
154 int error;
155 unsigned long first_index, mask;
156 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
157
158 /*
159 * Read the disk inode attributes into a new inode structure and get
160 * a new vnode for it. This should also initialize i_ino and i_mount.
161 */
162 error = xfs_iread(mp, tp, ino, &ip, bno,
163 (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
164 if (error)
165 return error;
166
167 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
168
169 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
170 error = ENOENT;
171 goto out_destroy;
172 }
173
David Chinner56e73ec2008-10-30 17:55:27 +1100174 if (lock_flags)
175 xfs_ilock(ip, lock_flags);
176
David Chinner6441e542008-10-30 17:21:19 +1100177 /*
178 * Preload the radix tree so we can insert safely under the
David Chinner56e73ec2008-10-30 17:55:27 +1100179 * write spinlock. Note that we cannot sleep inside the preload
180 * region.
David Chinner6441e542008-10-30 17:21:19 +1100181 */
182 if (radix_tree_preload(GFP_KERNEL)) {
183 error = EAGAIN;
David Chinner56e73ec2008-10-30 17:55:27 +1100184 goto out_unlock;
David Chinner6441e542008-10-30 17:21:19 +1100185 }
186
David Chinner6441e542008-10-30 17:21:19 +1100187 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
188 first_index = agino & mask;
189 write_lock(&pag->pag_ici_lock);
190
191 /* insert the new inode */
192 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
193 if (unlikely(error)) {
194 WARN_ON(error != -EEXIST);
195 XFS_STATS_INC(xs_ig_dup);
196 error = EAGAIN;
David Chinner56e73ec2008-10-30 17:55:27 +1100197 goto out_preload_end;
David Chinner6441e542008-10-30 17:21:19 +1100198 }
199
200 /* These values _must_ be set before releasing the radix tree lock! */
201 ip->i_udquot = ip->i_gdquot = NULL;
202 xfs_iflags_set(ip, XFS_INEW);
203
204 write_unlock(&pag->pag_ici_lock);
205 radix_tree_preload_end();
206 *ipp = ip;
207 return 0;
208
David Chinner56e73ec2008-10-30 17:55:27 +1100209out_preload_end:
David Chinner6441e542008-10-30 17:21:19 +1100210 write_unlock(&pag->pag_ici_lock);
211 radix_tree_preload_end();
David Chinner56e73ec2008-10-30 17:55:27 +1100212out_unlock:
213 if (lock_flags)
214 xfs_iunlock(ip, lock_flags);
David Chinner6441e542008-10-30 17:21:19 +1100215out_destroy:
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100216 xfs_destroy_inode(ip);
David Chinner6441e542008-10-30 17:21:19 +1100217 return error;
218}
219
220/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * Look up an inode by number in the given file system.
David Chinnerda353b02007-08-28 14:00:13 +1000222 * The inode is looked up in the cache held in each AG.
David Chinnerbf904242008-10-30 17:36:14 +1100223 * If the inode is found in the cache, initialise the vfs inode
224 * if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 *
David Chinnerda353b02007-08-28 14:00:13 +1000226 * If it is not in core, read it in from the file system's device,
David Chinnerbf904242008-10-30 17:36:14 +1100227 * add it to the cache and initialise the vfs inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 *
229 * The inode is locked according to the value of the lock_flags parameter.
230 * This flag parameter indicates how and if the inode's IO lock and inode lock
231 * should be taken.
232 *
233 * mp -- the mount point structure for the current file system. It points
234 * to the inode hash table.
235 * tp -- a pointer to the current transaction if there is one. This is
236 * simply passed through to the xfs_iread() call.
237 * ino -- the number of the inode desired. This is the unique identifier
238 * within the file system for the inode being requested.
239 * lock_flags -- flags indicating how to lock the inode. See the comment
240 * for xfs_ilock() for a list of valid values.
241 * bno -- the block number starting the buffer containing the inode,
242 * if known (as by bulkstat), else 0.
243 */
David Chinnerbf904242008-10-30 17:36:14 +1100244int
245xfs_iget(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 xfs_mount_t *mp,
247 xfs_trans_t *tp,
248 xfs_ino_t ino,
249 uint flags,
250 uint lock_flags,
251 xfs_inode_t **ipp,
252 xfs_daddr_t bno)
253{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 int error;
David Chinnerda353b02007-08-28 14:00:13 +1000256 xfs_perag_t *pag;
257 xfs_agino_t agino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
David Chinnerda353b02007-08-28 14:00:13 +1000259 /* the radix tree exists only in inode capable AGs */
260 if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
261 return EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
David Chinnerda353b02007-08-28 14:00:13 +1000263 /* get the perag structure and ensure that it's inode capable */
264 pag = xfs_get_perag(mp, ino);
265 if (!pag->pagi_inodeok)
266 return EINVAL;
267 ASSERT(pag->pag_ici_init);
268 agino = XFS_INO_TO_AGINO(mp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270again:
David Chinner6441e542008-10-30 17:21:19 +1100271 error = 0;
David Chinnerda353b02007-08-28 14:00:13 +1000272 read_lock(&pag->pag_ici_lock);
273 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
David Chinner6441e542008-10-30 17:21:19 +1100275 if (ip) {
David Chinnerbf904242008-10-30 17:36:14 +1100276 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
David Chinner6441e542008-10-30 17:21:19 +1100277 if (error)
278 goto out_error_or_again;
279 } else {
David Chinnerda353b02007-08-28 14:00:13 +1000280 read_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100281 XFS_STATS_INC(xs_ig_missed);
David Chinnerda353b02007-08-28 14:00:13 +1000282
David Chinner6441e542008-10-30 17:21:19 +1100283 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
284 flags, lock_flags);
285 if (error)
286 goto out_error_or_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
David Chinnerda353b02007-08-28 14:00:13 +1000288 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000290 xfs_iflags_set(ip, XFS_IMODIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *ipp = ip;
292
David Chinnerbf904242008-10-30 17:36:14 +1100293 ASSERT(ip->i_df.if_ext_max ==
294 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000295 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * If we have a real type for an on-disk inode, we can set ops(&unlock)
297 * now. If it's a new inode being created, xfs_ialloc will handle it.
298 */
David Chinnerbf904242008-10-30 17:36:14 +1100299 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000300 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return 0;
David Chinner6441e542008-10-30 17:21:19 +1100302
303out_error_or_again:
304 if (error == EAGAIN) {
305 delay(1);
306 goto again;
307 }
308 xfs_put_perag(mp, pag);
309 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312
313/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * Look for the inode corresponding to the given ino in the hash table.
315 * If it is there and its i_transp pointer matches tp, return it.
316 * Otherwise, return NULL.
317 */
318xfs_inode_t *
319xfs_inode_incore(xfs_mount_t *mp,
320 xfs_ino_t ino,
321 xfs_trans_t *tp)
322{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 xfs_inode_t *ip;
David Chinnerda353b02007-08-28 14:00:13 +1000324 xfs_perag_t *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
David Chinnerda353b02007-08-28 14:00:13 +1000326 pag = xfs_get_perag(mp, ino);
327 read_lock(&pag->pag_ici_lock);
328 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
329 read_unlock(&pag->pag_ici_lock);
330 xfs_put_perag(mp, pag);
331
332 /* the returned inode must match the transaction */
333 if (ip && (ip->i_transp != tp))
334 return NULL;
335 return ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338/*
339 * Decrement reference count of an inode structure and unlock it.
340 *
341 * ip -- the inode being released
342 * lock_flags -- this parameter indicates the inode's locks to be
343 * to be released. See the comment on xfs_iunlock() for a list
344 * of valid values.
345 */
346void
347xfs_iput(xfs_inode_t *ip,
348 uint lock_flags)
349{
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100350 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 xfs_iunlock(ip, lock_flags);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000352 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355/*
356 * Special iput for brand-new inodes that are still locked
357 */
358void
David Chinner01651642008-08-13 15:45:15 +1000359xfs_iput_new(
360 xfs_inode_t *ip,
361 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
David Chinner01651642008-08-13 15:45:15 +1000363 struct inode *inode = VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100365 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if ((ip->i_d.di_mode == 0)) {
David Chinner7a18c382006-11-11 18:04:54 +1100368 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
Christoph Hellwig10090be2007-10-11 18:11:03 +1000369 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371 if (inode->i_state & I_NEW)
372 unlock_new_inode(inode);
373 if (lock_flags)
374 xfs_iunlock(ip, lock_flags);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000375 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378
379/*
380 * This routine embodies the part of the reclaim code that pulls
381 * the inode from the inode hash table and the mount structure's
382 * inode list.
383 * This should only be called from xfs_reclaim().
384 */
385void
386xfs_ireclaim(xfs_inode_t *ip)
387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /*
389 * Remove from old hash list and mount list.
390 */
391 XFS_STATS_INC(xs_ig_reclaims);
392
393 xfs_iextract(ip);
394
395 /*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100396 * Here we do a spurious inode lock in order to coordinate with inode
397 * cache radix tree lookups. This is because the lookup can reference
398 * the inodes in the cache without taking references. We make that OK
399 * here by ensuring that we wait until the inode is unlocked after the
400 * lookup before we go ahead and free it. We get both the ilock and
401 * the iolock because the code may need to drop the ilock one but will
402 * still hold the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 */
404 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
405
406 /*
407 * Release dquots (and their references) if any. An inode may escape
408 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
409 */
410 XFS_QM_DQDETACH(ip->i_mount, ip);
411
412 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * Free all memory associated with the inode.
414 */
Tim Shimmin439b8432006-11-11 18:04:34 +1100415 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 xfs_idestroy(ip);
417}
418
419/*
420 * This routine removes an about-to-be-destroyed inode from
421 * all of the lists in which it is located with the exception
422 * of the behavior chain.
423 */
424void
425xfs_iextract(
426 xfs_inode_t *ip)
427{
David Chinnerda353b02007-08-28 14:00:13 +1000428 xfs_mount_t *mp = ip->i_mount;
429 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
David Chinnerda353b02007-08-28 14:00:13 +1000431 write_lock(&pag->pag_ici_lock);
432 radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
433 write_unlock(&pag->pag_ici_lock);
434 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 mp->m_ireclaims++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439/*
440 * This is a wrapper routine around the xfs_ilock() routine
441 * used to centralize some grungy code. It is used in places
442 * that wish to lock the inode solely for reading the extents.
443 * The reason these places can't just call xfs_ilock(SHARED)
444 * is that the inode lock also guards to bringing in of the
445 * extents from disk for a file in b-tree format. If the inode
446 * is in b-tree format, then we need to lock the inode exclusively
447 * until the extents are read in. Locking it exclusively all
448 * the time would limit our parallelism unnecessarily, though.
449 * What we do instead is check to see if the extents have been
450 * read in yet, and only lock the inode exclusively if they
451 * have not.
452 *
453 * The function returns a value which should be given to the
454 * corresponding xfs_iunlock_map_shared(). This value is
455 * the mode in which the lock was actually taken.
456 */
457uint
458xfs_ilock_map_shared(
459 xfs_inode_t *ip)
460{
461 uint lock_mode;
462
463 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
464 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
465 lock_mode = XFS_ILOCK_EXCL;
466 } else {
467 lock_mode = XFS_ILOCK_SHARED;
468 }
469
470 xfs_ilock(ip, lock_mode);
471
472 return lock_mode;
473}
474
475/*
476 * This is simply the unlock routine to go with xfs_ilock_map_shared().
477 * All it does is call xfs_iunlock() with the given lock_mode.
478 */
479void
480xfs_iunlock_map_shared(
481 xfs_inode_t *ip,
482 unsigned int lock_mode)
483{
484 xfs_iunlock(ip, lock_mode);
485}
486
487/*
488 * The xfs inode contains 2 locks: a multi-reader lock called the
489 * i_iolock and a multi-reader lock called the i_lock. This routine
490 * allows either or both of the locks to be obtained.
491 *
492 * The 2 locks should always be ordered so that the IO lock is
493 * obtained first in order to prevent deadlock.
494 *
495 * ip -- the inode being locked
496 * lock_flags -- this parameter indicates the inode's locks
497 * to be locked. It can be:
498 * XFS_IOLOCK_SHARED,
499 * XFS_IOLOCK_EXCL,
500 * XFS_ILOCK_SHARED,
501 * XFS_ILOCK_EXCL,
502 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
503 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
504 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
505 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
506 */
507void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000508xfs_ilock(
509 xfs_inode_t *ip,
510 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 /*
513 * You can't set both SHARED and EXCL for the same lock,
514 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
515 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
516 */
517 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
518 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
519 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
520 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000521 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000523 if (lock_flags & XFS_IOLOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000524 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000525 else if (lock_flags & XFS_IOLOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000526 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000527
528 if (lock_flags & XFS_ILOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000529 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000530 else if (lock_flags & XFS_ILOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000531 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
534}
535
536/*
537 * This is just like xfs_ilock(), except that the caller
538 * is guaranteed not to sleep. It returns 1 if it gets
539 * the requested locks and 0 otherwise. If the IO lock is
540 * obtained but the inode lock cannot be, then the IO lock
541 * is dropped before returning.
542 *
543 * ip -- the inode being locked
544 * lock_flags -- this parameter indicates the inode's locks to be
545 * to be locked. See the comment for xfs_ilock() for a list
546 * of valid values.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
548int
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000549xfs_ilock_nowait(
550 xfs_inode_t *ip,
551 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /*
554 * You can't set both SHARED and EXCL for the same lock,
555 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
556 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
557 */
558 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
559 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
560 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
561 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000562 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (lock_flags & XFS_IOLOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000565 if (!mrtryupdate(&ip->i_iolock))
566 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 } else if (lock_flags & XFS_IOLOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000568 if (!mrtryaccess(&ip->i_iolock))
569 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571 if (lock_flags & XFS_ILOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000572 if (!mrtryupdate(&ip->i_lock))
573 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 } else if (lock_flags & XFS_ILOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000575 if (!mrtryaccess(&ip->i_lock))
576 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
579 return 1;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000580
581 out_undo_iolock:
582 if (lock_flags & XFS_IOLOCK_EXCL)
583 mrunlock_excl(&ip->i_iolock);
584 else if (lock_flags & XFS_IOLOCK_SHARED)
585 mrunlock_shared(&ip->i_iolock);
586 out:
587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590/*
591 * xfs_iunlock() is used to drop the inode locks acquired with
592 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
593 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
594 * that we know which locks to drop.
595 *
596 * ip -- the inode being unlocked
597 * lock_flags -- this parameter indicates the inode's locks to be
598 * to be unlocked. See the comment for xfs_ilock() for a list
599 * of valid values for this parameter.
600 *
601 */
602void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000603xfs_iunlock(
604 xfs_inode_t *ip,
605 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 /*
608 * You can't set both SHARED and EXCL for the same lock,
609 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
610 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
611 */
612 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
613 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
614 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
615 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000616 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
617 XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 ASSERT(lock_flags != 0);
619
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000620 if (lock_flags & XFS_IOLOCK_EXCL)
621 mrunlock_excl(&ip->i_iolock);
622 else if (lock_flags & XFS_IOLOCK_SHARED)
623 mrunlock_shared(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000625 if (lock_flags & XFS_ILOCK_EXCL)
626 mrunlock_excl(&ip->i_lock);
627 else if (lock_flags & XFS_ILOCK_SHARED)
628 mrunlock_shared(&ip->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000630 if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
631 !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * Let the AIL know that this item has been unlocked in case
634 * it is in the AIL and anyone is waiting on it. Don't do
635 * this if the caller has asked us not to.
636 */
David Chinner783a2f62008-10-30 17:39:58 +1100637 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000638 (xfs_log_item_t*)(ip->i_itemp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
641}
642
643/*
644 * give up write locks. the i/o lock cannot be held nested
645 * if it is being demoted.
646 */
647void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000648xfs_ilock_demote(
649 xfs_inode_t *ip,
650 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
653 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
654
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000655 if (lock_flags & XFS_ILOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 mrdemote(&ip->i_lock);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000657 if (lock_flags & XFS_IOLOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 mrdemote(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000661#ifdef DEBUG
662/*
663 * Debug-only routine, without additional rw_semaphore APIs, we can
664 * now only answer requests regarding whether we hold the lock for write
665 * (reader state is outside our visibility, we only track writer state).
666 *
667 * Note: this means !xfs_isilocked would give false positives, so don't do that.
668 */
669int
670xfs_isilocked(
671 xfs_inode_t *ip,
672 uint lock_flags)
673{
674 if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
675 XFS_ILOCK_EXCL) {
676 if (!ip->i_lock.mr_writer)
677 return 0;
678 }
679
680 if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
681 XFS_IOLOCK_EXCL) {
682 if (!ip->i_iolock.mr_writer)
683 return 0;
684 }
685
686 return 1;
687}
688#endif
689