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