~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/xfs/xfs_buf.c

Version: ~ [ linux-5.8-rc5 ] ~ [ linux-5.7.8 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.51 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.132 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.188 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.230 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.230 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 37 #include "xfs_sb.h"
 38 #include "xfs_trans_resv.h"
 39 #include "xfs_log.h"
 40 #include "xfs_ag.h"
 41 #include "xfs_mount.h"
 42 #include "xfs_trace.h"
 43 
 44 static kmem_zone_t *xfs_buf_zone;
 45 
 46 static struct workqueue_struct *xfslogd_workqueue;
 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 static inline int
 63 xfs_buf_is_vmapped(
 64         struct xfs_buf  *bp)
 65 {
 66         /*
 67          * Return true if the buffer is vmapped.
 68          *
 69          * b_addr is null if the buffer is not mapped, but the code is clever
 70          * enough to know it doesn't have to map a single page, so the check has
 71          * to be both for b_addr and bp->b_page_count > 1.
 72          */
 73         return bp->b_addr && bp->b_page_count > 1;
 74 }
 75 
 76 static inline int
 77 xfs_buf_vmap_len(
 78         struct xfs_buf  *bp)
 79 {
 80         return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
 81 }
 82 
 83 /*
 84  * When we mark a buffer stale, we remove the buffer from the LRU and clear the
 85  * b_lru_ref count so that the buffer is freed immediately when the buffer
 86  * reference count falls to zero. If the buffer is already on the LRU, we need
 87  * to remove the reference that LRU holds on the buffer.
 88  *
 89  * This prevents build-up of stale buffers on the LRU.
 90  */
 91 void
 92 xfs_buf_stale(
 93         struct xfs_buf  *bp)
 94 {
 95         ASSERT(xfs_buf_islocked(bp));
 96 
 97         bp->b_flags |= XBF_STALE;
 98 
 99         /*
100          * Clear the delwri status so that a delwri queue walker will not
101          * flush this buffer to disk now that it is stale. The delwri queue has
102          * a reference to the buffer, so this is safe to do.
103          */
104         bp->b_flags &= ~_XBF_DELWRI_Q;
105 
106         spin_lock(&bp->b_lock);
107         atomic_set(&bp->b_lru_ref, 0);
108         if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
109             (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
110                 atomic_dec(&bp->b_hold);
111 
112         ASSERT(atomic_read(&bp->b_hold) >= 1);
113         spin_unlock(&bp->b_lock);
114 }
115 
116 static int
117 xfs_buf_get_maps(
118         struct xfs_buf          *bp,
119         int                     map_count)
120 {
121         ASSERT(bp->b_maps == NULL);
122         bp->b_map_count = map_count;
123 
124         if (map_count == 1) {
125                 bp->b_maps = &bp->__b_map;
126                 return 0;
127         }
128 
129         bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
130                                 KM_NOFS);
131         if (!bp->b_maps)
132                 return ENOMEM;
133         return 0;
134 }
135 
136 /*
137  *      Frees b_pages if it was allocated.
138  */
139 static void
140 xfs_buf_free_maps(
141         struct xfs_buf  *bp)
142 {
143         if (bp->b_maps != &bp->__b_map) {
144                 kmem_free(bp->b_maps);
145                 bp->b_maps = NULL;
146         }
147 }
148 
149 struct xfs_buf *
150 _xfs_buf_alloc(
151         struct xfs_buftarg      *target,
152         struct xfs_buf_map      *map,
153         int                     nmaps,
154         xfs_buf_flags_t         flags)
155 {
156         struct xfs_buf          *bp;
157         int                     error;
158         int                     i;
159 
160         bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
161         if (unlikely(!bp))
162                 return NULL;
163 
164         /*
165          * We don't want certain flags to appear in b_flags unless they are
166          * specifically set by later operations on the buffer.
167          */
168         flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
169 
170         atomic_set(&bp->b_hold, 1);
171         atomic_set(&bp->b_lru_ref, 1);
172         init_completion(&bp->b_iowait);
173         INIT_LIST_HEAD(&bp->b_lru);
174         INIT_LIST_HEAD(&bp->b_list);
175         RB_CLEAR_NODE(&bp->b_rbnode);
176         sema_init(&bp->b_sema, 0); /* held, no waiters */
177         spin_lock_init(&bp->b_lock);
178         XB_SET_OWNER(bp);
179         bp->b_target = target;
180         bp->b_flags = flags;
181 
182         /*
183          * Set length and io_length to the same value initially.
184          * I/O routines should use io_length, which will be the same in
185          * most cases but may be reset (e.g. XFS recovery).
186          */
187         error = xfs_buf_get_maps(bp, nmaps);
188         if (error)  {
189                 kmem_zone_free(xfs_buf_zone, bp);
190                 return NULL;
191         }
192 
193         bp->b_bn = map[0].bm_bn;
194         bp->b_length = 0;
195         for (i = 0; i < nmaps; i++) {
196                 bp->b_maps[i].bm_bn = map[i].bm_bn;
197                 bp->b_maps[i].bm_len = map[i].bm_len;
198                 bp->b_length += map[i].bm_len;
199         }
200         bp->b_io_length = bp->b_length;
201 
202         atomic_set(&bp->b_pin_count, 0);
203         init_waitqueue_head(&bp->b_waiters);
204 
205         XFS_STATS_INC(xb_create);
206         trace_xfs_buf_init(bp, _RET_IP_);
207 
208         return bp;
209 }
210 
211 /*
212  *      Allocate a page array capable of holding a specified number
213  *      of pages, and point the page buf at it.
214  */
215 STATIC int
216 _xfs_buf_get_pages(
217         xfs_buf_t               *bp,
218         int                     page_count,
219         xfs_buf_flags_t         flags)
220 {
221         /* Make sure that we have a page list */
222         if (bp->b_pages == NULL) {
223                 bp->b_page_count = page_count;
224                 if (page_count <= XB_PAGES) {
225                         bp->b_pages = bp->b_page_array;
226                 } else {
227                         bp->b_pages = kmem_alloc(sizeof(struct page *) *
228                                                  page_count, KM_NOFS);
229                         if (bp->b_pages == NULL)
230                                 return -ENOMEM;
231                 }
232                 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
233         }
234         return 0;
235 }
236 
237 /*
238  *      Frees b_pages if it was allocated.
239  */
240 STATIC void
241 _xfs_buf_free_pages(
242         xfs_buf_t       *bp)
243 {
244         if (bp->b_pages != bp->b_page_array) {
245                 kmem_free(bp->b_pages);
246                 bp->b_pages = NULL;
247         }
248 }
249 
250 /*
251  *      Releases the specified buffer.
252  *
253  *      The modification state of any associated pages is left unchanged.
254  *      The buffer must not be on any hash - use xfs_buf_rele instead for
255  *      hashed and refcounted buffers
256  */
257 void
258 xfs_buf_free(
259         xfs_buf_t               *bp)
260 {
261         trace_xfs_buf_free(bp, _RET_IP_);
262 
263         ASSERT(list_empty(&bp->b_lru));
264 
265         if (bp->b_flags & _XBF_PAGES) {
266                 uint            i;
267 
268                 if (xfs_buf_is_vmapped(bp))
269                         vm_unmap_ram(bp->b_addr - bp->b_offset,
270                                         bp->b_page_count);
271 
272                 for (i = 0; i < bp->b_page_count; i++) {
273                         struct page     *page = bp->b_pages[i];
274 
275                         __free_page(page);
276                 }
277         } else if (bp->b_flags & _XBF_KMEM)
278                 kmem_free(bp->b_addr);
279         _xfs_buf_free_pages(bp);
280         xfs_buf_free_maps(bp);
281         kmem_zone_free(xfs_buf_zone, bp);
282 }
283 
284 /*
285  * Allocates all the pages for buffer in question and builds it's page list.
286  */
287 STATIC int
288 xfs_buf_allocate_memory(
289         xfs_buf_t               *bp,
290         uint                    flags)
291 {
292         size_t                  size;
293         size_t                  nbytes, offset;
294         gfp_t                   gfp_mask = xb_to_gfp(flags);
295         unsigned short          page_count, i;
296         xfs_off_t               start, end;
297         int                     error;
298 
299         /*
300          * for buffers that are contained within a single page, just allocate
301          * the memory from the heap - there's no need for the complexity of
302          * page arrays to keep allocation down to order 0.
303          */
304         size = BBTOB(bp->b_length);
305         if (size < PAGE_SIZE) {
306                 bp->b_addr = kmem_alloc(size, KM_NOFS);
307                 if (!bp->b_addr) {
308                         /* low memory - use alloc_page loop instead */
309                         goto use_alloc_page;
310                 }
311 
312                 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
313                     ((unsigned long)bp->b_addr & PAGE_MASK)) {
314                         /* b_addr spans two pages - use alloc_page instead */
315                         kmem_free(bp->b_addr);
316                         bp->b_addr = NULL;
317                         goto use_alloc_page;
318                 }
319                 bp->b_offset = offset_in_page(bp->b_addr);
320                 bp->b_pages = bp->b_page_array;
321                 bp->b_pages[0] = virt_to_page(bp->b_addr);
322                 bp->b_page_count = 1;
323                 bp->b_flags |= _XBF_KMEM;
324                 return 0;
325         }
326 
327 use_alloc_page:
328         start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
329         end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
330                                                                 >> PAGE_SHIFT;
331         page_count = end - start;
332         error = _xfs_buf_get_pages(bp, page_count, flags);
333         if (unlikely(error))
334                 return error;
335 
336         offset = bp->b_offset;
337         bp->b_flags |= _XBF_PAGES;
338 
339         for (i = 0; i < bp->b_page_count; i++) {
340                 struct page     *page;
341                 uint            retries = 0;
342 retry:
343                 page = alloc_page(gfp_mask);
344                 if (unlikely(page == NULL)) {
345                         if (flags & XBF_READ_AHEAD) {
346                                 bp->b_page_count = i;
347                                 error = ENOMEM;
348                                 goto out_free_pages;
349                         }
350 
351                         /*
352                          * This could deadlock.
353                          *
354                          * But until all the XFS lowlevel code is revamped to
355                          * handle buffer allocation failures we can't do much.
356                          */
357                         if (!(++retries % 100))
358                                 xfs_err(NULL,
359                 "possible memory allocation deadlock in %s (mode:0x%x)",
360                                         __func__, gfp_mask);
361 
362                         XFS_STATS_INC(xb_page_retries);
363                         congestion_wait(BLK_RW_ASYNC, HZ/50);
364                         goto retry;
365                 }
366 
367                 XFS_STATS_INC(xb_page_found);
368 
369                 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
370                 size -= nbytes;
371                 bp->b_pages[i] = page;
372                 offset = 0;
373         }
374         return 0;
375 
376 out_free_pages:
377         for (i = 0; i < bp->b_page_count; i++)
378                 __free_page(bp->b_pages[i]);
379         bp->b_flags &= ~_XBF_PAGES;
380         return error;
381 }
382 
383 /*
384  *      Map buffer into kernel address-space if necessary.
385  */
386 STATIC int
387 _xfs_buf_map_pages(
388         xfs_buf_t               *bp,
389         uint                    flags)
390 {
391         ASSERT(bp->b_flags & _XBF_PAGES);
392         if (bp->b_page_count == 1) {
393                 /* A single page buffer is always mappable */
394                 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
395         } else if (flags & XBF_UNMAPPED) {
396                 bp->b_addr = NULL;
397         } else {
398                 int retried = 0;
399 
400                 do {
401                         bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
402                                                 -1, PAGE_KERNEL);
403                         if (bp->b_addr)
404                                 break;
405                         vm_unmap_aliases();
406                 } while (retried++ <= 1);
407 
408                 if (!bp->b_addr)
409                         return -ENOMEM;
410                 bp->b_addr += bp->b_offset;
411         }
412 
413         return 0;
414 }
415 
416 /*
417  *      Finding and Reading Buffers
418  */
419 
420 /*
421  *      Look up, and creates if absent, a lockable buffer for
422  *      a given range of an inode.  The buffer is returned
423  *      locked. No I/O is implied by this call.
424  */
425 xfs_buf_t *
426 _xfs_buf_find(
427         struct xfs_buftarg      *btp,
428         struct xfs_buf_map      *map,
429         int                     nmaps,
430         xfs_buf_flags_t         flags,
431         xfs_buf_t               *new_bp)
432 {
433         size_t                  numbytes;
434         struct xfs_perag        *pag;
435         struct rb_node          **rbp;
436         struct rb_node          *parent;
437         xfs_buf_t               *bp;
438         xfs_daddr_t             blkno = map[0].bm_bn;
439         xfs_daddr_t             eofs;
440         int                     numblks = 0;
441         int                     i;
442 
443         for (i = 0; i < nmaps; i++)
444                 numblks += map[i].bm_len;
445         numbytes = BBTOB(numblks);
446 
447         /* Check for IOs smaller than the sector size / not sector aligned */
448         ASSERT(!(numbytes < (1 << btp->bt_sshift)));
449         ASSERT(!(BBTOB(blkno) & (xfs_off_t)btp->bt_smask));
450 
451         /*
452          * Corrupted block numbers can get through to here, unfortunately, so we
453          * have to check that the buffer falls within the filesystem bounds.
454          */
455         eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
456         if (blkno >= eofs) {
457                 /*
458                  * XXX (dgc): we should really be returning EFSCORRUPTED here,
459                  * but none of the higher level infrastructure supports
460                  * returning a specific error on buffer lookup failures.
461                  */
462                 xfs_alert(btp->bt_mount,
463                           "%s: Block out of range: block 0x%llx, EOFS 0x%llx ",
464                           __func__, blkno, eofs);
465                 WARN_ON(1);
466                 return NULL;
467         }
468 
469         /* get tree root */
470         pag = xfs_perag_get(btp->bt_mount,
471                                 xfs_daddr_to_agno(btp->bt_mount, blkno));
472 
473         /* walk tree */
474         spin_lock(&pag->pag_buf_lock);
475         rbp = &pag->pag_buf_tree.rb_node;
476         parent = NULL;
477         bp = NULL;
478         while (*rbp) {
479                 parent = *rbp;
480                 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
481 
482                 if (blkno < bp->b_bn)
483                         rbp = &(*rbp)->rb_left;
484                 else if (blkno > bp->b_bn)
485                         rbp = &(*rbp)->rb_right;
486                 else {
487                         /*
488                          * found a block number match. If the range doesn't
489                          * match, the only way this is allowed is if the buffer
490                          * in the cache is stale and the transaction that made
491                          * it stale has not yet committed. i.e. we are
492                          * reallocating a busy extent. Skip this buffer and
493                          * continue searching to the right for an exact match.
494                          */
495                         if (bp->b_length != numblks) {
496                                 ASSERT(bp->b_flags & XBF_STALE);
497                                 rbp = &(*rbp)->rb_right;
498                                 continue;
499                         }
500                         atomic_inc(&bp->b_hold);
501                         goto found;
502                 }
503         }
504 
505         /* No match found */
506         if (new_bp) {
507                 rb_link_node(&new_bp->b_rbnode, parent, rbp);
508                 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
509                 /* the buffer keeps the perag reference until it is freed */
510                 new_bp->b_pag = pag;
511                 spin_unlock(&pag->pag_buf_lock);
512         } else {
513                 XFS_STATS_INC(xb_miss_locked);
514                 spin_unlock(&pag->pag_buf_lock);
515                 xfs_perag_put(pag);
516         }
517         return new_bp;
518 
519 found:
520         spin_unlock(&pag->pag_buf_lock);
521         xfs_perag_put(pag);
522 
523         if (!xfs_buf_trylock(bp)) {
524                 if (flags & XBF_TRYLOCK) {
525                         xfs_buf_rele(bp);
526                         XFS_STATS_INC(xb_busy_locked);
527                         return NULL;
528                 }
529                 xfs_buf_lock(bp);
530                 XFS_STATS_INC(xb_get_locked_waited);
531         }
532 
533         /*
534          * if the buffer is stale, clear all the external state associated with
535          * it. We need to keep flags such as how we allocated the buffer memory
536          * intact here.
537          */
538         if (bp->b_flags & XBF_STALE) {
539                 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
540                 ASSERT(bp->b_iodone == NULL);
541                 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
542                 bp->b_ops = NULL;
543         }
544 
545         trace_xfs_buf_find(bp, flags, _RET_IP_);
546         XFS_STATS_INC(xb_get_locked);
547         return bp;
548 }
549 
550 /*
551  * Assembles a buffer covering the specified range. The code is optimised for
552  * cache hits, as metadata intensive workloads will see 3 orders of magnitude
553  * more hits than misses.
554  */
555 struct xfs_buf *
556 xfs_buf_get_map(
557         struct xfs_buftarg      *target,
558         struct xfs_buf_map      *map,
559         int                     nmaps,
560         xfs_buf_flags_t         flags)
561 {
562         struct xfs_buf          *bp;
563         struct xfs_buf          *new_bp;
564         int                     error = 0;
565 
566         bp = _xfs_buf_find(target, map, nmaps, flags, NULL);
567         if (likely(bp))
568                 goto found;
569 
570         new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
571         if (unlikely(!new_bp))
572                 return NULL;
573 
574         error = xfs_buf_allocate_memory(new_bp, flags);
575         if (error) {
576                 xfs_buf_free(new_bp);
577                 return NULL;
578         }
579 
580         bp = _xfs_buf_find(target, map, nmaps, flags, new_bp);
581         if (!bp) {
582                 xfs_buf_free(new_bp);
583                 return NULL;
584         }
585 
586         if (bp != new_bp)
587                 xfs_buf_free(new_bp);
588 
589 found:
590         if (!bp->b_addr) {
591                 error = _xfs_buf_map_pages(bp, flags);
592                 if (unlikely(error)) {
593                         xfs_warn(target->bt_mount,
594                                 "%s: failed to map pages\n", __func__);
595                         xfs_buf_relse(bp);
596                         return NULL;
597                 }
598         }
599 
600         /*
601          * Clear b_error if this is a lookup from a caller that doesn't expect
602          * valid data to be found in the buffer.
603          */
604         if (!(flags & XBF_READ))
605                 xfs_buf_ioerror(bp, 0);
606 
607         XFS_STATS_INC(xb_get);
608         trace_xfs_buf_get(bp, flags, _RET_IP_);
609         return bp;
610 }
611 
612 STATIC int
613 _xfs_buf_read(
614         xfs_buf_t               *bp,
615         xfs_buf_flags_t         flags)
616 {
617         ASSERT(!(flags & XBF_WRITE));
618         ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
619 
620         bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
621         bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
622 
623         xfs_buf_iorequest(bp);
624         if (flags & XBF_ASYNC)
625                 return 0;
626         return xfs_buf_iowait(bp);
627 }
628 
629 xfs_buf_t *
630 xfs_buf_read_map(
631         struct xfs_buftarg      *target,
632         struct xfs_buf_map      *map,
633         int                     nmaps,
634         xfs_buf_flags_t         flags,
635         const struct xfs_buf_ops *ops)
636 {
637         struct xfs_buf          *bp;
638 
639         flags |= XBF_READ;
640 
641         bp = xfs_buf_get_map(target, map, nmaps, flags);
642         if (bp) {
643                 trace_xfs_buf_read(bp, flags, _RET_IP_);
644 
645                 if (!XFS_BUF_ISDONE(bp)) {
646                         XFS_STATS_INC(xb_get_read);
647                         bp->b_ops = ops;
648                         _xfs_buf_read(bp, flags);
649                 } else if (flags & XBF_ASYNC) {
650                         /*
651                          * Read ahead call which is already satisfied,
652                          * drop the buffer
653                          */
654                         xfs_buf_relse(bp);
655                         return NULL;
656                 } else {
657                         /* We do not want read in the flags */
658                         bp->b_flags &= ~XBF_READ;
659                 }
660         }
661 
662         return bp;
663 }
664 
665 /*
666  *      If we are not low on memory then do the readahead in a deadlock
667  *      safe manner.
668  */
669 void
670 xfs_buf_readahead_map(
671         struct xfs_buftarg      *target,
672         struct xfs_buf_map      *map,
673         int                     nmaps,
674         const struct xfs_buf_ops *ops)
675 {
676         if (bdi_read_congested(target->bt_bdi))
677                 return;
678 
679         xfs_buf_read_map(target, map, nmaps,
680                      XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
681 }
682 
683 /*
684  * Read an uncached buffer from disk. Allocates and returns a locked
685  * buffer containing the disk contents or nothing.
686  */
687 struct xfs_buf *
688 xfs_buf_read_uncached(
689         struct xfs_buftarg      *target,
690         xfs_daddr_t             daddr,
691         size_t                  numblks,
692         int                     flags,
693         const struct xfs_buf_ops *ops)
694 {
695         struct xfs_buf          *bp;
696 
697         bp = xfs_buf_get_uncached(target, numblks, flags);
698         if (!bp)
699                 return NULL;
700 
701         /* set up the buffer for a read IO */
702         ASSERT(bp->b_map_count == 1);
703         bp->b_bn = daddr;
704         bp->b_maps[0].bm_bn = daddr;
705         bp->b_flags |= XBF_READ;
706         bp->b_ops = ops;
707 
708         xfsbdstrat(target->bt_mount, bp);
709         xfs_buf_iowait(bp);
710         return bp;
711 }
712 
713 /*
714  * Return a buffer allocated as an empty buffer and associated to external
715  * memory via xfs_buf_associate_memory() back to it's empty state.
716  */
717 void
718 xfs_buf_set_empty(
719         struct xfs_buf          *bp,
720         size_t                  numblks)
721 {
722         if (bp->b_pages)
723                 _xfs_buf_free_pages(bp);
724 
725         bp->b_pages = NULL;
726         bp->b_page_count = 0;
727         bp->b_addr = NULL;
728         bp->b_length = numblks;
729         bp->b_io_length = numblks;
730 
731         ASSERT(bp->b_map_count == 1);
732         bp->b_bn = XFS_BUF_DADDR_NULL;
733         bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL;
734         bp->b_maps[0].bm_len = bp->b_length;
735 }
736 
737 static inline struct page *
738 mem_to_page(
739         void                    *addr)
740 {
741         if ((!is_vmalloc_addr(addr))) {
742                 return virt_to_page(addr);
743         } else {
744                 return vmalloc_to_page(addr);
745         }
746 }
747 
748 int
749 xfs_buf_associate_memory(
750         xfs_buf_t               *bp,
751         void                    *mem,
752         size_t                  len)
753 {
754         int                     rval;
755         int                     i = 0;
756         unsigned long           pageaddr;
757         unsigned long           offset;
758         size_t                  buflen;
759         int                     page_count;
760 
761         pageaddr = (unsigned long)mem & PAGE_MASK;
762         offset = (unsigned long)mem - pageaddr;
763         buflen = PAGE_ALIGN(len + offset);
764         page_count = buflen >> PAGE_SHIFT;
765 
766         /* Free any previous set of page pointers */
767         if (bp->b_pages)
768                 _xfs_buf_free_pages(bp);
769 
770         bp->b_pages = NULL;
771         bp->b_addr = mem;
772 
773         rval = _xfs_buf_get_pages(bp, page_count, 0);
774         if (rval)
775                 return rval;
776 
777         bp->b_offset = offset;
778 
779         for (i = 0; i < bp->b_page_count; i++) {
780                 bp->b_pages[i] = mem_to_page((void *)pageaddr);
781                 pageaddr += PAGE_SIZE;
782         }
783 
784         bp->b_io_length = BTOBB(len);
785         bp->b_length = BTOBB(buflen);
786 
787         return 0;
788 }
789 
790 xfs_buf_t *
791 xfs_buf_get_uncached(
792         struct xfs_buftarg      *target,
793         size_t                  numblks,
794         int                     flags)
795 {
796         unsigned long           page_count;
797         int                     error, i;
798         struct xfs_buf          *bp;
799         DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
800 
801         bp = _xfs_buf_alloc(target, &map, 1, 0);
802         if (unlikely(bp == NULL))
803                 goto fail;
804 
805         page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
806         error = _xfs_buf_get_pages(bp, page_count, 0);
807         if (error)
808                 goto fail_free_buf;
809 
810         for (i = 0; i < page_count; i++) {
811                 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
812                 if (!bp->b_pages[i])
813                         goto fail_free_mem;
814         }
815         bp->b_flags |= _XBF_PAGES;
816 
817         error = _xfs_buf_map_pages(bp, 0);
818         if (unlikely(error)) {
819                 xfs_warn(target->bt_mount,
820                         "%s: failed to map pages\n", __func__);
821                 goto fail_free_mem;
822         }
823 
824         trace_xfs_buf_get_uncached(bp, _RET_IP_);
825         return bp;
826 
827  fail_free_mem:
828         while (--i >= 0)
829                 __free_page(bp->b_pages[i]);
830         _xfs_buf_free_pages(bp);
831  fail_free_buf:
832         xfs_buf_free_maps(bp);
833         kmem_zone_free(xfs_buf_zone, bp);
834  fail:
835         return NULL;
836 }
837 
838 /*
839  *      Increment reference count on buffer, to hold the buffer concurrently
840  *      with another thread which may release (free) the buffer asynchronously.
841  *      Must hold the buffer already to call this function.
842  */
843 void
844 xfs_buf_hold(
845         xfs_buf_t               *bp)
846 {
847         trace_xfs_buf_hold(bp, _RET_IP_);
848         atomic_inc(&bp->b_hold);
849 }
850 
851 /*
852  *      Releases a hold on the specified buffer.  If the
853  *      the hold count is 1, calls xfs_buf_free.
854  */
855 void
856 xfs_buf_rele(
857         xfs_buf_t               *bp)
858 {
859         struct xfs_perag        *pag = bp->b_pag;
860 
861         trace_xfs_buf_rele(bp, _RET_IP_);
862 
863         if (!pag) {
864                 ASSERT(list_empty(&bp->b_lru));
865                 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
866                 if (atomic_dec_and_test(&bp->b_hold))
867                         xfs_buf_free(bp);
868                 return;
869         }
870 
871         ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
872 
873         ASSERT(atomic_read(&bp->b_hold) > 0);
874         if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
875                 spin_lock(&bp->b_lock);
876                 if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
877                         /*
878                          * If the buffer is added to the LRU take a new
879                          * reference to the buffer for the LRU and clear the
880                          * (now stale) dispose list state flag
881                          */
882                         if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
883                                 bp->b_state &= ~XFS_BSTATE_DISPOSE;
884                                 atomic_inc(&bp->b_hold);
885                         }
886                         spin_unlock(&bp->b_lock);
887                         spin_unlock(&pag->pag_buf_lock);
888                 } else {
889                         /*
890                          * most of the time buffers will already be removed from
891                          * the LRU, so optimise that case by checking for the
892                          * XFS_BSTATE_DISPOSE flag indicating the last list the
893                          * buffer was on was the disposal list
894                          */
895                         if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
896                                 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
897                         } else {
898                                 ASSERT(list_empty(&bp->b_lru));
899                         }
900                         spin_unlock(&bp->b_lock);
901 
902                         ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
903                         rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
904                         spin_unlock(&pag->pag_buf_lock);
905                         xfs_perag_put(pag);
906                         xfs_buf_free(bp);
907                 }
908         }
909 }
910 
911 
912 /*
913  *      Lock a buffer object, if it is not already locked.
914  *
915  *      If we come across a stale, pinned, locked buffer, we know that we are
916  *      being asked to lock a buffer that has been reallocated. Because it is
917  *      pinned, we know that the log has not been pushed to disk and hence it
918  *      will still be locked.  Rather than continuing to have trylock attempts
919  *      fail until someone else pushes the log, push it ourselves before
920  *      returning.  This means that the xfsaild will not get stuck trying
921  *      to push on stale inode buffers.
922  */
923 int
924 xfs_buf_trylock(
925         struct xfs_buf          *bp)
926 {
927         int                     locked;
928 
929         locked = down_trylock(&bp->b_sema) == 0;
930         if (locked)
931                 XB_SET_OWNER(bp);
932 
933         trace_xfs_buf_trylock(bp, _RET_IP_);
934         return locked;
935 }
936 
937 /*
938  *      Lock a buffer object.
939  *
940  *      If we come across a stale, pinned, locked buffer, we know that we
941  *      are being asked to lock a buffer that has been reallocated. Because
942  *      it is pinned, we know that the log has not been pushed to disk and
943  *      hence it will still be locked. Rather than sleeping until someone
944  *      else pushes the log, push it ourselves before trying to get the lock.
945  */
946 void
947 xfs_buf_lock(
948         struct xfs_buf          *bp)
949 {
950         trace_xfs_buf_lock(bp, _RET_IP_);
951 
952         if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
953                 xfs_log_force(bp->b_target->bt_mount, 0);
954         down(&bp->b_sema);
955         XB_SET_OWNER(bp);
956 
957         trace_xfs_buf_lock_done(bp, _RET_IP_);
958 }
959 
960 void
961 xfs_buf_unlock(
962         struct xfs_buf          *bp)
963 {
964         XB_CLEAR_OWNER(bp);
965         up(&bp->b_sema);
966 
967         trace_xfs_buf_unlock(bp, _RET_IP_);
968 }
969 
970 STATIC void
971 xfs_buf_wait_unpin(
972         xfs_buf_t               *bp)
973 {
974         DECLARE_WAITQUEUE       (wait, current);
975 
976         if (atomic_read(&bp->b_pin_count) == 0)
977                 return;
978 
979         add_wait_queue(&bp->b_waiters, &wait);
980         for (;;) {
981                 set_current_state(TASK_UNINTERRUPTIBLE);
982                 if (atomic_read(&bp->b_pin_count) == 0)
983                         break;
984                 io_schedule();
985         }
986         remove_wait_queue(&bp->b_waiters, &wait);
987         set_current_state(TASK_RUNNING);
988 }
989 
990 /*
991  *      Buffer Utility Routines
992  */
993 
994 STATIC void
995 xfs_buf_iodone_work(
996         struct work_struct      *work)
997 {
998         struct xfs_buf          *bp =
999                 container_of(work, xfs_buf_t, b_iodone_work);
1000         bool                    read = !!(bp->b_flags & XBF_READ);
1001 
1002         bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
1003 
1004         /* only validate buffers that were read without errors */
1005         if (read && bp->b_ops && !bp->b_error && (bp->b_flags & XBF_DONE))
1006                 bp->b_ops->verify_read(bp);
1007 
1008         if (bp->b_iodone)
1009                 (*(bp->b_iodone))(bp);
1010         else if (bp->b_flags & XBF_ASYNC)
1011                 xfs_buf_relse(bp);
1012         else {
1013                 ASSERT(read && bp->b_ops);
1014                 complete(&bp->b_iowait);
1015         }
1016 }
1017 
1018 void
1019 xfs_buf_ioend(
1020         struct xfs_buf  *bp,
1021         int             schedule)
1022 {
1023         bool            read = !!(bp->b_flags & XBF_READ);
1024 
1025         trace_xfs_buf_iodone(bp, _RET_IP_);
1026 
1027         if (bp->b_error == 0)
1028                 bp->b_flags |= XBF_DONE;
1029 
1030         if (bp->b_iodone || (read && bp->b_ops) || (bp->b_flags & XBF_ASYNC)) {
1031                 if (schedule) {
1032                         INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
1033                         queue_work(xfslogd_workqueue, &bp->b_iodone_work);
1034                 } else {
1035                         xfs_buf_iodone_work(&bp->b_iodone_work);
1036                 }
1037         } else {
1038                 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
1039                 complete(&bp->b_iowait);
1040         }
1041 }
1042 
1043 void
1044 xfs_buf_ioerror(
1045         xfs_buf_t               *bp,
1046         int                     error)
1047 {
1048         ASSERT(error >= 0 && error <= 0xffff);
1049         bp->b_error = (unsigned short)error;
1050         trace_xfs_buf_ioerror(bp, error, _RET_IP_);
1051 }
1052 
1053 void
1054 xfs_buf_ioerror_alert(
1055         struct xfs_buf          *bp,
1056         const char              *func)
1057 {
1058         xfs_alert(bp->b_target->bt_mount,
1059 "metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
1060                 (__uint64_t)XFS_BUF_ADDR(bp), func, bp->b_error, bp->b_length);
1061 }
1062 
1063 /*
1064  * Called when we want to stop a buffer from getting written or read.
1065  * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
1066  * so that the proper iodone callbacks get called.
1067  */
1068 STATIC int
1069 xfs_bioerror(
1070         xfs_buf_t *bp)
1071 {
1072 #ifdef XFSERRORDEBUG
1073         ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1074 #endif
1075 
1076         /*
1077          * No need to wait until the buffer is unpinned, we aren't flushing it.
1078          */
1079         xfs_buf_ioerror(bp, EIO);
1080 
1081         /*
1082          * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
1083          */
1084         XFS_BUF_UNREAD(bp);
1085         XFS_BUF_UNDONE(bp);
1086         xfs_buf_stale(bp);
1087 
1088         xfs_buf_ioend(bp, 0);
1089 
1090         return EIO;
1091 }
1092 
1093 /*
1094  * Same as xfs_bioerror, except that we are releasing the buffer
1095  * here ourselves, and avoiding the xfs_buf_ioend call.
1096  * This is meant for userdata errors; metadata bufs come with
1097  * iodone functions attached, so that we can track down errors.
1098  */
1099 STATIC int
1100 xfs_bioerror_relse(
1101         struct xfs_buf  *bp)
1102 {
1103         int64_t         fl = bp->b_flags;
1104         /*
1105          * No need to wait until the buffer is unpinned.
1106          * We aren't flushing it.
1107          *
1108          * chunkhold expects B_DONE to be set, whether
1109          * we actually finish the I/O or not. We don't want to
1110          * change that interface.
1111          */
1112         XFS_BUF_UNREAD(bp);
1113         XFS_BUF_DONE(bp);
1114         xfs_buf_stale(bp);
1115         bp->b_iodone = NULL;
1116         if (!(fl & XBF_ASYNC)) {
1117                 /*
1118                  * Mark b_error and B_ERROR _both_.
1119                  * Lot's of chunkcache code assumes that.
1120                  * There's no reason to mark error for
1121                  * ASYNC buffers.
1122                  */
1123                 xfs_buf_ioerror(bp, EIO);
1124                 complete(&bp->b_iowait);
1125         } else {
1126                 xfs_buf_relse(bp);
1127         }
1128 
1129         return EIO;
1130 }
1131 
1132 STATIC int
1133 xfs_bdstrat_cb(
1134         struct xfs_buf  *bp)
1135 {
1136         if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
1137                 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1138                 /*
1139                  * Metadata write that didn't get logged but
1140                  * written delayed anyway. These aren't associated
1141                  * with a transaction, and can be ignored.
1142                  */
1143                 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1144                         return xfs_bioerror_relse(bp);
1145                 else
1146                         return xfs_bioerror(bp);
1147         }
1148 
1149         xfs_buf_iorequest(bp);
1150         return 0;
1151 }
1152 
1153 int
1154 xfs_bwrite(
1155         struct xfs_buf          *bp)
1156 {
1157         int                     error;
1158 
1159         ASSERT(xfs_buf_islocked(bp));
1160 
1161         bp->b_flags |= XBF_WRITE;
1162         bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
1163 
1164         xfs_bdstrat_cb(bp);
1165 
1166         error = xfs_buf_iowait(bp);
1167         if (error) {
1168                 xfs_force_shutdown(bp->b_target->bt_mount,
1169                                    SHUTDOWN_META_IO_ERROR);
1170         }
1171         return error;
1172 }
1173 
1174 /*
1175  * Wrapper around bdstrat so that we can stop data from going to disk in case
1176  * we are shutting down the filesystem.  Typically user data goes thru this
1177  * path; one of the exceptions is the superblock.
1178  */
1179 void
1180 xfsbdstrat(
1181         struct xfs_mount        *mp,
1182         struct xfs_buf          *bp)
1183 {
1184         if (XFS_FORCED_SHUTDOWN(mp)) {
1185                 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1186                 xfs_bioerror_relse(bp);
1187                 return;
1188         }
1189 
1190         xfs_buf_iorequest(bp);
1191 }
1192 
1193 STATIC void
1194 _xfs_buf_ioend(
1195         xfs_buf_t               *bp,
1196         int                     schedule)
1197 {
1198         if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
1199                 xfs_buf_ioend(bp, schedule);
1200 }
1201 
1202 STATIC void
1203 xfs_buf_bio_end_io(
1204         struct bio              *bio,
1205         int                     error)
1206 {
1207         xfs_buf_t               *bp = (xfs_buf_t *)bio->bi_private;
1208 
1209         /*
1210          * don't overwrite existing errors - otherwise we can lose errors on
1211          * buffers that require multiple bios to complete.
1212          */
1213         if (!bp->b_error)
1214                 xfs_buf_ioerror(bp, -error);
1215 
1216         if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1217                 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1218 
1219         _xfs_buf_ioend(bp, 1);
1220         bio_put(bio);
1221 }
1222 
1223 static void
1224 xfs_buf_ioapply_map(
1225         struct xfs_buf  *bp,
1226         int             map,
1227         int             *buf_offset,
1228         int             *count,
1229         int             rw)
1230 {
1231         int             page_index;
1232         int             total_nr_pages = bp->b_page_count;
1233         int             nr_pages;
1234         struct bio      *bio;
1235         sector_t        sector =  bp->b_maps[map].bm_bn;
1236         int             size;
1237         int             offset;
1238 
1239         total_nr_pages = bp->b_page_count;
1240 
1241         /* skip the pages in the buffer before the start offset */
1242         page_index = 0;
1243         offset = *buf_offset;
1244         while (offset >= PAGE_SIZE) {
1245                 page_index++;
1246                 offset -= PAGE_SIZE;
1247         }
1248 
1249         /*
1250          * Limit the IO size to the length of the current vector, and update the
1251          * remaining IO count for the next time around.
1252          */
1253         size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1254         *count -= size;
1255         *buf_offset += size;
1256 
1257 next_chunk:
1258         atomic_inc(&bp->b_io_remaining);
1259         nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1260         if (nr_pages > total_nr_pages)
1261                 nr_pages = total_nr_pages;
1262 
1263         bio = bio_alloc(GFP_NOIO, nr_pages);
1264         bio->bi_bdev = bp->b_target->bt_bdev;
1265         bio->bi_sector = sector;
1266         bio->bi_end_io = xfs_buf_bio_end_io;
1267         bio->bi_private = bp;
1268 
1269 
1270         for (; size && nr_pages; nr_pages--, page_index++) {
1271                 int     rbytes, nbytes = PAGE_SIZE - offset;
1272 
1273                 if (nbytes > size)
1274                         nbytes = size;
1275 
1276                 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1277                                       offset);
1278                 if (rbytes < nbytes)
1279                         break;
1280 
1281                 offset = 0;
1282                 sector += BTOBB(nbytes);
1283                 size -= nbytes;
1284                 total_nr_pages--;
1285         }
1286 
1287         if (likely(bio->bi_size)) {
1288                 if (xfs_buf_is_vmapped(bp)) {
1289                         flush_kernel_vmap_range(bp->b_addr,
1290                                                 xfs_buf_vmap_len(bp));
1291                 }
1292                 submit_bio(rw, bio);
1293                 if (size)
1294                         goto next_chunk;
1295         } else {
1296                 /*
1297                  * This is guaranteed not to be the last io reference count
1298                  * because the caller (xfs_buf_iorequest) holds a count itself.
1299                  */
1300                 atomic_dec(&bp->b_io_remaining);
1301                 xfs_buf_ioerror(bp, EIO);
1302                 bio_put(bio);
1303         }
1304 
1305 }
1306 
1307 STATIC void
1308 _xfs_buf_ioapply(
1309         struct xfs_buf  *bp)
1310 {
1311         struct blk_plug plug;
1312         int             rw;
1313         int             offset;
1314         int             size;
1315         int             i;
1316 
1317         /*
1318          * Make sure we capture only current IO errors rather than stale errors
1319          * left over from previous use of the buffer (e.g. failed readahead).
1320          */
1321         bp->b_error = 0;
1322 
1323         if (bp->b_flags & XBF_WRITE) {
1324                 if (bp->b_flags & XBF_SYNCIO)
1325                         rw = WRITE_SYNC;
1326                 else
1327                         rw = WRITE;
1328                 if (bp->b_flags & XBF_FUA)
1329                         rw |= REQ_FUA;
1330                 if (bp->b_flags & XBF_FLUSH)
1331                         rw |= REQ_FLUSH;
1332 
1333                 /*
1334                  * Run the write verifier callback function if it exists. If
1335                  * this function fails it will mark the buffer with an error and
1336                  * the IO should not be dispatched.
1337                  */
1338                 if (bp->b_ops) {
1339                         bp->b_ops->verify_write(bp);
1340                         if (bp->b_error) {
1341                                 xfs_force_shutdown(bp->b_target->bt_mount,
1342                                                    SHUTDOWN_CORRUPT_INCORE);
1343                                 return;
1344                         }
1345                 }
1346         } else if (bp->b_flags & XBF_READ_AHEAD) {
1347                 rw = READA;
1348         } else {
1349                 rw = READ;
1350         }
1351 
1352         /* we only use the buffer cache for meta-data */
1353         rw |= REQ_META;
1354 
1355         /*
1356          * Walk all the vectors issuing IO on them. Set up the initial offset
1357          * into the buffer and the desired IO size before we start -
1358          * _xfs_buf_ioapply_vec() will modify them appropriately for each
1359          * subsequent call.
1360          */
1361         offset = bp->b_offset;
1362         size = BBTOB(bp->b_io_length);
1363         blk_start_plug(&plug);
1364         for (i = 0; i < bp->b_map_count; i++) {
1365                 xfs_buf_ioapply_map(bp, i, &offset, &size, rw);
1366                 if (bp->b_error)
1367                         break;
1368                 if (size <= 0)
1369                         break;  /* all done */
1370         }
1371         blk_finish_plug(&plug);
1372 }
1373 
1374 void
1375 xfs_buf_iorequest(
1376         xfs_buf_t               *bp)
1377 {
1378         trace_xfs_buf_iorequest(bp, _RET_IP_);
1379 
1380         ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
1381 
1382         if (bp->b_flags & XBF_WRITE)
1383                 xfs_buf_wait_unpin(bp);
1384         xfs_buf_hold(bp);
1385 
1386         /* Set the count to 1 initially, this will stop an I/O
1387          * completion callout which happens before we have started
1388          * all the I/O from calling xfs_buf_ioend too early.
1389          */
1390         atomic_set(&bp->b_io_remaining, 1);
1391         _xfs_buf_ioapply(bp);
1392         _xfs_buf_ioend(bp, 1);
1393 
1394         xfs_buf_rele(bp);
1395 }
1396 
1397 /*
1398  * Waits for I/O to complete on the buffer supplied.  It returns immediately if
1399  * no I/O is pending or there is already a pending error on the buffer.  It
1400  * returns the I/O error code, if any, or 0 if there was no error.
1401  */
1402 int
1403 xfs_buf_iowait(
1404         xfs_buf_t               *bp)
1405 {
1406         trace_xfs_buf_iowait(bp, _RET_IP_);
1407 
1408         if (!bp->b_error)
1409                 wait_for_completion(&bp->b_iowait);
1410 
1411         trace_xfs_buf_iowait_done(bp, _RET_IP_);
1412         return bp->b_error;
1413 }
1414 
1415 xfs_caddr_t
1416 xfs_buf_offset(
1417         xfs_buf_t               *bp,
1418         size_t                  offset)
1419 {
1420         struct page             *page;
1421 
1422         if (bp->b_addr)
1423                 return bp->b_addr + offset;
1424 
1425         offset += bp->b_offset;
1426         page = bp->b_pages[offset >> PAGE_SHIFT];
1427         return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
1428 }
1429 
1430 /*
1431  *      Move data into or out of a buffer.
1432  */
1433 void
1434 xfs_buf_iomove(
1435         xfs_buf_t               *bp,    /* buffer to process            */
1436         size_t                  boff,   /* starting buffer offset       */
1437         size_t                  bsize,  /* length to copy               */
1438         void                    *data,  /* data address                 */
1439         xfs_buf_rw_t            mode)   /* read/write/zero flag         */
1440 {
1441         size_t                  bend;
1442 
1443         bend = boff + bsize;
1444         while (boff < bend) {
1445                 struct page     *page;
1446                 int             page_index, page_offset, csize;
1447 
1448                 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1449                 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1450                 page = bp->b_pages[page_index];
1451                 csize = min_t(size_t, PAGE_SIZE - page_offset,
1452                                       BBTOB(bp->b_io_length) - boff);
1453 
1454                 ASSERT((csize + page_offset) <= PAGE_SIZE);
1455 
1456                 switch (mode) {
1457                 case XBRW_ZERO:
1458                         memset(page_address(page) + page_offset, 0, csize);
1459                         break;
1460                 case XBRW_READ:
1461                         memcpy(data, page_address(page) + page_offset, csize);
1462                         break;
1463                 case XBRW_WRITE:
1464                         memcpy(page_address(page) + page_offset, data, csize);
1465                 }
1466 
1467                 boff += csize;
1468                 data += csize;
1469         }
1470 }
1471 
1472 /*
1473  *      Handling of buffer targets (buftargs).
1474  */
1475 
1476 /*
1477  * Wait for any bufs with callbacks that have been submitted but have not yet
1478  * returned. These buffers will have an elevated hold count, so wait on those
1479  * while freeing all the buffers only held by the LRU.
1480  */
1481 static enum lru_status
1482 xfs_buftarg_wait_rele(
1483         struct list_head        *item,
1484         spinlock_t              *lru_lock,
1485         void                    *arg)
1486 
1487 {
1488         struct xfs_buf          *bp = container_of(item, struct xfs_buf, b_lru);
1489         struct list_head        *dispose = arg;
1490 
1491         if (atomic_read(&bp->b_hold) > 1) {
1492                 /* need to wait, so skip it this pass */
1493                 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
1494                 return LRU_SKIP;
1495         }
1496         if (!spin_trylock(&bp->b_lock))
1497                 return LRU_SKIP;
1498 
1499         /*
1500          * clear the LRU reference count so the buffer doesn't get
1501          * ignored in xfs_buf_rele().
1502          */
1503         atomic_set(&bp->b_lru_ref, 0);
1504         bp->b_state |= XFS_BSTATE_DISPOSE;
1505         list_move(item, dispose);
1506         spin_unlock(&bp->b_lock);
1507         return LRU_REMOVED;
1508 }
1509 
1510 void
1511 xfs_wait_buftarg(
1512         struct xfs_buftarg      *btp)
1513 {
1514         LIST_HEAD(dispose);
1515         int loop = 0;
1516 
1517         /* loop until there is nothing left on the lru list. */
1518         while (list_lru_count(&btp->bt_lru)) {
1519                 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
1520                               &dispose, LONG_MAX);
1521 
1522                 while (!list_empty(&dispose)) {
1523                         struct xfs_buf *bp;
1524                         bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1525                         list_del_init(&bp->b_lru);
1526                         xfs_buf_rele(bp);
1527                 }
1528                 if (loop++ != 0)
1529                         delay(100);
1530         }
1531 }
1532 
1533 static enum lru_status
1534 xfs_buftarg_isolate(
1535         struct list_head        *item,
1536         spinlock_t              *lru_lock,
1537         void                    *arg)
1538 {
1539         struct xfs_buf          *bp = container_of(item, struct xfs_buf, b_lru);
1540         struct list_head        *dispose = arg;
1541 
1542         /*
1543          * we are inverting the lru lock/bp->b_lock here, so use a trylock.
1544          * If we fail to get the lock, just skip it.
1545          */
1546         if (!spin_trylock(&bp->b_lock))
1547                 return LRU_SKIP;
1548         /*
1549          * Decrement the b_lru_ref count unless the value is already
1550          * zero. If the value is already zero, we need to reclaim the
1551          * buffer, otherwise it gets another trip through the LRU.
1552          */
1553         if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1554                 spin_unlock(&bp->b_lock);
1555                 return LRU_ROTATE;
1556         }
1557 
1558         bp->b_state |= XFS_BSTATE_DISPOSE;
1559         list_move(item, dispose);
1560         spin_unlock(&bp->b_lock);
1561         return LRU_REMOVED;
1562 }
1563 
1564 static unsigned long
1565 xfs_buftarg_shrink_scan(
1566         struct shrinker         *shrink,
1567         struct shrink_control   *sc)
1568 {
1569         struct xfs_buftarg      *btp = container_of(shrink,
1570                                         struct xfs_buftarg, bt_shrinker);
1571         LIST_HEAD(dispose);
1572         unsigned long           freed;
1573         unsigned long           nr_to_scan = sc->nr_to_scan;
1574 
1575         freed = list_lru_walk_node(&btp->bt_lru, sc->nid, xfs_buftarg_isolate,
1576                                        &dispose, &nr_to_scan);
1577 
1578         while (!list_empty(&dispose)) {
1579                 struct xfs_buf *bp;
1580                 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1581                 list_del_init(&bp->b_lru);
1582                 xfs_buf_rele(bp);
1583         }
1584 
1585         return freed;
1586 }
1587 
1588 static unsigned long
1589 xfs_buftarg_shrink_count(
1590         struct shrinker         *shrink,
1591         struct shrink_control   *sc)
1592 {
1593         struct xfs_buftarg      *btp = container_of(shrink,
1594                                         struct xfs_buftarg, bt_shrinker);
1595         return list_lru_count_node(&btp->bt_lru, sc->nid);
1596 }
1597 
1598 void
1599 xfs_free_buftarg(
1600         struct xfs_mount        *mp,
1601         struct xfs_buftarg      *btp)
1602 {
1603         unregister_shrinker(&btp->bt_shrinker);
1604         list_lru_destroy(&btp->bt_lru);
1605 
1606         if (mp->m_flags & XFS_MOUNT_BARRIER)
1607                 xfs_blkdev_issue_flush(btp);
1608 
1609         kmem_free(btp);
1610 }
1611 
1612 STATIC int
1613 xfs_setsize_buftarg_flags(
1614         xfs_buftarg_t           *btp,
1615         unsigned int            blocksize,
1616         unsigned int            sectorsize,
1617         int                     verbose)
1618 {
1619         btp->bt_bsize = blocksize;
1620         btp->bt_sshift = ffs(sectorsize) - 1;
1621         btp->bt_smask = sectorsize - 1;
1622 
1623         if (set_blocksize(btp->bt_bdev, sectorsize)) {
1624                 char name[BDEVNAME_SIZE];
1625 
1626                 bdevname(btp->bt_bdev, name);
1627 
1628                 xfs_warn(btp->bt_mount,
1629                         "Cannot set_blocksize to %u on device %s\n",
1630                         sectorsize, name);
1631                 return EINVAL;
1632         }
1633 
1634         return 0;
1635 }
1636 
1637 /*
1638  *      When allocating the initial buffer target we have not yet
1639  *      read in the superblock, so don't know what sized sectors
1640  *      are being used at this early stage.  Play safe.
1641  */
1642 STATIC int
1643 xfs_setsize_buftarg_early(
1644         xfs_buftarg_t           *btp,
1645         struct block_device     *bdev)
1646 {
1647         return xfs_setsize_buftarg_flags(btp,
1648                         PAGE_SIZE, bdev_logical_block_size(bdev), 0);
1649 }
1650 
1651 int
1652 xfs_setsize_buftarg(
1653         xfs_buftarg_t           *btp,
1654         unsigned int            blocksize,
1655         unsigned int            sectorsize)
1656 {
1657         return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1658 }
1659 
1660 xfs_buftarg_t *
1661 xfs_alloc_buftarg(
1662         struct xfs_mount        *mp,
1663         struct block_device     *bdev,
1664         int                     external,
1665         const char              *fsname)
1666 {
1667         xfs_buftarg_t           *btp;
1668 
1669         btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS);
1670 
1671         btp->bt_mount = mp;
1672         btp->bt_dev =  bdev->bd_dev;
1673         btp->bt_bdev = bdev;
1674         btp->bt_bdi = blk_get_backing_dev_info(bdev);
1675         if (!btp->bt_bdi)
1676                 goto error;
1677 
1678         if (xfs_setsize_buftarg_early(btp, bdev))
1679                 goto error;
1680 
1681         if (list_lru_init(&btp->bt_lru))
1682                 goto error;
1683 
1684         btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1685         btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
1686         btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1687         btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
1688         register_shrinker(&btp->bt_shrinker);
1689         return btp;
1690 
1691 error:
1692         kmem_free(btp);
1693         return NULL;
1694 }
1695 
1696 /*
1697  * Add a buffer to the delayed write list.
1698  *
1699  * This queues a buffer for writeout if it hasn't already been.  Note that
1700  * neither this routine nor the buffer list submission functions perform
1701  * any internal synchronization.  It is expected that the lists are thread-local
1702  * to the callers.
1703  *
1704  * Returns true if we queued up the buffer, or false if it already had
1705  * been on the buffer list.
1706  */
1707 bool
1708 xfs_buf_delwri_queue(
1709         struct xfs_buf          *bp,
1710         struct list_head        *list)
1711 {
1712         ASSERT(xfs_buf_islocked(bp));
1713         ASSERT(!(bp->b_flags & XBF_READ));
1714 
1715         /*
1716          * If the buffer is already marked delwri it already is queued up
1717          * by someone else for imediate writeout.  Just ignore it in that
1718          * case.
1719          */
1720         if (bp->b_flags & _XBF_DELWRI_Q) {
1721                 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1722                 return false;
1723         }
1724 
1725         trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1726 
1727         /*
1728          * If a buffer gets written out synchronously or marked stale while it
1729          * is on a delwri list we lazily remove it. To do this, the other party
1730          * clears the  _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1731          * It remains referenced and on the list.  In a rare corner case it
1732          * might get readded to a delwri list after the synchronous writeout, in
1733          * which case we need just need to re-add the flag here.
1734          */
1735         bp->b_flags |= _XBF_DELWRI_Q;
1736         if (list_empty(&bp->b_list)) {
1737                 atomic_inc(&bp->b_hold);
1738                 list_add_tail(&bp->b_list, list);
1739         }
1740 
1741         return true;
1742 }
1743 
1744 /*
1745  * Compare function is more complex than it needs to be because
1746  * the return value is only 32 bits and we are doing comparisons
1747  * on 64 bit values
1748  */
1749 static int
1750 xfs_buf_cmp(
1751         void            *priv,
1752         struct list_head *a,
1753         struct list_head *b)
1754 {
1755         struct xfs_buf  *ap = container_of(a, struct xfs_buf, b_list);
1756         struct xfs_buf  *bp = container_of(b, struct xfs_buf, b_list);
1757         xfs_daddr_t             diff;
1758 
1759         diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
1760         if (diff < 0)
1761                 return -1;
1762         if (diff > 0)
1763                 return 1;
1764         return 0;
1765 }
1766 
1767 static int
1768 __xfs_buf_delwri_submit(
1769         struct list_head        *buffer_list,
1770         struct list_head        *io_list,
1771         bool                    wait)
1772 {
1773         struct blk_plug         plug;
1774         struct xfs_buf          *bp, *n;
1775         int                     pinned = 0;
1776 
1777         list_for_each_entry_safe(bp, n, buffer_list, b_list) {
1778                 if (!wait) {
1779                         if (xfs_buf_ispinned(bp)) {
1780                                 pinned++;
1781                                 continue;
1782                         }
1783                         if (!xfs_buf_trylock(bp))
1784                                 continue;
1785                 } else {
1786                         xfs_buf_lock(bp);
1787                 }
1788 
1789                 /*
1790                  * Someone else might have written the buffer synchronously or
1791                  * marked it stale in the meantime.  In that case only the
1792                  * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1793                  * reference and remove it from the list here.
1794                  */
1795                 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1796                         list_del_init(&bp->b_list);
1797                         xfs_buf_relse(bp);
1798                         continue;
1799                 }
1800 
1801                 list_move_tail(&bp->b_list, io_list);
1802                 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1803         }
1804 
1805         list_sort(NULL, io_list, xfs_buf_cmp);
1806 
1807         blk_start_plug(&plug);
1808         list_for_each_entry_safe(bp, n, io_list, b_list) {
1809                 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
1810                 bp->b_flags |= XBF_WRITE;
1811 
1812                 if (!wait) {
1813                         bp->b_flags |= XBF_ASYNC;
1814                         list_del_init(&bp->b_list);
1815                 }
1816                 xfs_bdstrat_cb(bp);
1817         }
1818         blk_finish_plug(&plug);
1819 
1820         return pinned;
1821 }
1822 
1823 /*
1824  * Write out a buffer list asynchronously.
1825  *
1826  * This will take the @buffer_list, write all non-locked and non-pinned buffers
1827  * out and not wait for I/O completion on any of the buffers.  This interface
1828  * is only safely useable for callers that can track I/O completion by higher
1829  * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1830  * function.
1831  */
1832 int
1833 xfs_buf_delwri_submit_nowait(
1834         struct list_head        *buffer_list)
1835 {
1836         LIST_HEAD               (io_list);
1837         return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
1838 }
1839 
1840 /*
1841  * Write out a buffer list synchronously.
1842  *
1843  * This will take the @buffer_list, write all buffers out and wait for I/O
1844  * completion on all of the buffers. @buffer_list is consumed by the function,
1845  * so callers must have some other way of tracking buffers if they require such
1846  * functionality.
1847  */
1848 int
1849 xfs_buf_delwri_submit(
1850         struct list_head        *buffer_list)
1851 {
1852         LIST_HEAD               (io_list);
1853         int                     error = 0, error2;
1854         struct xfs_buf          *bp;
1855 
1856         __xfs_buf_delwri_submit(buffer_list, &io_list, true);
1857 
1858         /* Wait for IO to complete. */
1859         while (!list_empty(&io_list)) {
1860                 bp = list_first_entry(&io_list, struct xfs_buf, b_list);
1861 
1862                 list_del_init(&bp->b_list);
1863                 error2 = xfs_buf_iowait(bp);
1864                 xfs_buf_relse(bp);
1865                 if (!error)
1866                         error = error2;
1867         }
1868 
1869         return error;
1870 }
1871 
1872 int __init
1873 xfs_buf_init(void)
1874 {
1875         xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1876                                                 KM_ZONE_HWALIGN, NULL);
1877         if (!xfs_buf_zone)
1878                 goto out;
1879 
1880         xfslogd_workqueue = alloc_workqueue("xfslogd",
1881                                         WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
1882         if (!xfslogd_workqueue)
1883                 goto out_free_buf_zone;
1884 
1885         return 0;
1886 
1887  out_free_buf_zone:
1888         kmem_zone_destroy(xfs_buf_zone);
1889  out:
1890         return -ENOMEM;
1891 }
1892 
1893 void
1894 xfs_buf_terminate(void)
1895 {
1896         destroy_workqueue(xfslogd_workqueue);
1897         kmem_zone_destroy(xfs_buf_zone);
1898 }
1899 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp