blob: c805ffae8913002cb3d37dae77e69f7b8e22e97b [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 Chinner0e6e8472011-03-26 09:16:45 +1100330 } else if (bp->b_flags & _XBF_KMEM)
331 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000332 _xfs_buf_free_pages(bp);
Dave Chinner3e85c862012-06-22 18:50:09 +1000333 xfs_buf_free_maps(bp);
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800334 kmem_cache_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100338 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
340STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100341xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 xfs_buf_t *bp,
343 uint flags)
344{
Dave Chinneraa0e8832012-04-23 15:58:52 +1000345 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100347 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned short page_count, i;
Dave Chinner795cac72012-04-23 15:58:53 +1000349 xfs_off_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int error;
Bill O'Donnell3219e8c2019-10-04 16:38:44 -0700351 xfs_km_flags_t kmflag_mask = 0;
352
353 /*
354 * assure zeroed buffer for non-read cases.
355 */
356 if (!(flags & XBF_READ)) {
357 kmflag_mask |= KM_ZERO;
358 gfp_mask |= __GFP_ZERO;
359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Dave Chinner0e6e8472011-03-26 09:16:45 +1100361 /*
362 * for buffers that are contained within a single page, just allocate
363 * the memory from the heap - there's no need for the complexity of
364 * page arrays to keep allocation down to order 0.
365 */
Dave Chinner795cac72012-04-23 15:58:53 +1000366 size = BBTOB(bp->b_length);
367 if (size < PAGE_SIZE) {
Dave Chinnerf8f9ee42019-08-26 12:08:39 -0700368 int align_mask = xfs_buftarg_dma_alignment(bp->b_target);
Bill O'Donnell3219e8c2019-10-04 16:38:44 -0700369 bp->b_addr = kmem_alloc_io(size, align_mask,
370 KM_NOFS | kmflag_mask);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100371 if (!bp->b_addr) {
372 /* low memory - use alloc_page loop instead */
373 goto use_alloc_page;
374 }
375
Dave Chinner795cac72012-04-23 15:58:53 +1000376 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100377 ((unsigned long)bp->b_addr & PAGE_MASK)) {
378 /* b_addr spans two pages - use alloc_page instead */
379 kmem_free(bp->b_addr);
380 bp->b_addr = NULL;
381 goto use_alloc_page;
382 }
383 bp->b_offset = offset_in_page(bp->b_addr);
384 bp->b_pages = bp->b_page_array;
Dave Chinnerf8f9ee42019-08-26 12:08:39 -0700385 bp->b_pages[0] = kmem_to_page(bp->b_addr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100386 bp->b_page_count = 1;
Dave Chinner611c9942012-04-23 15:59:07 +1000387 bp->b_flags |= _XBF_KMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100388 return 0;
389 }
390
391use_alloc_page:
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600392 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
393 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000394 >> PAGE_SHIFT;
Dave Chinner795cac72012-04-23 15:58:53 +1000395 page_count = end - start;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000396 error = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (unlikely(error))
398 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Nathan Scottce8e9222006-01-11 15:39:08 +1100400 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100401 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Nathan Scottce8e9222006-01-11 15:39:08 +1100403 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct page *page;
405 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100406retry:
407 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100409 if (flags & XBF_READ_AHEAD) {
410 bp->b_page_count = i;
Dave Chinner24513372014-06-25 14:58:08 +1000411 error = -ENOMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100412 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 /*
416 * This could deadlock.
417 *
418 * But until all the XFS lowlevel code is revamped to
419 * handle buffer allocation failures we can't do much.
420 */
421 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100422 xfs_err(NULL,
Tetsuo Handa5bf97b12015-10-12 15:41:29 +1100423 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
424 current->comm, current->pid,
Harvey Harrison34a622b2008-04-10 12:19:21 +1000425 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700427 XFS_STATS_INC(bp->b_mount, xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200428 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 goto retry;
430 }
431
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700432 XFS_STATS_INC(bp->b_mount, xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Dave Chinner0e6e8472011-03-26 09:16:45 +1100434 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100436 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 offset = 0;
438 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Dave Chinner0e6e8472011-03-26 09:16:45 +1100441out_free_pages:
442 for (i = 0; i < bp->b_page_count; i++)
443 __free_page(bp->b_pages[i]);
Darrick J. Wong2aa6ba7b2017-01-25 20:24:57 -0800444 bp->b_flags &= ~_XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return error;
446}
447
448/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300449 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
451STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100452_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 xfs_buf_t *bp,
454 uint flags)
455{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100456 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100457 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100458 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100459 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
Dave Chinner611c9942012-04-23 15:59:07 +1000460 } else if (flags & XBF_UNMAPPED) {
461 bp->b_addr = NULL;
462 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100463 int retried = 0;
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700464 unsigned nofs_flag;
Dave Chinnera19fb382011-03-26 09:13:42 +1100465
Dave Chinnerae687e52014-03-07 16:19:14 +1100466 /*
Joe Perchescf085a12019-11-07 13:24:52 -0800467 * vm_map_ram() will allocate auxiliary structures (e.g.
Dave Chinnerae687e52014-03-07 16:19:14 +1100468 * pagetables) with GFP_KERNEL, yet we are likely to be under
469 * GFP_NOFS context here. Hence we need to tell memory reclaim
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700470 * that we are in such a context via PF_MEMALLOC_NOFS to prevent
Dave Chinnerae687e52014-03-07 16:19:14 +1100471 * memory reclaim re-entering the filesystem here and
472 * potentially deadlocking.
473 */
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700474 nofs_flag = memalloc_nofs_save();
Dave Chinnera19fb382011-03-26 09:13:42 +1100475 do {
476 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
477 -1, PAGE_KERNEL);
478 if (bp->b_addr)
479 break;
480 vm_unmap_aliases();
481 } while (retried++ <= 1);
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700482 memalloc_nofs_restore(nofs_flag);
Dave Chinnera19fb382011-03-26 09:13:42 +1100483
484 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100486 bp->b_addr += bp->b_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
489 return 0;
490}
491
492/*
493 * Finding and Reading Buffers
494 */
Lucas Stach6031e732016-12-07 17:36:36 +1100495static int
496_xfs_buf_obj_cmp(
497 struct rhashtable_compare_arg *arg,
498 const void *obj)
499{
500 const struct xfs_buf_map *map = arg->key;
501 const struct xfs_buf *bp = obj;
502
503 /*
504 * The key hashing in the lookup path depends on the key being the
505 * first element of the compare_arg, make sure to assert this.
506 */
507 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
508
509 if (bp->b_bn != map->bm_bn)
510 return 1;
511
512 if (unlikely(bp->b_length != map->bm_len)) {
513 /*
514 * found a block number match. If the range doesn't
515 * match, the only way this is allowed is if the buffer
516 * in the cache is stale and the transaction that made
517 * it stale has not yet committed. i.e. we are
518 * reallocating a busy extent. Skip this buffer and
519 * continue searching for an exact match.
520 */
521 ASSERT(bp->b_flags & XBF_STALE);
522 return 1;
523 }
524 return 0;
525}
526
527static const struct rhashtable_params xfs_buf_hash_params = {
528 .min_size = 32, /* empty AGs have minimal footprint */
529 .nelem_hint = 16,
530 .key_len = sizeof(xfs_daddr_t),
531 .key_offset = offsetof(struct xfs_buf, b_bn),
532 .head_offset = offsetof(struct xfs_buf, b_rhash_head),
533 .automatic_shrinking = true,
534 .obj_cmpfn = _xfs_buf_obj_cmp,
535};
536
537int
538xfs_buf_hash_init(
539 struct xfs_perag *pag)
540{
541 spin_lock_init(&pag->pag_buf_lock);
542 return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
543}
544
545void
546xfs_buf_hash_destroy(
547 struct xfs_perag *pag)
548{
549 rhashtable_destroy(&pag->pag_buf_hash);
550}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552/*
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700553 * Look up a buffer in the buffer cache and return it referenced and locked
554 * in @found_bp.
555 *
556 * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the
557 * cache.
558 *
559 * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return
560 * -EAGAIN if we fail to lock it.
561 *
562 * Return values are:
563 * -EFSCORRUPTED if have been supplied with an invalid address
564 * -EAGAIN on trylock failure
565 * -ENOENT if we fail to find a match and @new_bp was NULL
566 * 0, with @found_bp:
567 * - @new_bp if we inserted it into the cache
568 * - the buffer we found and locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700570static int
571xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000572 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000573 struct xfs_buf_map *map,
574 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100575 xfs_buf_flags_t flags,
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700576 struct xfs_buf *new_bp,
577 struct xfs_buf **found_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Dave Chinner74f75a02010-09-24 19:59:04 +1000579 struct xfs_perag *pag;
Dave Chinner74f75a02010-09-24 19:59:04 +1000580 xfs_buf_t *bp;
Lucas Stach6031e732016-12-07 17:36:36 +1100581 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
Dave Chinner10616b82013-01-21 23:53:52 +1100582 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000583 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700585 *found_bp = NULL;
586
Dave Chinner3e85c862012-06-22 18:50:09 +1000587 for (i = 0; i < nmaps; i++)
Lucas Stach6031e732016-12-07 17:36:36 +1100588 cmap.bm_len += map[i].bm_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* Check for IOs smaller than the sector size / not sector aligned */
Lucas Stach6031e732016-12-07 17:36:36 +1100591 ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
592 ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Dave Chinner10616b82013-01-21 23:53:52 +1100594 /*
595 * Corrupted block numbers can get through to here, unfortunately, so we
596 * have to check that the buffer falls within the filesystem bounds.
597 */
598 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
Lucas Stach6031e732016-12-07 17:36:36 +1100599 if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
Dave Chinner10616b82013-01-21 23:53:52 +1100600 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -0800601 "%s: daddr 0x%llx out of range, EOFS 0x%llx",
Lucas Stach6031e732016-12-07 17:36:36 +1100602 __func__, cmap.bm_bn, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000603 WARN_ON(1);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700604 return -EFSCORRUPTED;
Dave Chinner10616b82013-01-21 23:53:52 +1100605 }
606
Dave Chinner74f75a02010-09-24 19:59:04 +1000607 pag = xfs_perag_get(btp->bt_mount,
Lucas Stach6031e732016-12-07 17:36:36 +1100608 xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Dave Chinner74f75a02010-09-24 19:59:04 +1000610 spin_lock(&pag->pag_buf_lock);
Lucas Stach6031e732016-12-07 17:36:36 +1100611 bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
612 xfs_buf_hash_params);
613 if (bp) {
614 atomic_inc(&bp->b_hold);
615 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
618 /* No match found */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700619 if (!new_bp) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100620 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000621 spin_unlock(&pag->pag_buf_lock);
622 xfs_perag_put(pag);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700623 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700625
626 /* the buffer keeps the perag reference until it is freed */
627 new_bp->b_pag = pag;
628 rhashtable_insert_fast(&pag->pag_buf_hash, &new_bp->b_rhash_head,
629 xfs_buf_hash_params);
630 spin_unlock(&pag->pag_buf_lock);
631 *found_bp = new_bp;
632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000635 spin_unlock(&pag->pag_buf_lock);
636 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200638 if (!xfs_buf_trylock(bp)) {
639 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100640 xfs_buf_rele(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100641 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700642 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200644 xfs_buf_lock(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100645 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647
Dave Chinner0e6e8472011-03-26 09:16:45 +1100648 /*
649 * if the buffer is stale, clear all the external state associated with
650 * it. We need to keep flags such as how we allocated the buffer memory
651 * intact here.
652 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100653 if (bp->b_flags & XBF_STALE) {
654 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinnercfb02852012-11-12 22:54:19 +1100655 ASSERT(bp->b_iodone == NULL);
Dave Chinner611c9942012-04-23 15:59:07 +1000656 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100657 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000658 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000659
660 trace_xfs_buf_find(bp, flags, _RET_IP_);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100661 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700662 *found_bp = bp;
663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Dave Chinner8925a3d2018-04-18 08:25:20 -0700666struct xfs_buf *
667xfs_buf_incore(
668 struct xfs_buftarg *target,
669 xfs_daddr_t blkno,
670 size_t numblks,
671 xfs_buf_flags_t flags)
672{
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700673 struct xfs_buf *bp;
674 int error;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700675 DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700676
677 error = xfs_buf_find(target, &map, 1, flags, NULL, &bp);
678 if (error)
679 return NULL;
680 return bp;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700681}
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683/*
Dave Chinner38158322011-09-30 04:45:02 +0000684 * Assembles a buffer covering the specified range. The code is optimised for
685 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
686 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800688int
Dave Chinner6dde2702012-06-22 18:50:10 +1000689xfs_buf_get_map(
690 struct xfs_buftarg *target,
691 struct xfs_buf_map *map,
692 int nmaps,
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800693 xfs_buf_flags_t flags,
694 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Dave Chinner38158322011-09-30 04:45:02 +0000696 struct xfs_buf *bp;
697 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100698 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800700 *bpp = NULL;
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700701 error = xfs_buf_find(target, map, nmaps, flags, NULL, &bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800702 if (!error)
Dave Chinner38158322011-09-30 04:45:02 +0000703 goto found;
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800704 if (error != -ENOENT)
705 return error;
Dave Chinner38158322011-09-30 04:45:02 +0000706
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800707 error = _xfs_buf_alloc(target, map, nmaps, flags, &new_bp);
708 if (error)
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800709 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000711 error = xfs_buf_allocate_memory(new_bp, flags);
712 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000713 xfs_buf_free(new_bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800714 return error;
Dave Chinner38158322011-09-30 04:45:02 +0000715 }
716
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700717 error = xfs_buf_find(target, map, nmaps, flags, new_bp, &bp);
718 if (error) {
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000719 xfs_buf_free(new_bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800720 return error;
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000721 }
722
723 if (bp != new_bp)
724 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Dave Chinner38158322011-09-30 04:45:02 +0000726found:
Dave Chinner611c9942012-04-23 15:59:07 +1000727 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100728 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (unlikely(error)) {
Darrick J. Wong93baa552020-02-21 07:40:44 -0800730 xfs_warn_ratelimited(target->bt_mount,
731 "%s: failed to map %u pages", __func__,
732 bp->b_page_count);
Dave Chinnera8acad72012-04-23 15:58:54 +1000733 xfs_buf_relse(bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800734 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736 }
737
Dave Chinnerb79f4a12016-01-12 07:03:44 +1100738 /*
739 * Clear b_error if this is a lookup from a caller that doesn't expect
740 * valid data to be found in the buffer.
741 */
742 if (!(flags & XBF_READ))
743 xfs_buf_ioerror(bp, 0);
744
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100745 XFS_STATS_INC(target->bt_mount, xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000746 trace_xfs_buf_get(bp, flags, _RET_IP_);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800747 *bpp = bp;
748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100751STATIC int
752_xfs_buf_read(
753 xfs_buf_t *bp,
754 xfs_buf_flags_t flags)
755{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000756 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600757 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100758
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000759 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200760 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100761
Brian Foster6af88cd2018-07-11 22:26:35 -0700762 return xfs_buf_submit(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100763}
764
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100765/*
Brian Foster75d02302019-02-06 09:25:29 -0800766 * Reverify a buffer found in cache without an attached ->b_ops.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800767 *
Brian Foster75d02302019-02-06 09:25:29 -0800768 * If the caller passed an ops structure and the buffer doesn't have ops
769 * assigned, set the ops and use it to verify the contents. If verification
770 * fails, clear XBF_DONE. We assume the buffer has no recorded errors and is
771 * already in XBF_DONE state on entry.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800772 *
Brian Foster75d02302019-02-06 09:25:29 -0800773 * Under normal operations, every in-core buffer is verified on read I/O
774 * completion. There are two scenarios that can lead to in-core buffers without
775 * an assigned ->b_ops. The first is during log recovery of buffers on a V4
776 * filesystem, though these buffers are purged at the end of recovery. The
777 * other is online repair, which intentionally reads with a NULL buffer ops to
778 * run several verifiers across an in-core buffer in order to establish buffer
779 * type. If repair can't establish that, the buffer will be left in memory
780 * with NULL buffer ops.
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100781 */
782int
Brian Foster75d02302019-02-06 09:25:29 -0800783xfs_buf_reverify(
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100784 struct xfs_buf *bp,
785 const struct xfs_buf_ops *ops)
786{
787 ASSERT(bp->b_flags & XBF_DONE);
788 ASSERT(bp->b_error == 0);
789
790 if (!ops || bp->b_ops)
791 return 0;
792
793 bp->b_ops = ops;
794 bp->b_ops->verify_read(bp);
795 if (bp->b_error)
796 bp->b_flags &= ~XBF_DONE;
797 return bp->b_error;
798}
799
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800800int
Dave Chinner6dde2702012-06-22 18:50:10 +1000801xfs_buf_read_map(
802 struct xfs_buftarg *target,
803 struct xfs_buf_map *map,
804 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100805 xfs_buf_flags_t flags,
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800806 struct xfs_buf **bpp,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800807 const struct xfs_buf_ops *ops,
808 xfs_failaddr_t fa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Dave Chinner6dde2702012-06-22 18:50:10 +1000810 struct xfs_buf *bp;
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800811 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Nathan Scottce8e9222006-01-11 15:39:08 +1100813 flags |= XBF_READ;
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800814 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800816 error = xfs_buf_get_map(target, map, nmaps, flags, &bp);
817 if (error)
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800818 return error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000819
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100820 trace_xfs_buf_read(bp, flags, _RET_IP_);
821
822 if (!(bp->b_flags & XBF_DONE)) {
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800823 /* Initiate the buffer read and wait. */
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100824 XFS_STATS_INC(target->bt_mount, xb_get_read);
825 bp->b_ops = ops;
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800826 error = _xfs_buf_read(bp, flags);
827
828 /* Readahead iodone already dropped the buffer, so exit. */
829 if (flags & XBF_ASYNC)
830 return 0;
831 } else {
832 /* Buffer already read; all we need to do is check it. */
833 error = xfs_buf_reverify(bp, ops);
834
835 /* Readahead already finished; drop the buffer and exit. */
836 if (flags & XBF_ASYNC) {
837 xfs_buf_relse(bp);
838 return 0;
839 }
840
841 /* We do not want read in the flags */
842 bp->b_flags &= ~XBF_READ;
843 ASSERT(bp->b_ops != NULL || ops == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800846 /*
847 * If we've had a read error, then the contents of the buffer are
848 * invalid and should not be used. To ensure that a followup read tries
849 * to pull the buffer from disk again, we clear the XBF_DONE flag and
850 * mark the buffer stale. This ensures that anyone who has a current
851 * reference to the buffer will interpret it's contents correctly and
852 * future cache lookups will also treat it as an empty, uninitialised
853 * buffer.
854 */
855 if (error) {
856 if (!XFS_FORCED_SHUTDOWN(target->bt_mount))
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800857 xfs_buf_ioerror_alert(bp, fa);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100858
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800859 bp->b_flags &= ~XBF_DONE;
860 xfs_buf_stale(bp);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100861 xfs_buf_relse(bp);
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800862
863 /* bad CRC means corrupted metadata */
864 if (error == -EFSBADCRC)
865 error = -EFSCORRUPTED;
866 return error;
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100867 }
868
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800869 *bpp = bp;
870 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
873/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100874 * If we are not low on memory then do the readahead in a deadlock
875 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 */
877void
Dave Chinner6dde2702012-06-22 18:50:10 +1000878xfs_buf_readahead_map(
879 struct xfs_buftarg *target,
880 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100881 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100882 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800884 struct xfs_buf *bp;
885
Jan Karaefa7c9f2017-02-02 15:56:53 +0100886 if (bdi_read_congested(target->bt_bdev->bd_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return;
888
Dave Chinner6dde2702012-06-22 18:50:10 +1000889 xfs_buf_read_map(target, map, nmaps,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -0800890 XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD, &bp, ops,
891 __this_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Dave Chinner5adc94c2010-09-24 21:58:31 +1000894/*
895 * Read an uncached buffer from disk. Allocates and returns a locked
896 * buffer containing the disk contents or nothing.
897 */
Dave Chinnerba3726742014-10-02 09:05:32 +1000898int
Dave Chinner5adc94c2010-09-24 21:58:31 +1000899xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000900 struct xfs_buftarg *target,
901 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000902 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100903 int flags,
Dave Chinnerba3726742014-10-02 09:05:32 +1000904 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100905 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000906{
Dave Chinnereab4e632012-11-12 22:54:02 +1100907 struct xfs_buf *bp;
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800908 int error;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000909
Dave Chinnerba3726742014-10-02 09:05:32 +1000910 *bpp = NULL;
911
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800912 error = xfs_buf_get_uncached(target, numblks, flags, &bp);
913 if (error)
914 return error;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000915
916 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000917 ASSERT(bp->b_map_count == 1);
Dave Chinnerba3726742014-10-02 09:05:32 +1000918 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
Dave Chinner3e85c862012-06-22 18:50:09 +1000919 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000920 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100921 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000922
Brian Foster6af88cd2018-07-11 22:26:35 -0700923 xfs_buf_submit(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000924 if (bp->b_error) {
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800925 error = bp->b_error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800926 xfs_buf_relse(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000927 return error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800928 }
Dave Chinnerba3726742014-10-02 09:05:32 +1000929
930 *bpp = bp;
931 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800934int
Dave Chinner686865f2010-09-24 20:07:47 +1000935xfs_buf_get_uncached(
936 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000937 size_t numblks,
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800938 int flags,
939 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000941 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000942 int error, i;
Dave Chinner3e85c862012-06-22 18:50:09 +1000943 struct xfs_buf *bp;
944 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800946 *bpp = NULL;
947
Brian Fosterc891c302016-07-20 11:13:43 +1000948 /* flags might contain irrelevant bits, pass only what we care about */
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800949 error = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT, &bp);
950 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Dave Chinnere70b73f2012-04-23 15:58:49 +1000953 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000954 error = _xfs_buf_get_pages(bp, page_count);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000955 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 goto fail_free_buf;
957
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000958 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000959 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800960 if (!bp->b_pages[i]) {
961 error = -ENOMEM;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000962 goto fail_free_mem;
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000965 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Dave Chinner611c9942012-04-23 15:59:07 +1000967 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000968 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100969 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500970 "%s: failed to map pages", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Dave Chinner686865f2010-09-24 20:07:47 +1000974 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800975 *bpp = bp;
976 return 0;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000979 while (--i >= 0)
980 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000981 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 fail_free_buf:
Dave Chinner3e85c862012-06-22 18:50:09 +1000983 xfs_buf_free_maps(bp);
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800984 kmem_cache_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 fail:
Darrick J. Wong2842b6d2020-01-23 17:01:17 -0800986 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 * Increment reference count on buffer, to hold the buffer concurrently
991 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * Must hold the buffer already to call this function.
993 */
994void
Nathan Scottce8e9222006-01-11 15:39:08 +1100995xfs_buf_hold(
996 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000998 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100999 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
1002/*
Brian Foster9c7504a2016-07-20 11:15:28 +10001003 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
1004 * placed on LRU or freed (depending on b_lru_ref).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
1006void
Nathan Scottce8e9222006-01-11 15:39:08 +11001007xfs_buf_rele(
1008 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Dave Chinner74f75a02010-09-24 19:59:04 +10001010 struct xfs_perag *pag = bp->b_pag;
Brian Foster9c7504a2016-07-20 11:15:28 +10001011 bool release;
1012 bool freebuf = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001014 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Dave Chinner74f75a02010-09-24 19:59:04 +10001016 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +11001017 ASSERT(list_empty(&bp->b_lru));
Brian Foster9c7504a2016-07-20 11:15:28 +10001018 if (atomic_dec_and_test(&bp->b_hold)) {
1019 xfs_buf_ioacct_dec(bp);
Nathan Scottfad3aa12006-02-01 12:14:52 +11001020 xfs_buf_free(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001021 }
Nathan Scottfad3aa12006-02-01 12:14:52 +11001022 return;
1023 }
1024
Lachlan McIlroy37906892008-08-13 15:42:10 +10001025 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinnera4082352013-08-28 10:18:06 +10001026
Dave Chinner37fd1672018-10-18 17:21:29 +11001027 /*
1028 * We grab the b_lock here first to serialise racing xfs_buf_rele()
1029 * calls. The pag_buf_lock being taken on the last reference only
1030 * serialises against racing lookups in xfs_buf_find(). IOWs, the second
1031 * to last reference we drop here is not serialised against the last
1032 * reference until we take bp->b_lock. Hence if we don't grab b_lock
1033 * first, the last "release" reference can win the race to the lock and
1034 * free the buffer before the second-to-last reference is processed,
1035 * leading to a use-after-free scenario.
1036 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001037 spin_lock(&bp->b_lock);
Dave Chinner37fd1672018-10-18 17:21:29 +11001038 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +10001039 if (!release) {
1040 /*
1041 * Drop the in-flight state if the buffer is already on the LRU
1042 * and it holds the only reference. This is racy because we
1043 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1044 * ensures the decrement occurs only once per-buf.
1045 */
1046 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
Brian Foster63db7c82017-05-31 08:22:52 -07001047 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001048 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
Brian Foster9c7504a2016-07-20 11:15:28 +10001050
1051 /* the last reference has been dropped ... */
Brian Foster63db7c82017-05-31 08:22:52 -07001052 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001053 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1054 /*
1055 * If the buffer is added to the LRU take a new reference to the
1056 * buffer for the LRU and clear the (now stale) dispose list
1057 * state flag
1058 */
1059 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1060 bp->b_state &= ~XFS_BSTATE_DISPOSE;
1061 atomic_inc(&bp->b_hold);
1062 }
1063 spin_unlock(&pag->pag_buf_lock);
1064 } else {
1065 /*
1066 * most of the time buffers will already be removed from the
1067 * LRU, so optimise that case by checking for the
1068 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1069 * was on was the disposal list
1070 */
1071 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1072 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1073 } else {
1074 ASSERT(list_empty(&bp->b_lru));
1075 }
1076
1077 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Lucas Stach6031e732016-12-07 17:36:36 +11001078 rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
1079 xfs_buf_hash_params);
Brian Foster9c7504a2016-07-20 11:15:28 +10001080 spin_unlock(&pag->pag_buf_lock);
1081 xfs_perag_put(pag);
1082 freebuf = true;
1083 }
1084
1085out_unlock:
1086 spin_unlock(&bp->b_lock);
1087
1088 if (freebuf)
1089 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092
1093/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001094 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +11001095 *
1096 * If we come across a stale, pinned, locked buffer, we know that we are
1097 * being asked to lock a buffer that has been reallocated. Because it is
1098 * pinned, we know that the log has not been pushed to disk and hence it
1099 * will still be locked. Rather than continuing to have trylock attempts
1100 * fail until someone else pushes the log, push it ourselves before
1101 * returning. This means that the xfsaild will not get stuck trying
1102 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 */
1104int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001105xfs_buf_trylock(
1106 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 int locked;
1109
Nathan Scottce8e9222006-01-11 15:39:08 +11001110 locked = down_trylock(&bp->b_sema) == 0;
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001111 if (locked)
Darrick J. Wong479c6412016-06-21 11:53:28 +10001112 trace_xfs_buf_trylock(bp, _RET_IP_);
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001113 else
Darrick J. Wong479c6412016-06-21 11:53:28 +10001114 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001115 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001119 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +10001120 *
1121 * If we come across a stale, pinned, locked buffer, we know that we
1122 * are being asked to lock a buffer that has been reallocated. Because
1123 * it is pinned, we know that the log has not been pushed to disk and
1124 * hence it will still be locked. Rather than sleeping until someone
1125 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001127void
1128xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001129 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001131 trace_xfs_buf_lock(bp, _RET_IP_);
1132
Dave Chinnered3b4d62010-05-21 12:07:08 +10001133 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001134 xfs_log_force(bp->b_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +11001135 down(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001136
1137 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140void
Nathan Scottce8e9222006-01-11 15:39:08 +11001141xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001142 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Brian Foster20e8a062017-04-21 12:40:44 -07001144 ASSERT(xfs_buf_islocked(bp));
1145
Nathan Scottce8e9222006-01-11 15:39:08 +11001146 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001147 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Nathan Scottce8e9222006-01-11 15:39:08 +11001150STATIC void
1151xfs_buf_wait_unpin(
1152 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
1154 DECLARE_WAITQUEUE (wait, current);
1155
Nathan Scottce8e9222006-01-11 15:39:08 +11001156 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return;
1158
Nathan Scottce8e9222006-01-11 15:39:08 +11001159 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 for (;;) {
1161 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001162 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001164 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001166 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 set_current_state(TASK_RUNNING);
1168}
1169
1170/*
1171 * Buffer Utility Routines
1172 */
1173
Dave Chinnere8aaba92014-10-02 09:04:22 +10001174void
1175xfs_buf_ioend(
1176 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001178 bool read = bp->b_flags & XBF_READ;
1179
1180 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001181
1182 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Dave Chinnerd5929de2013-02-27 13:25:54 +11001183
Dave Chinner61be9c52014-10-02 09:04:31 +10001184 /*
1185 * Pull in IO completion errors now. We are guaranteed to be running
1186 * single threaded, so we don't need the lock to read b_io_error.
1187 */
1188 if (!bp->b_error && bp->b_io_error)
1189 xfs_buf_ioerror(bp, bp->b_io_error);
1190
Dave Chinnere8aaba92014-10-02 09:04:22 +10001191 /* Only validate buffers that were read without errors */
1192 if (read && !bp->b_error && bp->b_ops) {
1193 ASSERT(!bp->b_iodone);
Dave Chinner1813dd62012-11-14 17:54:40 +11001194 bp->b_ops->verify_read(bp);
Dave Chinnere8aaba92014-10-02 09:04:22 +10001195 }
1196
1197 if (!bp->b_error)
1198 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Christoph Hellwig80f6c292010-08-18 05:29:11 -04001200 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001201 (*(bp->b_iodone))(bp);
1202 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 xfs_buf_relse(bp);
Dave Chinner595bff72014-10-02 09:05:14 +10001204 else
Dave Chinner1813dd62012-11-14 17:54:40 +11001205 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
Dave Chinnere8aaba92014-10-02 09:04:22 +10001208static void
1209xfs_buf_ioend_work(
1210 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001212 struct xfs_buf *bp =
Brian Fosterb29c70f2014-12-04 09:43:17 +11001213 container_of(work, xfs_buf_t, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001214
Dave Chinnere8aaba92014-10-02 09:04:22 +10001215 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001218static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001219xfs_buf_ioend_async(
1220 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001222 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001223 queue_work(bp->b_mount->m_buf_workqueue, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226void
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001227__xfs_buf_ioerror(
Nathan Scottce8e9222006-01-11 15:39:08 +11001228 xfs_buf_t *bp,
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001229 int error,
1230 xfs_failaddr_t failaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Dave Chinner24513372014-06-25 14:58:08 +10001232 ASSERT(error <= 0 && error >= -1000);
1233 bp->b_error = error;
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001234 trace_xfs_buf_ioerror(bp, error, failaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Christoph Hellwig901796a2011-10-10 16:52:49 +00001237void
1238xfs_buf_ioerror_alert(
1239 struct xfs_buf *bp,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -08001240 xfs_failaddr_t func)
Christoph Hellwig901796a2011-10-10 16:52:49 +00001241{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001242 xfs_alert(bp->b_mount,
Darrick J. Wongcdbcf822020-01-23 17:01:20 -08001243"metadata I/O error in \"%pS\" at daddr 0x%llx len %d error %d",
Darrick J. Wongc219b012018-01-08 11:39:18 -08001244 func, (uint64_t)XFS_BUF_ADDR(bp), bp->b_length,
1245 -bp->b_error);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001246}
1247
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001248int
1249xfs_bwrite(
1250 struct xfs_buf *bp)
1251{
1252 int error;
1253
1254 ASSERT(xfs_buf_islocked(bp));
1255
1256 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001257 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
1258 XBF_WRITE_FAIL | XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001259
Brian Foster6af88cd2018-07-11 22:26:35 -07001260 error = xfs_buf_submit(bp);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001261 if (error)
1262 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001263 return error;
1264}
1265
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001266static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001267xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001268 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001270 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Dave Chinner37eb17e2012-11-12 22:09:46 +11001272 /*
1273 * don't overwrite existing errors - otherwise we can lose errors on
1274 * buffers that require multiple bios to complete.
1275 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001276 if (bio->bi_status) {
1277 int error = blk_status_to_errno(bio->bi_status);
1278
1279 cmpxchg(&bp->b_io_error, 0, error);
1280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Dave Chinner37eb17e2012-11-12 22:09:46 +11001282 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001283 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1284
Dave Chinnere8aaba92014-10-02 09:04:22 +10001285 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1286 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Dave Chinner3e85c862012-06-22 18:50:09 +10001290static void
1291xfs_buf_ioapply_map(
1292 struct xfs_buf *bp,
1293 int map,
1294 int *buf_offset,
1295 int *count,
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001296 int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Dave Chinner3e85c862012-06-22 18:50:09 +10001298 int page_index;
1299 int total_nr_pages = bp->b_page_count;
1300 int nr_pages;
1301 struct bio *bio;
1302 sector_t sector = bp->b_maps[map].bm_bn;
1303 int size;
1304 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Dave Chinner3e85c862012-06-22 18:50:09 +10001306 /* skip the pages in the buffer before the start offset */
1307 page_index = 0;
1308 offset = *buf_offset;
1309 while (offset >= PAGE_SIZE) {
1310 page_index++;
1311 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001312 }
1313
Dave Chinner3e85c862012-06-22 18:50:09 +10001314 /*
1315 * Limit the IO size to the length of the current vector, and update the
1316 * remaining IO count for the next time around.
1317 */
1318 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1319 *count -= size;
1320 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001323 atomic_inc(&bp->b_io_remaining);
Ming Leic908e382016-05-30 21:34:33 +08001324 nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 bio = bio_alloc(GFP_NOIO, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001327 bio_set_dev(bio, bp->b_target->bt_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001328 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001329 bio->bi_end_io = xfs_buf_bio_end_io;
1330 bio->bi_private = bp;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001331 bio->bi_opf = op;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001332
Dave Chinner3e85c862012-06-22 18:50:09 +10001333 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001334 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 if (nbytes > size)
1337 nbytes = size;
1338
Dave Chinner3e85c862012-06-22 18:50:09 +10001339 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1340 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001341 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 break;
1343
1344 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001345 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 size -= nbytes;
1347 total_nr_pages--;
1348 }
1349
Kent Overstreet4f024f32013-10-11 15:44:27 -07001350 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001351 if (xfs_buf_is_vmapped(bp)) {
1352 flush_kernel_vmap_range(bp->b_addr,
1353 xfs_buf_vmap_len(bp));
1354 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001355 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (size)
1357 goto next_chunk;
1358 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001359 /*
1360 * This is guaranteed not to be the last io reference count
Dave Chinner595bff72014-10-02 09:05:14 +10001361 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001362 */
1363 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001364 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001365 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001367
1368}
1369
1370STATIC void
1371_xfs_buf_ioapply(
1372 struct xfs_buf *bp)
1373{
1374 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001375 int op;
Dave Chinner3e85c862012-06-22 18:50:09 +10001376 int offset;
1377 int size;
1378 int i;
1379
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001380 /*
1381 * Make sure we capture only current IO errors rather than stale errors
1382 * left over from previous use of the buffer (e.g. failed readahead).
1383 */
1384 bp->b_error = 0;
1385
Dave Chinner3e85c862012-06-22 18:50:09 +10001386 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001387 op = REQ_OP_WRITE;
Dave Chinner1813dd62012-11-14 17:54:40 +11001388
1389 /*
1390 * Run the write verifier callback function if it exists. If
1391 * this function fails it will mark the buffer with an error and
1392 * the IO should not be dispatched.
1393 */
1394 if (bp->b_ops) {
1395 bp->b_ops->verify_write(bp);
1396 if (bp->b_error) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001397 xfs_force_shutdown(bp->b_mount,
Dave Chinner1813dd62012-11-14 17:54:40 +11001398 SHUTDOWN_CORRUPT_INCORE);
1399 return;
1400 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001401 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001402 struct xfs_mount *mp = bp->b_mount;
Dave Chinner400b9d82014-08-04 12:42:40 +10001403
1404 /*
1405 * non-crc filesystems don't attach verifiers during
1406 * log recovery, so don't warn for such filesystems.
1407 */
1408 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1409 xfs_warn(mp,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001410 "%s: no buf ops on daddr 0x%llx len %d",
Dave Chinner400b9d82014-08-04 12:42:40 +10001411 __func__, bp->b_bn, bp->b_length);
Darrick J. Wong9c712a12018-01-08 10:51:26 -08001412 xfs_hex_dump(bp->b_addr,
1413 XFS_CORRUPTION_DUMP_LEN);
Dave Chinner400b9d82014-08-04 12:42:40 +10001414 dump_stack();
1415 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001416 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001417 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001418 op = REQ_OP_READ;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001419 if (bp->b_flags & XBF_READ_AHEAD)
1420 op |= REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001421 }
1422
1423 /* we only use the buffer cache for meta-data */
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001424 op |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001425
1426 /*
1427 * Walk all the vectors issuing IO on them. Set up the initial offset
1428 * into the buffer and the desired IO size before we start -
1429 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1430 * subsequent call.
1431 */
1432 offset = bp->b_offset;
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001433 size = BBTOB(bp->b_length);
Dave Chinner3e85c862012-06-22 18:50:09 +10001434 blk_start_plug(&plug);
1435 for (i = 0; i < bp->b_map_count; i++) {
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001436 xfs_buf_ioapply_map(bp, i, &offset, &size, op);
Dave Chinner3e85c862012-06-22 18:50:09 +10001437 if (bp->b_error)
1438 break;
1439 if (size <= 0)
1440 break; /* all done */
1441 }
1442 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
Dave Chinner595bff72014-10-02 09:05:14 +10001445/*
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001446 * Wait for I/O completion of a sync buffer and return the I/O error code.
Dave Chinner595bff72014-10-02 09:05:14 +10001447 */
Brian Fostereaebb512018-07-11 22:26:34 -07001448static int
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001449xfs_buf_iowait(
Dave Chinner595bff72014-10-02 09:05:14 +10001450 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001452 ASSERT(!(bp->b_flags & XBF_ASYNC));
1453
1454 trace_xfs_buf_iowait(bp, _RET_IP_);
1455 wait_for_completion(&bp->b_iowait);
1456 trace_xfs_buf_iowait_done(bp, _RET_IP_);
1457
1458 return bp->b_error;
1459}
1460
1461/*
1462 * Buffer I/O submission path, read or write. Asynchronous submission transfers
1463 * the buffer lock ownership and the current reference to the IO. It is not
1464 * safe to reference the buffer after a call to this function unless the caller
1465 * holds an additional reference itself.
1466 */
1467int
1468__xfs_buf_submit(
1469 struct xfs_buf *bp,
1470 bool wait)
1471{
1472 int error = 0;
1473
Dave Chinner595bff72014-10-02 09:05:14 +10001474 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001476 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff72014-10-02 09:05:14 +10001477
1478 /* on shutdown we stale and complete the buffer immediately */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001479 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
Dave Chinner595bff72014-10-02 09:05:14 +10001480 xfs_buf_ioerror(bp, -EIO);
1481 bp->b_flags &= ~XBF_DONE;
1482 xfs_buf_stale(bp);
Brian Foster465fa172019-02-03 14:03:06 -08001483 xfs_buf_ioend(bp);
Brian Fostereaebb512018-07-11 22:26:34 -07001484 return -EIO;
Dave Chinner595bff72014-10-02 09:05:14 +10001485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001487 /*
1488 * Grab a reference so the buffer does not go away underneath us. For
1489 * async buffers, I/O completion drops the callers reference, which
1490 * could occur before submission returns.
1491 */
1492 xfs_buf_hold(bp);
1493
Christoph Hellwig375ec692011-08-23 08:28:03 +00001494 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001495 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Dave Chinner61be9c52014-10-02 09:04:31 +10001497 /* clear the internal error state to avoid spurious errors */
1498 bp->b_io_error = 0;
1499
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001500 /*
Brian Fostereaebb512018-07-11 22:26:34 -07001501 * Set the count to 1 initially, this will stop an I/O completion
1502 * callout which happens before we have started all the I/O from calling
1503 * xfs_buf_ioend too early.
1504 */
1505 atomic_set(&bp->b_io_remaining, 1);
1506 if (bp->b_flags & XBF_ASYNC)
1507 xfs_buf_ioacct_inc(bp);
1508 _xfs_buf_ioapply(bp);
1509
1510 /*
1511 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1512 * reference we took above. If we drop it to zero, run completion so
1513 * that we don't return to the caller with completion still pending.
1514 */
1515 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1516 if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
1517 xfs_buf_ioend(bp);
1518 else
1519 xfs_buf_ioend_async(bp);
1520 }
1521
Brian Foster6af88cd2018-07-11 22:26:35 -07001522 if (wait)
1523 error = xfs_buf_iowait(bp);
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001524
Dave Chinner595bff72014-10-02 09:05:14 +10001525 /*
Brian Foster6af88cd2018-07-11 22:26:35 -07001526 * Release the hold that keeps the buffer referenced for the entire
1527 * I/O. Note that if the buffer is async, it is not safe to reference
1528 * after this release.
Dave Chinner595bff72014-10-02 09:05:14 +10001529 */
1530 xfs_buf_rele(bp);
1531 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001534void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001535xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001536 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 size_t offset)
1538{
1539 struct page *page;
1540
Dave Chinner611c9942012-04-23 15:59:07 +10001541 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001542 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Nathan Scottce8e9222006-01-11 15:39:08 +11001544 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001545 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001546 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549void
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001550xfs_buf_zero(
1551 struct xfs_buf *bp,
1552 size_t boff,
1553 size_t bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Dave Chinner795cac72012-04-23 15:58:53 +10001555 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 bend = boff + bsize;
1558 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001559 struct page *page;
1560 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Dave Chinner795cac72012-04-23 15:58:53 +10001562 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1563 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1564 page = bp->b_pages[page_index];
1565 csize = min_t(size_t, PAGE_SIZE - page_offset,
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001566 BBTOB(bp->b_length) - boff);
Dave Chinner795cac72012-04-23 15:58:53 +10001567
1568 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001570 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 boff += csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574}
1575
1576/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001577 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 */
1579
1580/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001581 * Wait for any bufs with callbacks that have been submitted but have not yet
1582 * returned. These buffers will have an elevated hold count, so wait on those
1583 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001585static enum lru_status
1586xfs_buftarg_wait_rele(
1587 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001588 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001589 spinlock_t *lru_lock,
1590 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Dave Chinnere80dfa12013-08-28 10:18:05 +10001592{
1593 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001594 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001595
1596 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001597 /* need to wait, so skip it this pass */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001598 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001599 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
Dave Chinnera4082352013-08-28 10:18:06 +10001601 if (!spin_trylock(&bp->b_lock))
1602 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001603
Dave Chinnera4082352013-08-28 10:18:06 +10001604 /*
1605 * clear the LRU reference count so the buffer doesn't get
1606 * ignored in xfs_buf_rele().
1607 */
1608 atomic_set(&bp->b_lru_ref, 0);
1609 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001610 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001611 spin_unlock(&bp->b_lock);
1612 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Dave Chinnere80dfa12013-08-28 10:18:05 +10001615void
1616xfs_wait_buftarg(
1617 struct xfs_buftarg *btp)
1618{
Dave Chinnera4082352013-08-28 10:18:06 +10001619 LIST_HEAD(dispose);
1620 int loop = 0;
1621
Dave Chinner85bec542016-01-19 08:28:10 +11001622 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001623 * First wait on the buftarg I/O count for all in-flight buffers to be
1624 * released. This is critical as new buffers do not make the LRU until
1625 * they are released.
1626 *
1627 * Next, flush the buffer workqueue to ensure all completion processing
1628 * has finished. Just waiting on buffer locks is not sufficient for
1629 * async IO as the reference count held over IO is not released until
1630 * after the buffer lock is dropped. Hence we need to ensure here that
1631 * all reference counts have been dropped before we start walking the
1632 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001633 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001634 while (percpu_counter_sum(&btp->bt_io_count))
1635 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001636 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Dave Chinner85bec542016-01-19 08:28:10 +11001637
Dave Chinnera4082352013-08-28 10:18:06 +10001638 /* loop until there is nothing left on the lru list. */
1639 while (list_lru_count(&btp->bt_lru)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001640 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001641 &dispose, LONG_MAX);
1642
1643 while (!list_empty(&dispose)) {
1644 struct xfs_buf *bp;
1645 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1646 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001647 if (bp->b_flags & XBF_WRITE_FAIL) {
1648 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001649"Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001650 (long long)bp->b_bn);
Joe Perchesf41febd2015-07-29 11:52:04 +10001651 xfs_alert(btp->bt_mount,
1652"Please run xfs_repair to determine the extent of the problem.");
Dave Chinnerac8809f2013-12-12 16:34:38 +11001653 }
Dave Chinnera4082352013-08-28 10:18:06 +10001654 xfs_buf_rele(bp);
1655 }
1656 if (loop++ != 0)
1657 delay(100);
1658 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001659}
1660
1661static enum lru_status
1662xfs_buftarg_isolate(
1663 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001664 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001665 spinlock_t *lru_lock,
1666 void *arg)
1667{
1668 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1669 struct list_head *dispose = arg;
1670
1671 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001672 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1673 * If we fail to get the lock, just skip it.
1674 */
1675 if (!spin_trylock(&bp->b_lock))
1676 return LRU_SKIP;
1677 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001678 * Decrement the b_lru_ref count unless the value is already
1679 * zero. If the value is already zero, we need to reclaim the
1680 * buffer, otherwise it gets another trip through the LRU.
1681 */
Vratislav Bendel19957a12018-03-06 17:07:44 -08001682 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
Dave Chinnera4082352013-08-28 10:18:06 +10001683 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001684 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001685 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001686
Dave Chinnera4082352013-08-28 10:18:06 +10001687 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001688 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001689 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001690 return LRU_REMOVED;
1691}
1692
Andrew Mortonaddbda42013-08-28 10:18:06 +10001693static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001694xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001695 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001696 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001697{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001698 struct xfs_buftarg *btp = container_of(shrink,
1699 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001700 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001701 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001702
Vladimir Davydov503c3582015-02-12 14:58:47 -08001703 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1704 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001705
1706 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001707 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001708 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1709 list_del_init(&bp->b_lru);
1710 xfs_buf_rele(bp);
1711 }
1712
Dave Chinnere80dfa12013-08-28 10:18:05 +10001713 return freed;
1714}
1715
Andrew Mortonaddbda42013-08-28 10:18:06 +10001716static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001717xfs_buftarg_shrink_count(
1718 struct shrinker *shrink,
1719 struct shrink_control *sc)
1720{
1721 struct xfs_buftarg *btp = container_of(shrink,
1722 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001723 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001724}
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726void
1727xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001728 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001730 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001731 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1732 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001733 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001734
Dave Chinner2291dab2016-12-09 16:49:54 +11001735 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001736
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001737 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001740int
1741xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001743 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001745 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001746 btp->bt_meta_sectorsize = sectorsize;
1747 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Nathan Scottce8e9222006-01-11 15:39:08 +11001749 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001750 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001751 "Cannot set_blocksize to %u on device %pg",
1752 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001753 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001756 /* Set up device logical sector size mask */
1757 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1758 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return 0;
1761}
1762
1763/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001764 * When allocating the initial buffer target we have not yet
1765 * read in the superblock, so don't know what sized sectors
1766 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001767 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768STATIC int
1769xfs_setsize_buftarg_early(
1770 xfs_buftarg_t *btp,
1771 struct block_device *bdev)
1772{
Eric Sandeena96c4152014-04-14 19:00:29 +10001773 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776xfs_buftarg_t *
1777xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001778 struct xfs_mount *mp,
Dan Williams486aff52017-08-24 15:12:50 -07001779 struct block_device *bdev,
1780 struct dax_device *dax_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 xfs_buftarg_t *btp;
1783
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07001784 btp = kmem_zalloc(sizeof(*btp), KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Dave Chinnerebad8612010-09-22 10:47:20 +10001786 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001787 btp->bt_dev = bdev->bd_dev;
1788 btp->bt_bdev = bdev;
Dan Williams486aff52017-08-24 15:12:50 -07001789 btp->bt_daxdev = dax_dev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (xfs_setsize_buftarg_early(btp, bdev))
Michal Hockod210a982017-11-23 17:13:40 +01001792 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001793
1794 if (list_lru_init(&btp->bt_lru))
Michal Hockod210a982017-11-23 17:13:40 +01001795 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001796
Brian Foster9c7504a2016-07-20 11:15:28 +10001797 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
Michal Hockod210a982017-11-23 17:13:40 +01001798 goto error_lru;
Brian Foster9c7504a2016-07-20 11:15:28 +10001799
Dave Chinnere80dfa12013-08-28 10:18:05 +10001800 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1801 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001802 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001803 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Michal Hockod210a982017-11-23 17:13:40 +01001804 if (register_shrinker(&btp->bt_shrinker))
1805 goto error_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return btp;
1807
Michal Hockod210a982017-11-23 17:13:40 +01001808error_pcpu:
1809 percpu_counter_destroy(&btp->bt_io_count);
1810error_lru:
1811 list_lru_destroy(&btp->bt_lru);
1812error_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001813 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return NULL;
1815}
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817/*
Brian Foster20e8a062017-04-21 12:40:44 -07001818 * Cancel a delayed write list.
1819 *
1820 * Remove each buffer from the list, clear the delwri queue flag and drop the
1821 * associated buffer reference.
1822 */
1823void
1824xfs_buf_delwri_cancel(
1825 struct list_head *list)
1826{
1827 struct xfs_buf *bp;
1828
1829 while (!list_empty(list)) {
1830 bp = list_first_entry(list, struct xfs_buf, b_list);
1831
1832 xfs_buf_lock(bp);
1833 bp->b_flags &= ~_XBF_DELWRI_Q;
1834 list_del_init(&bp->b_list);
1835 xfs_buf_relse(bp);
1836 }
1837}
1838
1839/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001840 * Add a buffer to the delayed write list.
1841 *
1842 * This queues a buffer for writeout if it hasn't already been. Note that
1843 * neither this routine nor the buffer list submission functions perform
1844 * any internal synchronization. It is expected that the lists are thread-local
1845 * to the callers.
1846 *
1847 * Returns true if we queued up the buffer, or false if it already had
1848 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001850bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001851xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001852 struct xfs_buf *bp,
1853 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001855 ASSERT(xfs_buf_islocked(bp));
1856 ASSERT(!(bp->b_flags & XBF_READ));
1857
1858 /*
1859 * If the buffer is already marked delwri it already is queued up
1860 * by someone else for imediate writeout. Just ignore it in that
1861 * case.
1862 */
1863 if (bp->b_flags & _XBF_DELWRI_Q) {
1864 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1865 return false;
1866 }
David Chinnera6867a62006-01-11 15:37:58 +11001867
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001868 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1869
Dave Chinnerd808f612010-02-02 10:13:42 +11001870 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001871 * If a buffer gets written out synchronously or marked stale while it
1872 * is on a delwri list we lazily remove it. To do this, the other party
1873 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1874 * It remains referenced and on the list. In a rare corner case it
1875 * might get readded to a delwri list after the synchronous writeout, in
1876 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001877 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001878 bp->b_flags |= _XBF_DELWRI_Q;
1879 if (list_empty(&bp->b_list)) {
1880 atomic_inc(&bp->b_hold);
1881 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001882 }
David Chinner585e6d82007-02-10 18:32:29 +11001883
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001884 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001885}
1886
Dave Chinner089716a2010-01-26 15:13:25 +11001887/*
1888 * Compare function is more complex than it needs to be because
1889 * the return value is only 32 bits and we are doing comparisons
1890 * on 64 bit values
1891 */
1892static int
1893xfs_buf_cmp(
1894 void *priv,
1895 struct list_head *a,
1896 struct list_head *b)
1897{
1898 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1899 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1900 xfs_daddr_t diff;
1901
Mark Tinguelyf4b42422012-12-04 17:18:02 -06001902 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11001903 if (diff < 0)
1904 return -1;
1905 if (diff > 0)
1906 return 1;
1907 return 0;
1908}
1909
Dave Chinner26f1fe82016-06-01 17:38:15 +10001910/*
Brian Fostere339dd82018-07-11 22:26:34 -07001911 * Submit buffers for write. If wait_list is specified, the buffers are
1912 * submitted using sync I/O and placed on the wait list such that the caller can
1913 * iowait each buffer. Otherwise async I/O is used and the buffers are released
1914 * at I/O completion time. In either case, buffers remain locked until I/O
1915 * completes and the buffer is released from the queue.
Dave Chinner26f1fe82016-06-01 17:38:15 +10001916 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001917static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10001918xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001919 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10001920 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001922 struct xfs_buf *bp, *n;
1923 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001924 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Dave Chinner26f1fe82016-06-01 17:38:15 +10001926 list_sort(NULL, buffer_list, xfs_buf_cmp);
1927
1928 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001929 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10001930 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001931 if (xfs_buf_ispinned(bp)) {
1932 pinned++;
1933 continue;
1934 }
1935 if (!xfs_buf_trylock(bp))
1936 continue;
1937 } else {
1938 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001941 /*
1942 * Someone else might have written the buffer synchronously or
1943 * marked it stale in the meantime. In that case only the
1944 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1945 * reference and remove it from the list here.
1946 */
1947 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1948 list_del_init(&bp->b_list);
1949 xfs_buf_relse(bp);
1950 continue;
1951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001953 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001954
Dave Chinnercf53e992014-10-02 09:04:01 +10001955 /*
Brian Fostere339dd82018-07-11 22:26:34 -07001956 * If we have a wait list, each buffer (and associated delwri
1957 * queue reference) transfers to it and is submitted
1958 * synchronously. Otherwise, drop the buffer from the delwri
1959 * queue and submit async.
Dave Chinnercf53e992014-10-02 09:04:01 +10001960 */
Dave Chinnerbbfeb612016-07-20 11:53:35 +10001961 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL);
Brian Fostere339dd82018-07-11 22:26:34 -07001962 bp->b_flags |= XBF_WRITE;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001963 if (wait_list) {
Brian Fostere339dd82018-07-11 22:26:34 -07001964 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001965 list_move_tail(&bp->b_list, wait_list);
Brian Fostere339dd82018-07-11 22:26:34 -07001966 } else {
1967 bp->b_flags |= XBF_ASYNC;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001968 list_del_init(&bp->b_list);
Brian Fostere339dd82018-07-11 22:26:34 -07001969 }
Brian Foster6af88cd2018-07-11 22:26:35 -07001970 __xfs_buf_submit(bp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001972 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001974 return pinned;
1975}
Nathan Scottf07c2252006-09-28 10:52:15 +10001976
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001977/*
1978 * Write out a buffer list asynchronously.
1979 *
1980 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1981 * out and not wait for I/O completion on any of the buffers. This interface
1982 * is only safely useable for callers that can track I/O completion by higher
1983 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1984 * function.
Brian Fosterefc32892018-10-18 17:21:49 +11001985 *
1986 * Note: this function will skip buffers it would block on, and in doing so
1987 * leaves them on @buffer_list so they can be retried on a later pass. As such,
1988 * it is up to the caller to ensure that the buffer list is fully submitted or
1989 * cancelled appropriately when they are finished with the list. Failure to
1990 * cancel or resubmit the list until it is empty will result in leaked buffers
1991 * at unmount time.
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001992 */
1993int
1994xfs_buf_delwri_submit_nowait(
1995 struct list_head *buffer_list)
1996{
Dave Chinner26f1fe82016-06-01 17:38:15 +10001997 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001998}
1999
2000/*
2001 * Write out a buffer list synchronously.
2002 *
2003 * This will take the @buffer_list, write all buffers out and wait for I/O
2004 * completion on all of the buffers. @buffer_list is consumed by the function,
2005 * so callers must have some other way of tracking buffers if they require such
2006 * functionality.
2007 */
2008int
2009xfs_buf_delwri_submit(
2010 struct list_head *buffer_list)
2011{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002012 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002013 int error = 0, error2;
2014 struct xfs_buf *bp;
2015
Dave Chinner26f1fe82016-06-01 17:38:15 +10002016 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002017
2018 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10002019 while (!list_empty(&wait_list)) {
2020 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002021
2022 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10002023
Brian Fostere339dd82018-07-11 22:26:34 -07002024 /*
2025 * Wait on the locked buffer, check for errors and unlock and
2026 * release the delwri queue reference.
2027 */
2028 error2 = xfs_buf_iowait(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002029 xfs_buf_relse(bp);
2030 if (!error)
2031 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002034 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Brian Foster7912e7f2017-06-14 21:21:45 -07002037/*
2038 * Push a single buffer on a delwri queue.
2039 *
2040 * The purpose of this function is to submit a single buffer of a delwri queue
2041 * and return with the buffer still on the original queue. The waiting delwri
2042 * buffer submission infrastructure guarantees transfer of the delwri queue
2043 * buffer reference to a temporary wait list. We reuse this infrastructure to
2044 * transfer the buffer back to the original queue.
2045 *
2046 * Note the buffer transitions from the queued state, to the submitted and wait
2047 * listed state and back to the queued state during this call. The buffer
2048 * locking and queue management logic between _delwri_pushbuf() and
2049 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2050 * before returning.
2051 */
2052int
2053xfs_buf_delwri_pushbuf(
2054 struct xfs_buf *bp,
2055 struct list_head *buffer_list)
2056{
2057 LIST_HEAD (submit_list);
2058 int error;
2059
2060 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2061
2062 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2063
2064 /*
2065 * Isolate the buffer to a new local list so we can submit it for I/O
2066 * independently from the rest of the original list.
2067 */
2068 xfs_buf_lock(bp);
2069 list_move(&bp->b_list, &submit_list);
2070 xfs_buf_unlock(bp);
2071
2072 /*
2073 * Delwri submission clears the DELWRI_Q buffer flag and returns with
Brian Fostere339dd82018-07-11 22:26:34 -07002074 * the buffer on the wait list with the original reference. Rather than
Brian Foster7912e7f2017-06-14 21:21:45 -07002075 * bounce the buffer from a local wait list back to the original list
2076 * after I/O completion, reuse the original list as the wait list.
2077 */
2078 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2079
2080 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002081 * The buffer is now locked, under I/O and wait listed on the original
2082 * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2083 * return with the buffer unlocked and on the original queue.
Brian Foster7912e7f2017-06-14 21:21:45 -07002084 */
Brian Fostere339dd82018-07-11 22:26:34 -07002085 error = xfs_buf_iowait(bp);
Brian Foster7912e7f2017-06-14 21:21:45 -07002086 bp->b_flags |= _XBF_DELWRI_Q;
2087 xfs_buf_unlock(bp);
2088
2089 return error;
2090}
2091
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002092int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002093xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Carlos Maiolinob1231762019-11-14 12:43:03 -08002095 xfs_buf_zone = kmem_cache_create("xfs_buf",
2096 sizeof(struct xfs_buf), 0,
2097 SLAB_HWCACHE_ALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002098 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002099 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002100
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002103 out:
Nathan Scott87582802006-03-14 13:18:19 +11002104 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107void
Nathan Scottce8e9222006-01-11 15:39:08 +11002108xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08002110 kmem_cache_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
Brian Foster7561d272017-10-17 14:16:29 -07002112
2113void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
2114{
Brian Foster7561d272017-10-17 14:16:29 -07002115 /*
2116 * Set the lru reference count to 0 based on the error injection tag.
2117 * This allows userspace to disrupt buffer caching for debug/testing
2118 * purposes.
2119 */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002120 if (XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_LRU_REF))
Brian Foster7561d272017-10-17 14:16:29 -07002121 lru_ref = 0;
2122
2123 atomic_set(&bp->b_lru_ref, lru_ref);
2124}
Brian Foster8473fee2019-02-07 10:45:46 -08002125
2126/*
2127 * Verify an on-disk magic value against the magic value specified in the
2128 * verifier structure. The verifier magic is in disk byte order so the caller is
2129 * expected to pass the value directly from disk.
2130 */
2131bool
2132xfs_verify_magic(
2133 struct xfs_buf *bp,
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002134 __be32 dmagic)
Brian Foster8473fee2019-02-07 10:45:46 -08002135{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002136 struct xfs_mount *mp = bp->b_mount;
Brian Foster8473fee2019-02-07 10:45:46 -08002137 int idx;
2138
2139 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002140 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic[idx]))
Brian Foster8473fee2019-02-07 10:45:46 -08002141 return false;
2142 return dmagic == bp->b_ops->magic[idx];
2143}
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002144/*
2145 * Verify an on-disk magic value against the magic value specified in the
2146 * verifier structure. The verifier magic is in disk byte order so the caller is
2147 * expected to pass the value directly from disk.
2148 */
2149bool
2150xfs_verify_magic16(
2151 struct xfs_buf *bp,
2152 __be16 dmagic)
2153{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002154 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002155 int idx;
2156
2157 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002158 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic16[idx]))
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002159 return false;
2160 return dmagic == bp->b_ops->magic16[idx];
2161}