blob: 2ed3c65c602fc210bd64b1090c04a1f2f4371dad [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
Christoph Hellwig05645012019-06-28 19:27:27 -0700201static struct xfs_buf *
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,
Nathan Scottce8e9222006-01-11 15:39:08 +1100206 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000208 struct xfs_buf *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000209 int error;
210 int i;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000211
Dave Chinneraa5c1582012-04-23 15:58:56 +1000212 bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000213 if (unlikely(!bp))
214 return NULL;
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /*
Dave Chinner12bcb3f2012-04-23 15:59:05 +1000217 * We don't want certain flags to appear in b_flags unless they are
218 * specifically set by later operations on the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
Dave Chinner611c9942012-04-23 15:59:07 +1000220 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Nathan Scottce8e9222006-01-11 15:39:08 +1100222 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100223 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000224 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100225 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100226 INIT_LIST_HEAD(&bp->b_list);
Carlos Maiolino643c8c02018-01-24 13:38:49 -0800227 INIT_LIST_HEAD(&bp->b_li_list);
Thomas Gleixnera731cd112010-09-07 14:33:15 +0000228 sema_init(&bp->b_sema, 0); /* held, no waiters */
Dave Chinnera4082352013-08-28 10:18:06 +1000229 spin_lock_init(&bp->b_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100230 bp->b_target = target;
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700231 bp->b_mount = target->bt_mount;
Dave Chinner3e85c862012-06-22 18:50:09 +1000232 bp->b_flags = flags;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000235 * Set length and io_length to the same value initially.
236 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * most cases but may be reset (e.g. XFS recovery).
238 */
Dave Chinner3e85c862012-06-22 18:50:09 +1000239 error = xfs_buf_get_maps(bp, nmaps);
240 if (error) {
241 kmem_zone_free(xfs_buf_zone, bp);
242 return NULL;
243 }
244
245 bp->b_bn = map[0].bm_bn;
246 bp->b_length = 0;
247 for (i = 0; i < nmaps; i++) {
248 bp->b_maps[i].bm_bn = map[i].bm_bn;
249 bp->b_maps[i].bm_len = map[i].bm_len;
250 bp->b_length += map[i].bm_len;
251 }
Dave Chinner3e85c862012-06-22 18:50:09 +1000252
Nathan Scottce8e9222006-01-11 15:39:08 +1100253 atomic_set(&bp->b_pin_count, 0);
254 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700256 XFS_STATS_INC(bp->b_mount, xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000257 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000258
259 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100263 * Allocate a page array capable of holding a specified number
264 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
266STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100267_xfs_buf_get_pages(
268 xfs_buf_t *bp,
Eric Sandeen87937bf2014-04-14 19:01:20 +1000269 int page_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100272 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100273 bp->b_page_count = page_count;
274 if (page_count <= XB_PAGES) {
275 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100277 bp->b_pages = kmem_alloc(sizeof(struct page *) *
Dave Chinneraa5c1582012-04-23 15:58:56 +1000278 page_count, KM_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +1100279 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return -ENOMEM;
281 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100282 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 return 0;
285}
286
287/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100288 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
290STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100291_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 xfs_buf_t *bp)
293{
Nathan Scottce8e9222006-01-11 15:39:08 +1100294 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000295 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000296 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298}
299
300/*
301 * Releases the specified buffer.
302 *
303 * The modification state of any associated pages is left unchanged.
Zhi Yong Wub46fe822013-08-07 10:10:59 +0000304 * The buffer must not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 * hashed and refcounted buffers
306 */
Christoph Hellwig25a40952019-10-24 22:25:37 -0700307static void
Nathan Scottce8e9222006-01-11 15:39:08 +1100308xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 xfs_buf_t *bp)
310{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000311 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Dave Chinner430cbeb2010-12-02 16:30:55 +1100313 ASSERT(list_empty(&bp->b_lru));
314
Dave Chinner0e6e8472011-03-26 09:16:45 +1100315 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 uint i;
317
James Bottomley73c77e22010-01-25 11:42:24 -0600318 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000319 vm_unmap_ram(bp->b_addr - bp->b_offset,
320 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Nathan Scott948ecdb2006-09-28 11:03:13 +1000322 for (i = 0; i < bp->b_page_count; i++) {
323 struct page *page = bp->b_pages[i];
324
Dave Chinner0e6e8472011-03-26 09:16:45 +1100325 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000326 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100327 } else if (bp->b_flags & _XBF_KMEM)
328 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000329 _xfs_buf_free_pages(bp);
Dave Chinner3e85c862012-06-22 18:50:09 +1000330 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000331 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100335 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
337STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100338xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 xfs_buf_t *bp,
340 uint flags)
341{
Dave Chinneraa0e8832012-04-23 15:58:52 +1000342 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100344 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 unsigned short page_count, i;
Dave Chinner795cac72012-04-23 15:58:53 +1000346 xfs_off_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int error;
Bill O'Donnell3219e8c2019-10-04 16:38:44 -0700348 xfs_km_flags_t kmflag_mask = 0;
349
350 /*
351 * assure zeroed buffer for non-read cases.
352 */
353 if (!(flags & XBF_READ)) {
354 kmflag_mask |= KM_ZERO;
355 gfp_mask |= __GFP_ZERO;
356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Dave Chinner0e6e8472011-03-26 09:16:45 +1100358 /*
359 * for buffers that are contained within a single page, just allocate
360 * the memory from the heap - there's no need for the complexity of
361 * page arrays to keep allocation down to order 0.
362 */
Dave Chinner795cac72012-04-23 15:58:53 +1000363 size = BBTOB(bp->b_length);
364 if (size < PAGE_SIZE) {
Dave Chinnerf8f9ee42019-08-26 12:08:39 -0700365 int align_mask = xfs_buftarg_dma_alignment(bp->b_target);
Bill O'Donnell3219e8c2019-10-04 16:38:44 -0700366 bp->b_addr = kmem_alloc_io(size, align_mask,
367 KM_NOFS | kmflag_mask);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100368 if (!bp->b_addr) {
369 /* low memory - use alloc_page loop instead */
370 goto use_alloc_page;
371 }
372
Dave Chinner795cac72012-04-23 15:58:53 +1000373 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100374 ((unsigned long)bp->b_addr & PAGE_MASK)) {
375 /* b_addr spans two pages - use alloc_page instead */
376 kmem_free(bp->b_addr);
377 bp->b_addr = NULL;
378 goto use_alloc_page;
379 }
380 bp->b_offset = offset_in_page(bp->b_addr);
381 bp->b_pages = bp->b_page_array;
Dave Chinnerf8f9ee42019-08-26 12:08:39 -0700382 bp->b_pages[0] = kmem_to_page(bp->b_addr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100383 bp->b_page_count = 1;
Dave Chinner611c9942012-04-23 15:59:07 +1000384 bp->b_flags |= _XBF_KMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100385 return 0;
386 }
387
388use_alloc_page:
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600389 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
390 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000391 >> PAGE_SHIFT;
Dave Chinner795cac72012-04-23 15:58:53 +1000392 page_count = end - start;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000393 error = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (unlikely(error))
395 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Nathan Scottce8e9222006-01-11 15:39:08 +1100397 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100398 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Nathan Scottce8e9222006-01-11 15:39:08 +1100400 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct page *page;
402 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100403retry:
404 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100406 if (flags & XBF_READ_AHEAD) {
407 bp->b_page_count = i;
Dave Chinner24513372014-06-25 14:58:08 +1000408 error = -ENOMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100409 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
412 /*
413 * This could deadlock.
414 *
415 * But until all the XFS lowlevel code is revamped to
416 * handle buffer allocation failures we can't do much.
417 */
418 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100419 xfs_err(NULL,
Tetsuo Handa5bf97b12015-10-12 15:41:29 +1100420 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
421 current->comm, current->pid,
Harvey Harrison34a622b2008-04-10 12:19:21 +1000422 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700424 XFS_STATS_INC(bp->b_mount, xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200425 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 goto retry;
427 }
428
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700429 XFS_STATS_INC(bp->b_mount, xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Dave Chinner0e6e8472011-03-26 09:16:45 +1100431 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100433 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 offset = 0;
435 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Dave Chinner0e6e8472011-03-26 09:16:45 +1100438out_free_pages:
439 for (i = 0; i < bp->b_page_count; i++)
440 __free_page(bp->b_pages[i]);
Darrick J. Wong2aa6ba7b2017-01-25 20:24:57 -0800441 bp->b_flags &= ~_XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return error;
443}
444
445/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300446 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
448STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100449_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 xfs_buf_t *bp,
451 uint flags)
452{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100453 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100454 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100455 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100456 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
Dave Chinner611c9942012-04-23 15:59:07 +1000457 } else if (flags & XBF_UNMAPPED) {
458 bp->b_addr = NULL;
459 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100460 int retried = 0;
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700461 unsigned nofs_flag;
Dave Chinnera19fb382011-03-26 09:13:42 +1100462
Dave Chinnerae687e52014-03-07 16:19:14 +1100463 /*
Joe Perchescf085a12019-11-07 13:24:52 -0800464 * vm_map_ram() will allocate auxiliary structures (e.g.
Dave Chinnerae687e52014-03-07 16:19:14 +1100465 * pagetables) with GFP_KERNEL, yet we are likely to be under
466 * GFP_NOFS context here. Hence we need to tell memory reclaim
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700467 * that we are in such a context via PF_MEMALLOC_NOFS to prevent
Dave Chinnerae687e52014-03-07 16:19:14 +1100468 * memory reclaim re-entering the filesystem here and
469 * potentially deadlocking.
470 */
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700471 nofs_flag = memalloc_nofs_save();
Dave Chinnera19fb382011-03-26 09:13:42 +1100472 do {
473 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
474 -1, PAGE_KERNEL);
475 if (bp->b_addr)
476 break;
477 vm_unmap_aliases();
478 } while (retried++ <= 1);
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700479 memalloc_nofs_restore(nofs_flag);
Dave Chinnera19fb382011-03-26 09:13:42 +1100480
481 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100483 bp->b_addr += bp->b_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
486 return 0;
487}
488
489/*
490 * Finding and Reading Buffers
491 */
Lucas Stach6031e732016-12-07 17:36:36 +1100492static int
493_xfs_buf_obj_cmp(
494 struct rhashtable_compare_arg *arg,
495 const void *obj)
496{
497 const struct xfs_buf_map *map = arg->key;
498 const struct xfs_buf *bp = obj;
499
500 /*
501 * The key hashing in the lookup path depends on the key being the
502 * first element of the compare_arg, make sure to assert this.
503 */
504 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
505
506 if (bp->b_bn != map->bm_bn)
507 return 1;
508
509 if (unlikely(bp->b_length != map->bm_len)) {
510 /*
511 * found a block number match. If the range doesn't
512 * match, the only way this is allowed is if the buffer
513 * in the cache is stale and the transaction that made
514 * it stale has not yet committed. i.e. we are
515 * reallocating a busy extent. Skip this buffer and
516 * continue searching for an exact match.
517 */
518 ASSERT(bp->b_flags & XBF_STALE);
519 return 1;
520 }
521 return 0;
522}
523
524static const struct rhashtable_params xfs_buf_hash_params = {
525 .min_size = 32, /* empty AGs have minimal footprint */
526 .nelem_hint = 16,
527 .key_len = sizeof(xfs_daddr_t),
528 .key_offset = offsetof(struct xfs_buf, b_bn),
529 .head_offset = offsetof(struct xfs_buf, b_rhash_head),
530 .automatic_shrinking = true,
531 .obj_cmpfn = _xfs_buf_obj_cmp,
532};
533
534int
535xfs_buf_hash_init(
536 struct xfs_perag *pag)
537{
538 spin_lock_init(&pag->pag_buf_lock);
539 return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
540}
541
542void
543xfs_buf_hash_destroy(
544 struct xfs_perag *pag)
545{
546 rhashtable_destroy(&pag->pag_buf_hash);
547}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549/*
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700550 * Look up a buffer in the buffer cache and return it referenced and locked
551 * in @found_bp.
552 *
553 * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the
554 * cache.
555 *
556 * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return
557 * -EAGAIN if we fail to lock it.
558 *
559 * Return values are:
560 * -EFSCORRUPTED if have been supplied with an invalid address
561 * -EAGAIN on trylock failure
562 * -ENOENT if we fail to find a match and @new_bp was NULL
563 * 0, with @found_bp:
564 * - @new_bp if we inserted it into the cache
565 * - the buffer we found and locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700567static int
568xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000569 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000570 struct xfs_buf_map *map,
571 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100572 xfs_buf_flags_t flags,
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700573 struct xfs_buf *new_bp,
574 struct xfs_buf **found_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Dave Chinner74f75a02010-09-24 19:59:04 +1000576 struct xfs_perag *pag;
Dave Chinner74f75a02010-09-24 19:59:04 +1000577 xfs_buf_t *bp;
Lucas Stach6031e732016-12-07 17:36:36 +1100578 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
Dave Chinner10616b82013-01-21 23:53:52 +1100579 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000580 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700582 *found_bp = NULL;
583
Dave Chinner3e85c862012-06-22 18:50:09 +1000584 for (i = 0; i < nmaps; i++)
Lucas Stach6031e732016-12-07 17:36:36 +1100585 cmap.bm_len += map[i].bm_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 /* Check for IOs smaller than the sector size / not sector aligned */
Lucas Stach6031e732016-12-07 17:36:36 +1100588 ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
589 ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Dave Chinner10616b82013-01-21 23:53:52 +1100591 /*
592 * Corrupted block numbers can get through to here, unfortunately, so we
593 * have to check that the buffer falls within the filesystem bounds.
594 */
595 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
Lucas Stach6031e732016-12-07 17:36:36 +1100596 if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
Dave Chinner10616b82013-01-21 23:53:52 +1100597 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -0800598 "%s: daddr 0x%llx out of range, EOFS 0x%llx",
Lucas Stach6031e732016-12-07 17:36:36 +1100599 __func__, cmap.bm_bn, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000600 WARN_ON(1);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700601 return -EFSCORRUPTED;
Dave Chinner10616b82013-01-21 23:53:52 +1100602 }
603
Dave Chinner74f75a02010-09-24 19:59:04 +1000604 pag = xfs_perag_get(btp->bt_mount,
Lucas Stach6031e732016-12-07 17:36:36 +1100605 xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Dave Chinner74f75a02010-09-24 19:59:04 +1000607 spin_lock(&pag->pag_buf_lock);
Lucas Stach6031e732016-12-07 17:36:36 +1100608 bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
609 xfs_buf_hash_params);
610 if (bp) {
611 atomic_inc(&bp->b_hold);
612 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
615 /* No match found */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700616 if (!new_bp) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100617 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000618 spin_unlock(&pag->pag_buf_lock);
619 xfs_perag_put(pag);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700620 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700622
623 /* the buffer keeps the perag reference until it is freed */
624 new_bp->b_pag = pag;
625 rhashtable_insert_fast(&pag->pag_buf_hash, &new_bp->b_rhash_head,
626 xfs_buf_hash_params);
627 spin_unlock(&pag->pag_buf_lock);
628 *found_bp = new_bp;
629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000632 spin_unlock(&pag->pag_buf_lock);
633 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200635 if (!xfs_buf_trylock(bp)) {
636 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100637 xfs_buf_rele(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100638 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700639 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200641 xfs_buf_lock(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100642 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
Dave Chinner0e6e8472011-03-26 09:16:45 +1100645 /*
646 * if the buffer is stale, clear all the external state associated with
647 * it. We need to keep flags such as how we allocated the buffer memory
648 * intact here.
649 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100650 if (bp->b_flags & XBF_STALE) {
651 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinnercfb02852012-11-12 22:54:19 +1100652 ASSERT(bp->b_iodone == NULL);
Dave Chinner611c9942012-04-23 15:59:07 +1000653 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100654 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000655 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000656
657 trace_xfs_buf_find(bp, flags, _RET_IP_);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100658 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700659 *found_bp = bp;
660 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Dave Chinner8925a3d2018-04-18 08:25:20 -0700663struct xfs_buf *
664xfs_buf_incore(
665 struct xfs_buftarg *target,
666 xfs_daddr_t blkno,
667 size_t numblks,
668 xfs_buf_flags_t flags)
669{
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700670 struct xfs_buf *bp;
671 int error;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700672 DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700673
674 error = xfs_buf_find(target, &map, 1, flags, NULL, &bp);
675 if (error)
676 return NULL;
677 return bp;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680/*
Dave Chinner38158322011-09-30 04:45:02 +0000681 * Assembles a buffer covering the specified range. The code is optimised for
682 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
683 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
Dave Chinner38158322011-09-30 04:45:02 +0000685struct xfs_buf *
Dave Chinner6dde2702012-06-22 18:50:10 +1000686xfs_buf_get_map(
687 struct xfs_buftarg *target,
688 struct xfs_buf_map *map,
689 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100690 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Dave Chinner38158322011-09-30 04:45:02 +0000692 struct xfs_buf *bp;
693 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100694 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700696 error = xfs_buf_find(target, map, nmaps, flags, NULL, &bp);
697
698 switch (error) {
699 case 0:
700 /* cache hit */
Dave Chinner38158322011-09-30 04:45:02 +0000701 goto found;
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700702 case -EAGAIN:
703 /* cache hit, trylock failure, caller handles failure */
704 ASSERT(flags & XBF_TRYLOCK);
705 return NULL;
706 case -ENOENT:
707 /* cache miss, go for insert */
708 break;
709 case -EFSCORRUPTED:
710 default:
711 /*
712 * None of the higher layers understand failure types
713 * yet, so return NULL to signal a fatal lookup error.
714 */
715 return NULL;
716 }
Dave Chinner38158322011-09-30 04:45:02 +0000717
Dave Chinner6dde2702012-06-22 18:50:10 +1000718 new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100719 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return NULL;
721
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000722 error = xfs_buf_allocate_memory(new_bp, flags);
723 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000724 xfs_buf_free(new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000725 return NULL;
726 }
727
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700728 error = xfs_buf_find(target, map, nmaps, flags, new_bp, &bp);
729 if (error) {
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000730 xfs_buf_free(new_bp);
731 return NULL;
732 }
733
734 if (bp != new_bp)
735 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Dave Chinner38158322011-09-30 04:45:02 +0000737found:
Dave Chinner611c9942012-04-23 15:59:07 +1000738 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100739 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100741 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500742 "%s: failed to map pagesn", __func__);
Dave Chinnera8acad72012-04-23 15:58:54 +1000743 xfs_buf_relse(bp);
744 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 }
747
Dave Chinnerb79f4a12016-01-12 07:03:44 +1100748 /*
749 * Clear b_error if this is a lookup from a caller that doesn't expect
750 * valid data to be found in the buffer.
751 */
752 if (!(flags & XBF_READ))
753 xfs_buf_ioerror(bp, 0);
754
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100755 XFS_STATS_INC(target->bt_mount, xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000756 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100757 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100760STATIC int
761_xfs_buf_read(
762 xfs_buf_t *bp,
763 xfs_buf_flags_t flags)
764{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000765 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600766 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100767
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000768 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200769 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100770
Brian Foster6af88cd2018-07-11 22:26:35 -0700771 return xfs_buf_submit(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100772}
773
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100774/*
Brian Foster75d02302019-02-06 09:25:29 -0800775 * Reverify a buffer found in cache without an attached ->b_ops.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800776 *
Brian Foster75d02302019-02-06 09:25:29 -0800777 * If the caller passed an ops structure and the buffer doesn't have ops
778 * assigned, set the ops and use it to verify the contents. If verification
779 * fails, clear XBF_DONE. We assume the buffer has no recorded errors and is
780 * already in XBF_DONE state on entry.
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800781 *
Brian Foster75d02302019-02-06 09:25:29 -0800782 * Under normal operations, every in-core buffer is verified on read I/O
783 * completion. There are two scenarios that can lead to in-core buffers without
784 * an assigned ->b_ops. The first is during log recovery of buffers on a V4
785 * filesystem, though these buffers are purged at the end of recovery. The
786 * other is online repair, which intentionally reads with a NULL buffer ops to
787 * run several verifiers across an in-core buffer in order to establish buffer
788 * type. If repair can't establish that, the buffer will be left in memory
789 * with NULL buffer ops.
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100790 */
791int
Brian Foster75d02302019-02-06 09:25:29 -0800792xfs_buf_reverify(
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100793 struct xfs_buf *bp,
794 const struct xfs_buf_ops *ops)
795{
796 ASSERT(bp->b_flags & XBF_DONE);
797 ASSERT(bp->b_error == 0);
798
799 if (!ops || bp->b_ops)
800 return 0;
801
802 bp->b_ops = ops;
803 bp->b_ops->verify_read(bp);
804 if (bp->b_error)
805 bp->b_flags &= ~XBF_DONE;
806 return bp->b_error;
807}
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809xfs_buf_t *
Dave Chinner6dde2702012-06-22 18:50:10 +1000810xfs_buf_read_map(
811 struct xfs_buftarg *target,
812 struct xfs_buf_map *map,
813 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100814 xfs_buf_flags_t flags,
Dave Chinner1813dd62012-11-14 17:54:40 +1100815 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Dave Chinner6dde2702012-06-22 18:50:10 +1000817 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Nathan Scottce8e9222006-01-11 15:39:08 +1100819 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Dave Chinner6dde2702012-06-22 18:50:10 +1000821 bp = xfs_buf_get_map(target, map, nmaps, flags);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100822 if (!bp)
823 return NULL;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000824
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100825 trace_xfs_buf_read(bp, flags, _RET_IP_);
826
827 if (!(bp->b_flags & XBF_DONE)) {
828 XFS_STATS_INC(target->bt_mount, xb_get_read);
829 bp->b_ops = ops;
830 _xfs_buf_read(bp, flags);
831 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
Brian Foster75d02302019-02-06 09:25:29 -0800834 xfs_buf_reverify(bp, ops);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100835
836 if (flags & XBF_ASYNC) {
837 /*
838 * Read ahead call which is already satisfied,
839 * drop the buffer
840 */
841 xfs_buf_relse(bp);
842 return NULL;
843 }
844
845 /* We do not want read in the flags */
846 bp->b_flags &= ~XBF_READ;
847 ASSERT(bp->b_ops != NULL || ops == NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +1100848 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100852 * If we are not low on memory then do the readahead in a deadlock
853 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
855void
Dave Chinner6dde2702012-06-22 18:50:10 +1000856xfs_buf_readahead_map(
857 struct xfs_buftarg *target,
858 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100859 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100860 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Jan Karaefa7c9f2017-02-02 15:56:53 +0100862 if (bdi_read_congested(target->bt_bdev->bd_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return;
864
Dave Chinner6dde2702012-06-22 18:50:10 +1000865 xfs_buf_read_map(target, map, nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100866 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Dave Chinner5adc94c2010-09-24 21:58:31 +1000869/*
870 * Read an uncached buffer from disk. Allocates and returns a locked
871 * buffer containing the disk contents or nothing.
872 */
Dave Chinnerba3726742014-10-02 09:05:32 +1000873int
Dave Chinner5adc94c2010-09-24 21:58:31 +1000874xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000875 struct xfs_buftarg *target,
876 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000877 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100878 int flags,
Dave Chinnerba3726742014-10-02 09:05:32 +1000879 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100880 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000881{
Dave Chinnereab4e632012-11-12 22:54:02 +1100882 struct xfs_buf *bp;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000883
Dave Chinnerba3726742014-10-02 09:05:32 +1000884 *bpp = NULL;
885
Dave Chinnere70b73f2012-04-23 15:58:49 +1000886 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000887 if (!bp)
Dave Chinnerba3726742014-10-02 09:05:32 +1000888 return -ENOMEM;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000889
890 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000891 ASSERT(bp->b_map_count == 1);
Dave Chinnerba3726742014-10-02 09:05:32 +1000892 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
Dave Chinner3e85c862012-06-22 18:50:09 +1000893 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000894 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100895 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000896
Brian Foster6af88cd2018-07-11 22:26:35 -0700897 xfs_buf_submit(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000898 if (bp->b_error) {
899 int error = bp->b_error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800900 xfs_buf_relse(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000901 return error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800902 }
Dave Chinnerba3726742014-10-02 09:05:32 +1000903
904 *bpp = bp;
905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000909xfs_buf_get_uncached(
910 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000911 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000912 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000914 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000915 int error, i;
Dave Chinner3e85c862012-06-22 18:50:09 +1000916 struct xfs_buf *bp;
917 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Brian Fosterc891c302016-07-20 11:13:43 +1000919 /* flags might contain irrelevant bits, pass only what we care about */
920 bp = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (unlikely(bp == NULL))
922 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Dave Chinnere70b73f2012-04-23 15:58:49 +1000924 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000925 error = _xfs_buf_get_pages(bp, page_count);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000926 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 goto fail_free_buf;
928
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000929 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000930 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000931 if (!bp->b_pages[i])
932 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000934 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Dave Chinner611c9942012-04-23 15:59:07 +1000936 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000937 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100938 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500939 "%s: failed to map pages", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Dave Chinner686865f2010-09-24 20:07:47 +1000943 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000947 while (--i >= 0)
948 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000949 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 fail_free_buf:
Dave Chinner3e85c862012-06-22 18:50:09 +1000951 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000952 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 fail:
954 return NULL;
955}
956
957/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * Increment reference count on buffer, to hold the buffer concurrently
959 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 * Must hold the buffer already to call this function.
961 */
962void
Nathan Scottce8e9222006-01-11 15:39:08 +1100963xfs_buf_hold(
964 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000966 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100967 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
970/*
Brian Foster9c7504a2016-07-20 11:15:28 +1000971 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
972 * placed on LRU or freed (depending on b_lru_ref).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 */
974void
Nathan Scottce8e9222006-01-11 15:39:08 +1100975xfs_buf_rele(
976 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Dave Chinner74f75a02010-09-24 19:59:04 +1000978 struct xfs_perag *pag = bp->b_pag;
Brian Foster9c7504a2016-07-20 11:15:28 +1000979 bool release;
980 bool freebuf = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000982 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Dave Chinner74f75a02010-09-24 19:59:04 +1000984 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100985 ASSERT(list_empty(&bp->b_lru));
Brian Foster9c7504a2016-07-20 11:15:28 +1000986 if (atomic_dec_and_test(&bp->b_hold)) {
987 xfs_buf_ioacct_dec(bp);
Nathan Scottfad3aa12006-02-01 12:14:52 +1100988 xfs_buf_free(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +1000989 }
Nathan Scottfad3aa12006-02-01 12:14:52 +1100990 return;
991 }
992
Lachlan McIlroy37906892008-08-13 15:42:10 +1000993 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinnera4082352013-08-28 10:18:06 +1000994
Dave Chinner37fd1672018-10-18 17:21:29 +1100995 /*
996 * We grab the b_lock here first to serialise racing xfs_buf_rele()
997 * calls. The pag_buf_lock being taken on the last reference only
998 * serialises against racing lookups in xfs_buf_find(). IOWs, the second
999 * to last reference we drop here is not serialised against the last
1000 * reference until we take bp->b_lock. Hence if we don't grab b_lock
1001 * first, the last "release" reference can win the race to the lock and
1002 * free the buffer before the second-to-last reference is processed,
1003 * leading to a use-after-free scenario.
1004 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001005 spin_lock(&bp->b_lock);
Dave Chinner37fd1672018-10-18 17:21:29 +11001006 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +10001007 if (!release) {
1008 /*
1009 * Drop the in-flight state if the buffer is already on the LRU
1010 * and it holds the only reference. This is racy because we
1011 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1012 * ensures the decrement occurs only once per-buf.
1013 */
1014 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
Brian Foster63db7c82017-05-31 08:22:52 -07001015 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001016 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
Brian Foster9c7504a2016-07-20 11:15:28 +10001018
1019 /* the last reference has been dropped ... */
Brian Foster63db7c82017-05-31 08:22:52 -07001020 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001021 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1022 /*
1023 * If the buffer is added to the LRU take a new reference to the
1024 * buffer for the LRU and clear the (now stale) dispose list
1025 * state flag
1026 */
1027 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1028 bp->b_state &= ~XFS_BSTATE_DISPOSE;
1029 atomic_inc(&bp->b_hold);
1030 }
1031 spin_unlock(&pag->pag_buf_lock);
1032 } else {
1033 /*
1034 * most of the time buffers will already be removed from the
1035 * LRU, so optimise that case by checking for the
1036 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1037 * was on was the disposal list
1038 */
1039 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1040 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1041 } else {
1042 ASSERT(list_empty(&bp->b_lru));
1043 }
1044
1045 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Lucas Stach6031e732016-12-07 17:36:36 +11001046 rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
1047 xfs_buf_hash_params);
Brian Foster9c7504a2016-07-20 11:15:28 +10001048 spin_unlock(&pag->pag_buf_lock);
1049 xfs_perag_put(pag);
1050 freebuf = true;
1051 }
1052
1053out_unlock:
1054 spin_unlock(&bp->b_lock);
1055
1056 if (freebuf)
1057 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
1060
1061/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001062 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +11001063 *
1064 * If we come across a stale, pinned, locked buffer, we know that we are
1065 * being asked to lock a buffer that has been reallocated. Because it is
1066 * pinned, we know that the log has not been pushed to disk and hence it
1067 * will still be locked. Rather than continuing to have trylock attempts
1068 * fail until someone else pushes the log, push it ourselves before
1069 * returning. This means that the xfsaild will not get stuck trying
1070 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
1072int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001073xfs_buf_trylock(
1074 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 int locked;
1077
Nathan Scottce8e9222006-01-11 15:39:08 +11001078 locked = down_trylock(&bp->b_sema) == 0;
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001079 if (locked)
Darrick J. Wong479c6412016-06-21 11:53:28 +10001080 trace_xfs_buf_trylock(bp, _RET_IP_);
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001081 else
Darrick J. Wong479c6412016-06-21 11:53:28 +10001082 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001083 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001087 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +10001088 *
1089 * If we come across a stale, pinned, locked buffer, we know that we
1090 * are being asked to lock a buffer that has been reallocated. Because
1091 * it is pinned, we know that the log has not been pushed to disk and
1092 * hence it will still be locked. Rather than sleeping until someone
1093 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001095void
1096xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001097 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001099 trace_xfs_buf_lock(bp, _RET_IP_);
1100
Dave Chinnered3b4d62010-05-21 12:07:08 +10001101 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001102 xfs_log_force(bp->b_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +11001103 down(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001104
1105 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108void
Nathan Scottce8e9222006-01-11 15:39:08 +11001109xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001110 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Brian Foster20e8a062017-04-21 12:40:44 -07001112 ASSERT(xfs_buf_islocked(bp));
1113
Nathan Scottce8e9222006-01-11 15:39:08 +11001114 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001115 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
Nathan Scottce8e9222006-01-11 15:39:08 +11001118STATIC void
1119xfs_buf_wait_unpin(
1120 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 DECLARE_WAITQUEUE (wait, current);
1123
Nathan Scottce8e9222006-01-11 15:39:08 +11001124 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return;
1126
Nathan Scottce8e9222006-01-11 15:39:08 +11001127 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 for (;;) {
1129 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001130 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001132 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001134 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 set_current_state(TASK_RUNNING);
1136}
1137
1138/*
1139 * Buffer Utility Routines
1140 */
1141
Dave Chinnere8aaba92014-10-02 09:04:22 +10001142void
1143xfs_buf_ioend(
1144 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001146 bool read = bp->b_flags & XBF_READ;
1147
1148 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001149
1150 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Dave Chinnerd5929de2013-02-27 13:25:54 +11001151
Dave Chinner61be9c52014-10-02 09:04:31 +10001152 /*
1153 * Pull in IO completion errors now. We are guaranteed to be running
1154 * single threaded, so we don't need the lock to read b_io_error.
1155 */
1156 if (!bp->b_error && bp->b_io_error)
1157 xfs_buf_ioerror(bp, bp->b_io_error);
1158
Dave Chinnere8aaba92014-10-02 09:04:22 +10001159 /* Only validate buffers that were read without errors */
1160 if (read && !bp->b_error && bp->b_ops) {
1161 ASSERT(!bp->b_iodone);
Dave Chinner1813dd62012-11-14 17:54:40 +11001162 bp->b_ops->verify_read(bp);
Dave Chinnere8aaba92014-10-02 09:04:22 +10001163 }
1164
1165 if (!bp->b_error)
1166 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Christoph Hellwig80f6c292010-08-18 05:29:11 -04001168 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001169 (*(bp->b_iodone))(bp);
1170 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 xfs_buf_relse(bp);
Dave Chinner595bff72014-10-02 09:05:14 +10001172 else
Dave Chinner1813dd62012-11-14 17:54:40 +11001173 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Dave Chinnere8aaba92014-10-02 09:04:22 +10001176static void
1177xfs_buf_ioend_work(
1178 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001180 struct xfs_buf *bp =
Brian Fosterb29c70f2014-12-04 09:43:17 +11001181 container_of(work, xfs_buf_t, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001182
Dave Chinnere8aaba92014-10-02 09:04:22 +10001183 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001186static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001187xfs_buf_ioend_async(
1188 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001190 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001191 queue_work(bp->b_mount->m_buf_workqueue, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194void
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001195__xfs_buf_ioerror(
Nathan Scottce8e9222006-01-11 15:39:08 +11001196 xfs_buf_t *bp,
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001197 int error,
1198 xfs_failaddr_t failaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Dave Chinner24513372014-06-25 14:58:08 +10001200 ASSERT(error <= 0 && error >= -1000);
1201 bp->b_error = error;
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001202 trace_xfs_buf_ioerror(bp, error, failaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Christoph Hellwig901796a2011-10-10 16:52:49 +00001205void
1206xfs_buf_ioerror_alert(
1207 struct xfs_buf *bp,
1208 const char *func)
1209{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001210 xfs_alert(bp->b_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001211"metadata I/O error in \"%s\" at daddr 0x%llx len %d error %d",
1212 func, (uint64_t)XFS_BUF_ADDR(bp), bp->b_length,
1213 -bp->b_error);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001214}
1215
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001216int
1217xfs_bwrite(
1218 struct xfs_buf *bp)
1219{
1220 int error;
1221
1222 ASSERT(xfs_buf_islocked(bp));
1223
1224 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001225 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
1226 XBF_WRITE_FAIL | XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001227
Brian Foster6af88cd2018-07-11 22:26:35 -07001228 error = xfs_buf_submit(bp);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001229 if (error)
1230 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001231 return error;
1232}
1233
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001234static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001235xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001236 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001238 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Dave Chinner37eb17e2012-11-12 22:09:46 +11001240 /*
1241 * don't overwrite existing errors - otherwise we can lose errors on
1242 * buffers that require multiple bios to complete.
1243 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001244 if (bio->bi_status) {
1245 int error = blk_status_to_errno(bio->bi_status);
1246
1247 cmpxchg(&bp->b_io_error, 0, error);
1248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Dave Chinner37eb17e2012-11-12 22:09:46 +11001250 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001251 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1252
Dave Chinnere8aaba92014-10-02 09:04:22 +10001253 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1254 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
Dave Chinner3e85c862012-06-22 18:50:09 +10001258static void
1259xfs_buf_ioapply_map(
1260 struct xfs_buf *bp,
1261 int map,
1262 int *buf_offset,
1263 int *count,
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001264 int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Dave Chinner3e85c862012-06-22 18:50:09 +10001266 int page_index;
1267 int total_nr_pages = bp->b_page_count;
1268 int nr_pages;
1269 struct bio *bio;
1270 sector_t sector = bp->b_maps[map].bm_bn;
1271 int size;
1272 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Dave Chinner3e85c862012-06-22 18:50:09 +10001274 /* skip the pages in the buffer before the start offset */
1275 page_index = 0;
1276 offset = *buf_offset;
1277 while (offset >= PAGE_SIZE) {
1278 page_index++;
1279 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001280 }
1281
Dave Chinner3e85c862012-06-22 18:50:09 +10001282 /*
1283 * Limit the IO size to the length of the current vector, and update the
1284 * remaining IO count for the next time around.
1285 */
1286 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1287 *count -= size;
1288 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001291 atomic_inc(&bp->b_io_remaining);
Ming Leic908e382016-05-30 21:34:33 +08001292 nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 bio = bio_alloc(GFP_NOIO, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001295 bio_set_dev(bio, bp->b_target->bt_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001296 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001297 bio->bi_end_io = xfs_buf_bio_end_io;
1298 bio->bi_private = bp;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001299 bio->bi_opf = op;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001300
Dave Chinner3e85c862012-06-22 18:50:09 +10001301 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001302 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 if (nbytes > size)
1305 nbytes = size;
1306
Dave Chinner3e85c862012-06-22 18:50:09 +10001307 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1308 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001309 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 break;
1311
1312 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001313 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 size -= nbytes;
1315 total_nr_pages--;
1316 }
1317
Kent Overstreet4f024f32013-10-11 15:44:27 -07001318 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001319 if (xfs_buf_is_vmapped(bp)) {
1320 flush_kernel_vmap_range(bp->b_addr,
1321 xfs_buf_vmap_len(bp));
1322 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001323 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (size)
1325 goto next_chunk;
1326 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001327 /*
1328 * This is guaranteed not to be the last io reference count
Dave Chinner595bff72014-10-02 09:05:14 +10001329 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001330 */
1331 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001332 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001333 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001335
1336}
1337
1338STATIC void
1339_xfs_buf_ioapply(
1340 struct xfs_buf *bp)
1341{
1342 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001343 int op;
Dave Chinner3e85c862012-06-22 18:50:09 +10001344 int offset;
1345 int size;
1346 int i;
1347
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001348 /*
1349 * Make sure we capture only current IO errors rather than stale errors
1350 * left over from previous use of the buffer (e.g. failed readahead).
1351 */
1352 bp->b_error = 0;
1353
Dave Chinner3e85c862012-06-22 18:50:09 +10001354 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001355 op = REQ_OP_WRITE;
Dave Chinner1813dd62012-11-14 17:54:40 +11001356
1357 /*
1358 * Run the write verifier callback function if it exists. If
1359 * this function fails it will mark the buffer with an error and
1360 * the IO should not be dispatched.
1361 */
1362 if (bp->b_ops) {
1363 bp->b_ops->verify_write(bp);
1364 if (bp->b_error) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001365 xfs_force_shutdown(bp->b_mount,
Dave Chinner1813dd62012-11-14 17:54:40 +11001366 SHUTDOWN_CORRUPT_INCORE);
1367 return;
1368 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001369 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001370 struct xfs_mount *mp = bp->b_mount;
Dave Chinner400b9d82014-08-04 12:42:40 +10001371
1372 /*
1373 * non-crc filesystems don't attach verifiers during
1374 * log recovery, so don't warn for such filesystems.
1375 */
1376 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1377 xfs_warn(mp,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001378 "%s: no buf ops on daddr 0x%llx len %d",
Dave Chinner400b9d82014-08-04 12:42:40 +10001379 __func__, bp->b_bn, bp->b_length);
Darrick J. Wong9c712a12018-01-08 10:51:26 -08001380 xfs_hex_dump(bp->b_addr,
1381 XFS_CORRUPTION_DUMP_LEN);
Dave Chinner400b9d82014-08-04 12:42:40 +10001382 dump_stack();
1383 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001384 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001385 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001386 op = REQ_OP_READ;
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001387 if (bp->b_flags & XBF_READ_AHEAD)
1388 op |= REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001389 }
1390
1391 /* we only use the buffer cache for meta-data */
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001392 op |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001393
1394 /*
1395 * Walk all the vectors issuing IO on them. Set up the initial offset
1396 * into the buffer and the desired IO size before we start -
1397 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1398 * subsequent call.
1399 */
1400 offset = bp->b_offset;
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001401 size = BBTOB(bp->b_length);
Dave Chinner3e85c862012-06-22 18:50:09 +10001402 blk_start_plug(&plug);
1403 for (i = 0; i < bp->b_map_count; i++) {
Christoph Hellwig2123ef82019-10-28 08:41:42 -07001404 xfs_buf_ioapply_map(bp, i, &offset, &size, op);
Dave Chinner3e85c862012-06-22 18:50:09 +10001405 if (bp->b_error)
1406 break;
1407 if (size <= 0)
1408 break; /* all done */
1409 }
1410 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
Dave Chinner595bff72014-10-02 09:05:14 +10001413/*
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001414 * Wait for I/O completion of a sync buffer and return the I/O error code.
Dave Chinner595bff72014-10-02 09:05:14 +10001415 */
Brian Fostereaebb512018-07-11 22:26:34 -07001416static int
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001417xfs_buf_iowait(
Dave Chinner595bff72014-10-02 09:05:14 +10001418 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001420 ASSERT(!(bp->b_flags & XBF_ASYNC));
1421
1422 trace_xfs_buf_iowait(bp, _RET_IP_);
1423 wait_for_completion(&bp->b_iowait);
1424 trace_xfs_buf_iowait_done(bp, _RET_IP_);
1425
1426 return bp->b_error;
1427}
1428
1429/*
1430 * Buffer I/O submission path, read or write. Asynchronous submission transfers
1431 * the buffer lock ownership and the current reference to the IO. It is not
1432 * safe to reference the buffer after a call to this function unless the caller
1433 * holds an additional reference itself.
1434 */
1435int
1436__xfs_buf_submit(
1437 struct xfs_buf *bp,
1438 bool wait)
1439{
1440 int error = 0;
1441
Dave Chinner595bff72014-10-02 09:05:14 +10001442 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001444 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff72014-10-02 09:05:14 +10001445
1446 /* on shutdown we stale and complete the buffer immediately */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07001447 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
Dave Chinner595bff72014-10-02 09:05:14 +10001448 xfs_buf_ioerror(bp, -EIO);
1449 bp->b_flags &= ~XBF_DONE;
1450 xfs_buf_stale(bp);
Brian Foster465fa172019-02-03 14:03:06 -08001451 xfs_buf_ioend(bp);
Brian Fostereaebb512018-07-11 22:26:34 -07001452 return -EIO;
Dave Chinner595bff72014-10-02 09:05:14 +10001453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001455 /*
1456 * Grab a reference so the buffer does not go away underneath us. For
1457 * async buffers, I/O completion drops the callers reference, which
1458 * could occur before submission returns.
1459 */
1460 xfs_buf_hold(bp);
1461
Christoph Hellwig375ec692011-08-23 08:28:03 +00001462 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001463 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Dave Chinner61be9c52014-10-02 09:04:31 +10001465 /* clear the internal error state to avoid spurious errors */
1466 bp->b_io_error = 0;
1467
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001468 /*
Brian Fostereaebb512018-07-11 22:26:34 -07001469 * Set the count to 1 initially, this will stop an I/O completion
1470 * callout which happens before we have started all the I/O from calling
1471 * xfs_buf_ioend too early.
1472 */
1473 atomic_set(&bp->b_io_remaining, 1);
1474 if (bp->b_flags & XBF_ASYNC)
1475 xfs_buf_ioacct_inc(bp);
1476 _xfs_buf_ioapply(bp);
1477
1478 /*
1479 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1480 * reference we took above. If we drop it to zero, run completion so
1481 * that we don't return to the caller with completion still pending.
1482 */
1483 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1484 if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
1485 xfs_buf_ioend(bp);
1486 else
1487 xfs_buf_ioend_async(bp);
1488 }
1489
Brian Foster6af88cd2018-07-11 22:26:35 -07001490 if (wait)
1491 error = xfs_buf_iowait(bp);
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001492
Dave Chinner595bff72014-10-02 09:05:14 +10001493 /*
Brian Foster6af88cd2018-07-11 22:26:35 -07001494 * Release the hold that keeps the buffer referenced for the entire
1495 * I/O. Note that if the buffer is async, it is not safe to reference
1496 * after this release.
Dave Chinner595bff72014-10-02 09:05:14 +10001497 */
1498 xfs_buf_rele(bp);
1499 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001502void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001503xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001504 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 size_t offset)
1506{
1507 struct page *page;
1508
Dave Chinner611c9942012-04-23 15:59:07 +10001509 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001510 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Nathan Scottce8e9222006-01-11 15:39:08 +11001512 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001513 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001514 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517void
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001518xfs_buf_zero(
1519 struct xfs_buf *bp,
1520 size_t boff,
1521 size_t bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Dave Chinner795cac72012-04-23 15:58:53 +10001523 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 bend = boff + bsize;
1526 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001527 struct page *page;
1528 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Dave Chinner795cac72012-04-23 15:58:53 +10001530 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1531 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1532 page = bp->b_pages[page_index];
1533 csize = min_t(size_t, PAGE_SIZE - page_offset,
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07001534 BBTOB(bp->b_length) - boff);
Dave Chinner795cac72012-04-23 15:58:53 +10001535
1536 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Christoph Hellwigf9a196e2019-06-12 08:59:59 -07001538 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 boff += csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542}
1543
1544/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001545 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 */
1547
1548/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001549 * Wait for any bufs with callbacks that have been submitted but have not yet
1550 * returned. These buffers will have an elevated hold count, so wait on those
1551 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001553static enum lru_status
1554xfs_buftarg_wait_rele(
1555 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001556 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001557 spinlock_t *lru_lock,
1558 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Dave Chinnere80dfa12013-08-28 10:18:05 +10001560{
1561 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001562 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001563
1564 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001565 /* need to wait, so skip it this pass */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001566 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001567 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Dave Chinnera4082352013-08-28 10:18:06 +10001569 if (!spin_trylock(&bp->b_lock))
1570 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001571
Dave Chinnera4082352013-08-28 10:18:06 +10001572 /*
1573 * clear the LRU reference count so the buffer doesn't get
1574 * ignored in xfs_buf_rele().
1575 */
1576 atomic_set(&bp->b_lru_ref, 0);
1577 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001578 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001579 spin_unlock(&bp->b_lock);
1580 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
Dave Chinnere80dfa12013-08-28 10:18:05 +10001583void
1584xfs_wait_buftarg(
1585 struct xfs_buftarg *btp)
1586{
Dave Chinnera4082352013-08-28 10:18:06 +10001587 LIST_HEAD(dispose);
1588 int loop = 0;
1589
Dave Chinner85bec542016-01-19 08:28:10 +11001590 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001591 * First wait on the buftarg I/O count for all in-flight buffers to be
1592 * released. This is critical as new buffers do not make the LRU until
1593 * they are released.
1594 *
1595 * Next, flush the buffer workqueue to ensure all completion processing
1596 * has finished. Just waiting on buffer locks is not sufficient for
1597 * async IO as the reference count held over IO is not released until
1598 * after the buffer lock is dropped. Hence we need to ensure here that
1599 * all reference counts have been dropped before we start walking the
1600 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001601 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001602 while (percpu_counter_sum(&btp->bt_io_count))
1603 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001604 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Dave Chinner85bec542016-01-19 08:28:10 +11001605
Dave Chinnera4082352013-08-28 10:18:06 +10001606 /* loop until there is nothing left on the lru list. */
1607 while (list_lru_count(&btp->bt_lru)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001608 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001609 &dispose, LONG_MAX);
1610
1611 while (!list_empty(&dispose)) {
1612 struct xfs_buf *bp;
1613 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1614 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001615 if (bp->b_flags & XBF_WRITE_FAIL) {
1616 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001617"Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001618 (long long)bp->b_bn);
Joe Perchesf41febd2015-07-29 11:52:04 +10001619 xfs_alert(btp->bt_mount,
1620"Please run xfs_repair to determine the extent of the problem.");
Dave Chinnerac8809f2013-12-12 16:34:38 +11001621 }
Dave Chinnera4082352013-08-28 10:18:06 +10001622 xfs_buf_rele(bp);
1623 }
1624 if (loop++ != 0)
1625 delay(100);
1626 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001627}
1628
1629static enum lru_status
1630xfs_buftarg_isolate(
1631 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001632 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001633 spinlock_t *lru_lock,
1634 void *arg)
1635{
1636 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1637 struct list_head *dispose = arg;
1638
1639 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001640 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1641 * If we fail to get the lock, just skip it.
1642 */
1643 if (!spin_trylock(&bp->b_lock))
1644 return LRU_SKIP;
1645 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001646 * Decrement the b_lru_ref count unless the value is already
1647 * zero. If the value is already zero, we need to reclaim the
1648 * buffer, otherwise it gets another trip through the LRU.
1649 */
Vratislav Bendel19957a12018-03-06 17:07:44 -08001650 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
Dave Chinnera4082352013-08-28 10:18:06 +10001651 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001652 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001653 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001654
Dave Chinnera4082352013-08-28 10:18:06 +10001655 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001656 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001657 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001658 return LRU_REMOVED;
1659}
1660
Andrew Mortonaddbda42013-08-28 10:18:06 +10001661static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001662xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001663 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001664 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001665{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001666 struct xfs_buftarg *btp = container_of(shrink,
1667 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001668 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001669 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001670
Vladimir Davydov503c3582015-02-12 14:58:47 -08001671 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1672 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001673
1674 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001675 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001676 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1677 list_del_init(&bp->b_lru);
1678 xfs_buf_rele(bp);
1679 }
1680
Dave Chinnere80dfa12013-08-28 10:18:05 +10001681 return freed;
1682}
1683
Andrew Mortonaddbda42013-08-28 10:18:06 +10001684static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001685xfs_buftarg_shrink_count(
1686 struct shrinker *shrink,
1687 struct shrink_control *sc)
1688{
1689 struct xfs_buftarg *btp = container_of(shrink,
1690 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001691 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001692}
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694void
1695xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001696 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001698 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001699 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1700 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001701 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001702
Dave Chinner2291dab2016-12-09 16:49:54 +11001703 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001704
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001705 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001708int
1709xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001711 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001713 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001714 btp->bt_meta_sectorsize = sectorsize;
1715 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Nathan Scottce8e9222006-01-11 15:39:08 +11001717 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001718 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001719 "Cannot set_blocksize to %u on device %pg",
1720 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001721 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001724 /* Set up device logical sector size mask */
1725 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1726 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return 0;
1729}
1730
1731/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001732 * When allocating the initial buffer target we have not yet
1733 * read in the superblock, so don't know what sized sectors
1734 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001735 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736STATIC int
1737xfs_setsize_buftarg_early(
1738 xfs_buftarg_t *btp,
1739 struct block_device *bdev)
1740{
Eric Sandeena96c4152014-04-14 19:00:29 +10001741 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744xfs_buftarg_t *
1745xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001746 struct xfs_mount *mp,
Dan Williams486aff52017-08-24 15:12:50 -07001747 struct block_device *bdev,
1748 struct dax_device *dax_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 xfs_buftarg_t *btp;
1751
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07001752 btp = kmem_zalloc(sizeof(*btp), KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Dave Chinnerebad8612010-09-22 10:47:20 +10001754 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001755 btp->bt_dev = bdev->bd_dev;
1756 btp->bt_bdev = bdev;
Dan Williams486aff52017-08-24 15:12:50 -07001757 btp->bt_daxdev = dax_dev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (xfs_setsize_buftarg_early(btp, bdev))
Michal Hockod210a982017-11-23 17:13:40 +01001760 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001761
1762 if (list_lru_init(&btp->bt_lru))
Michal Hockod210a982017-11-23 17:13:40 +01001763 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001764
Brian Foster9c7504a2016-07-20 11:15:28 +10001765 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
Michal Hockod210a982017-11-23 17:13:40 +01001766 goto error_lru;
Brian Foster9c7504a2016-07-20 11:15:28 +10001767
Dave Chinnere80dfa12013-08-28 10:18:05 +10001768 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1769 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001770 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001771 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Michal Hockod210a982017-11-23 17:13:40 +01001772 if (register_shrinker(&btp->bt_shrinker))
1773 goto error_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return btp;
1775
Michal Hockod210a982017-11-23 17:13:40 +01001776error_pcpu:
1777 percpu_counter_destroy(&btp->bt_io_count);
1778error_lru:
1779 list_lru_destroy(&btp->bt_lru);
1780error_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001781 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return NULL;
1783}
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785/*
Brian Foster20e8a062017-04-21 12:40:44 -07001786 * Cancel a delayed write list.
1787 *
1788 * Remove each buffer from the list, clear the delwri queue flag and drop the
1789 * associated buffer reference.
1790 */
1791void
1792xfs_buf_delwri_cancel(
1793 struct list_head *list)
1794{
1795 struct xfs_buf *bp;
1796
1797 while (!list_empty(list)) {
1798 bp = list_first_entry(list, struct xfs_buf, b_list);
1799
1800 xfs_buf_lock(bp);
1801 bp->b_flags &= ~_XBF_DELWRI_Q;
1802 list_del_init(&bp->b_list);
1803 xfs_buf_relse(bp);
1804 }
1805}
1806
1807/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001808 * Add a buffer to the delayed write list.
1809 *
1810 * This queues a buffer for writeout if it hasn't already been. Note that
1811 * neither this routine nor the buffer list submission functions perform
1812 * any internal synchronization. It is expected that the lists are thread-local
1813 * to the callers.
1814 *
1815 * Returns true if we queued up the buffer, or false if it already had
1816 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001818bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001819xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001820 struct xfs_buf *bp,
1821 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001823 ASSERT(xfs_buf_islocked(bp));
1824 ASSERT(!(bp->b_flags & XBF_READ));
1825
1826 /*
1827 * If the buffer is already marked delwri it already is queued up
1828 * by someone else for imediate writeout. Just ignore it in that
1829 * case.
1830 */
1831 if (bp->b_flags & _XBF_DELWRI_Q) {
1832 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1833 return false;
1834 }
David Chinnera6867a62006-01-11 15:37:58 +11001835
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001836 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1837
Dave Chinnerd808f612010-02-02 10:13:42 +11001838 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001839 * If a buffer gets written out synchronously or marked stale while it
1840 * is on a delwri list we lazily remove it. To do this, the other party
1841 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1842 * It remains referenced and on the list. In a rare corner case it
1843 * might get readded to a delwri list after the synchronous writeout, in
1844 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001845 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001846 bp->b_flags |= _XBF_DELWRI_Q;
1847 if (list_empty(&bp->b_list)) {
1848 atomic_inc(&bp->b_hold);
1849 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001850 }
David Chinner585e6d82007-02-10 18:32:29 +11001851
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001852 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001853}
1854
Dave Chinner089716a2010-01-26 15:13:25 +11001855/*
1856 * Compare function is more complex than it needs to be because
1857 * the return value is only 32 bits and we are doing comparisons
1858 * on 64 bit values
1859 */
1860static int
1861xfs_buf_cmp(
1862 void *priv,
1863 struct list_head *a,
1864 struct list_head *b)
1865{
1866 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1867 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1868 xfs_daddr_t diff;
1869
Mark Tinguelyf4b42422012-12-04 17:18:02 -06001870 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11001871 if (diff < 0)
1872 return -1;
1873 if (diff > 0)
1874 return 1;
1875 return 0;
1876}
1877
Dave Chinner26f1fe82016-06-01 17:38:15 +10001878/*
Brian Fostere339dd82018-07-11 22:26:34 -07001879 * Submit buffers for write. If wait_list is specified, the buffers are
1880 * submitted using sync I/O and placed on the wait list such that the caller can
1881 * iowait each buffer. Otherwise async I/O is used and the buffers are released
1882 * at I/O completion time. In either case, buffers remain locked until I/O
1883 * completes and the buffer is released from the queue.
Dave Chinner26f1fe82016-06-01 17:38:15 +10001884 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001885static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10001886xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001887 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10001888 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001890 struct xfs_buf *bp, *n;
1891 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001892 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Dave Chinner26f1fe82016-06-01 17:38:15 +10001894 list_sort(NULL, buffer_list, xfs_buf_cmp);
1895
1896 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001897 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10001898 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001899 if (xfs_buf_ispinned(bp)) {
1900 pinned++;
1901 continue;
1902 }
1903 if (!xfs_buf_trylock(bp))
1904 continue;
1905 } else {
1906 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001909 /*
1910 * Someone else might have written the buffer synchronously or
1911 * marked it stale in the meantime. In that case only the
1912 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1913 * reference and remove it from the list here.
1914 */
1915 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1916 list_del_init(&bp->b_list);
1917 xfs_buf_relse(bp);
1918 continue;
1919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001921 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001922
Dave Chinnercf53e992014-10-02 09:04:01 +10001923 /*
Brian Fostere339dd82018-07-11 22:26:34 -07001924 * If we have a wait list, each buffer (and associated delwri
1925 * queue reference) transfers to it and is submitted
1926 * synchronously. Otherwise, drop the buffer from the delwri
1927 * queue and submit async.
Dave Chinnercf53e992014-10-02 09:04:01 +10001928 */
Dave Chinnerbbfeb612016-07-20 11:53:35 +10001929 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL);
Brian Fostere339dd82018-07-11 22:26:34 -07001930 bp->b_flags |= XBF_WRITE;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001931 if (wait_list) {
Brian Fostere339dd82018-07-11 22:26:34 -07001932 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001933 list_move_tail(&bp->b_list, wait_list);
Brian Fostere339dd82018-07-11 22:26:34 -07001934 } else {
1935 bp->b_flags |= XBF_ASYNC;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001936 list_del_init(&bp->b_list);
Brian Fostere339dd82018-07-11 22:26:34 -07001937 }
Brian Foster6af88cd2018-07-11 22:26:35 -07001938 __xfs_buf_submit(bp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001940 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001942 return pinned;
1943}
Nathan Scottf07c2252006-09-28 10:52:15 +10001944
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001945/*
1946 * Write out a buffer list asynchronously.
1947 *
1948 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1949 * out and not wait for I/O completion on any of the buffers. This interface
1950 * is only safely useable for callers that can track I/O completion by higher
1951 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1952 * function.
Brian Fosterefc32892018-10-18 17:21:49 +11001953 *
1954 * Note: this function will skip buffers it would block on, and in doing so
1955 * leaves them on @buffer_list so they can be retried on a later pass. As such,
1956 * it is up to the caller to ensure that the buffer list is fully submitted or
1957 * cancelled appropriately when they are finished with the list. Failure to
1958 * cancel or resubmit the list until it is empty will result in leaked buffers
1959 * at unmount time.
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001960 */
1961int
1962xfs_buf_delwri_submit_nowait(
1963 struct list_head *buffer_list)
1964{
Dave Chinner26f1fe82016-06-01 17:38:15 +10001965 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001966}
1967
1968/*
1969 * Write out a buffer list synchronously.
1970 *
1971 * This will take the @buffer_list, write all buffers out and wait for I/O
1972 * completion on all of the buffers. @buffer_list is consumed by the function,
1973 * so callers must have some other way of tracking buffers if they require such
1974 * functionality.
1975 */
1976int
1977xfs_buf_delwri_submit(
1978 struct list_head *buffer_list)
1979{
Dave Chinner26f1fe82016-06-01 17:38:15 +10001980 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001981 int error = 0, error2;
1982 struct xfs_buf *bp;
1983
Dave Chinner26f1fe82016-06-01 17:38:15 +10001984 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001985
1986 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10001987 while (!list_empty(&wait_list)) {
1988 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001989
1990 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10001991
Brian Fostere339dd82018-07-11 22:26:34 -07001992 /*
1993 * Wait on the locked buffer, check for errors and unlock and
1994 * release the delwri queue reference.
1995 */
1996 error2 = xfs_buf_iowait(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001997 xfs_buf_relse(bp);
1998 if (!error)
1999 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002002 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
Brian Foster7912e7f2017-06-14 21:21:45 -07002005/*
2006 * Push a single buffer on a delwri queue.
2007 *
2008 * The purpose of this function is to submit a single buffer of a delwri queue
2009 * and return with the buffer still on the original queue. The waiting delwri
2010 * buffer submission infrastructure guarantees transfer of the delwri queue
2011 * buffer reference to a temporary wait list. We reuse this infrastructure to
2012 * transfer the buffer back to the original queue.
2013 *
2014 * Note the buffer transitions from the queued state, to the submitted and wait
2015 * listed state and back to the queued state during this call. The buffer
2016 * locking and queue management logic between _delwri_pushbuf() and
2017 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2018 * before returning.
2019 */
2020int
2021xfs_buf_delwri_pushbuf(
2022 struct xfs_buf *bp,
2023 struct list_head *buffer_list)
2024{
2025 LIST_HEAD (submit_list);
2026 int error;
2027
2028 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2029
2030 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2031
2032 /*
2033 * Isolate the buffer to a new local list so we can submit it for I/O
2034 * independently from the rest of the original list.
2035 */
2036 xfs_buf_lock(bp);
2037 list_move(&bp->b_list, &submit_list);
2038 xfs_buf_unlock(bp);
2039
2040 /*
2041 * Delwri submission clears the DELWRI_Q buffer flag and returns with
Brian Fostere339dd82018-07-11 22:26:34 -07002042 * the buffer on the wait list with the original reference. Rather than
Brian Foster7912e7f2017-06-14 21:21:45 -07002043 * bounce the buffer from a local wait list back to the original list
2044 * after I/O completion, reuse the original list as the wait list.
2045 */
2046 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2047
2048 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002049 * The buffer is now locked, under I/O and wait listed on the original
2050 * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2051 * return with the buffer unlocked and on the original queue.
Brian Foster7912e7f2017-06-14 21:21:45 -07002052 */
Brian Fostere339dd82018-07-11 22:26:34 -07002053 error = xfs_buf_iowait(bp);
Brian Foster7912e7f2017-06-14 21:21:45 -07002054 bp->b_flags |= _XBF_DELWRI_Q;
2055 xfs_buf_unlock(bp);
2056
2057 return error;
2058}
2059
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002060int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002061xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Nathan Scott87582802006-03-14 13:18:19 +11002063 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
2064 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002065 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002066 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002067
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002068 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002070 out:
Nathan Scott87582802006-03-14 13:18:19 +11002071 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074void
Nathan Scottce8e9222006-01-11 15:39:08 +11002075xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Nathan Scottce8e9222006-01-11 15:39:08 +11002077 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
Brian Foster7561d272017-10-17 14:16:29 -07002079
2080void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
2081{
Brian Foster7561d272017-10-17 14:16:29 -07002082 /*
2083 * Set the lru reference count to 0 based on the error injection tag.
2084 * This allows userspace to disrupt buffer caching for debug/testing
2085 * purposes.
2086 */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002087 if (XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_LRU_REF))
Brian Foster7561d272017-10-17 14:16:29 -07002088 lru_ref = 0;
2089
2090 atomic_set(&bp->b_lru_ref, lru_ref);
2091}
Brian Foster8473fee2019-02-07 10:45:46 -08002092
2093/*
2094 * Verify an on-disk magic value against the magic value specified in the
2095 * verifier structure. The verifier magic is in disk byte order so the caller is
2096 * expected to pass the value directly from disk.
2097 */
2098bool
2099xfs_verify_magic(
2100 struct xfs_buf *bp,
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002101 __be32 dmagic)
Brian Foster8473fee2019-02-07 10:45:46 -08002102{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002103 struct xfs_mount *mp = bp->b_mount;
Brian Foster8473fee2019-02-07 10:45:46 -08002104 int idx;
2105
2106 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002107 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic[idx]))
Brian Foster8473fee2019-02-07 10:45:46 -08002108 return false;
2109 return dmagic == bp->b_ops->magic[idx];
2110}
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002111/*
2112 * Verify an on-disk magic value against the magic value specified in the
2113 * verifier structure. The verifier magic is in disk byte order so the caller is
2114 * expected to pass the value directly from disk.
2115 */
2116bool
2117xfs_verify_magic16(
2118 struct xfs_buf *bp,
2119 __be16 dmagic)
2120{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002121 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002122 int idx;
2123
2124 idx = xfs_sb_version_hascrc(&mp->m_sb);
Denis Efremov14ed8682019-09-25 16:49:37 -07002125 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic16[idx]))
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002126 return false;
2127 return dmagic == bp->b_ops->magic16[idx];
2128}