blob: 7f8abcbe98a44754d8178395029b69a3bd8f6743 [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 *
46 * xfs_buftarg_wait_rele
47 * 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
James Bottomley73c77e22010-01-25 11:42:24 -060055static inline int
56xfs_buf_is_vmapped(
57 struct xfs_buf *bp)
58{
59 /*
60 * Return true if the buffer is vmapped.
61 *
Dave Chinner611c9942012-04-23 15:59:07 +100062 * b_addr is null if the buffer is not mapped, but the code is clever
63 * enough to know it doesn't have to map a single page, so the check has
64 * to be both for b_addr and bp->b_page_count > 1.
James Bottomley73c77e22010-01-25 11:42:24 -060065 */
Dave Chinner611c9942012-04-23 15:59:07 +100066 return bp->b_addr && bp->b_page_count > 1;
James Bottomley73c77e22010-01-25 11:42:24 -060067}
68
69static inline int
70xfs_buf_vmap_len(
71 struct xfs_buf *bp)
72{
73 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
Brian Foster9c7504a2016-07-20 11:15:28 +100077 * Bump the I/O in flight count on the buftarg if we haven't yet done so for
78 * this buffer. The count is incremented once per buffer (per hold cycle)
79 * because the corresponding decrement is deferred to buffer release. Buffers
80 * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
81 * tracking adds unnecessary overhead. This is used for sychronization purposes
82 * with unmount (see xfs_wait_buftarg()), so all we really need is a count of
83 * in-flight buffers.
84 *
85 * Buffers that are never released (e.g., superblock, iclog buffers) must set
86 * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
87 * never reaches zero and unmount hangs indefinitely.
88 */
89static inline void
90xfs_buf_ioacct_inc(
91 struct xfs_buf *bp)
92{
Brian Foster63db7c82017-05-31 08:22:52 -070093 if (bp->b_flags & XBF_NO_IOACCT)
Brian Foster9c7504a2016-07-20 11:15:28 +100094 return;
95
96 ASSERT(bp->b_flags & XBF_ASYNC);
Brian Foster63db7c82017-05-31 08:22:52 -070097 spin_lock(&bp->b_lock);
98 if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) {
99 bp->b_state |= XFS_BSTATE_IN_FLIGHT;
100 percpu_counter_inc(&bp->b_target->bt_io_count);
101 }
102 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000103}
104
105/*
106 * Clear the in-flight state on a buffer about to be released to the LRU or
107 * freed and unaccount from the buftarg.
108 */
109static inline void
Brian Foster63db7c82017-05-31 08:22:52 -0700110__xfs_buf_ioacct_dec(
111 struct xfs_buf *bp)
112{
Brian Foster95989c42017-06-08 08:23:07 -0700113 lockdep_assert_held(&bp->b_lock);
Brian Foster63db7c82017-05-31 08:22:52 -0700114
115 if (bp->b_state & XFS_BSTATE_IN_FLIGHT) {
116 bp->b_state &= ~XFS_BSTATE_IN_FLIGHT;
117 percpu_counter_dec(&bp->b_target->bt_io_count);
118 }
119}
120
121static inline void
Brian Foster9c7504a2016-07-20 11:15:28 +1000122xfs_buf_ioacct_dec(
123 struct xfs_buf *bp)
124{
Brian Foster63db7c82017-05-31 08:22:52 -0700125 spin_lock(&bp->b_lock);
126 __xfs_buf_ioacct_dec(bp);
127 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000128}
129
130/*
Dave Chinner430cbeb2010-12-02 16:30:55 +1100131 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
132 * b_lru_ref count so that the buffer is freed immediately when the buffer
133 * reference count falls to zero. If the buffer is already on the LRU, we need
134 * to remove the reference that LRU holds on the buffer.
135 *
136 * This prevents build-up of stale buffers on the LRU.
137 */
138void
139xfs_buf_stale(
140 struct xfs_buf *bp)
141{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000142 ASSERT(xfs_buf_islocked(bp));
143
Dave Chinner430cbeb2010-12-02 16:30:55 +1100144 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000145
146 /*
147 * Clear the delwri status so that a delwri queue walker will not
148 * flush this buffer to disk now that it is stale. The delwri queue has
149 * a reference to the buffer, so this is safe to do.
150 */
151 bp->b_flags &= ~_XBF_DELWRI_Q;
152
Brian Foster9c7504a2016-07-20 11:15:28 +1000153 /*
154 * Once the buffer is marked stale and unlocked, a subsequent lookup
155 * could reset b_flags. There is no guarantee that the buffer is
156 * unaccounted (released to LRU) before that occurs. Drop in-flight
157 * status now to preserve accounting consistency.
158 */
Dave Chinnera4082352013-08-28 10:18:06 +1000159 spin_lock(&bp->b_lock);
Brian Foster63db7c82017-05-31 08:22:52 -0700160 __xfs_buf_ioacct_dec(bp);
161
Dave Chinnera4082352013-08-28 10:18:06 +1000162 atomic_set(&bp->b_lru_ref, 0);
163 if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
Dave Chinnere80dfa12013-08-28 10:18:05 +1000164 (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
165 atomic_dec(&bp->b_hold);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100166
Dave Chinner430cbeb2010-12-02 16:30:55 +1100167 ASSERT(atomic_read(&bp->b_hold) >= 1);
Dave Chinnera4082352013-08-28 10:18:06 +1000168 spin_unlock(&bp->b_lock);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Dave Chinner3e85c862012-06-22 18:50:09 +1000171static int
172xfs_buf_get_maps(
173 struct xfs_buf *bp,
174 int map_count)
175{
176 ASSERT(bp->b_maps == NULL);
177 bp->b_map_count = map_count;
178
179 if (map_count == 1) {
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600180 bp->b_maps = &bp->__b_map;
Dave Chinner3e85c862012-06-22 18:50:09 +1000181 return 0;
182 }
183
184 bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
185 KM_NOFS);
186 if (!bp->b_maps)
Dave Chinner24513372014-06-25 14:58:08 +1000187 return -ENOMEM;
Dave Chinner3e85c862012-06-22 18:50:09 +1000188 return 0;
189}
190
191/*
192 * Frees b_pages if it was allocated.
193 */
194static void
195xfs_buf_free_maps(
196 struct xfs_buf *bp)
197{
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600198 if (bp->b_maps != &bp->__b_map) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000199 kmem_free(bp->b_maps);
200 bp->b_maps = NULL;
201 }
202}
203
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800204static int
Dave Chinner3e85c862012-06-22 18:50:09 +1000205_xfs_buf_alloc(
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000206 struct xfs_buftarg *target,
Dave Chinner3e85c862012-06-22 18:50:09 +1000207 struct xfs_buf_map *map,
208 int nmaps,
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800209 xfs_buf_flags_t flags,
210 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000212 struct xfs_buf *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000213 int error;
214 int i;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000215
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800216 *bpp = NULL;
Carlos Maiolino32a2b112020-07-22 09:23:10 -0700217 bp = kmem_cache_zalloc(xfs_buf_zone, GFP_NOFS | __GFP_NOFAIL);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /*
Dave Chinner12bcb3f2012-04-23 15:59:05 +1000220 * We don't want certain flags to appear in b_flags unless they are
221 * specifically set by later operations on the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
Dave Chinner611c9942012-04-23 15:59:07 +1000223 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Nathan Scottce8e9222006-01-11 15:39:08 +1100225 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100226 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000227 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100228 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100229 INIT_LIST_HEAD(&bp->b_list);
Carlos Maiolino643c8c02018-01-24 13:38:49 -0800230 INIT_LIST_HEAD(&bp->b_li_list);
Thomas Gleixnera731cd112010-09-07 14:33:15 +0000231 sema_init(&bp->b_sema, 0); /* held, no waiters */
Dave Chinnera4082352013-08-28 10:18:06 +1000232 spin_lock_init(&bp->b_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100233 bp->b_target = target;
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700234 bp->b_mount = target->bt_mount;
Dave Chinner3e85c862012-06-22 18:50:09 +1000235 bp->b_flags = flags;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000238 * Set length and io_length to the same value initially.
239 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * most cases but may be reset (e.g. XFS recovery).
241 */
Dave Chinner3e85c862012-06-22 18:50:09 +1000242 error = xfs_buf_get_maps(bp, nmaps);
243 if (error) {
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800244 kmem_cache_free(xfs_buf_zone, bp);
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800245 return error;
Dave Chinner3e85c862012-06-22 18:50:09 +1000246 }
247
248 bp->b_bn = map[0].bm_bn;
249 bp->b_length = 0;
250 for (i = 0; i < nmaps; i++) {
251 bp->b_maps[i].bm_bn = map[i].bm_bn;
252 bp->b_maps[i].bm_len = map[i].bm_len;
253 bp->b_length += map[i].bm_len;
254 }
Dave Chinner3e85c862012-06-22 18:50:09 +1000255
Nathan Scottce8e9222006-01-11 15:39:08 +1100256 atomic_set(&bp->b_pin_count, 0);
257 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700259 XFS_STATS_INC(bp->b_mount, xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000260 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000261
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800262 *bpp = bp;
263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100267 * Allocate a page array capable of holding a specified number
268 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
270STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100271_xfs_buf_get_pages(
272 xfs_buf_t *bp,
Eric Sandeen87937bf2014-04-14 19:01:20 +1000273 int page_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100276 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100277 bp->b_page_count = page_count;
278 if (page_count <= XB_PAGES) {
279 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100281 bp->b_pages = kmem_alloc(sizeof(struct page *) *
Dave Chinneraa5c1582012-04-23 15:58:56 +1000282 page_count, KM_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +1100283 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return -ENOMEM;
285 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100286 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 return 0;
289}
290
291/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100292 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
294STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100295_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 xfs_buf_t *bp)
297{
Nathan Scottce8e9222006-01-11 15:39:08 +1100298 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000299 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000300 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302}
303
304/*
305 * Releases the specified buffer.
306 *
307 * The modification state of any associated pages is left unchanged.
Zhi Yong Wub46fe822013-08-07 10:10:59 +0000308 * The buffer must not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * hashed and refcounted buffers
310 */
Christoph Hellwig25a40952019-10-24 22:25:37 -0700311static void
Nathan Scottce8e9222006-01-11 15:39:08 +1100312xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 xfs_buf_t *bp)
314{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000315 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Dave Chinner430cbeb2010-12-02 16:30:55 +1100317 ASSERT(list_empty(&bp->b_lru));
318
Dave Chinner0e6e8472011-03-26 09:16:45 +1100319 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 uint i;
321
James Bottomley73c77e22010-01-25 11:42:24 -0600322 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000323 vm_unmap_ram(bp->b_addr - bp->b_offset,
324 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Nathan Scott948ecdb2006-09-28 11:03:13 +1000326 for (i = 0; i < bp->b_page_count; i++) {
327 struct page *page = bp->b_pages[i];
328
Dave Chinner0e6e8472011-03-26 09:16:45 +1100329 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000330 }
Dave Chinner12eba652020-03-24 20:10:28 -0700331 if (current->reclaim_state)
332 current->reclaim_state->reclaimed_slab +=
333 bp->b_page_count;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100334 } else if (bp->b_flags & _XBF_KMEM)
335 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000336 _xfs_buf_free_pages(bp);
Dave Chinner3e85c862012-06-22 18:50:09 +1000337 xfs_buf_free_maps(bp);
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800338 kmem_cache_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100342 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
344STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100345xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 xfs_buf_t *bp,
347 uint flags)
348{
Dave Chinneraa0e8832012-04-23 15:58:52 +1000349 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100351 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 unsigned short page_count, i;
Dave Chinner795cac72012-04-23 15:58:53 +1000353 xfs_off_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int error;
Bill O'Donnell3219e8c2019-10-04 16:38:44 -0700355 xfs_km_flags_t kmflag_mask = 0;
356
357 /*
358 * assure zeroed buffer for non-read cases.
359 */
360 if (!(flags & XBF_READ)) {
361 kmflag_mask |= KM_ZERO;
362 gfp_mask |= __GFP_ZERO;
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Dave Chinner0e6e8472011-03-26 09:16:45 +1100365 /*
366 * for buffers that are contained within a single page, just allocate
367 * the memory from the heap - there's no need for the complexity of
368 * page arrays to keep allocation down to order 0.
369 */
Dave Chinner795cac72012-04-23 15:58:53 +1000370 size = BBTOB(bp->b_length);
371 if (size < PAGE_SIZE) {
Dave Chinnerf8f9ee42019-08-26 12:08:39 -0700372 int align_mask = xfs_buftarg_dma_alignment(bp->b_target);
Bill O'Donnell3219e8c2019-10-04 16:38:44 -0700373 bp->b_addr = kmem_alloc_io(size, align_mask,
374 KM_NOFS | kmflag_mask);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100375 if (!bp->b_addr) {
376 /* low memory - use alloc_page loop instead */
377 goto use_alloc_page;
378 }
379
Dave Chinner795cac72012-04-23 15:58:53 +1000380 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100381 ((unsigned long)bp->b_addr & PAGE_MASK)) {
382 /* b_addr spans two pages - use alloc_page instead */
383 kmem_free(bp->b_addr);
384 bp->b_addr = NULL;
385 goto use_alloc_page;
386 }
387 bp->b_offset = offset_in_page(bp->b_addr);
388 bp->b_pages = bp->b_page_array;
Dave Chinnerf8f9ee42019-08-26 12:08:39 -0700389 bp->b_pages[0] = kmem_to_page(bp->b_addr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100390 bp->b_page_count = 1;
Dave Chinner611c9942012-04-23 15:59:07 +1000391 bp->b_flags |= _XBF_KMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100392 return 0;
393 }
394
395use_alloc_page:
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600396 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
397 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000398 >> PAGE_SHIFT;
Dave Chinner795cac72012-04-23 15:58:53 +1000399 page_count = end - start;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000400 error = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (unlikely(error))
402 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Nathan Scottce8e9222006-01-11 15:39:08 +1100404 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100405 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Nathan Scottce8e9222006-01-11 15:39:08 +1100407 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct page *page;
409 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100410retry:
411 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100413 if (flags & XBF_READ_AHEAD) {
414 bp->b_page_count = i;
Dave Chinner24513372014-06-25 14:58:08 +1000415 error = -ENOMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100416 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
419 /*
420 * This could deadlock.
421 *
422 * But until all the XFS lowlevel code is revamped to
423 * handle buffer allocation failures we can't do much.
424 */
425 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100426 xfs_err(NULL,
Tetsuo Handa5bf97b12015-10-12 15:41:29 +1100427 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
428 current->comm, current->pid,
Harvey Harrison34a622b2008-04-10 12:19:21 +1000429 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700431 XFS_STATS_INC(bp->b_mount, xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200432 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 goto retry;
434 }
435
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700436 XFS_STATS_INC(bp->b_mount, xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Dave Chinner0e6e8472011-03-26 09:16:45 +1100438 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100440 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 offset = 0;
442 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dave Chinner0e6e8472011-03-26 09:16:45 +1100445out_free_pages:
446 for (i = 0; i < bp->b_page_count; i++)
447 __free_page(bp->b_pages[i]);
Darrick J. Wong2aa6ba7b2017-01-25 20:24:57 -0800448 bp->b_flags &= ~_XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return error;
450}
451
452/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300453 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 */
455STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100456_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 xfs_buf_t *bp,
458 uint flags)
459{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100460 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100461 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100462 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100463 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
Dave Chinner611c9942012-04-23 15:59:07 +1000464 } else if (flags & XBF_UNMAPPED) {
465 bp->b_addr = NULL;
466 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100467 int retried = 0;
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700468 unsigned nofs_flag;
Dave Chinnera19fb382011-03-26 09:13:42 +1100469
Dave Chinnerae687e52014-03-07 16:19:14 +1100470 /*
Joe Perchescf085a12019-11-07 13:24:52 -0800471 * vm_map_ram() will allocate auxiliary structures (e.g.
Dave Chinnerae687e52014-03-07 16:19:14 +1100472 * pagetables) with GFP_KERNEL, yet we are likely to be under
473 * GFP_NOFS context here. Hence we need to tell memory reclaim
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700474 * that we are in such a context via PF_MEMALLOC_NOFS to prevent
Dave Chinnerae687e52014-03-07 16:19:14 +1100475 * memory reclaim re-entering the filesystem here and
476 * potentially deadlocking.
477 */
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700478 nofs_flag = memalloc_nofs_save();
Dave Chinnera19fb382011-03-26 09:13:42 +1100479 do {
480 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
Christoph Hellwigd4efd792020-06-01 21:51:27 -0700481 -1);
Dave Chinnera19fb382011-03-26 09:13:42 +1100482 if (bp->b_addr)
483 break;
484 vm_unmap_aliases();
485 } while (retried++ <= 1);
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700486 memalloc_nofs_restore(nofs_flag);
Dave Chinnera19fb382011-03-26 09:13:42 +1100487
488 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100490 bp->b_addr += bp->b_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
493 return 0;
494}
495
496/*
497 * Finding and Reading Buffers
498 */
Lucas Stach6031e732016-12-07 17:36:36 +1100499static int
500_xfs_buf_obj_cmp(
501 struct rhashtable_compare_arg *arg,
502 const void *obj)
503{
504 const struct xfs_buf_map *map = arg->key;
505 const struct xfs_buf *bp = obj;
506
507 /*
508 * The key hashing in the lookup path depends on the key being the
509 * first element of the compare_arg, make sure to assert this.
510 */
511 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
512
513 if (bp->b_bn != map->bm_bn)
514 return 1;
515
516 if (unlikely(bp->b_length != map->bm_len)) {
517 /*
518 * found a block number match. If the range doesn't
519 * match, the only way this is allowed is if the buffer
520 * in the cache is stale and the transaction that made
521 * it stale has not yet committed. i.e. we are
522 * reallocating a busy extent. Skip this buffer and
523 * continue searching for an exact match.
524 */
525 ASSERT(bp->b_flags & XBF_STALE);
526 return 1;
527 }
528 return 0;
529}
530
531static const struct rhashtable_params xfs_buf_hash_params = {
532 .min_size = 32, /* empty AGs have minimal footprint */
533 .nelem_hint = 16,
534 .key_len = sizeof(xfs_daddr_t),
535 .key_offset = offsetof(struct xfs_buf, b_bn),
536 .head_offset = offsetof(struct xfs_buf, b_rhash_head),
537 .automatic_shrinking = true,
538 .obj_cmpfn = _xfs_buf_obj_cmp,
539};
540
541int
542xfs_buf_hash_init(
543 struct xfs_perag *pag)
544{
545 spin_lock_init(&pag->pag_buf_lock);
546 return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
547}
548
549void
550xfs_buf_hash_destroy(
551 struct xfs_perag *pag)
552{
553 rhashtable_destroy(&pag->pag_buf_hash);
554}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556/*
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700557 * Look up a buffer in the buffer cache and return it referenced and locked
558 * in @found_bp.
559 *
560 * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the
561 * cache.
562 *
563 * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return
564 * -EAGAIN if we fail to lock it.
565 *
566 * Return values are:
567 * -EFSCORRUPTED if have been supplied with an invalid address
568 * -EAGAIN on trylock failure
569 * -ENOENT if we fail to find a match and @new_bp was NULL
570 * 0, with @found_bp:
571 * - @new_bp if we inserted it into the cache
572 * - the buffer we found and locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700574static int
575xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000576 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000577 struct xfs_buf_map *map,
578 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100579 xfs_buf_flags_t flags,
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700580 struct xfs_buf *new_bp,
581 struct xfs_buf **found_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Dave Chinner74f75a02010-09-24 19:59:04 +1000583 struct xfs_perag *pag;
Dave Chinner74f75a02010-09-24 19:59:04 +1000584 xfs_buf_t *bp;
Lucas Stach6031e732016-12-07 17:36:36 +1100585 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
Dave Chinner10616b82013-01-21 23:53:52 +1100586 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000587 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700589 *found_bp = NULL;
590
Dave Chinner3e85c862012-06-22 18:50:09 +1000591 for (i = 0; i < nmaps; i++)
Lucas Stach6031e732016-12-07 17:36:36 +1100592 cmap.bm_len += map[i].bm_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* Check for IOs smaller than the sector size / not sector aligned */
Lucas Stach6031e732016-12-07 17:36:36 +1100595 ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
596 ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Dave Chinner10616b82013-01-21 23:53:52 +1100598 /*
599 * Corrupted block numbers can get through to here, unfortunately, so we
600 * have to check that the buffer falls within the filesystem bounds.
601 */
602 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
Lucas Stach6031e732016-12-07 17:36:36 +1100603 if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
Dave Chinner10616b82013-01-21 23:53:52 +1100604 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -0800605 "%s: daddr 0x%llx out of range, EOFS 0x%llx",
Lucas Stach6031e732016-12-07 17:36:36 +1100606 __func__, cmap.bm_bn, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000607 WARN_ON(1);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700608 return -EFSCORRUPTED;
Dave Chinner10616b82013-01-21 23:53:52 +1100609 }
610
Dave Chinner74f75a02010-09-24 19:59:04 +1000611 pag = xfs_perag_get(btp->bt_mount,
Lucas Stach6031e732016-12-07 17:36:36 +1100612 xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Dave Chinner74f75a02010-09-24 19:59:04 +1000614 spin_lock(&pag->pag_buf_lock);
Lucas Stach6031e732016-12-07 17:36:36 +1100615 bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
616 xfs_buf_hash_params);
617 if (bp) {
618 atomic_inc(&bp->b_hold);
619 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
622 /* No match found */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700623 if (!new_bp) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100624 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000625 spin_unlock(&pag->pag_buf_lock);
626 xfs_perag_put(pag);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700627 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700629
630 /* the buffer keeps the perag reference until it is freed */
631 new_bp->b_pag = pag;
632 rhashtable_insert_fast(&pag->pag_buf_hash, &new_bp->b_rhash_head,
633 xfs_buf_hash_params);
634 spin_unlock(&pag->pag_buf_lock);
635 *found_bp = new_bp;
636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000639 spin_unlock(&pag->pag_buf_lock);
640 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200642 if (!xfs_buf_trylock(bp)) {
643 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100644 xfs_buf_rele(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100645 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700646 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200648 xfs_buf_lock(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100649 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
Dave Chinner0e6e8472011-03-26 09:16:45 +1100652 /*
653 * if the buffer is stale, clear all the external state associated with
654 * it. We need to keep flags such as how we allocated the buffer memory
655 * intact here.
656 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100657 if (bp->b_flags & XBF_STALE) {
658 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner611c9942012-04-23 15:59:07 +1000659 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100660 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000661 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000662
663 trace_xfs_buf_find(bp, flags, _RET_IP_);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100664 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700665 *found_bp = bp;
666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Dave Chinner8925a3d2018-04-18 08:25:20 -0700669struct xfs_buf *
670xfs_buf_incore(
671 struct xfs_buftarg *target,
672 xfs_daddr_t blkno,
673 size_t numblks,
674 xfs_buf_flags_t flags)
675{
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700676 struct xfs_buf *bp;
677 int error;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700678 DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700679
680 error = xfs_buf_find(target, &map, 1, flags, NULL, &bp);
681 if (error)
682 return NULL;
683 return bp;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700684}
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/*
Dave Chinner38158322011-09-30 04:45:02 +0000687 * Assembles a buffer covering the specified range. The code is optimised for
688 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
689 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 */
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800691int
Dave Chinner6dde2702012-06-22 18:50:10 +1000692xfs_buf_get_map(
693 struct xfs_buftarg *target,
694 struct xfs_buf_map *map,
695 int nmaps,
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800696 xfs_buf_flags_t flags,
697 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Dave Chinner38158322011-09-30 04:45:02 +0000699 struct xfs_buf *bp;
700 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100701 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800703 *bpp = NULL;
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700704 error = xfs_buf_find(target, map, nmaps, flags, NULL, &bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800705 if (!error)
Dave Chinner38158322011-09-30 04:45:02 +0000706 goto found;
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800707 if (error != -ENOENT)
708 return error;
Dave Chinner38158322011-09-30 04:45:02 +0000709
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800710 error = _xfs_buf_alloc(target, map, nmaps, flags, &new_bp);
711 if (error)
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800712 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000714 error = xfs_buf_allocate_memory(new_bp, flags);
715 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000716 xfs_buf_free(new_bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800717 return error;
Dave Chinner38158322011-09-30 04:45:02 +0000718 }
719
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700720 error = xfs_buf_find(target, map, nmaps, flags, new_bp, &bp);
721 if (error) {
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000722 xfs_buf_free(new_bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800723 return error;
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000724 }
725
726 if (bp != new_bp)
727 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Dave Chinner38158322011-09-30 04:45:02 +0000729found:
Dave Chinner611c9942012-04-23 15:59:07 +1000730 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100731 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (unlikely(error)) {
Darrick J. Wong93baa552020-02-21 07:40:44 -0800733 xfs_warn_ratelimited(target->bt_mount,
734 "%s: failed to map %u pages", __func__,
735 bp->b_page_count);
Dave Chinnera8acad72012-04-23 15:58:54 +1000736 xfs_buf_relse(bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800737 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739 }
740
Dave Chinnerb79f4a12016-01-12 07:03:44 +1100741 /*
742 * Clear b_error if this is a lookup from a caller that doesn't expect
743 * valid data to be found in the buffer.
744 */
745 if (!(flags & XBF_READ))
746 xfs_buf_ioerror(bp, 0);
747
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100748 XFS_STATS_INC(target->bt_mount, xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000749 trace_xfs_buf_get(bp, flags, _RET_IP_);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800750 *bpp = bp;
751 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100754STATIC int
755_xfs_buf_read(
756 xfs_buf_t *bp,
757 xfs_buf_flags_t flags)
758{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000759 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600760 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100761
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000762 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200763 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100764
Brian Foster6af88cd2018-07-11 22:26:35 -0700765 return xfs_buf_submit(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100766}
767
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100768/*
Brian Foster75d02302019-02-06 09:25:29 -0800769 * Reverify a buffer found in cache without an attached ->b_ops.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800770 *
Brian Foster75d02302019-02-06 09:25:29 -0800771 * If the caller passed an ops structure and the buffer doesn't have ops
772 * assigned, set the ops and use it to verify the contents. If verification
773 * fails, clear XBF_DONE. We assume the buffer has no recorded errors and is
774 * already in XBF_DONE state on entry.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800775 *
Brian Foster75d02302019-02-06 09:25:29 -0800776 * Under normal operations, every in-core buffer is verified on read I/O
777 * completion. There are two scenarios that can lead to in-core buffers without
778 * an assigned ->b_ops. The first is during log recovery of buffers on a V4
779 * filesystem, though these buffers are purged at the end of recovery. The
780 * other is online repair, which intentionally reads with a NULL buffer ops to
781 * run several verifiers across an in-core buffer in order to establish buffer
782 * type. If repair can't establish that, the buffer will be left in memory
783 * with NULL buffer ops.
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100784 */
785int
Brian Foster75d02302019-02-06 09:25:29 -0800786xfs_buf_reverify(
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100787 struct xfs_buf *bp,
788 const struct xfs_buf_ops *ops)
789{
790 ASSERT(bp->b_flags & XBF_DONE);
791 ASSERT(bp->b_error == 0);
792
793 if (!ops || bp->b_ops)
794 return 0;
795
796 bp->b_ops = ops;
797 bp->b_ops->verify_read(bp);
798 if (bp->b_error)
799 bp->b_flags &= ~XBF_DONE;
800 return bp->b_error;
801}
802
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800803int
Dave Chinner6dde2702012-06-22 18:50:10 +1000804xfs_buf_read_map(
805 struct xfs_buftarg *target,
806 struct xfs_buf_map *map,
807 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100808 xfs_buf_flags_t flags,
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800809 struct xfs_buf **bpp,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800810 const struct xfs_buf_ops *ops,
811 xfs_failaddr_t fa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Dave Chinner6dde2702012-06-22 18:50:10 +1000813 struct xfs_buf *bp;
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800814 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Nathan Scottce8e9222006-01-11 15:39:08 +1100816 flags |= XBF_READ;
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800817 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800819 error = xfs_buf_get_map(target, map, nmaps, flags, &bp);
820 if (error)
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800821 return error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000822
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100823 trace_xfs_buf_read(bp, flags, _RET_IP_);
824
825 if (!(bp->b_flags & XBF_DONE)) {
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800826 /* Initiate the buffer read and wait. */
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100827 XFS_STATS_INC(target->bt_mount, xb_get_read);
828 bp->b_ops = ops;
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800829 error = _xfs_buf_read(bp, flags);
830
831 /* Readahead iodone already dropped the buffer, so exit. */
832 if (flags & XBF_ASYNC)
833 return 0;
834 } else {
835 /* Buffer already read; all we need to do is check it. */
836 error = xfs_buf_reverify(bp, ops);
837
838 /* Readahead already finished; drop the buffer and exit. */
839 if (flags & XBF_ASYNC) {
840 xfs_buf_relse(bp);
841 return 0;
842 }
843
844 /* We do not want read in the flags */
845 bp->b_flags &= ~XBF_READ;
846 ASSERT(bp->b_ops != NULL || ops == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800849 /*
850 * If we've had a read error, then the contents of the buffer are
851 * invalid and should not be used. To ensure that a followup read tries
852 * to pull the buffer from disk again, we clear the XBF_DONE flag and
853 * mark the buffer stale. This ensures that anyone who has a current
854 * reference to the buffer will interpret it's contents correctly and
855 * future cache lookups will also treat it as an empty, uninitialised
856 * buffer.
857 */
858 if (error) {
859 if (!XFS_FORCED_SHUTDOWN(target->bt_mount))
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800860 xfs_buf_ioerror_alert(bp, fa);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100861
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800862 bp->b_flags &= ~XBF_DONE;
863 xfs_buf_stale(bp);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100864 xfs_buf_relse(bp);
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800865
866 /* bad CRC means corrupted metadata */
867 if (error == -EFSBADCRC)
868 error = -EFSCORRUPTED;
869 return error;
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100870 }
871
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800872 *bpp = bp;
873 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100877 * If we are not low on memory then do the readahead in a deadlock
878 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 */
880void
Dave Chinner6dde2702012-06-22 18:50:10 +1000881xfs_buf_readahead_map(
882 struct xfs_buftarg *target,
883 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100884 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100885 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800887 struct xfs_buf *bp;
888
Jan Karaefa7c9f2017-02-02 15:56:53 +0100889 if (bdi_read_congested(target->bt_bdev->bd_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return;
891
Dave Chinner6dde2702012-06-22 18:50:10 +1000892 xfs_buf_read_map(target, map, nmaps,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800893 XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD, &bp, ops,
894 __this_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
Dave Chinner5adc94c2010-09-24 21:58:31 +1000897/*
898 * Read an uncached buffer from disk. Allocates and returns a locked
899 * buffer containing the disk contents or nothing.
900 */
Dave Chinnerba3726742014-10-02 09:05:32 +1000901int
Dave Chinner5adc94c2010-09-24 21:58:31 +1000902xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000903 struct xfs_buftarg *target,
904 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000905 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100906 int flags,
Dave Chinnerba3726742014-10-02 09:05:32 +1000907 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100908 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000909{
Dave Chinnereab4e632012-11-12 22:54:02 +1100910 struct xfs_buf *bp;
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800911 int error;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000912
Dave Chinnerba3726742014-10-02 09:05:32 +1000913 *bpp = NULL;
914
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800915 error = xfs_buf_get_uncached(target, numblks, flags, &bp);
916 if (error)
917 return error;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000918
919 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000920 ASSERT(bp->b_map_count == 1);
Dave Chinnerba3726742014-10-02 09:05:32 +1000921 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
Dave Chinner3e85c862012-06-22 18:50:09 +1000922 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000923 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100924 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000925
Brian Foster6af88cd2018-07-11 22:26:35 -0700926 xfs_buf_submit(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000927 if (bp->b_error) {
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800928 error = bp->b_error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800929 xfs_buf_relse(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000930 return error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800931 }
Dave Chinnerba3726742014-10-02 09:05:32 +1000932
933 *bpp = bp;
934 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800937int
Dave Chinner686865f2010-09-24 20:07:47 +1000938xfs_buf_get_uncached(
939 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000940 size_t numblks,
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800941 int flags,
942 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000944 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000945 int error, i;
Dave Chinner3e85c862012-06-22 18:50:09 +1000946 struct xfs_buf *bp;
947 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800949 *bpp = NULL;
950
Brian Fosterc891c302016-07-20 11:13:43 +1000951 /* flags might contain irrelevant bits, pass only what we care about */
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800952 error = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT, &bp);
953 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Dave Chinnere70b73f2012-04-23 15:58:49 +1000956 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000957 error = _xfs_buf_get_pages(bp, page_count);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000958 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 goto fail_free_buf;
960
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000961 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000962 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800963 if (!bp->b_pages[i]) {
964 error = -ENOMEM;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000965 goto fail_free_mem;
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000968 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Dave Chinner611c9942012-04-23 15:59:07 +1000970 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000971 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100972 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500973 "%s: failed to map pages", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Dave Chinner686865f2010-09-24 20:07:47 +1000977 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800978 *bpp = bp;
979 return 0;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000982 while (--i >= 0)
983 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000984 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 fail_free_buf:
Dave Chinner3e85c862012-06-22 18:50:09 +1000986 xfs_buf_free_maps(bp);
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800987 kmem_cache_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 fail:
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800989 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 * Increment reference count on buffer, to hold the buffer concurrently
994 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 * Must hold the buffer already to call this function.
996 */
997void
Nathan Scottce8e9222006-01-11 15:39:08 +1100998xfs_buf_hold(
999 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001001 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001002 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
1005/*
Brian Foster9c7504a2016-07-20 11:15:28 +10001006 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
1007 * placed on LRU or freed (depending on b_lru_ref).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 */
1009void
Nathan Scottce8e9222006-01-11 15:39:08 +11001010xfs_buf_rele(
1011 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Dave Chinner74f75a02010-09-24 19:59:04 +10001013 struct xfs_perag *pag = bp->b_pag;
Brian Foster9c7504a2016-07-20 11:15:28 +10001014 bool release;
1015 bool freebuf = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001017 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Dave Chinner74f75a02010-09-24 19:59:04 +10001019 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +11001020 ASSERT(list_empty(&bp->b_lru));
Brian Foster9c7504a2016-07-20 11:15:28 +10001021 if (atomic_dec_and_test(&bp->b_hold)) {
1022 xfs_buf_ioacct_dec(bp);
Nathan Scottfad3aa12006-02-01 12:14:52 +11001023 xfs_buf_free(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001024 }
Nathan Scottfad3aa12006-02-01 12:14:52 +11001025 return;
1026 }
1027
Lachlan McIlroy37906892008-08-13 15:42:10 +10001028 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinnera4082352013-08-28 10:18:06 +10001029
Dave Chinner37fd1672018-10-18 17:21:29 +11001030 /*
1031 * We grab the b_lock here first to serialise racing xfs_buf_rele()
1032 * calls. The pag_buf_lock being taken on the last reference only
1033 * serialises against racing lookups in xfs_buf_find(). IOWs, the second
1034 * to last reference we drop here is not serialised against the last
1035 * reference until we take bp->b_lock. Hence if we don't grab b_lock
1036 * first, the last "release" reference can win the race to the lock and
1037 * free the buffer before the second-to-last reference is processed,
1038 * leading to a use-after-free scenario.
1039 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001040 spin_lock(&bp->b_lock);
Dave Chinner37fd1672018-10-18 17:21:29 +11001041 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +10001042 if (!release) {
1043 /*
1044 * Drop the in-flight state if the buffer is already on the LRU
1045 * and it holds the only reference. This is racy because we
1046 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1047 * ensures the decrement occurs only once per-buf.
1048 */
1049 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
Brian Foster63db7c82017-05-31 08:22:52 -07001050 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001051 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
Brian Foster9c7504a2016-07-20 11:15:28 +10001053
1054 /* the last reference has been dropped ... */
Brian Foster63db7c82017-05-31 08:22:52 -07001055 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001056 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1057 /*
1058 * If the buffer is added to the LRU take a new reference to the
1059 * buffer for the LRU and clear the (now stale) dispose list
1060 * state flag
1061 */
1062 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1063 bp->b_state &= ~XFS_BSTATE_DISPOSE;
1064 atomic_inc(&bp->b_hold);
1065 }
1066 spin_unlock(&pag->pag_buf_lock);
1067 } else {
1068 /*
1069 * most of the time buffers will already be removed from the
1070 * LRU, so optimise that case by checking for the
1071 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1072 * was on was the disposal list
1073 */
1074 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1075 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1076 } else {
1077 ASSERT(list_empty(&bp->b_lru));
1078 }
1079
1080 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Lucas Stach6031e732016-12-07 17:36:36 +11001081 rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
1082 xfs_buf_hash_params);
Brian Foster9c7504a2016-07-20 11:15:28 +10001083 spin_unlock(&pag->pag_buf_lock);
1084 xfs_perag_put(pag);
1085 freebuf = true;
1086 }
1087
1088out_unlock:
1089 spin_unlock(&bp->b_lock);
1090
1091 if (freebuf)
1092 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
1095
1096/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001097 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +11001098 *
1099 * If we come across a stale, pinned, locked buffer, we know that we are
1100 * being asked to lock a buffer that has been reallocated. Because it is
1101 * pinned, we know that the log has not been pushed to disk and hence it
1102 * will still be locked. Rather than continuing to have trylock attempts
1103 * fail until someone else pushes the log, push it ourselves before
1104 * returning. This means that the xfsaild will not get stuck trying
1105 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 */
1107int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001108xfs_buf_trylock(
1109 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 int locked;
1112
Nathan Scottce8e9222006-01-11 15:39:08 +11001113 locked = down_trylock(&bp->b_sema) == 0;
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001114 if (locked)
Darrick J. Wong479c6412016-06-21 11:53:28 +10001115 trace_xfs_buf_trylock(bp, _RET_IP_);
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001116 else
Darrick J. Wong479c6412016-06-21 11:53:28 +10001117 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001118 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001122 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +10001123 *
1124 * If we come across a stale, pinned, locked buffer, we know that we
1125 * are being asked to lock a buffer that has been reallocated. Because
1126 * it is pinned, we know that the log has not been pushed to disk and
1127 * hence it will still be locked. Rather than sleeping until someone
1128 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001130void
1131xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001132 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001134 trace_xfs_buf_lock(bp, _RET_IP_);
1135
Dave Chinnered3b4d62010-05-21 12:07:08 +10001136 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001137 xfs_log_force(bp->b_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +11001138 down(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001139
1140 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143void
Nathan Scottce8e9222006-01-11 15:39:08 +11001144xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001145 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Brian Foster20e8a062017-04-21 12:40:44 -07001147 ASSERT(xfs_buf_islocked(bp));
1148
Nathan Scottce8e9222006-01-11 15:39:08 +11001149 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001150 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
Nathan Scottce8e9222006-01-11 15:39:08 +11001153STATIC void
1154xfs_buf_wait_unpin(
1155 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 DECLARE_WAITQUEUE (wait, current);
1158
Nathan Scottce8e9222006-01-11 15:39:08 +11001159 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return;
1161
Nathan Scottce8e9222006-01-11 15:39:08 +11001162 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 for (;;) {
1164 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001165 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001167 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001169 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 set_current_state(TASK_RUNNING);
1171}
1172
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001173static void
1174xfs_buf_ioerror_alert_ratelimited(
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001175 struct xfs_buf *bp)
1176{
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001177 static unsigned long lasttime;
1178 static struct xfs_buftarg *lasttarg;
1179
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001180 if (bp->b_target != lasttarg ||
1181 time_after(jiffies, (lasttime + 5*HZ))) {
1182 lasttime = jiffies;
1183 xfs_buf_ioerror_alert(bp, __this_address);
1184 }
1185 lasttarg = bp->b_target;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001186}
1187
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001188/*
1189 * Account for this latest trip around the retry handler, and decide if
1190 * we've failed enough times to constitute a permanent failure.
1191 */
1192static bool
1193xfs_buf_ioerror_permanent(
1194 struct xfs_buf *bp,
1195 struct xfs_error_cfg *cfg)
1196{
1197 struct xfs_mount *mp = bp->b_mount;
1198
1199 if (cfg->max_retries != XFS_ERR_RETRY_FOREVER &&
1200 ++bp->b_retries > cfg->max_retries)
1201 return true;
1202 if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
1203 time_after(jiffies, cfg->retry_timeout + bp->b_first_retry_time))
1204 return true;
1205
1206 /* At unmount we may treat errors differently */
1207 if ((mp->m_flags & XFS_MOUNT_UNMOUNTING) && mp->m_fail_unmount)
1208 return true;
1209
1210 return false;
1211}
1212
1213/*
1214 * On a sync write or shutdown we just want to stale the buffer and let the
1215 * caller handle the error in bp->b_error appropriately.
1216 *
1217 * If the write was asynchronous then no one will be looking for the error. If
1218 * this is the first failure of this type, clear the error state and write the
1219 * buffer out again. This means we always retry an async write failure at least
1220 * once, but we also need to set the buffer up to behave correctly now for
1221 * repeated failures.
1222 *
1223 * If we get repeated async write failures, then we take action according to the
1224 * error configuration we have been set up to use.
1225 *
Christoph Hellwig70796c62020-09-01 10:55:45 -07001226 * Returns true if this function took care of error handling and the caller must
1227 * not touch the buffer again. Return false if the caller should proceed with
1228 * normal I/O completion handling.
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001229 */
Christoph Hellwig70796c62020-09-01 10:55:45 -07001230static bool
1231xfs_buf_ioend_handle_error(
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001232 struct xfs_buf *bp)
1233{
1234 struct xfs_mount *mp = bp->b_mount;
1235 struct xfs_error_cfg *cfg;
1236
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001237 /*
1238 * If we've already decided to shutdown the filesystem because of I/O
1239 * errors, there's no point in giving this a retry.
1240 */
1241 if (XFS_FORCED_SHUTDOWN(mp))
1242 goto out_stale;
1243
1244 xfs_buf_ioerror_alert_ratelimited(bp);
1245
1246 /*
Christoph Hellwig22c10582020-09-01 10:55:46 -07001247 * We're not going to bother about retrying this during recovery.
1248 * One strike!
1249 */
1250 if (bp->b_flags & _XBF_LOGRECOVERY) {
1251 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1252 return false;
1253 }
1254
1255 /*
Christoph Hellwigf58d0ea2020-09-01 10:55:44 -07001256 * Synchronous writes will have callers process the error.
1257 */
1258 if (!(bp->b_flags & XBF_ASYNC))
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001259 goto out_stale;
1260
1261 trace_xfs_buf_iodone_async(bp, _RET_IP_);
1262
1263 cfg = xfs_error_get_cfg(mp, XFS_ERR_METADATA, bp->b_error);
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001264 if (bp->b_last_error != bp->b_error ||
1265 !(bp->b_flags & (XBF_STALE | XBF_WRITE_FAIL))) {
1266 bp->b_last_error = bp->b_error;
1267 if (cfg->retry_timeout != XFS_ERR_RETRY_FOREVER &&
1268 !bp->b_first_retry_time)
1269 bp->b_first_retry_time = jiffies;
1270 goto resubmit;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001271 }
1272
1273 /*
1274 * Permanent error - we need to trigger a shutdown if we haven't already
1275 * to indicate that inconsistency will result from this action.
1276 */
1277 if (xfs_buf_ioerror_permanent(bp, cfg)) {
1278 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1279 goto out_stale;
1280 }
1281
1282 /* Still considered a transient error. Caller will schedule retries. */
Christoph Hellwig844c9352020-09-01 10:55:45 -07001283 if (bp->b_flags & _XBF_INODES)
1284 xfs_buf_inode_io_fail(bp);
1285 else if (bp->b_flags & _XBF_DQUOTS)
1286 xfs_buf_dquot_io_fail(bp);
1287 else
1288 ASSERT(list_empty(&bp->b_li_list));
1289 xfs_buf_ioerror(bp, 0);
1290 xfs_buf_relse(bp);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001291 return true;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001292
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001293resubmit:
1294 xfs_buf_ioerror(bp, 0);
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001295 bp->b_flags |= (XBF_DONE | XBF_WRITE_FAIL);
Christoph Hellwig3cc49882020-09-01 10:55:45 -07001296 xfs_buf_submit(bp);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001297 return true;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001298out_stale:
1299 xfs_buf_stale(bp);
1300 bp->b_flags |= XBF_DONE;
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001301 bp->b_flags &= ~XBF_WRITE;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001302 trace_xfs_buf_error_relse(bp, _RET_IP_);
Christoph Hellwig70796c62020-09-01 10:55:45 -07001303 return false;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001304}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Christoph Hellwig76b2d322020-09-01 10:55:20 -07001306static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001307xfs_buf_ioend(
1308 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001310 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001311
Dave Chinner61be9c52014-10-02 09:04:31 +10001312 /*
1313 * Pull in IO completion errors now. We are guaranteed to be running
1314 * single threaded, so we don't need the lock to read b_io_error.
1315 */
1316 if (!bp->b_error && bp->b_io_error)
1317 xfs_buf_ioerror(bp, bp->b_io_error);
1318
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001319 if (bp->b_flags & XBF_READ) {
Dave Chinnerb01d1462020-06-29 14:48:47 -07001320 if (!bp->b_error && bp->b_ops)
1321 bp->b_ops->verify_read(bp);
1322 if (!bp->b_error)
1323 bp->b_flags |= XBF_DONE;
Christoph Hellwig23fb5a92020-09-01 10:55:20 -07001324 } else {
1325 if (!bp->b_error) {
1326 bp->b_flags &= ~XBF_WRITE_FAIL;
1327 bp->b_flags |= XBF_DONE;
1328 }
Dave Chinner9fe5c772020-06-29 14:48:47 -07001329
Christoph Hellwig70796c62020-09-01 10:55:45 -07001330 if (unlikely(bp->b_error) && xfs_buf_ioend_handle_error(bp))
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001331 return;
Christoph Hellwig664ffb82020-09-01 10:55:29 -07001332
1333 /* clear the retry state */
1334 bp->b_last_error = 0;
1335 bp->b_retries = 0;
1336 bp->b_first_retry_time = 0;
1337
1338 /*
1339 * Note that for things like remote attribute buffers, there may
1340 * not be a buffer log item here, so processing the buffer log
1341 * item must remain optional.
1342 */
1343 if (bp->b_log_item)
1344 xfs_buf_item_done(bp);
1345
Christoph Hellwig23fb5a92020-09-01 10:55:20 -07001346 if (bp->b_flags & _XBF_INODES)
1347 xfs_buf_inode_iodone(bp);
1348 else if (bp->b_flags & _XBF_DQUOTS)
1349 xfs_buf_dquot_iodone(bp);
Christoph Hellwig22c10582020-09-01 10:55:46 -07001350
Dave Chinnerf593bf12020-06-29 14:48:46 -07001351 }
Christoph Hellwig6a7584b2020-09-01 10:55:44 -07001352
Christoph Hellwig22c10582020-09-01 10:55:46 -07001353 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD |
1354 _XBF_LOGRECOVERY);
Christoph Hellwig55b7d712020-09-01 10:55:46 -07001355
Christoph Hellwig6a7584b2020-09-01 10:55:44 -07001356 if (bp->b_flags & XBF_ASYNC)
1357 xfs_buf_relse(bp);
1358 else
1359 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
Dave Chinnere8aaba92014-10-02 09:04:22 +10001362static void
1363xfs_buf_ioend_work(
1364 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001366 struct xfs_buf *bp =
Brian Fosterb29c70f2014-12-04 09:43:17 +11001367 container_of(work, xfs_buf_t, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001368
Dave Chinnere8aaba92014-10-02 09:04:22 +10001369 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001372static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001373xfs_buf_ioend_async(
1374 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001376 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001377 queue_work(bp->b_mount->m_buf_workqueue, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380void
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001381__xfs_buf_ioerror(
Nathan Scottce8e9222006-01-11 15:39:08 +11001382 xfs_buf_t *bp,
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001383 int error,
1384 xfs_failaddr_t failaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Dave Chinner24513372014-06-25 14:58:08 +10001386 ASSERT(error <= 0 && error >= -1000);
1387 bp->b_error = error;
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001388 trace_xfs_buf_ioerror(bp, error, failaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
Christoph Hellwig901796a2011-10-10 16:52:49 +00001391void
1392xfs_buf_ioerror_alert(
1393 struct xfs_buf *bp,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -08001394 xfs_failaddr_t func)
Christoph Hellwig901796a2011-10-10 16:52:49 +00001395{
Brian Fosterf9bccfc2020-05-06 13:25:21 -07001396 xfs_buf_alert_ratelimited(bp, "XFS: metadata IO error",
1397 "metadata I/O error in \"%pS\" at daddr 0x%llx len %d error %d",
1398 func, (uint64_t)XFS_BUF_ADDR(bp),
1399 bp->b_length, -bp->b_error);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001400}
1401
Brian Foster54b3b1f2020-05-06 13:25:19 -07001402/*
1403 * To simulate an I/O failure, the buffer must be locked and held with at least
1404 * three references. The LRU reference is dropped by the stale call. The buf
1405 * item reference is dropped via ioend processing. The third reference is owned
1406 * by the caller and is dropped on I/O completion if the buffer is XBF_ASYNC.
1407 */
1408void
1409xfs_buf_ioend_fail(
1410 struct xfs_buf *bp)
1411{
1412 bp->b_flags &= ~XBF_DONE;
1413 xfs_buf_stale(bp);
1414 xfs_buf_ioerror(bp, -EIO);
1415 xfs_buf_ioend(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001416}
Christoph Hellwig939d7232010-07-20 17:51:16 +10001417
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001418int
1419xfs_bwrite(
1420 struct xfs_buf *bp)
1421{
1422 int error;
1423
1424 ASSERT(xfs_buf_islocked(bp));
1425
1426 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001427 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
Brian Fosterb6983e82020-05-06 13:25:20 -07001428 XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001429
Brian Foster6af88cd2018-07-11 22:26:35 -07001430 error = xfs_buf_submit(bp);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001431 if (error)
1432 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001433 return error;
1434}
1435
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001436static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001437xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001438 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001440 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Brian Foster7376d742020-05-06 13:29:19 -07001442 if (!bio->bi_status &&
1443 (bp->b_flags & XBF_WRITE) && (bp->b_flags & XBF_ASYNC) &&
Brian Foster43dc0aa2020-05-08 08:50:52 -07001444 XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_IOERROR))
Brian Foster7376d742020-05-06 13:29:19 -07001445 bio->bi_status = BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Dave Chinner37eb17e2012-11-12 22:09:46 +11001447 /*
1448 * don't overwrite existing errors - otherwise we can lose errors on
1449 * buffers that require multiple bios to complete.
1450 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001451 if (bio->bi_status) {
1452 int error = blk_status_to_errno(bio->bi_status);
1453
1454 cmpxchg(&bp->b_io_error, 0, error);
1455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Dave Chinner37eb17e2012-11-12 22:09:46 +11001457 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001458 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1459
Dave Chinnere8aaba92014-10-02 09:04:22 +10001460 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1461 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Dave Chinner3e85c862012-06-22 18:50:09 +10001465static void
1466xfs_buf_ioapply_map(
1467 struct xfs_buf *bp,
1468 int map,
1469 int *buf_offset,
1470 int *count,
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001471 int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Dave Chinner3e85c862012-06-22 18:50:09 +10001473 int page_index;
1474 int total_nr_pages = bp->b_page_count;
1475 int nr_pages;
1476 struct bio *bio;
1477 sector_t sector = bp->b_maps[map].bm_bn;
1478 int size;
1479 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Dave Chinner3e85c862012-06-22 18:50:09 +10001481 /* skip the pages in the buffer before the start offset */
1482 page_index = 0;
1483 offset = *buf_offset;
1484 while (offset >= PAGE_SIZE) {
1485 page_index++;
1486 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001487 }
1488
Dave Chinner3e85c862012-06-22 18:50:09 +10001489 /*
1490 * Limit the IO size to the length of the current vector, and update the
1491 * remaining IO count for the next time around.
1492 */
1493 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1494 *count -= size;
1495 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001498 atomic_inc(&bp->b_io_remaining);
Ming Leic908e382016-05-30 21:34:33 +08001499 nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 bio = bio_alloc(GFP_NOIO, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001502 bio_set_dev(bio, bp->b_target->bt_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001503 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001504 bio->bi_end_io = xfs_buf_bio_end_io;
1505 bio->bi_private = bp;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001506 bio->bi_opf = op;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001507
Dave Chinner3e85c862012-06-22 18:50:09 +10001508 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001509 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 if (nbytes > size)
1512 nbytes = size;
1513
Dave Chinner3e85c862012-06-22 18:50:09 +10001514 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1515 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001516 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 break;
1518
1519 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001520 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 size -= nbytes;
1522 total_nr_pages--;
1523 }
1524
Kent Overstreet4f024f32013-10-11 15:44:27 -07001525 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001526 if (xfs_buf_is_vmapped(bp)) {
1527 flush_kernel_vmap_range(bp->b_addr,
1528 xfs_buf_vmap_len(bp));
1529 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001530 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (size)
1532 goto next_chunk;
1533 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001534 /*
1535 * This is guaranteed not to be the last io reference count
Dave Chinner595bff72014-10-02 09:05:14 +10001536 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001537 */
1538 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001539 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001540 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001542
1543}
1544
1545STATIC void
1546_xfs_buf_ioapply(
1547 struct xfs_buf *bp)
1548{
1549 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001550 int op;
Dave Chinner3e85c862012-06-22 18:50:09 +10001551 int offset;
1552 int size;
1553 int i;
1554
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001555 /*
1556 * Make sure we capture only current IO errors rather than stale errors
1557 * left over from previous use of the buffer (e.g. failed readahead).
1558 */
1559 bp->b_error = 0;
1560
Dave Chinner3e85c862012-06-22 18:50:09 +10001561 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001562 op = REQ_OP_WRITE;
Dave Chinner1813dd62012-11-14 17:54:40 +11001563
1564 /*
1565 * Run the write verifier callback function if it exists. If
1566 * this function fails it will mark the buffer with an error and
1567 * the IO should not be dispatched.
1568 */
1569 if (bp->b_ops) {
1570 bp->b_ops->verify_write(bp);
1571 if (bp->b_error) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001572 xfs_force_shutdown(bp->b_mount,
Dave Chinner1813dd62012-11-14 17:54:40 +11001573 SHUTDOWN_CORRUPT_INCORE);
1574 return;
1575 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001576 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001577 struct xfs_mount *mp = bp->b_mount;
Dave Chinner400b9d82014-08-04 12:42:40 +10001578
1579 /*
1580 * non-crc filesystems don't attach verifiers during
1581 * log recovery, so don't warn for such filesystems.
1582 */
1583 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1584 xfs_warn(mp,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001585 "%s: no buf ops on daddr 0x%llx len %d",
Dave Chinner400b9d82014-08-04 12:42:40 +10001586 __func__, bp->b_bn, bp->b_length);
Darrick J. Wong9c712a12018-01-08 10:51:26 -08001587 xfs_hex_dump(bp->b_addr,
1588 XFS_CORRUPTION_DUMP_LEN);
Dave Chinner400b9d82014-08-04 12:42:40 +10001589 dump_stack();
1590 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001591 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001592 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001593 op = REQ_OP_READ;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001594 if (bp->b_flags & XBF_READ_AHEAD)
1595 op |= REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001596 }
1597
1598 /* we only use the buffer cache for meta-data */
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001599 op |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001600
1601 /*
1602 * Walk all the vectors issuing IO on them. Set up the initial offset
1603 * into the buffer and the desired IO size before we start -
1604 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1605 * subsequent call.
1606 */
1607 offset = bp->b_offset;
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001608 size = BBTOB(bp->b_length);
Dave Chinner3e85c862012-06-22 18:50:09 +10001609 blk_start_plug(&plug);
1610 for (i = 0; i < bp->b_map_count; i++) {
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001611 xfs_buf_ioapply_map(bp, i, &offset, &size, op);
Dave Chinner3e85c862012-06-22 18:50:09 +10001612 if (bp->b_error)
1613 break;
1614 if (size <= 0)
1615 break; /* all done */
1616 }
1617 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Dave Chinner595bff72014-10-02 09:05:14 +10001620/*
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001621 * Wait for I/O completion of a sync buffer and return the I/O error code.
Dave Chinner595bff72014-10-02 09:05:14 +10001622 */
Brian Fostereaebb512018-07-11 22:26:34 -07001623static int
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001624xfs_buf_iowait(
Dave Chinner595bff72014-10-02 09:05:14 +10001625 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001627 ASSERT(!(bp->b_flags & XBF_ASYNC));
1628
1629 trace_xfs_buf_iowait(bp, _RET_IP_);
1630 wait_for_completion(&bp->b_iowait);
1631 trace_xfs_buf_iowait_done(bp, _RET_IP_);
1632
1633 return bp->b_error;
1634}
1635
1636/*
1637 * Buffer I/O submission path, read or write. Asynchronous submission transfers
1638 * the buffer lock ownership and the current reference to the IO. It is not
1639 * safe to reference the buffer after a call to this function unless the caller
1640 * holds an additional reference itself.
1641 */
1642int
1643__xfs_buf_submit(
1644 struct xfs_buf *bp,
1645 bool wait)
1646{
1647 int error = 0;
1648
Dave Chinner595bff72014-10-02 09:05:14 +10001649 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001651 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff72014-10-02 09:05:14 +10001652
1653 /* on shutdown we stale and complete the buffer immediately */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001654 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
Brian Foster54b3b1f2020-05-06 13:25:19 -07001655 xfs_buf_ioend_fail(bp);
Brian Fostereaebb512018-07-11 22:26:34 -07001656 return -EIO;
Dave Chinner595bff72014-10-02 09:05:14 +10001657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001659 /*
1660 * Grab a reference so the buffer does not go away underneath us. For
1661 * async buffers, I/O completion drops the callers reference, which
1662 * could occur before submission returns.
1663 */
1664 xfs_buf_hold(bp);
1665
Christoph Hellwig375ec692011-08-23 08:28:03 +00001666 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001667 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Dave Chinner61be9c52014-10-02 09:04:31 +10001669 /* clear the internal error state to avoid spurious errors */
1670 bp->b_io_error = 0;
1671
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001672 /*
Brian Fostereaebb512018-07-11 22:26:34 -07001673 * Set the count to 1 initially, this will stop an I/O completion
1674 * callout which happens before we have started all the I/O from calling
1675 * xfs_buf_ioend too early.
1676 */
1677 atomic_set(&bp->b_io_remaining, 1);
1678 if (bp->b_flags & XBF_ASYNC)
1679 xfs_buf_ioacct_inc(bp);
1680 _xfs_buf_ioapply(bp);
1681
1682 /*
1683 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1684 * reference we took above. If we drop it to zero, run completion so
1685 * that we don't return to the caller with completion still pending.
1686 */
1687 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1688 if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
1689 xfs_buf_ioend(bp);
1690 else
1691 xfs_buf_ioend_async(bp);
1692 }
1693
Brian Foster6af88cd2018-07-11 22:26:35 -07001694 if (wait)
1695 error = xfs_buf_iowait(bp);
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001696
Dave Chinner595bff72014-10-02 09:05:14 +10001697 /*
Brian Foster6af88cd2018-07-11 22:26:35 -07001698 * Release the hold that keeps the buffer referenced for the entire
1699 * I/O. Note that if the buffer is async, it is not safe to reference
1700 * after this release.
Dave Chinner595bff72014-10-02 09:05:14 +10001701 */
1702 xfs_buf_rele(bp);
1703 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001706void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001707xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001708 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 size_t offset)
1710{
1711 struct page *page;
1712
Dave Chinner611c9942012-04-23 15:59:07 +10001713 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001714 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Nathan Scottce8e9222006-01-11 15:39:08 +11001716 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001717 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001718 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721void
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001722xfs_buf_zero(
1723 struct xfs_buf *bp,
1724 size_t boff,
1725 size_t bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Dave Chinner795cac72012-04-23 15:58:53 +10001727 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 bend = boff + bsize;
1730 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001731 struct page *page;
1732 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Dave Chinner795cac72012-04-23 15:58:53 +10001734 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1735 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1736 page = bp->b_pages[page_index];
1737 csize = min_t(size_t, PAGE_SIZE - page_offset,
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001738 BBTOB(bp->b_length) - boff);
Dave Chinner795cac72012-04-23 15:58:53 +10001739
1740 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001742 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 boff += csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746}
1747
1748/*
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001749 * Log a message about and stale a buffer that a caller has decided is corrupt.
1750 *
1751 * This function should be called for the kinds of metadata corruption that
1752 * cannot be detect from a verifier, such as incorrect inter-block relationship
1753 * data. Do /not/ call this function from a verifier function.
1754 *
1755 * The buffer must be XBF_DONE prior to the call. Afterwards, the buffer will
1756 * be marked stale, but b_error will not be set. The caller is responsible for
1757 * releasing the buffer or fixing it.
1758 */
1759void
1760__xfs_buf_mark_corrupt(
1761 struct xfs_buf *bp,
1762 xfs_failaddr_t fa)
1763{
1764 ASSERT(bp->b_flags & XBF_DONE);
1765
Darrick J. Wonge83cf872020-03-11 10:37:54 -07001766 xfs_buf_corruption_error(bp, fa);
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001767 xfs_buf_stale(bp);
1768}
1769
1770/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001771 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 */
1773
1774/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001775 * Wait for any bufs with callbacks that have been submitted but have not yet
1776 * returned. These buffers will have an elevated hold count, so wait on those
1777 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001779static enum lru_status
1780xfs_buftarg_wait_rele(
1781 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001782 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001783 spinlock_t *lru_lock,
1784 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Dave Chinnere80dfa12013-08-28 10:18:05 +10001786{
1787 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001788 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001789
1790 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001791 /* need to wait, so skip it this pass */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001792 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001793 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
Dave Chinnera4082352013-08-28 10:18:06 +10001795 if (!spin_trylock(&bp->b_lock))
1796 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001797
Dave Chinnera4082352013-08-28 10:18:06 +10001798 /*
1799 * clear the LRU reference count so the buffer doesn't get
1800 * ignored in xfs_buf_rele().
1801 */
1802 atomic_set(&bp->b_lru_ref, 0);
1803 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001804 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001805 spin_unlock(&bp->b_lock);
1806 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
Dave Chinnere80dfa12013-08-28 10:18:05 +10001809void
1810xfs_wait_buftarg(
1811 struct xfs_buftarg *btp)
1812{
Dave Chinnera4082352013-08-28 10:18:06 +10001813 LIST_HEAD(dispose);
Brian Foster61948b62020-05-06 13:25:21 -07001814 int loop = 0;
1815 bool write_fail = false;
Dave Chinnera4082352013-08-28 10:18:06 +10001816
Dave Chinner85bec542016-01-19 08:28:10 +11001817 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001818 * First wait on the buftarg I/O count for all in-flight buffers to be
1819 * released. This is critical as new buffers do not make the LRU until
1820 * they are released.
1821 *
1822 * Next, flush the buffer workqueue to ensure all completion processing
1823 * has finished. Just waiting on buffer locks is not sufficient for
1824 * async IO as the reference count held over IO is not released until
1825 * after the buffer lock is dropped. Hence we need to ensure here that
1826 * all reference counts have been dropped before we start walking the
1827 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001828 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001829 while (percpu_counter_sum(&btp->bt_io_count))
1830 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001831 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Dave Chinner85bec542016-01-19 08:28:10 +11001832
Dave Chinnera4082352013-08-28 10:18:06 +10001833 /* loop until there is nothing left on the lru list. */
1834 while (list_lru_count(&btp->bt_lru)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001835 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001836 &dispose, LONG_MAX);
1837
1838 while (!list_empty(&dispose)) {
1839 struct xfs_buf *bp;
1840 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1841 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001842 if (bp->b_flags & XBF_WRITE_FAIL) {
Brian Foster61948b62020-05-06 13:25:21 -07001843 write_fail = true;
1844 xfs_buf_alert_ratelimited(bp,
1845 "XFS: Corruption Alert",
Darrick J. Wongc219b012018-01-08 11:39:18 -08001846"Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001847 (long long)bp->b_bn);
1848 }
Dave Chinnera4082352013-08-28 10:18:06 +10001849 xfs_buf_rele(bp);
1850 }
1851 if (loop++ != 0)
1852 delay(100);
1853 }
Brian Foster61948b62020-05-06 13:25:21 -07001854
1855 /*
1856 * If one or more failed buffers were freed, that means dirty metadata
1857 * was thrown away. This should only ever happen after I/O completion
1858 * handling has elevated I/O error(s) to permanent failures and shuts
1859 * down the fs.
1860 */
1861 if (write_fail) {
1862 ASSERT(XFS_FORCED_SHUTDOWN(btp->bt_mount));
1863 xfs_alert(btp->bt_mount,
1864 "Please run xfs_repair to determine the extent of the problem.");
1865 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001866}
1867
1868static enum lru_status
1869xfs_buftarg_isolate(
1870 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001871 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001872 spinlock_t *lru_lock,
1873 void *arg)
1874{
1875 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1876 struct list_head *dispose = arg;
1877
1878 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001879 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1880 * If we fail to get the lock, just skip it.
1881 */
1882 if (!spin_trylock(&bp->b_lock))
1883 return LRU_SKIP;
1884 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001885 * Decrement the b_lru_ref count unless the value is already
1886 * zero. If the value is already zero, we need to reclaim the
1887 * buffer, otherwise it gets another trip through the LRU.
1888 */
Vratislav Bendel19957a12018-03-06 17:07:44 -08001889 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
Dave Chinnera4082352013-08-28 10:18:06 +10001890 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001891 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001892 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001893
Dave Chinnera4082352013-08-28 10:18:06 +10001894 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001895 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001896 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001897 return LRU_REMOVED;
1898}
1899
Andrew Mortonaddbda42013-08-28 10:18:06 +10001900static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001901xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001902 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001903 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001904{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001905 struct xfs_buftarg *btp = container_of(shrink,
1906 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001907 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001908 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001909
Vladimir Davydov503c3582015-02-12 14:58:47 -08001910 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1911 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001912
1913 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001914 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001915 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1916 list_del_init(&bp->b_lru);
1917 xfs_buf_rele(bp);
1918 }
1919
Dave Chinnere80dfa12013-08-28 10:18:05 +10001920 return freed;
1921}
1922
Andrew Mortonaddbda42013-08-28 10:18:06 +10001923static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001924xfs_buftarg_shrink_count(
1925 struct shrinker *shrink,
1926 struct shrink_control *sc)
1927{
1928 struct xfs_buftarg *btp = container_of(shrink,
1929 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001930 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001931}
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933void
1934xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001935 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001937 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001938 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1939 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001940 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001941
Dave Chinner2291dab2016-12-09 16:49:54 +11001942 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001943
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001944 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001947int
1948xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001950 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001952 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001953 btp->bt_meta_sectorsize = sectorsize;
1954 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Nathan Scottce8e9222006-01-11 15:39:08 +11001956 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001957 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001958 "Cannot set_blocksize to %u on device %pg",
1959 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001960 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001963 /* Set up device logical sector size mask */
1964 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1965 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return 0;
1968}
1969
1970/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001971 * When allocating the initial buffer target we have not yet
1972 * read in the superblock, so don't know what sized sectors
1973 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975STATIC int
1976xfs_setsize_buftarg_early(
1977 xfs_buftarg_t *btp,
1978 struct block_device *bdev)
1979{
Eric Sandeena96c4152014-04-14 19:00:29 +10001980 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983xfs_buftarg_t *
1984xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001985 struct xfs_mount *mp,
Dan Williams486aff52017-08-24 15:12:50 -07001986 struct block_device *bdev,
1987 struct dax_device *dax_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 xfs_buftarg_t *btp;
1990
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07001991 btp = kmem_zalloc(sizeof(*btp), KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Dave Chinnerebad8612010-09-22 10:47:20 +10001993 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001994 btp->bt_dev = bdev->bd_dev;
1995 btp->bt_bdev = bdev;
Dan Williams486aff52017-08-24 15:12:50 -07001996 btp->bt_daxdev = dax_dev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001997
Brian Fosterf9bccfc2020-05-06 13:25:21 -07001998 /*
1999 * Buffer IO error rate limiting. Limit it to no more than 10 messages
2000 * per 30 seconds so as to not spam logs too much on repeated errors.
2001 */
2002 ratelimit_state_init(&btp->bt_ioerror_rl, 30 * HZ,
2003 DEFAULT_RATELIMIT_BURST);
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (xfs_setsize_buftarg_early(btp, bdev))
Michal Hockod210a982017-11-23 17:13:40 +01002006 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10002007
2008 if (list_lru_init(&btp->bt_lru))
Michal Hockod210a982017-11-23 17:13:40 +01002009 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10002010
Brian Foster9c7504a2016-07-20 11:15:28 +10002011 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
Michal Hockod210a982017-11-23 17:13:40 +01002012 goto error_lru;
Brian Foster9c7504a2016-07-20 11:15:28 +10002013
Dave Chinnere80dfa12013-08-28 10:18:05 +10002014 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
2015 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11002016 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10002017 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Michal Hockod210a982017-11-23 17:13:40 +01002018 if (register_shrinker(&btp->bt_shrinker))
2019 goto error_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return btp;
2021
Michal Hockod210a982017-11-23 17:13:40 +01002022error_pcpu:
2023 percpu_counter_destroy(&btp->bt_io_count);
2024error_lru:
2025 list_lru_destroy(&btp->bt_lru);
2026error_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002027 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 return NULL;
2029}
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031/*
Brian Foster20e8a062017-04-21 12:40:44 -07002032 * Cancel a delayed write list.
2033 *
2034 * Remove each buffer from the list, clear the delwri queue flag and drop the
2035 * associated buffer reference.
2036 */
2037void
2038xfs_buf_delwri_cancel(
2039 struct list_head *list)
2040{
2041 struct xfs_buf *bp;
2042
2043 while (!list_empty(list)) {
2044 bp = list_first_entry(list, struct xfs_buf, b_list);
2045
2046 xfs_buf_lock(bp);
2047 bp->b_flags &= ~_XBF_DELWRI_Q;
2048 list_del_init(&bp->b_list);
2049 xfs_buf_relse(bp);
2050 }
2051}
2052
2053/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002054 * Add a buffer to the delayed write list.
2055 *
2056 * This queues a buffer for writeout if it hasn't already been. Note that
2057 * neither this routine nor the buffer list submission functions perform
2058 * any internal synchronization. It is expected that the lists are thread-local
2059 * to the callers.
2060 *
2061 * Returns true if we queued up the buffer, or false if it already had
2062 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002064bool
Nathan Scottce8e9222006-01-11 15:39:08 +11002065xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002066 struct xfs_buf *bp,
2067 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002069 ASSERT(xfs_buf_islocked(bp));
2070 ASSERT(!(bp->b_flags & XBF_READ));
2071
2072 /*
2073 * If the buffer is already marked delwri it already is queued up
2074 * by someone else for imediate writeout. Just ignore it in that
2075 * case.
2076 */
2077 if (bp->b_flags & _XBF_DELWRI_Q) {
2078 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
2079 return false;
2080 }
David Chinnera6867a62006-01-11 15:37:58 +11002081
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002082 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
2083
Dave Chinnerd808f612010-02-02 10:13:42 +11002084 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002085 * If a buffer gets written out synchronously or marked stale while it
2086 * is on a delwri list we lazily remove it. To do this, the other party
2087 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
2088 * It remains referenced and on the list. In a rare corner case it
2089 * might get readded to a delwri list after the synchronous writeout, in
2090 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11002091 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002092 bp->b_flags |= _XBF_DELWRI_Q;
2093 if (list_empty(&bp->b_list)) {
2094 atomic_inc(&bp->b_hold);
2095 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11002096 }
David Chinner585e6d82007-02-10 18:32:29 +11002097
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002098 return true;
David Chinner585e6d82007-02-10 18:32:29 +11002099}
2100
Dave Chinner089716a2010-01-26 15:13:25 +11002101/*
2102 * Compare function is more complex than it needs to be because
2103 * the return value is only 32 bits and we are doing comparisons
2104 * on 64 bit values
2105 */
2106static int
2107xfs_buf_cmp(
2108 void *priv,
2109 struct list_head *a,
2110 struct list_head *b)
2111{
2112 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
2113 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
2114 xfs_daddr_t diff;
2115
Mark Tinguelyf4b42422012-12-04 17:18:02 -06002116 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11002117 if (diff < 0)
2118 return -1;
2119 if (diff > 0)
2120 return 1;
2121 return 0;
2122}
2123
Dave Chinner26f1fe82016-06-01 17:38:15 +10002124/*
Brian Fostere339dd82018-07-11 22:26:34 -07002125 * Submit buffers for write. If wait_list is specified, the buffers are
2126 * submitted using sync I/O and placed on the wait list such that the caller can
2127 * iowait each buffer. Otherwise async I/O is used and the buffers are released
2128 * at I/O completion time. In either case, buffers remain locked until I/O
2129 * completes and the buffer is released from the queue.
Dave Chinner26f1fe82016-06-01 17:38:15 +10002130 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002131static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10002132xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002133 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10002134 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002136 struct xfs_buf *bp, *n;
2137 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002138 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Dave Chinner26f1fe82016-06-01 17:38:15 +10002140 list_sort(NULL, buffer_list, xfs_buf_cmp);
2141
2142 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002143 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10002144 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002145 if (xfs_buf_ispinned(bp)) {
2146 pinned++;
2147 continue;
2148 }
2149 if (!xfs_buf_trylock(bp))
2150 continue;
2151 } else {
2152 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002155 /*
2156 * Someone else might have written the buffer synchronously or
2157 * marked it stale in the meantime. In that case only the
2158 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
2159 * reference and remove it from the list here.
2160 */
2161 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
2162 list_del_init(&bp->b_list);
2163 xfs_buf_relse(bp);
2164 continue;
2165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002167 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002168
Dave Chinnercf53e992014-10-02 09:04:01 +10002169 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002170 * If we have a wait list, each buffer (and associated delwri
2171 * queue reference) transfers to it and is submitted
2172 * synchronously. Otherwise, drop the buffer from the delwri
2173 * queue and submit async.
Dave Chinnercf53e992014-10-02 09:04:01 +10002174 */
Brian Fosterb6983e82020-05-06 13:25:20 -07002175 bp->b_flags &= ~_XBF_DELWRI_Q;
Brian Fostere339dd82018-07-11 22:26:34 -07002176 bp->b_flags |= XBF_WRITE;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002177 if (wait_list) {
Brian Fostere339dd82018-07-11 22:26:34 -07002178 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002179 list_move_tail(&bp->b_list, wait_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002180 } else {
2181 bp->b_flags |= XBF_ASYNC;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002182 list_del_init(&bp->b_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002183 }
Brian Foster6af88cd2018-07-11 22:26:35 -07002184 __xfs_buf_submit(bp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00002186 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002188 return pinned;
2189}
Nathan Scottf07c2252006-09-28 10:52:15 +10002190
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002191/*
2192 * Write out a buffer list asynchronously.
2193 *
2194 * This will take the @buffer_list, write all non-locked and non-pinned buffers
2195 * out and not wait for I/O completion on any of the buffers. This interface
2196 * is only safely useable for callers that can track I/O completion by higher
2197 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
2198 * function.
Brian Fosterefc32892018-10-18 17:21:49 +11002199 *
2200 * Note: this function will skip buffers it would block on, and in doing so
2201 * leaves them on @buffer_list so they can be retried on a later pass. As such,
2202 * it is up to the caller to ensure that the buffer list is fully submitted or
2203 * cancelled appropriately when they are finished with the list. Failure to
2204 * cancel or resubmit the list until it is empty will result in leaked buffers
2205 * at unmount time.
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002206 */
2207int
2208xfs_buf_delwri_submit_nowait(
2209 struct list_head *buffer_list)
2210{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002211 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002212}
2213
2214/*
2215 * Write out a buffer list synchronously.
2216 *
2217 * This will take the @buffer_list, write all buffers out and wait for I/O
2218 * completion on all of the buffers. @buffer_list is consumed by the function,
2219 * so callers must have some other way of tracking buffers if they require such
2220 * functionality.
2221 */
2222int
2223xfs_buf_delwri_submit(
2224 struct list_head *buffer_list)
2225{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002226 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002227 int error = 0, error2;
2228 struct xfs_buf *bp;
2229
Dave Chinner26f1fe82016-06-01 17:38:15 +10002230 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002231
2232 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10002233 while (!list_empty(&wait_list)) {
2234 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002235
2236 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10002237
Brian Fostere339dd82018-07-11 22:26:34 -07002238 /*
2239 * Wait on the locked buffer, check for errors and unlock and
2240 * release the delwri queue reference.
2241 */
2242 error2 = xfs_buf_iowait(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002243 xfs_buf_relse(bp);
2244 if (!error)
2245 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
2247
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002248 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Brian Foster7912e7f2017-06-14 21:21:45 -07002251/*
2252 * Push a single buffer on a delwri queue.
2253 *
2254 * The purpose of this function is to submit a single buffer of a delwri queue
2255 * and return with the buffer still on the original queue. The waiting delwri
2256 * buffer submission infrastructure guarantees transfer of the delwri queue
2257 * buffer reference to a temporary wait list. We reuse this infrastructure to
2258 * transfer the buffer back to the original queue.
2259 *
2260 * Note the buffer transitions from the queued state, to the submitted and wait
2261 * listed state and back to the queued state during this call. The buffer
2262 * locking and queue management logic between _delwri_pushbuf() and
2263 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2264 * before returning.
2265 */
2266int
2267xfs_buf_delwri_pushbuf(
2268 struct xfs_buf *bp,
2269 struct list_head *buffer_list)
2270{
2271 LIST_HEAD (submit_list);
2272 int error;
2273
2274 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2275
2276 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2277
2278 /*
2279 * Isolate the buffer to a new local list so we can submit it for I/O
2280 * independently from the rest of the original list.
2281 */
2282 xfs_buf_lock(bp);
2283 list_move(&bp->b_list, &submit_list);
2284 xfs_buf_unlock(bp);
2285
2286 /*
2287 * Delwri submission clears the DELWRI_Q buffer flag and returns with
Brian Fostere339dd82018-07-11 22:26:34 -07002288 * the buffer on the wait list with the original reference. Rather than
Brian Foster7912e7f2017-06-14 21:21:45 -07002289 * bounce the buffer from a local wait list back to the original list
2290 * after I/O completion, reuse the original list as the wait list.
2291 */
2292 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2293
2294 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002295 * The buffer is now locked, under I/O and wait listed on the original
2296 * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2297 * return with the buffer unlocked and on the original queue.
Brian Foster7912e7f2017-06-14 21:21:45 -07002298 */
Brian Fostere339dd82018-07-11 22:26:34 -07002299 error = xfs_buf_iowait(bp);
Brian Foster7912e7f2017-06-14 21:21:45 -07002300 bp->b_flags |= _XBF_DELWRI_Q;
2301 xfs_buf_unlock(bp);
2302
2303 return error;
2304}
2305
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002306int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002307xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Dave Chinner12eba652020-03-24 20:10:28 -07002309 xfs_buf_zone = kmem_cache_create("xfs_buf", sizeof(struct xfs_buf), 0,
2310 SLAB_HWCACHE_ALIGN |
2311 SLAB_RECLAIM_ACCOUNT |
2312 SLAB_MEM_SPREAD,
2313 NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002314 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002315 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002316
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002319 out:
Nathan Scott87582802006-03-14 13:18:19 +11002320 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323void
Nathan Scottce8e9222006-01-11 15:39:08 +11002324xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08002326 kmem_cache_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
Brian Foster7561d272017-10-17 14:16:29 -07002328
2329void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
2330{
Brian Foster7561d272017-10-17 14:16:29 -07002331 /*
2332 * Set the lru reference count to 0 based on the error injection tag.
2333 * This allows userspace to disrupt buffer caching for debug/testing
2334 * purposes.
2335 */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002336 if (XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_LRU_REF))
Brian Foster7561d272017-10-17 14:16:29 -07002337 lru_ref = 0;
2338
2339 atomic_set(&bp->b_lru_ref, lru_ref);
2340}
Brian Foster8473fee2019-02-07 10:45:46 -08002341
2342/*
2343 * Verify an on-disk magic value against the magic value specified in the
2344 * verifier structure. The verifier magic is in disk byte order so the caller is
2345 * expected to pass the value directly from disk.
2346 */
2347bool
2348xfs_verify_magic(
2349 struct xfs_buf *bp,
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002350 __be32 dmagic)
Brian Foster8473fee2019-02-07 10:45:46 -08002351{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002352 struct xfs_mount *mp = bp->b_mount;
Brian Foster8473fee2019-02-07 10:45:46 -08002353 int idx;
2354
2355 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002356 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic[idx]))
Brian Foster8473fee2019-02-07 10:45:46 -08002357 return false;
2358 return dmagic == bp->b_ops->magic[idx];
2359}
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002360/*
2361 * Verify an on-disk magic value against the magic value specified in the
2362 * verifier structure. The verifier magic is in disk byte order so the caller is
2363 * expected to pass the value directly from disk.
2364 */
2365bool
2366xfs_verify_magic16(
2367 struct xfs_buf *bp,
2368 __be16 dmagic)
2369{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002370 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002371 int idx;
2372
2373 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002374 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic16[idx]))
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002375 return false;
2376 return dmagic == bp->b_ops->magic16[idx];
2377}