blob: 06149bac2f581a064fa8bf76efea0738e0c8325e [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scottf07c2252006-09-28 10:52:15 +10003 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Vlad Apostolov93c189c2006-11-11 18:03:49 +11006#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/stddef.h>
8#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/pagemap.h>
11#include <linux/init.h>
12#include <linux/vmalloc.h>
13#include <linux/bio.h>
14#include <linux/sysctl.h>
15#include <linux/proc_fs.h>
16#include <linux/workqueue.h>
17#include <linux/percpu.h>
18#include <linux/blkdev.h>
19#include <linux/hash.h>
Christoph Hellwig4df08c52005-09-05 08:34:18 +100020#include <linux/kthread.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080021#include <linux/migrate.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070022#include <linux/backing-dev.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080023#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Christoph Hellwig4fb6e8a2014-11-28 14:25:04 +110025#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110026#include "xfs_log_format.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100027#include "xfs_trans_resv.h"
Dave Chinner239880e2013-10-23 10:50:10 +110028#include "xfs_sb.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050029#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000030#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_log.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070032#include "xfs_errortag.h"
Brian Foster7561d272017-10-17 14:16:29 -070033#include "xfs_error.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050034
David Chinner7989cb82007-02-10 18:34:56 +110035static kmem_zone_t *xfs_buf_zone;
Christoph Hellwig23ea4032005-06-21 15:14:01 +100036
Nathan Scottce8e9222006-01-11 15:39:08 +110037#define xb_to_gfp(flags) \
Dave Chinneraa5c1582012-04-23 15:58:56 +100038 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
James Bottomley73c77e22010-01-25 11:42:24 -060041static inline int
42xfs_buf_is_vmapped(
43 struct xfs_buf *bp)
44{
45 /*
46 * Return true if the buffer is vmapped.
47 *
Dave Chinner611c9942012-04-23 15:59:07 +100048 * b_addr is null if the buffer is not mapped, but the code is clever
49 * enough to know it doesn't have to map a single page, so the check has
50 * to be both for b_addr and bp->b_page_count > 1.
James Bottomley73c77e22010-01-25 11:42:24 -060051 */
Dave Chinner611c9942012-04-23 15:59:07 +100052 return bp->b_addr && bp->b_page_count > 1;
James Bottomley73c77e22010-01-25 11:42:24 -060053}
54
55static inline int
56xfs_buf_vmap_len(
57 struct xfs_buf *bp)
58{
59 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
Brian Foster9c7504a2016-07-20 11:15:28 +100063 * Bump the I/O in flight count on the buftarg if we haven't yet done so for
64 * this buffer. The count is incremented once per buffer (per hold cycle)
65 * because the corresponding decrement is deferred to buffer release. Buffers
66 * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
67 * tracking adds unnecessary overhead. This is used for sychronization purposes
68 * with unmount (see xfs_wait_buftarg()), so all we really need is a count of
69 * in-flight buffers.
70 *
71 * Buffers that are never released (e.g., superblock, iclog buffers) must set
72 * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
73 * never reaches zero and unmount hangs indefinitely.
74 */
75static inline void
76xfs_buf_ioacct_inc(
77 struct xfs_buf *bp)
78{
Brian Foster63db7c82017-05-31 08:22:52 -070079 if (bp->b_flags & XBF_NO_IOACCT)
Brian Foster9c7504a2016-07-20 11:15:28 +100080 return;
81
82 ASSERT(bp->b_flags & XBF_ASYNC);
Brian Foster63db7c82017-05-31 08:22:52 -070083 spin_lock(&bp->b_lock);
84 if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) {
85 bp->b_state |= XFS_BSTATE_IN_FLIGHT;
86 percpu_counter_inc(&bp->b_target->bt_io_count);
87 }
88 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +100089}
90
91/*
92 * Clear the in-flight state on a buffer about to be released to the LRU or
93 * freed and unaccount from the buftarg.
94 */
95static inline void
Brian Foster63db7c82017-05-31 08:22:52 -070096__xfs_buf_ioacct_dec(
97 struct xfs_buf *bp)
98{
Brian Foster95989c42017-06-08 08:23:07 -070099 lockdep_assert_held(&bp->b_lock);
Brian Foster63db7c82017-05-31 08:22:52 -0700100
101 if (bp->b_state & XFS_BSTATE_IN_FLIGHT) {
102 bp->b_state &= ~XFS_BSTATE_IN_FLIGHT;
103 percpu_counter_dec(&bp->b_target->bt_io_count);
104 }
105}
106
107static inline void
Brian Foster9c7504a2016-07-20 11:15:28 +1000108xfs_buf_ioacct_dec(
109 struct xfs_buf *bp)
110{
Brian Foster63db7c82017-05-31 08:22:52 -0700111 spin_lock(&bp->b_lock);
112 __xfs_buf_ioacct_dec(bp);
113 spin_unlock(&bp->b_lock);
Brian Foster9c7504a2016-07-20 11:15:28 +1000114}
115
116/*
Dave Chinner430cbeb2010-12-02 16:30:55 +1100117 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
118 * b_lru_ref count so that the buffer is freed immediately when the buffer
119 * reference count falls to zero. If the buffer is already on the LRU, we need
120 * to remove the reference that LRU holds on the buffer.
121 *
122 * This prevents build-up of stale buffers on the LRU.
123 */
124void
125xfs_buf_stale(
126 struct xfs_buf *bp)
127{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000128 ASSERT(xfs_buf_islocked(bp));
129
Dave Chinner430cbeb2010-12-02 16:30:55 +1100130 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000131
132 /*
133 * Clear the delwri status so that a delwri queue walker will not
134 * flush this buffer to disk now that it is stale. The delwri queue has
135 * a reference to the buffer, so this is safe to do.
136 */
137 bp->b_flags &= ~_XBF_DELWRI_Q;
138
Brian Foster9c7504a2016-07-20 11:15:28 +1000139 /*
140 * Once the buffer is marked stale and unlocked, a subsequent lookup
141 * could reset b_flags. There is no guarantee that the buffer is
142 * unaccounted (released to LRU) before that occurs. Drop in-flight
143 * status now to preserve accounting consistency.
144 */
Dave Chinnera4082352013-08-28 10:18:06 +1000145 spin_lock(&bp->b_lock);
Brian Foster63db7c82017-05-31 08:22:52 -0700146 __xfs_buf_ioacct_dec(bp);
147
Dave Chinnera4082352013-08-28 10:18:06 +1000148 atomic_set(&bp->b_lru_ref, 0);
149 if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
Dave Chinnere80dfa12013-08-28 10:18:05 +1000150 (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
151 atomic_dec(&bp->b_hold);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100152
Dave Chinner430cbeb2010-12-02 16:30:55 +1100153 ASSERT(atomic_read(&bp->b_hold) >= 1);
Dave Chinnera4082352013-08-28 10:18:06 +1000154 spin_unlock(&bp->b_lock);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100155}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Dave Chinner3e85c862012-06-22 18:50:09 +1000157static int
158xfs_buf_get_maps(
159 struct xfs_buf *bp,
160 int map_count)
161{
162 ASSERT(bp->b_maps == NULL);
163 bp->b_map_count = map_count;
164
165 if (map_count == 1) {
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600166 bp->b_maps = &bp->__b_map;
Dave Chinner3e85c862012-06-22 18:50:09 +1000167 return 0;
168 }
169
170 bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
171 KM_NOFS);
172 if (!bp->b_maps)
Dave Chinner24513372014-06-25 14:58:08 +1000173 return -ENOMEM;
Dave Chinner3e85c862012-06-22 18:50:09 +1000174 return 0;
175}
176
177/*
178 * Frees b_pages if it was allocated.
179 */
180static void
181xfs_buf_free_maps(
182 struct xfs_buf *bp)
183{
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600184 if (bp->b_maps != &bp->__b_map) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000185 kmem_free(bp->b_maps);
186 bp->b_maps = NULL;
187 }
188}
189
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000190struct xfs_buf *
Dave Chinner3e85c862012-06-22 18:50:09 +1000191_xfs_buf_alloc(
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000192 struct xfs_buftarg *target,
Dave Chinner3e85c862012-06-22 18:50:09 +1000193 struct xfs_buf_map *map,
194 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100195 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000197 struct xfs_buf *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000198 int error;
199 int i;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000200
Dave Chinneraa5c1582012-04-23 15:58:56 +1000201 bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000202 if (unlikely(!bp))
203 return NULL;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /*
Dave Chinner12bcb3f2012-04-23 15:59:05 +1000206 * We don't want certain flags to appear in b_flags unless they are
207 * specifically set by later operations on the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
Dave Chinner611c9942012-04-23 15:59:07 +1000209 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Nathan Scottce8e9222006-01-11 15:39:08 +1100211 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100212 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000213 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100214 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100215 INIT_LIST_HEAD(&bp->b_list);
Carlos Maiolino643c8c02018-01-24 13:38:49 -0800216 INIT_LIST_HEAD(&bp->b_li_list);
Thomas Gleixnera731cd112010-09-07 14:33:15 +0000217 sema_init(&bp->b_sema, 0); /* held, no waiters */
Dave Chinnera4082352013-08-28 10:18:06 +1000218 spin_lock_init(&bp->b_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100219 bp->b_target = target;
Dave Chinner3e85c862012-06-22 18:50:09 +1000220 bp->b_flags = flags;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000223 * Set length and io_length to the same value initially.
224 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * most cases but may be reset (e.g. XFS recovery).
226 */
Dave Chinner3e85c862012-06-22 18:50:09 +1000227 error = xfs_buf_get_maps(bp, nmaps);
228 if (error) {
229 kmem_zone_free(xfs_buf_zone, bp);
230 return NULL;
231 }
232
233 bp->b_bn = map[0].bm_bn;
234 bp->b_length = 0;
235 for (i = 0; i < nmaps; i++) {
236 bp->b_maps[i].bm_bn = map[i].bm_bn;
237 bp->b_maps[i].bm_len = map[i].bm_len;
238 bp->b_length += map[i].bm_len;
239 }
240 bp->b_io_length = bp->b_length;
241
Nathan Scottce8e9222006-01-11 15:39:08 +1100242 atomic_set(&bp->b_pin_count, 0);
243 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100245 XFS_STATS_INC(target->bt_mount, xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000246 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000247
248 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100252 * Allocate a page array capable of holding a specified number
253 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
255STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100256_xfs_buf_get_pages(
257 xfs_buf_t *bp,
Eric Sandeen87937bf2014-04-14 19:01:20 +1000258 int page_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100261 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100262 bp->b_page_count = page_count;
263 if (page_count <= XB_PAGES) {
264 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100266 bp->b_pages = kmem_alloc(sizeof(struct page *) *
Dave Chinneraa5c1582012-04-23 15:58:56 +1000267 page_count, KM_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +1100268 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return -ENOMEM;
270 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100271 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273 return 0;
274}
275
276/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100277 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
279STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100280_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 xfs_buf_t *bp)
282{
Nathan Scottce8e9222006-01-11 15:39:08 +1100283 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000284 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000285 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287}
288
289/*
290 * Releases the specified buffer.
291 *
292 * The modification state of any associated pages is left unchanged.
Zhi Yong Wub46fe822013-08-07 10:10:59 +0000293 * The buffer must not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * hashed and refcounted buffers
295 */
296void
Nathan Scottce8e9222006-01-11 15:39:08 +1100297xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 xfs_buf_t *bp)
299{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000300 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Dave Chinner430cbeb2010-12-02 16:30:55 +1100302 ASSERT(list_empty(&bp->b_lru));
303
Dave Chinner0e6e8472011-03-26 09:16:45 +1100304 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 uint i;
306
James Bottomley73c77e22010-01-25 11:42:24 -0600307 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000308 vm_unmap_ram(bp->b_addr - bp->b_offset,
309 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Nathan Scott948ecdb2006-09-28 11:03:13 +1000311 for (i = 0; i < bp->b_page_count; i++) {
312 struct page *page = bp->b_pages[i];
313
Dave Chinner0e6e8472011-03-26 09:16:45 +1100314 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000315 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100316 } else if (bp->b_flags & _XBF_KMEM)
317 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000318 _xfs_buf_free_pages(bp);
Dave Chinner3e85c862012-06-22 18:50:09 +1000319 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000320 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100324 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
326STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100327xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 xfs_buf_t *bp,
329 uint flags)
330{
Dave Chinneraa0e8832012-04-23 15:58:52 +1000331 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100333 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 unsigned short page_count, i;
Dave Chinner795cac72012-04-23 15:58:53 +1000335 xfs_off_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int error;
337
Dave Chinner0e6e8472011-03-26 09:16:45 +1100338 /*
339 * for buffers that are contained within a single page, just allocate
340 * the memory from the heap - there's no need for the complexity of
341 * page arrays to keep allocation down to order 0.
342 */
Dave Chinner795cac72012-04-23 15:58:53 +1000343 size = BBTOB(bp->b_length);
344 if (size < PAGE_SIZE) {
Dave Chinneraa5c1582012-04-23 15:58:56 +1000345 bp->b_addr = kmem_alloc(size, KM_NOFS);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100346 if (!bp->b_addr) {
347 /* low memory - use alloc_page loop instead */
348 goto use_alloc_page;
349 }
350
Dave Chinner795cac72012-04-23 15:58:53 +1000351 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100352 ((unsigned long)bp->b_addr & PAGE_MASK)) {
353 /* b_addr spans two pages - use alloc_page instead */
354 kmem_free(bp->b_addr);
355 bp->b_addr = NULL;
356 goto use_alloc_page;
357 }
358 bp->b_offset = offset_in_page(bp->b_addr);
359 bp->b_pages = bp->b_page_array;
360 bp->b_pages[0] = virt_to_page(bp->b_addr);
361 bp->b_page_count = 1;
Dave Chinner611c9942012-04-23 15:59:07 +1000362 bp->b_flags |= _XBF_KMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100363 return 0;
364 }
365
366use_alloc_page:
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600367 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
368 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000369 >> PAGE_SHIFT;
Dave Chinner795cac72012-04-23 15:58:53 +1000370 page_count = end - start;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000371 error = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (unlikely(error))
373 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Nathan Scottce8e9222006-01-11 15:39:08 +1100375 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100376 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Nathan Scottce8e9222006-01-11 15:39:08 +1100378 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct page *page;
380 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100381retry:
382 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100384 if (flags & XBF_READ_AHEAD) {
385 bp->b_page_count = i;
Dave Chinner24513372014-06-25 14:58:08 +1000386 error = -ENOMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100387 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
390 /*
391 * This could deadlock.
392 *
393 * But until all the XFS lowlevel code is revamped to
394 * handle buffer allocation failures we can't do much.
395 */
396 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100397 xfs_err(NULL,
Tetsuo Handa5bf97b12015-10-12 15:41:29 +1100398 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
399 current->comm, current->pid,
Harvey Harrison34a622b2008-04-10 12:19:21 +1000400 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100402 XFS_STATS_INC(bp->b_target->bt_mount, xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200403 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 goto retry;
405 }
406
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100407 XFS_STATS_INC(bp->b_target->bt_mount, xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Dave Chinner0e6e8472011-03-26 09:16:45 +1100409 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100411 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 offset = 0;
413 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Dave Chinner0e6e8472011-03-26 09:16:45 +1100416out_free_pages:
417 for (i = 0; i < bp->b_page_count; i++)
418 __free_page(bp->b_pages[i]);
Darrick J. Wong2aa6ba7b2017-01-25 20:24:57 -0800419 bp->b_flags &= ~_XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return error;
421}
422
423/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300424 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
426STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100427_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 xfs_buf_t *bp,
429 uint flags)
430{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100431 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100432 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100433 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100434 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
Dave Chinner611c9942012-04-23 15:59:07 +1000435 } else if (flags & XBF_UNMAPPED) {
436 bp->b_addr = NULL;
437 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100438 int retried = 0;
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700439 unsigned nofs_flag;
Dave Chinnera19fb382011-03-26 09:13:42 +1100440
Dave Chinnerae687e52014-03-07 16:19:14 +1100441 /*
442 * vm_map_ram() will allocate auxillary structures (e.g.
443 * pagetables) with GFP_KERNEL, yet we are likely to be under
444 * GFP_NOFS context here. Hence we need to tell memory reclaim
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700445 * that we are in such a context via PF_MEMALLOC_NOFS to prevent
Dave Chinnerae687e52014-03-07 16:19:14 +1100446 * memory reclaim re-entering the filesystem here and
447 * potentially deadlocking.
448 */
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700449 nofs_flag = memalloc_nofs_save();
Dave Chinnera19fb382011-03-26 09:13:42 +1100450 do {
451 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
452 -1, PAGE_KERNEL);
453 if (bp->b_addr)
454 break;
455 vm_unmap_aliases();
456 } while (retried++ <= 1);
Michal Hocko9ba1fb22017-05-03 14:53:19 -0700457 memalloc_nofs_restore(nofs_flag);
Dave Chinnera19fb382011-03-26 09:13:42 +1100458
459 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100461 bp->b_addr += bp->b_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
464 return 0;
465}
466
467/*
468 * Finding and Reading Buffers
469 */
Lucas Stach6031e732016-12-07 17:36:36 +1100470static int
471_xfs_buf_obj_cmp(
472 struct rhashtable_compare_arg *arg,
473 const void *obj)
474{
475 const struct xfs_buf_map *map = arg->key;
476 const struct xfs_buf *bp = obj;
477
478 /*
479 * The key hashing in the lookup path depends on the key being the
480 * first element of the compare_arg, make sure to assert this.
481 */
482 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
483
484 if (bp->b_bn != map->bm_bn)
485 return 1;
486
487 if (unlikely(bp->b_length != map->bm_len)) {
488 /*
489 * found a block number match. If the range doesn't
490 * match, the only way this is allowed is if the buffer
491 * in the cache is stale and the transaction that made
492 * it stale has not yet committed. i.e. we are
493 * reallocating a busy extent. Skip this buffer and
494 * continue searching for an exact match.
495 */
496 ASSERT(bp->b_flags & XBF_STALE);
497 return 1;
498 }
499 return 0;
500}
501
502static const struct rhashtable_params xfs_buf_hash_params = {
503 .min_size = 32, /* empty AGs have minimal footprint */
504 .nelem_hint = 16,
505 .key_len = sizeof(xfs_daddr_t),
506 .key_offset = offsetof(struct xfs_buf, b_bn),
507 .head_offset = offsetof(struct xfs_buf, b_rhash_head),
508 .automatic_shrinking = true,
509 .obj_cmpfn = _xfs_buf_obj_cmp,
510};
511
512int
513xfs_buf_hash_init(
514 struct xfs_perag *pag)
515{
516 spin_lock_init(&pag->pag_buf_lock);
517 return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
518}
519
520void
521xfs_buf_hash_destroy(
522 struct xfs_perag *pag)
523{
524 rhashtable_destroy(&pag->pag_buf_hash);
525}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527/*
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700528 * Look up a buffer in the buffer cache and return it referenced and locked
529 * in @found_bp.
530 *
531 * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the
532 * cache.
533 *
534 * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return
535 * -EAGAIN if we fail to lock it.
536 *
537 * Return values are:
538 * -EFSCORRUPTED if have been supplied with an invalid address
539 * -EAGAIN on trylock failure
540 * -ENOENT if we fail to find a match and @new_bp was NULL
541 * 0, with @found_bp:
542 * - @new_bp if we inserted it into the cache
543 * - the buffer we found and locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700545static int
546xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000547 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000548 struct xfs_buf_map *map,
549 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100550 xfs_buf_flags_t flags,
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700551 struct xfs_buf *new_bp,
552 struct xfs_buf **found_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Dave Chinner74f75a02010-09-24 19:59:04 +1000554 struct xfs_perag *pag;
Dave Chinner74f75a02010-09-24 19:59:04 +1000555 xfs_buf_t *bp;
Lucas Stach6031e732016-12-07 17:36:36 +1100556 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
Dave Chinner10616b82013-01-21 23:53:52 +1100557 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000558 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700560 *found_bp = NULL;
561
Dave Chinner3e85c862012-06-22 18:50:09 +1000562 for (i = 0; i < nmaps; i++)
Lucas Stach6031e732016-12-07 17:36:36 +1100563 cmap.bm_len += map[i].bm_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /* Check for IOs smaller than the sector size / not sector aligned */
Lucas Stach6031e732016-12-07 17:36:36 +1100566 ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
567 ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Dave Chinner10616b82013-01-21 23:53:52 +1100569 /*
570 * Corrupted block numbers can get through to here, unfortunately, so we
571 * have to check that the buffer falls within the filesystem bounds.
572 */
573 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
Lucas Stach6031e732016-12-07 17:36:36 +1100574 if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
Dave Chinner10616b82013-01-21 23:53:52 +1100575 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -0800576 "%s: daddr 0x%llx out of range, EOFS 0x%llx",
Lucas Stach6031e732016-12-07 17:36:36 +1100577 __func__, cmap.bm_bn, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000578 WARN_ON(1);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700579 return -EFSCORRUPTED;
Dave Chinner10616b82013-01-21 23:53:52 +1100580 }
581
Dave Chinner74f75a02010-09-24 19:59:04 +1000582 pag = xfs_perag_get(btp->bt_mount,
Lucas Stach6031e732016-12-07 17:36:36 +1100583 xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Dave Chinner74f75a02010-09-24 19:59:04 +1000585 spin_lock(&pag->pag_buf_lock);
Lucas Stach6031e732016-12-07 17:36:36 +1100586 bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
587 xfs_buf_hash_params);
588 if (bp) {
589 atomic_inc(&bp->b_hold);
590 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
593 /* No match found */
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700594 if (!new_bp) {
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100595 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000596 spin_unlock(&pag->pag_buf_lock);
597 xfs_perag_put(pag);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700598 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700600
601 /* the buffer keeps the perag reference until it is freed */
602 new_bp->b_pag = pag;
603 rhashtable_insert_fast(&pag->pag_buf_hash, &new_bp->b_rhash_head,
604 xfs_buf_hash_params);
605 spin_unlock(&pag->pag_buf_lock);
606 *found_bp = new_bp;
607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000610 spin_unlock(&pag->pag_buf_lock);
611 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200613 if (!xfs_buf_trylock(bp)) {
614 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100615 xfs_buf_rele(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100616 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700617 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200619 xfs_buf_lock(bp);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100620 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
Dave Chinner0e6e8472011-03-26 09:16:45 +1100623 /*
624 * if the buffer is stale, clear all the external state associated with
625 * it. We need to keep flags such as how we allocated the buffer memory
626 * intact here.
627 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100628 if (bp->b_flags & XBF_STALE) {
629 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinnercfb02852012-11-12 22:54:19 +1100630 ASSERT(bp->b_iodone == NULL);
Dave Chinner611c9942012-04-23 15:59:07 +1000631 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100632 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000633 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000634
635 trace_xfs_buf_find(bp, flags, _RET_IP_);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100636 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700637 *found_bp = bp;
638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Dave Chinner8925a3d2018-04-18 08:25:20 -0700641struct xfs_buf *
642xfs_buf_incore(
643 struct xfs_buftarg *target,
644 xfs_daddr_t blkno,
645 size_t numblks,
646 xfs_buf_flags_t flags)
647{
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700648 struct xfs_buf *bp;
649 int error;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700650 DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700651
652 error = xfs_buf_find(target, &map, 1, flags, NULL, &bp);
653 if (error)
654 return NULL;
655 return bp;
Dave Chinner8925a3d2018-04-18 08:25:20 -0700656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658/*
Dave Chinner38158322011-09-30 04:45:02 +0000659 * Assembles a buffer covering the specified range. The code is optimised for
660 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
661 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
Dave Chinner38158322011-09-30 04:45:02 +0000663struct xfs_buf *
Dave Chinner6dde2702012-06-22 18:50:10 +1000664xfs_buf_get_map(
665 struct xfs_buftarg *target,
666 struct xfs_buf_map *map,
667 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100668 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Dave Chinner38158322011-09-30 04:45:02 +0000670 struct xfs_buf *bp;
671 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100672 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700674 error = xfs_buf_find(target, map, nmaps, flags, NULL, &bp);
675
676 switch (error) {
677 case 0:
678 /* cache hit */
Dave Chinner38158322011-09-30 04:45:02 +0000679 goto found;
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700680 case -EAGAIN:
681 /* cache hit, trylock failure, caller handles failure */
682 ASSERT(flags & XBF_TRYLOCK);
683 return NULL;
684 case -ENOENT:
685 /* cache miss, go for insert */
686 break;
687 case -EFSCORRUPTED:
688 default:
689 /*
690 * None of the higher layers understand failure types
691 * yet, so return NULL to signal a fatal lookup error.
692 */
693 return NULL;
694 }
Dave Chinner38158322011-09-30 04:45:02 +0000695
Dave Chinner6dde2702012-06-22 18:50:10 +1000696 new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100697 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return NULL;
699
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000700 error = xfs_buf_allocate_memory(new_bp, flags);
701 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000702 xfs_buf_free(new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000703 return NULL;
704 }
705
Dave Chinnerb027d4c2018-04-18 08:25:21 -0700706 error = xfs_buf_find(target, map, nmaps, flags, new_bp, &bp);
707 if (error) {
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000708 xfs_buf_free(new_bp);
709 return NULL;
710 }
711
712 if (bp != new_bp)
713 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Dave Chinner38158322011-09-30 04:45:02 +0000715found:
Dave Chinner611c9942012-04-23 15:59:07 +1000716 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100717 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100719 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500720 "%s: failed to map pagesn", __func__);
Dave Chinnera8acad72012-04-23 15:58:54 +1000721 xfs_buf_relse(bp);
722 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724 }
725
Dave Chinnerb79f4a12016-01-12 07:03:44 +1100726 /*
727 * Clear b_error if this is a lookup from a caller that doesn't expect
728 * valid data to be found in the buffer.
729 */
730 if (!(flags & XBF_READ))
731 xfs_buf_ioerror(bp, 0);
732
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100733 XFS_STATS_INC(target->bt_mount, xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000734 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100735 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100738STATIC int
739_xfs_buf_read(
740 xfs_buf_t *bp,
741 xfs_buf_flags_t flags)
742{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000743 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600744 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100745
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000746 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200747 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100748
Brian Foster6af88cd2018-07-11 22:26:35 -0700749 return xfs_buf_submit(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100750}
751
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100752/*
753 * If the caller passed in an ops structure and the buffer doesn't have ops
754 * assigned, set the ops and use them to verify the contents. If the contents
755 * cannot be verified, we'll clear XBF_DONE. We assume the buffer has no
756 * recorded errors and is already in XBF_DONE state.
757 */
758int
759xfs_buf_ensure_ops(
760 struct xfs_buf *bp,
761 const struct xfs_buf_ops *ops)
762{
763 ASSERT(bp->b_flags & XBF_DONE);
764 ASSERT(bp->b_error == 0);
765
766 if (!ops || bp->b_ops)
767 return 0;
768
769 bp->b_ops = ops;
770 bp->b_ops->verify_read(bp);
771 if (bp->b_error)
772 bp->b_flags &= ~XBF_DONE;
773 return bp->b_error;
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776xfs_buf_t *
Dave Chinner6dde2702012-06-22 18:50:10 +1000777xfs_buf_read_map(
778 struct xfs_buftarg *target,
779 struct xfs_buf_map *map,
780 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100781 xfs_buf_flags_t flags,
Dave Chinner1813dd62012-11-14 17:54:40 +1100782 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Dave Chinner6dde2702012-06-22 18:50:10 +1000784 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Nathan Scottce8e9222006-01-11 15:39:08 +1100786 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Dave Chinner6dde2702012-06-22 18:50:10 +1000788 bp = xfs_buf_get_map(target, map, nmaps, flags);
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100789 if (!bp)
790 return NULL;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000791
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100792 trace_xfs_buf_read(bp, flags, _RET_IP_);
793
794 if (!(bp->b_flags & XBF_DONE)) {
795 XFS_STATS_INC(target->bt_mount, xb_get_read);
796 bp->b_ops = ops;
797 _xfs_buf_read(bp, flags);
798 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800
Darrick J. Wong1aff5692018-10-18 17:20:30 +1100801 xfs_buf_ensure_ops(bp, ops);
802
803 if (flags & XBF_ASYNC) {
804 /*
805 * Read ahead call which is already satisfied,
806 * drop the buffer
807 */
808 xfs_buf_relse(bp);
809 return NULL;
810 }
811
812 /* We do not want read in the flags */
813 bp->b_flags &= ~XBF_READ;
814 ASSERT(bp->b_ops != NULL || ops == NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +1100815 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100819 * If we are not low on memory then do the readahead in a deadlock
820 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 */
822void
Dave Chinner6dde2702012-06-22 18:50:10 +1000823xfs_buf_readahead_map(
824 struct xfs_buftarg *target,
825 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100826 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100827 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Jan Karaefa7c9f2017-02-02 15:56:53 +0100829 if (bdi_read_congested(target->bt_bdev->bd_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return;
831
Dave Chinner6dde2702012-06-22 18:50:10 +1000832 xfs_buf_read_map(target, map, nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100833 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Dave Chinner5adc94c2010-09-24 21:58:31 +1000836/*
837 * Read an uncached buffer from disk. Allocates and returns a locked
838 * buffer containing the disk contents or nothing.
839 */
Dave Chinnerba3726742014-10-02 09:05:32 +1000840int
Dave Chinner5adc94c2010-09-24 21:58:31 +1000841xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000842 struct xfs_buftarg *target,
843 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000844 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100845 int flags,
Dave Chinnerba3726742014-10-02 09:05:32 +1000846 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100847 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000848{
Dave Chinnereab4e632012-11-12 22:54:02 +1100849 struct xfs_buf *bp;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000850
Dave Chinnerba3726742014-10-02 09:05:32 +1000851 *bpp = NULL;
852
Dave Chinnere70b73f2012-04-23 15:58:49 +1000853 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000854 if (!bp)
Dave Chinnerba3726742014-10-02 09:05:32 +1000855 return -ENOMEM;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000856
857 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000858 ASSERT(bp->b_map_count == 1);
Dave Chinnerba3726742014-10-02 09:05:32 +1000859 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
Dave Chinner3e85c862012-06-22 18:50:09 +1000860 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000861 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100862 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000863
Brian Foster6af88cd2018-07-11 22:26:35 -0700864 xfs_buf_submit(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000865 if (bp->b_error) {
866 int error = bp->b_error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800867 xfs_buf_relse(bp);
Dave Chinnerba3726742014-10-02 09:05:32 +1000868 return error;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800869 }
Dave Chinnerba3726742014-10-02 09:05:32 +1000870
871 *bpp = bp;
872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Dave Chinner44396472011-04-21 09:34:27 +0000875/*
876 * Return a buffer allocated as an empty buffer and associated to external
877 * memory via xfs_buf_associate_memory() back to it's empty state.
878 */
879void
880xfs_buf_set_empty(
881 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000882 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000883{
884 if (bp->b_pages)
885 _xfs_buf_free_pages(bp);
886
887 bp->b_pages = NULL;
888 bp->b_page_count = 0;
889 bp->b_addr = NULL;
Dave Chinner4e94b712012-04-23 15:58:51 +1000890 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000891 bp->b_io_length = numblks;
Dave Chinner3e85c862012-06-22 18:50:09 +1000892
893 ASSERT(bp->b_map_count == 1);
Dave Chinner44396472011-04-21 09:34:27 +0000894 bp->b_bn = XFS_BUF_DADDR_NULL;
Dave Chinner3e85c862012-06-22 18:50:09 +1000895 bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL;
896 bp->b_maps[0].bm_len = bp->b_length;
Dave Chinner44396472011-04-21 09:34:27 +0000897}
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899static inline struct page *
900mem_to_page(
901 void *addr)
902{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800903 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return virt_to_page(addr);
905 } else {
906 return vmalloc_to_page(addr);
907 }
908}
909
910int
Nathan Scottce8e9222006-01-11 15:39:08 +1100911xfs_buf_associate_memory(
912 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 void *mem,
914 size_t len)
915{
916 int rval;
917 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100918 unsigned long pageaddr;
919 unsigned long offset;
920 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int page_count;
922
Dave Chinner0e6e8472011-03-26 09:16:45 +1100923 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100924 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100925 buflen = PAGE_ALIGN(len + offset);
926 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100929 if (bp->b_pages)
930 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Nathan Scottce8e9222006-01-11 15:39:08 +1100932 bp->b_pages = NULL;
933 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Eric Sandeen87937bf2014-04-14 19:01:20 +1000935 rval = _xfs_buf_get_pages(bp, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (rval)
937 return rval;
938
Nathan Scottce8e9222006-01-11 15:39:08 +1100939 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100941 for (i = 0; i < bp->b_page_count; i++) {
942 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100943 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Dave Chinneraa0e8832012-04-23 15:58:52 +1000946 bp->b_io_length = BTOBB(len);
Dave Chinner4e94b712012-04-23 15:58:51 +1000947 bp->b_length = BTOBB(buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 return 0;
950}
951
952xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000953xfs_buf_get_uncached(
954 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000955 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000956 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000958 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000959 int error, i;
Dave Chinner3e85c862012-06-22 18:50:09 +1000960 struct xfs_buf *bp;
961 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Brian Fosterc891c302016-07-20 11:13:43 +1000963 /* flags might contain irrelevant bits, pass only what we care about */
964 bp = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (unlikely(bp == NULL))
966 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Dave Chinnere70b73f2012-04-23 15:58:49 +1000968 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Eric Sandeen87937bf2014-04-14 19:01:20 +1000969 error = _xfs_buf_get_pages(bp, page_count);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000970 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 goto fail_free_buf;
972
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000973 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000974 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000975 if (!bp->b_pages[i])
976 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000978 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Dave Chinner611c9942012-04-23 15:59:07 +1000980 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000981 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100982 xfs_warn(target->bt_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500983 "%s: failed to map pages", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Dave Chinner686865f2010-09-24 20:07:47 +1000987 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000991 while (--i >= 0)
992 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000993 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 fail_free_buf:
Dave Chinner3e85c862012-06-22 18:50:09 +1000995 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000996 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 fail:
998 return NULL;
999}
1000
1001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 * Increment reference count on buffer, to hold the buffer concurrently
1003 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * Must hold the buffer already to call this function.
1005 */
1006void
Nathan Scottce8e9222006-01-11 15:39:08 +11001007xfs_buf_hold(
1008 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001010 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001011 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014/*
Brian Foster9c7504a2016-07-20 11:15:28 +10001015 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
1016 * placed on LRU or freed (depending on b_lru_ref).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 */
1018void
Nathan Scottce8e9222006-01-11 15:39:08 +11001019xfs_buf_rele(
1020 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Dave Chinner74f75a02010-09-24 19:59:04 +10001022 struct xfs_perag *pag = bp->b_pag;
Brian Foster9c7504a2016-07-20 11:15:28 +10001023 bool release;
1024 bool freebuf = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001026 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Dave Chinner74f75a02010-09-24 19:59:04 +10001028 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +11001029 ASSERT(list_empty(&bp->b_lru));
Brian Foster9c7504a2016-07-20 11:15:28 +10001030 if (atomic_dec_and_test(&bp->b_hold)) {
1031 xfs_buf_ioacct_dec(bp);
Nathan Scottfad3aa12006-02-01 12:14:52 +11001032 xfs_buf_free(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001033 }
Nathan Scottfad3aa12006-02-01 12:14:52 +11001034 return;
1035 }
1036
Lachlan McIlroy37906892008-08-13 15:42:10 +10001037 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinnera4082352013-08-28 10:18:06 +10001038
Brian Foster9c7504a2016-07-20 11:15:28 +10001039 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
1040 spin_lock(&bp->b_lock);
1041 if (!release) {
1042 /*
1043 * Drop the in-flight state if the buffer is already on the LRU
1044 * and it holds the only reference. This is racy because we
1045 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1046 * ensures the decrement occurs only once per-buf.
1047 */
1048 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
Brian Foster63db7c82017-05-31 08:22:52 -07001049 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001050 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
Brian Foster9c7504a2016-07-20 11:15:28 +10001052
1053 /* the last reference has been dropped ... */
Brian Foster63db7c82017-05-31 08:22:52 -07001054 __xfs_buf_ioacct_dec(bp);
Brian Foster9c7504a2016-07-20 11:15:28 +10001055 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1056 /*
1057 * If the buffer is added to the LRU take a new reference to the
1058 * buffer for the LRU and clear the (now stale) dispose list
1059 * state flag
1060 */
1061 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1062 bp->b_state &= ~XFS_BSTATE_DISPOSE;
1063 atomic_inc(&bp->b_hold);
1064 }
1065 spin_unlock(&pag->pag_buf_lock);
1066 } else {
1067 /*
1068 * most of the time buffers will already be removed from the
1069 * LRU, so optimise that case by checking for the
1070 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1071 * was on was the disposal list
1072 */
1073 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1074 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1075 } else {
1076 ASSERT(list_empty(&bp->b_lru));
1077 }
1078
1079 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Lucas Stach6031e732016-12-07 17:36:36 +11001080 rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
1081 xfs_buf_hash_params);
Brian Foster9c7504a2016-07-20 11:15:28 +10001082 spin_unlock(&pag->pag_buf_lock);
1083 xfs_perag_put(pag);
1084 freebuf = true;
1085 }
1086
1087out_unlock:
1088 spin_unlock(&bp->b_lock);
1089
1090 if (freebuf)
1091 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
1094
1095/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001096 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +11001097 *
1098 * If we come across a stale, pinned, locked buffer, we know that we are
1099 * being asked to lock a buffer that has been reallocated. Because it is
1100 * pinned, we know that the log has not been pushed to disk and hence it
1101 * will still be locked. Rather than continuing to have trylock attempts
1102 * fail until someone else pushes the log, push it ourselves before
1103 * returning. This means that the xfsaild will not get stuck trying
1104 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 */
1106int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001107xfs_buf_trylock(
1108 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 int locked;
1111
Nathan Scottce8e9222006-01-11 15:39:08 +11001112 locked = down_trylock(&bp->b_sema) == 0;
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001113 if (locked)
Darrick J. Wong479c6412016-06-21 11:53:28 +10001114 trace_xfs_buf_trylock(bp, _RET_IP_);
Eric Sandeenfa6c6682018-08-10 13:56:25 -07001115 else
Darrick J. Wong479c6412016-06-21 11:53:28 +10001116 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001117 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120/*
Dave Chinner0e6e8472011-03-26 09:16:45 +11001121 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +10001122 *
1123 * If we come across a stale, pinned, locked buffer, we know that we
1124 * are being asked to lock a buffer that has been reallocated. Because
1125 * it is pinned, we know that the log has not been pushed to disk and
1126 * hence it will still be locked. Rather than sleeping until someone
1127 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001129void
1130xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001131 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001133 trace_xfs_buf_lock(bp, _RET_IP_);
1134
Dave Chinnered3b4d62010-05-21 12:07:08 +10001135 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +10001136 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +11001137 down(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001138
1139 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142void
Nathan Scottce8e9222006-01-11 15:39:08 +11001143xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02001144 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Brian Foster20e8a062017-04-21 12:40:44 -07001146 ASSERT(xfs_buf_islocked(bp));
1147
Nathan Scottce8e9222006-01-11 15:39:08 +11001148 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001149 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
Nathan Scottce8e9222006-01-11 15:39:08 +11001152STATIC void
1153xfs_buf_wait_unpin(
1154 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
1156 DECLARE_WAITQUEUE (wait, current);
1157
Nathan Scottce8e9222006-01-11 15:39:08 +11001158 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return;
1160
Nathan Scottce8e9222006-01-11 15:39:08 +11001161 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 for (;;) {
1163 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001164 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001166 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001168 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 set_current_state(TASK_RUNNING);
1170}
1171
1172/*
1173 * Buffer Utility Routines
1174 */
1175
Dave Chinnere8aaba92014-10-02 09:04:22 +10001176void
1177xfs_buf_ioend(
1178 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001180 bool read = bp->b_flags & XBF_READ;
1181
1182 trace_xfs_buf_iodone(bp, _RET_IP_);
Dave Chinner1813dd62012-11-14 17:54:40 +11001183
1184 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Dave Chinnerd5929de2013-02-27 13:25:54 +11001185
Dave Chinner61be9c52014-10-02 09:04:31 +10001186 /*
1187 * Pull in IO completion errors now. We are guaranteed to be running
1188 * single threaded, so we don't need the lock to read b_io_error.
1189 */
1190 if (!bp->b_error && bp->b_io_error)
1191 xfs_buf_ioerror(bp, bp->b_io_error);
1192
Dave Chinnere8aaba92014-10-02 09:04:22 +10001193 /* Only validate buffers that were read without errors */
1194 if (read && !bp->b_error && bp->b_ops) {
1195 ASSERT(!bp->b_iodone);
Dave Chinner1813dd62012-11-14 17:54:40 +11001196 bp->b_ops->verify_read(bp);
Dave Chinnere8aaba92014-10-02 09:04:22 +10001197 }
1198
1199 if (!bp->b_error)
1200 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Christoph Hellwig80f6c292010-08-18 05:29:11 -04001202 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001203 (*(bp->b_iodone))(bp);
1204 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 xfs_buf_relse(bp);
Dave Chinner595bff72014-10-02 09:05:14 +10001206 else
Dave Chinner1813dd62012-11-14 17:54:40 +11001207 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Dave Chinnere8aaba92014-10-02 09:04:22 +10001210static void
1211xfs_buf_ioend_work(
1212 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Dave Chinnere8aaba92014-10-02 09:04:22 +10001214 struct xfs_buf *bp =
Brian Fosterb29c70f2014-12-04 09:43:17 +11001215 container_of(work, xfs_buf_t, b_ioend_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001216
Dave Chinnere8aaba92014-10-02 09:04:22 +10001217 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Alexander Kuleshov211fe1a2016-01-04 16:10:42 +11001220static void
Dave Chinnere8aaba92014-10-02 09:04:22 +10001221xfs_buf_ioend_async(
1222 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Brian Fosterb29c70f2014-12-04 09:43:17 +11001224 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
1225 queue_work(bp->b_ioend_wq, &bp->b_ioend_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228void
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001229__xfs_buf_ioerror(
Nathan Scottce8e9222006-01-11 15:39:08 +11001230 xfs_buf_t *bp,
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001231 int error,
1232 xfs_failaddr_t failaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Dave Chinner24513372014-06-25 14:58:08 +10001234 ASSERT(error <= 0 && error >= -1000);
1235 bp->b_error = error;
Darrick J. Wong31ca03c2018-01-08 10:51:02 -08001236 trace_xfs_buf_ioerror(bp, error, failaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Christoph Hellwig901796a2011-10-10 16:52:49 +00001239void
1240xfs_buf_ioerror_alert(
1241 struct xfs_buf *bp,
1242 const char *func)
1243{
1244 xfs_alert(bp->b_target->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001245"metadata I/O error in \"%s\" at daddr 0x%llx len %d error %d",
1246 func, (uint64_t)XFS_BUF_ADDR(bp), bp->b_length,
1247 -bp->b_error);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001248}
1249
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001250int
1251xfs_bwrite(
1252 struct xfs_buf *bp)
1253{
1254 int error;
1255
1256 ASSERT(xfs_buf_islocked(bp));
1257
1258 bp->b_flags |= XBF_WRITE;
Dave Chinner27187752014-10-02 09:04:56 +10001259 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
1260 XBF_WRITE_FAIL | XBF_DONE);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001261
Brian Foster6af88cd2018-07-11 22:26:35 -07001262 error = xfs_buf_submit(bp);
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001263 if (error) {
1264 xfs_force_shutdown(bp->b_target->bt_mount,
1265 SHUTDOWN_META_IO_ERROR);
1266 }
1267 return error;
1268}
1269
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001270static void
Nathan Scottce8e9222006-01-11 15:39:08 +11001271xfs_buf_bio_end_io(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001272 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Brian Foster9bdd9bd2016-05-18 10:56:41 +10001274 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Dave Chinner37eb17e2012-11-12 22:09:46 +11001276 /*
1277 * don't overwrite existing errors - otherwise we can lose errors on
1278 * buffers that require multiple bios to complete.
1279 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001280 if (bio->bi_status) {
1281 int error = blk_status_to_errno(bio->bi_status);
1282
1283 cmpxchg(&bp->b_io_error, 0, error);
1284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Dave Chinner37eb17e2012-11-12 22:09:46 +11001286 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001287 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1288
Dave Chinnere8aaba92014-10-02 09:04:22 +10001289 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1290 xfs_buf_ioend_async(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Dave Chinner3e85c862012-06-22 18:50:09 +10001294static void
1295xfs_buf_ioapply_map(
1296 struct xfs_buf *bp,
1297 int map,
1298 int *buf_offset,
1299 int *count,
Mike Christie50bfcd02016-06-05 14:31:57 -05001300 int op,
1301 int op_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Dave Chinner3e85c862012-06-22 18:50:09 +10001303 int page_index;
1304 int total_nr_pages = bp->b_page_count;
1305 int nr_pages;
1306 struct bio *bio;
1307 sector_t sector = bp->b_maps[map].bm_bn;
1308 int size;
1309 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Dave Chinner3e85c862012-06-22 18:50:09 +10001311 /* skip the pages in the buffer before the start offset */
1312 page_index = 0;
1313 offset = *buf_offset;
1314 while (offset >= PAGE_SIZE) {
1315 page_index++;
1316 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001317 }
1318
Dave Chinner3e85c862012-06-22 18:50:09 +10001319 /*
1320 * Limit the IO size to the length of the current vector, and update the
1321 * remaining IO count for the next time around.
1322 */
1323 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1324 *count -= size;
1325 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001328 atomic_inc(&bp->b_io_remaining);
Ming Leic908e382016-05-30 21:34:33 +08001329 nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 bio = bio_alloc(GFP_NOIO, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001332 bio_set_dev(bio, bp->b_target->bt_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001333 bio->bi_iter.bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001334 bio->bi_end_io = xfs_buf_bio_end_io;
1335 bio->bi_private = bp;
Mike Christie50bfcd02016-06-05 14:31:57 -05001336 bio_set_op_attrs(bio, op, op_flags);
Dave Chinner0e6e8472011-03-26 09:16:45 +11001337
Dave Chinner3e85c862012-06-22 18:50:09 +10001338 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001339 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 if (nbytes > size)
1342 nbytes = size;
1343
Dave Chinner3e85c862012-06-22 18:50:09 +10001344 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1345 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001346 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 break;
1348
1349 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001350 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 size -= nbytes;
1352 total_nr_pages--;
1353 }
1354
Kent Overstreet4f024f32013-10-11 15:44:27 -07001355 if (likely(bio->bi_iter.bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001356 if (xfs_buf_is_vmapped(bp)) {
1357 flush_kernel_vmap_range(bp->b_addr,
1358 xfs_buf_vmap_len(bp));
1359 }
Mike Christie4e49ea42016-06-05 14:31:41 -05001360 submit_bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (size)
1362 goto next_chunk;
1363 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001364 /*
1365 * This is guaranteed not to be the last io reference count
Dave Chinner595bff72014-10-02 09:05:14 +10001366 * because the caller (xfs_buf_submit) holds a count itself.
Dave Chinner37eb17e2012-11-12 22:09:46 +11001367 */
1368 atomic_dec(&bp->b_io_remaining);
Dave Chinner24513372014-06-25 14:58:08 +10001369 xfs_buf_ioerror(bp, -EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001370 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001372
1373}
1374
1375STATIC void
1376_xfs_buf_ioapply(
1377 struct xfs_buf *bp)
1378{
1379 struct blk_plug plug;
Mike Christie50bfcd02016-06-05 14:31:57 -05001380 int op;
1381 int op_flags = 0;
Dave Chinner3e85c862012-06-22 18:50:09 +10001382 int offset;
1383 int size;
1384 int i;
1385
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001386 /*
1387 * Make sure we capture only current IO errors rather than stale errors
1388 * left over from previous use of the buffer (e.g. failed readahead).
1389 */
1390 bp->b_error = 0;
1391
Brian Fosterb29c70f2014-12-04 09:43:17 +11001392 /*
1393 * Initialize the I/O completion workqueue if we haven't yet or the
1394 * submitter has not opted to specify a custom one.
1395 */
1396 if (!bp->b_ioend_wq)
1397 bp->b_ioend_wq = bp->b_target->bt_mount->m_buf_workqueue;
1398
Dave Chinner3e85c862012-06-22 18:50:09 +10001399 if (bp->b_flags & XBF_WRITE) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001400 op = REQ_OP_WRITE;
Dave Chinner3e85c862012-06-22 18:50:09 +10001401 if (bp->b_flags & XBF_SYNCIO)
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001402 op_flags = REQ_SYNC;
Dave Chinner3e85c862012-06-22 18:50:09 +10001403 if (bp->b_flags & XBF_FUA)
Mike Christie50bfcd02016-06-05 14:31:57 -05001404 op_flags |= REQ_FUA;
Dave Chinner3e85c862012-06-22 18:50:09 +10001405 if (bp->b_flags & XBF_FLUSH)
Mike Christie28a8f0d2016-06-05 14:32:25 -05001406 op_flags |= REQ_PREFLUSH;
Dave Chinner1813dd62012-11-14 17:54:40 +11001407
1408 /*
1409 * Run the write verifier callback function if it exists. If
1410 * this function fails it will mark the buffer with an error and
1411 * the IO should not be dispatched.
1412 */
1413 if (bp->b_ops) {
1414 bp->b_ops->verify_write(bp);
1415 if (bp->b_error) {
1416 xfs_force_shutdown(bp->b_target->bt_mount,
1417 SHUTDOWN_CORRUPT_INCORE);
1418 return;
1419 }
Dave Chinner400b9d82014-08-04 12:42:40 +10001420 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
1421 struct xfs_mount *mp = bp->b_target->bt_mount;
1422
1423 /*
1424 * non-crc filesystems don't attach verifiers during
1425 * log recovery, so don't warn for such filesystems.
1426 */
1427 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1428 xfs_warn(mp,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001429 "%s: no buf ops on daddr 0x%llx len %d",
Dave Chinner400b9d82014-08-04 12:42:40 +10001430 __func__, bp->b_bn, bp->b_length);
Darrick J. Wong9c712a12018-01-08 10:51:26 -08001431 xfs_hex_dump(bp->b_addr,
1432 XFS_CORRUPTION_DUMP_LEN);
Dave Chinner400b9d82014-08-04 12:42:40 +10001433 dump_stack();
1434 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001435 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001436 } else if (bp->b_flags & XBF_READ_AHEAD) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001437 op = REQ_OP_READ;
1438 op_flags = REQ_RAHEAD;
Dave Chinner3e85c862012-06-22 18:50:09 +10001439 } else {
Mike Christie50bfcd02016-06-05 14:31:57 -05001440 op = REQ_OP_READ;
Dave Chinner3e85c862012-06-22 18:50:09 +10001441 }
1442
1443 /* we only use the buffer cache for meta-data */
Mike Christie50bfcd02016-06-05 14:31:57 -05001444 op_flags |= REQ_META;
Dave Chinner3e85c862012-06-22 18:50:09 +10001445
1446 /*
1447 * Walk all the vectors issuing IO on them. Set up the initial offset
1448 * into the buffer and the desired IO size before we start -
1449 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1450 * subsequent call.
1451 */
1452 offset = bp->b_offset;
1453 size = BBTOB(bp->b_io_length);
1454 blk_start_plug(&plug);
1455 for (i = 0; i < bp->b_map_count; i++) {
Mike Christie50bfcd02016-06-05 14:31:57 -05001456 xfs_buf_ioapply_map(bp, i, &offset, &size, op, op_flags);
Dave Chinner3e85c862012-06-22 18:50:09 +10001457 if (bp->b_error)
1458 break;
1459 if (size <= 0)
1460 break; /* all done */
1461 }
1462 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Dave Chinner595bff72014-10-02 09:05:14 +10001465/*
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001466 * Wait for I/O completion of a sync buffer and return the I/O error code.
Dave Chinner595bff72014-10-02 09:05:14 +10001467 */
Brian Fostereaebb512018-07-11 22:26:34 -07001468static int
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001469xfs_buf_iowait(
Dave Chinner595bff72014-10-02 09:05:14 +10001470 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001472 ASSERT(!(bp->b_flags & XBF_ASYNC));
1473
1474 trace_xfs_buf_iowait(bp, _RET_IP_);
1475 wait_for_completion(&bp->b_iowait);
1476 trace_xfs_buf_iowait_done(bp, _RET_IP_);
1477
1478 return bp->b_error;
1479}
1480
1481/*
1482 * Buffer I/O submission path, read or write. Asynchronous submission transfers
1483 * the buffer lock ownership and the current reference to the IO. It is not
1484 * safe to reference the buffer after a call to this function unless the caller
1485 * holds an additional reference itself.
1486 */
1487int
1488__xfs_buf_submit(
1489 struct xfs_buf *bp,
1490 bool wait)
1491{
1492 int error = 0;
1493
Dave Chinner595bff72014-10-02 09:05:14 +10001494 trace_xfs_buf_submit(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001496 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner595bff72014-10-02 09:05:14 +10001497
1498 /* on shutdown we stale and complete the buffer immediately */
1499 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
1500 xfs_buf_ioerror(bp, -EIO);
1501 bp->b_flags &= ~XBF_DONE;
1502 xfs_buf_stale(bp);
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001503 if (bp->b_flags & XBF_ASYNC)
1504 xfs_buf_ioend(bp);
Brian Fostereaebb512018-07-11 22:26:34 -07001505 return -EIO;
Dave Chinner595bff72014-10-02 09:05:14 +10001506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001508 /*
1509 * Grab a reference so the buffer does not go away underneath us. For
1510 * async buffers, I/O completion drops the callers reference, which
1511 * could occur before submission returns.
1512 */
1513 xfs_buf_hold(bp);
1514
Christoph Hellwig375ec692011-08-23 08:28:03 +00001515 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001516 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Dave Chinner61be9c52014-10-02 09:04:31 +10001518 /* clear the internal error state to avoid spurious errors */
1519 bp->b_io_error = 0;
1520
Eric Sandeen8d6c1212014-04-17 08:15:28 +10001521 /*
Brian Fostereaebb512018-07-11 22:26:34 -07001522 * Set the count to 1 initially, this will stop an I/O completion
1523 * callout which happens before we have started all the I/O from calling
1524 * xfs_buf_ioend too early.
1525 */
1526 atomic_set(&bp->b_io_remaining, 1);
1527 if (bp->b_flags & XBF_ASYNC)
1528 xfs_buf_ioacct_inc(bp);
1529 _xfs_buf_ioapply(bp);
1530
1531 /*
1532 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1533 * reference we took above. If we drop it to zero, run completion so
1534 * that we don't return to the caller with completion still pending.
1535 */
1536 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1537 if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
1538 xfs_buf_ioend(bp);
1539 else
1540 xfs_buf_ioend_async(bp);
1541 }
1542
Brian Foster6af88cd2018-07-11 22:26:35 -07001543 if (wait)
1544 error = xfs_buf_iowait(bp);
Brian Fosterbb00b6f2018-07-11 22:26:35 -07001545
Dave Chinner595bff72014-10-02 09:05:14 +10001546 /*
Brian Foster6af88cd2018-07-11 22:26:35 -07001547 * Release the hold that keeps the buffer referenced for the entire
1548 * I/O. Note that if the buffer is async, it is not safe to reference
1549 * after this release.
Dave Chinner595bff72014-10-02 09:05:14 +10001550 */
1551 xfs_buf_rele(bp);
1552 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001555void *
Nathan Scottce8e9222006-01-11 15:39:08 +11001556xfs_buf_offset(
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001557 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 size_t offset)
1559{
1560 struct page *page;
1561
Dave Chinner611c9942012-04-23 15:59:07 +10001562 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001563 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Nathan Scottce8e9222006-01-11 15:39:08 +11001565 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001566 page = bp->b_pages[offset >> PAGE_SHIFT];
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10001567 return page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
1570/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 * Move data into or out of a buffer.
1572 */
1573void
Nathan Scottce8e9222006-01-11 15:39:08 +11001574xfs_buf_iomove(
1575 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 size_t boff, /* starting buffer offset */
1577 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001578 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001579 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Dave Chinner795cac72012-04-23 15:58:53 +10001581 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 bend = boff + bsize;
1584 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001585 struct page *page;
1586 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Dave Chinner795cac72012-04-23 15:58:53 +10001588 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1589 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1590 page = bp->b_pages[page_index];
1591 csize = min_t(size_t, PAGE_SIZE - page_offset,
1592 BBTOB(bp->b_io_length) - boff);
1593
1594 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001597 case XBRW_ZERO:
Dave Chinner795cac72012-04-23 15:58:53 +10001598 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001600 case XBRW_READ:
Dave Chinner795cac72012-04-23 15:58:53 +10001601 memcpy(data, page_address(page) + page_offset, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001603 case XBRW_WRITE:
Dave Chinner795cac72012-04-23 15:58:53 +10001604 memcpy(page_address(page) + page_offset, data, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606
1607 boff += csize;
1608 data += csize;
1609 }
1610}
1611
1612/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001613 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 */
1615
1616/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001617 * Wait for any bufs with callbacks that have been submitted but have not yet
1618 * returned. These buffers will have an elevated hold count, so wait on those
1619 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001621static enum lru_status
1622xfs_buftarg_wait_rele(
1623 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001624 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001625 spinlock_t *lru_lock,
1626 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Dave Chinnere80dfa12013-08-28 10:18:05 +10001628{
1629 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
Dave Chinnera4082352013-08-28 10:18:06 +10001630 struct list_head *dispose = arg;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001631
1632 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinnera4082352013-08-28 10:18:06 +10001633 /* need to wait, so skip it this pass */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001634 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
Dave Chinnera4082352013-08-28 10:18:06 +10001635 return LRU_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Dave Chinnera4082352013-08-28 10:18:06 +10001637 if (!spin_trylock(&bp->b_lock))
1638 return LRU_SKIP;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001639
Dave Chinnera4082352013-08-28 10:18:06 +10001640 /*
1641 * clear the LRU reference count so the buffer doesn't get
1642 * ignored in xfs_buf_rele().
1643 */
1644 atomic_set(&bp->b_lru_ref, 0);
1645 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001646 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001647 spin_unlock(&bp->b_lock);
1648 return LRU_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
Dave Chinnere80dfa12013-08-28 10:18:05 +10001651void
1652xfs_wait_buftarg(
1653 struct xfs_buftarg *btp)
1654{
Dave Chinnera4082352013-08-28 10:18:06 +10001655 LIST_HEAD(dispose);
1656 int loop = 0;
1657
Dave Chinner85bec542016-01-19 08:28:10 +11001658 /*
Brian Foster9c7504a2016-07-20 11:15:28 +10001659 * First wait on the buftarg I/O count for all in-flight buffers to be
1660 * released. This is critical as new buffers do not make the LRU until
1661 * they are released.
1662 *
1663 * Next, flush the buffer workqueue to ensure all completion processing
1664 * has finished. Just waiting on buffer locks is not sufficient for
1665 * async IO as the reference count held over IO is not released until
1666 * after the buffer lock is dropped. Hence we need to ensure here that
1667 * all reference counts have been dropped before we start walking the
1668 * LRU list.
Dave Chinner85bec542016-01-19 08:28:10 +11001669 */
Brian Foster9c7504a2016-07-20 11:15:28 +10001670 while (percpu_counter_sum(&btp->bt_io_count))
1671 delay(100);
Brian Foster800b2692016-08-26 16:01:59 +10001672 flush_workqueue(btp->bt_mount->m_buf_workqueue);
Dave Chinner85bec542016-01-19 08:28:10 +11001673
Dave Chinnera4082352013-08-28 10:18:06 +10001674 /* loop until there is nothing left on the lru list. */
1675 while (list_lru_count(&btp->bt_lru)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001676 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
Dave Chinnera4082352013-08-28 10:18:06 +10001677 &dispose, LONG_MAX);
1678
1679 while (!list_empty(&dispose)) {
1680 struct xfs_buf *bp;
1681 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1682 list_del_init(&bp->b_lru);
Dave Chinnerac8809f2013-12-12 16:34:38 +11001683 if (bp->b_flags & XBF_WRITE_FAIL) {
1684 xfs_alert(btp->bt_mount,
Darrick J. Wongc219b012018-01-08 11:39:18 -08001685"Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
Dave Chinnerac8809f2013-12-12 16:34:38 +11001686 (long long)bp->b_bn);
Joe Perchesf41febd2015-07-29 11:52:04 +10001687 xfs_alert(btp->bt_mount,
1688"Please run xfs_repair to determine the extent of the problem.");
Dave Chinnerac8809f2013-12-12 16:34:38 +11001689 }
Dave Chinnera4082352013-08-28 10:18:06 +10001690 xfs_buf_rele(bp);
1691 }
1692 if (loop++ != 0)
1693 delay(100);
1694 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001695}
1696
1697static enum lru_status
1698xfs_buftarg_isolate(
1699 struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001700 struct list_lru_one *lru,
Dave Chinnere80dfa12013-08-28 10:18:05 +10001701 spinlock_t *lru_lock,
1702 void *arg)
1703{
1704 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1705 struct list_head *dispose = arg;
1706
1707 /*
Dave Chinnera4082352013-08-28 10:18:06 +10001708 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1709 * If we fail to get the lock, just skip it.
1710 */
1711 if (!spin_trylock(&bp->b_lock))
1712 return LRU_SKIP;
1713 /*
Dave Chinnere80dfa12013-08-28 10:18:05 +10001714 * Decrement the b_lru_ref count unless the value is already
1715 * zero. If the value is already zero, we need to reclaim the
1716 * buffer, otherwise it gets another trip through the LRU.
1717 */
Vratislav Bendel19957a12018-03-06 17:07:44 -08001718 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
Dave Chinnera4082352013-08-28 10:18:06 +10001719 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001720 return LRU_ROTATE;
Dave Chinnera4082352013-08-28 10:18:06 +10001721 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001722
Dave Chinnera4082352013-08-28 10:18:06 +10001723 bp->b_state |= XFS_BSTATE_DISPOSE;
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001724 list_lru_isolate_move(lru, item, dispose);
Dave Chinnera4082352013-08-28 10:18:06 +10001725 spin_unlock(&bp->b_lock);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001726 return LRU_REMOVED;
1727}
1728
Andrew Mortonaddbda42013-08-28 10:18:06 +10001729static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001730xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001731 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001732 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001733{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001734 struct xfs_buftarg *btp = container_of(shrink,
1735 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001736 LIST_HEAD(dispose);
Andrew Mortonaddbda42013-08-28 10:18:06 +10001737 unsigned long freed;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001738
Vladimir Davydov503c3582015-02-12 14:58:47 -08001739 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1740 xfs_buftarg_isolate, &dispose);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001741
1742 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001743 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001744 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1745 list_del_init(&bp->b_lru);
1746 xfs_buf_rele(bp);
1747 }
1748
Dave Chinnere80dfa12013-08-28 10:18:05 +10001749 return freed;
1750}
1751
Andrew Mortonaddbda42013-08-28 10:18:06 +10001752static unsigned long
Dave Chinnere80dfa12013-08-28 10:18:05 +10001753xfs_buftarg_shrink_count(
1754 struct shrinker *shrink,
1755 struct shrink_control *sc)
1756{
1757 struct xfs_buftarg *btp = container_of(shrink,
1758 struct xfs_buftarg, bt_shrinker);
Vladimir Davydov503c3582015-02-12 14:58:47 -08001759 return list_lru_shrink_count(&btp->bt_lru, sc);
David Chinnera6867a62006-01-11 15:37:58 +11001760}
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762void
1763xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001764 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001766 unregister_shrinker(&btp->bt_shrinker);
Brian Foster9c7504a2016-07-20 11:15:28 +10001767 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1768 percpu_counter_destroy(&btp->bt_io_count);
Glauber Costaf5e1dd32013-08-28 10:18:18 +10001769 list_lru_destroy(&btp->bt_lru);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001770
Dave Chinner2291dab2016-12-09 16:49:54 +11001771 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001772
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001773 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001776int
1777xfs_setsize_buftarg(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 xfs_buftarg_t *btp,
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001779 unsigned int sectorsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001781 /* Set up metadata sector size info */
Eric Sandeen6da54172014-01-21 16:45:52 -06001782 btp->bt_meta_sectorsize = sectorsize;
1783 btp->bt_meta_sectormask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Nathan Scottce8e9222006-01-11 15:39:08 +11001785 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001786 xfs_warn(btp->bt_mount,
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001787 "Cannot set_blocksize to %u on device %pg",
1788 sectorsize, btp->bt_bdev);
Dave Chinner24513372014-06-25 14:58:08 +10001789 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
1791
Eric Sandeen7c71ee72014-01-21 16:46:23 -06001792 /* Set up device logical sector size mask */
1793 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1794 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return 0;
1797}
1798
1799/*
Eric Sandeen3fefdee2013-11-13 14:53:45 -06001800 * When allocating the initial buffer target we have not yet
1801 * read in the superblock, so don't know what sized sectors
1802 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001803 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804STATIC int
1805xfs_setsize_buftarg_early(
1806 xfs_buftarg_t *btp,
1807 struct block_device *bdev)
1808{
Eric Sandeena96c4152014-04-14 19:00:29 +10001809 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812xfs_buftarg_t *
1813xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001814 struct xfs_mount *mp,
Dan Williams486aff52017-08-24 15:12:50 -07001815 struct block_device *bdev,
1816 struct dax_device *dax_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 xfs_buftarg_t *btp;
1819
Dave Chinnerb17cb362013-05-20 09:51:12 +10001820 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Dave Chinnerebad8612010-09-22 10:47:20 +10001822 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001823 btp->bt_dev = bdev->bd_dev;
1824 btp->bt_bdev = bdev;
Dan Williams486aff52017-08-24 15:12:50 -07001825 btp->bt_daxdev = dax_dev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (xfs_setsize_buftarg_early(btp, bdev))
Michal Hockod210a982017-11-23 17:13:40 +01001828 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001829
1830 if (list_lru_init(&btp->bt_lru))
Michal Hockod210a982017-11-23 17:13:40 +01001831 goto error_free;
Glauber Costa5ca302c2013-08-28 10:18:18 +10001832
Brian Foster9c7504a2016-07-20 11:15:28 +10001833 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
Michal Hockod210a982017-11-23 17:13:40 +01001834 goto error_lru;
Brian Foster9c7504a2016-07-20 11:15:28 +10001835
Dave Chinnere80dfa12013-08-28 10:18:05 +10001836 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1837 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001838 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001839 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Michal Hockod210a982017-11-23 17:13:40 +01001840 if (register_shrinker(&btp->bt_shrinker))
1841 goto error_pcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return btp;
1843
Michal Hockod210a982017-11-23 17:13:40 +01001844error_pcpu:
1845 percpu_counter_destroy(&btp->bt_io_count);
1846error_lru:
1847 list_lru_destroy(&btp->bt_lru);
1848error_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001849 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return NULL;
1851}
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853/*
Brian Foster20e8a062017-04-21 12:40:44 -07001854 * Cancel a delayed write list.
1855 *
1856 * Remove each buffer from the list, clear the delwri queue flag and drop the
1857 * associated buffer reference.
1858 */
1859void
1860xfs_buf_delwri_cancel(
1861 struct list_head *list)
1862{
1863 struct xfs_buf *bp;
1864
1865 while (!list_empty(list)) {
1866 bp = list_first_entry(list, struct xfs_buf, b_list);
1867
1868 xfs_buf_lock(bp);
1869 bp->b_flags &= ~_XBF_DELWRI_Q;
1870 list_del_init(&bp->b_list);
1871 xfs_buf_relse(bp);
1872 }
1873}
1874
1875/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001876 * Add a buffer to the delayed write list.
1877 *
1878 * This queues a buffer for writeout if it hasn't already been. Note that
1879 * neither this routine nor the buffer list submission functions perform
1880 * any internal synchronization. It is expected that the lists are thread-local
1881 * to the callers.
1882 *
1883 * Returns true if we queued up the buffer, or false if it already had
1884 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001886bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001887xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001888 struct xfs_buf *bp,
1889 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001891 ASSERT(xfs_buf_islocked(bp));
1892 ASSERT(!(bp->b_flags & XBF_READ));
1893
1894 /*
1895 * If the buffer is already marked delwri it already is queued up
1896 * by someone else for imediate writeout. Just ignore it in that
1897 * case.
1898 */
1899 if (bp->b_flags & _XBF_DELWRI_Q) {
1900 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1901 return false;
1902 }
David Chinnera6867a62006-01-11 15:37:58 +11001903
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001904 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1905
Dave Chinnerd808f612010-02-02 10:13:42 +11001906 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001907 * If a buffer gets written out synchronously or marked stale while it
1908 * is on a delwri list we lazily remove it. To do this, the other party
1909 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1910 * It remains referenced and on the list. In a rare corner case it
1911 * might get readded to a delwri list after the synchronous writeout, in
1912 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001913 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001914 bp->b_flags |= _XBF_DELWRI_Q;
1915 if (list_empty(&bp->b_list)) {
1916 atomic_inc(&bp->b_hold);
1917 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001918 }
David Chinner585e6d82007-02-10 18:32:29 +11001919
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001920 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001921}
1922
Dave Chinner089716a2010-01-26 15:13:25 +11001923/*
1924 * Compare function is more complex than it needs to be because
1925 * the return value is only 32 bits and we are doing comparisons
1926 * on 64 bit values
1927 */
1928static int
1929xfs_buf_cmp(
1930 void *priv,
1931 struct list_head *a,
1932 struct list_head *b)
1933{
1934 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1935 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1936 xfs_daddr_t diff;
1937
Mark Tinguelyf4b42422012-12-04 17:18:02 -06001938 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11001939 if (diff < 0)
1940 return -1;
1941 if (diff > 0)
1942 return 1;
1943 return 0;
1944}
1945
Dave Chinner26f1fe82016-06-01 17:38:15 +10001946/*
Brian Fostere339dd82018-07-11 22:26:34 -07001947 * Submit buffers for write. If wait_list is specified, the buffers are
1948 * submitted using sync I/O and placed on the wait list such that the caller can
1949 * iowait each buffer. Otherwise async I/O is used and the buffers are released
1950 * at I/O completion time. In either case, buffers remain locked until I/O
1951 * completes and the buffer is released from the queue.
Dave Chinner26f1fe82016-06-01 17:38:15 +10001952 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001953static int
Dave Chinner26f1fe82016-06-01 17:38:15 +10001954xfs_buf_delwri_submit_buffers(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001955 struct list_head *buffer_list,
Dave Chinner26f1fe82016-06-01 17:38:15 +10001956 struct list_head *wait_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001958 struct xfs_buf *bp, *n;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001959 LIST_HEAD (submit_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001960 int pinned = 0;
Dave Chinner26f1fe82016-06-01 17:38:15 +10001961 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Dave Chinner26f1fe82016-06-01 17:38:15 +10001963 list_sort(NULL, buffer_list, xfs_buf_cmp);
1964
1965 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001966 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
Dave Chinner26f1fe82016-06-01 17:38:15 +10001967 if (!wait_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001968 if (xfs_buf_ispinned(bp)) {
1969 pinned++;
1970 continue;
1971 }
1972 if (!xfs_buf_trylock(bp))
1973 continue;
1974 } else {
1975 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001978 /*
1979 * Someone else might have written the buffer synchronously or
1980 * marked it stale in the meantime. In that case only the
1981 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1982 * reference and remove it from the list here.
1983 */
1984 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1985 list_del_init(&bp->b_list);
1986 xfs_buf_relse(bp);
1987 continue;
1988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001990 trace_xfs_buf_delwri_split(bp, _RET_IP_);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001991
Dave Chinnercf53e992014-10-02 09:04:01 +10001992 /*
Brian Fostere339dd82018-07-11 22:26:34 -07001993 * If we have a wait list, each buffer (and associated delwri
1994 * queue reference) transfers to it and is submitted
1995 * synchronously. Otherwise, drop the buffer from the delwri
1996 * queue and submit async.
Dave Chinnercf53e992014-10-02 09:04:01 +10001997 */
Dave Chinnerbbfeb612016-07-20 11:53:35 +10001998 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL);
Brian Fostere339dd82018-07-11 22:26:34 -07001999 bp->b_flags |= XBF_WRITE;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002000 if (wait_list) {
Brian Fostere339dd82018-07-11 22:26:34 -07002001 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner26f1fe82016-06-01 17:38:15 +10002002 list_move_tail(&bp->b_list, wait_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002003 } else {
2004 bp->b_flags |= XBF_ASYNC;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002005 list_del_init(&bp->b_list);
Brian Fostere339dd82018-07-11 22:26:34 -07002006 }
Brian Foster6af88cd2018-07-11 22:26:35 -07002007 __xfs_buf_submit(bp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00002009 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002011 return pinned;
2012}
Nathan Scottf07c2252006-09-28 10:52:15 +10002013
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002014/*
2015 * Write out a buffer list asynchronously.
2016 *
2017 * This will take the @buffer_list, write all non-locked and non-pinned buffers
2018 * out and not wait for I/O completion on any of the buffers. This interface
2019 * is only safely useable for callers that can track I/O completion by higher
2020 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
2021 * function.
2022 */
2023int
2024xfs_buf_delwri_submit_nowait(
2025 struct list_head *buffer_list)
2026{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002027 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002028}
2029
2030/*
2031 * Write out a buffer list synchronously.
2032 *
2033 * This will take the @buffer_list, write all buffers out and wait for I/O
2034 * completion on all of the buffers. @buffer_list is consumed by the function,
2035 * so callers must have some other way of tracking buffers if they require such
2036 * functionality.
2037 */
2038int
2039xfs_buf_delwri_submit(
2040 struct list_head *buffer_list)
2041{
Dave Chinner26f1fe82016-06-01 17:38:15 +10002042 LIST_HEAD (wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002043 int error = 0, error2;
2044 struct xfs_buf *bp;
2045
Dave Chinner26f1fe82016-06-01 17:38:15 +10002046 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002047
2048 /* Wait for IO to complete. */
Dave Chinner26f1fe82016-06-01 17:38:15 +10002049 while (!list_empty(&wait_list)) {
2050 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002051
2052 list_del_init(&bp->b_list);
Dave Chinnercf53e992014-10-02 09:04:01 +10002053
Brian Fostere339dd82018-07-11 22:26:34 -07002054 /*
2055 * Wait on the locked buffer, check for errors and unlock and
2056 * release the delwri queue reference.
2057 */
2058 error2 = xfs_buf_iowait(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002059 xfs_buf_relse(bp);
2060 if (!error)
2061 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002064 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
Brian Foster7912e7f2017-06-14 21:21:45 -07002067/*
2068 * Push a single buffer on a delwri queue.
2069 *
2070 * The purpose of this function is to submit a single buffer of a delwri queue
2071 * and return with the buffer still on the original queue. The waiting delwri
2072 * buffer submission infrastructure guarantees transfer of the delwri queue
2073 * buffer reference to a temporary wait list. We reuse this infrastructure to
2074 * transfer the buffer back to the original queue.
2075 *
2076 * Note the buffer transitions from the queued state, to the submitted and wait
2077 * listed state and back to the queued state during this call. The buffer
2078 * locking and queue management logic between _delwri_pushbuf() and
2079 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2080 * before returning.
2081 */
2082int
2083xfs_buf_delwri_pushbuf(
2084 struct xfs_buf *bp,
2085 struct list_head *buffer_list)
2086{
2087 LIST_HEAD (submit_list);
2088 int error;
2089
2090 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2091
2092 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2093
2094 /*
2095 * Isolate the buffer to a new local list so we can submit it for I/O
2096 * independently from the rest of the original list.
2097 */
2098 xfs_buf_lock(bp);
2099 list_move(&bp->b_list, &submit_list);
2100 xfs_buf_unlock(bp);
2101
2102 /*
2103 * Delwri submission clears the DELWRI_Q buffer flag and returns with
Brian Fostere339dd82018-07-11 22:26:34 -07002104 * the buffer on the wait list with the original reference. Rather than
Brian Foster7912e7f2017-06-14 21:21:45 -07002105 * bounce the buffer from a local wait list back to the original list
2106 * after I/O completion, reuse the original list as the wait list.
2107 */
2108 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2109
2110 /*
Brian Fostere339dd82018-07-11 22:26:34 -07002111 * The buffer is now locked, under I/O and wait listed on the original
2112 * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2113 * return with the buffer unlocked and on the original queue.
Brian Foster7912e7f2017-06-14 21:21:45 -07002114 */
Brian Fostere339dd82018-07-11 22:26:34 -07002115 error = xfs_buf_iowait(bp);
Brian Foster7912e7f2017-06-14 21:21:45 -07002116 bp->b_flags |= _XBF_DELWRI_Q;
2117 xfs_buf_unlock(bp);
2118
2119 return error;
2120}
2121
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002122int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11002123xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Nathan Scott87582802006-03-14 13:18:19 +11002125 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
2126 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11002127 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002128 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11002129
Christoph Hellwig23ea4032005-06-21 15:14:01 +10002130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002132 out:
Nathan Scott87582802006-03-14 13:18:19 +11002133 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136void
Nathan Scottce8e9222006-01-11 15:39:08 +11002137xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Nathan Scottce8e9222006-01-11 15:39:08 +11002139 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
Brian Foster7561d272017-10-17 14:16:29 -07002141
2142void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
2143{
Brian Foster7561d272017-10-17 14:16:29 -07002144 /*
2145 * Set the lru reference count to 0 based on the error injection tag.
2146 * This allows userspace to disrupt buffer caching for debug/testing
2147 * purposes.
2148 */
Brian Foster4eadcf92017-10-27 09:20:28 -07002149 if (XFS_TEST_ERROR(false, bp->b_target->bt_mount,
2150 XFS_ERRTAG_BUF_LRU_REF))
Brian Foster7561d272017-10-17 14:16:29 -07002151 lru_ref = 0;
2152
2153 atomic_set(&bp->b_lru_ref, lru_ref);
2154}