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