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