blob: a1f209b0596f7c0dce26a83a226e661cdbf6c4cf [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 Chinner6441e542008-10-30 17:21:19 +110055 int error = 0;
56
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 error = EAGAIN;
64 XFS_STATS_INC(xs_ig_frecycle);
65 goto out_error;
66 }
67
David Chinnerbf904242008-10-30 17:36:14 +110068 /* If IRECLAIMABLE is set, we've torn down the vfs inode part */
69 if (xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
David Chinner6441e542008-10-30 17:21:19 +110070
71 /*
David Chinnerbf904242008-10-30 17:36:14 +110072 * If lookup is racing with unlink, then we should return an
73 * error immediately so we don't remove it from the reclaim
74 * list and potentially leak the inode.
David Chinner6441e542008-10-30 17:21:19 +110075 */
David Chinnerbf904242008-10-30 17:36:14 +110076
77 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
David Chinner6441e542008-10-30 17:21:19 +110078 error = ENOENT;
79 goto out_error;
80 }
David Chinnerbf904242008-10-30 17:36:14 +110081
David Chinner6441e542008-10-30 17:21:19 +110082 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
83
David Chinnerbf904242008-10-30 17:36:14 +110084 /*
85 * We need to re-initialise the VFS inode as it has been
86 * 'freed' by the VFS. Do this here so we can deal with
87 * errors cleanly, then tag it so it can be set up correctly
88 * later.
89 */
90 if (!inode_init_always(mp->m_super, VFS_I(ip))) {
91 error = ENOMEM;
92 goto out_error;
93 }
94 xfs_iflags_set(ip, XFS_INEW);
David Chinner6441e542008-10-30 17:21:19 +110095 xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +110096
97 /* clear the radix tree reclaim flag as well. */
98 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
David Chinner6441e542008-10-30 17:21:19 +110099 read_unlock(&pag->pag_ici_lock);
David Chinnerbf904242008-10-30 17:36:14 +1100100 } else if (!igrab(VFS_I(ip))) {
101 /* If the VFS inode is being torn down, pause and try again. */
102 error = EAGAIN;
103 XFS_STATS_INC(xs_ig_frecycle);
104 goto out_error;
David Chinner6441e542008-10-30 17:21:19 +1100105 } else {
David Chinnerbf904242008-10-30 17:36:14 +1100106 /* we've got a live one */
David Chinner6441e542008-10-30 17:21:19 +1100107 read_unlock(&pag->pag_ici_lock);
108 }
109
110 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
111 error = ENOENT;
112 goto out;
113 }
114
115 if (lock_flags != 0)
116 xfs_ilock(ip, lock_flags);
117
118 xfs_iflags_clear(ip, XFS_ISTALE);
119 xfs_itrace_exit_tag(ip, "xfs_iget.found");
120 XFS_STATS_INC(xs_ig_found);
121 return 0;
122
123out_error:
124 read_unlock(&pag->pag_ici_lock);
125out:
126 return error;
127}
128
129
130static int
131xfs_iget_cache_miss(
132 struct xfs_mount *mp,
133 struct xfs_perag *pag,
134 xfs_trans_t *tp,
135 xfs_ino_t ino,
136 struct xfs_inode **ipp,
137 xfs_daddr_t bno,
138 int flags,
139 int lock_flags) __releases(pag->pag_ici_lock)
140{
141 struct xfs_inode *ip;
142 int error;
143 unsigned long first_index, mask;
144 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
145
146 /*
147 * Read the disk inode attributes into a new inode structure and get
148 * a new vnode for it. This should also initialize i_ino and i_mount.
149 */
150 error = xfs_iread(mp, tp, ino, &ip, bno,
151 (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
152 if (error)
153 return error;
154
155 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
156
157 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
158 error = ENOENT;
159 goto out_destroy;
160 }
161
162 /*
163 * Preload the radix tree so we can insert safely under the
164 * write spinlock.
165 */
166 if (radix_tree_preload(GFP_KERNEL)) {
167 error = EAGAIN;
168 goto out_destroy;
169 }
170
171 if (lock_flags)
172 xfs_ilock(ip, lock_flags);
173
174 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
175 first_index = agino & mask;
176 write_lock(&pag->pag_ici_lock);
177
178 /* insert the new inode */
179 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
180 if (unlikely(error)) {
181 WARN_ON(error != -EEXIST);
182 XFS_STATS_INC(xs_ig_dup);
183 error = EAGAIN;
184 goto out_unlock;
185 }
186
187 /* These values _must_ be set before releasing the radix tree lock! */
188 ip->i_udquot = ip->i_gdquot = NULL;
189 xfs_iflags_set(ip, XFS_INEW);
190
191 write_unlock(&pag->pag_ici_lock);
192 radix_tree_preload_end();
193 *ipp = ip;
194 return 0;
195
196out_unlock:
197 write_unlock(&pag->pag_ici_lock);
198 radix_tree_preload_end();
199out_destroy:
200 xfs_idestroy(ip);
201 return error;
202}
203
204/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * Look up an inode by number in the given file system.
David Chinnerda353b02007-08-28 14:00:13 +1000206 * The inode is looked up in the cache held in each AG.
David Chinnerbf904242008-10-30 17:36:14 +1100207 * If the inode is found in the cache, initialise the vfs inode
208 * if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 *
David Chinnerda353b02007-08-28 14:00:13 +1000210 * If it is not in core, read it in from the file system's device,
David Chinnerbf904242008-10-30 17:36:14 +1100211 * add it to the cache and initialise the vfs inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *
213 * The inode is locked according to the value of the lock_flags parameter.
214 * This flag parameter indicates how and if the inode's IO lock and inode lock
215 * should be taken.
216 *
217 * mp -- the mount point structure for the current file system. It points
218 * to the inode hash table.
219 * tp -- a pointer to the current transaction if there is one. This is
220 * simply passed through to the xfs_iread() call.
221 * ino -- the number of the inode desired. This is the unique identifier
222 * within the file system for the inode being requested.
223 * lock_flags -- flags indicating how to lock the inode. See the comment
224 * for xfs_ilock() for a list of valid values.
225 * bno -- the block number starting the buffer containing the inode,
226 * if known (as by bulkstat), else 0.
227 */
David Chinnerbf904242008-10-30 17:36:14 +1100228int
229xfs_iget(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 xfs_mount_t *mp,
231 xfs_trans_t *tp,
232 xfs_ino_t ino,
233 uint flags,
234 uint lock_flags,
235 xfs_inode_t **ipp,
236 xfs_daddr_t bno)
237{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 int error;
David Chinnerda353b02007-08-28 14:00:13 +1000240 xfs_perag_t *pag;
241 xfs_agino_t agino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
David Chinnerda353b02007-08-28 14:00:13 +1000243 /* the radix tree exists only in inode capable AGs */
244 if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
245 return EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David Chinnerda353b02007-08-28 14:00:13 +1000247 /* get the perag structure and ensure that it's inode capable */
248 pag = xfs_get_perag(mp, ino);
249 if (!pag->pagi_inodeok)
250 return EINVAL;
251 ASSERT(pag->pag_ici_init);
252 agino = XFS_INO_TO_AGINO(mp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254again:
David Chinner6441e542008-10-30 17:21:19 +1100255 error = 0;
David Chinnerda353b02007-08-28 14:00:13 +1000256 read_lock(&pag->pag_ici_lock);
257 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
David Chinner6441e542008-10-30 17:21:19 +1100259 if (ip) {
David Chinnerbf904242008-10-30 17:36:14 +1100260 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
David Chinner6441e542008-10-30 17:21:19 +1100261 if (error)
262 goto out_error_or_again;
263 } else {
David Chinnerda353b02007-08-28 14:00:13 +1000264 read_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100265 XFS_STATS_INC(xs_ig_missed);
David Chinnerda353b02007-08-28 14:00:13 +1000266
David Chinner6441e542008-10-30 17:21:19 +1100267 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
268 flags, lock_flags);
269 if (error)
270 goto out_error_or_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
David Chinnerda353b02007-08-28 14:00:13 +1000272 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000274 xfs_iflags_set(ip, XFS_IMODIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 *ipp = ip;
276
David Chinnerbf904242008-10-30 17:36:14 +1100277 ASSERT(ip->i_df.if_ext_max ==
278 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000279 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * If we have a real type for an on-disk inode, we can set ops(&unlock)
281 * now. If it's a new inode being created, xfs_ialloc will handle it.
282 */
David Chinnerbf904242008-10-30 17:36:14 +1100283 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000284 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return 0;
David Chinner6441e542008-10-30 17:21:19 +1100286
287out_error_or_again:
288 if (error == EAGAIN) {
289 delay(1);
290 goto again;
291 }
292 xfs_put_perag(mp, pag);
293 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296
297/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * Look for the inode corresponding to the given ino in the hash table.
299 * If it is there and its i_transp pointer matches tp, return it.
300 * Otherwise, return NULL.
301 */
302xfs_inode_t *
303xfs_inode_incore(xfs_mount_t *mp,
304 xfs_ino_t ino,
305 xfs_trans_t *tp)
306{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 xfs_inode_t *ip;
David Chinnerda353b02007-08-28 14:00:13 +1000308 xfs_perag_t *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
David Chinnerda353b02007-08-28 14:00:13 +1000310 pag = xfs_get_perag(mp, ino);
311 read_lock(&pag->pag_ici_lock);
312 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
313 read_unlock(&pag->pag_ici_lock);
314 xfs_put_perag(mp, pag);
315
316 /* the returned inode must match the transaction */
317 if (ip && (ip->i_transp != tp))
318 return NULL;
319 return ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322/*
323 * Decrement reference count of an inode structure and unlock it.
324 *
325 * ip -- the inode being released
326 * lock_flags -- this parameter indicates the inode's locks to be
327 * to be released. See the comment on xfs_iunlock() for a list
328 * of valid values.
329 */
330void
331xfs_iput(xfs_inode_t *ip,
332 uint lock_flags)
333{
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100334 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 xfs_iunlock(ip, lock_flags);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000336 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339/*
340 * Special iput for brand-new inodes that are still locked
341 */
342void
David Chinner01651642008-08-13 15:45:15 +1000343xfs_iput_new(
344 xfs_inode_t *ip,
345 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
David Chinner01651642008-08-13 15:45:15 +1000347 struct inode *inode = VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100349 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 if ((ip->i_d.di_mode == 0)) {
David Chinner7a18c382006-11-11 18:04:54 +1100352 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
Christoph Hellwig10090be2007-10-11 18:11:03 +1000353 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355 if (inode->i_state & I_NEW)
356 unlock_new_inode(inode);
357 if (lock_flags)
358 xfs_iunlock(ip, lock_flags);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000359 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362
363/*
364 * This routine embodies the part of the reclaim code that pulls
365 * the inode from the inode hash table and the mount structure's
366 * inode list.
367 * This should only be called from xfs_reclaim().
368 */
369void
370xfs_ireclaim(xfs_inode_t *ip)
371{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /*
373 * Remove from old hash list and mount list.
374 */
375 XFS_STATS_INC(xs_ig_reclaims);
376
377 xfs_iextract(ip);
378
379 /*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100380 * Here we do a spurious inode lock in order to coordinate with inode
381 * cache radix tree lookups. This is because the lookup can reference
382 * the inodes in the cache without taking references. We make that OK
383 * here by ensuring that we wait until the inode is unlocked after the
384 * lookup before we go ahead and free it. We get both the ilock and
385 * the iolock because the code may need to drop the ilock one but will
386 * still hold the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
388 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
389
390 /*
391 * Release dquots (and their references) if any. An inode may escape
392 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
393 */
394 XFS_QM_DQDETACH(ip->i_mount, ip);
395
396 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 * Free all memory associated with the inode.
398 */
Tim Shimmin439b8432006-11-11 18:04:34 +1100399 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 xfs_idestroy(ip);
401}
402
403/*
404 * This routine removes an about-to-be-destroyed inode from
405 * all of the lists in which it is located with the exception
406 * of the behavior chain.
407 */
408void
409xfs_iextract(
410 xfs_inode_t *ip)
411{
David Chinnerda353b02007-08-28 14:00:13 +1000412 xfs_mount_t *mp = ip->i_mount;
413 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
David Chinnerda353b02007-08-28 14:00:13 +1000415 write_lock(&pag->pag_ici_lock);
416 radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
417 write_unlock(&pag->pag_ici_lock);
418 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 mp->m_ireclaims++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423/*
424 * This is a wrapper routine around the xfs_ilock() routine
425 * used to centralize some grungy code. It is used in places
426 * that wish to lock the inode solely for reading the extents.
427 * The reason these places can't just call xfs_ilock(SHARED)
428 * is that the inode lock also guards to bringing in of the
429 * extents from disk for a file in b-tree format. If the inode
430 * is in b-tree format, then we need to lock the inode exclusively
431 * until the extents are read in. Locking it exclusively all
432 * the time would limit our parallelism unnecessarily, though.
433 * What we do instead is check to see if the extents have been
434 * read in yet, and only lock the inode exclusively if they
435 * have not.
436 *
437 * The function returns a value which should be given to the
438 * corresponding xfs_iunlock_map_shared(). This value is
439 * the mode in which the lock was actually taken.
440 */
441uint
442xfs_ilock_map_shared(
443 xfs_inode_t *ip)
444{
445 uint lock_mode;
446
447 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
448 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
449 lock_mode = XFS_ILOCK_EXCL;
450 } else {
451 lock_mode = XFS_ILOCK_SHARED;
452 }
453
454 xfs_ilock(ip, lock_mode);
455
456 return lock_mode;
457}
458
459/*
460 * This is simply the unlock routine to go with xfs_ilock_map_shared().
461 * All it does is call xfs_iunlock() with the given lock_mode.
462 */
463void
464xfs_iunlock_map_shared(
465 xfs_inode_t *ip,
466 unsigned int lock_mode)
467{
468 xfs_iunlock(ip, lock_mode);
469}
470
471/*
472 * The xfs inode contains 2 locks: a multi-reader lock called the
473 * i_iolock and a multi-reader lock called the i_lock. This routine
474 * allows either or both of the locks to be obtained.
475 *
476 * The 2 locks should always be ordered so that the IO lock is
477 * obtained first in order to prevent deadlock.
478 *
479 * ip -- the inode being locked
480 * lock_flags -- this parameter indicates the inode's locks
481 * to be locked. It can be:
482 * XFS_IOLOCK_SHARED,
483 * XFS_IOLOCK_EXCL,
484 * XFS_ILOCK_SHARED,
485 * XFS_ILOCK_EXCL,
486 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
487 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
488 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
489 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
490 */
491void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000492xfs_ilock(
493 xfs_inode_t *ip,
494 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 /*
497 * You can't set both SHARED and EXCL for the same lock,
498 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
499 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
500 */
501 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
502 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
503 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
504 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000505 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000507 if (lock_flags & XFS_IOLOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000508 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000509 else if (lock_flags & XFS_IOLOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000510 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000511
512 if (lock_flags & XFS_ILOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000513 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000514 else if (lock_flags & XFS_ILOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000515 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
518}
519
520/*
521 * This is just like xfs_ilock(), except that the caller
522 * is guaranteed not to sleep. It returns 1 if it gets
523 * the requested locks and 0 otherwise. If the IO lock is
524 * obtained but the inode lock cannot be, then the IO lock
525 * is dropped before returning.
526 *
527 * ip -- the inode being locked
528 * lock_flags -- this parameter indicates the inode's locks to be
529 * to be locked. See the comment for xfs_ilock() for a list
530 * of valid values.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
532int
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000533xfs_ilock_nowait(
534 xfs_inode_t *ip,
535 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
538 * You can't set both SHARED and EXCL for the same lock,
539 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
540 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
541 */
542 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
543 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
544 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
545 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000546 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (lock_flags & XFS_IOLOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000549 if (!mrtryupdate(&ip->i_iolock))
550 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 } else if (lock_flags & XFS_IOLOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000552 if (!mrtryaccess(&ip->i_iolock))
553 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555 if (lock_flags & XFS_ILOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000556 if (!mrtryupdate(&ip->i_lock))
557 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 } else if (lock_flags & XFS_ILOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000559 if (!mrtryaccess(&ip->i_lock))
560 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
563 return 1;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000564
565 out_undo_iolock:
566 if (lock_flags & XFS_IOLOCK_EXCL)
567 mrunlock_excl(&ip->i_iolock);
568 else if (lock_flags & XFS_IOLOCK_SHARED)
569 mrunlock_shared(&ip->i_iolock);
570 out:
571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574/*
575 * xfs_iunlock() is used to drop the inode locks acquired with
576 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
577 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
578 * that we know which locks to drop.
579 *
580 * ip -- the inode being unlocked
581 * lock_flags -- this parameter indicates the inode's locks to be
582 * to be unlocked. See the comment for xfs_ilock() for a list
583 * of valid values for this parameter.
584 *
585 */
586void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000587xfs_iunlock(
588 xfs_inode_t *ip,
589 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 /*
592 * You can't set both SHARED and EXCL for the same lock,
593 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
594 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
595 */
596 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
597 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
598 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
599 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000600 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
601 XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 ASSERT(lock_flags != 0);
603
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000604 if (lock_flags & XFS_IOLOCK_EXCL)
605 mrunlock_excl(&ip->i_iolock);
606 else if (lock_flags & XFS_IOLOCK_SHARED)
607 mrunlock_shared(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000609 if (lock_flags & XFS_ILOCK_EXCL)
610 mrunlock_excl(&ip->i_lock);
611 else if (lock_flags & XFS_ILOCK_SHARED)
612 mrunlock_shared(&ip->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000614 if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
615 !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /*
617 * Let the AIL know that this item has been unlocked in case
618 * it is in the AIL and anyone is waiting on it. Don't do
619 * this if the caller has asked us not to.
620 */
David Chinner783a2f62008-10-30 17:39:58 +1100621 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000622 (xfs_log_item_t*)(ip->i_itemp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
625}
626
627/*
628 * give up write locks. the i/o lock cannot be held nested
629 * if it is being demoted.
630 */
631void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000632xfs_ilock_demote(
633 xfs_inode_t *ip,
634 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
637 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
638
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000639 if (lock_flags & XFS_ILOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 mrdemote(&ip->i_lock);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000641 if (lock_flags & XFS_IOLOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 mrdemote(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000645#ifdef DEBUG
646/*
647 * Debug-only routine, without additional rw_semaphore APIs, we can
648 * now only answer requests regarding whether we hold the lock for write
649 * (reader state is outside our visibility, we only track writer state).
650 *
651 * Note: this means !xfs_isilocked would give false positives, so don't do that.
652 */
653int
654xfs_isilocked(
655 xfs_inode_t *ip,
656 uint lock_flags)
657{
658 if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
659 XFS_ILOCK_EXCL) {
660 if (!ip->i_lock.mr_writer)
661 return 0;
662 }
663
664 if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
665 XFS_IOLOCK_EXCL) {
666 if (!ip->i_iolock.mr_writer)
667 return 0;
668 }
669
670 return 1;
671}
672#endif
673