blob: b66c99329b7acfdc565b960ed10627baa138538f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scottf07c2252006-09-28 10:52:15 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Vlad Apostolov93c189c2006-11-11 18:03:49 +110018#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/stddef.h>
20#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
23#include <linux/init.h>
24#include <linux/vmalloc.h>
25#include <linux/bio.h>
26#include <linux/sysctl.h>
27#include <linux/proc_fs.h>
28#include <linux/workqueue.h>
29#include <linux/percpu.h>
30#include <linux/blkdev.h>
31#include <linux/hash.h>
Christoph Hellwig4df08c52005-09-05 08:34:18 +100032#include <linux/kthread.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080033#include <linux/migrate.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070034#include <linux/backing-dev.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Christoph Hellwig4fb6e8a2014-11-28 14:25:04 +110037#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110038#include "xfs_log_format.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100039#include "xfs_trans_resv.h"
Dave Chinner239880e2013-10-23 10:50:10 +110040#include "xfs_sb.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050041#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110043#include "xfs_log.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050044
David Chinner7989cb82007-02-10 18:34:56 +110045static kmem_zone_t *xfs_buf_zone;
Christoph Hellwig23ea4032005-06-21 15:14:01 +100046
Nathan Scottce8e9222006-01-11 15:39:08 +110047#ifdef XFS_BUF_LOCK_TRACKING
48# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
49# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
50# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#else
Nathan Scottce8e9222006-01-11 15:39:08 +110052# define XB_SET_OWNER(bp) do { } while (0)
53# define XB_CLEAR_OWNER(bp) do { } while (0)
54# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#endif
56
Nathan Scottce8e9222006-01-11 15:39:08 +110057#define xb_to_gfp(flags) \
Dave Chinneraa5c1582012-04-23 15:58:56 +100058 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Dave Chinnerb9c89712018-10-18 17:21:29 +110060/*
61 * Locking orders
62 *
63 * xfs_buf_ioacct_inc:
64 * xfs_buf_ioacct_dec:
65 * b_sema (caller holds)
66 * b_lock
67 *
68 * xfs_buf_stale:
69 * b_sema (caller holds)
70 * b_lock
71 * lru_lock
72 *
73 * xfs_buf_rele:
74 * b_lock
75 * pag_buf_lock
76 * lru_lock
77 *
78 * xfs_buftarg_wait_rele
79 * lru_lock
80 * b_lock (trylock due to inversion)
81 *
82 * xfs_buftarg_isolate
83 * lru_lock
84 * b_lock (trylock due to inversion)
85 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
James Bottomley73c77e22010-01-25 11:42:24 -060087static inline int
88xfs_buf_is_vmapped(
89 struct xfs_buf *bp)
90{
91 /*
92 * Return true if the buffer is vmapped.
93 *
Dave Chinner611c9942012-04-23 15:59:07 +100094 * b_addr is null if the buffer is not mapped, but the code is clever
95 * enough to know it doesn't have to map a single page, so the check has
96 * to be both for b_addr and bp->b_page_count > 1.
James Bottomley73c77e22010-01-25 11:42:24 -060097 */
Dave Chinner611c9942012-04-23 15:59:07 +100098 return bp->b_addr && bp->b_page_count > 1;
James Bottomley73c77e22010-01-25 11:42:24 -060099}
100
101static inline int
102xfs_buf_vmap_len(
103 struct xfs_buf *bp)
104{
105 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
Brian Foster9c7504a2016-07-20 11:15:28 +1000109 * Bump the I/O in flight count on the buftarg if we haven't yet done so for
110 * this buffer. The count is incremented once per buffer (per hold cycle)
111 * because the corresponding decrement is deferred to buffer release. Buffers
112 * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
113 * tracking adds unnecessary overhead. This is used for sychronization purposes
114 * with unmount (see xfs_wait_buftarg()), so all we really need is a count of
115 * in-flight buffers.
116 *
117 * Buffers that are never released (e.g., superblock, iclog buffers) must set
118 * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
119 * never reaches zero and unmount hangs indefinitely.
120 */
121static inline void
122xfs_buf_ioacct_inc(
123 struct xfs_buf *bp)
124{
Brian Foster9c795ff2017-05-31 08:22:52 -0700125 if (bp->b_flags & XBF_NO_IOACCT)
Brian Foster9c7504a2016-07-20 11:15:28 +1000126 return;
127
128 ASSERT(bp->b_flags & XBF_ASYNC);
Brian Foster9c795ff2017-05-31 08:22:52 -0700129 spin_lock(&bp->b_lock);
130 if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) {
131 bp->b_state |= XFS_BSTATE_IN_FLIGHT;
132 percpu_counter_inc(&bp->b_target->bt_io_count);
133 }
134 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000135}
136
137/*
138 * Clear the in-flight state on a buffer about to be released to the LRU or
139 * freed and unaccount from the buftarg.
140 */
141static inline void
Brian Foster9c795ff2017-05-31 08:22:52 -0700142__xfs_buf_ioacct_dec(
143 struct xfs_buf *bp)
144{
Brian Foster85ab1b22017-06-08 08:23:07 -0700145 lockdep_assert_held(&bp->b_lock);
Brian Foster9c795ff2017-05-31 08:22:52 -0700146
147 if (bp->b_state & XFS_BSTATE_IN_FLIGHT) {
148 bp->b_state &= ~XFS_BSTATE_IN_FLIGHT;
149 percpu_counter_dec(&bp->b_target->bt_io_count);
150 }
151}
152
153static inline void
Brian Foster9c7504a2016-07-20 11:15:28 +1000154xfs_buf_ioacct_dec(
155 struct xfs_buf *bp)
156{
Brian Foster9c795ff2017-05-31 08:22:52 -0700157 spin_lock(&bp->b_lock);
158 __xfs_buf_ioacct_dec(bp);
159 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000160}
161
162/*
Dave Chinner430cbeb2010-12-02 16:30:55 +1100163 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
164 * b_lru_ref count so that the buffer is freed immediately when the buffer
165 * reference count falls to zero. If the buffer is already on the LRU, we need
166 * to remove the reference that LRU holds on the buffer.
167 *
168 * This prevents build-up of stale buffers on the LRU.
169 */
170void
171xfs_buf_stale(
172 struct xfs_buf *bp)
173{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000174 ASSERT(xfs_buf_islocked(bp));
175
Dave Chinner430cbeb2010-12-02 16:30:55 +1100176 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000177
178 /*
179 * Clear the delwri status so that a delwri queue walker will not
180 * flush this buffer to disk now that it is stale. The delwri queue has
181 * a reference to the buffer, so this is safe to do.
182 */
183 bp->b_flags &= ~_XBF_DELWRI_Q;
184
Brian Foster9c7504a2016-07-20 11:15:28 +1000185 /*
186 * Once the buffer is marked stale and unlocked, a subsequent lookup
187 * could reset b_flags. There is no guarantee that the buffer is
188 * unaccounted (released to LRU) before that occurs. Drop in-flight
189 * status now to preserve accounting consistency.
190 */
Dave Chinnera4082352013-08-28 10:18:06 +1000191 spin_lock(&bp->b_lock);
Brian Foster9c795ff2017-05-31 08:22:52 -0700192 __xfs_buf_ioacct_dec(bp);
193
Dave Chinnera4082352013-08-28 10:18:06 +1000194 atomic_set(&bp->b_lru_ref, 0);
195 if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
Dave Chinnere80dfa12013-08-28 10:18:05 +1000196 (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
197 atomic_dec(&bp->b_hold);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100198
Dave Chinner430cbeb2010-12-02 16:30:55 +1100199 ASSERT(atomic_read(&bp->b_hold) >= 1);
Dave Chinnera4082352013-08-28 10:18:06 +1000200 spin_unlock(&bp->b_lock);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100201}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Dave Chinner3e85c862012-06-22 18:50:09 +1000203static int
204xfs_buf_get_maps(
205 struct xfs_buf *bp,
206 int map_count)
207{
208 ASSERT(bp->b_maps == NULL);
209 bp->b_map_count = map_count;
210
211 if (map_count == 1) {
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600212 bp->b_maps = &bp->__b_map;
Dave Chinner3e85c862012-06-22 18:50:09 +1000213 return 0;
214 }
215
216 bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
217 KM_NOFS);
218 if (!bp->b_maps)
Dave Chinner24513372014-06-25 14:58:08 +1000219 return -ENOMEM;
Dave Chinner3e85c862012-06-22 18:50:09 +1000220 return 0;
221}
222
223/*
224 * Frees b_pages if it was allocated.
225 */
226static void
227xfs_buf_free_maps(
228 struct xfs_buf *bp)
229{
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600230 if (bp->b_maps != &bp->__b_map) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000231 kmem_free(bp->b_maps);
232 bp->b_maps = NULL;
233 }
234}
235
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000236struct xfs_buf *
Dave Chinner3e85c862012-06-22 18:50:09 +1000237_xfs_buf_alloc(
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000238 struct xfs_buftarg *target,
Dave Chinner3e85c862012-06-22 18:50:09 +1000239 struct xfs_buf_map *map,
240 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100241 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000243 struct xfs_buf *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000244 int error;
245 int i;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000246
Dave Chinneraa5c1582012-04-23 15:58:56 +1000247 bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000248 if (unlikely(!bp))
249 return NULL;
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /*
Dave Chinner12bcb3f2012-04-23 15:59:05 +1000252 * We don't want certain flags to appear in b_flags unless they are
253 * specifically set by later operations on the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
Dave Chinner611c9942012-04-23 15:59:07 +1000255 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Nathan Scottce8e9222006-01-11 15:39:08 +1100257 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100258 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000259 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100260 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100261 INIT_LIST_HEAD(&bp->b_list);
Dave Chinner74f75a02010-09-24 19:59:04 +1000262 RB_CLEAR_NODE(&bp->b_rbnode);
Thomas Gleixnera731cd12010-09-07 14:33:15 +0000263 sema_init(&bp->b_sema, 0); /* held, no waiters */
Dave Chinnera4082352013-08-28 10:18:06 +1000264 spin_lock_init(&bp->b_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100265 XB_SET_OWNER(bp);
266 bp->b_target = target;
Dave Chinner3e85c862012-06-22 18:50:09 +1000267 bp->b_flags = flags;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000270 * Set length and io_length to the same value initially.
271 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * most cases but may be reset (e.g. XFS recovery).
273 */
Dave Chinner3e85c862012-06-22 18:50:09 +1000274 error = xfs_buf_get_maps(bp, nmaps);
275 if (error) {
276 kmem_zone_free(xfs_buf_zone, bp);
277 return NULL;
278 }
279
280 bp->b_bn = map[0].bm_bn;
281 bp->b_length = 0;
282 for (i = 0; i < nmaps; i++) {
283 bp->b_maps[i].bm_bn = map[i].bm_bn;
284 bp->b_maps[i].bm_len = map[i].bm_len;
285 bp->b_length += map[i].bm_len;
286 }
287 bp->b_io_length = bp->b_length;
288
Nathan Scottce8e9222006-01-11 15:39:08 +1100289 atomic_set(&bp->b_pin_count, 0);
290 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100292 XFS_STATS_INC(target->bt_mount, xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000293 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000294
295 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100299 * Allocate a page array capable of holding a specified number
300 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 */
302STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100303_xfs_buf_get_pages(
304 xfs_buf_t *bp,
Eric Sandeen87937bf2014-04-14 19:01:20 +1000305 int page_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100308 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100309 bp->b_page_count = page_count;
310 if (page_count <= XB_PAGES) {
311 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100313 bp->b_pages = kmem_alloc(sizeof(struct page *) *
Dave Chinneraa5c1582012-04-23 15:58:56 +1000314 page_count, KM_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +1100315 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return -ENOMEM;
317 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100318 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320 return 0;
321}
322
323/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100324 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
326STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100327_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 xfs_buf_t *bp)
329{
Nathan Scottce8e9222006-01-11 15:39:08 +1100330 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000331 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000332 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334}
335
336/*
337 * Releases the specified buffer.
338 *
339 * The modification state of any associated pages is left unchanged.
Zhi Yong Wub46fe822013-08-07 10:10:59 +0000340 * The buffer must not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * hashed and refcounted buffers
342 */
343void
Nathan Scottce8e9222006-01-11 15:39:08 +1100344xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 xfs_buf_t *bp)
346{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000347 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Dave Chinner430cbeb2010-12-02 16:30:55 +1100349 ASSERT(list_empty(&bp->b_lru));
350
Dave Chinner0e6e8472011-03-26 09:16:45 +1100351 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 uint i;
353
James Bottomley73c77e22010-01-25 11:42:24 -0600354 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000355 vm_unmap_ram(bp->b_addr - bp->b_offset,
356 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Nathan Scott948ecdb2006-09-28 11:03:13 +1000358 for (i = 0; i < bp->b_page_count; i++) {
359 struct page *page = bp->b_pages[i];
360
Dave Chinner0e6e8472011-03-26 09:16:45 +1100361 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000362 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100363 } else if (bp->b_flags & _XBF_KMEM)
364 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000365 _xfs_buf_free_pages(bp);
Dave Chinner3e85c862012-06-22 18:50:09 +1000366 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000367 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100371 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
373STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100374xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 xfs_buf_t *bp,
376 uint flags)
377{
Dave Chinneraa0e8832012-04-23 15:58:52 +1000378 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100380 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 unsigned short page_count, i;
Dave Chinner795cac72012-04-23 15:58:53 +1000382 xfs_off_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int error;
384
Dave Chinner0e6e8472011-03-26 09:16:45 +1100385 /*
386 * for buffers that are contained within a single page, just allocate
387 * the memory from the heap - there's no need for the complexity of
388 * page arrays to keep allocation down to order 0.
389 */
Dave Chinner795cac72012-04-23 15:58:53 +1000390 size = BBTOB(bp->b_length);
391 if (size < PAGE_SIZE) {
Dave Chinneraa5c1582012-04-23 15:58:56 +1000392 bp->b_addr = kmem_alloc(size, KM_NOFS);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100393 if (!bp->b_addr) {
394 /* low memory - use alloc_page loop instead */
395 goto use_alloc_page;
396 }
397
Dave Chinner795cac72012-04-23 15:58:53 +1000398 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100399 ((unsigned long)bp->b_addr & PAGE_MASK)) {
400 /* b_addr spans two pages - use alloc_page instead */
401 kmem_free(bp->b_addr);
402 bp->b_addr = NULL;
403 goto use_alloc_page;
404 }
405 bp->b_offset = offset_in_page(bp->b_addr);
406 bp->b_pages = bp->b_page_array;
407 bp->b_pages[0] = virt_to_page(bp->b_addr);
408 bp->b_page_count = 1;
Dave Chinner611c9942012-04-23 15:59:07 +1000409 bp->b_flags |= _XBF_KMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100410 return 0;
411 }
412
413use_alloc_page:
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600414 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
415 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
Dave Chinnercbb7baab2012-06-22 18:50:08 +1000416 >> PAGE_SHIFT;
Dave Chinner795cac72012-04-23 15:58:53 +1000417 page_count = end - start;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000418 error = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (unlikely(error))
420 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Nathan Scottce8e9222006-01-11 15:39:08 +1100422 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100423 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Nathan Scottce8e9222006-01-11 15:39:08 +1100425 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 struct page *page;
427 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100428retry:
429 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100431 if (flags & XBF_READ_AHEAD) {
432 bp->b_page_count = i;
Dave Chinner24513372014-06-25 14:58:08 +1000433 error = -ENOMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100434 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
437 /*
438 * This could deadlock.
439 *
440 * But until all the XFS lowlevel code is revamped to
441 * handle buffer allocation failures we can't do much.
442 */
443 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100444 xfs_err(NULL,
Tetsuo Handa5bf97b12015-10-12 15:41:29 +1100445 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
446 current->comm, current->pid,
Harvey Harrison34a622b2008-04-10 12:19:21 +1000447 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100449 XFS_STATS_INC(bp->b_target->bt_mount, xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200450 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 goto retry;
452 }
453
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100454 XFS_STATS_INC(bp->b_target->bt_mount, xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dave Chinner0e6e8472011-03-26 09:16:45 +1100456 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100458 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 offset = 0;
460 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Dave Chinner0e6e8472011-03-26 09:16:45 +1100463out_free_pages:
464 for (i = 0; i < bp->b_page_count; i++)
465 __free_page(bp->b_pages[i]);
Darrick J. Wong5d44dd52017-02-02 08:56:10 +0100466 bp->b_flags &= ~_XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return error;
468}
469
470/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300471 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
473STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100474_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 xfs_buf_t *bp,
476 uint flags)
477{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100478 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100479 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100480 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100481 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
Dave Chinner611c9942012-04-23 15:59:07 +1000482 } else if (flags & XBF_UNMAPPED) {
483 bp->b_addr = NULL;
484 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100485 int retried = 0;
Dave Chinnerae687e52014-03-07 16:19:14 +1100486 unsigned noio_flag;
Dave Chinnera19fb382011-03-26 09:13:42 +1100487
Dave Chinnerae687e52014-03-07 16:19:14 +1100488 /*
489 * vm_map_ram() will allocate auxillary structures (e.g.
490 * pagetables) with GFP_KERNEL, yet we are likely to be under
491 * GFP_NOFS context here. Hence we need to tell memory reclaim
492 * that we are in such a context via PF_MEMALLOC_NOIO to prevent
493 * memory reclaim re-entering the filesystem here and
494 * potentially deadlocking.
495 */
496 noio_flag = memalloc_noio_save();
Dave Chinnera19fb382011-03-26 09:13:42 +1100497 do {
498 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
499 -1, PAGE_KERNEL);
500 if (bp->b_addr)
501 break;
502 vm_unmap_aliases();
503 } while (retried++ <= 1);
Dave Chinnerae687e52014-03-07 16:19:14 +1100504 memalloc_noio_restore(noio_flag);
Dave Chinnera19fb382011-03-26 09:13:42 +1100505
506 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100508 bp->b_addr += bp->b_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
511 return 0;
512}
513
514/*
515 * Finding and Reading Buffers
516 */
517
518/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100519 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * a given range of an inode. The buffer is returned
Chandra Seetharamaneabbaf12011-09-08 20:18:50 +0000521 * locked. No I/O is implied by this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 */
523xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100524_xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000525 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000526 struct xfs_buf_map *map,
527 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100528 xfs_buf_flags_t flags,
529 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Dave Chinner74f75a02010-09-24 19:59:04 +1000531 struct xfs_perag *pag;
532 struct rb_node **rbp;
533 struct rb_node *parent;
534 xfs_buf_t *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000535 xfs_daddr_t blkno = map[0].bm_bn;
Dave Chinner10616b82013-01-21 23:53:52 +1100536 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000537 int numblks = 0;
538 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Dave Chinner3e85c862012-06-22 18:50:09 +1000540 for (i = 0; i < nmaps; i++)
541 numblks += map[i].bm_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* Check for IOs smaller than the sector size / not sector aligned */
Dave Chinnerf79af0b2015-08-25 10:05:13 +1000544 ASSERT(!(BBTOB(numblks) < btp->bt_meta_sectorsize));
Eric Sandeen6da54172014-01-21 16:45:52 -0600545 ASSERT(!(BBTOB(blkno) & (xfs_off_t)btp->bt_meta_sectormask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Dave Chinner10616b82013-01-21 23:53:52 +1100547 /*
548 * Corrupted block numbers can get through to here, unfortunately, so we
549 * have to check that the buffer falls within the filesystem bounds.
550 */
551 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
Eric Sandeendb52d092014-11-28 14:03:55 +1100552 if (blkno < 0 || blkno >= eofs) {
Dave Chinner10616b82013-01-21 23:53:52 +1100553 /*
Dave Chinner24513372014-06-25 14:58:08 +1000554 * XXX (dgc): we should really be returning -EFSCORRUPTED here,
Dave Chinner10616b82013-01-21 23:53:52 +1100555 * but none of the higher level infrastructure supports
556 * returning a specific error on buffer lookup failures.
557 */
558 xfs_alert(btp->bt_mount,
559 "%s: Block out of range: block 0x%llx, EOFS 0x%llx ",
560 __func__, blkno, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000561 WARN_ON(1);
Dave Chinner10616b82013-01-21 23:53:52 +1100562 return NULL;
563 }
564
Dave Chinner74f75a02010-09-24 19:59:04 +1000565 /* get tree root */
566 pag = xfs_perag_get(btp->bt_mount,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000567 xfs_daddr_to_agno(btp->bt_mount, blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Dave Chinner74f75a02010-09-24 19:59:04 +1000569 /* walk tree */
570 spin_lock(&pag->pag_buf_lock);
571 rbp = &pag->pag_buf_tree.rb_node;
572 parent = NULL;
573 bp = NULL;
574 while (*rbp) {
575 parent = *rbp;
576 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000578 if (blkno < bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000579 rbp = &(*rbp)->rb_left;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000580 else if (blkno > bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000581 rbp = &(*rbp)->rb_right;
582 else {
583 /*
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000584 * found a block number match. If the range doesn't
Dave Chinner74f75a02010-09-24 19:59:04 +1000585 * match, the only way this is allowed is if the buffer
586 * in the cache is stale and the transaction that made
587 * it stale has not yet committed. i.e. we are
588 * reallocating a busy extent. Skip this buffer and
589 * continue searching to the right for an exact match.
590 */
Dave Chinner4e94b712012-04-23 15:58:51 +1000591 if (bp->b_length != numblks) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000592 ASSERT(bp->b_flags & XBF_STALE);
593 rbp = &(*rbp)->rb_right;
594 continue;
595 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100596 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 goto found;
598 }
599 }
600
601 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100602 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000603 rb_link_node(&new_bp->b_rbnode, parent, rbp);
604 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
605 /* the buffer keeps the perag reference until it is freed */
606 new_bp->b_pag = pag;
607 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 } else {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100609 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000610 spin_unlock(&pag->pag_buf_lock);
611 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100613 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000616 spin_unlock(&pag->pag_buf_lock);
617 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200619 if (!xfs_buf_trylock(bp)) {
620 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100621 xfs_buf_rele(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100622 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
Nathan Scottce8e9222006-01-11 15:39:08 +1100623 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200625 xfs_buf_lock(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100626 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
Dave Chinner0e6e8472011-03-26 09:16:45 +1100629 /*
630 * if the buffer is stale, clear all the external state associated with
631 * it. We need to keep flags such as how we allocated the buffer memory
632 * intact here.
633 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100634 if (bp->b_flags & XBF_STALE) {
635 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinnercfb02852012-11-12 22:54:19 +1100636 ASSERT(bp->b_iodone == NULL);
Dave Chinner611c9942012-04-23 15:59:07 +1000637 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100638 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000639 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000640
641 trace_xfs_buf_find(bp, flags, _RET_IP_);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100642 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
Nathan Scottce8e9222006-01-11 15:39:08 +1100643 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646/*
Dave Chinner38158322011-09-30 04:45:02 +0000647 * Assembles a buffer covering the specified range. The code is optimised for
648 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
649 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
Dave Chinner38158322011-09-30 04:45:02 +0000651struct xfs_buf *
Dave Chinner6dde2702012-06-22 18:50:10 +1000652xfs_buf_get_map(
653 struct xfs_buftarg *target,
654 struct xfs_buf_map *map,
655 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100656 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Dave Chinner38158322011-09-30 04:45:02 +0000658 struct xfs_buf *bp;
659 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100660 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Dave Chinner6dde2702012-06-22 18:50:10 +1000662 bp = _xfs_buf_find(target, map, nmaps, flags, NULL);
Dave Chinner38158322011-09-30 04:45:02 +0000663 if (likely(bp))
664 goto found;
665
Dave Chinner6dde2702012-06-22 18:50:10 +1000666 new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100667 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return NULL;
669
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000670 error = xfs_buf_allocate_memory(new_bp, flags);
671 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000672 xfs_buf_free(new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000673 return NULL;
674 }
675
Dave Chinner6dde2702012-06-22 18:50:10 +1000676 bp = _xfs_buf_find(target, map, nmaps, flags, new_bp);
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000677 if (!bp) {
678 xfs_buf_free(new_bp);
679 return NULL;
680 }
681
682 if (bp != new_bp)
683 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Dave Chinner38158322011-09-30 04:45:02 +0000685found:
Dave Chinner611c9942012-04-23 15:59:07 +1000686 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100687 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100689 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500690 "%s: failed to map pagesn", __func__);
Dave Chinnera8acad72012-04-23 15:58:54 +1000691 xfs_buf_relse(bp);
692 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 }
695
Dave Chinnerb79f4a12016-01-12 07:03:44 +1100696 /*
697 * Clear b_error if this is a lookup from a caller that doesn't expect
698 * valid data to be found in the buffer.
699 */
700 if (!(flags & XBF_READ))
701 xfs_buf_ioerror(bp, 0);
702
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100703 XFS_STATS_INC(target->bt_mount, xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000704 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100705 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100708STATIC int
709_xfs_buf_read(
710 xfs_buf_t *bp,
711 xfs_buf_flags_t flags)
712{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000713 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600714 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100715
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000716 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200717 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100718
Dave Chinner595bff72014-10-02 09:05:14 +1000719 if (flags & XBF_ASYNC) {
720 xfs_buf_submit(bp);
Dave Chinner0e95f192012-04-23 15:58:46 +1000721 return 0;
Dave Chinner595bff72014-10-02 09:05:14 +1000722 }
723 return xfs_buf_submit_wait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100724}
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726xfs_buf_t *
Dave Chinner6dde2702012-06-22 18:50:10 +1000727xfs_buf_read_map(
728 struct xfs_buftarg *target,
729 struct xfs_buf_map *map,
730 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100731 xfs_buf_flags_t flags,
Dave Chinner1813dd62012-11-14 17:54:40 +1100732 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Dave Chinner6dde2702012-06-22 18:50:10 +1000734 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Nathan Scottce8e9222006-01-11 15:39:08 +1100736 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Dave Chinner6dde2702012-06-22 18:50:10 +1000738 bp = xfs_buf_get_map(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100739 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000740 trace_xfs_buf_read(bp, flags, _RET_IP_);
741
Dave Chinnerb0388bf2016-02-10 15:01:11 +1100742 if (!(bp->b_flags & XBF_DONE)) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100743 XFS_STATS_INC(target->bt_mount, xb_get_read);
Dave Chinner1813dd62012-11-14 17:54:40 +1100744 bp->b_ops = ops;
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100745 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100746 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /*
748 * Read ahead call which is already satisfied,
749 * drop the buffer
750 */
Dave Chinnera8acad72012-04-23 15:58:54 +1000751 xfs_buf_relse(bp);
752 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100755 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757 }
758
Nathan Scottce8e9222006-01-11 15:39:08 +1100759 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100763 * If we are not low on memory then do the readahead in a deadlock
764 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 */
766void
Dave Chinner6dde2702012-06-22 18:50:10 +1000767xfs_buf_readahead_map(
768 struct xfs_buftarg *target,
769 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100770 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100771 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Jan Karab8439982017-02-02 15:56:53 +0100773 if (bdi_read_congested(target->bt_bdev->bd_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return;
775
Dave Chinner6dde2702012-06-22 18:50:10 +1000776 xfs_buf_read_map(target, map, nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100777 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Dave Chinner5adc94c2010-09-24 21:58:31 +1000780/*
781 * Read an uncached buffer from disk. Allocates and returns a locked
782 * buffer containing the disk contents or nothing.
783 */
Dave Chinnerba372672014-10-02 09:05:32 +1000784int
Dave Chinner5adc94c2010-09-24 21:58:31 +1000785xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000786 struct xfs_buftarg *target,
787 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000788 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100789 int flags,
Dave Chinnerba372672014-10-02 09:05:32 +1000790 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100791 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000792{
Dave Chinnereab4e632012-11-12 22:54:02 +1100793 struct xfs_buf *bp;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000794
Dave Chinnerba372672014-10-02 09:05:32 +1000795 *bpp = NULL;
796
Dave Chinnere70b73f2012-04-23 15:58:49 +1000797 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000798 if (!bp)
Dave Chinnerba372672014-10-02 09:05:32 +1000799 return -ENOMEM;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000800
801 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000802 ASSERT(bp->b_map_count == 1);
Dave Chinnerba372672014-10-02 09:05:32 +1000803 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
Dave Chinner3e85c862012-06-22 18:50:09 +1000804 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baab2012-06-22 18:50:08 +1000805 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100806 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000807
Dave Chinner595bff72014-10-02 09:05:14 +1000808 xfs_buf_submit_wait(bp);
Dave Chinnerba372672014-10-02 09:05:32 +1000809 if (bp->b_error) {
810 int error = bp->b_error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800811 xfs_buf_relse(bp);
Dave Chinnerba372672014-10-02 09:05:32 +1000812 return error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800813 }
Dave Chinnerba372672014-10-02 09:05:32 +1000814
815 *bpp = bp;
816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Dave Chinner44396472011-04-21 09:34:27 +0000819/*
820 * Return a buffer allocated as an empty buffer and associated to external
821 * memory via xfs_buf_associate_memory() back to it's empty state.
822 */
823void
824xfs_buf_set_empty(
825 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000826 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000827{
828 if (bp->b_pages)
829 _xfs_buf_free_pages(bp);
830
831 bp->b_pages = NULL;
832 bp->b_page_count = 0;
833 bp->b_addr = NULL;
Dave Chinner4e94b712012-04-23 15:58:51 +1000834 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000835 bp->b_io_length = numblks;
Dave Chinner3e85c862012-06-22 18:50:09 +1000836
837 ASSERT(bp->b_map_count == 1);
Dave Chinner44396472011-04-21 09:34:27 +0000838 bp->b_bn = XFS_BUF_DADDR_NULL;
Dave Chinner3e85c862012-06-22 18:50:09 +1000839 bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL;
840 bp->b_maps[0].bm_len = bp->b_length;
Dave Chinner44396472011-04-21 09:34:27 +0000841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843static inline struct page *
844mem_to_page(
845 void *addr)
846{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800847 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return virt_to_page(addr);
849 } else {
850 return vmalloc_to_page(addr);
851 }
852}
853
854int
Nathan Scottce8e9222006-01-11 15:39:08 +1100855xfs_buf_associate_memory(
856 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 void *mem,
858 size_t len)
859{
860 int rval;
861 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100862 unsigned long pageaddr;
863 unsigned long offset;
864 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 int page_count;
866
Dave Chinner0e6e8472011-03-26 09:16:45 +1100867 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100868 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100869 buflen = PAGE_ALIGN(len + offset);
870 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100873 if (bp->b_pages)
874 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Nathan Scottce8e9222006-01-11 15:39:08 +1100876 bp->b_pages = NULL;
877 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Eric Sandeen87937bf2014-04-14 19:01:20 +1000879 rval = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (rval)
881 return rval;
882
Nathan Scottce8e9222006-01-11 15:39:08 +1100883 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100885 for (i = 0; i < bp->b_page_count; i++) {
886 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100887 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Dave Chinneraa0e8832012-04-23 15:58:52 +1000890 bp->b_io_length = BTOBB(len);
Dave Chinner4e94b712012-04-23 15:58:51 +1000891 bp->b_length = BTOBB(buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 return 0;
894}
895
896xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000897xfs_buf_get_uncached(
898 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000899 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000900 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000902 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000903 int error, i;
Dave Chinner3e85c862012-06-22 18:50:09 +1000904 struct xfs_buf *bp;
905 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Brian Fosterc891c302016-07-20 11:13:43 +1000907 /* flags might contain irrelevant bits, pass only what we care about */
908 bp = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (unlikely(bp == NULL))
910 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Dave Chinnere70b73f2012-04-23 15:58:49 +1000912 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000913 error = _xfs_buf_get_pages(bp, page_count);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000914 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 goto fail_free_buf;
916
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000917 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000918 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000919 if (!bp->b_pages[i])
920 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000922 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Dave Chinner611c9942012-04-23 15:59:07 +1000924 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000925 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100926 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500927 "%s: failed to map pages", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Dave Chinner686865f2010-09-24 20:07:47 +1000931 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000935 while (--i >= 0)
936 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000937 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 fail_free_buf:
Dave Chinner3e85c862012-06-22 18:50:09 +1000939 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000940 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 fail:
942 return NULL;
943}
944
945/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * Increment reference count on buffer, to hold the buffer concurrently
947 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * Must hold the buffer already to call this function.
949 */
950void
Nathan Scottce8e9222006-01-11 15:39:08 +1100951xfs_buf_hold(
952 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000954 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100955 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958/*
Brian Foster9c7504a2016-07-20 11:15:28 +1000959 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
960 * placed on LRU or freed (depending on b_lru_ref).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 */
962void
Nathan Scottce8e9222006-01-11 15:39:08 +1100963xfs_buf_rele(
964 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Dave Chinner74f75a02010-09-24 19:59:04 +1000966 struct xfs_perag *pag = bp->b_pag;
Brian Foster9c7504a2016-07-20 11:15:28 +1000967 bool release;
968 bool freebuf = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000970 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Dave Chinner74f75a02010-09-24 19:59:04 +1000972 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100973 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000974 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Brian Foster9c7504a2016-07-20 11:15:28 +1000975 if (atomic_dec_and_test(&bp->b_hold)) {
976 xfs_buf_ioacct_dec(bp);
Nathan Scottfad3aa12006-02-01 12:14:52 +1100977 xfs_buf_free(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +1000978 }
Nathan Scottfad3aa12006-02-01 12:14:52 +1100979 return;
980 }
981
Dave Chinner74f75a02010-09-24 19:59:04 +1000982 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100983
Lachlan McIlroy37906892008-08-13 15:42:10 +1000984 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinnera4082352013-08-28 10:18:06 +1000985
Dave Chinnerb9c89712018-10-18 17:21:29 +1100986 /*
987 * We grab the b_lock here first to serialise racing xfs_buf_rele()
988 * calls. The pag_buf_lock being taken on the last reference only
989 * serialises against racing lookups in xfs_buf_find(). IOWs, the second
990 * to last reference we drop here is not serialised against the last
991 * reference until we take bp->b_lock. Hence if we don't grab b_lock
992 * first, the last "release" reference can win the race to the lock and
993 * free the buffer before the second-to-last reference is processed,
994 * leading to a use-after-free scenario.
995 */
Brian Foster9c7504a2016-07-20 11:15:28 +1000996 spin_lock(&bp->b_lock);
Dave Chinnerb9c89712018-10-18 17:21:29 +1100997 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000998 if (!release) {
999 /*
1000 * Drop the in-flight state if the buffer is already on the LRU
1001 * and it holds the only reference. This is racy because we
1002 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1003 * ensures the decrement occurs only once per-buf.
1004 */
1005 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
Brian Foster9c795ff2017-05-31 08:22:52 -07001006 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001007 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
Brian Foster9c7504a2016-07-20 11:15:28 +10001009
1010 /* the last reference has been dropped ... */
Brian Foster9c795ff2017-05-31 08:22:52 -07001011 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001012 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1013 /*
1014 * If the buffer is added to the LRU take a new reference to the
1015 * buffer for the LRU and clear the (now stale) dispose list
1016 * state flag
1017 */
1018 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1019 bp->b_state &= ~XFS_BSTATE_DISPOSE;
1020 atomic_inc(&bp->b_hold);
1021 }
1022 spin_unlock(&pag->pag_buf_lock);
1023 } else {
1024 /*
1025 * most of the time buffers will already be removed from the
1026 * LRU, so optimise that case by checking for the
1027 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1028 * was on was the disposal list
1029 */
1030 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1031 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1032 } else {
1033 ASSERT(list_empty(&bp->b_lru));
1034 }
1035
1036 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
1037 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
1038 spin_unlock(&pag->pag_buf_lock);
1039 xfs_perag_put(pag);
1040 freebuf = true;
1041 }
1042
1043out_unlock:
1044 spin_unlock(&bp->b_lock);
1045
1046 if (freebuf)
1047 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050
1051/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001052 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +11001053 *
1054 * If we come across a stale, pinned, locked buffer, we know that we are
1055 * being asked to lock a buffer that has been reallocated. Because it is
1056 * pinned, we know that the log has not been pushed to disk and hence it
1057 * will still be locked. Rather than continuing to have trylock attempts
1058 * fail until someone else pushes the log, push it ourselves before
1059 * returning. This means that the xfsaild will not get stuck trying
1060 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
1062int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001063xfs_buf_trylock(
1064 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 int locked;
1067
Nathan Scottce8e9222006-01-11 15:39:08 +11001068 locked = down_trylock(&bp->b_sema) == 0;
Darrick J. Wong479c6412016-06-21 11:53:28 +10001069 if (locked) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001070 XB_SET_OWNER(bp);
Darrick J. Wong479c6412016-06-21 11:53:28 +10001071 trace_xfs_buf_trylock(bp, _RET_IP_);
1072 } else {
1073 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
1074 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001075 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001079 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +10001080 *
1081 * If we come across a stale, pinned, locked buffer, we know that we
1082 * are being asked to lock a buffer that has been reallocated. Because
1083 * it is pinned, we know that the log has not been pushed to disk and
1084 * hence it will still be locked. Rather than sleeping until someone
1085 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001087void
1088xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001089 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001091 trace_xfs_buf_lock(bp, _RET_IP_);
1092
Dave Chinnered3b4d62010-05-21 12:07:08 +10001093 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +10001094 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +11001095 down(&bp->b_sema);
1096 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001097
1098 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101void
Nathan Scottce8e9222006-01-11 15:39:08 +11001102xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001103 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Brian Foster10f0b2c2017-04-21 12:40:44 -07001105 ASSERT(xfs_buf_islocked(bp));
1106
Nathan Scottce8e9222006-01-11 15:39:08 +11001107 XB_CLEAR_OWNER(bp);
1108 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001109
1110 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
Nathan Scottce8e9222006-01-11 15:39:08 +11001113STATIC void
1114xfs_buf_wait_unpin(
1115 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 DECLARE_WAITQUEUE (wait, current);
1118
Nathan Scottce8e9222006-01-11 15:39:08 +11001119 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return;
1121
Nathan Scottce8e9222006-01-11 15:39:08 +11001122 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 for (;;) {
1124 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001125 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001127 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001129 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 set_current_state(TASK_RUNNING);
1131}
1132
1133/*
1134 * Buffer Utility Routines
1135 */
1136
Dave Chinnere8aaba92014-10-02 09:04:22 +10001137void
1138xfs_buf_ioend(
1139 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001141 bool read = bp->b_flags & XBF_READ;
1142
1143 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001144
1145 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Dave Chinnerd5929de2013-02-27 13:25:54 +11001146
Dave Chinner61be9c52014-10-02 09:04:31 +10001147 /*
1148 * Pull in IO completion errors now. We are guaranteed to be running
1149 * single threaded, so we don't need the lock to read b_io_error.
1150 */
1151 if (!bp->b_error && bp->b_io_error)
1152 xfs_buf_ioerror(bp, bp->b_io_error);
1153
Dave Chinnere8aaba92014-10-02 09:04:22 +10001154 /* Only validate buffers that were read without errors */
1155 if (read && !bp->b_error && bp->b_ops) {
1156 ASSERT(!bp->b_iodone);
Dave Chinner1813dd62012-11-14 17:54:40 +11001157 bp->b_ops->verify_read(bp);
Dave Chinnere8aaba92014-10-02 09:04:22 +10001158 }
1159
1160 if (!bp->b_error)
1161 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Christoph Hellwig80f6c292010-08-18 05:29:11 -04001163 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001164 (*(bp->b_iodone))(bp);
1165 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 xfs_buf_relse(bp);
Dave Chinner595bff72014-10-02 09:05:14 +10001167 else
Dave Chinner1813dd62012-11-14 17:54:40 +11001168 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Dave Chinnere8aaba92014-10-02 09:04:22 +10001171static void
1172xfs_buf_ioend_work(
1173 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001175 struct xfs_buf *bp =
Brian Fosterb29c70f2014-12-04 09:43:17 +11001176 container_of(work, xfs_buf_t, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001177
Dave Chinnere8aaba92014-10-02 09:04:22 +10001178 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001181static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001182xfs_buf_ioend_async(
1183 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001185 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
1186 queue_work(bp->b_ioend_wq, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189void
Nathan Scottce8e9222006-01-11 15:39:08 +11001190xfs_buf_ioerror(
1191 xfs_buf_t *bp,
1192 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Dave Chinner24513372014-06-25 14:58:08 +10001194 ASSERT(error <= 0 && error >= -1000);
1195 bp->b_error = error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001196 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Christoph Hellwig901796a2011-10-10 16:52:49 +00001199void
1200xfs_buf_ioerror_alert(
1201 struct xfs_buf *bp,
1202 const char *func)
1203{
1204 xfs_alert(bp->b_target->bt_mount,
Dave Chinneraa0e8832012-04-23 15:58:52 +10001205"metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
Dave Chinner24513372014-06-25 14:58:08 +10001206 (__uint64_t)XFS_BUF_ADDR(bp), func, -bp->b_error, bp->b_length);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001207}
1208
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001209int
1210xfs_bwrite(
1211 struct xfs_buf *bp)
1212{
1213 int error;
1214
1215 ASSERT(xfs_buf_islocked(bp));
1216
1217 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001218 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
1219 XBF_WRITE_FAIL | XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001220
Dave Chinner595bff72014-10-02 09:05:14 +10001221 error = xfs_buf_submit_wait(bp);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001222 if (error) {
1223 xfs_force_shutdown(bp->b_target->bt_mount,
1224 SHUTDOWN_META_IO_ERROR);
1225 }
1226 return error;
1227}
1228
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001229static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001230xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001231 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001233 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Dave Chinner37eb17e2012-11-12 22:09:46 +11001235 /*
1236 * don't overwrite existing errors - otherwise we can lose errors on
1237 * buffers that require multiple bios to complete.
1238 */
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001239 if (bio->bi_error)
1240 cmpxchg(&bp->b_io_error, 0, bio->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Dave Chinner37eb17e2012-11-12 22:09:46 +11001242 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001243 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1244
Dave Chinnere8aaba92014-10-02 09:04:22 +10001245 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1246 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Dave Chinner3e85c862012-06-22 18:50:09 +10001250static void
1251xfs_buf_ioapply_map(
1252 struct xfs_buf *bp,
1253 int map,
1254 int *buf_offset,
1255 int *count,
Mike Christie50bfcd02016-06-05 14:31:57 -05001256 int op,
1257 int op_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Dave Chinner3e85c862012-06-22 18:50:09 +10001259 int page_index;
1260 int total_nr_pages = bp->b_page_count;
1261 int nr_pages;
1262 struct bio *bio;
1263 sector_t sector = bp->b_maps[map].bm_bn;
1264 int size;
1265 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Nathan Scottce8e9222006-01-11 15:39:08 +11001267 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Dave Chinner3e85c862012-06-22 18:50:09 +10001269 /* skip the pages in the buffer before the start offset */
1270 page_index = 0;
1271 offset = *buf_offset;
1272 while (offset >= PAGE_SIZE) {
1273 page_index++;
1274 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001275 }
1276
Dave Chinner3e85c862012-06-22 18:50:09 +10001277 /*
1278 * Limit the IO size to the length of the current vector, and update the
1279 * remaining IO count for the next time around.
1280 */
1281 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1282 *count -= size;
1283 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001286 atomic_inc(&bp->b_io_remaining);
Ming Leic908e382016-05-30 21:34:33 +08001287 nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001290 bio->bi_bdev = bp->b_target->bt_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001291 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001292 bio->bi_end_io = xfs_buf_bio_end_io;
1293 bio->bi_private = bp;
Mike Christie50bfcd02016-06-05 14:31:57 -05001294 bio_set_op_attrs(bio, op, op_flags);
Dave Chinner0e6e8472011-03-26 09:16:45 +11001295
Dave Chinner3e85c862012-06-22 18:50:09 +10001296 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001297 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 if (nbytes > size)
1300 nbytes = size;
1301
Dave Chinner3e85c862012-06-22 18:50:09 +10001302 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1303 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001304 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 break;
1306
1307 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001308 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 size -= nbytes;
1310 total_nr_pages--;
1311 }
1312
Kent Overstreet4f024f32013-10-11 15:44:27 -07001313 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001314 if (xfs_buf_is_vmapped(bp)) {
1315 flush_kernel_vmap_range(bp->b_addr,
1316 xfs_buf_vmap_len(bp));
1317 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001318 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (size)
1320 goto next_chunk;
1321 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001322 /*
1323 * This is guaranteed not to be the last io reference count
Dave Chinner595bff72014-10-02 09:05:14 +10001324 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001325 */
1326 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001327 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001328 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001330
1331}
1332
1333STATIC void
1334_xfs_buf_ioapply(
1335 struct xfs_buf *bp)
1336{
1337 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001338 int op;
1339 int op_flags = 0;
Dave Chinner3e85c862012-06-22 18:50:09 +10001340 int offset;
1341 int size;
1342 int i;
1343
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001344 /*
1345 * Make sure we capture only current IO errors rather than stale errors
1346 * left over from previous use of the buffer (e.g. failed readahead).
1347 */
1348 bp->b_error = 0;
1349
Brian Fosterb29c70f2014-12-04 09:43:17 +11001350 /*
1351 * Initialize the I/O completion workqueue if we haven't yet or the
1352 * submitter has not opted to specify a custom one.
1353 */
1354 if (!bp->b_ioend_wq)
1355 bp->b_ioend_wq = bp->b_target->bt_mount->m_buf_workqueue;
1356
Dave Chinner3e85c862012-06-22 18:50:09 +10001357 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001358 op = REQ_OP_WRITE;
Dave Chinner3e85c862012-06-22 18:50:09 +10001359 if (bp->b_flags & XBF_SYNCIO)
Mike Christie50bfcd02016-06-05 14:31:57 -05001360 op_flags = WRITE_SYNC;
Dave Chinner3e85c862012-06-22 18:50:09 +10001361 if (bp->b_flags & XBF_FUA)
Mike Christie50bfcd02016-06-05 14:31:57 -05001362 op_flags |= REQ_FUA;
Dave Chinner3e85c862012-06-22 18:50:09 +10001363 if (bp->b_flags & XBF_FLUSH)
Mike Christie28a8f0d2016-06-05 14:32:25 -05001364 op_flags |= REQ_PREFLUSH;
Dave Chinner1813dd62012-11-14 17:54:40 +11001365
1366 /*
1367 * Run the write verifier callback function if it exists. If
1368 * this function fails it will mark the buffer with an error and
1369 * the IO should not be dispatched.
1370 */
1371 if (bp->b_ops) {
1372 bp->b_ops->verify_write(bp);
1373 if (bp->b_error) {
1374 xfs_force_shutdown(bp->b_target->bt_mount,
1375 SHUTDOWN_CORRUPT_INCORE);
1376 return;
1377 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001378 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
1379 struct xfs_mount *mp = bp->b_target->bt_mount;
1380
1381 /*
1382 * non-crc filesystems don't attach verifiers during
1383 * log recovery, so don't warn for such filesystems.
1384 */
1385 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1386 xfs_warn(mp,
1387 "%s: no ops on block 0x%llx/0x%x",
1388 __func__, bp->b_bn, bp->b_length);
1389 xfs_hex_dump(bp->b_addr, 64);
1390 dump_stack();
1391 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001392 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001393 } else if (bp->b_flags & XBF_READ_AHEAD) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001394 op = REQ_OP_READ;
1395 op_flags = REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001396 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001397 op = REQ_OP_READ;
Dave Chinner3e85c862012-06-22 18:50:09 +10001398 }
1399
1400 /* we only use the buffer cache for meta-data */
Mike Christie50bfcd02016-06-05 14:31:57 -05001401 op_flags |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001402
1403 /*
1404 * Walk all the vectors issuing IO on them. Set up the initial offset
1405 * into the buffer and the desired IO size before we start -
1406 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1407 * subsequent call.
1408 */
1409 offset = bp->b_offset;
1410 size = BBTOB(bp->b_io_length);
1411 blk_start_plug(&plug);
1412 for (i = 0; i < bp->b_map_count; i++) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001413 xfs_buf_ioapply_map(bp, i, &offset, &size, op, op_flags);
Dave Chinner3e85c862012-06-22 18:50:09 +10001414 if (bp->b_error)
1415 break;
1416 if (size <= 0)
1417 break; /* all done */
1418 }
1419 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Dave Chinner595bff72014-10-02 09:05:14 +10001422/*
1423 * Asynchronous IO submission path. This transfers the buffer lock ownership and
1424 * the current reference to the IO. It is not safe to reference the buffer after
1425 * a call to this function unless the caller holds an additional reference
1426 * itself.
1427 */
Dave Chinner0e95f192012-04-23 15:58:46 +10001428void
Dave Chinner595bff72014-10-02 09:05:14 +10001429xfs_buf_submit(
1430 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Dave Chinner595bff72014-10-02 09:05:14 +10001432 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001434 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff72014-10-02 09:05:14 +10001435 ASSERT(bp->b_flags & XBF_ASYNC);
1436
1437 /* on shutdown we stale and complete the buffer immediately */
1438 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
1439 xfs_buf_ioerror(bp, -EIO);
1440 bp->b_flags &= ~XBF_DONE;
1441 xfs_buf_stale(bp);
1442 xfs_buf_ioend(bp);
1443 return;
1444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Christoph Hellwig375ec69d2011-08-23 08:28:03 +00001446 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001447 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Dave Chinner61be9c52014-10-02 09:04:31 +10001449 /* clear the internal error state to avoid spurious errors */
1450 bp->b_io_error = 0;
1451
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001452 /*
Dave Chinner595bff72014-10-02 09:05:14 +10001453 * The caller's reference is released during I/O completion.
1454 * This occurs some time after the last b_io_remaining reference is
1455 * released, so after we drop our Io reference we have to have some
1456 * other reference to ensure the buffer doesn't go away from underneath
1457 * us. Take a direct reference to ensure we have safe access to the
1458 * buffer until we are finished with it.
Dave Chinnere11bb802014-10-02 09:04:11 +10001459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 xfs_buf_hold(bp);
1461
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001462 /*
Dave Chinnere11bb802014-10-02 09:04:11 +10001463 * Set the count to 1 initially, this will stop an I/O completion
1464 * callout which happens before we have started all the I/O from calling
1465 * xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001467 atomic_set(&bp->b_io_remaining, 1);
Brian Foster9c7504a2016-07-20 11:15:28 +10001468 xfs_buf_ioacct_inc(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001469 _xfs_buf_ioapply(bp);
Dave Chinnere11bb802014-10-02 09:04:11 +10001470
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001471 /*
Dave Chinner595bff72014-10-02 09:05:14 +10001472 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1473 * reference we took above. If we drop it to zero, run completion so
1474 * that we don't return to the caller with completion still pending.
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001475 */
Dave Chinnere8aaba92014-10-02 09:04:22 +10001476 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
Dave Chinner595bff72014-10-02 09:05:14 +10001477 if (bp->b_error)
Dave Chinnere8aaba92014-10-02 09:04:22 +10001478 xfs_buf_ioend(bp);
1479 else
1480 xfs_buf_ioend_async(bp);
1481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Nathan Scottce8e9222006-01-11 15:39:08 +11001483 xfs_buf_rele(bp);
Dave Chinner595bff72014-10-02 09:05:14 +10001484 /* Note: it is not safe to reference bp now we've dropped our ref */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487/*
Dave Chinner595bff72014-10-02 09:05:14 +10001488 * Synchronous buffer IO submission path, read or write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 */
1490int
Dave Chinner595bff72014-10-02 09:05:14 +10001491xfs_buf_submit_wait(
1492 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Dave Chinner595bff72014-10-02 09:05:14 +10001494 int error;
1495
1496 trace_xfs_buf_submit_wait(bp, _RET_IP_);
1497
1498 ASSERT(!(bp->b_flags & (_XBF_DELWRI_Q | XBF_ASYNC)));
1499
1500 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
1501 xfs_buf_ioerror(bp, -EIO);
1502 xfs_buf_stale(bp);
1503 bp->b_flags &= ~XBF_DONE;
1504 return -EIO;
1505 }
1506
1507 if (bp->b_flags & XBF_WRITE)
1508 xfs_buf_wait_unpin(bp);
1509
1510 /* clear the internal error state to avoid spurious errors */
1511 bp->b_io_error = 0;
1512
1513 /*
1514 * For synchronous IO, the IO does not inherit the submitters reference
1515 * count, nor the buffer lock. Hence we cannot release the reference we
1516 * are about to take until we've waited for all IO completion to occur,
1517 * including any xfs_buf_ioend_async() work that may be pending.
1518 */
1519 xfs_buf_hold(bp);
1520
1521 /*
1522 * Set the count to 1 initially, this will stop an I/O completion
1523 * callout which happens before we have started all the I/O from calling
1524 * xfs_buf_ioend too early.
1525 */
1526 atomic_set(&bp->b_io_remaining, 1);
1527 _xfs_buf_ioapply(bp);
1528
1529 /*
1530 * make sure we run completion synchronously if it raced with us and is
1531 * already complete.
1532 */
1533 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1534 xfs_buf_ioend(bp);
1535
1536 /* wait for completion before gathering the error from the buffer */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001537 trace_xfs_buf_iowait(bp, _RET_IP_);
Dave Chinner595bff72014-10-02 09:05:14 +10001538 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001539 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Dave Chinner595bff72014-10-02 09:05:14 +10001540 error = bp->b_error;
1541
1542 /*
1543 * all done now, we can release the hold that keeps the buffer
1544 * referenced for the entire IO.
1545 */
1546 xfs_buf_rele(bp);
1547 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001550void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001551xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001552 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 size_t offset)
1554{
1555 struct page *page;
1556
Dave Chinner611c9942012-04-23 15:59:07 +10001557 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001558 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Nathan Scottce8e9222006-01-11 15:39:08 +11001560 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001561 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001562 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
1565/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 * Move data into or out of a buffer.
1567 */
1568void
Nathan Scottce8e9222006-01-11 15:39:08 +11001569xfs_buf_iomove(
1570 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 size_t boff, /* starting buffer offset */
1572 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001573 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001574 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Dave Chinner795cac72012-04-23 15:58:53 +10001576 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 bend = boff + bsize;
1579 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001580 struct page *page;
1581 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Dave Chinner795cac72012-04-23 15:58:53 +10001583 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1584 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1585 page = bp->b_pages[page_index];
1586 csize = min_t(size_t, PAGE_SIZE - page_offset,
1587 BBTOB(bp->b_io_length) - boff);
1588
1589 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001592 case XBRW_ZERO:
Dave Chinner795cac72012-04-23 15:58:53 +10001593 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001595 case XBRW_READ:
Dave Chinner795cac72012-04-23 15:58:53 +10001596 memcpy(data, page_address(page) + page_offset, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001598 case XBRW_WRITE:
Dave Chinner795cac72012-04-23 15:58:53 +10001599 memcpy(page_address(page) + page_offset, data, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601
1602 boff += csize;
1603 data += csize;
1604 }
1605}
1606
1607/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001608 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 */
1610
1611/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001612 * Wait for any bufs with callbacks that have been submitted but have not yet
1613 * returned. These buffers will have an elevated hold count, so wait on those
1614 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001616static enum lru_status
1617xfs_buftarg_wait_rele(
1618 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001619 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001620 spinlock_t *lru_lock,
1621 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Dave Chinnere80dfa12013-08-28 10:18:05 +10001623{
1624 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001625 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001626
1627 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001628 /* need to wait, so skip it this pass */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001629 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001630 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
Dave Chinnera4082352013-08-28 10:18:06 +10001632 if (!spin_trylock(&bp->b_lock))
1633 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001634
Dave Chinnera4082352013-08-28 10:18:06 +10001635 /*
1636 * clear the LRU reference count so the buffer doesn't get
1637 * ignored in xfs_buf_rele().
1638 */
1639 atomic_set(&bp->b_lru_ref, 0);
1640 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001641 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001642 spin_unlock(&bp->b_lock);
1643 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
Dave Chinnere80dfa12013-08-28 10:18:05 +10001646void
1647xfs_wait_buftarg(
1648 struct xfs_buftarg *btp)
1649{
Dave Chinnera4082352013-08-28 10:18:06 +10001650 LIST_HEAD(dispose);
1651 int loop = 0;
1652
Dave Chinner85bec542016-01-19 08:28:10 +11001653 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001654 * First wait on the buftarg I/O count for all in-flight buffers to be
1655 * released. This is critical as new buffers do not make the LRU until
1656 * they are released.
1657 *
1658 * Next, flush the buffer workqueue to ensure all completion processing
1659 * has finished. Just waiting on buffer locks is not sufficient for
1660 * async IO as the reference count held over IO is not released until
1661 * after the buffer lock is dropped. Hence we need to ensure here that
1662 * all reference counts have been dropped before we start walking the
1663 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001664 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001665 while (percpu_counter_sum(&btp->bt_io_count))
1666 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001667 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Dave Chinner85bec542016-01-19 08:28:10 +11001668
Dave Chinnera4082352013-08-28 10:18:06 +10001669 /* loop until there is nothing left on the lru list. */
1670 while (list_lru_count(&btp->bt_lru)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001671 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001672 &dispose, LONG_MAX);
1673
1674 while (!list_empty(&dispose)) {
1675 struct xfs_buf *bp;
1676 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1677 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001678 if (bp->b_flags & XBF_WRITE_FAIL) {
1679 xfs_alert(btp->bt_mount,
Joe Perchesf41febd2015-07-29 11:52:04 +10001680"Corruption Alert: Buffer at block 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001681 (long long)bp->b_bn);
Joe Perchesf41febd2015-07-29 11:52:04 +10001682 xfs_alert(btp->bt_mount,
1683"Please run xfs_repair to determine the extent of the problem.");
Dave Chinnerac8809f2013-12-12 16:34:38 +11001684 }
Dave Chinnera4082352013-08-28 10:18:06 +10001685 xfs_buf_rele(bp);
1686 }
1687 if (loop++ != 0)
1688 delay(100);
1689 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001690}
1691
1692static enum lru_status
1693xfs_buftarg_isolate(
1694 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001695 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001696 spinlock_t *lru_lock,
1697 void *arg)
1698{
1699 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1700 struct list_head *dispose = arg;
1701
1702 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001703 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1704 * If we fail to get the lock, just skip it.
1705 */
1706 if (!spin_trylock(&bp->b_lock))
1707 return LRU_SKIP;
1708 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001709 * Decrement the b_lru_ref count unless the value is already
1710 * zero. If the value is already zero, we need to reclaim the
1711 * buffer, otherwise it gets another trip through the LRU.
1712 */
Vratislav Bendel00eab762018-03-06 17:07:44 -08001713 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
Dave Chinnera4082352013-08-28 10:18:06 +10001714 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001715 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001716 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001717
Dave Chinnera4082352013-08-28 10:18:06 +10001718 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001719 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001720 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001721 return LRU_REMOVED;
1722}
1723
Andrew Mortonaddbda42013-08-28 10:18:06 +10001724static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001725xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001726 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001727 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001728{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001729 struct xfs_buftarg *btp = container_of(shrink,
1730 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001731 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001732 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001733
Vladimir Davydov503c3582015-02-12 14:58:47 -08001734 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1735 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001736
1737 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001738 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001739 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1740 list_del_init(&bp->b_lru);
1741 xfs_buf_rele(bp);
1742 }
1743
Dave Chinnere80dfa12013-08-28 10:18:05 +10001744 return freed;
1745}
1746
Andrew Mortonaddbda42013-08-28 10:18:06 +10001747static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001748xfs_buftarg_shrink_count(
1749 struct shrinker *shrink,
1750 struct shrink_control *sc)
1751{
1752 struct xfs_buftarg *btp = container_of(shrink,
1753 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001754 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001755}
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757void
1758xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001759 struct xfs_mount *mp,
1760 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001762 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001763 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1764 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001765 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001766
Christoph Hellwigb7963132009-03-03 14:48:37 -05001767 if (mp->m_flags & XFS_MOUNT_BARRIER)
1768 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001769
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001770 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001773int
1774xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001776 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001778 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001779 btp->bt_meta_sectorsize = sectorsize;
1780 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Nathan Scottce8e9222006-01-11 15:39:08 +11001782 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001783 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001784 "Cannot set_blocksize to %u on device %pg",
1785 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001786 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001789 /* Set up device logical sector size mask */
1790 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1791 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return 0;
1794}
1795
1796/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001797 * When allocating the initial buffer target we have not yet
1798 * read in the superblock, so don't know what sized sectors
1799 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001800 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801STATIC int
1802xfs_setsize_buftarg_early(
1803 xfs_buftarg_t *btp,
1804 struct block_device *bdev)
1805{
Eric Sandeena96c4152014-04-14 19:00:29 +10001806 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809xfs_buftarg_t *
1810xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001811 struct xfs_mount *mp,
Eric Sandeen34dcefd2014-04-14 19:01:00 +10001812 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 xfs_buftarg_t *btp;
1815
Dave Chinnerb17cb362013-05-20 09:51:12 +10001816 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Dave Chinnerebad8612010-09-22 10:47:20 +10001818 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001819 btp->bt_dev = bdev->bd_dev;
1820 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (xfs_setsize_buftarg_early(btp, bdev))
Michal Hocko54a1fdf2017-11-23 17:13:40 +01001823 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001824
1825 if (list_lru_init(&btp->bt_lru))
Michal Hocko54a1fdf2017-11-23 17:13:40 +01001826 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001827
Brian Foster9c7504a2016-07-20 11:15:28 +10001828 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
Michal Hocko54a1fdf2017-11-23 17:13:40 +01001829 goto error_lru;
Brian Foster9c7504a2016-07-20 11:15:28 +10001830
Dave Chinnere80dfa12013-08-28 10:18:05 +10001831 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1832 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001833 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001834 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Michal Hocko54a1fdf2017-11-23 17:13:40 +01001835 if (register_shrinker(&btp->bt_shrinker))
1836 goto error_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return btp;
1838
Michal Hocko54a1fdf2017-11-23 17:13:40 +01001839error_pcpu:
1840 percpu_counter_destroy(&btp->bt_io_count);
1841error_lru:
1842 list_lru_destroy(&btp->bt_lru);
1843error_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001844 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return NULL;
1846}
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848/*
Brian Foster10f0b2c2017-04-21 12:40:44 -07001849 * Cancel a delayed write list.
1850 *
1851 * Remove each buffer from the list, clear the delwri queue flag and drop the
1852 * associated buffer reference.
1853 */
1854void
1855xfs_buf_delwri_cancel(
1856 struct list_head *list)
1857{
1858 struct xfs_buf *bp;
1859
1860 while (!list_empty(list)) {
1861 bp = list_first_entry(list, struct xfs_buf, b_list);
1862
1863 xfs_buf_lock(bp);
1864 bp->b_flags &= ~_XBF_DELWRI_Q;
1865 list_del_init(&bp->b_list);
1866 xfs_buf_relse(bp);
1867 }
1868}
1869
1870/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001871 * Add a buffer to the delayed write list.
1872 *
1873 * This queues a buffer for writeout if it hasn't already been. Note that
1874 * neither this routine nor the buffer list submission functions perform
1875 * any internal synchronization. It is expected that the lists are thread-local
1876 * to the callers.
1877 *
1878 * Returns true if we queued up the buffer, or false if it already had
1879 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001881bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001882xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001883 struct xfs_buf *bp,
1884 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001886 ASSERT(xfs_buf_islocked(bp));
1887 ASSERT(!(bp->b_flags & XBF_READ));
1888
1889 /*
1890 * If the buffer is already marked delwri it already is queued up
1891 * by someone else for imediate writeout. Just ignore it in that
1892 * case.
1893 */
1894 if (bp->b_flags & _XBF_DELWRI_Q) {
1895 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1896 return false;
1897 }
David Chinnera6867a62006-01-11 15:37:58 +11001898
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001899 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1900
Dave Chinnerd808f612010-02-02 10:13:42 +11001901 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001902 * If a buffer gets written out synchronously or marked stale while it
1903 * is on a delwri list we lazily remove it. To do this, the other party
1904 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1905 * It remains referenced and on the list. In a rare corner case it
1906 * might get readded to a delwri list after the synchronous writeout, in
1907 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001908 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001909 bp->b_flags |= _XBF_DELWRI_Q;
1910 if (list_empty(&bp->b_list)) {
1911 atomic_inc(&bp->b_hold);
1912 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001913 }
David Chinner585e6d82007-02-10 18:32:29 +11001914
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001915 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001916}
1917
Dave Chinner089716a2010-01-26 15:13:25 +11001918/*
1919 * Compare function is more complex than it needs to be because
1920 * the return value is only 32 bits and we are doing comparisons
1921 * on 64 bit values
1922 */
1923static int
1924xfs_buf_cmp(
1925 void *priv,
1926 struct list_head *a,
1927 struct list_head *b)
1928{
1929 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1930 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1931 xfs_daddr_t diff;
1932
Mark Tinguelyf4b42422012-12-04 17:18:02 -06001933 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11001934 if (diff < 0)
1935 return -1;
1936 if (diff > 0)
1937 return 1;
1938 return 0;
1939}
1940
Dave Chinner26f1fe82016-06-01 17:38:15 +10001941/*
1942 * submit buffers for write.
1943 *
1944 * When we have a large buffer list, we do not want to hold all the buffers
1945 * locked while we block on the request queue waiting for IO dispatch. To avoid
1946 * this problem, we lock and submit buffers in groups of 50, thereby minimising
1947 * the lock hold times for lists which may contain thousands of objects.
1948 *
1949 * To do this, we sort the buffer list before we walk the list to lock and
1950 * submit buffers, and we plug and unplug around each group of buffers we
1951 * submit.
1952 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001953static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10001954xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001955 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10001956 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001958 struct xfs_buf *bp, *n;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001959 LIST_HEAD (submit_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001960 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001961 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Dave Chinner26f1fe82016-06-01 17:38:15 +10001963 list_sort(NULL, buffer_list, xfs_buf_cmp);
1964
1965 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001966 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10001967 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001968 if (xfs_buf_ispinned(bp)) {
1969 pinned++;
1970 continue;
1971 }
1972 if (!xfs_buf_trylock(bp))
1973 continue;
1974 } else {
1975 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001978 /*
1979 * Someone else might have written the buffer synchronously or
1980 * marked it stale in the meantime. In that case only the
1981 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1982 * reference and remove it from the list here.
1983 */
1984 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1985 list_del_init(&bp->b_list);
1986 xfs_buf_relse(bp);
1987 continue;
1988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001990 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001991
Dave Chinnercf53e992014-10-02 09:04:01 +10001992 /*
Dave Chinner26f1fe82016-06-01 17:38:15 +10001993 * We do all IO submission async. This means if we need
1994 * to wait for IO completion we need to take an extra
1995 * reference so the buffer is still valid on the other
1996 * side. We need to move the buffer onto the io_list
1997 * at this point so the caller can still access it.
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);
Dave Chinner26f1fe82016-06-01 17:38:15 +10002000 bp->b_flags |= XBF_WRITE | XBF_ASYNC;
2001 if (wait_list) {
Dave Chinnercf53e992014-10-02 09:04:01 +10002002 xfs_buf_hold(bp);
Dave Chinner26f1fe82016-06-01 17:38:15 +10002003 list_move_tail(&bp->b_list, wait_list);
2004 } else
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002005 list_del_init(&bp->b_list);
Dave Chinner8dac3922014-10-02 09:04:40 +10002006
Dave Chinner595bff72014-10-02 09:05:14 +10002007 xfs_buf_submit(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00002009 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002011 return pinned;
2012}
Nathan Scottf07c2252006-09-28 10:52:15 +10002013
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002014/*
2015 * Write out a buffer list asynchronously.
2016 *
2017 * This will take the @buffer_list, write all non-locked and non-pinned buffers
2018 * out and not wait for I/O completion on any of the buffers. This interface
2019 * is only safely useable for callers that can track I/O completion by higher
2020 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
2021 * function.
2022 */
2023int
2024xfs_buf_delwri_submit_nowait(
2025 struct list_head *buffer_list)
2026{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002027 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002028}
2029
2030/*
2031 * Write out a buffer list synchronously.
2032 *
2033 * This will take the @buffer_list, write all buffers out and wait for I/O
2034 * completion on all of the buffers. @buffer_list is consumed by the function,
2035 * so callers must have some other way of tracking buffers if they require such
2036 * functionality.
2037 */
2038int
2039xfs_buf_delwri_submit(
2040 struct list_head *buffer_list)
2041{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002042 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002043 int error = 0, error2;
2044 struct xfs_buf *bp;
2045
Dave Chinner26f1fe82016-06-01 17:38:15 +10002046 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002047
2048 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10002049 while (!list_empty(&wait_list)) {
2050 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002051
2052 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10002053
2054 /* locking the buffer will wait for async IO completion. */
2055 xfs_buf_lock(bp);
2056 error2 = bp->b_error;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002057 xfs_buf_relse(bp);
2058 if (!error)
2059 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
2061
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002062 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Brian Foster7cb011b2017-06-14 21:21:45 -07002065/*
2066 * Push a single buffer on a delwri queue.
2067 *
2068 * The purpose of this function is to submit a single buffer of a delwri queue
2069 * and return with the buffer still on the original queue. The waiting delwri
2070 * buffer submission infrastructure guarantees transfer of the delwri queue
2071 * buffer reference to a temporary wait list. We reuse this infrastructure to
2072 * transfer the buffer back to the original queue.
2073 *
2074 * Note the buffer transitions from the queued state, to the submitted and wait
2075 * listed state and back to the queued state during this call. The buffer
2076 * locking and queue management logic between _delwri_pushbuf() and
2077 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2078 * before returning.
2079 */
2080int
2081xfs_buf_delwri_pushbuf(
2082 struct xfs_buf *bp,
2083 struct list_head *buffer_list)
2084{
2085 LIST_HEAD (submit_list);
2086 int error;
2087
2088 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2089
2090 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2091
2092 /*
2093 * Isolate the buffer to a new local list so we can submit it for I/O
2094 * independently from the rest of the original list.
2095 */
2096 xfs_buf_lock(bp);
2097 list_move(&bp->b_list, &submit_list);
2098 xfs_buf_unlock(bp);
2099
2100 /*
2101 * Delwri submission clears the DELWRI_Q buffer flag and returns with
2102 * the buffer on the wait list with an associated reference. Rather than
2103 * bounce the buffer from a local wait list back to the original list
2104 * after I/O completion, reuse the original list as the wait list.
2105 */
2106 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2107
2108 /*
2109 * The buffer is now under I/O and wait listed as during typical delwri
2110 * submission. Lock the buffer to wait for I/O completion. Rather than
2111 * remove the buffer from the wait list and release the reference, we
2112 * want to return with the buffer queued to the original list. The
2113 * buffer already sits on the original list with a wait list reference,
2114 * however. If we let the queue inherit that wait list reference, all we
2115 * need to do is reset the DELWRI_Q flag.
2116 */
2117 xfs_buf_lock(bp);
2118 error = bp->b_error;
2119 bp->b_flags |= _XBF_DELWRI_Q;
2120 xfs_buf_unlock(bp);
2121
2122 return error;
2123}
2124
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002125int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002126xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Nathan Scott87582802006-03-14 13:18:19 +11002128 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
2129 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002130 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002131 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002132
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002135 out:
Nathan Scott87582802006-03-14 13:18:19 +11002136 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139void
Nathan Scottce8e9222006-01-11 15:39:08 +11002140xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
Nathan Scottce8e9222006-01-11 15:39:08 +11002142 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}