blob: 5c6a7140d28de36712c96d5754b143f06bd8b4dd [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,
Dave Chinner0a683792021-06-01 13:40:02 +1000351 xfs_buf_flags_t flags)
352{
353 gfp_t gfp_mask = xb_to_gfp(flags);
Dave Chinnerc9fa5632021-06-01 13:40:36 +1000354 long filled = 0;
Dave Chinner0a683792021-06-01 13:40:02 +1000355
Dave Chinner02c51172021-06-01 13:40:36 +1000356 /* Make sure that we have a page list */
Christoph Hellwig934d10762021-06-07 11:50:00 +1000357 bp->b_page_count = DIV_ROUND_UP(BBTOB(bp->b_length), PAGE_SIZE);
Dave Chinner02c51172021-06-01 13:40:36 +1000358 if (bp->b_page_count <= XB_PAGES) {
359 bp->b_pages = bp->b_page_array;
360 } else {
361 bp->b_pages = kzalloc(sizeof(struct page *) * bp->b_page_count,
362 gfp_mask);
363 if (!bp->b_pages)
364 return -ENOMEM;
365 }
366 bp->b_flags |= _XBF_PAGES;
367
Dave Chinner0a683792021-06-01 13:40:02 +1000368 /* Assure zeroed buffer for non-read cases. */
369 if (!(flags & XBF_READ))
Bill O'Donnell3219e8c2019-10-04 16:38:44 -0700370 gfp_mask |= __GFP_ZERO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Dave Chinnerc9fa5632021-06-01 13:40:36 +1000372 /*
373 * Bulk filling of pages can take multiple calls. Not filling the entire
374 * array is not an allocation failure, so don't back off if we get at
375 * least one extra page.
376 */
377 for (;;) {
378 long last = filled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dave Chinnerc9fa5632021-06-01 13:40:36 +1000380 filled = alloc_pages_bulk_array(gfp_mask, bp->b_page_count,
381 bp->b_pages);
382 if (filled == bp->b_page_count) {
383 XFS_STATS_INC(bp->b_mount, xb_page_found);
384 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
Dave Chinnerc9fa5632021-06-01 13:40:36 +1000387 if (filled != last)
388 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Dave Chinnerc9fa5632021-06-01 13:40:36 +1000390 if (flags & XBF_READ_AHEAD) {
Dave Chinnere7d236a2021-06-01 13:40:36 +1000391 xfs_buf_free_pages(bp);
392 return -ENOMEM;
Dave Chinnerc9fa5632021-06-01 13:40:36 +1000393 }
394
395 XFS_STATS_INC(bp->b_mount, xb_page_retries);
396 congestion_wait(BLK_RW_ASYNC, HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Dave Chinner0a683792021-06-01 13:40:02 +1000401
402/*
403 * Allocates all the pages for buffer in question and builds it's page list.
404 */
405static int
406xfs_buf_allocate_memory(
407 struct xfs_buf *bp,
408 uint flags)
409{
410 size_t size;
Dave Chinner0a683792021-06-01 13:40:02 +1000411 int error;
412
413 /*
414 * For buffers that fit entirely within a single page, first attempt to
415 * allocate the memory from the heap to minimise memory usage. If we
416 * can't get heap memory for these small buffers, we fall back to using
417 * the page allocator.
418 */
419 size = BBTOB(bp->b_length);
420 if (size < PAGE_SIZE) {
421 error = xfs_buf_alloc_kmem(bp, size, flags);
422 if (!error)
423 return 0;
424 }
Christoph Hellwig934d10762021-06-07 11:50:00 +1000425 return xfs_buf_alloc_pages(bp, flags);
Dave Chinner0a683792021-06-01 13:40:02 +1000426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300429 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
431STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100432_xfs_buf_map_pages(
Dave Chinnere8222612020-12-16 16:07:34 -0800433 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 uint flags)
435{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100436 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100437 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100438 /* A single page buffer is always mappable */
Christoph Hellwig54cd3aa2021-06-07 11:49:50 +1000439 bp->b_addr = page_address(bp->b_pages[0]);
Dave Chinner611c9942012-04-23 15:59:07 +1000440 } else if (flags & XBF_UNMAPPED) {
441 bp->b_addr = NULL;
442 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100443 int retried = 0;
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700444 unsigned nofs_flag;
Dave Chinnera19fb382011-03-26 09:13:42 +1100445
Dave Chinnerae687e52014-03-07 16:19:14 +1100446 /*
Joe Perchescf085a12019-11-07 13:24:52 -0800447 * vm_map_ram() will allocate auxiliary structures (e.g.
Dave Chinnerae687e52014-03-07 16:19:14 +1100448 * pagetables) with GFP_KERNEL, yet we are likely to be under
449 * GFP_NOFS context here. Hence we need to tell memory reclaim
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700450 * that we are in such a context via PF_MEMALLOC_NOFS to prevent
Dave Chinnerae687e52014-03-07 16:19:14 +1100451 * memory reclaim re-entering the filesystem here and
452 * potentially deadlocking.
453 */
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700454 nofs_flag = memalloc_nofs_save();
Dave Chinnera19fb382011-03-26 09:13:42 +1100455 do {
456 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
Christoph Hellwigd4efd792020-06-01 21:51:27 -0700457 -1);
Dave Chinnera19fb382011-03-26 09:13:42 +1100458 if (bp->b_addr)
459 break;
460 vm_unmap_aliases();
461 } while (retried++ <= 1);
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700462 memalloc_nofs_restore(nofs_flag);
Dave Chinnera19fb382011-03-26 09:13:42 +1100463
464 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
468 return 0;
469}
470
471/*
472 * Finding and Reading Buffers
473 */
Lucas Stach6031e732016-12-07 17:36:36 +1100474static int
475_xfs_buf_obj_cmp(
476 struct rhashtable_compare_arg *arg,
477 const void *obj)
478{
479 const struct xfs_buf_map *map = arg->key;
480 const struct xfs_buf *bp = obj;
481
482 /*
483 * The key hashing in the lookup path depends on the key being the
484 * first element of the compare_arg, make sure to assert this.
485 */
486 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
487
488 if (bp->b_bn != map->bm_bn)
489 return 1;
490
491 if (unlikely(bp->b_length != map->bm_len)) {
492 /*
493 * found a block number match. If the range doesn't
494 * match, the only way this is allowed is if the buffer
495 * in the cache is stale and the transaction that made
496 * it stale has not yet committed. i.e. we are
497 * reallocating a busy extent. Skip this buffer and
498 * continue searching for an exact match.
499 */
500 ASSERT(bp->b_flags & XBF_STALE);
501 return 1;
502 }
503 return 0;
504}
505
506static const struct rhashtable_params xfs_buf_hash_params = {
507 .min_size = 32, /* empty AGs have minimal footprint */
508 .nelem_hint = 16,
509 .key_len = sizeof(xfs_daddr_t),
510 .key_offset = offsetof(struct xfs_buf, b_bn),
511 .head_offset = offsetof(struct xfs_buf, b_rhash_head),
512 .automatic_shrinking = true,
513 .obj_cmpfn = _xfs_buf_obj_cmp,
514};
515
516int
517xfs_buf_hash_init(
518 struct xfs_perag *pag)
519{
520 spin_lock_init(&pag->pag_buf_lock);
521 return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
522}
523
524void
525xfs_buf_hash_destroy(
526 struct xfs_perag *pag)
527{
528 rhashtable_destroy(&pag->pag_buf_hash);
529}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531/*
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700532 * Look up a buffer in the buffer cache and return it referenced and locked
533 * in @found_bp.
534 *
535 * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the
536 * cache.
537 *
538 * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return
539 * -EAGAIN if we fail to lock it.
540 *
541 * Return values are:
542 * -EFSCORRUPTED if have been supplied with an invalid address
543 * -EAGAIN on trylock failure
544 * -ENOENT if we fail to find a match and @new_bp was NULL
545 * 0, with @found_bp:
546 * - @new_bp if we inserted it into the cache
547 * - the buffer we found and locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700549static int
550xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000551 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000552 struct xfs_buf_map *map,
553 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100554 xfs_buf_flags_t flags,
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700555 struct xfs_buf *new_bp,
556 struct xfs_buf **found_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Dave Chinner74f75a02010-09-24 19:59:04 +1000558 struct xfs_perag *pag;
Dave Chinnere8222612020-12-16 16:07:34 -0800559 struct xfs_buf *bp;
Lucas Stach6031e732016-12-07 17:36:36 +1100560 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
Dave Chinner10616b82013-01-21 23:53:52 +1100561 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000562 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700564 *found_bp = NULL;
565
Dave Chinner3e85c862012-06-22 18:50:09 +1000566 for (i = 0; i < nmaps; i++)
Lucas Stach6031e732016-12-07 17:36:36 +1100567 cmap.bm_len += map[i].bm_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* Check for IOs smaller than the sector size / not sector aligned */
Lucas Stach6031e732016-12-07 17:36:36 +1100570 ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
571 ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Dave Chinner10616b82013-01-21 23:53:52 +1100573 /*
574 * Corrupted block numbers can get through to here, unfortunately, so we
575 * have to check that the buffer falls within the filesystem bounds.
576 */
577 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
Lucas Stach6031e732016-12-07 17:36:36 +1100578 if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
Dave Chinner10616b82013-01-21 23:53:52 +1100579 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -0800580 "%s: daddr 0x%llx out of range, EOFS 0x%llx",
Lucas Stach6031e732016-12-07 17:36:36 +1100581 __func__, cmap.bm_bn, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000582 WARN_ON(1);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700583 return -EFSCORRUPTED;
Dave Chinner10616b82013-01-21 23:53:52 +1100584 }
585
Dave Chinner74f75a02010-09-24 19:59:04 +1000586 pag = xfs_perag_get(btp->bt_mount,
Lucas Stach6031e732016-12-07 17:36:36 +1100587 xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Dave Chinner74f75a02010-09-24 19:59:04 +1000589 spin_lock(&pag->pag_buf_lock);
Lucas Stach6031e732016-12-07 17:36:36 +1100590 bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
591 xfs_buf_hash_params);
592 if (bp) {
593 atomic_inc(&bp->b_hold);
594 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
597 /* No match found */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700598 if (!new_bp) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100599 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000600 spin_unlock(&pag->pag_buf_lock);
601 xfs_perag_put(pag);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700602 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700604
605 /* the buffer keeps the perag reference until it is freed */
606 new_bp->b_pag = pag;
607 rhashtable_insert_fast(&pag->pag_buf_hash, &new_bp->b_rhash_head,
608 xfs_buf_hash_params);
609 spin_unlock(&pag->pag_buf_lock);
610 *found_bp = new_bp;
611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000614 spin_unlock(&pag->pag_buf_lock);
615 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200617 if (!xfs_buf_trylock(bp)) {
618 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100619 xfs_buf_rele(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100620 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700621 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200623 xfs_buf_lock(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100624 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
Dave Chinner0e6e8472011-03-26 09:16:45 +1100627 /*
628 * if the buffer is stale, clear all the external state associated with
629 * it. We need to keep flags such as how we allocated the buffer memory
630 * intact here.
631 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100632 if (bp->b_flags & XBF_STALE) {
633 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner611c9942012-04-23 15:59:07 +1000634 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100635 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000636 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000637
638 trace_xfs_buf_find(bp, flags, _RET_IP_);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100639 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700640 *found_bp = bp;
641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Dave Chinner8925a3d2018-04-18 08:25:20 -0700644struct xfs_buf *
645xfs_buf_incore(
646 struct xfs_buftarg *target,
647 xfs_daddr_t blkno,
648 size_t numblks,
649 xfs_buf_flags_t flags)
650{
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700651 struct xfs_buf *bp;
652 int error;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700653 DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700654
655 error = xfs_buf_find(target, &map, 1, flags, NULL, &bp);
656 if (error)
657 return NULL;
658 return bp;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700659}
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661/*
Dave Chinner38158322011-09-30 04:45:02 +0000662 * Assembles a buffer covering the specified range. The code is optimised for
663 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
664 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800666int
Dave Chinner6dde2702012-06-22 18:50:10 +1000667xfs_buf_get_map(
668 struct xfs_buftarg *target,
669 struct xfs_buf_map *map,
670 int nmaps,
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800671 xfs_buf_flags_t flags,
672 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Dave Chinner38158322011-09-30 04:45:02 +0000674 struct xfs_buf *bp;
675 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100676 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800678 *bpp = NULL;
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700679 error = xfs_buf_find(target, map, nmaps, flags, NULL, &bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800680 if (!error)
Dave Chinner38158322011-09-30 04:45:02 +0000681 goto found;
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800682 if (error != -ENOENT)
683 return error;
Dave Chinner38158322011-09-30 04:45:02 +0000684
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800685 error = _xfs_buf_alloc(target, map, nmaps, flags, &new_bp);
686 if (error)
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800687 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000689 error = xfs_buf_allocate_memory(new_bp, flags);
690 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000691 xfs_buf_free(new_bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800692 return error;
Dave Chinner38158322011-09-30 04:45:02 +0000693 }
694
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700695 error = xfs_buf_find(target, map, nmaps, flags, new_bp, &bp);
696 if (error) {
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000697 xfs_buf_free(new_bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800698 return error;
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000699 }
700
701 if (bp != new_bp)
702 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Dave Chinner38158322011-09-30 04:45:02 +0000704found:
Dave Chinner611c9942012-04-23 15:59:07 +1000705 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100706 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (unlikely(error)) {
Darrick J. Wong93baa552020-02-21 07:40:44 -0800708 xfs_warn_ratelimited(target->bt_mount,
709 "%s: failed to map %u pages", __func__,
710 bp->b_page_count);
Dave Chinnera8acad72012-04-23 15:58:54 +1000711 xfs_buf_relse(bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800712 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714 }
715
Dave Chinnerb79f4a12016-01-12 07:03:44 +1100716 /*
717 * Clear b_error if this is a lookup from a caller that doesn't expect
718 * valid data to be found in the buffer.
719 */
720 if (!(flags & XBF_READ))
721 xfs_buf_ioerror(bp, 0);
722
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100723 XFS_STATS_INC(target->bt_mount, xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000724 trace_xfs_buf_get(bp, flags, _RET_IP_);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800725 *bpp = bp;
726 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Christoph Hellwig26e328752020-09-01 10:55:47 -0700729int
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100730_xfs_buf_read(
Dave Chinnere8222612020-12-16 16:07:34 -0800731 struct xfs_buf *bp,
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100732 xfs_buf_flags_t flags)
733{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000734 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600735 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100736
Christoph Hellwig26e328752020-09-01 10:55:47 -0700737 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD | XBF_DONE);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200738 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100739
Brian Foster6af88cd2018-07-11 22:26:35 -0700740 return xfs_buf_submit(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100741}
742
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100743/*
Brian Foster75d02302019-02-06 09:25:29 -0800744 * Reverify a buffer found in cache without an attached ->b_ops.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800745 *
Brian Foster75d02302019-02-06 09:25:29 -0800746 * If the caller passed an ops structure and the buffer doesn't have ops
747 * assigned, set the ops and use it to verify the contents. If verification
748 * fails, clear XBF_DONE. We assume the buffer has no recorded errors and is
749 * already in XBF_DONE state on entry.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800750 *
Brian Foster75d02302019-02-06 09:25:29 -0800751 * Under normal operations, every in-core buffer is verified on read I/O
752 * completion. There are two scenarios that can lead to in-core buffers without
753 * an assigned ->b_ops. The first is during log recovery of buffers on a V4
754 * filesystem, though these buffers are purged at the end of recovery. The
755 * other is online repair, which intentionally reads with a NULL buffer ops to
756 * run several verifiers across an in-core buffer in order to establish buffer
757 * type. If repair can't establish that, the buffer will be left in memory
758 * with NULL buffer ops.
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100759 */
760int
Brian Foster75d02302019-02-06 09:25:29 -0800761xfs_buf_reverify(
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100762 struct xfs_buf *bp,
763 const struct xfs_buf_ops *ops)
764{
765 ASSERT(bp->b_flags & XBF_DONE);
766 ASSERT(bp->b_error == 0);
767
768 if (!ops || bp->b_ops)
769 return 0;
770
771 bp->b_ops = ops;
772 bp->b_ops->verify_read(bp);
773 if (bp->b_error)
774 bp->b_flags &= ~XBF_DONE;
775 return bp->b_error;
776}
777
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800778int
Dave Chinner6dde2702012-06-22 18:50:10 +1000779xfs_buf_read_map(
780 struct xfs_buftarg *target,
781 struct xfs_buf_map *map,
782 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100783 xfs_buf_flags_t flags,
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800784 struct xfs_buf **bpp,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800785 const struct xfs_buf_ops *ops,
786 xfs_failaddr_t fa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Dave Chinner6dde2702012-06-22 18:50:10 +1000788 struct xfs_buf *bp;
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800789 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Nathan Scottce8e9222006-01-11 15:39:08 +1100791 flags |= XBF_READ;
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800792 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800794 error = xfs_buf_get_map(target, map, nmaps, flags, &bp);
795 if (error)
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800796 return error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000797
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100798 trace_xfs_buf_read(bp, flags, _RET_IP_);
799
800 if (!(bp->b_flags & XBF_DONE)) {
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800801 /* Initiate the buffer read and wait. */
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100802 XFS_STATS_INC(target->bt_mount, xb_get_read);
803 bp->b_ops = ops;
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800804 error = _xfs_buf_read(bp, flags);
805
806 /* Readahead iodone already dropped the buffer, so exit. */
807 if (flags & XBF_ASYNC)
808 return 0;
809 } else {
810 /* Buffer already read; all we need to do is check it. */
811 error = xfs_buf_reverify(bp, ops);
812
813 /* Readahead already finished; drop the buffer and exit. */
814 if (flags & XBF_ASYNC) {
815 xfs_buf_relse(bp);
816 return 0;
817 }
818
819 /* We do not want read in the flags */
820 bp->b_flags &= ~XBF_READ;
821 ASSERT(bp->b_ops != NULL || ops == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800824 /*
825 * If we've had a read error, then the contents of the buffer are
826 * invalid and should not be used. To ensure that a followup read tries
827 * to pull the buffer from disk again, we clear the XBF_DONE flag and
828 * mark the buffer stale. This ensures that anyone who has a current
829 * reference to the buffer will interpret it's contents correctly and
830 * future cache lookups will also treat it as an empty, uninitialised
831 * buffer.
832 */
833 if (error) {
834 if (!XFS_FORCED_SHUTDOWN(target->bt_mount))
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800835 xfs_buf_ioerror_alert(bp, fa);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100836
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800837 bp->b_flags &= ~XBF_DONE;
838 xfs_buf_stale(bp);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100839 xfs_buf_relse(bp);
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800840
841 /* bad CRC means corrupted metadata */
842 if (error == -EFSBADCRC)
843 error = -EFSCORRUPTED;
844 return error;
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100845 }
846
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800847 *bpp = bp;
848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100852 * If we are not low on memory then do the readahead in a deadlock
853 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
855void
Dave Chinner6dde2702012-06-22 18:50:10 +1000856xfs_buf_readahead_map(
857 struct xfs_buftarg *target,
858 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100859 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100860 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800862 struct xfs_buf *bp;
863
Jan Karaefa7c9f2017-02-02 15:56:53 +0100864 if (bdi_read_congested(target->bt_bdev->bd_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return;
866
Dave Chinner6dde2702012-06-22 18:50:10 +1000867 xfs_buf_read_map(target, map, nmaps,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800868 XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD, &bp, ops,
869 __this_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
Dave Chinner5adc94c2010-09-24 21:58:31 +1000872/*
873 * Read an uncached buffer from disk. Allocates and returns a locked
874 * buffer containing the disk contents or nothing.
875 */
Dave Chinnerba3726742014-10-02 09:05:32 +1000876int
Dave Chinner5adc94c2010-09-24 21:58:31 +1000877xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000878 struct xfs_buftarg *target,
879 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000880 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100881 int flags,
Dave Chinnerba3726742014-10-02 09:05:32 +1000882 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100883 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000884{
Dave Chinnereab4e632012-11-12 22:54:02 +1100885 struct xfs_buf *bp;
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800886 int error;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000887
Dave Chinnerba3726742014-10-02 09:05:32 +1000888 *bpp = NULL;
889
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800890 error = xfs_buf_get_uncached(target, numblks, flags, &bp);
891 if (error)
892 return error;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000893
894 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000895 ASSERT(bp->b_map_count == 1);
Dave Chinnerba3726742014-10-02 09:05:32 +1000896 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
Dave Chinner3e85c862012-06-22 18:50:09 +1000897 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000898 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100899 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000900
Brian Foster6af88cd2018-07-11 22:26:35 -0700901 xfs_buf_submit(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000902 if (bp->b_error) {
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800903 error = bp->b_error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800904 xfs_buf_relse(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000905 return error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800906 }
Dave Chinnerba3726742014-10-02 09:05:32 +1000907
908 *bpp = bp;
909 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800912int
Dave Chinner686865f2010-09-24 20:07:47 +1000913xfs_buf_get_uncached(
914 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000915 size_t numblks,
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800916 int flags,
917 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Dave Chinner07b5c5a2021-06-01 13:40:35 +1000919 int error;
Dave Chinner3e85c862012-06-22 18:50:09 +1000920 struct xfs_buf *bp;
921 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800923 *bpp = NULL;
924
Brian Fosterc891c302016-07-20 11:13:43 +1000925 /* flags might contain irrelevant bits, pass only what we care about */
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800926 error = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT, &bp);
927 if (error)
Dave Chinner07b5c5a2021-06-01 13:40:35 +1000928 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Christoph Hellwig934d10762021-06-07 11:50:00 +1000930 error = xfs_buf_alloc_pages(bp, flags);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000931 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 goto fail_free_buf;
933
Dave Chinner611c9942012-04-23 15:59:07 +1000934 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000935 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100936 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500937 "%s: failed to map pages", __func__);
Dave Chinner07b5c5a2021-06-01 13:40:35 +1000938 goto fail_free_buf;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Dave Chinner686865f2010-09-24 20:07:47 +1000941 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800942 *bpp = bp;
943 return 0;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000944
Dave Chinner07b5c5a2021-06-01 13:40:35 +1000945fail_free_buf:
946 xfs_buf_free(bp);
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800947 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
950/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 * Increment reference count on buffer, to hold the buffer concurrently
952 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 * Must hold the buffer already to call this function.
954 */
955void
Nathan Scottce8e9222006-01-11 15:39:08 +1100956xfs_buf_hold(
Dave Chinnere8222612020-12-16 16:07:34 -0800957 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000959 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100960 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963/*
Brian Foster9c7504a2016-07-20 11:15:28 +1000964 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
965 * placed on LRU or freed (depending on b_lru_ref).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 */
967void
Nathan Scottce8e9222006-01-11 15:39:08 +1100968xfs_buf_rele(
Dave Chinnere8222612020-12-16 16:07:34 -0800969 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Dave Chinner74f75a02010-09-24 19:59:04 +1000971 struct xfs_perag *pag = bp->b_pag;
Brian Foster9c7504a2016-07-20 11:15:28 +1000972 bool release;
973 bool freebuf = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000975 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Dave Chinner74f75a02010-09-24 19:59:04 +1000977 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100978 ASSERT(list_empty(&bp->b_lru));
Brian Foster9c7504a2016-07-20 11:15:28 +1000979 if (atomic_dec_and_test(&bp->b_hold)) {
980 xfs_buf_ioacct_dec(bp);
Nathan Scottfad3aa12006-02-01 12:14:52 +1100981 xfs_buf_free(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +1000982 }
Nathan Scottfad3aa12006-02-01 12:14:52 +1100983 return;
984 }
985
Lachlan McIlroy37906892008-08-13 15:42:10 +1000986 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinnera4082352013-08-28 10:18:06 +1000987
Dave Chinner37fd1672018-10-18 17:21:29 +1100988 /*
989 * We grab the b_lock here first to serialise racing xfs_buf_rele()
990 * calls. The pag_buf_lock being taken on the last reference only
991 * serialises against racing lookups in xfs_buf_find(). IOWs, the second
992 * to last reference we drop here is not serialised against the last
993 * reference until we take bp->b_lock. Hence if we don't grab b_lock
994 * first, the last "release" reference can win the race to the lock and
995 * free the buffer before the second-to-last reference is processed,
996 * leading to a use-after-free scenario.
997 */
Brian Foster9c7504a2016-07-20 11:15:28 +1000998 spin_lock(&bp->b_lock);
Dave Chinner37fd1672018-10-18 17:21:29 +1100999 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +10001000 if (!release) {
1001 /*
1002 * Drop the in-flight state if the buffer is already on the LRU
1003 * and it holds the only reference. This is racy because we
1004 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1005 * ensures the decrement occurs only once per-buf.
1006 */
1007 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
Brian Foster63db7c82017-05-31 08:22:52 -07001008 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001009 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
Brian Foster9c7504a2016-07-20 11:15:28 +10001011
1012 /* the last reference has been dropped ... */
Brian Foster63db7c82017-05-31 08:22:52 -07001013 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001014 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1015 /*
1016 * If the buffer is added to the LRU take a new reference to the
1017 * buffer for the LRU and clear the (now stale) dispose list
1018 * state flag
1019 */
1020 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1021 bp->b_state &= ~XFS_BSTATE_DISPOSE;
1022 atomic_inc(&bp->b_hold);
1023 }
1024 spin_unlock(&pag->pag_buf_lock);
1025 } else {
1026 /*
1027 * most of the time buffers will already be removed from the
1028 * LRU, so optimise that case by checking for the
1029 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1030 * was on was the disposal list
1031 */
1032 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1033 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1034 } else {
1035 ASSERT(list_empty(&bp->b_lru));
1036 }
1037
1038 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Lucas Stach6031e732016-12-07 17:36:36 +11001039 rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
1040 xfs_buf_hash_params);
Brian Foster9c7504a2016-07-20 11:15:28 +10001041 spin_unlock(&pag->pag_buf_lock);
1042 xfs_perag_put(pag);
1043 freebuf = true;
1044 }
1045
1046out_unlock:
1047 spin_unlock(&bp->b_lock);
1048
1049 if (freebuf)
1050 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053
1054/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001055 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +11001056 *
1057 * If we come across a stale, pinned, locked buffer, we know that we are
1058 * being asked to lock a buffer that has been reallocated. Because it is
1059 * pinned, we know that the log has not been pushed to disk and hence it
1060 * will still be locked. Rather than continuing to have trylock attempts
1061 * fail until someone else pushes the log, push it ourselves before
1062 * returning. This means that the xfsaild will not get stuck trying
1063 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 */
1065int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001066xfs_buf_trylock(
1067 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 int locked;
1070
Nathan Scottce8e9222006-01-11 15:39:08 +11001071 locked = down_trylock(&bp->b_sema) == 0;
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001072 if (locked)
Darrick J. Wong479c6412016-06-21 11:53:28 +10001073 trace_xfs_buf_trylock(bp, _RET_IP_);
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001074 else
Darrick J. Wong479c6412016-06-21 11:53:28 +10001075 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001076 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001080 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +10001081 *
1082 * If we come across a stale, pinned, locked buffer, we know that we
1083 * are being asked to lock a buffer that has been reallocated. Because
1084 * it is pinned, we know that the log has not been pushed to disk and
1085 * hence it will still be locked. Rather than sleeping until someone
1086 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001088void
1089xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001090 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001092 trace_xfs_buf_lock(bp, _RET_IP_);
1093
Dave Chinnered3b4d62010-05-21 12:07:08 +10001094 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001095 xfs_log_force(bp->b_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +11001096 down(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001097
1098 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101void
Nathan Scottce8e9222006-01-11 15:39:08 +11001102xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001103 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Brian Foster20e8a062017-04-21 12:40:44 -07001105 ASSERT(xfs_buf_islocked(bp));
1106
Nathan Scottce8e9222006-01-11 15:39:08 +11001107 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001108 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
Nathan Scottce8e9222006-01-11 15:39:08 +11001111STATIC void
1112xfs_buf_wait_unpin(
Dave Chinnere8222612020-12-16 16:07:34 -08001113 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 DECLARE_WAITQUEUE (wait, current);
1116
Nathan Scottce8e9222006-01-11 15:39:08 +11001117 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return;
1119
Nathan Scottce8e9222006-01-11 15:39:08 +11001120 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 for (;;) {
1122 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001123 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001125 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001127 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 set_current_state(TASK_RUNNING);
1129}
1130
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001131static void
1132xfs_buf_ioerror_alert_ratelimited(
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001133 struct xfs_buf *bp)
1134{
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001135 static unsigned long lasttime;
1136 static struct xfs_buftarg *lasttarg;
1137
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001138 if (bp->b_target != lasttarg ||
1139 time_after(jiffies, (lasttime + 5*HZ))) {
1140 lasttime = jiffies;
1141 xfs_buf_ioerror_alert(bp, __this_address);
1142 }
1143 lasttarg = bp->b_target;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001144}
1145
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001146/*
1147 * Account for this latest trip around the retry handler, and decide if
1148 * we've failed enough times to constitute a permanent failure.
1149 */
1150static bool
1151xfs_buf_ioerror_permanent(
1152 struct xfs_buf *bp,
1153 struct xfs_error_cfg *cfg)
1154{
1155 struct xfs_mount *mp = bp->b_mount;
1156
1157 if (cfg->max_retries != XFS_ERR_RETRY_FOREVER &&
1158 ++bp->b_retries > cfg->max_retries)
1159 return true;
1160 if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
1161 time_after(jiffies, cfg->retry_timeout + bp->b_first_retry_time))
1162 return true;
1163
1164 /* At unmount we may treat errors differently */
1165 if ((mp->m_flags & XFS_MOUNT_UNMOUNTING) && mp->m_fail_unmount)
1166 return true;
1167
1168 return false;
1169}
1170
1171/*
1172 * On a sync write or shutdown we just want to stale the buffer and let the
1173 * caller handle the error in bp->b_error appropriately.
1174 *
1175 * If the write was asynchronous then no one will be looking for the error. If
1176 * this is the first failure of this type, clear the error state and write the
1177 * buffer out again. This means we always retry an async write failure at least
1178 * once, but we also need to set the buffer up to behave correctly now for
1179 * repeated failures.
1180 *
1181 * If we get repeated async write failures, then we take action according to the
1182 * error configuration we have been set up to use.
1183 *
Christoph Hellwig70796c62020-09-01 10:55:45 -07001184 * Returns true if this function took care of error handling and the caller must
1185 * not touch the buffer again. Return false if the caller should proceed with
1186 * normal I/O completion handling.
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001187 */
Christoph Hellwig70796c62020-09-01 10:55:45 -07001188static bool
1189xfs_buf_ioend_handle_error(
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001190 struct xfs_buf *bp)
1191{
1192 struct xfs_mount *mp = bp->b_mount;
1193 struct xfs_error_cfg *cfg;
1194
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001195 /*
1196 * If we've already decided to shutdown the filesystem because of I/O
1197 * errors, there's no point in giving this a retry.
1198 */
1199 if (XFS_FORCED_SHUTDOWN(mp))
1200 goto out_stale;
1201
1202 xfs_buf_ioerror_alert_ratelimited(bp);
1203
1204 /*
Christoph Hellwig22c10582020-09-01 10:55:46 -07001205 * We're not going to bother about retrying this during recovery.
1206 * One strike!
1207 */
1208 if (bp->b_flags & _XBF_LOGRECOVERY) {
1209 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1210 return false;
1211 }
1212
1213 /*
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001214 * Synchronous writes will have callers process the error.
1215 */
1216 if (!(bp->b_flags & XBF_ASYNC))
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001217 goto out_stale;
1218
1219 trace_xfs_buf_iodone_async(bp, _RET_IP_);
1220
1221 cfg = xfs_error_get_cfg(mp, XFS_ERR_METADATA, bp->b_error);
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001222 if (bp->b_last_error != bp->b_error ||
1223 !(bp->b_flags & (XBF_STALE | XBF_WRITE_FAIL))) {
1224 bp->b_last_error = bp->b_error;
1225 if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
1226 !bp->b_first_retry_time)
1227 bp->b_first_retry_time = jiffies;
1228 goto resubmit;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001229 }
1230
1231 /*
1232 * Permanent error - we need to trigger a shutdown if we haven't already
1233 * to indicate that inconsistency will result from this action.
1234 */
1235 if (xfs_buf_ioerror_permanent(bp, cfg)) {
1236 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1237 goto out_stale;
1238 }
1239
1240 /* Still considered a transient error. Caller will schedule retries. */
Christoph Hellwig844c9352020-09-01 10:55:45 -07001241 if (bp->b_flags & _XBF_INODES)
1242 xfs_buf_inode_io_fail(bp);
1243 else if (bp->b_flags & _XBF_DQUOTS)
1244 xfs_buf_dquot_io_fail(bp);
1245 else
1246 ASSERT(list_empty(&bp->b_li_list));
1247 xfs_buf_ioerror(bp, 0);
1248 xfs_buf_relse(bp);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001249 return true;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001250
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001251resubmit:
1252 xfs_buf_ioerror(bp, 0);
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001253 bp->b_flags |= (XBF_DONE | XBF_WRITE_FAIL);
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001254 xfs_buf_submit(bp);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001255 return true;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001256out_stale:
1257 xfs_buf_stale(bp);
1258 bp->b_flags |= XBF_DONE;
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001259 bp->b_flags &= ~XBF_WRITE;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001260 trace_xfs_buf_error_relse(bp, _RET_IP_);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001261 return false;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001262}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Christoph Hellwig76b2d322020-09-01 10:55:20 -07001264static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001265xfs_buf_ioend(
1266 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001268 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001269
Dave Chinner61be9c52014-10-02 09:04:31 +10001270 /*
1271 * Pull in IO completion errors now. We are guaranteed to be running
1272 * single threaded, so we don't need the lock to read b_io_error.
1273 */
1274 if (!bp->b_error && bp->b_io_error)
1275 xfs_buf_ioerror(bp, bp->b_io_error);
1276
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001277 if (bp->b_flags & XBF_READ) {
Dave Chinnerb01d1462020-06-29 14:48:47 -07001278 if (!bp->b_error && bp->b_ops)
1279 bp->b_ops->verify_read(bp);
1280 if (!bp->b_error)
1281 bp->b_flags |= XBF_DONE;
Christoph Hellwig23fb5a92020-09-01 10:55:20 -07001282 } else {
1283 if (!bp->b_error) {
1284 bp->b_flags &= ~XBF_WRITE_FAIL;
1285 bp->b_flags |= XBF_DONE;
1286 }
Dave Chinner9fe5c772020-06-29 14:48:47 -07001287
Christoph Hellwig70796c62020-09-01 10:55:45 -07001288 if (unlikely(bp->b_error) && xfs_buf_ioend_handle_error(bp))
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001289 return;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001290
1291 /* clear the retry state */
1292 bp->b_last_error = 0;
1293 bp->b_retries = 0;
1294 bp->b_first_retry_time = 0;
1295
1296 /*
1297 * Note that for things like remote attribute buffers, there may
1298 * not be a buffer log item here, so processing the buffer log
1299 * item must remain optional.
1300 */
1301 if (bp->b_log_item)
1302 xfs_buf_item_done(bp);
1303
Christoph Hellwig23fb5a92020-09-01 10:55:20 -07001304 if (bp->b_flags & _XBF_INODES)
1305 xfs_buf_inode_iodone(bp);
1306 else if (bp->b_flags & _XBF_DQUOTS)
1307 xfs_buf_dquot_iodone(bp);
Christoph Hellwig22c10582020-09-01 10:55:46 -07001308
Dave Chinnerf593bf12020-06-29 14:48:46 -07001309 }
Christoph Hellwig6a7584b2020-09-01 10:55:44 -07001310
Christoph Hellwig22c10582020-09-01 10:55:46 -07001311 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD |
1312 _XBF_LOGRECOVERY);
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001313
Christoph Hellwig6a7584b2020-09-01 10:55:44 -07001314 if (bp->b_flags & XBF_ASYNC)
1315 xfs_buf_relse(bp);
1316 else
1317 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
Dave Chinnere8aaba92014-10-02 09:04:22 +10001320static void
1321xfs_buf_ioend_work(
1322 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001324 struct xfs_buf *bp =
Dave Chinnere8222612020-12-16 16:07:34 -08001325 container_of(work, struct xfs_buf, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001326
Dave Chinnere8aaba92014-10-02 09:04:22 +10001327 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001330static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001331xfs_buf_ioend_async(
1332 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001334 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001335 queue_work(bp->b_mount->m_buf_workqueue, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338void
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001339__xfs_buf_ioerror(
Dave Chinnere8222612020-12-16 16:07:34 -08001340 struct xfs_buf *bp,
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001341 int error,
1342 xfs_failaddr_t failaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Dave Chinner24513372014-06-25 14:58:08 +10001344 ASSERT(error <= 0 && error >= -1000);
1345 bp->b_error = error;
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001346 trace_xfs_buf_ioerror(bp, error, failaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Christoph Hellwig901796a2011-10-10 16:52:49 +00001349void
1350xfs_buf_ioerror_alert(
1351 struct xfs_buf *bp,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -08001352 xfs_failaddr_t func)
Christoph Hellwig901796a2011-10-10 16:52:49 +00001353{
Brian Fosterf9bccfc2020-05-06 13:25:21 -07001354 xfs_buf_alert_ratelimited(bp, "XFS: metadata IO error",
1355 "metadata I/O error in \"%pS\" at daddr 0x%llx len %d error %d",
1356 func, (uint64_t)XFS_BUF_ADDR(bp),
1357 bp->b_length, -bp->b_error);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001358}
1359
Brian Foster54b3b1f2020-05-06 13:25:19 -07001360/*
1361 * To simulate an I/O failure, the buffer must be locked and held with at least
1362 * three references. The LRU reference is dropped by the stale call. The buf
1363 * item reference is dropped via ioend processing. The third reference is owned
1364 * by the caller and is dropped on I/O completion if the buffer is XBF_ASYNC.
1365 */
1366void
1367xfs_buf_ioend_fail(
1368 struct xfs_buf *bp)
1369{
1370 bp->b_flags &= ~XBF_DONE;
1371 xfs_buf_stale(bp);
1372 xfs_buf_ioerror(bp, -EIO);
1373 xfs_buf_ioend(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001374}
Christoph Hellwig939d7232010-07-20 17:51:16 +10001375
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001376int
1377xfs_bwrite(
1378 struct xfs_buf *bp)
1379{
1380 int error;
1381
1382 ASSERT(xfs_buf_islocked(bp));
1383
1384 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001385 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
Brian Fosterb6983e82020-05-06 13:25:20 -07001386 XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001387
Brian Foster6af88cd2018-07-11 22:26:35 -07001388 error = xfs_buf_submit(bp);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001389 if (error)
1390 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001391 return error;
1392}
1393
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001394static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001395xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001396 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001398 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Brian Foster7376d742020-05-06 13:29:19 -07001400 if (!bio->bi_status &&
1401 (bp->b_flags & XBF_WRITE) && (bp->b_flags & XBF_ASYNC) &&
Brian Foster43dc0aa2020-05-08 08:50:52 -07001402 XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_IOERROR))
Brian Foster7376d742020-05-06 13:29:19 -07001403 bio->bi_status = BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Dave Chinner37eb17e2012-11-12 22:09:46 +11001405 /*
1406 * don't overwrite existing errors - otherwise we can lose errors on
1407 * buffers that require multiple bios to complete.
1408 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001409 if (bio->bi_status) {
1410 int error = blk_status_to_errno(bio->bi_status);
1411
1412 cmpxchg(&bp->b_io_error, 0, error);
1413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Dave Chinner37eb17e2012-11-12 22:09:46 +11001415 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001416 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1417
Dave Chinnere8aaba92014-10-02 09:04:22 +10001418 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1419 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Dave Chinner3e85c862012-06-22 18:50:09 +10001423static void
1424xfs_buf_ioapply_map(
1425 struct xfs_buf *bp,
1426 int map,
1427 int *buf_offset,
1428 int *count,
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001429 int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Dave Chinner3e85c862012-06-22 18:50:09 +10001431 int page_index;
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +00001432 unsigned int total_nr_pages = bp->b_page_count;
Dave Chinner3e85c862012-06-22 18:50:09 +10001433 int nr_pages;
1434 struct bio *bio;
1435 sector_t sector = bp->b_maps[map].bm_bn;
1436 int size;
1437 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Dave Chinner3e85c862012-06-22 18:50:09 +10001439 /* skip the pages in the buffer before the start offset */
1440 page_index = 0;
1441 offset = *buf_offset;
1442 while (offset >= PAGE_SIZE) {
1443 page_index++;
1444 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001445 }
1446
Dave Chinner3e85c862012-06-22 18:50:09 +10001447 /*
1448 * Limit the IO size to the length of the current vector, and update the
1449 * remaining IO count for the next time around.
1450 */
1451 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1452 *count -= size;
1453 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001456 atomic_inc(&bp->b_io_remaining);
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +00001457 nr_pages = bio_max_segs(total_nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 bio = bio_alloc(GFP_NOIO, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001460 bio_set_dev(bio, bp->b_target->bt_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001461 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001462 bio->bi_end_io = xfs_buf_bio_end_io;
1463 bio->bi_private = bp;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001464 bio->bi_opf = op;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001465
Dave Chinner3e85c862012-06-22 18:50:09 +10001466 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001467 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 if (nbytes > size)
1470 nbytes = size;
1471
Dave Chinner3e85c862012-06-22 18:50:09 +10001472 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1473 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001474 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 break;
1476
1477 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001478 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 size -= nbytes;
1480 total_nr_pages--;
1481 }
1482
Kent Overstreet4f024f32013-10-11 15:44:27 -07001483 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001484 if (xfs_buf_is_vmapped(bp)) {
1485 flush_kernel_vmap_range(bp->b_addr,
1486 xfs_buf_vmap_len(bp));
1487 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001488 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (size)
1490 goto next_chunk;
1491 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001492 /*
1493 * This is guaranteed not to be the last io reference count
Dave Chinner595bff72014-10-02 09:05:14 +10001494 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001495 */
1496 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001497 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001498 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001500
1501}
1502
1503STATIC void
1504_xfs_buf_ioapply(
1505 struct xfs_buf *bp)
1506{
1507 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001508 int op;
Dave Chinner3e85c862012-06-22 18:50:09 +10001509 int offset;
1510 int size;
1511 int i;
1512
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001513 /*
1514 * Make sure we capture only current IO errors rather than stale errors
1515 * left over from previous use of the buffer (e.g. failed readahead).
1516 */
1517 bp->b_error = 0;
1518
Dave Chinner3e85c862012-06-22 18:50:09 +10001519 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001520 op = REQ_OP_WRITE;
Dave Chinner1813dd62012-11-14 17:54:40 +11001521
1522 /*
1523 * Run the write verifier callback function if it exists. If
1524 * this function fails it will mark the buffer with an error and
1525 * the IO should not be dispatched.
1526 */
1527 if (bp->b_ops) {
1528 bp->b_ops->verify_write(bp);
1529 if (bp->b_error) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001530 xfs_force_shutdown(bp->b_mount,
Dave Chinner1813dd62012-11-14 17:54:40 +11001531 SHUTDOWN_CORRUPT_INCORE);
1532 return;
1533 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001534 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001535 struct xfs_mount *mp = bp->b_mount;
Dave Chinner400b9d82014-08-04 12:42:40 +10001536
1537 /*
1538 * non-crc filesystems don't attach verifiers during
1539 * log recovery, so don't warn for such filesystems.
1540 */
1541 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1542 xfs_warn(mp,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001543 "%s: no buf ops on daddr 0x%llx len %d",
Dave Chinner400b9d82014-08-04 12:42:40 +10001544 __func__, bp->b_bn, bp->b_length);
Darrick J. Wong9c712a12018-01-08 10:51:26 -08001545 xfs_hex_dump(bp->b_addr,
1546 XFS_CORRUPTION_DUMP_LEN);
Dave Chinner400b9d82014-08-04 12:42:40 +10001547 dump_stack();
1548 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001549 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001550 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001551 op = REQ_OP_READ;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001552 if (bp->b_flags & XBF_READ_AHEAD)
1553 op |= REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001554 }
1555
1556 /* we only use the buffer cache for meta-data */
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001557 op |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001558
1559 /*
1560 * Walk all the vectors issuing IO on them. Set up the initial offset
1561 * into the buffer and the desired IO size before we start -
1562 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1563 * subsequent call.
1564 */
1565 offset = bp->b_offset;
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001566 size = BBTOB(bp->b_length);
Dave Chinner3e85c862012-06-22 18:50:09 +10001567 blk_start_plug(&plug);
1568 for (i = 0; i < bp->b_map_count; i++) {
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001569 xfs_buf_ioapply_map(bp, i, &offset, &size, op);
Dave Chinner3e85c862012-06-22 18:50:09 +10001570 if (bp->b_error)
1571 break;
1572 if (size <= 0)
1573 break; /* all done */
1574 }
1575 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
Dave Chinner595bff72014-10-02 09:05:14 +10001578/*
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001579 * Wait for I/O completion of a sync buffer and return the I/O error code.
Dave Chinner595bff72014-10-02 09:05:14 +10001580 */
Brian Fostereaebb512018-07-11 22:26:34 -07001581static int
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001582xfs_buf_iowait(
Dave Chinner595bff72014-10-02 09:05:14 +10001583 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001585 ASSERT(!(bp->b_flags & XBF_ASYNC));
1586
1587 trace_xfs_buf_iowait(bp, _RET_IP_);
1588 wait_for_completion(&bp->b_iowait);
1589 trace_xfs_buf_iowait_done(bp, _RET_IP_);
1590
1591 return bp->b_error;
1592}
1593
1594/*
1595 * Buffer I/O submission path, read or write. Asynchronous submission transfers
1596 * the buffer lock ownership and the current reference to the IO. It is not
1597 * safe to reference the buffer after a call to this function unless the caller
1598 * holds an additional reference itself.
1599 */
Christoph Hellwig26e328752020-09-01 10:55:47 -07001600static int
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001601__xfs_buf_submit(
1602 struct xfs_buf *bp,
1603 bool wait)
1604{
1605 int error = 0;
1606
Dave Chinner595bff72014-10-02 09:05:14 +10001607 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001609 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff72014-10-02 09:05:14 +10001610
1611 /* on shutdown we stale and complete the buffer immediately */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001612 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
Brian Foster54b3b1f2020-05-06 13:25:19 -07001613 xfs_buf_ioend_fail(bp);
Brian Fostereaebb512018-07-11 22:26:34 -07001614 return -EIO;
Dave Chinner595bff72014-10-02 09:05:14 +10001615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001617 /*
1618 * Grab a reference so the buffer does not go away underneath us. For
1619 * async buffers, I/O completion drops the callers reference, which
1620 * could occur before submission returns.
1621 */
1622 xfs_buf_hold(bp);
1623
Christoph Hellwig375ec692011-08-23 08:28:03 +00001624 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001625 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Dave Chinner61be9c52014-10-02 09:04:31 +10001627 /* clear the internal error state to avoid spurious errors */
1628 bp->b_io_error = 0;
1629
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001630 /*
Brian Fostereaebb512018-07-11 22:26:34 -07001631 * Set the count to 1 initially, this will stop an I/O completion
1632 * callout which happens before we have started all the I/O from calling
1633 * xfs_buf_ioend too early.
1634 */
1635 atomic_set(&bp->b_io_remaining, 1);
1636 if (bp->b_flags & XBF_ASYNC)
1637 xfs_buf_ioacct_inc(bp);
1638 _xfs_buf_ioapply(bp);
1639
1640 /*
1641 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1642 * reference we took above. If we drop it to zero, run completion so
1643 * that we don't return to the caller with completion still pending.
1644 */
1645 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1646 if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
1647 xfs_buf_ioend(bp);
1648 else
1649 xfs_buf_ioend_async(bp);
1650 }
1651
Brian Foster6af88cd2018-07-11 22:26:35 -07001652 if (wait)
1653 error = xfs_buf_iowait(bp);
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001654
Dave Chinner595bff72014-10-02 09:05:14 +10001655 /*
Brian Foster6af88cd2018-07-11 22:26:35 -07001656 * Release the hold that keeps the buffer referenced for the entire
1657 * I/O. Note that if the buffer is async, it is not safe to reference
1658 * after this release.
Dave Chinner595bff72014-10-02 09:05:14 +10001659 */
1660 xfs_buf_rele(bp);
1661 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001664void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001665xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001666 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 size_t offset)
1668{
1669 struct page *page;
1670
Dave Chinner611c9942012-04-23 15:59:07 +10001671 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001672 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Dave Chinner0e6e8472011-03-26 09:16:45 +11001674 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001675 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678void
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001679xfs_buf_zero(
1680 struct xfs_buf *bp,
1681 size_t boff,
1682 size_t bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Dave Chinner795cac72012-04-23 15:58:53 +10001684 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 bend = boff + bsize;
1687 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001688 struct page *page;
1689 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Dave Chinner795cac72012-04-23 15:58:53 +10001691 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1692 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1693 page = bp->b_pages[page_index];
1694 csize = min_t(size_t, PAGE_SIZE - page_offset,
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001695 BBTOB(bp->b_length) - boff);
Dave Chinner795cac72012-04-23 15:58:53 +10001696
1697 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001699 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 boff += csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703}
1704
1705/*
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001706 * Log a message about and stale a buffer that a caller has decided is corrupt.
1707 *
1708 * This function should be called for the kinds of metadata corruption that
1709 * cannot be detect from a verifier, such as incorrect inter-block relationship
1710 * data. Do /not/ call this function from a verifier function.
1711 *
1712 * The buffer must be XBF_DONE prior to the call. Afterwards, the buffer will
1713 * be marked stale, but b_error will not be set. The caller is responsible for
1714 * releasing the buffer or fixing it.
1715 */
1716void
1717__xfs_buf_mark_corrupt(
1718 struct xfs_buf *bp,
1719 xfs_failaddr_t fa)
1720{
1721 ASSERT(bp->b_flags & XBF_DONE);
1722
Darrick J. Wonge83cf872020-03-11 10:37:54 -07001723 xfs_buf_corruption_error(bp, fa);
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001724 xfs_buf_stale(bp);
1725}
1726
1727/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001728 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 */
1730
1731/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001732 * Wait for any bufs with callbacks that have been submitted but have not yet
1733 * returned. These buffers will have an elevated hold count, so wait on those
1734 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001736static enum lru_status
Brian Foster10fb9ac2021-01-22 16:48:19 -08001737xfs_buftarg_drain_rele(
Dave Chinnere80dfa12013-08-28 10:18:05 +10001738 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001739 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001740 spinlock_t *lru_lock,
1741 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Dave Chinnere80dfa12013-08-28 10:18:05 +10001743{
1744 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001745 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001746
1747 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001748 /* need to wait, so skip it this pass */
Brian Foster10fb9ac2021-01-22 16:48:19 -08001749 trace_xfs_buf_drain_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001750 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
Dave Chinnera4082352013-08-28 10:18:06 +10001752 if (!spin_trylock(&bp->b_lock))
1753 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001754
Dave Chinnera4082352013-08-28 10:18:06 +10001755 /*
1756 * clear the LRU reference count so the buffer doesn't get
1757 * ignored in xfs_buf_rele().
1758 */
1759 atomic_set(&bp->b_lru_ref, 0);
1760 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001761 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001762 spin_unlock(&bp->b_lock);
1763 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
Brian Foster8321ddb2021-01-22 16:48:20 -08001766/*
1767 * Wait for outstanding I/O on the buftarg to complete.
1768 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001769void
Brian Foster8321ddb2021-01-22 16:48:20 -08001770xfs_buftarg_wait(
Dave Chinnere80dfa12013-08-28 10:18:05 +10001771 struct xfs_buftarg *btp)
1772{
Dave Chinner85bec542016-01-19 08:28:10 +11001773 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001774 * First wait on the buftarg I/O count for all in-flight buffers to be
1775 * released. This is critical as new buffers do not make the LRU until
1776 * they are released.
1777 *
1778 * Next, flush the buffer workqueue to ensure all completion processing
1779 * has finished. Just waiting on buffer locks is not sufficient for
1780 * async IO as the reference count held over IO is not released until
1781 * after the buffer lock is dropped. Hence we need to ensure here that
1782 * all reference counts have been dropped before we start walking the
1783 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001784 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001785 while (percpu_counter_sum(&btp->bt_io_count))
1786 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001787 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Brian Foster8321ddb2021-01-22 16:48:20 -08001788}
1789
1790void
1791xfs_buftarg_drain(
1792 struct xfs_buftarg *btp)
1793{
1794 LIST_HEAD(dispose);
1795 int loop = 0;
1796 bool write_fail = false;
1797
1798 xfs_buftarg_wait(btp);
Dave Chinner85bec542016-01-19 08:28:10 +11001799
Dave Chinnera4082352013-08-28 10:18:06 +10001800 /* loop until there is nothing left on the lru list. */
1801 while (list_lru_count(&btp->bt_lru)) {
Brian Foster10fb9ac2021-01-22 16:48:19 -08001802 list_lru_walk(&btp->bt_lru, xfs_buftarg_drain_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001803 &dispose, LONG_MAX);
1804
1805 while (!list_empty(&dispose)) {
1806 struct xfs_buf *bp;
1807 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1808 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001809 if (bp->b_flags & XBF_WRITE_FAIL) {
Brian Foster61948b62020-05-06 13:25:21 -07001810 write_fail = true;
1811 xfs_buf_alert_ratelimited(bp,
1812 "XFS: Corruption Alert",
Darrick J. Wongc219b012018-01-08 11:39:18 -08001813"Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001814 (long long)bp->b_bn);
1815 }
Dave Chinnera4082352013-08-28 10:18:06 +10001816 xfs_buf_rele(bp);
1817 }
1818 if (loop++ != 0)
1819 delay(100);
1820 }
Brian Foster61948b62020-05-06 13:25:21 -07001821
1822 /*
1823 * If one or more failed buffers were freed, that means dirty metadata
1824 * was thrown away. This should only ever happen after I/O completion
1825 * handling has elevated I/O error(s) to permanent failures and shuts
1826 * down the fs.
1827 */
1828 if (write_fail) {
1829 ASSERT(XFS_FORCED_SHUTDOWN(btp->bt_mount));
1830 xfs_alert(btp->bt_mount,
1831 "Please run xfs_repair to determine the extent of the problem.");
1832 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001833}
1834
1835static enum lru_status
1836xfs_buftarg_isolate(
1837 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001838 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001839 spinlock_t *lru_lock,
1840 void *arg)
1841{
1842 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1843 struct list_head *dispose = arg;
1844
1845 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001846 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1847 * If we fail to get the lock, just skip it.
1848 */
1849 if (!spin_trylock(&bp->b_lock))
1850 return LRU_SKIP;
1851 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001852 * Decrement the b_lru_ref count unless the value is already
1853 * zero. If the value is already zero, we need to reclaim the
1854 * buffer, otherwise it gets another trip through the LRU.
1855 */
Vratislav Bendel19957a12018-03-06 17:07:44 -08001856 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
Dave Chinnera4082352013-08-28 10:18:06 +10001857 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001858 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001859 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001860
Dave Chinnera4082352013-08-28 10:18:06 +10001861 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001862 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001863 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001864 return LRU_REMOVED;
1865}
1866
Andrew Mortonaddbda42013-08-28 10:18:06 +10001867static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001868xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001869 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001870 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001871{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001872 struct xfs_buftarg *btp = container_of(shrink,
1873 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001874 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001875 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001876
Vladimir Davydov503c3582015-02-12 14:58:47 -08001877 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1878 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001879
1880 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001881 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001882 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1883 list_del_init(&bp->b_lru);
1884 xfs_buf_rele(bp);
1885 }
1886
Dave Chinnere80dfa12013-08-28 10:18:05 +10001887 return freed;
1888}
1889
Andrew Mortonaddbda42013-08-28 10:18:06 +10001890static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001891xfs_buftarg_shrink_count(
1892 struct shrinker *shrink,
1893 struct shrink_control *sc)
1894{
1895 struct xfs_buftarg *btp = container_of(shrink,
1896 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001897 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001898}
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900void
1901xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001902 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001904 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001905 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1906 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001907 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001908
Dave Chinner2291dab2016-12-09 16:49:54 +11001909 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001910
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001911 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001914int
1915xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001917 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001919 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001920 btp->bt_meta_sectorsize = sectorsize;
1921 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Nathan Scottce8e9222006-01-11 15:39:08 +11001923 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001924 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001925 "Cannot set_blocksize to %u on device %pg",
1926 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001927 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
1929
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001930 /* Set up device logical sector size mask */
1931 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1932 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return 0;
1935}
1936
1937/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001938 * When allocating the initial buffer target we have not yet
1939 * read in the superblock, so don't know what sized sectors
1940 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001941 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942STATIC int
1943xfs_setsize_buftarg_early(
1944 xfs_buftarg_t *btp,
1945 struct block_device *bdev)
1946{
Eric Sandeena96c4152014-04-14 19:00:29 +10001947 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950xfs_buftarg_t *
1951xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001952 struct xfs_mount *mp,
Dan Williams486aff52017-08-24 15:12:50 -07001953 struct block_device *bdev,
1954 struct dax_device *dax_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
1956 xfs_buftarg_t *btp;
1957
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07001958 btp = kmem_zalloc(sizeof(*btp), KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Dave Chinnerebad8612010-09-22 10:47:20 +10001960 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001961 btp->bt_dev = bdev->bd_dev;
1962 btp->bt_bdev = bdev;
Dan Williams486aff52017-08-24 15:12:50 -07001963 btp->bt_daxdev = dax_dev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001964
Brian Fosterf9bccfc2020-05-06 13:25:21 -07001965 /*
1966 * Buffer IO error rate limiting. Limit it to no more than 10 messages
1967 * per 30 seconds so as to not spam logs too much on repeated errors.
1968 */
1969 ratelimit_state_init(&btp->bt_ioerror_rl, 30 * HZ,
1970 DEFAULT_RATELIMIT_BURST);
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (xfs_setsize_buftarg_early(btp, bdev))
Michal Hockod210a982017-11-23 17:13:40 +01001973 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001974
1975 if (list_lru_init(&btp->bt_lru))
Michal Hockod210a982017-11-23 17:13:40 +01001976 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001977
Brian Foster9c7504a2016-07-20 11:15:28 +10001978 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
Michal Hockod210a982017-11-23 17:13:40 +01001979 goto error_lru;
Brian Foster9c7504a2016-07-20 11:15:28 +10001980
Dave Chinnere80dfa12013-08-28 10:18:05 +10001981 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1982 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001983 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001984 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Michal Hockod210a982017-11-23 17:13:40 +01001985 if (register_shrinker(&btp->bt_shrinker))
1986 goto error_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return btp;
1988
Michal Hockod210a982017-11-23 17:13:40 +01001989error_pcpu:
1990 percpu_counter_destroy(&btp->bt_io_count);
1991error_lru:
1992 list_lru_destroy(&btp->bt_lru);
1993error_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001994 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return NULL;
1996}
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998/*
Brian Foster20e8a062017-04-21 12:40:44 -07001999 * Cancel a delayed write list.
2000 *
2001 * Remove each buffer from the list, clear the delwri queue flag and drop the
2002 * associated buffer reference.
2003 */
2004void
2005xfs_buf_delwri_cancel(
2006 struct list_head *list)
2007{
2008 struct xfs_buf *bp;
2009
2010 while (!list_empty(list)) {
2011 bp = list_first_entry(list, struct xfs_buf, b_list);
2012
2013 xfs_buf_lock(bp);
2014 bp->b_flags &= ~_XBF_DELWRI_Q;
2015 list_del_init(&bp->b_list);
2016 xfs_buf_relse(bp);
2017 }
2018}
2019
2020/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002021 * Add a buffer to the delayed write list.
2022 *
2023 * This queues a buffer for writeout if it hasn't already been. Note that
2024 * neither this routine nor the buffer list submission functions perform
2025 * any internal synchronization. It is expected that the lists are thread-local
2026 * to the callers.
2027 *
2028 * Returns true if we queued up the buffer, or false if it already had
2029 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002031bool
Nathan Scottce8e9222006-01-11 15:39:08 +11002032xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002033 struct xfs_buf *bp,
2034 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002036 ASSERT(xfs_buf_islocked(bp));
2037 ASSERT(!(bp->b_flags & XBF_READ));
2038
2039 /*
2040 * If the buffer is already marked delwri it already is queued up
2041 * by someone else for imediate writeout. Just ignore it in that
2042 * case.
2043 */
2044 if (bp->b_flags & _XBF_DELWRI_Q) {
2045 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
2046 return false;
2047 }
David Chinnera6867a62006-01-11 15:37:58 +11002048
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002049 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
2050
Dave Chinnerd808f612010-02-02 10:13:42 +11002051 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002052 * If a buffer gets written out synchronously or marked stale while it
2053 * is on a delwri list we lazily remove it. To do this, the other party
2054 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
2055 * It remains referenced and on the list. In a rare corner case it
2056 * might get readded to a delwri list after the synchronous writeout, in
2057 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11002058 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002059 bp->b_flags |= _XBF_DELWRI_Q;
2060 if (list_empty(&bp->b_list)) {
2061 atomic_inc(&bp->b_hold);
2062 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11002063 }
David Chinner585e6d82007-02-10 18:32:29 +11002064
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002065 return true;
David Chinner585e6d82007-02-10 18:32:29 +11002066}
2067
Dave Chinner089716a2010-01-26 15:13:25 +11002068/*
2069 * Compare function is more complex than it needs to be because
2070 * the return value is only 32 bits and we are doing comparisons
2071 * on 64 bit values
2072 */
2073static int
2074xfs_buf_cmp(
Sami Tolvanen4f0f5862021-04-08 11:28:34 -07002075 void *priv,
2076 const struct list_head *a,
2077 const struct list_head *b)
Dave Chinner089716a2010-01-26 15:13:25 +11002078{
2079 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
2080 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
2081 xfs_daddr_t diff;
2082
Mark Tinguelyf4b42422012-12-04 17:18:02 -06002083 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11002084 if (diff < 0)
2085 return -1;
2086 if (diff > 0)
2087 return 1;
2088 return 0;
2089}
2090
Dave Chinner26f1fe82016-06-01 17:38:15 +10002091/*
Brian Fostere339dd82018-07-11 22:26:34 -07002092 * Submit buffers for write. If wait_list is specified, the buffers are
2093 * submitted using sync I/O and placed on the wait list such that the caller can
2094 * iowait each buffer. Otherwise async I/O is used and the buffers are released
2095 * at I/O completion time. In either case, buffers remain locked until I/O
2096 * completes and the buffer is released from the queue.
Dave Chinner26f1fe82016-06-01 17:38:15 +10002097 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002098static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10002099xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002100 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10002101 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002103 struct xfs_buf *bp, *n;
2104 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002105 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Dave Chinner26f1fe82016-06-01 17:38:15 +10002107 list_sort(NULL, buffer_list, xfs_buf_cmp);
2108
2109 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002110 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10002111 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002112 if (xfs_buf_ispinned(bp)) {
2113 pinned++;
2114 continue;
2115 }
2116 if (!xfs_buf_trylock(bp))
2117 continue;
2118 } else {
2119 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002122 /*
2123 * Someone else might have written the buffer synchronously or
2124 * marked it stale in the meantime. In that case only the
2125 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
2126 * reference and remove it from the list here.
2127 */
2128 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
2129 list_del_init(&bp->b_list);
2130 xfs_buf_relse(bp);
2131 continue;
2132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002134 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002135
Dave Chinnercf53e992014-10-02 09:04:01 +10002136 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002137 * If we have a wait list, each buffer (and associated delwri
2138 * queue reference) transfers to it and is submitted
2139 * synchronously. Otherwise, drop the buffer from the delwri
2140 * queue and submit async.
Dave Chinnercf53e992014-10-02 09:04:01 +10002141 */
Brian Fosterb6983e82020-05-06 13:25:20 -07002142 bp->b_flags &= ~_XBF_DELWRI_Q;
Brian Fostere339dd82018-07-11 22:26:34 -07002143 bp->b_flags |= XBF_WRITE;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002144 if (wait_list) {
Brian Fostere339dd82018-07-11 22:26:34 -07002145 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002146 list_move_tail(&bp->b_list, wait_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002147 } else {
2148 bp->b_flags |= XBF_ASYNC;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002149 list_del_init(&bp->b_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002150 }
Brian Foster6af88cd2018-07-11 22:26:35 -07002151 __xfs_buf_submit(bp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00002153 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002155 return pinned;
2156}
Nathan Scottf07c2252006-09-28 10:52:15 +10002157
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002158/*
2159 * Write out a buffer list asynchronously.
2160 *
2161 * This will take the @buffer_list, write all non-locked and non-pinned buffers
2162 * out and not wait for I/O completion on any of the buffers. This interface
2163 * is only safely useable for callers that can track I/O completion by higher
2164 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
2165 * function.
Brian Fosterefc32892018-10-18 17:21:49 +11002166 *
2167 * Note: this function will skip buffers it would block on, and in doing so
2168 * leaves them on @buffer_list so they can be retried on a later pass. As such,
2169 * it is up to the caller to ensure that the buffer list is fully submitted or
2170 * cancelled appropriately when they are finished with the list. Failure to
2171 * cancel or resubmit the list until it is empty will result in leaked buffers
2172 * at unmount time.
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002173 */
2174int
2175xfs_buf_delwri_submit_nowait(
2176 struct list_head *buffer_list)
2177{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002178 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002179}
2180
2181/*
2182 * Write out a buffer list synchronously.
2183 *
2184 * This will take the @buffer_list, write all buffers out and wait for I/O
2185 * completion on all of the buffers. @buffer_list is consumed by the function,
2186 * so callers must have some other way of tracking buffers if they require such
2187 * functionality.
2188 */
2189int
2190xfs_buf_delwri_submit(
2191 struct list_head *buffer_list)
2192{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002193 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002194 int error = 0, error2;
2195 struct xfs_buf *bp;
2196
Dave Chinner26f1fe82016-06-01 17:38:15 +10002197 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002198
2199 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10002200 while (!list_empty(&wait_list)) {
2201 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002202
2203 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10002204
Brian Fostere339dd82018-07-11 22:26:34 -07002205 /*
2206 * Wait on the locked buffer, check for errors and unlock and
2207 * release the delwri queue reference.
2208 */
2209 error2 = xfs_buf_iowait(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002210 xfs_buf_relse(bp);
2211 if (!error)
2212 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
2214
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002215 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
Brian Foster7912e7f2017-06-14 21:21:45 -07002218/*
2219 * Push a single buffer on a delwri queue.
2220 *
2221 * The purpose of this function is to submit a single buffer of a delwri queue
2222 * and return with the buffer still on the original queue. The waiting delwri
2223 * buffer submission infrastructure guarantees transfer of the delwri queue
2224 * buffer reference to a temporary wait list. We reuse this infrastructure to
2225 * transfer the buffer back to the original queue.
2226 *
2227 * Note the buffer transitions from the queued state, to the submitted and wait
2228 * listed state and back to the queued state during this call. The buffer
2229 * locking and queue management logic between _delwri_pushbuf() and
2230 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2231 * before returning.
2232 */
2233int
2234xfs_buf_delwri_pushbuf(
2235 struct xfs_buf *bp,
2236 struct list_head *buffer_list)
2237{
2238 LIST_HEAD (submit_list);
2239 int error;
2240
2241 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2242
2243 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2244
2245 /*
2246 * Isolate the buffer to a new local list so we can submit it for I/O
2247 * independently from the rest of the original list.
2248 */
2249 xfs_buf_lock(bp);
2250 list_move(&bp->b_list, &submit_list);
2251 xfs_buf_unlock(bp);
2252
2253 /*
2254 * Delwri submission clears the DELWRI_Q buffer flag and returns with
Brian Fostere339dd82018-07-11 22:26:34 -07002255 * the buffer on the wait list with the original reference. Rather than
Brian Foster7912e7f2017-06-14 21:21:45 -07002256 * bounce the buffer from a local wait list back to the original list
2257 * after I/O completion, reuse the original list as the wait list.
2258 */
2259 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2260
2261 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002262 * The buffer is now locked, under I/O and wait listed on the original
2263 * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2264 * return with the buffer unlocked and on the original queue.
Brian Foster7912e7f2017-06-14 21:21:45 -07002265 */
Brian Fostere339dd82018-07-11 22:26:34 -07002266 error = xfs_buf_iowait(bp);
Brian Foster7912e7f2017-06-14 21:21:45 -07002267 bp->b_flags |= _XBF_DELWRI_Q;
2268 xfs_buf_unlock(bp);
2269
2270 return error;
2271}
2272
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002273int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002274xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Dave Chinner12eba652020-03-24 20:10:28 -07002276 xfs_buf_zone = kmem_cache_create("xfs_buf", sizeof(struct xfs_buf), 0,
2277 SLAB_HWCACHE_ALIGN |
2278 SLAB_RECLAIM_ACCOUNT |
2279 SLAB_MEM_SPREAD,
2280 NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002281 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002282 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002283
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002286 out:
Nathan Scott87582802006-03-14 13:18:19 +11002287 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290void
Nathan Scottce8e9222006-01-11 15:39:08 +11002291xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08002293 kmem_cache_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
Brian Foster7561d272017-10-17 14:16:29 -07002295
2296void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
2297{
Brian Foster7561d272017-10-17 14:16:29 -07002298 /*
2299 * Set the lru reference count to 0 based on the error injection tag.
2300 * This allows userspace to disrupt buffer caching for debug/testing
2301 * purposes.
2302 */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002303 if (XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_LRU_REF))
Brian Foster7561d272017-10-17 14:16:29 -07002304 lru_ref = 0;
2305
2306 atomic_set(&bp->b_lru_ref, lru_ref);
2307}
Brian Foster8473fee2019-02-07 10:45:46 -08002308
2309/*
2310 * Verify an on-disk magic value against the magic value specified in the
2311 * verifier structure. The verifier magic is in disk byte order so the caller is
2312 * expected to pass the value directly from disk.
2313 */
2314bool
2315xfs_verify_magic(
2316 struct xfs_buf *bp,
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002317 __be32 dmagic)
Brian Foster8473fee2019-02-07 10:45:46 -08002318{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002319 struct xfs_mount *mp = bp->b_mount;
Brian Foster8473fee2019-02-07 10:45:46 -08002320 int idx;
2321
2322 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002323 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic[idx]))
Brian Foster8473fee2019-02-07 10:45:46 -08002324 return false;
2325 return dmagic == bp->b_ops->magic[idx];
2326}
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002327/*
2328 * Verify an on-disk magic value against the magic value specified in the
2329 * verifier structure. The verifier magic is in disk byte order so the caller is
2330 * expected to pass the value directly from disk.
2331 */
2332bool
2333xfs_verify_magic16(
2334 struct xfs_buf *bp,
2335 __be16 dmagic)
2336{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002337 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002338 int idx;
2339
2340 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002341 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic16[idx]))
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002342 return false;
2343 return dmagic == bp->b_ops->magic16[idx];
2344}