blob: be79dd25b8cfb0fd12d7cde0e8f57227d2e30eec [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/stddef.h>
8#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/pagemap.h>
11#include <linux/init.h>
12#include <linux/vmalloc.h>
13#include <linux/bio.h>
14#include <linux/sysctl.h>
15#include <linux/proc_fs.h>
16#include <linux/workqueue.h>
17#include <linux/percpu.h>
18#include <linux/blkdev.h>
19#include <linux/hash.h>
Christoph Hellwig4df08c52005-09-05 08:34:18 +100020#include <linux/kthread.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080021#include <linux/migrate.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070022#include <linux/backing-dev.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080023#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Christoph Hellwig4fb6e8a2014-11-28 14:25:04 +110025#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110026#include "xfs_log_format.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100027#include "xfs_trans_resv.h"
Dave Chinner239880e2013-10-23 10:50:10 +110028#include "xfs_sb.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050029#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000030#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_log.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070032#include "xfs_errortag.h"
Brian Foster7561d272017-10-17 14:16:29 -070033#include "xfs_error.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050034
David Chinner7989cb82007-02-10 18:34:56 +110035static kmem_zone_t *xfs_buf_zone;
Christoph Hellwig23ea4032005-06-21 15:14:01 +100036
Nathan Scottce8e9222006-01-11 15:39:08 +110037#ifdef XFS_BUF_LOCK_TRACKING
38# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
39# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
40# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#else
Nathan Scottce8e9222006-01-11 15:39:08 +110042# define XB_SET_OWNER(bp) do { } while (0)
43# define XB_CLEAR_OWNER(bp) do { } while (0)
44# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#endif
46
Nathan Scottce8e9222006-01-11 15:39:08 +110047#define xb_to_gfp(flags) \
Dave Chinneraa5c1582012-04-23 15:58:56 +100048 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
James Bottomley73c77e22010-01-25 11:42:24 -060051static inline int
52xfs_buf_is_vmapped(
53 struct xfs_buf *bp)
54{
55 /*
56 * Return true if the buffer is vmapped.
57 *
Dave Chinner611c9942012-04-23 15:59:07 +100058 * b_addr is null if the buffer is not mapped, but the code is clever
59 * enough to know it doesn't have to map a single page, so the check has
60 * to be both for b_addr and bp->b_page_count > 1.
James Bottomley73c77e22010-01-25 11:42:24 -060061 */
Dave Chinner611c9942012-04-23 15:59:07 +100062 return bp->b_addr && bp->b_page_count > 1;
James Bottomley73c77e22010-01-25 11:42:24 -060063}
64
65static inline int
66xfs_buf_vmap_len(
67 struct xfs_buf *bp)
68{
69 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
Brian Foster9c7504a2016-07-20 11:15:28 +100073 * Bump the I/O in flight count on the buftarg if we haven't yet done so for
74 * this buffer. The count is incremented once per buffer (per hold cycle)
75 * because the corresponding decrement is deferred to buffer release. Buffers
76 * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
77 * tracking adds unnecessary overhead. This is used for sychronization purposes
78 * with unmount (see xfs_wait_buftarg()), so all we really need is a count of
79 * in-flight buffers.
80 *
81 * Buffers that are never released (e.g., superblock, iclog buffers) must set
82 * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
83 * never reaches zero and unmount hangs indefinitely.
84 */
85static inline void
86xfs_buf_ioacct_inc(
87 struct xfs_buf *bp)
88{
Brian Foster63db7c82017-05-31 08:22:52 -070089 if (bp->b_flags & XBF_NO_IOACCT)
Brian Foster9c7504a2016-07-20 11:15:28 +100090 return;
91
92 ASSERT(bp->b_flags & XBF_ASYNC);
Brian Foster63db7c82017-05-31 08:22:52 -070093 spin_lock(&bp->b_lock);
94 if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) {
95 bp->b_state |= XFS_BSTATE_IN_FLIGHT;
96 percpu_counter_inc(&bp->b_target->bt_io_count);
97 }
98 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +100099}
100
101/*
102 * Clear the in-flight state on a buffer about to be released to the LRU or
103 * freed and unaccount from the buftarg.
104 */
105static inline void
Brian Foster63db7c82017-05-31 08:22:52 -0700106__xfs_buf_ioacct_dec(
107 struct xfs_buf *bp)
108{
Brian Foster95989c42017-06-08 08:23:07 -0700109 lockdep_assert_held(&bp->b_lock);
Brian Foster63db7c82017-05-31 08:22:52 -0700110
111 if (bp->b_state & XFS_BSTATE_IN_FLIGHT) {
112 bp->b_state &= ~XFS_BSTATE_IN_FLIGHT;
113 percpu_counter_dec(&bp->b_target->bt_io_count);
114 }
115}
116
117static inline void
Brian Foster9c7504a2016-07-20 11:15:28 +1000118xfs_buf_ioacct_dec(
119 struct xfs_buf *bp)
120{
Brian Foster63db7c82017-05-31 08:22:52 -0700121 spin_lock(&bp->b_lock);
122 __xfs_buf_ioacct_dec(bp);
123 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000124}
125
126/*
Dave Chinner430cbeb2010-12-02 16:30:55 +1100127 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
128 * b_lru_ref count so that the buffer is freed immediately when the buffer
129 * reference count falls to zero. If the buffer is already on the LRU, we need
130 * to remove the reference that LRU holds on the buffer.
131 *
132 * This prevents build-up of stale buffers on the LRU.
133 */
134void
135xfs_buf_stale(
136 struct xfs_buf *bp)
137{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000138 ASSERT(xfs_buf_islocked(bp));
139
Dave Chinner430cbeb2010-12-02 16:30:55 +1100140 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000141
142 /*
143 * Clear the delwri status so that a delwri queue walker will not
144 * flush this buffer to disk now that it is stale. The delwri queue has
145 * a reference to the buffer, so this is safe to do.
146 */
147 bp->b_flags &= ~_XBF_DELWRI_Q;
148
Brian Foster9c7504a2016-07-20 11:15:28 +1000149 /*
150 * Once the buffer is marked stale and unlocked, a subsequent lookup
151 * could reset b_flags. There is no guarantee that the buffer is
152 * unaccounted (released to LRU) before that occurs. Drop in-flight
153 * status now to preserve accounting consistency.
154 */
Dave Chinnera4082352013-08-28 10:18:06 +1000155 spin_lock(&bp->b_lock);
Brian Foster63db7c82017-05-31 08:22:52 -0700156 __xfs_buf_ioacct_dec(bp);
157
Dave Chinnera4082352013-08-28 10:18:06 +1000158 atomic_set(&bp->b_lru_ref, 0);
159 if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
Dave Chinnere80dfa12013-08-28 10:18:05 +1000160 (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
161 atomic_dec(&bp->b_hold);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100162
Dave Chinner430cbeb2010-12-02 16:30:55 +1100163 ASSERT(atomic_read(&bp->b_hold) >= 1);
Dave Chinnera4082352013-08-28 10:18:06 +1000164 spin_unlock(&bp->b_lock);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Dave Chinner3e85c862012-06-22 18:50:09 +1000167static int
168xfs_buf_get_maps(
169 struct xfs_buf *bp,
170 int map_count)
171{
172 ASSERT(bp->b_maps == NULL);
173 bp->b_map_count = map_count;
174
175 if (map_count == 1) {
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600176 bp->b_maps = &bp->__b_map;
Dave Chinner3e85c862012-06-22 18:50:09 +1000177 return 0;
178 }
179
180 bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
181 KM_NOFS);
182 if (!bp->b_maps)
Dave Chinner24513372014-06-25 14:58:08 +1000183 return -ENOMEM;
Dave Chinner3e85c862012-06-22 18:50:09 +1000184 return 0;
185}
186
187/*
188 * Frees b_pages if it was allocated.
189 */
190static void
191xfs_buf_free_maps(
192 struct xfs_buf *bp)
193{
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600194 if (bp->b_maps != &bp->__b_map) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000195 kmem_free(bp->b_maps);
196 bp->b_maps = NULL;
197 }
198}
199
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000200struct xfs_buf *
Dave Chinner3e85c862012-06-22 18:50:09 +1000201_xfs_buf_alloc(
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000202 struct xfs_buftarg *target,
Dave Chinner3e85c862012-06-22 18:50:09 +1000203 struct xfs_buf_map *map,
204 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100205 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000207 struct xfs_buf *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000208 int error;
209 int i;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000210
Dave Chinneraa5c1582012-04-23 15:58:56 +1000211 bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000212 if (unlikely(!bp))
213 return NULL;
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /*
Dave Chinner12bcb3f2012-04-23 15:59:05 +1000216 * We don't want certain flags to appear in b_flags unless they are
217 * specifically set by later operations on the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 */
Dave Chinner611c9942012-04-23 15:59:07 +1000219 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Nathan Scottce8e9222006-01-11 15:39:08 +1100221 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100222 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000223 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100224 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100225 INIT_LIST_HEAD(&bp->b_list);
Carlos Maiolino643c8c02018-01-24 13:38:49 -0800226 INIT_LIST_HEAD(&bp->b_li_list);
Thomas Gleixnera731cd112010-09-07 14:33:15 +0000227 sema_init(&bp->b_sema, 0); /* held, no waiters */
Dave Chinnera4082352013-08-28 10:18:06 +1000228 spin_lock_init(&bp->b_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100229 XB_SET_OWNER(bp);
230 bp->b_target = target;
Dave Chinner3e85c862012-06-22 18:50:09 +1000231 bp->b_flags = flags;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000234 * Set length and io_length to the same value initially.
235 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * most cases but may be reset (e.g. XFS recovery).
237 */
Dave Chinner3e85c862012-06-22 18:50:09 +1000238 error = xfs_buf_get_maps(bp, nmaps);
239 if (error) {
240 kmem_zone_free(xfs_buf_zone, bp);
241 return NULL;
242 }
243
244 bp->b_bn = map[0].bm_bn;
245 bp->b_length = 0;
246 for (i = 0; i < nmaps; i++) {
247 bp->b_maps[i].bm_bn = map[i].bm_bn;
248 bp->b_maps[i].bm_len = map[i].bm_len;
249 bp->b_length += map[i].bm_len;
250 }
251 bp->b_io_length = bp->b_length;
252
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
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100256 XFS_STATS_INC(target->bt_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 */
307void
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;
348
Dave Chinner0e6e8472011-03-26 09:16:45 +1100349 /*
350 * for buffers that are contained within a single page, just allocate
351 * the memory from the heap - there's no need for the complexity of
352 * page arrays to keep allocation down to order 0.
353 */
Dave Chinner795cac72012-04-23 15:58:53 +1000354 size = BBTOB(bp->b_length);
355 if (size < PAGE_SIZE) {
Dave Chinneraa5c1582012-04-23 15:58:56 +1000356 bp->b_addr = kmem_alloc(size, KM_NOFS);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100357 if (!bp->b_addr) {
358 /* low memory - use alloc_page loop instead */
359 goto use_alloc_page;
360 }
361
Dave Chinner795cac72012-04-23 15:58:53 +1000362 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100363 ((unsigned long)bp->b_addr & PAGE_MASK)) {
364 /* b_addr spans two pages - use alloc_page instead */
365 kmem_free(bp->b_addr);
366 bp->b_addr = NULL;
367 goto use_alloc_page;
368 }
369 bp->b_offset = offset_in_page(bp->b_addr);
370 bp->b_pages = bp->b_page_array;
371 bp->b_pages[0] = virt_to_page(bp->b_addr);
372 bp->b_page_count = 1;
Dave Chinner611c9942012-04-23 15:59:07 +1000373 bp->b_flags |= _XBF_KMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100374 return 0;
375 }
376
377use_alloc_page:
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600378 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
379 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000380 >> PAGE_SHIFT;
Dave Chinner795cac72012-04-23 15:58:53 +1000381 page_count = end - start;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000382 error = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (unlikely(error))
384 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Nathan Scottce8e9222006-01-11 15:39:08 +1100386 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100387 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Nathan Scottce8e9222006-01-11 15:39:08 +1100389 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct page *page;
391 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100392retry:
393 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100395 if (flags & XBF_READ_AHEAD) {
396 bp->b_page_count = i;
Dave Chinner24513372014-06-25 14:58:08 +1000397 error = -ENOMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100398 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
401 /*
402 * This could deadlock.
403 *
404 * But until all the XFS lowlevel code is revamped to
405 * handle buffer allocation failures we can't do much.
406 */
407 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100408 xfs_err(NULL,
Tetsuo Handa5bf97b12015-10-12 15:41:29 +1100409 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
410 current->comm, current->pid,
Harvey Harrison34a622b2008-04-10 12:19:21 +1000411 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100413 XFS_STATS_INC(bp->b_target->bt_mount, xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200414 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 goto retry;
416 }
417
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100418 XFS_STATS_INC(bp->b_target->bt_mount, xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Dave Chinner0e6e8472011-03-26 09:16:45 +1100420 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100422 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 offset = 0;
424 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Dave Chinner0e6e8472011-03-26 09:16:45 +1100427out_free_pages:
428 for (i = 0; i < bp->b_page_count; i++)
429 __free_page(bp->b_pages[i]);
Darrick J. Wong2aa6ba7b2017-01-25 20:24:57 -0800430 bp->b_flags &= ~_XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return error;
432}
433
434/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300435 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
437STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100438_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 xfs_buf_t *bp,
440 uint flags)
441{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100442 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100443 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100444 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100445 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
Dave Chinner611c9942012-04-23 15:59:07 +1000446 } else if (flags & XBF_UNMAPPED) {
447 bp->b_addr = NULL;
448 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100449 int retried = 0;
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700450 unsigned nofs_flag;
Dave Chinnera19fb382011-03-26 09:13:42 +1100451
Dave Chinnerae687e52014-03-07 16:19:14 +1100452 /*
453 * vm_map_ram() will allocate auxillary structures (e.g.
454 * pagetables) with GFP_KERNEL, yet we are likely to be under
455 * GFP_NOFS context here. Hence we need to tell memory reclaim
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700456 * that we are in such a context via PF_MEMALLOC_NOFS to prevent
Dave Chinnerae687e52014-03-07 16:19:14 +1100457 * memory reclaim re-entering the filesystem here and
458 * potentially deadlocking.
459 */
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700460 nofs_flag = memalloc_nofs_save();
Dave Chinnera19fb382011-03-26 09:13:42 +1100461 do {
462 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
463 -1, PAGE_KERNEL);
464 if (bp->b_addr)
465 break;
466 vm_unmap_aliases();
467 } while (retried++ <= 1);
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700468 memalloc_nofs_restore(nofs_flag);
Dave Chinnera19fb382011-03-26 09:13:42 +1100469
470 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100472 bp->b_addr += bp->b_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
475 return 0;
476}
477
478/*
479 * Finding and Reading Buffers
480 */
Lucas Stach6031e732016-12-07 17:36:36 +1100481static int
482_xfs_buf_obj_cmp(
483 struct rhashtable_compare_arg *arg,
484 const void *obj)
485{
486 const struct xfs_buf_map *map = arg->key;
487 const struct xfs_buf *bp = obj;
488
489 /*
490 * The key hashing in the lookup path depends on the key being the
491 * first element of the compare_arg, make sure to assert this.
492 */
493 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
494
495 if (bp->b_bn != map->bm_bn)
496 return 1;
497
498 if (unlikely(bp->b_length != map->bm_len)) {
499 /*
500 * found a block number match. If the range doesn't
501 * match, the only way this is allowed is if the buffer
502 * in the cache is stale and the transaction that made
503 * it stale has not yet committed. i.e. we are
504 * reallocating a busy extent. Skip this buffer and
505 * continue searching for an exact match.
506 */
507 ASSERT(bp->b_flags & XBF_STALE);
508 return 1;
509 }
510 return 0;
511}
512
513static const struct rhashtable_params xfs_buf_hash_params = {
514 .min_size = 32, /* empty AGs have minimal footprint */
515 .nelem_hint = 16,
516 .key_len = sizeof(xfs_daddr_t),
517 .key_offset = offsetof(struct xfs_buf, b_bn),
518 .head_offset = offsetof(struct xfs_buf, b_rhash_head),
519 .automatic_shrinking = true,
520 .obj_cmpfn = _xfs_buf_obj_cmp,
521};
522
523int
524xfs_buf_hash_init(
525 struct xfs_perag *pag)
526{
527 spin_lock_init(&pag->pag_buf_lock);
528 return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
529}
530
531void
532xfs_buf_hash_destroy(
533 struct xfs_perag *pag)
534{
535 rhashtable_destroy(&pag->pag_buf_hash);
536}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538/*
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700539 * Look up a buffer in the buffer cache and return it referenced and locked
540 * in @found_bp.
541 *
542 * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the
543 * cache.
544 *
545 * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return
546 * -EAGAIN if we fail to lock it.
547 *
548 * Return values are:
549 * -EFSCORRUPTED if have been supplied with an invalid address
550 * -EAGAIN on trylock failure
551 * -ENOENT if we fail to find a match and @new_bp was NULL
552 * 0, with @found_bp:
553 * - @new_bp if we inserted it into the cache
554 * - the buffer we found and locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700556static int
557xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000558 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000559 struct xfs_buf_map *map,
560 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100561 xfs_buf_flags_t flags,
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700562 struct xfs_buf *new_bp,
563 struct xfs_buf **found_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Dave Chinner74f75a02010-09-24 19:59:04 +1000565 struct xfs_perag *pag;
Dave Chinner74f75a02010-09-24 19:59:04 +1000566 xfs_buf_t *bp;
Lucas Stach6031e732016-12-07 17:36:36 +1100567 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
Dave Chinner10616b82013-01-21 23:53:52 +1100568 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000569 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700571 *found_bp = NULL;
572
Dave Chinner3e85c862012-06-22 18:50:09 +1000573 for (i = 0; i < nmaps; i++)
Lucas Stach6031e732016-12-07 17:36:36 +1100574 cmap.bm_len += map[i].bm_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 /* Check for IOs smaller than the sector size / not sector aligned */
Lucas Stach6031e732016-12-07 17:36:36 +1100577 ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
578 ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Dave Chinner10616b82013-01-21 23:53:52 +1100580 /*
581 * Corrupted block numbers can get through to here, unfortunately, so we
582 * have to check that the buffer falls within the filesystem bounds.
583 */
584 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
Lucas Stach6031e732016-12-07 17:36:36 +1100585 if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
Dave Chinner10616b82013-01-21 23:53:52 +1100586 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -0800587 "%s: daddr 0x%llx out of range, EOFS 0x%llx",
Lucas Stach6031e732016-12-07 17:36:36 +1100588 __func__, cmap.bm_bn, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000589 WARN_ON(1);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700590 return -EFSCORRUPTED;
Dave Chinner10616b82013-01-21 23:53:52 +1100591 }
592
Dave Chinner74f75a02010-09-24 19:59:04 +1000593 pag = xfs_perag_get(btp->bt_mount,
Lucas Stach6031e732016-12-07 17:36:36 +1100594 xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Dave Chinner74f75a02010-09-24 19:59:04 +1000596 spin_lock(&pag->pag_buf_lock);
Lucas Stach6031e732016-12-07 17:36:36 +1100597 bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
598 xfs_buf_hash_params);
599 if (bp) {
600 atomic_inc(&bp->b_hold);
601 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
604 /* No match found */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700605 if (!new_bp) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100606 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000607 spin_unlock(&pag->pag_buf_lock);
608 xfs_perag_put(pag);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700609 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700611
612 /* the buffer keeps the perag reference until it is freed */
613 new_bp->b_pag = pag;
614 rhashtable_insert_fast(&pag->pag_buf_hash, &new_bp->b_rhash_head,
615 xfs_buf_hash_params);
616 spin_unlock(&pag->pag_buf_lock);
617 *found_bp = new_bp;
618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000621 spin_unlock(&pag->pag_buf_lock);
622 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200624 if (!xfs_buf_trylock(bp)) {
625 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100626 xfs_buf_rele(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100627 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700628 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200630 xfs_buf_lock(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100631 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
Dave Chinner0e6e8472011-03-26 09:16:45 +1100634 /*
635 * if the buffer is stale, clear all the external state associated with
636 * it. We need to keep flags such as how we allocated the buffer memory
637 * intact here.
638 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100639 if (bp->b_flags & XBF_STALE) {
640 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinnercfb02852012-11-12 22:54:19 +1100641 ASSERT(bp->b_iodone == NULL);
Dave Chinner611c9942012-04-23 15:59:07 +1000642 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100643 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000644 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000645
646 trace_xfs_buf_find(bp, flags, _RET_IP_);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100647 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700648 *found_bp = bp;
649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Dave Chinner8925a3d2018-04-18 08:25:20 -0700652struct xfs_buf *
653xfs_buf_incore(
654 struct xfs_buftarg *target,
655 xfs_daddr_t blkno,
656 size_t numblks,
657 xfs_buf_flags_t flags)
658{
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700659 struct xfs_buf *bp;
660 int error;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700661 DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700662
663 error = xfs_buf_find(target, &map, 1, flags, NULL, &bp);
664 if (error)
665 return NULL;
666 return bp;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/*
Dave Chinner38158322011-09-30 04:45:02 +0000670 * Assembles a buffer covering the specified range. The code is optimised for
671 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
672 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 */
Dave Chinner38158322011-09-30 04:45:02 +0000674struct xfs_buf *
Dave Chinner6dde2702012-06-22 18:50:10 +1000675xfs_buf_get_map(
676 struct xfs_buftarg *target,
677 struct xfs_buf_map *map,
678 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100679 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Dave Chinner38158322011-09-30 04:45:02 +0000681 struct xfs_buf *bp;
682 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100683 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700685 error = xfs_buf_find(target, map, nmaps, flags, NULL, &bp);
686
687 switch (error) {
688 case 0:
689 /* cache hit */
Dave Chinner38158322011-09-30 04:45:02 +0000690 goto found;
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700691 case -EAGAIN:
692 /* cache hit, trylock failure, caller handles failure */
693 ASSERT(flags & XBF_TRYLOCK);
694 return NULL;
695 case -ENOENT:
696 /* cache miss, go for insert */
697 break;
698 case -EFSCORRUPTED:
699 default:
700 /*
701 * None of the higher layers understand failure types
702 * yet, so return NULL to signal a fatal lookup error.
703 */
704 return NULL;
705 }
Dave Chinner38158322011-09-30 04:45:02 +0000706
Dave Chinner6dde2702012-06-22 18:50:10 +1000707 new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100708 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return NULL;
710
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000711 error = xfs_buf_allocate_memory(new_bp, flags);
712 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000713 xfs_buf_free(new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000714 return NULL;
715 }
716
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700717 error = xfs_buf_find(target, map, nmaps, flags, new_bp, &bp);
718 if (error) {
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000719 xfs_buf_free(new_bp);
720 return NULL;
721 }
722
723 if (bp != new_bp)
724 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Dave Chinner38158322011-09-30 04:45:02 +0000726found:
Dave Chinner611c9942012-04-23 15:59:07 +1000727 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100728 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100730 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500731 "%s: failed to map pagesn", __func__);
Dave Chinnera8acad72012-04-23 15:58:54 +1000732 xfs_buf_relse(bp);
733 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735 }
736
Dave Chinnerb79f4a12016-01-12 07:03:44 +1100737 /*
738 * Clear b_error if this is a lookup from a caller that doesn't expect
739 * valid data to be found in the buffer.
740 */
741 if (!(flags & XBF_READ))
742 xfs_buf_ioerror(bp, 0);
743
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100744 XFS_STATS_INC(target->bt_mount, xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000745 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100746 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100749STATIC int
750_xfs_buf_read(
751 xfs_buf_t *bp,
752 xfs_buf_flags_t flags)
753{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000754 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600755 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100756
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000757 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200758 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100759
Brian Foster6af88cd2018-07-11 22:26:35 -0700760 return xfs_buf_submit(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763xfs_buf_t *
Dave Chinner6dde2702012-06-22 18:50:10 +1000764xfs_buf_read_map(
765 struct xfs_buftarg *target,
766 struct xfs_buf_map *map,
767 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100768 xfs_buf_flags_t flags,
Dave Chinner1813dd62012-11-14 17:54:40 +1100769 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Dave Chinner6dde2702012-06-22 18:50:10 +1000771 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Nathan Scottce8e9222006-01-11 15:39:08 +1100773 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Dave Chinner6dde2702012-06-22 18:50:10 +1000775 bp = xfs_buf_get_map(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100776 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000777 trace_xfs_buf_read(bp, flags, _RET_IP_);
778
Dave Chinnerb0388bf2016-02-10 15:01:11 +1100779 if (!(bp->b_flags & XBF_DONE)) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100780 XFS_STATS_INC(target->bt_mount, xb_get_read);
Dave Chinner1813dd62012-11-14 17:54:40 +1100781 bp->b_ops = ops;
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100782 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100783 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 /*
785 * Read ahead call which is already satisfied,
786 * drop the buffer
787 */
Dave Chinnera8acad72012-04-23 15:58:54 +1000788 xfs_buf_relse(bp);
789 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100792 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794 }
795
Nathan Scottce8e9222006-01-11 15:39:08 +1100796 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100800 * If we are not low on memory then do the readahead in a deadlock
801 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 */
803void
Dave Chinner6dde2702012-06-22 18:50:10 +1000804xfs_buf_readahead_map(
805 struct xfs_buftarg *target,
806 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100807 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100808 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Jan Karaefa7c9f2017-02-02 15:56:53 +0100810 if (bdi_read_congested(target->bt_bdev->bd_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return;
812
Dave Chinner6dde2702012-06-22 18:50:10 +1000813 xfs_buf_read_map(target, map, nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100814 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Dave Chinner5adc94c2010-09-24 21:58:31 +1000817/*
818 * Read an uncached buffer from disk. Allocates and returns a locked
819 * buffer containing the disk contents or nothing.
820 */
Dave Chinnerba3726742014-10-02 09:05:32 +1000821int
Dave Chinner5adc94c2010-09-24 21:58:31 +1000822xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000823 struct xfs_buftarg *target,
824 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000825 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100826 int flags,
Dave Chinnerba3726742014-10-02 09:05:32 +1000827 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100828 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000829{
Dave Chinnereab4e632012-11-12 22:54:02 +1100830 struct xfs_buf *bp;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000831
Dave Chinnerba3726742014-10-02 09:05:32 +1000832 *bpp = NULL;
833
Dave Chinnere70b73f2012-04-23 15:58:49 +1000834 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000835 if (!bp)
Dave Chinnerba3726742014-10-02 09:05:32 +1000836 return -ENOMEM;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000837
838 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000839 ASSERT(bp->b_map_count == 1);
Dave Chinnerba3726742014-10-02 09:05:32 +1000840 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
Dave Chinner3e85c862012-06-22 18:50:09 +1000841 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000842 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100843 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000844
Brian Foster6af88cd2018-07-11 22:26:35 -0700845 xfs_buf_submit(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000846 if (bp->b_error) {
847 int error = bp->b_error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800848 xfs_buf_relse(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000849 return error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800850 }
Dave Chinnerba3726742014-10-02 09:05:32 +1000851
852 *bpp = bp;
853 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
Dave Chinner44396472011-04-21 09:34:27 +0000856/*
857 * Return a buffer allocated as an empty buffer and associated to external
858 * memory via xfs_buf_associate_memory() back to it's empty state.
859 */
860void
861xfs_buf_set_empty(
862 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000863 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000864{
865 if (bp->b_pages)
866 _xfs_buf_free_pages(bp);
867
868 bp->b_pages = NULL;
869 bp->b_page_count = 0;
870 bp->b_addr = NULL;
Dave Chinner4e94b712012-04-23 15:58:51 +1000871 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000872 bp->b_io_length = numblks;
Dave Chinner3e85c862012-06-22 18:50:09 +1000873
874 ASSERT(bp->b_map_count == 1);
Dave Chinner44396472011-04-21 09:34:27 +0000875 bp->b_bn = XFS_BUF_DADDR_NULL;
Dave Chinner3e85c862012-06-22 18:50:09 +1000876 bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL;
877 bp->b_maps[0].bm_len = bp->b_length;
Dave Chinner44396472011-04-21 09:34:27 +0000878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880static inline struct page *
881mem_to_page(
882 void *addr)
883{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800884 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return virt_to_page(addr);
886 } else {
887 return vmalloc_to_page(addr);
888 }
889}
890
891int
Nathan Scottce8e9222006-01-11 15:39:08 +1100892xfs_buf_associate_memory(
893 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 void *mem,
895 size_t len)
896{
897 int rval;
898 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100899 unsigned long pageaddr;
900 unsigned long offset;
901 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 int page_count;
903
Dave Chinner0e6e8472011-03-26 09:16:45 +1100904 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100905 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100906 buflen = PAGE_ALIGN(len + offset);
907 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100910 if (bp->b_pages)
911 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Nathan Scottce8e9222006-01-11 15:39:08 +1100913 bp->b_pages = NULL;
914 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Eric Sandeen87937bf2014-04-14 19:01:20 +1000916 rval = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (rval)
918 return rval;
919
Nathan Scottce8e9222006-01-11 15:39:08 +1100920 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100922 for (i = 0; i < bp->b_page_count; i++) {
923 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100924 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Dave Chinneraa0e8832012-04-23 15:58:52 +1000927 bp->b_io_length = BTOBB(len);
Dave Chinner4e94b712012-04-23 15:58:51 +1000928 bp->b_length = BTOBB(buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 return 0;
931}
932
933xfs_buf_t *
Dave Chinner686865f72010-09-24 20:07:47 +1000934xfs_buf_get_uncached(
935 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000936 size_t numblks,
Dave Chinner686865f72010-09-24 20:07:47 +1000937 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000939 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000940 int error, i;
Dave Chinner3e85c862012-06-22 18:50:09 +1000941 struct xfs_buf *bp;
942 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Brian Fosterc891c302016-07-20 11:13:43 +1000944 /* flags might contain irrelevant bits, pass only what we care about */
945 bp = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (unlikely(bp == NULL))
947 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Dave Chinnere70b73f2012-04-23 15:58:49 +1000949 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000950 error = _xfs_buf_get_pages(bp, page_count);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000951 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 goto fail_free_buf;
953
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000954 for (i = 0; i < page_count; i++) {
Dave Chinner686865f72010-09-24 20:07:47 +1000955 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000956 if (!bp->b_pages[i])
957 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000959 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Dave Chinner611c9942012-04-23 15:59:07 +1000961 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000962 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100963 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500964 "%s: failed to map pages", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Dave Chinner686865f72010-09-24 20:07:47 +1000968 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000972 while (--i >= 0)
973 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000974 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 fail_free_buf:
Dave Chinner3e85c862012-06-22 18:50:09 +1000976 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000977 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 fail:
979 return NULL;
980}
981
982/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * Increment reference count on buffer, to hold the buffer concurrently
984 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 * Must hold the buffer already to call this function.
986 */
987void
Nathan Scottce8e9222006-01-11 15:39:08 +1100988xfs_buf_hold(
989 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000991 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100992 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
995/*
Brian Foster9c7504a2016-07-20 11:15:28 +1000996 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
997 * placed on LRU or freed (depending on b_lru_ref).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
999void
Nathan Scottce8e9222006-01-11 15:39:08 +11001000xfs_buf_rele(
1001 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Dave Chinner74f75a02010-09-24 19:59:04 +10001003 struct xfs_perag *pag = bp->b_pag;
Brian Foster9c7504a2016-07-20 11:15:28 +10001004 bool release;
1005 bool freebuf = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001007 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Dave Chinner74f75a02010-09-24 19:59:04 +10001009 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +11001010 ASSERT(list_empty(&bp->b_lru));
Brian Foster9c7504a2016-07-20 11:15:28 +10001011 if (atomic_dec_and_test(&bp->b_hold)) {
1012 xfs_buf_ioacct_dec(bp);
Nathan Scottfad3aa12006-02-01 12:14:52 +11001013 xfs_buf_free(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001014 }
Nathan Scottfad3aa12006-02-01 12:14:52 +11001015 return;
1016 }
1017
Lachlan McIlroy37906892008-08-13 15:42:10 +10001018 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinnera4082352013-08-28 10:18:06 +10001019
Brian Foster9c7504a2016-07-20 11:15:28 +10001020 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
1021 spin_lock(&bp->b_lock);
1022 if (!release) {
1023 /*
1024 * Drop the in-flight state if the buffer is already on the LRU
1025 * and it holds the only reference. This is racy because we
1026 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1027 * ensures the decrement occurs only once per-buf.
1028 */
1029 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
Brian Foster63db7c82017-05-31 08:22:52 -07001030 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001031 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
Brian Foster9c7504a2016-07-20 11:15:28 +10001033
1034 /* the last reference has been dropped ... */
Brian Foster63db7c82017-05-31 08:22:52 -07001035 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001036 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1037 /*
1038 * If the buffer is added to the LRU take a new reference to the
1039 * buffer for the LRU and clear the (now stale) dispose list
1040 * state flag
1041 */
1042 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1043 bp->b_state &= ~XFS_BSTATE_DISPOSE;
1044 atomic_inc(&bp->b_hold);
1045 }
1046 spin_unlock(&pag->pag_buf_lock);
1047 } else {
1048 /*
1049 * most of the time buffers will already be removed from the
1050 * LRU, so optimise that case by checking for the
1051 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1052 * was on was the disposal list
1053 */
1054 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1055 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1056 } else {
1057 ASSERT(list_empty(&bp->b_lru));
1058 }
1059
1060 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Lucas Stach6031e732016-12-07 17:36:36 +11001061 rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
1062 xfs_buf_hash_params);
Brian Foster9c7504a2016-07-20 11:15:28 +10001063 spin_unlock(&pag->pag_buf_lock);
1064 xfs_perag_put(pag);
1065 freebuf = true;
1066 }
1067
1068out_unlock:
1069 spin_unlock(&bp->b_lock);
1070
1071 if (freebuf)
1072 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
1075
1076/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001077 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +11001078 *
1079 * If we come across a stale, pinned, locked buffer, we know that we are
1080 * being asked to lock a buffer that has been reallocated. Because it is
1081 * pinned, we know that the log has not been pushed to disk and hence it
1082 * will still be locked. Rather than continuing to have trylock attempts
1083 * fail until someone else pushes the log, push it ourselves before
1084 * returning. This means that the xfsaild will not get stuck trying
1085 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
1087int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001088xfs_buf_trylock(
1089 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 int locked;
1092
Nathan Scottce8e9222006-01-11 15:39:08 +11001093 locked = down_trylock(&bp->b_sema) == 0;
Darrick J. Wong479c6412016-06-21 11:53:28 +10001094 if (locked) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001095 XB_SET_OWNER(bp);
Darrick J. Wong479c6412016-06-21 11:53:28 +10001096 trace_xfs_buf_trylock(bp, _RET_IP_);
1097 } else {
1098 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
1099 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001100 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001104 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +10001105 *
1106 * If we come across a stale, pinned, locked buffer, we know that we
1107 * are being asked to lock a buffer that has been reallocated. Because
1108 * it is pinned, we know that the log has not been pushed to disk and
1109 * hence it will still be locked. Rather than sleeping until someone
1110 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001112void
1113xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001114 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001116 trace_xfs_buf_lock(bp, _RET_IP_);
1117
Dave Chinnered3b4d62010-05-21 12:07:08 +10001118 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +10001119 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +11001120 down(&bp->b_sema);
1121 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001122
1123 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126void
Nathan Scottce8e9222006-01-11 15:39:08 +11001127xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001128 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Brian Foster20e8a062017-04-21 12:40:44 -07001130 ASSERT(xfs_buf_islocked(bp));
1131
Nathan Scottce8e9222006-01-11 15:39:08 +11001132 XB_CLEAR_OWNER(bp);
1133 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001134
1135 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
Nathan Scottce8e9222006-01-11 15:39:08 +11001138STATIC void
1139xfs_buf_wait_unpin(
1140 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 DECLARE_WAITQUEUE (wait, current);
1143
Nathan Scottce8e9222006-01-11 15:39:08 +11001144 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return;
1146
Nathan Scottce8e9222006-01-11 15:39:08 +11001147 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 for (;;) {
1149 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001150 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001152 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001154 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 set_current_state(TASK_RUNNING);
1156}
1157
1158/*
1159 * Buffer Utility Routines
1160 */
1161
Dave Chinnere8aaba92014-10-02 09:04:22 +10001162void
1163xfs_buf_ioend(
1164 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001166 bool read = bp->b_flags & XBF_READ;
1167
1168 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001169
1170 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Dave Chinnerd5929de2013-02-27 13:25:54 +11001171
Dave Chinner61be9c52014-10-02 09:04:31 +10001172 /*
1173 * Pull in IO completion errors now. We are guaranteed to be running
1174 * single threaded, so we don't need the lock to read b_io_error.
1175 */
1176 if (!bp->b_error && bp->b_io_error)
1177 xfs_buf_ioerror(bp, bp->b_io_error);
1178
Dave Chinnere8aaba92014-10-02 09:04:22 +10001179 /* Only validate buffers that were read without errors */
1180 if (read && !bp->b_error && bp->b_ops) {
1181 ASSERT(!bp->b_iodone);
Dave Chinner1813dd62012-11-14 17:54:40 +11001182 bp->b_ops->verify_read(bp);
Dave Chinnere8aaba92014-10-02 09:04:22 +10001183 }
1184
1185 if (!bp->b_error)
1186 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Christoph Hellwig80f6c292010-08-18 05:29:11 -04001188 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001189 (*(bp->b_iodone))(bp);
1190 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 xfs_buf_relse(bp);
Dave Chinner595bff752014-10-02 09:05:14 +10001192 else
Dave Chinner1813dd62012-11-14 17:54:40 +11001193 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Dave Chinnere8aaba92014-10-02 09:04:22 +10001196static void
1197xfs_buf_ioend_work(
1198 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001200 struct xfs_buf *bp =
Brian Fosterb29c70f2014-12-04 09:43:17 +11001201 container_of(work, xfs_buf_t, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001202
Dave Chinnere8aaba92014-10-02 09:04:22 +10001203 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001206static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001207xfs_buf_ioend_async(
1208 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001210 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
1211 queue_work(bp->b_ioend_wq, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214void
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001215__xfs_buf_ioerror(
Nathan Scottce8e9222006-01-11 15:39:08 +11001216 xfs_buf_t *bp,
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001217 int error,
1218 xfs_failaddr_t failaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Dave Chinner24513372014-06-25 14:58:08 +10001220 ASSERT(error <= 0 && error >= -1000);
1221 bp->b_error = error;
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001222 trace_xfs_buf_ioerror(bp, error, failaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Christoph Hellwig901796a2011-10-10 16:52:49 +00001225void
1226xfs_buf_ioerror_alert(
1227 struct xfs_buf *bp,
1228 const char *func)
1229{
1230 xfs_alert(bp->b_target->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001231"metadata I/O error in \"%s\" at daddr 0x%llx len %d error %d",
1232 func, (uint64_t)XFS_BUF_ADDR(bp), bp->b_length,
1233 -bp->b_error);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001234}
1235
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001236int
1237xfs_bwrite(
1238 struct xfs_buf *bp)
1239{
1240 int error;
1241
1242 ASSERT(xfs_buf_islocked(bp));
1243
1244 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001245 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
1246 XBF_WRITE_FAIL | XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001247
Brian Foster6af88cd2018-07-11 22:26:35 -07001248 error = xfs_buf_submit(bp);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001249 if (error) {
1250 xfs_force_shutdown(bp->b_target->bt_mount,
1251 SHUTDOWN_META_IO_ERROR);
1252 }
1253 return error;
1254}
1255
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001256static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001257xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001258 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001260 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Dave Chinner37eb17e2012-11-12 22:09:46 +11001262 /*
1263 * don't overwrite existing errors - otherwise we can lose errors on
1264 * buffers that require multiple bios to complete.
1265 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001266 if (bio->bi_status) {
1267 int error = blk_status_to_errno(bio->bi_status);
1268
1269 cmpxchg(&bp->b_io_error, 0, error);
1270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Dave Chinner37eb17e2012-11-12 22:09:46 +11001272 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001273 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1274
Dave Chinnere8aaba92014-10-02 09:04:22 +10001275 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1276 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Dave Chinner3e85c862012-06-22 18:50:09 +10001280static void
1281xfs_buf_ioapply_map(
1282 struct xfs_buf *bp,
1283 int map,
1284 int *buf_offset,
1285 int *count,
Mike Christie50bfcd02016-06-05 14:31:57 -05001286 int op,
1287 int op_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Dave Chinner3e85c862012-06-22 18:50:09 +10001289 int page_index;
1290 int total_nr_pages = bp->b_page_count;
1291 int nr_pages;
1292 struct bio *bio;
1293 sector_t sector = bp->b_maps[map].bm_bn;
1294 int size;
1295 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Dave Chinner3e85c862012-06-22 18:50:09 +10001297 /* skip the pages in the buffer before the start offset */
1298 page_index = 0;
1299 offset = *buf_offset;
1300 while (offset >= PAGE_SIZE) {
1301 page_index++;
1302 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001303 }
1304
Dave Chinner3e85c862012-06-22 18:50:09 +10001305 /*
1306 * Limit the IO size to the length of the current vector, and update the
1307 * remaining IO count for the next time around.
1308 */
1309 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1310 *count -= size;
1311 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001314 atomic_inc(&bp->b_io_remaining);
Ming Leic908e382016-05-30 21:34:33 +08001315 nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 bio = bio_alloc(GFP_NOIO, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001318 bio_set_dev(bio, bp->b_target->bt_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001319 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001320 bio->bi_end_io = xfs_buf_bio_end_io;
1321 bio->bi_private = bp;
Mike Christie50bfcd02016-06-05 14:31:57 -05001322 bio_set_op_attrs(bio, op, op_flags);
Dave Chinner0e6e8472011-03-26 09:16:45 +11001323
Dave Chinner3e85c862012-06-22 18:50:09 +10001324 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001325 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 if (nbytes > size)
1328 nbytes = size;
1329
Dave Chinner3e85c862012-06-22 18:50:09 +10001330 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1331 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001332 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 break;
1334
1335 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001336 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 size -= nbytes;
1338 total_nr_pages--;
1339 }
1340
Kent Overstreet4f024f32013-10-11 15:44:27 -07001341 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001342 if (xfs_buf_is_vmapped(bp)) {
1343 flush_kernel_vmap_range(bp->b_addr,
1344 xfs_buf_vmap_len(bp));
1345 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001346 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (size)
1348 goto next_chunk;
1349 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001350 /*
1351 * This is guaranteed not to be the last io reference count
Dave Chinner595bff752014-10-02 09:05:14 +10001352 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001353 */
1354 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001355 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001356 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001358
1359}
1360
1361STATIC void
1362_xfs_buf_ioapply(
1363 struct xfs_buf *bp)
1364{
1365 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001366 int op;
1367 int op_flags = 0;
Dave Chinner3e85c862012-06-22 18:50:09 +10001368 int offset;
1369 int size;
1370 int i;
1371
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001372 /*
1373 * Make sure we capture only current IO errors rather than stale errors
1374 * left over from previous use of the buffer (e.g. failed readahead).
1375 */
1376 bp->b_error = 0;
1377
Brian Fosterb29c70f2014-12-04 09:43:17 +11001378 /*
1379 * Initialize the I/O completion workqueue if we haven't yet or the
1380 * submitter has not opted to specify a custom one.
1381 */
1382 if (!bp->b_ioend_wq)
1383 bp->b_ioend_wq = bp->b_target->bt_mount->m_buf_workqueue;
1384
Dave Chinner3e85c862012-06-22 18:50:09 +10001385 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001386 op = REQ_OP_WRITE;
Dave Chinner3e85c862012-06-22 18:50:09 +10001387 if (bp->b_flags & XBF_SYNCIO)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001388 op_flags = REQ_SYNC;
Dave Chinner3e85c862012-06-22 18:50:09 +10001389 if (bp->b_flags & XBF_FUA)
Mike Christie50bfcd02016-06-05 14:31:57 -05001390 op_flags |= REQ_FUA;
Dave Chinner3e85c862012-06-22 18:50:09 +10001391 if (bp->b_flags & XBF_FLUSH)
Mike Christie28a8f0d2016-06-05 14:32:25 -05001392 op_flags |= REQ_PREFLUSH;
Dave Chinner1813dd62012-11-14 17:54:40 +11001393
1394 /*
1395 * Run the write verifier callback function if it exists. If
1396 * this function fails it will mark the buffer with an error and
1397 * the IO should not be dispatched.
1398 */
1399 if (bp->b_ops) {
1400 bp->b_ops->verify_write(bp);
1401 if (bp->b_error) {
1402 xfs_force_shutdown(bp->b_target->bt_mount,
1403 SHUTDOWN_CORRUPT_INCORE);
1404 return;
1405 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001406 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
1407 struct xfs_mount *mp = bp->b_target->bt_mount;
1408
1409 /*
1410 * non-crc filesystems don't attach verifiers during
1411 * log recovery, so don't warn for such filesystems.
1412 */
1413 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1414 xfs_warn(mp,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001415 "%s: no buf ops on daddr 0x%llx len %d",
Dave Chinner400b9d82014-08-04 12:42:40 +10001416 __func__, bp->b_bn, bp->b_length);
Darrick J. Wong9c712a12018-01-08 10:51:26 -08001417 xfs_hex_dump(bp->b_addr,
1418 XFS_CORRUPTION_DUMP_LEN);
Dave Chinner400b9d82014-08-04 12:42:40 +10001419 dump_stack();
1420 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001421 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001422 } else if (bp->b_flags & XBF_READ_AHEAD) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001423 op = REQ_OP_READ;
1424 op_flags = REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001425 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001426 op = REQ_OP_READ;
Dave Chinner3e85c862012-06-22 18:50:09 +10001427 }
1428
1429 /* we only use the buffer cache for meta-data */
Mike Christie50bfcd02016-06-05 14:31:57 -05001430 op_flags |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001431
1432 /*
1433 * Walk all the vectors issuing IO on them. Set up the initial offset
1434 * into the buffer and the desired IO size before we start -
1435 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1436 * subsequent call.
1437 */
1438 offset = bp->b_offset;
1439 size = BBTOB(bp->b_io_length);
1440 blk_start_plug(&plug);
1441 for (i = 0; i < bp->b_map_count; i++) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001442 xfs_buf_ioapply_map(bp, i, &offset, &size, op, op_flags);
Dave Chinner3e85c862012-06-22 18:50:09 +10001443 if (bp->b_error)
1444 break;
1445 if (size <= 0)
1446 break; /* all done */
1447 }
1448 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
Dave Chinner595bff752014-10-02 09:05:14 +10001451/*
1452 * Asynchronous IO submission path. This transfers the buffer lock ownership and
1453 * the current reference to the IO. It is not safe to reference the buffer after
1454 * a call to this function unless the caller holds an additional reference
1455 * itself.
1456 */
Brian Fostereaebb512018-07-11 22:26:34 -07001457static int
Brian Foster6af88cd2018-07-11 22:26:35 -07001458__xfs_buf_submit_common(
Dave Chinner595bff752014-10-02 09:05:14 +10001459 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Dave Chinner595bff752014-10-02 09:05:14 +10001461 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001463 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff752014-10-02 09:05:14 +10001464
1465 /* on shutdown we stale and complete the buffer immediately */
1466 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
1467 xfs_buf_ioerror(bp, -EIO);
1468 bp->b_flags &= ~XBF_DONE;
1469 xfs_buf_stale(bp);
Brian Fostereaebb512018-07-11 22:26:34 -07001470 return -EIO;
Dave Chinner595bff752014-10-02 09:05:14 +10001471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Christoph Hellwig375ec692011-08-23 08:28:03 +00001473 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001474 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Dave Chinner61be9c52014-10-02 09:04:31 +10001476 /* clear the internal error state to avoid spurious errors */
1477 bp->b_io_error = 0;
1478
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001479 /*
Brian Fostereaebb512018-07-11 22:26:34 -07001480 * Set the count to 1 initially, this will stop an I/O completion
1481 * callout which happens before we have started all the I/O from calling
1482 * xfs_buf_ioend too early.
1483 */
1484 atomic_set(&bp->b_io_remaining, 1);
1485 if (bp->b_flags & XBF_ASYNC)
1486 xfs_buf_ioacct_inc(bp);
1487 _xfs_buf_ioapply(bp);
1488
1489 /*
1490 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1491 * reference we took above. If we drop it to zero, run completion so
1492 * that we don't return to the caller with completion still pending.
1493 */
1494 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1495 if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
1496 xfs_buf_ioend(bp);
1497 else
1498 xfs_buf_ioend_async(bp);
1499 }
1500
1501 return 0;
1502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504/*
Brian Fostere339dd82018-07-11 22:26:34 -07001505 * Wait for I/O completion of a sync buffer and return the I/O error code.
1506 */
1507static int
1508xfs_buf_iowait(
1509 struct xfs_buf *bp)
1510{
Brian Foster6af88cd2018-07-11 22:26:35 -07001511 ASSERT(!(bp->b_flags & XBF_ASYNC));
1512
Brian Fostere339dd82018-07-11 22:26:34 -07001513 trace_xfs_buf_iowait(bp, _RET_IP_);
1514 wait_for_completion(&bp->b_iowait);
1515 trace_xfs_buf_iowait_done(bp, _RET_IP_);
1516
1517 return bp->b_error;
1518}
1519
1520/*
Dave Chinner595bff752014-10-02 09:05:14 +10001521 * Synchronous buffer IO submission path, read or write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 */
1523int
Brian Foster6af88cd2018-07-11 22:26:35 -07001524__xfs_buf_submit(
1525 struct xfs_buf *bp,
1526 bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Dave Chinner595bff752014-10-02 09:05:14 +10001528 int error;
1529
Dave Chinner595bff752014-10-02 09:05:14 +10001530 /*
Brian Foster6af88cd2018-07-11 22:26:35 -07001531 * Grab a reference so the buffer does not go away underneath us. For
1532 * async buffers, I/O completion drops the callers reference, which
1533 * could occur before submission returns.
Dave Chinner595bff752014-10-02 09:05:14 +10001534 */
1535 xfs_buf_hold(bp);
1536
Brian Foster6af88cd2018-07-11 22:26:35 -07001537 error = __xfs_buf_submit_common(bp);
1538 if (error) {
1539 if (bp->b_flags & XBF_ASYNC)
1540 xfs_buf_ioend(bp);
Brian Fostereaebb512018-07-11 22:26:34 -07001541 goto out;
Brian Foster6af88cd2018-07-11 22:26:35 -07001542 }
Dave Chinner595bff752014-10-02 09:05:14 +10001543
Brian Foster6af88cd2018-07-11 22:26:35 -07001544 if (wait)
1545 error = xfs_buf_iowait(bp);
Brian Fostereaebb512018-07-11 22:26:34 -07001546out:
Dave Chinner595bff752014-10-02 09:05:14 +10001547 /*
Brian Foster6af88cd2018-07-11 22:26:35 -07001548 * Release the hold that keeps the buffer referenced for the entire
1549 * I/O. Note that if the buffer is async, it is not safe to reference
1550 * after this release.
Dave Chinner595bff752014-10-02 09:05:14 +10001551 */
1552 xfs_buf_rele(bp);
1553 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001556void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001557xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001558 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 size_t offset)
1560{
1561 struct page *page;
1562
Dave Chinner611c9942012-04-23 15:59:07 +10001563 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001564 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Nathan Scottce8e9222006-01-11 15:39:08 +11001566 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001567 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001568 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
1571/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 * Move data into or out of a buffer.
1573 */
1574void
Nathan Scottce8e9222006-01-11 15:39:08 +11001575xfs_buf_iomove(
1576 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 size_t boff, /* starting buffer offset */
1578 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001579 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001580 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Dave Chinner795cac72012-04-23 15:58:53 +10001582 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 bend = boff + bsize;
1585 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001586 struct page *page;
1587 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Dave Chinner795cac72012-04-23 15:58:53 +10001589 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1590 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1591 page = bp->b_pages[page_index];
1592 csize = min_t(size_t, PAGE_SIZE - page_offset,
1593 BBTOB(bp->b_io_length) - boff);
1594
1595 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001598 case XBRW_ZERO:
Dave Chinner795cac72012-04-23 15:58:53 +10001599 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001601 case XBRW_READ:
Dave Chinner795cac72012-04-23 15:58:53 +10001602 memcpy(data, page_address(page) + page_offset, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001604 case XBRW_WRITE:
Dave Chinner795cac72012-04-23 15:58:53 +10001605 memcpy(page_address(page) + page_offset, data, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
1607
1608 boff += csize;
1609 data += csize;
1610 }
1611}
1612
1613/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001614 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 */
1616
1617/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001618 * Wait for any bufs with callbacks that have been submitted but have not yet
1619 * returned. These buffers will have an elevated hold count, so wait on those
1620 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001622static enum lru_status
1623xfs_buftarg_wait_rele(
1624 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001625 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001626 spinlock_t *lru_lock,
1627 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Dave Chinnere80dfa12013-08-28 10:18:05 +10001629{
1630 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001631 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001632
1633 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001634 /* need to wait, so skip it this pass */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001635 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001636 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
Dave Chinnera4082352013-08-28 10:18:06 +10001638 if (!spin_trylock(&bp->b_lock))
1639 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001640
Dave Chinnera4082352013-08-28 10:18:06 +10001641 /*
1642 * clear the LRU reference count so the buffer doesn't get
1643 * ignored in xfs_buf_rele().
1644 */
1645 atomic_set(&bp->b_lru_ref, 0);
1646 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001647 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001648 spin_unlock(&bp->b_lock);
1649 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
Dave Chinnere80dfa12013-08-28 10:18:05 +10001652void
1653xfs_wait_buftarg(
1654 struct xfs_buftarg *btp)
1655{
Dave Chinnera4082352013-08-28 10:18:06 +10001656 LIST_HEAD(dispose);
1657 int loop = 0;
1658
Dave Chinner85bec542016-01-19 08:28:10 +11001659 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001660 * First wait on the buftarg I/O count for all in-flight buffers to be
1661 * released. This is critical as new buffers do not make the LRU until
1662 * they are released.
1663 *
1664 * Next, flush the buffer workqueue to ensure all completion processing
1665 * has finished. Just waiting on buffer locks is not sufficient for
1666 * async IO as the reference count held over IO is not released until
1667 * after the buffer lock is dropped. Hence we need to ensure here that
1668 * all reference counts have been dropped before we start walking the
1669 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001670 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001671 while (percpu_counter_sum(&btp->bt_io_count))
1672 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001673 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Dave Chinner85bec542016-01-19 08:28:10 +11001674
Dave Chinnera4082352013-08-28 10:18:06 +10001675 /* loop until there is nothing left on the lru list. */
1676 while (list_lru_count(&btp->bt_lru)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001677 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001678 &dispose, LONG_MAX);
1679
1680 while (!list_empty(&dispose)) {
1681 struct xfs_buf *bp;
1682 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1683 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001684 if (bp->b_flags & XBF_WRITE_FAIL) {
1685 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001686"Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001687 (long long)bp->b_bn);
Joe Perchesf41febd2015-07-29 11:52:04 +10001688 xfs_alert(btp->bt_mount,
1689"Please run xfs_repair to determine the extent of the problem.");
Dave Chinnerac8809f2013-12-12 16:34:38 +11001690 }
Dave Chinnera4082352013-08-28 10:18:06 +10001691 xfs_buf_rele(bp);
1692 }
1693 if (loop++ != 0)
1694 delay(100);
1695 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001696}
1697
1698static enum lru_status
1699xfs_buftarg_isolate(
1700 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001701 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001702 spinlock_t *lru_lock,
1703 void *arg)
1704{
1705 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1706 struct list_head *dispose = arg;
1707
1708 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001709 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1710 * If we fail to get the lock, just skip it.
1711 */
1712 if (!spin_trylock(&bp->b_lock))
1713 return LRU_SKIP;
1714 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001715 * Decrement the b_lru_ref count unless the value is already
1716 * zero. If the value is already zero, we need to reclaim the
1717 * buffer, otherwise it gets another trip through the LRU.
1718 */
Vratislav Bendel19957a12018-03-06 17:07:44 -08001719 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
Dave Chinnera4082352013-08-28 10:18:06 +10001720 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001721 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001722 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001723
Dave Chinnera4082352013-08-28 10:18:06 +10001724 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001725 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001726 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001727 return LRU_REMOVED;
1728}
1729
Andrew Mortonaddbda42013-08-28 10:18:06 +10001730static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001731xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001732 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001733 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001734{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001735 struct xfs_buftarg *btp = container_of(shrink,
1736 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001737 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001738 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001739
Vladimir Davydov503c3582015-02-12 14:58:47 -08001740 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1741 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001742
1743 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001744 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001745 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1746 list_del_init(&bp->b_lru);
1747 xfs_buf_rele(bp);
1748 }
1749
Dave Chinnere80dfa12013-08-28 10:18:05 +10001750 return freed;
1751}
1752
Andrew Mortonaddbda42013-08-28 10:18:06 +10001753static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001754xfs_buftarg_shrink_count(
1755 struct shrinker *shrink,
1756 struct shrink_control *sc)
1757{
1758 struct xfs_buftarg *btp = container_of(shrink,
1759 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001760 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001761}
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763void
1764xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001765 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001767 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001768 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1769 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001770 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001771
Dave Chinner2291dab2016-12-09 16:49:54 +11001772 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001773
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001774 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001777int
1778xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001780 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001782 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001783 btp->bt_meta_sectorsize = sectorsize;
1784 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Nathan Scottce8e9222006-01-11 15:39:08 +11001786 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001787 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001788 "Cannot set_blocksize to %u on device %pg",
1789 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001790 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001793 /* Set up device logical sector size mask */
1794 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1795 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return 0;
1798}
1799
1800/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001801 * When allocating the initial buffer target we have not yet
1802 * read in the superblock, so don't know what sized sectors
1803 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001804 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805STATIC int
1806xfs_setsize_buftarg_early(
1807 xfs_buftarg_t *btp,
1808 struct block_device *bdev)
1809{
Eric Sandeena96c4152014-04-14 19:00:29 +10001810 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813xfs_buftarg_t *
1814xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001815 struct xfs_mount *mp,
Dan Williams486aff52017-08-24 15:12:50 -07001816 struct block_device *bdev,
1817 struct dax_device *dax_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
1819 xfs_buftarg_t *btp;
1820
Dave Chinnerb17cb362013-05-20 09:51:12 +10001821 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Dave Chinnerebad8612010-09-22 10:47:20 +10001823 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001824 btp->bt_dev = bdev->bd_dev;
1825 btp->bt_bdev = bdev;
Dan Williams486aff52017-08-24 15:12:50 -07001826 btp->bt_daxdev = dax_dev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if (xfs_setsize_buftarg_early(btp, bdev))
Michal Hockod210a982017-11-23 17:13:40 +01001829 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001830
1831 if (list_lru_init(&btp->bt_lru))
Michal Hockod210a982017-11-23 17:13:40 +01001832 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001833
Brian Foster9c7504a2016-07-20 11:15:28 +10001834 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
Michal Hockod210a982017-11-23 17:13:40 +01001835 goto error_lru;
Brian Foster9c7504a2016-07-20 11:15:28 +10001836
Dave Chinnere80dfa12013-08-28 10:18:05 +10001837 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1838 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001839 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001840 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Michal Hockod210a982017-11-23 17:13:40 +01001841 if (register_shrinker(&btp->bt_shrinker))
1842 goto error_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return btp;
1844
Michal Hockod210a982017-11-23 17:13:40 +01001845error_pcpu:
1846 percpu_counter_destroy(&btp->bt_io_count);
1847error_lru:
1848 list_lru_destroy(&btp->bt_lru);
1849error_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001850 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return NULL;
1852}
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854/*
Brian Foster20e8a062017-04-21 12:40:44 -07001855 * Cancel a delayed write list.
1856 *
1857 * Remove each buffer from the list, clear the delwri queue flag and drop the
1858 * associated buffer reference.
1859 */
1860void
1861xfs_buf_delwri_cancel(
1862 struct list_head *list)
1863{
1864 struct xfs_buf *bp;
1865
1866 while (!list_empty(list)) {
1867 bp = list_first_entry(list, struct xfs_buf, b_list);
1868
1869 xfs_buf_lock(bp);
1870 bp->b_flags &= ~_XBF_DELWRI_Q;
1871 list_del_init(&bp->b_list);
1872 xfs_buf_relse(bp);
1873 }
1874}
1875
1876/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001877 * Add a buffer to the delayed write list.
1878 *
1879 * This queues a buffer for writeout if it hasn't already been. Note that
1880 * neither this routine nor the buffer list submission functions perform
1881 * any internal synchronization. It is expected that the lists are thread-local
1882 * to the callers.
1883 *
1884 * Returns true if we queued up the buffer, or false if it already had
1885 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001887bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001888xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001889 struct xfs_buf *bp,
1890 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001892 ASSERT(xfs_buf_islocked(bp));
1893 ASSERT(!(bp->b_flags & XBF_READ));
1894
1895 /*
1896 * If the buffer is already marked delwri it already is queued up
1897 * by someone else for imediate writeout. Just ignore it in that
1898 * case.
1899 */
1900 if (bp->b_flags & _XBF_DELWRI_Q) {
1901 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1902 return false;
1903 }
David Chinnera6867a62006-01-11 15:37:58 +11001904
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001905 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1906
Dave Chinnerd808f612010-02-02 10:13:42 +11001907 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001908 * If a buffer gets written out synchronously or marked stale while it
1909 * is on a delwri list we lazily remove it. To do this, the other party
1910 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1911 * It remains referenced and on the list. In a rare corner case it
1912 * might get readded to a delwri list after the synchronous writeout, in
1913 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001914 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001915 bp->b_flags |= _XBF_DELWRI_Q;
1916 if (list_empty(&bp->b_list)) {
1917 atomic_inc(&bp->b_hold);
1918 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001919 }
David Chinner585e6d82007-02-10 18:32:29 +11001920
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001921 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001922}
1923
Dave Chinner089716a2010-01-26 15:13:25 +11001924/*
1925 * Compare function is more complex than it needs to be because
1926 * the return value is only 32 bits and we are doing comparisons
1927 * on 64 bit values
1928 */
1929static int
1930xfs_buf_cmp(
1931 void *priv,
1932 struct list_head *a,
1933 struct list_head *b)
1934{
1935 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1936 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1937 xfs_daddr_t diff;
1938
Mark Tinguelyf4b42422012-12-04 17:18:02 -06001939 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11001940 if (diff < 0)
1941 return -1;
1942 if (diff > 0)
1943 return 1;
1944 return 0;
1945}
1946
Dave Chinner26f1fe82016-06-01 17:38:15 +10001947/*
Brian Fostere339dd82018-07-11 22:26:34 -07001948 * Submit buffers for write. If wait_list is specified, the buffers are
1949 * submitted using sync I/O and placed on the wait list such that the caller can
1950 * iowait each buffer. Otherwise async I/O is used and the buffers are released
1951 * at I/O completion time. In either case, buffers remain locked until I/O
1952 * completes and the buffer is released from the queue.
Dave Chinner26f1fe82016-06-01 17:38:15 +10001953 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001954static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10001955xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001956 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10001957 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001959 struct xfs_buf *bp, *n;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001960 LIST_HEAD (submit_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001961 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001962 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Dave Chinner26f1fe82016-06-01 17:38:15 +10001964 list_sort(NULL, buffer_list, xfs_buf_cmp);
1965
1966 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001967 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10001968 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001969 if (xfs_buf_ispinned(bp)) {
1970 pinned++;
1971 continue;
1972 }
1973 if (!xfs_buf_trylock(bp))
1974 continue;
1975 } else {
1976 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001979 /*
1980 * Someone else might have written the buffer synchronously or
1981 * marked it stale in the meantime. In that case only the
1982 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1983 * reference and remove it from the list here.
1984 */
1985 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1986 list_del_init(&bp->b_list);
1987 xfs_buf_relse(bp);
1988 continue;
1989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001991 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001992
Dave Chinnercf53e992014-10-02 09:04:01 +10001993 /*
Brian Fostere339dd82018-07-11 22:26:34 -07001994 * If we have a wait list, each buffer (and associated delwri
1995 * queue reference) transfers to it and is submitted
1996 * synchronously. Otherwise, drop the buffer from the delwri
1997 * queue and submit async.
Dave Chinnercf53e992014-10-02 09:04:01 +10001998 */
Dave Chinnerbbfeb612016-07-20 11:53:35 +10001999 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL);
Brian Fostere339dd82018-07-11 22:26:34 -07002000 bp->b_flags |= XBF_WRITE;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002001 if (wait_list) {
Brian Fostere339dd82018-07-11 22:26:34 -07002002 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002003 list_move_tail(&bp->b_list, wait_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002004 } else {
2005 bp->b_flags |= XBF_ASYNC;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002006 list_del_init(&bp->b_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002007 }
Brian Foster6af88cd2018-07-11 22:26:35 -07002008 __xfs_buf_submit(bp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00002010 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002012 return pinned;
2013}
Nathan Scottf07c2252006-09-28 10:52:15 +10002014
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002015/*
2016 * Write out a buffer list asynchronously.
2017 *
2018 * This will take the @buffer_list, write all non-locked and non-pinned buffers
2019 * out and not wait for I/O completion on any of the buffers. This interface
2020 * is only safely useable for callers that can track I/O completion by higher
2021 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
2022 * function.
2023 */
2024int
2025xfs_buf_delwri_submit_nowait(
2026 struct list_head *buffer_list)
2027{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002028 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002029}
2030
2031/*
2032 * Write out a buffer list synchronously.
2033 *
2034 * This will take the @buffer_list, write all buffers out and wait for I/O
2035 * completion on all of the buffers. @buffer_list is consumed by the function,
2036 * so callers must have some other way of tracking buffers if they require such
2037 * functionality.
2038 */
2039int
2040xfs_buf_delwri_submit(
2041 struct list_head *buffer_list)
2042{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002043 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002044 int error = 0, error2;
2045 struct xfs_buf *bp;
2046
Dave Chinner26f1fe82016-06-01 17:38:15 +10002047 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002048
2049 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10002050 while (!list_empty(&wait_list)) {
2051 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002052
2053 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10002054
Brian Fostere339dd82018-07-11 22:26:34 -07002055 /*
2056 * Wait on the locked buffer, check for errors and unlock and
2057 * release the delwri queue reference.
2058 */
2059 error2 = xfs_buf_iowait(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002060 xfs_buf_relse(bp);
2061 if (!error)
2062 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002065 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
Brian Foster7912e7f2017-06-14 21:21:45 -07002068/*
2069 * Push a single buffer on a delwri queue.
2070 *
2071 * The purpose of this function is to submit a single buffer of a delwri queue
2072 * and return with the buffer still on the original queue. The waiting delwri
2073 * buffer submission infrastructure guarantees transfer of the delwri queue
2074 * buffer reference to a temporary wait list. We reuse this infrastructure to
2075 * transfer the buffer back to the original queue.
2076 *
2077 * Note the buffer transitions from the queued state, to the submitted and wait
2078 * listed state and back to the queued state during this call. The buffer
2079 * locking and queue management logic between _delwri_pushbuf() and
2080 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2081 * before returning.
2082 */
2083int
2084xfs_buf_delwri_pushbuf(
2085 struct xfs_buf *bp,
2086 struct list_head *buffer_list)
2087{
2088 LIST_HEAD (submit_list);
2089 int error;
2090
2091 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2092
2093 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2094
2095 /*
2096 * Isolate the buffer to a new local list so we can submit it for I/O
2097 * independently from the rest of the original list.
2098 */
2099 xfs_buf_lock(bp);
2100 list_move(&bp->b_list, &submit_list);
2101 xfs_buf_unlock(bp);
2102
2103 /*
2104 * Delwri submission clears the DELWRI_Q buffer flag and returns with
Brian Fostere339dd82018-07-11 22:26:34 -07002105 * the buffer on the wait list with the original reference. Rather than
Brian Foster7912e7f2017-06-14 21:21:45 -07002106 * bounce the buffer from a local wait list back to the original list
2107 * after I/O completion, reuse the original list as the wait list.
2108 */
2109 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2110
2111 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002112 * The buffer is now locked, under I/O and wait listed on the original
2113 * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2114 * return with the buffer unlocked and on the original queue.
Brian Foster7912e7f2017-06-14 21:21:45 -07002115 */
Brian Fostere339dd82018-07-11 22:26:34 -07002116 error = xfs_buf_iowait(bp);
Brian Foster7912e7f2017-06-14 21:21:45 -07002117 bp->b_flags |= _XBF_DELWRI_Q;
2118 xfs_buf_unlock(bp);
2119
2120 return error;
2121}
2122
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002123int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002124xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Nathan Scott87582802006-03-14 13:18:19 +11002126 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
2127 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002128 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002129 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002130
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002133 out:
Nathan Scott87582802006-03-14 13:18:19 +11002134 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137void
Nathan Scottce8e9222006-01-11 15:39:08 +11002138xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
Nathan Scottce8e9222006-01-11 15:39:08 +11002140 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
Brian Foster7561d272017-10-17 14:16:29 -07002142
2143void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
2144{
Brian Foster7561d272017-10-17 14:16:29 -07002145 /*
2146 * Set the lru reference count to 0 based on the error injection tag.
2147 * This allows userspace to disrupt buffer caching for debug/testing
2148 * purposes.
2149 */
Brian Foster4eadcf92017-10-27 09:20:28 -07002150 if (XFS_TEST_ERROR(false, bp->b_target->bt_mount,
2151 XFS_ERRTAG_BUF_LRU_REF))
Brian Foster7561d272017-10-17 14:16:29 -07002152 lru_ref = 0;
2153
2154 atomic_set(&bp->b_lru_ref, lru_ref);
2155}