blob: d02edb683cfd9d9491d87f0040dd8f52ddc29a03 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scottf07c2252006-09-28 10:52:15 +10003 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Vlad Apostolov93c189c2006-11-11 18:03:49 +11006#include "xfs.h"
Andrew Morton3fcfab12006-10-19 23:28:16 -07007#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Darrick J. Wong5467b342019-06-28 19:25:35 -07009#include "xfs_shared.h"
Christoph Hellwig4fb6e8a2014-11-28 14:25:04 +110010#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_log_format.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100012#include "xfs_trans_resv.h"
Dave Chinner239880e2013-10-23 10:50:10 +110013#include "xfs_sb.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050014#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000015#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110016#include "xfs_log.h"
Dave Chinner9fe5c772020-06-29 14:48:47 -070017#include "xfs_log_recover.h"
Dave Chinnerf593bf12020-06-29 14:48:46 -070018#include "xfs_trans.h"
19#include "xfs_buf_item.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070020#include "xfs_errortag.h"
Brian Foster7561d272017-10-17 14:16:29 -070021#include "xfs_error.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050022
David Chinner7989cb82007-02-10 18:34:56 +110023static kmem_zone_t *xfs_buf_zone;
Christoph Hellwig23ea4032005-06-21 15:14:01 +100024
Nathan Scottce8e9222006-01-11 15:39:08 +110025#define xb_to_gfp(flags) \
Dave Chinneraa5c1582012-04-23 15:58:56 +100026 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Dave Chinner37fd1672018-10-18 17:21:29 +110028/*
29 * Locking orders
30 *
31 * xfs_buf_ioacct_inc:
32 * xfs_buf_ioacct_dec:
33 * b_sema (caller holds)
34 * b_lock
35 *
36 * xfs_buf_stale:
37 * b_sema (caller holds)
38 * b_lock
39 * lru_lock
40 *
41 * xfs_buf_rele:
42 * b_lock
43 * pag_buf_lock
44 * lru_lock
45 *
Brian Foster10fb9ac2021-01-22 16:48:19 -080046 * xfs_buftarg_drain_rele
Dave Chinner37fd1672018-10-18 17:21:29 +110047 * lru_lock
48 * b_lock (trylock due to inversion)
49 *
50 * xfs_buftarg_isolate
51 * lru_lock
52 * b_lock (trylock due to inversion)
53 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Christoph Hellwig26e328752020-09-01 10:55:47 -070055static int __xfs_buf_submit(struct xfs_buf *bp, bool wait);
56
57static inline int
58xfs_buf_submit(
59 struct xfs_buf *bp)
60{
61 return __xfs_buf_submit(bp, !(bp->b_flags & XBF_ASYNC));
62}
63
James Bottomley73c77e22010-01-25 11:42:24 -060064static inline int
65xfs_buf_is_vmapped(
66 struct xfs_buf *bp)
67{
68 /*
69 * Return true if the buffer is vmapped.
70 *
Dave Chinner611c9942012-04-23 15:59:07 +100071 * b_addr is null if the buffer is not mapped, but the code is clever
72 * enough to know it doesn't have to map a single page, so the check has
73 * to be both for b_addr and bp->b_page_count > 1.
James Bottomley73c77e22010-01-25 11:42:24 -060074 */
Dave Chinner611c9942012-04-23 15:59:07 +100075 return bp->b_addr && bp->b_page_count > 1;
James Bottomley73c77e22010-01-25 11:42:24 -060076}
77
78static inline int
79xfs_buf_vmap_len(
80 struct xfs_buf *bp)
81{
82 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/*
Brian Foster9c7504a2016-07-20 11:15:28 +100086 * Bump the I/O in flight count on the buftarg if we haven't yet done so for
87 * this buffer. The count is incremented once per buffer (per hold cycle)
88 * because the corresponding decrement is deferred to buffer release. Buffers
89 * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
90 * tracking adds unnecessary overhead. This is used for sychronization purposes
Brian Foster10fb9ac2021-01-22 16:48:19 -080091 * with unmount (see xfs_buftarg_drain()), so all we really need is a count of
Brian Foster9c7504a2016-07-20 11:15:28 +100092 * in-flight buffers.
93 *
94 * Buffers that are never released (e.g., superblock, iclog buffers) must set
95 * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
96 * never reaches zero and unmount hangs indefinitely.
97 */
98static inline void
99xfs_buf_ioacct_inc(
100 struct xfs_buf *bp)
101{
Brian Foster63db7c82017-05-31 08:22:52 -0700102 if (bp->b_flags & XBF_NO_IOACCT)
Brian Foster9c7504a2016-07-20 11:15:28 +1000103 return;
104
105 ASSERT(bp->b_flags & XBF_ASYNC);
Brian Foster63db7c82017-05-31 08:22:52 -0700106 spin_lock(&bp->b_lock);
107 if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) {
108 bp->b_state |= XFS_BSTATE_IN_FLIGHT;
109 percpu_counter_inc(&bp->b_target->bt_io_count);
110 }
111 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000112}
113
114/*
115 * Clear the in-flight state on a buffer about to be released to the LRU or
116 * freed and unaccount from the buftarg.
117 */
118static inline void
Brian Foster63db7c82017-05-31 08:22:52 -0700119__xfs_buf_ioacct_dec(
120 struct xfs_buf *bp)
121{
Brian Foster95989c42017-06-08 08:23:07 -0700122 lockdep_assert_held(&bp->b_lock);
Brian Foster63db7c82017-05-31 08:22:52 -0700123
124 if (bp->b_state & XFS_BSTATE_IN_FLIGHT) {
125 bp->b_state &= ~XFS_BSTATE_IN_FLIGHT;
126 percpu_counter_dec(&bp->b_target->bt_io_count);
127 }
128}
129
130static inline void
Brian Foster9c7504a2016-07-20 11:15:28 +1000131xfs_buf_ioacct_dec(
132 struct xfs_buf *bp)
133{
Brian Foster63db7c82017-05-31 08:22:52 -0700134 spin_lock(&bp->b_lock);
135 __xfs_buf_ioacct_dec(bp);
136 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000137}
138
139/*
Dave Chinner430cbeb2010-12-02 16:30:55 +1100140 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
141 * b_lru_ref count so that the buffer is freed immediately when the buffer
142 * reference count falls to zero. If the buffer is already on the LRU, we need
143 * to remove the reference that LRU holds on the buffer.
144 *
145 * This prevents build-up of stale buffers on the LRU.
146 */
147void
148xfs_buf_stale(
149 struct xfs_buf *bp)
150{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000151 ASSERT(xfs_buf_islocked(bp));
152
Dave Chinner430cbeb2010-12-02 16:30:55 +1100153 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000154
155 /*
156 * Clear the delwri status so that a delwri queue walker will not
157 * flush this buffer to disk now that it is stale. The delwri queue has
158 * a reference to the buffer, so this is safe to do.
159 */
160 bp->b_flags &= ~_XBF_DELWRI_Q;
161
Brian Foster9c7504a2016-07-20 11:15:28 +1000162 /*
163 * Once the buffer is marked stale and unlocked, a subsequent lookup
164 * could reset b_flags. There is no guarantee that the buffer is
165 * unaccounted (released to LRU) before that occurs. Drop in-flight
166 * status now to preserve accounting consistency.
167 */
Dave Chinnera4082352013-08-28 10:18:06 +1000168 spin_lock(&bp->b_lock);
Brian Foster63db7c82017-05-31 08:22:52 -0700169 __xfs_buf_ioacct_dec(bp);
170
Dave Chinnera4082352013-08-28 10:18:06 +1000171 atomic_set(&bp->b_lru_ref, 0);
172 if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
Dave Chinnere80dfa12013-08-28 10:18:05 +1000173 (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
174 atomic_dec(&bp->b_hold);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100175
Dave Chinner430cbeb2010-12-02 16:30:55 +1100176 ASSERT(atomic_read(&bp->b_hold) >= 1);
Dave Chinnera4082352013-08-28 10:18:06 +1000177 spin_unlock(&bp->b_lock);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Dave Chinner3e85c862012-06-22 18:50:09 +1000180static int
181xfs_buf_get_maps(
182 struct xfs_buf *bp,
183 int map_count)
184{
185 ASSERT(bp->b_maps == NULL);
186 bp->b_map_count = map_count;
187
188 if (map_count == 1) {
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600189 bp->b_maps = &bp->__b_map;
Dave Chinner3e85c862012-06-22 18:50:09 +1000190 return 0;
191 }
192
193 bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
194 KM_NOFS);
195 if (!bp->b_maps)
Dave Chinner24513372014-06-25 14:58:08 +1000196 return -ENOMEM;
Dave Chinner3e85c862012-06-22 18:50:09 +1000197 return 0;
198}
199
200/*
201 * Frees b_pages if it was allocated.
202 */
203static void
204xfs_buf_free_maps(
205 struct xfs_buf *bp)
206{
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600207 if (bp->b_maps != &bp->__b_map) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000208 kmem_free(bp->b_maps);
209 bp->b_maps = NULL;
210 }
211}
212
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800213static int
Dave Chinner3e85c862012-06-22 18:50:09 +1000214_xfs_buf_alloc(
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000215 struct xfs_buftarg *target,
Dave Chinner3e85c862012-06-22 18:50:09 +1000216 struct xfs_buf_map *map,
217 int nmaps,
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800218 xfs_buf_flags_t flags,
219 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000221 struct xfs_buf *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000222 int error;
223 int i;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000224
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800225 *bpp = NULL;
Carlos Maiolino32a2b112020-07-22 09:23:10 -0700226 bp = kmem_cache_zalloc(xfs_buf_zone, GFP_NOFS | __GFP_NOFAIL);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /*
Dave Chinner12bcb3f2012-04-23 15:59:05 +1000229 * We don't want certain flags to appear in b_flags unless they are
230 * specifically set by later operations on the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
Dave Chinner611c9942012-04-23 15:59:07 +1000232 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Nathan Scottce8e9222006-01-11 15:39:08 +1100234 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100235 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000236 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100237 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100238 INIT_LIST_HEAD(&bp->b_list);
Carlos Maiolino643c8c02018-01-24 13:38:49 -0800239 INIT_LIST_HEAD(&bp->b_li_list);
Thomas Gleixnera731cd112010-09-07 14:33:15 +0000240 sema_init(&bp->b_sema, 0); /* held, no waiters */
Dave Chinnera4082352013-08-28 10:18:06 +1000241 spin_lock_init(&bp->b_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100242 bp->b_target = target;
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700243 bp->b_mount = target->bt_mount;
Dave Chinner3e85c862012-06-22 18:50:09 +1000244 bp->b_flags = flags;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000247 * Set length and io_length to the same value initially.
248 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 * most cases but may be reset (e.g. XFS recovery).
250 */
Dave Chinner3e85c862012-06-22 18:50:09 +1000251 error = xfs_buf_get_maps(bp, nmaps);
252 if (error) {
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800253 kmem_cache_free(xfs_buf_zone, bp);
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800254 return error;
Dave Chinner3e85c862012-06-22 18:50:09 +1000255 }
256
257 bp->b_bn = map[0].bm_bn;
258 bp->b_length = 0;
259 for (i = 0; i < nmaps; i++) {
260 bp->b_maps[i].bm_bn = map[i].bm_bn;
261 bp->b_maps[i].bm_len = map[i].bm_len;
262 bp->b_length += map[i].bm_len;
263 }
Dave Chinner3e85c862012-06-22 18:50:09 +1000264
Nathan Scottce8e9222006-01-11 15:39:08 +1100265 atomic_set(&bp->b_pin_count, 0);
266 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700268 XFS_STATS_INC(bp->b_mount, xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000269 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000270
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800271 *bpp = bp;
272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Dave Chinnere7d236a2021-06-01 13:40:36 +1000275static void
276xfs_buf_free_pages(
Dave Chinnere8222612020-12-16 16:07:34 -0800277 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Dave Chinnere7d236a2021-06-01 13:40:36 +1000279 uint i;
280
281 ASSERT(bp->b_flags & _XBF_PAGES);
282
283 if (xfs_buf_is_vmapped(bp))
284 vm_unmap_ram(bp->b_addr - bp->b_offset, bp->b_page_count);
285
286 for (i = 0; i < bp->b_page_count; i++) {
287 if (bp->b_pages[i])
288 __free_page(bp->b_pages[i]);
289 }
290 if (current->reclaim_state)
291 current->reclaim_state->reclaimed_slab += bp->b_page_count;
292
Dave Chinner02c51172021-06-01 13:40:36 +1000293 if (bp->b_pages != bp->b_page_array)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000294 kmem_free(bp->b_pages);
Dave Chinner02c51172021-06-01 13:40:36 +1000295 bp->b_pages = NULL;
Dave Chinnere7d236a2021-06-01 13:40:36 +1000296 bp->b_flags &= ~_XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Christoph Hellwig25a40952019-10-24 22:25:37 -0700299static void
Nathan Scottce8e9222006-01-11 15:39:08 +1100300xfs_buf_free(
Dave Chinnere8222612020-12-16 16:07:34 -0800301 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000303 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Dave Chinner430cbeb2010-12-02 16:30:55 +1100305 ASSERT(list_empty(&bp->b_lru));
306
Dave Chinnere7d236a2021-06-01 13:40:36 +1000307 if (bp->b_flags & _XBF_PAGES)
308 xfs_buf_free_pages(bp);
309 else if (bp->b_flags & _XBF_KMEM)
Dave Chinner0e6e8472011-03-26 09:16:45 +1100310 kmem_free(bp->b_addr);
Dave Chinnere7d236a2021-06-01 13:40:36 +1000311
Dave Chinner3e85c862012-06-22 18:50:09 +1000312 xfs_buf_free_maps(bp);
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800313 kmem_cache_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Dave Chinner0a683792021-06-01 13:40:02 +1000316static int
317xfs_buf_alloc_kmem(
318 struct xfs_buf *bp,
319 size_t size,
320 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Dave Chinner0a683792021-06-01 13:40:02 +1000322 int align_mask = xfs_buftarg_dma_alignment(bp->b_target);
323 xfs_km_flags_t kmflag_mask = KM_NOFS;
Bill O'Donnell3219e8c2019-10-04 16:38:44 -0700324
Dave Chinner0a683792021-06-01 13:40:02 +1000325 /* Assure zeroed buffer for non-read cases. */
326 if (!(flags & XBF_READ))
Bill O'Donnell3219e8c2019-10-04 16:38:44 -0700327 kmflag_mask |= KM_ZERO;
Dave Chinner0a683792021-06-01 13:40:02 +1000328
329 bp->b_addr = kmem_alloc_io(size, align_mask, kmflag_mask);
330 if (!bp->b_addr)
331 return -ENOMEM;
332
333 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
334 ((unsigned long)bp->b_addr & PAGE_MASK)) {
335 /* b_addr spans two pages - use alloc_page instead */
336 kmem_free(bp->b_addr);
337 bp->b_addr = NULL;
338 return -ENOMEM;
339 }
340 bp->b_offset = offset_in_page(bp->b_addr);
341 bp->b_pages = bp->b_page_array;
342 bp->b_pages[0] = kmem_to_page(bp->b_addr);
343 bp->b_page_count = 1;
344 bp->b_flags |= _XBF_KMEM;
345 return 0;
346}
347
348static int
349xfs_buf_alloc_pages(
350 struct xfs_buf *bp,
351 uint page_count,
352 xfs_buf_flags_t flags)
353{
354 gfp_t gfp_mask = xb_to_gfp(flags);
Dave Chinnerc9fa5632021-06-01 13:40:36 +1000355 long filled = 0;
Dave Chinner0a683792021-06-01 13:40:02 +1000356
Dave Chinner02c51172021-06-01 13:40:36 +1000357 /* Make sure that we have a page list */
358 bp->b_page_count = page_count;
359 if (bp->b_page_count <= XB_PAGES) {
360 bp->b_pages = bp->b_page_array;
361 } else {
362 bp->b_pages = kzalloc(sizeof(struct page *) * bp->b_page_count,
363 gfp_mask);
364 if (!bp->b_pages)
365 return -ENOMEM;
366 }
367 bp->b_flags |= _XBF_PAGES;
368
Dave Chinner0a683792021-06-01 13:40:02 +1000369 /* Assure zeroed buffer for non-read cases. */
370 if (!(flags & XBF_READ))
Bill O'Donnell3219e8c2019-10-04 16:38:44 -0700371 gfp_mask |= __GFP_ZERO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Dave Chinnerc9fa5632021-06-01 13:40:36 +1000373 /*
374 * Bulk filling of pages can take multiple calls. Not filling the entire
375 * array is not an allocation failure, so don't back off if we get at
376 * least one extra page.
377 */
378 for (;;) {
379 long last = filled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Dave Chinnerc9fa5632021-06-01 13:40:36 +1000381 filled = alloc_pages_bulk_array(gfp_mask, bp->b_page_count,
382 bp->b_pages);
383 if (filled == bp->b_page_count) {
384 XFS_STATS_INC(bp->b_mount, xb_page_found);
385 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
Dave Chinnerc9fa5632021-06-01 13:40:36 +1000388 if (filled != last)
389 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Dave Chinnerc9fa5632021-06-01 13:40:36 +1000391 if (flags & XBF_READ_AHEAD) {
Dave Chinnere7d236a2021-06-01 13:40:36 +1000392 xfs_buf_free_pages(bp);
393 return -ENOMEM;
Dave Chinnerc9fa5632021-06-01 13:40:36 +1000394 }
395
396 XFS_STATS_INC(bp->b_mount, xb_page_retries);
397 congestion_wait(BLK_RW_ASYNC, HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Dave Chinner0a683792021-06-01 13:40:02 +1000402
403/*
404 * Allocates all the pages for buffer in question and builds it's page list.
405 */
406static int
407xfs_buf_allocate_memory(
408 struct xfs_buf *bp,
409 uint flags)
410{
411 size_t size;
412 xfs_off_t start, end;
413 int error;
414
415 /*
416 * For buffers that fit entirely within a single page, first attempt to
417 * allocate the memory from the heap to minimise memory usage. If we
418 * can't get heap memory for these small buffers, we fall back to using
419 * the page allocator.
420 */
421 size = BBTOB(bp->b_length);
422 if (size < PAGE_SIZE) {
423 error = xfs_buf_alloc_kmem(bp, size, flags);
424 if (!error)
425 return 0;
426 }
427
428 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
429 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
430 >> PAGE_SHIFT;
431 return xfs_buf_alloc_pages(bp, end - start, flags);
432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300435 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
437STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100438_xfs_buf_map_pages(
Dave Chinnere8222612020-12-16 16:07:34 -0800439 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 uint flags)
441{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100442 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100443 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100444 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100445 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
Dave Chinner611c9942012-04-23 15:59:07 +1000446 } else if (flags & XBF_UNMAPPED) {
447 bp->b_addr = NULL;
448 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100449 int retried = 0;
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700450 unsigned nofs_flag;
Dave Chinnera19fb382011-03-26 09:13:42 +1100451
Dave Chinnerae687e52014-03-07 16:19:14 +1100452 /*
Joe Perchescf085a12019-11-07 13:24:52 -0800453 * vm_map_ram() will allocate auxiliary structures (e.g.
Dave Chinnerae687e52014-03-07 16:19:14 +1100454 * pagetables) with GFP_KERNEL, yet we are likely to be under
455 * GFP_NOFS context here. Hence we need to tell memory reclaim
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700456 * that we are in such a context via PF_MEMALLOC_NOFS to prevent
Dave Chinnerae687e52014-03-07 16:19:14 +1100457 * memory reclaim re-entering the filesystem here and
458 * potentially deadlocking.
459 */
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700460 nofs_flag = memalloc_nofs_save();
Dave Chinnera19fb382011-03-26 09:13:42 +1100461 do {
462 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
Christoph Hellwigd4efd792020-06-01 21:51:27 -0700463 -1);
Dave Chinnera19fb382011-03-26 09:13:42 +1100464 if (bp->b_addr)
465 break;
466 vm_unmap_aliases();
467 } while (retried++ <= 1);
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700468 memalloc_nofs_restore(nofs_flag);
Dave Chinnera19fb382011-03-26 09:13:42 +1100469
470 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100472 bp->b_addr += bp->b_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
475 return 0;
476}
477
478/*
479 * Finding and Reading Buffers
480 */
Lucas Stach6031e732016-12-07 17:36:36 +1100481static int
482_xfs_buf_obj_cmp(
483 struct rhashtable_compare_arg *arg,
484 const void *obj)
485{
486 const struct xfs_buf_map *map = arg->key;
487 const struct xfs_buf *bp = obj;
488
489 /*
490 * The key hashing in the lookup path depends on the key being the
491 * first element of the compare_arg, make sure to assert this.
492 */
493 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
494
495 if (bp->b_bn != map->bm_bn)
496 return 1;
497
498 if (unlikely(bp->b_length != map->bm_len)) {
499 /*
500 * found a block number match. If the range doesn't
501 * match, the only way this is allowed is if the buffer
502 * in the cache is stale and the transaction that made
503 * it stale has not yet committed. i.e. we are
504 * reallocating a busy extent. Skip this buffer and
505 * continue searching for an exact match.
506 */
507 ASSERT(bp->b_flags & XBF_STALE);
508 return 1;
509 }
510 return 0;
511}
512
513static const struct rhashtable_params xfs_buf_hash_params = {
514 .min_size = 32, /* empty AGs have minimal footprint */
515 .nelem_hint = 16,
516 .key_len = sizeof(xfs_daddr_t),
517 .key_offset = offsetof(struct xfs_buf, b_bn),
518 .head_offset = offsetof(struct xfs_buf, b_rhash_head),
519 .automatic_shrinking = true,
520 .obj_cmpfn = _xfs_buf_obj_cmp,
521};
522
523int
524xfs_buf_hash_init(
525 struct xfs_perag *pag)
526{
527 spin_lock_init(&pag->pag_buf_lock);
528 return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
529}
530
531void
532xfs_buf_hash_destroy(
533 struct xfs_perag *pag)
534{
535 rhashtable_destroy(&pag->pag_buf_hash);
536}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538/*
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700539 * Look up a buffer in the buffer cache and return it referenced and locked
540 * in @found_bp.
541 *
542 * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the
543 * cache.
544 *
545 * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return
546 * -EAGAIN if we fail to lock it.
547 *
548 * Return values are:
549 * -EFSCORRUPTED if have been supplied with an invalid address
550 * -EAGAIN on trylock failure
551 * -ENOENT if we fail to find a match and @new_bp was NULL
552 * 0, with @found_bp:
553 * - @new_bp if we inserted it into the cache
554 * - the buffer we found and locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700556static int
557xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000558 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000559 struct xfs_buf_map *map,
560 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100561 xfs_buf_flags_t flags,
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700562 struct xfs_buf *new_bp,
563 struct xfs_buf **found_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Dave Chinner74f75a02010-09-24 19:59:04 +1000565 struct xfs_perag *pag;
Dave Chinnere8222612020-12-16 16:07:34 -0800566 struct xfs_buf *bp;
Lucas Stach6031e732016-12-07 17:36:36 +1100567 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
Dave Chinner10616b82013-01-21 23:53:52 +1100568 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000569 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700571 *found_bp = NULL;
572
Dave Chinner3e85c862012-06-22 18:50:09 +1000573 for (i = 0; i < nmaps; i++)
Lucas Stach6031e732016-12-07 17:36:36 +1100574 cmap.bm_len += map[i].bm_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 /* Check for IOs smaller than the sector size / not sector aligned */
Lucas Stach6031e732016-12-07 17:36:36 +1100577 ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
578 ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Dave Chinner10616b82013-01-21 23:53:52 +1100580 /*
581 * Corrupted block numbers can get through to here, unfortunately, so we
582 * have to check that the buffer falls within the filesystem bounds.
583 */
584 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
Lucas Stach6031e732016-12-07 17:36:36 +1100585 if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
Dave Chinner10616b82013-01-21 23:53:52 +1100586 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -0800587 "%s: daddr 0x%llx out of range, EOFS 0x%llx",
Lucas Stach6031e732016-12-07 17:36:36 +1100588 __func__, cmap.bm_bn, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000589 WARN_ON(1);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700590 return -EFSCORRUPTED;
Dave Chinner10616b82013-01-21 23:53:52 +1100591 }
592
Dave Chinner74f75a02010-09-24 19:59:04 +1000593 pag = xfs_perag_get(btp->bt_mount,
Lucas Stach6031e732016-12-07 17:36:36 +1100594 xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Dave Chinner74f75a02010-09-24 19:59:04 +1000596 spin_lock(&pag->pag_buf_lock);
Lucas Stach6031e732016-12-07 17:36:36 +1100597 bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
598 xfs_buf_hash_params);
599 if (bp) {
600 atomic_inc(&bp->b_hold);
601 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
604 /* No match found */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700605 if (!new_bp) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100606 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000607 spin_unlock(&pag->pag_buf_lock);
608 xfs_perag_put(pag);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700609 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700611
612 /* the buffer keeps the perag reference until it is freed */
613 new_bp->b_pag = pag;
614 rhashtable_insert_fast(&pag->pag_buf_hash, &new_bp->b_rhash_head,
615 xfs_buf_hash_params);
616 spin_unlock(&pag->pag_buf_lock);
617 *found_bp = new_bp;
618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000621 spin_unlock(&pag->pag_buf_lock);
622 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200624 if (!xfs_buf_trylock(bp)) {
625 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100626 xfs_buf_rele(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100627 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700628 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200630 xfs_buf_lock(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100631 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
Dave Chinner0e6e8472011-03-26 09:16:45 +1100634 /*
635 * if the buffer is stale, clear all the external state associated with
636 * it. We need to keep flags such as how we allocated the buffer memory
637 * intact here.
638 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100639 if (bp->b_flags & XBF_STALE) {
640 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner611c9942012-04-23 15:59:07 +1000641 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100642 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000643 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000644
645 trace_xfs_buf_find(bp, flags, _RET_IP_);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100646 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700647 *found_bp = bp;
648 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Dave Chinner8925a3d2018-04-18 08:25:20 -0700651struct xfs_buf *
652xfs_buf_incore(
653 struct xfs_buftarg *target,
654 xfs_daddr_t blkno,
655 size_t numblks,
656 xfs_buf_flags_t flags)
657{
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700658 struct xfs_buf *bp;
659 int error;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700660 DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700661
662 error = xfs_buf_find(target, &map, 1, flags, NULL, &bp);
663 if (error)
664 return NULL;
665 return bp;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668/*
Dave Chinner38158322011-09-30 04:45:02 +0000669 * Assembles a buffer covering the specified range. The code is optimised for
670 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
671 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 */
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800673int
Dave Chinner6dde2702012-06-22 18:50:10 +1000674xfs_buf_get_map(
675 struct xfs_buftarg *target,
676 struct xfs_buf_map *map,
677 int nmaps,
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800678 xfs_buf_flags_t flags,
679 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Dave Chinner38158322011-09-30 04:45:02 +0000681 struct xfs_buf *bp;
682 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100683 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800685 *bpp = NULL;
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700686 error = xfs_buf_find(target, map, nmaps, flags, NULL, &bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800687 if (!error)
Dave Chinner38158322011-09-30 04:45:02 +0000688 goto found;
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800689 if (error != -ENOENT)
690 return error;
Dave Chinner38158322011-09-30 04:45:02 +0000691
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800692 error = _xfs_buf_alloc(target, map, nmaps, flags, &new_bp);
693 if (error)
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800694 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000696 error = xfs_buf_allocate_memory(new_bp, flags);
697 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000698 xfs_buf_free(new_bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800699 return error;
Dave Chinner38158322011-09-30 04:45:02 +0000700 }
701
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700702 error = xfs_buf_find(target, map, nmaps, flags, new_bp, &bp);
703 if (error) {
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000704 xfs_buf_free(new_bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800705 return error;
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000706 }
707
708 if (bp != new_bp)
709 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Dave Chinner38158322011-09-30 04:45:02 +0000711found:
Dave Chinner611c9942012-04-23 15:59:07 +1000712 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100713 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (unlikely(error)) {
Darrick J. Wong93baa552020-02-21 07:40:44 -0800715 xfs_warn_ratelimited(target->bt_mount,
716 "%s: failed to map %u pages", __func__,
717 bp->b_page_count);
Dave Chinnera8acad72012-04-23 15:58:54 +1000718 xfs_buf_relse(bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800719 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 }
722
Dave Chinnerb79f4a12016-01-12 07:03:44 +1100723 /*
724 * Clear b_error if this is a lookup from a caller that doesn't expect
725 * valid data to be found in the buffer.
726 */
727 if (!(flags & XBF_READ))
728 xfs_buf_ioerror(bp, 0);
729
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100730 XFS_STATS_INC(target->bt_mount, xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000731 trace_xfs_buf_get(bp, flags, _RET_IP_);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800732 *bpp = bp;
733 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Christoph Hellwig26e328752020-09-01 10:55:47 -0700736int
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100737_xfs_buf_read(
Dave Chinnere8222612020-12-16 16:07:34 -0800738 struct xfs_buf *bp,
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100739 xfs_buf_flags_t flags)
740{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000741 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600742 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100743
Christoph Hellwig26e328752020-09-01 10:55:47 -0700744 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD | XBF_DONE);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200745 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100746
Brian Foster6af88cd2018-07-11 22:26:35 -0700747 return xfs_buf_submit(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100748}
749
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100750/*
Brian Foster75d02302019-02-06 09:25:29 -0800751 * Reverify a buffer found in cache without an attached ->b_ops.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800752 *
Brian Foster75d02302019-02-06 09:25:29 -0800753 * If the caller passed an ops structure and the buffer doesn't have ops
754 * assigned, set the ops and use it to verify the contents. If verification
755 * fails, clear XBF_DONE. We assume the buffer has no recorded errors and is
756 * already in XBF_DONE state on entry.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800757 *
Brian Foster75d02302019-02-06 09:25:29 -0800758 * Under normal operations, every in-core buffer is verified on read I/O
759 * completion. There are two scenarios that can lead to in-core buffers without
760 * an assigned ->b_ops. The first is during log recovery of buffers on a V4
761 * filesystem, though these buffers are purged at the end of recovery. The
762 * other is online repair, which intentionally reads with a NULL buffer ops to
763 * run several verifiers across an in-core buffer in order to establish buffer
764 * type. If repair can't establish that, the buffer will be left in memory
765 * with NULL buffer ops.
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100766 */
767int
Brian Foster75d02302019-02-06 09:25:29 -0800768xfs_buf_reverify(
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100769 struct xfs_buf *bp,
770 const struct xfs_buf_ops *ops)
771{
772 ASSERT(bp->b_flags & XBF_DONE);
773 ASSERT(bp->b_error == 0);
774
775 if (!ops || bp->b_ops)
776 return 0;
777
778 bp->b_ops = ops;
779 bp->b_ops->verify_read(bp);
780 if (bp->b_error)
781 bp->b_flags &= ~XBF_DONE;
782 return bp->b_error;
783}
784
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800785int
Dave Chinner6dde2702012-06-22 18:50:10 +1000786xfs_buf_read_map(
787 struct xfs_buftarg *target,
788 struct xfs_buf_map *map,
789 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100790 xfs_buf_flags_t flags,
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800791 struct xfs_buf **bpp,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800792 const struct xfs_buf_ops *ops,
793 xfs_failaddr_t fa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Dave Chinner6dde2702012-06-22 18:50:10 +1000795 struct xfs_buf *bp;
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800796 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Nathan Scottce8e9222006-01-11 15:39:08 +1100798 flags |= XBF_READ;
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800799 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800801 error = xfs_buf_get_map(target, map, nmaps, flags, &bp);
802 if (error)
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800803 return error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000804
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100805 trace_xfs_buf_read(bp, flags, _RET_IP_);
806
807 if (!(bp->b_flags & XBF_DONE)) {
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800808 /* Initiate the buffer read and wait. */
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100809 XFS_STATS_INC(target->bt_mount, xb_get_read);
810 bp->b_ops = ops;
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800811 error = _xfs_buf_read(bp, flags);
812
813 /* Readahead iodone already dropped the buffer, so exit. */
814 if (flags & XBF_ASYNC)
815 return 0;
816 } else {
817 /* Buffer already read; all we need to do is check it. */
818 error = xfs_buf_reverify(bp, ops);
819
820 /* Readahead already finished; drop the buffer and exit. */
821 if (flags & XBF_ASYNC) {
822 xfs_buf_relse(bp);
823 return 0;
824 }
825
826 /* We do not want read in the flags */
827 bp->b_flags &= ~XBF_READ;
828 ASSERT(bp->b_ops != NULL || ops == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800831 /*
832 * If we've had a read error, then the contents of the buffer are
833 * invalid and should not be used. To ensure that a followup read tries
834 * to pull the buffer from disk again, we clear the XBF_DONE flag and
835 * mark the buffer stale. This ensures that anyone who has a current
836 * reference to the buffer will interpret it's contents correctly and
837 * future cache lookups will also treat it as an empty, uninitialised
838 * buffer.
839 */
840 if (error) {
841 if (!XFS_FORCED_SHUTDOWN(target->bt_mount))
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800842 xfs_buf_ioerror_alert(bp, fa);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100843
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800844 bp->b_flags &= ~XBF_DONE;
845 xfs_buf_stale(bp);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100846 xfs_buf_relse(bp);
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800847
848 /* bad CRC means corrupted metadata */
849 if (error == -EFSBADCRC)
850 error = -EFSCORRUPTED;
851 return error;
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100852 }
853
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800854 *bpp = bp;
855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100859 * If we are not low on memory then do the readahead in a deadlock
860 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 */
862void
Dave Chinner6dde2702012-06-22 18:50:10 +1000863xfs_buf_readahead_map(
864 struct xfs_buftarg *target,
865 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100866 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100867 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800869 struct xfs_buf *bp;
870
Jan Karaefa7c9f2017-02-02 15:56:53 +0100871 if (bdi_read_congested(target->bt_bdev->bd_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return;
873
Dave Chinner6dde2702012-06-22 18:50:10 +1000874 xfs_buf_read_map(target, map, nmaps,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800875 XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD, &bp, ops,
876 __this_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Dave Chinner5adc94c2010-09-24 21:58:31 +1000879/*
880 * Read an uncached buffer from disk. Allocates and returns a locked
881 * buffer containing the disk contents or nothing.
882 */
Dave Chinnerba3726742014-10-02 09:05:32 +1000883int
Dave Chinner5adc94c2010-09-24 21:58:31 +1000884xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000885 struct xfs_buftarg *target,
886 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000887 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100888 int flags,
Dave Chinnerba3726742014-10-02 09:05:32 +1000889 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100890 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000891{
Dave Chinnereab4e632012-11-12 22:54:02 +1100892 struct xfs_buf *bp;
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800893 int error;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000894
Dave Chinnerba3726742014-10-02 09:05:32 +1000895 *bpp = NULL;
896
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800897 error = xfs_buf_get_uncached(target, numblks, flags, &bp);
898 if (error)
899 return error;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000900
901 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000902 ASSERT(bp->b_map_count == 1);
Dave Chinnerba3726742014-10-02 09:05:32 +1000903 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
Dave Chinner3e85c862012-06-22 18:50:09 +1000904 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000905 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100906 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000907
Brian Foster6af88cd2018-07-11 22:26:35 -0700908 xfs_buf_submit(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000909 if (bp->b_error) {
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800910 error = bp->b_error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800911 xfs_buf_relse(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000912 return error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800913 }
Dave Chinnerba3726742014-10-02 09:05:32 +1000914
915 *bpp = bp;
916 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800919int
Dave Chinner686865f2010-09-24 20:07:47 +1000920xfs_buf_get_uncached(
921 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000922 size_t numblks,
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800923 int flags,
924 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000926 unsigned long page_count;
Dave Chinner07b5c5a2021-06-01 13:40:35 +1000927 int error;
Dave Chinner3e85c862012-06-22 18:50:09 +1000928 struct xfs_buf *bp;
929 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800931 *bpp = NULL;
932
Brian Fosterc891c302016-07-20 11:13:43 +1000933 /* flags might contain irrelevant bits, pass only what we care about */
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800934 error = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT, &bp);
935 if (error)
Dave Chinner07b5c5a2021-06-01 13:40:35 +1000936 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Dave Chinnere70b73f2012-04-23 15:58:49 +1000938 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Dave Chinner07b5c5a2021-06-01 13:40:35 +1000939 error = xfs_buf_alloc_pages(bp, page_count, flags);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000940 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 goto fail_free_buf;
942
Dave Chinner611c9942012-04-23 15:59:07 +1000943 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000944 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100945 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500946 "%s: failed to map pages", __func__);
Dave Chinner07b5c5a2021-06-01 13:40:35 +1000947 goto fail_free_buf;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Dave Chinner686865f2010-09-24 20:07:47 +1000950 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800951 *bpp = bp;
952 return 0;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000953
Dave Chinner07b5c5a2021-06-01 13:40:35 +1000954fail_free_buf:
955 xfs_buf_free(bp);
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800956 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
959/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 * Increment reference count on buffer, to hold the buffer concurrently
961 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * Must hold the buffer already to call this function.
963 */
964void
Nathan Scottce8e9222006-01-11 15:39:08 +1100965xfs_buf_hold(
Dave Chinnere8222612020-12-16 16:07:34 -0800966 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000968 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100969 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972/*
Brian Foster9c7504a2016-07-20 11:15:28 +1000973 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
974 * placed on LRU or freed (depending on b_lru_ref).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 */
976void
Nathan Scottce8e9222006-01-11 15:39:08 +1100977xfs_buf_rele(
Dave Chinnere8222612020-12-16 16:07:34 -0800978 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Dave Chinner74f75a02010-09-24 19:59:04 +1000980 struct xfs_perag *pag = bp->b_pag;
Brian Foster9c7504a2016-07-20 11:15:28 +1000981 bool release;
982 bool freebuf = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000984 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Dave Chinner74f75a02010-09-24 19:59:04 +1000986 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100987 ASSERT(list_empty(&bp->b_lru));
Brian Foster9c7504a2016-07-20 11:15:28 +1000988 if (atomic_dec_and_test(&bp->b_hold)) {
989 xfs_buf_ioacct_dec(bp);
Nathan Scottfad3aa12006-02-01 12:14:52 +1100990 xfs_buf_free(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +1000991 }
Nathan Scottfad3aa12006-02-01 12:14:52 +1100992 return;
993 }
994
Lachlan McIlroy37906892008-08-13 15:42:10 +1000995 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinnera4082352013-08-28 10:18:06 +1000996
Dave Chinner37fd1672018-10-18 17:21:29 +1100997 /*
998 * We grab the b_lock here first to serialise racing xfs_buf_rele()
999 * calls. The pag_buf_lock being taken on the last reference only
1000 * serialises against racing lookups in xfs_buf_find(). IOWs, the second
1001 * to last reference we drop here is not serialised against the last
1002 * reference until we take bp->b_lock. Hence if we don't grab b_lock
1003 * first, the last "release" reference can win the race to the lock and
1004 * free the buffer before the second-to-last reference is processed,
1005 * leading to a use-after-free scenario.
1006 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001007 spin_lock(&bp->b_lock);
Dave Chinner37fd1672018-10-18 17:21:29 +11001008 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +10001009 if (!release) {
1010 /*
1011 * Drop the in-flight state if the buffer is already on the LRU
1012 * and it holds the only reference. This is racy because we
1013 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1014 * ensures the decrement occurs only once per-buf.
1015 */
1016 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
Brian Foster63db7c82017-05-31 08:22:52 -07001017 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001018 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
Brian Foster9c7504a2016-07-20 11:15:28 +10001020
1021 /* the last reference has been dropped ... */
Brian Foster63db7c82017-05-31 08:22:52 -07001022 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001023 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1024 /*
1025 * If the buffer is added to the LRU take a new reference to the
1026 * buffer for the LRU and clear the (now stale) dispose list
1027 * state flag
1028 */
1029 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1030 bp->b_state &= ~XFS_BSTATE_DISPOSE;
1031 atomic_inc(&bp->b_hold);
1032 }
1033 spin_unlock(&pag->pag_buf_lock);
1034 } else {
1035 /*
1036 * most of the time buffers will already be removed from the
1037 * LRU, so optimise that case by checking for the
1038 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1039 * was on was the disposal list
1040 */
1041 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1042 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1043 } else {
1044 ASSERT(list_empty(&bp->b_lru));
1045 }
1046
1047 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Lucas Stach6031e732016-12-07 17:36:36 +11001048 rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
1049 xfs_buf_hash_params);
Brian Foster9c7504a2016-07-20 11:15:28 +10001050 spin_unlock(&pag->pag_buf_lock);
1051 xfs_perag_put(pag);
1052 freebuf = true;
1053 }
1054
1055out_unlock:
1056 spin_unlock(&bp->b_lock);
1057
1058 if (freebuf)
1059 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062
1063/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001064 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +11001065 *
1066 * If we come across a stale, pinned, locked buffer, we know that we are
1067 * being asked to lock a buffer that has been reallocated. Because it is
1068 * pinned, we know that the log has not been pushed to disk and hence it
1069 * will still be locked. Rather than continuing to have trylock attempts
1070 * fail until someone else pushes the log, push it ourselves before
1071 * returning. This means that the xfsaild will not get stuck trying
1072 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 */
1074int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001075xfs_buf_trylock(
1076 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 int locked;
1079
Nathan Scottce8e9222006-01-11 15:39:08 +11001080 locked = down_trylock(&bp->b_sema) == 0;
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001081 if (locked)
Darrick J. Wong479c6412016-06-21 11:53:28 +10001082 trace_xfs_buf_trylock(bp, _RET_IP_);
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001083 else
Darrick J. Wong479c6412016-06-21 11:53:28 +10001084 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001085 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001089 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +10001090 *
1091 * If we come across a stale, pinned, locked buffer, we know that we
1092 * are being asked to lock a buffer that has been reallocated. Because
1093 * it is pinned, we know that the log has not been pushed to disk and
1094 * hence it will still be locked. Rather than sleeping until someone
1095 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001097void
1098xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001099 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001101 trace_xfs_buf_lock(bp, _RET_IP_);
1102
Dave Chinnered3b4d62010-05-21 12:07:08 +10001103 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001104 xfs_log_force(bp->b_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +11001105 down(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001106
1107 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110void
Nathan Scottce8e9222006-01-11 15:39:08 +11001111xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001112 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Brian Foster20e8a062017-04-21 12:40:44 -07001114 ASSERT(xfs_buf_islocked(bp));
1115
Nathan Scottce8e9222006-01-11 15:39:08 +11001116 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001117 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Nathan Scottce8e9222006-01-11 15:39:08 +11001120STATIC void
1121xfs_buf_wait_unpin(
Dave Chinnere8222612020-12-16 16:07:34 -08001122 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 DECLARE_WAITQUEUE (wait, current);
1125
Nathan Scottce8e9222006-01-11 15:39:08 +11001126 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return;
1128
Nathan Scottce8e9222006-01-11 15:39:08 +11001129 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 for (;;) {
1131 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001132 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001134 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001136 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 set_current_state(TASK_RUNNING);
1138}
1139
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001140static void
1141xfs_buf_ioerror_alert_ratelimited(
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001142 struct xfs_buf *bp)
1143{
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001144 static unsigned long lasttime;
1145 static struct xfs_buftarg *lasttarg;
1146
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001147 if (bp->b_target != lasttarg ||
1148 time_after(jiffies, (lasttime + 5*HZ))) {
1149 lasttime = jiffies;
1150 xfs_buf_ioerror_alert(bp, __this_address);
1151 }
1152 lasttarg = bp->b_target;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001153}
1154
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001155/*
1156 * Account for this latest trip around the retry handler, and decide if
1157 * we've failed enough times to constitute a permanent failure.
1158 */
1159static bool
1160xfs_buf_ioerror_permanent(
1161 struct xfs_buf *bp,
1162 struct xfs_error_cfg *cfg)
1163{
1164 struct xfs_mount *mp = bp->b_mount;
1165
1166 if (cfg->max_retries != XFS_ERR_RETRY_FOREVER &&
1167 ++bp->b_retries > cfg->max_retries)
1168 return true;
1169 if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
1170 time_after(jiffies, cfg->retry_timeout + bp->b_first_retry_time))
1171 return true;
1172
1173 /* At unmount we may treat errors differently */
1174 if ((mp->m_flags & XFS_MOUNT_UNMOUNTING) && mp->m_fail_unmount)
1175 return true;
1176
1177 return false;
1178}
1179
1180/*
1181 * On a sync write or shutdown we just want to stale the buffer and let the
1182 * caller handle the error in bp->b_error appropriately.
1183 *
1184 * If the write was asynchronous then no one will be looking for the error. If
1185 * this is the first failure of this type, clear the error state and write the
1186 * buffer out again. This means we always retry an async write failure at least
1187 * once, but we also need to set the buffer up to behave correctly now for
1188 * repeated failures.
1189 *
1190 * If we get repeated async write failures, then we take action according to the
1191 * error configuration we have been set up to use.
1192 *
Christoph Hellwig70796c62020-09-01 10:55:45 -07001193 * Returns true if this function took care of error handling and the caller must
1194 * not touch the buffer again. Return false if the caller should proceed with
1195 * normal I/O completion handling.
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001196 */
Christoph Hellwig70796c62020-09-01 10:55:45 -07001197static bool
1198xfs_buf_ioend_handle_error(
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001199 struct xfs_buf *bp)
1200{
1201 struct xfs_mount *mp = bp->b_mount;
1202 struct xfs_error_cfg *cfg;
1203
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001204 /*
1205 * If we've already decided to shutdown the filesystem because of I/O
1206 * errors, there's no point in giving this a retry.
1207 */
1208 if (XFS_FORCED_SHUTDOWN(mp))
1209 goto out_stale;
1210
1211 xfs_buf_ioerror_alert_ratelimited(bp);
1212
1213 /*
Christoph Hellwig22c10582020-09-01 10:55:46 -07001214 * We're not going to bother about retrying this during recovery.
1215 * One strike!
1216 */
1217 if (bp->b_flags & _XBF_LOGRECOVERY) {
1218 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1219 return false;
1220 }
1221
1222 /*
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001223 * Synchronous writes will have callers process the error.
1224 */
1225 if (!(bp->b_flags & XBF_ASYNC))
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001226 goto out_stale;
1227
1228 trace_xfs_buf_iodone_async(bp, _RET_IP_);
1229
1230 cfg = xfs_error_get_cfg(mp, XFS_ERR_METADATA, bp->b_error);
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001231 if (bp->b_last_error != bp->b_error ||
1232 !(bp->b_flags & (XBF_STALE | XBF_WRITE_FAIL))) {
1233 bp->b_last_error = bp->b_error;
1234 if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
1235 !bp->b_first_retry_time)
1236 bp->b_first_retry_time = jiffies;
1237 goto resubmit;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001238 }
1239
1240 /*
1241 * Permanent error - we need to trigger a shutdown if we haven't already
1242 * to indicate that inconsistency will result from this action.
1243 */
1244 if (xfs_buf_ioerror_permanent(bp, cfg)) {
1245 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1246 goto out_stale;
1247 }
1248
1249 /* Still considered a transient error. Caller will schedule retries. */
Christoph Hellwig844c9352020-09-01 10:55:45 -07001250 if (bp->b_flags & _XBF_INODES)
1251 xfs_buf_inode_io_fail(bp);
1252 else if (bp->b_flags & _XBF_DQUOTS)
1253 xfs_buf_dquot_io_fail(bp);
1254 else
1255 ASSERT(list_empty(&bp->b_li_list));
1256 xfs_buf_ioerror(bp, 0);
1257 xfs_buf_relse(bp);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001258 return true;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001259
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001260resubmit:
1261 xfs_buf_ioerror(bp, 0);
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001262 bp->b_flags |= (XBF_DONE | XBF_WRITE_FAIL);
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001263 xfs_buf_submit(bp);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001264 return true;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001265out_stale:
1266 xfs_buf_stale(bp);
1267 bp->b_flags |= XBF_DONE;
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001268 bp->b_flags &= ~XBF_WRITE;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001269 trace_xfs_buf_error_relse(bp, _RET_IP_);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001270 return false;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001271}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Christoph Hellwig76b2d322020-09-01 10:55:20 -07001273static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001274xfs_buf_ioend(
1275 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001277 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001278
Dave Chinner61be9c52014-10-02 09:04:31 +10001279 /*
1280 * Pull in IO completion errors now. We are guaranteed to be running
1281 * single threaded, so we don't need the lock to read b_io_error.
1282 */
1283 if (!bp->b_error && bp->b_io_error)
1284 xfs_buf_ioerror(bp, bp->b_io_error);
1285
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001286 if (bp->b_flags & XBF_READ) {
Dave Chinnerb01d1462020-06-29 14:48:47 -07001287 if (!bp->b_error && bp->b_ops)
1288 bp->b_ops->verify_read(bp);
1289 if (!bp->b_error)
1290 bp->b_flags |= XBF_DONE;
Christoph Hellwig23fb5a92020-09-01 10:55:20 -07001291 } else {
1292 if (!bp->b_error) {
1293 bp->b_flags &= ~XBF_WRITE_FAIL;
1294 bp->b_flags |= XBF_DONE;
1295 }
Dave Chinner9fe5c772020-06-29 14:48:47 -07001296
Christoph Hellwig70796c62020-09-01 10:55:45 -07001297 if (unlikely(bp->b_error) && xfs_buf_ioend_handle_error(bp))
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001298 return;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001299
1300 /* clear the retry state */
1301 bp->b_last_error = 0;
1302 bp->b_retries = 0;
1303 bp->b_first_retry_time = 0;
1304
1305 /*
1306 * Note that for things like remote attribute buffers, there may
1307 * not be a buffer log item here, so processing the buffer log
1308 * item must remain optional.
1309 */
1310 if (bp->b_log_item)
1311 xfs_buf_item_done(bp);
1312
Christoph Hellwig23fb5a92020-09-01 10:55:20 -07001313 if (bp->b_flags & _XBF_INODES)
1314 xfs_buf_inode_iodone(bp);
1315 else if (bp->b_flags & _XBF_DQUOTS)
1316 xfs_buf_dquot_iodone(bp);
Christoph Hellwig22c10582020-09-01 10:55:46 -07001317
Dave Chinnerf593bf12020-06-29 14:48:46 -07001318 }
Christoph Hellwig6a7584b2020-09-01 10:55:44 -07001319
Christoph Hellwig22c10582020-09-01 10:55:46 -07001320 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD |
1321 _XBF_LOGRECOVERY);
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001322
Christoph Hellwig6a7584b2020-09-01 10:55:44 -07001323 if (bp->b_flags & XBF_ASYNC)
1324 xfs_buf_relse(bp);
1325 else
1326 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Dave Chinnere8aaba92014-10-02 09:04:22 +10001329static void
1330xfs_buf_ioend_work(
1331 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001333 struct xfs_buf *bp =
Dave Chinnere8222612020-12-16 16:07:34 -08001334 container_of(work, struct xfs_buf, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001335
Dave Chinnere8aaba92014-10-02 09:04:22 +10001336 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001339static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001340xfs_buf_ioend_async(
1341 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001343 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001344 queue_work(bp->b_mount->m_buf_workqueue, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347void
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001348__xfs_buf_ioerror(
Dave Chinnere8222612020-12-16 16:07:34 -08001349 struct xfs_buf *bp,
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001350 int error,
1351 xfs_failaddr_t failaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Dave Chinner24513372014-06-25 14:58:08 +10001353 ASSERT(error <= 0 && error >= -1000);
1354 bp->b_error = error;
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001355 trace_xfs_buf_ioerror(bp, error, failaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Christoph Hellwig901796a2011-10-10 16:52:49 +00001358void
1359xfs_buf_ioerror_alert(
1360 struct xfs_buf *bp,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -08001361 xfs_failaddr_t func)
Christoph Hellwig901796a2011-10-10 16:52:49 +00001362{
Brian Fosterf9bccfc2020-05-06 13:25:21 -07001363 xfs_buf_alert_ratelimited(bp, "XFS: metadata IO error",
1364 "metadata I/O error in \"%pS\" at daddr 0x%llx len %d error %d",
1365 func, (uint64_t)XFS_BUF_ADDR(bp),
1366 bp->b_length, -bp->b_error);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001367}
1368
Brian Foster54b3b1f2020-05-06 13:25:19 -07001369/*
1370 * To simulate an I/O failure, the buffer must be locked and held with at least
1371 * three references. The LRU reference is dropped by the stale call. The buf
1372 * item reference is dropped via ioend processing. The third reference is owned
1373 * by the caller and is dropped on I/O completion if the buffer is XBF_ASYNC.
1374 */
1375void
1376xfs_buf_ioend_fail(
1377 struct xfs_buf *bp)
1378{
1379 bp->b_flags &= ~XBF_DONE;
1380 xfs_buf_stale(bp);
1381 xfs_buf_ioerror(bp, -EIO);
1382 xfs_buf_ioend(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001383}
Christoph Hellwig939d7232010-07-20 17:51:16 +10001384
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001385int
1386xfs_bwrite(
1387 struct xfs_buf *bp)
1388{
1389 int error;
1390
1391 ASSERT(xfs_buf_islocked(bp));
1392
1393 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001394 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
Brian Fosterb6983e82020-05-06 13:25:20 -07001395 XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001396
Brian Foster6af88cd2018-07-11 22:26:35 -07001397 error = xfs_buf_submit(bp);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001398 if (error)
1399 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001400 return error;
1401}
1402
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001403static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001404xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001405 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001407 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Brian Foster7376d742020-05-06 13:29:19 -07001409 if (!bio->bi_status &&
1410 (bp->b_flags & XBF_WRITE) && (bp->b_flags & XBF_ASYNC) &&
Brian Foster43dc0aa2020-05-08 08:50:52 -07001411 XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_IOERROR))
Brian Foster7376d742020-05-06 13:29:19 -07001412 bio->bi_status = BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Dave Chinner37eb17e2012-11-12 22:09:46 +11001414 /*
1415 * don't overwrite existing errors - otherwise we can lose errors on
1416 * buffers that require multiple bios to complete.
1417 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001418 if (bio->bi_status) {
1419 int error = blk_status_to_errno(bio->bi_status);
1420
1421 cmpxchg(&bp->b_io_error, 0, error);
1422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Dave Chinner37eb17e2012-11-12 22:09:46 +11001424 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001425 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1426
Dave Chinnere8aaba92014-10-02 09:04:22 +10001427 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1428 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Dave Chinner3e85c862012-06-22 18:50:09 +10001432static void
1433xfs_buf_ioapply_map(
1434 struct xfs_buf *bp,
1435 int map,
1436 int *buf_offset,
1437 int *count,
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001438 int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Dave Chinner3e85c862012-06-22 18:50:09 +10001440 int page_index;
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +00001441 unsigned int total_nr_pages = bp->b_page_count;
Dave Chinner3e85c862012-06-22 18:50:09 +10001442 int nr_pages;
1443 struct bio *bio;
1444 sector_t sector = bp->b_maps[map].bm_bn;
1445 int size;
1446 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Dave Chinner3e85c862012-06-22 18:50:09 +10001448 /* skip the pages in the buffer before the start offset */
1449 page_index = 0;
1450 offset = *buf_offset;
1451 while (offset >= PAGE_SIZE) {
1452 page_index++;
1453 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001454 }
1455
Dave Chinner3e85c862012-06-22 18:50:09 +10001456 /*
1457 * Limit the IO size to the length of the current vector, and update the
1458 * remaining IO count for the next time around.
1459 */
1460 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1461 *count -= size;
1462 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001465 atomic_inc(&bp->b_io_remaining);
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +00001466 nr_pages = bio_max_segs(total_nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 bio = bio_alloc(GFP_NOIO, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001469 bio_set_dev(bio, bp->b_target->bt_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001470 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001471 bio->bi_end_io = xfs_buf_bio_end_io;
1472 bio->bi_private = bp;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001473 bio->bi_opf = op;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001474
Dave Chinner3e85c862012-06-22 18:50:09 +10001475 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001476 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 if (nbytes > size)
1479 nbytes = size;
1480
Dave Chinner3e85c862012-06-22 18:50:09 +10001481 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1482 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001483 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 break;
1485
1486 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001487 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 size -= nbytes;
1489 total_nr_pages--;
1490 }
1491
Kent Overstreet4f024f32013-10-11 15:44:27 -07001492 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001493 if (xfs_buf_is_vmapped(bp)) {
1494 flush_kernel_vmap_range(bp->b_addr,
1495 xfs_buf_vmap_len(bp));
1496 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001497 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (size)
1499 goto next_chunk;
1500 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001501 /*
1502 * This is guaranteed not to be the last io reference count
Dave Chinner595bff72014-10-02 09:05:14 +10001503 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001504 */
1505 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001506 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001507 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001509
1510}
1511
1512STATIC void
1513_xfs_buf_ioapply(
1514 struct xfs_buf *bp)
1515{
1516 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001517 int op;
Dave Chinner3e85c862012-06-22 18:50:09 +10001518 int offset;
1519 int size;
1520 int i;
1521
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001522 /*
1523 * Make sure we capture only current IO errors rather than stale errors
1524 * left over from previous use of the buffer (e.g. failed readahead).
1525 */
1526 bp->b_error = 0;
1527
Dave Chinner3e85c862012-06-22 18:50:09 +10001528 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001529 op = REQ_OP_WRITE;
Dave Chinner1813dd62012-11-14 17:54:40 +11001530
1531 /*
1532 * Run the write verifier callback function if it exists. If
1533 * this function fails it will mark the buffer with an error and
1534 * the IO should not be dispatched.
1535 */
1536 if (bp->b_ops) {
1537 bp->b_ops->verify_write(bp);
1538 if (bp->b_error) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001539 xfs_force_shutdown(bp->b_mount,
Dave Chinner1813dd62012-11-14 17:54:40 +11001540 SHUTDOWN_CORRUPT_INCORE);
1541 return;
1542 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001543 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001544 struct xfs_mount *mp = bp->b_mount;
Dave Chinner400b9d82014-08-04 12:42:40 +10001545
1546 /*
1547 * non-crc filesystems don't attach verifiers during
1548 * log recovery, so don't warn for such filesystems.
1549 */
1550 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1551 xfs_warn(mp,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001552 "%s: no buf ops on daddr 0x%llx len %d",
Dave Chinner400b9d82014-08-04 12:42:40 +10001553 __func__, bp->b_bn, bp->b_length);
Darrick J. Wong9c712a12018-01-08 10:51:26 -08001554 xfs_hex_dump(bp->b_addr,
1555 XFS_CORRUPTION_DUMP_LEN);
Dave Chinner400b9d82014-08-04 12:42:40 +10001556 dump_stack();
1557 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001558 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001559 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001560 op = REQ_OP_READ;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001561 if (bp->b_flags & XBF_READ_AHEAD)
1562 op |= REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001563 }
1564
1565 /* we only use the buffer cache for meta-data */
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001566 op |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001567
1568 /*
1569 * Walk all the vectors issuing IO on them. Set up the initial offset
1570 * into the buffer and the desired IO size before we start -
1571 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1572 * subsequent call.
1573 */
1574 offset = bp->b_offset;
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001575 size = BBTOB(bp->b_length);
Dave Chinner3e85c862012-06-22 18:50:09 +10001576 blk_start_plug(&plug);
1577 for (i = 0; i < bp->b_map_count; i++) {
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001578 xfs_buf_ioapply_map(bp, i, &offset, &size, op);
Dave Chinner3e85c862012-06-22 18:50:09 +10001579 if (bp->b_error)
1580 break;
1581 if (size <= 0)
1582 break; /* all done */
1583 }
1584 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
Dave Chinner595bff72014-10-02 09:05:14 +10001587/*
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001588 * Wait for I/O completion of a sync buffer and return the I/O error code.
Dave Chinner595bff72014-10-02 09:05:14 +10001589 */
Brian Fostereaebb512018-07-11 22:26:34 -07001590static int
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001591xfs_buf_iowait(
Dave Chinner595bff72014-10-02 09:05:14 +10001592 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001594 ASSERT(!(bp->b_flags & XBF_ASYNC));
1595
1596 trace_xfs_buf_iowait(bp, _RET_IP_);
1597 wait_for_completion(&bp->b_iowait);
1598 trace_xfs_buf_iowait_done(bp, _RET_IP_);
1599
1600 return bp->b_error;
1601}
1602
1603/*
1604 * Buffer I/O submission path, read or write. Asynchronous submission transfers
1605 * the buffer lock ownership and the current reference to the IO. It is not
1606 * safe to reference the buffer after a call to this function unless the caller
1607 * holds an additional reference itself.
1608 */
Christoph Hellwig26e328752020-09-01 10:55:47 -07001609static int
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001610__xfs_buf_submit(
1611 struct xfs_buf *bp,
1612 bool wait)
1613{
1614 int error = 0;
1615
Dave Chinner595bff72014-10-02 09:05:14 +10001616 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001618 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff72014-10-02 09:05:14 +10001619
1620 /* on shutdown we stale and complete the buffer immediately */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001621 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
Brian Foster54b3b1f2020-05-06 13:25:19 -07001622 xfs_buf_ioend_fail(bp);
Brian Fostereaebb512018-07-11 22:26:34 -07001623 return -EIO;
Dave Chinner595bff72014-10-02 09:05:14 +10001624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001626 /*
1627 * Grab a reference so the buffer does not go away underneath us. For
1628 * async buffers, I/O completion drops the callers reference, which
1629 * could occur before submission returns.
1630 */
1631 xfs_buf_hold(bp);
1632
Christoph Hellwig375ec692011-08-23 08:28:03 +00001633 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001634 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Dave Chinner61be9c52014-10-02 09:04:31 +10001636 /* clear the internal error state to avoid spurious errors */
1637 bp->b_io_error = 0;
1638
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001639 /*
Brian Fostereaebb512018-07-11 22:26:34 -07001640 * Set the count to 1 initially, this will stop an I/O completion
1641 * callout which happens before we have started all the I/O from calling
1642 * xfs_buf_ioend too early.
1643 */
1644 atomic_set(&bp->b_io_remaining, 1);
1645 if (bp->b_flags & XBF_ASYNC)
1646 xfs_buf_ioacct_inc(bp);
1647 _xfs_buf_ioapply(bp);
1648
1649 /*
1650 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1651 * reference we took above. If we drop it to zero, run completion so
1652 * that we don't return to the caller with completion still pending.
1653 */
1654 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1655 if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
1656 xfs_buf_ioend(bp);
1657 else
1658 xfs_buf_ioend_async(bp);
1659 }
1660
Brian Foster6af88cd2018-07-11 22:26:35 -07001661 if (wait)
1662 error = xfs_buf_iowait(bp);
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001663
Dave Chinner595bff72014-10-02 09:05:14 +10001664 /*
Brian Foster6af88cd2018-07-11 22:26:35 -07001665 * Release the hold that keeps the buffer referenced for the entire
1666 * I/O. Note that if the buffer is async, it is not safe to reference
1667 * after this release.
Dave Chinner595bff72014-10-02 09:05:14 +10001668 */
1669 xfs_buf_rele(bp);
1670 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001673void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001674xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001675 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 size_t offset)
1677{
1678 struct page *page;
1679
Dave Chinner611c9942012-04-23 15:59:07 +10001680 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001681 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Nathan Scottce8e9222006-01-11 15:39:08 +11001683 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001684 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001685 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688void
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001689xfs_buf_zero(
1690 struct xfs_buf *bp,
1691 size_t boff,
1692 size_t bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Dave Chinner795cac72012-04-23 15:58:53 +10001694 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 bend = boff + bsize;
1697 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001698 struct page *page;
1699 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Dave Chinner795cac72012-04-23 15:58:53 +10001701 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1702 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1703 page = bp->b_pages[page_index];
1704 csize = min_t(size_t, PAGE_SIZE - page_offset,
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001705 BBTOB(bp->b_length) - boff);
Dave Chinner795cac72012-04-23 15:58:53 +10001706
1707 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001709 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 boff += csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713}
1714
1715/*
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001716 * Log a message about and stale a buffer that a caller has decided is corrupt.
1717 *
1718 * This function should be called for the kinds of metadata corruption that
1719 * cannot be detect from a verifier, such as incorrect inter-block relationship
1720 * data. Do /not/ call this function from a verifier function.
1721 *
1722 * The buffer must be XBF_DONE prior to the call. Afterwards, the buffer will
1723 * be marked stale, but b_error will not be set. The caller is responsible for
1724 * releasing the buffer or fixing it.
1725 */
1726void
1727__xfs_buf_mark_corrupt(
1728 struct xfs_buf *bp,
1729 xfs_failaddr_t fa)
1730{
1731 ASSERT(bp->b_flags & XBF_DONE);
1732
Darrick J. Wonge83cf872020-03-11 10:37:54 -07001733 xfs_buf_corruption_error(bp, fa);
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001734 xfs_buf_stale(bp);
1735}
1736
1737/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001738 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 */
1740
1741/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001742 * Wait for any bufs with callbacks that have been submitted but have not yet
1743 * returned. These buffers will have an elevated hold count, so wait on those
1744 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001746static enum lru_status
Brian Foster10fb9ac2021-01-22 16:48:19 -08001747xfs_buftarg_drain_rele(
Dave Chinnere80dfa12013-08-28 10:18:05 +10001748 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001749 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001750 spinlock_t *lru_lock,
1751 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Dave Chinnere80dfa12013-08-28 10:18:05 +10001753{
1754 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001755 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001756
1757 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001758 /* need to wait, so skip it this pass */
Brian Foster10fb9ac2021-01-22 16:48:19 -08001759 trace_xfs_buf_drain_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001760 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Dave Chinnera4082352013-08-28 10:18:06 +10001762 if (!spin_trylock(&bp->b_lock))
1763 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001764
Dave Chinnera4082352013-08-28 10:18:06 +10001765 /*
1766 * clear the LRU reference count so the buffer doesn't get
1767 * ignored in xfs_buf_rele().
1768 */
1769 atomic_set(&bp->b_lru_ref, 0);
1770 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001771 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001772 spin_unlock(&bp->b_lock);
1773 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Brian Foster8321ddb2021-01-22 16:48:20 -08001776/*
1777 * Wait for outstanding I/O on the buftarg to complete.
1778 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001779void
Brian Foster8321ddb2021-01-22 16:48:20 -08001780xfs_buftarg_wait(
Dave Chinnere80dfa12013-08-28 10:18:05 +10001781 struct xfs_buftarg *btp)
1782{
Dave Chinner85bec542016-01-19 08:28:10 +11001783 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001784 * First wait on the buftarg I/O count for all in-flight buffers to be
1785 * released. This is critical as new buffers do not make the LRU until
1786 * they are released.
1787 *
1788 * Next, flush the buffer workqueue to ensure all completion processing
1789 * has finished. Just waiting on buffer locks is not sufficient for
1790 * async IO as the reference count held over IO is not released until
1791 * after the buffer lock is dropped. Hence we need to ensure here that
1792 * all reference counts have been dropped before we start walking the
1793 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001794 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001795 while (percpu_counter_sum(&btp->bt_io_count))
1796 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001797 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Brian Foster8321ddb2021-01-22 16:48:20 -08001798}
1799
1800void
1801xfs_buftarg_drain(
1802 struct xfs_buftarg *btp)
1803{
1804 LIST_HEAD(dispose);
1805 int loop = 0;
1806 bool write_fail = false;
1807
1808 xfs_buftarg_wait(btp);
Dave Chinner85bec542016-01-19 08:28:10 +11001809
Dave Chinnera4082352013-08-28 10:18:06 +10001810 /* loop until there is nothing left on the lru list. */
1811 while (list_lru_count(&btp->bt_lru)) {
Brian Foster10fb9ac2021-01-22 16:48:19 -08001812 list_lru_walk(&btp->bt_lru, xfs_buftarg_drain_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001813 &dispose, LONG_MAX);
1814
1815 while (!list_empty(&dispose)) {
1816 struct xfs_buf *bp;
1817 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1818 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001819 if (bp->b_flags & XBF_WRITE_FAIL) {
Brian Foster61948b62020-05-06 13:25:21 -07001820 write_fail = true;
1821 xfs_buf_alert_ratelimited(bp,
1822 "XFS: Corruption Alert",
Darrick J. Wongc219b012018-01-08 11:39:18 -08001823"Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001824 (long long)bp->b_bn);
1825 }
Dave Chinnera4082352013-08-28 10:18:06 +10001826 xfs_buf_rele(bp);
1827 }
1828 if (loop++ != 0)
1829 delay(100);
1830 }
Brian Foster61948b62020-05-06 13:25:21 -07001831
1832 /*
1833 * If one or more failed buffers were freed, that means dirty metadata
1834 * was thrown away. This should only ever happen after I/O completion
1835 * handling has elevated I/O error(s) to permanent failures and shuts
1836 * down the fs.
1837 */
1838 if (write_fail) {
1839 ASSERT(XFS_FORCED_SHUTDOWN(btp->bt_mount));
1840 xfs_alert(btp->bt_mount,
1841 "Please run xfs_repair to determine the extent of the problem.");
1842 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001843}
1844
1845static enum lru_status
1846xfs_buftarg_isolate(
1847 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001848 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001849 spinlock_t *lru_lock,
1850 void *arg)
1851{
1852 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1853 struct list_head *dispose = arg;
1854
1855 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001856 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1857 * If we fail to get the lock, just skip it.
1858 */
1859 if (!spin_trylock(&bp->b_lock))
1860 return LRU_SKIP;
1861 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001862 * Decrement the b_lru_ref count unless the value is already
1863 * zero. If the value is already zero, we need to reclaim the
1864 * buffer, otherwise it gets another trip through the LRU.
1865 */
Vratislav Bendel19957a12018-03-06 17:07:44 -08001866 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
Dave Chinnera4082352013-08-28 10:18:06 +10001867 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001868 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001869 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001870
Dave Chinnera4082352013-08-28 10:18:06 +10001871 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001872 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001873 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001874 return LRU_REMOVED;
1875}
1876
Andrew Mortonaddbda42013-08-28 10:18:06 +10001877static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001878xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001879 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001880 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001881{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001882 struct xfs_buftarg *btp = container_of(shrink,
1883 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001884 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001885 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001886
Vladimir Davydov503c3582015-02-12 14:58:47 -08001887 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1888 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001889
1890 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001891 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001892 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1893 list_del_init(&bp->b_lru);
1894 xfs_buf_rele(bp);
1895 }
1896
Dave Chinnere80dfa12013-08-28 10:18:05 +10001897 return freed;
1898}
1899
Andrew Mortonaddbda42013-08-28 10:18:06 +10001900static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001901xfs_buftarg_shrink_count(
1902 struct shrinker *shrink,
1903 struct shrink_control *sc)
1904{
1905 struct xfs_buftarg *btp = container_of(shrink,
1906 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001907 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001908}
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910void
1911xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001912 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001914 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001915 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1916 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001917 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001918
Dave Chinner2291dab2016-12-09 16:49:54 +11001919 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001920
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001921 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001924int
1925xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001927 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001929 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001930 btp->bt_meta_sectorsize = sectorsize;
1931 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Nathan Scottce8e9222006-01-11 15:39:08 +11001933 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001934 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001935 "Cannot set_blocksize to %u on device %pg",
1936 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001937 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
1939
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001940 /* Set up device logical sector size mask */
1941 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1942 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return 0;
1945}
1946
1947/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001948 * When allocating the initial buffer target we have not yet
1949 * read in the superblock, so don't know what sized sectors
1950 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001951 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952STATIC int
1953xfs_setsize_buftarg_early(
1954 xfs_buftarg_t *btp,
1955 struct block_device *bdev)
1956{
Eric Sandeena96c4152014-04-14 19:00:29 +10001957 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960xfs_buftarg_t *
1961xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001962 struct xfs_mount *mp,
Dan Williams486aff52017-08-24 15:12:50 -07001963 struct block_device *bdev,
1964 struct dax_device *dax_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 xfs_buftarg_t *btp;
1967
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07001968 btp = kmem_zalloc(sizeof(*btp), KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Dave Chinnerebad8612010-09-22 10:47:20 +10001970 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001971 btp->bt_dev = bdev->bd_dev;
1972 btp->bt_bdev = bdev;
Dan Williams486aff52017-08-24 15:12:50 -07001973 btp->bt_daxdev = dax_dev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001974
Brian Fosterf9bccfc2020-05-06 13:25:21 -07001975 /*
1976 * Buffer IO error rate limiting. Limit it to no more than 10 messages
1977 * per 30 seconds so as to not spam logs too much on repeated errors.
1978 */
1979 ratelimit_state_init(&btp->bt_ioerror_rl, 30 * HZ,
1980 DEFAULT_RATELIMIT_BURST);
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if (xfs_setsize_buftarg_early(btp, bdev))
Michal Hockod210a982017-11-23 17:13:40 +01001983 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001984
1985 if (list_lru_init(&btp->bt_lru))
Michal Hockod210a982017-11-23 17:13:40 +01001986 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001987
Brian Foster9c7504a2016-07-20 11:15:28 +10001988 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
Michal Hockod210a982017-11-23 17:13:40 +01001989 goto error_lru;
Brian Foster9c7504a2016-07-20 11:15:28 +10001990
Dave Chinnere80dfa12013-08-28 10:18:05 +10001991 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1992 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001993 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001994 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Michal Hockod210a982017-11-23 17:13:40 +01001995 if (register_shrinker(&btp->bt_shrinker))
1996 goto error_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return btp;
1998
Michal Hockod210a982017-11-23 17:13:40 +01001999error_pcpu:
2000 percpu_counter_destroy(&btp->bt_io_count);
2001error_lru:
2002 list_lru_destroy(&btp->bt_lru);
2003error_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002004 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return NULL;
2006}
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008/*
Brian Foster20e8a062017-04-21 12:40:44 -07002009 * Cancel a delayed write list.
2010 *
2011 * Remove each buffer from the list, clear the delwri queue flag and drop the
2012 * associated buffer reference.
2013 */
2014void
2015xfs_buf_delwri_cancel(
2016 struct list_head *list)
2017{
2018 struct xfs_buf *bp;
2019
2020 while (!list_empty(list)) {
2021 bp = list_first_entry(list, struct xfs_buf, b_list);
2022
2023 xfs_buf_lock(bp);
2024 bp->b_flags &= ~_XBF_DELWRI_Q;
2025 list_del_init(&bp->b_list);
2026 xfs_buf_relse(bp);
2027 }
2028}
2029
2030/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002031 * Add a buffer to the delayed write list.
2032 *
2033 * This queues a buffer for writeout if it hasn't already been. Note that
2034 * neither this routine nor the buffer list submission functions perform
2035 * any internal synchronization. It is expected that the lists are thread-local
2036 * to the callers.
2037 *
2038 * Returns true if we queued up the buffer, or false if it already had
2039 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002041bool
Nathan Scottce8e9222006-01-11 15:39:08 +11002042xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002043 struct xfs_buf *bp,
2044 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002046 ASSERT(xfs_buf_islocked(bp));
2047 ASSERT(!(bp->b_flags & XBF_READ));
2048
2049 /*
2050 * If the buffer is already marked delwri it already is queued up
2051 * by someone else for imediate writeout. Just ignore it in that
2052 * case.
2053 */
2054 if (bp->b_flags & _XBF_DELWRI_Q) {
2055 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
2056 return false;
2057 }
David Chinnera6867a62006-01-11 15:37:58 +11002058
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002059 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
2060
Dave Chinnerd808f612010-02-02 10:13:42 +11002061 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002062 * If a buffer gets written out synchronously or marked stale while it
2063 * is on a delwri list we lazily remove it. To do this, the other party
2064 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
2065 * It remains referenced and on the list. In a rare corner case it
2066 * might get readded to a delwri list after the synchronous writeout, in
2067 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11002068 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002069 bp->b_flags |= _XBF_DELWRI_Q;
2070 if (list_empty(&bp->b_list)) {
2071 atomic_inc(&bp->b_hold);
2072 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11002073 }
David Chinner585e6d82007-02-10 18:32:29 +11002074
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002075 return true;
David Chinner585e6d82007-02-10 18:32:29 +11002076}
2077
Dave Chinner089716a2010-01-26 15:13:25 +11002078/*
2079 * Compare function is more complex than it needs to be because
2080 * the return value is only 32 bits and we are doing comparisons
2081 * on 64 bit values
2082 */
2083static int
2084xfs_buf_cmp(
Sami Tolvanen4f0f5862021-04-08 11:28:34 -07002085 void *priv,
2086 const struct list_head *a,
2087 const struct list_head *b)
Dave Chinner089716a2010-01-26 15:13:25 +11002088{
2089 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
2090 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
2091 xfs_daddr_t diff;
2092
Mark Tinguelyf4b42422012-12-04 17:18:02 -06002093 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11002094 if (diff < 0)
2095 return -1;
2096 if (diff > 0)
2097 return 1;
2098 return 0;
2099}
2100
Dave Chinner26f1fe82016-06-01 17:38:15 +10002101/*
Brian Fostere339dd82018-07-11 22:26:34 -07002102 * Submit buffers for write. If wait_list is specified, the buffers are
2103 * submitted using sync I/O and placed on the wait list such that the caller can
2104 * iowait each buffer. Otherwise async I/O is used and the buffers are released
2105 * at I/O completion time. In either case, buffers remain locked until I/O
2106 * completes and the buffer is released from the queue.
Dave Chinner26f1fe82016-06-01 17:38:15 +10002107 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002108static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10002109xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002110 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10002111 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002113 struct xfs_buf *bp, *n;
2114 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002115 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Dave Chinner26f1fe82016-06-01 17:38:15 +10002117 list_sort(NULL, buffer_list, xfs_buf_cmp);
2118
2119 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002120 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10002121 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002122 if (xfs_buf_ispinned(bp)) {
2123 pinned++;
2124 continue;
2125 }
2126 if (!xfs_buf_trylock(bp))
2127 continue;
2128 } else {
2129 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002132 /*
2133 * Someone else might have written the buffer synchronously or
2134 * marked it stale in the meantime. In that case only the
2135 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
2136 * reference and remove it from the list here.
2137 */
2138 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
2139 list_del_init(&bp->b_list);
2140 xfs_buf_relse(bp);
2141 continue;
2142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002144 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002145
Dave Chinnercf53e992014-10-02 09:04:01 +10002146 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002147 * If we have a wait list, each buffer (and associated delwri
2148 * queue reference) transfers to it and is submitted
2149 * synchronously. Otherwise, drop the buffer from the delwri
2150 * queue and submit async.
Dave Chinnercf53e992014-10-02 09:04:01 +10002151 */
Brian Fosterb6983e82020-05-06 13:25:20 -07002152 bp->b_flags &= ~_XBF_DELWRI_Q;
Brian Fostere339dd82018-07-11 22:26:34 -07002153 bp->b_flags |= XBF_WRITE;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002154 if (wait_list) {
Brian Fostere339dd82018-07-11 22:26:34 -07002155 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002156 list_move_tail(&bp->b_list, wait_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002157 } else {
2158 bp->b_flags |= XBF_ASYNC;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002159 list_del_init(&bp->b_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002160 }
Brian Foster6af88cd2018-07-11 22:26:35 -07002161 __xfs_buf_submit(bp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00002163 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002165 return pinned;
2166}
Nathan Scottf07c2252006-09-28 10:52:15 +10002167
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002168/*
2169 * Write out a buffer list asynchronously.
2170 *
2171 * This will take the @buffer_list, write all non-locked and non-pinned buffers
2172 * out and not wait for I/O completion on any of the buffers. This interface
2173 * is only safely useable for callers that can track I/O completion by higher
2174 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
2175 * function.
Brian Fosterefc32892018-10-18 17:21:49 +11002176 *
2177 * Note: this function will skip buffers it would block on, and in doing so
2178 * leaves them on @buffer_list so they can be retried on a later pass. As such,
2179 * it is up to the caller to ensure that the buffer list is fully submitted or
2180 * cancelled appropriately when they are finished with the list. Failure to
2181 * cancel or resubmit the list until it is empty will result in leaked buffers
2182 * at unmount time.
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002183 */
2184int
2185xfs_buf_delwri_submit_nowait(
2186 struct list_head *buffer_list)
2187{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002188 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002189}
2190
2191/*
2192 * Write out a buffer list synchronously.
2193 *
2194 * This will take the @buffer_list, write all buffers out and wait for I/O
2195 * completion on all of the buffers. @buffer_list is consumed by the function,
2196 * so callers must have some other way of tracking buffers if they require such
2197 * functionality.
2198 */
2199int
2200xfs_buf_delwri_submit(
2201 struct list_head *buffer_list)
2202{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002203 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002204 int error = 0, error2;
2205 struct xfs_buf *bp;
2206
Dave Chinner26f1fe82016-06-01 17:38:15 +10002207 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002208
2209 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10002210 while (!list_empty(&wait_list)) {
2211 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002212
2213 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10002214
Brian Fostere339dd82018-07-11 22:26:34 -07002215 /*
2216 * Wait on the locked buffer, check for errors and unlock and
2217 * release the delwri queue reference.
2218 */
2219 error2 = xfs_buf_iowait(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002220 xfs_buf_relse(bp);
2221 if (!error)
2222 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
2224
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002225 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
Brian Foster7912e7f2017-06-14 21:21:45 -07002228/*
2229 * Push a single buffer on a delwri queue.
2230 *
2231 * The purpose of this function is to submit a single buffer of a delwri queue
2232 * and return with the buffer still on the original queue. The waiting delwri
2233 * buffer submission infrastructure guarantees transfer of the delwri queue
2234 * buffer reference to a temporary wait list. We reuse this infrastructure to
2235 * transfer the buffer back to the original queue.
2236 *
2237 * Note the buffer transitions from the queued state, to the submitted and wait
2238 * listed state and back to the queued state during this call. The buffer
2239 * locking and queue management logic between _delwri_pushbuf() and
2240 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2241 * before returning.
2242 */
2243int
2244xfs_buf_delwri_pushbuf(
2245 struct xfs_buf *bp,
2246 struct list_head *buffer_list)
2247{
2248 LIST_HEAD (submit_list);
2249 int error;
2250
2251 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2252
2253 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2254
2255 /*
2256 * Isolate the buffer to a new local list so we can submit it for I/O
2257 * independently from the rest of the original list.
2258 */
2259 xfs_buf_lock(bp);
2260 list_move(&bp->b_list, &submit_list);
2261 xfs_buf_unlock(bp);
2262
2263 /*
2264 * Delwri submission clears the DELWRI_Q buffer flag and returns with
Brian Fostere339dd82018-07-11 22:26:34 -07002265 * the buffer on the wait list with the original reference. Rather than
Brian Foster7912e7f2017-06-14 21:21:45 -07002266 * bounce the buffer from a local wait list back to the original list
2267 * after I/O completion, reuse the original list as the wait list.
2268 */
2269 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2270
2271 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002272 * The buffer is now locked, under I/O and wait listed on the original
2273 * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2274 * return with the buffer unlocked and on the original queue.
Brian Foster7912e7f2017-06-14 21:21:45 -07002275 */
Brian Fostere339dd82018-07-11 22:26:34 -07002276 error = xfs_buf_iowait(bp);
Brian Foster7912e7f2017-06-14 21:21:45 -07002277 bp->b_flags |= _XBF_DELWRI_Q;
2278 xfs_buf_unlock(bp);
2279
2280 return error;
2281}
2282
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002283int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002284xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Dave Chinner12eba652020-03-24 20:10:28 -07002286 xfs_buf_zone = kmem_cache_create("xfs_buf", sizeof(struct xfs_buf), 0,
2287 SLAB_HWCACHE_ALIGN |
2288 SLAB_RECLAIM_ACCOUNT |
2289 SLAB_MEM_SPREAD,
2290 NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002291 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002292 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002293
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002296 out:
Nathan Scott87582802006-03-14 13:18:19 +11002297 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300void
Nathan Scottce8e9222006-01-11 15:39:08 +11002301xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08002303 kmem_cache_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
Brian Foster7561d272017-10-17 14:16:29 -07002305
2306void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
2307{
Brian Foster7561d272017-10-17 14:16:29 -07002308 /*
2309 * Set the lru reference count to 0 based on the error injection tag.
2310 * This allows userspace to disrupt buffer caching for debug/testing
2311 * purposes.
2312 */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002313 if (XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_LRU_REF))
Brian Foster7561d272017-10-17 14:16:29 -07002314 lru_ref = 0;
2315
2316 atomic_set(&bp->b_lru_ref, lru_ref);
2317}
Brian Foster8473fee2019-02-07 10:45:46 -08002318
2319/*
2320 * Verify an on-disk magic value against the magic value specified in the
2321 * verifier structure. The verifier magic is in disk byte order so the caller is
2322 * expected to pass the value directly from disk.
2323 */
2324bool
2325xfs_verify_magic(
2326 struct xfs_buf *bp,
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002327 __be32 dmagic)
Brian Foster8473fee2019-02-07 10:45:46 -08002328{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002329 struct xfs_mount *mp = bp->b_mount;
Brian Foster8473fee2019-02-07 10:45:46 -08002330 int idx;
2331
2332 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002333 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic[idx]))
Brian Foster8473fee2019-02-07 10:45:46 -08002334 return false;
2335 return dmagic == bp->b_ops->magic[idx];
2336}
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002337/*
2338 * Verify an on-disk magic value against the magic value specified in the
2339 * verifier structure. The verifier magic is in disk byte order so the caller is
2340 * expected to pass the value directly from disk.
2341 */
2342bool
2343xfs_verify_magic16(
2344 struct xfs_buf *bp,
2345 __be16 dmagic)
2346{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002347 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002348 int idx;
2349
2350 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002351 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic16[idx]))
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002352 return false;
2353 return dmagic == bp->b_ops->magic16[idx];
2354}