blob: 8bbec20aa13852c2d8aea4bb6adfe19a4bd54e5b [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 Gleixnera731cd12010-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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return error;
426}
427
428/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300429 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
431STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100432_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 xfs_buf_t *bp,
434 uint flags)
435{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100436 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100437 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100438 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100439 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
Dave Chinner611c9942012-04-23 15:59:07 +1000440 } else if (flags & XBF_UNMAPPED) {
441 bp->b_addr = NULL;
442 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100443 int retried = 0;
Dave Chinnerae687e52014-03-07 16:19:14 +1100444 unsigned noio_flag;
Dave Chinnera19fb382011-03-26 09:13:42 +1100445
Dave Chinnerae687e52014-03-07 16:19:14 +1100446 /*
447 * vm_map_ram() will allocate auxillary structures (e.g.
448 * pagetables) with GFP_KERNEL, yet we are likely to be under
449 * GFP_NOFS context here. Hence we need to tell memory reclaim
450 * that we are in such a context via PF_MEMALLOC_NOIO to prevent
451 * memory reclaim re-entering the filesystem here and
452 * potentially deadlocking.
453 */
454 noio_flag = memalloc_noio_save();
Dave Chinnera19fb382011-03-26 09:13:42 +1100455 do {
456 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
457 -1, PAGE_KERNEL);
458 if (bp->b_addr)
459 break;
460 vm_unmap_aliases();
461 } while (retried++ <= 1);
Dave Chinnerae687e52014-03-07 16:19:14 +1100462 memalloc_noio_restore(noio_flag);
Dave Chinnera19fb382011-03-26 09:13:42 +1100463
464 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100466 bp->b_addr += bp->b_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
469 return 0;
470}
471
472/*
473 * Finding and Reading Buffers
474 */
Lucas Stach6031e732016-12-07 17:36:36 +1100475static int
476_xfs_buf_obj_cmp(
477 struct rhashtable_compare_arg *arg,
478 const void *obj)
479{
480 const struct xfs_buf_map *map = arg->key;
481 const struct xfs_buf *bp = obj;
482
483 /*
484 * The key hashing in the lookup path depends on the key being the
485 * first element of the compare_arg, make sure to assert this.
486 */
487 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
488
489 if (bp->b_bn != map->bm_bn)
490 return 1;
491
492 if (unlikely(bp->b_length != map->bm_len)) {
493 /*
494 * found a block number match. If the range doesn't
495 * match, the only way this is allowed is if the buffer
496 * in the cache is stale and the transaction that made
497 * it stale has not yet committed. i.e. we are
498 * reallocating a busy extent. Skip this buffer and
499 * continue searching for an exact match.
500 */
501 ASSERT(bp->b_flags & XBF_STALE);
502 return 1;
503 }
504 return 0;
505}
506
507static const struct rhashtable_params xfs_buf_hash_params = {
508 .min_size = 32, /* empty AGs have minimal footprint */
509 .nelem_hint = 16,
510 .key_len = sizeof(xfs_daddr_t),
511 .key_offset = offsetof(struct xfs_buf, b_bn),
512 .head_offset = offsetof(struct xfs_buf, b_rhash_head),
513 .automatic_shrinking = true,
514 .obj_cmpfn = _xfs_buf_obj_cmp,
515};
516
517int
518xfs_buf_hash_init(
519 struct xfs_perag *pag)
520{
521 spin_lock_init(&pag->pag_buf_lock);
522 return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
523}
524
525void
526xfs_buf_hash_destroy(
527 struct xfs_perag *pag)
528{
529 rhashtable_destroy(&pag->pag_buf_hash);
530}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100533 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 * a given range of an inode. The buffer is returned
Chandra Seetharamaneabbaf12011-09-08 20:18:50 +0000535 * locked. No I/O is implied by this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 */
537xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100538_xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000539 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000540 struct xfs_buf_map *map,
541 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100542 xfs_buf_flags_t flags,
543 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Dave Chinner74f75a02010-09-24 19:59:04 +1000545 struct xfs_perag *pag;
Dave Chinner74f75a02010-09-24 19:59:04 +1000546 xfs_buf_t *bp;
Lucas Stach6031e732016-12-07 17:36:36 +1100547 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
Dave Chinner10616b802013-01-21 23:53:52 +1100548 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000549 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Dave Chinner3e85c862012-06-22 18:50:09 +1000551 for (i = 0; i < nmaps; i++)
Lucas Stach6031e732016-12-07 17:36:36 +1100552 cmap.bm_len += map[i].bm_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* Check for IOs smaller than the sector size / not sector aligned */
Lucas Stach6031e732016-12-07 17:36:36 +1100555 ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
556 ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Dave Chinner10616b802013-01-21 23:53:52 +1100558 /*
559 * Corrupted block numbers can get through to here, unfortunately, so we
560 * have to check that the buffer falls within the filesystem bounds.
561 */
562 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
Lucas Stach6031e732016-12-07 17:36:36 +1100563 if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
Dave Chinner10616b802013-01-21 23:53:52 +1100564 /*
Dave Chinner24513372014-06-25 14:58:08 +1000565 * XXX (dgc): we should really be returning -EFSCORRUPTED here,
Dave Chinner10616b802013-01-21 23:53:52 +1100566 * but none of the higher level infrastructure supports
567 * returning a specific error on buffer lookup failures.
568 */
569 xfs_alert(btp->bt_mount,
570 "%s: Block out of range: block 0x%llx, EOFS 0x%llx ",
Lucas Stach6031e732016-12-07 17:36:36 +1100571 __func__, cmap.bm_bn, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000572 WARN_ON(1);
Dave Chinner10616b802013-01-21 23:53:52 +1100573 return NULL;
574 }
575
Dave Chinner74f75a02010-09-24 19:59:04 +1000576 pag = xfs_perag_get(btp->bt_mount,
Lucas Stach6031e732016-12-07 17:36:36 +1100577 xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Dave Chinner74f75a02010-09-24 19:59:04 +1000579 spin_lock(&pag->pag_buf_lock);
Lucas Stach6031e732016-12-07 17:36:36 +1100580 bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
581 xfs_buf_hash_params);
582 if (bp) {
583 atomic_inc(&bp->b_hold);
584 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
587 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100588 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000589 /* the buffer keeps the perag reference until it is freed */
590 new_bp->b_pag = pag;
Lucas Stach6031e732016-12-07 17:36:36 +1100591 rhashtable_insert_fast(&pag->pag_buf_hash,
592 &new_bp->b_rhash_head,
593 xfs_buf_hash_params);
Dave Chinner74f75a02010-09-24 19:59:04 +1000594 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 } else {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100596 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000597 spin_unlock(&pag->pag_buf_lock);
598 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100600 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000603 spin_unlock(&pag->pag_buf_lock);
604 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200606 if (!xfs_buf_trylock(bp)) {
607 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100608 xfs_buf_rele(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100609 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
Nathan Scottce8e9222006-01-11 15:39:08 +1100610 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200612 xfs_buf_lock(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100613 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615
Dave Chinner0e6e8472011-03-26 09:16:45 +1100616 /*
617 * if the buffer is stale, clear all the external state associated with
618 * it. We need to keep flags such as how we allocated the buffer memory
619 * intact here.
620 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100621 if (bp->b_flags & XBF_STALE) {
622 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinnercfb02852012-11-12 22:54:19 +1100623 ASSERT(bp->b_iodone == NULL);
Dave Chinner611c9942012-04-23 15:59:07 +1000624 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100625 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000626 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000627
628 trace_xfs_buf_find(bp, flags, _RET_IP_);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100629 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
Nathan Scottce8e9222006-01-11 15:39:08 +1100630 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633/*
Dave Chinner38158322011-09-30 04:45:02 +0000634 * Assembles a buffer covering the specified range. The code is optimised for
635 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
636 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Dave Chinner38158322011-09-30 04:45:02 +0000638struct xfs_buf *
Dave Chinner6dde2702012-06-22 18:50:10 +1000639xfs_buf_get_map(
640 struct xfs_buftarg *target,
641 struct xfs_buf_map *map,
642 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100643 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Dave Chinner38158322011-09-30 04:45:02 +0000645 struct xfs_buf *bp;
646 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100647 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Dave Chinner6dde2702012-06-22 18:50:10 +1000649 bp = _xfs_buf_find(target, map, nmaps, flags, NULL);
Dave Chinner38158322011-09-30 04:45:02 +0000650 if (likely(bp))
651 goto found;
652
Dave Chinner6dde2702012-06-22 18:50:10 +1000653 new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100654 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return NULL;
656
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000657 error = xfs_buf_allocate_memory(new_bp, flags);
658 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000659 xfs_buf_free(new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000660 return NULL;
661 }
662
Dave Chinner6dde2702012-06-22 18:50:10 +1000663 bp = _xfs_buf_find(target, map, nmaps, flags, new_bp);
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000664 if (!bp) {
665 xfs_buf_free(new_bp);
666 return NULL;
667 }
668
669 if (bp != new_bp)
670 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Dave Chinner38158322011-09-30 04:45:02 +0000672found:
Dave Chinner611c9942012-04-23 15:59:07 +1000673 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100674 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100676 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500677 "%s: failed to map pagesn", __func__);
Dave Chinnera8acad72012-04-23 15:58:54 +1000678 xfs_buf_relse(bp);
679 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681 }
682
Dave Chinnerb79f4a12016-01-12 07:03:44 +1100683 /*
684 * Clear b_error if this is a lookup from a caller that doesn't expect
685 * valid data to be found in the buffer.
686 */
687 if (!(flags & XBF_READ))
688 xfs_buf_ioerror(bp, 0);
689
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100690 XFS_STATS_INC(target->bt_mount, xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000691 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100692 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100695STATIC int
696_xfs_buf_read(
697 xfs_buf_t *bp,
698 xfs_buf_flags_t flags)
699{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000700 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600701 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100702
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000703 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200704 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100705
Dave Chinner595bff72014-10-02 09:05:14 +1000706 if (flags & XBF_ASYNC) {
707 xfs_buf_submit(bp);
Dave Chinner0e95f192012-04-23 15:58:46 +1000708 return 0;
Dave Chinner595bff72014-10-02 09:05:14 +1000709 }
710 return xfs_buf_submit_wait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713xfs_buf_t *
Dave Chinner6dde2702012-06-22 18:50:10 +1000714xfs_buf_read_map(
715 struct xfs_buftarg *target,
716 struct xfs_buf_map *map,
717 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100718 xfs_buf_flags_t flags,
Dave Chinner1813dd62012-11-14 17:54:40 +1100719 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Dave Chinner6dde2702012-06-22 18:50:10 +1000721 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Nathan Scottce8e9222006-01-11 15:39:08 +1100723 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Dave Chinner6dde2702012-06-22 18:50:10 +1000725 bp = xfs_buf_get_map(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100726 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000727 trace_xfs_buf_read(bp, flags, _RET_IP_);
728
Dave Chinnerb0388bf2016-02-10 15:01:11 +1100729 if (!(bp->b_flags & XBF_DONE)) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100730 XFS_STATS_INC(target->bt_mount, xb_get_read);
Dave Chinner1813dd62012-11-14 17:54:40 +1100731 bp->b_ops = ops;
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100732 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100733 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /*
735 * Read ahead call which is already satisfied,
736 * drop the buffer
737 */
Dave Chinnera8acad72012-04-23 15:58:54 +1000738 xfs_buf_relse(bp);
739 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100742 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744 }
745
Nathan Scottce8e9222006-01-11 15:39:08 +1100746 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100750 * If we are not low on memory then do the readahead in a deadlock
751 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
753void
Dave Chinner6dde2702012-06-22 18:50:10 +1000754xfs_buf_readahead_map(
755 struct xfs_buftarg *target,
756 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100757 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100758 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Jan Karaefa7c9f2017-02-02 15:56:53 +0100760 if (bdi_read_congested(target->bt_bdev->bd_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return;
762
Dave Chinner6dde2702012-06-22 18:50:10 +1000763 xfs_buf_read_map(target, map, nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100764 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Dave Chinner5adc94c2010-09-24 21:58:31 +1000767/*
768 * Read an uncached buffer from disk. Allocates and returns a locked
769 * buffer containing the disk contents or nothing.
770 */
Dave Chinnerba372672014-10-02 09:05:32 +1000771int
Dave Chinner5adc94c2010-09-24 21:58:31 +1000772xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000773 struct xfs_buftarg *target,
774 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000775 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100776 int flags,
Dave Chinnerba372672014-10-02 09:05:32 +1000777 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100778 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000779{
Dave Chinnereab4e632012-11-12 22:54:02 +1100780 struct xfs_buf *bp;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000781
Dave Chinnerba372672014-10-02 09:05:32 +1000782 *bpp = NULL;
783
Dave Chinnere70b73f2012-04-23 15:58:49 +1000784 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000785 if (!bp)
Dave Chinnerba372672014-10-02 09:05:32 +1000786 return -ENOMEM;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000787
788 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000789 ASSERT(bp->b_map_count == 1);
Dave Chinnerba372672014-10-02 09:05:32 +1000790 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
Dave Chinner3e85c862012-06-22 18:50:09 +1000791 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000792 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100793 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000794
Dave Chinner595bff72014-10-02 09:05:14 +1000795 xfs_buf_submit_wait(bp);
Dave Chinnerba372672014-10-02 09:05:32 +1000796 if (bp->b_error) {
797 int error = bp->b_error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800798 xfs_buf_relse(bp);
Dave Chinnerba372672014-10-02 09:05:32 +1000799 return error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800800 }
Dave Chinnerba372672014-10-02 09:05:32 +1000801
802 *bpp = bp;
803 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Dave Chinner44396472011-04-21 09:34:27 +0000806/*
807 * Return a buffer allocated as an empty buffer and associated to external
808 * memory via xfs_buf_associate_memory() back to it's empty state.
809 */
810void
811xfs_buf_set_empty(
812 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000813 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000814{
815 if (bp->b_pages)
816 _xfs_buf_free_pages(bp);
817
818 bp->b_pages = NULL;
819 bp->b_page_count = 0;
820 bp->b_addr = NULL;
Dave Chinner4e94b712012-04-23 15:58:51 +1000821 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000822 bp->b_io_length = numblks;
Dave Chinner3e85c862012-06-22 18:50:09 +1000823
824 ASSERT(bp->b_map_count == 1);
Dave Chinner44396472011-04-21 09:34:27 +0000825 bp->b_bn = XFS_BUF_DADDR_NULL;
Dave Chinner3e85c862012-06-22 18:50:09 +1000826 bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL;
827 bp->b_maps[0].bm_len = bp->b_length;
Dave Chinner44396472011-04-21 09:34:27 +0000828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830static inline struct page *
831mem_to_page(
832 void *addr)
833{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800834 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return virt_to_page(addr);
836 } else {
837 return vmalloc_to_page(addr);
838 }
839}
840
841int
Nathan Scottce8e9222006-01-11 15:39:08 +1100842xfs_buf_associate_memory(
843 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 void *mem,
845 size_t len)
846{
847 int rval;
848 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100849 unsigned long pageaddr;
850 unsigned long offset;
851 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int page_count;
853
Dave Chinner0e6e8472011-03-26 09:16:45 +1100854 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100855 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100856 buflen = PAGE_ALIGN(len + offset);
857 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100860 if (bp->b_pages)
861 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Nathan Scottce8e9222006-01-11 15:39:08 +1100863 bp->b_pages = NULL;
864 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Eric Sandeen87937bf2014-04-14 19:01:20 +1000866 rval = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (rval)
868 return rval;
869
Nathan Scottce8e9222006-01-11 15:39:08 +1100870 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100872 for (i = 0; i < bp->b_page_count; i++) {
873 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100874 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Dave Chinneraa0e8832012-04-23 15:58:52 +1000877 bp->b_io_length = BTOBB(len);
Dave Chinner4e94b712012-04-23 15:58:51 +1000878 bp->b_length = BTOBB(buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 return 0;
881}
882
883xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000884xfs_buf_get_uncached(
885 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000886 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000887 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000889 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000890 int error, i;
Dave Chinner3e85c862012-06-22 18:50:09 +1000891 struct xfs_buf *bp;
892 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Brian Fosterc891c302016-07-20 11:13:43 +1000894 /* flags might contain irrelevant bits, pass only what we care about */
895 bp = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (unlikely(bp == NULL))
897 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Dave Chinnere70b73f2012-04-23 15:58:49 +1000899 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000900 error = _xfs_buf_get_pages(bp, page_count);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000901 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 goto fail_free_buf;
903
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000904 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000905 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000906 if (!bp->b_pages[i])
907 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000909 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Dave Chinner611c9942012-04-23 15:59:07 +1000911 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000912 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100913 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500914 "%s: failed to map pages", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Dave Chinner686865f2010-09-24 20:07:47 +1000918 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000922 while (--i >= 0)
923 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000924 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 fail_free_buf:
Dave Chinner3e85c862012-06-22 18:50:09 +1000926 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000927 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 fail:
929 return NULL;
930}
931
932/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 * Increment reference count on buffer, to hold the buffer concurrently
934 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 * Must hold the buffer already to call this function.
936 */
937void
Nathan Scottce8e9222006-01-11 15:39:08 +1100938xfs_buf_hold(
939 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000941 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100942 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
945/*
Brian Foster9c7504a2016-07-20 11:15:28 +1000946 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
947 * placed on LRU or freed (depending on b_lru_ref).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
949void
Nathan Scottce8e9222006-01-11 15:39:08 +1100950xfs_buf_rele(
951 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Dave Chinner74f75a02010-09-24 19:59:04 +1000953 struct xfs_perag *pag = bp->b_pag;
Brian Foster9c7504a2016-07-20 11:15:28 +1000954 bool release;
955 bool freebuf = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000957 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Dave Chinner74f75a02010-09-24 19:59:04 +1000959 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100960 ASSERT(list_empty(&bp->b_lru));
Brian Foster9c7504a2016-07-20 11:15:28 +1000961 if (atomic_dec_and_test(&bp->b_hold)) {
962 xfs_buf_ioacct_dec(bp);
Nathan Scottfad3aa12006-02-01 12:14:52 +1100963 xfs_buf_free(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +1000964 }
Nathan Scottfad3aa12006-02-01 12:14:52 +1100965 return;
966 }
967
Lachlan McIlroy37906892008-08-13 15:42:10 +1000968 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinnera4082352013-08-28 10:18:06 +1000969
Brian Foster9c7504a2016-07-20 11:15:28 +1000970 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
971 spin_lock(&bp->b_lock);
972 if (!release) {
973 /*
974 * Drop the in-flight state if the buffer is already on the LRU
975 * and it holds the only reference. This is racy because we
976 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
977 * ensures the decrement occurs only once per-buf.
978 */
979 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
980 xfs_buf_ioacct_dec(bp);
981 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
Brian Foster9c7504a2016-07-20 11:15:28 +1000983
984 /* the last reference has been dropped ... */
985 xfs_buf_ioacct_dec(bp);
986 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
987 /*
988 * If the buffer is added to the LRU take a new reference to the
989 * buffer for the LRU and clear the (now stale) dispose list
990 * state flag
991 */
992 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
993 bp->b_state &= ~XFS_BSTATE_DISPOSE;
994 atomic_inc(&bp->b_hold);
995 }
996 spin_unlock(&pag->pag_buf_lock);
997 } else {
998 /*
999 * most of the time buffers will already be removed from the
1000 * LRU, so optimise that case by checking for the
1001 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1002 * was on was the disposal list
1003 */
1004 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1005 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1006 } else {
1007 ASSERT(list_empty(&bp->b_lru));
1008 }
1009
1010 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Lucas Stach6031e732016-12-07 17:36:36 +11001011 rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
1012 xfs_buf_hash_params);
Brian Foster9c7504a2016-07-20 11:15:28 +10001013 spin_unlock(&pag->pag_buf_lock);
1014 xfs_perag_put(pag);
1015 freebuf = true;
1016 }
1017
1018out_unlock:
1019 spin_unlock(&bp->b_lock);
1020
1021 if (freebuf)
1022 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025
1026/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001027 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +11001028 *
1029 * If we come across a stale, pinned, locked buffer, we know that we are
1030 * being asked to lock a buffer that has been reallocated. Because it is
1031 * pinned, we know that the log has not been pushed to disk and hence it
1032 * will still be locked. Rather than continuing to have trylock attempts
1033 * fail until someone else pushes the log, push it ourselves before
1034 * returning. This means that the xfsaild will not get stuck trying
1035 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
1037int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001038xfs_buf_trylock(
1039 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 int locked;
1042
Nathan Scottce8e9222006-01-11 15:39:08 +11001043 locked = down_trylock(&bp->b_sema) == 0;
Darrick J. Wong479c6412016-06-21 11:53:28 +10001044 if (locked) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001045 XB_SET_OWNER(bp);
Darrick J. Wong479c6412016-06-21 11:53:28 +10001046 trace_xfs_buf_trylock(bp, _RET_IP_);
1047 } else {
1048 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
1049 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001050 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001054 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +10001055 *
1056 * If we come across a stale, pinned, locked buffer, we know that we
1057 * are being asked to lock a buffer that has been reallocated. Because
1058 * it is pinned, we know that the log has not been pushed to disk and
1059 * hence it will still be locked. Rather than sleeping until someone
1060 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001062void
1063xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001064 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001066 trace_xfs_buf_lock(bp, _RET_IP_);
1067
Dave Chinnered3b4d62010-05-21 12:07:08 +10001068 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +10001069 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +11001070 down(&bp->b_sema);
1071 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001072
1073 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076void
Nathan Scottce8e9222006-01-11 15:39:08 +11001077xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001078 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Nathan Scottce8e9222006-01-11 15:39:08 +11001080 XB_CLEAR_OWNER(bp);
1081 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001082
1083 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
Nathan Scottce8e9222006-01-11 15:39:08 +11001086STATIC void
1087xfs_buf_wait_unpin(
1088 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 DECLARE_WAITQUEUE (wait, current);
1091
Nathan Scottce8e9222006-01-11 15:39:08 +11001092 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return;
1094
Nathan Scottce8e9222006-01-11 15:39:08 +11001095 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 for (;;) {
1097 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001098 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001100 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001102 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 set_current_state(TASK_RUNNING);
1104}
1105
1106/*
1107 * Buffer Utility Routines
1108 */
1109
Dave Chinnere8aaba92014-10-02 09:04:22 +10001110void
1111xfs_buf_ioend(
1112 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001114 bool read = bp->b_flags & XBF_READ;
1115
1116 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001117
1118 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Dave Chinnerd5929de2013-02-27 13:25:54 +11001119
Dave Chinner61be9c52014-10-02 09:04:31 +10001120 /*
1121 * Pull in IO completion errors now. We are guaranteed to be running
1122 * single threaded, so we don't need the lock to read b_io_error.
1123 */
1124 if (!bp->b_error && bp->b_io_error)
1125 xfs_buf_ioerror(bp, bp->b_io_error);
1126
Dave Chinnere8aaba92014-10-02 09:04:22 +10001127 /* Only validate buffers that were read without errors */
1128 if (read && !bp->b_error && bp->b_ops) {
1129 ASSERT(!bp->b_iodone);
Dave Chinner1813dd62012-11-14 17:54:40 +11001130 bp->b_ops->verify_read(bp);
Dave Chinnere8aaba92014-10-02 09:04:22 +10001131 }
1132
1133 if (!bp->b_error)
1134 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Christoph Hellwig80f6c292010-08-18 05:29:11 -04001136 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001137 (*(bp->b_iodone))(bp);
1138 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 xfs_buf_relse(bp);
Dave Chinner595bff72014-10-02 09:05:14 +10001140 else
Dave Chinner1813dd62012-11-14 17:54:40 +11001141 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Dave Chinnere8aaba92014-10-02 09:04:22 +10001144static void
1145xfs_buf_ioend_work(
1146 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001148 struct xfs_buf *bp =
Brian Fosterb29c70f2014-12-04 09:43:17 +11001149 container_of(work, xfs_buf_t, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001150
Dave Chinnere8aaba92014-10-02 09:04:22 +10001151 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001154static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001155xfs_buf_ioend_async(
1156 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001158 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
1159 queue_work(bp->b_ioend_wq, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162void
Nathan Scottce8e9222006-01-11 15:39:08 +11001163xfs_buf_ioerror(
1164 xfs_buf_t *bp,
1165 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Dave Chinner24513372014-06-25 14:58:08 +10001167 ASSERT(error <= 0 && error >= -1000);
1168 bp->b_error = error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001169 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Christoph Hellwig901796a2011-10-10 16:52:49 +00001172void
1173xfs_buf_ioerror_alert(
1174 struct xfs_buf *bp,
1175 const char *func)
1176{
1177 xfs_alert(bp->b_target->bt_mount,
Dave Chinneraa0e8832012-04-23 15:58:52 +10001178"metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
Dave Chinner24513372014-06-25 14:58:08 +10001179 (__uint64_t)XFS_BUF_ADDR(bp), func, -bp->b_error, bp->b_length);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001180}
1181
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001182int
1183xfs_bwrite(
1184 struct xfs_buf *bp)
1185{
1186 int error;
1187
1188 ASSERT(xfs_buf_islocked(bp));
1189
1190 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001191 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
1192 XBF_WRITE_FAIL | XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001193
Dave Chinner595bff72014-10-02 09:05:14 +10001194 error = xfs_buf_submit_wait(bp);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001195 if (error) {
1196 xfs_force_shutdown(bp->b_target->bt_mount,
1197 SHUTDOWN_META_IO_ERROR);
1198 }
1199 return error;
1200}
1201
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001202static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001203xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001204 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001206 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Dave Chinner37eb17e2012-11-12 22:09:46 +11001208 /*
1209 * don't overwrite existing errors - otherwise we can lose errors on
1210 * buffers that require multiple bios to complete.
1211 */
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001212 if (bio->bi_error)
1213 cmpxchg(&bp->b_io_error, 0, bio->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Dave Chinner37eb17e2012-11-12 22:09:46 +11001215 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001216 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1217
Dave Chinnere8aaba92014-10-02 09:04:22 +10001218 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1219 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Dave Chinner3e85c862012-06-22 18:50:09 +10001223static void
1224xfs_buf_ioapply_map(
1225 struct xfs_buf *bp,
1226 int map,
1227 int *buf_offset,
1228 int *count,
Mike Christie50bfcd02016-06-05 14:31:57 -05001229 int op,
1230 int op_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Dave Chinner3e85c862012-06-22 18:50:09 +10001232 int page_index;
1233 int total_nr_pages = bp->b_page_count;
1234 int nr_pages;
1235 struct bio *bio;
1236 sector_t sector = bp->b_maps[map].bm_bn;
1237 int size;
1238 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Nathan Scottce8e9222006-01-11 15:39:08 +11001240 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Dave Chinner3e85c862012-06-22 18:50:09 +10001242 /* skip the pages in the buffer before the start offset */
1243 page_index = 0;
1244 offset = *buf_offset;
1245 while (offset >= PAGE_SIZE) {
1246 page_index++;
1247 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001248 }
1249
Dave Chinner3e85c862012-06-22 18:50:09 +10001250 /*
1251 * Limit the IO size to the length of the current vector, and update the
1252 * remaining IO count for the next time around.
1253 */
1254 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1255 *count -= size;
1256 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001259 atomic_inc(&bp->b_io_remaining);
Ming Leic908e382016-05-30 21:34:33 +08001260 nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001263 bio->bi_bdev = bp->b_target->bt_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001264 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001265 bio->bi_end_io = xfs_buf_bio_end_io;
1266 bio->bi_private = bp;
Mike Christie50bfcd02016-06-05 14:31:57 -05001267 bio_set_op_attrs(bio, op, op_flags);
Dave Chinner0e6e8472011-03-26 09:16:45 +11001268
Dave Chinner3e85c862012-06-22 18:50:09 +10001269 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001270 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 if (nbytes > size)
1273 nbytes = size;
1274
Dave Chinner3e85c862012-06-22 18:50:09 +10001275 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1276 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001277 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 break;
1279
1280 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001281 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 size -= nbytes;
1283 total_nr_pages--;
1284 }
1285
Kent Overstreet4f024f32013-10-11 15:44:27 -07001286 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001287 if (xfs_buf_is_vmapped(bp)) {
1288 flush_kernel_vmap_range(bp->b_addr,
1289 xfs_buf_vmap_len(bp));
1290 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001291 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (size)
1293 goto next_chunk;
1294 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001295 /*
1296 * This is guaranteed not to be the last io reference count
Dave Chinner595bff72014-10-02 09:05:14 +10001297 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001298 */
1299 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001300 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001301 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001303
1304}
1305
1306STATIC void
1307_xfs_buf_ioapply(
1308 struct xfs_buf *bp)
1309{
1310 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001311 int op;
1312 int op_flags = 0;
Dave Chinner3e85c862012-06-22 18:50:09 +10001313 int offset;
1314 int size;
1315 int i;
1316
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001317 /*
1318 * Make sure we capture only current IO errors rather than stale errors
1319 * left over from previous use of the buffer (e.g. failed readahead).
1320 */
1321 bp->b_error = 0;
1322
Brian Fosterb29c70f2014-12-04 09:43:17 +11001323 /*
1324 * Initialize the I/O completion workqueue if we haven't yet or the
1325 * submitter has not opted to specify a custom one.
1326 */
1327 if (!bp->b_ioend_wq)
1328 bp->b_ioend_wq = bp->b_target->bt_mount->m_buf_workqueue;
1329
Dave Chinner3e85c862012-06-22 18:50:09 +10001330 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001331 op = REQ_OP_WRITE;
Dave Chinner3e85c862012-06-22 18:50:09 +10001332 if (bp->b_flags & XBF_SYNCIO)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001333 op_flags = REQ_SYNC;
Dave Chinner3e85c862012-06-22 18:50:09 +10001334 if (bp->b_flags & XBF_FUA)
Mike Christie50bfcd02016-06-05 14:31:57 -05001335 op_flags |= REQ_FUA;
Dave Chinner3e85c862012-06-22 18:50:09 +10001336 if (bp->b_flags & XBF_FLUSH)
Mike Christie28a8f0d2016-06-05 14:32:25 -05001337 op_flags |= REQ_PREFLUSH;
Dave Chinner1813dd62012-11-14 17:54:40 +11001338
1339 /*
1340 * Run the write verifier callback function if it exists. If
1341 * this function fails it will mark the buffer with an error and
1342 * the IO should not be dispatched.
1343 */
1344 if (bp->b_ops) {
1345 bp->b_ops->verify_write(bp);
1346 if (bp->b_error) {
1347 xfs_force_shutdown(bp->b_target->bt_mount,
1348 SHUTDOWN_CORRUPT_INCORE);
1349 return;
1350 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001351 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
1352 struct xfs_mount *mp = bp->b_target->bt_mount;
1353
1354 /*
1355 * non-crc filesystems don't attach verifiers during
1356 * log recovery, so don't warn for such filesystems.
1357 */
1358 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1359 xfs_warn(mp,
1360 "%s: no ops on block 0x%llx/0x%x",
1361 __func__, bp->b_bn, bp->b_length);
1362 xfs_hex_dump(bp->b_addr, 64);
1363 dump_stack();
1364 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001365 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001366 } else if (bp->b_flags & XBF_READ_AHEAD) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001367 op = REQ_OP_READ;
1368 op_flags = REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001369 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001370 op = REQ_OP_READ;
Dave Chinner3e85c862012-06-22 18:50:09 +10001371 }
1372
1373 /* we only use the buffer cache for meta-data */
Mike Christie50bfcd02016-06-05 14:31:57 -05001374 op_flags |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001375
1376 /*
1377 * Walk all the vectors issuing IO on them. Set up the initial offset
1378 * into the buffer and the desired IO size before we start -
1379 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1380 * subsequent call.
1381 */
1382 offset = bp->b_offset;
1383 size = BBTOB(bp->b_io_length);
1384 blk_start_plug(&plug);
1385 for (i = 0; i < bp->b_map_count; i++) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001386 xfs_buf_ioapply_map(bp, i, &offset, &size, op, op_flags);
Dave Chinner3e85c862012-06-22 18:50:09 +10001387 if (bp->b_error)
1388 break;
1389 if (size <= 0)
1390 break; /* all done */
1391 }
1392 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
Dave Chinner595bff72014-10-02 09:05:14 +10001395/*
1396 * Asynchronous IO submission path. This transfers the buffer lock ownership and
1397 * the current reference to the IO. It is not safe to reference the buffer after
1398 * a call to this function unless the caller holds an additional reference
1399 * itself.
1400 */
Dave Chinner0e95f192012-04-23 15:58:46 +10001401void
Dave Chinner595bff72014-10-02 09:05:14 +10001402xfs_buf_submit(
1403 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Dave Chinner595bff72014-10-02 09:05:14 +10001405 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001407 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff72014-10-02 09:05:14 +10001408 ASSERT(bp->b_flags & XBF_ASYNC);
1409
1410 /* on shutdown we stale and complete the buffer immediately */
1411 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
1412 xfs_buf_ioerror(bp, -EIO);
1413 bp->b_flags &= ~XBF_DONE;
1414 xfs_buf_stale(bp);
1415 xfs_buf_ioend(bp);
1416 return;
1417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Christoph Hellwig375ec692011-08-23 08:28:03 +00001419 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001420 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Dave Chinner61be9c52014-10-02 09:04:31 +10001422 /* clear the internal error state to avoid spurious errors */
1423 bp->b_io_error = 0;
1424
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001425 /*
Dave Chinner595bff72014-10-02 09:05:14 +10001426 * The caller's reference is released during I/O completion.
1427 * This occurs some time after the last b_io_remaining reference is
1428 * released, so after we drop our Io reference we have to have some
1429 * other reference to ensure the buffer doesn't go away from underneath
1430 * us. Take a direct reference to ensure we have safe access to the
1431 * buffer until we are finished with it.
Dave Chinnere11bb802014-10-02 09:04:11 +10001432 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 xfs_buf_hold(bp);
1434
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001435 /*
Dave Chinnere11bb802014-10-02 09:04:11 +10001436 * Set the count to 1 initially, this will stop an I/O completion
1437 * callout which happens before we have started all the I/O from calling
1438 * xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001440 atomic_set(&bp->b_io_remaining, 1);
Brian Foster9c7504a2016-07-20 11:15:28 +10001441 xfs_buf_ioacct_inc(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001442 _xfs_buf_ioapply(bp);
Dave Chinnere11bb802014-10-02 09:04:11 +10001443
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001444 /*
Dave Chinner595bff72014-10-02 09:05:14 +10001445 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1446 * reference we took above. If we drop it to zero, run completion so
1447 * that we don't return to the caller with completion still pending.
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001448 */
Dave Chinnere8aaba92014-10-02 09:04:22 +10001449 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
Dave Chinner595bff72014-10-02 09:05:14 +10001450 if (bp->b_error)
Dave Chinnere8aaba92014-10-02 09:04:22 +10001451 xfs_buf_ioend(bp);
1452 else
1453 xfs_buf_ioend_async(bp);
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Nathan Scottce8e9222006-01-11 15:39:08 +11001456 xfs_buf_rele(bp);
Dave Chinner595bff72014-10-02 09:05:14 +10001457 /* Note: it is not safe to reference bp now we've dropped our ref */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
1460/*
Dave Chinner595bff72014-10-02 09:05:14 +10001461 * Synchronous buffer IO submission path, read or write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 */
1463int
Dave Chinner595bff72014-10-02 09:05:14 +10001464xfs_buf_submit_wait(
1465 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Dave Chinner595bff72014-10-02 09:05:14 +10001467 int error;
1468
1469 trace_xfs_buf_submit_wait(bp, _RET_IP_);
1470
1471 ASSERT(!(bp->b_flags & (_XBF_DELWRI_Q | XBF_ASYNC)));
1472
1473 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
1474 xfs_buf_ioerror(bp, -EIO);
1475 xfs_buf_stale(bp);
1476 bp->b_flags &= ~XBF_DONE;
1477 return -EIO;
1478 }
1479
1480 if (bp->b_flags & XBF_WRITE)
1481 xfs_buf_wait_unpin(bp);
1482
1483 /* clear the internal error state to avoid spurious errors */
1484 bp->b_io_error = 0;
1485
1486 /*
1487 * For synchronous IO, the IO does not inherit the submitters reference
1488 * count, nor the buffer lock. Hence we cannot release the reference we
1489 * are about to take until we've waited for all IO completion to occur,
1490 * including any xfs_buf_ioend_async() work that may be pending.
1491 */
1492 xfs_buf_hold(bp);
1493
1494 /*
1495 * Set the count to 1 initially, this will stop an I/O completion
1496 * callout which happens before we have started all the I/O from calling
1497 * xfs_buf_ioend too early.
1498 */
1499 atomic_set(&bp->b_io_remaining, 1);
1500 _xfs_buf_ioapply(bp);
1501
1502 /*
1503 * make sure we run completion synchronously if it raced with us and is
1504 * already complete.
1505 */
1506 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1507 xfs_buf_ioend(bp);
1508
1509 /* wait for completion before gathering the error from the buffer */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001510 trace_xfs_buf_iowait(bp, _RET_IP_);
Dave Chinner595bff72014-10-02 09:05:14 +10001511 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001512 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Dave Chinner595bff72014-10-02 09:05:14 +10001513 error = bp->b_error;
1514
1515 /*
1516 * all done now, we can release the hold that keeps the buffer
1517 * referenced for the entire IO.
1518 */
1519 xfs_buf_rele(bp);
1520 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001523void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001524xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001525 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 size_t offset)
1527{
1528 struct page *page;
1529
Dave Chinner611c9942012-04-23 15:59:07 +10001530 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001531 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Nathan Scottce8e9222006-01-11 15:39:08 +11001533 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001534 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001535 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
1538/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 * Move data into or out of a buffer.
1540 */
1541void
Nathan Scottce8e9222006-01-11 15:39:08 +11001542xfs_buf_iomove(
1543 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 size_t boff, /* starting buffer offset */
1545 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001546 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001547 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Dave Chinner795cac72012-04-23 15:58:53 +10001549 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 bend = boff + bsize;
1552 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001553 struct page *page;
1554 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Dave Chinner795cac72012-04-23 15:58:53 +10001556 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1557 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1558 page = bp->b_pages[page_index];
1559 csize = min_t(size_t, PAGE_SIZE - page_offset,
1560 BBTOB(bp->b_io_length) - boff);
1561
1562 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001565 case XBRW_ZERO:
Dave Chinner795cac72012-04-23 15:58:53 +10001566 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001568 case XBRW_READ:
Dave Chinner795cac72012-04-23 15:58:53 +10001569 memcpy(data, page_address(page) + page_offset, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001571 case XBRW_WRITE:
Dave Chinner795cac72012-04-23 15:58:53 +10001572 memcpy(page_address(page) + page_offset, data, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574
1575 boff += csize;
1576 data += csize;
1577 }
1578}
1579
1580/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001581 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 */
1583
1584/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001585 * Wait for any bufs with callbacks that have been submitted but have not yet
1586 * returned. These buffers will have an elevated hold count, so wait on those
1587 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001589static enum lru_status
1590xfs_buftarg_wait_rele(
1591 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001592 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001593 spinlock_t *lru_lock,
1594 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Dave Chinnere80dfa12013-08-28 10:18:05 +10001596{
1597 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001598 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001599
1600 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001601 /* need to wait, so skip it this pass */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001602 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001603 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
Dave Chinnera4082352013-08-28 10:18:06 +10001605 if (!spin_trylock(&bp->b_lock))
1606 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001607
Dave Chinnera4082352013-08-28 10:18:06 +10001608 /*
1609 * clear the LRU reference count so the buffer doesn't get
1610 * ignored in xfs_buf_rele().
1611 */
1612 atomic_set(&bp->b_lru_ref, 0);
1613 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001614 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001615 spin_unlock(&bp->b_lock);
1616 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Dave Chinnere80dfa12013-08-28 10:18:05 +10001619void
1620xfs_wait_buftarg(
1621 struct xfs_buftarg *btp)
1622{
Dave Chinnera4082352013-08-28 10:18:06 +10001623 LIST_HEAD(dispose);
1624 int loop = 0;
1625
Dave Chinner85bec542016-01-19 08:28:10 +11001626 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001627 * First wait on the buftarg I/O count for all in-flight buffers to be
1628 * released. This is critical as new buffers do not make the LRU until
1629 * they are released.
1630 *
1631 * Next, flush the buffer workqueue to ensure all completion processing
1632 * has finished. Just waiting on buffer locks is not sufficient for
1633 * async IO as the reference count held over IO is not released until
1634 * after the buffer lock is dropped. Hence we need to ensure here that
1635 * all reference counts have been dropped before we start walking the
1636 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001637 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001638 while (percpu_counter_sum(&btp->bt_io_count))
1639 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001640 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Dave Chinner85bec542016-01-19 08:28:10 +11001641
Dave Chinnera4082352013-08-28 10:18:06 +10001642 /* loop until there is nothing left on the lru list. */
1643 while (list_lru_count(&btp->bt_lru)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001644 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001645 &dispose, LONG_MAX);
1646
1647 while (!list_empty(&dispose)) {
1648 struct xfs_buf *bp;
1649 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1650 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001651 if (bp->b_flags & XBF_WRITE_FAIL) {
1652 xfs_alert(btp->bt_mount,
Joe Perchesf41febd2015-07-29 11:52:04 +10001653"Corruption Alert: Buffer at block 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001654 (long long)bp->b_bn);
Joe Perchesf41febd2015-07-29 11:52:04 +10001655 xfs_alert(btp->bt_mount,
1656"Please run xfs_repair to determine the extent of the problem.");
Dave Chinnerac8809f2013-12-12 16:34:38 +11001657 }
Dave Chinnera4082352013-08-28 10:18:06 +10001658 xfs_buf_rele(bp);
1659 }
1660 if (loop++ != 0)
1661 delay(100);
1662 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001663}
1664
1665static enum lru_status
1666xfs_buftarg_isolate(
1667 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001668 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001669 spinlock_t *lru_lock,
1670 void *arg)
1671{
1672 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1673 struct list_head *dispose = arg;
1674
1675 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001676 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1677 * If we fail to get the lock, just skip it.
1678 */
1679 if (!spin_trylock(&bp->b_lock))
1680 return LRU_SKIP;
1681 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001682 * Decrement the b_lru_ref count unless the value is already
1683 * zero. If the value is already zero, we need to reclaim the
1684 * buffer, otherwise it gets another trip through the LRU.
1685 */
Dave Chinnera4082352013-08-28 10:18:06 +10001686 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1687 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001688 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001689 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001690
Dave Chinnera4082352013-08-28 10:18:06 +10001691 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001692 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001693 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001694 return LRU_REMOVED;
1695}
1696
Andrew Mortonaddbda42013-08-28 10:18:06 +10001697static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001698xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001699 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001700 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001701{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001702 struct xfs_buftarg *btp = container_of(shrink,
1703 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001704 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001705 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001706
Vladimir Davydov503c3582015-02-12 14:58:47 -08001707 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1708 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001709
1710 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001711 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001712 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1713 list_del_init(&bp->b_lru);
1714 xfs_buf_rele(bp);
1715 }
1716
Dave Chinnere80dfa12013-08-28 10:18:05 +10001717 return freed;
1718}
1719
Andrew Mortonaddbda42013-08-28 10:18:06 +10001720static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001721xfs_buftarg_shrink_count(
1722 struct shrinker *shrink,
1723 struct shrink_control *sc)
1724{
1725 struct xfs_buftarg *btp = container_of(shrink,
1726 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001727 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001728}
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730void
1731xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001732 struct xfs_mount *mp,
1733 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001735 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001736 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1737 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001738 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001739
Dave Chinner2291dab2016-12-09 16:49:54 +11001740 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001741
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001742 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001745int
1746xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001748 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001750 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001751 btp->bt_meta_sectorsize = sectorsize;
1752 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Nathan Scottce8e9222006-01-11 15:39:08 +11001754 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001755 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001756 "Cannot set_blocksize to %u on device %pg",
1757 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001758 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001761 /* Set up device logical sector size mask */
1762 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1763 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return 0;
1766}
1767
1768/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001769 * When allocating the initial buffer target we have not yet
1770 * read in the superblock, so don't know what sized sectors
1771 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001772 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773STATIC int
1774xfs_setsize_buftarg_early(
1775 xfs_buftarg_t *btp,
1776 struct block_device *bdev)
1777{
Eric Sandeena96c4152014-04-14 19:00:29 +10001778 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781xfs_buftarg_t *
1782xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001783 struct xfs_mount *mp,
Eric Sandeen34dcefd2014-04-14 19:01:00 +10001784 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 xfs_buftarg_t *btp;
1787
Dave Chinnerb17cb362013-05-20 09:51:12 +10001788 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Dave Chinnerebad8612010-09-22 10:47:20 +10001790 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001791 btp->bt_dev = bdev->bd_dev;
1792 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (xfs_setsize_buftarg_early(btp, bdev))
1795 goto error;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001796
1797 if (list_lru_init(&btp->bt_lru))
1798 goto error;
1799
Brian Foster9c7504a2016-07-20 11:15:28 +10001800 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
1801 goto error;
1802
Dave Chinnere80dfa12013-08-28 10:18:05 +10001803 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1804 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001805 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001806 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001807 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return btp;
1809
1810error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001811 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return NULL;
1813}
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001816 * Add a buffer to the delayed write list.
1817 *
1818 * This queues a buffer for writeout if it hasn't already been. Note that
1819 * neither this routine nor the buffer list submission functions perform
1820 * any internal synchronization. It is expected that the lists are thread-local
1821 * to the callers.
1822 *
1823 * Returns true if we queued up the buffer, or false if it already had
1824 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001826bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001827xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001828 struct xfs_buf *bp,
1829 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001831 ASSERT(xfs_buf_islocked(bp));
1832 ASSERT(!(bp->b_flags & XBF_READ));
1833
1834 /*
1835 * If the buffer is already marked delwri it already is queued up
1836 * by someone else for imediate writeout. Just ignore it in that
1837 * case.
1838 */
1839 if (bp->b_flags & _XBF_DELWRI_Q) {
1840 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1841 return false;
1842 }
David Chinnera6867a62006-01-11 15:37:58 +11001843
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001844 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1845
Dave Chinnerd808f612010-02-02 10:13:42 +11001846 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001847 * If a buffer gets written out synchronously or marked stale while it
1848 * is on a delwri list we lazily remove it. To do this, the other party
1849 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1850 * It remains referenced and on the list. In a rare corner case it
1851 * might get readded to a delwri list after the synchronous writeout, in
1852 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001853 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001854 bp->b_flags |= _XBF_DELWRI_Q;
1855 if (list_empty(&bp->b_list)) {
1856 atomic_inc(&bp->b_hold);
1857 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001858 }
David Chinner585e6d82007-02-10 18:32:29 +11001859
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001860 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001861}
1862
Dave Chinner089716a2010-01-26 15:13:25 +11001863/*
1864 * Compare function is more complex than it needs to be because
1865 * the return value is only 32 bits and we are doing comparisons
1866 * on 64 bit values
1867 */
1868static int
1869xfs_buf_cmp(
1870 void *priv,
1871 struct list_head *a,
1872 struct list_head *b)
1873{
1874 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1875 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1876 xfs_daddr_t diff;
1877
Mark Tinguelyf4b42422012-12-04 17:18:02 -06001878 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11001879 if (diff < 0)
1880 return -1;
1881 if (diff > 0)
1882 return 1;
1883 return 0;
1884}
1885
Dave Chinner26f1fe82016-06-01 17:38:15 +10001886/*
1887 * submit buffers for write.
1888 *
1889 * When we have a large buffer list, we do not want to hold all the buffers
1890 * locked while we block on the request queue waiting for IO dispatch. To avoid
1891 * this problem, we lock and submit buffers in groups of 50, thereby minimising
1892 * the lock hold times for lists which may contain thousands of objects.
1893 *
1894 * To do this, we sort the buffer list before we walk the list to lock and
1895 * submit buffers, and we plug and unplug around each group of buffers we
1896 * submit.
1897 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001898static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10001899xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001900 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10001901 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001903 struct xfs_buf *bp, *n;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001904 LIST_HEAD (submit_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001905 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001906 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Dave Chinner26f1fe82016-06-01 17:38:15 +10001908 list_sort(NULL, buffer_list, xfs_buf_cmp);
1909
1910 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001911 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10001912 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001913 if (xfs_buf_ispinned(bp)) {
1914 pinned++;
1915 continue;
1916 }
1917 if (!xfs_buf_trylock(bp))
1918 continue;
1919 } else {
1920 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001923 /*
1924 * Someone else might have written the buffer synchronously or
1925 * marked it stale in the meantime. In that case only the
1926 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1927 * reference and remove it from the list here.
1928 */
1929 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1930 list_del_init(&bp->b_list);
1931 xfs_buf_relse(bp);
1932 continue;
1933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001935 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001936
Dave Chinnercf53e992014-10-02 09:04:01 +10001937 /*
Dave Chinner26f1fe82016-06-01 17:38:15 +10001938 * We do all IO submission async. This means if we need
1939 * to wait for IO completion we need to take an extra
1940 * reference so the buffer is still valid on the other
1941 * side. We need to move the buffer onto the io_list
1942 * at this point so the caller can still access it.
Dave Chinnercf53e992014-10-02 09:04:01 +10001943 */
Dave Chinnerbbfeb612016-07-20 11:53:35 +10001944 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL);
Dave Chinner26f1fe82016-06-01 17:38:15 +10001945 bp->b_flags |= XBF_WRITE | XBF_ASYNC;
1946 if (wait_list) {
Dave Chinnercf53e992014-10-02 09:04:01 +10001947 xfs_buf_hold(bp);
Dave Chinner26f1fe82016-06-01 17:38:15 +10001948 list_move_tail(&bp->b_list, wait_list);
1949 } else
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001950 list_del_init(&bp->b_list);
Dave Chinner8dac3922014-10-02 09:04:40 +10001951
Dave Chinner595bff72014-10-02 09:05:14 +10001952 xfs_buf_submit(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001954 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001956 return pinned;
1957}
Nathan Scottf07c2252006-09-28 10:52:15 +10001958
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001959/*
1960 * Write out a buffer list asynchronously.
1961 *
1962 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1963 * out and not wait for I/O completion on any of the buffers. This interface
1964 * is only safely useable for callers that can track I/O completion by higher
1965 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1966 * function.
1967 */
1968int
1969xfs_buf_delwri_submit_nowait(
1970 struct list_head *buffer_list)
1971{
Dave Chinner26f1fe82016-06-01 17:38:15 +10001972 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001973}
1974
1975/*
1976 * Write out a buffer list synchronously.
1977 *
1978 * This will take the @buffer_list, write all buffers out and wait for I/O
1979 * completion on all of the buffers. @buffer_list is consumed by the function,
1980 * so callers must have some other way of tracking buffers if they require such
1981 * functionality.
1982 */
1983int
1984xfs_buf_delwri_submit(
1985 struct list_head *buffer_list)
1986{
Dave Chinner26f1fe82016-06-01 17:38:15 +10001987 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001988 int error = 0, error2;
1989 struct xfs_buf *bp;
1990
Dave Chinner26f1fe82016-06-01 17:38:15 +10001991 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001992
1993 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10001994 while (!list_empty(&wait_list)) {
1995 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001996
1997 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10001998
1999 /* locking the buffer will wait for async IO completion. */
2000 xfs_buf_lock(bp);
2001 error2 = bp->b_error;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002002 xfs_buf_relse(bp);
2003 if (!error)
2004 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
2006
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002007 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002010int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002011xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
Nathan Scott87582802006-03-14 13:18:19 +11002013 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
2014 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002015 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002016 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002017
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002020 out:
Nathan Scott87582802006-03-14 13:18:19 +11002021 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024void
Nathan Scottce8e9222006-01-11 15:39:08 +11002025xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Nathan Scottce8e9222006-01-11 15:39:08 +11002027 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}