]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/xfs/xfs_buf.c
xfs: remove the kuid/kgid conversion wrappers
[mirror_ubuntu-jammy-kernel.git] / fs / xfs / xfs_buf.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
f07c2250 3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 4 * All Rights Reserved.
1da177e4 5 */
93c189c1 6#include "xfs.h"
3fcfab16 7#include <linux/backing-dev.h>
1da177e4 8
5467b34b 9#include "xfs_shared.h"
4fb6e8ad 10#include "xfs_format.h"
239880ef 11#include "xfs_log_format.h"
7fd36c44 12#include "xfs_trans_resv.h"
239880ef 13#include "xfs_sb.h"
b7963133 14#include "xfs_mount.h"
0b1b213f 15#include "xfs_trace.h"
239880ef 16#include "xfs_log.h"
e9e899a2 17#include "xfs_errortag.h"
7561d27e 18#include "xfs_error.h"
b7963133 19
7989cb8e 20static kmem_zone_t *xfs_buf_zone;
23ea4032 21
ce8e922c 22#define xb_to_gfp(flags) \
aa5c158e 23 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
1da177e4 24
37fd1678
DC
25/*
26 * Locking orders
27 *
28 * xfs_buf_ioacct_inc:
29 * xfs_buf_ioacct_dec:
30 * b_sema (caller holds)
31 * b_lock
32 *
33 * xfs_buf_stale:
34 * b_sema (caller holds)
35 * b_lock
36 * lru_lock
37 *
38 * xfs_buf_rele:
39 * b_lock
40 * pag_buf_lock
41 * lru_lock
42 *
43 * xfs_buftarg_wait_rele
44 * lru_lock
45 * b_lock (trylock due to inversion)
46 *
47 * xfs_buftarg_isolate
48 * lru_lock
49 * b_lock (trylock due to inversion)
50 */
1da177e4 51
73c77e2c
JB
52static inline int
53xfs_buf_is_vmapped(
54 struct xfs_buf *bp)
55{
56 /*
57 * Return true if the buffer is vmapped.
58 *
611c9946
DC
59 * b_addr is null if the buffer is not mapped, but the code is clever
60 * enough to know it doesn't have to map a single page, so the check has
61 * to be both for b_addr and bp->b_page_count > 1.
73c77e2c 62 */
611c9946 63 return bp->b_addr && bp->b_page_count > 1;
73c77e2c
JB
64}
65
66static inline int
67xfs_buf_vmap_len(
68 struct xfs_buf *bp)
69{
70 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
71}
72
9c7504aa
BF
73/*
74 * Bump the I/O in flight count on the buftarg if we haven't yet done so for
75 * this buffer. The count is incremented once per buffer (per hold cycle)
76 * because the corresponding decrement is deferred to buffer release. Buffers
77 * can undergo I/O multiple times in a hold-release cycle and per buffer I/O
78 * tracking adds unnecessary overhead. This is used for sychronization purposes
79 * with unmount (see xfs_wait_buftarg()), so all we really need is a count of
80 * in-flight buffers.
81 *
82 * Buffers that are never released (e.g., superblock, iclog buffers) must set
83 * the XBF_NO_IOACCT flag before I/O submission. Otherwise, the buftarg count
84 * never reaches zero and unmount hangs indefinitely.
85 */
86static inline void
87xfs_buf_ioacct_inc(
88 struct xfs_buf *bp)
89{
63db7c81 90 if (bp->b_flags & XBF_NO_IOACCT)
9c7504aa
BF
91 return;
92
93 ASSERT(bp->b_flags & XBF_ASYNC);
63db7c81
BF
94 spin_lock(&bp->b_lock);
95 if (!(bp->b_state & XFS_BSTATE_IN_FLIGHT)) {
96 bp->b_state |= XFS_BSTATE_IN_FLIGHT;
97 percpu_counter_inc(&bp->b_target->bt_io_count);
98 }
99 spin_unlock(&bp->b_lock);
9c7504aa
BF
100}
101
102/*
103 * Clear the in-flight state on a buffer about to be released to the LRU or
104 * freed and unaccount from the buftarg.
105 */
106static inline void
63db7c81 107__xfs_buf_ioacct_dec(
9c7504aa
BF
108 struct xfs_buf *bp)
109{
95989c46 110 lockdep_assert_held(&bp->b_lock);
9c7504aa 111
63db7c81
BF
112 if (bp->b_state & XFS_BSTATE_IN_FLIGHT) {
113 bp->b_state &= ~XFS_BSTATE_IN_FLIGHT;
114 percpu_counter_dec(&bp->b_target->bt_io_count);
115 }
116}
117
118static inline void
119xfs_buf_ioacct_dec(
120 struct xfs_buf *bp)
121{
122 spin_lock(&bp->b_lock);
123 __xfs_buf_ioacct_dec(bp);
124 spin_unlock(&bp->b_lock);
9c7504aa
BF
125}
126
430cbeb8
DC
127/*
128 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
129 * b_lru_ref count so that the buffer is freed immediately when the buffer
130 * reference count falls to zero. If the buffer is already on the LRU, we need
131 * to remove the reference that LRU holds on the buffer.
132 *
133 * This prevents build-up of stale buffers on the LRU.
134 */
135void
136xfs_buf_stale(
137 struct xfs_buf *bp)
138{
43ff2122
CH
139 ASSERT(xfs_buf_islocked(bp));
140
430cbeb8 141 bp->b_flags |= XBF_STALE;
43ff2122
CH
142
143 /*
144 * Clear the delwri status so that a delwri queue walker will not
145 * flush this buffer to disk now that it is stale. The delwri queue has
146 * a reference to the buffer, so this is safe to do.
147 */
148 bp->b_flags &= ~_XBF_DELWRI_Q;
149
9c7504aa
BF
150 /*
151 * Once the buffer is marked stale and unlocked, a subsequent lookup
152 * could reset b_flags. There is no guarantee that the buffer is
153 * unaccounted (released to LRU) before that occurs. Drop in-flight
154 * status now to preserve accounting consistency.
155 */
a4082357 156 spin_lock(&bp->b_lock);
63db7c81
BF
157 __xfs_buf_ioacct_dec(bp);
158
a4082357
DC
159 atomic_set(&bp->b_lru_ref, 0);
160 if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
e80dfa19
DC
161 (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
162 atomic_dec(&bp->b_hold);
163
430cbeb8 164 ASSERT(atomic_read(&bp->b_hold) >= 1);
a4082357 165 spin_unlock(&bp->b_lock);
430cbeb8 166}
1da177e4 167
3e85c868
DC
168static int
169xfs_buf_get_maps(
170 struct xfs_buf *bp,
171 int map_count)
172{
173 ASSERT(bp->b_maps == NULL);
174 bp->b_map_count = map_count;
175
176 if (map_count == 1) {
f4b42421 177 bp->b_maps = &bp->__b_map;
3e85c868
DC
178 return 0;
179 }
180
181 bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
182 KM_NOFS);
183 if (!bp->b_maps)
2451337d 184 return -ENOMEM;
3e85c868
DC
185 return 0;
186}
187
188/*
189 * Frees b_pages if it was allocated.
190 */
191static void
192xfs_buf_free_maps(
193 struct xfs_buf *bp)
194{
f4b42421 195 if (bp->b_maps != &bp->__b_map) {
3e85c868
DC
196 kmem_free(bp->b_maps);
197 bp->b_maps = NULL;
198 }
199}
200
32dff5e5 201static int
3e85c868 202_xfs_buf_alloc(
4347b9d7 203 struct xfs_buftarg *target,
3e85c868
DC
204 struct xfs_buf_map *map,
205 int nmaps,
32dff5e5
DW
206 xfs_buf_flags_t flags,
207 struct xfs_buf **bpp)
1da177e4 208{
4347b9d7 209 struct xfs_buf *bp;
3e85c868
DC
210 int error;
211 int i;
4347b9d7 212
32dff5e5 213 *bpp = NULL;
aa5c158e 214 bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
4347b9d7 215 if (unlikely(!bp))
32dff5e5 216 return -ENOMEM;
4347b9d7 217
1da177e4 218 /*
12bcb3f7
DC
219 * We don't want certain flags to appear in b_flags unless they are
220 * specifically set by later operations on the buffer.
1da177e4 221 */
611c9946 222 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
ce8e922c 223
ce8e922c 224 atomic_set(&bp->b_hold, 1);
430cbeb8 225 atomic_set(&bp->b_lru_ref, 1);
b4dd330b 226 init_completion(&bp->b_iowait);
430cbeb8 227 INIT_LIST_HEAD(&bp->b_lru);
ce8e922c 228 INIT_LIST_HEAD(&bp->b_list);
643c8c05 229 INIT_LIST_HEAD(&bp->b_li_list);
a731cd11 230 sema_init(&bp->b_sema, 0); /* held, no waiters */
a4082357 231 spin_lock_init(&bp->b_lock);
ce8e922c 232 bp->b_target = target;
dbd329f1 233 bp->b_mount = target->bt_mount;
3e85c868 234 bp->b_flags = flags;
de1cbee4 235
1da177e4 236 /*
aa0e8833
DC
237 * Set length and io_length to the same value initially.
238 * I/O routines should use io_length, which will be the same in
1da177e4
LT
239 * most cases but may be reset (e.g. XFS recovery).
240 */
3e85c868
DC
241 error = xfs_buf_get_maps(bp, nmaps);
242 if (error) {
377bcd5f 243 kmem_cache_free(xfs_buf_zone, bp);
32dff5e5 244 return error;
3e85c868
DC
245 }
246
247 bp->b_bn = map[0].bm_bn;
248 bp->b_length = 0;
249 for (i = 0; i < nmaps; i++) {
250 bp->b_maps[i].bm_bn = map[i].bm_bn;
251 bp->b_maps[i].bm_len = map[i].bm_len;
252 bp->b_length += map[i].bm_len;
253 }
3e85c868 254
ce8e922c
NS
255 atomic_set(&bp->b_pin_count, 0);
256 init_waitqueue_head(&bp->b_waiters);
257
dbd329f1 258 XFS_STATS_INC(bp->b_mount, xb_create);
0b1b213f 259 trace_xfs_buf_init(bp, _RET_IP_);
4347b9d7 260
32dff5e5
DW
261 *bpp = bp;
262 return 0;
1da177e4
LT
263}
264
265/*
ce8e922c
NS
266 * Allocate a page array capable of holding a specified number
267 * of pages, and point the page buf at it.
1da177e4
LT
268 */
269STATIC int
ce8e922c
NS
270_xfs_buf_get_pages(
271 xfs_buf_t *bp,
87937bf8 272 int page_count)
1da177e4
LT
273{
274 /* Make sure that we have a page list */
ce8e922c 275 if (bp->b_pages == NULL) {
ce8e922c
NS
276 bp->b_page_count = page_count;
277 if (page_count <= XB_PAGES) {
278 bp->b_pages = bp->b_page_array;
1da177e4 279 } else {
ce8e922c 280 bp->b_pages = kmem_alloc(sizeof(struct page *) *
aa5c158e 281 page_count, KM_NOFS);
ce8e922c 282 if (bp->b_pages == NULL)
1da177e4
LT
283 return -ENOMEM;
284 }
ce8e922c 285 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
1da177e4
LT
286 }
287 return 0;
288}
289
290/*
ce8e922c 291 * Frees b_pages if it was allocated.
1da177e4
LT
292 */
293STATIC void
ce8e922c 294_xfs_buf_free_pages(
1da177e4
LT
295 xfs_buf_t *bp)
296{
ce8e922c 297 if (bp->b_pages != bp->b_page_array) {
f0e2d93c 298 kmem_free(bp->b_pages);
3fc98b1a 299 bp->b_pages = NULL;
1da177e4
LT
300 }
301}
302
303/*
304 * Releases the specified buffer.
305 *
306 * The modification state of any associated pages is left unchanged.
b46fe825 307 * The buffer must not be on any hash - use xfs_buf_rele instead for
1da177e4
LT
308 * hashed and refcounted buffers
309 */
25a40957 310static void
ce8e922c 311xfs_buf_free(
1da177e4
LT
312 xfs_buf_t *bp)
313{
0b1b213f 314 trace_xfs_buf_free(bp, _RET_IP_);
1da177e4 315
430cbeb8
DC
316 ASSERT(list_empty(&bp->b_lru));
317
0e6e847f 318 if (bp->b_flags & _XBF_PAGES) {
1da177e4
LT
319 uint i;
320
73c77e2c 321 if (xfs_buf_is_vmapped(bp))
8a262e57
AE
322 vm_unmap_ram(bp->b_addr - bp->b_offset,
323 bp->b_page_count);
1da177e4 324
948ecdb4
NS
325 for (i = 0; i < bp->b_page_count; i++) {
326 struct page *page = bp->b_pages[i];
327
0e6e847f 328 __free_page(page);
948ecdb4 329 }
0e6e847f
DC
330 } else if (bp->b_flags & _XBF_KMEM)
331 kmem_free(bp->b_addr);
3fc98b1a 332 _xfs_buf_free_pages(bp);
3e85c868 333 xfs_buf_free_maps(bp);
377bcd5f 334 kmem_cache_free(xfs_buf_zone, bp);
1da177e4
LT
335}
336
337/*
0e6e847f 338 * Allocates all the pages for buffer in question and builds it's page list.
1da177e4
LT
339 */
340STATIC int
0e6e847f 341xfs_buf_allocate_memory(
1da177e4
LT
342 xfs_buf_t *bp,
343 uint flags)
344{
aa0e8833 345 size_t size;
1da177e4 346 size_t nbytes, offset;
ce8e922c 347 gfp_t gfp_mask = xb_to_gfp(flags);
1da177e4 348 unsigned short page_count, i;
795cac72 349 xfs_off_t start, end;
1da177e4 350 int error;
3219e8cf
BD
351 xfs_km_flags_t kmflag_mask = 0;
352
353 /*
354 * assure zeroed buffer for non-read cases.
355 */
356 if (!(flags & XBF_READ)) {
357 kmflag_mask |= KM_ZERO;
358 gfp_mask |= __GFP_ZERO;
359 }
1da177e4 360
0e6e847f
DC
361 /*
362 * for buffers that are contained within a single page, just allocate
363 * the memory from the heap - there's no need for the complexity of
364 * page arrays to keep allocation down to order 0.
365 */
795cac72
DC
366 size = BBTOB(bp->b_length);
367 if (size < PAGE_SIZE) {
f8f9ee47 368 int align_mask = xfs_buftarg_dma_alignment(bp->b_target);
3219e8cf
BD
369 bp->b_addr = kmem_alloc_io(size, align_mask,
370 KM_NOFS | kmflag_mask);
0e6e847f
DC
371 if (!bp->b_addr) {
372 /* low memory - use alloc_page loop instead */
373 goto use_alloc_page;
374 }
375
795cac72 376 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
0e6e847f
DC
377 ((unsigned long)bp->b_addr & PAGE_MASK)) {
378 /* b_addr spans two pages - use alloc_page instead */
379 kmem_free(bp->b_addr);
380 bp->b_addr = NULL;
381 goto use_alloc_page;
382 }
383 bp->b_offset = offset_in_page(bp->b_addr);
384 bp->b_pages = bp->b_page_array;
f8f9ee47 385 bp->b_pages[0] = kmem_to_page(bp->b_addr);
0e6e847f 386 bp->b_page_count = 1;
611c9946 387 bp->b_flags |= _XBF_KMEM;
0e6e847f
DC
388 return 0;
389 }
390
391use_alloc_page:
f4b42421
MT
392 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
393 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
cbb7baab 394 >> PAGE_SHIFT;
795cac72 395 page_count = end - start;
87937bf8 396 error = _xfs_buf_get_pages(bp, page_count);
1da177e4
LT
397 if (unlikely(error))
398 return error;
1da177e4 399
ce8e922c 400 offset = bp->b_offset;
0e6e847f 401 bp->b_flags |= _XBF_PAGES;
1da177e4 402
ce8e922c 403 for (i = 0; i < bp->b_page_count; i++) {
1da177e4
LT
404 struct page *page;
405 uint retries = 0;
0e6e847f
DC
406retry:
407 page = alloc_page(gfp_mask);
1da177e4 408 if (unlikely(page == NULL)) {
ce8e922c
NS
409 if (flags & XBF_READ_AHEAD) {
410 bp->b_page_count = i;
2451337d 411 error = -ENOMEM;
0e6e847f 412 goto out_free_pages;
1da177e4
LT
413 }
414
415 /*
416 * This could deadlock.
417 *
418 * But until all the XFS lowlevel code is revamped to
419 * handle buffer allocation failures we can't do much.
420 */
421 if (!(++retries % 100))
4f10700a 422 xfs_err(NULL,
5bf97b1c
TH
423 "%s(%u) possible memory allocation deadlock in %s (mode:0x%x)",
424 current->comm, current->pid,
34a622b2 425 __func__, gfp_mask);
1da177e4 426
dbd329f1 427 XFS_STATS_INC(bp->b_mount, xb_page_retries);
8aa7e847 428 congestion_wait(BLK_RW_ASYNC, HZ/50);
1da177e4
LT
429 goto retry;
430 }
431
dbd329f1 432 XFS_STATS_INC(bp->b_mount, xb_page_found);
1da177e4 433
0e6e847f 434 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
1da177e4 435 size -= nbytes;
ce8e922c 436 bp->b_pages[i] = page;
1da177e4
LT
437 offset = 0;
438 }
0e6e847f 439 return 0;
1da177e4 440
0e6e847f
DC
441out_free_pages:
442 for (i = 0; i < bp->b_page_count; i++)
443 __free_page(bp->b_pages[i]);
2aa6ba7b 444 bp->b_flags &= ~_XBF_PAGES;
1da177e4
LT
445 return error;
446}
447
448/*
25985edc 449 * Map buffer into kernel address-space if necessary.
1da177e4
LT
450 */
451STATIC int
ce8e922c 452_xfs_buf_map_pages(
1da177e4
LT
453 xfs_buf_t *bp,
454 uint flags)
455{
0e6e847f 456 ASSERT(bp->b_flags & _XBF_PAGES);
ce8e922c 457 if (bp->b_page_count == 1) {
0e6e847f 458 /* A single page buffer is always mappable */
ce8e922c 459 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
611c9946
DC
460 } else if (flags & XBF_UNMAPPED) {
461 bp->b_addr = NULL;
462 } else {
a19fb380 463 int retried = 0;
9ba1fb2c 464 unsigned nofs_flag;
ae687e58
DC
465
466 /*
cf085a1b 467 * vm_map_ram() will allocate auxiliary structures (e.g.
ae687e58
DC
468 * pagetables) with GFP_KERNEL, yet we are likely to be under
469 * GFP_NOFS context here. Hence we need to tell memory reclaim
9ba1fb2c 470 * that we are in such a context via PF_MEMALLOC_NOFS to prevent
ae687e58
DC
471 * memory reclaim re-entering the filesystem here and
472 * potentially deadlocking.
473 */
9ba1fb2c 474 nofs_flag = memalloc_nofs_save();
a19fb380
DC
475 do {
476 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
477 -1, PAGE_KERNEL);
478 if (bp->b_addr)
479 break;
480 vm_unmap_aliases();
481 } while (retried++ <= 1);
9ba1fb2c 482 memalloc_nofs_restore(nofs_flag);
a19fb380
DC
483
484 if (!bp->b_addr)
1da177e4 485 return -ENOMEM;
ce8e922c 486 bp->b_addr += bp->b_offset;
1da177e4
LT
487 }
488
489 return 0;
490}
491
492/*
493 * Finding and Reading Buffers
494 */
6031e73a
LS
495static int
496_xfs_buf_obj_cmp(
497 struct rhashtable_compare_arg *arg,
498 const void *obj)
499{
500 const struct xfs_buf_map *map = arg->key;
501 const struct xfs_buf *bp = obj;
502
503 /*
504 * The key hashing in the lookup path depends on the key being the
505 * first element of the compare_arg, make sure to assert this.
506 */
507 BUILD_BUG_ON(offsetof(struct xfs_buf_map, bm_bn) != 0);
508
509 if (bp->b_bn != map->bm_bn)
510 return 1;
511
512 if (unlikely(bp->b_length != map->bm_len)) {
513 /*
514 * found a block number match. If the range doesn't
515 * match, the only way this is allowed is if the buffer
516 * in the cache is stale and the transaction that made
517 * it stale has not yet committed. i.e. we are
518 * reallocating a busy extent. Skip this buffer and
519 * continue searching for an exact match.
520 */
521 ASSERT(bp->b_flags & XBF_STALE);
522 return 1;
523 }
524 return 0;
525}
526
527static const struct rhashtable_params xfs_buf_hash_params = {
528 .min_size = 32, /* empty AGs have minimal footprint */
529 .nelem_hint = 16,
530 .key_len = sizeof(xfs_daddr_t),
531 .key_offset = offsetof(struct xfs_buf, b_bn),
532 .head_offset = offsetof(struct xfs_buf, b_rhash_head),
533 .automatic_shrinking = true,
534 .obj_cmpfn = _xfs_buf_obj_cmp,
535};
536
537int
538xfs_buf_hash_init(
539 struct xfs_perag *pag)
540{
541 spin_lock_init(&pag->pag_buf_lock);
542 return rhashtable_init(&pag->pag_buf_hash, &xfs_buf_hash_params);
543}
544
545void
546xfs_buf_hash_destroy(
547 struct xfs_perag *pag)
548{
549 rhashtable_destroy(&pag->pag_buf_hash);
550}
1da177e4
LT
551
552/*
b027d4c9
DC
553 * Look up a buffer in the buffer cache and return it referenced and locked
554 * in @found_bp.
555 *
556 * If @new_bp is supplied and we have a lookup miss, insert @new_bp into the
557 * cache.
558 *
559 * If XBF_TRYLOCK is set in @flags, only try to lock the buffer and return
560 * -EAGAIN if we fail to lock it.
561 *
562 * Return values are:
563 * -EFSCORRUPTED if have been supplied with an invalid address
564 * -EAGAIN on trylock failure
565 * -ENOENT if we fail to find a match and @new_bp was NULL
566 * 0, with @found_bp:
567 * - @new_bp if we inserted it into the cache
568 * - the buffer we found and locked.
1da177e4 569 */
b027d4c9
DC
570static int
571xfs_buf_find(
e70b73f8 572 struct xfs_buftarg *btp,
3e85c868
DC
573 struct xfs_buf_map *map,
574 int nmaps,
ce8e922c 575 xfs_buf_flags_t flags,
b027d4c9
DC
576 struct xfs_buf *new_bp,
577 struct xfs_buf **found_bp)
1da177e4 578{
74f75a0c 579 struct xfs_perag *pag;
74f75a0c 580 xfs_buf_t *bp;
6031e73a 581 struct xfs_buf_map cmap = { .bm_bn = map[0].bm_bn };
10616b80 582 xfs_daddr_t eofs;
3e85c868 583 int i;
1da177e4 584
b027d4c9
DC
585 *found_bp = NULL;
586
3e85c868 587 for (i = 0; i < nmaps; i++)
6031e73a 588 cmap.bm_len += map[i].bm_len;
1da177e4
LT
589
590 /* Check for IOs smaller than the sector size / not sector aligned */
6031e73a
LS
591 ASSERT(!(BBTOB(cmap.bm_len) < btp->bt_meta_sectorsize));
592 ASSERT(!(BBTOB(cmap.bm_bn) & (xfs_off_t)btp->bt_meta_sectormask));
1da177e4 593
10616b80
DC
594 /*
595 * Corrupted block numbers can get through to here, unfortunately, so we
596 * have to check that the buffer falls within the filesystem bounds.
597 */
598 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
6031e73a 599 if (cmap.bm_bn < 0 || cmap.bm_bn >= eofs) {
10616b80 600 xfs_alert(btp->bt_mount,
c219b015 601 "%s: daddr 0x%llx out of range, EOFS 0x%llx",
6031e73a 602 __func__, cmap.bm_bn, eofs);
7bc0dc27 603 WARN_ON(1);
b027d4c9 604 return -EFSCORRUPTED;
10616b80
DC
605 }
606
74f75a0c 607 pag = xfs_perag_get(btp->bt_mount,
6031e73a 608 xfs_daddr_to_agno(btp->bt_mount, cmap.bm_bn));
74f75a0c 609
74f75a0c 610 spin_lock(&pag->pag_buf_lock);
6031e73a
LS
611 bp = rhashtable_lookup_fast(&pag->pag_buf_hash, &cmap,
612 xfs_buf_hash_params);
613 if (bp) {
614 atomic_inc(&bp->b_hold);
615 goto found;
1da177e4
LT
616 }
617
618 /* No match found */
b027d4c9 619 if (!new_bp) {
ff6d6af2 620 XFS_STATS_INC(btp->bt_mount, xb_miss_locked);
74f75a0c
DC
621 spin_unlock(&pag->pag_buf_lock);
622 xfs_perag_put(pag);
b027d4c9 623 return -ENOENT;
1da177e4 624 }
b027d4c9
DC
625
626 /* the buffer keeps the perag reference until it is freed */
627 new_bp->b_pag = pag;
628 rhashtable_insert_fast(&pag->pag_buf_hash, &new_bp->b_rhash_head,
629 xfs_buf_hash_params);
630 spin_unlock(&pag->pag_buf_lock);
631 *found_bp = new_bp;
632 return 0;
1da177e4
LT
633
634found:
74f75a0c
DC
635 spin_unlock(&pag->pag_buf_lock);
636 xfs_perag_put(pag);
1da177e4 637
0c842ad4
CH
638 if (!xfs_buf_trylock(bp)) {
639 if (flags & XBF_TRYLOCK) {
ce8e922c 640 xfs_buf_rele(bp);
ff6d6af2 641 XFS_STATS_INC(btp->bt_mount, xb_busy_locked);
b027d4c9 642 return -EAGAIN;
1da177e4 643 }
0c842ad4 644 xfs_buf_lock(bp);
ff6d6af2 645 XFS_STATS_INC(btp->bt_mount, xb_get_locked_waited);
1da177e4
LT
646 }
647
0e6e847f
DC
648 /*
649 * if the buffer is stale, clear all the external state associated with
650 * it. We need to keep flags such as how we allocated the buffer memory
651 * intact here.
652 */
ce8e922c
NS
653 if (bp->b_flags & XBF_STALE) {
654 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
cfb02852 655 ASSERT(bp->b_iodone == NULL);
611c9946 656 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
1813dd64 657 bp->b_ops = NULL;
2f926587 658 }
0b1b213f
CH
659
660 trace_xfs_buf_find(bp, flags, _RET_IP_);
ff6d6af2 661 XFS_STATS_INC(btp->bt_mount, xb_get_locked);
b027d4c9
DC
662 *found_bp = bp;
663 return 0;
1da177e4
LT
664}
665
8925a3dc
DC
666struct xfs_buf *
667xfs_buf_incore(
668 struct xfs_buftarg *target,
669 xfs_daddr_t blkno,
670 size_t numblks,
671 xfs_buf_flags_t flags)
672{
b027d4c9
DC
673 struct xfs_buf *bp;
674 int error;
8925a3dc 675 DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
b027d4c9
DC
676
677 error = xfs_buf_find(target, &map, 1, flags, NULL, &bp);
678 if (error)
679 return NULL;
680 return bp;
8925a3dc
DC
681}
682
1da177e4 683/*
3815832a
DC
684 * Assembles a buffer covering the specified range. The code is optimised for
685 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
686 * more hits than misses.
1da177e4 687 */
3848b5f6 688int
6dde2707
DC
689xfs_buf_get_map(
690 struct xfs_buftarg *target,
691 struct xfs_buf_map *map,
692 int nmaps,
3848b5f6
DW
693 xfs_buf_flags_t flags,
694 struct xfs_buf **bpp)
1da177e4 695{
3815832a
DC
696 struct xfs_buf *bp;
697 struct xfs_buf *new_bp;
0e6e847f 698 int error = 0;
1da177e4 699
3848b5f6 700 *bpp = NULL;
b027d4c9 701 error = xfs_buf_find(target, map, nmaps, flags, NULL, &bp);
3848b5f6 702 if (!error)
3815832a 703 goto found;
3848b5f6
DW
704 if (error != -ENOENT)
705 return error;
3815832a 706
32dff5e5
DW
707 error = _xfs_buf_alloc(target, map, nmaps, flags, &new_bp);
708 if (error)
3848b5f6 709 return error;
1da177e4 710
fe2429b0
DC
711 error = xfs_buf_allocate_memory(new_bp, flags);
712 if (error) {
3e85c868 713 xfs_buf_free(new_bp);
3848b5f6 714 return error;
fe2429b0
DC
715 }
716
b027d4c9
DC
717 error = xfs_buf_find(target, map, nmaps, flags, new_bp, &bp);
718 if (error) {
fe2429b0 719 xfs_buf_free(new_bp);
3848b5f6 720 return error;
3815832a
DC
721 }
722
fe2429b0
DC
723 if (bp != new_bp)
724 xfs_buf_free(new_bp);
1da177e4 725
3815832a 726found:
611c9946 727 if (!bp->b_addr) {
ce8e922c 728 error = _xfs_buf_map_pages(bp, flags);
1da177e4 729 if (unlikely(error)) {
93baa55a
DW
730 xfs_warn_ratelimited(target->bt_mount,
731 "%s: failed to map %u pages", __func__,
732 bp->b_page_count);
a8acad70 733 xfs_buf_relse(bp);
3848b5f6 734 return error;
1da177e4
LT
735 }
736 }
737
b79f4a1c
DC
738 /*
739 * Clear b_error if this is a lookup from a caller that doesn't expect
740 * valid data to be found in the buffer.
741 */
742 if (!(flags & XBF_READ))
743 xfs_buf_ioerror(bp, 0);
744
ff6d6af2 745 XFS_STATS_INC(target->bt_mount, xb_get);
0b1b213f 746 trace_xfs_buf_get(bp, flags, _RET_IP_);
3848b5f6
DW
747 *bpp = bp;
748 return 0;
1da177e4
LT
749}
750
5d765b97
CH
751STATIC int
752_xfs_buf_read(
753 xfs_buf_t *bp,
754 xfs_buf_flags_t flags)
755{
43ff2122 756 ASSERT(!(flags & XBF_WRITE));
f4b42421 757 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
5d765b97 758
43ff2122 759 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
1d5ae5df 760 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
5d765b97 761
6af88cda 762 return xfs_buf_submit(bp);
5d765b97
CH
763}
764
1aff5696 765/*
75d02303 766 * Reverify a buffer found in cache without an attached ->b_ops.
add46b3b 767 *
75d02303
BF
768 * If the caller passed an ops structure and the buffer doesn't have ops
769 * assigned, set the ops and use it to verify the contents. If verification
770 * fails, clear XBF_DONE. We assume the buffer has no recorded errors and is
771 * already in XBF_DONE state on entry.
add46b3b 772 *
75d02303
BF
773 * Under normal operations, every in-core buffer is verified on read I/O
774 * completion. There are two scenarios that can lead to in-core buffers without
775 * an assigned ->b_ops. The first is during log recovery of buffers on a V4
776 * filesystem, though these buffers are purged at the end of recovery. The
777 * other is online repair, which intentionally reads with a NULL buffer ops to
778 * run several verifiers across an in-core buffer in order to establish buffer
779 * type. If repair can't establish that, the buffer will be left in memory
780 * with NULL buffer ops.
1aff5696
DW
781 */
782int
75d02303 783xfs_buf_reverify(
1aff5696
DW
784 struct xfs_buf *bp,
785 const struct xfs_buf_ops *ops)
786{
787 ASSERT(bp->b_flags & XBF_DONE);
788 ASSERT(bp->b_error == 0);
789
790 if (!ops || bp->b_ops)
791 return 0;
792
793 bp->b_ops = ops;
794 bp->b_ops->verify_read(bp);
795 if (bp->b_error)
796 bp->b_flags &= ~XBF_DONE;
797 return bp->b_error;
798}
799
4ed8e27b 800int
6dde2707
DC
801xfs_buf_read_map(
802 struct xfs_buftarg *target,
803 struct xfs_buf_map *map,
804 int nmaps,
c3f8fc73 805 xfs_buf_flags_t flags,
4ed8e27b 806 struct xfs_buf **bpp,
cdbcf82b
DW
807 const struct xfs_buf_ops *ops,
808 xfs_failaddr_t fa)
1da177e4 809{
6dde2707 810 struct xfs_buf *bp;
3848b5f6 811 int error;
ce8e922c
NS
812
813 flags |= XBF_READ;
4ed8e27b 814 *bpp = NULL;
ce8e922c 815
3848b5f6
DW
816 error = xfs_buf_get_map(target, map, nmaps, flags, &bp);
817 if (error)
4ed8e27b 818 return error;
0b1b213f 819
1aff5696
DW
820 trace_xfs_buf_read(bp, flags, _RET_IP_);
821
822 if (!(bp->b_flags & XBF_DONE)) {
4ed8e27b 823 /* Initiate the buffer read and wait. */
1aff5696
DW
824 XFS_STATS_INC(target->bt_mount, xb_get_read);
825 bp->b_ops = ops;
4ed8e27b
DW
826 error = _xfs_buf_read(bp, flags);
827
828 /* Readahead iodone already dropped the buffer, so exit. */
829 if (flags & XBF_ASYNC)
830 return 0;
831 } else {
832 /* Buffer already read; all we need to do is check it. */
833 error = xfs_buf_reverify(bp, ops);
834
835 /* Readahead already finished; drop the buffer and exit. */
836 if (flags & XBF_ASYNC) {
837 xfs_buf_relse(bp);
838 return 0;
839 }
840
841 /* We do not want read in the flags */
842 bp->b_flags &= ~XBF_READ;
843 ASSERT(bp->b_ops != NULL || ops == NULL);
1aff5696
DW
844 }
845
4ed8e27b
DW
846 /*
847 * If we've had a read error, then the contents of the buffer are
848 * invalid and should not be used. To ensure that a followup read tries
849 * to pull the buffer from disk again, we clear the XBF_DONE flag and
850 * mark the buffer stale. This ensures that anyone who has a current
851 * reference to the buffer will interpret it's contents correctly and
852 * future cache lookups will also treat it as an empty, uninitialised
853 * buffer.
854 */
855 if (error) {
856 if (!XFS_FORCED_SHUTDOWN(target->bt_mount))
cdbcf82b 857 xfs_buf_ioerror_alert(bp, fa);
1aff5696 858
4ed8e27b
DW
859 bp->b_flags &= ~XBF_DONE;
860 xfs_buf_stale(bp);
1aff5696 861 xfs_buf_relse(bp);
4ed8e27b
DW
862
863 /* bad CRC means corrupted metadata */
864 if (error == -EFSBADCRC)
865 error = -EFSCORRUPTED;
866 return error;
1da177e4
LT
867 }
868
4ed8e27b
DW
869 *bpp = bp;
870 return 0;
1da177e4
LT
871}
872
1da177e4 873/*
ce8e922c
NS
874 * If we are not low on memory then do the readahead in a deadlock
875 * safe manner.
1da177e4
LT
876 */
877void
6dde2707
DC
878xfs_buf_readahead_map(
879 struct xfs_buftarg *target,
880 struct xfs_buf_map *map,
c3f8fc73 881 int nmaps,
1813dd64 882 const struct xfs_buf_ops *ops)
1da177e4 883{
4ed8e27b
DW
884 struct xfs_buf *bp;
885
efa7c9f9 886 if (bdi_read_congested(target->bt_bdev->bd_bdi))
1da177e4
LT
887 return;
888
6dde2707 889 xfs_buf_read_map(target, map, nmaps,
cdbcf82b
DW
890 XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD, &bp, ops,
891 __this_address);
1da177e4
LT
892}
893
5adc94c2
DC
894/*
895 * Read an uncached buffer from disk. Allocates and returns a locked
896 * buffer containing the disk contents or nothing.
897 */
ba372674 898int
5adc94c2 899xfs_buf_read_uncached(
5adc94c2
DC
900 struct xfs_buftarg *target,
901 xfs_daddr_t daddr,
e70b73f8 902 size_t numblks,
c3f8fc73 903 int flags,
ba372674 904 struct xfs_buf **bpp,
1813dd64 905 const struct xfs_buf_ops *ops)
5adc94c2 906{
eab4e633 907 struct xfs_buf *bp;
2842b6db 908 int error;
5adc94c2 909
ba372674
DC
910 *bpp = NULL;
911
2842b6db
DW
912 error = xfs_buf_get_uncached(target, numblks, flags, &bp);
913 if (error)
914 return error;
5adc94c2
DC
915
916 /* set up the buffer for a read IO */
3e85c868 917 ASSERT(bp->b_map_count == 1);
ba372674 918 bp->b_bn = XFS_BUF_DADDR_NULL; /* always null for uncached buffers */
3e85c868 919 bp->b_maps[0].bm_bn = daddr;
cbb7baab 920 bp->b_flags |= XBF_READ;
1813dd64 921 bp->b_ops = ops;
5adc94c2 922
6af88cda 923 xfs_buf_submit(bp);
ba372674 924 if (bp->b_error) {
2842b6db 925 error = bp->b_error;
83a0adc3 926 xfs_buf_relse(bp);
ba372674 927 return error;
83a0adc3 928 }
ba372674
DC
929
930 *bpp = bp;
931 return 0;
1da177e4
LT
932}
933
2842b6db 934int
686865f7
DC
935xfs_buf_get_uncached(
936 struct xfs_buftarg *target,
e70b73f8 937 size_t numblks,
2842b6db
DW
938 int flags,
939 struct xfs_buf **bpp)
1da177e4 940{
e70b73f8 941 unsigned long page_count;
1fa40b01 942 int error, i;
3e85c868
DC
943 struct xfs_buf *bp;
944 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
1da177e4 945
2842b6db
DW
946 *bpp = NULL;
947
c891c30a 948 /* flags might contain irrelevant bits, pass only what we care about */
32dff5e5
DW
949 error = _xfs_buf_alloc(target, &map, 1, flags & XBF_NO_IOACCT, &bp);
950 if (error)
1da177e4 951 goto fail;
1da177e4 952
e70b73f8 953 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
87937bf8 954 error = _xfs_buf_get_pages(bp, page_count);
1fa40b01 955 if (error)
1da177e4
LT
956 goto fail_free_buf;
957
1fa40b01 958 for (i = 0; i < page_count; i++) {
686865f7 959 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
2842b6db
DW
960 if (!bp->b_pages[i]) {
961 error = -ENOMEM;
1fa40b01 962 goto fail_free_mem;
2842b6db 963 }
1da177e4 964 }
1fa40b01 965 bp->b_flags |= _XBF_PAGES;
1da177e4 966
611c9946 967 error = _xfs_buf_map_pages(bp, 0);
1fa40b01 968 if (unlikely(error)) {
4f10700a 969 xfs_warn(target->bt_mount,
08e96e1a 970 "%s: failed to map pages", __func__);
1da177e4 971 goto fail_free_mem;
1fa40b01 972 }
1da177e4 973
686865f7 974 trace_xfs_buf_get_uncached(bp, _RET_IP_);
2842b6db
DW
975 *bpp = bp;
976 return 0;
1fa40b01 977
1da177e4 978 fail_free_mem:
1fa40b01
CH
979 while (--i >= 0)
980 __free_page(bp->b_pages[i]);
ca165b88 981 _xfs_buf_free_pages(bp);
1da177e4 982 fail_free_buf:
3e85c868 983 xfs_buf_free_maps(bp);
377bcd5f 984 kmem_cache_free(xfs_buf_zone, bp);
1da177e4 985 fail:
2842b6db 986 return error;
1da177e4
LT
987}
988
989/*
1da177e4
LT
990 * Increment reference count on buffer, to hold the buffer concurrently
991 * with another thread which may release (free) the buffer asynchronously.
1da177e4
LT
992 * Must hold the buffer already to call this function.
993 */
994void
ce8e922c
NS
995xfs_buf_hold(
996 xfs_buf_t *bp)
1da177e4 997{
0b1b213f 998 trace_xfs_buf_hold(bp, _RET_IP_);
ce8e922c 999 atomic_inc(&bp->b_hold);
1da177e4
LT
1000}
1001
1002/*
9c7504aa
BF
1003 * Release a hold on the specified buffer. If the hold count is 1, the buffer is
1004 * placed on LRU or freed (depending on b_lru_ref).
1da177e4
LT
1005 */
1006void
ce8e922c
NS
1007xfs_buf_rele(
1008 xfs_buf_t *bp)
1da177e4 1009{
74f75a0c 1010 struct xfs_perag *pag = bp->b_pag;
9c7504aa
BF
1011 bool release;
1012 bool freebuf = false;
1da177e4 1013
0b1b213f 1014 trace_xfs_buf_rele(bp, _RET_IP_);
1da177e4 1015
74f75a0c 1016 if (!pag) {
430cbeb8 1017 ASSERT(list_empty(&bp->b_lru));
9c7504aa
BF
1018 if (atomic_dec_and_test(&bp->b_hold)) {
1019 xfs_buf_ioacct_dec(bp);
fad3aa1e 1020 xfs_buf_free(bp);
9c7504aa 1021 }
fad3aa1e
NS
1022 return;
1023 }
1024
3790689f 1025 ASSERT(atomic_read(&bp->b_hold) > 0);
a4082357 1026
37fd1678
DC
1027 /*
1028 * We grab the b_lock here first to serialise racing xfs_buf_rele()
1029 * calls. The pag_buf_lock being taken on the last reference only
1030 * serialises against racing lookups in xfs_buf_find(). IOWs, the second
1031 * to last reference we drop here is not serialised against the last
1032 * reference until we take bp->b_lock. Hence if we don't grab b_lock
1033 * first, the last "release" reference can win the race to the lock and
1034 * free the buffer before the second-to-last reference is processed,
1035 * leading to a use-after-free scenario.
1036 */
9c7504aa 1037 spin_lock(&bp->b_lock);
37fd1678 1038 release = atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock);
9c7504aa
BF
1039 if (!release) {
1040 /*
1041 * Drop the in-flight state if the buffer is already on the LRU
1042 * and it holds the only reference. This is racy because we
1043 * haven't acquired the pag lock, but the use of _XBF_IN_FLIGHT
1044 * ensures the decrement occurs only once per-buf.
1045 */
1046 if ((atomic_read(&bp->b_hold) == 1) && !list_empty(&bp->b_lru))
63db7c81 1047 __xfs_buf_ioacct_dec(bp);
9c7504aa
BF
1048 goto out_unlock;
1049 }
1050
1051 /* the last reference has been dropped ... */
63db7c81 1052 __xfs_buf_ioacct_dec(bp);
9c7504aa
BF
1053 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
1054 /*
1055 * If the buffer is added to the LRU take a new reference to the
1056 * buffer for the LRU and clear the (now stale) dispose list
1057 * state flag
1058 */
1059 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
1060 bp->b_state &= ~XFS_BSTATE_DISPOSE;
1061 atomic_inc(&bp->b_hold);
1da177e4 1062 }
9c7504aa
BF
1063 spin_unlock(&pag->pag_buf_lock);
1064 } else {
1065 /*
1066 * most of the time buffers will already be removed from the
1067 * LRU, so optimise that case by checking for the
1068 * XFS_BSTATE_DISPOSE flag indicating the last list the buffer
1069 * was on was the disposal list
1070 */
1071 if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
1072 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
1073 } else {
1074 ASSERT(list_empty(&bp->b_lru));
1da177e4 1075 }
9c7504aa
BF
1076
1077 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
6031e73a
LS
1078 rhashtable_remove_fast(&pag->pag_buf_hash, &bp->b_rhash_head,
1079 xfs_buf_hash_params);
9c7504aa
BF
1080 spin_unlock(&pag->pag_buf_lock);
1081 xfs_perag_put(pag);
1082 freebuf = true;
1da177e4 1083 }
9c7504aa
BF
1084
1085out_unlock:
1086 spin_unlock(&bp->b_lock);
1087
1088 if (freebuf)
1089 xfs_buf_free(bp);
1da177e4
LT
1090}
1091
1092
1093/*
0e6e847f 1094 * Lock a buffer object, if it is not already locked.
90810b9e
DC
1095 *
1096 * If we come across a stale, pinned, locked buffer, we know that we are
1097 * being asked to lock a buffer that has been reallocated. Because it is
1098 * pinned, we know that the log has not been pushed to disk and hence it
1099 * will still be locked. Rather than continuing to have trylock attempts
1100 * fail until someone else pushes the log, push it ourselves before
1101 * returning. This means that the xfsaild will not get stuck trying
1102 * to push on stale inode buffers.
1da177e4
LT
1103 */
1104int
0c842ad4
CH
1105xfs_buf_trylock(
1106 struct xfs_buf *bp)
1da177e4
LT
1107{
1108 int locked;
1109
ce8e922c 1110 locked = down_trylock(&bp->b_sema) == 0;
fa6c668d 1111 if (locked)
479c6412 1112 trace_xfs_buf_trylock(bp, _RET_IP_);
fa6c668d 1113 else
479c6412 1114 trace_xfs_buf_trylock_fail(bp, _RET_IP_);
0c842ad4 1115 return locked;
1da177e4 1116}
1da177e4
LT
1117
1118/*
0e6e847f 1119 * Lock a buffer object.
ed3b4d6c
DC
1120 *
1121 * If we come across a stale, pinned, locked buffer, we know that we
1122 * are being asked to lock a buffer that has been reallocated. Because
1123 * it is pinned, we know that the log has not been pushed to disk and
1124 * hence it will still be locked. Rather than sleeping until someone
1125 * else pushes the log, push it ourselves before trying to get the lock.
1da177e4 1126 */
ce8e922c
NS
1127void
1128xfs_buf_lock(
0c842ad4 1129 struct xfs_buf *bp)
1da177e4 1130{
0b1b213f
CH
1131 trace_xfs_buf_lock(bp, _RET_IP_);
1132
ed3b4d6c 1133 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
dbd329f1 1134 xfs_log_force(bp->b_mount, 0);
ce8e922c 1135 down(&bp->b_sema);
0b1b213f
CH
1136
1137 trace_xfs_buf_lock_done(bp, _RET_IP_);
1da177e4
LT
1138}
1139
1da177e4 1140void
ce8e922c 1141xfs_buf_unlock(
0c842ad4 1142 struct xfs_buf *bp)
1da177e4 1143{
20e8a063
BF
1144 ASSERT(xfs_buf_islocked(bp));
1145
ce8e922c 1146 up(&bp->b_sema);
0b1b213f 1147 trace_xfs_buf_unlock(bp, _RET_IP_);
1da177e4
LT
1148}
1149
ce8e922c
NS
1150STATIC void
1151xfs_buf_wait_unpin(
1152 xfs_buf_t *bp)
1da177e4
LT
1153{
1154 DECLARE_WAITQUEUE (wait, current);
1155
ce8e922c 1156 if (atomic_read(&bp->b_pin_count) == 0)
1da177e4
LT
1157 return;
1158
ce8e922c 1159 add_wait_queue(&bp->b_waiters, &wait);
1da177e4
LT
1160 for (;;) {
1161 set_current_state(TASK_UNINTERRUPTIBLE);
ce8e922c 1162 if (atomic_read(&bp->b_pin_count) == 0)
1da177e4 1163 break;
7eaceacc 1164 io_schedule();
1da177e4 1165 }
ce8e922c 1166 remove_wait_queue(&bp->b_waiters, &wait);
1da177e4
LT
1167 set_current_state(TASK_RUNNING);
1168}
1169
1170/*
1171 * Buffer Utility Routines
1172 */
1173
e8aaba9a
DC
1174void
1175xfs_buf_ioend(
1176 struct xfs_buf *bp)
1da177e4 1177{
e8aaba9a
DC
1178 bool read = bp->b_flags & XBF_READ;
1179
1180 trace_xfs_buf_iodone(bp, _RET_IP_);
1813dd64
DC
1181
1182 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
d5929de8 1183
61be9c52
DC
1184 /*
1185 * Pull in IO completion errors now. We are guaranteed to be running
1186 * single threaded, so we don't need the lock to read b_io_error.
1187 */
1188 if (!bp->b_error && bp->b_io_error)
1189 xfs_buf_ioerror(bp, bp->b_io_error);
1190
e8aaba9a
DC
1191 /* Only validate buffers that were read without errors */
1192 if (read && !bp->b_error && bp->b_ops) {
1193 ASSERT(!bp->b_iodone);
1813dd64 1194 bp->b_ops->verify_read(bp);
e8aaba9a
DC
1195 }
1196
1197 if (!bp->b_error)
1198 bp->b_flags |= XBF_DONE;
1da177e4 1199
80f6c29d 1200 if (bp->b_iodone)
ce8e922c
NS
1201 (*(bp->b_iodone))(bp);
1202 else if (bp->b_flags & XBF_ASYNC)
1da177e4 1203 xfs_buf_relse(bp);
595bff75 1204 else
1813dd64 1205 complete(&bp->b_iowait);
1da177e4
LT
1206}
1207
e8aaba9a
DC
1208static void
1209xfs_buf_ioend_work(
1210 struct work_struct *work)
1da177e4 1211{
e8aaba9a 1212 struct xfs_buf *bp =
b29c70f5 1213 container_of(work, xfs_buf_t, b_ioend_work);
0b1b213f 1214
e8aaba9a
DC
1215 xfs_buf_ioend(bp);
1216}
1da177e4 1217
211fe1a4 1218static void
e8aaba9a
DC
1219xfs_buf_ioend_async(
1220 struct xfs_buf *bp)
1221{
b29c70f5 1222 INIT_WORK(&bp->b_ioend_work, xfs_buf_ioend_work);
dbd329f1 1223 queue_work(bp->b_mount->m_buf_workqueue, &bp->b_ioend_work);
1da177e4
LT
1224}
1225
1da177e4 1226void
31ca03c9 1227__xfs_buf_ioerror(
ce8e922c 1228 xfs_buf_t *bp,
31ca03c9
DW
1229 int error,
1230 xfs_failaddr_t failaddr)
1da177e4 1231{
2451337d
DC
1232 ASSERT(error <= 0 && error >= -1000);
1233 bp->b_error = error;
31ca03c9 1234 trace_xfs_buf_ioerror(bp, error, failaddr);
1da177e4
LT
1235}
1236
901796af
CH
1237void
1238xfs_buf_ioerror_alert(
1239 struct xfs_buf *bp,
cdbcf82b 1240 xfs_failaddr_t func)
901796af 1241{
dbd329f1 1242 xfs_alert(bp->b_mount,
cdbcf82b 1243"metadata I/O error in \"%pS\" at daddr 0x%llx len %d error %d",
c219b015
DW
1244 func, (uint64_t)XFS_BUF_ADDR(bp), bp->b_length,
1245 -bp->b_error);
901796af
CH
1246}
1247
a2dcf5df
CH
1248int
1249xfs_bwrite(
1250 struct xfs_buf *bp)
1251{
1252 int error;
1253
1254 ASSERT(xfs_buf_islocked(bp));
1255
1256 bp->b_flags |= XBF_WRITE;
27187754
DC
1257 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q |
1258 XBF_WRITE_FAIL | XBF_DONE);
a2dcf5df 1259
6af88cda 1260 error = xfs_buf_submit(bp);
dbd329f1
CH
1261 if (error)
1262 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
a2dcf5df
CH
1263 return error;
1264}
1265
9bdd9bd6 1266static void
ce8e922c 1267xfs_buf_bio_end_io(
4246a0b6 1268 struct bio *bio)
1da177e4 1269{
9bdd9bd6 1270 struct xfs_buf *bp = (struct xfs_buf *)bio->bi_private;
1da177e4 1271
37eb17e6
DC
1272 /*
1273 * don't overwrite existing errors - otherwise we can lose errors on
1274 * buffers that require multiple bios to complete.
1275 */
4e4cbee9
CH
1276 if (bio->bi_status) {
1277 int error = blk_status_to_errno(bio->bi_status);
1278
1279 cmpxchg(&bp->b_io_error, 0, error);
1280 }
1da177e4 1281
37eb17e6 1282 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
73c77e2c
JB
1283 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1284
e8aaba9a
DC
1285 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1286 xfs_buf_ioend_async(bp);
1da177e4 1287 bio_put(bio);
1da177e4
LT
1288}
1289
3e85c868
DC
1290static void
1291xfs_buf_ioapply_map(
1292 struct xfs_buf *bp,
1293 int map,
1294 int *buf_offset,
1295 int *count,
2123ef85 1296 int op)
1da177e4 1297{
3e85c868
DC
1298 int page_index;
1299 int total_nr_pages = bp->b_page_count;
1300 int nr_pages;
1301 struct bio *bio;
1302 sector_t sector = bp->b_maps[map].bm_bn;
1303 int size;
1304 int offset;
1da177e4 1305
3e85c868
DC
1306 /* skip the pages in the buffer before the start offset */
1307 page_index = 0;
1308 offset = *buf_offset;
1309 while (offset >= PAGE_SIZE) {
1310 page_index++;
1311 offset -= PAGE_SIZE;
f538d4da
CH
1312 }
1313
3e85c868
DC
1314 /*
1315 * Limit the IO size to the length of the current vector, and update the
1316 * remaining IO count for the next time around.
1317 */
1318 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1319 *count -= size;
1320 *buf_offset += size;
34951f5c 1321
1da177e4 1322next_chunk:
ce8e922c 1323 atomic_inc(&bp->b_io_remaining);
c908e380 1324 nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
1da177e4
LT
1325
1326 bio = bio_alloc(GFP_NOIO, nr_pages);
74d46992 1327 bio_set_dev(bio, bp->b_target->bt_bdev);
4f024f37 1328 bio->bi_iter.bi_sector = sector;
ce8e922c
NS
1329 bio->bi_end_io = xfs_buf_bio_end_io;
1330 bio->bi_private = bp;
2123ef85 1331 bio->bi_opf = op;
0e6e847f 1332
3e85c868 1333 for (; size && nr_pages; nr_pages--, page_index++) {
0e6e847f 1334 int rbytes, nbytes = PAGE_SIZE - offset;
1da177e4
LT
1335
1336 if (nbytes > size)
1337 nbytes = size;
1338
3e85c868
DC
1339 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1340 offset);
ce8e922c 1341 if (rbytes < nbytes)
1da177e4
LT
1342 break;
1343
1344 offset = 0;
aa0e8833 1345 sector += BTOBB(nbytes);
1da177e4
LT
1346 size -= nbytes;
1347 total_nr_pages--;
1348 }
1349
4f024f37 1350 if (likely(bio->bi_iter.bi_size)) {
73c77e2c
JB
1351 if (xfs_buf_is_vmapped(bp)) {
1352 flush_kernel_vmap_range(bp->b_addr,
1353 xfs_buf_vmap_len(bp));
1354 }
4e49ea4a 1355 submit_bio(bio);
1da177e4
LT
1356 if (size)
1357 goto next_chunk;
1358 } else {
37eb17e6
DC
1359 /*
1360 * This is guaranteed not to be the last io reference count
595bff75 1361 * because the caller (xfs_buf_submit) holds a count itself.
37eb17e6
DC
1362 */
1363 atomic_dec(&bp->b_io_remaining);
2451337d 1364 xfs_buf_ioerror(bp, -EIO);
ec53d1db 1365 bio_put(bio);
1da177e4 1366 }
3e85c868
DC
1367
1368}
1369
1370STATIC void
1371_xfs_buf_ioapply(
1372 struct xfs_buf *bp)
1373{
1374 struct blk_plug plug;
50bfcd0c 1375 int op;
3e85c868
DC
1376 int offset;
1377 int size;
1378 int i;
1379
c163f9a1
DC
1380 /*
1381 * Make sure we capture only current IO errors rather than stale errors
1382 * left over from previous use of the buffer (e.g. failed readahead).
1383 */
1384 bp->b_error = 0;
1385
3e85c868 1386 if (bp->b_flags & XBF_WRITE) {
50bfcd0c 1387 op = REQ_OP_WRITE;
1813dd64
DC
1388
1389 /*
1390 * Run the write verifier callback function if it exists. If
1391 * this function fails it will mark the buffer with an error and
1392 * the IO should not be dispatched.
1393 */
1394 if (bp->b_ops) {
1395 bp->b_ops->verify_write(bp);
1396 if (bp->b_error) {
dbd329f1 1397 xfs_force_shutdown(bp->b_mount,
1813dd64
DC
1398 SHUTDOWN_CORRUPT_INCORE);
1399 return;
1400 }
400b9d88 1401 } else if (bp->b_bn != XFS_BUF_DADDR_NULL) {
dbd329f1 1402 struct xfs_mount *mp = bp->b_mount;
400b9d88
DC
1403
1404 /*
1405 * non-crc filesystems don't attach verifiers during
1406 * log recovery, so don't warn for such filesystems.
1407 */
1408 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1409 xfs_warn(mp,
c219b015 1410 "%s: no buf ops on daddr 0x%llx len %d",
400b9d88 1411 __func__, bp->b_bn, bp->b_length);
9c712a13
DW
1412 xfs_hex_dump(bp->b_addr,
1413 XFS_CORRUPTION_DUMP_LEN);
400b9d88
DC
1414 dump_stack();
1415 }
1813dd64 1416 }
3e85c868 1417 } else {
50bfcd0c 1418 op = REQ_OP_READ;
2123ef85
CH
1419 if (bp->b_flags & XBF_READ_AHEAD)
1420 op |= REQ_RAHEAD;
3e85c868
DC
1421 }
1422
1423 /* we only use the buffer cache for meta-data */
2123ef85 1424 op |= REQ_META;
3e85c868
DC
1425
1426 /*
1427 * Walk all the vectors issuing IO on them. Set up the initial offset
1428 * into the buffer and the desired IO size before we start -
1429 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1430 * subsequent call.
1431 */
1432 offset = bp->b_offset;
8124b9b6 1433 size = BBTOB(bp->b_length);
3e85c868
DC
1434 blk_start_plug(&plug);
1435 for (i = 0; i < bp->b_map_count; i++) {
2123ef85 1436 xfs_buf_ioapply_map(bp, i, &offset, &size, op);
3e85c868
DC
1437 if (bp->b_error)
1438 break;
1439 if (size <= 0)
1440 break; /* all done */
1441 }
1442 blk_finish_plug(&plug);
1da177e4
LT
1443}
1444
595bff75 1445/*
bb00b6f1 1446 * Wait for I/O completion of a sync buffer and return the I/O error code.
595bff75 1447 */
eaebb515 1448static int
bb00b6f1 1449xfs_buf_iowait(
595bff75 1450 struct xfs_buf *bp)
1da177e4 1451{
bb00b6f1
BF
1452 ASSERT(!(bp->b_flags & XBF_ASYNC));
1453
1454 trace_xfs_buf_iowait(bp, _RET_IP_);
1455 wait_for_completion(&bp->b_iowait);
1456 trace_xfs_buf_iowait_done(bp, _RET_IP_);
1457
1458 return bp->b_error;
1459}
1460
1461/*
1462 * Buffer I/O submission path, read or write. Asynchronous submission transfers
1463 * the buffer lock ownership and the current reference to the IO. It is not
1464 * safe to reference the buffer after a call to this function unless the caller
1465 * holds an additional reference itself.
1466 */
1467int
1468__xfs_buf_submit(
1469 struct xfs_buf *bp,
1470 bool wait)
1471{
1472 int error = 0;
1473
595bff75 1474 trace_xfs_buf_submit(bp, _RET_IP_);
1da177e4 1475
43ff2122 1476 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
595bff75
DC
1477
1478 /* on shutdown we stale and complete the buffer immediately */
dbd329f1 1479 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
595bff75
DC
1480 xfs_buf_ioerror(bp, -EIO);
1481 bp->b_flags &= ~XBF_DONE;
1482 xfs_buf_stale(bp);
465fa17f 1483 xfs_buf_ioend(bp);
eaebb515 1484 return -EIO;
595bff75 1485 }
1da177e4 1486
bb00b6f1
BF
1487 /*
1488 * Grab a reference so the buffer does not go away underneath us. For
1489 * async buffers, I/O completion drops the callers reference, which
1490 * could occur before submission returns.
1491 */
1492 xfs_buf_hold(bp);
1493
375ec69d 1494 if (bp->b_flags & XBF_WRITE)
ce8e922c 1495 xfs_buf_wait_unpin(bp);
e11bb805 1496
61be9c52
DC
1497 /* clear the internal error state to avoid spurious errors */
1498 bp->b_io_error = 0;
1499
8d6c1210 1500 /*
e11bb805
DC
1501 * Set the count to 1 initially, this will stop an I/O completion
1502 * callout which happens before we have started all the I/O from calling
1503 * xfs_buf_ioend too early.
1da177e4 1504 */
ce8e922c 1505 atomic_set(&bp->b_io_remaining, 1);
eaebb515
BF
1506 if (bp->b_flags & XBF_ASYNC)
1507 xfs_buf_ioacct_inc(bp);
ce8e922c 1508 _xfs_buf_ioapply(bp);
e11bb805 1509
8d6c1210 1510 /*
595bff75
DC
1511 * If _xfs_buf_ioapply failed, we can get back here with only the IO
1512 * reference we took above. If we drop it to zero, run completion so
1513 * that we don't return to the caller with completion still pending.
8d6c1210 1514 */
e8aaba9a 1515 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
eaebb515 1516 if (bp->b_error || !(bp->b_flags & XBF_ASYNC))
e8aaba9a
DC
1517 xfs_buf_ioend(bp);
1518 else
1519 xfs_buf_ioend_async(bp);
1520 }
1da177e4 1521
6af88cda
BF
1522 if (wait)
1523 error = xfs_buf_iowait(bp);
bb00b6f1 1524
595bff75 1525 /*
6af88cda
BF
1526 * Release the hold that keeps the buffer referenced for the entire
1527 * I/O. Note that if the buffer is async, it is not safe to reference
1528 * after this release.
595bff75
DC
1529 */
1530 xfs_buf_rele(bp);
1531 return error;
1da177e4
LT
1532}
1533
88ee2df7 1534void *
ce8e922c 1535xfs_buf_offset(
88ee2df7 1536 struct xfs_buf *bp,
1da177e4
LT
1537 size_t offset)
1538{
1539 struct page *page;
1540
611c9946 1541 if (bp->b_addr)
62926044 1542 return bp->b_addr + offset;
1da177e4 1543
ce8e922c 1544 offset += bp->b_offset;
0e6e847f 1545 page = bp->b_pages[offset >> PAGE_SHIFT];
88ee2df7 1546 return page_address(page) + (offset & (PAGE_SIZE-1));
1da177e4
LT
1547}
1548
1da177e4 1549void
f9a196ee
CH
1550xfs_buf_zero(
1551 struct xfs_buf *bp,
1552 size_t boff,
1553 size_t bsize)
1da177e4 1554{
795cac72 1555 size_t bend;
1da177e4
LT
1556
1557 bend = boff + bsize;
1558 while (boff < bend) {
795cac72
DC
1559 struct page *page;
1560 int page_index, page_offset, csize;
1561
1562 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1563 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1564 page = bp->b_pages[page_index];
1565 csize = min_t(size_t, PAGE_SIZE - page_offset,
8124b9b6 1566 BBTOB(bp->b_length) - boff);
1da177e4 1567
795cac72 1568 ASSERT((csize + page_offset) <= PAGE_SIZE);
1da177e4 1569
f9a196ee 1570 memset(page_address(page) + page_offset, 0, csize);
1da177e4
LT
1571
1572 boff += csize;
1da177e4
LT
1573 }
1574}
1575
1576/*
ce8e922c 1577 * Handling of buffer targets (buftargs).
1da177e4
LT
1578 */
1579
1580/*
430cbeb8
DC
1581 * Wait for any bufs with callbacks that have been submitted but have not yet
1582 * returned. These buffers will have an elevated hold count, so wait on those
1583 * while freeing all the buffers only held by the LRU.
1da177e4 1584 */
e80dfa19
DC
1585static enum lru_status
1586xfs_buftarg_wait_rele(
1587 struct list_head *item,
3f97b163 1588 struct list_lru_one *lru,
e80dfa19
DC
1589 spinlock_t *lru_lock,
1590 void *arg)
1591
1da177e4 1592{
e80dfa19 1593 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
a4082357 1594 struct list_head *dispose = arg;
430cbeb8 1595
e80dfa19 1596 if (atomic_read(&bp->b_hold) > 1) {
a4082357 1597 /* need to wait, so skip it this pass */
e80dfa19 1598 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
a4082357 1599 return LRU_SKIP;
1da177e4 1600 }
a4082357
DC
1601 if (!spin_trylock(&bp->b_lock))
1602 return LRU_SKIP;
e80dfa19 1603
a4082357
DC
1604 /*
1605 * clear the LRU reference count so the buffer doesn't get
1606 * ignored in xfs_buf_rele().
1607 */
1608 atomic_set(&bp->b_lru_ref, 0);
1609 bp->b_state |= XFS_BSTATE_DISPOSE;
3f97b163 1610 list_lru_isolate_move(lru, item, dispose);
a4082357
DC
1611 spin_unlock(&bp->b_lock);
1612 return LRU_REMOVED;
1da177e4
LT
1613}
1614
e80dfa19
DC
1615void
1616xfs_wait_buftarg(
1617 struct xfs_buftarg *btp)
1618{
a4082357
DC
1619 LIST_HEAD(dispose);
1620 int loop = 0;
1621
85bec546 1622 /*
9c7504aa
BF
1623 * First wait on the buftarg I/O count for all in-flight buffers to be
1624 * released. This is critical as new buffers do not make the LRU until
1625 * they are released.
1626 *
1627 * Next, flush the buffer workqueue to ensure all completion processing
1628 * has finished. Just waiting on buffer locks is not sufficient for
1629 * async IO as the reference count held over IO is not released until
1630 * after the buffer lock is dropped. Hence we need to ensure here that
1631 * all reference counts have been dropped before we start walking the
1632 * LRU list.
85bec546 1633 */
9c7504aa
BF
1634 while (percpu_counter_sum(&btp->bt_io_count))
1635 delay(100);
800b2694 1636 flush_workqueue(btp->bt_mount->m_buf_workqueue);
85bec546 1637
a4082357
DC
1638 /* loop until there is nothing left on the lru list. */
1639 while (list_lru_count(&btp->bt_lru)) {
e80dfa19 1640 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
a4082357
DC
1641 &dispose, LONG_MAX);
1642
1643 while (!list_empty(&dispose)) {
1644 struct xfs_buf *bp;
1645 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1646 list_del_init(&bp->b_lru);
ac8809f9
DC
1647 if (bp->b_flags & XBF_WRITE_FAIL) {
1648 xfs_alert(btp->bt_mount,
c219b015 1649"Corruption Alert: Buffer at daddr 0x%llx had permanent write failures!",
ac8809f9 1650 (long long)bp->b_bn);
f41febd2
JP
1651 xfs_alert(btp->bt_mount,
1652"Please run xfs_repair to determine the extent of the problem.");
ac8809f9 1653 }
a4082357
DC
1654 xfs_buf_rele(bp);
1655 }
1656 if (loop++ != 0)
1657 delay(100);
1658 }
e80dfa19
DC
1659}
1660
1661static enum lru_status
1662xfs_buftarg_isolate(
1663 struct list_head *item,
3f97b163 1664 struct list_lru_one *lru,
e80dfa19
DC
1665 spinlock_t *lru_lock,
1666 void *arg)
1667{
1668 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1669 struct list_head *dispose = arg;
1670
a4082357
DC
1671 /*
1672 * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1673 * If we fail to get the lock, just skip it.
1674 */
1675 if (!spin_trylock(&bp->b_lock))
1676 return LRU_SKIP;
e80dfa19
DC
1677 /*
1678 * Decrement the b_lru_ref count unless the value is already
1679 * zero. If the value is already zero, we need to reclaim the
1680 * buffer, otherwise it gets another trip through the LRU.
1681 */
19957a18 1682 if (atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
a4082357 1683 spin_unlock(&bp->b_lock);
e80dfa19 1684 return LRU_ROTATE;
a4082357 1685 }
e80dfa19 1686
a4082357 1687 bp->b_state |= XFS_BSTATE_DISPOSE;
3f97b163 1688 list_lru_isolate_move(lru, item, dispose);
a4082357 1689 spin_unlock(&bp->b_lock);
e80dfa19
DC
1690 return LRU_REMOVED;
1691}
1692
addbda40 1693static unsigned long
e80dfa19 1694xfs_buftarg_shrink_scan(
ff57ab21 1695 struct shrinker *shrink,
1495f230 1696 struct shrink_control *sc)
a6867a68 1697{
ff57ab21
DC
1698 struct xfs_buftarg *btp = container_of(shrink,
1699 struct xfs_buftarg, bt_shrinker);
430cbeb8 1700 LIST_HEAD(dispose);
addbda40 1701 unsigned long freed;
430cbeb8 1702
503c358c
VD
1703 freed = list_lru_shrink_walk(&btp->bt_lru, sc,
1704 xfs_buftarg_isolate, &dispose);
430cbeb8
DC
1705
1706 while (!list_empty(&dispose)) {
e80dfa19 1707 struct xfs_buf *bp;
430cbeb8
DC
1708 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1709 list_del_init(&bp->b_lru);
1710 xfs_buf_rele(bp);
1711 }
1712
e80dfa19
DC
1713 return freed;
1714}
1715
addbda40 1716static unsigned long
e80dfa19
DC
1717xfs_buftarg_shrink_count(
1718 struct shrinker *shrink,
1719 struct shrink_control *sc)
1720{
1721 struct xfs_buftarg *btp = container_of(shrink,
1722 struct xfs_buftarg, bt_shrinker);
503c358c 1723 return list_lru_shrink_count(&btp->bt_lru, sc);
a6867a68
DC
1724}
1725
1da177e4
LT
1726void
1727xfs_free_buftarg(
b7963133 1728 struct xfs_buftarg *btp)
1da177e4 1729{
ff57ab21 1730 unregister_shrinker(&btp->bt_shrinker);
9c7504aa
BF
1731 ASSERT(percpu_counter_sum(&btp->bt_io_count) == 0);
1732 percpu_counter_destroy(&btp->bt_io_count);
f5e1dd34 1733 list_lru_destroy(&btp->bt_lru);
ff57ab21 1734
2291dab2 1735 xfs_blkdev_issue_flush(btp);
a6867a68 1736
f0e2d93c 1737 kmem_free(btp);
1da177e4
LT
1738}
1739
3fefdeee
ES
1740int
1741xfs_setsize_buftarg(
1da177e4 1742 xfs_buftarg_t *btp,
3fefdeee 1743 unsigned int sectorsize)
1da177e4 1744{
7c71ee78 1745 /* Set up metadata sector size info */
6da54179
ES
1746 btp->bt_meta_sectorsize = sectorsize;
1747 btp->bt_meta_sectormask = sectorsize - 1;
1da177e4 1748
ce8e922c 1749 if (set_blocksize(btp->bt_bdev, sectorsize)) {
4f10700a 1750 xfs_warn(btp->bt_mount,
a1c6f057
DM
1751 "Cannot set_blocksize to %u on device %pg",
1752 sectorsize, btp->bt_bdev);
2451337d 1753 return -EINVAL;
1da177e4
LT
1754 }
1755
7c71ee78
ES
1756 /* Set up device logical sector size mask */
1757 btp->bt_logical_sectorsize = bdev_logical_block_size(btp->bt_bdev);
1758 btp->bt_logical_sectormask = bdev_logical_block_size(btp->bt_bdev) - 1;
1759
1da177e4
LT
1760 return 0;
1761}
1762
1763/*
3fefdeee
ES
1764 * When allocating the initial buffer target we have not yet
1765 * read in the superblock, so don't know what sized sectors
1766 * are being used at this early stage. Play safe.
ce8e922c 1767 */
1da177e4
LT
1768STATIC int
1769xfs_setsize_buftarg_early(
1770 xfs_buftarg_t *btp,
1771 struct block_device *bdev)
1772{
a96c4151 1773 return xfs_setsize_buftarg(btp, bdev_logical_block_size(bdev));
1da177e4
LT
1774}
1775
1da177e4
LT
1776xfs_buftarg_t *
1777xfs_alloc_buftarg(
ebad861b 1778 struct xfs_mount *mp,
486aff5e
DW
1779 struct block_device *bdev,
1780 struct dax_device *dax_dev)
1da177e4
LT
1781{
1782 xfs_buftarg_t *btp;
1783
707e0dda 1784 btp = kmem_zalloc(sizeof(*btp), KM_NOFS);
1da177e4 1785
ebad861b 1786 btp->bt_mount = mp;
ce8e922c
NS
1787 btp->bt_dev = bdev->bd_dev;
1788 btp->bt_bdev = bdev;
486aff5e 1789 btp->bt_daxdev = dax_dev;
0e6e847f 1790
1da177e4 1791 if (xfs_setsize_buftarg_early(btp, bdev))
d210a987 1792 goto error_free;
5ca302c8
GC
1793
1794 if (list_lru_init(&btp->bt_lru))
d210a987 1795 goto error_free;
5ca302c8 1796
9c7504aa 1797 if (percpu_counter_init(&btp->bt_io_count, 0, GFP_KERNEL))
d210a987 1798 goto error_lru;
9c7504aa 1799
e80dfa19
DC
1800 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1801 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
ff57ab21 1802 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
e80dfa19 1803 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
d210a987
MH
1804 if (register_shrinker(&btp->bt_shrinker))
1805 goto error_pcpu;
1da177e4
LT
1806 return btp;
1807
d210a987
MH
1808error_pcpu:
1809 percpu_counter_destroy(&btp->bt_io_count);
1810error_lru:
1811 list_lru_destroy(&btp->bt_lru);
1812error_free:
f0e2d93c 1813 kmem_free(btp);
1da177e4
LT
1814 return NULL;
1815}
1816
20e8a063
BF
1817/*
1818 * Cancel a delayed write list.
1819 *
1820 * Remove each buffer from the list, clear the delwri queue flag and drop the
1821 * associated buffer reference.
1822 */
1823void
1824xfs_buf_delwri_cancel(
1825 struct list_head *list)
1826{
1827 struct xfs_buf *bp;
1828
1829 while (!list_empty(list)) {
1830 bp = list_first_entry(list, struct xfs_buf, b_list);
1831
1832 xfs_buf_lock(bp);
1833 bp->b_flags &= ~_XBF_DELWRI_Q;
1834 list_del_init(&bp->b_list);
1835 xfs_buf_relse(bp);
1836 }
1837}
1838
1da177e4 1839/*
43ff2122
CH
1840 * Add a buffer to the delayed write list.
1841 *
1842 * This queues a buffer for writeout if it hasn't already been. Note that
1843 * neither this routine nor the buffer list submission functions perform
1844 * any internal synchronization. It is expected that the lists are thread-local
1845 * to the callers.
1846 *
1847 * Returns true if we queued up the buffer, or false if it already had
1848 * been on the buffer list.
1da177e4 1849 */
43ff2122 1850bool
ce8e922c 1851xfs_buf_delwri_queue(
43ff2122
CH
1852 struct xfs_buf *bp,
1853 struct list_head *list)
1da177e4 1854{
43ff2122 1855 ASSERT(xfs_buf_islocked(bp));
5a8ee6ba 1856 ASSERT(!(bp->b_flags & XBF_READ));
1da177e4 1857
43ff2122
CH
1858 /*
1859 * If the buffer is already marked delwri it already is queued up
1860 * by someone else for imediate writeout. Just ignore it in that
1861 * case.
1862 */
1863 if (bp->b_flags & _XBF_DELWRI_Q) {
1864 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1865 return false;
1da177e4 1866 }
1da177e4 1867
43ff2122 1868 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
d808f617
DC
1869
1870 /*
43ff2122
CH
1871 * If a buffer gets written out synchronously or marked stale while it
1872 * is on a delwri list we lazily remove it. To do this, the other party
1873 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1874 * It remains referenced and on the list. In a rare corner case it
1875 * might get readded to a delwri list after the synchronous writeout, in
1876 * which case we need just need to re-add the flag here.
d808f617 1877 */
43ff2122
CH
1878 bp->b_flags |= _XBF_DELWRI_Q;
1879 if (list_empty(&bp->b_list)) {
1880 atomic_inc(&bp->b_hold);
1881 list_add_tail(&bp->b_list, list);
585e6d88 1882 }
585e6d88 1883
43ff2122 1884 return true;
585e6d88
DC
1885}
1886
089716aa
DC
1887/*
1888 * Compare function is more complex than it needs to be because
1889 * the return value is only 32 bits and we are doing comparisons
1890 * on 64 bit values
1891 */
1892static int
1893xfs_buf_cmp(
1894 void *priv,
1895 struct list_head *a,
1896 struct list_head *b)
1897{
1898 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1899 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1900 xfs_daddr_t diff;
1901
f4b42421 1902 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
089716aa
DC
1903 if (diff < 0)
1904 return -1;
1905 if (diff > 0)
1906 return 1;
1907 return 0;
1908}
1909
26f1fe85 1910/*
e339dd8d
BF
1911 * Submit buffers for write. If wait_list is specified, the buffers are
1912 * submitted using sync I/O and placed on the wait list such that the caller can
1913 * iowait each buffer. Otherwise async I/O is used and the buffers are released
1914 * at I/O completion time. In either case, buffers remain locked until I/O
1915 * completes and the buffer is released from the queue.
26f1fe85 1916 */
43ff2122 1917static int
26f1fe85 1918xfs_buf_delwri_submit_buffers(
43ff2122 1919 struct list_head *buffer_list,
26f1fe85 1920 struct list_head *wait_list)
1da177e4 1921{
43ff2122
CH
1922 struct xfs_buf *bp, *n;
1923 int pinned = 0;
26f1fe85 1924 struct blk_plug plug;
43ff2122 1925
26f1fe85 1926 list_sort(NULL, buffer_list, xfs_buf_cmp);
43ff2122 1927
26f1fe85 1928 blk_start_plug(&plug);
43ff2122 1929 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
26f1fe85 1930 if (!wait_list) {
43ff2122
CH
1931 if (xfs_buf_ispinned(bp)) {
1932 pinned++;
1933 continue;
1934 }
1935 if (!xfs_buf_trylock(bp))
1936 continue;
1937 } else {
1938 xfs_buf_lock(bp);
1939 }
978c7b2f 1940
43ff2122
CH
1941 /*
1942 * Someone else might have written the buffer synchronously or
1943 * marked it stale in the meantime. In that case only the
1944 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1945 * reference and remove it from the list here.
1946 */
1947 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1948 list_del_init(&bp->b_list);
1949 xfs_buf_relse(bp);
1950 continue;
1951 }
c9c12971 1952
43ff2122 1953 trace_xfs_buf_delwri_split(bp, _RET_IP_);
a1b7ea5d 1954
cf53e99d 1955 /*
e339dd8d
BF
1956 * If we have a wait list, each buffer (and associated delwri
1957 * queue reference) transfers to it and is submitted
1958 * synchronously. Otherwise, drop the buffer from the delwri
1959 * queue and submit async.
cf53e99d 1960 */
bbfeb614 1961 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_WRITE_FAIL);
e339dd8d 1962 bp->b_flags |= XBF_WRITE;
26f1fe85 1963 if (wait_list) {
e339dd8d 1964 bp->b_flags &= ~XBF_ASYNC;
26f1fe85 1965 list_move_tail(&bp->b_list, wait_list);
e339dd8d
BF
1966 } else {
1967 bp->b_flags |= XBF_ASYNC;
ce8e922c 1968 list_del_init(&bp->b_list);
e339dd8d 1969 }
6af88cda 1970 __xfs_buf_submit(bp, false);
43ff2122
CH
1971 }
1972 blk_finish_plug(&plug);
1da177e4 1973
43ff2122 1974 return pinned;
1da177e4
LT
1975}
1976
1977/*
43ff2122
CH
1978 * Write out a buffer list asynchronously.
1979 *
1980 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1981 * out and not wait for I/O completion on any of the buffers. This interface
1982 * is only safely useable for callers that can track I/O completion by higher
1983 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1984 * function.
efc3289c
BF
1985 *
1986 * Note: this function will skip buffers it would block on, and in doing so
1987 * leaves them on @buffer_list so they can be retried on a later pass. As such,
1988 * it is up to the caller to ensure that the buffer list is fully submitted or
1989 * cancelled appropriately when they are finished with the list. Failure to
1990 * cancel or resubmit the list until it is empty will result in leaked buffers
1991 * at unmount time.
1da177e4
LT
1992 */
1993int
43ff2122
CH
1994xfs_buf_delwri_submit_nowait(
1995 struct list_head *buffer_list)
1da177e4 1996{
26f1fe85 1997 return xfs_buf_delwri_submit_buffers(buffer_list, NULL);
43ff2122 1998}
1da177e4 1999
43ff2122
CH
2000/*
2001 * Write out a buffer list synchronously.
2002 *
2003 * This will take the @buffer_list, write all buffers out and wait for I/O
2004 * completion on all of the buffers. @buffer_list is consumed by the function,
2005 * so callers must have some other way of tracking buffers if they require such
2006 * functionality.
2007 */
2008int
2009xfs_buf_delwri_submit(
2010 struct list_head *buffer_list)
2011{
26f1fe85 2012 LIST_HEAD (wait_list);
43ff2122
CH
2013 int error = 0, error2;
2014 struct xfs_buf *bp;
1da177e4 2015
26f1fe85 2016 xfs_buf_delwri_submit_buffers(buffer_list, &wait_list);
1da177e4 2017
43ff2122 2018 /* Wait for IO to complete. */
26f1fe85
DC
2019 while (!list_empty(&wait_list)) {
2020 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
a1b7ea5d 2021
089716aa 2022 list_del_init(&bp->b_list);
cf53e99d 2023
e339dd8d
BF
2024 /*
2025 * Wait on the locked buffer, check for errors and unlock and
2026 * release the delwri queue reference.
2027 */
2028 error2 = xfs_buf_iowait(bp);
43ff2122
CH
2029 xfs_buf_relse(bp);
2030 if (!error)
2031 error = error2;
1da177e4
LT
2032 }
2033
43ff2122 2034 return error;
1da177e4
LT
2035}
2036
7912e7fe
BF
2037/*
2038 * Push a single buffer on a delwri queue.
2039 *
2040 * The purpose of this function is to submit a single buffer of a delwri queue
2041 * and return with the buffer still on the original queue. The waiting delwri
2042 * buffer submission infrastructure guarantees transfer of the delwri queue
2043 * buffer reference to a temporary wait list. We reuse this infrastructure to
2044 * transfer the buffer back to the original queue.
2045 *
2046 * Note the buffer transitions from the queued state, to the submitted and wait
2047 * listed state and back to the queued state during this call. The buffer
2048 * locking and queue management logic between _delwri_pushbuf() and
2049 * _delwri_queue() guarantee that the buffer cannot be queued to another list
2050 * before returning.
2051 */
2052int
2053xfs_buf_delwri_pushbuf(
2054 struct xfs_buf *bp,
2055 struct list_head *buffer_list)
2056{
2057 LIST_HEAD (submit_list);
2058 int error;
2059
2060 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
2061
2062 trace_xfs_buf_delwri_pushbuf(bp, _RET_IP_);
2063
2064 /*
2065 * Isolate the buffer to a new local list so we can submit it for I/O
2066 * independently from the rest of the original list.
2067 */
2068 xfs_buf_lock(bp);
2069 list_move(&bp->b_list, &submit_list);
2070 xfs_buf_unlock(bp);
2071
2072 /*
2073 * Delwri submission clears the DELWRI_Q buffer flag and returns with
e339dd8d 2074 * the buffer on the wait list with the original reference. Rather than
7912e7fe
BF
2075 * bounce the buffer from a local wait list back to the original list
2076 * after I/O completion, reuse the original list as the wait list.
2077 */
2078 xfs_buf_delwri_submit_buffers(&submit_list, buffer_list);
2079
2080 /*
e339dd8d
BF
2081 * The buffer is now locked, under I/O and wait listed on the original
2082 * delwri queue. Wait for I/O completion, restore the DELWRI_Q flag and
2083 * return with the buffer unlocked and on the original queue.
7912e7fe 2084 */
e339dd8d 2085 error = xfs_buf_iowait(bp);
7912e7fe
BF
2086 bp->b_flags |= _XBF_DELWRI_Q;
2087 xfs_buf_unlock(bp);
2088
2089 return error;
2090}
2091
04d8b284 2092int __init
ce8e922c 2093xfs_buf_init(void)
1da177e4 2094{
b1231760
CM
2095 xfs_buf_zone = kmem_cache_create("xfs_buf",
2096 sizeof(struct xfs_buf), 0,
2097 SLAB_HWCACHE_ALIGN, NULL);
ce8e922c 2098 if (!xfs_buf_zone)
0b1b213f 2099 goto out;
04d8b284 2100
23ea4032 2101 return 0;
1da177e4 2102
0b1b213f 2103 out:
8758280f 2104 return -ENOMEM;
1da177e4
LT
2105}
2106
1da177e4 2107void
ce8e922c 2108xfs_buf_terminate(void)
1da177e4 2109{
aaf54eb8 2110 kmem_cache_destroy(xfs_buf_zone);
1da177e4 2111}
7561d27e
BF
2112
2113void xfs_buf_set_ref(struct xfs_buf *bp, int lru_ref)
2114{
7561d27e
BF
2115 /*
2116 * Set the lru reference count to 0 based on the error injection tag.
2117 * This allows userspace to disrupt buffer caching for debug/testing
2118 * purposes.
2119 */
dbd329f1 2120 if (XFS_TEST_ERROR(false, bp->b_mount, XFS_ERRTAG_BUF_LRU_REF))
7561d27e
BF
2121 lru_ref = 0;
2122
2123 atomic_set(&bp->b_lru_ref, lru_ref);
2124}
8473fee3
BF
2125
2126/*
2127 * Verify an on-disk magic value against the magic value specified in the
2128 * verifier structure. The verifier magic is in disk byte order so the caller is
2129 * expected to pass the value directly from disk.
2130 */
2131bool
2132xfs_verify_magic(
2133 struct xfs_buf *bp,
15baadf7 2134 __be32 dmagic)
8473fee3 2135{
dbd329f1 2136 struct xfs_mount *mp = bp->b_mount;
8473fee3
BF
2137 int idx;
2138
2139 idx = xfs_sb_version_hascrc(&mp->m_sb);
14ed8688 2140 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic[idx]))
8473fee3
BF
2141 return false;
2142 return dmagic == bp->b_ops->magic[idx];
2143}
15baadf7
DW
2144/*
2145 * Verify an on-disk magic value against the magic value specified in the
2146 * verifier structure. The verifier magic is in disk byte order so the caller is
2147 * expected to pass the value directly from disk.
2148 */
2149bool
2150xfs_verify_magic16(
2151 struct xfs_buf *bp,
2152 __be16 dmagic)
2153{
dbd329f1 2154 struct xfs_mount *mp = bp->b_mount;
15baadf7
DW
2155 int idx;
2156
2157 idx = xfs_sb_version_hascrc(&mp->m_sb);
14ed8688 2158 if (WARN_ON(!bp->b_ops || !bp->b_ops->magic16[idx]))
15baadf7
DW
2159 return false;
2160 return dmagic == bp->b_ops->magic16[idx];
2161}