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