xfs: Non-blocking inode locking in IO completion
[safe/jmp/linux-2.6] / fs / xfs / xfs_filestream.c
index eb03eab..390850e 100644 (file)
@@ -34,6 +34,7 @@
 #include "xfs_utils.h"
 #include "xfs_mru_cache.h"
 #include "xfs_filestream.h"
+#include "xfs_trace.h"
 
 #ifdef XFS_FILESTREAMS_TRACE
 
@@ -73,7 +74,7 @@ xfs_filestreams_trace(
 #define TRACE4(mp,t,a0,a1,a2,a3)       TRACE6(mp,t,a0,a1,a2,a3,0,0)
 #define TRACE5(mp,t,a0,a1,a2,a3,a4)    TRACE6(mp,t,a0,a1,a2,a3,a4,0)
 #define TRACE6(mp,t,a0,a1,a2,a3,a4,a5) \
-       xfs_filestreams_trace(mp, t, __FUNCTION__, __LINE__, \
+       xfs_filestreams_trace(mp, t, __func__, __LINE__, \
                                (__psunsigned_t)a0, (__psunsigned_t)a1, \
                                (__psunsigned_t)a2, (__psunsigned_t)a3, \
                                (__psunsigned_t)a4, (__psunsigned_t)a5)
@@ -139,8 +140,9 @@ _xfs_filestream_pick_ag(
        int             flags,
        xfs_extlen_t    minlen)
 {
+       int             streams, max_streams;
        int             err, trylock, nscan;
-       xfs_extlen_t    delta, longest, need, free, minfree, maxfree = 0;
+       xfs_extlen_t    longest, free, minfree, maxfree = 0;
        xfs_agnumber_t  ag, max_ag = NULLAGNUMBER;
        struct xfs_perag *pag;
 
@@ -154,15 +156,15 @@ _xfs_filestream_pick_ag(
        trylock = XFS_ALLOC_FLAG_TRYLOCK;
 
        for (nscan = 0; 1; nscan++) {
-
-               TRACE_AG_SCAN(mp, ag, xfs_filestream_peek_ag(mp, ag));
-
-               pag = mp->m_perag + ag;
+               pag = xfs_perag_get(mp, ag);
+               TRACE_AG_SCAN(mp, ag, atomic_read(&pag->pagf_fstrms));
 
                if (!pag->pagf_init) {
                        err = xfs_alloc_pagf_init(mp, NULL, ag, trylock);
-                       if (err && !trylock)
+                       if (err && !trylock) {
+                               xfs_perag_put(pag);
                                return err;
+                       }
                }
 
                /* Might fail sometimes during the 1st pass with trylock set. */
@@ -172,6 +174,7 @@ _xfs_filestream_pick_ag(
                /* Keep track of the AG with the most free blocks. */
                if (pag->pagf_freeblks > maxfree) {
                        maxfree = pag->pagf_freeblks;
+                       max_streams = atomic_read(&pag->pagf_fstrms);
                        max_ag = ag;
                }
 
@@ -186,12 +189,7 @@ _xfs_filestream_pick_ag(
                        goto next_ag;
                }
 
-               need = XFS_MIN_FREELIST_PAG(pag, mp);
-               delta = need > pag->pagf_flcount ? need - pag->pagf_flcount : 0;
-               longest = (pag->pagf_longest > delta) ?
-                         (pag->pagf_longest - delta) :
-                         (pag->pagf_flcount > 0 || pag->pagf_longest > 0);
-
+               longest = xfs_alloc_longest_free_extent(mp, pag);
                if (((minlen && longest >= minlen) ||
                     (!minlen && pag->pagf_freeblks >= minfree)) &&
                    (!pag->pagf_metadata || !(flags & XFS_PICK_USERDATA) ||
@@ -199,6 +197,8 @@ _xfs_filestream_pick_ag(
 
                        /* Break out, retaining the reference on the AG. */
                        free = pag->pagf_freeblks;
+                       streams = atomic_read(&pag->pagf_fstrms);
+                       xfs_perag_put(pag);
                        *agp = ag;
                        break;
                }
@@ -206,6 +206,7 @@ _xfs_filestream_pick_ag(
                /* Drop the reference on this AG, it's not usable. */
                xfs_filestream_put_ag(mp, ag);
 next_ag:
+               xfs_perag_put(pag);
                /* Move to the next AG, wrapping to AG 0 if necessary. */
                if (++ag >= mp->m_sb.sb_agcount)
                        ag = 0;
@@ -233,6 +234,7 @@ next_ag:
                if (max_ag != NULLAGNUMBER) {
                        xfs_filestream_get_ag(mp, max_ag);
                        TRACE_AG_PICK1(mp, max_ag, maxfree);
+                       streams = max_streams;
                        free = maxfree;
                        *agp = max_ag;
                        break;
@@ -244,16 +246,14 @@ next_ag:
                return 0;
        }
 
-       TRACE_AG_PICK2(mp, startag, *agp, xfs_filestream_peek_ag(mp, *agp),
-                       free, nscan, flags);
+       TRACE_AG_PICK2(mp, startag, *agp, streams, free, nscan, flags);
 
        return 0;
 }
 
 /*
  * Set the allocation group number for a file or a directory, updating inode
- * references and per-AG references as appropriate.  Must be called with the
- * m_peraglock held in read mode.
+ * references and per-AG references as appropriate.
  */
 static int
 _xfs_filestream_update_ag(
@@ -397,10 +397,10 @@ int
 xfs_filestream_init(void)
 {
        item_zone = kmem_zone_init(sizeof(fstrm_item_t), "fstrm_item");
-#ifdef XFS_FILESTREAMS_TRACE
-       xfs_filestreams_trace_buf = ktrace_alloc(XFS_FSTRM_KTRACE_SIZE, KM_SLEEP);
-#endif
-       return item_zone ? 0 : -ENOMEM;
+       if (!item_zone)
+               return -ENOMEM;
+
+       return 0;
 }
 
 /*
@@ -410,9 +410,6 @@ xfs_filestream_init(void)
 void
 xfs_filestream_uninit(void)
 {
-#ifdef XFS_FILESTREAMS_TRACE
-       ktrace_free(xfs_filestreams_trace_buf);
-#endif
        kmem_zone_destroy(item_zone);
 }
 
@@ -458,20 +455,6 @@ xfs_filestream_unmount(
 }
 
 /*
- * If the mount point's m_perag array is going to be reallocated, all
- * outstanding cache entries must be flushed to avoid accessing reference count
- * addresses that have been freed.  The call to xfs_filestream_flush() must be
- * made inside the block that holds the m_peraglock in write mode to do the
- * reallocation.
- */
-void
-xfs_filestream_flush(
-       xfs_mount_t     *mp)
-{
-       xfs_mru_cache_flush(mp->m_filestream);
-}
-
-/*
  * Return the AG of the filestream the file or directory belongs to, or
  * NULLAGNUMBER otherwise.
  */
@@ -533,7 +516,6 @@ xfs_filestream_associate(
 
        mp = pip->i_mount;
        cache = mp->m_filestream;
-       down_read(&mp->m_peraglock);
 
        /*
         * We have a problem, Houston.
@@ -545,17 +527,13 @@ xfs_filestream_associate(
         * waiting for the lock because someone else is waiting on the lock we
         * hold and we cannot drop that as we are in a transaction here.
         *
-        * Lucky for us, this inversion is rarely a problem because it's a
-        * directory inode that we are trying to lock here and that means the
-        * only place that matters is xfs_sync_inodes() and SYNC_DELWRI is
-        * used. i.e. freeze, remount-ro, quotasync or unmount.
+        * Lucky for us, this inversion is not a problem because it's a
+        * directory inode that we are trying to lock here.
         *
         * So, if we can't get the iolock without sleeping then just give up
         */
-       if (!xfs_ilock_nowait(pip, XFS_IOLOCK_EXCL)) {
-               up_read(&mp->m_peraglock);
+       if (!xfs_ilock_nowait(pip, XFS_IOLOCK_EXCL))
                return 1;
-       }
 
        /* If the parent directory is already in the cache, use its AG. */
        item = xfs_mru_cache_lookup(cache, pip->i_ino);
@@ -610,7 +588,6 @@ exit_did_pick:
 
 exit:
        xfs_iunlock(pip, XFS_IOLOCK_EXCL);
-       up_read(&mp->m_peraglock);
        return -err;
 }