[XFS] free partially initialized inodes using destroy_inode
[linux-2.6] / fs / xfs / xfs_iget.c
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
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.
8  *
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.
13  *
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
17  */
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_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_quota.h"
40 #include "xfs_utils.h"
41 #include "xfs_trans_priv.h"
42 #include "xfs_inode_item.h"
43
44 /*
45  * Check the validity of the inode we just found it the cache
46  */
47 static int
48 xfs_iget_cache_hit(
49         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;
55         int                     error = 0;
56
57         /*
58          * If INEW is set this inode is being set up
59          * If IRECLAIM is set this inode is being torn down
60          * Pause and try again.
61          */
62         if (xfs_iflags_test(ip, (XFS_INEW|XFS_IRECLAIM))) {
63                 error = EAGAIN;
64                 XFS_STATS_INC(xs_ig_frecycle);
65                 goto out_error;
66         }
67
68         /* If IRECLAIMABLE is set, we've torn down the vfs inode part */
69         if (xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
70
71                 /*
72                  * 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.
75                  */
76
77                 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
78                         error = ENOENT;
79                         goto out_error;
80                 }
81
82                 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
83
84                 /*
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);
95                 xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
96
97                 /* clear the radix tree reclaim flag as well. */
98                 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
99                 read_unlock(&pag->pag_ici_lock);
100         } 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;
105         } else {
106                 /* we've got a live one */
107                 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
123 out_error:
124         read_unlock(&pag->pag_ici_lock);
125 out:
126         return error;
127 }
128
129
130 static int
131 xfs_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         if (lock_flags)
163                 xfs_ilock(ip, lock_flags);
164
165         /*
166          * Preload the radix tree so we can insert safely under the
167          * write spinlock. Note that we cannot sleep inside the preload
168          * region.
169          */
170         if (radix_tree_preload(GFP_KERNEL)) {
171                 error = EAGAIN;
172                 goto out_unlock;
173         }
174
175         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;
185                 goto out_preload_end;
186         }
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
197 out_preload_end:
198         write_unlock(&pag->pag_ici_lock);
199         radix_tree_preload_end();
200 out_unlock:
201         if (lock_flags)
202                 xfs_iunlock(ip, lock_flags);
203 out_destroy:
204         xfs_destroy_inode(ip);
205         return error;
206 }
207
208 /*
209  * Look up an inode by number in the given file system.
210  * The inode is looked up in the cache held in each AG.
211  * If the inode is found in the cache, initialise the vfs inode
212  * if necessary.
213  *
214  * If it is not in core, read it in from the file system's device,
215  * add it to the cache and initialise the vfs inode.
216  *
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  */
232 int
233 xfs_iget(
234         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 {
242         xfs_inode_t     *ip;
243         int             error;
244         xfs_perag_t     *pag;
245         xfs_agino_t     agino;
246
247         /* the radix tree exists only in inode capable AGs */
248         if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
249                 return EINVAL;
250
251         /* 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);
257
258 again:
259         error = 0;
260         read_lock(&pag->pag_ici_lock);
261         ip = radix_tree_lookup(&pag->pag_ici_root, agino);
262
263         if (ip) {
264                 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
265                 if (error)
266                         goto out_error_or_again;
267         } else {
268                 read_unlock(&pag->pag_ici_lock);
269                 XFS_STATS_INC(xs_ig_missed);
270
271                 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
272                                                         flags, lock_flags);
273                 if (error)
274                         goto out_error_or_again;
275         }
276         xfs_put_perag(mp, pag);
277
278         xfs_iflags_set(ip, XFS_IMODIFIED);
279         *ipp = ip;
280
281         ASSERT(ip->i_df.if_ext_max ==
282                XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
283         /*
284          * 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          */
287         if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
288                 xfs_setup_inode(ip);
289         return 0;
290
291 out_error_or_again:
292         if (error == EAGAIN) {
293                 delay(1);
294                 goto again;
295         }
296         xfs_put_perag(mp, pag);
297         return error;
298 }
299
300
301 /*
302  * 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  */
306 xfs_inode_t *
307 xfs_inode_incore(xfs_mount_t    *mp,
308                  xfs_ino_t      ino,
309                  xfs_trans_t    *tp)
310 {
311         xfs_inode_t     *ip;
312         xfs_perag_t     *pag;
313
314         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;
324 }
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  */
334 void
335 xfs_iput(xfs_inode_t    *ip,
336          uint           lock_flags)
337 {
338         xfs_itrace_entry(ip);
339         xfs_iunlock(ip, lock_flags);
340         IRELE(ip);
341 }
342
343 /*
344  * Special iput for brand-new inodes that are still locked
345  */
346 void
347 xfs_iput_new(
348         xfs_inode_t     *ip,
349         uint            lock_flags)
350 {
351         struct inode    *inode = VFS_I(ip);
352
353         xfs_itrace_entry(ip);
354
355         if ((ip->i_d.di_mode == 0)) {
356                 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
357                 make_bad_inode(inode);
358         }
359         if (inode->i_state & I_NEW)
360                 unlock_new_inode(inode);
361         if (lock_flags)
362                 xfs_iunlock(ip, lock_flags);
363         IRELE(ip);
364 }
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  */
373 void
374 xfs_ireclaim(xfs_inode_t *ip)
375 {
376         /*
377          * Remove from old hash list and mount list.
378          */
379         XFS_STATS_INC(xs_ig_reclaims);
380
381         xfs_iextract(ip);
382
383         /*
384          * 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.
391          */
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         /*
401          * Free all memory associated with the inode.
402          */
403         xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
404         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  */
412 void
413 xfs_iextract(
414         xfs_inode_t     *ip)
415 {
416         xfs_mount_t     *mp = ip->i_mount;
417         xfs_perag_t     *pag = xfs_get_perag(mp, ip->i_ino);
418
419         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);
423
424         mp->m_ireclaims++;
425 }
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  */
445 uint
446 xfs_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  */
467 void
468 xfs_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  */
495 void
496 xfs_ilock(
497         xfs_inode_t             *ip,
498         uint                    lock_flags)
499 {
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));
509         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
510
511         if (lock_flags & XFS_IOLOCK_EXCL)
512                 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
513         else if (lock_flags & XFS_IOLOCK_SHARED)
514                 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
515
516         if (lock_flags & XFS_ILOCK_EXCL)
517                 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
518         else if (lock_flags & XFS_ILOCK_SHARED)
519                 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
520
521         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.
535  */
536 int
537 xfs_ilock_nowait(
538         xfs_inode_t             *ip,
539         uint                    lock_flags)
540 {
541         /*
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));
550         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
551
552         if (lock_flags & XFS_IOLOCK_EXCL) {
553                 if (!mrtryupdate(&ip->i_iolock))
554                         goto out;
555         } else if (lock_flags & XFS_IOLOCK_SHARED) {
556                 if (!mrtryaccess(&ip->i_iolock))
557                         goto out;
558         }
559         if (lock_flags & XFS_ILOCK_EXCL) {
560                 if (!mrtryupdate(&ip->i_lock))
561                         goto out_undo_iolock;
562         } else if (lock_flags & XFS_ILOCK_SHARED) {
563                 if (!mrtryaccess(&ip->i_lock))
564                         goto out_undo_iolock;
565         }
566         xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
567         return 1;
568
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;
576 }
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  */
590 void
591 xfs_iunlock(
592         xfs_inode_t             *ip,
593         uint                    lock_flags)
594 {
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));
604         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
605                         XFS_LOCK_DEP_MASK)) == 0);
606         ASSERT(lock_flags != 0);
607
608         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);
612
613         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);
617
618         if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
619             !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
620                 /*
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                  */
625                 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
626                                         (xfs_log_item_t*)(ip->i_itemp));
627         }
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  */
635 void
636 xfs_ilock_demote(
637         xfs_inode_t             *ip,
638         uint                    lock_flags)
639 {
640         ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
641         ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
642
643         if (lock_flags & XFS_ILOCK_EXCL)
644                 mrdemote(&ip->i_lock);
645         if (lock_flags & XFS_IOLOCK_EXCL)
646                 mrdemote(&ip->i_iolock);
647 }
648
649 #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  */
657 int
658 xfs_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