Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | f07c225 | 2006-09-28 10:52:15 +1000 | [diff] [blame] | 3 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Vlad Apostolov | 93c189c | 2006-11-11 18:03:49 +1100 | [diff] [blame] | 6 | #include "xfs.h" |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 7 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
Darrick J. Wong | 5467b34 | 2019-06-28 19:25:35 -0700 | [diff] [blame] | 9 | #include "xfs_shared.h" |
Christoph Hellwig | 4fb6e8a | 2014-11-28 14:25:04 +1100 | [diff] [blame] | 10 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 11 | #include "xfs_log_format.h" |
Dave Chinner | 7fd36c4 | 2013-08-12 20:49:32 +1000 | [diff] [blame] | 12 | #include "xfs_trans_resv.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 13 | #include "xfs_sb.h" |
Christoph Hellwig | b796313 | 2009-03-03 14:48:37 -0500 | [diff] [blame] | 14 | #include "xfs_mount.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 15 | #include "xfs_trace.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 16 | #include "xfs_log.h" |
Darrick J. Wong | e9e899a | 2017-10-31 12:04:49 -0700 | [diff] [blame] | 17 | #include "xfs_errortag.h" |
Brian Foster | 7561d27 | 2017-10-17 14:16:29 -0700 | [diff] [blame] | 18 | #include "xfs_error.h" |
Christoph Hellwig | b796313 | 2009-03-03 14:48:37 -0500 | [diff] [blame] | 19 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 20 | static kmem_zone_t *xfs_buf_zone; |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 21 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 22 | #define xb_to_gfp(flags) \ |
Dave Chinner | aa5c158 | 2012-04-23 15:58:56 +1000 | [diff] [blame] | 23 | ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Dave Chinner | 37fd167 | 2018-10-18 17:21:29 +1100 | [diff] [blame] | 25 | /* |
| 26 | * Locking orders |
| 27 | * |
| 28 | * xfs_buf_ioacct_inc: |
| 29 | * xfs_buf_ioacct_dec: |
| 30 | * b_sema (caller holds) |
| 31 | * b_lock |
| 32 | * |
| 33 | * xfs_buf_stale: |
| 34 | * b_sema (caller holds) |
| 35 | * b_lock |
| 36 | * lru_lock |
| 37 | * |
| 38 | * xfs_buf_rele: |
| 39 | * b_lock |
| 40 | * pag_buf_lock |
| 41 | * lru_lock |
| 42 | * |
| 43 | * xfs_buftarg_wait_rele |
| 44 | * lru_lock |
| 45 | * b_lock (trylock due to inversion) |
| 46 | * |
| 47 | * xfs_buftarg_isolate |
| 48 | * lru_lock |
| 49 | * b_lock (trylock due to inversion) |
| 50 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
James Bottomley | 73c77e2 | 2010-01-25 11:42:24 -0600 | [diff] [blame] | 52 | static inline int |
| 53 | xfs_buf_is_vmapped( |
| 54 | struct xfs_buf *bp) |
| 55 | { |
| 56 | /* |
| 57 | * Return true if the buffer is vmapped. |
| 58 | * |
Dave Chinner | 611c994 | 2012-04-23 15:59:07 +1000 | [diff] [blame] | 59 | * b_addr is null if the buffer is not mapped, but the code is clever |
| 60 | * enough to know it doesn't have to map a single page, so the check has |
| 61 | * to be both for b_addr and bp->b_page_count > 1. |
James Bottomley | 73c77e2 | 2010-01-25 11:42:24 -0600 | [diff] [blame] | 62 | */ |
Dave Chinner | 611c994 | 2012-04-23 15:59:07 +1000 | [diff] [blame] | 63 | return bp->b_addr && bp->b_page_count > 1; |
James Bottomley | 73c77e2 | 2010-01-25 11:42:24 -0600 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static inline int |
| 67 | xfs_buf_vmap_len( |
| 68 | struct xfs_buf *bp) |
| 69 | { |
| 70 | return (bp->b_page_count * PAGE_SIZE) - bp->b_offset; |
| 71 | } |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | /* |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 74 | * Bump the I/O in flight count on the buftarg if we haven't yet done so for |
| 75 | * this buffer. The count is incremented once per buffer (per hold cycle) |
| 76 | * because the corresponding decrement is deferred to buffer release. Buffers |
| 77 | * can undergo I/O multiple times in a hold-release cycle and per buffer I/O |
| 78 | * tracking adds unnecessary overhead. This is used for sychronization purposes |
| 79 | * with unmount (see xfs_wait_buftarg()), so all we really need is a count of |
| 80 | * in-flight buffers. |
| 81 | * |
| 82 | * Buffers that are never released (e.g., superblock, iclog buffers) must set |
| 83 | * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count |
| 84 | * never reaches zero and unmount hangs indefinitely. |
| 85 | */ |
| 86 | static inline void |
| 87 | xfs_buf_ioacct_inc( |
| 88 | struct xfs_buf *bp) |
| 89 | { |
Brian Foster | 63db7c8 | 2017-05-31 08:22:52 -0700 | [diff] [blame] | 90 | if (bp->b_flags & XBF_NO_IOACCT) |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 91 | return; |
| 92 | |
| 93 | ASSERT(bp->b_flags & XBF_ASYNC); |
Brian Foster | 63db7c8 | 2017-05-31 08:22:52 -0700 | [diff] [blame] | 94 | spin_lock(&bp->b_lock); |
| 95 | if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) { |
| 96 | bp->b_state |= XFS_BSTATE_IN_FLIGHT; |
| 97 | percpu_counter_inc(&bp->b_target->bt_io_count); |
| 98 | } |
| 99 | spin_unlock(&bp->b_lock); |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Clear the in-flight state on a buffer about to be released to the LRU or |
| 104 | * freed and unaccount from the buftarg. |
| 105 | */ |
| 106 | static inline void |
Brian Foster | 63db7c8 | 2017-05-31 08:22:52 -0700 | [diff] [blame] | 107 | __xfs_buf_ioacct_dec( |
| 108 | struct xfs_buf *bp) |
| 109 | { |
Brian Foster | 95989c4 | 2017-06-08 08:23:07 -0700 | [diff] [blame] | 110 | lockdep_assert_held(&bp->b_lock); |
Brian Foster | 63db7c8 | 2017-05-31 08:22:52 -0700 | [diff] [blame] | 111 | |
| 112 | if (bp->b_state & XFS_BSTATE_IN_FLIGHT) { |
| 113 | bp->b_state &= ~XFS_BSTATE_IN_FLIGHT; |
| 114 | percpu_counter_dec(&bp->b_target->bt_io_count); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static inline void |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 119 | xfs_buf_ioacct_dec( |
| 120 | struct xfs_buf *bp) |
| 121 | { |
Brian Foster | 63db7c8 | 2017-05-31 08:22:52 -0700 | [diff] [blame] | 122 | spin_lock(&bp->b_lock); |
| 123 | __xfs_buf_ioacct_dec(bp); |
| 124 | spin_unlock(&bp->b_lock); |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 128 | * When we mark a buffer stale, we remove the buffer from the LRU and clear the |
| 129 | * b_lru_ref count so that the buffer is freed immediately when the buffer |
| 130 | * reference count falls to zero. If the buffer is already on the LRU, we need |
| 131 | * to remove the reference that LRU holds on the buffer. |
| 132 | * |
| 133 | * This prevents build-up of stale buffers on the LRU. |
| 134 | */ |
| 135 | void |
| 136 | xfs_buf_stale( |
| 137 | struct xfs_buf *bp) |
| 138 | { |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 139 | ASSERT(xfs_buf_islocked(bp)); |
| 140 | |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 141 | bp->b_flags |= XBF_STALE; |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | * Clear the delwri status so that a delwri queue walker will not |
| 145 | * flush this buffer to disk now that it is stale. The delwri queue has |
| 146 | * a reference to the buffer, so this is safe to do. |
| 147 | */ |
| 148 | bp->b_flags &= ~_XBF_DELWRI_Q; |
| 149 | |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 150 | /* |
| 151 | * Once the buffer is marked stale and unlocked, a subsequent lookup |
| 152 | * could reset b_flags. There is no guarantee that the buffer is |
| 153 | * unaccounted (released to LRU) before that occurs. Drop in-flight |
| 154 | * status now to preserve accounting consistency. |
| 155 | */ |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 156 | spin_lock(&bp->b_lock); |
Brian Foster | 63db7c8 | 2017-05-31 08:22:52 -0700 | [diff] [blame] | 157 | __xfs_buf_ioacct_dec(bp); |
| 158 | |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 159 | atomic_set(&bp->b_lru_ref, 0); |
| 160 | if (!(bp->b_state & XFS_BSTATE_DISPOSE) && |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 161 | (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru))) |
| 162 | atomic_dec(&bp->b_hold); |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 163 | |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 164 | ASSERT(atomic_read(&bp->b_hold) >= 1); |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 165 | spin_unlock(&bp->b_lock); |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 168 | static int |
| 169 | xfs_buf_get_maps( |
| 170 | struct xfs_buf *bp, |
| 171 | int map_count) |
| 172 | { |
| 173 | ASSERT(bp->b_maps == NULL); |
| 174 | bp->b_map_count = map_count; |
| 175 | |
| 176 | if (map_count == 1) { |
Mark Tinguely | f4b4242 | 2012-12-04 17:18:02 -0600 | [diff] [blame] | 177 | bp->b_maps = &bp->__b_map; |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map), |
| 182 | KM_NOFS); |
| 183 | if (!bp->b_maps) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 184 | return -ENOMEM; |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Frees b_pages if it was allocated. |
| 190 | */ |
| 191 | static void |
| 192 | xfs_buf_free_maps( |
| 193 | struct xfs_buf *bp) |
| 194 | { |
Mark Tinguely | f4b4242 | 2012-12-04 17:18:02 -0600 | [diff] [blame] | 195 | if (bp->b_maps != &bp->__b_map) { |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 196 | kmem_free(bp->b_maps); |
| 197 | bp->b_maps = NULL; |
| 198 | } |
| 199 | } |
| 200 | |
Darrick J. Wong | 32dff5e | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 201 | static int |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 202 | _xfs_buf_alloc( |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 203 | struct xfs_buftarg *target, |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 204 | struct xfs_buf_map *map, |
| 205 | int nmaps, |
Darrick J. Wong | 32dff5e | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 206 | xfs_buf_flags_t flags, |
| 207 | struct xfs_buf **bpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 209 | struct xfs_buf *bp; |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 210 | int error; |
| 211 | int i; |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 212 | |
Darrick J. Wong | 32dff5e | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 213 | *bpp = NULL; |
Dave Chinner | aa5c158 | 2012-04-23 15:58:56 +1000 | [diff] [blame] | 214 | bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS); |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 215 | if (unlikely(!bp)) |
Darrick J. Wong | 32dff5e | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 216 | return -ENOMEM; |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | /* |
Dave Chinner | 12bcb3f | 2012-04-23 15:59:05 +1000 | [diff] [blame] | 219 | * We don't want certain flags to appear in b_flags unless they are |
| 220 | * specifically set by later operations on the buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | */ |
Dave Chinner | 611c994 | 2012-04-23 15:59:07 +1000 | [diff] [blame] | 222 | flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 224 | atomic_set(&bp->b_hold, 1); |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 225 | atomic_set(&bp->b_lru_ref, 1); |
David Chinner | b4dd330 | 2008-08-13 16:36:11 +1000 | [diff] [blame] | 226 | init_completion(&bp->b_iowait); |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 227 | INIT_LIST_HEAD(&bp->b_lru); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 228 | INIT_LIST_HEAD(&bp->b_list); |
Carlos Maiolino | 643c8c0 | 2018-01-24 13:38:49 -0800 | [diff] [blame] | 229 | INIT_LIST_HEAD(&bp->b_li_list); |
Thomas Gleixner | a731cd11 | 2010-09-07 14:33:15 +0000 | [diff] [blame] | 230 | sema_init(&bp->b_sema, 0); /* held, no waiters */ |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 231 | spin_lock_init(&bp->b_lock); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 232 | bp->b_target = target; |
Christoph Hellwig | dbd329f1 | 2019-06-28 19:27:29 -0700 | [diff] [blame] | 233 | bp->b_mount = target->bt_mount; |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 234 | bp->b_flags = flags; |
Dave Chinner | de1cbee | 2012-04-23 15:58:50 +1000 | [diff] [blame] | 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | /* |
Dave Chinner | aa0e883 | 2012-04-23 15:58:52 +1000 | [diff] [blame] | 237 | * Set length and io_length to the same value initially. |
| 238 | * I/O routines should use io_length, which will be the same in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | * most cases but may be reset (e.g. XFS recovery). |
| 240 | */ |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 241 | error = xfs_buf_get_maps(bp, nmaps); |
| 242 | if (error) { |
Carlos Maiolino | 377bcd5 | 2019-11-14 12:43:04 -0800 | [diff] [blame] | 243 | kmem_cache_free(xfs_buf_zone, bp); |
Darrick J. Wong | 32dff5e | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 244 | return error; |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | bp->b_bn = map[0].bm_bn; |
| 248 | bp->b_length = 0; |
| 249 | for (i = 0; i < nmaps; i++) { |
| 250 | bp->b_maps[i].bm_bn = map[i].bm_bn; |
| 251 | bp->b_maps[i].bm_len = map[i].bm_len; |
| 252 | bp->b_length += map[i].bm_len; |
| 253 | } |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 254 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 255 | atomic_set(&bp->b_pin_count, 0); |
| 256 | init_waitqueue_head(&bp->b_waiters); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Christoph Hellwig | dbd329f1 | 2019-06-28 19:27:29 -0700 | [diff] [blame] | 258 | XFS_STATS_INC(bp->b_mount, xb_create); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 259 | trace_xfs_buf_init(bp, _RET_IP_); |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 260 | |
Darrick J. Wong | 32dff5e | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 261 | *bpp = bp; |
| 262 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 266 | * Allocate a page array capable of holding a specified number |
| 267 | * of pages, and point the page buf at it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | */ |
| 269 | STATIC int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 270 | _xfs_buf_get_pages( |
| 271 | xfs_buf_t *bp, |
Eric Sandeen | 87937bf | 2014-04-14 19:01:20 +1000 | [diff] [blame] | 272 | int page_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
| 274 | /* Make sure that we have a page list */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 275 | if (bp->b_pages == NULL) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 276 | bp->b_page_count = page_count; |
| 277 | if (page_count <= XB_PAGES) { |
| 278 | bp->b_pages = bp->b_page_array; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } else { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 280 | bp->b_pages = kmem_alloc(sizeof(struct page *) * |
Dave Chinner | aa5c158 | 2012-04-23 15:58:56 +1000 | [diff] [blame] | 281 | page_count, KM_NOFS); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 282 | if (bp->b_pages == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | return -ENOMEM; |
| 284 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 285 | memset(bp->b_pages, 0, sizeof(struct page *) * page_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 291 | * Frees b_pages if it was allocated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | */ |
| 293 | STATIC void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 294 | _xfs_buf_free_pages( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | xfs_buf_t *bp) |
| 296 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 297 | if (bp->b_pages != bp->b_page_array) { |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 298 | kmem_free(bp->b_pages); |
Dave Chinner | 3fc98b1 | 2009-12-14 23:11:57 +0000 | [diff] [blame] | 299 | bp->b_pages = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * Releases the specified buffer. |
| 305 | * |
| 306 | * The modification state of any associated pages is left unchanged. |
Zhi Yong Wu | b46fe82 | 2013-08-07 10:10:59 +0000 | [diff] [blame] | 307 | * The buffer must not be on any hash - use xfs_buf_rele instead for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | * hashed and refcounted buffers |
| 309 | */ |
Christoph Hellwig | 25a4095 | 2019-10-24 22:25:37 -0700 | [diff] [blame] | 310 | static void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 311 | xfs_buf_free( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | xfs_buf_t *bp) |
| 313 | { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 314 | trace_xfs_buf_free(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 316 | ASSERT(list_empty(&bp->b_lru)); |
| 317 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 318 | if (bp->b_flags & _XBF_PAGES) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | uint i; |
| 320 | |
James Bottomley | 73c77e2 | 2010-01-25 11:42:24 -0600 | [diff] [blame] | 321 | if (xfs_buf_is_vmapped(bp)) |
Alex Elder | 8a262e5 | 2010-03-16 18:55:56 +0000 | [diff] [blame] | 322 | vm_unmap_ram(bp->b_addr - bp->b_offset, |
| 323 | bp->b_page_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Nathan Scott | 948ecdb | 2006-09-28 11:03:13 +1000 | [diff] [blame] | 325 | for (i = 0; i < bp->b_page_count; i++) { |
| 326 | struct page *page = bp->b_pages[i]; |
| 327 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 328 | __free_page(page); |
Nathan Scott | 948ecdb | 2006-09-28 11:03:13 +1000 | [diff] [blame] | 329 | } |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 330 | } else if (bp->b_flags & _XBF_KMEM) |
| 331 | kmem_free(bp->b_addr); |
Dave Chinner | 3fc98b1 | 2009-12-14 23:11:57 +0000 | [diff] [blame] | 332 | _xfs_buf_free_pages(bp); |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 333 | xfs_buf_free_maps(bp); |
Carlos Maiolino | 377bcd5 | 2019-11-14 12:43:04 -0800 | [diff] [blame] | 334 | kmem_cache_free(xfs_buf_zone, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /* |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 338 | * Allocates all the pages for buffer in question and builds it's page list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | */ |
| 340 | STATIC int |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 341 | xfs_buf_allocate_memory( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | xfs_buf_t *bp, |
| 343 | uint flags) |
| 344 | { |
Dave Chinner | aa0e883 | 2012-04-23 15:58:52 +1000 | [diff] [blame] | 345 | size_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | size_t nbytes, offset; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 347 | gfp_t gfp_mask = xb_to_gfp(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | unsigned short page_count, i; |
Dave Chinner | 795cac7 | 2012-04-23 15:58:53 +1000 | [diff] [blame] | 349 | xfs_off_t start, end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | int error; |
Bill O'Donnell | 3219e8c | 2019-10-04 16:38:44 -0700 | [diff] [blame] | 351 | xfs_km_flags_t kmflag_mask = 0; |
| 352 | |
| 353 | /* |
| 354 | * assure zeroed buffer for non-read cases. |
| 355 | */ |
| 356 | if (!(flags & XBF_READ)) { |
| 357 | kmflag_mask |= KM_ZERO; |
| 358 | gfp_mask |= __GFP_ZERO; |
| 359 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 361 | /* |
| 362 | * for buffers that are contained within a single page, just allocate |
| 363 | * the memory from the heap - there's no need for the complexity of |
| 364 | * page arrays to keep allocation down to order 0. |
| 365 | */ |
Dave Chinner | 795cac7 | 2012-04-23 15:58:53 +1000 | [diff] [blame] | 366 | size = BBTOB(bp->b_length); |
| 367 | if (size < PAGE_SIZE) { |
Dave Chinner | f8f9ee4 | 2019-08-26 12:08:39 -0700 | [diff] [blame] | 368 | int align_mask = xfs_buftarg_dma_alignment(bp->b_target); |
Bill O'Donnell | 3219e8c | 2019-10-04 16:38:44 -0700 | [diff] [blame] | 369 | bp->b_addr = kmem_alloc_io(size, align_mask, |
| 370 | KM_NOFS | kmflag_mask); |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 371 | if (!bp->b_addr) { |
| 372 | /* low memory - use alloc_page loop instead */ |
| 373 | goto use_alloc_page; |
| 374 | } |
| 375 | |
Dave Chinner | 795cac7 | 2012-04-23 15:58:53 +1000 | [diff] [blame] | 376 | if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) != |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 377 | ((unsigned long)bp->b_addr & PAGE_MASK)) { |
| 378 | /* b_addr spans two pages - use alloc_page instead */ |
| 379 | kmem_free(bp->b_addr); |
| 380 | bp->b_addr = NULL; |
| 381 | goto use_alloc_page; |
| 382 | } |
| 383 | bp->b_offset = offset_in_page(bp->b_addr); |
| 384 | bp->b_pages = bp->b_page_array; |
Dave Chinner | f8f9ee4 | 2019-08-26 12:08:39 -0700 | [diff] [blame] | 385 | bp->b_pages[0] = kmem_to_page(bp->b_addr); |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 386 | bp->b_page_count = 1; |
Dave Chinner | 611c994 | 2012-04-23 15:59:07 +1000 | [diff] [blame] | 387 | bp->b_flags |= _XBF_KMEM; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | use_alloc_page: |
Mark Tinguely | f4b4242 | 2012-12-04 17:18:02 -0600 | [diff] [blame] | 392 | start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT; |
| 393 | end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1) |
Dave Chinner | cbb7baa | 2012-06-22 18:50:08 +1000 | [diff] [blame] | 394 | >> PAGE_SHIFT; |
Dave Chinner | 795cac7 | 2012-04-23 15:58:53 +1000 | [diff] [blame] | 395 | page_count = end - start; |
Eric Sandeen | 87937bf | 2014-04-14 19:01:20 +1000 | [diff] [blame] | 396 | error = _xfs_buf_get_pages(bp, page_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | if (unlikely(error)) |
| 398 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 400 | offset = bp->b_offset; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 401 | bp->b_flags |= _XBF_PAGES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 403 | for (i = 0; i < bp->b_page_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | struct page *page; |
| 405 | uint retries = 0; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 406 | retry: |
| 407 | page = alloc_page(gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | if (unlikely(page == NULL)) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 409 | if (flags & XBF_READ_AHEAD) { |
| 410 | bp->b_page_count = i; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 411 | error = -ENOMEM; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 412 | goto out_free_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* |
| 416 | * This could deadlock. |
| 417 | * |
| 418 | * But until all the XFS lowlevel code is revamped to |
| 419 | * handle buffer allocation failures we can't do much. |
| 420 | */ |
| 421 | if (!(++retries % 100)) |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 422 | xfs_err(NULL, |
Tetsuo Handa | 5bf97b1 | 2015-10-12 15:41:29 +1100 | [diff] [blame] | 423 | "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)", |
| 424 | current->comm, current->pid, |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 425 | __func__, gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Christoph Hellwig | dbd329f1 | 2019-06-28 19:27:29 -0700 | [diff] [blame] | 427 | XFS_STATS_INC(bp->b_mount, xb_page_retries); |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 428 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | goto retry; |
| 430 | } |
| 431 | |
Christoph Hellwig | dbd329f1 | 2019-06-28 19:27:29 -0700 | [diff] [blame] | 432 | XFS_STATS_INC(bp->b_mount, xb_page_found); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 434 | nbytes = min_t(size_t, size, PAGE_SIZE - offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | size -= nbytes; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 436 | bp->b_pages[i] = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | offset = 0; |
| 438 | } |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 439 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 441 | out_free_pages: |
| 442 | for (i = 0; i < bp->b_page_count; i++) |
| 443 | __free_page(bp->b_pages[i]); |
Darrick J. Wong | 2aa6ba7b | 2017-01-25 20:24:57 -0800 | [diff] [blame] | 444 | bp->b_flags &= ~_XBF_PAGES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | return error; |
| 446 | } |
| 447 | |
| 448 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 449 | * Map buffer into kernel address-space if necessary. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | */ |
| 451 | STATIC int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 452 | _xfs_buf_map_pages( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | xfs_buf_t *bp, |
| 454 | uint flags) |
| 455 | { |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 456 | ASSERT(bp->b_flags & _XBF_PAGES); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 457 | if (bp->b_page_count == 1) { |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 458 | /* A single page buffer is always mappable */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 459 | bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset; |
Dave Chinner | 611c994 | 2012-04-23 15:59:07 +1000 | [diff] [blame] | 460 | } else if (flags & XBF_UNMAPPED) { |
| 461 | bp->b_addr = NULL; |
| 462 | } else { |
Dave Chinner | a19fb38 | 2011-03-26 09:13:42 +1100 | [diff] [blame] | 463 | int retried = 0; |
Michal Hocko | 9ba1fb2 | 2017-05-03 14:53:19 -0700 | [diff] [blame] | 464 | unsigned nofs_flag; |
Dave Chinner | a19fb38 | 2011-03-26 09:13:42 +1100 | [diff] [blame] | 465 | |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 466 | /* |
Joe Perches | cf085a1 | 2019-11-07 13:24:52 -0800 | [diff] [blame] | 467 | * vm_map_ram() will allocate auxiliary structures (e.g. |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 468 | * pagetables) with GFP_KERNEL, yet we are likely to be under |
| 469 | * GFP_NOFS context here. Hence we need to tell memory reclaim |
Michal Hocko | 9ba1fb2 | 2017-05-03 14:53:19 -0700 | [diff] [blame] | 470 | * that we are in such a context via PF_MEMALLOC_NOFS to prevent |
Dave Chinner | ae687e5 | 2014-03-07 16:19:14 +1100 | [diff] [blame] | 471 | * memory reclaim re-entering the filesystem here and |
| 472 | * potentially deadlocking. |
| 473 | */ |
Michal Hocko | 9ba1fb2 | 2017-05-03 14:53:19 -0700 | [diff] [blame] | 474 | nofs_flag = memalloc_nofs_save(); |
Dave Chinner | a19fb38 | 2011-03-26 09:13:42 +1100 | [diff] [blame] | 475 | do { |
| 476 | bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count, |
| 477 | -1, PAGE_KERNEL); |
| 478 | if (bp->b_addr) |
| 479 | break; |
| 480 | vm_unmap_aliases(); |
| 481 | } while (retried++ <= 1); |
Michal Hocko | 9ba1fb2 | 2017-05-03 14:53:19 -0700 | [diff] [blame] | 482 | memalloc_nofs_restore(nofs_flag); |
Dave Chinner | a19fb38 | 2011-03-26 09:13:42 +1100 | [diff] [blame] | 483 | |
| 484 | if (!bp->b_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | return -ENOMEM; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 486 | bp->b_addr += bp->b_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Finding and Reading Buffers |
| 494 | */ |
Lucas Stach | 6031e73 | 2016-12-07 17:36:36 +1100 | [diff] [blame] | 495 | static int |
| 496 | _xfs_buf_obj_cmp( |
| 497 | struct rhashtable_compare_arg *arg, |
| 498 | const void *obj) |
| 499 | { |
| 500 | const struct xfs_buf_map *map = arg->key; |
| 501 | const struct xfs_buf *bp = obj; |
| 502 | |
| 503 | /* |
| 504 | * The key hashing in the lookup path depends on the key being the |
| 505 | * first element of the compare_arg, make sure to assert this. |
| 506 | */ |
| 507 | BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0); |
| 508 | |
| 509 | if (bp->b_bn != map->bm_bn) |
| 510 | return 1; |
| 511 | |
| 512 | if (unlikely(bp->b_length != map->bm_len)) { |
| 513 | /* |
| 514 | * found a block number match. If the range doesn't |
| 515 | * match, the only way this is allowed is if the buffer |
| 516 | * in the cache is stale and the transaction that made |
| 517 | * it stale has not yet committed. i.e. we are |
| 518 | * reallocating a busy extent. Skip this buffer and |
| 519 | * continue searching for an exact match. |
| 520 | */ |
| 521 | ASSERT(bp->b_flags & XBF_STALE); |
| 522 | return 1; |
| 523 | } |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static const struct rhashtable_params xfs_buf_hash_params = { |
| 528 | .min_size = 32, /* empty AGs have minimal footprint */ |
| 529 | .nelem_hint = 16, |
| 530 | .key_len = sizeof(xfs_daddr_t), |
| 531 | .key_offset = offsetof(struct xfs_buf, b_bn), |
| 532 | .head_offset = offsetof(struct xfs_buf, b_rhash_head), |
| 533 | .automatic_shrinking = true, |
| 534 | .obj_cmpfn = _xfs_buf_obj_cmp, |
| 535 | }; |
| 536 | |
| 537 | int |
| 538 | xfs_buf_hash_init( |
| 539 | struct xfs_perag *pag) |
| 540 | { |
| 541 | spin_lock_init(&pag->pag_buf_lock); |
| 542 | return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params); |
| 543 | } |
| 544 | |
| 545 | void |
| 546 | xfs_buf_hash_destroy( |
| 547 | struct xfs_perag *pag) |
| 548 | { |
| 549 | rhashtable_destroy(&pag->pag_buf_hash); |
| 550 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
| 552 | /* |
Dave Chinner | b027d4c | 2018-04-18 08:25:21 -0700 | [diff] [blame] | 553 | * Look up a buffer in the buffer cache and return it referenced and locked |
| 554 | * in @found_bp. |
| 555 | * |
| 556 | * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the |
| 557 | * cache. |
| 558 | * |
| 559 | * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return |
| 560 | * -EAGAIN if we fail to lock it. |
| 561 | * |
| 562 | * Return values are: |
| 563 | * -EFSCORRUPTED if have been supplied with an invalid address |
| 564 | * -EAGAIN on trylock failure |
| 565 | * -ENOENT if we fail to find a match and @new_bp was NULL |
| 566 | * 0, with @found_bp: |
| 567 | * - @new_bp if we inserted it into the cache |
| 568 | * - the buffer we found and locked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | */ |
Dave Chinner | b027d4c | 2018-04-18 08:25:21 -0700 | [diff] [blame] | 570 | static int |
| 571 | xfs_buf_find( |
Dave Chinner | e70b73f | 2012-04-23 15:58:49 +1000 | [diff] [blame] | 572 | struct xfs_buftarg *btp, |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 573 | struct xfs_buf_map *map, |
| 574 | int nmaps, |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 575 | xfs_buf_flags_t flags, |
Dave Chinner | b027d4c | 2018-04-18 08:25:21 -0700 | [diff] [blame] | 576 | struct xfs_buf *new_bp, |
| 577 | struct xfs_buf **found_bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 579 | struct xfs_perag *pag; |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 580 | xfs_buf_t *bp; |
Lucas Stach | 6031e73 | 2016-12-07 17:36:36 +1100 | [diff] [blame] | 581 | struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn }; |
Dave Chinner | 10616b8 | 2013-01-21 23:53:52 +1100 | [diff] [blame] | 582 | xfs_daddr_t eofs; |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 583 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | |
Dave Chinner | b027d4c | 2018-04-18 08:25:21 -0700 | [diff] [blame] | 585 | *found_bp = NULL; |
| 586 | |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 587 | for (i = 0; i < nmaps; i++) |
Lucas Stach | 6031e73 | 2016-12-07 17:36:36 +1100 | [diff] [blame] | 588 | cmap.bm_len += map[i].bm_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
| 590 | /* Check for IOs smaller than the sector size / not sector aligned */ |
Lucas Stach | 6031e73 | 2016-12-07 17:36:36 +1100 | [diff] [blame] | 591 | ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize)); |
| 592 | ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
Dave Chinner | 10616b8 | 2013-01-21 23:53:52 +1100 | [diff] [blame] | 594 | /* |
| 595 | * Corrupted block numbers can get through to here, unfortunately, so we |
| 596 | * have to check that the buffer falls within the filesystem bounds. |
| 597 | */ |
| 598 | eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks); |
Lucas Stach | 6031e73 | 2016-12-07 17:36:36 +1100 | [diff] [blame] | 599 | if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) { |
Dave Chinner | 10616b8 | 2013-01-21 23:53:52 +1100 | [diff] [blame] | 600 | xfs_alert(btp->bt_mount, |
Darrick J. Wong | c219b01 | 2018-01-08 11:39:18 -0800 | [diff] [blame] | 601 | "%s: daddr 0x%llx out of range, EOFS 0x%llx", |
Lucas Stach | 6031e73 | 2016-12-07 17:36:36 +1100 | [diff] [blame] | 602 | __func__, cmap.bm_bn, eofs); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 603 | WARN_ON(1); |
Dave Chinner | b027d4c | 2018-04-18 08:25:21 -0700 | [diff] [blame] | 604 | return -EFSCORRUPTED; |
Dave Chinner | 10616b8 | 2013-01-21 23:53:52 +1100 | [diff] [blame] | 605 | } |
| 606 | |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 607 | pag = xfs_perag_get(btp->bt_mount, |
Lucas Stach | 6031e73 | 2016-12-07 17:36:36 +1100 | [diff] [blame] | 608 | xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 610 | spin_lock(&pag->pag_buf_lock); |
Lucas Stach | 6031e73 | 2016-12-07 17:36:36 +1100 | [diff] [blame] | 611 | bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap, |
| 612 | xfs_buf_hash_params); |
| 613 | if (bp) { |
| 614 | atomic_inc(&bp->b_hold); |
| 615 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /* No match found */ |
Dave Chinner | b027d4c | 2018-04-18 08:25:21 -0700 | [diff] [blame] | 619 | if (!new_bp) { |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 620 | XFS_STATS_INC(btp->bt_mount, xb_miss_locked); |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 621 | spin_unlock(&pag->pag_buf_lock); |
| 622 | xfs_perag_put(pag); |
Dave Chinner | b027d4c | 2018-04-18 08:25:21 -0700 | [diff] [blame] | 623 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | } |
Dave Chinner | b027d4c | 2018-04-18 08:25:21 -0700 | [diff] [blame] | 625 | |
| 626 | /* the buffer keeps the perag reference until it is freed */ |
| 627 | new_bp->b_pag = pag; |
| 628 | rhashtable_insert_fast(&pag->pag_buf_hash, &new_bp->b_rhash_head, |
| 629 | xfs_buf_hash_params); |
| 630 | spin_unlock(&pag->pag_buf_lock); |
| 631 | *found_bp = new_bp; |
| 632 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
| 634 | found: |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 635 | spin_unlock(&pag->pag_buf_lock); |
| 636 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 638 | if (!xfs_buf_trylock(bp)) { |
| 639 | if (flags & XBF_TRYLOCK) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 640 | xfs_buf_rele(bp); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 641 | XFS_STATS_INC(btp->bt_mount, xb_busy_locked); |
Dave Chinner | b027d4c | 2018-04-18 08:25:21 -0700 | [diff] [blame] | 642 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 644 | xfs_buf_lock(bp); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 645 | XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 648 | /* |
| 649 | * if the buffer is stale, clear all the external state associated with |
| 650 | * it. We need to keep flags such as how we allocated the buffer memory |
| 651 | * intact here. |
| 652 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 653 | if (bp->b_flags & XBF_STALE) { |
| 654 | ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0); |
Dave Chinner | cfb0285 | 2012-11-12 22:54:19 +1100 | [diff] [blame] | 655 | ASSERT(bp->b_iodone == NULL); |
Dave Chinner | 611c994 | 2012-04-23 15:59:07 +1000 | [diff] [blame] | 656 | bp->b_flags &= _XBF_KMEM | _XBF_PAGES; |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 657 | bp->b_ops = NULL; |
David Chinner | 2f92658 | 2005-09-05 08:33:35 +1000 | [diff] [blame] | 658 | } |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 659 | |
| 660 | trace_xfs_buf_find(bp, flags, _RET_IP_); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 661 | XFS_STATS_INC(btp->bt_mount, xb_get_locked); |
Dave Chinner | b027d4c | 2018-04-18 08:25:21 -0700 | [diff] [blame] | 662 | *found_bp = bp; |
| 663 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Dave Chinner | 8925a3d | 2018-04-18 08:25:20 -0700 | [diff] [blame] | 666 | struct xfs_buf * |
| 667 | xfs_buf_incore( |
| 668 | struct xfs_buftarg *target, |
| 669 | xfs_daddr_t blkno, |
| 670 | size_t numblks, |
| 671 | xfs_buf_flags_t flags) |
| 672 | { |
Dave Chinner | b027d4c | 2018-04-18 08:25:21 -0700 | [diff] [blame] | 673 | struct xfs_buf *bp; |
| 674 | int error; |
Dave Chinner | 8925a3d | 2018-04-18 08:25:20 -0700 | [diff] [blame] | 675 | DEFINE_SINGLE_BUF_MAP(map, blkno, numblks); |
Dave Chinner | b027d4c | 2018-04-18 08:25:21 -0700 | [diff] [blame] | 676 | |
| 677 | error = xfs_buf_find(target, &map, 1, flags, NULL, &bp); |
| 678 | if (error) |
| 679 | return NULL; |
| 680 | return bp; |
Dave Chinner | 8925a3d | 2018-04-18 08:25:20 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | /* |
Dave Chinner | 3815832 | 2011-09-30 04:45:02 +0000 | [diff] [blame] | 684 | * Assembles a buffer covering the specified range. The code is optimised for |
| 685 | * cache hits, as metadata intensive workloads will see 3 orders of magnitude |
| 686 | * more hits than misses. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | */ |
Darrick J. Wong | 3848b5f | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 688 | int |
Dave Chinner | 6dde270 | 2012-06-22 18:50:10 +1000 | [diff] [blame] | 689 | xfs_buf_get_map( |
| 690 | struct xfs_buftarg *target, |
| 691 | struct xfs_buf_map *map, |
| 692 | int nmaps, |
Darrick J. Wong | 3848b5f | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 693 | xfs_buf_flags_t flags, |
| 694 | struct xfs_buf **bpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | { |
Dave Chinner | 3815832 | 2011-09-30 04:45:02 +0000 | [diff] [blame] | 696 | struct xfs_buf *bp; |
| 697 | struct xfs_buf *new_bp; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 698 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Darrick J. Wong | 3848b5f | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 700 | *bpp = NULL; |
Dave Chinner | b027d4c | 2018-04-18 08:25:21 -0700 | [diff] [blame] | 701 | error = xfs_buf_find(target, map, nmaps, flags, NULL, &bp); |
Darrick J. Wong | 3848b5f | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 702 | if (!error) |
Dave Chinner | 3815832 | 2011-09-30 04:45:02 +0000 | [diff] [blame] | 703 | goto found; |
Darrick J. Wong | 3848b5f | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 704 | if (error != -ENOENT) |
| 705 | return error; |
Dave Chinner | 3815832 | 2011-09-30 04:45:02 +0000 | [diff] [blame] | 706 | |
Darrick J. Wong | 32dff5e | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 707 | error = _xfs_buf_alloc(target, map, nmaps, flags, &new_bp); |
| 708 | if (error) |
Darrick J. Wong | 3848b5f | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 709 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
Dave Chinner | fe2429b | 2012-04-23 15:58:45 +1000 | [diff] [blame] | 711 | error = xfs_buf_allocate_memory(new_bp, flags); |
| 712 | if (error) { |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 713 | xfs_buf_free(new_bp); |
Darrick J. Wong | 3848b5f | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 714 | return error; |
Dave Chinner | 3815832 | 2011-09-30 04:45:02 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Dave Chinner | b027d4c | 2018-04-18 08:25:21 -0700 | [diff] [blame] | 717 | error = xfs_buf_find(target, map, nmaps, flags, new_bp, &bp); |
| 718 | if (error) { |
Dave Chinner | fe2429b | 2012-04-23 15:58:45 +1000 | [diff] [blame] | 719 | xfs_buf_free(new_bp); |
Darrick J. Wong | 3848b5f | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 720 | return error; |
Dave Chinner | fe2429b | 2012-04-23 15:58:45 +1000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | if (bp != new_bp) |
| 724 | xfs_buf_free(new_bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | |
Dave Chinner | 3815832 | 2011-09-30 04:45:02 +0000 | [diff] [blame] | 726 | found: |
Dave Chinner | 611c994 | 2012-04-23 15:59:07 +1000 | [diff] [blame] | 727 | if (!bp->b_addr) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 728 | error = _xfs_buf_map_pages(bp, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | if (unlikely(error)) { |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 730 | xfs_warn(target->bt_mount, |
Eric Sandeen | 08e96e1 | 2013-10-11 20:59:05 -0500 | [diff] [blame] | 731 | "%s: failed to map pagesn", __func__); |
Dave Chinner | a8acad7 | 2012-04-23 15:58:54 +1000 | [diff] [blame] | 732 | xfs_buf_relse(bp); |
Darrick J. Wong | 3848b5f | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 733 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
Dave Chinner | b79f4a1 | 2016-01-12 07:03:44 +1100 | [diff] [blame] | 737 | /* |
| 738 | * Clear b_error if this is a lookup from a caller that doesn't expect |
| 739 | * valid data to be found in the buffer. |
| 740 | */ |
| 741 | if (!(flags & XBF_READ)) |
| 742 | xfs_buf_ioerror(bp, 0); |
| 743 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 744 | XFS_STATS_INC(target->bt_mount, xb_get); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 745 | trace_xfs_buf_get(bp, flags, _RET_IP_); |
Darrick J. Wong | 3848b5f | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 746 | *bpp = bp; |
| 747 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Christoph Hellwig | 5d765b9 | 2008-12-03 12:20:26 +0100 | [diff] [blame] | 750 | STATIC int |
| 751 | _xfs_buf_read( |
| 752 | xfs_buf_t *bp, |
| 753 | xfs_buf_flags_t flags) |
| 754 | { |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 755 | ASSERT(!(flags & XBF_WRITE)); |
Mark Tinguely | f4b4242 | 2012-12-04 17:18:02 -0600 | [diff] [blame] | 756 | ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL); |
Christoph Hellwig | 5d765b9 | 2008-12-03 12:20:26 +0100 | [diff] [blame] | 757 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 758 | bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD); |
Christoph Hellwig | 1d5ae5d | 2011-07-08 14:36:32 +0200 | [diff] [blame] | 759 | bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD); |
Christoph Hellwig | 5d765b9 | 2008-12-03 12:20:26 +0100 | [diff] [blame] | 760 | |
Brian Foster | 6af88cd | 2018-07-11 22:26:35 -0700 | [diff] [blame] | 761 | return xfs_buf_submit(bp); |
Christoph Hellwig | 5d765b9 | 2008-12-03 12:20:26 +0100 | [diff] [blame] | 762 | } |
| 763 | |
Darrick J. Wong | 1aff569 | 2018-10-18 17:20:30 +1100 | [diff] [blame] | 764 | /* |
Brian Foster | 75d0230 | 2019-02-06 09:25:29 -0800 | [diff] [blame] | 765 | * Reverify a buffer found in cache without an attached ->b_ops. |
Darrick J. Wong | add46b3 | 2019-02-03 14:03:59 -0800 | [diff] [blame] | 766 | * |
Brian Foster | 75d0230 | 2019-02-06 09:25:29 -0800 | [diff] [blame] | 767 | * If the caller passed an ops structure and the buffer doesn't have ops |
| 768 | * assigned, set the ops and use it to verify the contents. If verification |
| 769 | * fails, clear XBF_DONE. We assume the buffer has no recorded errors and is |
| 770 | * already in XBF_DONE state on entry. |
Darrick J. Wong | add46b3 | 2019-02-03 14:03:59 -0800 | [diff] [blame] | 771 | * |
Brian Foster | 75d0230 | 2019-02-06 09:25:29 -0800 | [diff] [blame] | 772 | * Under normal operations, every in-core buffer is verified on read I/O |
| 773 | * completion. There are two scenarios that can lead to in-core buffers without |
| 774 | * an assigned ->b_ops. The first is during log recovery of buffers on a V4 |
| 775 | * filesystem, though these buffers are purged at the end of recovery. The |
| 776 | * other is online repair, which intentionally reads with a NULL buffer ops to |
| 777 | * run several verifiers across an in-core buffer in order to establish buffer |
| 778 | * type. If repair can't establish that, the buffer will be left in memory |
| 779 | * with NULL buffer ops. |
Darrick J. Wong | 1aff569 | 2018-10-18 17:20:30 +1100 | [diff] [blame] | 780 | */ |
| 781 | int |
Brian Foster | 75d0230 | 2019-02-06 09:25:29 -0800 | [diff] [blame] | 782 | xfs_buf_reverify( |
Darrick J. Wong | 1aff569 | 2018-10-18 17:20:30 +1100 | [diff] [blame] | 783 | struct xfs_buf *bp, |
| 784 | const struct xfs_buf_ops *ops) |
| 785 | { |
| 786 | ASSERT(bp->b_flags & XBF_DONE); |
| 787 | ASSERT(bp->b_error == 0); |
| 788 | |
| 789 | if (!ops || bp->b_ops) |
| 790 | return 0; |
| 791 | |
| 792 | bp->b_ops = ops; |
| 793 | bp->b_ops->verify_read(bp); |
| 794 | if (bp->b_error) |
| 795 | bp->b_flags &= ~XBF_DONE; |
| 796 | return bp->b_error; |
| 797 | } |
| 798 | |
Darrick J. Wong | 4ed8e27 | 2020-01-23 17:01:16 -0800 | [diff] [blame^] | 799 | int |
Dave Chinner | 6dde270 | 2012-06-22 18:50:10 +1000 | [diff] [blame] | 800 | xfs_buf_read_map( |
| 801 | struct xfs_buftarg *target, |
| 802 | struct xfs_buf_map *map, |
| 803 | int nmaps, |
Dave Chinner | c3f8fc7 | 2012-11-12 22:54:01 +1100 | [diff] [blame] | 804 | xfs_buf_flags_t flags, |
Darrick J. Wong | 4ed8e27 | 2020-01-23 17:01:16 -0800 | [diff] [blame^] | 805 | struct xfs_buf **bpp, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 806 | const struct xfs_buf_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | { |
Dave Chinner | 6dde270 | 2012-06-22 18:50:10 +1000 | [diff] [blame] | 808 | struct xfs_buf *bp; |
Darrick J. Wong | 3848b5f | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 809 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 811 | flags |= XBF_READ; |
Darrick J. Wong | 4ed8e27 | 2020-01-23 17:01:16 -0800 | [diff] [blame^] | 812 | *bpp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
Darrick J. Wong | 3848b5f | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 814 | error = xfs_buf_get_map(target, map, nmaps, flags, &bp); |
| 815 | if (error) |
Darrick J. Wong | 4ed8e27 | 2020-01-23 17:01:16 -0800 | [diff] [blame^] | 816 | return error; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 817 | |
Darrick J. Wong | 1aff569 | 2018-10-18 17:20:30 +1100 | [diff] [blame] | 818 | trace_xfs_buf_read(bp, flags, _RET_IP_); |
| 819 | |
| 820 | if (!(bp->b_flags & XBF_DONE)) { |
Darrick J. Wong | 4ed8e27 | 2020-01-23 17:01:16 -0800 | [diff] [blame^] | 821 | /* Initiate the buffer read and wait. */ |
Darrick J. Wong | 1aff569 | 2018-10-18 17:20:30 +1100 | [diff] [blame] | 822 | XFS_STATS_INC(target->bt_mount, xb_get_read); |
| 823 | bp->b_ops = ops; |
Darrick J. Wong | 4ed8e27 | 2020-01-23 17:01:16 -0800 | [diff] [blame^] | 824 | error = _xfs_buf_read(bp, flags); |
| 825 | |
| 826 | /* Readahead iodone already dropped the buffer, so exit. */ |
| 827 | if (flags & XBF_ASYNC) |
| 828 | return 0; |
| 829 | } else { |
| 830 | /* Buffer already read; all we need to do is check it. */ |
| 831 | error = xfs_buf_reverify(bp, ops); |
| 832 | |
| 833 | /* Readahead already finished; drop the buffer and exit. */ |
| 834 | if (flags & XBF_ASYNC) { |
| 835 | xfs_buf_relse(bp); |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | /* We do not want read in the flags */ |
| 840 | bp->b_flags &= ~XBF_READ; |
| 841 | ASSERT(bp->b_ops != NULL || ops == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Darrick J. Wong | 4ed8e27 | 2020-01-23 17:01:16 -0800 | [diff] [blame^] | 844 | /* |
| 845 | * If we've had a read error, then the contents of the buffer are |
| 846 | * invalid and should not be used. To ensure that a followup read tries |
| 847 | * to pull the buffer from disk again, we clear the XBF_DONE flag and |
| 848 | * mark the buffer stale. This ensures that anyone who has a current |
| 849 | * reference to the buffer will interpret it's contents correctly and |
| 850 | * future cache lookups will also treat it as an empty, uninitialised |
| 851 | * buffer. |
| 852 | */ |
| 853 | if (error) { |
| 854 | if (!XFS_FORCED_SHUTDOWN(target->bt_mount)) |
| 855 | xfs_buf_ioerror_alert(bp, __func__); |
Darrick J. Wong | 1aff569 | 2018-10-18 17:20:30 +1100 | [diff] [blame] | 856 | |
Darrick J. Wong | 4ed8e27 | 2020-01-23 17:01:16 -0800 | [diff] [blame^] | 857 | bp->b_flags &= ~XBF_DONE; |
| 858 | xfs_buf_stale(bp); |
Darrick J. Wong | 1aff569 | 2018-10-18 17:20:30 +1100 | [diff] [blame] | 859 | xfs_buf_relse(bp); |
Darrick J. Wong | 4ed8e27 | 2020-01-23 17:01:16 -0800 | [diff] [blame^] | 860 | |
| 861 | /* bad CRC means corrupted metadata */ |
| 862 | if (error == -EFSBADCRC) |
| 863 | error = -EFSCORRUPTED; |
| 864 | return error; |
Darrick J. Wong | 1aff569 | 2018-10-18 17:20:30 +1100 | [diff] [blame] | 865 | } |
| 866 | |
Darrick J. Wong | 4ed8e27 | 2020-01-23 17:01:16 -0800 | [diff] [blame^] | 867 | *bpp = bp; |
| 868 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 872 | * If we are not low on memory then do the readahead in a deadlock |
| 873 | * safe manner. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | */ |
| 875 | void |
Dave Chinner | 6dde270 | 2012-06-22 18:50:10 +1000 | [diff] [blame] | 876 | xfs_buf_readahead_map( |
| 877 | struct xfs_buftarg *target, |
| 878 | struct xfs_buf_map *map, |
Dave Chinner | c3f8fc7 | 2012-11-12 22:54:01 +1100 | [diff] [blame] | 879 | int nmaps, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 880 | const struct xfs_buf_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | { |
Darrick J. Wong | 4ed8e27 | 2020-01-23 17:01:16 -0800 | [diff] [blame^] | 882 | struct xfs_buf *bp; |
| 883 | |
Jan Kara | efa7c9f | 2017-02-02 15:56:53 +0100 | [diff] [blame] | 884 | if (bdi_read_congested(target->bt_bdev->bd_bdi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | return; |
| 886 | |
Dave Chinner | 6dde270 | 2012-06-22 18:50:10 +1000 | [diff] [blame] | 887 | xfs_buf_read_map(target, map, nmaps, |
Darrick J. Wong | 4ed8e27 | 2020-01-23 17:01:16 -0800 | [diff] [blame^] | 888 | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD, &bp, ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Dave Chinner | 5adc94c | 2010-09-24 21:58:31 +1000 | [diff] [blame] | 891 | /* |
| 892 | * Read an uncached buffer from disk. Allocates and returns a locked |
| 893 | * buffer containing the disk contents or nothing. |
| 894 | */ |
Dave Chinner | ba372674 | 2014-10-02 09:05:32 +1000 | [diff] [blame] | 895 | int |
Dave Chinner | 5adc94c | 2010-09-24 21:58:31 +1000 | [diff] [blame] | 896 | xfs_buf_read_uncached( |
Dave Chinner | 5adc94c | 2010-09-24 21:58:31 +1000 | [diff] [blame] | 897 | struct xfs_buftarg *target, |
| 898 | xfs_daddr_t daddr, |
Dave Chinner | e70b73f | 2012-04-23 15:58:49 +1000 | [diff] [blame] | 899 | size_t numblks, |
Dave Chinner | c3f8fc7 | 2012-11-12 22:54:01 +1100 | [diff] [blame] | 900 | int flags, |
Dave Chinner | ba372674 | 2014-10-02 09:05:32 +1000 | [diff] [blame] | 901 | struct xfs_buf **bpp, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 902 | const struct xfs_buf_ops *ops) |
Dave Chinner | 5adc94c | 2010-09-24 21:58:31 +1000 | [diff] [blame] | 903 | { |
Dave Chinner | eab4e63 | 2012-11-12 22:54:02 +1100 | [diff] [blame] | 904 | struct xfs_buf *bp; |
Dave Chinner | 5adc94c | 2010-09-24 21:58:31 +1000 | [diff] [blame] | 905 | |
Dave Chinner | ba372674 | 2014-10-02 09:05:32 +1000 | [diff] [blame] | 906 | *bpp = NULL; |
| 907 | |
Dave Chinner | e70b73f | 2012-04-23 15:58:49 +1000 | [diff] [blame] | 908 | bp = xfs_buf_get_uncached(target, numblks, flags); |
Dave Chinner | 5adc94c | 2010-09-24 21:58:31 +1000 | [diff] [blame] | 909 | if (!bp) |
Dave Chinner | ba372674 | 2014-10-02 09:05:32 +1000 | [diff] [blame] | 910 | return -ENOMEM; |
Dave Chinner | 5adc94c | 2010-09-24 21:58:31 +1000 | [diff] [blame] | 911 | |
| 912 | /* set up the buffer for a read IO */ |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 913 | ASSERT(bp->b_map_count == 1); |
Dave Chinner | ba372674 | 2014-10-02 09:05:32 +1000 | [diff] [blame] | 914 | bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */ |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 915 | bp->b_maps[0].bm_bn = daddr; |
Dave Chinner | cbb7baa | 2012-06-22 18:50:08 +1000 | [diff] [blame] | 916 | bp->b_flags |= XBF_READ; |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 917 | bp->b_ops = ops; |
Dave Chinner | 5adc94c | 2010-09-24 21:58:31 +1000 | [diff] [blame] | 918 | |
Brian Foster | 6af88cd | 2018-07-11 22:26:35 -0700 | [diff] [blame] | 919 | xfs_buf_submit(bp); |
Dave Chinner | ba372674 | 2014-10-02 09:05:32 +1000 | [diff] [blame] | 920 | if (bp->b_error) { |
| 921 | int error = bp->b_error; |
Christoph Hellwig | 83a0adc | 2013-12-17 00:03:52 -0800 | [diff] [blame] | 922 | xfs_buf_relse(bp); |
Dave Chinner | ba372674 | 2014-10-02 09:05:32 +1000 | [diff] [blame] | 923 | return error; |
Christoph Hellwig | 83a0adc | 2013-12-17 00:03:52 -0800 | [diff] [blame] | 924 | } |
Dave Chinner | ba372674 | 2014-10-02 09:05:32 +1000 | [diff] [blame] | 925 | |
| 926 | *bpp = bp; |
| 927 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | xfs_buf_t * |
Dave Chinner | 686865f | 2010-09-24 20:07:47 +1000 | [diff] [blame] | 931 | xfs_buf_get_uncached( |
| 932 | struct xfs_buftarg *target, |
Dave Chinner | e70b73f | 2012-04-23 15:58:49 +1000 | [diff] [blame] | 933 | size_t numblks, |
Dave Chinner | 686865f | 2010-09-24 20:07:47 +1000 | [diff] [blame] | 934 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | { |
Dave Chinner | e70b73f | 2012-04-23 15:58:49 +1000 | [diff] [blame] | 936 | unsigned long page_count; |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 937 | int error, i; |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 938 | struct xfs_buf *bp; |
| 939 | DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
Brian Foster | c891c30 | 2016-07-20 11:13:43 +1000 | [diff] [blame] | 941 | /* flags might contain irrelevant bits, pass only what we care about */ |
Darrick J. Wong | 32dff5e | 2020-01-23 17:01:15 -0800 | [diff] [blame] | 942 | error = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT, &bp); |
| 943 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
Dave Chinner | e70b73f | 2012-04-23 15:58:49 +1000 | [diff] [blame] | 946 | page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT; |
Eric Sandeen | 87937bf | 2014-04-14 19:01:20 +1000 | [diff] [blame] | 947 | error = _xfs_buf_get_pages(bp, page_count); |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 948 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | goto fail_free_buf; |
| 950 | |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 951 | for (i = 0; i < page_count; i++) { |
Dave Chinner | 686865f | 2010-09-24 20:07:47 +1000 | [diff] [blame] | 952 | bp->b_pages[i] = alloc_page(xb_to_gfp(flags)); |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 953 | if (!bp->b_pages[i]) |
| 954 | goto fail_free_mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | } |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 956 | bp->b_flags |= _XBF_PAGES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
Dave Chinner | 611c994 | 2012-04-23 15:59:07 +1000 | [diff] [blame] | 958 | error = _xfs_buf_map_pages(bp, 0); |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 959 | if (unlikely(error)) { |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 960 | xfs_warn(target->bt_mount, |
Eric Sandeen | 08e96e1 | 2013-10-11 20:59:05 -0500 | [diff] [blame] | 961 | "%s: failed to map pages", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | goto fail_free_mem; |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
Dave Chinner | 686865f | 2010-09-24 20:07:47 +1000 | [diff] [blame] | 965 | trace_xfs_buf_get_uncached(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | return bp; |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | fail_free_mem: |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 969 | while (--i >= 0) |
| 970 | __free_page(bp->b_pages[i]); |
Christoph Hellwig | ca165b8 | 2007-05-24 15:21:11 +1000 | [diff] [blame] | 971 | _xfs_buf_free_pages(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | fail_free_buf: |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 973 | xfs_buf_free_maps(bp); |
Carlos Maiolino | 377bcd5 | 2019-11-14 12:43:04 -0800 | [diff] [blame] | 974 | kmem_cache_free(xfs_buf_zone, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | fail: |
| 976 | return NULL; |
| 977 | } |
| 978 | |
| 979 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | * Increment reference count on buffer, to hold the buffer concurrently |
| 981 | * with another thread which may release (free) the buffer asynchronously. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | * Must hold the buffer already to call this function. |
| 983 | */ |
| 984 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 985 | xfs_buf_hold( |
| 986 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 988 | trace_xfs_buf_hold(bp, _RET_IP_); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 989 | atomic_inc(&bp->b_hold); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /* |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 993 | * Release a hold on the specified buffer. If the hold count is 1, the buffer is |
| 994 | * placed on LRU or freed (depending on b_lru_ref). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | */ |
| 996 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 997 | xfs_buf_rele( |
| 998 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | { |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 1000 | struct xfs_perag *pag = bp->b_pag; |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 1001 | bool release; |
| 1002 | bool freebuf = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1004 | trace_xfs_buf_rele(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 1006 | if (!pag) { |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1007 | ASSERT(list_empty(&bp->b_lru)); |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 1008 | if (atomic_dec_and_test(&bp->b_hold)) { |
| 1009 | xfs_buf_ioacct_dec(bp); |
Nathan Scott | fad3aa1 | 2006-02-01 12:14:52 +1100 | [diff] [blame] | 1010 | xfs_buf_free(bp); |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 1011 | } |
Nathan Scott | fad3aa1 | 2006-02-01 12:14:52 +1100 | [diff] [blame] | 1012 | return; |
| 1013 | } |
| 1014 | |
Lachlan McIlroy | 3790689 | 2008-08-13 15:42:10 +1000 | [diff] [blame] | 1015 | ASSERT(atomic_read(&bp->b_hold) > 0); |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1016 | |
Dave Chinner | 37fd167 | 2018-10-18 17:21:29 +1100 | [diff] [blame] | 1017 | /* |
| 1018 | * We grab the b_lock here first to serialise racing xfs_buf_rele() |
| 1019 | * calls. The pag_buf_lock being taken on the last reference only |
| 1020 | * serialises against racing lookups in xfs_buf_find(). IOWs, the second |
| 1021 | * to last reference we drop here is not serialised against the last |
| 1022 | * reference until we take bp->b_lock. Hence if we don't grab b_lock |
| 1023 | * first, the last "release" reference can win the race to the lock and |
| 1024 | * free the buffer before the second-to-last reference is processed, |
| 1025 | * leading to a use-after-free scenario. |
| 1026 | */ |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 1027 | spin_lock(&bp->b_lock); |
Dave Chinner | 37fd167 | 2018-10-18 17:21:29 +1100 | [diff] [blame] | 1028 | release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock); |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 1029 | if (!release) { |
| 1030 | /* |
| 1031 | * Drop the in-flight state if the buffer is already on the LRU |
| 1032 | * and it holds the only reference. This is racy because we |
| 1033 | * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT |
| 1034 | * ensures the decrement occurs only once per-buf. |
| 1035 | */ |
| 1036 | if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru)) |
Brian Foster | 63db7c8 | 2017-05-31 08:22:52 -0700 | [diff] [blame] | 1037 | __xfs_buf_ioacct_dec(bp); |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 1038 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | } |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 1040 | |
| 1041 | /* the last reference has been dropped ... */ |
Brian Foster | 63db7c8 | 2017-05-31 08:22:52 -0700 | [diff] [blame] | 1042 | __xfs_buf_ioacct_dec(bp); |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 1043 | if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) { |
| 1044 | /* |
| 1045 | * If the buffer is added to the LRU take a new reference to the |
| 1046 | * buffer for the LRU and clear the (now stale) dispose list |
| 1047 | * state flag |
| 1048 | */ |
| 1049 | if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) { |
| 1050 | bp->b_state &= ~XFS_BSTATE_DISPOSE; |
| 1051 | atomic_inc(&bp->b_hold); |
| 1052 | } |
| 1053 | spin_unlock(&pag->pag_buf_lock); |
| 1054 | } else { |
| 1055 | /* |
| 1056 | * most of the time buffers will already be removed from the |
| 1057 | * LRU, so optimise that case by checking for the |
| 1058 | * XFS_BSTATE_DISPOSE flag indicating the last list the buffer |
| 1059 | * was on was the disposal list |
| 1060 | */ |
| 1061 | if (!(bp->b_state & XFS_BSTATE_DISPOSE)) { |
| 1062 | list_lru_del(&bp->b_target->bt_lru, &bp->b_lru); |
| 1063 | } else { |
| 1064 | ASSERT(list_empty(&bp->b_lru)); |
| 1065 | } |
| 1066 | |
| 1067 | ASSERT(!(bp->b_flags & _XBF_DELWRI_Q)); |
Lucas Stach | 6031e73 | 2016-12-07 17:36:36 +1100 | [diff] [blame] | 1068 | rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head, |
| 1069 | xfs_buf_hash_params); |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 1070 | spin_unlock(&pag->pag_buf_lock); |
| 1071 | xfs_perag_put(pag); |
| 1072 | freebuf = true; |
| 1073 | } |
| 1074 | |
| 1075 | out_unlock: |
| 1076 | spin_unlock(&bp->b_lock); |
| 1077 | |
| 1078 | if (freebuf) |
| 1079 | xfs_buf_free(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | /* |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 1084 | * Lock a buffer object, if it is not already locked. |
Dave Chinner | 90810b9 | 2010-11-30 15:16:16 +1100 | [diff] [blame] | 1085 | * |
| 1086 | * If we come across a stale, pinned, locked buffer, we know that we are |
| 1087 | * being asked to lock a buffer that has been reallocated. Because it is |
| 1088 | * pinned, we know that the log has not been pushed to disk and hence it |
| 1089 | * will still be locked. Rather than continuing to have trylock attempts |
| 1090 | * fail until someone else pushes the log, push it ourselves before |
| 1091 | * returning. This means that the xfsaild will not get stuck trying |
| 1092 | * to push on stale inode buffers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | */ |
| 1094 | int |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 1095 | xfs_buf_trylock( |
| 1096 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | { |
| 1098 | int locked; |
| 1099 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1100 | locked = down_trylock(&bp->b_sema) == 0; |
Eric Sandeen | fa6c668 | 2018-08-10 13:56:25 -0700 | [diff] [blame] | 1101 | if (locked) |
Darrick J. Wong | 479c641 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 1102 | trace_xfs_buf_trylock(bp, _RET_IP_); |
Eric Sandeen | fa6c668 | 2018-08-10 13:56:25 -0700 | [diff] [blame] | 1103 | else |
Darrick J. Wong | 479c641 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 1104 | trace_xfs_buf_trylock_fail(bp, _RET_IP_); |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 1105 | return locked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | |
| 1108 | /* |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 1109 | * Lock a buffer object. |
Dave Chinner | ed3b4d6 | 2010-05-21 12:07:08 +1000 | [diff] [blame] | 1110 | * |
| 1111 | * If we come across a stale, pinned, locked buffer, we know that we |
| 1112 | * are being asked to lock a buffer that has been reallocated. Because |
| 1113 | * it is pinned, we know that the log has not been pushed to disk and |
| 1114 | * hence it will still be locked. Rather than sleeping until someone |
| 1115 | * else pushes the log, push it ourselves before trying to get the lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1117 | void |
| 1118 | xfs_buf_lock( |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 1119 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1121 | trace_xfs_buf_lock(bp, _RET_IP_); |
| 1122 | |
Dave Chinner | ed3b4d6 | 2010-05-21 12:07:08 +1000 | [diff] [blame] | 1123 | if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE)) |
Christoph Hellwig | dbd329f1 | 2019-06-28 19:27:29 -0700 | [diff] [blame] | 1124 | xfs_log_force(bp->b_mount, 0); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1125 | down(&bp->b_sema); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1126 | |
| 1127 | trace_xfs_buf_lock_done(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1131 | xfs_buf_unlock( |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 1132 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | { |
Brian Foster | 20e8a06 | 2017-04-21 12:40:44 -0700 | [diff] [blame] | 1134 | ASSERT(xfs_buf_islocked(bp)); |
| 1135 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1136 | up(&bp->b_sema); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1137 | trace_xfs_buf_unlock(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1140 | STATIC void |
| 1141 | xfs_buf_wait_unpin( |
| 1142 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | { |
| 1144 | DECLARE_WAITQUEUE (wait, current); |
| 1145 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1146 | if (atomic_read(&bp->b_pin_count) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | return; |
| 1148 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1149 | add_wait_queue(&bp->b_waiters, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | for (;;) { |
| 1151 | set_current_state(TASK_UNINTERRUPTIBLE); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1152 | if (atomic_read(&bp->b_pin_count) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | break; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 1154 | io_schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1156 | remove_wait_queue(&bp->b_waiters, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | set_current_state(TASK_RUNNING); |
| 1158 | } |
| 1159 | |
| 1160 | /* |
| 1161 | * Buffer Utility Routines |
| 1162 | */ |
| 1163 | |
Dave Chinner | e8aaba9 | 2014-10-02 09:04:22 +1000 | [diff] [blame] | 1164 | void |
| 1165 | xfs_buf_ioend( |
| 1166 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | { |
Dave Chinner | e8aaba9 | 2014-10-02 09:04:22 +1000 | [diff] [blame] | 1168 | bool read = bp->b_flags & XBF_READ; |
| 1169 | |
| 1170 | trace_xfs_buf_iodone(bp, _RET_IP_); |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 1171 | |
| 1172 | bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD); |
Dave Chinner | d5929de | 2013-02-27 13:25:54 +1100 | [diff] [blame] | 1173 | |
Dave Chinner | 61be9c5 | 2014-10-02 09:04:31 +1000 | [diff] [blame] | 1174 | /* |
| 1175 | * Pull in IO completion errors now. We are guaranteed to be running |
| 1176 | * single threaded, so we don't need the lock to read b_io_error. |
| 1177 | */ |
| 1178 | if (!bp->b_error && bp->b_io_error) |
| 1179 | xfs_buf_ioerror(bp, bp->b_io_error); |
| 1180 | |
Dave Chinner | e8aaba9 | 2014-10-02 09:04:22 +1000 | [diff] [blame] | 1181 | /* Only validate buffers that were read without errors */ |
| 1182 | if (read && !bp->b_error && bp->b_ops) { |
| 1183 | ASSERT(!bp->b_iodone); |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 1184 | bp->b_ops->verify_read(bp); |
Dave Chinner | e8aaba9 | 2014-10-02 09:04:22 +1000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | if (!bp->b_error) |
| 1188 | bp->b_flags |= XBF_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | |
Christoph Hellwig | 80f6c29 | 2010-08-18 05:29:11 -0400 | [diff] [blame] | 1190 | if (bp->b_iodone) |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1191 | (*(bp->b_iodone))(bp); |
| 1192 | else if (bp->b_flags & XBF_ASYNC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | xfs_buf_relse(bp); |
Dave Chinner | 595bff7 | 2014-10-02 09:05:14 +1000 | [diff] [blame] | 1194 | else |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 1195 | complete(&bp->b_iowait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Dave Chinner | e8aaba9 | 2014-10-02 09:04:22 +1000 | [diff] [blame] | 1198 | static void |
| 1199 | xfs_buf_ioend_work( |
| 1200 | struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | { |
Dave Chinner | e8aaba9 | 2014-10-02 09:04:22 +1000 | [diff] [blame] | 1202 | struct xfs_buf *bp = |
Brian Foster | b29c70f | 2014-12-04 09:43:17 +1100 | [diff] [blame] | 1203 | container_of(work, xfs_buf_t, b_ioend_work); |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 1204 | |
Dave Chinner | e8aaba9 | 2014-10-02 09:04:22 +1000 | [diff] [blame] | 1205 | xfs_buf_ioend(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
Alexander Kuleshov | 211fe1a | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1208 | static void |
Dave Chinner | e8aaba9 | 2014-10-02 09:04:22 +1000 | [diff] [blame] | 1209 | xfs_buf_ioend_async( |
| 1210 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | { |
Brian Foster | b29c70f | 2014-12-04 09:43:17 +1100 | [diff] [blame] | 1212 | INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work); |
Christoph Hellwig | dbd329f1 | 2019-06-28 19:27:29 -0700 | [diff] [blame] | 1213 | queue_work(bp->b_mount->m_buf_workqueue, &bp->b_ioend_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | void |
Darrick J. Wong | 31ca03c | 2018-01-08 10:51:02 -0800 | [diff] [blame] | 1217 | __xfs_buf_ioerror( |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1218 | xfs_buf_t *bp, |
Darrick J. Wong | 31ca03c | 2018-01-08 10:51:02 -0800 | [diff] [blame] | 1219 | int error, |
| 1220 | xfs_failaddr_t failaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1222 | ASSERT(error <= 0 && error >= -1000); |
| 1223 | bp->b_error = error; |
Darrick J. Wong | 31ca03c | 2018-01-08 10:51:02 -0800 | [diff] [blame] | 1224 | trace_xfs_buf_ioerror(bp, error, failaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
Christoph Hellwig | 901796a | 2011-10-10 16:52:49 +0000 | [diff] [blame] | 1227 | void |
| 1228 | xfs_buf_ioerror_alert( |
| 1229 | struct xfs_buf *bp, |
| 1230 | const char *func) |
| 1231 | { |
Christoph Hellwig | dbd329f1 | 2019-06-28 19:27:29 -0700 | [diff] [blame] | 1232 | xfs_alert(bp->b_mount, |
Darrick J. Wong | c219b01 | 2018-01-08 11:39:18 -0800 | [diff] [blame] | 1233 | "metadata I/O error in \"%s\" at daddr 0x%llx len %d error %d", |
| 1234 | func, (uint64_t)XFS_BUF_ADDR(bp), bp->b_length, |
| 1235 | -bp->b_error); |
Christoph Hellwig | 901796a | 2011-10-10 16:52:49 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Christoph Hellwig | a2dcf5d | 2012-07-13 02:24:10 -0400 | [diff] [blame] | 1238 | int |
| 1239 | xfs_bwrite( |
| 1240 | struct xfs_buf *bp) |
| 1241 | { |
| 1242 | int error; |
| 1243 | |
| 1244 | ASSERT(xfs_buf_islocked(bp)); |
| 1245 | |
| 1246 | bp->b_flags |= XBF_WRITE; |
Dave Chinner | 2718775 | 2014-10-02 09:04:56 +1000 | [diff] [blame] | 1247 | bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q | |
| 1248 | XBF_WRITE_FAIL | XBF_DONE); |
Christoph Hellwig | a2dcf5d | 2012-07-13 02:24:10 -0400 | [diff] [blame] | 1249 | |
Brian Foster | 6af88cd | 2018-07-11 22:26:35 -0700 | [diff] [blame] | 1250 | error = xfs_buf_submit(bp); |
Christoph Hellwig | dbd329f1 | 2019-06-28 19:27:29 -0700 | [diff] [blame] | 1251 | if (error) |
| 1252 | xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR); |
Christoph Hellwig | a2dcf5d | 2012-07-13 02:24:10 -0400 | [diff] [blame] | 1253 | return error; |
| 1254 | } |
| 1255 | |
Brian Foster | 9bdd9bd | 2016-05-18 10:56:41 +1000 | [diff] [blame] | 1256 | static void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1257 | xfs_buf_bio_end_io( |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1258 | struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | { |
Brian Foster | 9bdd9bd | 2016-05-18 10:56:41 +1000 | [diff] [blame] | 1260 | struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | |
Dave Chinner | 37eb17e | 2012-11-12 22:09:46 +1100 | [diff] [blame] | 1262 | /* |
| 1263 | * don't overwrite existing errors - otherwise we can lose errors on |
| 1264 | * buffers that require multiple bios to complete. |
| 1265 | */ |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1266 | if (bio->bi_status) { |
| 1267 | int error = blk_status_to_errno(bio->bi_status); |
| 1268 | |
| 1269 | cmpxchg(&bp->b_io_error, 0, error); |
| 1270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | |
Dave Chinner | 37eb17e | 2012-11-12 22:09:46 +1100 | [diff] [blame] | 1272 | if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ)) |
James Bottomley | 73c77e2 | 2010-01-25 11:42:24 -0600 | [diff] [blame] | 1273 | invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp)); |
| 1274 | |
Dave Chinner | e8aaba9 | 2014-10-02 09:04:22 +1000 | [diff] [blame] | 1275 | if (atomic_dec_and_test(&bp->b_io_remaining) == 1) |
| 1276 | xfs_buf_ioend_async(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 1280 | static void |
| 1281 | xfs_buf_ioapply_map( |
| 1282 | struct xfs_buf *bp, |
| 1283 | int map, |
| 1284 | int *buf_offset, |
| 1285 | int *count, |
Christoph Hellwig | 2123ef8 | 2019-10-28 08:41:42 -0700 | [diff] [blame] | 1286 | int op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | { |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 1288 | int page_index; |
| 1289 | int total_nr_pages = bp->b_page_count; |
| 1290 | int nr_pages; |
| 1291 | struct bio *bio; |
| 1292 | sector_t sector = bp->b_maps[map].bm_bn; |
| 1293 | int size; |
| 1294 | int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 1296 | /* skip the pages in the buffer before the start offset */ |
| 1297 | page_index = 0; |
| 1298 | offset = *buf_offset; |
| 1299 | while (offset >= PAGE_SIZE) { |
| 1300 | page_index++; |
| 1301 | offset -= PAGE_SIZE; |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 1302 | } |
| 1303 | |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 1304 | /* |
| 1305 | * Limit the IO size to the length of the current vector, and update the |
| 1306 | * remaining IO count for the next time around. |
| 1307 | */ |
| 1308 | size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count); |
| 1309 | *count -= size; |
| 1310 | *buf_offset += size; |
Christoph Hellwig | 34951f5 | 2011-07-26 15:06:44 +0000 | [diff] [blame] | 1311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | next_chunk: |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1313 | atomic_inc(&bp->b_io_remaining); |
Ming Lei | c908e38 | 2016-05-30 21:34:33 +0800 | [diff] [blame] | 1314 | nr_pages = min(total_nr_pages, BIO_MAX_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | |
| 1316 | bio = bio_alloc(GFP_NOIO, nr_pages); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1317 | bio_set_dev(bio, bp->b_target->bt_bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1318 | bio->bi_iter.bi_sector = sector; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1319 | bio->bi_end_io = xfs_buf_bio_end_io; |
| 1320 | bio->bi_private = bp; |
Christoph Hellwig | 2123ef8 | 2019-10-28 08:41:42 -0700 | [diff] [blame] | 1321 | bio->bi_opf = op; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 1322 | |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 1323 | for (; size && nr_pages; nr_pages--, page_index++) { |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 1324 | int rbytes, nbytes = PAGE_SIZE - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
| 1326 | if (nbytes > size) |
| 1327 | nbytes = size; |
| 1328 | |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 1329 | rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes, |
| 1330 | offset); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1331 | if (rbytes < nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | break; |
| 1333 | |
| 1334 | offset = 0; |
Dave Chinner | aa0e883 | 2012-04-23 15:58:52 +1000 | [diff] [blame] | 1335 | sector += BTOBB(nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | size -= nbytes; |
| 1337 | total_nr_pages--; |
| 1338 | } |
| 1339 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1340 | if (likely(bio->bi_iter.bi_size)) { |
James Bottomley | 73c77e2 | 2010-01-25 11:42:24 -0600 | [diff] [blame] | 1341 | if (xfs_buf_is_vmapped(bp)) { |
| 1342 | flush_kernel_vmap_range(bp->b_addr, |
| 1343 | xfs_buf_vmap_len(bp)); |
| 1344 | } |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 1345 | submit_bio(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | if (size) |
| 1347 | goto next_chunk; |
| 1348 | } else { |
Dave Chinner | 37eb17e | 2012-11-12 22:09:46 +1100 | [diff] [blame] | 1349 | /* |
| 1350 | * This is guaranteed not to be the last io reference count |
Dave Chinner | 595bff7 | 2014-10-02 09:05:14 +1000 | [diff] [blame] | 1351 | * because the caller (xfs_buf_submit) holds a count itself. |
Dave Chinner | 37eb17e | 2012-11-12 22:09:46 +1100 | [diff] [blame] | 1352 | */ |
| 1353 | atomic_dec(&bp->b_io_remaining); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1354 | xfs_buf_ioerror(bp, -EIO); |
Dave Chinner | ec53d1d | 2010-07-20 17:52:59 +1000 | [diff] [blame] | 1355 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | } |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 1357 | |
| 1358 | } |
| 1359 | |
| 1360 | STATIC void |
| 1361 | _xfs_buf_ioapply( |
| 1362 | struct xfs_buf *bp) |
| 1363 | { |
| 1364 | struct blk_plug plug; |
Mike Christie | 50bfcd0 | 2016-06-05 14:31:57 -0500 | [diff] [blame] | 1365 | int op; |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 1366 | int offset; |
| 1367 | int size; |
| 1368 | int i; |
| 1369 | |
Dave Chinner | c163f9a | 2013-03-12 23:30:34 +1100 | [diff] [blame] | 1370 | /* |
| 1371 | * Make sure we capture only current IO errors rather than stale errors |
| 1372 | * left over from previous use of the buffer (e.g. failed readahead). |
| 1373 | */ |
| 1374 | bp->b_error = 0; |
| 1375 | |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 1376 | if (bp->b_flags & XBF_WRITE) { |
Mike Christie | 50bfcd0 | 2016-06-05 14:31:57 -0500 | [diff] [blame] | 1377 | op = REQ_OP_WRITE; |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 1378 | |
| 1379 | /* |
| 1380 | * Run the write verifier callback function if it exists. If |
| 1381 | * this function fails it will mark the buffer with an error and |
| 1382 | * the IO should not be dispatched. |
| 1383 | */ |
| 1384 | if (bp->b_ops) { |
| 1385 | bp->b_ops->verify_write(bp); |
| 1386 | if (bp->b_error) { |
Christoph Hellwig | dbd329f1 | 2019-06-28 19:27:29 -0700 | [diff] [blame] | 1387 | xfs_force_shutdown(bp->b_mount, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 1388 | SHUTDOWN_CORRUPT_INCORE); |
| 1389 | return; |
| 1390 | } |
Dave Chinner | 400b9d8 | 2014-08-04 12:42:40 +1000 | [diff] [blame] | 1391 | } else if (bp->b_bn != XFS_BUF_DADDR_NULL) { |
Christoph Hellwig | dbd329f1 | 2019-06-28 19:27:29 -0700 | [diff] [blame] | 1392 | struct xfs_mount *mp = bp->b_mount; |
Dave Chinner | 400b9d8 | 2014-08-04 12:42:40 +1000 | [diff] [blame] | 1393 | |
| 1394 | /* |
| 1395 | * non-crc filesystems don't attach verifiers during |
| 1396 | * log recovery, so don't warn for such filesystems. |
| 1397 | */ |
| 1398 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 1399 | xfs_warn(mp, |
Darrick J. Wong | c219b01 | 2018-01-08 11:39:18 -0800 | [diff] [blame] | 1400 | "%s: no buf ops on daddr 0x%llx len %d", |
Dave Chinner | 400b9d8 | 2014-08-04 12:42:40 +1000 | [diff] [blame] | 1401 | __func__, bp->b_bn, bp->b_length); |
Darrick J. Wong | 9c712a1 | 2018-01-08 10:51:26 -0800 | [diff] [blame] | 1402 | xfs_hex_dump(bp->b_addr, |
| 1403 | XFS_CORRUPTION_DUMP_LEN); |
Dave Chinner | 400b9d8 | 2014-08-04 12:42:40 +1000 | [diff] [blame] | 1404 | dump_stack(); |
| 1405 | } |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 1406 | } |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 1407 | } else { |
Mike Christie | 50bfcd0 | 2016-06-05 14:31:57 -0500 | [diff] [blame] | 1408 | op = REQ_OP_READ; |
Christoph Hellwig | 2123ef8 | 2019-10-28 08:41:42 -0700 | [diff] [blame] | 1409 | if (bp->b_flags & XBF_READ_AHEAD) |
| 1410 | op |= REQ_RAHEAD; |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | /* we only use the buffer cache for meta-data */ |
Christoph Hellwig | 2123ef8 | 2019-10-28 08:41:42 -0700 | [diff] [blame] | 1414 | op |= REQ_META; |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 1415 | |
| 1416 | /* |
| 1417 | * Walk all the vectors issuing IO on them. Set up the initial offset |
| 1418 | * into the buffer and the desired IO size before we start - |
| 1419 | * _xfs_buf_ioapply_vec() will modify them appropriately for each |
| 1420 | * subsequent call. |
| 1421 | */ |
| 1422 | offset = bp->b_offset; |
Christoph Hellwig | 8124b9b | 2019-06-28 19:27:28 -0700 | [diff] [blame] | 1423 | size = BBTOB(bp->b_length); |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 1424 | blk_start_plug(&plug); |
| 1425 | for (i = 0; i < bp->b_map_count; i++) { |
Christoph Hellwig | 2123ef8 | 2019-10-28 08:41:42 -0700 | [diff] [blame] | 1426 | xfs_buf_ioapply_map(bp, i, &offset, &size, op); |
Dave Chinner | 3e85c86 | 2012-06-22 18:50:09 +1000 | [diff] [blame] | 1427 | if (bp->b_error) |
| 1428 | break; |
| 1429 | if (size <= 0) |
| 1430 | break; /* all done */ |
| 1431 | } |
| 1432 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
Dave Chinner | 595bff7 | 2014-10-02 09:05:14 +1000 | [diff] [blame] | 1435 | /* |
Brian Foster | bb00b6f | 2018-07-11 22:26:35 -0700 | [diff] [blame] | 1436 | * Wait for I/O completion of a sync buffer and return the I/O error code. |
Dave Chinner | 595bff7 | 2014-10-02 09:05:14 +1000 | [diff] [blame] | 1437 | */ |
Brian Foster | eaebb51 | 2018-07-11 22:26:34 -0700 | [diff] [blame] | 1438 | static int |
Brian Foster | bb00b6f | 2018-07-11 22:26:35 -0700 | [diff] [blame] | 1439 | xfs_buf_iowait( |
Dave Chinner | 595bff7 | 2014-10-02 09:05:14 +1000 | [diff] [blame] | 1440 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | { |
Brian Foster | bb00b6f | 2018-07-11 22:26:35 -0700 | [diff] [blame] | 1442 | ASSERT(!(bp->b_flags & XBF_ASYNC)); |
| 1443 | |
| 1444 | trace_xfs_buf_iowait(bp, _RET_IP_); |
| 1445 | wait_for_completion(&bp->b_iowait); |
| 1446 | trace_xfs_buf_iowait_done(bp, _RET_IP_); |
| 1447 | |
| 1448 | return bp->b_error; |
| 1449 | } |
| 1450 | |
| 1451 | /* |
| 1452 | * Buffer I/O submission path, read or write. Asynchronous submission transfers |
| 1453 | * the buffer lock ownership and the current reference to the IO. It is not |
| 1454 | * safe to reference the buffer after a call to this function unless the caller |
| 1455 | * holds an additional reference itself. |
| 1456 | */ |
| 1457 | int |
| 1458 | __xfs_buf_submit( |
| 1459 | struct xfs_buf *bp, |
| 1460 | bool wait) |
| 1461 | { |
| 1462 | int error = 0; |
| 1463 | |
Dave Chinner | 595bff7 | 2014-10-02 09:05:14 +1000 | [diff] [blame] | 1464 | trace_xfs_buf_submit(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1466 | ASSERT(!(bp->b_flags & _XBF_DELWRI_Q)); |
Dave Chinner | 595bff7 | 2014-10-02 09:05:14 +1000 | [diff] [blame] | 1467 | |
| 1468 | /* on shutdown we stale and complete the buffer immediately */ |
Christoph Hellwig | dbd329f1 | 2019-06-28 19:27:29 -0700 | [diff] [blame] | 1469 | if (XFS_FORCED_SHUTDOWN(bp->b_mount)) { |
Dave Chinner | 595bff7 | 2014-10-02 09:05:14 +1000 | [diff] [blame] | 1470 | xfs_buf_ioerror(bp, -EIO); |
| 1471 | bp->b_flags &= ~XBF_DONE; |
| 1472 | xfs_buf_stale(bp); |
Brian Foster | 465fa17 | 2019-02-03 14:03:06 -0800 | [diff] [blame] | 1473 | xfs_buf_ioend(bp); |
Brian Foster | eaebb51 | 2018-07-11 22:26:34 -0700 | [diff] [blame] | 1474 | return -EIO; |
Dave Chinner | 595bff7 | 2014-10-02 09:05:14 +1000 | [diff] [blame] | 1475 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
Brian Foster | bb00b6f | 2018-07-11 22:26:35 -0700 | [diff] [blame] | 1477 | /* |
| 1478 | * Grab a reference so the buffer does not go away underneath us. For |
| 1479 | * async buffers, I/O completion drops the callers reference, which |
| 1480 | * could occur before submission returns. |
| 1481 | */ |
| 1482 | xfs_buf_hold(bp); |
| 1483 | |
Christoph Hellwig | 375ec69 | 2011-08-23 08:28:03 +0000 | [diff] [blame] | 1484 | if (bp->b_flags & XBF_WRITE) |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1485 | xfs_buf_wait_unpin(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | |
Dave Chinner | 61be9c5 | 2014-10-02 09:04:31 +1000 | [diff] [blame] | 1487 | /* clear the internal error state to avoid spurious errors */ |
| 1488 | bp->b_io_error = 0; |
| 1489 | |
Eric Sandeen | 8d6c121 | 2014-04-17 08:15:28 +1000 | [diff] [blame] | 1490 | /* |
Brian Foster | eaebb51 | 2018-07-11 22:26:34 -0700 | [diff] [blame] | 1491 | * Set the count to 1 initially, this will stop an I/O completion |
| 1492 | * callout which happens before we have started all the I/O from calling |
| 1493 | * xfs_buf_ioend too early. |
| 1494 | */ |
| 1495 | atomic_set(&bp->b_io_remaining, 1); |
| 1496 | if (bp->b_flags & XBF_ASYNC) |
| 1497 | xfs_buf_ioacct_inc(bp); |
| 1498 | _xfs_buf_ioapply(bp); |
| 1499 | |
| 1500 | /* |
| 1501 | * If _xfs_buf_ioapply failed, we can get back here with only the IO |
| 1502 | * reference we took above. If we drop it to zero, run completion so |
| 1503 | * that we don't return to the caller with completion still pending. |
| 1504 | */ |
| 1505 | if (atomic_dec_and_test(&bp->b_io_remaining) == 1) { |
| 1506 | if (bp->b_error || !(bp->b_flags & XBF_ASYNC)) |
| 1507 | xfs_buf_ioend(bp); |
| 1508 | else |
| 1509 | xfs_buf_ioend_async(bp); |
| 1510 | } |
| 1511 | |
Brian Foster | 6af88cd | 2018-07-11 22:26:35 -0700 | [diff] [blame] | 1512 | if (wait) |
| 1513 | error = xfs_buf_iowait(bp); |
Brian Foster | bb00b6f | 2018-07-11 22:26:35 -0700 | [diff] [blame] | 1514 | |
Dave Chinner | 595bff7 | 2014-10-02 09:05:14 +1000 | [diff] [blame] | 1515 | /* |
Brian Foster | 6af88cd | 2018-07-11 22:26:35 -0700 | [diff] [blame] | 1516 | * Release the hold that keeps the buffer referenced for the entire |
| 1517 | * I/O. Note that if the buffer is async, it is not safe to reference |
| 1518 | * after this release. |
Dave Chinner | 595bff7 | 2014-10-02 09:05:14 +1000 | [diff] [blame] | 1519 | */ |
| 1520 | xfs_buf_rele(bp); |
| 1521 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
Christoph Hellwig | 88ee2df | 2015-06-22 09:44:29 +1000 | [diff] [blame] | 1524 | void * |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1525 | xfs_buf_offset( |
Christoph Hellwig | 88ee2df | 2015-06-22 09:44:29 +1000 | [diff] [blame] | 1526 | struct xfs_buf *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | size_t offset) |
| 1528 | { |
| 1529 | struct page *page; |
| 1530 | |
Dave Chinner | 611c994 | 2012-04-23 15:59:07 +1000 | [diff] [blame] | 1531 | if (bp->b_addr) |
Chandra Seetharaman | 6292604 | 2011-07-22 23:40:15 +0000 | [diff] [blame] | 1532 | return bp->b_addr + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1534 | offset += bp->b_offset; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 1535 | page = bp->b_pages[offset >> PAGE_SHIFT]; |
Christoph Hellwig | 88ee2df | 2015-06-22 09:44:29 +1000 | [diff] [blame] | 1536 | return page_address(page) + (offset & (PAGE_SIZE-1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | void |
Christoph Hellwig | f9a196e | 2019-06-12 08:59:59 -0700 | [diff] [blame] | 1540 | xfs_buf_zero( |
| 1541 | struct xfs_buf *bp, |
| 1542 | size_t boff, |
| 1543 | size_t bsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | { |
Dave Chinner | 795cac7 | 2012-04-23 15:58:53 +1000 | [diff] [blame] | 1545 | size_t bend; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | |
| 1547 | bend = boff + bsize; |
| 1548 | while (boff < bend) { |
Dave Chinner | 795cac7 | 2012-04-23 15:58:53 +1000 | [diff] [blame] | 1549 | struct page *page; |
| 1550 | int page_index, page_offset, csize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
Dave Chinner | 795cac7 | 2012-04-23 15:58:53 +1000 | [diff] [blame] | 1552 | page_index = (boff + bp->b_offset) >> PAGE_SHIFT; |
| 1553 | page_offset = (boff + bp->b_offset) & ~PAGE_MASK; |
| 1554 | page = bp->b_pages[page_index]; |
| 1555 | csize = min_t(size_t, PAGE_SIZE - page_offset, |
Christoph Hellwig | 8124b9b | 2019-06-28 19:27:28 -0700 | [diff] [blame] | 1556 | BBTOB(bp->b_length) - boff); |
Dave Chinner | 795cac7 | 2012-04-23 15:58:53 +1000 | [diff] [blame] | 1557 | |
| 1558 | ASSERT((csize + page_offset) <= PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | |
Christoph Hellwig | f9a196e | 2019-06-12 08:59:59 -0700 | [diff] [blame] | 1560 | memset(page_address(page) + page_offset, 0, csize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | |
| 1562 | boff += csize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1567 | * Handling of buffer targets (buftargs). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | */ |
| 1569 | |
| 1570 | /* |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1571 | * Wait for any bufs with callbacks that have been submitted but have not yet |
| 1572 | * returned. These buffers will have an elevated hold count, so wait on those |
| 1573 | * while freeing all the buffers only held by the LRU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | */ |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1575 | static enum lru_status |
| 1576 | xfs_buftarg_wait_rele( |
| 1577 | struct list_head *item, |
Vladimir Davydov | 3f97b16 | 2015-02-12 14:59:35 -0800 | [diff] [blame] | 1578 | struct list_lru_one *lru, |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1579 | spinlock_t *lru_lock, |
| 1580 | void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1582 | { |
| 1583 | struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru); |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1584 | struct list_head *dispose = arg; |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1585 | |
| 1586 | if (atomic_read(&bp->b_hold) > 1) { |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1587 | /* need to wait, so skip it this pass */ |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1588 | trace_xfs_buf_wait_buftarg(bp, _RET_IP_); |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1589 | return LRU_SKIP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | } |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1591 | if (!spin_trylock(&bp->b_lock)) |
| 1592 | return LRU_SKIP; |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1593 | |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1594 | /* |
| 1595 | * clear the LRU reference count so the buffer doesn't get |
| 1596 | * ignored in xfs_buf_rele(). |
| 1597 | */ |
| 1598 | atomic_set(&bp->b_lru_ref, 0); |
| 1599 | bp->b_state |= XFS_BSTATE_DISPOSE; |
Vladimir Davydov | 3f97b16 | 2015-02-12 14:59:35 -0800 | [diff] [blame] | 1600 | list_lru_isolate_move(lru, item, dispose); |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1601 | spin_unlock(&bp->b_lock); |
| 1602 | return LRU_REMOVED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1605 | void |
| 1606 | xfs_wait_buftarg( |
| 1607 | struct xfs_buftarg *btp) |
| 1608 | { |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1609 | LIST_HEAD(dispose); |
| 1610 | int loop = 0; |
| 1611 | |
Dave Chinner | 85bec54 | 2016-01-19 08:28:10 +1100 | [diff] [blame] | 1612 | /* |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 1613 | * First wait on the buftarg I/O count for all in-flight buffers to be |
| 1614 | * released. This is critical as new buffers do not make the LRU until |
| 1615 | * they are released. |
| 1616 | * |
| 1617 | * Next, flush the buffer workqueue to ensure all completion processing |
| 1618 | * has finished. Just waiting on buffer locks is not sufficient for |
| 1619 | * async IO as the reference count held over IO is not released until |
| 1620 | * after the buffer lock is dropped. Hence we need to ensure here that |
| 1621 | * all reference counts have been dropped before we start walking the |
| 1622 | * LRU list. |
Dave Chinner | 85bec54 | 2016-01-19 08:28:10 +1100 | [diff] [blame] | 1623 | */ |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 1624 | while (percpu_counter_sum(&btp->bt_io_count)) |
| 1625 | delay(100); |
Brian Foster | 800b269 | 2016-08-26 16:01:59 +1000 | [diff] [blame] | 1626 | flush_workqueue(btp->bt_mount->m_buf_workqueue); |
Dave Chinner | 85bec54 | 2016-01-19 08:28:10 +1100 | [diff] [blame] | 1627 | |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1628 | /* loop until there is nothing left on the lru list. */ |
| 1629 | while (list_lru_count(&btp->bt_lru)) { |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1630 | list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele, |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1631 | &dispose, LONG_MAX); |
| 1632 | |
| 1633 | while (!list_empty(&dispose)) { |
| 1634 | struct xfs_buf *bp; |
| 1635 | bp = list_first_entry(&dispose, struct xfs_buf, b_lru); |
| 1636 | list_del_init(&bp->b_lru); |
Dave Chinner | ac8809f | 2013-12-12 16:34:38 +1100 | [diff] [blame] | 1637 | if (bp->b_flags & XBF_WRITE_FAIL) { |
| 1638 | xfs_alert(btp->bt_mount, |
Darrick J. Wong | c219b01 | 2018-01-08 11:39:18 -0800 | [diff] [blame] | 1639 | "Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!", |
Dave Chinner | ac8809f | 2013-12-12 16:34:38 +1100 | [diff] [blame] | 1640 | (long long)bp->b_bn); |
Joe Perches | f41febd | 2015-07-29 11:52:04 +1000 | [diff] [blame] | 1641 | xfs_alert(btp->bt_mount, |
| 1642 | "Please run xfs_repair to determine the extent of the problem."); |
Dave Chinner | ac8809f | 2013-12-12 16:34:38 +1100 | [diff] [blame] | 1643 | } |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1644 | xfs_buf_rele(bp); |
| 1645 | } |
| 1646 | if (loop++ != 0) |
| 1647 | delay(100); |
| 1648 | } |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | static enum lru_status |
| 1652 | xfs_buftarg_isolate( |
| 1653 | struct list_head *item, |
Vladimir Davydov | 3f97b16 | 2015-02-12 14:59:35 -0800 | [diff] [blame] | 1654 | struct list_lru_one *lru, |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1655 | spinlock_t *lru_lock, |
| 1656 | void *arg) |
| 1657 | { |
| 1658 | struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru); |
| 1659 | struct list_head *dispose = arg; |
| 1660 | |
| 1661 | /* |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1662 | * we are inverting the lru lock/bp->b_lock here, so use a trylock. |
| 1663 | * If we fail to get the lock, just skip it. |
| 1664 | */ |
| 1665 | if (!spin_trylock(&bp->b_lock)) |
| 1666 | return LRU_SKIP; |
| 1667 | /* |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1668 | * Decrement the b_lru_ref count unless the value is already |
| 1669 | * zero. If the value is already zero, we need to reclaim the |
| 1670 | * buffer, otherwise it gets another trip through the LRU. |
| 1671 | */ |
Vratislav Bendel | 19957a1 | 2018-03-06 17:07:44 -0800 | [diff] [blame] | 1672 | if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) { |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1673 | spin_unlock(&bp->b_lock); |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1674 | return LRU_ROTATE; |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1675 | } |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1676 | |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1677 | bp->b_state |= XFS_BSTATE_DISPOSE; |
Vladimir Davydov | 3f97b16 | 2015-02-12 14:59:35 -0800 | [diff] [blame] | 1678 | list_lru_isolate_move(lru, item, dispose); |
Dave Chinner | a408235 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1679 | spin_unlock(&bp->b_lock); |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1680 | return LRU_REMOVED; |
| 1681 | } |
| 1682 | |
Andrew Morton | addbda4 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1683 | static unsigned long |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1684 | xfs_buftarg_shrink_scan( |
Dave Chinner | ff57ab2 | 2010-11-30 17:27:57 +1100 | [diff] [blame] | 1685 | struct shrinker *shrink, |
Ying Han | 1495f23 | 2011-05-24 17:12:27 -0700 | [diff] [blame] | 1686 | struct shrink_control *sc) |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1687 | { |
Dave Chinner | ff57ab2 | 2010-11-30 17:27:57 +1100 | [diff] [blame] | 1688 | struct xfs_buftarg *btp = container_of(shrink, |
| 1689 | struct xfs_buftarg, bt_shrinker); |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1690 | LIST_HEAD(dispose); |
Andrew Morton | addbda4 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1691 | unsigned long freed; |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1692 | |
Vladimir Davydov | 503c358 | 2015-02-12 14:58:47 -0800 | [diff] [blame] | 1693 | freed = list_lru_shrink_walk(&btp->bt_lru, sc, |
| 1694 | xfs_buftarg_isolate, &dispose); |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1695 | |
| 1696 | while (!list_empty(&dispose)) { |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1697 | struct xfs_buf *bp; |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1698 | bp = list_first_entry(&dispose, struct xfs_buf, b_lru); |
| 1699 | list_del_init(&bp->b_lru); |
| 1700 | xfs_buf_rele(bp); |
| 1701 | } |
| 1702 | |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1703 | return freed; |
| 1704 | } |
| 1705 | |
Andrew Morton | addbda4 | 2013-08-28 10:18:06 +1000 | [diff] [blame] | 1706 | static unsigned long |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1707 | xfs_buftarg_shrink_count( |
| 1708 | struct shrinker *shrink, |
| 1709 | struct shrink_control *sc) |
| 1710 | { |
| 1711 | struct xfs_buftarg *btp = container_of(shrink, |
| 1712 | struct xfs_buftarg, bt_shrinker); |
Vladimir Davydov | 503c358 | 2015-02-12 14:58:47 -0800 | [diff] [blame] | 1713 | return list_lru_shrink_count(&btp->bt_lru, sc); |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1714 | } |
| 1715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | void |
| 1717 | xfs_free_buftarg( |
Christoph Hellwig | b796313 | 2009-03-03 14:48:37 -0500 | [diff] [blame] | 1718 | struct xfs_buftarg *btp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | { |
Dave Chinner | ff57ab2 | 2010-11-30 17:27:57 +1100 | [diff] [blame] | 1720 | unregister_shrinker(&btp->bt_shrinker); |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 1721 | ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0); |
| 1722 | percpu_counter_destroy(&btp->bt_io_count); |
Glauber Costa | f5e1dd3 | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 1723 | list_lru_destroy(&btp->bt_lru); |
Dave Chinner | ff57ab2 | 2010-11-30 17:27:57 +1100 | [diff] [blame] | 1724 | |
Dave Chinner | 2291dab | 2016-12-09 16:49:54 +1100 | [diff] [blame] | 1725 | xfs_blkdev_issue_flush(btp); |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1726 | |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1727 | kmem_free(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
Eric Sandeen | 3fefdee | 2013-11-13 14:53:45 -0600 | [diff] [blame] | 1730 | int |
| 1731 | xfs_setsize_buftarg( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | xfs_buftarg_t *btp, |
Eric Sandeen | 3fefdee | 2013-11-13 14:53:45 -0600 | [diff] [blame] | 1733 | unsigned int sectorsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | { |
Eric Sandeen | 7c71ee7 | 2014-01-21 16:46:23 -0600 | [diff] [blame] | 1735 | /* Set up metadata sector size info */ |
Eric Sandeen | 6da5417 | 2014-01-21 16:45:52 -0600 | [diff] [blame] | 1736 | btp->bt_meta_sectorsize = sectorsize; |
| 1737 | btp->bt_meta_sectormask = sectorsize - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1739 | if (set_blocksize(btp->bt_bdev, sectorsize)) { |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 1740 | xfs_warn(btp->bt_mount, |
Dmitry Monakhov | a1c6f057 | 2015-04-13 16:31:37 +0400 | [diff] [blame] | 1741 | "Cannot set_blocksize to %u on device %pg", |
| 1742 | sectorsize, btp->bt_bdev); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1743 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Eric Sandeen | 7c71ee7 | 2014-01-21 16:46:23 -0600 | [diff] [blame] | 1746 | /* Set up device logical sector size mask */ |
| 1747 | btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev); |
| 1748 | btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1; |
| 1749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | return 0; |
| 1751 | } |
| 1752 | |
| 1753 | /* |
Eric Sandeen | 3fefdee | 2013-11-13 14:53:45 -0600 | [diff] [blame] | 1754 | * When allocating the initial buffer target we have not yet |
| 1755 | * read in the superblock, so don't know what sized sectors |
| 1756 | * are being used at this early stage. Play safe. |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1757 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | STATIC int |
| 1759 | xfs_setsize_buftarg_early( |
| 1760 | xfs_buftarg_t *btp, |
| 1761 | struct block_device *bdev) |
| 1762 | { |
Eric Sandeen | a96c415 | 2014-04-14 19:00:29 +1000 | [diff] [blame] | 1763 | return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | xfs_buftarg_t * |
| 1767 | xfs_alloc_buftarg( |
Dave Chinner | ebad861 | 2010-09-22 10:47:20 +1000 | [diff] [blame] | 1768 | struct xfs_mount *mp, |
Dan Williams | 486aff5 | 2017-08-24 15:12:50 -0700 | [diff] [blame] | 1769 | struct block_device *bdev, |
| 1770 | struct dax_device *dax_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | { |
| 1772 | xfs_buftarg_t *btp; |
| 1773 | |
Tetsuo Handa | 707e0dd | 2019-08-26 12:06:22 -0700 | [diff] [blame] | 1774 | btp = kmem_zalloc(sizeof(*btp), KM_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
Dave Chinner | ebad861 | 2010-09-22 10:47:20 +1000 | [diff] [blame] | 1776 | btp->bt_mount = mp; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1777 | btp->bt_dev = bdev->bd_dev; |
| 1778 | btp->bt_bdev = bdev; |
Dan Williams | 486aff5 | 2017-08-24 15:12:50 -0700 | [diff] [blame] | 1779 | btp->bt_daxdev = dax_dev; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 1780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | if (xfs_setsize_buftarg_early(btp, bdev)) |
Michal Hocko | d210a98 | 2017-11-23 17:13:40 +0100 | [diff] [blame] | 1782 | goto error_free; |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 1783 | |
| 1784 | if (list_lru_init(&btp->bt_lru)) |
Michal Hocko | d210a98 | 2017-11-23 17:13:40 +0100 | [diff] [blame] | 1785 | goto error_free; |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 1786 | |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 1787 | if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL)) |
Michal Hocko | d210a98 | 2017-11-23 17:13:40 +0100 | [diff] [blame] | 1788 | goto error_lru; |
Brian Foster | 9c7504a | 2016-07-20 11:15:28 +1000 | [diff] [blame] | 1789 | |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1790 | btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count; |
| 1791 | btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan; |
Dave Chinner | ff57ab2 | 2010-11-30 17:27:57 +1100 | [diff] [blame] | 1792 | btp->bt_shrinker.seeks = DEFAULT_SEEKS; |
Dave Chinner | e80dfa1 | 2013-08-28 10:18:05 +1000 | [diff] [blame] | 1793 | btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE; |
Michal Hocko | d210a98 | 2017-11-23 17:13:40 +0100 | [diff] [blame] | 1794 | if (register_shrinker(&btp->bt_shrinker)) |
| 1795 | goto error_pcpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | return btp; |
| 1797 | |
Michal Hocko | d210a98 | 2017-11-23 17:13:40 +0100 | [diff] [blame] | 1798 | error_pcpu: |
| 1799 | percpu_counter_destroy(&btp->bt_io_count); |
| 1800 | error_lru: |
| 1801 | list_lru_destroy(&btp->bt_lru); |
| 1802 | error_free: |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1803 | kmem_free(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | return NULL; |
| 1805 | } |
| 1806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | /* |
Brian Foster | 20e8a06 | 2017-04-21 12:40:44 -0700 | [diff] [blame] | 1808 | * Cancel a delayed write list. |
| 1809 | * |
| 1810 | * Remove each buffer from the list, clear the delwri queue flag and drop the |
| 1811 | * associated buffer reference. |
| 1812 | */ |
| 1813 | void |
| 1814 | xfs_buf_delwri_cancel( |
| 1815 | struct list_head *list) |
| 1816 | { |
| 1817 | struct xfs_buf *bp; |
| 1818 | |
| 1819 | while (!list_empty(list)) { |
| 1820 | bp = list_first_entry(list, struct xfs_buf, b_list); |
| 1821 | |
| 1822 | xfs_buf_lock(bp); |
| 1823 | bp->b_flags &= ~_XBF_DELWRI_Q; |
| 1824 | list_del_init(&bp->b_list); |
| 1825 | xfs_buf_relse(bp); |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | /* |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1830 | * Add a buffer to the delayed write list. |
| 1831 | * |
| 1832 | * This queues a buffer for writeout if it hasn't already been. Note that |
| 1833 | * neither this routine nor the buffer list submission functions perform |
| 1834 | * any internal synchronization. It is expected that the lists are thread-local |
| 1835 | * to the callers. |
| 1836 | * |
| 1837 | * Returns true if we queued up the buffer, or false if it already had |
| 1838 | * been on the buffer list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | */ |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1840 | bool |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1841 | xfs_buf_delwri_queue( |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1842 | struct xfs_buf *bp, |
| 1843 | struct list_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | { |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1845 | ASSERT(xfs_buf_islocked(bp)); |
| 1846 | ASSERT(!(bp->b_flags & XBF_READ)); |
| 1847 | |
| 1848 | /* |
| 1849 | * If the buffer is already marked delwri it already is queued up |
| 1850 | * by someone else for imediate writeout. Just ignore it in that |
| 1851 | * case. |
| 1852 | */ |
| 1853 | if (bp->b_flags & _XBF_DELWRI_Q) { |
| 1854 | trace_xfs_buf_delwri_queued(bp, _RET_IP_); |
| 1855 | return false; |
| 1856 | } |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1857 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1858 | trace_xfs_buf_delwri_queue(bp, _RET_IP_); |
| 1859 | |
Dave Chinner | d808f61 | 2010-02-02 10:13:42 +1100 | [diff] [blame] | 1860 | /* |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1861 | * If a buffer gets written out synchronously or marked stale while it |
| 1862 | * is on a delwri list we lazily remove it. To do this, the other party |
| 1863 | * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone. |
| 1864 | * It remains referenced and on the list. In a rare corner case it |
| 1865 | * might get readded to a delwri list after the synchronous writeout, in |
| 1866 | * which case we need just need to re-add the flag here. |
Dave Chinner | d808f61 | 2010-02-02 10:13:42 +1100 | [diff] [blame] | 1867 | */ |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1868 | bp->b_flags |= _XBF_DELWRI_Q; |
| 1869 | if (list_empty(&bp->b_list)) { |
| 1870 | atomic_inc(&bp->b_hold); |
| 1871 | list_add_tail(&bp->b_list, list); |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1872 | } |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1873 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1874 | return true; |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1875 | } |
| 1876 | |
Dave Chinner | 089716a | 2010-01-26 15:13:25 +1100 | [diff] [blame] | 1877 | /* |
| 1878 | * Compare function is more complex than it needs to be because |
| 1879 | * the return value is only 32 bits and we are doing comparisons |
| 1880 | * on 64 bit values |
| 1881 | */ |
| 1882 | static int |
| 1883 | xfs_buf_cmp( |
| 1884 | void *priv, |
| 1885 | struct list_head *a, |
| 1886 | struct list_head *b) |
| 1887 | { |
| 1888 | struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list); |
| 1889 | struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list); |
| 1890 | xfs_daddr_t diff; |
| 1891 | |
Mark Tinguely | f4b4242 | 2012-12-04 17:18:02 -0600 | [diff] [blame] | 1892 | diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn; |
Dave Chinner | 089716a | 2010-01-26 15:13:25 +1100 | [diff] [blame] | 1893 | if (diff < 0) |
| 1894 | return -1; |
| 1895 | if (diff > 0) |
| 1896 | return 1; |
| 1897 | return 0; |
| 1898 | } |
| 1899 | |
Dave Chinner | 26f1fe8 | 2016-06-01 17:38:15 +1000 | [diff] [blame] | 1900 | /* |
Brian Foster | e339dd8 | 2018-07-11 22:26:34 -0700 | [diff] [blame] | 1901 | * Submit buffers for write. If wait_list is specified, the buffers are |
| 1902 | * submitted using sync I/O and placed on the wait list such that the caller can |
| 1903 | * iowait each buffer. Otherwise async I/O is used and the buffers are released |
| 1904 | * at I/O completion time. In either case, buffers remain locked until I/O |
| 1905 | * completes and the buffer is released from the queue. |
Dave Chinner | 26f1fe8 | 2016-06-01 17:38:15 +1000 | [diff] [blame] | 1906 | */ |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1907 | static int |
Dave Chinner | 26f1fe8 | 2016-06-01 17:38:15 +1000 | [diff] [blame] | 1908 | xfs_buf_delwri_submit_buffers( |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1909 | struct list_head *buffer_list, |
Dave Chinner | 26f1fe8 | 2016-06-01 17:38:15 +1000 | [diff] [blame] | 1910 | struct list_head *wait_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | { |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1912 | struct xfs_buf *bp, *n; |
| 1913 | int pinned = 0; |
Dave Chinner | 26f1fe8 | 2016-06-01 17:38:15 +1000 | [diff] [blame] | 1914 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | |
Dave Chinner | 26f1fe8 | 2016-06-01 17:38:15 +1000 | [diff] [blame] | 1916 | list_sort(NULL, buffer_list, xfs_buf_cmp); |
| 1917 | |
| 1918 | blk_start_plug(&plug); |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1919 | list_for_each_entry_safe(bp, n, buffer_list, b_list) { |
Dave Chinner | 26f1fe8 | 2016-06-01 17:38:15 +1000 | [diff] [blame] | 1920 | if (!wait_list) { |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1921 | if (xfs_buf_ispinned(bp)) { |
| 1922 | pinned++; |
| 1923 | continue; |
| 1924 | } |
| 1925 | if (!xfs_buf_trylock(bp)) |
| 1926 | continue; |
| 1927 | } else { |
| 1928 | xfs_buf_lock(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1931 | /* |
| 1932 | * Someone else might have written the buffer synchronously or |
| 1933 | * marked it stale in the meantime. In that case only the |
| 1934 | * _XBF_DELWRI_Q flag got cleared, and we have to drop the |
| 1935 | * reference and remove it from the list here. |
| 1936 | */ |
| 1937 | if (!(bp->b_flags & _XBF_DELWRI_Q)) { |
| 1938 | list_del_init(&bp->b_list); |
| 1939 | xfs_buf_relse(bp); |
| 1940 | continue; |
| 1941 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1943 | trace_xfs_buf_delwri_split(bp, _RET_IP_); |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1944 | |
Dave Chinner | cf53e99 | 2014-10-02 09:04:01 +1000 | [diff] [blame] | 1945 | /* |
Brian Foster | e339dd8 | 2018-07-11 22:26:34 -0700 | [diff] [blame] | 1946 | * If we have a wait list, each buffer (and associated delwri |
| 1947 | * queue reference) transfers to it and is submitted |
| 1948 | * synchronously. Otherwise, drop the buffer from the delwri |
| 1949 | * queue and submit async. |
Dave Chinner | cf53e99 | 2014-10-02 09:04:01 +1000 | [diff] [blame] | 1950 | */ |
Dave Chinner | bbfeb61 | 2016-07-20 11:53:35 +1000 | [diff] [blame] | 1951 | bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL); |
Brian Foster | e339dd8 | 2018-07-11 22:26:34 -0700 | [diff] [blame] | 1952 | bp->b_flags |= XBF_WRITE; |
Dave Chinner | 26f1fe8 | 2016-06-01 17:38:15 +1000 | [diff] [blame] | 1953 | if (wait_list) { |
Brian Foster | e339dd8 | 2018-07-11 22:26:34 -0700 | [diff] [blame] | 1954 | bp->b_flags &= ~XBF_ASYNC; |
Dave Chinner | 26f1fe8 | 2016-06-01 17:38:15 +1000 | [diff] [blame] | 1955 | list_move_tail(&bp->b_list, wait_list); |
Brian Foster | e339dd8 | 2018-07-11 22:26:34 -0700 | [diff] [blame] | 1956 | } else { |
| 1957 | bp->b_flags |= XBF_ASYNC; |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1958 | list_del_init(&bp->b_list); |
Brian Foster | e339dd8 | 2018-07-11 22:26:34 -0700 | [diff] [blame] | 1959 | } |
Brian Foster | 6af88cd | 2018-07-11 22:26:35 -0700 | [diff] [blame] | 1960 | __xfs_buf_submit(bp, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | } |
Christoph Hellwig | a1b7ea5 | 2011-03-30 11:05:09 +0000 | [diff] [blame] | 1962 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1964 | return pinned; |
| 1965 | } |
Nathan Scott | f07c225 | 2006-09-28 10:52:15 +1000 | [diff] [blame] | 1966 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1967 | /* |
| 1968 | * Write out a buffer list asynchronously. |
| 1969 | * |
| 1970 | * This will take the @buffer_list, write all non-locked and non-pinned buffers |
| 1971 | * out and not wait for I/O completion on any of the buffers. This interface |
| 1972 | * is only safely useable for callers that can track I/O completion by higher |
| 1973 | * level means, e.g. AIL pushing as the @buffer_list is consumed in this |
| 1974 | * function. |
Brian Foster | efc3289 | 2018-10-18 17:21:49 +1100 | [diff] [blame] | 1975 | * |
| 1976 | * Note: this function will skip buffers it would block on, and in doing so |
| 1977 | * leaves them on @buffer_list so they can be retried on a later pass. As such, |
| 1978 | * it is up to the caller to ensure that the buffer list is fully submitted or |
| 1979 | * cancelled appropriately when they are finished with the list. Failure to |
| 1980 | * cancel or resubmit the list until it is empty will result in leaked buffers |
| 1981 | * at unmount time. |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1982 | */ |
| 1983 | int |
| 1984 | xfs_buf_delwri_submit_nowait( |
| 1985 | struct list_head *buffer_list) |
| 1986 | { |
Dave Chinner | 26f1fe8 | 2016-06-01 17:38:15 +1000 | [diff] [blame] | 1987 | return xfs_buf_delwri_submit_buffers(buffer_list, NULL); |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | /* |
| 1991 | * Write out a buffer list synchronously. |
| 1992 | * |
| 1993 | * This will take the @buffer_list, write all buffers out and wait for I/O |
| 1994 | * completion on all of the buffers. @buffer_list is consumed by the function, |
| 1995 | * so callers must have some other way of tracking buffers if they require such |
| 1996 | * functionality. |
| 1997 | */ |
| 1998 | int |
| 1999 | xfs_buf_delwri_submit( |
| 2000 | struct list_head *buffer_list) |
| 2001 | { |
Dave Chinner | 26f1fe8 | 2016-06-01 17:38:15 +1000 | [diff] [blame] | 2002 | LIST_HEAD (wait_list); |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 2003 | int error = 0, error2; |
| 2004 | struct xfs_buf *bp; |
| 2005 | |
Dave Chinner | 26f1fe8 | 2016-06-01 17:38:15 +1000 | [diff] [blame] | 2006 | xfs_buf_delwri_submit_buffers(buffer_list, &wait_list); |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 2007 | |
| 2008 | /* Wait for IO to complete. */ |
Dave Chinner | 26f1fe8 | 2016-06-01 17:38:15 +1000 | [diff] [blame] | 2009 | while (!list_empty(&wait_list)) { |
| 2010 | bp = list_first_entry(&wait_list, struct xfs_buf, b_list); |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 2011 | |
| 2012 | list_del_init(&bp->b_list); |
Dave Chinner | cf53e99 | 2014-10-02 09:04:01 +1000 | [diff] [blame] | 2013 | |
Brian Foster | e339dd8 | 2018-07-11 22:26:34 -0700 | [diff] [blame] | 2014 | /* |
| 2015 | * Wait on the locked buffer, check for errors and unlock and |
| 2016 | * release the delwri queue reference. |
| 2017 | */ |
| 2018 | error2 = xfs_buf_iowait(bp); |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 2019 | xfs_buf_relse(bp); |
| 2020 | if (!error) |
| 2021 | error = error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 2024 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | } |
| 2026 | |
Brian Foster | 7912e7f | 2017-06-14 21:21:45 -0700 | [diff] [blame] | 2027 | /* |
| 2028 | * Push a single buffer on a delwri queue. |
| 2029 | * |
| 2030 | * The purpose of this function is to submit a single buffer of a delwri queue |
| 2031 | * and return with the buffer still on the original queue. The waiting delwri |
| 2032 | * buffer submission infrastructure guarantees transfer of the delwri queue |
| 2033 | * buffer reference to a temporary wait list. We reuse this infrastructure to |
| 2034 | * transfer the buffer back to the original queue. |
| 2035 | * |
| 2036 | * Note the buffer transitions from the queued state, to the submitted and wait |
| 2037 | * listed state and back to the queued state during this call. The buffer |
| 2038 | * locking and queue management logic between _delwri_pushbuf() and |
| 2039 | * _delwri_queue() guarantee that the buffer cannot be queued to another list |
| 2040 | * before returning. |
| 2041 | */ |
| 2042 | int |
| 2043 | xfs_buf_delwri_pushbuf( |
| 2044 | struct xfs_buf *bp, |
| 2045 | struct list_head *buffer_list) |
| 2046 | { |
| 2047 | LIST_HEAD (submit_list); |
| 2048 | int error; |
| 2049 | |
| 2050 | ASSERT(bp->b_flags & _XBF_DELWRI_Q); |
| 2051 | |
| 2052 | trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_); |
| 2053 | |
| 2054 | /* |
| 2055 | * Isolate the buffer to a new local list so we can submit it for I/O |
| 2056 | * independently from the rest of the original list. |
| 2057 | */ |
| 2058 | xfs_buf_lock(bp); |
| 2059 | list_move(&bp->b_list, &submit_list); |
| 2060 | xfs_buf_unlock(bp); |
| 2061 | |
| 2062 | /* |
| 2063 | * Delwri submission clears the DELWRI_Q buffer flag and returns with |
Brian Foster | e339dd8 | 2018-07-11 22:26:34 -0700 | [diff] [blame] | 2064 | * the buffer on the wait list with the original reference. Rather than |
Brian Foster | 7912e7f | 2017-06-14 21:21:45 -0700 | [diff] [blame] | 2065 | * bounce the buffer from a local wait list back to the original list |
| 2066 | * after I/O completion, reuse the original list as the wait list. |
| 2067 | */ |
| 2068 | xfs_buf_delwri_submit_buffers(&submit_list, buffer_list); |
| 2069 | |
| 2070 | /* |
Brian Foster | e339dd8 | 2018-07-11 22:26:34 -0700 | [diff] [blame] | 2071 | * The buffer is now locked, under I/O and wait listed on the original |
| 2072 | * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and |
| 2073 | * return with the buffer unlocked and on the original queue. |
Brian Foster | 7912e7f | 2017-06-14 21:21:45 -0700 | [diff] [blame] | 2074 | */ |
Brian Foster | e339dd8 | 2018-07-11 22:26:34 -0700 | [diff] [blame] | 2075 | error = xfs_buf_iowait(bp); |
Brian Foster | 7912e7f | 2017-06-14 21:21:45 -0700 | [diff] [blame] | 2076 | bp->b_flags |= _XBF_DELWRI_Q; |
| 2077 | xfs_buf_unlock(bp); |
| 2078 | |
| 2079 | return error; |
| 2080 | } |
| 2081 | |
Christoph Hellwig | 04d8b28 | 2005-11-02 10:15:05 +1100 | [diff] [blame] | 2082 | int __init |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 2083 | xfs_buf_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | { |
Carlos Maiolino | b123176 | 2019-11-14 12:43:03 -0800 | [diff] [blame] | 2085 | xfs_buf_zone = kmem_cache_create("xfs_buf", |
| 2086 | sizeof(struct xfs_buf), 0, |
| 2087 | SLAB_HWCACHE_ALIGN, NULL); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 2088 | if (!xfs_buf_zone) |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2089 | goto out; |
Christoph Hellwig | 04d8b28 | 2005-11-02 10:15:05 +1100 | [diff] [blame] | 2090 | |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 2091 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2093 | out: |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 2094 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 2098 | xfs_buf_terminate(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | { |
Carlos Maiolino | aaf54eb | 2019-11-14 12:43:04 -0800 | [diff] [blame] | 2100 | kmem_cache_destroy(xfs_buf_zone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | } |
Brian Foster | 7561d27 | 2017-10-17 14:16:29 -0700 | [diff] [blame] | 2102 | |
| 2103 | void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref) |
| 2104 | { |
Brian Foster | 7561d27 | 2017-10-17 14:16:29 -0700 | [diff] [blame] | 2105 | /* |
| 2106 | * Set the lru reference count to 0 based on the error injection tag. |
| 2107 | * This allows userspace to disrupt buffer caching for debug/testing |
| 2108 | * purposes. |
| 2109 | */ |
Christoph Hellwig | dbd329f1 | 2019-06-28 19:27:29 -0700 | [diff] [blame] | 2110 | if (XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_LRU_REF)) |
Brian Foster | 7561d27 | 2017-10-17 14:16:29 -0700 | [diff] [blame] | 2111 | lru_ref = 0; |
| 2112 | |
| 2113 | atomic_set(&bp->b_lru_ref, lru_ref); |
| 2114 | } |
Brian Foster | 8473fee | 2019-02-07 10:45:46 -0800 | [diff] [blame] | 2115 | |
| 2116 | /* |
| 2117 | * Verify an on-disk magic value against the magic value specified in the |
| 2118 | * verifier structure. The verifier magic is in disk byte order so the caller is |
| 2119 | * expected to pass the value directly from disk. |
| 2120 | */ |
| 2121 | bool |
| 2122 | xfs_verify_magic( |
| 2123 | struct xfs_buf *bp, |
Darrick J. Wong | 15baadf | 2019-02-16 11:47:28 -0800 | [diff] [blame] | 2124 | __be32 dmagic) |
Brian Foster | 8473fee | 2019-02-07 10:45:46 -0800 | [diff] [blame] | 2125 | { |
Christoph Hellwig | dbd329f1 | 2019-06-28 19:27:29 -0700 | [diff] [blame] | 2126 | struct xfs_mount *mp = bp->b_mount; |
Brian Foster | 8473fee | 2019-02-07 10:45:46 -0800 | [diff] [blame] | 2127 | int idx; |
| 2128 | |
| 2129 | idx = xfs_sb_version_hascrc(&mp->m_sb); |
Denis Efremov | 14ed868 | 2019-09-25 16:49:37 -0700 | [diff] [blame] | 2130 | if (WARN_ON(!bp->b_ops || !bp->b_ops->magic[idx])) |
Brian Foster | 8473fee | 2019-02-07 10:45:46 -0800 | [diff] [blame] | 2131 | return false; |
| 2132 | return dmagic == bp->b_ops->magic[idx]; |
| 2133 | } |
Darrick J. Wong | 15baadf | 2019-02-16 11:47:28 -0800 | [diff] [blame] | 2134 | /* |
| 2135 | * Verify an on-disk magic value against the magic value specified in the |
| 2136 | * verifier structure. The verifier magic is in disk byte order so the caller is |
| 2137 | * expected to pass the value directly from disk. |
| 2138 | */ |
| 2139 | bool |
| 2140 | xfs_verify_magic16( |
| 2141 | struct xfs_buf *bp, |
| 2142 | __be16 dmagic) |
| 2143 | { |
Christoph Hellwig | dbd329f1 | 2019-06-28 19:27:29 -0700 | [diff] [blame] | 2144 | struct xfs_mount *mp = bp->b_mount; |
Darrick J. Wong | 15baadf | 2019-02-16 11:47:28 -0800 | [diff] [blame] | 2145 | int idx; |
| 2146 | |
| 2147 | idx = xfs_sb_version_hascrc(&mp->m_sb); |
Denis Efremov | 14ed868 | 2019-09-25 16:49:37 -0700 | [diff] [blame] | 2148 | if (WARN_ON(!bp->b_ops || !bp->b_ops->magic16[idx])) |
Darrick J. Wong | 15baadf | 2019-02-16 11:47:28 -0800 | [diff] [blame] | 2149 | return false; |
| 2150 | return dmagic == bp->b_ops->magic16[idx]; |
| 2151 | } |