blob: 871abaabff3d6607a53602a95e545c88010e17da [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)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100730 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500731 "%s: failed to map pagesn", __func__);
Dave Chinnera8acad72012-04-23 15:58:54 +1000732 xfs_buf_relse(bp);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800733 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735 }
736
Dave Chinnerb79f4a12016-01-12 07:03:44 +1100737 /*
738 * Clear b_error if this is a lookup from a caller that doesn't expect
739 * valid data to be found in the buffer.
740 */
741 if (!(flags & XBF_READ))
742 xfs_buf_ioerror(bp, 0);
743
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100744 XFS_STATS_INC(target->bt_mount, xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000745 trace_xfs_buf_get(bp, flags, _RET_IP_);
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800746 *bpp = bp;
747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100750STATIC int
751_xfs_buf_read(
752 xfs_buf_t *bp,
753 xfs_buf_flags_t flags)
754{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000755 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600756 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100757
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000758 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200759 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100760
Brian Foster6af88cd2018-07-11 22:26:35 -0700761 return xfs_buf_submit(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100762}
763
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100764/*
Brian Foster75d02302019-02-06 09:25:29 -0800765 * Reverify a buffer found in cache without an attached ->b_ops.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800766 *
Brian Foster75d02302019-02-06 09:25:29 -0800767 * If the caller passed an ops structure and the buffer doesn't have ops
768 * assigned, set the ops and use it to verify the contents. If verification
769 * fails, clear XBF_DONE. We assume the buffer has no recorded errors and is
770 * already in XBF_DONE state on entry.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800771 *
Brian Foster75d02302019-02-06 09:25:29 -0800772 * Under normal operations, every in-core buffer is verified on read I/O
773 * completion. There are two scenarios that can lead to in-core buffers without
774 * an assigned ->b_ops. The first is during log recovery of buffers on a V4
775 * filesystem, though these buffers are purged at the end of recovery. The
776 * other is online repair, which intentionally reads with a NULL buffer ops to
777 * run several verifiers across an in-core buffer in order to establish buffer
778 * type. If repair can't establish that, the buffer will be left in memory
779 * with NULL buffer ops.
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100780 */
781int
Brian Foster75d02302019-02-06 09:25:29 -0800782xfs_buf_reverify(
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100783 struct xfs_buf *bp,
784 const struct xfs_buf_ops *ops)
785{
786 ASSERT(bp->b_flags & XBF_DONE);
787 ASSERT(bp->b_error == 0);
788
789 if (!ops || bp->b_ops)
790 return 0;
791
792 bp->b_ops = ops;
793 bp->b_ops->verify_read(bp);
794 if (bp->b_error)
795 bp->b_flags &= ~XBF_DONE;
796 return bp->b_error;
797}
798
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800799int
Dave Chinner6dde2702012-06-22 18:50:10 +1000800xfs_buf_read_map(
801 struct xfs_buftarg *target,
802 struct xfs_buf_map *map,
803 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100804 xfs_buf_flags_t flags,
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800805 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100806 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Dave Chinner6dde2702012-06-22 18:50:10 +1000808 struct xfs_buf *bp;
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800809 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Nathan Scottce8e9222006-01-11 15:39:08 +1100811 flags |= XBF_READ;
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800812 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Darrick J. Wong3848b5f2020-01-23 17:01:15 -0800814 error = xfs_buf_get_map(target, map, nmaps, flags, &bp);
815 if (error)
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800816 return error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000817
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100818 trace_xfs_buf_read(bp, flags, _RET_IP_);
819
820 if (!(bp->b_flags & XBF_DONE)) {
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800821 /* Initiate the buffer read and wait. */
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100822 XFS_STATS_INC(target->bt_mount, xb_get_read);
823 bp->b_ops = ops;
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800824 error = _xfs_buf_read(bp, flags);
825
826 /* Readahead iodone already dropped the buffer, so exit. */
827 if (flags & XBF_ASYNC)
828 return 0;
829 } else {
830 /* Buffer already read; all we need to do is check it. */
831 error = xfs_buf_reverify(bp, ops);
832
833 /* Readahead already finished; drop the buffer and exit. */
834 if (flags & XBF_ASYNC) {
835 xfs_buf_relse(bp);
836 return 0;
837 }
838
839 /* We do not want read in the flags */
840 bp->b_flags &= ~XBF_READ;
841 ASSERT(bp->b_ops != NULL || ops == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800844 /*
845 * If we've had a read error, then the contents of the buffer are
846 * invalid and should not be used. To ensure that a followup read tries
847 * to pull the buffer from disk again, we clear the XBF_DONE flag and
848 * mark the buffer stale. This ensures that anyone who has a current
849 * reference to the buffer will interpret it's contents correctly and
850 * future cache lookups will also treat it as an empty, uninitialised
851 * buffer.
852 */
853 if (error) {
854 if (!XFS_FORCED_SHUTDOWN(target->bt_mount))
855 xfs_buf_ioerror_alert(bp, __func__);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100856
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800857 bp->b_flags &= ~XBF_DONE;
858 xfs_buf_stale(bp);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100859 xfs_buf_relse(bp);
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800860
861 /* bad CRC means corrupted metadata */
862 if (error == -EFSBADCRC)
863 error = -EFSCORRUPTED;
864 return error;
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100865 }
866
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800867 *bpp = bp;
868 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100872 * If we are not low on memory then do the readahead in a deadlock
873 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 */
875void
Dave Chinner6dde2702012-06-22 18:50:10 +1000876xfs_buf_readahead_map(
877 struct xfs_buftarg *target,
878 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100879 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100880 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800882 struct xfs_buf *bp;
883
Jan Karaefa7c9f2017-02-02 15:56:53 +0100884 if (bdi_read_congested(target->bt_bdev->bd_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return;
886
Dave Chinner6dde2702012-06-22 18:50:10 +1000887 xfs_buf_read_map(target, map, nmaps,
Darrick J. Wong4ed8e272020-01-23 17:01:16 -0800888 XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD, &bp, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Dave Chinner5adc94c2010-09-24 21:58:31 +1000891/*
892 * Read an uncached buffer from disk. Allocates and returns a locked
893 * buffer containing the disk contents or nothing.
894 */
Dave Chinnerba3726742014-10-02 09:05:32 +1000895int
Dave Chinner5adc94c2010-09-24 21:58:31 +1000896xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000897 struct xfs_buftarg *target,
898 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000899 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100900 int flags,
Dave Chinnerba3726742014-10-02 09:05:32 +1000901 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100902 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000903{
Dave Chinnereab4e632012-11-12 22:54:02 +1100904 struct xfs_buf *bp;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000905
Dave Chinnerba3726742014-10-02 09:05:32 +1000906 *bpp = NULL;
907
Dave Chinnere70b73f2012-04-23 15:58:49 +1000908 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000909 if (!bp)
Dave Chinnerba3726742014-10-02 09:05:32 +1000910 return -ENOMEM;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000911
912 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000913 ASSERT(bp->b_map_count == 1);
Dave Chinnerba3726742014-10-02 09:05:32 +1000914 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
Dave Chinner3e85c862012-06-22 18:50:09 +1000915 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000916 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100917 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000918
Brian Foster6af88cd2018-07-11 22:26:35 -0700919 xfs_buf_submit(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000920 if (bp->b_error) {
921 int error = bp->b_error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800922 xfs_buf_relse(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000923 return error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800924 }
Dave Chinnerba3726742014-10-02 09:05:32 +1000925
926 *bpp = bp;
927 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000931xfs_buf_get_uncached(
932 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000933 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000934 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000936 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000937 int error, i;
Dave Chinner3e85c862012-06-22 18:50:09 +1000938 struct xfs_buf *bp;
939 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Brian Fosterc891c302016-07-20 11:13:43 +1000941 /* flags might contain irrelevant bits, pass only what we care about */
Darrick J. Wong32dff5e2020-01-23 17:01:15 -0800942 error = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT, &bp);
943 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Dave Chinnere70b73f2012-04-23 15:58:49 +1000946 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000947 error = _xfs_buf_get_pages(bp, page_count);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000948 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 goto fail_free_buf;
950
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000951 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000952 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000953 if (!bp->b_pages[i])
954 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000956 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Dave Chinner611c9942012-04-23 15:59:07 +1000958 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000959 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100960 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500961 "%s: failed to map pages", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Dave Chinner686865f2010-09-24 20:07:47 +1000965 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000969 while (--i >= 0)
970 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000971 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 fail_free_buf:
Dave Chinner3e85c862012-06-22 18:50:09 +1000973 xfs_buf_free_maps(bp);
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800974 kmem_cache_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 fail:
976 return NULL;
977}
978
979/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * Increment reference count on buffer, to hold the buffer concurrently
981 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * Must hold the buffer already to call this function.
983 */
984void
Nathan Scottce8e9222006-01-11 15:39:08 +1100985xfs_buf_hold(
986 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000988 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100989 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992/*
Brian Foster9c7504a2016-07-20 11:15:28 +1000993 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
994 * placed on LRU or freed (depending on b_lru_ref).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 */
996void
Nathan Scottce8e9222006-01-11 15:39:08 +1100997xfs_buf_rele(
998 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Dave Chinner74f75a02010-09-24 19:59:04 +10001000 struct xfs_perag *pag = bp->b_pag;
Brian Foster9c7504a2016-07-20 11:15:28 +10001001 bool release;
1002 bool freebuf = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001004 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Dave Chinner74f75a02010-09-24 19:59:04 +10001006 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +11001007 ASSERT(list_empty(&bp->b_lru));
Brian Foster9c7504a2016-07-20 11:15:28 +10001008 if (atomic_dec_and_test(&bp->b_hold)) {
1009 xfs_buf_ioacct_dec(bp);
Nathan Scottfad3aa12006-02-01 12:14:52 +11001010 xfs_buf_free(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001011 }
Nathan Scottfad3aa12006-02-01 12:14:52 +11001012 return;
1013 }
1014
Lachlan McIlroy37906892008-08-13 15:42:10 +10001015 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinnera4082352013-08-28 10:18:06 +10001016
Dave Chinner37fd1672018-10-18 17:21:29 +11001017 /*
1018 * We grab the b_lock here first to serialise racing xfs_buf_rele()
1019 * calls. The pag_buf_lock being taken on the last reference only
1020 * serialises against racing lookups in xfs_buf_find(). IOWs, the second
1021 * to last reference we drop here is not serialised against the last
1022 * reference until we take bp->b_lock. Hence if we don't grab b_lock
1023 * first, the last "release" reference can win the race to the lock and
1024 * free the buffer before the second-to-last reference is processed,
1025 * leading to a use-after-free scenario.
1026 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001027 spin_lock(&bp->b_lock);
Dave Chinner37fd1672018-10-18 17:21:29 +11001028 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +10001029 if (!release) {
1030 /*
1031 * Drop the in-flight state if the buffer is already on the LRU
1032 * and it holds the only reference. This is racy because we
1033 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1034 * ensures the decrement occurs only once per-buf.
1035 */
1036 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
Brian Foster63db7c82017-05-31 08:22:52 -07001037 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001038 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
Brian Foster9c7504a2016-07-20 11:15:28 +10001040
1041 /* the last reference has been dropped ... */
Brian Foster63db7c82017-05-31 08:22:52 -07001042 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001043 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1044 /*
1045 * If the buffer is added to the LRU take a new reference to the
1046 * buffer for the LRU and clear the (now stale) dispose list
1047 * state flag
1048 */
1049 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1050 bp->b_state &= ~XFS_BSTATE_DISPOSE;
1051 atomic_inc(&bp->b_hold);
1052 }
1053 spin_unlock(&pag->pag_buf_lock);
1054 } else {
1055 /*
1056 * most of the time buffers will already be removed from the
1057 * LRU, so optimise that case by checking for the
1058 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1059 * was on was the disposal list
1060 */
1061 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1062 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1063 } else {
1064 ASSERT(list_empty(&bp->b_lru));
1065 }
1066
1067 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Lucas Stach6031e732016-12-07 17:36:36 +11001068 rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
1069 xfs_buf_hash_params);
Brian Foster9c7504a2016-07-20 11:15:28 +10001070 spin_unlock(&pag->pag_buf_lock);
1071 xfs_perag_put(pag);
1072 freebuf = true;
1073 }
1074
1075out_unlock:
1076 spin_unlock(&bp->b_lock);
1077
1078 if (freebuf)
1079 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082
1083/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001084 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +11001085 *
1086 * If we come across a stale, pinned, locked buffer, we know that we are
1087 * being asked to lock a buffer that has been reallocated. Because it is
1088 * pinned, we know that the log has not been pushed to disk and hence it
1089 * will still be locked. Rather than continuing to have trylock attempts
1090 * fail until someone else pushes the log, push it ourselves before
1091 * returning. This means that the xfsaild will not get stuck trying
1092 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 */
1094int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001095xfs_buf_trylock(
1096 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 int locked;
1099
Nathan Scottce8e9222006-01-11 15:39:08 +11001100 locked = down_trylock(&bp->b_sema) == 0;
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001101 if (locked)
Darrick J. Wong479c6412016-06-21 11:53:28 +10001102 trace_xfs_buf_trylock(bp, _RET_IP_);
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001103 else
Darrick J. Wong479c6412016-06-21 11:53:28 +10001104 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001105 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001109 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +10001110 *
1111 * If we come across a stale, pinned, locked buffer, we know that we
1112 * are being asked to lock a buffer that has been reallocated. Because
1113 * it is pinned, we know that the log has not been pushed to disk and
1114 * hence it will still be locked. Rather than sleeping until someone
1115 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001117void
1118xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001119 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001121 trace_xfs_buf_lock(bp, _RET_IP_);
1122
Dave Chinnered3b4d62010-05-21 12:07:08 +10001123 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001124 xfs_log_force(bp->b_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +11001125 down(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001126
1127 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130void
Nathan Scottce8e9222006-01-11 15:39:08 +11001131xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001132 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Brian Foster20e8a062017-04-21 12:40:44 -07001134 ASSERT(xfs_buf_islocked(bp));
1135
Nathan Scottce8e9222006-01-11 15:39:08 +11001136 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001137 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
Nathan Scottce8e9222006-01-11 15:39:08 +11001140STATIC void
1141xfs_buf_wait_unpin(
1142 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 DECLARE_WAITQUEUE (wait, current);
1145
Nathan Scottce8e9222006-01-11 15:39:08 +11001146 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return;
1148
Nathan Scottce8e9222006-01-11 15:39:08 +11001149 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 for (;;) {
1151 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001152 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001154 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001156 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 set_current_state(TASK_RUNNING);
1158}
1159
1160/*
1161 * Buffer Utility Routines
1162 */
1163
Dave Chinnere8aaba92014-10-02 09:04:22 +10001164void
1165xfs_buf_ioend(
1166 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001168 bool read = bp->b_flags & XBF_READ;
1169
1170 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001171
1172 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Dave Chinnerd5929de2013-02-27 13:25:54 +11001173
Dave Chinner61be9c52014-10-02 09:04:31 +10001174 /*
1175 * Pull in IO completion errors now. We are guaranteed to be running
1176 * single threaded, so we don't need the lock to read b_io_error.
1177 */
1178 if (!bp->b_error && bp->b_io_error)
1179 xfs_buf_ioerror(bp, bp->b_io_error);
1180
Dave Chinnere8aaba92014-10-02 09:04:22 +10001181 /* Only validate buffers that were read without errors */
1182 if (read && !bp->b_error && bp->b_ops) {
1183 ASSERT(!bp->b_iodone);
Dave Chinner1813dd62012-11-14 17:54:40 +11001184 bp->b_ops->verify_read(bp);
Dave Chinnere8aaba92014-10-02 09:04:22 +10001185 }
1186
1187 if (!bp->b_error)
1188 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Christoph Hellwig80f6c292010-08-18 05:29:11 -04001190 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001191 (*(bp->b_iodone))(bp);
1192 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 xfs_buf_relse(bp);
Dave Chinner595bff72014-10-02 09:05:14 +10001194 else
Dave Chinner1813dd62012-11-14 17:54:40 +11001195 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Dave Chinnere8aaba92014-10-02 09:04:22 +10001198static void
1199xfs_buf_ioend_work(
1200 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001202 struct xfs_buf *bp =
Brian Fosterb29c70f2014-12-04 09:43:17 +11001203 container_of(work, xfs_buf_t, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001204
Dave Chinnere8aaba92014-10-02 09:04:22 +10001205 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001208static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001209xfs_buf_ioend_async(
1210 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001212 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001213 queue_work(bp->b_mount->m_buf_workqueue, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216void
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001217__xfs_buf_ioerror(
Nathan Scottce8e9222006-01-11 15:39:08 +11001218 xfs_buf_t *bp,
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001219 int error,
1220 xfs_failaddr_t failaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Dave Chinner24513372014-06-25 14:58:08 +10001222 ASSERT(error <= 0 && error >= -1000);
1223 bp->b_error = error;
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001224 trace_xfs_buf_ioerror(bp, error, failaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Christoph Hellwig901796a2011-10-10 16:52:49 +00001227void
1228xfs_buf_ioerror_alert(
1229 struct xfs_buf *bp,
1230 const char *func)
1231{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001232 xfs_alert(bp->b_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001233"metadata I/O error in \"%s\" at daddr 0x%llx len %d error %d",
1234 func, (uint64_t)XFS_BUF_ADDR(bp), bp->b_length,
1235 -bp->b_error);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001236}
1237
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001238int
1239xfs_bwrite(
1240 struct xfs_buf *bp)
1241{
1242 int error;
1243
1244 ASSERT(xfs_buf_islocked(bp));
1245
1246 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001247 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
1248 XBF_WRITE_FAIL | XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001249
Brian Foster6af88cd2018-07-11 22:26:35 -07001250 error = xfs_buf_submit(bp);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001251 if (error)
1252 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001253 return error;
1254}
1255
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001256static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001257xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001258 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001260 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Dave Chinner37eb17e2012-11-12 22:09:46 +11001262 /*
1263 * don't overwrite existing errors - otherwise we can lose errors on
1264 * buffers that require multiple bios to complete.
1265 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001266 if (bio->bi_status) {
1267 int error = blk_status_to_errno(bio->bi_status);
1268
1269 cmpxchg(&bp->b_io_error, 0, error);
1270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Dave Chinner37eb17e2012-11-12 22:09:46 +11001272 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001273 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1274
Dave Chinnere8aaba92014-10-02 09:04:22 +10001275 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1276 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Dave Chinner3e85c862012-06-22 18:50:09 +10001280static void
1281xfs_buf_ioapply_map(
1282 struct xfs_buf *bp,
1283 int map,
1284 int *buf_offset,
1285 int *count,
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001286 int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Dave Chinner3e85c862012-06-22 18:50:09 +10001288 int page_index;
1289 int total_nr_pages = bp->b_page_count;
1290 int nr_pages;
1291 struct bio *bio;
1292 sector_t sector = bp->b_maps[map].bm_bn;
1293 int size;
1294 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Dave Chinner3e85c862012-06-22 18:50:09 +10001296 /* skip the pages in the buffer before the start offset */
1297 page_index = 0;
1298 offset = *buf_offset;
1299 while (offset >= PAGE_SIZE) {
1300 page_index++;
1301 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001302 }
1303
Dave Chinner3e85c862012-06-22 18:50:09 +10001304 /*
1305 * Limit the IO size to the length of the current vector, and update the
1306 * remaining IO count for the next time around.
1307 */
1308 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1309 *count -= size;
1310 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001313 atomic_inc(&bp->b_io_remaining);
Ming Leic908e382016-05-30 21:34:33 +08001314 nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 bio = bio_alloc(GFP_NOIO, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001317 bio_set_dev(bio, bp->b_target->bt_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001318 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001319 bio->bi_end_io = xfs_buf_bio_end_io;
1320 bio->bi_private = bp;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001321 bio->bi_opf = op;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001322
Dave Chinner3e85c862012-06-22 18:50:09 +10001323 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001324 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 if (nbytes > size)
1327 nbytes = size;
1328
Dave Chinner3e85c862012-06-22 18:50:09 +10001329 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1330 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001331 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 break;
1333
1334 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001335 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 size -= nbytes;
1337 total_nr_pages--;
1338 }
1339
Kent Overstreet4f024f32013-10-11 15:44:27 -07001340 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001341 if (xfs_buf_is_vmapped(bp)) {
1342 flush_kernel_vmap_range(bp->b_addr,
1343 xfs_buf_vmap_len(bp));
1344 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001345 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (size)
1347 goto next_chunk;
1348 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001349 /*
1350 * This is guaranteed not to be the last io reference count
Dave Chinner595bff72014-10-02 09:05:14 +10001351 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001352 */
1353 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001354 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001355 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001357
1358}
1359
1360STATIC void
1361_xfs_buf_ioapply(
1362 struct xfs_buf *bp)
1363{
1364 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001365 int op;
Dave Chinner3e85c862012-06-22 18:50:09 +10001366 int offset;
1367 int size;
1368 int i;
1369
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001370 /*
1371 * Make sure we capture only current IO errors rather than stale errors
1372 * left over from previous use of the buffer (e.g. failed readahead).
1373 */
1374 bp->b_error = 0;
1375
Dave Chinner3e85c862012-06-22 18:50:09 +10001376 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001377 op = REQ_OP_WRITE;
Dave Chinner1813dd62012-11-14 17:54:40 +11001378
1379 /*
1380 * Run the write verifier callback function if it exists. If
1381 * this function fails it will mark the buffer with an error and
1382 * the IO should not be dispatched.
1383 */
1384 if (bp->b_ops) {
1385 bp->b_ops->verify_write(bp);
1386 if (bp->b_error) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001387 xfs_force_shutdown(bp->b_mount,
Dave Chinner1813dd62012-11-14 17:54:40 +11001388 SHUTDOWN_CORRUPT_INCORE);
1389 return;
1390 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001391 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001392 struct xfs_mount *mp = bp->b_mount;
Dave Chinner400b9d82014-08-04 12:42:40 +10001393
1394 /*
1395 * non-crc filesystems don't attach verifiers during
1396 * log recovery, so don't warn for such filesystems.
1397 */
1398 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1399 xfs_warn(mp,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001400 "%s: no buf ops on daddr 0x%llx len %d",
Dave Chinner400b9d82014-08-04 12:42:40 +10001401 __func__, bp->b_bn, bp->b_length);
Darrick J. Wong9c712a12018-01-08 10:51:26 -08001402 xfs_hex_dump(bp->b_addr,
1403 XFS_CORRUPTION_DUMP_LEN);
Dave Chinner400b9d82014-08-04 12:42:40 +10001404 dump_stack();
1405 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001406 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001407 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001408 op = REQ_OP_READ;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001409 if (bp->b_flags & XBF_READ_AHEAD)
1410 op |= REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001411 }
1412
1413 /* we only use the buffer cache for meta-data */
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001414 op |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001415
1416 /*
1417 * Walk all the vectors issuing IO on them. Set up the initial offset
1418 * into the buffer and the desired IO size before we start -
1419 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1420 * subsequent call.
1421 */
1422 offset = bp->b_offset;
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001423 size = BBTOB(bp->b_length);
Dave Chinner3e85c862012-06-22 18:50:09 +10001424 blk_start_plug(&plug);
1425 for (i = 0; i < bp->b_map_count; i++) {
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001426 xfs_buf_ioapply_map(bp, i, &offset, &size, op);
Dave Chinner3e85c862012-06-22 18:50:09 +10001427 if (bp->b_error)
1428 break;
1429 if (size <= 0)
1430 break; /* all done */
1431 }
1432 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
Dave Chinner595bff72014-10-02 09:05:14 +10001435/*
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001436 * Wait for I/O completion of a sync buffer and return the I/O error code.
Dave Chinner595bff72014-10-02 09:05:14 +10001437 */
Brian Fostereaebb512018-07-11 22:26:34 -07001438static int
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001439xfs_buf_iowait(
Dave Chinner595bff72014-10-02 09:05:14 +10001440 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001442 ASSERT(!(bp->b_flags & XBF_ASYNC));
1443
1444 trace_xfs_buf_iowait(bp, _RET_IP_);
1445 wait_for_completion(&bp->b_iowait);
1446 trace_xfs_buf_iowait_done(bp, _RET_IP_);
1447
1448 return bp->b_error;
1449}
1450
1451/*
1452 * Buffer I/O submission path, read or write. Asynchronous submission transfers
1453 * the buffer lock ownership and the current reference to the IO. It is not
1454 * safe to reference the buffer after a call to this function unless the caller
1455 * holds an additional reference itself.
1456 */
1457int
1458__xfs_buf_submit(
1459 struct xfs_buf *bp,
1460 bool wait)
1461{
1462 int error = 0;
1463
Dave Chinner595bff72014-10-02 09:05:14 +10001464 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001466 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff72014-10-02 09:05:14 +10001467
1468 /* on shutdown we stale and complete the buffer immediately */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001469 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
Dave Chinner595bff72014-10-02 09:05:14 +10001470 xfs_buf_ioerror(bp, -EIO);
1471 bp->b_flags &= ~XBF_DONE;
1472 xfs_buf_stale(bp);
Brian Foster465fa172019-02-03 14:03:06 -08001473 xfs_buf_ioend(bp);
Brian Fostereaebb512018-07-11 22:26:34 -07001474 return -EIO;
Dave Chinner595bff72014-10-02 09:05:14 +10001475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001477 /*
1478 * Grab a reference so the buffer does not go away underneath us. For
1479 * async buffers, I/O completion drops the callers reference, which
1480 * could occur before submission returns.
1481 */
1482 xfs_buf_hold(bp);
1483
Christoph Hellwig375ec692011-08-23 08:28:03 +00001484 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001485 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Dave Chinner61be9c52014-10-02 09:04:31 +10001487 /* clear the internal error state to avoid spurious errors */
1488 bp->b_io_error = 0;
1489
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001490 /*
Brian Fostereaebb512018-07-11 22:26:34 -07001491 * Set the count to 1 initially, this will stop an I/O completion
1492 * callout which happens before we have started all the I/O from calling
1493 * xfs_buf_ioend too early.
1494 */
1495 atomic_set(&bp->b_io_remaining, 1);
1496 if (bp->b_flags & XBF_ASYNC)
1497 xfs_buf_ioacct_inc(bp);
1498 _xfs_buf_ioapply(bp);
1499
1500 /*
1501 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1502 * reference we took above. If we drop it to zero, run completion so
1503 * that we don't return to the caller with completion still pending.
1504 */
1505 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1506 if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
1507 xfs_buf_ioend(bp);
1508 else
1509 xfs_buf_ioend_async(bp);
1510 }
1511
Brian Foster6af88cd2018-07-11 22:26:35 -07001512 if (wait)
1513 error = xfs_buf_iowait(bp);
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001514
Dave Chinner595bff72014-10-02 09:05:14 +10001515 /*
Brian Foster6af88cd2018-07-11 22:26:35 -07001516 * Release the hold that keeps the buffer referenced for the entire
1517 * I/O. Note that if the buffer is async, it is not safe to reference
1518 * after this release.
Dave Chinner595bff72014-10-02 09:05:14 +10001519 */
1520 xfs_buf_rele(bp);
1521 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001524void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001525xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001526 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 size_t offset)
1528{
1529 struct page *page;
1530
Dave Chinner611c9942012-04-23 15:59:07 +10001531 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001532 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Nathan Scottce8e9222006-01-11 15:39:08 +11001534 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001535 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001536 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539void
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001540xfs_buf_zero(
1541 struct xfs_buf *bp,
1542 size_t boff,
1543 size_t bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Dave Chinner795cac72012-04-23 15:58:53 +10001545 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 bend = boff + bsize;
1548 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001549 struct page *page;
1550 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Dave Chinner795cac72012-04-23 15:58:53 +10001552 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1553 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1554 page = bp->b_pages[page_index];
1555 csize = min_t(size_t, PAGE_SIZE - page_offset,
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001556 BBTOB(bp->b_length) - boff);
Dave Chinner795cac72012-04-23 15:58:53 +10001557
1558 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001560 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 boff += csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564}
1565
1566/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001567 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 */
1569
1570/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001571 * Wait for any bufs with callbacks that have been submitted but have not yet
1572 * returned. These buffers will have an elevated hold count, so wait on those
1573 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001575static enum lru_status
1576xfs_buftarg_wait_rele(
1577 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001578 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001579 spinlock_t *lru_lock,
1580 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Dave Chinnere80dfa12013-08-28 10:18:05 +10001582{
1583 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001584 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001585
1586 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001587 /* need to wait, so skip it this pass */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001588 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001589 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Dave Chinnera4082352013-08-28 10:18:06 +10001591 if (!spin_trylock(&bp->b_lock))
1592 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001593
Dave Chinnera4082352013-08-28 10:18:06 +10001594 /*
1595 * clear the LRU reference count so the buffer doesn't get
1596 * ignored in xfs_buf_rele().
1597 */
1598 atomic_set(&bp->b_lru_ref, 0);
1599 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001600 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001601 spin_unlock(&bp->b_lock);
1602 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
Dave Chinnere80dfa12013-08-28 10:18:05 +10001605void
1606xfs_wait_buftarg(
1607 struct xfs_buftarg *btp)
1608{
Dave Chinnera4082352013-08-28 10:18:06 +10001609 LIST_HEAD(dispose);
1610 int loop = 0;
1611
Dave Chinner85bec542016-01-19 08:28:10 +11001612 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001613 * First wait on the buftarg I/O count for all in-flight buffers to be
1614 * released. This is critical as new buffers do not make the LRU until
1615 * they are released.
1616 *
1617 * Next, flush the buffer workqueue to ensure all completion processing
1618 * has finished. Just waiting on buffer locks is not sufficient for
1619 * async IO as the reference count held over IO is not released until
1620 * after the buffer lock is dropped. Hence we need to ensure here that
1621 * all reference counts have been dropped before we start walking the
1622 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001623 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001624 while (percpu_counter_sum(&btp->bt_io_count))
1625 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001626 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Dave Chinner85bec542016-01-19 08:28:10 +11001627
Dave Chinnera4082352013-08-28 10:18:06 +10001628 /* loop until there is nothing left on the lru list. */
1629 while (list_lru_count(&btp->bt_lru)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001630 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001631 &dispose, LONG_MAX);
1632
1633 while (!list_empty(&dispose)) {
1634 struct xfs_buf *bp;
1635 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1636 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001637 if (bp->b_flags & XBF_WRITE_FAIL) {
1638 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001639"Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001640 (long long)bp->b_bn);
Joe Perchesf41febd2015-07-29 11:52:04 +10001641 xfs_alert(btp->bt_mount,
1642"Please run xfs_repair to determine the extent of the problem.");
Dave Chinnerac8809f2013-12-12 16:34:38 +11001643 }
Dave Chinnera4082352013-08-28 10:18:06 +10001644 xfs_buf_rele(bp);
1645 }
1646 if (loop++ != 0)
1647 delay(100);
1648 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001649}
1650
1651static enum lru_status
1652xfs_buftarg_isolate(
1653 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001654 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001655 spinlock_t *lru_lock,
1656 void *arg)
1657{
1658 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1659 struct list_head *dispose = arg;
1660
1661 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001662 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1663 * If we fail to get the lock, just skip it.
1664 */
1665 if (!spin_trylock(&bp->b_lock))
1666 return LRU_SKIP;
1667 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001668 * Decrement the b_lru_ref count unless the value is already
1669 * zero. If the value is already zero, we need to reclaim the
1670 * buffer, otherwise it gets another trip through the LRU.
1671 */
Vratislav Bendel19957a12018-03-06 17:07:44 -08001672 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
Dave Chinnera4082352013-08-28 10:18:06 +10001673 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001674 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001675 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001676
Dave Chinnera4082352013-08-28 10:18:06 +10001677 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001678 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001679 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001680 return LRU_REMOVED;
1681}
1682
Andrew Mortonaddbda42013-08-28 10:18:06 +10001683static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001684xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001685 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001686 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001687{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001688 struct xfs_buftarg *btp = container_of(shrink,
1689 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001690 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001691 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001692
Vladimir Davydov503c3582015-02-12 14:58:47 -08001693 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1694 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001695
1696 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001697 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001698 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1699 list_del_init(&bp->b_lru);
1700 xfs_buf_rele(bp);
1701 }
1702
Dave Chinnere80dfa12013-08-28 10:18:05 +10001703 return freed;
1704}
1705
Andrew Mortonaddbda42013-08-28 10:18:06 +10001706static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001707xfs_buftarg_shrink_count(
1708 struct shrinker *shrink,
1709 struct shrink_control *sc)
1710{
1711 struct xfs_buftarg *btp = container_of(shrink,
1712 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001713 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001714}
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716void
1717xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001718 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001720 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001721 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1722 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001723 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001724
Dave Chinner2291dab2016-12-09 16:49:54 +11001725 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001726
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001727 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001730int
1731xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001733 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001735 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001736 btp->bt_meta_sectorsize = sectorsize;
1737 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Nathan Scottce8e9222006-01-11 15:39:08 +11001739 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001740 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001741 "Cannot set_blocksize to %u on device %pg",
1742 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001743 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001746 /* Set up device logical sector size mask */
1747 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1748 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return 0;
1751}
1752
1753/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001754 * When allocating the initial buffer target we have not yet
1755 * read in the superblock, so don't know what sized sectors
1756 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758STATIC int
1759xfs_setsize_buftarg_early(
1760 xfs_buftarg_t *btp,
1761 struct block_device *bdev)
1762{
Eric Sandeena96c4152014-04-14 19:00:29 +10001763 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766xfs_buftarg_t *
1767xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001768 struct xfs_mount *mp,
Dan Williams486aff52017-08-24 15:12:50 -07001769 struct block_device *bdev,
1770 struct dax_device *dax_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
1772 xfs_buftarg_t *btp;
1773
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07001774 btp = kmem_zalloc(sizeof(*btp), KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Dave Chinnerebad8612010-09-22 10:47:20 +10001776 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001777 btp->bt_dev = bdev->bd_dev;
1778 btp->bt_bdev = bdev;
Dan Williams486aff52017-08-24 15:12:50 -07001779 btp->bt_daxdev = dax_dev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (xfs_setsize_buftarg_early(btp, bdev))
Michal Hockod210a982017-11-23 17:13:40 +01001782 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001783
1784 if (list_lru_init(&btp->bt_lru))
Michal Hockod210a982017-11-23 17:13:40 +01001785 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001786
Brian Foster9c7504a2016-07-20 11:15:28 +10001787 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
Michal Hockod210a982017-11-23 17:13:40 +01001788 goto error_lru;
Brian Foster9c7504a2016-07-20 11:15:28 +10001789
Dave Chinnere80dfa12013-08-28 10:18:05 +10001790 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1791 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001792 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001793 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Michal Hockod210a982017-11-23 17:13:40 +01001794 if (register_shrinker(&btp->bt_shrinker))
1795 goto error_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return btp;
1797
Michal Hockod210a982017-11-23 17:13:40 +01001798error_pcpu:
1799 percpu_counter_destroy(&btp->bt_io_count);
1800error_lru:
1801 list_lru_destroy(&btp->bt_lru);
1802error_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001803 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return NULL;
1805}
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807/*
Brian Foster20e8a062017-04-21 12:40:44 -07001808 * Cancel a delayed write list.
1809 *
1810 * Remove each buffer from the list, clear the delwri queue flag and drop the
1811 * associated buffer reference.
1812 */
1813void
1814xfs_buf_delwri_cancel(
1815 struct list_head *list)
1816{
1817 struct xfs_buf *bp;
1818
1819 while (!list_empty(list)) {
1820 bp = list_first_entry(list, struct xfs_buf, b_list);
1821
1822 xfs_buf_lock(bp);
1823 bp->b_flags &= ~_XBF_DELWRI_Q;
1824 list_del_init(&bp->b_list);
1825 xfs_buf_relse(bp);
1826 }
1827}
1828
1829/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001830 * Add a buffer to the delayed write list.
1831 *
1832 * This queues a buffer for writeout if it hasn't already been. Note that
1833 * neither this routine nor the buffer list submission functions perform
1834 * any internal synchronization. It is expected that the lists are thread-local
1835 * to the callers.
1836 *
1837 * Returns true if we queued up the buffer, or false if it already had
1838 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001840bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001841xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001842 struct xfs_buf *bp,
1843 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001845 ASSERT(xfs_buf_islocked(bp));
1846 ASSERT(!(bp->b_flags & XBF_READ));
1847
1848 /*
1849 * If the buffer is already marked delwri it already is queued up
1850 * by someone else for imediate writeout. Just ignore it in that
1851 * case.
1852 */
1853 if (bp->b_flags & _XBF_DELWRI_Q) {
1854 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1855 return false;
1856 }
David Chinnera6867a62006-01-11 15:37:58 +11001857
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001858 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1859
Dave Chinnerd808f612010-02-02 10:13:42 +11001860 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001861 * If a buffer gets written out synchronously or marked stale while it
1862 * is on a delwri list we lazily remove it. To do this, the other party
1863 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1864 * It remains referenced and on the list. In a rare corner case it
1865 * might get readded to a delwri list after the synchronous writeout, in
1866 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001867 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001868 bp->b_flags |= _XBF_DELWRI_Q;
1869 if (list_empty(&bp->b_list)) {
1870 atomic_inc(&bp->b_hold);
1871 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001872 }
David Chinner585e6d82007-02-10 18:32:29 +11001873
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001874 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001875}
1876
Dave Chinner089716a2010-01-26 15:13:25 +11001877/*
1878 * Compare function is more complex than it needs to be because
1879 * the return value is only 32 bits and we are doing comparisons
1880 * on 64 bit values
1881 */
1882static int
1883xfs_buf_cmp(
1884 void *priv,
1885 struct list_head *a,
1886 struct list_head *b)
1887{
1888 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1889 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1890 xfs_daddr_t diff;
1891
Mark Tinguelyf4b42422012-12-04 17:18:02 -06001892 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11001893 if (diff < 0)
1894 return -1;
1895 if (diff > 0)
1896 return 1;
1897 return 0;
1898}
1899
Dave Chinner26f1fe82016-06-01 17:38:15 +10001900/*
Brian Fostere339dd82018-07-11 22:26:34 -07001901 * Submit buffers for write. If wait_list is specified, the buffers are
1902 * submitted using sync I/O and placed on the wait list such that the caller can
1903 * iowait each buffer. Otherwise async I/O is used and the buffers are released
1904 * at I/O completion time. In either case, buffers remain locked until I/O
1905 * completes and the buffer is released from the queue.
Dave Chinner26f1fe82016-06-01 17:38:15 +10001906 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001907static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10001908xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001909 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10001910 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001912 struct xfs_buf *bp, *n;
1913 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001914 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Dave Chinner26f1fe82016-06-01 17:38:15 +10001916 list_sort(NULL, buffer_list, xfs_buf_cmp);
1917
1918 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001919 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10001920 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001921 if (xfs_buf_ispinned(bp)) {
1922 pinned++;
1923 continue;
1924 }
1925 if (!xfs_buf_trylock(bp))
1926 continue;
1927 } else {
1928 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001931 /*
1932 * Someone else might have written the buffer synchronously or
1933 * marked it stale in the meantime. In that case only the
1934 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1935 * reference and remove it from the list here.
1936 */
1937 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1938 list_del_init(&bp->b_list);
1939 xfs_buf_relse(bp);
1940 continue;
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001943 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001944
Dave Chinnercf53e992014-10-02 09:04:01 +10001945 /*
Brian Fostere339dd82018-07-11 22:26:34 -07001946 * If we have a wait list, each buffer (and associated delwri
1947 * queue reference) transfers to it and is submitted
1948 * synchronously. Otherwise, drop the buffer from the delwri
1949 * queue and submit async.
Dave Chinnercf53e992014-10-02 09:04:01 +10001950 */
Dave Chinnerbbfeb612016-07-20 11:53:35 +10001951 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL);
Brian Fostere339dd82018-07-11 22:26:34 -07001952 bp->b_flags |= XBF_WRITE;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001953 if (wait_list) {
Brian Fostere339dd82018-07-11 22:26:34 -07001954 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001955 list_move_tail(&bp->b_list, wait_list);
Brian Fostere339dd82018-07-11 22:26:34 -07001956 } else {
1957 bp->b_flags |= XBF_ASYNC;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001958 list_del_init(&bp->b_list);
Brian Fostere339dd82018-07-11 22:26:34 -07001959 }
Brian Foster6af88cd2018-07-11 22:26:35 -07001960 __xfs_buf_submit(bp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001962 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001964 return pinned;
1965}
Nathan Scottf07c2252006-09-28 10:52:15 +10001966
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001967/*
1968 * Write out a buffer list asynchronously.
1969 *
1970 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1971 * out and not wait for I/O completion on any of the buffers. This interface
1972 * is only safely useable for callers that can track I/O completion by higher
1973 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1974 * function.
Brian Fosterefc32892018-10-18 17:21:49 +11001975 *
1976 * Note: this function will skip buffers it would block on, and in doing so
1977 * leaves them on @buffer_list so they can be retried on a later pass. As such,
1978 * it is up to the caller to ensure that the buffer list is fully submitted or
1979 * cancelled appropriately when they are finished with the list. Failure to
1980 * cancel or resubmit the list until it is empty will result in leaked buffers
1981 * at unmount time.
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001982 */
1983int
1984xfs_buf_delwri_submit_nowait(
1985 struct list_head *buffer_list)
1986{
Dave Chinner26f1fe82016-06-01 17:38:15 +10001987 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001988}
1989
1990/*
1991 * Write out a buffer list synchronously.
1992 *
1993 * This will take the @buffer_list, write all buffers out and wait for I/O
1994 * completion on all of the buffers. @buffer_list is consumed by the function,
1995 * so callers must have some other way of tracking buffers if they require such
1996 * functionality.
1997 */
1998int
1999xfs_buf_delwri_submit(
2000 struct list_head *buffer_list)
2001{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002002 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002003 int error = 0, error2;
2004 struct xfs_buf *bp;
2005
Dave Chinner26f1fe82016-06-01 17:38:15 +10002006 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002007
2008 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10002009 while (!list_empty(&wait_list)) {
2010 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002011
2012 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10002013
Brian Fostere339dd82018-07-11 22:26:34 -07002014 /*
2015 * Wait on the locked buffer, check for errors and unlock and
2016 * release the delwri queue reference.
2017 */
2018 error2 = xfs_buf_iowait(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002019 xfs_buf_relse(bp);
2020 if (!error)
2021 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002024 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Brian Foster7912e7f2017-06-14 21:21:45 -07002027/*
2028 * Push a single buffer on a delwri queue.
2029 *
2030 * The purpose of this function is to submit a single buffer of a delwri queue
2031 * and return with the buffer still on the original queue. The waiting delwri
2032 * buffer submission infrastructure guarantees transfer of the delwri queue
2033 * buffer reference to a temporary wait list. We reuse this infrastructure to
2034 * transfer the buffer back to the original queue.
2035 *
2036 * Note the buffer transitions from the queued state, to the submitted and wait
2037 * listed state and back to the queued state during this call. The buffer
2038 * locking and queue management logic between _delwri_pushbuf() and
2039 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2040 * before returning.
2041 */
2042int
2043xfs_buf_delwri_pushbuf(
2044 struct xfs_buf *bp,
2045 struct list_head *buffer_list)
2046{
2047 LIST_HEAD (submit_list);
2048 int error;
2049
2050 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2051
2052 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2053
2054 /*
2055 * Isolate the buffer to a new local list so we can submit it for I/O
2056 * independently from the rest of the original list.
2057 */
2058 xfs_buf_lock(bp);
2059 list_move(&bp->b_list, &submit_list);
2060 xfs_buf_unlock(bp);
2061
2062 /*
2063 * Delwri submission clears the DELWRI_Q buffer flag and returns with
Brian Fostere339dd82018-07-11 22:26:34 -07002064 * the buffer on the wait list with the original reference. Rather than
Brian Foster7912e7f2017-06-14 21:21:45 -07002065 * bounce the buffer from a local wait list back to the original list
2066 * after I/O completion, reuse the original list as the wait list.
2067 */
2068 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2069
2070 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002071 * The buffer is now locked, under I/O and wait listed on the original
2072 * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2073 * return with the buffer unlocked and on the original queue.
Brian Foster7912e7f2017-06-14 21:21:45 -07002074 */
Brian Fostere339dd82018-07-11 22:26:34 -07002075 error = xfs_buf_iowait(bp);
Brian Foster7912e7f2017-06-14 21:21:45 -07002076 bp->b_flags |= _XBF_DELWRI_Q;
2077 xfs_buf_unlock(bp);
2078
2079 return error;
2080}
2081
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002082int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002083xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Carlos Maiolinob1231762019-11-14 12:43:03 -08002085 xfs_buf_zone = kmem_cache_create("xfs_buf",
2086 sizeof(struct xfs_buf), 0,
2087 SLAB_HWCACHE_ALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002088 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002089 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002090
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002093 out:
Nathan Scott87582802006-03-14 13:18:19 +11002094 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097void
Nathan Scottce8e9222006-01-11 15:39:08 +11002098xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Carlos Maiolinoaaf54eb2019-11-14 12:43:04 -08002100 kmem_cache_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
Brian Foster7561d272017-10-17 14:16:29 -07002102
2103void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
2104{
Brian Foster7561d272017-10-17 14:16:29 -07002105 /*
2106 * Set the lru reference count to 0 based on the error injection tag.
2107 * This allows userspace to disrupt buffer caching for debug/testing
2108 * purposes.
2109 */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002110 if (XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_LRU_REF))
Brian Foster7561d272017-10-17 14:16:29 -07002111 lru_ref = 0;
2112
2113 atomic_set(&bp->b_lru_ref, lru_ref);
2114}
Brian Foster8473fee2019-02-07 10:45:46 -08002115
2116/*
2117 * Verify an on-disk magic value against the magic value specified in the
2118 * verifier structure. The verifier magic is in disk byte order so the caller is
2119 * expected to pass the value directly from disk.
2120 */
2121bool
2122xfs_verify_magic(
2123 struct xfs_buf *bp,
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002124 __be32 dmagic)
Brian Foster8473fee2019-02-07 10:45:46 -08002125{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002126 struct xfs_mount *mp = bp->b_mount;
Brian Foster8473fee2019-02-07 10:45:46 -08002127 int idx;
2128
2129 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002130 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic[idx]))
Brian Foster8473fee2019-02-07 10:45:46 -08002131 return false;
2132 return dmagic == bp->b_ops->magic[idx];
2133}
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002134/*
2135 * Verify an on-disk magic value against the magic value specified in the
2136 * verifier structure. The verifier magic is in disk byte order so the caller is
2137 * expected to pass the value directly from disk.
2138 */
2139bool
2140xfs_verify_magic16(
2141 struct xfs_buf *bp,
2142 __be16 dmagic)
2143{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002144 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002145 int idx;
2146
2147 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002148 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic16[idx]))
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002149 return false;
2150 return dmagic == bp->b_ops->magic16[idx];
2151}