blob: db786bce7c03eaea062d9be5bdd05403f42592b0 [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>
Ingo Molnar5b3cc152017-02-02 20:43:54 +010036#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Christoph Hellwig4fb6e8a2014-11-28 14:25:04 +110038#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110039#include "xfs_log_format.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100040#include "xfs_trans_resv.h"
Dave Chinner239880e2013-10-23 10:50:10 +110041#include "xfs_sb.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050042#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110044#include "xfs_log.h"
Brian Foster7561d272017-10-17 14:16:29 -070045#include "xfs_error.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050046
David Chinner7989cb82007-02-10 18:34:56 +110047static kmem_zone_t *xfs_buf_zone;
Christoph Hellwig23ea4032005-06-21 15:14:01 +100048
Nathan Scottce8e9222006-01-11 15:39:08 +110049#ifdef XFS_BUF_LOCK_TRACKING
50# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
51# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
52# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#else
Nathan Scottce8e9222006-01-11 15:39:08 +110054# define XB_SET_OWNER(bp) do { } while (0)
55# define XB_CLEAR_OWNER(bp) do { } while (0)
56# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#endif
58
Nathan Scottce8e9222006-01-11 15:39:08 +110059#define xb_to_gfp(flags) \
Dave Chinneraa5c1582012-04-23 15:58:56 +100060 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
James Bottomley73c77e22010-01-25 11:42:24 -060063static inline int
64xfs_buf_is_vmapped(
65 struct xfs_buf *bp)
66{
67 /*
68 * Return true if the buffer is vmapped.
69 *
Dave Chinner611c9942012-04-23 15:59:07 +100070 * b_addr is null if the buffer is not mapped, but the code is clever
71 * enough to know it doesn't have to map a single page, so the check has
72 * to be both for b_addr and bp->b_page_count > 1.
James Bottomley73c77e22010-01-25 11:42:24 -060073 */
Dave Chinner611c9942012-04-23 15:59:07 +100074 return bp->b_addr && bp->b_page_count > 1;
James Bottomley73c77e22010-01-25 11:42:24 -060075}
76
77static inline int
78xfs_buf_vmap_len(
79 struct xfs_buf *bp)
80{
81 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
82}
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
Brian Foster9c7504a2016-07-20 11:15:28 +100085 * Bump the I/O in flight count on the buftarg if we haven't yet done so for
86 * this buffer. The count is incremented once per buffer (per hold cycle)
87 * because the corresponding decrement is deferred to buffer release. Buffers
88 * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
89 * tracking adds unnecessary overhead. This is used for sychronization purposes
90 * with unmount (see xfs_wait_buftarg()), so all we really need is a count of
91 * in-flight buffers.
92 *
93 * Buffers that are never released (e.g., superblock, iclog buffers) must set
94 * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
95 * never reaches zero and unmount hangs indefinitely.
96 */
97static inline void
98xfs_buf_ioacct_inc(
99 struct xfs_buf *bp)
100{
Brian Foster63db7c82017-05-31 08:22:52 -0700101 if (bp->b_flags & XBF_NO_IOACCT)
Brian Foster9c7504a2016-07-20 11:15:28 +1000102 return;
103
104 ASSERT(bp->b_flags & XBF_ASYNC);
Brian Foster63db7c82017-05-31 08:22:52 -0700105 spin_lock(&bp->b_lock);
106 if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) {
107 bp->b_state |= XFS_BSTATE_IN_FLIGHT;
108 percpu_counter_inc(&bp->b_target->bt_io_count);
109 }
110 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000111}
112
113/*
114 * Clear the in-flight state on a buffer about to be released to the LRU or
115 * freed and unaccount from the buftarg.
116 */
117static inline void
Brian Foster63db7c82017-05-31 08:22:52 -0700118__xfs_buf_ioacct_dec(
119 struct xfs_buf *bp)
120{
Brian Foster95989c42017-06-08 08:23:07 -0700121 lockdep_assert_held(&bp->b_lock);
Brian Foster63db7c82017-05-31 08:22:52 -0700122
123 if (bp->b_state & XFS_BSTATE_IN_FLIGHT) {
124 bp->b_state &= ~XFS_BSTATE_IN_FLIGHT;
125 percpu_counter_dec(&bp->b_target->bt_io_count);
126 }
127}
128
129static inline void
Brian Foster9c7504a2016-07-20 11:15:28 +1000130xfs_buf_ioacct_dec(
131 struct xfs_buf *bp)
132{
Brian Foster63db7c82017-05-31 08:22:52 -0700133 spin_lock(&bp->b_lock);
134 __xfs_buf_ioacct_dec(bp);
135 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000136}
137
138/*
Dave Chinner430cbeb2010-12-02 16:30:55 +1100139 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
140 * b_lru_ref count so that the buffer is freed immediately when the buffer
141 * reference count falls to zero. If the buffer is already on the LRU, we need
142 * to remove the reference that LRU holds on the buffer.
143 *
144 * This prevents build-up of stale buffers on the LRU.
145 */
146void
147xfs_buf_stale(
148 struct xfs_buf *bp)
149{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000150 ASSERT(xfs_buf_islocked(bp));
151
Dave Chinner430cbeb2010-12-02 16:30:55 +1100152 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000153
154 /*
155 * Clear the delwri status so that a delwri queue walker will not
156 * flush this buffer to disk now that it is stale. The delwri queue has
157 * a reference to the buffer, so this is safe to do.
158 */
159 bp->b_flags &= ~_XBF_DELWRI_Q;
160
Brian Foster9c7504a2016-07-20 11:15:28 +1000161 /*
162 * Once the buffer is marked stale and unlocked, a subsequent lookup
163 * could reset b_flags. There is no guarantee that the buffer is
164 * unaccounted (released to LRU) before that occurs. Drop in-flight
165 * status now to preserve accounting consistency.
166 */
Dave Chinnera4082352013-08-28 10:18:06 +1000167 spin_lock(&bp->b_lock);
Brian Foster63db7c82017-05-31 08:22:52 -0700168 __xfs_buf_ioacct_dec(bp);
169
Dave Chinnera4082352013-08-28 10:18:06 +1000170 atomic_set(&bp->b_lru_ref, 0);
171 if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
Dave Chinnere80dfa12013-08-28 10:18:05 +1000172 (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
173 atomic_dec(&bp->b_hold);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100174
Dave Chinner430cbeb2010-12-02 16:30:55 +1100175 ASSERT(atomic_read(&bp->b_hold) >= 1);
Dave Chinnera4082352013-08-28 10:18:06 +1000176 spin_unlock(&bp->b_lock);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100177}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Dave Chinner3e85c862012-06-22 18:50:09 +1000179static int
180xfs_buf_get_maps(
181 struct xfs_buf *bp,
182 int map_count)
183{
184 ASSERT(bp->b_maps == NULL);
185 bp->b_map_count = map_count;
186
187 if (map_count == 1) {
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600188 bp->b_maps = &bp->__b_map;
Dave Chinner3e85c862012-06-22 18:50:09 +1000189 return 0;
190 }
191
192 bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
193 KM_NOFS);
194 if (!bp->b_maps)
Dave Chinner24513372014-06-25 14:58:08 +1000195 return -ENOMEM;
Dave Chinner3e85c862012-06-22 18:50:09 +1000196 return 0;
197}
198
199/*
200 * Frees b_pages if it was allocated.
201 */
202static void
203xfs_buf_free_maps(
204 struct xfs_buf *bp)
205{
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600206 if (bp->b_maps != &bp->__b_map) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000207 kmem_free(bp->b_maps);
208 bp->b_maps = NULL;
209 }
210}
211
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000212struct xfs_buf *
Dave Chinner3e85c862012-06-22 18:50:09 +1000213_xfs_buf_alloc(
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000214 struct xfs_buftarg *target,
Dave Chinner3e85c862012-06-22 18:50:09 +1000215 struct xfs_buf_map *map,
216 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100217 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000219 struct xfs_buf *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000220 int error;
221 int i;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000222
Dave Chinneraa5c1582012-04-23 15:58:56 +1000223 bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000224 if (unlikely(!bp))
225 return NULL;
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /*
Dave Chinner12bcb3f2012-04-23 15:59:05 +1000228 * We don't want certain flags to appear in b_flags unless they are
229 * specifically set by later operations on the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
Dave Chinner611c9942012-04-23 15:59:07 +1000231 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Nathan Scottce8e9222006-01-11 15:39:08 +1100233 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100234 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000235 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100236 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100237 INIT_LIST_HEAD(&bp->b_list);
Thomas Gleixnera731cd12010-09-07 14:33:15 +0000238 sema_init(&bp->b_sema, 0); /* held, no waiters */
Dave Chinnera4082352013-08-28 10:18:06 +1000239 spin_lock_init(&bp->b_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100240 XB_SET_OWNER(bp);
241 bp->b_target = target;
Dave Chinner3e85c862012-06-22 18:50:09 +1000242 bp->b_flags = flags;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000245 * Set length and io_length to the same value initially.
246 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * most cases but may be reset (e.g. XFS recovery).
248 */
Dave Chinner3e85c862012-06-22 18:50:09 +1000249 error = xfs_buf_get_maps(bp, nmaps);
250 if (error) {
251 kmem_zone_free(xfs_buf_zone, bp);
252 return NULL;
253 }
254
255 bp->b_bn = map[0].bm_bn;
256 bp->b_length = 0;
257 for (i = 0; i < nmaps; i++) {
258 bp->b_maps[i].bm_bn = map[i].bm_bn;
259 bp->b_maps[i].bm_len = map[i].bm_len;
260 bp->b_length += map[i].bm_len;
261 }
262 bp->b_io_length = bp->b_length;
263
Nathan Scottce8e9222006-01-11 15:39:08 +1100264 atomic_set(&bp->b_pin_count, 0);
265 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100267 XFS_STATS_INC(target->bt_mount, xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000268 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000269
270 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100274 * Allocate a page array capable of holding a specified number
275 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
277STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100278_xfs_buf_get_pages(
279 xfs_buf_t *bp,
Eric Sandeen87937bf2014-04-14 19:01:20 +1000280 int page_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100283 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100284 bp->b_page_count = page_count;
285 if (page_count <= XB_PAGES) {
286 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100288 bp->b_pages = kmem_alloc(sizeof(struct page *) *
Dave Chinneraa5c1582012-04-23 15:58:56 +1000289 page_count, KM_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +1100290 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -ENOMEM;
292 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100293 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295 return 0;
296}
297
298/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100299 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 */
301STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100302_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 xfs_buf_t *bp)
304{
Nathan Scottce8e9222006-01-11 15:39:08 +1100305 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000306 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000307 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309}
310
311/*
312 * Releases the specified buffer.
313 *
314 * The modification state of any associated pages is left unchanged.
Zhi Yong Wub46fe822013-08-07 10:10:59 +0000315 * The buffer must not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * hashed and refcounted buffers
317 */
318void
Nathan Scottce8e9222006-01-11 15:39:08 +1100319xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 xfs_buf_t *bp)
321{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000322 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Dave Chinner430cbeb2010-12-02 16:30:55 +1100324 ASSERT(list_empty(&bp->b_lru));
325
Dave Chinner0e6e8472011-03-26 09:16:45 +1100326 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 uint i;
328
James Bottomley73c77e22010-01-25 11:42:24 -0600329 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000330 vm_unmap_ram(bp->b_addr - bp->b_offset,
331 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Nathan Scott948ecdb2006-09-28 11:03:13 +1000333 for (i = 0; i < bp->b_page_count; i++) {
334 struct page *page = bp->b_pages[i];
335
Dave Chinner0e6e8472011-03-26 09:16:45 +1100336 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000337 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100338 } else if (bp->b_flags & _XBF_KMEM)
339 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000340 _xfs_buf_free_pages(bp);
Dave Chinner3e85c862012-06-22 18:50:09 +1000341 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000342 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100346 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
348STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100349xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 xfs_buf_t *bp,
351 uint flags)
352{
Dave Chinneraa0e8832012-04-23 15:58:52 +1000353 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100355 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 unsigned short page_count, i;
Dave Chinner795cac72012-04-23 15:58:53 +1000357 xfs_off_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 int error;
359
Dave Chinner0e6e8472011-03-26 09:16:45 +1100360 /*
361 * for buffers that are contained within a single page, just allocate
362 * the memory from the heap - there's no need for the complexity of
363 * page arrays to keep allocation down to order 0.
364 */
Dave Chinner795cac72012-04-23 15:58:53 +1000365 size = BBTOB(bp->b_length);
366 if (size < PAGE_SIZE) {
Dave Chinneraa5c1582012-04-23 15:58:56 +1000367 bp->b_addr = kmem_alloc(size, KM_NOFS);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100368 if (!bp->b_addr) {
369 /* low memory - use alloc_page loop instead */
370 goto use_alloc_page;
371 }
372
Dave Chinner795cac72012-04-23 15:58:53 +1000373 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100374 ((unsigned long)bp->b_addr & PAGE_MASK)) {
375 /* b_addr spans two pages - use alloc_page instead */
376 kmem_free(bp->b_addr);
377 bp->b_addr = NULL;
378 goto use_alloc_page;
379 }
380 bp->b_offset = offset_in_page(bp->b_addr);
381 bp->b_pages = bp->b_page_array;
382 bp->b_pages[0] = virt_to_page(bp->b_addr);
383 bp->b_page_count = 1;
Dave Chinner611c9942012-04-23 15:59:07 +1000384 bp->b_flags |= _XBF_KMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100385 return 0;
386 }
387
388use_alloc_page:
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600389 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
390 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000391 >> PAGE_SHIFT;
Dave Chinner795cac72012-04-23 15:58:53 +1000392 page_count = end - start;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000393 error = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (unlikely(error))
395 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Nathan Scottce8e9222006-01-11 15:39:08 +1100397 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100398 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Nathan Scottce8e9222006-01-11 15:39:08 +1100400 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct page *page;
402 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100403retry:
404 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100406 if (flags & XBF_READ_AHEAD) {
407 bp->b_page_count = i;
Dave Chinner24513372014-06-25 14:58:08 +1000408 error = -ENOMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100409 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
412 /*
413 * This could deadlock.
414 *
415 * But until all the XFS lowlevel code is revamped to
416 * handle buffer allocation failures we can't do much.
417 */
418 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100419 xfs_err(NULL,
Tetsuo Handa5bf97b12015-10-12 15:41:29 +1100420 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
421 current->comm, current->pid,
Harvey Harrison34a622b2008-04-10 12:19:21 +1000422 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100424 XFS_STATS_INC(bp->b_target->bt_mount, xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200425 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 goto retry;
427 }
428
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100429 XFS_STATS_INC(bp->b_target->bt_mount, xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Dave Chinner0e6e8472011-03-26 09:16:45 +1100431 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100433 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 offset = 0;
435 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Dave Chinner0e6e8472011-03-26 09:16:45 +1100438out_free_pages:
439 for (i = 0; i < bp->b_page_count; i++)
440 __free_page(bp->b_pages[i]);
Darrick J. Wong2aa6ba7b2017-01-25 20:24:57 -0800441 bp->b_flags &= ~_XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return error;
443}
444
445/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300446 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
448STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100449_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 xfs_buf_t *bp,
451 uint flags)
452{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100453 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100454 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100455 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100456 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
Dave Chinner611c9942012-04-23 15:59:07 +1000457 } else if (flags & XBF_UNMAPPED) {
458 bp->b_addr = NULL;
459 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100460 int retried = 0;
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700461 unsigned nofs_flag;
Dave Chinnera19fb382011-03-26 09:13:42 +1100462
Dave Chinnerae687e52014-03-07 16:19:14 +1100463 /*
464 * vm_map_ram() will allocate auxillary structures (e.g.
465 * pagetables) with GFP_KERNEL, yet we are likely to be under
466 * GFP_NOFS context here. Hence we need to tell memory reclaim
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700467 * that we are in such a context via PF_MEMALLOC_NOFS to prevent
Dave Chinnerae687e52014-03-07 16:19:14 +1100468 * memory reclaim re-entering the filesystem here and
469 * potentially deadlocking.
470 */
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700471 nofs_flag = memalloc_nofs_save();
Dave Chinnera19fb382011-03-26 09:13:42 +1100472 do {
473 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
474 -1, PAGE_KERNEL);
475 if (bp->b_addr)
476 break;
477 vm_unmap_aliases();
478 } while (retried++ <= 1);
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700479 memalloc_nofs_restore(nofs_flag);
Dave Chinnera19fb382011-03-26 09:13:42 +1100480
481 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100483 bp->b_addr += bp->b_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
486 return 0;
487}
488
489/*
490 * Finding and Reading Buffers
491 */
Lucas Stach6031e732016-12-07 17:36:36 +1100492static int
493_xfs_buf_obj_cmp(
494 struct rhashtable_compare_arg *arg,
495 const void *obj)
496{
497 const struct xfs_buf_map *map = arg->key;
498 const struct xfs_buf *bp = obj;
499
500 /*
501 * The key hashing in the lookup path depends on the key being the
502 * first element of the compare_arg, make sure to assert this.
503 */
504 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
505
506 if (bp->b_bn != map->bm_bn)
507 return 1;
508
509 if (unlikely(bp->b_length != map->bm_len)) {
510 /*
511 * found a block number match. If the range doesn't
512 * match, the only way this is allowed is if the buffer
513 * in the cache is stale and the transaction that made
514 * it stale has not yet committed. i.e. we are
515 * reallocating a busy extent. Skip this buffer and
516 * continue searching for an exact match.
517 */
518 ASSERT(bp->b_flags & XBF_STALE);
519 return 1;
520 }
521 return 0;
522}
523
524static const struct rhashtable_params xfs_buf_hash_params = {
525 .min_size = 32, /* empty AGs have minimal footprint */
526 .nelem_hint = 16,
527 .key_len = sizeof(xfs_daddr_t),
528 .key_offset = offsetof(struct xfs_buf, b_bn),
529 .head_offset = offsetof(struct xfs_buf, b_rhash_head),
530 .automatic_shrinking = true,
531 .obj_cmpfn = _xfs_buf_obj_cmp,
532};
533
534int
535xfs_buf_hash_init(
536 struct xfs_perag *pag)
537{
538 spin_lock_init(&pag->pag_buf_lock);
539 return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
540}
541
542void
543xfs_buf_hash_destroy(
544 struct xfs_perag *pag)
545{
546 rhashtable_destroy(&pag->pag_buf_hash);
547}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100550 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 * a given range of an inode. The buffer is returned
Chandra Seetharamaneabbaf12011-09-08 20:18:50 +0000552 * locked. No I/O is implied by this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 */
554xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100555_xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000556 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000557 struct xfs_buf_map *map,
558 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100559 xfs_buf_flags_t flags,
560 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Dave Chinner74f75a02010-09-24 19:59:04 +1000562 struct xfs_perag *pag;
Dave Chinner74f75a02010-09-24 19:59:04 +1000563 xfs_buf_t *bp;
Lucas Stach6031e732016-12-07 17:36:36 +1100564 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
Dave Chinner10616b802013-01-21 23:53:52 +1100565 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000566 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Dave Chinner3e85c862012-06-22 18:50:09 +1000568 for (i = 0; i < nmaps; i++)
Lucas Stach6031e732016-12-07 17:36:36 +1100569 cmap.bm_len += map[i].bm_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /* Check for IOs smaller than the sector size / not sector aligned */
Lucas Stach6031e732016-12-07 17:36:36 +1100572 ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
573 ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Dave Chinner10616b802013-01-21 23:53:52 +1100575 /*
576 * Corrupted block numbers can get through to here, unfortunately, so we
577 * have to check that the buffer falls within the filesystem bounds.
578 */
579 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
Lucas Stach6031e732016-12-07 17:36:36 +1100580 if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
Dave Chinner10616b802013-01-21 23:53:52 +1100581 /*
Dave Chinner24513372014-06-25 14:58:08 +1000582 * XXX (dgc): we should really be returning -EFSCORRUPTED here,
Dave Chinner10616b802013-01-21 23:53:52 +1100583 * but none of the higher level infrastructure supports
584 * returning a specific error on buffer lookup failures.
585 */
586 xfs_alert(btp->bt_mount,
587 "%s: Block out of range: block 0x%llx, EOFS 0x%llx ",
Lucas Stach6031e732016-12-07 17:36:36 +1100588 __func__, cmap.bm_bn, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000589 WARN_ON(1);
Dave Chinner10616b802013-01-21 23:53:52 +1100590 return NULL;
591 }
592
Dave Chinner74f75a02010-09-24 19:59:04 +1000593 pag = xfs_perag_get(btp->bt_mount,
Lucas Stach6031e732016-12-07 17:36:36 +1100594 xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Dave Chinner74f75a02010-09-24 19:59:04 +1000596 spin_lock(&pag->pag_buf_lock);
Lucas Stach6031e732016-12-07 17:36:36 +1100597 bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
598 xfs_buf_hash_params);
599 if (bp) {
600 atomic_inc(&bp->b_hold);
601 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
604 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100605 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000606 /* the buffer keeps the perag reference until it is freed */
607 new_bp->b_pag = pag;
Lucas Stach6031e732016-12-07 17:36:36 +1100608 rhashtable_insert_fast(&pag->pag_buf_hash,
609 &new_bp->b_rhash_head,
610 xfs_buf_hash_params);
Dave Chinner74f75a02010-09-24 19:59:04 +1000611 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 } else {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100613 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000614 spin_unlock(&pag->pag_buf_lock);
615 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100617 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000620 spin_unlock(&pag->pag_buf_lock);
621 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200623 if (!xfs_buf_trylock(bp)) {
624 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100625 xfs_buf_rele(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100626 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
Nathan Scottce8e9222006-01-11 15:39:08 +1100627 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200629 xfs_buf_lock(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100630 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
Dave Chinner0e6e8472011-03-26 09:16:45 +1100633 /*
634 * if the buffer is stale, clear all the external state associated with
635 * it. We need to keep flags such as how we allocated the buffer memory
636 * intact here.
637 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100638 if (bp->b_flags & XBF_STALE) {
639 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinnercfb02852012-11-12 22:54:19 +1100640 ASSERT(bp->b_iodone == NULL);
Dave Chinner611c9942012-04-23 15:59:07 +1000641 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100642 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000643 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000644
645 trace_xfs_buf_find(bp, flags, _RET_IP_);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100646 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
Nathan Scottce8e9222006-01-11 15:39:08 +1100647 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650/*
Dave Chinner38158322011-09-30 04:45:02 +0000651 * Assembles a buffer covering the specified range. The code is optimised for
652 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
653 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 */
Dave Chinner38158322011-09-30 04:45:02 +0000655struct xfs_buf *
Dave Chinner6dde2702012-06-22 18:50:10 +1000656xfs_buf_get_map(
657 struct xfs_buftarg *target,
658 struct xfs_buf_map *map,
659 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100660 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Dave Chinner38158322011-09-30 04:45:02 +0000662 struct xfs_buf *bp;
663 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100664 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Dave Chinner6dde2702012-06-22 18:50:10 +1000666 bp = _xfs_buf_find(target, map, nmaps, flags, NULL);
Dave Chinner38158322011-09-30 04:45:02 +0000667 if (likely(bp))
668 goto found;
669
Dave Chinner6dde2702012-06-22 18:50:10 +1000670 new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100671 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return NULL;
673
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000674 error = xfs_buf_allocate_memory(new_bp, flags);
675 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000676 xfs_buf_free(new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000677 return NULL;
678 }
679
Dave Chinner6dde2702012-06-22 18:50:10 +1000680 bp = _xfs_buf_find(target, map, nmaps, flags, new_bp);
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000681 if (!bp) {
682 xfs_buf_free(new_bp);
683 return NULL;
684 }
685
686 if (bp != new_bp)
687 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Dave Chinner38158322011-09-30 04:45:02 +0000689found:
Dave Chinner611c9942012-04-23 15:59:07 +1000690 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100691 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100693 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500694 "%s: failed to map pagesn", __func__);
Dave Chinnera8acad72012-04-23 15:58:54 +1000695 xfs_buf_relse(bp);
696 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698 }
699
Dave Chinnerb79f4a12016-01-12 07:03:44 +1100700 /*
701 * Clear b_error if this is a lookup from a caller that doesn't expect
702 * valid data to be found in the buffer.
703 */
704 if (!(flags & XBF_READ))
705 xfs_buf_ioerror(bp, 0);
706
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100707 XFS_STATS_INC(target->bt_mount, xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000708 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100709 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100712STATIC int
713_xfs_buf_read(
714 xfs_buf_t *bp,
715 xfs_buf_flags_t flags)
716{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000717 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600718 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100719
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000720 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200721 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100722
Dave Chinner595bff72014-10-02 09:05:14 +1000723 if (flags & XBF_ASYNC) {
724 xfs_buf_submit(bp);
Dave Chinner0e95f192012-04-23 15:58:46 +1000725 return 0;
Dave Chinner595bff72014-10-02 09:05:14 +1000726 }
727 return xfs_buf_submit_wait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730xfs_buf_t *
Dave Chinner6dde2702012-06-22 18:50:10 +1000731xfs_buf_read_map(
732 struct xfs_buftarg *target,
733 struct xfs_buf_map *map,
734 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100735 xfs_buf_flags_t flags,
Dave Chinner1813dd62012-11-14 17:54:40 +1100736 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Dave Chinner6dde2702012-06-22 18:50:10 +1000738 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Nathan Scottce8e9222006-01-11 15:39:08 +1100740 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Dave Chinner6dde2702012-06-22 18:50:10 +1000742 bp = xfs_buf_get_map(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100743 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000744 trace_xfs_buf_read(bp, flags, _RET_IP_);
745
Dave Chinnerb0388bf2016-02-10 15:01:11 +1100746 if (!(bp->b_flags & XBF_DONE)) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100747 XFS_STATS_INC(target->bt_mount, xb_get_read);
Dave Chinner1813dd62012-11-14 17:54:40 +1100748 bp->b_ops = ops;
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100749 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100750 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /*
752 * Read ahead call which is already satisfied,
753 * drop the buffer
754 */
Dave Chinnera8acad72012-04-23 15:58:54 +1000755 xfs_buf_relse(bp);
756 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100759 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761 }
762
Nathan Scottce8e9222006-01-11 15:39:08 +1100763 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100767 * If we are not low on memory then do the readahead in a deadlock
768 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
770void
Dave Chinner6dde2702012-06-22 18:50:10 +1000771xfs_buf_readahead_map(
772 struct xfs_buftarg *target,
773 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100774 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100775 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Jan Karaefa7c9f2017-02-02 15:56:53 +0100777 if (bdi_read_congested(target->bt_bdev->bd_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return;
779
Dave Chinner6dde2702012-06-22 18:50:10 +1000780 xfs_buf_read_map(target, map, nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100781 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Dave Chinner5adc94c2010-09-24 21:58:31 +1000784/*
785 * Read an uncached buffer from disk. Allocates and returns a locked
786 * buffer containing the disk contents or nothing.
787 */
Dave Chinnerba372672014-10-02 09:05:32 +1000788int
Dave Chinner5adc94c2010-09-24 21:58:31 +1000789xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000790 struct xfs_buftarg *target,
791 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000792 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100793 int flags,
Dave Chinnerba372672014-10-02 09:05:32 +1000794 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100795 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000796{
Dave Chinnereab4e632012-11-12 22:54:02 +1100797 struct xfs_buf *bp;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000798
Dave Chinnerba372672014-10-02 09:05:32 +1000799 *bpp = NULL;
800
Dave Chinnere70b73f2012-04-23 15:58:49 +1000801 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000802 if (!bp)
Dave Chinnerba372672014-10-02 09:05:32 +1000803 return -ENOMEM;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000804
805 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000806 ASSERT(bp->b_map_count == 1);
Dave Chinnerba372672014-10-02 09:05:32 +1000807 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
Dave Chinner3e85c862012-06-22 18:50:09 +1000808 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000809 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100810 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000811
Dave Chinner595bff72014-10-02 09:05:14 +1000812 xfs_buf_submit_wait(bp);
Dave Chinnerba372672014-10-02 09:05:32 +1000813 if (bp->b_error) {
814 int error = bp->b_error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800815 xfs_buf_relse(bp);
Dave Chinnerba372672014-10-02 09:05:32 +1000816 return error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800817 }
Dave Chinnerba372672014-10-02 09:05:32 +1000818
819 *bpp = bp;
820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Dave Chinner44396472011-04-21 09:34:27 +0000823/*
824 * Return a buffer allocated as an empty buffer and associated to external
825 * memory via xfs_buf_associate_memory() back to it's empty state.
826 */
827void
828xfs_buf_set_empty(
829 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000830 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000831{
832 if (bp->b_pages)
833 _xfs_buf_free_pages(bp);
834
835 bp->b_pages = NULL;
836 bp->b_page_count = 0;
837 bp->b_addr = NULL;
Dave Chinner4e94b712012-04-23 15:58:51 +1000838 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000839 bp->b_io_length = numblks;
Dave Chinner3e85c862012-06-22 18:50:09 +1000840
841 ASSERT(bp->b_map_count == 1);
Dave Chinner44396472011-04-21 09:34:27 +0000842 bp->b_bn = XFS_BUF_DADDR_NULL;
Dave Chinner3e85c862012-06-22 18:50:09 +1000843 bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL;
844 bp->b_maps[0].bm_len = bp->b_length;
Dave Chinner44396472011-04-21 09:34:27 +0000845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847static inline struct page *
848mem_to_page(
849 void *addr)
850{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800851 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return virt_to_page(addr);
853 } else {
854 return vmalloc_to_page(addr);
855 }
856}
857
858int
Nathan Scottce8e9222006-01-11 15:39:08 +1100859xfs_buf_associate_memory(
860 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 void *mem,
862 size_t len)
863{
864 int rval;
865 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100866 unsigned long pageaddr;
867 unsigned long offset;
868 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 int page_count;
870
Dave Chinner0e6e8472011-03-26 09:16:45 +1100871 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100872 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100873 buflen = PAGE_ALIGN(len + offset);
874 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100877 if (bp->b_pages)
878 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Nathan Scottce8e9222006-01-11 15:39:08 +1100880 bp->b_pages = NULL;
881 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Eric Sandeen87937bf2014-04-14 19:01:20 +1000883 rval = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (rval)
885 return rval;
886
Nathan Scottce8e9222006-01-11 15:39:08 +1100887 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100889 for (i = 0; i < bp->b_page_count; i++) {
890 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100891 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Dave Chinneraa0e8832012-04-23 15:58:52 +1000894 bp->b_io_length = BTOBB(len);
Dave Chinner4e94b712012-04-23 15:58:51 +1000895 bp->b_length = BTOBB(buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 return 0;
898}
899
900xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000901xfs_buf_get_uncached(
902 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000903 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000904 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000906 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000907 int error, i;
Dave Chinner3e85c862012-06-22 18:50:09 +1000908 struct xfs_buf *bp;
909 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Brian Fosterc891c302016-07-20 11:13:43 +1000911 /* flags might contain irrelevant bits, pass only what we care about */
912 bp = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (unlikely(bp == NULL))
914 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Dave Chinnere70b73f2012-04-23 15:58:49 +1000916 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000917 error = _xfs_buf_get_pages(bp, page_count);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000918 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 goto fail_free_buf;
920
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000921 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000922 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000923 if (!bp->b_pages[i])
924 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000926 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Dave Chinner611c9942012-04-23 15:59:07 +1000928 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000929 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100930 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500931 "%s: failed to map pages", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Dave Chinner686865f2010-09-24 20:07:47 +1000935 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000939 while (--i >= 0)
940 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000941 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 fail_free_buf:
Dave Chinner3e85c862012-06-22 18:50:09 +1000943 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000944 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 fail:
946 return NULL;
947}
948
949/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * Increment reference count on buffer, to hold the buffer concurrently
951 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 * Must hold the buffer already to call this function.
953 */
954void
Nathan Scottce8e9222006-01-11 15:39:08 +1100955xfs_buf_hold(
956 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000958 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100959 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
962/*
Brian Foster9c7504a2016-07-20 11:15:28 +1000963 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
964 * placed on LRU or freed (depending on b_lru_ref).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
966void
Nathan Scottce8e9222006-01-11 15:39:08 +1100967xfs_buf_rele(
968 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Dave Chinner74f75a02010-09-24 19:59:04 +1000970 struct xfs_perag *pag = bp->b_pag;
Brian Foster9c7504a2016-07-20 11:15:28 +1000971 bool release;
972 bool freebuf = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000974 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Dave Chinner74f75a02010-09-24 19:59:04 +1000976 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100977 ASSERT(list_empty(&bp->b_lru));
Brian Foster9c7504a2016-07-20 11:15:28 +1000978 if (atomic_dec_and_test(&bp->b_hold)) {
979 xfs_buf_ioacct_dec(bp);
Nathan Scottfad3aa12006-02-01 12:14:52 +1100980 xfs_buf_free(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +1000981 }
Nathan Scottfad3aa12006-02-01 12:14:52 +1100982 return;
983 }
984
Lachlan McIlroy37906892008-08-13 15:42:10 +1000985 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinnera4082352013-08-28 10:18:06 +1000986
Brian Foster9c7504a2016-07-20 11:15:28 +1000987 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
988 spin_lock(&bp->b_lock);
989 if (!release) {
990 /*
991 * Drop the in-flight state if the buffer is already on the LRU
992 * and it holds the only reference. This is racy because we
993 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
994 * ensures the decrement occurs only once per-buf.
995 */
996 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
Brian Foster63db7c82017-05-31 08:22:52 -0700997 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +1000998 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
Brian Foster9c7504a2016-07-20 11:15:28 +10001000
1001 /* the last reference has been dropped ... */
Brian Foster63db7c82017-05-31 08:22:52 -07001002 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001003 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1004 /*
1005 * If the buffer is added to the LRU take a new reference to the
1006 * buffer for the LRU and clear the (now stale) dispose list
1007 * state flag
1008 */
1009 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1010 bp->b_state &= ~XFS_BSTATE_DISPOSE;
1011 atomic_inc(&bp->b_hold);
1012 }
1013 spin_unlock(&pag->pag_buf_lock);
1014 } else {
1015 /*
1016 * most of the time buffers will already be removed from the
1017 * LRU, so optimise that case by checking for the
1018 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1019 * was on was the disposal list
1020 */
1021 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1022 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1023 } else {
1024 ASSERT(list_empty(&bp->b_lru));
1025 }
1026
1027 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Lucas Stach6031e732016-12-07 17:36:36 +11001028 rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
1029 xfs_buf_hash_params);
Brian Foster9c7504a2016-07-20 11:15:28 +10001030 spin_unlock(&pag->pag_buf_lock);
1031 xfs_perag_put(pag);
1032 freebuf = true;
1033 }
1034
1035out_unlock:
1036 spin_unlock(&bp->b_lock);
1037
1038 if (freebuf)
1039 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042
1043/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001044 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +11001045 *
1046 * If we come across a stale, pinned, locked buffer, we know that we are
1047 * being asked to lock a buffer that has been reallocated. Because it is
1048 * pinned, we know that the log has not been pushed to disk and hence it
1049 * will still be locked. Rather than continuing to have trylock attempts
1050 * fail until someone else pushes the log, push it ourselves before
1051 * returning. This means that the xfsaild will not get stuck trying
1052 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
1054int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001055xfs_buf_trylock(
1056 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 int locked;
1059
Nathan Scottce8e9222006-01-11 15:39:08 +11001060 locked = down_trylock(&bp->b_sema) == 0;
Darrick J. Wong479c6412016-06-21 11:53:28 +10001061 if (locked) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001062 XB_SET_OWNER(bp);
Darrick J. Wong479c6412016-06-21 11:53:28 +10001063 trace_xfs_buf_trylock(bp, _RET_IP_);
1064 } else {
1065 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
1066 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001067 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001071 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +10001072 *
1073 * If we come across a stale, pinned, locked buffer, we know that we
1074 * are being asked to lock a buffer that has been reallocated. Because
1075 * it is pinned, we know that the log has not been pushed to disk and
1076 * hence it will still be locked. Rather than sleeping until someone
1077 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001079void
1080xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001081 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001083 trace_xfs_buf_lock(bp, _RET_IP_);
1084
Dave Chinnered3b4d62010-05-21 12:07:08 +10001085 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +10001086 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +11001087 down(&bp->b_sema);
1088 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001089
1090 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093void
Nathan Scottce8e9222006-01-11 15:39:08 +11001094xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001095 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Brian Foster20e8a062017-04-21 12:40:44 -07001097 ASSERT(xfs_buf_islocked(bp));
1098
Nathan Scottce8e9222006-01-11 15:39:08 +11001099 XB_CLEAR_OWNER(bp);
1100 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001101
1102 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Nathan Scottce8e9222006-01-11 15:39:08 +11001105STATIC void
1106xfs_buf_wait_unpin(
1107 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 DECLARE_WAITQUEUE (wait, current);
1110
Nathan Scottce8e9222006-01-11 15:39:08 +11001111 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return;
1113
Nathan Scottce8e9222006-01-11 15:39:08 +11001114 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 for (;;) {
1116 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001117 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001119 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001121 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 set_current_state(TASK_RUNNING);
1123}
1124
1125/*
1126 * Buffer Utility Routines
1127 */
1128
Dave Chinnere8aaba92014-10-02 09:04:22 +10001129void
1130xfs_buf_ioend(
1131 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001133 bool read = bp->b_flags & XBF_READ;
1134
1135 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001136
1137 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Dave Chinnerd5929de2013-02-27 13:25:54 +11001138
Dave Chinner61be9c52014-10-02 09:04:31 +10001139 /*
1140 * Pull in IO completion errors now. We are guaranteed to be running
1141 * single threaded, so we don't need the lock to read b_io_error.
1142 */
1143 if (!bp->b_error && bp->b_io_error)
1144 xfs_buf_ioerror(bp, bp->b_io_error);
1145
Dave Chinnere8aaba92014-10-02 09:04:22 +10001146 /* Only validate buffers that were read without errors */
1147 if (read && !bp->b_error && bp->b_ops) {
1148 ASSERT(!bp->b_iodone);
Dave Chinner1813dd62012-11-14 17:54:40 +11001149 bp->b_ops->verify_read(bp);
Dave Chinnere8aaba92014-10-02 09:04:22 +10001150 }
1151
1152 if (!bp->b_error)
1153 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Christoph Hellwig80f6c292010-08-18 05:29:11 -04001155 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001156 (*(bp->b_iodone))(bp);
1157 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 xfs_buf_relse(bp);
Dave Chinner595bff72014-10-02 09:05:14 +10001159 else
Dave Chinner1813dd62012-11-14 17:54:40 +11001160 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Dave Chinnere8aaba92014-10-02 09:04:22 +10001163static void
1164xfs_buf_ioend_work(
1165 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001167 struct xfs_buf *bp =
Brian Fosterb29c70f2014-12-04 09:43:17 +11001168 container_of(work, xfs_buf_t, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001169
Dave Chinnere8aaba92014-10-02 09:04:22 +10001170 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001173static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001174xfs_buf_ioend_async(
1175 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001177 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
1178 queue_work(bp->b_ioend_wq, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181void
Nathan Scottce8e9222006-01-11 15:39:08 +11001182xfs_buf_ioerror(
1183 xfs_buf_t *bp,
1184 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Dave Chinner24513372014-06-25 14:58:08 +10001186 ASSERT(error <= 0 && error >= -1000);
1187 bp->b_error = error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001188 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
Christoph Hellwig901796a2011-10-10 16:52:49 +00001191void
1192xfs_buf_ioerror_alert(
1193 struct xfs_buf *bp,
1194 const char *func)
1195{
1196 xfs_alert(bp->b_target->bt_mount,
Dave Chinneraa0e8832012-04-23 15:58:52 +10001197"metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001198 (uint64_t)XFS_BUF_ADDR(bp), func, -bp->b_error, bp->b_length);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001199}
1200
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001201int
1202xfs_bwrite(
1203 struct xfs_buf *bp)
1204{
1205 int error;
1206
1207 ASSERT(xfs_buf_islocked(bp));
1208
1209 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001210 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
1211 XBF_WRITE_FAIL | XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001212
Dave Chinner595bff72014-10-02 09:05:14 +10001213 error = xfs_buf_submit_wait(bp);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001214 if (error) {
1215 xfs_force_shutdown(bp->b_target->bt_mount,
1216 SHUTDOWN_META_IO_ERROR);
1217 }
1218 return error;
1219}
1220
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001221static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001222xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001223 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001225 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Dave Chinner37eb17e2012-11-12 22:09:46 +11001227 /*
1228 * don't overwrite existing errors - otherwise we can lose errors on
1229 * buffers that require multiple bios to complete.
1230 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001231 if (bio->bi_status) {
1232 int error = blk_status_to_errno(bio->bi_status);
1233
1234 cmpxchg(&bp->b_io_error, 0, error);
1235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Dave Chinner37eb17e2012-11-12 22:09:46 +11001237 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001238 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1239
Dave Chinnere8aaba92014-10-02 09:04:22 +10001240 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1241 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Dave Chinner3e85c862012-06-22 18:50:09 +10001245static void
1246xfs_buf_ioapply_map(
1247 struct xfs_buf *bp,
1248 int map,
1249 int *buf_offset,
1250 int *count,
Mike Christie50bfcd02016-06-05 14:31:57 -05001251 int op,
1252 int op_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Dave Chinner3e85c862012-06-22 18:50:09 +10001254 int page_index;
1255 int total_nr_pages = bp->b_page_count;
1256 int nr_pages;
1257 struct bio *bio;
1258 sector_t sector = bp->b_maps[map].bm_bn;
1259 int size;
1260 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Dave Chinner3e85c862012-06-22 18:50:09 +10001262 /* skip the pages in the buffer before the start offset */
1263 page_index = 0;
1264 offset = *buf_offset;
1265 while (offset >= PAGE_SIZE) {
1266 page_index++;
1267 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001268 }
1269
Dave Chinner3e85c862012-06-22 18:50:09 +10001270 /*
1271 * Limit the IO size to the length of the current vector, and update the
1272 * remaining IO count for the next time around.
1273 */
1274 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1275 *count -= size;
1276 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001279 atomic_inc(&bp->b_io_remaining);
Ming Leic908e382016-05-30 21:34:33 +08001280 nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 bio = bio_alloc(GFP_NOIO, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001283 bio_set_dev(bio, bp->b_target->bt_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001284 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001285 bio->bi_end_io = xfs_buf_bio_end_io;
1286 bio->bi_private = bp;
Mike Christie50bfcd02016-06-05 14:31:57 -05001287 bio_set_op_attrs(bio, op, op_flags);
Dave Chinner0e6e8472011-03-26 09:16:45 +11001288
Dave Chinner3e85c862012-06-22 18:50:09 +10001289 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001290 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 if (nbytes > size)
1293 nbytes = size;
1294
Dave Chinner3e85c862012-06-22 18:50:09 +10001295 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1296 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001297 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 break;
1299
1300 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001301 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 size -= nbytes;
1303 total_nr_pages--;
1304 }
1305
Kent Overstreet4f024f32013-10-11 15:44:27 -07001306 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001307 if (xfs_buf_is_vmapped(bp)) {
1308 flush_kernel_vmap_range(bp->b_addr,
1309 xfs_buf_vmap_len(bp));
1310 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001311 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (size)
1313 goto next_chunk;
1314 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001315 /*
1316 * This is guaranteed not to be the last io reference count
Dave Chinner595bff72014-10-02 09:05:14 +10001317 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001318 */
1319 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001320 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001321 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001323
1324}
1325
1326STATIC void
1327_xfs_buf_ioapply(
1328 struct xfs_buf *bp)
1329{
1330 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001331 int op;
1332 int op_flags = 0;
Dave Chinner3e85c862012-06-22 18:50:09 +10001333 int offset;
1334 int size;
1335 int i;
1336
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001337 /*
1338 * Make sure we capture only current IO errors rather than stale errors
1339 * left over from previous use of the buffer (e.g. failed readahead).
1340 */
1341 bp->b_error = 0;
1342
Brian Fosterb29c70f2014-12-04 09:43:17 +11001343 /*
1344 * Initialize the I/O completion workqueue if we haven't yet or the
1345 * submitter has not opted to specify a custom one.
1346 */
1347 if (!bp->b_ioend_wq)
1348 bp->b_ioend_wq = bp->b_target->bt_mount->m_buf_workqueue;
1349
Dave Chinner3e85c862012-06-22 18:50:09 +10001350 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001351 op = REQ_OP_WRITE;
Dave Chinner3e85c862012-06-22 18:50:09 +10001352 if (bp->b_flags & XBF_SYNCIO)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001353 op_flags = REQ_SYNC;
Dave Chinner3e85c862012-06-22 18:50:09 +10001354 if (bp->b_flags & XBF_FUA)
Mike Christie50bfcd02016-06-05 14:31:57 -05001355 op_flags |= REQ_FUA;
Dave Chinner3e85c862012-06-22 18:50:09 +10001356 if (bp->b_flags & XBF_FLUSH)
Mike Christie28a8f0d2016-06-05 14:32:25 -05001357 op_flags |= REQ_PREFLUSH;
Dave Chinner1813dd62012-11-14 17:54:40 +11001358
1359 /*
1360 * Run the write verifier callback function if it exists. If
1361 * this function fails it will mark the buffer with an error and
1362 * the IO should not be dispatched.
1363 */
1364 if (bp->b_ops) {
1365 bp->b_ops->verify_write(bp);
1366 if (bp->b_error) {
1367 xfs_force_shutdown(bp->b_target->bt_mount,
1368 SHUTDOWN_CORRUPT_INCORE);
1369 return;
1370 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001371 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
1372 struct xfs_mount *mp = bp->b_target->bt_mount;
1373
1374 /*
1375 * non-crc filesystems don't attach verifiers during
1376 * log recovery, so don't warn for such filesystems.
1377 */
1378 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1379 xfs_warn(mp,
1380 "%s: no ops on block 0x%llx/0x%x",
1381 __func__, bp->b_bn, bp->b_length);
1382 xfs_hex_dump(bp->b_addr, 64);
1383 dump_stack();
1384 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001385 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001386 } else if (bp->b_flags & XBF_READ_AHEAD) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001387 op = REQ_OP_READ;
1388 op_flags = REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001389 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001390 op = REQ_OP_READ;
Dave Chinner3e85c862012-06-22 18:50:09 +10001391 }
1392
1393 /* we only use the buffer cache for meta-data */
Mike Christie50bfcd02016-06-05 14:31:57 -05001394 op_flags |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001395
1396 /*
1397 * Walk all the vectors issuing IO on them. Set up the initial offset
1398 * into the buffer and the desired IO size before we start -
1399 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1400 * subsequent call.
1401 */
1402 offset = bp->b_offset;
1403 size = BBTOB(bp->b_io_length);
1404 blk_start_plug(&plug);
1405 for (i = 0; i < bp->b_map_count; i++) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001406 xfs_buf_ioapply_map(bp, i, &offset, &size, op, op_flags);
Dave Chinner3e85c862012-06-22 18:50:09 +10001407 if (bp->b_error)
1408 break;
1409 if (size <= 0)
1410 break; /* all done */
1411 }
1412 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Dave Chinner595bff72014-10-02 09:05:14 +10001415/*
1416 * Asynchronous IO submission path. This transfers the buffer lock ownership and
1417 * the current reference to the IO. It is not safe to reference the buffer after
1418 * a call to this function unless the caller holds an additional reference
1419 * itself.
1420 */
Dave Chinner0e95f192012-04-23 15:58:46 +10001421void
Dave Chinner595bff72014-10-02 09:05:14 +10001422xfs_buf_submit(
1423 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Dave Chinner595bff72014-10-02 09:05:14 +10001425 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001427 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff72014-10-02 09:05:14 +10001428 ASSERT(bp->b_flags & XBF_ASYNC);
1429
1430 /* on shutdown we stale and complete the buffer immediately */
1431 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
1432 xfs_buf_ioerror(bp, -EIO);
1433 bp->b_flags &= ~XBF_DONE;
1434 xfs_buf_stale(bp);
1435 xfs_buf_ioend(bp);
1436 return;
1437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Christoph Hellwig375ec692011-08-23 08:28:03 +00001439 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001440 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Dave Chinner61be9c52014-10-02 09:04:31 +10001442 /* clear the internal error state to avoid spurious errors */
1443 bp->b_io_error = 0;
1444
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001445 /*
Dave Chinner595bff72014-10-02 09:05:14 +10001446 * The caller's reference is released during I/O completion.
1447 * This occurs some time after the last b_io_remaining reference is
1448 * released, so after we drop our Io reference we have to have some
1449 * other reference to ensure the buffer doesn't go away from underneath
1450 * us. Take a direct reference to ensure we have safe access to the
1451 * buffer until we are finished with it.
Dave Chinnere11bb802014-10-02 09:04:11 +10001452 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 xfs_buf_hold(bp);
1454
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001455 /*
Dave Chinnere11bb802014-10-02 09:04:11 +10001456 * Set the count to 1 initially, this will stop an I/O completion
1457 * callout which happens before we have started all the I/O from calling
1458 * xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001460 atomic_set(&bp->b_io_remaining, 1);
Brian Foster9c7504a2016-07-20 11:15:28 +10001461 xfs_buf_ioacct_inc(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001462 _xfs_buf_ioapply(bp);
Dave Chinnere11bb802014-10-02 09:04:11 +10001463
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001464 /*
Dave Chinner595bff72014-10-02 09:05:14 +10001465 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1466 * reference we took above. If we drop it to zero, run completion so
1467 * that we don't return to the caller with completion still pending.
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001468 */
Dave Chinnere8aaba92014-10-02 09:04:22 +10001469 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
Dave Chinner595bff72014-10-02 09:05:14 +10001470 if (bp->b_error)
Dave Chinnere8aaba92014-10-02 09:04:22 +10001471 xfs_buf_ioend(bp);
1472 else
1473 xfs_buf_ioend_async(bp);
1474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Nathan Scottce8e9222006-01-11 15:39:08 +11001476 xfs_buf_rele(bp);
Dave Chinner595bff72014-10-02 09:05:14 +10001477 /* Note: it is not safe to reference bp now we've dropped our ref */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480/*
Dave Chinner595bff72014-10-02 09:05:14 +10001481 * Synchronous buffer IO submission path, read or write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 */
1483int
Dave Chinner595bff72014-10-02 09:05:14 +10001484xfs_buf_submit_wait(
1485 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Dave Chinner595bff72014-10-02 09:05:14 +10001487 int error;
1488
1489 trace_xfs_buf_submit_wait(bp, _RET_IP_);
1490
1491 ASSERT(!(bp->b_flags & (_XBF_DELWRI_Q | XBF_ASYNC)));
1492
1493 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
1494 xfs_buf_ioerror(bp, -EIO);
1495 xfs_buf_stale(bp);
1496 bp->b_flags &= ~XBF_DONE;
1497 return -EIO;
1498 }
1499
1500 if (bp->b_flags & XBF_WRITE)
1501 xfs_buf_wait_unpin(bp);
1502
1503 /* clear the internal error state to avoid spurious errors */
1504 bp->b_io_error = 0;
1505
1506 /*
1507 * For synchronous IO, the IO does not inherit the submitters reference
1508 * count, nor the buffer lock. Hence we cannot release the reference we
1509 * are about to take until we've waited for all IO completion to occur,
1510 * including any xfs_buf_ioend_async() work that may be pending.
1511 */
1512 xfs_buf_hold(bp);
1513
1514 /*
1515 * Set the count to 1 initially, this will stop an I/O completion
1516 * callout which happens before we have started all the I/O from calling
1517 * xfs_buf_ioend too early.
1518 */
1519 atomic_set(&bp->b_io_remaining, 1);
1520 _xfs_buf_ioapply(bp);
1521
1522 /*
1523 * make sure we run completion synchronously if it raced with us and is
1524 * already complete.
1525 */
1526 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1527 xfs_buf_ioend(bp);
1528
1529 /* wait for completion before gathering the error from the buffer */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001530 trace_xfs_buf_iowait(bp, _RET_IP_);
Dave Chinner595bff72014-10-02 09:05:14 +10001531 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001532 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Dave Chinner595bff72014-10-02 09:05:14 +10001533 error = bp->b_error;
1534
1535 /*
1536 * all done now, we can release the hold that keeps the buffer
1537 * referenced for the entire IO.
1538 */
1539 xfs_buf_rele(bp);
1540 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001543void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001544xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001545 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 size_t offset)
1547{
1548 struct page *page;
1549
Dave Chinner611c9942012-04-23 15:59:07 +10001550 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001551 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Nathan Scottce8e9222006-01-11 15:39:08 +11001553 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001554 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001555 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
1558/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 * Move data into or out of a buffer.
1560 */
1561void
Nathan Scottce8e9222006-01-11 15:39:08 +11001562xfs_buf_iomove(
1563 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 size_t boff, /* starting buffer offset */
1565 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001566 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001567 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Dave Chinner795cac72012-04-23 15:58:53 +10001569 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 bend = boff + bsize;
1572 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001573 struct page *page;
1574 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Dave Chinner795cac72012-04-23 15:58:53 +10001576 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1577 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1578 page = bp->b_pages[page_index];
1579 csize = min_t(size_t, PAGE_SIZE - page_offset,
1580 BBTOB(bp->b_io_length) - boff);
1581
1582 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001585 case XBRW_ZERO:
Dave Chinner795cac72012-04-23 15:58:53 +10001586 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001588 case XBRW_READ:
Dave Chinner795cac72012-04-23 15:58:53 +10001589 memcpy(data, page_address(page) + page_offset, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001591 case XBRW_WRITE:
Dave Chinner795cac72012-04-23 15:58:53 +10001592 memcpy(page_address(page) + page_offset, data, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
1594
1595 boff += csize;
1596 data += csize;
1597 }
1598}
1599
1600/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001601 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 */
1603
1604/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001605 * Wait for any bufs with callbacks that have been submitted but have not yet
1606 * returned. These buffers will have an elevated hold count, so wait on those
1607 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001609static enum lru_status
1610xfs_buftarg_wait_rele(
1611 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001612 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001613 spinlock_t *lru_lock,
1614 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Dave Chinnere80dfa12013-08-28 10:18:05 +10001616{
1617 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001618 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001619
1620 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001621 /* need to wait, so skip it this pass */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001622 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001623 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
Dave Chinnera4082352013-08-28 10:18:06 +10001625 if (!spin_trylock(&bp->b_lock))
1626 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001627
Dave Chinnera4082352013-08-28 10:18:06 +10001628 /*
1629 * clear the LRU reference count so the buffer doesn't get
1630 * ignored in xfs_buf_rele().
1631 */
1632 atomic_set(&bp->b_lru_ref, 0);
1633 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001634 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001635 spin_unlock(&bp->b_lock);
1636 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
Dave Chinnere80dfa12013-08-28 10:18:05 +10001639void
1640xfs_wait_buftarg(
1641 struct xfs_buftarg *btp)
1642{
Dave Chinnera4082352013-08-28 10:18:06 +10001643 LIST_HEAD(dispose);
1644 int loop = 0;
1645
Dave Chinner85bec542016-01-19 08:28:10 +11001646 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001647 * First wait on the buftarg I/O count for all in-flight buffers to be
1648 * released. This is critical as new buffers do not make the LRU until
1649 * they are released.
1650 *
1651 * Next, flush the buffer workqueue to ensure all completion processing
1652 * has finished. Just waiting on buffer locks is not sufficient for
1653 * async IO as the reference count held over IO is not released until
1654 * after the buffer lock is dropped. Hence we need to ensure here that
1655 * all reference counts have been dropped before we start walking the
1656 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001657 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001658 while (percpu_counter_sum(&btp->bt_io_count))
1659 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001660 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Dave Chinner85bec542016-01-19 08:28:10 +11001661
Dave Chinnera4082352013-08-28 10:18:06 +10001662 /* loop until there is nothing left on the lru list. */
1663 while (list_lru_count(&btp->bt_lru)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001664 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001665 &dispose, LONG_MAX);
1666
1667 while (!list_empty(&dispose)) {
1668 struct xfs_buf *bp;
1669 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1670 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001671 if (bp->b_flags & XBF_WRITE_FAIL) {
1672 xfs_alert(btp->bt_mount,
Joe Perchesf41febd2015-07-29 11:52:04 +10001673"Corruption Alert: Buffer at block 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001674 (long long)bp->b_bn);
Joe Perchesf41febd2015-07-29 11:52:04 +10001675 xfs_alert(btp->bt_mount,
1676"Please run xfs_repair to determine the extent of the problem.");
Dave Chinnerac8809f2013-12-12 16:34:38 +11001677 }
Dave Chinnera4082352013-08-28 10:18:06 +10001678 xfs_buf_rele(bp);
1679 }
1680 if (loop++ != 0)
1681 delay(100);
1682 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001683}
1684
1685static enum lru_status
1686xfs_buftarg_isolate(
1687 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001688 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001689 spinlock_t *lru_lock,
1690 void *arg)
1691{
1692 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1693 struct list_head *dispose = arg;
1694
1695 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001696 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1697 * If we fail to get the lock, just skip it.
1698 */
1699 if (!spin_trylock(&bp->b_lock))
1700 return LRU_SKIP;
1701 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001702 * Decrement the b_lru_ref count unless the value is already
1703 * zero. If the value is already zero, we need to reclaim the
1704 * buffer, otherwise it gets another trip through the LRU.
1705 */
Dave Chinnera4082352013-08-28 10:18:06 +10001706 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1707 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001708 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001709 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001710
Dave Chinnera4082352013-08-28 10:18:06 +10001711 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001712 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001713 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001714 return LRU_REMOVED;
1715}
1716
Andrew Mortonaddbda42013-08-28 10:18:06 +10001717static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001718xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001719 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001720 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001721{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001722 struct xfs_buftarg *btp = container_of(shrink,
1723 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001724 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001725 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001726
Vladimir Davydov503c3582015-02-12 14:58:47 -08001727 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1728 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001729
1730 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001731 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001732 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1733 list_del_init(&bp->b_lru);
1734 xfs_buf_rele(bp);
1735 }
1736
Dave Chinnere80dfa12013-08-28 10:18:05 +10001737 return freed;
1738}
1739
Andrew Mortonaddbda42013-08-28 10:18:06 +10001740static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001741xfs_buftarg_shrink_count(
1742 struct shrinker *shrink,
1743 struct shrink_control *sc)
1744{
1745 struct xfs_buftarg *btp = container_of(shrink,
1746 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001747 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001748}
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750void
1751xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001752 struct xfs_mount *mp,
1753 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001755 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001756 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1757 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001758 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001759
Dave Chinner2291dab2016-12-09 16:49:54 +11001760 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001761
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001762 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001765int
1766xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001768 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001770 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001771 btp->bt_meta_sectorsize = sectorsize;
1772 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Nathan Scottce8e9222006-01-11 15:39:08 +11001774 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001775 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001776 "Cannot set_blocksize to %u on device %pg",
1777 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001778 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001781 /* Set up device logical sector size mask */
1782 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1783 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return 0;
1786}
1787
1788/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001789 * When allocating the initial buffer target we have not yet
1790 * read in the superblock, so don't know what sized sectors
1791 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001792 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793STATIC int
1794xfs_setsize_buftarg_early(
1795 xfs_buftarg_t *btp,
1796 struct block_device *bdev)
1797{
Eric Sandeena96c4152014-04-14 19:00:29 +10001798 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801xfs_buftarg_t *
1802xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001803 struct xfs_mount *mp,
Dan Williams486aff52017-08-24 15:12:50 -07001804 struct block_device *bdev,
1805 struct dax_device *dax_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 xfs_buftarg_t *btp;
1808
Dave Chinnerb17cb362013-05-20 09:51:12 +10001809 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Dave Chinnerebad8612010-09-22 10:47:20 +10001811 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001812 btp->bt_dev = bdev->bd_dev;
1813 btp->bt_bdev = bdev;
Dan Williams486aff52017-08-24 15:12:50 -07001814 btp->bt_daxdev = dax_dev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (xfs_setsize_buftarg_early(btp, bdev))
1817 goto error;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001818
1819 if (list_lru_init(&btp->bt_lru))
1820 goto error;
1821
Brian Foster9c7504a2016-07-20 11:15:28 +10001822 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
1823 goto error;
1824
Dave Chinnere80dfa12013-08-28 10:18:05 +10001825 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1826 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001827 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001828 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001829 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return btp;
1831
1832error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001833 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return NULL;
1835}
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837/*
Brian Foster20e8a062017-04-21 12:40:44 -07001838 * Cancel a delayed write list.
1839 *
1840 * Remove each buffer from the list, clear the delwri queue flag and drop the
1841 * associated buffer reference.
1842 */
1843void
1844xfs_buf_delwri_cancel(
1845 struct list_head *list)
1846{
1847 struct xfs_buf *bp;
1848
1849 while (!list_empty(list)) {
1850 bp = list_first_entry(list, struct xfs_buf, b_list);
1851
1852 xfs_buf_lock(bp);
1853 bp->b_flags &= ~_XBF_DELWRI_Q;
1854 list_del_init(&bp->b_list);
1855 xfs_buf_relse(bp);
1856 }
1857}
1858
1859/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001860 * Add a buffer to the delayed write list.
1861 *
1862 * This queues a buffer for writeout if it hasn't already been. Note that
1863 * neither this routine nor the buffer list submission functions perform
1864 * any internal synchronization. It is expected that the lists are thread-local
1865 * to the callers.
1866 *
1867 * Returns true if we queued up the buffer, or false if it already had
1868 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001870bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001871xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001872 struct xfs_buf *bp,
1873 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001875 ASSERT(xfs_buf_islocked(bp));
1876 ASSERT(!(bp->b_flags & XBF_READ));
1877
1878 /*
1879 * If the buffer is already marked delwri it already is queued up
1880 * by someone else for imediate writeout. Just ignore it in that
1881 * case.
1882 */
1883 if (bp->b_flags & _XBF_DELWRI_Q) {
1884 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1885 return false;
1886 }
David Chinnera6867a62006-01-11 15:37:58 +11001887
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001888 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1889
Dave Chinnerd808f612010-02-02 10:13:42 +11001890 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001891 * If a buffer gets written out synchronously or marked stale while it
1892 * is on a delwri list we lazily remove it. To do this, the other party
1893 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1894 * It remains referenced and on the list. In a rare corner case it
1895 * might get readded to a delwri list after the synchronous writeout, in
1896 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001897 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001898 bp->b_flags |= _XBF_DELWRI_Q;
1899 if (list_empty(&bp->b_list)) {
1900 atomic_inc(&bp->b_hold);
1901 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001902 }
David Chinner585e6d82007-02-10 18:32:29 +11001903
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001904 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001905}
1906
Dave Chinner089716a2010-01-26 15:13:25 +11001907/*
1908 * Compare function is more complex than it needs to be because
1909 * the return value is only 32 bits and we are doing comparisons
1910 * on 64 bit values
1911 */
1912static int
1913xfs_buf_cmp(
1914 void *priv,
1915 struct list_head *a,
1916 struct list_head *b)
1917{
1918 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1919 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1920 xfs_daddr_t diff;
1921
Mark Tinguelyf4b42422012-12-04 17:18:02 -06001922 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11001923 if (diff < 0)
1924 return -1;
1925 if (diff > 0)
1926 return 1;
1927 return 0;
1928}
1929
Dave Chinner26f1fe82016-06-01 17:38:15 +10001930/*
1931 * submit buffers for write.
1932 *
1933 * When we have a large buffer list, we do not want to hold all the buffers
1934 * locked while we block on the request queue waiting for IO dispatch. To avoid
1935 * this problem, we lock and submit buffers in groups of 50, thereby minimising
1936 * the lock hold times for lists which may contain thousands of objects.
1937 *
1938 * To do this, we sort the buffer list before we walk the list to lock and
1939 * submit buffers, and we plug and unplug around each group of buffers we
1940 * submit.
1941 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001942static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10001943xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001944 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10001945 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001947 struct xfs_buf *bp, *n;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001948 LIST_HEAD (submit_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001949 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001950 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Dave Chinner26f1fe82016-06-01 17:38:15 +10001952 list_sort(NULL, buffer_list, xfs_buf_cmp);
1953
1954 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001955 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10001956 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001957 if (xfs_buf_ispinned(bp)) {
1958 pinned++;
1959 continue;
1960 }
1961 if (!xfs_buf_trylock(bp))
1962 continue;
1963 } else {
1964 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001967 /*
1968 * Someone else might have written the buffer synchronously or
1969 * marked it stale in the meantime. In that case only the
1970 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1971 * reference and remove it from the list here.
1972 */
1973 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1974 list_del_init(&bp->b_list);
1975 xfs_buf_relse(bp);
1976 continue;
1977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001979 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001980
Dave Chinnercf53e992014-10-02 09:04:01 +10001981 /*
Dave Chinner26f1fe82016-06-01 17:38:15 +10001982 * We do all IO submission async. This means if we need
1983 * to wait for IO completion we need to take an extra
1984 * reference so the buffer is still valid on the other
1985 * side. We need to move the buffer onto the io_list
1986 * at this point so the caller can still access it.
Dave Chinnercf53e992014-10-02 09:04:01 +10001987 */
Dave Chinnerbbfeb612016-07-20 11:53:35 +10001988 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL);
Dave Chinner26f1fe82016-06-01 17:38:15 +10001989 bp->b_flags |= XBF_WRITE | XBF_ASYNC;
1990 if (wait_list) {
Dave Chinnercf53e992014-10-02 09:04:01 +10001991 xfs_buf_hold(bp);
Dave Chinner26f1fe82016-06-01 17:38:15 +10001992 list_move_tail(&bp->b_list, wait_list);
1993 } else
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001994 list_del_init(&bp->b_list);
Dave Chinner8dac3922014-10-02 09:04:40 +10001995
Dave Chinner595bff72014-10-02 09:05:14 +10001996 xfs_buf_submit(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001998 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002000 return pinned;
2001}
Nathan Scottf07c2252006-09-28 10:52:15 +10002002
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002003/*
2004 * Write out a buffer list asynchronously.
2005 *
2006 * This will take the @buffer_list, write all non-locked and non-pinned buffers
2007 * out and not wait for I/O completion on any of the buffers. This interface
2008 * is only safely useable for callers that can track I/O completion by higher
2009 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
2010 * function.
2011 */
2012int
2013xfs_buf_delwri_submit_nowait(
2014 struct list_head *buffer_list)
2015{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002016 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002017}
2018
2019/*
2020 * Write out a buffer list synchronously.
2021 *
2022 * This will take the @buffer_list, write all buffers out and wait for I/O
2023 * completion on all of the buffers. @buffer_list is consumed by the function,
2024 * so callers must have some other way of tracking buffers if they require such
2025 * functionality.
2026 */
2027int
2028xfs_buf_delwri_submit(
2029 struct list_head *buffer_list)
2030{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002031 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002032 int error = 0, error2;
2033 struct xfs_buf *bp;
2034
Dave Chinner26f1fe82016-06-01 17:38:15 +10002035 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002036
2037 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10002038 while (!list_empty(&wait_list)) {
2039 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002040
2041 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10002042
2043 /* locking the buffer will wait for async IO completion. */
2044 xfs_buf_lock(bp);
2045 error2 = bp->b_error;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002046 xfs_buf_relse(bp);
2047 if (!error)
2048 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002051 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
Brian Foster7912e7f2017-06-14 21:21:45 -07002054/*
2055 * Push a single buffer on a delwri queue.
2056 *
2057 * The purpose of this function is to submit a single buffer of a delwri queue
2058 * and return with the buffer still on the original queue. The waiting delwri
2059 * buffer submission infrastructure guarantees transfer of the delwri queue
2060 * buffer reference to a temporary wait list. We reuse this infrastructure to
2061 * transfer the buffer back to the original queue.
2062 *
2063 * Note the buffer transitions from the queued state, to the submitted and wait
2064 * listed state and back to the queued state during this call. The buffer
2065 * locking and queue management logic between _delwri_pushbuf() and
2066 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2067 * before returning.
2068 */
2069int
2070xfs_buf_delwri_pushbuf(
2071 struct xfs_buf *bp,
2072 struct list_head *buffer_list)
2073{
2074 LIST_HEAD (submit_list);
2075 int error;
2076
2077 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2078
2079 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2080
2081 /*
2082 * Isolate the buffer to a new local list so we can submit it for I/O
2083 * independently from the rest of the original list.
2084 */
2085 xfs_buf_lock(bp);
2086 list_move(&bp->b_list, &submit_list);
2087 xfs_buf_unlock(bp);
2088
2089 /*
2090 * Delwri submission clears the DELWRI_Q buffer flag and returns with
2091 * the buffer on the wait list with an associated reference. Rather than
2092 * bounce the buffer from a local wait list back to the original list
2093 * after I/O completion, reuse the original list as the wait list.
2094 */
2095 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2096
2097 /*
2098 * The buffer is now under I/O and wait listed as during typical delwri
2099 * submission. Lock the buffer to wait for I/O completion. Rather than
2100 * remove the buffer from the wait list and release the reference, we
2101 * want to return with the buffer queued to the original list. The
2102 * buffer already sits on the original list with a wait list reference,
2103 * however. If we let the queue inherit that wait list reference, all we
2104 * need to do is reset the DELWRI_Q flag.
2105 */
2106 xfs_buf_lock(bp);
2107 error = bp->b_error;
2108 bp->b_flags |= _XBF_DELWRI_Q;
2109 xfs_buf_unlock(bp);
2110
2111 return error;
2112}
2113
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002114int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002115xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Nathan Scott87582802006-03-14 13:18:19 +11002117 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
2118 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002119 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002120 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002121
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002124 out:
Nathan Scott87582802006-03-14 13:18:19 +11002125 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128void
Nathan Scottce8e9222006-01-11 15:39:08 +11002129xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Nathan Scottce8e9222006-01-11 15:39:08 +11002131 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
Brian Foster7561d272017-10-17 14:16:29 -07002133
2134void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
2135{
Brian Foster7561d272017-10-17 14:16:29 -07002136 /*
2137 * Set the lru reference count to 0 based on the error injection tag.
2138 * This allows userspace to disrupt buffer caching for debug/testing
2139 * purposes.
2140 */
Brian Foster4eadcf92017-10-27 09:20:28 -07002141 if (XFS_TEST_ERROR(false, bp->b_target->bt_mount,
2142 XFS_ERRTAG_BUF_LRU_REF))
Brian Foster7561d272017-10-17 14:16:29 -07002143 lru_ref = 0;
2144
2145 atomic_set(&bp->b_lru_ref, lru_ref);
2146}