]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - fs/xfs/xfs_buf.c
Merge tag 'docs-5.6-2' of git://git.lwn.net/linux
[linux.git] / fs / xfs / xfs_buf.c
index f9a6cf71f4abffc6940ba79c3fcb059fb926a9b3..217e4f82a44a2601351c8e46f7503512b9a5cc31 100644 (file)
@@ -685,53 +685,39 @@ xfs_buf_incore(
  * cache hits, as metadata intensive workloads will see 3 orders of magnitude
  * more hits than misses.
  */
-struct xfs_buf *
+int
 xfs_buf_get_map(
        struct xfs_buftarg      *target,
        struct xfs_buf_map      *map,
        int                     nmaps,
-       xfs_buf_flags_t         flags)
+       xfs_buf_flags_t         flags,
+       struct xfs_buf          **bpp)
 {
        struct xfs_buf          *bp;
        struct xfs_buf          *new_bp;
        int                     error = 0;
 
+       *bpp = NULL;
        error = xfs_buf_find(target, map, nmaps, flags, NULL, &bp);
-
-       switch (error) {
-       case 0:
-               /* cache hit */
+       if (!error)
                goto found;
-       case -EAGAIN:
-               /* cache hit, trylock failure, caller handles failure */
-               ASSERT(flags & XBF_TRYLOCK);
-               return NULL;
-       case -ENOENT:
-               /* cache miss, go for insert */
-               break;
-       case -EFSCORRUPTED:
-       default:
-               /*
-                * None of the higher layers understand failure types
-                * yet, so return NULL to signal a fatal lookup error.
-                */
-               return NULL;
-       }
+       if (error != -ENOENT)
+               return error;
 
        error = _xfs_buf_alloc(target, map, nmaps, flags, &new_bp);
        if (error)
-               return NULL;
+               return error;
 
        error = xfs_buf_allocate_memory(new_bp, flags);
        if (error) {
                xfs_buf_free(new_bp);
-               return NULL;
+               return error;
        }
 
        error = xfs_buf_find(target, map, nmaps, flags, new_bp, &bp);
        if (error) {
                xfs_buf_free(new_bp);
-               return NULL;
+               return error;
        }
 
        if (bp != new_bp)
@@ -744,7 +730,7 @@ xfs_buf_get_map(
                        xfs_warn(target->bt_mount,
                                "%s: failed to map pagesn", __func__);
                        xfs_buf_relse(bp);
-                       return NULL;
+                       return error;
                }
        }
 
@@ -757,7 +743,8 @@ xfs_buf_get_map(
 
        XFS_STATS_INC(target->bt_mount, xb_get);
        trace_xfs_buf_get(bp, flags, _RET_IP_);
-       return bp;
+       *bpp = bp;
+       return 0;
 }
 
 STATIC int
@@ -809,46 +796,77 @@ xfs_buf_reverify(
        return bp->b_error;
 }
 
-xfs_buf_t *
+int
 xfs_buf_read_map(
        struct xfs_buftarg      *target,
        struct xfs_buf_map      *map,
        int                     nmaps,
        xfs_buf_flags_t         flags,
-       const struct xfs_buf_ops *ops)
+       struct xfs_buf          **bpp,
+       const struct xfs_buf_ops *ops,
+       xfs_failaddr_t          fa)
 {
        struct xfs_buf          *bp;
+       int                     error;
 
        flags |= XBF_READ;
+       *bpp = NULL;
 
-       bp = xfs_buf_get_map(target, map, nmaps, flags);
-       if (!bp)
-               return NULL;
+       error = xfs_buf_get_map(target, map, nmaps, flags, &bp);
+       if (error)
+               return error;
 
        trace_xfs_buf_read(bp, flags, _RET_IP_);
 
        if (!(bp->b_flags & XBF_DONE)) {
+               /* Initiate the buffer read and wait. */
                XFS_STATS_INC(target->bt_mount, xb_get_read);
                bp->b_ops = ops;
-               _xfs_buf_read(bp, flags);
-               return bp;
+               error = _xfs_buf_read(bp, flags);
+
+               /* Readahead iodone already dropped the buffer, so exit. */
+               if (flags & XBF_ASYNC)
+                       return 0;
+       } else {
+               /* Buffer already read; all we need to do is check it. */
+               error = xfs_buf_reverify(bp, ops);
+
+               /* Readahead already finished; drop the buffer and exit. */
+               if (flags & XBF_ASYNC) {
+                       xfs_buf_relse(bp);
+                       return 0;
+               }
+
+               /* We do not want read in the flags */
+               bp->b_flags &= ~XBF_READ;
+               ASSERT(bp->b_ops != NULL || ops == NULL);
        }
 
-       xfs_buf_reverify(bp, ops);
+       /*
+        * If we've had a read error, then the contents of the buffer are
+        * invalid and should not be used. To ensure that a followup read tries
+        * to pull the buffer from disk again, we clear the XBF_DONE flag and
+        * mark the buffer stale. This ensures that anyone who has a current
+        * reference to the buffer will interpret it's contents correctly and
+        * future cache lookups will also treat it as an empty, uninitialised
+        * buffer.
+        */
+       if (error) {
+               if (!XFS_FORCED_SHUTDOWN(target->bt_mount))
+                       xfs_buf_ioerror_alert(bp, fa);
 
-       if (flags & XBF_ASYNC) {
-               /*
-                * Read ahead call which is already satisfied,
-                * drop the buffer
-                */
+               bp->b_flags &= ~XBF_DONE;
+               xfs_buf_stale(bp);
                xfs_buf_relse(bp);
-               return NULL;
+
+               /* bad CRC means corrupted metadata */
+               if (error == -EFSBADCRC)
+                       error = -EFSCORRUPTED;
+               return error;
        }
 
-       /* We do not want read in the flags */
-       bp->b_flags &= ~XBF_READ;
-       ASSERT(bp->b_ops != NULL || ops == NULL);
-       return bp;
+       *bpp = bp;
+       return 0;
 }
 
 /*
@@ -862,11 +880,14 @@ xfs_buf_readahead_map(
        int                     nmaps,
        const struct xfs_buf_ops *ops)
 {
+       struct xfs_buf          *bp;
+
        if (bdi_read_congested(target->bt_bdev->bd_bdi))
                return;
 
        xfs_buf_read_map(target, map, nmaps,
-                    XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
+                    XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD, &bp, ops,
+                    __this_address);
 }
 
 /*
@@ -883,12 +904,13 @@ xfs_buf_read_uncached(
        const struct xfs_buf_ops *ops)
 {
        struct xfs_buf          *bp;
+       int                     error;
 
        *bpp = NULL;
 
-       bp = xfs_buf_get_uncached(target, numblks, flags);
-       if (!bp)
-               return -ENOMEM;
+       error = xfs_buf_get_uncached(target, numblks, flags, &bp);
+       if (error)
+               return error;
 
        /* set up the buffer for a read IO */
        ASSERT(bp->b_map_count == 1);
@@ -899,7 +921,7 @@ xfs_buf_read_uncached(
 
        xfs_buf_submit(bp);
        if (bp->b_error) {
-               int     error = bp->b_error;
+               error = bp->b_error;
                xfs_buf_relse(bp);
                return error;
        }
@@ -908,17 +930,20 @@ xfs_buf_read_uncached(
        return 0;
 }
 
-xfs_buf_t *
+int
 xfs_buf_get_uncached(
        struct xfs_buftarg      *target,
        size_t                  numblks,
-       int                     flags)
+       int                     flags,
+       struct xfs_buf          **bpp)
 {
        unsigned long           page_count;
        int                     error, i;
        struct xfs_buf          *bp;
        DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
 
+       *bpp = NULL;
+
        /* flags might contain irrelevant bits, pass only what we care about */
        error = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT, &bp);
        if (error)
@@ -931,8 +956,10 @@ xfs_buf_get_uncached(
 
        for (i = 0; i < page_count; i++) {
                bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
-               if (!bp->b_pages[i])
+               if (!bp->b_pages[i]) {
+                       error = -ENOMEM;
                        goto fail_free_mem;
+               }
        }
        bp->b_flags |= _XBF_PAGES;
 
@@ -944,7 +971,8 @@ xfs_buf_get_uncached(
        }
 
        trace_xfs_buf_get_uncached(bp, _RET_IP_);
-       return bp;
+       *bpp = bp;
+       return 0;
 
  fail_free_mem:
        while (--i >= 0)
@@ -954,7 +982,7 @@ xfs_buf_get_uncached(
        xfs_buf_free_maps(bp);
        kmem_cache_free(xfs_buf_zone, bp);
  fail:
-       return NULL;
+       return error;
 }
 
 /*
@@ -1208,10 +1236,10 @@ __xfs_buf_ioerror(
 void
 xfs_buf_ioerror_alert(
        struct xfs_buf          *bp,
-       const char              *func)
+       xfs_failaddr_t          func)
 {
        xfs_alert(bp->b_mount,
-"metadata I/O error in \"%s\" at daddr 0x%llx len %d error %d",
+"metadata I/O error in \"%pS\" at daddr 0x%llx len %d error %d",
                        func, (uint64_t)XFS_BUF_ADDR(bp), bp->b_length,
                        -bp->b_error);
 }