blob: f6e5235df7c904a0936bb6862bf585a04ee64732 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scottf07c2252006-09-28 10:52:15 +10003 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Vlad Apostolov93c189c2006-11-11 18:03:49 +11006#include "xfs.h"
Andrew Morton3fcfab12006-10-19 23:28:16 -07007#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Darrick J. Wong5467b342019-06-28 19:25:35 -07009#include "xfs_shared.h"
Christoph Hellwig4fb6e8a2014-11-28 14:25:04 +110010#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_log_format.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100012#include "xfs_trans_resv.h"
Dave Chinner239880e2013-10-23 10:50:10 +110013#include "xfs_sb.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050014#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000015#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110016#include "xfs_log.h"
Dave Chinner9fe5c772020-06-29 14:48:47 -070017#include "xfs_log_recover.h"
Dave Chinnerf593bf12020-06-29 14:48:46 -070018#include "xfs_trans.h"
19#include "xfs_buf_item.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070020#include "xfs_errortag.h"
Brian Foster7561d272017-10-17 14:16:29 -070021#include "xfs_error.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050022
David Chinner7989cb82007-02-10 18:34:56 +110023static kmem_zone_t *xfs_buf_zone;
Christoph Hellwig23ea4032005-06-21 15:14:01 +100024
Nathan Scottce8e9222006-01-11 15:39:08 +110025#define xb_to_gfp(flags) \
Dave Chinneraa5c1582012-04-23 15:58:56 +100026 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Dave Chinner37fd1672018-10-18 17:21:29 +110028/*
29 * Locking orders
30 *
31 * xfs_buf_ioacct_inc:
32 * xfs_buf_ioacct_dec:
33 * b_sema (caller holds)
34 * b_lock
35 *
36 * xfs_buf_stale:
37 * b_sema (caller holds)
38 * b_lock
39 * lru_lock
40 *
41 * xfs_buf_rele:
42 * b_lock
43 * pag_buf_lock
44 * lru_lock
45 *
Brian Foster10fb9ac2021-01-22 16:48:19 -080046 * xfs_buftarg_drain_rele
Dave Chinner37fd1672018-10-18 17:21:29 +110047 * lru_lock
48 * b_lock (trylock due to inversion)
49 *
50 * xfs_buftarg_isolate
51 * lru_lock
52 * b_lock (trylock due to inversion)
53 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Christoph Hellwig26e328752020-09-01 10:55:47 -070055static int __xfs_buf_submit(struct xfs_buf *bp, bool wait);
56
57static inline int
58xfs_buf_submit(
59 struct xfs_buf *bp)
60{
61 return __xfs_buf_submit(bp, !(bp->b_flags & XBF_ASYNC));
62}
63
James Bottomley73c77e22010-01-25 11:42:24 -060064static inline int
65xfs_buf_is_vmapped(
66 struct xfs_buf *bp)
67{
68 /*
69 * Return true if the buffer is vmapped.
70 *
Dave Chinner611c9942012-04-23 15:59:07 +100071 * b_addr is null if the buffer is not mapped, but the code is clever
72 * enough to know it doesn't have to map a single page, so the check has
73 * to be both for b_addr and bp->b_page_count > 1.
James Bottomley73c77e22010-01-25 11:42:24 -060074 */
Dave Chinner611c9942012-04-23 15:59:07 +100075 return bp->b_addr && bp->b_page_count > 1;
James Bottomley73c77e22010-01-25 11:42:24 -060076}
77
78static inline int
79xfs_buf_vmap_len(
80 struct xfs_buf *bp)
81{
82 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/*
Brian Foster9c7504a2016-07-20 11:15:28 +100086 * Bump the I/O in flight count on the buftarg if we haven't yet done so for
87 * this buffer. The count is incremented once per buffer (per hold cycle)
88 * because the corresponding decrement is deferred to buffer release. Buffers
89 * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
90 * tracking adds unnecessary overhead. This is used for sychronization purposes
Brian Foster10fb9ac2021-01-22 16:48:19 -080091 * with unmount (see xfs_buftarg_drain()), so all we really need is a count of
Brian Foster9c7504a2016-07-20 11:15:28 +100092 * in-flight buffers.
93 *
94 * Buffers that are never released (e.g., superblock, iclog buffers) must set
95 * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
96 * never reaches zero and unmount hangs indefinitely.
97 */
98static inline void
99xfs_buf_ioacct_inc(
100 struct xfs_buf *bp)
101{
Brian Foster63db7c82017-05-31 08:22:52 -0700102 if (bp->b_flags & XBF_NO_IOACCT)
Brian Foster9c7504a2016-07-20 11:15:28 +1000103 return;
104
105 ASSERT(bp->b_flags & XBF_ASYNC);
Brian Foster63db7c82017-05-31 08:22:52 -0700106 spin_lock(&bp->b_lock);
107 if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) {
108 bp->b_state |= XFS_BSTATE_IN_FLIGHT;
109 percpu_counter_inc(&bp->b_target->bt_io_count);
110 }
111 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000112}
113
114/*
115 * Clear the in-flight state on a buffer about to be released to the LRU or
116 * freed and unaccount from the buftarg.
117 */
118static inline void
Brian Foster63db7c82017-05-31 08:22:52 -0700119__xfs_buf_ioacct_dec(
120 struct xfs_buf *bp)
121{
Brian Foster95989c42017-06-08 08:23:07 -0700122 lockdep_assert_held(&bp->b_lock);
Brian Foster63db7c82017-05-31 08:22:52 -0700123
124 if (bp->b_state & XFS_BSTATE_IN_FLIGHT) {
125 bp->b_state &= ~XFS_BSTATE_IN_FLIGHT;
126 percpu_counter_dec(&bp->b_target->bt_io_count);
127 }
128}
129
130static inline void
Brian Foster9c7504a2016-07-20 11:15:28 +1000131xfs_buf_ioacct_dec(
132 struct xfs_buf *bp)
133{
Brian Foster63db7c82017-05-31 08:22:52 -0700134 spin_lock(&bp->b_lock);
135 __xfs_buf_ioacct_dec(bp);
136 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000137}
138
139/*
Dave Chinner430cbeb2010-12-02 16:30:55 +1100140 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
141 * b_lru_ref count so that the buffer is freed immediately when the buffer
142 * reference count falls to zero. If the buffer is already on the LRU, we need
143 * to remove the reference that LRU holds on the buffer.
144 *
145 * This prevents build-up of stale buffers on the LRU.
146 */
147void
148xfs_buf_stale(
149 struct xfs_buf *bp)
150{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000151 ASSERT(xfs_buf_islocked(bp));
152
Dave Chinner430cbeb2010-12-02 16:30:55 +1100153 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000154
155 /*
156 * Clear the delwri status so that a delwri queue walker will not
157 * flush this buffer to disk now that it is stale. The delwri queue has
158 * a reference to the buffer, so this is safe to do.
159 */
160 bp->b_flags &= ~_XBF_DELWRI_Q;
161
Brian Foster9c7504a2016-07-20 11:15:28 +1000162 /*
163 * Once the buffer is marked stale and unlocked, a subsequent lookup
164 * could reset b_flags. There is no guarantee that the buffer is
165 * unaccounted (released to LRU) before that occurs. Drop in-flight
166 * status now to preserve accounting consistency.
167 */
Dave Chinnera4082352013-08-28 10:18:06 +1000168 spin_lock(&bp->b_lock);
Brian Foster63db7c82017-05-31 08:22:52 -0700169 __xfs_buf_ioacct_dec(bp);
170
Dave Chinnera4082352013-08-28 10:18:06 +1000171 atomic_set(&bp->b_lru_ref, 0);
172 if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
Dave Chinnere80dfa12013-08-28 10:18:05 +1000173 (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
174 atomic_dec(&bp->b_hold);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100175
Dave Chinner430cbeb2010-12-02 16:30:55 +1100176 ASSERT(atomic_read(&bp->b_hold) >= 1);
Dave Chinnera4082352013-08-28 10:18:06 +1000177 spin_unlock(&bp->b_lock);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Dave Chinner3e85c862012-06-22 18:50:09 +1000180static int
181xfs_buf_get_maps(
182 struct xfs_buf *bp,
183 int map_count)
184{
185 ASSERT(bp->b_maps == NULL);
186 bp->b_map_count = map_count;
187
188 if (map_count == 1) {
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600189 bp->b_maps = &bp->__b_map;
Dave Chinner3e85c862012-06-22 18:50:09 +1000190 return 0;
191 }
192
193 bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
194 KM_NOFS);
195 if (!bp->b_maps)
Dave Chinner24513372014-06-25 14:58:08 +1000196 return -ENOMEM;
Dave Chinner3e85c862012-06-22 18:50:09 +1000197 return 0;
198}
199
200/*
201 * Frees b_pages if it was allocated.
202 */
203static void
204xfs_buf_free_maps(
205 struct xfs_buf *bp)
206{
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600207 if (bp->b_maps != &bp->__b_map) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000208 kmem_free(bp->b_maps);
209 bp->b_maps = NULL;
210 }
211}
212
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800213static int
Dave Chinner3e85c862012-06-22 18:50:09 +1000214_xfs_buf_alloc(
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000215 struct xfs_buftarg *target,
Dave Chinner3e85c862012-06-22 18:50:09 +1000216 struct xfs_buf_map *map,
217 int nmaps,
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800218 xfs_buf_flags_t flags,
219 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000221 struct xfs_buf *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000222 int error;
223 int i;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000224
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800225 *bpp = NULL;
Carlos Maiolino32a2b112020-07-22 09:23:10 -0700226 bp = kmem_cache_zalloc(xfs_buf_zone, GFP_NOFS | __GFP_NOFAIL);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /*
Dave Chinner12bcb3f2012-04-23 15:59:05 +1000229 * We don't want certain flags to appear in b_flags unless they are
230 * specifically set by later operations on the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
Dave Chinner611c9942012-04-23 15:59:07 +1000232 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Nathan Scottce8e9222006-01-11 15:39:08 +1100234 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100235 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000236 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100237 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100238 INIT_LIST_HEAD(&bp->b_list);
Carlos Maiolino643c8c02018-01-24 13:38:49 -0800239 INIT_LIST_HEAD(&bp->b_li_list);
Thomas Gleixnera731cd112010-09-07 14:33:15 +0000240 sema_init(&bp->b_sema, 0); /* held, no waiters */
Dave Chinnera4082352013-08-28 10:18:06 +1000241 spin_lock_init(&bp->b_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100242 bp->b_target = target;
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700243 bp->b_mount = target->bt_mount;
Dave Chinner3e85c862012-06-22 18:50:09 +1000244 bp->b_flags = flags;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000247 * Set length and io_length to the same value initially.
248 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 * most cases but may be reset (e.g. XFS recovery).
250 */
Dave Chinner3e85c862012-06-22 18:50:09 +1000251 error = xfs_buf_get_maps(bp, nmaps);
252 if (error) {
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800253 kmem_cache_free(xfs_buf_zone, bp);
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800254 return error;
Dave Chinner3e85c862012-06-22 18:50:09 +1000255 }
256
257 bp->b_bn = map[0].bm_bn;
258 bp->b_length = 0;
259 for (i = 0; i < nmaps; i++) {
260 bp->b_maps[i].bm_bn = map[i].bm_bn;
261 bp->b_maps[i].bm_len = map[i].bm_len;
262 bp->b_length += map[i].bm_len;
263 }
Dave Chinner3e85c862012-06-22 18:50:09 +1000264
Nathan Scottce8e9222006-01-11 15:39:08 +1100265 atomic_set(&bp->b_pin_count, 0);
266 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700268 XFS_STATS_INC(bp->b_mount, xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000269 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000270
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800271 *bpp = bp;
272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100276 * Allocate a page array capable of holding a specified number
277 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
279STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100280_xfs_buf_get_pages(
Dave Chinnere8222612020-12-16 16:07:34 -0800281 struct xfs_buf *bp,
Eric Sandeen87937bf2014-04-14 19:01:20 +1000282 int page_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100285 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100286 bp->b_page_count = page_count;
287 if (page_count <= XB_PAGES) {
288 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100290 bp->b_pages = kmem_alloc(sizeof(struct page *) *
Dave Chinneraa5c1582012-04-23 15:58:56 +1000291 page_count, KM_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +1100292 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return -ENOMEM;
294 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100295 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297 return 0;
298}
299
300/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100301 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 */
303STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100304_xfs_buf_free_pages(
Dave Chinnere8222612020-12-16 16:07:34 -0800305 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Nathan Scottce8e9222006-01-11 15:39:08 +1100307 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000308 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000309 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311}
312
313/*
314 * Releases the specified buffer.
315 *
316 * The modification state of any associated pages is left unchanged.
Zhi Yong Wub46fe822013-08-07 10:10:59 +0000317 * The buffer must not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * hashed and refcounted buffers
319 */
Christoph Hellwig25a40952019-10-24 22:25:37 -0700320static void
Nathan Scottce8e9222006-01-11 15:39:08 +1100321xfs_buf_free(
Dave Chinnere8222612020-12-16 16:07:34 -0800322 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000324 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Dave Chinner430cbeb2010-12-02 16:30:55 +1100326 ASSERT(list_empty(&bp->b_lru));
327
Dave Chinner0e6e8472011-03-26 09:16:45 +1100328 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 uint i;
330
James Bottomley73c77e22010-01-25 11:42:24 -0600331 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000332 vm_unmap_ram(bp->b_addr - bp->b_offset,
333 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Nathan Scott948ecdb2006-09-28 11:03:13 +1000335 for (i = 0; i < bp->b_page_count; i++) {
336 struct page *page = bp->b_pages[i];
337
Dave Chinner0e6e8472011-03-26 09:16:45 +1100338 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000339 }
Dave Chinner12eba652020-03-24 20:10:28 -0700340 if (current->reclaim_state)
341 current->reclaim_state->reclaimed_slab +=
342 bp->b_page_count;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100343 } else if (bp->b_flags & _XBF_KMEM)
344 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000345 _xfs_buf_free_pages(bp);
Dave Chinner3e85c862012-06-22 18:50:09 +1000346 xfs_buf_free_maps(bp);
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800347 kmem_cache_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100351 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 */
353STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100354xfs_buf_allocate_memory(
Dave Chinnere8222612020-12-16 16:07:34 -0800355 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 uint flags)
357{
Dave Chinneraa0e8832012-04-23 15:58:52 +1000358 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100360 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 unsigned short page_count, i;
Dave Chinner795cac72012-04-23 15:58:53 +1000362 xfs_off_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 int error;
Bill O'Donnell3219e8c2019-10-04 16:38:44 -0700364 xfs_km_flags_t kmflag_mask = 0;
365
366 /*
367 * assure zeroed buffer for non-read cases.
368 */
369 if (!(flags & XBF_READ)) {
370 kmflag_mask |= KM_ZERO;
371 gfp_mask |= __GFP_ZERO;
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Dave Chinner0e6e8472011-03-26 09:16:45 +1100374 /*
375 * for buffers that are contained within a single page, just allocate
376 * the memory from the heap - there's no need for the complexity of
377 * page arrays to keep allocation down to order 0.
378 */
Dave Chinner795cac72012-04-23 15:58:53 +1000379 size = BBTOB(bp->b_length);
380 if (size < PAGE_SIZE) {
Dave Chinnerf8f9ee42019-08-26 12:08:39 -0700381 int align_mask = xfs_buftarg_dma_alignment(bp->b_target);
Bill O'Donnell3219e8c2019-10-04 16:38:44 -0700382 bp->b_addr = kmem_alloc_io(size, align_mask,
383 KM_NOFS | kmflag_mask);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100384 if (!bp->b_addr) {
385 /* low memory - use alloc_page loop instead */
386 goto use_alloc_page;
387 }
388
Dave Chinner795cac72012-04-23 15:58:53 +1000389 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100390 ((unsigned long)bp->b_addr & PAGE_MASK)) {
391 /* b_addr spans two pages - use alloc_page instead */
392 kmem_free(bp->b_addr);
393 bp->b_addr = NULL;
394 goto use_alloc_page;
395 }
396 bp->b_offset = offset_in_page(bp->b_addr);
397 bp->b_pages = bp->b_page_array;
Dave Chinnerf8f9ee42019-08-26 12:08:39 -0700398 bp->b_pages[0] = kmem_to_page(bp->b_addr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100399 bp->b_page_count = 1;
Dave Chinner611c9942012-04-23 15:59:07 +1000400 bp->b_flags |= _XBF_KMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100401 return 0;
402 }
403
404use_alloc_page:
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600405 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
406 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000407 >> PAGE_SHIFT;
Dave Chinner795cac72012-04-23 15:58:53 +1000408 page_count = end - start;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000409 error = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (unlikely(error))
411 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Nathan Scottce8e9222006-01-11 15:39:08 +1100413 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100414 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Nathan Scottce8e9222006-01-11 15:39:08 +1100416 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 struct page *page;
418 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100419retry:
420 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100422 if (flags & XBF_READ_AHEAD) {
423 bp->b_page_count = i;
Dave Chinner24513372014-06-25 14:58:08 +1000424 error = -ENOMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100425 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427
428 /*
429 * This could deadlock.
430 *
431 * But until all the XFS lowlevel code is revamped to
432 * handle buffer allocation failures we can't do much.
433 */
434 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100435 xfs_err(NULL,
Tetsuo Handa5bf97b12015-10-12 15:41:29 +1100436 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
437 current->comm, current->pid,
Harvey Harrison34a622b2008-04-10 12:19:21 +1000438 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700440 XFS_STATS_INC(bp->b_mount, xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200441 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 goto retry;
443 }
444
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700445 XFS_STATS_INC(bp->b_mount, xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Dave Chinner0e6e8472011-03-26 09:16:45 +1100447 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100449 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 offset = 0;
451 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Dave Chinner0e6e8472011-03-26 09:16:45 +1100454out_free_pages:
455 for (i = 0; i < bp->b_page_count; i++)
456 __free_page(bp->b_pages[i]);
Darrick J. Wong2aa6ba7b2017-01-25 20:24:57 -0800457 bp->b_flags &= ~_XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return error;
459}
460
461/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300462 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 */
464STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100465_xfs_buf_map_pages(
Dave Chinnere8222612020-12-16 16:07:34 -0800466 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 uint flags)
468{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100469 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100470 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100471 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100472 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
Dave Chinner611c9942012-04-23 15:59:07 +1000473 } else if (flags & XBF_UNMAPPED) {
474 bp->b_addr = NULL;
475 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100476 int retried = 0;
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700477 unsigned nofs_flag;
Dave Chinnera19fb382011-03-26 09:13:42 +1100478
Dave Chinnerae687e52014-03-07 16:19:14 +1100479 /*
Joe Perchescf085a12019-11-07 13:24:52 -0800480 * vm_map_ram() will allocate auxiliary structures (e.g.
Dave Chinnerae687e52014-03-07 16:19:14 +1100481 * pagetables) with GFP_KERNEL, yet we are likely to be under
482 * GFP_NOFS context here. Hence we need to tell memory reclaim
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700483 * that we are in such a context via PF_MEMALLOC_NOFS to prevent
Dave Chinnerae687e52014-03-07 16:19:14 +1100484 * memory reclaim re-entering the filesystem here and
485 * potentially deadlocking.
486 */
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700487 nofs_flag = memalloc_nofs_save();
Dave Chinnera19fb382011-03-26 09:13:42 +1100488 do {
489 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
Christoph Hellwigd4efd792020-06-01 21:51:27 -0700490 -1);
Dave Chinnera19fb382011-03-26 09:13:42 +1100491 if (bp->b_addr)
492 break;
493 vm_unmap_aliases();
494 } while (retried++ <= 1);
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700495 memalloc_nofs_restore(nofs_flag);
Dave Chinnera19fb382011-03-26 09:13:42 +1100496
497 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100499 bp->b_addr += bp->b_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
502 return 0;
503}
504
505/*
506 * Finding and Reading Buffers
507 */
Lucas Stach6031e732016-12-07 17:36:36 +1100508static int
509_xfs_buf_obj_cmp(
510 struct rhashtable_compare_arg *arg,
511 const void *obj)
512{
513 const struct xfs_buf_map *map = arg->key;
514 const struct xfs_buf *bp = obj;
515
516 /*
517 * The key hashing in the lookup path depends on the key being the
518 * first element of the compare_arg, make sure to assert this.
519 */
520 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
521
522 if (bp->b_bn != map->bm_bn)
523 return 1;
524
525 if (unlikely(bp->b_length != map->bm_len)) {
526 /*
527 * found a block number match. If the range doesn't
528 * match, the only way this is allowed is if the buffer
529 * in the cache is stale and the transaction that made
530 * it stale has not yet committed. i.e. we are
531 * reallocating a busy extent. Skip this buffer and
532 * continue searching for an exact match.
533 */
534 ASSERT(bp->b_flags & XBF_STALE);
535 return 1;
536 }
537 return 0;
538}
539
540static const struct rhashtable_params xfs_buf_hash_params = {
541 .min_size = 32, /* empty AGs have minimal footprint */
542 .nelem_hint = 16,
543 .key_len = sizeof(xfs_daddr_t),
544 .key_offset = offsetof(struct xfs_buf, b_bn),
545 .head_offset = offsetof(struct xfs_buf, b_rhash_head),
546 .automatic_shrinking = true,
547 .obj_cmpfn = _xfs_buf_obj_cmp,
548};
549
550int
551xfs_buf_hash_init(
552 struct xfs_perag *pag)
553{
554 spin_lock_init(&pag->pag_buf_lock);
555 return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
556}
557
558void
559xfs_buf_hash_destroy(
560 struct xfs_perag *pag)
561{
562 rhashtable_destroy(&pag->pag_buf_hash);
563}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565/*
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700566 * Look up a buffer in the buffer cache and return it referenced and locked
567 * in @found_bp.
568 *
569 * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the
570 * cache.
571 *
572 * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return
573 * -EAGAIN if we fail to lock it.
574 *
575 * Return values are:
576 * -EFSCORRUPTED if have been supplied with an invalid address
577 * -EAGAIN on trylock failure
578 * -ENOENT if we fail to find a match and @new_bp was NULL
579 * 0, with @found_bp:
580 * - @new_bp if we inserted it into the cache
581 * - the buffer we found and locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700583static int
584xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000585 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000586 struct xfs_buf_map *map,
587 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100588 xfs_buf_flags_t flags,
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700589 struct xfs_buf *new_bp,
590 struct xfs_buf **found_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Dave Chinner74f75a02010-09-24 19:59:04 +1000592 struct xfs_perag *pag;
Dave Chinnere8222612020-12-16 16:07:34 -0800593 struct xfs_buf *bp;
Lucas Stach6031e732016-12-07 17:36:36 +1100594 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
Dave Chinner10616b82013-01-21 23:53:52 +1100595 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000596 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700598 *found_bp = NULL;
599
Dave Chinner3e85c862012-06-22 18:50:09 +1000600 for (i = 0; i < nmaps; i++)
Lucas Stach6031e732016-12-07 17:36:36 +1100601 cmap.bm_len += map[i].bm_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* Check for IOs smaller than the sector size / not sector aligned */
Lucas Stach6031e732016-12-07 17:36:36 +1100604 ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
605 ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Dave Chinner10616b82013-01-21 23:53:52 +1100607 /*
608 * Corrupted block numbers can get through to here, unfortunately, so we
609 * have to check that the buffer falls within the filesystem bounds.
610 */
611 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
Lucas Stach6031e732016-12-07 17:36:36 +1100612 if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
Dave Chinner10616b82013-01-21 23:53:52 +1100613 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -0800614 "%s: daddr 0x%llx out of range, EOFS 0x%llx",
Lucas Stach6031e732016-12-07 17:36:36 +1100615 __func__, cmap.bm_bn, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000616 WARN_ON(1);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700617 return -EFSCORRUPTED;
Dave Chinner10616b82013-01-21 23:53:52 +1100618 }
619
Dave Chinner74f75a02010-09-24 19:59:04 +1000620 pag = xfs_perag_get(btp->bt_mount,
Lucas Stach6031e732016-12-07 17:36:36 +1100621 xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Dave Chinner74f75a02010-09-24 19:59:04 +1000623 spin_lock(&pag->pag_buf_lock);
Lucas Stach6031e732016-12-07 17:36:36 +1100624 bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
625 xfs_buf_hash_params);
626 if (bp) {
627 atomic_inc(&bp->b_hold);
628 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630
631 /* No match found */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700632 if (!new_bp) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100633 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000634 spin_unlock(&pag->pag_buf_lock);
635 xfs_perag_put(pag);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700636 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700638
639 /* the buffer keeps the perag reference until it is freed */
640 new_bp->b_pag = pag;
641 rhashtable_insert_fast(&pag->pag_buf_hash, &new_bp->b_rhash_head,
642 xfs_buf_hash_params);
643 spin_unlock(&pag->pag_buf_lock);
644 *found_bp = new_bp;
645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000648 spin_unlock(&pag->pag_buf_lock);
649 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200651 if (!xfs_buf_trylock(bp)) {
652 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100653 xfs_buf_rele(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100654 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700655 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200657 xfs_buf_lock(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100658 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Dave Chinner0e6e8472011-03-26 09:16:45 +1100661 /*
662 * if the buffer is stale, clear all the external state associated with
663 * it. We need to keep flags such as how we allocated the buffer memory
664 * intact here.
665 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100666 if (bp->b_flags & XBF_STALE) {
667 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner611c9942012-04-23 15:59:07 +1000668 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100669 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000670 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000671
672 trace_xfs_buf_find(bp, flags, _RET_IP_);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100673 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700674 *found_bp = bp;
675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Dave Chinner8925a3d2018-04-18 08:25:20 -0700678struct xfs_buf *
679xfs_buf_incore(
680 struct xfs_buftarg *target,
681 xfs_daddr_t blkno,
682 size_t numblks,
683 xfs_buf_flags_t flags)
684{
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700685 struct xfs_buf *bp;
686 int error;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700687 DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700688
689 error = xfs_buf_find(target, &map, 1, flags, NULL, &bp);
690 if (error)
691 return NULL;
692 return bp;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700693}
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/*
Dave Chinner38158322011-09-30 04:45:02 +0000696 * Assembles a buffer covering the specified range. The code is optimised for
697 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
698 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800700int
Dave Chinner6dde2702012-06-22 18:50:10 +1000701xfs_buf_get_map(
702 struct xfs_buftarg *target,
703 struct xfs_buf_map *map,
704 int nmaps,
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800705 xfs_buf_flags_t flags,
706 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Dave Chinner38158322011-09-30 04:45:02 +0000708 struct xfs_buf *bp;
709 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100710 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800712 *bpp = NULL;
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700713 error = xfs_buf_find(target, map, nmaps, flags, NULL, &bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800714 if (!error)
Dave Chinner38158322011-09-30 04:45:02 +0000715 goto found;
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800716 if (error != -ENOENT)
717 return error;
Dave Chinner38158322011-09-30 04:45:02 +0000718
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800719 error = _xfs_buf_alloc(target, map, nmaps, flags, &new_bp);
720 if (error)
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800721 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000723 error = xfs_buf_allocate_memory(new_bp, flags);
724 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000725 xfs_buf_free(new_bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800726 return error;
Dave Chinner38158322011-09-30 04:45:02 +0000727 }
728
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700729 error = xfs_buf_find(target, map, nmaps, flags, new_bp, &bp);
730 if (error) {
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000731 xfs_buf_free(new_bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800732 return error;
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000733 }
734
735 if (bp != new_bp)
736 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Dave Chinner38158322011-09-30 04:45:02 +0000738found:
Dave Chinner611c9942012-04-23 15:59:07 +1000739 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100740 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (unlikely(error)) {
Darrick J. Wong93baa552020-02-21 07:40:44 -0800742 xfs_warn_ratelimited(target->bt_mount,
743 "%s: failed to map %u pages", __func__,
744 bp->b_page_count);
Dave Chinnera8acad72012-04-23 15:58:54 +1000745 xfs_buf_relse(bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800746 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748 }
749
Dave Chinnerb79f4a12016-01-12 07:03:44 +1100750 /*
751 * Clear b_error if this is a lookup from a caller that doesn't expect
752 * valid data to be found in the buffer.
753 */
754 if (!(flags & XBF_READ))
755 xfs_buf_ioerror(bp, 0);
756
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100757 XFS_STATS_INC(target->bt_mount, xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000758 trace_xfs_buf_get(bp, flags, _RET_IP_);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800759 *bpp = bp;
760 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Christoph Hellwig26e328752020-09-01 10:55:47 -0700763int
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100764_xfs_buf_read(
Dave Chinnere8222612020-12-16 16:07:34 -0800765 struct xfs_buf *bp,
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100766 xfs_buf_flags_t flags)
767{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000768 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600769 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100770
Christoph Hellwig26e328752020-09-01 10:55:47 -0700771 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD | XBF_DONE);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200772 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100773
Brian Foster6af88cd2018-07-11 22:26:35 -0700774 return xfs_buf_submit(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100775}
776
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100777/*
Brian Foster75d02302019-02-06 09:25:29 -0800778 * Reverify a buffer found in cache without an attached ->b_ops.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800779 *
Brian Foster75d02302019-02-06 09:25:29 -0800780 * If the caller passed an ops structure and the buffer doesn't have ops
781 * assigned, set the ops and use it to verify the contents. If verification
782 * fails, clear XBF_DONE. We assume the buffer has no recorded errors and is
783 * already in XBF_DONE state on entry.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800784 *
Brian Foster75d02302019-02-06 09:25:29 -0800785 * Under normal operations, every in-core buffer is verified on read I/O
786 * completion. There are two scenarios that can lead to in-core buffers without
787 * an assigned ->b_ops. The first is during log recovery of buffers on a V4
788 * filesystem, though these buffers are purged at the end of recovery. The
789 * other is online repair, which intentionally reads with a NULL buffer ops to
790 * run several verifiers across an in-core buffer in order to establish buffer
791 * type. If repair can't establish that, the buffer will be left in memory
792 * with NULL buffer ops.
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100793 */
794int
Brian Foster75d02302019-02-06 09:25:29 -0800795xfs_buf_reverify(
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100796 struct xfs_buf *bp,
797 const struct xfs_buf_ops *ops)
798{
799 ASSERT(bp->b_flags & XBF_DONE);
800 ASSERT(bp->b_error == 0);
801
802 if (!ops || bp->b_ops)
803 return 0;
804
805 bp->b_ops = ops;
806 bp->b_ops->verify_read(bp);
807 if (bp->b_error)
808 bp->b_flags &= ~XBF_DONE;
809 return bp->b_error;
810}
811
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800812int
Dave Chinner6dde2702012-06-22 18:50:10 +1000813xfs_buf_read_map(
814 struct xfs_buftarg *target,
815 struct xfs_buf_map *map,
816 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100817 xfs_buf_flags_t flags,
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800818 struct xfs_buf **bpp,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800819 const struct xfs_buf_ops *ops,
820 xfs_failaddr_t fa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Dave Chinner6dde2702012-06-22 18:50:10 +1000822 struct xfs_buf *bp;
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800823 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Nathan Scottce8e9222006-01-11 15:39:08 +1100825 flags |= XBF_READ;
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800826 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800828 error = xfs_buf_get_map(target, map, nmaps, flags, &bp);
829 if (error)
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800830 return error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000831
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100832 trace_xfs_buf_read(bp, flags, _RET_IP_);
833
834 if (!(bp->b_flags & XBF_DONE)) {
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800835 /* Initiate the buffer read and wait. */
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100836 XFS_STATS_INC(target->bt_mount, xb_get_read);
837 bp->b_ops = ops;
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800838 error = _xfs_buf_read(bp, flags);
839
840 /* Readahead iodone already dropped the buffer, so exit. */
841 if (flags & XBF_ASYNC)
842 return 0;
843 } else {
844 /* Buffer already read; all we need to do is check it. */
845 error = xfs_buf_reverify(bp, ops);
846
847 /* Readahead already finished; drop the buffer and exit. */
848 if (flags & XBF_ASYNC) {
849 xfs_buf_relse(bp);
850 return 0;
851 }
852
853 /* We do not want read in the flags */
854 bp->b_flags &= ~XBF_READ;
855 ASSERT(bp->b_ops != NULL || ops == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800858 /*
859 * If we've had a read error, then the contents of the buffer are
860 * invalid and should not be used. To ensure that a followup read tries
861 * to pull the buffer from disk again, we clear the XBF_DONE flag and
862 * mark the buffer stale. This ensures that anyone who has a current
863 * reference to the buffer will interpret it's contents correctly and
864 * future cache lookups will also treat it as an empty, uninitialised
865 * buffer.
866 */
867 if (error) {
868 if (!XFS_FORCED_SHUTDOWN(target->bt_mount))
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800869 xfs_buf_ioerror_alert(bp, fa);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100870
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800871 bp->b_flags &= ~XBF_DONE;
872 xfs_buf_stale(bp);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100873 xfs_buf_relse(bp);
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800874
875 /* bad CRC means corrupted metadata */
876 if (error == -EFSBADCRC)
877 error = -EFSCORRUPTED;
878 return error;
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100879 }
880
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800881 *bpp = bp;
882 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100886 * If we are not low on memory then do the readahead in a deadlock
887 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
889void
Dave Chinner6dde2702012-06-22 18:50:10 +1000890xfs_buf_readahead_map(
891 struct xfs_buftarg *target,
892 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100893 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100894 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800896 struct xfs_buf *bp;
897
Jan Karaefa7c9f2017-02-02 15:56:53 +0100898 if (bdi_read_congested(target->bt_bdev->bd_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return;
900
Dave Chinner6dde2702012-06-22 18:50:10 +1000901 xfs_buf_read_map(target, map, nmaps,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800902 XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD, &bp, ops,
903 __this_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Dave Chinner5adc94c2010-09-24 21:58:31 +1000906/*
907 * Read an uncached buffer from disk. Allocates and returns a locked
908 * buffer containing the disk contents or nothing.
909 */
Dave Chinnerba3726742014-10-02 09:05:32 +1000910int
Dave Chinner5adc94c2010-09-24 21:58:31 +1000911xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000912 struct xfs_buftarg *target,
913 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000914 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100915 int flags,
Dave Chinnerba3726742014-10-02 09:05:32 +1000916 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100917 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000918{
Dave Chinnereab4e632012-11-12 22:54:02 +1100919 struct xfs_buf *bp;
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800920 int error;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000921
Dave Chinnerba3726742014-10-02 09:05:32 +1000922 *bpp = NULL;
923
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800924 error = xfs_buf_get_uncached(target, numblks, flags, &bp);
925 if (error)
926 return error;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000927
928 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000929 ASSERT(bp->b_map_count == 1);
Dave Chinnerba3726742014-10-02 09:05:32 +1000930 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
Dave Chinner3e85c862012-06-22 18:50:09 +1000931 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000932 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100933 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000934
Brian Foster6af88cd2018-07-11 22:26:35 -0700935 xfs_buf_submit(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000936 if (bp->b_error) {
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800937 error = bp->b_error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800938 xfs_buf_relse(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000939 return error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800940 }
Dave Chinnerba3726742014-10-02 09:05:32 +1000941
942 *bpp = bp;
943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800946int
Dave Chinner686865f2010-09-24 20:07:47 +1000947xfs_buf_get_uncached(
948 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000949 size_t numblks,
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800950 int flags,
951 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000953 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000954 int error, i;
Dave Chinner3e85c862012-06-22 18:50:09 +1000955 struct xfs_buf *bp;
956 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800958 *bpp = NULL;
959
Brian Fosterc891c302016-07-20 11:13:43 +1000960 /* flags might contain irrelevant bits, pass only what we care about */
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800961 error = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT, &bp);
962 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Dave Chinnere70b73f2012-04-23 15:58:49 +1000965 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000966 error = _xfs_buf_get_pages(bp, page_count);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000967 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 goto fail_free_buf;
969
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000970 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000971 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800972 if (!bp->b_pages[i]) {
973 error = -ENOMEM;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000974 goto fail_free_mem;
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000977 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Dave Chinner611c9942012-04-23 15:59:07 +1000979 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000980 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100981 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500982 "%s: failed to map pages", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Dave Chinner686865f2010-09-24 20:07:47 +1000986 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800987 *bpp = bp;
988 return 0;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000991 while (--i >= 0)
992 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000993 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 fail_free_buf:
Dave Chinner3e85c862012-06-22 18:50:09 +1000995 xfs_buf_free_maps(bp);
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800996 kmem_cache_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 fail:
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800998 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 * Increment reference count on buffer, to hold the buffer concurrently
1003 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * Must hold the buffer already to call this function.
1005 */
1006void
Nathan Scottce8e9222006-01-11 15:39:08 +11001007xfs_buf_hold(
Dave Chinnere8222612020-12-16 16:07:34 -08001008 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001010 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001011 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014/*
Brian Foster9c7504a2016-07-20 11:15:28 +10001015 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
1016 * placed on LRU or freed (depending on b_lru_ref).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 */
1018void
Nathan Scottce8e9222006-01-11 15:39:08 +11001019xfs_buf_rele(
Dave Chinnere8222612020-12-16 16:07:34 -08001020 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Dave Chinner74f75a02010-09-24 19:59:04 +10001022 struct xfs_perag *pag = bp->b_pag;
Brian Foster9c7504a2016-07-20 11:15:28 +10001023 bool release;
1024 bool freebuf = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001026 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Dave Chinner74f75a02010-09-24 19:59:04 +10001028 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +11001029 ASSERT(list_empty(&bp->b_lru));
Brian Foster9c7504a2016-07-20 11:15:28 +10001030 if (atomic_dec_and_test(&bp->b_hold)) {
1031 xfs_buf_ioacct_dec(bp);
Nathan Scottfad3aa12006-02-01 12:14:52 +11001032 xfs_buf_free(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001033 }
Nathan Scottfad3aa12006-02-01 12:14:52 +11001034 return;
1035 }
1036
Lachlan McIlroy37906892008-08-13 15:42:10 +10001037 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinnera4082352013-08-28 10:18:06 +10001038
Dave Chinner37fd1672018-10-18 17:21:29 +11001039 /*
1040 * We grab the b_lock here first to serialise racing xfs_buf_rele()
1041 * calls. The pag_buf_lock being taken on the last reference only
1042 * serialises against racing lookups in xfs_buf_find(). IOWs, the second
1043 * to last reference we drop here is not serialised against the last
1044 * reference until we take bp->b_lock. Hence if we don't grab b_lock
1045 * first, the last "release" reference can win the race to the lock and
1046 * free the buffer before the second-to-last reference is processed,
1047 * leading to a use-after-free scenario.
1048 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001049 spin_lock(&bp->b_lock);
Dave Chinner37fd1672018-10-18 17:21:29 +11001050 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +10001051 if (!release) {
1052 /*
1053 * Drop the in-flight state if the buffer is already on the LRU
1054 * and it holds the only reference. This is racy because we
1055 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1056 * ensures the decrement occurs only once per-buf.
1057 */
1058 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
Brian Foster63db7c82017-05-31 08:22:52 -07001059 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001060 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
Brian Foster9c7504a2016-07-20 11:15:28 +10001062
1063 /* the last reference has been dropped ... */
Brian Foster63db7c82017-05-31 08:22:52 -07001064 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001065 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1066 /*
1067 * If the buffer is added to the LRU take a new reference to the
1068 * buffer for the LRU and clear the (now stale) dispose list
1069 * state flag
1070 */
1071 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1072 bp->b_state &= ~XFS_BSTATE_DISPOSE;
1073 atomic_inc(&bp->b_hold);
1074 }
1075 spin_unlock(&pag->pag_buf_lock);
1076 } else {
1077 /*
1078 * most of the time buffers will already be removed from the
1079 * LRU, so optimise that case by checking for the
1080 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1081 * was on was the disposal list
1082 */
1083 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1084 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1085 } else {
1086 ASSERT(list_empty(&bp->b_lru));
1087 }
1088
1089 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Lucas Stach6031e732016-12-07 17:36:36 +11001090 rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
1091 xfs_buf_hash_params);
Brian Foster9c7504a2016-07-20 11:15:28 +10001092 spin_unlock(&pag->pag_buf_lock);
1093 xfs_perag_put(pag);
1094 freebuf = true;
1095 }
1096
1097out_unlock:
1098 spin_unlock(&bp->b_lock);
1099
1100 if (freebuf)
1101 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104
1105/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001106 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +11001107 *
1108 * If we come across a stale, pinned, locked buffer, we know that we are
1109 * being asked to lock a buffer that has been reallocated. Because it is
1110 * pinned, we know that the log has not been pushed to disk and hence it
1111 * will still be locked. Rather than continuing to have trylock attempts
1112 * fail until someone else pushes the log, push it ourselves before
1113 * returning. This means that the xfsaild will not get stuck trying
1114 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 */
1116int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001117xfs_buf_trylock(
1118 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 int locked;
1121
Nathan Scottce8e9222006-01-11 15:39:08 +11001122 locked = down_trylock(&bp->b_sema) == 0;
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001123 if (locked)
Darrick J. Wong479c6412016-06-21 11:53:28 +10001124 trace_xfs_buf_trylock(bp, _RET_IP_);
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001125 else
Darrick J. Wong479c6412016-06-21 11:53:28 +10001126 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001127 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001131 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +10001132 *
1133 * If we come across a stale, pinned, locked buffer, we know that we
1134 * are being asked to lock a buffer that has been reallocated. Because
1135 * it is pinned, we know that the log has not been pushed to disk and
1136 * hence it will still be locked. Rather than sleeping until someone
1137 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001139void
1140xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001141 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001143 trace_xfs_buf_lock(bp, _RET_IP_);
1144
Dave Chinnered3b4d62010-05-21 12:07:08 +10001145 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001146 xfs_log_force(bp->b_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +11001147 down(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001148
1149 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152void
Nathan Scottce8e9222006-01-11 15:39:08 +11001153xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001154 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Brian Foster20e8a062017-04-21 12:40:44 -07001156 ASSERT(xfs_buf_islocked(bp));
1157
Nathan Scottce8e9222006-01-11 15:39:08 +11001158 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001159 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Nathan Scottce8e9222006-01-11 15:39:08 +11001162STATIC void
1163xfs_buf_wait_unpin(
Dave Chinnere8222612020-12-16 16:07:34 -08001164 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 DECLARE_WAITQUEUE (wait, current);
1167
Nathan Scottce8e9222006-01-11 15:39:08 +11001168 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return;
1170
Nathan Scottce8e9222006-01-11 15:39:08 +11001171 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 for (;;) {
1173 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001174 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001176 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001178 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 set_current_state(TASK_RUNNING);
1180}
1181
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001182static void
1183xfs_buf_ioerror_alert_ratelimited(
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001184 struct xfs_buf *bp)
1185{
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001186 static unsigned long lasttime;
1187 static struct xfs_buftarg *lasttarg;
1188
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001189 if (bp->b_target != lasttarg ||
1190 time_after(jiffies, (lasttime + 5*HZ))) {
1191 lasttime = jiffies;
1192 xfs_buf_ioerror_alert(bp, __this_address);
1193 }
1194 lasttarg = bp->b_target;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001195}
1196
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001197/*
1198 * Account for this latest trip around the retry handler, and decide if
1199 * we've failed enough times to constitute a permanent failure.
1200 */
1201static bool
1202xfs_buf_ioerror_permanent(
1203 struct xfs_buf *bp,
1204 struct xfs_error_cfg *cfg)
1205{
1206 struct xfs_mount *mp = bp->b_mount;
1207
1208 if (cfg->max_retries != XFS_ERR_RETRY_FOREVER &&
1209 ++bp->b_retries > cfg->max_retries)
1210 return true;
1211 if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
1212 time_after(jiffies, cfg->retry_timeout + bp->b_first_retry_time))
1213 return true;
1214
1215 /* At unmount we may treat errors differently */
1216 if ((mp->m_flags & XFS_MOUNT_UNMOUNTING) && mp->m_fail_unmount)
1217 return true;
1218
1219 return false;
1220}
1221
1222/*
1223 * On a sync write or shutdown we just want to stale the buffer and let the
1224 * caller handle the error in bp->b_error appropriately.
1225 *
1226 * If the write was asynchronous then no one will be looking for the error. If
1227 * this is the first failure of this type, clear the error state and write the
1228 * buffer out again. This means we always retry an async write failure at least
1229 * once, but we also need to set the buffer up to behave correctly now for
1230 * repeated failures.
1231 *
1232 * If we get repeated async write failures, then we take action according to the
1233 * error configuration we have been set up to use.
1234 *
Christoph Hellwig70796c62020-09-01 10:55:45 -07001235 * Returns true if this function took care of error handling and the caller must
1236 * not touch the buffer again. Return false if the caller should proceed with
1237 * normal I/O completion handling.
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001238 */
Christoph Hellwig70796c62020-09-01 10:55:45 -07001239static bool
1240xfs_buf_ioend_handle_error(
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001241 struct xfs_buf *bp)
1242{
1243 struct xfs_mount *mp = bp->b_mount;
1244 struct xfs_error_cfg *cfg;
1245
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001246 /*
1247 * If we've already decided to shutdown the filesystem because of I/O
1248 * errors, there's no point in giving this a retry.
1249 */
1250 if (XFS_FORCED_SHUTDOWN(mp))
1251 goto out_stale;
1252
1253 xfs_buf_ioerror_alert_ratelimited(bp);
1254
1255 /*
Christoph Hellwig22c10582020-09-01 10:55:46 -07001256 * We're not going to bother about retrying this during recovery.
1257 * One strike!
1258 */
1259 if (bp->b_flags & _XBF_LOGRECOVERY) {
1260 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1261 return false;
1262 }
1263
1264 /*
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001265 * Synchronous writes will have callers process the error.
1266 */
1267 if (!(bp->b_flags & XBF_ASYNC))
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001268 goto out_stale;
1269
1270 trace_xfs_buf_iodone_async(bp, _RET_IP_);
1271
1272 cfg = xfs_error_get_cfg(mp, XFS_ERR_METADATA, bp->b_error);
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001273 if (bp->b_last_error != bp->b_error ||
1274 !(bp->b_flags & (XBF_STALE | XBF_WRITE_FAIL))) {
1275 bp->b_last_error = bp->b_error;
1276 if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
1277 !bp->b_first_retry_time)
1278 bp->b_first_retry_time = jiffies;
1279 goto resubmit;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001280 }
1281
1282 /*
1283 * Permanent error - we need to trigger a shutdown if we haven't already
1284 * to indicate that inconsistency will result from this action.
1285 */
1286 if (xfs_buf_ioerror_permanent(bp, cfg)) {
1287 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1288 goto out_stale;
1289 }
1290
1291 /* Still considered a transient error. Caller will schedule retries. */
Christoph Hellwig844c9352020-09-01 10:55:45 -07001292 if (bp->b_flags & _XBF_INODES)
1293 xfs_buf_inode_io_fail(bp);
1294 else if (bp->b_flags & _XBF_DQUOTS)
1295 xfs_buf_dquot_io_fail(bp);
1296 else
1297 ASSERT(list_empty(&bp->b_li_list));
1298 xfs_buf_ioerror(bp, 0);
1299 xfs_buf_relse(bp);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001300 return true;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001301
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001302resubmit:
1303 xfs_buf_ioerror(bp, 0);
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001304 bp->b_flags |= (XBF_DONE | XBF_WRITE_FAIL);
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001305 xfs_buf_submit(bp);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001306 return true;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001307out_stale:
1308 xfs_buf_stale(bp);
1309 bp->b_flags |= XBF_DONE;
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001310 bp->b_flags &= ~XBF_WRITE;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001311 trace_xfs_buf_error_relse(bp, _RET_IP_);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001312 return false;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001313}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Christoph Hellwig76b2d322020-09-01 10:55:20 -07001315static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001316xfs_buf_ioend(
1317 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001319 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001320
Dave Chinner61be9c52014-10-02 09:04:31 +10001321 /*
1322 * Pull in IO completion errors now. We are guaranteed to be running
1323 * single threaded, so we don't need the lock to read b_io_error.
1324 */
1325 if (!bp->b_error && bp->b_io_error)
1326 xfs_buf_ioerror(bp, bp->b_io_error);
1327
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001328 if (bp->b_flags & XBF_READ) {
Dave Chinnerb01d1462020-06-29 14:48:47 -07001329 if (!bp->b_error && bp->b_ops)
1330 bp->b_ops->verify_read(bp);
1331 if (!bp->b_error)
1332 bp->b_flags |= XBF_DONE;
Christoph Hellwig23fb5a92020-09-01 10:55:20 -07001333 } else {
1334 if (!bp->b_error) {
1335 bp->b_flags &= ~XBF_WRITE_FAIL;
1336 bp->b_flags |= XBF_DONE;
1337 }
Dave Chinner9fe5c772020-06-29 14:48:47 -07001338
Christoph Hellwig70796c62020-09-01 10:55:45 -07001339 if (unlikely(bp->b_error) && xfs_buf_ioend_handle_error(bp))
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001340 return;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001341
1342 /* clear the retry state */
1343 bp->b_last_error = 0;
1344 bp->b_retries = 0;
1345 bp->b_first_retry_time = 0;
1346
1347 /*
1348 * Note that for things like remote attribute buffers, there may
1349 * not be a buffer log item here, so processing the buffer log
1350 * item must remain optional.
1351 */
1352 if (bp->b_log_item)
1353 xfs_buf_item_done(bp);
1354
Christoph Hellwig23fb5a92020-09-01 10:55:20 -07001355 if (bp->b_flags & _XBF_INODES)
1356 xfs_buf_inode_iodone(bp);
1357 else if (bp->b_flags & _XBF_DQUOTS)
1358 xfs_buf_dquot_iodone(bp);
Christoph Hellwig22c10582020-09-01 10:55:46 -07001359
Dave Chinnerf593bf12020-06-29 14:48:46 -07001360 }
Christoph Hellwig6a7584b2020-09-01 10:55:44 -07001361
Christoph Hellwig22c10582020-09-01 10:55:46 -07001362 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD |
1363 _XBF_LOGRECOVERY);
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001364
Christoph Hellwig6a7584b2020-09-01 10:55:44 -07001365 if (bp->b_flags & XBF_ASYNC)
1366 xfs_buf_relse(bp);
1367 else
1368 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
Dave Chinnere8aaba92014-10-02 09:04:22 +10001371static void
1372xfs_buf_ioend_work(
1373 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001375 struct xfs_buf *bp =
Dave Chinnere8222612020-12-16 16:07:34 -08001376 container_of(work, struct xfs_buf, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001377
Dave Chinnere8aaba92014-10-02 09:04:22 +10001378 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001381static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001382xfs_buf_ioend_async(
1383 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001385 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001386 queue_work(bp->b_mount->m_buf_workqueue, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389void
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001390__xfs_buf_ioerror(
Dave Chinnere8222612020-12-16 16:07:34 -08001391 struct xfs_buf *bp,
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001392 int error,
1393 xfs_failaddr_t failaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Dave Chinner24513372014-06-25 14:58:08 +10001395 ASSERT(error <= 0 && error >= -1000);
1396 bp->b_error = error;
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001397 trace_xfs_buf_ioerror(bp, error, failaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Christoph Hellwig901796a2011-10-10 16:52:49 +00001400void
1401xfs_buf_ioerror_alert(
1402 struct xfs_buf *bp,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -08001403 xfs_failaddr_t func)
Christoph Hellwig901796a2011-10-10 16:52:49 +00001404{
Brian Fosterf9bccfc2020-05-06 13:25:21 -07001405 xfs_buf_alert_ratelimited(bp, "XFS: metadata IO error",
1406 "metadata I/O error in \"%pS\" at daddr 0x%llx len %d error %d",
1407 func, (uint64_t)XFS_BUF_ADDR(bp),
1408 bp->b_length, -bp->b_error);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001409}
1410
Brian Foster54b3b1f2020-05-06 13:25:19 -07001411/*
1412 * To simulate an I/O failure, the buffer must be locked and held with at least
1413 * three references. The LRU reference is dropped by the stale call. The buf
1414 * item reference is dropped via ioend processing. The third reference is owned
1415 * by the caller and is dropped on I/O completion if the buffer is XBF_ASYNC.
1416 */
1417void
1418xfs_buf_ioend_fail(
1419 struct xfs_buf *bp)
1420{
1421 bp->b_flags &= ~XBF_DONE;
1422 xfs_buf_stale(bp);
1423 xfs_buf_ioerror(bp, -EIO);
1424 xfs_buf_ioend(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001425}
Christoph Hellwig939d7232010-07-20 17:51:16 +10001426
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001427int
1428xfs_bwrite(
1429 struct xfs_buf *bp)
1430{
1431 int error;
1432
1433 ASSERT(xfs_buf_islocked(bp));
1434
1435 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001436 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
Brian Fosterb6983e82020-05-06 13:25:20 -07001437 XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001438
Brian Foster6af88cd2018-07-11 22:26:35 -07001439 error = xfs_buf_submit(bp);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001440 if (error)
1441 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001442 return error;
1443}
1444
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001445static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001446xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001447 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001449 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Brian Foster7376d742020-05-06 13:29:19 -07001451 if (!bio->bi_status &&
1452 (bp->b_flags & XBF_WRITE) && (bp->b_flags & XBF_ASYNC) &&
Brian Foster43dc0aa2020-05-08 08:50:52 -07001453 XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_IOERROR))
Brian Foster7376d742020-05-06 13:29:19 -07001454 bio->bi_status = BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Dave Chinner37eb17e2012-11-12 22:09:46 +11001456 /*
1457 * don't overwrite existing errors - otherwise we can lose errors on
1458 * buffers that require multiple bios to complete.
1459 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001460 if (bio->bi_status) {
1461 int error = blk_status_to_errno(bio->bi_status);
1462
1463 cmpxchg(&bp->b_io_error, 0, error);
1464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Dave Chinner37eb17e2012-11-12 22:09:46 +11001466 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001467 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1468
Dave Chinnere8aaba92014-10-02 09:04:22 +10001469 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1470 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
Dave Chinner3e85c862012-06-22 18:50:09 +10001474static void
1475xfs_buf_ioapply_map(
1476 struct xfs_buf *bp,
1477 int map,
1478 int *buf_offset,
1479 int *count,
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001480 int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Dave Chinner3e85c862012-06-22 18:50:09 +10001482 int page_index;
1483 int total_nr_pages = bp->b_page_count;
1484 int nr_pages;
1485 struct bio *bio;
1486 sector_t sector = bp->b_maps[map].bm_bn;
1487 int size;
1488 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Dave Chinner3e85c862012-06-22 18:50:09 +10001490 /* skip the pages in the buffer before the start offset */
1491 page_index = 0;
1492 offset = *buf_offset;
1493 while (offset >= PAGE_SIZE) {
1494 page_index++;
1495 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001496 }
1497
Dave Chinner3e85c862012-06-22 18:50:09 +10001498 /*
1499 * Limit the IO size to the length of the current vector, and update the
1500 * remaining IO count for the next time around.
1501 */
1502 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1503 *count -= size;
1504 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001507 atomic_inc(&bp->b_io_remaining);
Ming Leic908e382016-05-30 21:34:33 +08001508 nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 bio = bio_alloc(GFP_NOIO, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001511 bio_set_dev(bio, bp->b_target->bt_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001512 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001513 bio->bi_end_io = xfs_buf_bio_end_io;
1514 bio->bi_private = bp;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001515 bio->bi_opf = op;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001516
Dave Chinner3e85c862012-06-22 18:50:09 +10001517 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001518 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 if (nbytes > size)
1521 nbytes = size;
1522
Dave Chinner3e85c862012-06-22 18:50:09 +10001523 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1524 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001525 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 break;
1527
1528 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001529 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 size -= nbytes;
1531 total_nr_pages--;
1532 }
1533
Kent Overstreet4f024f32013-10-11 15:44:27 -07001534 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001535 if (xfs_buf_is_vmapped(bp)) {
1536 flush_kernel_vmap_range(bp->b_addr,
1537 xfs_buf_vmap_len(bp));
1538 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001539 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (size)
1541 goto next_chunk;
1542 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001543 /*
1544 * This is guaranteed not to be the last io reference count
Dave Chinner595bff72014-10-02 09:05:14 +10001545 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001546 */
1547 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001548 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001549 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001551
1552}
1553
1554STATIC void
1555_xfs_buf_ioapply(
1556 struct xfs_buf *bp)
1557{
1558 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001559 int op;
Dave Chinner3e85c862012-06-22 18:50:09 +10001560 int offset;
1561 int size;
1562 int i;
1563
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001564 /*
1565 * Make sure we capture only current IO errors rather than stale errors
1566 * left over from previous use of the buffer (e.g. failed readahead).
1567 */
1568 bp->b_error = 0;
1569
Dave Chinner3e85c862012-06-22 18:50:09 +10001570 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001571 op = REQ_OP_WRITE;
Dave Chinner1813dd62012-11-14 17:54:40 +11001572
1573 /*
1574 * Run the write verifier callback function if it exists. If
1575 * this function fails it will mark the buffer with an error and
1576 * the IO should not be dispatched.
1577 */
1578 if (bp->b_ops) {
1579 bp->b_ops->verify_write(bp);
1580 if (bp->b_error) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001581 xfs_force_shutdown(bp->b_mount,
Dave Chinner1813dd62012-11-14 17:54:40 +11001582 SHUTDOWN_CORRUPT_INCORE);
1583 return;
1584 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001585 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001586 struct xfs_mount *mp = bp->b_mount;
Dave Chinner400b9d82014-08-04 12:42:40 +10001587
1588 /*
1589 * non-crc filesystems don't attach verifiers during
1590 * log recovery, so don't warn for such filesystems.
1591 */
1592 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1593 xfs_warn(mp,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001594 "%s: no buf ops on daddr 0x%llx len %d",
Dave Chinner400b9d82014-08-04 12:42:40 +10001595 __func__, bp->b_bn, bp->b_length);
Darrick J. Wong9c712a12018-01-08 10:51:26 -08001596 xfs_hex_dump(bp->b_addr,
1597 XFS_CORRUPTION_DUMP_LEN);
Dave Chinner400b9d82014-08-04 12:42:40 +10001598 dump_stack();
1599 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001600 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001601 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001602 op = REQ_OP_READ;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001603 if (bp->b_flags & XBF_READ_AHEAD)
1604 op |= REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001605 }
1606
1607 /* we only use the buffer cache for meta-data */
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001608 op |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001609
1610 /*
1611 * Walk all the vectors issuing IO on them. Set up the initial offset
1612 * into the buffer and the desired IO size before we start -
1613 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1614 * subsequent call.
1615 */
1616 offset = bp->b_offset;
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001617 size = BBTOB(bp->b_length);
Dave Chinner3e85c862012-06-22 18:50:09 +10001618 blk_start_plug(&plug);
1619 for (i = 0; i < bp->b_map_count; i++) {
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001620 xfs_buf_ioapply_map(bp, i, &offset, &size, op);
Dave Chinner3e85c862012-06-22 18:50:09 +10001621 if (bp->b_error)
1622 break;
1623 if (size <= 0)
1624 break; /* all done */
1625 }
1626 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
Dave Chinner595bff72014-10-02 09:05:14 +10001629/*
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001630 * Wait for I/O completion of a sync buffer and return the I/O error code.
Dave Chinner595bff72014-10-02 09:05:14 +10001631 */
Brian Fostereaebb512018-07-11 22:26:34 -07001632static int
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001633xfs_buf_iowait(
Dave Chinner595bff72014-10-02 09:05:14 +10001634 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001636 ASSERT(!(bp->b_flags & XBF_ASYNC));
1637
1638 trace_xfs_buf_iowait(bp, _RET_IP_);
1639 wait_for_completion(&bp->b_iowait);
1640 trace_xfs_buf_iowait_done(bp, _RET_IP_);
1641
1642 return bp->b_error;
1643}
1644
1645/*
1646 * Buffer I/O submission path, read or write. Asynchronous submission transfers
1647 * the buffer lock ownership and the current reference to the IO. It is not
1648 * safe to reference the buffer after a call to this function unless the caller
1649 * holds an additional reference itself.
1650 */
Christoph Hellwig26e328752020-09-01 10:55:47 -07001651static int
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001652__xfs_buf_submit(
1653 struct xfs_buf *bp,
1654 bool wait)
1655{
1656 int error = 0;
1657
Dave Chinner595bff72014-10-02 09:05:14 +10001658 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001660 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff72014-10-02 09:05:14 +10001661
1662 /* on shutdown we stale and complete the buffer immediately */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001663 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
Brian Foster54b3b1f2020-05-06 13:25:19 -07001664 xfs_buf_ioend_fail(bp);
Brian Fostereaebb512018-07-11 22:26:34 -07001665 return -EIO;
Dave Chinner595bff72014-10-02 09:05:14 +10001666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001668 /*
1669 * Grab a reference so the buffer does not go away underneath us. For
1670 * async buffers, I/O completion drops the callers reference, which
1671 * could occur before submission returns.
1672 */
1673 xfs_buf_hold(bp);
1674
Christoph Hellwig375ec692011-08-23 08:28:03 +00001675 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001676 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Dave Chinner61be9c52014-10-02 09:04:31 +10001678 /* clear the internal error state to avoid spurious errors */
1679 bp->b_io_error = 0;
1680
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001681 /*
Brian Fostereaebb512018-07-11 22:26:34 -07001682 * Set the count to 1 initially, this will stop an I/O completion
1683 * callout which happens before we have started all the I/O from calling
1684 * xfs_buf_ioend too early.
1685 */
1686 atomic_set(&bp->b_io_remaining, 1);
1687 if (bp->b_flags & XBF_ASYNC)
1688 xfs_buf_ioacct_inc(bp);
1689 _xfs_buf_ioapply(bp);
1690
1691 /*
1692 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1693 * reference we took above. If we drop it to zero, run completion so
1694 * that we don't return to the caller with completion still pending.
1695 */
1696 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1697 if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
1698 xfs_buf_ioend(bp);
1699 else
1700 xfs_buf_ioend_async(bp);
1701 }
1702
Brian Foster6af88cd2018-07-11 22:26:35 -07001703 if (wait)
1704 error = xfs_buf_iowait(bp);
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001705
Dave Chinner595bff72014-10-02 09:05:14 +10001706 /*
Brian Foster6af88cd2018-07-11 22:26:35 -07001707 * Release the hold that keeps the buffer referenced for the entire
1708 * I/O. Note that if the buffer is async, it is not safe to reference
1709 * after this release.
Dave Chinner595bff72014-10-02 09:05:14 +10001710 */
1711 xfs_buf_rele(bp);
1712 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001715void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001716xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001717 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 size_t offset)
1719{
1720 struct page *page;
1721
Dave Chinner611c9942012-04-23 15:59:07 +10001722 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001723 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Nathan Scottce8e9222006-01-11 15:39:08 +11001725 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001726 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001727 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730void
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001731xfs_buf_zero(
1732 struct xfs_buf *bp,
1733 size_t boff,
1734 size_t bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Dave Chinner795cac72012-04-23 15:58:53 +10001736 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 bend = boff + bsize;
1739 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001740 struct page *page;
1741 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Dave Chinner795cac72012-04-23 15:58:53 +10001743 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1744 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1745 page = bp->b_pages[page_index];
1746 csize = min_t(size_t, PAGE_SIZE - page_offset,
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001747 BBTOB(bp->b_length) - boff);
Dave Chinner795cac72012-04-23 15:58:53 +10001748
1749 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001751 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 boff += csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755}
1756
1757/*
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001758 * Log a message about and stale a buffer that a caller has decided is corrupt.
1759 *
1760 * This function should be called for the kinds of metadata corruption that
1761 * cannot be detect from a verifier, such as incorrect inter-block relationship
1762 * data. Do /not/ call this function from a verifier function.
1763 *
1764 * The buffer must be XBF_DONE prior to the call. Afterwards, the buffer will
1765 * be marked stale, but b_error will not be set. The caller is responsible for
1766 * releasing the buffer or fixing it.
1767 */
1768void
1769__xfs_buf_mark_corrupt(
1770 struct xfs_buf *bp,
1771 xfs_failaddr_t fa)
1772{
1773 ASSERT(bp->b_flags & XBF_DONE);
1774
Darrick J. Wonge83cf872020-03-11 10:37:54 -07001775 xfs_buf_corruption_error(bp, fa);
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001776 xfs_buf_stale(bp);
1777}
1778
1779/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001780 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 */
1782
1783/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001784 * Wait for any bufs with callbacks that have been submitted but have not yet
1785 * returned. These buffers will have an elevated hold count, so wait on those
1786 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001788static enum lru_status
Brian Foster10fb9ac2021-01-22 16:48:19 -08001789xfs_buftarg_drain_rele(
Dave Chinnere80dfa12013-08-28 10:18:05 +10001790 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001791 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001792 spinlock_t *lru_lock,
1793 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Dave Chinnere80dfa12013-08-28 10:18:05 +10001795{
1796 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001797 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001798
1799 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001800 /* need to wait, so skip it this pass */
Brian Foster10fb9ac2021-01-22 16:48:19 -08001801 trace_xfs_buf_drain_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001802 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
Dave Chinnera4082352013-08-28 10:18:06 +10001804 if (!spin_trylock(&bp->b_lock))
1805 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001806
Dave Chinnera4082352013-08-28 10:18:06 +10001807 /*
1808 * clear the LRU reference count so the buffer doesn't get
1809 * ignored in xfs_buf_rele().
1810 */
1811 atomic_set(&bp->b_lru_ref, 0);
1812 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001813 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001814 spin_unlock(&bp->b_lock);
1815 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
Brian Foster8321ddb2021-01-22 16:48:20 -08001818/*
1819 * Wait for outstanding I/O on the buftarg to complete.
1820 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001821void
Brian Foster8321ddb2021-01-22 16:48:20 -08001822xfs_buftarg_wait(
Dave Chinnere80dfa12013-08-28 10:18:05 +10001823 struct xfs_buftarg *btp)
1824{
Dave Chinner85bec542016-01-19 08:28:10 +11001825 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001826 * First wait on the buftarg I/O count for all in-flight buffers to be
1827 * released. This is critical as new buffers do not make the LRU until
1828 * they are released.
1829 *
1830 * Next, flush the buffer workqueue to ensure all completion processing
1831 * has finished. Just waiting on buffer locks is not sufficient for
1832 * async IO as the reference count held over IO is not released until
1833 * after the buffer lock is dropped. Hence we need to ensure here that
1834 * all reference counts have been dropped before we start walking the
1835 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001836 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001837 while (percpu_counter_sum(&btp->bt_io_count))
1838 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001839 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Brian Foster8321ddb2021-01-22 16:48:20 -08001840}
1841
1842void
1843xfs_buftarg_drain(
1844 struct xfs_buftarg *btp)
1845{
1846 LIST_HEAD(dispose);
1847 int loop = 0;
1848 bool write_fail = false;
1849
1850 xfs_buftarg_wait(btp);
Dave Chinner85bec542016-01-19 08:28:10 +11001851
Dave Chinnera4082352013-08-28 10:18:06 +10001852 /* loop until there is nothing left on the lru list. */
1853 while (list_lru_count(&btp->bt_lru)) {
Brian Foster10fb9ac2021-01-22 16:48:19 -08001854 list_lru_walk(&btp->bt_lru, xfs_buftarg_drain_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001855 &dispose, LONG_MAX);
1856
1857 while (!list_empty(&dispose)) {
1858 struct xfs_buf *bp;
1859 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1860 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001861 if (bp->b_flags & XBF_WRITE_FAIL) {
Brian Foster61948b62020-05-06 13:25:21 -07001862 write_fail = true;
1863 xfs_buf_alert_ratelimited(bp,
1864 "XFS: Corruption Alert",
Darrick J. Wongc219b012018-01-08 11:39:18 -08001865"Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001866 (long long)bp->b_bn);
1867 }
Dave Chinnera4082352013-08-28 10:18:06 +10001868 xfs_buf_rele(bp);
1869 }
1870 if (loop++ != 0)
1871 delay(100);
1872 }
Brian Foster61948b62020-05-06 13:25:21 -07001873
1874 /*
1875 * If one or more failed buffers were freed, that means dirty metadata
1876 * was thrown away. This should only ever happen after I/O completion
1877 * handling has elevated I/O error(s) to permanent failures and shuts
1878 * down the fs.
1879 */
1880 if (write_fail) {
1881 ASSERT(XFS_FORCED_SHUTDOWN(btp->bt_mount));
1882 xfs_alert(btp->bt_mount,
1883 "Please run xfs_repair to determine the extent of the problem.");
1884 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001885}
1886
1887static enum lru_status
1888xfs_buftarg_isolate(
1889 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001890 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001891 spinlock_t *lru_lock,
1892 void *arg)
1893{
1894 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1895 struct list_head *dispose = arg;
1896
1897 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001898 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1899 * If we fail to get the lock, just skip it.
1900 */
1901 if (!spin_trylock(&bp->b_lock))
1902 return LRU_SKIP;
1903 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001904 * Decrement the b_lru_ref count unless the value is already
1905 * zero. If the value is already zero, we need to reclaim the
1906 * buffer, otherwise it gets another trip through the LRU.
1907 */
Vratislav Bendel19957a12018-03-06 17:07:44 -08001908 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
Dave Chinnera4082352013-08-28 10:18:06 +10001909 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001910 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001911 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001912
Dave Chinnera4082352013-08-28 10:18:06 +10001913 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001914 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001915 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001916 return LRU_REMOVED;
1917}
1918
Andrew Mortonaddbda42013-08-28 10:18:06 +10001919static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001920xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001921 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001922 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001923{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001924 struct xfs_buftarg *btp = container_of(shrink,
1925 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001926 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001927 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001928
Vladimir Davydov503c3582015-02-12 14:58:47 -08001929 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1930 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001931
1932 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001933 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001934 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1935 list_del_init(&bp->b_lru);
1936 xfs_buf_rele(bp);
1937 }
1938
Dave Chinnere80dfa12013-08-28 10:18:05 +10001939 return freed;
1940}
1941
Andrew Mortonaddbda42013-08-28 10:18:06 +10001942static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001943xfs_buftarg_shrink_count(
1944 struct shrinker *shrink,
1945 struct shrink_control *sc)
1946{
1947 struct xfs_buftarg *btp = container_of(shrink,
1948 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001949 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001950}
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952void
1953xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001954 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001956 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001957 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1958 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001959 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001960
Dave Chinner2291dab2016-12-09 16:49:54 +11001961 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001962
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001963 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001966int
1967xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001969 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001971 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001972 btp->bt_meta_sectorsize = sectorsize;
1973 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Nathan Scottce8e9222006-01-11 15:39:08 +11001975 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001976 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001977 "Cannot set_blocksize to %u on device %pg",
1978 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001979 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001982 /* Set up device logical sector size mask */
1983 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1984 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return 0;
1987}
1988
1989/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001990 * When allocating the initial buffer target we have not yet
1991 * read in the superblock, so don't know what sized sectors
1992 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001993 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994STATIC int
1995xfs_setsize_buftarg_early(
1996 xfs_buftarg_t *btp,
1997 struct block_device *bdev)
1998{
Eric Sandeena96c4152014-04-14 19:00:29 +10001999 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002xfs_buftarg_t *
2003xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10002004 struct xfs_mount *mp,
Dan Williams486aff52017-08-24 15:12:50 -07002005 struct block_device *bdev,
2006 struct dax_device *dax_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
2008 xfs_buftarg_t *btp;
2009
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07002010 btp = kmem_zalloc(sizeof(*btp), KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Dave Chinnerebad8612010-09-22 10:47:20 +10002012 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11002013 btp->bt_dev = bdev->bd_dev;
2014 btp->bt_bdev = bdev;
Dan Williams486aff52017-08-24 15:12:50 -07002015 btp->bt_daxdev = dax_dev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11002016
Brian Fosterf9bccfc2020-05-06 13:25:21 -07002017 /*
2018 * Buffer IO error rate limiting. Limit it to no more than 10 messages
2019 * per 30 seconds so as to not spam logs too much on repeated errors.
2020 */
2021 ratelimit_state_init(&btp->bt_ioerror_rl, 30 * HZ,
2022 DEFAULT_RATELIMIT_BURST);
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (xfs_setsize_buftarg_early(btp, bdev))
Michal Hockod210a982017-11-23 17:13:40 +01002025 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10002026
2027 if (list_lru_init(&btp->bt_lru))
Michal Hockod210a982017-11-23 17:13:40 +01002028 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10002029
Brian Foster9c7504a2016-07-20 11:15:28 +10002030 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
Michal Hockod210a982017-11-23 17:13:40 +01002031 goto error_lru;
Brian Foster9c7504a2016-07-20 11:15:28 +10002032
Dave Chinnere80dfa12013-08-28 10:18:05 +10002033 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
2034 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11002035 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10002036 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Michal Hockod210a982017-11-23 17:13:40 +01002037 if (register_shrinker(&btp->bt_shrinker))
2038 goto error_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return btp;
2040
Michal Hockod210a982017-11-23 17:13:40 +01002041error_pcpu:
2042 percpu_counter_destroy(&btp->bt_io_count);
2043error_lru:
2044 list_lru_destroy(&btp->bt_lru);
2045error_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002046 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return NULL;
2048}
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050/*
Brian Foster20e8a062017-04-21 12:40:44 -07002051 * Cancel a delayed write list.
2052 *
2053 * Remove each buffer from the list, clear the delwri queue flag and drop the
2054 * associated buffer reference.
2055 */
2056void
2057xfs_buf_delwri_cancel(
2058 struct list_head *list)
2059{
2060 struct xfs_buf *bp;
2061
2062 while (!list_empty(list)) {
2063 bp = list_first_entry(list, struct xfs_buf, b_list);
2064
2065 xfs_buf_lock(bp);
2066 bp->b_flags &= ~_XBF_DELWRI_Q;
2067 list_del_init(&bp->b_list);
2068 xfs_buf_relse(bp);
2069 }
2070}
2071
2072/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002073 * Add a buffer to the delayed write list.
2074 *
2075 * This queues a buffer for writeout if it hasn't already been. Note that
2076 * neither this routine nor the buffer list submission functions perform
2077 * any internal synchronization. It is expected that the lists are thread-local
2078 * to the callers.
2079 *
2080 * Returns true if we queued up the buffer, or false if it already had
2081 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002083bool
Nathan Scottce8e9222006-01-11 15:39:08 +11002084xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002085 struct xfs_buf *bp,
2086 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002088 ASSERT(xfs_buf_islocked(bp));
2089 ASSERT(!(bp->b_flags & XBF_READ));
2090
2091 /*
2092 * If the buffer is already marked delwri it already is queued up
2093 * by someone else for imediate writeout. Just ignore it in that
2094 * case.
2095 */
2096 if (bp->b_flags & _XBF_DELWRI_Q) {
2097 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
2098 return false;
2099 }
David Chinnera6867a62006-01-11 15:37:58 +11002100
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002101 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
2102
Dave Chinnerd808f612010-02-02 10:13:42 +11002103 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002104 * If a buffer gets written out synchronously or marked stale while it
2105 * is on a delwri list we lazily remove it. To do this, the other party
2106 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
2107 * It remains referenced and on the list. In a rare corner case it
2108 * might get readded to a delwri list after the synchronous writeout, in
2109 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11002110 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002111 bp->b_flags |= _XBF_DELWRI_Q;
2112 if (list_empty(&bp->b_list)) {
2113 atomic_inc(&bp->b_hold);
2114 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11002115 }
David Chinner585e6d82007-02-10 18:32:29 +11002116
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002117 return true;
David Chinner585e6d82007-02-10 18:32:29 +11002118}
2119
Dave Chinner089716a2010-01-26 15:13:25 +11002120/*
2121 * Compare function is more complex than it needs to be because
2122 * the return value is only 32 bits and we are doing comparisons
2123 * on 64 bit values
2124 */
2125static int
2126xfs_buf_cmp(
2127 void *priv,
2128 struct list_head *a,
2129 struct list_head *b)
2130{
2131 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
2132 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
2133 xfs_daddr_t diff;
2134
Mark Tinguelyf4b42422012-12-04 17:18:02 -06002135 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11002136 if (diff < 0)
2137 return -1;
2138 if (diff > 0)
2139 return 1;
2140 return 0;
2141}
2142
Dave Chinner26f1fe82016-06-01 17:38:15 +10002143/*
Brian Fostere339dd82018-07-11 22:26:34 -07002144 * Submit buffers for write. If wait_list is specified, the buffers are
2145 * submitted using sync I/O and placed on the wait list such that the caller can
2146 * iowait each buffer. Otherwise async I/O is used and the buffers are released
2147 * at I/O completion time. In either case, buffers remain locked until I/O
2148 * completes and the buffer is released from the queue.
Dave Chinner26f1fe82016-06-01 17:38:15 +10002149 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002150static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10002151xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002152 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10002153 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002155 struct xfs_buf *bp, *n;
2156 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002157 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Dave Chinner26f1fe82016-06-01 17:38:15 +10002159 list_sort(NULL, buffer_list, xfs_buf_cmp);
2160
2161 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002162 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10002163 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002164 if (xfs_buf_ispinned(bp)) {
2165 pinned++;
2166 continue;
2167 }
2168 if (!xfs_buf_trylock(bp))
2169 continue;
2170 } else {
2171 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002174 /*
2175 * Someone else might have written the buffer synchronously or
2176 * marked it stale in the meantime. In that case only the
2177 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
2178 * reference and remove it from the list here.
2179 */
2180 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
2181 list_del_init(&bp->b_list);
2182 xfs_buf_relse(bp);
2183 continue;
2184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002186 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002187
Dave Chinnercf53e992014-10-02 09:04:01 +10002188 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002189 * If we have a wait list, each buffer (and associated delwri
2190 * queue reference) transfers to it and is submitted
2191 * synchronously. Otherwise, drop the buffer from the delwri
2192 * queue and submit async.
Dave Chinnercf53e992014-10-02 09:04:01 +10002193 */
Brian Fosterb6983e82020-05-06 13:25:20 -07002194 bp->b_flags &= ~_XBF_DELWRI_Q;
Brian Fostere339dd82018-07-11 22:26:34 -07002195 bp->b_flags |= XBF_WRITE;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002196 if (wait_list) {
Brian Fostere339dd82018-07-11 22:26:34 -07002197 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002198 list_move_tail(&bp->b_list, wait_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002199 } else {
2200 bp->b_flags |= XBF_ASYNC;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002201 list_del_init(&bp->b_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002202 }
Brian Foster6af88cd2018-07-11 22:26:35 -07002203 __xfs_buf_submit(bp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00002205 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002207 return pinned;
2208}
Nathan Scottf07c2252006-09-28 10:52:15 +10002209
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002210/*
2211 * Write out a buffer list asynchronously.
2212 *
2213 * This will take the @buffer_list, write all non-locked and non-pinned buffers
2214 * out and not wait for I/O completion on any of the buffers. This interface
2215 * is only safely useable for callers that can track I/O completion by higher
2216 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
2217 * function.
Brian Fosterefc32892018-10-18 17:21:49 +11002218 *
2219 * Note: this function will skip buffers it would block on, and in doing so
2220 * leaves them on @buffer_list so they can be retried on a later pass. As such,
2221 * it is up to the caller to ensure that the buffer list is fully submitted or
2222 * cancelled appropriately when they are finished with the list. Failure to
2223 * cancel or resubmit the list until it is empty will result in leaked buffers
2224 * at unmount time.
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002225 */
2226int
2227xfs_buf_delwri_submit_nowait(
2228 struct list_head *buffer_list)
2229{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002230 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002231}
2232
2233/*
2234 * Write out a buffer list synchronously.
2235 *
2236 * This will take the @buffer_list, write all buffers out and wait for I/O
2237 * completion on all of the buffers. @buffer_list is consumed by the function,
2238 * so callers must have some other way of tracking buffers if they require such
2239 * functionality.
2240 */
2241int
2242xfs_buf_delwri_submit(
2243 struct list_head *buffer_list)
2244{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002245 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002246 int error = 0, error2;
2247 struct xfs_buf *bp;
2248
Dave Chinner26f1fe82016-06-01 17:38:15 +10002249 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002250
2251 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10002252 while (!list_empty(&wait_list)) {
2253 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002254
2255 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10002256
Brian Fostere339dd82018-07-11 22:26:34 -07002257 /*
2258 * Wait on the locked buffer, check for errors and unlock and
2259 * release the delwri queue reference.
2260 */
2261 error2 = xfs_buf_iowait(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002262 xfs_buf_relse(bp);
2263 if (!error)
2264 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
2266
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002267 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
Brian Foster7912e7f2017-06-14 21:21:45 -07002270/*
2271 * Push a single buffer on a delwri queue.
2272 *
2273 * The purpose of this function is to submit a single buffer of a delwri queue
2274 * and return with the buffer still on the original queue. The waiting delwri
2275 * buffer submission infrastructure guarantees transfer of the delwri queue
2276 * buffer reference to a temporary wait list. We reuse this infrastructure to
2277 * transfer the buffer back to the original queue.
2278 *
2279 * Note the buffer transitions from the queued state, to the submitted and wait
2280 * listed state and back to the queued state during this call. The buffer
2281 * locking and queue management logic between _delwri_pushbuf() and
2282 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2283 * before returning.
2284 */
2285int
2286xfs_buf_delwri_pushbuf(
2287 struct xfs_buf *bp,
2288 struct list_head *buffer_list)
2289{
2290 LIST_HEAD (submit_list);
2291 int error;
2292
2293 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2294
2295 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2296
2297 /*
2298 * Isolate the buffer to a new local list so we can submit it for I/O
2299 * independently from the rest of the original list.
2300 */
2301 xfs_buf_lock(bp);
2302 list_move(&bp->b_list, &submit_list);
2303 xfs_buf_unlock(bp);
2304
2305 /*
2306 * Delwri submission clears the DELWRI_Q buffer flag and returns with
Brian Fostere339dd82018-07-11 22:26:34 -07002307 * the buffer on the wait list with the original reference. Rather than
Brian Foster7912e7f2017-06-14 21:21:45 -07002308 * bounce the buffer from a local wait list back to the original list
2309 * after I/O completion, reuse the original list as the wait list.
2310 */
2311 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2312
2313 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002314 * The buffer is now locked, under I/O and wait listed on the original
2315 * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2316 * return with the buffer unlocked and on the original queue.
Brian Foster7912e7f2017-06-14 21:21:45 -07002317 */
Brian Fostere339dd82018-07-11 22:26:34 -07002318 error = xfs_buf_iowait(bp);
Brian Foster7912e7f2017-06-14 21:21:45 -07002319 bp->b_flags |= _XBF_DELWRI_Q;
2320 xfs_buf_unlock(bp);
2321
2322 return error;
2323}
2324
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002325int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002326xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
Dave Chinner12eba652020-03-24 20:10:28 -07002328 xfs_buf_zone = kmem_cache_create("xfs_buf", sizeof(struct xfs_buf), 0,
2329 SLAB_HWCACHE_ALIGN |
2330 SLAB_RECLAIM_ACCOUNT |
2331 SLAB_MEM_SPREAD,
2332 NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002333 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002334 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002335
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002338 out:
Nathan Scott87582802006-03-14 13:18:19 +11002339 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342void
Nathan Scottce8e9222006-01-11 15:39:08 +11002343xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08002345 kmem_cache_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
Brian Foster7561d272017-10-17 14:16:29 -07002347
2348void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
2349{
Brian Foster7561d272017-10-17 14:16:29 -07002350 /*
2351 * Set the lru reference count to 0 based on the error injection tag.
2352 * This allows userspace to disrupt buffer caching for debug/testing
2353 * purposes.
2354 */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002355 if (XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_LRU_REF))
Brian Foster7561d272017-10-17 14:16:29 -07002356 lru_ref = 0;
2357
2358 atomic_set(&bp->b_lru_ref, lru_ref);
2359}
Brian Foster8473fee2019-02-07 10:45:46 -08002360
2361/*
2362 * Verify an on-disk magic value against the magic value specified in the
2363 * verifier structure. The verifier magic is in disk byte order so the caller is
2364 * expected to pass the value directly from disk.
2365 */
2366bool
2367xfs_verify_magic(
2368 struct xfs_buf *bp,
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002369 __be32 dmagic)
Brian Foster8473fee2019-02-07 10:45:46 -08002370{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002371 struct xfs_mount *mp = bp->b_mount;
Brian Foster8473fee2019-02-07 10:45:46 -08002372 int idx;
2373
2374 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002375 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic[idx]))
Brian Foster8473fee2019-02-07 10:45:46 -08002376 return false;
2377 return dmagic == bp->b_ops->magic[idx];
2378}
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002379/*
2380 * Verify an on-disk magic value against the magic value specified in the
2381 * verifier structure. The verifier magic is in disk byte order so the caller is
2382 * expected to pass the value directly from disk.
2383 */
2384bool
2385xfs_verify_magic16(
2386 struct xfs_buf *bp,
2387 __be16 dmagic)
2388{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002389 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002390 int idx;
2391
2392 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002393 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic16[idx]))
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002394 return false;
2395 return dmagic == bp->b_ops->magic16[idx];
2396}