blob: f901a74d6176486ab80c9fbb84c738260f3bada2 [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{
Christoph Hellwig54cd3aa2021-06-07 11:49:50 +100082 return (bp->b_page_count * PAGE_SIZE);
James Bottomley73c77e22010-01-25 11:42:24 -060083}
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))
Christoph Hellwig54cd3aa2021-06-07 11:49:50 +1000284 vm_unmap_ram(bp->b_addr, bp->b_page_count);
Dave Chinnere7d236a2021-06-01 13:40:36 +1000285
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 */
Christoph Hellwig54cd3aa2021-06-07 11:49:50 +1000445 bp->b_addr = page_address(bp->b_pages[0]);
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
474 return 0;
475}
476
477/*
478 * Finding and Reading Buffers
479 */
Lucas Stach6031e732016-12-07 17:36:36 +1100480static int
481_xfs_buf_obj_cmp(
482 struct rhashtable_compare_arg *arg,
483 const void *obj)
484{
485 const struct xfs_buf_map *map = arg->key;
486 const struct xfs_buf *bp = obj;
487
488 /*
489 * The key hashing in the lookup path depends on the key being the
490 * first element of the compare_arg, make sure to assert this.
491 */
492 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
493
494 if (bp->b_bn != map->bm_bn)
495 return 1;
496
497 if (unlikely(bp->b_length != map->bm_len)) {
498 /*
499 * found a block number match. If the range doesn't
500 * match, the only way this is allowed is if the buffer
501 * in the cache is stale and the transaction that made
502 * it stale has not yet committed. i.e. we are
503 * reallocating a busy extent. Skip this buffer and
504 * continue searching for an exact match.
505 */
506 ASSERT(bp->b_flags & XBF_STALE);
507 return 1;
508 }
509 return 0;
510}
511
512static const struct rhashtable_params xfs_buf_hash_params = {
513 .min_size = 32, /* empty AGs have minimal footprint */
514 .nelem_hint = 16,
515 .key_len = sizeof(xfs_daddr_t),
516 .key_offset = offsetof(struct xfs_buf, b_bn),
517 .head_offset = offsetof(struct xfs_buf, b_rhash_head),
518 .automatic_shrinking = true,
519 .obj_cmpfn = _xfs_buf_obj_cmp,
520};
521
522int
523xfs_buf_hash_init(
524 struct xfs_perag *pag)
525{
526 spin_lock_init(&pag->pag_buf_lock);
527 return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
528}
529
530void
531xfs_buf_hash_destroy(
532 struct xfs_perag *pag)
533{
534 rhashtable_destroy(&pag->pag_buf_hash);
535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537/*
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700538 * Look up a buffer in the buffer cache and return it referenced and locked
539 * in @found_bp.
540 *
541 * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the
542 * cache.
543 *
544 * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return
545 * -EAGAIN if we fail to lock it.
546 *
547 * Return values are:
548 * -EFSCORRUPTED if have been supplied with an invalid address
549 * -EAGAIN on trylock failure
550 * -ENOENT if we fail to find a match and @new_bp was NULL
551 * 0, with @found_bp:
552 * - @new_bp if we inserted it into the cache
553 * - the buffer we found and locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700555static int
556xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000557 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000558 struct xfs_buf_map *map,
559 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100560 xfs_buf_flags_t flags,
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700561 struct xfs_buf *new_bp,
562 struct xfs_buf **found_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Dave Chinner74f75a02010-09-24 19:59:04 +1000564 struct xfs_perag *pag;
Dave Chinnere8222612020-12-16 16:07:34 -0800565 struct xfs_buf *bp;
Lucas Stach6031e732016-12-07 17:36:36 +1100566 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
Dave Chinner10616b82013-01-21 23:53:52 +1100567 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000568 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700570 *found_bp = NULL;
571
Dave Chinner3e85c862012-06-22 18:50:09 +1000572 for (i = 0; i < nmaps; i++)
Lucas Stach6031e732016-12-07 17:36:36 +1100573 cmap.bm_len += map[i].bm_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /* Check for IOs smaller than the sector size / not sector aligned */
Lucas Stach6031e732016-12-07 17:36:36 +1100576 ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
577 ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Dave Chinner10616b82013-01-21 23:53:52 +1100579 /*
580 * Corrupted block numbers can get through to here, unfortunately, so we
581 * have to check that the buffer falls within the filesystem bounds.
582 */
583 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
Lucas Stach6031e732016-12-07 17:36:36 +1100584 if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
Dave Chinner10616b82013-01-21 23:53:52 +1100585 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -0800586 "%s: daddr 0x%llx out of range, EOFS 0x%llx",
Lucas Stach6031e732016-12-07 17:36:36 +1100587 __func__, cmap.bm_bn, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000588 WARN_ON(1);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700589 return -EFSCORRUPTED;
Dave Chinner10616b82013-01-21 23:53:52 +1100590 }
591
Dave Chinner74f75a02010-09-24 19:59:04 +1000592 pag = xfs_perag_get(btp->bt_mount,
Lucas Stach6031e732016-12-07 17:36:36 +1100593 xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Dave Chinner74f75a02010-09-24 19:59:04 +1000595 spin_lock(&pag->pag_buf_lock);
Lucas Stach6031e732016-12-07 17:36:36 +1100596 bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
597 xfs_buf_hash_params);
598 if (bp) {
599 atomic_inc(&bp->b_hold);
600 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602
603 /* No match found */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700604 if (!new_bp) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100605 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000606 spin_unlock(&pag->pag_buf_lock);
607 xfs_perag_put(pag);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700608 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700610
611 /* the buffer keeps the perag reference until it is freed */
612 new_bp->b_pag = pag;
613 rhashtable_insert_fast(&pag->pag_buf_hash, &new_bp->b_rhash_head,
614 xfs_buf_hash_params);
615 spin_unlock(&pag->pag_buf_lock);
616 *found_bp = new_bp;
617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000620 spin_unlock(&pag->pag_buf_lock);
621 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200623 if (!xfs_buf_trylock(bp)) {
624 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100625 xfs_buf_rele(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100626 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700627 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200629 xfs_buf_lock(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100630 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
Dave Chinner0e6e8472011-03-26 09:16:45 +1100633 /*
634 * if the buffer is stale, clear all the external state associated with
635 * it. We need to keep flags such as how we allocated the buffer memory
636 * intact here.
637 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100638 if (bp->b_flags & XBF_STALE) {
639 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner611c9942012-04-23 15:59:07 +1000640 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100641 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000642 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000643
644 trace_xfs_buf_find(bp, flags, _RET_IP_);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100645 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700646 *found_bp = bp;
647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Dave Chinner8925a3d2018-04-18 08:25:20 -0700650struct xfs_buf *
651xfs_buf_incore(
652 struct xfs_buftarg *target,
653 xfs_daddr_t blkno,
654 size_t numblks,
655 xfs_buf_flags_t flags)
656{
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700657 struct xfs_buf *bp;
658 int error;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700659 DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700660
661 error = xfs_buf_find(target, &map, 1, flags, NULL, &bp);
662 if (error)
663 return NULL;
664 return bp;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/*
Dave Chinner38158322011-09-30 04:45:02 +0000668 * Assembles a buffer covering the specified range. The code is optimised for
669 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
670 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 */
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800672int
Dave Chinner6dde2702012-06-22 18:50:10 +1000673xfs_buf_get_map(
674 struct xfs_buftarg *target,
675 struct xfs_buf_map *map,
676 int nmaps,
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800677 xfs_buf_flags_t flags,
678 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Dave Chinner38158322011-09-30 04:45:02 +0000680 struct xfs_buf *bp;
681 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100682 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800684 *bpp = NULL;
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700685 error = xfs_buf_find(target, map, nmaps, flags, NULL, &bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800686 if (!error)
Dave Chinner38158322011-09-30 04:45:02 +0000687 goto found;
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800688 if (error != -ENOENT)
689 return error;
Dave Chinner38158322011-09-30 04:45:02 +0000690
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800691 error = _xfs_buf_alloc(target, map, nmaps, flags, &new_bp);
692 if (error)
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800693 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000695 error = xfs_buf_allocate_memory(new_bp, flags);
696 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000697 xfs_buf_free(new_bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800698 return error;
Dave Chinner38158322011-09-30 04:45:02 +0000699 }
700
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700701 error = xfs_buf_find(target, map, nmaps, flags, new_bp, &bp);
702 if (error) {
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000703 xfs_buf_free(new_bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800704 return error;
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000705 }
706
707 if (bp != new_bp)
708 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Dave Chinner38158322011-09-30 04:45:02 +0000710found:
Dave Chinner611c9942012-04-23 15:59:07 +1000711 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100712 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (unlikely(error)) {
Darrick J. Wong93baa552020-02-21 07:40:44 -0800714 xfs_warn_ratelimited(target->bt_mount,
715 "%s: failed to map %u pages", __func__,
716 bp->b_page_count);
Dave Chinnera8acad72012-04-23 15:58:54 +1000717 xfs_buf_relse(bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800718 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720 }
721
Dave Chinnerb79f4a12016-01-12 07:03:44 +1100722 /*
723 * Clear b_error if this is a lookup from a caller that doesn't expect
724 * valid data to be found in the buffer.
725 */
726 if (!(flags & XBF_READ))
727 xfs_buf_ioerror(bp, 0);
728
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100729 XFS_STATS_INC(target->bt_mount, xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000730 trace_xfs_buf_get(bp, flags, _RET_IP_);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800731 *bpp = bp;
732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Christoph Hellwig26e328752020-09-01 10:55:47 -0700735int
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100736_xfs_buf_read(
Dave Chinnere8222612020-12-16 16:07:34 -0800737 struct xfs_buf *bp,
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100738 xfs_buf_flags_t flags)
739{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000740 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600741 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100742
Christoph Hellwig26e328752020-09-01 10:55:47 -0700743 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD | XBF_DONE);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200744 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100745
Brian Foster6af88cd2018-07-11 22:26:35 -0700746 return xfs_buf_submit(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100747}
748
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100749/*
Brian Foster75d02302019-02-06 09:25:29 -0800750 * Reverify a buffer found in cache without an attached ->b_ops.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800751 *
Brian Foster75d02302019-02-06 09:25:29 -0800752 * If the caller passed an ops structure and the buffer doesn't have ops
753 * assigned, set the ops and use it to verify the contents. If verification
754 * fails, clear XBF_DONE. We assume the buffer has no recorded errors and is
755 * already in XBF_DONE state on entry.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800756 *
Brian Foster75d02302019-02-06 09:25:29 -0800757 * Under normal operations, every in-core buffer is verified on read I/O
758 * completion. There are two scenarios that can lead to in-core buffers without
759 * an assigned ->b_ops. The first is during log recovery of buffers on a V4
760 * filesystem, though these buffers are purged at the end of recovery. The
761 * other is online repair, which intentionally reads with a NULL buffer ops to
762 * run several verifiers across an in-core buffer in order to establish buffer
763 * type. If repair can't establish that, the buffer will be left in memory
764 * with NULL buffer ops.
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100765 */
766int
Brian Foster75d02302019-02-06 09:25:29 -0800767xfs_buf_reverify(
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100768 struct xfs_buf *bp,
769 const struct xfs_buf_ops *ops)
770{
771 ASSERT(bp->b_flags & XBF_DONE);
772 ASSERT(bp->b_error == 0);
773
774 if (!ops || bp->b_ops)
775 return 0;
776
777 bp->b_ops = ops;
778 bp->b_ops->verify_read(bp);
779 if (bp->b_error)
780 bp->b_flags &= ~XBF_DONE;
781 return bp->b_error;
782}
783
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800784int
Dave Chinner6dde2702012-06-22 18:50:10 +1000785xfs_buf_read_map(
786 struct xfs_buftarg *target,
787 struct xfs_buf_map *map,
788 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100789 xfs_buf_flags_t flags,
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800790 struct xfs_buf **bpp,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800791 const struct xfs_buf_ops *ops,
792 xfs_failaddr_t fa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Dave Chinner6dde2702012-06-22 18:50:10 +1000794 struct xfs_buf *bp;
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800795 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Nathan Scottce8e9222006-01-11 15:39:08 +1100797 flags |= XBF_READ;
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800798 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800800 error = xfs_buf_get_map(target, map, nmaps, flags, &bp);
801 if (error)
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800802 return error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000803
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100804 trace_xfs_buf_read(bp, flags, _RET_IP_);
805
806 if (!(bp->b_flags & XBF_DONE)) {
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800807 /* Initiate the buffer read and wait. */
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100808 XFS_STATS_INC(target->bt_mount, xb_get_read);
809 bp->b_ops = ops;
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800810 error = _xfs_buf_read(bp, flags);
811
812 /* Readahead iodone already dropped the buffer, so exit. */
813 if (flags & XBF_ASYNC)
814 return 0;
815 } else {
816 /* Buffer already read; all we need to do is check it. */
817 error = xfs_buf_reverify(bp, ops);
818
819 /* Readahead already finished; drop the buffer and exit. */
820 if (flags & XBF_ASYNC) {
821 xfs_buf_relse(bp);
822 return 0;
823 }
824
825 /* We do not want read in the flags */
826 bp->b_flags &= ~XBF_READ;
827 ASSERT(bp->b_ops != NULL || ops == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800830 /*
831 * If we've had a read error, then the contents of the buffer are
832 * invalid and should not be used. To ensure that a followup read tries
833 * to pull the buffer from disk again, we clear the XBF_DONE flag and
834 * mark the buffer stale. This ensures that anyone who has a current
835 * reference to the buffer will interpret it's contents correctly and
836 * future cache lookups will also treat it as an empty, uninitialised
837 * buffer.
838 */
839 if (error) {
840 if (!XFS_FORCED_SHUTDOWN(target->bt_mount))
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800841 xfs_buf_ioerror_alert(bp, fa);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100842
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800843 bp->b_flags &= ~XBF_DONE;
844 xfs_buf_stale(bp);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100845 xfs_buf_relse(bp);
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800846
847 /* bad CRC means corrupted metadata */
848 if (error == -EFSBADCRC)
849 error = -EFSCORRUPTED;
850 return error;
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100851 }
852
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800853 *bpp = bp;
854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
857/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100858 * If we are not low on memory then do the readahead in a deadlock
859 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 */
861void
Dave Chinner6dde2702012-06-22 18:50:10 +1000862xfs_buf_readahead_map(
863 struct xfs_buftarg *target,
864 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100865 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100866 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800868 struct xfs_buf *bp;
869
Jan Karaefa7c9f2017-02-02 15:56:53 +0100870 if (bdi_read_congested(target->bt_bdev->bd_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return;
872
Dave Chinner6dde2702012-06-22 18:50:10 +1000873 xfs_buf_read_map(target, map, nmaps,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800874 XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD, &bp, ops,
875 __this_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Dave Chinner5adc94c2010-09-24 21:58:31 +1000878/*
879 * Read an uncached buffer from disk. Allocates and returns a locked
880 * buffer containing the disk contents or nothing.
881 */
Dave Chinnerba3726742014-10-02 09:05:32 +1000882int
Dave Chinner5adc94c2010-09-24 21:58:31 +1000883xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000884 struct xfs_buftarg *target,
885 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000886 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100887 int flags,
Dave Chinnerba3726742014-10-02 09:05:32 +1000888 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100889 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000890{
Dave Chinnereab4e632012-11-12 22:54:02 +1100891 struct xfs_buf *bp;
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800892 int error;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000893
Dave Chinnerba3726742014-10-02 09:05:32 +1000894 *bpp = NULL;
895
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800896 error = xfs_buf_get_uncached(target, numblks, flags, &bp);
897 if (error)
898 return error;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000899
900 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000901 ASSERT(bp->b_map_count == 1);
Dave Chinnerba3726742014-10-02 09:05:32 +1000902 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
Dave Chinner3e85c862012-06-22 18:50:09 +1000903 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000904 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100905 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000906
Brian Foster6af88cd2018-07-11 22:26:35 -0700907 xfs_buf_submit(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000908 if (bp->b_error) {
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800909 error = bp->b_error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800910 xfs_buf_relse(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000911 return error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800912 }
Dave Chinnerba3726742014-10-02 09:05:32 +1000913
914 *bpp = bp;
915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800918int
Dave Chinner686865f2010-09-24 20:07:47 +1000919xfs_buf_get_uncached(
920 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000921 size_t numblks,
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800922 int flags,
923 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000925 unsigned long page_count;
Dave Chinner07b5c5a2021-06-01 13:40:35 +1000926 int error;
Dave Chinner3e85c862012-06-22 18:50:09 +1000927 struct xfs_buf *bp;
928 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800930 *bpp = NULL;
931
Brian Fosterc891c302016-07-20 11:13:43 +1000932 /* flags might contain irrelevant bits, pass only what we care about */
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800933 error = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT, &bp);
934 if (error)
Dave Chinner07b5c5a2021-06-01 13:40:35 +1000935 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Dave Chinnere70b73f2012-04-23 15:58:49 +1000937 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Dave Chinner07b5c5a2021-06-01 13:40:35 +1000938 error = xfs_buf_alloc_pages(bp, page_count, flags);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000939 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 goto fail_free_buf;
941
Dave Chinner611c9942012-04-23 15:59:07 +1000942 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000943 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100944 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500945 "%s: failed to map pages", __func__);
Dave Chinner07b5c5a2021-06-01 13:40:35 +1000946 goto fail_free_buf;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Dave Chinner686865f2010-09-24 20:07:47 +1000949 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800950 *bpp = bp;
951 return 0;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000952
Dave Chinner07b5c5a2021-06-01 13:40:35 +1000953fail_free_buf:
954 xfs_buf_free(bp);
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800955 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * Increment reference count on buffer, to hold the buffer concurrently
960 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * Must hold the buffer already to call this function.
962 */
963void
Nathan Scottce8e9222006-01-11 15:39:08 +1100964xfs_buf_hold(
Dave Chinnere8222612020-12-16 16:07:34 -0800965 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000967 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100968 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
971/*
Brian Foster9c7504a2016-07-20 11:15:28 +1000972 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
973 * placed on LRU or freed (depending on b_lru_ref).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
975void
Nathan Scottce8e9222006-01-11 15:39:08 +1100976xfs_buf_rele(
Dave Chinnere8222612020-12-16 16:07:34 -0800977 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Dave Chinner74f75a02010-09-24 19:59:04 +1000979 struct xfs_perag *pag = bp->b_pag;
Brian Foster9c7504a2016-07-20 11:15:28 +1000980 bool release;
981 bool freebuf = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000983 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Dave Chinner74f75a02010-09-24 19:59:04 +1000985 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100986 ASSERT(list_empty(&bp->b_lru));
Brian Foster9c7504a2016-07-20 11:15:28 +1000987 if (atomic_dec_and_test(&bp->b_hold)) {
988 xfs_buf_ioacct_dec(bp);
Nathan Scottfad3aa12006-02-01 12:14:52 +1100989 xfs_buf_free(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +1000990 }
Nathan Scottfad3aa12006-02-01 12:14:52 +1100991 return;
992 }
993
Lachlan McIlroy37906892008-08-13 15:42:10 +1000994 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinnera4082352013-08-28 10:18:06 +1000995
Dave Chinner37fd1672018-10-18 17:21:29 +1100996 /*
997 * We grab the b_lock here first to serialise racing xfs_buf_rele()
998 * calls. The pag_buf_lock being taken on the last reference only
999 * serialises against racing lookups in xfs_buf_find(). IOWs, the second
1000 * to last reference we drop here is not serialised against the last
1001 * reference until we take bp->b_lock. Hence if we don't grab b_lock
1002 * first, the last "release" reference can win the race to the lock and
1003 * free the buffer before the second-to-last reference is processed,
1004 * leading to a use-after-free scenario.
1005 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001006 spin_lock(&bp->b_lock);
Dave Chinner37fd1672018-10-18 17:21:29 +11001007 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +10001008 if (!release) {
1009 /*
1010 * Drop the in-flight state if the buffer is already on the LRU
1011 * and it holds the only reference. This is racy because we
1012 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1013 * ensures the decrement occurs only once per-buf.
1014 */
1015 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
Brian Foster63db7c82017-05-31 08:22:52 -07001016 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001017 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
Brian Foster9c7504a2016-07-20 11:15:28 +10001019
1020 /* the last reference has been dropped ... */
Brian Foster63db7c82017-05-31 08:22:52 -07001021 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001022 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1023 /*
1024 * If the buffer is added to the LRU take a new reference to the
1025 * buffer for the LRU and clear the (now stale) dispose list
1026 * state flag
1027 */
1028 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1029 bp->b_state &= ~XFS_BSTATE_DISPOSE;
1030 atomic_inc(&bp->b_hold);
1031 }
1032 spin_unlock(&pag->pag_buf_lock);
1033 } else {
1034 /*
1035 * most of the time buffers will already be removed from the
1036 * LRU, so optimise that case by checking for the
1037 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1038 * was on was the disposal list
1039 */
1040 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1041 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1042 } else {
1043 ASSERT(list_empty(&bp->b_lru));
1044 }
1045
1046 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Lucas Stach6031e732016-12-07 17:36:36 +11001047 rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
1048 xfs_buf_hash_params);
Brian Foster9c7504a2016-07-20 11:15:28 +10001049 spin_unlock(&pag->pag_buf_lock);
1050 xfs_perag_put(pag);
1051 freebuf = true;
1052 }
1053
1054out_unlock:
1055 spin_unlock(&bp->b_lock);
1056
1057 if (freebuf)
1058 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061
1062/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001063 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +11001064 *
1065 * If we come across a stale, pinned, locked buffer, we know that we are
1066 * being asked to lock a buffer that has been reallocated. Because it is
1067 * pinned, we know that the log has not been pushed to disk and hence it
1068 * will still be locked. Rather than continuing to have trylock attempts
1069 * fail until someone else pushes the log, push it ourselves before
1070 * returning. This means that the xfsaild will not get stuck trying
1071 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 */
1073int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001074xfs_buf_trylock(
1075 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 int locked;
1078
Nathan Scottce8e9222006-01-11 15:39:08 +11001079 locked = down_trylock(&bp->b_sema) == 0;
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001080 if (locked)
Darrick J. Wong479c6412016-06-21 11:53:28 +10001081 trace_xfs_buf_trylock(bp, _RET_IP_);
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001082 else
Darrick J. Wong479c6412016-06-21 11:53:28 +10001083 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001084 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001088 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +10001089 *
1090 * If we come across a stale, pinned, locked buffer, we know that we
1091 * are being asked to lock a buffer that has been reallocated. Because
1092 * it is pinned, we know that the log has not been pushed to disk and
1093 * hence it will still be locked. Rather than sleeping until someone
1094 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001096void
1097xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001098 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001100 trace_xfs_buf_lock(bp, _RET_IP_);
1101
Dave Chinnered3b4d62010-05-21 12:07:08 +10001102 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001103 xfs_log_force(bp->b_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +11001104 down(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001105
1106 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109void
Nathan Scottce8e9222006-01-11 15:39:08 +11001110xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001111 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Brian Foster20e8a062017-04-21 12:40:44 -07001113 ASSERT(xfs_buf_islocked(bp));
1114
Nathan Scottce8e9222006-01-11 15:39:08 +11001115 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001116 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
Nathan Scottce8e9222006-01-11 15:39:08 +11001119STATIC void
1120xfs_buf_wait_unpin(
Dave Chinnere8222612020-12-16 16:07:34 -08001121 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 DECLARE_WAITQUEUE (wait, current);
1124
Nathan Scottce8e9222006-01-11 15:39:08 +11001125 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return;
1127
Nathan Scottce8e9222006-01-11 15:39:08 +11001128 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 for (;;) {
1130 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001131 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001133 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001135 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 set_current_state(TASK_RUNNING);
1137}
1138
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001139static void
1140xfs_buf_ioerror_alert_ratelimited(
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001141 struct xfs_buf *bp)
1142{
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001143 static unsigned long lasttime;
1144 static struct xfs_buftarg *lasttarg;
1145
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001146 if (bp->b_target != lasttarg ||
1147 time_after(jiffies, (lasttime + 5*HZ))) {
1148 lasttime = jiffies;
1149 xfs_buf_ioerror_alert(bp, __this_address);
1150 }
1151 lasttarg = bp->b_target;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001152}
1153
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001154/*
1155 * Account for this latest trip around the retry handler, and decide if
1156 * we've failed enough times to constitute a permanent failure.
1157 */
1158static bool
1159xfs_buf_ioerror_permanent(
1160 struct xfs_buf *bp,
1161 struct xfs_error_cfg *cfg)
1162{
1163 struct xfs_mount *mp = bp->b_mount;
1164
1165 if (cfg->max_retries != XFS_ERR_RETRY_FOREVER &&
1166 ++bp->b_retries > cfg->max_retries)
1167 return true;
1168 if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
1169 time_after(jiffies, cfg->retry_timeout + bp->b_first_retry_time))
1170 return true;
1171
1172 /* At unmount we may treat errors differently */
1173 if ((mp->m_flags & XFS_MOUNT_UNMOUNTING) && mp->m_fail_unmount)
1174 return true;
1175
1176 return false;
1177}
1178
1179/*
1180 * On a sync write or shutdown we just want to stale the buffer and let the
1181 * caller handle the error in bp->b_error appropriately.
1182 *
1183 * If the write was asynchronous then no one will be looking for the error. If
1184 * this is the first failure of this type, clear the error state and write the
1185 * buffer out again. This means we always retry an async write failure at least
1186 * once, but we also need to set the buffer up to behave correctly now for
1187 * repeated failures.
1188 *
1189 * If we get repeated async write failures, then we take action according to the
1190 * error configuration we have been set up to use.
1191 *
Christoph Hellwig70796c62020-09-01 10:55:45 -07001192 * Returns true if this function took care of error handling and the caller must
1193 * not touch the buffer again. Return false if the caller should proceed with
1194 * normal I/O completion handling.
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001195 */
Christoph Hellwig70796c62020-09-01 10:55:45 -07001196static bool
1197xfs_buf_ioend_handle_error(
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001198 struct xfs_buf *bp)
1199{
1200 struct xfs_mount *mp = bp->b_mount;
1201 struct xfs_error_cfg *cfg;
1202
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001203 /*
1204 * If we've already decided to shutdown the filesystem because of I/O
1205 * errors, there's no point in giving this a retry.
1206 */
1207 if (XFS_FORCED_SHUTDOWN(mp))
1208 goto out_stale;
1209
1210 xfs_buf_ioerror_alert_ratelimited(bp);
1211
1212 /*
Christoph Hellwig22c10582020-09-01 10:55:46 -07001213 * We're not going to bother about retrying this during recovery.
1214 * One strike!
1215 */
1216 if (bp->b_flags & _XBF_LOGRECOVERY) {
1217 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1218 return false;
1219 }
1220
1221 /*
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001222 * Synchronous writes will have callers process the error.
1223 */
1224 if (!(bp->b_flags & XBF_ASYNC))
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001225 goto out_stale;
1226
1227 trace_xfs_buf_iodone_async(bp, _RET_IP_);
1228
1229 cfg = xfs_error_get_cfg(mp, XFS_ERR_METADATA, bp->b_error);
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001230 if (bp->b_last_error != bp->b_error ||
1231 !(bp->b_flags & (XBF_STALE | XBF_WRITE_FAIL))) {
1232 bp->b_last_error = bp->b_error;
1233 if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
1234 !bp->b_first_retry_time)
1235 bp->b_first_retry_time = jiffies;
1236 goto resubmit;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001237 }
1238
1239 /*
1240 * Permanent error - we need to trigger a shutdown if we haven't already
1241 * to indicate that inconsistency will result from this action.
1242 */
1243 if (xfs_buf_ioerror_permanent(bp, cfg)) {
1244 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1245 goto out_stale;
1246 }
1247
1248 /* Still considered a transient error. Caller will schedule retries. */
Christoph Hellwig844c9352020-09-01 10:55:45 -07001249 if (bp->b_flags & _XBF_INODES)
1250 xfs_buf_inode_io_fail(bp);
1251 else if (bp->b_flags & _XBF_DQUOTS)
1252 xfs_buf_dquot_io_fail(bp);
1253 else
1254 ASSERT(list_empty(&bp->b_li_list));
1255 xfs_buf_ioerror(bp, 0);
1256 xfs_buf_relse(bp);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001257 return true;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001258
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001259resubmit:
1260 xfs_buf_ioerror(bp, 0);
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001261 bp->b_flags |= (XBF_DONE | XBF_WRITE_FAIL);
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001262 xfs_buf_submit(bp);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001263 return true;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001264out_stale:
1265 xfs_buf_stale(bp);
1266 bp->b_flags |= XBF_DONE;
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001267 bp->b_flags &= ~XBF_WRITE;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001268 trace_xfs_buf_error_relse(bp, _RET_IP_);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001269 return false;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001270}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Christoph Hellwig76b2d322020-09-01 10:55:20 -07001272static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001273xfs_buf_ioend(
1274 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001276 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001277
Dave Chinner61be9c52014-10-02 09:04:31 +10001278 /*
1279 * Pull in IO completion errors now. We are guaranteed to be running
1280 * single threaded, so we don't need the lock to read b_io_error.
1281 */
1282 if (!bp->b_error && bp->b_io_error)
1283 xfs_buf_ioerror(bp, bp->b_io_error);
1284
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001285 if (bp->b_flags & XBF_READ) {
Dave Chinnerb01d1462020-06-29 14:48:47 -07001286 if (!bp->b_error && bp->b_ops)
1287 bp->b_ops->verify_read(bp);
1288 if (!bp->b_error)
1289 bp->b_flags |= XBF_DONE;
Christoph Hellwig23fb5a92020-09-01 10:55:20 -07001290 } else {
1291 if (!bp->b_error) {
1292 bp->b_flags &= ~XBF_WRITE_FAIL;
1293 bp->b_flags |= XBF_DONE;
1294 }
Dave Chinner9fe5c772020-06-29 14:48:47 -07001295
Christoph Hellwig70796c62020-09-01 10:55:45 -07001296 if (unlikely(bp->b_error) && xfs_buf_ioend_handle_error(bp))
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001297 return;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001298
1299 /* clear the retry state */
1300 bp->b_last_error = 0;
1301 bp->b_retries = 0;
1302 bp->b_first_retry_time = 0;
1303
1304 /*
1305 * Note that for things like remote attribute buffers, there may
1306 * not be a buffer log item here, so processing the buffer log
1307 * item must remain optional.
1308 */
1309 if (bp->b_log_item)
1310 xfs_buf_item_done(bp);
1311
Christoph Hellwig23fb5a92020-09-01 10:55:20 -07001312 if (bp->b_flags & _XBF_INODES)
1313 xfs_buf_inode_iodone(bp);
1314 else if (bp->b_flags & _XBF_DQUOTS)
1315 xfs_buf_dquot_iodone(bp);
Christoph Hellwig22c10582020-09-01 10:55:46 -07001316
Dave Chinnerf593bf12020-06-29 14:48:46 -07001317 }
Christoph Hellwig6a7584b2020-09-01 10:55:44 -07001318
Christoph Hellwig22c10582020-09-01 10:55:46 -07001319 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD |
1320 _XBF_LOGRECOVERY);
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001321
Christoph Hellwig6a7584b2020-09-01 10:55:44 -07001322 if (bp->b_flags & XBF_ASYNC)
1323 xfs_buf_relse(bp);
1324 else
1325 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
Dave Chinnere8aaba92014-10-02 09:04:22 +10001328static void
1329xfs_buf_ioend_work(
1330 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001332 struct xfs_buf *bp =
Dave Chinnere8222612020-12-16 16:07:34 -08001333 container_of(work, struct xfs_buf, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001334
Dave Chinnere8aaba92014-10-02 09:04:22 +10001335 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001338static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001339xfs_buf_ioend_async(
1340 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001342 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001343 queue_work(bp->b_mount->m_buf_workqueue, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346void
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001347__xfs_buf_ioerror(
Dave Chinnere8222612020-12-16 16:07:34 -08001348 struct xfs_buf *bp,
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001349 int error,
1350 xfs_failaddr_t failaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Dave Chinner24513372014-06-25 14:58:08 +10001352 ASSERT(error <= 0 && error >= -1000);
1353 bp->b_error = error;
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001354 trace_xfs_buf_ioerror(bp, error, failaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Christoph Hellwig901796a2011-10-10 16:52:49 +00001357void
1358xfs_buf_ioerror_alert(
1359 struct xfs_buf *bp,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -08001360 xfs_failaddr_t func)
Christoph Hellwig901796a2011-10-10 16:52:49 +00001361{
Brian Fosterf9bccfc2020-05-06 13:25:21 -07001362 xfs_buf_alert_ratelimited(bp, "XFS: metadata IO error",
1363 "metadata I/O error in \"%pS\" at daddr 0x%llx len %d error %d",
1364 func, (uint64_t)XFS_BUF_ADDR(bp),
1365 bp->b_length, -bp->b_error);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001366}
1367
Brian Foster54b3b1f2020-05-06 13:25:19 -07001368/*
1369 * To simulate an I/O failure, the buffer must be locked and held with at least
1370 * three references. The LRU reference is dropped by the stale call. The buf
1371 * item reference is dropped via ioend processing. The third reference is owned
1372 * by the caller and is dropped on I/O completion if the buffer is XBF_ASYNC.
1373 */
1374void
1375xfs_buf_ioend_fail(
1376 struct xfs_buf *bp)
1377{
1378 bp->b_flags &= ~XBF_DONE;
1379 xfs_buf_stale(bp);
1380 xfs_buf_ioerror(bp, -EIO);
1381 xfs_buf_ioend(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001382}
Christoph Hellwig939d7232010-07-20 17:51:16 +10001383
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001384int
1385xfs_bwrite(
1386 struct xfs_buf *bp)
1387{
1388 int error;
1389
1390 ASSERT(xfs_buf_islocked(bp));
1391
1392 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001393 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
Brian Fosterb6983e82020-05-06 13:25:20 -07001394 XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001395
Brian Foster6af88cd2018-07-11 22:26:35 -07001396 error = xfs_buf_submit(bp);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001397 if (error)
1398 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001399 return error;
1400}
1401
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001402static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001403xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001404 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001406 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Brian Foster7376d742020-05-06 13:29:19 -07001408 if (!bio->bi_status &&
1409 (bp->b_flags & XBF_WRITE) && (bp->b_flags & XBF_ASYNC) &&
Brian Foster43dc0aa2020-05-08 08:50:52 -07001410 XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_IOERROR))
Brian Foster7376d742020-05-06 13:29:19 -07001411 bio->bi_status = BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Dave Chinner37eb17e2012-11-12 22:09:46 +11001413 /*
1414 * don't overwrite existing errors - otherwise we can lose errors on
1415 * buffers that require multiple bios to complete.
1416 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001417 if (bio->bi_status) {
1418 int error = blk_status_to_errno(bio->bi_status);
1419
1420 cmpxchg(&bp->b_io_error, 0, error);
1421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Dave Chinner37eb17e2012-11-12 22:09:46 +11001423 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001424 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1425
Dave Chinnere8aaba92014-10-02 09:04:22 +10001426 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1427 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Dave Chinner3e85c862012-06-22 18:50:09 +10001431static void
1432xfs_buf_ioapply_map(
1433 struct xfs_buf *bp,
1434 int map,
1435 int *buf_offset,
1436 int *count,
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001437 int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Dave Chinner3e85c862012-06-22 18:50:09 +10001439 int page_index;
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +00001440 unsigned int total_nr_pages = bp->b_page_count;
Dave Chinner3e85c862012-06-22 18:50:09 +10001441 int nr_pages;
1442 struct bio *bio;
1443 sector_t sector = bp->b_maps[map].bm_bn;
1444 int size;
1445 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Dave Chinner3e85c862012-06-22 18:50:09 +10001447 /* skip the pages in the buffer before the start offset */
1448 page_index = 0;
1449 offset = *buf_offset;
1450 while (offset >= PAGE_SIZE) {
1451 page_index++;
1452 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001453 }
1454
Dave Chinner3e85c862012-06-22 18:50:09 +10001455 /*
1456 * Limit the IO size to the length of the current vector, and update the
1457 * remaining IO count for the next time around.
1458 */
1459 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1460 *count -= size;
1461 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001464 atomic_inc(&bp->b_io_remaining);
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +00001465 nr_pages = bio_max_segs(total_nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 bio = bio_alloc(GFP_NOIO, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001468 bio_set_dev(bio, bp->b_target->bt_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001469 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001470 bio->bi_end_io = xfs_buf_bio_end_io;
1471 bio->bi_private = bp;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001472 bio->bi_opf = op;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001473
Dave Chinner3e85c862012-06-22 18:50:09 +10001474 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001475 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 if (nbytes > size)
1478 nbytes = size;
1479
Dave Chinner3e85c862012-06-22 18:50:09 +10001480 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1481 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001482 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 break;
1484
1485 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001486 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 size -= nbytes;
1488 total_nr_pages--;
1489 }
1490
Kent Overstreet4f024f32013-10-11 15:44:27 -07001491 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001492 if (xfs_buf_is_vmapped(bp)) {
1493 flush_kernel_vmap_range(bp->b_addr,
1494 xfs_buf_vmap_len(bp));
1495 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001496 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (size)
1498 goto next_chunk;
1499 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001500 /*
1501 * This is guaranteed not to be the last io reference count
Dave Chinner595bff72014-10-02 09:05:14 +10001502 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001503 */
1504 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001505 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001506 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001508
1509}
1510
1511STATIC void
1512_xfs_buf_ioapply(
1513 struct xfs_buf *bp)
1514{
1515 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001516 int op;
Dave Chinner3e85c862012-06-22 18:50:09 +10001517 int offset;
1518 int size;
1519 int i;
1520
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001521 /*
1522 * Make sure we capture only current IO errors rather than stale errors
1523 * left over from previous use of the buffer (e.g. failed readahead).
1524 */
1525 bp->b_error = 0;
1526
Dave Chinner3e85c862012-06-22 18:50:09 +10001527 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001528 op = REQ_OP_WRITE;
Dave Chinner1813dd62012-11-14 17:54:40 +11001529
1530 /*
1531 * Run the write verifier callback function if it exists. If
1532 * this function fails it will mark the buffer with an error and
1533 * the IO should not be dispatched.
1534 */
1535 if (bp->b_ops) {
1536 bp->b_ops->verify_write(bp);
1537 if (bp->b_error) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001538 xfs_force_shutdown(bp->b_mount,
Dave Chinner1813dd62012-11-14 17:54:40 +11001539 SHUTDOWN_CORRUPT_INCORE);
1540 return;
1541 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001542 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001543 struct xfs_mount *mp = bp->b_mount;
Dave Chinner400b9d82014-08-04 12:42:40 +10001544
1545 /*
1546 * non-crc filesystems don't attach verifiers during
1547 * log recovery, so don't warn for such filesystems.
1548 */
1549 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1550 xfs_warn(mp,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001551 "%s: no buf ops on daddr 0x%llx len %d",
Dave Chinner400b9d82014-08-04 12:42:40 +10001552 __func__, bp->b_bn, bp->b_length);
Darrick J. Wong9c712a12018-01-08 10:51:26 -08001553 xfs_hex_dump(bp->b_addr,
1554 XFS_CORRUPTION_DUMP_LEN);
Dave Chinner400b9d82014-08-04 12:42:40 +10001555 dump_stack();
1556 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001557 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001558 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001559 op = REQ_OP_READ;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001560 if (bp->b_flags & XBF_READ_AHEAD)
1561 op |= REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001562 }
1563
1564 /* we only use the buffer cache for meta-data */
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001565 op |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001566
1567 /*
1568 * Walk all the vectors issuing IO on them. Set up the initial offset
1569 * into the buffer and the desired IO size before we start -
1570 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1571 * subsequent call.
1572 */
1573 offset = bp->b_offset;
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001574 size = BBTOB(bp->b_length);
Dave Chinner3e85c862012-06-22 18:50:09 +10001575 blk_start_plug(&plug);
1576 for (i = 0; i < bp->b_map_count; i++) {
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001577 xfs_buf_ioapply_map(bp, i, &offset, &size, op);
Dave Chinner3e85c862012-06-22 18:50:09 +10001578 if (bp->b_error)
1579 break;
1580 if (size <= 0)
1581 break; /* all done */
1582 }
1583 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
Dave Chinner595bff72014-10-02 09:05:14 +10001586/*
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001587 * Wait for I/O completion of a sync buffer and return the I/O error code.
Dave Chinner595bff72014-10-02 09:05:14 +10001588 */
Brian Fostereaebb512018-07-11 22:26:34 -07001589static int
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001590xfs_buf_iowait(
Dave Chinner595bff72014-10-02 09:05:14 +10001591 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001593 ASSERT(!(bp->b_flags & XBF_ASYNC));
1594
1595 trace_xfs_buf_iowait(bp, _RET_IP_);
1596 wait_for_completion(&bp->b_iowait);
1597 trace_xfs_buf_iowait_done(bp, _RET_IP_);
1598
1599 return bp->b_error;
1600}
1601
1602/*
1603 * Buffer I/O submission path, read or write. Asynchronous submission transfers
1604 * the buffer lock ownership and the current reference to the IO. It is not
1605 * safe to reference the buffer after a call to this function unless the caller
1606 * holds an additional reference itself.
1607 */
Christoph Hellwig26e328752020-09-01 10:55:47 -07001608static int
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001609__xfs_buf_submit(
1610 struct xfs_buf *bp,
1611 bool wait)
1612{
1613 int error = 0;
1614
Dave Chinner595bff72014-10-02 09:05:14 +10001615 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001617 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff72014-10-02 09:05:14 +10001618
1619 /* on shutdown we stale and complete the buffer immediately */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001620 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
Brian Foster54b3b1f2020-05-06 13:25:19 -07001621 xfs_buf_ioend_fail(bp);
Brian Fostereaebb512018-07-11 22:26:34 -07001622 return -EIO;
Dave Chinner595bff72014-10-02 09:05:14 +10001623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001625 /*
1626 * Grab a reference so the buffer does not go away underneath us. For
1627 * async buffers, I/O completion drops the callers reference, which
1628 * could occur before submission returns.
1629 */
1630 xfs_buf_hold(bp);
1631
Christoph Hellwig375ec692011-08-23 08:28:03 +00001632 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001633 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Dave Chinner61be9c52014-10-02 09:04:31 +10001635 /* clear the internal error state to avoid spurious errors */
1636 bp->b_io_error = 0;
1637
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001638 /*
Brian Fostereaebb512018-07-11 22:26:34 -07001639 * Set the count to 1 initially, this will stop an I/O completion
1640 * callout which happens before we have started all the I/O from calling
1641 * xfs_buf_ioend too early.
1642 */
1643 atomic_set(&bp->b_io_remaining, 1);
1644 if (bp->b_flags & XBF_ASYNC)
1645 xfs_buf_ioacct_inc(bp);
1646 _xfs_buf_ioapply(bp);
1647
1648 /*
1649 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1650 * reference we took above. If we drop it to zero, run completion so
1651 * that we don't return to the caller with completion still pending.
1652 */
1653 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1654 if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
1655 xfs_buf_ioend(bp);
1656 else
1657 xfs_buf_ioend_async(bp);
1658 }
1659
Brian Foster6af88cd2018-07-11 22:26:35 -07001660 if (wait)
1661 error = xfs_buf_iowait(bp);
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001662
Dave Chinner595bff72014-10-02 09:05:14 +10001663 /*
Brian Foster6af88cd2018-07-11 22:26:35 -07001664 * Release the hold that keeps the buffer referenced for the entire
1665 * I/O. Note that if the buffer is async, it is not safe to reference
1666 * after this release.
Dave Chinner595bff72014-10-02 09:05:14 +10001667 */
1668 xfs_buf_rele(bp);
1669 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001672void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001673xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001674 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 size_t offset)
1676{
1677 struct page *page;
1678
Dave Chinner611c9942012-04-23 15:59:07 +10001679 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001680 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Dave Chinner0e6e8472011-03-26 09:16:45 +11001682 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001683 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686void
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001687xfs_buf_zero(
1688 struct xfs_buf *bp,
1689 size_t boff,
1690 size_t bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Dave Chinner795cac72012-04-23 15:58:53 +10001692 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 bend = boff + bsize;
1695 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001696 struct page *page;
1697 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Dave Chinner795cac72012-04-23 15:58:53 +10001699 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1700 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1701 page = bp->b_pages[page_index];
1702 csize = min_t(size_t, PAGE_SIZE - page_offset,
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001703 BBTOB(bp->b_length) - boff);
Dave Chinner795cac72012-04-23 15:58:53 +10001704
1705 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001707 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 boff += csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711}
1712
1713/*
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001714 * Log a message about and stale a buffer that a caller has decided is corrupt.
1715 *
1716 * This function should be called for the kinds of metadata corruption that
1717 * cannot be detect from a verifier, such as incorrect inter-block relationship
1718 * data. Do /not/ call this function from a verifier function.
1719 *
1720 * The buffer must be XBF_DONE prior to the call. Afterwards, the buffer will
1721 * be marked stale, but b_error will not be set. The caller is responsible for
1722 * releasing the buffer or fixing it.
1723 */
1724void
1725__xfs_buf_mark_corrupt(
1726 struct xfs_buf *bp,
1727 xfs_failaddr_t fa)
1728{
1729 ASSERT(bp->b_flags & XBF_DONE);
1730
Darrick J. Wonge83cf872020-03-11 10:37:54 -07001731 xfs_buf_corruption_error(bp, fa);
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001732 xfs_buf_stale(bp);
1733}
1734
1735/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001736 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 */
1738
1739/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001740 * Wait for any bufs with callbacks that have been submitted but have not yet
1741 * returned. These buffers will have an elevated hold count, so wait on those
1742 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001744static enum lru_status
Brian Foster10fb9ac2021-01-22 16:48:19 -08001745xfs_buftarg_drain_rele(
Dave Chinnere80dfa12013-08-28 10:18:05 +10001746 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001747 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001748 spinlock_t *lru_lock,
1749 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Dave Chinnere80dfa12013-08-28 10:18:05 +10001751{
1752 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001753 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001754
1755 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001756 /* need to wait, so skip it this pass */
Brian Foster10fb9ac2021-01-22 16:48:19 -08001757 trace_xfs_buf_drain_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001758 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
Dave Chinnera4082352013-08-28 10:18:06 +10001760 if (!spin_trylock(&bp->b_lock))
1761 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001762
Dave Chinnera4082352013-08-28 10:18:06 +10001763 /*
1764 * clear the LRU reference count so the buffer doesn't get
1765 * ignored in xfs_buf_rele().
1766 */
1767 atomic_set(&bp->b_lru_ref, 0);
1768 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001769 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001770 spin_unlock(&bp->b_lock);
1771 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
Brian Foster8321ddb2021-01-22 16:48:20 -08001774/*
1775 * Wait for outstanding I/O on the buftarg to complete.
1776 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001777void
Brian Foster8321ddb2021-01-22 16:48:20 -08001778xfs_buftarg_wait(
Dave Chinnere80dfa12013-08-28 10:18:05 +10001779 struct xfs_buftarg *btp)
1780{
Dave Chinner85bec542016-01-19 08:28:10 +11001781 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001782 * First wait on the buftarg I/O count for all in-flight buffers to be
1783 * released. This is critical as new buffers do not make the LRU until
1784 * they are released.
1785 *
1786 * Next, flush the buffer workqueue to ensure all completion processing
1787 * has finished. Just waiting on buffer locks is not sufficient for
1788 * async IO as the reference count held over IO is not released until
1789 * after the buffer lock is dropped. Hence we need to ensure here that
1790 * all reference counts have been dropped before we start walking the
1791 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001792 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001793 while (percpu_counter_sum(&btp->bt_io_count))
1794 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001795 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Brian Foster8321ddb2021-01-22 16:48:20 -08001796}
1797
1798void
1799xfs_buftarg_drain(
1800 struct xfs_buftarg *btp)
1801{
1802 LIST_HEAD(dispose);
1803 int loop = 0;
1804 bool write_fail = false;
1805
1806 xfs_buftarg_wait(btp);
Dave Chinner85bec542016-01-19 08:28:10 +11001807
Dave Chinnera4082352013-08-28 10:18:06 +10001808 /* loop until there is nothing left on the lru list. */
1809 while (list_lru_count(&btp->bt_lru)) {
Brian Foster10fb9ac2021-01-22 16:48:19 -08001810 list_lru_walk(&btp->bt_lru, xfs_buftarg_drain_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001811 &dispose, LONG_MAX);
1812
1813 while (!list_empty(&dispose)) {
1814 struct xfs_buf *bp;
1815 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1816 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001817 if (bp->b_flags & XBF_WRITE_FAIL) {
Brian Foster61948b62020-05-06 13:25:21 -07001818 write_fail = true;
1819 xfs_buf_alert_ratelimited(bp,
1820 "XFS: Corruption Alert",
Darrick J. Wongc219b012018-01-08 11:39:18 -08001821"Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001822 (long long)bp->b_bn);
1823 }
Dave Chinnera4082352013-08-28 10:18:06 +10001824 xfs_buf_rele(bp);
1825 }
1826 if (loop++ != 0)
1827 delay(100);
1828 }
Brian Foster61948b62020-05-06 13:25:21 -07001829
1830 /*
1831 * If one or more failed buffers were freed, that means dirty metadata
1832 * was thrown away. This should only ever happen after I/O completion
1833 * handling has elevated I/O error(s) to permanent failures and shuts
1834 * down the fs.
1835 */
1836 if (write_fail) {
1837 ASSERT(XFS_FORCED_SHUTDOWN(btp->bt_mount));
1838 xfs_alert(btp->bt_mount,
1839 "Please run xfs_repair to determine the extent of the problem.");
1840 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001841}
1842
1843static enum lru_status
1844xfs_buftarg_isolate(
1845 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001846 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001847 spinlock_t *lru_lock,
1848 void *arg)
1849{
1850 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1851 struct list_head *dispose = arg;
1852
1853 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001854 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1855 * If we fail to get the lock, just skip it.
1856 */
1857 if (!spin_trylock(&bp->b_lock))
1858 return LRU_SKIP;
1859 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001860 * Decrement the b_lru_ref count unless the value is already
1861 * zero. If the value is already zero, we need to reclaim the
1862 * buffer, otherwise it gets another trip through the LRU.
1863 */
Vratislav Bendel19957a12018-03-06 17:07:44 -08001864 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
Dave Chinnera4082352013-08-28 10:18:06 +10001865 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001866 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001867 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001868
Dave Chinnera4082352013-08-28 10:18:06 +10001869 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001870 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001871 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001872 return LRU_REMOVED;
1873}
1874
Andrew Mortonaddbda42013-08-28 10:18:06 +10001875static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001876xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001877 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001878 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001879{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001880 struct xfs_buftarg *btp = container_of(shrink,
1881 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001882 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001883 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001884
Vladimir Davydov503c3582015-02-12 14:58:47 -08001885 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1886 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001887
1888 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001889 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001890 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1891 list_del_init(&bp->b_lru);
1892 xfs_buf_rele(bp);
1893 }
1894
Dave Chinnere80dfa12013-08-28 10:18:05 +10001895 return freed;
1896}
1897
Andrew Mortonaddbda42013-08-28 10:18:06 +10001898static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001899xfs_buftarg_shrink_count(
1900 struct shrinker *shrink,
1901 struct shrink_control *sc)
1902{
1903 struct xfs_buftarg *btp = container_of(shrink,
1904 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001905 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001906}
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908void
1909xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001910 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001912 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001913 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1914 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001915 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001916
Dave Chinner2291dab2016-12-09 16:49:54 +11001917 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001918
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001919 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001922int
1923xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001925 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001927 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001928 btp->bt_meta_sectorsize = sectorsize;
1929 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Nathan Scottce8e9222006-01-11 15:39:08 +11001931 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001932 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001933 "Cannot set_blocksize to %u on device %pg",
1934 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001935 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
1937
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001938 /* Set up device logical sector size mask */
1939 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1940 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return 0;
1943}
1944
1945/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001946 * When allocating the initial buffer target we have not yet
1947 * read in the superblock, so don't know what sized sectors
1948 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001949 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950STATIC int
1951xfs_setsize_buftarg_early(
1952 xfs_buftarg_t *btp,
1953 struct block_device *bdev)
1954{
Eric Sandeena96c4152014-04-14 19:00:29 +10001955 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958xfs_buftarg_t *
1959xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001960 struct xfs_mount *mp,
Dan Williams486aff52017-08-24 15:12:50 -07001961 struct block_device *bdev,
1962 struct dax_device *dax_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 xfs_buftarg_t *btp;
1965
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07001966 btp = kmem_zalloc(sizeof(*btp), KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Dave Chinnerebad8612010-09-22 10:47:20 +10001968 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001969 btp->bt_dev = bdev->bd_dev;
1970 btp->bt_bdev = bdev;
Dan Williams486aff52017-08-24 15:12:50 -07001971 btp->bt_daxdev = dax_dev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001972
Brian Fosterf9bccfc2020-05-06 13:25:21 -07001973 /*
1974 * Buffer IO error rate limiting. Limit it to no more than 10 messages
1975 * per 30 seconds so as to not spam logs too much on repeated errors.
1976 */
1977 ratelimit_state_init(&btp->bt_ioerror_rl, 30 * HZ,
1978 DEFAULT_RATELIMIT_BURST);
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (xfs_setsize_buftarg_early(btp, bdev))
Michal Hockod210a982017-11-23 17:13:40 +01001981 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001982
1983 if (list_lru_init(&btp->bt_lru))
Michal Hockod210a982017-11-23 17:13:40 +01001984 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001985
Brian Foster9c7504a2016-07-20 11:15:28 +10001986 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
Michal Hockod210a982017-11-23 17:13:40 +01001987 goto error_lru;
Brian Foster9c7504a2016-07-20 11:15:28 +10001988
Dave Chinnere80dfa12013-08-28 10:18:05 +10001989 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1990 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001991 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001992 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Michal Hockod210a982017-11-23 17:13:40 +01001993 if (register_shrinker(&btp->bt_shrinker))
1994 goto error_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return btp;
1996
Michal Hockod210a982017-11-23 17:13:40 +01001997error_pcpu:
1998 percpu_counter_destroy(&btp->bt_io_count);
1999error_lru:
2000 list_lru_destroy(&btp->bt_lru);
2001error_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002002 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return NULL;
2004}
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006/*
Brian Foster20e8a062017-04-21 12:40:44 -07002007 * Cancel a delayed write list.
2008 *
2009 * Remove each buffer from the list, clear the delwri queue flag and drop the
2010 * associated buffer reference.
2011 */
2012void
2013xfs_buf_delwri_cancel(
2014 struct list_head *list)
2015{
2016 struct xfs_buf *bp;
2017
2018 while (!list_empty(list)) {
2019 bp = list_first_entry(list, struct xfs_buf, b_list);
2020
2021 xfs_buf_lock(bp);
2022 bp->b_flags &= ~_XBF_DELWRI_Q;
2023 list_del_init(&bp->b_list);
2024 xfs_buf_relse(bp);
2025 }
2026}
2027
2028/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002029 * Add a buffer to the delayed write list.
2030 *
2031 * This queues a buffer for writeout if it hasn't already been. Note that
2032 * neither this routine nor the buffer list submission functions perform
2033 * any internal synchronization. It is expected that the lists are thread-local
2034 * to the callers.
2035 *
2036 * Returns true if we queued up the buffer, or false if it already had
2037 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002039bool
Nathan Scottce8e9222006-01-11 15:39:08 +11002040xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002041 struct xfs_buf *bp,
2042 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002044 ASSERT(xfs_buf_islocked(bp));
2045 ASSERT(!(bp->b_flags & XBF_READ));
2046
2047 /*
2048 * If the buffer is already marked delwri it already is queued up
2049 * by someone else for imediate writeout. Just ignore it in that
2050 * case.
2051 */
2052 if (bp->b_flags & _XBF_DELWRI_Q) {
2053 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
2054 return false;
2055 }
David Chinnera6867a62006-01-11 15:37:58 +11002056
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002057 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
2058
Dave Chinnerd808f612010-02-02 10:13:42 +11002059 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002060 * If a buffer gets written out synchronously or marked stale while it
2061 * is on a delwri list we lazily remove it. To do this, the other party
2062 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
2063 * It remains referenced and on the list. In a rare corner case it
2064 * might get readded to a delwri list after the synchronous writeout, in
2065 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11002066 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002067 bp->b_flags |= _XBF_DELWRI_Q;
2068 if (list_empty(&bp->b_list)) {
2069 atomic_inc(&bp->b_hold);
2070 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11002071 }
David Chinner585e6d82007-02-10 18:32:29 +11002072
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002073 return true;
David Chinner585e6d82007-02-10 18:32:29 +11002074}
2075
Dave Chinner089716a2010-01-26 15:13:25 +11002076/*
2077 * Compare function is more complex than it needs to be because
2078 * the return value is only 32 bits and we are doing comparisons
2079 * on 64 bit values
2080 */
2081static int
2082xfs_buf_cmp(
Sami Tolvanen4f0f5862021-04-08 11:28:34 -07002083 void *priv,
2084 const struct list_head *a,
2085 const struct list_head *b)
Dave Chinner089716a2010-01-26 15:13:25 +11002086{
2087 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
2088 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
2089 xfs_daddr_t diff;
2090
Mark Tinguelyf4b42422012-12-04 17:18:02 -06002091 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11002092 if (diff < 0)
2093 return -1;
2094 if (diff > 0)
2095 return 1;
2096 return 0;
2097}
2098
Dave Chinner26f1fe82016-06-01 17:38:15 +10002099/*
Brian Fostere339dd82018-07-11 22:26:34 -07002100 * Submit buffers for write. If wait_list is specified, the buffers are
2101 * submitted using sync I/O and placed on the wait list such that the caller can
2102 * iowait each buffer. Otherwise async I/O is used and the buffers are released
2103 * at I/O completion time. In either case, buffers remain locked until I/O
2104 * completes and the buffer is released from the queue.
Dave Chinner26f1fe82016-06-01 17:38:15 +10002105 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002106static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10002107xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002108 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10002109 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002111 struct xfs_buf *bp, *n;
2112 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002113 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Dave Chinner26f1fe82016-06-01 17:38:15 +10002115 list_sort(NULL, buffer_list, xfs_buf_cmp);
2116
2117 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002118 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10002119 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002120 if (xfs_buf_ispinned(bp)) {
2121 pinned++;
2122 continue;
2123 }
2124 if (!xfs_buf_trylock(bp))
2125 continue;
2126 } else {
2127 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002130 /*
2131 * Someone else might have written the buffer synchronously or
2132 * marked it stale in the meantime. In that case only the
2133 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
2134 * reference and remove it from the list here.
2135 */
2136 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
2137 list_del_init(&bp->b_list);
2138 xfs_buf_relse(bp);
2139 continue;
2140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002142 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002143
Dave Chinnercf53e992014-10-02 09:04:01 +10002144 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002145 * If we have a wait list, each buffer (and associated delwri
2146 * queue reference) transfers to it and is submitted
2147 * synchronously. Otherwise, drop the buffer from the delwri
2148 * queue and submit async.
Dave Chinnercf53e992014-10-02 09:04:01 +10002149 */
Brian Fosterb6983e82020-05-06 13:25:20 -07002150 bp->b_flags &= ~_XBF_DELWRI_Q;
Brian Fostere339dd82018-07-11 22:26:34 -07002151 bp->b_flags |= XBF_WRITE;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002152 if (wait_list) {
Brian Fostere339dd82018-07-11 22:26:34 -07002153 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002154 list_move_tail(&bp->b_list, wait_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002155 } else {
2156 bp->b_flags |= XBF_ASYNC;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002157 list_del_init(&bp->b_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002158 }
Brian Foster6af88cd2018-07-11 22:26:35 -07002159 __xfs_buf_submit(bp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00002161 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002163 return pinned;
2164}
Nathan Scottf07c2252006-09-28 10:52:15 +10002165
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002166/*
2167 * Write out a buffer list asynchronously.
2168 *
2169 * This will take the @buffer_list, write all non-locked and non-pinned buffers
2170 * out and not wait for I/O completion on any of the buffers. This interface
2171 * is only safely useable for callers that can track I/O completion by higher
2172 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
2173 * function.
Brian Fosterefc32892018-10-18 17:21:49 +11002174 *
2175 * Note: this function will skip buffers it would block on, and in doing so
2176 * leaves them on @buffer_list so they can be retried on a later pass. As such,
2177 * it is up to the caller to ensure that the buffer list is fully submitted or
2178 * cancelled appropriately when they are finished with the list. Failure to
2179 * cancel or resubmit the list until it is empty will result in leaked buffers
2180 * at unmount time.
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002181 */
2182int
2183xfs_buf_delwri_submit_nowait(
2184 struct list_head *buffer_list)
2185{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002186 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002187}
2188
2189/*
2190 * Write out a buffer list synchronously.
2191 *
2192 * This will take the @buffer_list, write all buffers out and wait for I/O
2193 * completion on all of the buffers. @buffer_list is consumed by the function,
2194 * so callers must have some other way of tracking buffers if they require such
2195 * functionality.
2196 */
2197int
2198xfs_buf_delwri_submit(
2199 struct list_head *buffer_list)
2200{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002201 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002202 int error = 0, error2;
2203 struct xfs_buf *bp;
2204
Dave Chinner26f1fe82016-06-01 17:38:15 +10002205 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002206
2207 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10002208 while (!list_empty(&wait_list)) {
2209 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002210
2211 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10002212
Brian Fostere339dd82018-07-11 22:26:34 -07002213 /*
2214 * Wait on the locked buffer, check for errors and unlock and
2215 * release the delwri queue reference.
2216 */
2217 error2 = xfs_buf_iowait(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002218 xfs_buf_relse(bp);
2219 if (!error)
2220 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
2222
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002223 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
Brian Foster7912e7f2017-06-14 21:21:45 -07002226/*
2227 * Push a single buffer on a delwri queue.
2228 *
2229 * The purpose of this function is to submit a single buffer of a delwri queue
2230 * and return with the buffer still on the original queue. The waiting delwri
2231 * buffer submission infrastructure guarantees transfer of the delwri queue
2232 * buffer reference to a temporary wait list. We reuse this infrastructure to
2233 * transfer the buffer back to the original queue.
2234 *
2235 * Note the buffer transitions from the queued state, to the submitted and wait
2236 * listed state and back to the queued state during this call. The buffer
2237 * locking and queue management logic between _delwri_pushbuf() and
2238 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2239 * before returning.
2240 */
2241int
2242xfs_buf_delwri_pushbuf(
2243 struct xfs_buf *bp,
2244 struct list_head *buffer_list)
2245{
2246 LIST_HEAD (submit_list);
2247 int error;
2248
2249 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2250
2251 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2252
2253 /*
2254 * Isolate the buffer to a new local list so we can submit it for I/O
2255 * independently from the rest of the original list.
2256 */
2257 xfs_buf_lock(bp);
2258 list_move(&bp->b_list, &submit_list);
2259 xfs_buf_unlock(bp);
2260
2261 /*
2262 * Delwri submission clears the DELWRI_Q buffer flag and returns with
Brian Fostere339dd82018-07-11 22:26:34 -07002263 * the buffer on the wait list with the original reference. Rather than
Brian Foster7912e7f2017-06-14 21:21:45 -07002264 * bounce the buffer from a local wait list back to the original list
2265 * after I/O completion, reuse the original list as the wait list.
2266 */
2267 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2268
2269 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002270 * The buffer is now locked, under I/O and wait listed on the original
2271 * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2272 * return with the buffer unlocked and on the original queue.
Brian Foster7912e7f2017-06-14 21:21:45 -07002273 */
Brian Fostere339dd82018-07-11 22:26:34 -07002274 error = xfs_buf_iowait(bp);
Brian Foster7912e7f2017-06-14 21:21:45 -07002275 bp->b_flags |= _XBF_DELWRI_Q;
2276 xfs_buf_unlock(bp);
2277
2278 return error;
2279}
2280
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002281int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002282xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
Dave Chinner12eba652020-03-24 20:10:28 -07002284 xfs_buf_zone = kmem_cache_create("xfs_buf", sizeof(struct xfs_buf), 0,
2285 SLAB_HWCACHE_ALIGN |
2286 SLAB_RECLAIM_ACCOUNT |
2287 SLAB_MEM_SPREAD,
2288 NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002289 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002290 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002291
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002294 out:
Nathan Scott87582802006-03-14 13:18:19 +11002295 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298void
Nathan Scottce8e9222006-01-11 15:39:08 +11002299xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08002301 kmem_cache_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
Brian Foster7561d272017-10-17 14:16:29 -07002303
2304void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
2305{
Brian Foster7561d272017-10-17 14:16:29 -07002306 /*
2307 * Set the lru reference count to 0 based on the error injection tag.
2308 * This allows userspace to disrupt buffer caching for debug/testing
2309 * purposes.
2310 */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002311 if (XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_LRU_REF))
Brian Foster7561d272017-10-17 14:16:29 -07002312 lru_ref = 0;
2313
2314 atomic_set(&bp->b_lru_ref, lru_ref);
2315}
Brian Foster8473fee2019-02-07 10:45:46 -08002316
2317/*
2318 * Verify an on-disk magic value against the magic value specified in the
2319 * verifier structure. The verifier magic is in disk byte order so the caller is
2320 * expected to pass the value directly from disk.
2321 */
2322bool
2323xfs_verify_magic(
2324 struct xfs_buf *bp,
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002325 __be32 dmagic)
Brian Foster8473fee2019-02-07 10:45:46 -08002326{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002327 struct xfs_mount *mp = bp->b_mount;
Brian Foster8473fee2019-02-07 10:45:46 -08002328 int idx;
2329
2330 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002331 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic[idx]))
Brian Foster8473fee2019-02-07 10:45:46 -08002332 return false;
2333 return dmagic == bp->b_ops->magic[idx];
2334}
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002335/*
2336 * Verify an on-disk magic value against the magic value specified in the
2337 * verifier structure. The verifier magic is in disk byte order so the caller is
2338 * expected to pass the value directly from disk.
2339 */
2340bool
2341xfs_verify_magic16(
2342 struct xfs_buf *bp,
2343 __be16 dmagic)
2344{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002345 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002346 int idx;
2347
2348 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002349 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic16[idx]))
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002350 return false;
2351 return dmagic == bp->b_ops->magic16[idx];
2352}