blob: 837cae781536c4716b92286a008fe70a24e38aa4 [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
David Chinner56e73ec2008-10-30 17:55:27 +1100162 if (lock_flags)
163 xfs_ilock(ip, lock_flags);
164
David Chinner6441e542008-10-30 17:21:19 +1100165 /*
166 * Preload the radix tree so we can insert safely under the
David Chinner56e73ec2008-10-30 17:55:27 +1100167 * write spinlock. Note that we cannot sleep inside the preload
168 * region.
David Chinner6441e542008-10-30 17:21:19 +1100169 */
170 if (radix_tree_preload(GFP_KERNEL)) {
171 error = EAGAIN;
David Chinner56e73ec2008-10-30 17:55:27 +1100172 goto out_unlock;
David Chinner6441e542008-10-30 17:21:19 +1100173 }
174
David Chinner6441e542008-10-30 17:21:19 +1100175 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
176 first_index = agino & mask;
177 write_lock(&pag->pag_ici_lock);
178
179 /* insert the new inode */
180 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
181 if (unlikely(error)) {
182 WARN_ON(error != -EEXIST);
183 XFS_STATS_INC(xs_ig_dup);
184 error = EAGAIN;
David Chinner56e73ec2008-10-30 17:55:27 +1100185 goto out_preload_end;
David Chinner6441e542008-10-30 17:21:19 +1100186 }
187
188 /* These values _must_ be set before releasing the radix tree lock! */
189 ip->i_udquot = ip->i_gdquot = NULL;
190 xfs_iflags_set(ip, XFS_INEW);
191
192 write_unlock(&pag->pag_ici_lock);
193 radix_tree_preload_end();
194 *ipp = ip;
195 return 0;
196
David Chinner56e73ec2008-10-30 17:55:27 +1100197out_preload_end:
David Chinner6441e542008-10-30 17:21:19 +1100198 write_unlock(&pag->pag_ici_lock);
199 radix_tree_preload_end();
David Chinner56e73ec2008-10-30 17:55:27 +1100200out_unlock:
201 if (lock_flags)
202 xfs_iunlock(ip, lock_flags);
David Chinner6441e542008-10-30 17:21:19 +1100203out_destroy:
Christoph Hellwig9ed04512008-10-30 18:26:04 +1100204 xfs_destroy_inode(ip);
David Chinner6441e542008-10-30 17:21:19 +1100205 return error;
206}
207
208/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * Look up an inode by number in the given file system.
David Chinnerda353b02007-08-28 14:00:13 +1000210 * The inode is looked up in the cache held in each AG.
David Chinnerbf904242008-10-30 17:36:14 +1100211 * If the inode is found in the cache, initialise the vfs inode
212 * if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 *
David Chinnerda353b02007-08-28 14:00:13 +1000214 * If it is not in core, read it in from the file system's device,
David Chinnerbf904242008-10-30 17:36:14 +1100215 * add it to the cache and initialise the vfs inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 *
217 * The inode is locked according to the value of the lock_flags parameter.
218 * This flag parameter indicates how and if the inode's IO lock and inode lock
219 * should be taken.
220 *
221 * mp -- the mount point structure for the current file system. It points
222 * to the inode hash table.
223 * tp -- a pointer to the current transaction if there is one. This is
224 * simply passed through to the xfs_iread() call.
225 * ino -- the number of the inode desired. This is the unique identifier
226 * within the file system for the inode being requested.
227 * lock_flags -- flags indicating how to lock the inode. See the comment
228 * for xfs_ilock() for a list of valid values.
229 * bno -- the block number starting the buffer containing the inode,
230 * if known (as by bulkstat), else 0.
231 */
David Chinnerbf904242008-10-30 17:36:14 +1100232int
233xfs_iget(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 xfs_mount_t *mp,
235 xfs_trans_t *tp,
236 xfs_ino_t ino,
237 uint flags,
238 uint lock_flags,
239 xfs_inode_t **ipp,
240 xfs_daddr_t bno)
241{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 int error;
David Chinnerda353b02007-08-28 14:00:13 +1000244 xfs_perag_t *pag;
245 xfs_agino_t agino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David Chinnerda353b02007-08-28 14:00:13 +1000247 /* the radix tree exists only in inode capable AGs */
248 if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
249 return EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
David Chinnerda353b02007-08-28 14:00:13 +1000251 /* get the perag structure and ensure that it's inode capable */
252 pag = xfs_get_perag(mp, ino);
253 if (!pag->pagi_inodeok)
254 return EINVAL;
255 ASSERT(pag->pag_ici_init);
256 agino = XFS_INO_TO_AGINO(mp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258again:
David Chinner6441e542008-10-30 17:21:19 +1100259 error = 0;
David Chinnerda353b02007-08-28 14:00:13 +1000260 read_lock(&pag->pag_ici_lock);
261 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
David Chinner6441e542008-10-30 17:21:19 +1100263 if (ip) {
David Chinnerbf904242008-10-30 17:36:14 +1100264 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
David Chinner6441e542008-10-30 17:21:19 +1100265 if (error)
266 goto out_error_or_again;
267 } else {
David Chinnerda353b02007-08-28 14:00:13 +1000268 read_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100269 XFS_STATS_INC(xs_ig_missed);
David Chinnerda353b02007-08-28 14:00:13 +1000270
David Chinner6441e542008-10-30 17:21:19 +1100271 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
272 flags, lock_flags);
273 if (error)
274 goto out_error_or_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
David Chinnerda353b02007-08-28 14:00:13 +1000276 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000278 xfs_iflags_set(ip, XFS_IMODIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 *ipp = ip;
280
David Chinnerbf904242008-10-30 17:36:14 +1100281 ASSERT(ip->i_df.if_ext_max ==
282 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000283 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * If we have a real type for an on-disk inode, we can set ops(&unlock)
285 * now. If it's a new inode being created, xfs_ialloc will handle it.
286 */
David Chinnerbf904242008-10-30 17:36:14 +1100287 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000288 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return 0;
David Chinner6441e542008-10-30 17:21:19 +1100290
291out_error_or_again:
292 if (error == EAGAIN) {
293 delay(1);
294 goto again;
295 }
296 xfs_put_perag(mp, pag);
297 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300
301/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * Look for the inode corresponding to the given ino in the hash table.
303 * If it is there and its i_transp pointer matches tp, return it.
304 * Otherwise, return NULL.
305 */
306xfs_inode_t *
307xfs_inode_incore(xfs_mount_t *mp,
308 xfs_ino_t ino,
309 xfs_trans_t *tp)
310{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 xfs_inode_t *ip;
David Chinnerda353b02007-08-28 14:00:13 +1000312 xfs_perag_t *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
David Chinnerda353b02007-08-28 14:00:13 +1000314 pag = xfs_get_perag(mp, ino);
315 read_lock(&pag->pag_ici_lock);
316 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
317 read_unlock(&pag->pag_ici_lock);
318 xfs_put_perag(mp, pag);
319
320 /* the returned inode must match the transaction */
321 if (ip && (ip->i_transp != tp))
322 return NULL;
323 return ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326/*
327 * Decrement reference count of an inode structure and unlock it.
328 *
329 * ip -- the inode being released
330 * lock_flags -- this parameter indicates the inode's locks to be
331 * to be released. See the comment on xfs_iunlock() for a list
332 * of valid values.
333 */
334void
335xfs_iput(xfs_inode_t *ip,
336 uint lock_flags)
337{
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100338 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 xfs_iunlock(ip, lock_flags);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000340 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343/*
344 * Special iput for brand-new inodes that are still locked
345 */
346void
David Chinner01651642008-08-13 15:45:15 +1000347xfs_iput_new(
348 xfs_inode_t *ip,
349 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
David Chinner01651642008-08-13 15:45:15 +1000351 struct inode *inode = VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100353 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if ((ip->i_d.di_mode == 0)) {
David Chinner7a18c382006-11-11 18:04:54 +1100356 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
Christoph Hellwig10090be2007-10-11 18:11:03 +1000357 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359 if (inode->i_state & I_NEW)
360 unlock_new_inode(inode);
361 if (lock_flags)
362 xfs_iunlock(ip, lock_flags);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000363 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366
367/*
368 * This routine embodies the part of the reclaim code that pulls
369 * the inode from the inode hash table and the mount structure's
370 * inode list.
371 * This should only be called from xfs_reclaim().
372 */
373void
374xfs_ireclaim(xfs_inode_t *ip)
375{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /*
377 * Remove from old hash list and mount list.
378 */
379 XFS_STATS_INC(xs_ig_reclaims);
380
381 xfs_iextract(ip);
382
383 /*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100384 * Here we do a spurious inode lock in order to coordinate with inode
385 * cache radix tree lookups. This is because the lookup can reference
386 * the inodes in the cache without taking references. We make that OK
387 * here by ensuring that we wait until the inode is unlocked after the
388 * lookup before we go ahead and free it. We get both the ilock and
389 * the iolock because the code may need to drop the ilock one but will
390 * still hold the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
392 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
393
394 /*
395 * Release dquots (and their references) if any. An inode may escape
396 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
397 */
398 XFS_QM_DQDETACH(ip->i_mount, ip);
399
400 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 * Free all memory associated with the inode.
402 */
Tim Shimmin439b8432006-11-11 18:04:34 +1100403 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 xfs_idestroy(ip);
405}
406
407/*
408 * This routine removes an about-to-be-destroyed inode from
409 * all of the lists in which it is located with the exception
410 * of the behavior chain.
411 */
412void
413xfs_iextract(
414 xfs_inode_t *ip)
415{
David Chinnerda353b02007-08-28 14:00:13 +1000416 xfs_mount_t *mp = ip->i_mount;
417 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
David Chinnerda353b02007-08-28 14:00:13 +1000419 write_lock(&pag->pag_ici_lock);
420 radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
421 write_unlock(&pag->pag_ici_lock);
422 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 mp->m_ireclaims++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427/*
428 * This is a wrapper routine around the xfs_ilock() routine
429 * used to centralize some grungy code. It is used in places
430 * that wish to lock the inode solely for reading the extents.
431 * The reason these places can't just call xfs_ilock(SHARED)
432 * is that the inode lock also guards to bringing in of the
433 * extents from disk for a file in b-tree format. If the inode
434 * is in b-tree format, then we need to lock the inode exclusively
435 * until the extents are read in. Locking it exclusively all
436 * the time would limit our parallelism unnecessarily, though.
437 * What we do instead is check to see if the extents have been
438 * read in yet, and only lock the inode exclusively if they
439 * have not.
440 *
441 * The function returns a value which should be given to the
442 * corresponding xfs_iunlock_map_shared(). This value is
443 * the mode in which the lock was actually taken.
444 */
445uint
446xfs_ilock_map_shared(
447 xfs_inode_t *ip)
448{
449 uint lock_mode;
450
451 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
452 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
453 lock_mode = XFS_ILOCK_EXCL;
454 } else {
455 lock_mode = XFS_ILOCK_SHARED;
456 }
457
458 xfs_ilock(ip, lock_mode);
459
460 return lock_mode;
461}
462
463/*
464 * This is simply the unlock routine to go with xfs_ilock_map_shared().
465 * All it does is call xfs_iunlock() with the given lock_mode.
466 */
467void
468xfs_iunlock_map_shared(
469 xfs_inode_t *ip,
470 unsigned int lock_mode)
471{
472 xfs_iunlock(ip, lock_mode);
473}
474
475/*
476 * The xfs inode contains 2 locks: a multi-reader lock called the
477 * i_iolock and a multi-reader lock called the i_lock. This routine
478 * allows either or both of the locks to be obtained.
479 *
480 * The 2 locks should always be ordered so that the IO lock is
481 * obtained first in order to prevent deadlock.
482 *
483 * ip -- the inode being locked
484 * lock_flags -- this parameter indicates the inode's locks
485 * to be locked. It can be:
486 * XFS_IOLOCK_SHARED,
487 * XFS_IOLOCK_EXCL,
488 * XFS_ILOCK_SHARED,
489 * XFS_ILOCK_EXCL,
490 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
491 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
492 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
493 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
494 */
495void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000496xfs_ilock(
497 xfs_inode_t *ip,
498 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 /*
501 * You can't set both SHARED and EXCL for the same lock,
502 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
503 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
504 */
505 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
506 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
507 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
508 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000509 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000511 if (lock_flags & XFS_IOLOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000512 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000513 else if (lock_flags & XFS_IOLOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000514 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000515
516 if (lock_flags & XFS_ILOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000517 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000518 else if (lock_flags & XFS_ILOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000519 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
522}
523
524/*
525 * This is just like xfs_ilock(), except that the caller
526 * is guaranteed not to sleep. It returns 1 if it gets
527 * the requested locks and 0 otherwise. If the IO lock is
528 * obtained but the inode lock cannot be, then the IO lock
529 * is dropped before returning.
530 *
531 * ip -- the inode being locked
532 * lock_flags -- this parameter indicates the inode's locks to be
533 * to be locked. See the comment for xfs_ilock() for a list
534 * of valid values.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 */
536int
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000537xfs_ilock_nowait(
538 xfs_inode_t *ip,
539 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /*
542 * You can't set both SHARED and EXCL for the same lock,
543 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
544 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
545 */
546 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
547 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
548 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
549 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000550 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (lock_flags & XFS_IOLOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000553 if (!mrtryupdate(&ip->i_iolock))
554 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 } else if (lock_flags & XFS_IOLOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000556 if (!mrtryaccess(&ip->i_iolock))
557 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559 if (lock_flags & XFS_ILOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000560 if (!mrtryupdate(&ip->i_lock))
561 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 } else if (lock_flags & XFS_ILOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000563 if (!mrtryaccess(&ip->i_lock))
564 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
567 return 1;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000568
569 out_undo_iolock:
570 if (lock_flags & XFS_IOLOCK_EXCL)
571 mrunlock_excl(&ip->i_iolock);
572 else if (lock_flags & XFS_IOLOCK_SHARED)
573 mrunlock_shared(&ip->i_iolock);
574 out:
575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578/*
579 * xfs_iunlock() is used to drop the inode locks acquired with
580 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
581 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
582 * that we know which locks to drop.
583 *
584 * ip -- the inode being unlocked
585 * lock_flags -- this parameter indicates the inode's locks to be
586 * to be unlocked. See the comment for xfs_ilock() for a list
587 * of valid values for this parameter.
588 *
589 */
590void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000591xfs_iunlock(
592 xfs_inode_t *ip,
593 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 /*
596 * You can't set both SHARED and EXCL for the same lock,
597 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
598 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
599 */
600 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
601 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
602 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
603 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000604 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
605 XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 ASSERT(lock_flags != 0);
607
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000608 if (lock_flags & XFS_IOLOCK_EXCL)
609 mrunlock_excl(&ip->i_iolock);
610 else if (lock_flags & XFS_IOLOCK_SHARED)
611 mrunlock_shared(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000613 if (lock_flags & XFS_ILOCK_EXCL)
614 mrunlock_excl(&ip->i_lock);
615 else if (lock_flags & XFS_ILOCK_SHARED)
616 mrunlock_shared(&ip->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000618 if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
619 !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /*
621 * Let the AIL know that this item has been unlocked in case
622 * it is in the AIL and anyone is waiting on it. Don't do
623 * this if the caller has asked us not to.
624 */
David Chinner783a2f62008-10-30 17:39:58 +1100625 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000626 (xfs_log_item_t*)(ip->i_itemp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
629}
630
631/*
632 * give up write locks. the i/o lock cannot be held nested
633 * if it is being demoted.
634 */
635void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000636xfs_ilock_demote(
637 xfs_inode_t *ip,
638 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
641 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
642
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000643 if (lock_flags & XFS_ILOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 mrdemote(&ip->i_lock);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000645 if (lock_flags & XFS_IOLOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 mrdemote(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000649#ifdef DEBUG
650/*
651 * Debug-only routine, without additional rw_semaphore APIs, we can
652 * now only answer requests regarding whether we hold the lock for write
653 * (reader state is outside our visibility, we only track writer state).
654 *
655 * Note: this means !xfs_isilocked would give false positives, so don't do that.
656 */
657int
658xfs_isilocked(
659 xfs_inode_t *ip,
660 uint lock_flags)
661{
662 if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
663 XFS_ILOCK_EXCL) {
664 if (!ip->i_lock.mr_writer)
665 return 0;
666 }
667
668 if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
669 XFS_IOLOCK_EXCL) {
670 if (!ip->i_iolock.mr_writer)
671 return 0;
672 }
673
674 return 1;
675}
676#endif
677