blob: d5d6a68bb1e677b7957c866e17fb9fab1019a220 [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"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070017#include "xfs_errortag.h"
Brian Foster7561d272017-10-17 14:16:29 -070018#include "xfs_error.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050019
David Chinner7989cb82007-02-10 18:34:56 +110020static kmem_zone_t *xfs_buf_zone;
Christoph Hellwig23ea4032005-06-21 15:14:01 +100021
Nathan Scottce8e9222006-01-11 15:39:08 +110022#define xb_to_gfp(flags) \
Dave Chinneraa5c1582012-04-23 15:58:56 +100023 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Dave Chinner37fd1672018-10-18 17:21:29 +110025/*
26 * Locking orders
27 *
28 * xfs_buf_ioacct_inc:
29 * xfs_buf_ioacct_dec:
30 * b_sema (caller holds)
31 * b_lock
32 *
33 * xfs_buf_stale:
34 * b_sema (caller holds)
35 * b_lock
36 * lru_lock
37 *
38 * xfs_buf_rele:
39 * b_lock
40 * pag_buf_lock
41 * lru_lock
42 *
43 * xfs_buftarg_wait_rele
44 * lru_lock
45 * b_lock (trylock due to inversion)
46 *
47 * xfs_buftarg_isolate
48 * lru_lock
49 * b_lock (trylock due to inversion)
50 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
James Bottomley73c77e22010-01-25 11:42:24 -060052static inline int
53xfs_buf_is_vmapped(
54 struct xfs_buf *bp)
55{
56 /*
57 * Return true if the buffer is vmapped.
58 *
Dave Chinner611c9942012-04-23 15:59:07 +100059 * b_addr is null if the buffer is not mapped, but the code is clever
60 * enough to know it doesn't have to map a single page, so the check has
61 * to be both for b_addr and bp->b_page_count > 1.
James Bottomley73c77e22010-01-25 11:42:24 -060062 */
Dave Chinner611c9942012-04-23 15:59:07 +100063 return bp->b_addr && bp->b_page_count > 1;
James Bottomley73c77e22010-01-25 11:42:24 -060064}
65
66static inline int
67xfs_buf_vmap_len(
68 struct xfs_buf *bp)
69{
70 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
71}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
Brian Foster9c7504a2016-07-20 11:15:28 +100074 * Bump the I/O in flight count on the buftarg if we haven't yet done so for
75 * this buffer. The count is incremented once per buffer (per hold cycle)
76 * because the corresponding decrement is deferred to buffer release. Buffers
77 * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
78 * tracking adds unnecessary overhead. This is used for sychronization purposes
79 * with unmount (see xfs_wait_buftarg()), so all we really need is a count of
80 * in-flight buffers.
81 *
82 * Buffers that are never released (e.g., superblock, iclog buffers) must set
83 * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
84 * never reaches zero and unmount hangs indefinitely.
85 */
86static inline void
87xfs_buf_ioacct_inc(
88 struct xfs_buf *bp)
89{
Brian Foster63db7c82017-05-31 08:22:52 -070090 if (bp->b_flags & XBF_NO_IOACCT)
Brian Foster9c7504a2016-07-20 11:15:28 +100091 return;
92
93 ASSERT(bp->b_flags & XBF_ASYNC);
Brian Foster63db7c82017-05-31 08:22:52 -070094 spin_lock(&bp->b_lock);
95 if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) {
96 bp->b_state |= XFS_BSTATE_IN_FLIGHT;
97 percpu_counter_inc(&bp->b_target->bt_io_count);
98 }
99 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000100}
101
102/*
103 * Clear the in-flight state on a buffer about to be released to the LRU or
104 * freed and unaccount from the buftarg.
105 */
106static inline void
Brian Foster63db7c82017-05-31 08:22:52 -0700107__xfs_buf_ioacct_dec(
108 struct xfs_buf *bp)
109{
Brian Foster95989c42017-06-08 08:23:07 -0700110 lockdep_assert_held(&bp->b_lock);
Brian Foster63db7c82017-05-31 08:22:52 -0700111
112 if (bp->b_state & XFS_BSTATE_IN_FLIGHT) {
113 bp->b_state &= ~XFS_BSTATE_IN_FLIGHT;
114 percpu_counter_dec(&bp->b_target->bt_io_count);
115 }
116}
117
118static inline void
Brian Foster9c7504a2016-07-20 11:15:28 +1000119xfs_buf_ioacct_dec(
120 struct xfs_buf *bp)
121{
Brian Foster63db7c82017-05-31 08:22:52 -0700122 spin_lock(&bp->b_lock);
123 __xfs_buf_ioacct_dec(bp);
124 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000125}
126
127/*
Dave Chinner430cbeb2010-12-02 16:30:55 +1100128 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
129 * b_lru_ref count so that the buffer is freed immediately when the buffer
130 * reference count falls to zero. If the buffer is already on the LRU, we need
131 * to remove the reference that LRU holds on the buffer.
132 *
133 * This prevents build-up of stale buffers on the LRU.
134 */
135void
136xfs_buf_stale(
137 struct xfs_buf *bp)
138{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000139 ASSERT(xfs_buf_islocked(bp));
140
Dave Chinner430cbeb2010-12-02 16:30:55 +1100141 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000142
143 /*
144 * Clear the delwri status so that a delwri queue walker will not
145 * flush this buffer to disk now that it is stale. The delwri queue has
146 * a reference to the buffer, so this is safe to do.
147 */
148 bp->b_flags &= ~_XBF_DELWRI_Q;
149
Brian Foster9c7504a2016-07-20 11:15:28 +1000150 /*
151 * Once the buffer is marked stale and unlocked, a subsequent lookup
152 * could reset b_flags. There is no guarantee that the buffer is
153 * unaccounted (released to LRU) before that occurs. Drop in-flight
154 * status now to preserve accounting consistency.
155 */
Dave Chinnera4082352013-08-28 10:18:06 +1000156 spin_lock(&bp->b_lock);
Brian Foster63db7c82017-05-31 08:22:52 -0700157 __xfs_buf_ioacct_dec(bp);
158
Dave Chinnera4082352013-08-28 10:18:06 +1000159 atomic_set(&bp->b_lru_ref, 0);
160 if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
Dave Chinnere80dfa12013-08-28 10:18:05 +1000161 (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
162 atomic_dec(&bp->b_hold);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100163
Dave Chinner430cbeb2010-12-02 16:30:55 +1100164 ASSERT(atomic_read(&bp->b_hold) >= 1);
Dave Chinnera4082352013-08-28 10:18:06 +1000165 spin_unlock(&bp->b_lock);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Dave Chinner3e85c862012-06-22 18:50:09 +1000168static int
169xfs_buf_get_maps(
170 struct xfs_buf *bp,
171 int map_count)
172{
173 ASSERT(bp->b_maps == NULL);
174 bp->b_map_count = map_count;
175
176 if (map_count == 1) {
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600177 bp->b_maps = &bp->__b_map;
Dave Chinner3e85c862012-06-22 18:50:09 +1000178 return 0;
179 }
180
181 bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
182 KM_NOFS);
183 if (!bp->b_maps)
Dave Chinner24513372014-06-25 14:58:08 +1000184 return -ENOMEM;
Dave Chinner3e85c862012-06-22 18:50:09 +1000185 return 0;
186}
187
188/*
189 * Frees b_pages if it was allocated.
190 */
191static void
192xfs_buf_free_maps(
193 struct xfs_buf *bp)
194{
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600195 if (bp->b_maps != &bp->__b_map) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000196 kmem_free(bp->b_maps);
197 bp->b_maps = NULL;
198 }
199}
200
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800201static int
Dave Chinner3e85c862012-06-22 18:50:09 +1000202_xfs_buf_alloc(
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000203 struct xfs_buftarg *target,
Dave Chinner3e85c862012-06-22 18:50:09 +1000204 struct xfs_buf_map *map,
205 int nmaps,
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800206 xfs_buf_flags_t flags,
207 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000209 struct xfs_buf *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000210 int error;
211 int i;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000212
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800213 *bpp = NULL;
Dave Chinneraa5c1582012-04-23 15:58:56 +1000214 bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000215 if (unlikely(!bp))
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800216 return -ENOMEM;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 /*
Dave Chinner12bcb3f2012-04-23 15:59:05 +1000219 * We don't want certain flags to appear in b_flags unless they are
220 * specifically set by later operations on the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 */
Dave Chinner611c9942012-04-23 15:59:07 +1000222 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Nathan Scottce8e9222006-01-11 15:39:08 +1100224 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100225 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000226 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100227 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100228 INIT_LIST_HEAD(&bp->b_list);
Carlos Maiolino643c8c02018-01-24 13:38:49 -0800229 INIT_LIST_HEAD(&bp->b_li_list);
Thomas Gleixnera731cd112010-09-07 14:33:15 +0000230 sema_init(&bp->b_sema, 0); /* held, no waiters */
Dave Chinnera4082352013-08-28 10:18:06 +1000231 spin_lock_init(&bp->b_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100232 bp->b_target = target;
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700233 bp->b_mount = target->bt_mount;
Dave Chinner3e85c862012-06-22 18:50:09 +1000234 bp->b_flags = flags;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000237 * Set length and io_length to the same value initially.
238 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * most cases but may be reset (e.g. XFS recovery).
240 */
Dave Chinner3e85c862012-06-22 18:50:09 +1000241 error = xfs_buf_get_maps(bp, nmaps);
242 if (error) {
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800243 kmem_cache_free(xfs_buf_zone, bp);
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800244 return error;
Dave Chinner3e85c862012-06-22 18:50:09 +1000245 }
246
247 bp->b_bn = map[0].bm_bn;
248 bp->b_length = 0;
249 for (i = 0; i < nmaps; i++) {
250 bp->b_maps[i].bm_bn = map[i].bm_bn;
251 bp->b_maps[i].bm_len = map[i].bm_len;
252 bp->b_length += map[i].bm_len;
253 }
Dave Chinner3e85c862012-06-22 18:50:09 +1000254
Nathan Scottce8e9222006-01-11 15:39:08 +1100255 atomic_set(&bp->b_pin_count, 0);
256 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700258 XFS_STATS_INC(bp->b_mount, xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000259 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000260
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800261 *bpp = bp;
262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100266 * Allocate a page array capable of holding a specified number
267 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
269STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100270_xfs_buf_get_pages(
271 xfs_buf_t *bp,
Eric Sandeen87937bf2014-04-14 19:01:20 +1000272 int page_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100275 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100276 bp->b_page_count = page_count;
277 if (page_count <= XB_PAGES) {
278 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100280 bp->b_pages = kmem_alloc(sizeof(struct page *) *
Dave Chinneraa5c1582012-04-23 15:58:56 +1000281 page_count, KM_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +1100282 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return -ENOMEM;
284 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100285 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 return 0;
288}
289
290/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100291 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
293STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100294_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 xfs_buf_t *bp)
296{
Nathan Scottce8e9222006-01-11 15:39:08 +1100297 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000298 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000299 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301}
302
303/*
304 * Releases the specified buffer.
305 *
306 * The modification state of any associated pages is left unchanged.
Zhi Yong Wub46fe822013-08-07 10:10:59 +0000307 * The buffer must not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * hashed and refcounted buffers
309 */
Christoph Hellwig25a40952019-10-24 22:25:37 -0700310static void
Nathan Scottce8e9222006-01-11 15:39:08 +1100311xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 xfs_buf_t *bp)
313{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000314 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Dave Chinner430cbeb2010-12-02 16:30:55 +1100316 ASSERT(list_empty(&bp->b_lru));
317
Dave Chinner0e6e8472011-03-26 09:16:45 +1100318 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 uint i;
320
James Bottomley73c77e22010-01-25 11:42:24 -0600321 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000322 vm_unmap_ram(bp->b_addr - bp->b_offset,
323 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Nathan Scott948ecdb2006-09-28 11:03:13 +1000325 for (i = 0; i < bp->b_page_count; i++) {
326 struct page *page = bp->b_pages[i];
327
Dave Chinner0e6e8472011-03-26 09:16:45 +1100328 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000329 }
Dave Chinner12eba652020-03-24 20:10:28 -0700330 if (current->reclaim_state)
331 current->reclaim_state->reclaimed_slab +=
332 bp->b_page_count;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100333 } else if (bp->b_flags & _XBF_KMEM)
334 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000335 _xfs_buf_free_pages(bp);
Dave Chinner3e85c862012-06-22 18:50:09 +1000336 xfs_buf_free_maps(bp);
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800337 kmem_cache_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100341 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 */
343STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100344xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 xfs_buf_t *bp,
346 uint flags)
347{
Dave Chinneraa0e8832012-04-23 15:58:52 +1000348 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100350 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 unsigned short page_count, i;
Dave Chinner795cac72012-04-23 15:58:53 +1000352 xfs_off_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 int error;
Bill O'Donnell3219e8c2019-10-04 16:38:44 -0700354 xfs_km_flags_t kmflag_mask = 0;
355
356 /*
357 * assure zeroed buffer for non-read cases.
358 */
359 if (!(flags & XBF_READ)) {
360 kmflag_mask |= KM_ZERO;
361 gfp_mask |= __GFP_ZERO;
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Dave Chinner0e6e8472011-03-26 09:16:45 +1100364 /*
365 * for buffers that are contained within a single page, just allocate
366 * the memory from the heap - there's no need for the complexity of
367 * page arrays to keep allocation down to order 0.
368 */
Dave Chinner795cac72012-04-23 15:58:53 +1000369 size = BBTOB(bp->b_length);
370 if (size < PAGE_SIZE) {
Dave Chinnerf8f9ee42019-08-26 12:08:39 -0700371 int align_mask = xfs_buftarg_dma_alignment(bp->b_target);
Bill O'Donnell3219e8c2019-10-04 16:38:44 -0700372 bp->b_addr = kmem_alloc_io(size, align_mask,
373 KM_NOFS | kmflag_mask);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100374 if (!bp->b_addr) {
375 /* low memory - use alloc_page loop instead */
376 goto use_alloc_page;
377 }
378
Dave Chinner795cac72012-04-23 15:58:53 +1000379 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100380 ((unsigned long)bp->b_addr & PAGE_MASK)) {
381 /* b_addr spans two pages - use alloc_page instead */
382 kmem_free(bp->b_addr);
383 bp->b_addr = NULL;
384 goto use_alloc_page;
385 }
386 bp->b_offset = offset_in_page(bp->b_addr);
387 bp->b_pages = bp->b_page_array;
Dave Chinnerf8f9ee42019-08-26 12:08:39 -0700388 bp->b_pages[0] = kmem_to_page(bp->b_addr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100389 bp->b_page_count = 1;
Dave Chinner611c9942012-04-23 15:59:07 +1000390 bp->b_flags |= _XBF_KMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100391 return 0;
392 }
393
394use_alloc_page:
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600395 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
396 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000397 >> PAGE_SHIFT;
Dave Chinner795cac72012-04-23 15:58:53 +1000398 page_count = end - start;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000399 error = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (unlikely(error))
401 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Nathan Scottce8e9222006-01-11 15:39:08 +1100403 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100404 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Nathan Scottce8e9222006-01-11 15:39:08 +1100406 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct page *page;
408 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100409retry:
410 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100412 if (flags & XBF_READ_AHEAD) {
413 bp->b_page_count = i;
Dave Chinner24513372014-06-25 14:58:08 +1000414 error = -ENOMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100415 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
418 /*
419 * This could deadlock.
420 *
421 * But until all the XFS lowlevel code is revamped to
422 * handle buffer allocation failures we can't do much.
423 */
424 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100425 xfs_err(NULL,
Tetsuo Handa5bf97b12015-10-12 15:41:29 +1100426 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
427 current->comm, current->pid,
Harvey Harrison34a622b2008-04-10 12:19:21 +1000428 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700430 XFS_STATS_INC(bp->b_mount, xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200431 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 goto retry;
433 }
434
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700435 XFS_STATS_INC(bp->b_mount, xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Dave Chinner0e6e8472011-03-26 09:16:45 +1100437 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100439 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 offset = 0;
441 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Dave Chinner0e6e8472011-03-26 09:16:45 +1100444out_free_pages:
445 for (i = 0; i < bp->b_page_count; i++)
446 __free_page(bp->b_pages[i]);
Darrick J. Wong2aa6ba7b2017-01-25 20:24:57 -0800447 bp->b_flags &= ~_XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return error;
449}
450
451/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300452 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
454STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100455_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 xfs_buf_t *bp,
457 uint flags)
458{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100459 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100460 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100461 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100462 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
Dave Chinner611c9942012-04-23 15:59:07 +1000463 } else if (flags & XBF_UNMAPPED) {
464 bp->b_addr = NULL;
465 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100466 int retried = 0;
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700467 unsigned nofs_flag;
Dave Chinnera19fb382011-03-26 09:13:42 +1100468
Dave Chinnerae687e52014-03-07 16:19:14 +1100469 /*
Joe Perchescf085a12019-11-07 13:24:52 -0800470 * vm_map_ram() will allocate auxiliary structures (e.g.
Dave Chinnerae687e52014-03-07 16:19:14 +1100471 * pagetables) with GFP_KERNEL, yet we are likely to be under
472 * GFP_NOFS context here. Hence we need to tell memory reclaim
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700473 * that we are in such a context via PF_MEMALLOC_NOFS to prevent
Dave Chinnerae687e52014-03-07 16:19:14 +1100474 * memory reclaim re-entering the filesystem here and
475 * potentially deadlocking.
476 */
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700477 nofs_flag = memalloc_nofs_save();
Dave Chinnera19fb382011-03-26 09:13:42 +1100478 do {
479 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
480 -1, PAGE_KERNEL);
481 if (bp->b_addr)
482 break;
483 vm_unmap_aliases();
484 } while (retried++ <= 1);
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700485 memalloc_nofs_restore(nofs_flag);
Dave Chinnera19fb382011-03-26 09:13:42 +1100486
487 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100489 bp->b_addr += bp->b_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491
492 return 0;
493}
494
495/*
496 * Finding and Reading Buffers
497 */
Lucas Stach6031e732016-12-07 17:36:36 +1100498static int
499_xfs_buf_obj_cmp(
500 struct rhashtable_compare_arg *arg,
501 const void *obj)
502{
503 const struct xfs_buf_map *map = arg->key;
504 const struct xfs_buf *bp = obj;
505
506 /*
507 * The key hashing in the lookup path depends on the key being the
508 * first element of the compare_arg, make sure to assert this.
509 */
510 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
511
512 if (bp->b_bn != map->bm_bn)
513 return 1;
514
515 if (unlikely(bp->b_length != map->bm_len)) {
516 /*
517 * found a block number match. If the range doesn't
518 * match, the only way this is allowed is if the buffer
519 * in the cache is stale and the transaction that made
520 * it stale has not yet committed. i.e. we are
521 * reallocating a busy extent. Skip this buffer and
522 * continue searching for an exact match.
523 */
524 ASSERT(bp->b_flags & XBF_STALE);
525 return 1;
526 }
527 return 0;
528}
529
530static const struct rhashtable_params xfs_buf_hash_params = {
531 .min_size = 32, /* empty AGs have minimal footprint */
532 .nelem_hint = 16,
533 .key_len = sizeof(xfs_daddr_t),
534 .key_offset = offsetof(struct xfs_buf, b_bn),
535 .head_offset = offsetof(struct xfs_buf, b_rhash_head),
536 .automatic_shrinking = true,
537 .obj_cmpfn = _xfs_buf_obj_cmp,
538};
539
540int
541xfs_buf_hash_init(
542 struct xfs_perag *pag)
543{
544 spin_lock_init(&pag->pag_buf_lock);
545 return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
546}
547
548void
549xfs_buf_hash_destroy(
550 struct xfs_perag *pag)
551{
552 rhashtable_destroy(&pag->pag_buf_hash);
553}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555/*
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700556 * Look up a buffer in the buffer cache and return it referenced and locked
557 * in @found_bp.
558 *
559 * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the
560 * cache.
561 *
562 * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return
563 * -EAGAIN if we fail to lock it.
564 *
565 * Return values are:
566 * -EFSCORRUPTED if have been supplied with an invalid address
567 * -EAGAIN on trylock failure
568 * -ENOENT if we fail to find a match and @new_bp was NULL
569 * 0, with @found_bp:
570 * - @new_bp if we inserted it into the cache
571 * - the buffer we found and locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700573static int
574xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000575 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000576 struct xfs_buf_map *map,
577 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100578 xfs_buf_flags_t flags,
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700579 struct xfs_buf *new_bp,
580 struct xfs_buf **found_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Dave Chinner74f75a02010-09-24 19:59:04 +1000582 struct xfs_perag *pag;
Dave Chinner74f75a02010-09-24 19:59:04 +1000583 xfs_buf_t *bp;
Lucas Stach6031e732016-12-07 17:36:36 +1100584 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
Dave Chinner10616b82013-01-21 23:53:52 +1100585 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000586 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700588 *found_bp = NULL;
589
Dave Chinner3e85c862012-06-22 18:50:09 +1000590 for (i = 0; i < nmaps; i++)
Lucas Stach6031e732016-12-07 17:36:36 +1100591 cmap.bm_len += map[i].bm_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /* Check for IOs smaller than the sector size / not sector aligned */
Lucas Stach6031e732016-12-07 17:36:36 +1100594 ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
595 ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Dave Chinner10616b82013-01-21 23:53:52 +1100597 /*
598 * Corrupted block numbers can get through to here, unfortunately, so we
599 * have to check that the buffer falls within the filesystem bounds.
600 */
601 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
Lucas Stach6031e732016-12-07 17:36:36 +1100602 if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
Dave Chinner10616b82013-01-21 23:53:52 +1100603 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -0800604 "%s: daddr 0x%llx out of range, EOFS 0x%llx",
Lucas Stach6031e732016-12-07 17:36:36 +1100605 __func__, cmap.bm_bn, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000606 WARN_ON(1);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700607 return -EFSCORRUPTED;
Dave Chinner10616b82013-01-21 23:53:52 +1100608 }
609
Dave Chinner74f75a02010-09-24 19:59:04 +1000610 pag = xfs_perag_get(btp->bt_mount,
Lucas Stach6031e732016-12-07 17:36:36 +1100611 xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Dave Chinner74f75a02010-09-24 19:59:04 +1000613 spin_lock(&pag->pag_buf_lock);
Lucas Stach6031e732016-12-07 17:36:36 +1100614 bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
615 xfs_buf_hash_params);
616 if (bp) {
617 atomic_inc(&bp->b_hold);
618 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
621 /* No match found */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700622 if (!new_bp) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100623 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000624 spin_unlock(&pag->pag_buf_lock);
625 xfs_perag_put(pag);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700626 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700628
629 /* the buffer keeps the perag reference until it is freed */
630 new_bp->b_pag = pag;
631 rhashtable_insert_fast(&pag->pag_buf_hash, &new_bp->b_rhash_head,
632 xfs_buf_hash_params);
633 spin_unlock(&pag->pag_buf_lock);
634 *found_bp = new_bp;
635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000638 spin_unlock(&pag->pag_buf_lock);
639 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200641 if (!xfs_buf_trylock(bp)) {
642 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100643 xfs_buf_rele(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100644 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700645 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200647 xfs_buf_lock(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100648 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650
Dave Chinner0e6e8472011-03-26 09:16:45 +1100651 /*
652 * if the buffer is stale, clear all the external state associated with
653 * it. We need to keep flags such as how we allocated the buffer memory
654 * intact here.
655 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100656 if (bp->b_flags & XBF_STALE) {
657 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinnercfb02852012-11-12 22:54:19 +1100658 ASSERT(bp->b_iodone == NULL);
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
1173/*
1174 * Buffer Utility Routines
1175 */
1176
Dave Chinnere8aaba92014-10-02 09:04:22 +10001177void
1178xfs_buf_ioend(
1179 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001181 bool read = bp->b_flags & XBF_READ;
1182
1183 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001184
1185 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Dave Chinnerd5929de2013-02-27 13:25:54 +11001186
Dave Chinner61be9c52014-10-02 09:04:31 +10001187 /*
1188 * Pull in IO completion errors now. We are guaranteed to be running
1189 * single threaded, so we don't need the lock to read b_io_error.
1190 */
1191 if (!bp->b_error && bp->b_io_error)
1192 xfs_buf_ioerror(bp, bp->b_io_error);
1193
Dave Chinnere8aaba92014-10-02 09:04:22 +10001194 /* Only validate buffers that were read without errors */
1195 if (read && !bp->b_error && bp->b_ops) {
1196 ASSERT(!bp->b_iodone);
Dave Chinner1813dd62012-11-14 17:54:40 +11001197 bp->b_ops->verify_read(bp);
Dave Chinnere8aaba92014-10-02 09:04:22 +10001198 }
1199
1200 if (!bp->b_error)
1201 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Christoph Hellwig80f6c292010-08-18 05:29:11 -04001203 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001204 (*(bp->b_iodone))(bp);
1205 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 xfs_buf_relse(bp);
Dave Chinner595bff72014-10-02 09:05:14 +10001207 else
Dave Chinner1813dd62012-11-14 17:54:40 +11001208 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Dave Chinnere8aaba92014-10-02 09:04:22 +10001211static void
1212xfs_buf_ioend_work(
1213 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001215 struct xfs_buf *bp =
Brian Fosterb29c70f2014-12-04 09:43:17 +11001216 container_of(work, xfs_buf_t, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001217
Dave Chinnere8aaba92014-10-02 09:04:22 +10001218 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001221static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001222xfs_buf_ioend_async(
1223 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001225 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001226 queue_work(bp->b_mount->m_buf_workqueue, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229void
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001230__xfs_buf_ioerror(
Nathan Scottce8e9222006-01-11 15:39:08 +11001231 xfs_buf_t *bp,
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001232 int error,
1233 xfs_failaddr_t failaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Dave Chinner24513372014-06-25 14:58:08 +10001235 ASSERT(error <= 0 && error >= -1000);
1236 bp->b_error = error;
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001237 trace_xfs_buf_ioerror(bp, error, failaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Christoph Hellwig901796a2011-10-10 16:52:49 +00001240void
1241xfs_buf_ioerror_alert(
1242 struct xfs_buf *bp,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -08001243 xfs_failaddr_t func)
Christoph Hellwig901796a2011-10-10 16:52:49 +00001244{
Christoph Hellwig13b1f812020-02-21 07:34:47 -08001245 xfs_alert_ratelimited(bp->b_mount,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -08001246"metadata I/O error in \"%pS\" at daddr 0x%llx len %d error %d",
Darrick J. Wongc219b012018-01-08 11:39:18 -08001247 func, (uint64_t)XFS_BUF_ADDR(bp), bp->b_length,
1248 -bp->b_error);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001249}
1250
Brian Foster54b3b1f2020-05-06 13:25:19 -07001251/*
1252 * To simulate an I/O failure, the buffer must be locked and held with at least
1253 * three references. The LRU reference is dropped by the stale call. The buf
1254 * item reference is dropped via ioend processing. The third reference is owned
1255 * by the caller and is dropped on I/O completion if the buffer is XBF_ASYNC.
1256 */
1257void
1258xfs_buf_ioend_fail(
1259 struct xfs_buf *bp)
1260{
1261 bp->b_flags &= ~XBF_DONE;
1262 xfs_buf_stale(bp);
1263 xfs_buf_ioerror(bp, -EIO);
1264 xfs_buf_ioend(bp);
1265}
1266
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001267int
1268xfs_bwrite(
1269 struct xfs_buf *bp)
1270{
1271 int error;
1272
1273 ASSERT(xfs_buf_islocked(bp));
1274
1275 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001276 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
1277 XBF_WRITE_FAIL | XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001278
Brian Foster6af88cd2018-07-11 22:26:35 -07001279 error = xfs_buf_submit(bp);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001280 if (error)
1281 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001282 return error;
1283}
1284
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001285static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001286xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001287 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001289 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Dave Chinner37eb17e2012-11-12 22:09:46 +11001291 /*
1292 * don't overwrite existing errors - otherwise we can lose errors on
1293 * buffers that require multiple bios to complete.
1294 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001295 if (bio->bi_status) {
1296 int error = blk_status_to_errno(bio->bi_status);
1297
1298 cmpxchg(&bp->b_io_error, 0, error);
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Dave Chinner37eb17e2012-11-12 22:09:46 +11001301 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001302 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1303
Dave Chinnere8aaba92014-10-02 09:04:22 +10001304 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1305 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
Dave Chinner3e85c862012-06-22 18:50:09 +10001309static void
1310xfs_buf_ioapply_map(
1311 struct xfs_buf *bp,
1312 int map,
1313 int *buf_offset,
1314 int *count,
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001315 int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Dave Chinner3e85c862012-06-22 18:50:09 +10001317 int page_index;
1318 int total_nr_pages = bp->b_page_count;
1319 int nr_pages;
1320 struct bio *bio;
1321 sector_t sector = bp->b_maps[map].bm_bn;
1322 int size;
1323 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Dave Chinner3e85c862012-06-22 18:50:09 +10001325 /* skip the pages in the buffer before the start offset */
1326 page_index = 0;
1327 offset = *buf_offset;
1328 while (offset >= PAGE_SIZE) {
1329 page_index++;
1330 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001331 }
1332
Dave Chinner3e85c862012-06-22 18:50:09 +10001333 /*
1334 * Limit the IO size to the length of the current vector, and update the
1335 * remaining IO count for the next time around.
1336 */
1337 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1338 *count -= size;
1339 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001342 atomic_inc(&bp->b_io_remaining);
Ming Leic908e382016-05-30 21:34:33 +08001343 nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 bio = bio_alloc(GFP_NOIO, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001346 bio_set_dev(bio, bp->b_target->bt_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001347 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001348 bio->bi_end_io = xfs_buf_bio_end_io;
1349 bio->bi_private = bp;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001350 bio->bi_opf = op;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001351
Dave Chinner3e85c862012-06-22 18:50:09 +10001352 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001353 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 if (nbytes > size)
1356 nbytes = size;
1357
Dave Chinner3e85c862012-06-22 18:50:09 +10001358 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1359 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001360 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 break;
1362
1363 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001364 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 size -= nbytes;
1366 total_nr_pages--;
1367 }
1368
Kent Overstreet4f024f32013-10-11 15:44:27 -07001369 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001370 if (xfs_buf_is_vmapped(bp)) {
1371 flush_kernel_vmap_range(bp->b_addr,
1372 xfs_buf_vmap_len(bp));
1373 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001374 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (size)
1376 goto next_chunk;
1377 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001378 /*
1379 * This is guaranteed not to be the last io reference count
Dave Chinner595bff72014-10-02 09:05:14 +10001380 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001381 */
1382 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001383 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001384 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001386
1387}
1388
1389STATIC void
1390_xfs_buf_ioapply(
1391 struct xfs_buf *bp)
1392{
1393 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001394 int op;
Dave Chinner3e85c862012-06-22 18:50:09 +10001395 int offset;
1396 int size;
1397 int i;
1398
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001399 /*
1400 * Make sure we capture only current IO errors rather than stale errors
1401 * left over from previous use of the buffer (e.g. failed readahead).
1402 */
1403 bp->b_error = 0;
1404
Dave Chinner3e85c862012-06-22 18:50:09 +10001405 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001406 op = REQ_OP_WRITE;
Dave Chinner1813dd62012-11-14 17:54:40 +11001407
1408 /*
1409 * Run the write verifier callback function if it exists. If
1410 * this function fails it will mark the buffer with an error and
1411 * the IO should not be dispatched.
1412 */
1413 if (bp->b_ops) {
1414 bp->b_ops->verify_write(bp);
1415 if (bp->b_error) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001416 xfs_force_shutdown(bp->b_mount,
Dave Chinner1813dd62012-11-14 17:54:40 +11001417 SHUTDOWN_CORRUPT_INCORE);
1418 return;
1419 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001420 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001421 struct xfs_mount *mp = bp->b_mount;
Dave Chinner400b9d82014-08-04 12:42:40 +10001422
1423 /*
1424 * non-crc filesystems don't attach verifiers during
1425 * log recovery, so don't warn for such filesystems.
1426 */
1427 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1428 xfs_warn(mp,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001429 "%s: no buf ops on daddr 0x%llx len %d",
Dave Chinner400b9d82014-08-04 12:42:40 +10001430 __func__, bp->b_bn, bp->b_length);
Darrick J. Wong9c712a12018-01-08 10:51:26 -08001431 xfs_hex_dump(bp->b_addr,
1432 XFS_CORRUPTION_DUMP_LEN);
Dave Chinner400b9d82014-08-04 12:42:40 +10001433 dump_stack();
1434 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001435 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001436 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001437 op = REQ_OP_READ;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001438 if (bp->b_flags & XBF_READ_AHEAD)
1439 op |= REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001440 }
1441
1442 /* we only use the buffer cache for meta-data */
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001443 op |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001444
1445 /*
1446 * Walk all the vectors issuing IO on them. Set up the initial offset
1447 * into the buffer and the desired IO size before we start -
1448 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1449 * subsequent call.
1450 */
1451 offset = bp->b_offset;
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001452 size = BBTOB(bp->b_length);
Dave Chinner3e85c862012-06-22 18:50:09 +10001453 blk_start_plug(&plug);
1454 for (i = 0; i < bp->b_map_count; i++) {
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001455 xfs_buf_ioapply_map(bp, i, &offset, &size, op);
Dave Chinner3e85c862012-06-22 18:50:09 +10001456 if (bp->b_error)
1457 break;
1458 if (size <= 0)
1459 break; /* all done */
1460 }
1461 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Dave Chinner595bff72014-10-02 09:05:14 +10001464/*
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001465 * Wait for I/O completion of a sync buffer and return the I/O error code.
Dave Chinner595bff72014-10-02 09:05:14 +10001466 */
Brian Fostereaebb512018-07-11 22:26:34 -07001467static int
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001468xfs_buf_iowait(
Dave Chinner595bff72014-10-02 09:05:14 +10001469 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001471 ASSERT(!(bp->b_flags & XBF_ASYNC));
1472
1473 trace_xfs_buf_iowait(bp, _RET_IP_);
1474 wait_for_completion(&bp->b_iowait);
1475 trace_xfs_buf_iowait_done(bp, _RET_IP_);
1476
1477 return bp->b_error;
1478}
1479
1480/*
1481 * Buffer I/O submission path, read or write. Asynchronous submission transfers
1482 * the buffer lock ownership and the current reference to the IO. It is not
1483 * safe to reference the buffer after a call to this function unless the caller
1484 * holds an additional reference itself.
1485 */
1486int
1487__xfs_buf_submit(
1488 struct xfs_buf *bp,
1489 bool wait)
1490{
1491 int error = 0;
1492
Dave Chinner595bff72014-10-02 09:05:14 +10001493 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001495 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff72014-10-02 09:05:14 +10001496
1497 /* on shutdown we stale and complete the buffer immediately */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001498 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
Brian Foster54b3b1f2020-05-06 13:25:19 -07001499 xfs_buf_ioend_fail(bp);
Brian Fostereaebb512018-07-11 22:26:34 -07001500 return -EIO;
Dave Chinner595bff72014-10-02 09:05:14 +10001501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001503 /*
1504 * Grab a reference so the buffer does not go away underneath us. For
1505 * async buffers, I/O completion drops the callers reference, which
1506 * could occur before submission returns.
1507 */
1508 xfs_buf_hold(bp);
1509
Christoph Hellwig375ec692011-08-23 08:28:03 +00001510 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001511 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Dave Chinner61be9c52014-10-02 09:04:31 +10001513 /* clear the internal error state to avoid spurious errors */
1514 bp->b_io_error = 0;
1515
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001516 /*
Brian Fostereaebb512018-07-11 22:26:34 -07001517 * Set the count to 1 initially, this will stop an I/O completion
1518 * callout which happens before we have started all the I/O from calling
1519 * xfs_buf_ioend too early.
1520 */
1521 atomic_set(&bp->b_io_remaining, 1);
1522 if (bp->b_flags & XBF_ASYNC)
1523 xfs_buf_ioacct_inc(bp);
1524 _xfs_buf_ioapply(bp);
1525
1526 /*
1527 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1528 * reference we took above. If we drop it to zero, run completion so
1529 * that we don't return to the caller with completion still pending.
1530 */
1531 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1532 if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
1533 xfs_buf_ioend(bp);
1534 else
1535 xfs_buf_ioend_async(bp);
1536 }
1537
Brian Foster6af88cd2018-07-11 22:26:35 -07001538 if (wait)
1539 error = xfs_buf_iowait(bp);
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001540
Dave Chinner595bff72014-10-02 09:05:14 +10001541 /*
Brian Foster6af88cd2018-07-11 22:26:35 -07001542 * Release the hold that keeps the buffer referenced for the entire
1543 * I/O. Note that if the buffer is async, it is not safe to reference
1544 * after this release.
Dave Chinner595bff72014-10-02 09:05:14 +10001545 */
1546 xfs_buf_rele(bp);
1547 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001550void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001551xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001552 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 size_t offset)
1554{
1555 struct page *page;
1556
Dave Chinner611c9942012-04-23 15:59:07 +10001557 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001558 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Nathan Scottce8e9222006-01-11 15:39:08 +11001560 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001561 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001562 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565void
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001566xfs_buf_zero(
1567 struct xfs_buf *bp,
1568 size_t boff,
1569 size_t bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Dave Chinner795cac72012-04-23 15:58:53 +10001571 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 bend = boff + bsize;
1574 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001575 struct page *page;
1576 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Dave Chinner795cac72012-04-23 15:58:53 +10001578 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1579 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1580 page = bp->b_pages[page_index];
1581 csize = min_t(size_t, PAGE_SIZE - page_offset,
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001582 BBTOB(bp->b_length) - boff);
Dave Chinner795cac72012-04-23 15:58:53 +10001583
1584 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001586 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 boff += csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
1590}
1591
1592/*
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001593 * Log a message about and stale a buffer that a caller has decided is corrupt.
1594 *
1595 * This function should be called for the kinds of metadata corruption that
1596 * cannot be detect from a verifier, such as incorrect inter-block relationship
1597 * data. Do /not/ call this function from a verifier function.
1598 *
1599 * The buffer must be XBF_DONE prior to the call. Afterwards, the buffer will
1600 * be marked stale, but b_error will not be set. The caller is responsible for
1601 * releasing the buffer or fixing it.
1602 */
1603void
1604__xfs_buf_mark_corrupt(
1605 struct xfs_buf *bp,
1606 xfs_failaddr_t fa)
1607{
1608 ASSERT(bp->b_flags & XBF_DONE);
1609
Darrick J. Wonge83cf872020-03-11 10:37:54 -07001610 xfs_buf_corruption_error(bp, fa);
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001611 xfs_buf_stale(bp);
1612}
1613
1614/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001615 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 */
1617
1618/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001619 * Wait for any bufs with callbacks that have been submitted but have not yet
1620 * returned. These buffers will have an elevated hold count, so wait on those
1621 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001623static enum lru_status
1624xfs_buftarg_wait_rele(
1625 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001626 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001627 spinlock_t *lru_lock,
1628 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Dave Chinnere80dfa12013-08-28 10:18:05 +10001630{
1631 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001632 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001633
1634 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001635 /* need to wait, so skip it this pass */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001636 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001637 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
Dave Chinnera4082352013-08-28 10:18:06 +10001639 if (!spin_trylock(&bp->b_lock))
1640 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001641
Dave Chinnera4082352013-08-28 10:18:06 +10001642 /*
1643 * clear the LRU reference count so the buffer doesn't get
1644 * ignored in xfs_buf_rele().
1645 */
1646 atomic_set(&bp->b_lru_ref, 0);
1647 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001648 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001649 spin_unlock(&bp->b_lock);
1650 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
Dave Chinnere80dfa12013-08-28 10:18:05 +10001653void
1654xfs_wait_buftarg(
1655 struct xfs_buftarg *btp)
1656{
Dave Chinnera4082352013-08-28 10:18:06 +10001657 LIST_HEAD(dispose);
1658 int loop = 0;
1659
Dave Chinner85bec542016-01-19 08:28:10 +11001660 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001661 * First wait on the buftarg I/O count for all in-flight buffers to be
1662 * released. This is critical as new buffers do not make the LRU until
1663 * they are released.
1664 *
1665 * Next, flush the buffer workqueue to ensure all completion processing
1666 * has finished. Just waiting on buffer locks is not sufficient for
1667 * async IO as the reference count held over IO is not released until
1668 * after the buffer lock is dropped. Hence we need to ensure here that
1669 * all reference counts have been dropped before we start walking the
1670 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001671 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001672 while (percpu_counter_sum(&btp->bt_io_count))
1673 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001674 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Dave Chinner85bec542016-01-19 08:28:10 +11001675
Dave Chinnera4082352013-08-28 10:18:06 +10001676 /* loop until there is nothing left on the lru list. */
1677 while (list_lru_count(&btp->bt_lru)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001678 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001679 &dispose, LONG_MAX);
1680
1681 while (!list_empty(&dispose)) {
1682 struct xfs_buf *bp;
1683 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1684 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001685 if (bp->b_flags & XBF_WRITE_FAIL) {
1686 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001687"Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001688 (long long)bp->b_bn);
Joe Perchesf41febd2015-07-29 11:52:04 +10001689 xfs_alert(btp->bt_mount,
1690"Please run xfs_repair to determine the extent of the problem.");
Dave Chinnerac8809f2013-12-12 16:34:38 +11001691 }
Dave Chinnera4082352013-08-28 10:18:06 +10001692 xfs_buf_rele(bp);
1693 }
1694 if (loop++ != 0)
1695 delay(100);
1696 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001697}
1698
1699static enum lru_status
1700xfs_buftarg_isolate(
1701 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001702 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001703 spinlock_t *lru_lock,
1704 void *arg)
1705{
1706 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1707 struct list_head *dispose = arg;
1708
1709 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001710 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1711 * If we fail to get the lock, just skip it.
1712 */
1713 if (!spin_trylock(&bp->b_lock))
1714 return LRU_SKIP;
1715 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001716 * Decrement the b_lru_ref count unless the value is already
1717 * zero. If the value is already zero, we need to reclaim the
1718 * buffer, otherwise it gets another trip through the LRU.
1719 */
Vratislav Bendel19957a12018-03-06 17:07:44 -08001720 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
Dave Chinnera4082352013-08-28 10:18:06 +10001721 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001722 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001723 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001724
Dave Chinnera4082352013-08-28 10:18:06 +10001725 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001726 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001727 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001728 return LRU_REMOVED;
1729}
1730
Andrew Mortonaddbda42013-08-28 10:18:06 +10001731static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001732xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001733 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001734 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001735{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001736 struct xfs_buftarg *btp = container_of(shrink,
1737 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001738 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001739 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001740
Vladimir Davydov503c3582015-02-12 14:58:47 -08001741 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1742 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001743
1744 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001745 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001746 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1747 list_del_init(&bp->b_lru);
1748 xfs_buf_rele(bp);
1749 }
1750
Dave Chinnere80dfa12013-08-28 10:18:05 +10001751 return freed;
1752}
1753
Andrew Mortonaddbda42013-08-28 10:18:06 +10001754static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001755xfs_buftarg_shrink_count(
1756 struct shrinker *shrink,
1757 struct shrink_control *sc)
1758{
1759 struct xfs_buftarg *btp = container_of(shrink,
1760 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001761 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001762}
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764void
1765xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001766 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001768 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001769 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1770 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001771 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001772
Dave Chinner2291dab2016-12-09 16:49:54 +11001773 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001774
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001775 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001778int
1779xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001781 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001783 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001784 btp->bt_meta_sectorsize = sectorsize;
1785 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Nathan Scottce8e9222006-01-11 15:39:08 +11001787 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001788 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001789 "Cannot set_blocksize to %u on device %pg",
1790 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001791 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001794 /* Set up device logical sector size mask */
1795 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1796 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return 0;
1799}
1800
1801/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001802 * When allocating the initial buffer target we have not yet
1803 * read in the superblock, so don't know what sized sectors
1804 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001805 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806STATIC int
1807xfs_setsize_buftarg_early(
1808 xfs_buftarg_t *btp,
1809 struct block_device *bdev)
1810{
Eric Sandeena96c4152014-04-14 19:00:29 +10001811 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814xfs_buftarg_t *
1815xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001816 struct xfs_mount *mp,
Dan Williams486aff52017-08-24 15:12:50 -07001817 struct block_device *bdev,
1818 struct dax_device *dax_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 xfs_buftarg_t *btp;
1821
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07001822 btp = kmem_zalloc(sizeof(*btp), KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Dave Chinnerebad8612010-09-22 10:47:20 +10001824 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001825 btp->bt_dev = bdev->bd_dev;
1826 btp->bt_bdev = bdev;
Dan Williams486aff52017-08-24 15:12:50 -07001827 btp->bt_daxdev = dax_dev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (xfs_setsize_buftarg_early(btp, bdev))
Michal Hockod210a982017-11-23 17:13:40 +01001830 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001831
1832 if (list_lru_init(&btp->bt_lru))
Michal Hockod210a982017-11-23 17:13:40 +01001833 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001834
Brian Foster9c7504a2016-07-20 11:15:28 +10001835 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
Michal Hockod210a982017-11-23 17:13:40 +01001836 goto error_lru;
Brian Foster9c7504a2016-07-20 11:15:28 +10001837
Dave Chinnere80dfa12013-08-28 10:18:05 +10001838 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1839 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001840 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001841 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Michal Hockod210a982017-11-23 17:13:40 +01001842 if (register_shrinker(&btp->bt_shrinker))
1843 goto error_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return btp;
1845
Michal Hockod210a982017-11-23 17:13:40 +01001846error_pcpu:
1847 percpu_counter_destroy(&btp->bt_io_count);
1848error_lru:
1849 list_lru_destroy(&btp->bt_lru);
1850error_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001851 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return NULL;
1853}
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855/*
Brian Foster20e8a062017-04-21 12:40:44 -07001856 * Cancel a delayed write list.
1857 *
1858 * Remove each buffer from the list, clear the delwri queue flag and drop the
1859 * associated buffer reference.
1860 */
1861void
1862xfs_buf_delwri_cancel(
1863 struct list_head *list)
1864{
1865 struct xfs_buf *bp;
1866
1867 while (!list_empty(list)) {
1868 bp = list_first_entry(list, struct xfs_buf, b_list);
1869
1870 xfs_buf_lock(bp);
1871 bp->b_flags &= ~_XBF_DELWRI_Q;
1872 list_del_init(&bp->b_list);
1873 xfs_buf_relse(bp);
1874 }
1875}
1876
1877/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001878 * Add a buffer to the delayed write list.
1879 *
1880 * This queues a buffer for writeout if it hasn't already been. Note that
1881 * neither this routine nor the buffer list submission functions perform
1882 * any internal synchronization. It is expected that the lists are thread-local
1883 * to the callers.
1884 *
1885 * Returns true if we queued up the buffer, or false if it already had
1886 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001888bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001889xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001890 struct xfs_buf *bp,
1891 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001893 ASSERT(xfs_buf_islocked(bp));
1894 ASSERT(!(bp->b_flags & XBF_READ));
1895
1896 /*
1897 * If the buffer is already marked delwri it already is queued up
1898 * by someone else for imediate writeout. Just ignore it in that
1899 * case.
1900 */
1901 if (bp->b_flags & _XBF_DELWRI_Q) {
1902 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1903 return false;
1904 }
David Chinnera6867a62006-01-11 15:37:58 +11001905
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001906 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1907
Dave Chinnerd808f612010-02-02 10:13:42 +11001908 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001909 * If a buffer gets written out synchronously or marked stale while it
1910 * is on a delwri list we lazily remove it. To do this, the other party
1911 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1912 * It remains referenced and on the list. In a rare corner case it
1913 * might get readded to a delwri list after the synchronous writeout, in
1914 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001915 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001916 bp->b_flags |= _XBF_DELWRI_Q;
1917 if (list_empty(&bp->b_list)) {
1918 atomic_inc(&bp->b_hold);
1919 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001920 }
David Chinner585e6d82007-02-10 18:32:29 +11001921
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001922 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001923}
1924
Dave Chinner089716a2010-01-26 15:13:25 +11001925/*
1926 * Compare function is more complex than it needs to be because
1927 * the return value is only 32 bits and we are doing comparisons
1928 * on 64 bit values
1929 */
1930static int
1931xfs_buf_cmp(
1932 void *priv,
1933 struct list_head *a,
1934 struct list_head *b)
1935{
1936 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1937 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1938 xfs_daddr_t diff;
1939
Mark Tinguelyf4b42422012-12-04 17:18:02 -06001940 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11001941 if (diff < 0)
1942 return -1;
1943 if (diff > 0)
1944 return 1;
1945 return 0;
1946}
1947
Dave Chinner26f1fe82016-06-01 17:38:15 +10001948/*
Brian Fostere339dd82018-07-11 22:26:34 -07001949 * Submit buffers for write. If wait_list is specified, the buffers are
1950 * submitted using sync I/O and placed on the wait list such that the caller can
1951 * iowait each buffer. Otherwise async I/O is used and the buffers are released
1952 * at I/O completion time. In either case, buffers remain locked until I/O
1953 * completes and the buffer is released from the queue.
Dave Chinner26f1fe82016-06-01 17:38:15 +10001954 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001955static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10001956xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001957 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10001958 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001960 struct xfs_buf *bp, *n;
1961 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001962 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Dave Chinner26f1fe82016-06-01 17:38:15 +10001964 list_sort(NULL, buffer_list, xfs_buf_cmp);
1965
1966 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001967 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10001968 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001969 if (xfs_buf_ispinned(bp)) {
1970 pinned++;
1971 continue;
1972 }
1973 if (!xfs_buf_trylock(bp))
1974 continue;
1975 } else {
1976 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001979 /*
1980 * Someone else might have written the buffer synchronously or
1981 * marked it stale in the meantime. In that case only the
1982 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1983 * reference and remove it from the list here.
1984 */
1985 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1986 list_del_init(&bp->b_list);
1987 xfs_buf_relse(bp);
1988 continue;
1989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001991 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001992
Dave Chinnercf53e992014-10-02 09:04:01 +10001993 /*
Brian Fostere339dd82018-07-11 22:26:34 -07001994 * If we have a wait list, each buffer (and associated delwri
1995 * queue reference) transfers to it and is submitted
1996 * synchronously. Otherwise, drop the buffer from the delwri
1997 * queue and submit async.
Dave Chinnercf53e992014-10-02 09:04:01 +10001998 */
Dave Chinnerbbfeb612016-07-20 11:53:35 +10001999 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL);
Brian Fostere339dd82018-07-11 22:26:34 -07002000 bp->b_flags |= XBF_WRITE;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002001 if (wait_list) {
Brian Fostere339dd82018-07-11 22:26:34 -07002002 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002003 list_move_tail(&bp->b_list, wait_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002004 } else {
2005 bp->b_flags |= XBF_ASYNC;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002006 list_del_init(&bp->b_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002007 }
Brian Foster6af88cd2018-07-11 22:26:35 -07002008 __xfs_buf_submit(bp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00002010 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002012 return pinned;
2013}
Nathan Scottf07c2252006-09-28 10:52:15 +10002014
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002015/*
2016 * Write out a buffer list asynchronously.
2017 *
2018 * This will take the @buffer_list, write all non-locked and non-pinned buffers
2019 * out and not wait for I/O completion on any of the buffers. This interface
2020 * is only safely useable for callers that can track I/O completion by higher
2021 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
2022 * function.
Brian Fosterefc32892018-10-18 17:21:49 +11002023 *
2024 * Note: this function will skip buffers it would block on, and in doing so
2025 * leaves them on @buffer_list so they can be retried on a later pass. As such,
2026 * it is up to the caller to ensure that the buffer list is fully submitted or
2027 * cancelled appropriately when they are finished with the list. Failure to
2028 * cancel or resubmit the list until it is empty will result in leaked buffers
2029 * at unmount time.
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002030 */
2031int
2032xfs_buf_delwri_submit_nowait(
2033 struct list_head *buffer_list)
2034{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002035 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002036}
2037
2038/*
2039 * Write out a buffer list synchronously.
2040 *
2041 * This will take the @buffer_list, write all buffers out and wait for I/O
2042 * completion on all of the buffers. @buffer_list is consumed by the function,
2043 * so callers must have some other way of tracking buffers if they require such
2044 * functionality.
2045 */
2046int
2047xfs_buf_delwri_submit(
2048 struct list_head *buffer_list)
2049{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002050 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002051 int error = 0, error2;
2052 struct xfs_buf *bp;
2053
Dave Chinner26f1fe82016-06-01 17:38:15 +10002054 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002055
2056 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10002057 while (!list_empty(&wait_list)) {
2058 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002059
2060 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10002061
Brian Fostere339dd82018-07-11 22:26:34 -07002062 /*
2063 * Wait on the locked buffer, check for errors and unlock and
2064 * release the delwri queue reference.
2065 */
2066 error2 = xfs_buf_iowait(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002067 xfs_buf_relse(bp);
2068 if (!error)
2069 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
2071
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002072 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
Brian Foster7912e7f2017-06-14 21:21:45 -07002075/*
2076 * Push a single buffer on a delwri queue.
2077 *
2078 * The purpose of this function is to submit a single buffer of a delwri queue
2079 * and return with the buffer still on the original queue. The waiting delwri
2080 * buffer submission infrastructure guarantees transfer of the delwri queue
2081 * buffer reference to a temporary wait list. We reuse this infrastructure to
2082 * transfer the buffer back to the original queue.
2083 *
2084 * Note the buffer transitions from the queued state, to the submitted and wait
2085 * listed state and back to the queued state during this call. The buffer
2086 * locking and queue management logic between _delwri_pushbuf() and
2087 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2088 * before returning.
2089 */
2090int
2091xfs_buf_delwri_pushbuf(
2092 struct xfs_buf *bp,
2093 struct list_head *buffer_list)
2094{
2095 LIST_HEAD (submit_list);
2096 int error;
2097
2098 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2099
2100 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2101
2102 /*
2103 * Isolate the buffer to a new local list so we can submit it for I/O
2104 * independently from the rest of the original list.
2105 */
2106 xfs_buf_lock(bp);
2107 list_move(&bp->b_list, &submit_list);
2108 xfs_buf_unlock(bp);
2109
2110 /*
2111 * Delwri submission clears the DELWRI_Q buffer flag and returns with
Brian Fostere339dd82018-07-11 22:26:34 -07002112 * the buffer on the wait list with the original reference. Rather than
Brian Foster7912e7f2017-06-14 21:21:45 -07002113 * bounce the buffer from a local wait list back to the original list
2114 * after I/O completion, reuse the original list as the wait list.
2115 */
2116 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2117
2118 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002119 * The buffer is now locked, under I/O and wait listed on the original
2120 * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2121 * return with the buffer unlocked and on the original queue.
Brian Foster7912e7f2017-06-14 21:21:45 -07002122 */
Brian Fostere339dd82018-07-11 22:26:34 -07002123 error = xfs_buf_iowait(bp);
Brian Foster7912e7f2017-06-14 21:21:45 -07002124 bp->b_flags |= _XBF_DELWRI_Q;
2125 xfs_buf_unlock(bp);
2126
2127 return error;
2128}
2129
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002130int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002131xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Dave Chinner12eba652020-03-24 20:10:28 -07002133 xfs_buf_zone = kmem_cache_create("xfs_buf", sizeof(struct xfs_buf), 0,
2134 SLAB_HWCACHE_ALIGN |
2135 SLAB_RECLAIM_ACCOUNT |
2136 SLAB_MEM_SPREAD,
2137 NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002138 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002139 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002140
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002143 out:
Nathan Scott87582802006-03-14 13:18:19 +11002144 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147void
Nathan Scottce8e9222006-01-11 15:39:08 +11002148xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08002150 kmem_cache_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
Brian Foster7561d272017-10-17 14:16:29 -07002152
2153void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
2154{
Brian Foster7561d272017-10-17 14:16:29 -07002155 /*
2156 * Set the lru reference count to 0 based on the error injection tag.
2157 * This allows userspace to disrupt buffer caching for debug/testing
2158 * purposes.
2159 */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002160 if (XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_LRU_REF))
Brian Foster7561d272017-10-17 14:16:29 -07002161 lru_ref = 0;
2162
2163 atomic_set(&bp->b_lru_ref, lru_ref);
2164}
Brian Foster8473fee2019-02-07 10:45:46 -08002165
2166/*
2167 * Verify an on-disk magic value against the magic value specified in the
2168 * verifier structure. The verifier magic is in disk byte order so the caller is
2169 * expected to pass the value directly from disk.
2170 */
2171bool
2172xfs_verify_magic(
2173 struct xfs_buf *bp,
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002174 __be32 dmagic)
Brian Foster8473fee2019-02-07 10:45:46 -08002175{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002176 struct xfs_mount *mp = bp->b_mount;
Brian Foster8473fee2019-02-07 10:45:46 -08002177 int idx;
2178
2179 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002180 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic[idx]))
Brian Foster8473fee2019-02-07 10:45:46 -08002181 return false;
2182 return dmagic == bp->b_ops->magic[idx];
2183}
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002184/*
2185 * Verify an on-disk magic value against the magic value specified in the
2186 * verifier structure. The verifier magic is in disk byte order so the caller is
2187 * expected to pass the value directly from disk.
2188 */
2189bool
2190xfs_verify_magic16(
2191 struct xfs_buf *bp,
2192 __be16 dmagic)
2193{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002194 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002195 int idx;
2196
2197 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002198 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic16[idx]))
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002199 return false;
2200 return dmagic == bp->b_ops->magic16[idx];
2201}