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