blob: 8c7d01b759221b4c35f43801a00f3f4605da421e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scottf07c2252006-09-28 10:52:15 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Vlad Apostolov93c189c2006-11-11 18:03:49 +110018#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/stddef.h>
20#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
23#include <linux/init.h>
24#include <linux/vmalloc.h>
25#include <linux/bio.h>
26#include <linux/sysctl.h>
27#include <linux/proc_fs.h>
28#include <linux/workqueue.h>
29#include <linux/percpu.h>
30#include <linux/blkdev.h>
31#include <linux/hash.h>
Christoph Hellwig4df08c52005-09-05 08:34:18 +100032#include <linux/kthread.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080033#include <linux/migrate.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070034#include <linux/backing-dev.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Christoph Hellwig4fb6e8a2014-11-28 14:25:04 +110037#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110038#include "xfs_log_format.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100039#include "xfs_trans_resv.h"
Dave Chinner239880e2013-10-23 10:50:10 +110040#include "xfs_sb.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050041#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110043#include "xfs_log.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050044
David Chinner7989cb82007-02-10 18:34:56 +110045static kmem_zone_t *xfs_buf_zone;
Christoph Hellwig23ea4032005-06-21 15:14:01 +100046
Nathan Scottce8e9222006-01-11 15:39:08 +110047#ifdef XFS_BUF_LOCK_TRACKING
48# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
49# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
50# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#else
Nathan Scottce8e9222006-01-11 15:39:08 +110052# define XB_SET_OWNER(bp) do { } while (0)
53# define XB_CLEAR_OWNER(bp) do { } while (0)
54# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#endif
56
Nathan Scottce8e9222006-01-11 15:39:08 +110057#define xb_to_gfp(flags) \
Dave Chinneraa5c1582012-04-23 15:58:56 +100058 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
James Bottomley73c77e22010-01-25 11:42:24 -060061static inline int
62xfs_buf_is_vmapped(
63 struct xfs_buf *bp)
64{
65 /*
66 * Return true if the buffer is vmapped.
67 *
Dave Chinner611c9942012-04-23 15:59:07 +100068 * b_addr is null if the buffer is not mapped, but the code is clever
69 * enough to know it doesn't have to map a single page, so the check has
70 * to be both for b_addr and bp->b_page_count > 1.
James Bottomley73c77e22010-01-25 11:42:24 -060071 */
Dave Chinner611c9942012-04-23 15:59:07 +100072 return bp->b_addr && bp->b_page_count > 1;
James Bottomley73c77e22010-01-25 11:42:24 -060073}
74
75static inline int
76xfs_buf_vmap_len(
77 struct xfs_buf *bp)
78{
79 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
Brian Foster9c7504a2016-07-20 11:15:28 +100083 * Bump the I/O in flight count on the buftarg if we haven't yet done so for
84 * this buffer. The count is incremented once per buffer (per hold cycle)
85 * because the corresponding decrement is deferred to buffer release. Buffers
86 * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
87 * tracking adds unnecessary overhead. This is used for sychronization purposes
88 * with unmount (see xfs_wait_buftarg()), so all we really need is a count of
89 * in-flight buffers.
90 *
91 * Buffers that are never released (e.g., superblock, iclog buffers) must set
92 * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
93 * never reaches zero and unmount hangs indefinitely.
94 */
95static inline void
96xfs_buf_ioacct_inc(
97 struct xfs_buf *bp)
98{
99 if (bp->b_flags & (XBF_NO_IOACCT|_XBF_IN_FLIGHT))
100 return;
101
102 ASSERT(bp->b_flags & XBF_ASYNC);
103 bp->b_flags |= _XBF_IN_FLIGHT;
104 percpu_counter_inc(&bp->b_target->bt_io_count);
105}
106
107/*
108 * Clear the in-flight state on a buffer about to be released to the LRU or
109 * freed and unaccount from the buftarg.
110 */
111static inline void
112xfs_buf_ioacct_dec(
113 struct xfs_buf *bp)
114{
115 if (!(bp->b_flags & _XBF_IN_FLIGHT))
116 return;
117
Brian Foster9c7504a2016-07-20 11:15:28 +1000118 bp->b_flags &= ~_XBF_IN_FLIGHT;
119 percpu_counter_dec(&bp->b_target->bt_io_count);
120}
121
122/*
Dave Chinner430cbeb2010-12-02 16:30:55 +1100123 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
124 * b_lru_ref count so that the buffer is freed immediately when the buffer
125 * reference count falls to zero. If the buffer is already on the LRU, we need
126 * to remove the reference that LRU holds on the buffer.
127 *
128 * This prevents build-up of stale buffers on the LRU.
129 */
130void
131xfs_buf_stale(
132 struct xfs_buf *bp)
133{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000134 ASSERT(xfs_buf_islocked(bp));
135
Dave Chinner430cbeb2010-12-02 16:30:55 +1100136 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000137
138 /*
139 * Clear the delwri status so that a delwri queue walker will not
140 * flush this buffer to disk now that it is stale. The delwri queue has
141 * a reference to the buffer, so this is safe to do.
142 */
143 bp->b_flags &= ~_XBF_DELWRI_Q;
144
Brian Foster9c7504a2016-07-20 11:15:28 +1000145 /*
146 * Once the buffer is marked stale and unlocked, a subsequent lookup
147 * could reset b_flags. There is no guarantee that the buffer is
148 * unaccounted (released to LRU) before that occurs. Drop in-flight
149 * status now to preserve accounting consistency.
150 */
151 xfs_buf_ioacct_dec(bp);
152
Dave Chinnera4082352013-08-28 10:18:06 +1000153 spin_lock(&bp->b_lock);
154 atomic_set(&bp->b_lru_ref, 0);
155 if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
Dave Chinnere80dfa12013-08-28 10:18:05 +1000156 (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
157 atomic_dec(&bp->b_hold);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100158
Dave Chinner430cbeb2010-12-02 16:30:55 +1100159 ASSERT(atomic_read(&bp->b_hold) >= 1);
Dave Chinnera4082352013-08-28 10:18:06 +1000160 spin_unlock(&bp->b_lock);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Dave Chinner3e85c862012-06-22 18:50:09 +1000163static int
164xfs_buf_get_maps(
165 struct xfs_buf *bp,
166 int map_count)
167{
168 ASSERT(bp->b_maps == NULL);
169 bp->b_map_count = map_count;
170
171 if (map_count == 1) {
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600172 bp->b_maps = &bp->__b_map;
Dave Chinner3e85c862012-06-22 18:50:09 +1000173 return 0;
174 }
175
176 bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
177 KM_NOFS);
178 if (!bp->b_maps)
Dave Chinner24513372014-06-25 14:58:08 +1000179 return -ENOMEM;
Dave Chinner3e85c862012-06-22 18:50:09 +1000180 return 0;
181}
182
183/*
184 * Frees b_pages if it was allocated.
185 */
186static void
187xfs_buf_free_maps(
188 struct xfs_buf *bp)
189{
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600190 if (bp->b_maps != &bp->__b_map) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000191 kmem_free(bp->b_maps);
192 bp->b_maps = NULL;
193 }
194}
195
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000196struct xfs_buf *
Dave Chinner3e85c862012-06-22 18:50:09 +1000197_xfs_buf_alloc(
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000198 struct xfs_buftarg *target,
Dave Chinner3e85c862012-06-22 18:50:09 +1000199 struct xfs_buf_map *map,
200 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100201 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000203 struct xfs_buf *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000204 int error;
205 int i;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000206
Dave Chinneraa5c1582012-04-23 15:58:56 +1000207 bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000208 if (unlikely(!bp))
209 return NULL;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /*
Dave Chinner12bcb3f2012-04-23 15:59:05 +1000212 * We don't want certain flags to appear in b_flags unless they are
213 * specifically set by later operations on the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
Dave Chinner611c9942012-04-23 15:59:07 +1000215 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Nathan Scottce8e9222006-01-11 15:39:08 +1100217 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100218 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000219 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100220 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100221 INIT_LIST_HEAD(&bp->b_list);
Thomas Gleixnera731cd112010-09-07 14:33:15 +0000222 sema_init(&bp->b_sema, 0); /* held, no waiters */
Dave Chinnera4082352013-08-28 10:18:06 +1000223 spin_lock_init(&bp->b_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100224 XB_SET_OWNER(bp);
225 bp->b_target = target;
Dave Chinner3e85c862012-06-22 18:50:09 +1000226 bp->b_flags = flags;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000229 * Set length and io_length to the same value initially.
230 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * most cases but may be reset (e.g. XFS recovery).
232 */
Dave Chinner3e85c862012-06-22 18:50:09 +1000233 error = xfs_buf_get_maps(bp, nmaps);
234 if (error) {
235 kmem_zone_free(xfs_buf_zone, bp);
236 return NULL;
237 }
238
239 bp->b_bn = map[0].bm_bn;
240 bp->b_length = 0;
241 for (i = 0; i < nmaps; i++) {
242 bp->b_maps[i].bm_bn = map[i].bm_bn;
243 bp->b_maps[i].bm_len = map[i].bm_len;
244 bp->b_length += map[i].bm_len;
245 }
246 bp->b_io_length = bp->b_length;
247
Nathan Scottce8e9222006-01-11 15:39:08 +1100248 atomic_set(&bp->b_pin_count, 0);
249 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100251 XFS_STATS_INC(target->bt_mount, xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000252 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000253
254 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100258 * Allocate a page array capable of holding a specified number
259 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
261STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100262_xfs_buf_get_pages(
263 xfs_buf_t *bp,
Eric Sandeen87937bf2014-04-14 19:01:20 +1000264 int page_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100267 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100268 bp->b_page_count = page_count;
269 if (page_count <= XB_PAGES) {
270 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100272 bp->b_pages = kmem_alloc(sizeof(struct page *) *
Dave Chinneraa5c1582012-04-23 15:58:56 +1000273 page_count, KM_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +1100274 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return -ENOMEM;
276 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100277 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279 return 0;
280}
281
282/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100283 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
285STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100286_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 xfs_buf_t *bp)
288{
Nathan Scottce8e9222006-01-11 15:39:08 +1100289 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000290 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000291 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293}
294
295/*
296 * Releases the specified buffer.
297 *
298 * The modification state of any associated pages is left unchanged.
Zhi Yong Wub46fe822013-08-07 10:10:59 +0000299 * The buffer must not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * hashed and refcounted buffers
301 */
302void
Nathan Scottce8e9222006-01-11 15:39:08 +1100303xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 xfs_buf_t *bp)
305{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000306 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Dave Chinner430cbeb2010-12-02 16:30:55 +1100308 ASSERT(list_empty(&bp->b_lru));
309
Dave Chinner0e6e8472011-03-26 09:16:45 +1100310 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 uint i;
312
James Bottomley73c77e22010-01-25 11:42:24 -0600313 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000314 vm_unmap_ram(bp->b_addr - bp->b_offset,
315 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Nathan Scott948ecdb2006-09-28 11:03:13 +1000317 for (i = 0; i < bp->b_page_count; i++) {
318 struct page *page = bp->b_pages[i];
319
Dave Chinner0e6e8472011-03-26 09:16:45 +1100320 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000321 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100322 } else if (bp->b_flags & _XBF_KMEM)
323 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000324 _xfs_buf_free_pages(bp);
Dave Chinner3e85c862012-06-22 18:50:09 +1000325 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000326 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100330 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
332STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100333xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 xfs_buf_t *bp,
335 uint flags)
336{
Dave Chinneraa0e8832012-04-23 15:58:52 +1000337 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100339 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 unsigned short page_count, i;
Dave Chinner795cac72012-04-23 15:58:53 +1000341 xfs_off_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 int error;
343
Dave Chinner0e6e8472011-03-26 09:16:45 +1100344 /*
345 * for buffers that are contained within a single page, just allocate
346 * the memory from the heap - there's no need for the complexity of
347 * page arrays to keep allocation down to order 0.
348 */
Dave Chinner795cac72012-04-23 15:58:53 +1000349 size = BBTOB(bp->b_length);
350 if (size < PAGE_SIZE) {
Dave Chinneraa5c1582012-04-23 15:58:56 +1000351 bp->b_addr = kmem_alloc(size, KM_NOFS);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100352 if (!bp->b_addr) {
353 /* low memory - use alloc_page loop instead */
354 goto use_alloc_page;
355 }
356
Dave Chinner795cac72012-04-23 15:58:53 +1000357 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100358 ((unsigned long)bp->b_addr & PAGE_MASK)) {
359 /* b_addr spans two pages - use alloc_page instead */
360 kmem_free(bp->b_addr);
361 bp->b_addr = NULL;
362 goto use_alloc_page;
363 }
364 bp->b_offset = offset_in_page(bp->b_addr);
365 bp->b_pages = bp->b_page_array;
366 bp->b_pages[0] = virt_to_page(bp->b_addr);
367 bp->b_page_count = 1;
Dave Chinner611c9942012-04-23 15:59:07 +1000368 bp->b_flags |= _XBF_KMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100369 return 0;
370 }
371
372use_alloc_page:
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600373 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
374 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000375 >> PAGE_SHIFT;
Dave Chinner795cac72012-04-23 15:58:53 +1000376 page_count = end - start;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000377 error = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (unlikely(error))
379 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Nathan Scottce8e9222006-01-11 15:39:08 +1100381 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100382 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Nathan Scottce8e9222006-01-11 15:39:08 +1100384 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 struct page *page;
386 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100387retry:
388 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100390 if (flags & XBF_READ_AHEAD) {
391 bp->b_page_count = i;
Dave Chinner24513372014-06-25 14:58:08 +1000392 error = -ENOMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100393 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
396 /*
397 * This could deadlock.
398 *
399 * But until all the XFS lowlevel code is revamped to
400 * handle buffer allocation failures we can't do much.
401 */
402 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100403 xfs_err(NULL,
Tetsuo Handa5bf97b12015-10-12 15:41:29 +1100404 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
405 current->comm, current->pid,
Harvey Harrison34a622b2008-04-10 12:19:21 +1000406 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100408 XFS_STATS_INC(bp->b_target->bt_mount, xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200409 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 goto retry;
411 }
412
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100413 XFS_STATS_INC(bp->b_target->bt_mount, xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Dave Chinner0e6e8472011-03-26 09:16:45 +1100415 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100417 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 offset = 0;
419 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Dave Chinner0e6e8472011-03-26 09:16:45 +1100422out_free_pages:
423 for (i = 0; i < bp->b_page_count; i++)
424 __free_page(bp->b_pages[i]);
Darrick J. Wong2aa6ba7b2017-01-25 20:24:57 -0800425 bp->b_flags &= ~_XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return error;
427}
428
429/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300430 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 */
432STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100433_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 xfs_buf_t *bp,
435 uint flags)
436{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100437 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100438 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100439 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100440 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
Dave Chinner611c9942012-04-23 15:59:07 +1000441 } else if (flags & XBF_UNMAPPED) {
442 bp->b_addr = NULL;
443 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100444 int retried = 0;
Dave Chinnerae687e52014-03-07 16:19:14 +1100445 unsigned noio_flag;
Dave Chinnera19fb382011-03-26 09:13:42 +1100446
Dave Chinnerae687e52014-03-07 16:19:14 +1100447 /*
448 * vm_map_ram() will allocate auxillary structures (e.g.
449 * pagetables) with GFP_KERNEL, yet we are likely to be under
450 * GFP_NOFS context here. Hence we need to tell memory reclaim
451 * that we are in such a context via PF_MEMALLOC_NOIO to prevent
452 * memory reclaim re-entering the filesystem here and
453 * potentially deadlocking.
454 */
455 noio_flag = memalloc_noio_save();
Dave Chinnera19fb382011-03-26 09:13:42 +1100456 do {
457 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
458 -1, PAGE_KERNEL);
459 if (bp->b_addr)
460 break;
461 vm_unmap_aliases();
462 } while (retried++ <= 1);
Dave Chinnerae687e52014-03-07 16:19:14 +1100463 memalloc_noio_restore(noio_flag);
Dave Chinnera19fb382011-03-26 09:13:42 +1100464
465 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100467 bp->b_addr += bp->b_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
470 return 0;
471}
472
473/*
474 * Finding and Reading Buffers
475 */
Lucas Stach6031e732016-12-07 17:36:36 +1100476static int
477_xfs_buf_obj_cmp(
478 struct rhashtable_compare_arg *arg,
479 const void *obj)
480{
481 const struct xfs_buf_map *map = arg->key;
482 const struct xfs_buf *bp = obj;
483
484 /*
485 * The key hashing in the lookup path depends on the key being the
486 * first element of the compare_arg, make sure to assert this.
487 */
488 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
489
490 if (bp->b_bn != map->bm_bn)
491 return 1;
492
493 if (unlikely(bp->b_length != map->bm_len)) {
494 /*
495 * found a block number match. If the range doesn't
496 * match, the only way this is allowed is if the buffer
497 * in the cache is stale and the transaction that made
498 * it stale has not yet committed. i.e. we are
499 * reallocating a busy extent. Skip this buffer and
500 * continue searching for an exact match.
501 */
502 ASSERT(bp->b_flags & XBF_STALE);
503 return 1;
504 }
505 return 0;
506}
507
508static const struct rhashtable_params xfs_buf_hash_params = {
509 .min_size = 32, /* empty AGs have minimal footprint */
510 .nelem_hint = 16,
511 .key_len = sizeof(xfs_daddr_t),
512 .key_offset = offsetof(struct xfs_buf, b_bn),
513 .head_offset = offsetof(struct xfs_buf, b_rhash_head),
514 .automatic_shrinking = true,
515 .obj_cmpfn = _xfs_buf_obj_cmp,
516};
517
518int
519xfs_buf_hash_init(
520 struct xfs_perag *pag)
521{
522 spin_lock_init(&pag->pag_buf_lock);
523 return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
524}
525
526void
527xfs_buf_hash_destroy(
528 struct xfs_perag *pag)
529{
530 rhashtable_destroy(&pag->pag_buf_hash);
531}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100534 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * a given range of an inode. The buffer is returned
Chandra Seetharamaneabbaf12011-09-08 20:18:50 +0000536 * locked. No I/O is implied by this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
538xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100539_xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000540 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000541 struct xfs_buf_map *map,
542 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100543 xfs_buf_flags_t flags,
544 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Dave Chinner74f75a02010-09-24 19:59:04 +1000546 struct xfs_perag *pag;
Dave Chinner74f75a02010-09-24 19:59:04 +1000547 xfs_buf_t *bp;
Lucas Stach6031e732016-12-07 17:36:36 +1100548 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
Dave Chinner10616b82013-01-21 23:53:52 +1100549 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000550 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Dave Chinner3e85c862012-06-22 18:50:09 +1000552 for (i = 0; i < nmaps; i++)
Lucas Stach6031e732016-12-07 17:36:36 +1100553 cmap.bm_len += map[i].bm_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* Check for IOs smaller than the sector size / not sector aligned */
Lucas Stach6031e732016-12-07 17:36:36 +1100556 ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
557 ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Dave Chinner10616b82013-01-21 23:53:52 +1100559 /*
560 * Corrupted block numbers can get through to here, unfortunately, so we
561 * have to check that the buffer falls within the filesystem bounds.
562 */
563 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
Lucas Stach6031e732016-12-07 17:36:36 +1100564 if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
Dave Chinner10616b82013-01-21 23:53:52 +1100565 /*
Dave Chinner24513372014-06-25 14:58:08 +1000566 * XXX (dgc): we should really be returning -EFSCORRUPTED here,
Dave Chinner10616b82013-01-21 23:53:52 +1100567 * but none of the higher level infrastructure supports
568 * returning a specific error on buffer lookup failures.
569 */
570 xfs_alert(btp->bt_mount,
571 "%s: Block out of range: block 0x%llx, EOFS 0x%llx ",
Lucas Stach6031e732016-12-07 17:36:36 +1100572 __func__, cmap.bm_bn, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000573 WARN_ON(1);
Dave Chinner10616b82013-01-21 23:53:52 +1100574 return NULL;
575 }
576
Dave Chinner74f75a02010-09-24 19:59:04 +1000577 pag = xfs_perag_get(btp->bt_mount,
Lucas Stach6031e732016-12-07 17:36:36 +1100578 xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Dave Chinner74f75a02010-09-24 19:59:04 +1000580 spin_lock(&pag->pag_buf_lock);
Lucas Stach6031e732016-12-07 17:36:36 +1100581 bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
582 xfs_buf_hash_params);
583 if (bp) {
584 atomic_inc(&bp->b_hold);
585 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
588 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100589 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000590 /* the buffer keeps the perag reference until it is freed */
591 new_bp->b_pag = pag;
Lucas Stach6031e732016-12-07 17:36:36 +1100592 rhashtable_insert_fast(&pag->pag_buf_hash,
593 &new_bp->b_rhash_head,
594 xfs_buf_hash_params);
Dave Chinner74f75a02010-09-24 19:59:04 +1000595 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 } else {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100597 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000598 spin_unlock(&pag->pag_buf_lock);
599 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100601 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000604 spin_unlock(&pag->pag_buf_lock);
605 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200607 if (!xfs_buf_trylock(bp)) {
608 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100609 xfs_buf_rele(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100610 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
Nathan Scottce8e9222006-01-11 15:39:08 +1100611 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200613 xfs_buf_lock(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100614 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
Dave Chinner0e6e8472011-03-26 09:16:45 +1100617 /*
618 * if the buffer is stale, clear all the external state associated with
619 * it. We need to keep flags such as how we allocated the buffer memory
620 * intact here.
621 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100622 if (bp->b_flags & XBF_STALE) {
623 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinnercfb02852012-11-12 22:54:19 +1100624 ASSERT(bp->b_iodone == NULL);
Dave Chinner611c9942012-04-23 15:59:07 +1000625 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100626 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000627 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000628
629 trace_xfs_buf_find(bp, flags, _RET_IP_);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100630 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
Nathan Scottce8e9222006-01-11 15:39:08 +1100631 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634/*
Dave Chinner38158322011-09-30 04:45:02 +0000635 * Assembles a buffer covering the specified range. The code is optimised for
636 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
637 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Dave Chinner38158322011-09-30 04:45:02 +0000639struct xfs_buf *
Dave Chinner6dde2702012-06-22 18:50:10 +1000640xfs_buf_get_map(
641 struct xfs_buftarg *target,
642 struct xfs_buf_map *map,
643 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100644 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Dave Chinner38158322011-09-30 04:45:02 +0000646 struct xfs_buf *bp;
647 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100648 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Dave Chinner6dde2702012-06-22 18:50:10 +1000650 bp = _xfs_buf_find(target, map, nmaps, flags, NULL);
Dave Chinner38158322011-09-30 04:45:02 +0000651 if (likely(bp))
652 goto found;
653
Dave Chinner6dde2702012-06-22 18:50:10 +1000654 new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100655 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return NULL;
657
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000658 error = xfs_buf_allocate_memory(new_bp, flags);
659 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000660 xfs_buf_free(new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000661 return NULL;
662 }
663
Dave Chinner6dde2702012-06-22 18:50:10 +1000664 bp = _xfs_buf_find(target, map, nmaps, flags, new_bp);
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000665 if (!bp) {
666 xfs_buf_free(new_bp);
667 return NULL;
668 }
669
670 if (bp != new_bp)
671 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Dave Chinner38158322011-09-30 04:45:02 +0000673found:
Dave Chinner611c9942012-04-23 15:59:07 +1000674 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100675 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100677 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500678 "%s: failed to map pagesn", __func__);
Dave Chinnera8acad72012-04-23 15:58:54 +1000679 xfs_buf_relse(bp);
680 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 }
683
Dave Chinnerb79f4a12016-01-12 07:03:44 +1100684 /*
685 * Clear b_error if this is a lookup from a caller that doesn't expect
686 * valid data to be found in the buffer.
687 */
688 if (!(flags & XBF_READ))
689 xfs_buf_ioerror(bp, 0);
690
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100691 XFS_STATS_INC(target->bt_mount, xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000692 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100693 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100696STATIC int
697_xfs_buf_read(
698 xfs_buf_t *bp,
699 xfs_buf_flags_t flags)
700{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000701 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600702 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100703
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000704 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200705 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100706
Dave Chinner595bff72014-10-02 09:05:14 +1000707 if (flags & XBF_ASYNC) {
708 xfs_buf_submit(bp);
Dave Chinner0e95f192012-04-23 15:58:46 +1000709 return 0;
Dave Chinner595bff72014-10-02 09:05:14 +1000710 }
711 return xfs_buf_submit_wait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100712}
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714xfs_buf_t *
Dave Chinner6dde2702012-06-22 18:50:10 +1000715xfs_buf_read_map(
716 struct xfs_buftarg *target,
717 struct xfs_buf_map *map,
718 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100719 xfs_buf_flags_t flags,
Dave Chinner1813dd62012-11-14 17:54:40 +1100720 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Dave Chinner6dde2702012-06-22 18:50:10 +1000722 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Nathan Scottce8e9222006-01-11 15:39:08 +1100724 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Dave Chinner6dde2702012-06-22 18:50:10 +1000726 bp = xfs_buf_get_map(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100727 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000728 trace_xfs_buf_read(bp, flags, _RET_IP_);
729
Dave Chinnerb0388bf2016-02-10 15:01:11 +1100730 if (!(bp->b_flags & XBF_DONE)) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100731 XFS_STATS_INC(target->bt_mount, xb_get_read);
Dave Chinner1813dd62012-11-14 17:54:40 +1100732 bp->b_ops = ops;
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100733 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100734 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /*
736 * Read ahead call which is already satisfied,
737 * drop the buffer
738 */
Dave Chinnera8acad72012-04-23 15:58:54 +1000739 xfs_buf_relse(bp);
740 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100743 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745 }
746
Nathan Scottce8e9222006-01-11 15:39:08 +1100747 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100751 * If we are not low on memory then do the readahead in a deadlock
752 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
754void
Dave Chinner6dde2702012-06-22 18:50:10 +1000755xfs_buf_readahead_map(
756 struct xfs_buftarg *target,
757 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100758 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100759 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Jan Karaefa7c9f2017-02-02 15:56:53 +0100761 if (bdi_read_congested(target->bt_bdev->bd_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return;
763
Dave Chinner6dde2702012-06-22 18:50:10 +1000764 xfs_buf_read_map(target, map, nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100765 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Dave Chinner5adc94c2010-09-24 21:58:31 +1000768/*
769 * Read an uncached buffer from disk. Allocates and returns a locked
770 * buffer containing the disk contents or nothing.
771 */
Dave Chinnerba372672014-10-02 09:05:32 +1000772int
Dave Chinner5adc94c2010-09-24 21:58:31 +1000773xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000774 struct xfs_buftarg *target,
775 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000776 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100777 int flags,
Dave Chinnerba372672014-10-02 09:05:32 +1000778 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100779 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000780{
Dave Chinnereab4e632012-11-12 22:54:02 +1100781 struct xfs_buf *bp;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000782
Dave Chinnerba372672014-10-02 09:05:32 +1000783 *bpp = NULL;
784
Dave Chinnere70b73f2012-04-23 15:58:49 +1000785 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000786 if (!bp)
Dave Chinnerba372672014-10-02 09:05:32 +1000787 return -ENOMEM;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000788
789 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000790 ASSERT(bp->b_map_count == 1);
Dave Chinnerba372672014-10-02 09:05:32 +1000791 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
Dave Chinner3e85c862012-06-22 18:50:09 +1000792 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000793 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100794 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000795
Dave Chinner595bff72014-10-02 09:05:14 +1000796 xfs_buf_submit_wait(bp);
Dave Chinnerba372672014-10-02 09:05:32 +1000797 if (bp->b_error) {
798 int error = bp->b_error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800799 xfs_buf_relse(bp);
Dave Chinnerba372672014-10-02 09:05:32 +1000800 return error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800801 }
Dave Chinnerba372672014-10-02 09:05:32 +1000802
803 *bpp = bp;
804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Dave Chinner44396472011-04-21 09:34:27 +0000807/*
808 * Return a buffer allocated as an empty buffer and associated to external
809 * memory via xfs_buf_associate_memory() back to it's empty state.
810 */
811void
812xfs_buf_set_empty(
813 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000814 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000815{
816 if (bp->b_pages)
817 _xfs_buf_free_pages(bp);
818
819 bp->b_pages = NULL;
820 bp->b_page_count = 0;
821 bp->b_addr = NULL;
Dave Chinner4e94b712012-04-23 15:58:51 +1000822 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000823 bp->b_io_length = numblks;
Dave Chinner3e85c862012-06-22 18:50:09 +1000824
825 ASSERT(bp->b_map_count == 1);
Dave Chinner44396472011-04-21 09:34:27 +0000826 bp->b_bn = XFS_BUF_DADDR_NULL;
Dave Chinner3e85c862012-06-22 18:50:09 +1000827 bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL;
828 bp->b_maps[0].bm_len = bp->b_length;
Dave Chinner44396472011-04-21 09:34:27 +0000829}
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831static inline struct page *
832mem_to_page(
833 void *addr)
834{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800835 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return virt_to_page(addr);
837 } else {
838 return vmalloc_to_page(addr);
839 }
840}
841
842int
Nathan Scottce8e9222006-01-11 15:39:08 +1100843xfs_buf_associate_memory(
844 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 void *mem,
846 size_t len)
847{
848 int rval;
849 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100850 unsigned long pageaddr;
851 unsigned long offset;
852 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 int page_count;
854
Dave Chinner0e6e8472011-03-26 09:16:45 +1100855 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100856 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100857 buflen = PAGE_ALIGN(len + offset);
858 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100861 if (bp->b_pages)
862 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Nathan Scottce8e9222006-01-11 15:39:08 +1100864 bp->b_pages = NULL;
865 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Eric Sandeen87937bf2014-04-14 19:01:20 +1000867 rval = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (rval)
869 return rval;
870
Nathan Scottce8e9222006-01-11 15:39:08 +1100871 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100873 for (i = 0; i < bp->b_page_count; i++) {
874 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100875 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Dave Chinneraa0e8832012-04-23 15:58:52 +1000878 bp->b_io_length = BTOBB(len);
Dave Chinner4e94b712012-04-23 15:58:51 +1000879 bp->b_length = BTOBB(buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 return 0;
882}
883
884xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000885xfs_buf_get_uncached(
886 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000887 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000888 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000890 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000891 int error, i;
Dave Chinner3e85c862012-06-22 18:50:09 +1000892 struct xfs_buf *bp;
893 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Brian Fosterc891c302016-07-20 11:13:43 +1000895 /* flags might contain irrelevant bits, pass only what we care about */
896 bp = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (unlikely(bp == NULL))
898 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Dave Chinnere70b73f2012-04-23 15:58:49 +1000900 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000901 error = _xfs_buf_get_pages(bp, page_count);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000902 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 goto fail_free_buf;
904
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000905 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000906 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000907 if (!bp->b_pages[i])
908 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000910 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Dave Chinner611c9942012-04-23 15:59:07 +1000912 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000913 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100914 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500915 "%s: failed to map pages", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Dave Chinner686865f2010-09-24 20:07:47 +1000919 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000923 while (--i >= 0)
924 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000925 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 fail_free_buf:
Dave Chinner3e85c862012-06-22 18:50:09 +1000927 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000928 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 fail:
930 return NULL;
931}
932
933/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 * Increment reference count on buffer, to hold the buffer concurrently
935 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * Must hold the buffer already to call this function.
937 */
938void
Nathan Scottce8e9222006-01-11 15:39:08 +1100939xfs_buf_hold(
940 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000942 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100943 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
946/*
Brian Foster9c7504a2016-07-20 11:15:28 +1000947 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
948 * placed on LRU or freed (depending on b_lru_ref).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 */
950void
Nathan Scottce8e9222006-01-11 15:39:08 +1100951xfs_buf_rele(
952 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Dave Chinner74f75a02010-09-24 19:59:04 +1000954 struct xfs_perag *pag = bp->b_pag;
Brian Foster9c7504a2016-07-20 11:15:28 +1000955 bool release;
956 bool freebuf = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000958 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Dave Chinner74f75a02010-09-24 19:59:04 +1000960 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100961 ASSERT(list_empty(&bp->b_lru));
Brian Foster9c7504a2016-07-20 11:15:28 +1000962 if (atomic_dec_and_test(&bp->b_hold)) {
963 xfs_buf_ioacct_dec(bp);
Nathan Scottfad3aa12006-02-01 12:14:52 +1100964 xfs_buf_free(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +1000965 }
Nathan Scottfad3aa12006-02-01 12:14:52 +1100966 return;
967 }
968
Lachlan McIlroy37906892008-08-13 15:42:10 +1000969 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinnera4082352013-08-28 10:18:06 +1000970
Brian Foster9c7504a2016-07-20 11:15:28 +1000971 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
972 spin_lock(&bp->b_lock);
973 if (!release) {
974 /*
975 * Drop the in-flight state if the buffer is already on the LRU
976 * and it holds the only reference. This is racy because we
977 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
978 * ensures the decrement occurs only once per-buf.
979 */
980 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
981 xfs_buf_ioacct_dec(bp);
982 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
Brian Foster9c7504a2016-07-20 11:15:28 +1000984
985 /* the last reference has been dropped ... */
986 xfs_buf_ioacct_dec(bp);
987 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
988 /*
989 * If the buffer is added to the LRU take a new reference to the
990 * buffer for the LRU and clear the (now stale) dispose list
991 * state flag
992 */
993 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
994 bp->b_state &= ~XFS_BSTATE_DISPOSE;
995 atomic_inc(&bp->b_hold);
996 }
997 spin_unlock(&pag->pag_buf_lock);
998 } else {
999 /*
1000 * most of the time buffers will already be removed from the
1001 * LRU, so optimise that case by checking for the
1002 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1003 * was on was the disposal list
1004 */
1005 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1006 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1007 } else {
1008 ASSERT(list_empty(&bp->b_lru));
1009 }
1010
1011 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Lucas Stach6031e732016-12-07 17:36:36 +11001012 rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
1013 xfs_buf_hash_params);
Brian Foster9c7504a2016-07-20 11:15:28 +10001014 spin_unlock(&pag->pag_buf_lock);
1015 xfs_perag_put(pag);
1016 freebuf = true;
1017 }
1018
1019out_unlock:
1020 spin_unlock(&bp->b_lock);
1021
1022 if (freebuf)
1023 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
1026
1027/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001028 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +11001029 *
1030 * If we come across a stale, pinned, locked buffer, we know that we are
1031 * being asked to lock a buffer that has been reallocated. Because it is
1032 * pinned, we know that the log has not been pushed to disk and hence it
1033 * will still be locked. Rather than continuing to have trylock attempts
1034 * fail until someone else pushes the log, push it ourselves before
1035 * returning. This means that the xfsaild will not get stuck trying
1036 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 */
1038int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001039xfs_buf_trylock(
1040 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 int locked;
1043
Nathan Scottce8e9222006-01-11 15:39:08 +11001044 locked = down_trylock(&bp->b_sema) == 0;
Darrick J. Wong479c6412016-06-21 11:53:28 +10001045 if (locked) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001046 XB_SET_OWNER(bp);
Darrick J. Wong479c6412016-06-21 11:53:28 +10001047 trace_xfs_buf_trylock(bp, _RET_IP_);
1048 } else {
1049 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
1050 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001051 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001055 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +10001056 *
1057 * If we come across a stale, pinned, locked buffer, we know that we
1058 * are being asked to lock a buffer that has been reallocated. Because
1059 * it is pinned, we know that the log has not been pushed to disk and
1060 * hence it will still be locked. Rather than sleeping until someone
1061 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001063void
1064xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001065 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001067 trace_xfs_buf_lock(bp, _RET_IP_);
1068
Dave Chinnered3b4d62010-05-21 12:07:08 +10001069 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +10001070 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +11001071 down(&bp->b_sema);
1072 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001073
1074 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077void
Nathan Scottce8e9222006-01-11 15:39:08 +11001078xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001079 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Nathan Scottce8e9222006-01-11 15:39:08 +11001081 XB_CLEAR_OWNER(bp);
1082 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001083
1084 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
Nathan Scottce8e9222006-01-11 15:39:08 +11001087STATIC void
1088xfs_buf_wait_unpin(
1089 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 DECLARE_WAITQUEUE (wait, current);
1092
Nathan Scottce8e9222006-01-11 15:39:08 +11001093 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return;
1095
Nathan Scottce8e9222006-01-11 15:39:08 +11001096 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 for (;;) {
1098 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001099 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001101 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001103 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 set_current_state(TASK_RUNNING);
1105}
1106
1107/*
1108 * Buffer Utility Routines
1109 */
1110
Dave Chinnere8aaba92014-10-02 09:04:22 +10001111void
1112xfs_buf_ioend(
1113 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001115 bool read = bp->b_flags & XBF_READ;
1116
1117 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001118
1119 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Dave Chinnerd5929de2013-02-27 13:25:54 +11001120
Dave Chinner61be9c52014-10-02 09:04:31 +10001121 /*
1122 * Pull in IO completion errors now. We are guaranteed to be running
1123 * single threaded, so we don't need the lock to read b_io_error.
1124 */
1125 if (!bp->b_error && bp->b_io_error)
1126 xfs_buf_ioerror(bp, bp->b_io_error);
1127
Dave Chinnere8aaba92014-10-02 09:04:22 +10001128 /* Only validate buffers that were read without errors */
1129 if (read && !bp->b_error && bp->b_ops) {
1130 ASSERT(!bp->b_iodone);
Dave Chinner1813dd62012-11-14 17:54:40 +11001131 bp->b_ops->verify_read(bp);
Dave Chinnere8aaba92014-10-02 09:04:22 +10001132 }
1133
1134 if (!bp->b_error)
1135 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Christoph Hellwig80f6c292010-08-18 05:29:11 -04001137 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001138 (*(bp->b_iodone))(bp);
1139 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 xfs_buf_relse(bp);
Dave Chinner595bff72014-10-02 09:05:14 +10001141 else
Dave Chinner1813dd62012-11-14 17:54:40 +11001142 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Dave Chinnere8aaba92014-10-02 09:04:22 +10001145static void
1146xfs_buf_ioend_work(
1147 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001149 struct xfs_buf *bp =
Brian Fosterb29c70f2014-12-04 09:43:17 +11001150 container_of(work, xfs_buf_t, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001151
Dave Chinnere8aaba92014-10-02 09:04:22 +10001152 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001155static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001156xfs_buf_ioend_async(
1157 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001159 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
1160 queue_work(bp->b_ioend_wq, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163void
Nathan Scottce8e9222006-01-11 15:39:08 +11001164xfs_buf_ioerror(
1165 xfs_buf_t *bp,
1166 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Dave Chinner24513372014-06-25 14:58:08 +10001168 ASSERT(error <= 0 && error >= -1000);
1169 bp->b_error = error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001170 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
Christoph Hellwig901796a2011-10-10 16:52:49 +00001173void
1174xfs_buf_ioerror_alert(
1175 struct xfs_buf *bp,
1176 const char *func)
1177{
1178 xfs_alert(bp->b_target->bt_mount,
Dave Chinneraa0e8832012-04-23 15:58:52 +10001179"metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
Dave Chinner24513372014-06-25 14:58:08 +10001180 (__uint64_t)XFS_BUF_ADDR(bp), func, -bp->b_error, bp->b_length);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001181}
1182
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001183int
1184xfs_bwrite(
1185 struct xfs_buf *bp)
1186{
1187 int error;
1188
1189 ASSERT(xfs_buf_islocked(bp));
1190
1191 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001192 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
1193 XBF_WRITE_FAIL | XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001194
Dave Chinner595bff72014-10-02 09:05:14 +10001195 error = xfs_buf_submit_wait(bp);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001196 if (error) {
1197 xfs_force_shutdown(bp->b_target->bt_mount,
1198 SHUTDOWN_META_IO_ERROR);
1199 }
1200 return error;
1201}
1202
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001203static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001204xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001205 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001207 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Dave Chinner37eb17e2012-11-12 22:09:46 +11001209 /*
1210 * don't overwrite existing errors - otherwise we can lose errors on
1211 * buffers that require multiple bios to complete.
1212 */
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001213 if (bio->bi_error)
1214 cmpxchg(&bp->b_io_error, 0, bio->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Dave Chinner37eb17e2012-11-12 22:09:46 +11001216 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001217 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1218
Dave Chinnere8aaba92014-10-02 09:04:22 +10001219 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1220 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Dave Chinner3e85c862012-06-22 18:50:09 +10001224static void
1225xfs_buf_ioapply_map(
1226 struct xfs_buf *bp,
1227 int map,
1228 int *buf_offset,
1229 int *count,
Mike Christie50bfcd02016-06-05 14:31:57 -05001230 int op,
1231 int op_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Dave Chinner3e85c862012-06-22 18:50:09 +10001233 int page_index;
1234 int total_nr_pages = bp->b_page_count;
1235 int nr_pages;
1236 struct bio *bio;
1237 sector_t sector = bp->b_maps[map].bm_bn;
1238 int size;
1239 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Nathan Scottce8e9222006-01-11 15:39:08 +11001241 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Dave Chinner3e85c862012-06-22 18:50:09 +10001243 /* skip the pages in the buffer before the start offset */
1244 page_index = 0;
1245 offset = *buf_offset;
1246 while (offset >= PAGE_SIZE) {
1247 page_index++;
1248 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001249 }
1250
Dave Chinner3e85c862012-06-22 18:50:09 +10001251 /*
1252 * Limit the IO size to the length of the current vector, and update the
1253 * remaining IO count for the next time around.
1254 */
1255 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1256 *count -= size;
1257 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001260 atomic_inc(&bp->b_io_remaining);
Ming Leic908e382016-05-30 21:34:33 +08001261 nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001264 bio->bi_bdev = bp->b_target->bt_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001265 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001266 bio->bi_end_io = xfs_buf_bio_end_io;
1267 bio->bi_private = bp;
Mike Christie50bfcd02016-06-05 14:31:57 -05001268 bio_set_op_attrs(bio, op, op_flags);
Dave Chinner0e6e8472011-03-26 09:16:45 +11001269
Dave Chinner3e85c862012-06-22 18:50:09 +10001270 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001271 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 if (nbytes > size)
1274 nbytes = size;
1275
Dave Chinner3e85c862012-06-22 18:50:09 +10001276 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1277 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001278 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 break;
1280
1281 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001282 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 size -= nbytes;
1284 total_nr_pages--;
1285 }
1286
Kent Overstreet4f024f32013-10-11 15:44:27 -07001287 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001288 if (xfs_buf_is_vmapped(bp)) {
1289 flush_kernel_vmap_range(bp->b_addr,
1290 xfs_buf_vmap_len(bp));
1291 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001292 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (size)
1294 goto next_chunk;
1295 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001296 /*
1297 * This is guaranteed not to be the last io reference count
Dave Chinner595bff72014-10-02 09:05:14 +10001298 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001299 */
1300 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001301 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001302 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001304
1305}
1306
1307STATIC void
1308_xfs_buf_ioapply(
1309 struct xfs_buf *bp)
1310{
1311 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001312 int op;
1313 int op_flags = 0;
Dave Chinner3e85c862012-06-22 18:50:09 +10001314 int offset;
1315 int size;
1316 int i;
1317
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001318 /*
1319 * Make sure we capture only current IO errors rather than stale errors
1320 * left over from previous use of the buffer (e.g. failed readahead).
1321 */
1322 bp->b_error = 0;
1323
Brian Fosterb29c70f2014-12-04 09:43:17 +11001324 /*
1325 * Initialize the I/O completion workqueue if we haven't yet or the
1326 * submitter has not opted to specify a custom one.
1327 */
1328 if (!bp->b_ioend_wq)
1329 bp->b_ioend_wq = bp->b_target->bt_mount->m_buf_workqueue;
1330
Dave Chinner3e85c862012-06-22 18:50:09 +10001331 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001332 op = REQ_OP_WRITE;
Dave Chinner3e85c862012-06-22 18:50:09 +10001333 if (bp->b_flags & XBF_SYNCIO)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001334 op_flags = REQ_SYNC;
Dave Chinner3e85c862012-06-22 18:50:09 +10001335 if (bp->b_flags & XBF_FUA)
Mike Christie50bfcd02016-06-05 14:31:57 -05001336 op_flags |= REQ_FUA;
Dave Chinner3e85c862012-06-22 18:50:09 +10001337 if (bp->b_flags & XBF_FLUSH)
Mike Christie28a8f0d2016-06-05 14:32:25 -05001338 op_flags |= REQ_PREFLUSH;
Dave Chinner1813dd62012-11-14 17:54:40 +11001339
1340 /*
1341 * Run the write verifier callback function if it exists. If
1342 * this function fails it will mark the buffer with an error and
1343 * the IO should not be dispatched.
1344 */
1345 if (bp->b_ops) {
1346 bp->b_ops->verify_write(bp);
1347 if (bp->b_error) {
1348 xfs_force_shutdown(bp->b_target->bt_mount,
1349 SHUTDOWN_CORRUPT_INCORE);
1350 return;
1351 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001352 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
1353 struct xfs_mount *mp = bp->b_target->bt_mount;
1354
1355 /*
1356 * non-crc filesystems don't attach verifiers during
1357 * log recovery, so don't warn for such filesystems.
1358 */
1359 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1360 xfs_warn(mp,
1361 "%s: no ops on block 0x%llx/0x%x",
1362 __func__, bp->b_bn, bp->b_length);
1363 xfs_hex_dump(bp->b_addr, 64);
1364 dump_stack();
1365 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001366 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001367 } else if (bp->b_flags & XBF_READ_AHEAD) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001368 op = REQ_OP_READ;
1369 op_flags = REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001370 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001371 op = REQ_OP_READ;
Dave Chinner3e85c862012-06-22 18:50:09 +10001372 }
1373
1374 /* we only use the buffer cache for meta-data */
Mike Christie50bfcd02016-06-05 14:31:57 -05001375 op_flags |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001376
1377 /*
1378 * Walk all the vectors issuing IO on them. Set up the initial offset
1379 * into the buffer and the desired IO size before we start -
1380 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1381 * subsequent call.
1382 */
1383 offset = bp->b_offset;
1384 size = BBTOB(bp->b_io_length);
1385 blk_start_plug(&plug);
1386 for (i = 0; i < bp->b_map_count; i++) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001387 xfs_buf_ioapply_map(bp, i, &offset, &size, op, op_flags);
Dave Chinner3e85c862012-06-22 18:50:09 +10001388 if (bp->b_error)
1389 break;
1390 if (size <= 0)
1391 break; /* all done */
1392 }
1393 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
Dave Chinner595bff72014-10-02 09:05:14 +10001396/*
1397 * Asynchronous IO submission path. This transfers the buffer lock ownership and
1398 * the current reference to the IO. It is not safe to reference the buffer after
1399 * a call to this function unless the caller holds an additional reference
1400 * itself.
1401 */
Dave Chinner0e95f192012-04-23 15:58:46 +10001402void
Dave Chinner595bff72014-10-02 09:05:14 +10001403xfs_buf_submit(
1404 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Dave Chinner595bff72014-10-02 09:05:14 +10001406 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001408 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff72014-10-02 09:05:14 +10001409 ASSERT(bp->b_flags & XBF_ASYNC);
1410
1411 /* on shutdown we stale and complete the buffer immediately */
1412 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
1413 xfs_buf_ioerror(bp, -EIO);
1414 bp->b_flags &= ~XBF_DONE;
1415 xfs_buf_stale(bp);
1416 xfs_buf_ioend(bp);
1417 return;
1418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Christoph Hellwig375ec692011-08-23 08:28:03 +00001420 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001421 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Dave Chinner61be9c52014-10-02 09:04:31 +10001423 /* clear the internal error state to avoid spurious errors */
1424 bp->b_io_error = 0;
1425
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001426 /*
Dave Chinner595bff72014-10-02 09:05:14 +10001427 * The caller's reference is released during I/O completion.
1428 * This occurs some time after the last b_io_remaining reference is
1429 * released, so after we drop our Io reference we have to have some
1430 * other reference to ensure the buffer doesn't go away from underneath
1431 * us. Take a direct reference to ensure we have safe access to the
1432 * buffer until we are finished with it.
Dave Chinnere11bb802014-10-02 09:04:11 +10001433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 xfs_buf_hold(bp);
1435
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001436 /*
Dave Chinnere11bb802014-10-02 09:04:11 +10001437 * Set the count to 1 initially, this will stop an I/O completion
1438 * callout which happens before we have started all the I/O from calling
1439 * xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001441 atomic_set(&bp->b_io_remaining, 1);
Brian Foster9c7504a2016-07-20 11:15:28 +10001442 xfs_buf_ioacct_inc(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001443 _xfs_buf_ioapply(bp);
Dave Chinnere11bb802014-10-02 09:04:11 +10001444
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001445 /*
Dave Chinner595bff72014-10-02 09:05:14 +10001446 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1447 * reference we took above. If we drop it to zero, run completion so
1448 * that we don't return to the caller with completion still pending.
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001449 */
Dave Chinnere8aaba92014-10-02 09:04:22 +10001450 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
Dave Chinner595bff72014-10-02 09:05:14 +10001451 if (bp->b_error)
Dave Chinnere8aaba92014-10-02 09:04:22 +10001452 xfs_buf_ioend(bp);
1453 else
1454 xfs_buf_ioend_async(bp);
1455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Nathan Scottce8e9222006-01-11 15:39:08 +11001457 xfs_buf_rele(bp);
Dave Chinner595bff72014-10-02 09:05:14 +10001458 /* Note: it is not safe to reference bp now we've dropped our ref */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461/*
Dave Chinner595bff72014-10-02 09:05:14 +10001462 * Synchronous buffer IO submission path, read or write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 */
1464int
Dave Chinner595bff72014-10-02 09:05:14 +10001465xfs_buf_submit_wait(
1466 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Dave Chinner595bff72014-10-02 09:05:14 +10001468 int error;
1469
1470 trace_xfs_buf_submit_wait(bp, _RET_IP_);
1471
1472 ASSERT(!(bp->b_flags & (_XBF_DELWRI_Q | XBF_ASYNC)));
1473
1474 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
1475 xfs_buf_ioerror(bp, -EIO);
1476 xfs_buf_stale(bp);
1477 bp->b_flags &= ~XBF_DONE;
1478 return -EIO;
1479 }
1480
1481 if (bp->b_flags & XBF_WRITE)
1482 xfs_buf_wait_unpin(bp);
1483
1484 /* clear the internal error state to avoid spurious errors */
1485 bp->b_io_error = 0;
1486
1487 /*
1488 * For synchronous IO, the IO does not inherit the submitters reference
1489 * count, nor the buffer lock. Hence we cannot release the reference we
1490 * are about to take until we've waited for all IO completion to occur,
1491 * including any xfs_buf_ioend_async() work that may be pending.
1492 */
1493 xfs_buf_hold(bp);
1494
1495 /*
1496 * Set the count to 1 initially, this will stop an I/O completion
1497 * callout which happens before we have started all the I/O from calling
1498 * xfs_buf_ioend too early.
1499 */
1500 atomic_set(&bp->b_io_remaining, 1);
1501 _xfs_buf_ioapply(bp);
1502
1503 /*
1504 * make sure we run completion synchronously if it raced with us and is
1505 * already complete.
1506 */
1507 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1508 xfs_buf_ioend(bp);
1509
1510 /* wait for completion before gathering the error from the buffer */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001511 trace_xfs_buf_iowait(bp, _RET_IP_);
Dave Chinner595bff72014-10-02 09:05:14 +10001512 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001513 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Dave Chinner595bff72014-10-02 09:05:14 +10001514 error = bp->b_error;
1515
1516 /*
1517 * all done now, we can release the hold that keeps the buffer
1518 * referenced for the entire IO.
1519 */
1520 xfs_buf_rele(bp);
1521 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001524void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001525xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001526 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 size_t offset)
1528{
1529 struct page *page;
1530
Dave Chinner611c9942012-04-23 15:59:07 +10001531 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001532 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Nathan Scottce8e9222006-01-11 15:39:08 +11001534 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001535 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001536 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
1539/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 * Move data into or out of a buffer.
1541 */
1542void
Nathan Scottce8e9222006-01-11 15:39:08 +11001543xfs_buf_iomove(
1544 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 size_t boff, /* starting buffer offset */
1546 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001547 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001548 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Dave Chinner795cac72012-04-23 15:58:53 +10001550 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 bend = boff + bsize;
1553 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001554 struct page *page;
1555 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Dave Chinner795cac72012-04-23 15:58:53 +10001557 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1558 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1559 page = bp->b_pages[page_index];
1560 csize = min_t(size_t, PAGE_SIZE - page_offset,
1561 BBTOB(bp->b_io_length) - boff);
1562
1563 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001566 case XBRW_ZERO:
Dave Chinner795cac72012-04-23 15:58:53 +10001567 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001569 case XBRW_READ:
Dave Chinner795cac72012-04-23 15:58:53 +10001570 memcpy(data, page_address(page) + page_offset, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001572 case XBRW_WRITE:
Dave Chinner795cac72012-04-23 15:58:53 +10001573 memcpy(page_address(page) + page_offset, data, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575
1576 boff += csize;
1577 data += csize;
1578 }
1579}
1580
1581/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001582 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 */
1584
1585/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001586 * Wait for any bufs with callbacks that have been submitted but have not yet
1587 * returned. These buffers will have an elevated hold count, so wait on those
1588 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001590static enum lru_status
1591xfs_buftarg_wait_rele(
1592 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001593 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001594 spinlock_t *lru_lock,
1595 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Dave Chinnere80dfa12013-08-28 10:18:05 +10001597{
1598 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001599 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001600
1601 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001602 /* need to wait, so skip it this pass */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001603 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001604 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
Dave Chinnera4082352013-08-28 10:18:06 +10001606 if (!spin_trylock(&bp->b_lock))
1607 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001608
Dave Chinnera4082352013-08-28 10:18:06 +10001609 /*
1610 * clear the LRU reference count so the buffer doesn't get
1611 * ignored in xfs_buf_rele().
1612 */
1613 atomic_set(&bp->b_lru_ref, 0);
1614 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001615 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001616 spin_unlock(&bp->b_lock);
1617 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Dave Chinnere80dfa12013-08-28 10:18:05 +10001620void
1621xfs_wait_buftarg(
1622 struct xfs_buftarg *btp)
1623{
Dave Chinnera4082352013-08-28 10:18:06 +10001624 LIST_HEAD(dispose);
1625 int loop = 0;
1626
Dave Chinner85bec542016-01-19 08:28:10 +11001627 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001628 * First wait on the buftarg I/O count for all in-flight buffers to be
1629 * released. This is critical as new buffers do not make the LRU until
1630 * they are released.
1631 *
1632 * Next, flush the buffer workqueue to ensure all completion processing
1633 * has finished. Just waiting on buffer locks is not sufficient for
1634 * async IO as the reference count held over IO is not released until
1635 * after the buffer lock is dropped. Hence we need to ensure here that
1636 * all reference counts have been dropped before we start walking the
1637 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001638 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001639 while (percpu_counter_sum(&btp->bt_io_count))
1640 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001641 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Dave Chinner85bec542016-01-19 08:28:10 +11001642
Dave Chinnera4082352013-08-28 10:18:06 +10001643 /* loop until there is nothing left on the lru list. */
1644 while (list_lru_count(&btp->bt_lru)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001645 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001646 &dispose, LONG_MAX);
1647
1648 while (!list_empty(&dispose)) {
1649 struct xfs_buf *bp;
1650 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1651 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001652 if (bp->b_flags & XBF_WRITE_FAIL) {
1653 xfs_alert(btp->bt_mount,
Joe Perchesf41febd2015-07-29 11:52:04 +10001654"Corruption Alert: Buffer at block 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001655 (long long)bp->b_bn);
Joe Perchesf41febd2015-07-29 11:52:04 +10001656 xfs_alert(btp->bt_mount,
1657"Please run xfs_repair to determine the extent of the problem.");
Dave Chinnerac8809f2013-12-12 16:34:38 +11001658 }
Dave Chinnera4082352013-08-28 10:18:06 +10001659 xfs_buf_rele(bp);
1660 }
1661 if (loop++ != 0)
1662 delay(100);
1663 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001664}
1665
1666static enum lru_status
1667xfs_buftarg_isolate(
1668 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001669 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001670 spinlock_t *lru_lock,
1671 void *arg)
1672{
1673 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1674 struct list_head *dispose = arg;
1675
1676 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001677 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1678 * If we fail to get the lock, just skip it.
1679 */
1680 if (!spin_trylock(&bp->b_lock))
1681 return LRU_SKIP;
1682 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001683 * Decrement the b_lru_ref count unless the value is already
1684 * zero. If the value is already zero, we need to reclaim the
1685 * buffer, otherwise it gets another trip through the LRU.
1686 */
Dave Chinnera4082352013-08-28 10:18:06 +10001687 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1688 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001689 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001690 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001691
Dave Chinnera4082352013-08-28 10:18:06 +10001692 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001693 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001694 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001695 return LRU_REMOVED;
1696}
1697
Andrew Mortonaddbda42013-08-28 10:18:06 +10001698static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001699xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001700 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001701 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001702{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001703 struct xfs_buftarg *btp = container_of(shrink,
1704 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001705 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001706 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001707
Vladimir Davydov503c3582015-02-12 14:58:47 -08001708 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1709 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001710
1711 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001712 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001713 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1714 list_del_init(&bp->b_lru);
1715 xfs_buf_rele(bp);
1716 }
1717
Dave Chinnere80dfa12013-08-28 10:18:05 +10001718 return freed;
1719}
1720
Andrew Mortonaddbda42013-08-28 10:18:06 +10001721static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001722xfs_buftarg_shrink_count(
1723 struct shrinker *shrink,
1724 struct shrink_control *sc)
1725{
1726 struct xfs_buftarg *btp = container_of(shrink,
1727 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001728 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001729}
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731void
1732xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001733 struct xfs_mount *mp,
1734 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001736 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001737 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1738 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001739 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001740
Dave Chinner2291dab2016-12-09 16:49:54 +11001741 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001742
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001743 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001746int
1747xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001749 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001751 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001752 btp->bt_meta_sectorsize = sectorsize;
1753 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Nathan Scottce8e9222006-01-11 15:39:08 +11001755 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001756 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001757 "Cannot set_blocksize to %u on device %pg",
1758 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001759 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001762 /* Set up device logical sector size mask */
1763 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1764 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return 0;
1767}
1768
1769/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001770 * When allocating the initial buffer target we have not yet
1771 * read in the superblock, so don't know what sized sectors
1772 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001773 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774STATIC int
1775xfs_setsize_buftarg_early(
1776 xfs_buftarg_t *btp,
1777 struct block_device *bdev)
1778{
Eric Sandeena96c4152014-04-14 19:00:29 +10001779 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782xfs_buftarg_t *
1783xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001784 struct xfs_mount *mp,
Eric Sandeen34dcefd2014-04-14 19:01:00 +10001785 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 xfs_buftarg_t *btp;
1788
Dave Chinnerb17cb362013-05-20 09:51:12 +10001789 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Dave Chinnerebad8612010-09-22 10:47:20 +10001791 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001792 btp->bt_dev = bdev->bd_dev;
1793 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (xfs_setsize_buftarg_early(btp, bdev))
1796 goto error;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001797
1798 if (list_lru_init(&btp->bt_lru))
1799 goto error;
1800
Brian Foster9c7504a2016-07-20 11:15:28 +10001801 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
1802 goto error;
1803
Dave Chinnere80dfa12013-08-28 10:18:05 +10001804 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1805 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001806 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001807 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001808 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return btp;
1810
1811error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001812 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return NULL;
1814}
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001817 * Add a buffer to the delayed write list.
1818 *
1819 * This queues a buffer for writeout if it hasn't already been. Note that
1820 * neither this routine nor the buffer list submission functions perform
1821 * any internal synchronization. It is expected that the lists are thread-local
1822 * to the callers.
1823 *
1824 * Returns true if we queued up the buffer, or false if it already had
1825 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001827bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001828xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001829 struct xfs_buf *bp,
1830 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001832 ASSERT(xfs_buf_islocked(bp));
1833 ASSERT(!(bp->b_flags & XBF_READ));
1834
1835 /*
1836 * If the buffer is already marked delwri it already is queued up
1837 * by someone else for imediate writeout. Just ignore it in that
1838 * case.
1839 */
1840 if (bp->b_flags & _XBF_DELWRI_Q) {
1841 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1842 return false;
1843 }
David Chinnera6867a62006-01-11 15:37:58 +11001844
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001845 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1846
Dave Chinnerd808f612010-02-02 10:13:42 +11001847 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001848 * If a buffer gets written out synchronously or marked stale while it
1849 * is on a delwri list we lazily remove it. To do this, the other party
1850 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1851 * It remains referenced and on the list. In a rare corner case it
1852 * might get readded to a delwri list after the synchronous writeout, in
1853 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001854 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001855 bp->b_flags |= _XBF_DELWRI_Q;
1856 if (list_empty(&bp->b_list)) {
1857 atomic_inc(&bp->b_hold);
1858 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001859 }
David Chinner585e6d82007-02-10 18:32:29 +11001860
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001861 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001862}
1863
Dave Chinner089716a2010-01-26 15:13:25 +11001864/*
1865 * Compare function is more complex than it needs to be because
1866 * the return value is only 32 bits and we are doing comparisons
1867 * on 64 bit values
1868 */
1869static int
1870xfs_buf_cmp(
1871 void *priv,
1872 struct list_head *a,
1873 struct list_head *b)
1874{
1875 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1876 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1877 xfs_daddr_t diff;
1878
Mark Tinguelyf4b42422012-12-04 17:18:02 -06001879 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11001880 if (diff < 0)
1881 return -1;
1882 if (diff > 0)
1883 return 1;
1884 return 0;
1885}
1886
Dave Chinner26f1fe82016-06-01 17:38:15 +10001887/*
1888 * submit buffers for write.
1889 *
1890 * When we have a large buffer list, we do not want to hold all the buffers
1891 * locked while we block on the request queue waiting for IO dispatch. To avoid
1892 * this problem, we lock and submit buffers in groups of 50, thereby minimising
1893 * the lock hold times for lists which may contain thousands of objects.
1894 *
1895 * To do this, we sort the buffer list before we walk the list to lock and
1896 * submit buffers, and we plug and unplug around each group of buffers we
1897 * submit.
1898 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001899static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10001900xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001901 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10001902 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001904 struct xfs_buf *bp, *n;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001905 LIST_HEAD (submit_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001906 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001907 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Dave Chinner26f1fe82016-06-01 17:38:15 +10001909 list_sort(NULL, buffer_list, xfs_buf_cmp);
1910
1911 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001912 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10001913 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001914 if (xfs_buf_ispinned(bp)) {
1915 pinned++;
1916 continue;
1917 }
1918 if (!xfs_buf_trylock(bp))
1919 continue;
1920 } else {
1921 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001924 /*
1925 * Someone else might have written the buffer synchronously or
1926 * marked it stale in the meantime. In that case only the
1927 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1928 * reference and remove it from the list here.
1929 */
1930 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1931 list_del_init(&bp->b_list);
1932 xfs_buf_relse(bp);
1933 continue;
1934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001936 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001937
Dave Chinnercf53e992014-10-02 09:04:01 +10001938 /*
Dave Chinner26f1fe82016-06-01 17:38:15 +10001939 * We do all IO submission async. This means if we need
1940 * to wait for IO completion we need to take an extra
1941 * reference so the buffer is still valid on the other
1942 * side. We need to move the buffer onto the io_list
1943 * at this point so the caller can still access it.
Dave Chinnercf53e992014-10-02 09:04:01 +10001944 */
Dave Chinnerbbfeb612016-07-20 11:53:35 +10001945 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL);
Dave Chinner26f1fe82016-06-01 17:38:15 +10001946 bp->b_flags |= XBF_WRITE | XBF_ASYNC;
1947 if (wait_list) {
Dave Chinnercf53e992014-10-02 09:04:01 +10001948 xfs_buf_hold(bp);
Dave Chinner26f1fe82016-06-01 17:38:15 +10001949 list_move_tail(&bp->b_list, wait_list);
1950 } else
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001951 list_del_init(&bp->b_list);
Dave Chinner8dac3922014-10-02 09:04:40 +10001952
Dave Chinner595bff72014-10-02 09:05:14 +10001953 xfs_buf_submit(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001955 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001957 return pinned;
1958}
Nathan Scottf07c2252006-09-28 10:52:15 +10001959
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001960/*
1961 * Write out a buffer list asynchronously.
1962 *
1963 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1964 * out and not wait for I/O completion on any of the buffers. This interface
1965 * is only safely useable for callers that can track I/O completion by higher
1966 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1967 * function.
1968 */
1969int
1970xfs_buf_delwri_submit_nowait(
1971 struct list_head *buffer_list)
1972{
Dave Chinner26f1fe82016-06-01 17:38:15 +10001973 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001974}
1975
1976/*
1977 * Write out a buffer list synchronously.
1978 *
1979 * This will take the @buffer_list, write all buffers out and wait for I/O
1980 * completion on all of the buffers. @buffer_list is consumed by the function,
1981 * so callers must have some other way of tracking buffers if they require such
1982 * functionality.
1983 */
1984int
1985xfs_buf_delwri_submit(
1986 struct list_head *buffer_list)
1987{
Dave Chinner26f1fe82016-06-01 17:38:15 +10001988 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001989 int error = 0, error2;
1990 struct xfs_buf *bp;
1991
Dave Chinner26f1fe82016-06-01 17:38:15 +10001992 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001993
1994 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10001995 while (!list_empty(&wait_list)) {
1996 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001997
1998 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10001999
2000 /* locking the buffer will wait for async IO completion. */
2001 xfs_buf_lock(bp);
2002 error2 = bp->b_error;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002003 xfs_buf_relse(bp);
2004 if (!error)
2005 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
2007
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002008 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002011int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002012xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
Nathan Scott87582802006-03-14 13:18:19 +11002014 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
2015 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002016 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002017 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002018
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002021 out:
Nathan Scott87582802006-03-14 13:18:19 +11002022 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025void
Nathan Scottce8e9222006-01-11 15:39:08 +11002026xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Nathan Scottce8e9222006-01-11 15:39:08 +11002028 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}