blob: 2eef165f4f39cc4a019f23914443e90b75ed61a5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scottf07c2252006-09-28 10:52:15 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Vlad Apostolov93c189c2006-11-11 18:03:49 +110018#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/stddef.h>
20#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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>
Christoph Hellwig4df08c52005-09-05 08:34:18 +100032#include <linux/kthread.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080033#include <linux/migrate.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070034#include <linux/backing-dev.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035#include <linux/freezer.h>
Dave Chinner089716a2010-01-26 15:13:25 +110036#include <linux/list_sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Christoph Hellwigb7963132009-03-03 14:48:37 -050038#include "xfs_sb.h"
39#include "xfs_inum.h"
Dave Chinnered3b4d62010-05-21 12:07:08 +100040#include "xfs_log.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050041#include "xfs_ag.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050042#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050044
David Chinner7989cb82007-02-10 18:34:56 +110045static kmem_zone_t *xfs_buf_zone;
David Chinnera6867a62006-01-11 15:37:58 +110046STATIC int xfsbufd(void *);
Nathan Scottce8e9222006-01-11 15:39:08 +110047STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
Christoph Hellwig23ea4032005-06-21 15:14:01 +100048
David Chinner7989cb82007-02-10 18:34:56 +110049static struct workqueue_struct *xfslogd_workqueue;
Christoph Hellwig0829c362005-09-02 16:58:49 +100050struct workqueue_struct *xfsdatad_workqueue;
Dave Chinnerc626d172009-04-06 18:42:11 +020051struct workqueue_struct *xfsconvertd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Nathan Scottce8e9222006-01-11 15:39:08 +110053#ifdef XFS_BUF_LOCK_TRACKING
54# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
55# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
56# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#else
Nathan Scottce8e9222006-01-11 15:39:08 +110058# define XB_SET_OWNER(bp) do { } while (0)
59# define XB_CLEAR_OWNER(bp) do { } while (0)
60# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
62
Nathan Scottce8e9222006-01-11 15:39:08 +110063#define xb_to_gfp(flags) \
64 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
65 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Nathan Scottce8e9222006-01-11 15:39:08 +110067#define xb_to_km(flags) \
68 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Nathan Scottce8e9222006-01-11 15:39:08 +110070#define xfs_buf_allocate(flags) \
71 kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
72#define xfs_buf_deallocate(bp) \
73 kmem_zone_free(xfs_buf_zone, (bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
James Bottomley73c77e22010-01-25 11:42:24 -060075static inline int
76xfs_buf_is_vmapped(
77 struct xfs_buf *bp)
78{
79 /*
80 * Return true if the buffer is vmapped.
81 *
82 * The XBF_MAPPED flag is set if the buffer should be mapped, but the
83 * code is clever enough to know it doesn't have to map a single page,
84 * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
85 */
86 return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
87}
88
89static inline int
90xfs_buf_vmap_len(
91 struct xfs_buf *bp)
92{
93 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
Dave Chinner430cbeb2010-12-02 16:30:55 +110097 * xfs_buf_lru_add - add a buffer to the LRU.
98 *
99 * The LRU takes a new reference to the buffer so that it will only be freed
100 * once the shrinker takes the buffer off the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
Dave Chinner430cbeb2010-12-02 16:30:55 +1100102STATIC void
103xfs_buf_lru_add(
104 struct xfs_buf *bp)
105{
106 struct xfs_buftarg *btp = bp->b_target;
107
108 spin_lock(&btp->bt_lru_lock);
109 if (list_empty(&bp->b_lru)) {
110 atomic_inc(&bp->b_hold);
111 list_add_tail(&bp->b_lru, &btp->bt_lru);
112 btp->bt_lru_nr++;
113 }
114 spin_unlock(&btp->bt_lru_lock);
115}
116
117/*
118 * xfs_buf_lru_del - remove a buffer from the LRU
119 *
120 * The unlocked check is safe here because it only occurs when there are not
121 * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there
122 * to optimise the shrinker removing the buffer from the LRU and calling
123 * xfs_buf_free(). i.e. it removes an unneccessary round trip on the
124 * bt_lru_lock.
125 */
126STATIC void
127xfs_buf_lru_del(
128 struct xfs_buf *bp)
129{
130 struct xfs_buftarg *btp = bp->b_target;
131
132 if (list_empty(&bp->b_lru))
133 return;
134
135 spin_lock(&btp->bt_lru_lock);
136 if (!list_empty(&bp->b_lru)) {
137 list_del_init(&bp->b_lru);
138 btp->bt_lru_nr--;
139 }
140 spin_unlock(&btp->bt_lru_lock);
141}
142
143/*
144 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
145 * b_lru_ref count so that the buffer is freed immediately when the buffer
146 * reference count falls to zero. If the buffer is already on the LRU, we need
147 * to remove the reference that LRU holds on the buffer.
148 *
149 * This prevents build-up of stale buffers on the LRU.
150 */
151void
152xfs_buf_stale(
153 struct xfs_buf *bp)
154{
155 bp->b_flags |= XBF_STALE;
156 atomic_set(&(bp)->b_lru_ref, 0);
157 if (!list_empty(&bp->b_lru)) {
158 struct xfs_buftarg *btp = bp->b_target;
159
160 spin_lock(&btp->bt_lru_lock);
161 if (!list_empty(&bp->b_lru)) {
162 list_del_init(&bp->b_lru);
163 btp->bt_lru_nr--;
164 atomic_dec(&bp->b_hold);
165 }
166 spin_unlock(&btp->bt_lru_lock);
167 }
168 ASSERT(atomic_read(&bp->b_hold) >= 1);
169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100172_xfs_buf_initialize(
173 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100175 xfs_off_t range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 size_t range_length,
Nathan Scottce8e9222006-01-11 15:39:08 +1100177 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100180 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100182 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Nathan Scottce8e9222006-01-11 15:39:08 +1100184 memset(bp, 0, sizeof(xfs_buf_t));
185 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100186 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000187 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100188 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100189 INIT_LIST_HEAD(&bp->b_list);
Dave Chinner74f75a02010-09-24 19:59:04 +1000190 RB_CLEAR_NODE(&bp->b_rbnode);
Thomas Gleixnera731cd12010-09-07 14:33:15 +0000191 sema_init(&bp->b_sema, 0); /* held, no waiters */
Nathan Scottce8e9222006-01-11 15:39:08 +1100192 XB_SET_OWNER(bp);
193 bp->b_target = target;
194 bp->b_file_offset = range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /*
196 * Set buffer_length and count_desired to the same value initially.
197 * I/O routines should use count_desired, which will be the same in
198 * most cases but may be reset (e.g. XFS recovery).
199 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100200 bp->b_buffer_length = bp->b_count_desired = range_length;
201 bp->b_flags = flags;
202 bp->b_bn = XFS_BUF_DADDR_NULL;
203 atomic_set(&bp->b_pin_count, 0);
204 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Nathan Scottce8e9222006-01-11 15:39:08 +1100206 XFS_STATS_INC(xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000207
208 trace_xfs_buf_init(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100212 * Allocate a page array capable of holding a specified number
213 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
215STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100216_xfs_buf_get_pages(
217 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100219 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100222 if (bp->b_pages == NULL) {
223 bp->b_offset = xfs_buf_poff(bp->b_file_offset);
224 bp->b_page_count = page_count;
225 if (page_count <= XB_PAGES) {
226 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100228 bp->b_pages = kmem_alloc(sizeof(struct page *) *
229 page_count, xb_to_km(flags));
230 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -ENOMEM;
232 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100233 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235 return 0;
236}
237
238/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100239 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100242_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 xfs_buf_t *bp)
244{
Nathan Scottce8e9222006-01-11 15:39:08 +1100245 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000246 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000247 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249}
250
251/*
252 * Releases the specified buffer.
253 *
254 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100255 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 * hashed and refcounted buffers
257 */
258void
Nathan Scottce8e9222006-01-11 15:39:08 +1100259xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 xfs_buf_t *bp)
261{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000262 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Dave Chinner430cbeb2010-12-02 16:30:55 +1100264 ASSERT(list_empty(&bp->b_lru));
265
Dave Chinner0e6e8472011-03-26 09:16:45 +1100266 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 uint i;
268
James Bottomley73c77e22010-01-25 11:42:24 -0600269 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000270 vm_unmap_ram(bp->b_addr - bp->b_offset,
271 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Nathan Scott948ecdb2006-09-28 11:03:13 +1000273 for (i = 0; i < bp->b_page_count; i++) {
274 struct page *page = bp->b_pages[i];
275
Dave Chinner0e6e8472011-03-26 09:16:45 +1100276 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000277 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100278 } else if (bp->b_flags & _XBF_KMEM)
279 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000280 _xfs_buf_free_pages(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +1100281 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100285 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 */
287STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100288xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 xfs_buf_t *bp,
290 uint flags)
291{
Nathan Scottce8e9222006-01-11 15:39:08 +1100292 size_t size = bp->b_count_desired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100294 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 unsigned short page_count, i;
Nathan Scott204ab252006-01-11 20:50:22 +1100296 xfs_off_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int error;
298
Dave Chinner0e6e8472011-03-26 09:16:45 +1100299 /*
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 if (bp->b_buffer_length < PAGE_SIZE) {
305 bp->b_addr = kmem_alloc(bp->b_buffer_length, xb_to_km(flags));
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 + bp->b_buffer_length - 1) &
312 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_MAPPED | _XBF_KMEM;
324 return 0;
325 }
326
327use_alloc_page:
Nathan Scottce8e9222006-01-11 15:39:08 +1100328 end = bp->b_file_offset + bp->b_buffer_length;
329 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
Nathan Scottce8e9222006-01-11 15:39:08 +1100330 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (unlikely(error))
332 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Nathan Scottce8e9222006-01-11 15:39:08 +1100334 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100335 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Nathan Scottce8e9222006-01-11 15:39:08 +1100337 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct page *page;
339 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100340retry:
341 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100343 if (flags & XBF_READ_AHEAD) {
344 bp->b_page_count = i;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100345 error = ENOMEM;
346 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 /*
350 * This could deadlock.
351 *
352 * But until all the XFS lowlevel code is revamped to
353 * handle buffer allocation failures we can't do much.
354 */
355 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100356 xfs_err(NULL,
357 "possible memory allocation deadlock in %s (mode:0x%x)",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000358 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Nathan Scottce8e9222006-01-11 15:39:08 +1100360 XFS_STATS_INC(xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200361 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 goto retry;
363 }
364
Nathan Scottce8e9222006-01-11 15:39:08 +1100365 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Dave Chinner0e6e8472011-03-26 09:16:45 +1100367 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100369 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 offset = 0;
371 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Dave Chinner0e6e8472011-03-26 09:16:45 +1100374out_free_pages:
375 for (i = 0; i < bp->b_page_count; i++)
376 __free_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return error;
378}
379
380/*
381 * Map buffer into kernel address-space if nessecary.
382 */
383STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100384_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 xfs_buf_t *bp,
386 uint flags)
387{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100388 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100389 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100390 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100391 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
392 bp->b_flags |= XBF_MAPPED;
393 } else if (flags & XBF_MAPPED) {
Dave Chinnera19fb382011-03-26 09:13:42 +1100394 int retried = 0;
395
396 do {
397 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
398 -1, PAGE_KERNEL);
399 if (bp->b_addr)
400 break;
401 vm_unmap_aliases();
402 } while (retried++ <= 1);
403
404 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100406 bp->b_addr += bp->b_offset;
407 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
410 return 0;
411}
412
413/*
414 * Finding and Reading Buffers
415 */
416
417/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100418 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * a given range of an inode. The buffer is returned
420 * locked. If other overlapping buffers exist, they are
421 * released before the new buffer is created and locked,
422 * which may imply that this call will block until those buffers
423 * are unlocked. No I/O is implied by this call.
424 */
425xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100426_xfs_buf_find(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 xfs_buftarg_t *btp, /* block device target */
Nathan Scott204ab252006-01-11 20:50:22 +1100428 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100430 xfs_buf_flags_t flags,
431 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Nathan Scott204ab252006-01-11 20:50:22 +1100433 xfs_off_t range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 size_t range_length;
Dave Chinner74f75a02010-09-24 19:59:04 +1000435 struct xfs_perag *pag;
436 struct rb_node **rbp;
437 struct rb_node *parent;
438 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 range_base = (ioff << BBSHIFT);
441 range_length = (isize << BBSHIFT);
442
443 /* Check for IOs smaller than the sector size / not sector aligned */
Nathan Scottce8e9222006-01-11 15:39:08 +1100444 ASSERT(!(range_length < (1 << btp->bt_sshift)));
Nathan Scott204ab252006-01-11 20:50:22 +1100445 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Dave Chinner74f75a02010-09-24 19:59:04 +1000447 /* get tree root */
448 pag = xfs_perag_get(btp->bt_mount,
449 xfs_daddr_to_agno(btp->bt_mount, ioff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Dave Chinner74f75a02010-09-24 19:59:04 +1000451 /* walk tree */
452 spin_lock(&pag->pag_buf_lock);
453 rbp = &pag->pag_buf_tree.rb_node;
454 parent = NULL;
455 bp = NULL;
456 while (*rbp) {
457 parent = *rbp;
458 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Dave Chinner74f75a02010-09-24 19:59:04 +1000460 if (range_base < bp->b_file_offset)
461 rbp = &(*rbp)->rb_left;
462 else if (range_base > bp->b_file_offset)
463 rbp = &(*rbp)->rb_right;
464 else {
465 /*
466 * found a block offset match. If the range doesn't
467 * match, the only way this is allowed is if the buffer
468 * in the cache is stale and the transaction that made
469 * it stale has not yet committed. i.e. we are
470 * reallocating a busy extent. Skip this buffer and
471 * continue searching to the right for an exact match.
472 */
473 if (bp->b_buffer_length != range_length) {
474 ASSERT(bp->b_flags & XBF_STALE);
475 rbp = &(*rbp)->rb_right;
476 continue;
477 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100478 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto found;
480 }
481 }
482
483 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100484 if (new_bp) {
485 _xfs_buf_initialize(new_bp, btp, range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 range_length, flags);
Dave Chinner74f75a02010-09-24 19:59:04 +1000487 rb_link_node(&new_bp->b_rbnode, parent, rbp);
488 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
489 /* the buffer keeps the perag reference until it is freed */
490 new_bp->b_pag = pag;
491 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100493 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000494 spin_unlock(&pag->pag_buf_lock);
495 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100497 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000500 spin_unlock(&pag->pag_buf_lock);
501 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Dave Chinner90810b92010-11-30 15:16:16 +1100503 if (xfs_buf_cond_lock(bp)) {
504 /* failed, so wait for the lock if requested. */
Nathan Scottce8e9222006-01-11 15:39:08 +1100505 if (!(flags & XBF_TRYLOCK)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100506 xfs_buf_lock(bp);
507 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100509 xfs_buf_rele(bp);
510 XFS_STATS_INC(xb_busy_locked);
511 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
Dave Chinner0e6e8472011-03-26 09:16:45 +1100515 /*
516 * if the buffer is stale, clear all the external state associated with
517 * it. We need to keep flags such as how we allocated the buffer memory
518 * intact here.
519 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100520 if (bp->b_flags & XBF_STALE) {
521 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100522 bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
David Chinner2f926582005-09-05 08:33:35 +1000523 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000524
525 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100526 XFS_STATS_INC(xb_get_locked);
527 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100531 * Assembles a buffer covering the specified range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 * Storage in memory for all portions of the buffer will be allocated,
533 * although backing storage may not be.
534 */
535xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000536xfs_buf_get(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100538 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100540 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Nathan Scottce8e9222006-01-11 15:39:08 +1100542 xfs_buf_t *bp, *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100543 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Nathan Scottce8e9222006-01-11 15:39:08 +1100545 new_bp = xfs_buf_allocate(flags);
546 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return NULL;
548
Nathan Scottce8e9222006-01-11 15:39:08 +1100549 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
550 if (bp == new_bp) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100551 error = xfs_buf_allocate_memory(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (error)
553 goto no_buffer;
554 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100555 xfs_buf_deallocate(new_bp);
556 if (unlikely(bp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return NULL;
558 }
559
Nathan Scottce8e9222006-01-11 15:39:08 +1100560 if (!(bp->b_flags & XBF_MAPPED)) {
561 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100563 xfs_warn(target->bt_mount,
564 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto no_buffer;
566 }
567 }
568
Nathan Scottce8e9222006-01-11 15:39:08 +1100569 XFS_STATS_INC(xb_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /*
572 * Always fill in the block number now, the mapped cases can do
573 * their own overlay of this later.
574 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100575 bp->b_bn = ioff;
576 bp->b_count_desired = bp->b_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000578 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100579 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100582 if (flags & (XBF_LOCK | XBF_TRYLOCK))
583 xfs_buf_unlock(bp);
584 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return NULL;
586}
587
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100588STATIC int
589_xfs_buf_read(
590 xfs_buf_t *bp,
591 xfs_buf_flags_t flags)
592{
593 int status;
594
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100595 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
596 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
597
598 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
599 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
600 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
601 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
602
603 status = xfs_buf_iorequest(bp);
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000604 if (status || XFS_BUF_ISERROR(bp) || (flags & XBF_ASYNC))
605 return status;
606 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000610xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100612 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100614 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Nathan Scottce8e9222006-01-11 15:39:08 +1100616 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Nathan Scottce8e9222006-01-11 15:39:08 +1100618 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000620 bp = xfs_buf_get(target, ioff, isize, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100621 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000622 trace_xfs_buf_read(bp, flags, _RET_IP_);
623
Nathan Scottce8e9222006-01-11 15:39:08 +1100624 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100625 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100626 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100627 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /*
629 * Read ahead call which is already satisfied,
630 * drop the buffer
631 */
632 goto no_buffer;
633 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100635 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637 }
638
Nathan Scottce8e9222006-01-11 15:39:08 +1100639 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100642 if (flags & (XBF_LOCK | XBF_TRYLOCK))
643 xfs_buf_unlock(bp);
644 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return NULL;
646}
647
648/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100649 * If we are not low on memory then do the readahead in a deadlock
650 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 */
652void
Nathan Scottce8e9222006-01-11 15:39:08 +1100653xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100655 xfs_off_t ioff,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000656 size_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100658 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return;
660
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000661 xfs_buf_read(target, ioff, isize,
662 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Dave Chinner5adc94c2010-09-24 21:58:31 +1000665/*
666 * Read an uncached buffer from disk. Allocates and returns a locked
667 * buffer containing the disk contents or nothing.
668 */
669struct xfs_buf *
670xfs_buf_read_uncached(
671 struct xfs_mount *mp,
672 struct xfs_buftarg *target,
673 xfs_daddr_t daddr,
674 size_t length,
675 int flags)
676{
677 xfs_buf_t *bp;
678 int error;
679
680 bp = xfs_buf_get_uncached(target, length, flags);
681 if (!bp)
682 return NULL;
683
684 /* set up the buffer for a read IO */
685 xfs_buf_lock(bp);
686 XFS_BUF_SET_ADDR(bp, daddr);
687 XFS_BUF_READ(bp);
688 XFS_BUF_BUSY(bp);
689
690 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000691 error = xfs_buf_iowait(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000692 if (error || bp->b_error) {
693 xfs_buf_relse(bp);
694 return NULL;
695 }
696 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100700xfs_buf_get_empty(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 size_t len,
702 xfs_buftarg_t *target)
703{
Nathan Scottce8e9222006-01-11 15:39:08 +1100704 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Nathan Scottce8e9222006-01-11 15:39:08 +1100706 bp = xfs_buf_allocate(0);
707 if (bp)
708 _xfs_buf_initialize(bp, target, 0, len, 0);
709 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712static inline struct page *
713mem_to_page(
714 void *addr)
715{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800716 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return virt_to_page(addr);
718 } else {
719 return vmalloc_to_page(addr);
720 }
721}
722
723int
Nathan Scottce8e9222006-01-11 15:39:08 +1100724xfs_buf_associate_memory(
725 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 void *mem,
727 size_t len)
728{
729 int rval;
730 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100731 unsigned long pageaddr;
732 unsigned long offset;
733 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int page_count;
735
Dave Chinner0e6e8472011-03-26 09:16:45 +1100736 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100737 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100738 buflen = PAGE_ALIGN(len + offset);
739 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100742 if (bp->b_pages)
743 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Nathan Scottce8e9222006-01-11 15:39:08 +1100745 bp->b_pages = NULL;
746 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Christoph Hellwig36fae172009-07-18 18:14:58 -0400748 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (rval)
750 return rval;
751
Nathan Scottce8e9222006-01-11 15:39:08 +1100752 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100754 for (i = 0; i < bp->b_page_count; i++) {
755 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100756 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100759 bp->b_count_desired = len;
760 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100761 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 return 0;
764}
765
766xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000767xfs_buf_get_uncached(
768 struct xfs_buftarg *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 size_t len,
Dave Chinner686865f2010-09-24 20:07:47 +1000770 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000772 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
773 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Nathan Scottce8e9222006-01-11 15:39:08 +1100776 bp = xfs_buf_allocate(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (unlikely(bp == NULL))
778 goto fail;
Nathan Scottce8e9222006-01-11 15:39:08 +1100779 _xfs_buf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000781 error = _xfs_buf_get_pages(bp, page_count, 0);
782 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 goto fail_free_buf;
784
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000785 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000786 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000787 if (!bp->b_pages[i])
788 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000790 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000792 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
793 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100794 xfs_warn(target->bt_mount,
795 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Nathan Scottce8e9222006-01-11 15:39:08 +1100799 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Dave Chinner686865f2010-09-24 20:07:47 +1000801 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000805 while (--i >= 0)
806 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000807 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 fail_free_buf:
Christoph Hellwigca165b82007-05-24 15:21:11 +1000809 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 fail:
811 return NULL;
812}
813
814/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 * Increment reference count on buffer, to hold the buffer concurrently
816 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 * Must hold the buffer already to call this function.
818 */
819void
Nathan Scottce8e9222006-01-11 15:39:08 +1100820xfs_buf_hold(
821 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000823 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100824 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
827/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100828 * Releases a hold on the specified buffer. If the
829 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 */
831void
Nathan Scottce8e9222006-01-11 15:39:08 +1100832xfs_buf_rele(
833 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Dave Chinner74f75a02010-09-24 19:59:04 +1000835 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000837 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Dave Chinner74f75a02010-09-24 19:59:04 +1000839 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100840 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000841 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100842 if (atomic_dec_and_test(&bp->b_hold))
843 xfs_buf_free(bp);
844 return;
845 }
846
Dave Chinner74f75a02010-09-24 19:59:04 +1000847 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100848
Lachlan McIlroy37906892008-08-13 15:42:10 +1000849 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000850 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000851 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100852 atomic_read(&bp->b_lru_ref)) {
853 xfs_buf_lru_add(bp);
854 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100855 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100856 xfs_buf_lru_del(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +1100857 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
Dave Chinner74f75a02010-09-24 19:59:04 +1000858 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
859 spin_unlock(&pag->pag_buf_lock);
860 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100861 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 }
864}
865
866
867/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100868 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100869 *
870 * If we come across a stale, pinned, locked buffer, we know that we are
871 * being asked to lock a buffer that has been reallocated. Because it is
872 * pinned, we know that the log has not been pushed to disk and hence it
873 * will still be locked. Rather than continuing to have trylock attempts
874 * fail until someone else pushes the log, push it ourselves before
875 * returning. This means that the xfsaild will not get stuck trying
876 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 */
878int
Nathan Scottce8e9222006-01-11 15:39:08 +1100879xfs_buf_cond_lock(
880 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 int locked;
883
Nathan Scottce8e9222006-01-11 15:39:08 +1100884 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000885 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100886 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100887 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
888 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000889
890 trace_xfs_buf_cond_lock(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100891 return locked ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894int
Nathan Scottce8e9222006-01-11 15:39:08 +1100895xfs_buf_lock_value(
896 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Stephen Rothwelladaa6932008-04-22 15:26:13 +1000898 return bp->b_sema.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100902 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000903 *
904 * If we come across a stale, pinned, locked buffer, we know that we
905 * are being asked to lock a buffer that has been reallocated. Because
906 * it is pinned, we know that the log has not been pushed to disk and
907 * hence it will still be locked. Rather than sleeping until someone
908 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100910void
911xfs_buf_lock(
912 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000914 trace_xfs_buf_lock(bp, _RET_IP_);
915
Dave Chinnered3b4d62010-05-21 12:07:08 +1000916 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000917 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100918 if (atomic_read(&bp->b_io_remaining))
Jens Axboe7eaceac2011-03-10 08:52:07 +0100919 blk_flush_plug(current);
Nathan Scottce8e9222006-01-11 15:39:08 +1100920 down(&bp->b_sema);
921 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000922
923 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100927 * Releases the lock on the buffer object.
David Chinner2f926582005-09-05 08:33:35 +1000928 * If the buffer is marked delwri but is not queued, do so before we
Nathan Scottce8e9222006-01-11 15:39:08 +1100929 * unlock the buffer as we need to set flags correctly. We also need to
David Chinner2f926582005-09-05 08:33:35 +1000930 * take a reference for the delwri queue because the unlocker is going to
931 * drop their's and they don't know we just queued it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
933void
Nathan Scottce8e9222006-01-11 15:39:08 +1100934xfs_buf_unlock(
935 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Nathan Scottce8e9222006-01-11 15:39:08 +1100937 if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
938 atomic_inc(&bp->b_hold);
939 bp->b_flags |= XBF_ASYNC;
940 xfs_buf_delwri_queue(bp, 0);
David Chinner2f926582005-09-05 08:33:35 +1000941 }
942
Nathan Scottce8e9222006-01-11 15:39:08 +1100943 XB_CLEAR_OWNER(bp);
944 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000945
946 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Nathan Scottce8e9222006-01-11 15:39:08 +1100949STATIC void
950xfs_buf_wait_unpin(
951 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 DECLARE_WAITQUEUE (wait, current);
954
Nathan Scottce8e9222006-01-11 15:39:08 +1100955 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return;
957
Nathan Scottce8e9222006-01-11 15:39:08 +1100958 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 for (;;) {
960 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100961 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100963 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100965 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 set_current_state(TASK_RUNNING);
967}
968
969/*
970 * Buffer Utility Routines
971 */
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100974xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000975 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
David Howellsc4028952006-11-22 14:57:56 +0000977 xfs_buf_t *bp =
978 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400980 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100981 (*(bp->b_iodone))(bp);
982 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 xfs_buf_relse(bp);
984}
985
986void
Nathan Scottce8e9222006-01-11 15:39:08 +1100987xfs_buf_ioend(
988 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 int schedule)
990{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000991 trace_xfs_buf_iodone(bp, _RET_IP_);
992
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100993 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100994 if (bp->b_error == 0)
995 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Nathan Scottce8e9222006-01-11 15:39:08 +1100997 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000999 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +11001000 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 } else {
David Howellsc4028952006-11-22 14:57:56 +00001002 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +10001005 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007}
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009void
Nathan Scottce8e9222006-01-11 15:39:08 +11001010xfs_buf_ioerror(
1011 xfs_buf_t *bp,
1012 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001015 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001016 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001020xfs_bwrite(
1021 struct xfs_mount *mp,
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001022 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001024 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001026 bp->b_flags |= XBF_WRITE;
Christoph Hellwig8c383662010-03-12 10:59:40 +00001027 bp->b_flags &= ~(XBF_ASYNC | XBF_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001029 xfs_buf_delwri_dequeue(bp);
Christoph Hellwig939d7232010-07-20 17:51:16 +10001030 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Christoph Hellwig8c383662010-03-12 10:59:40 +00001032 error = xfs_buf_iowait(bp);
1033 if (error)
1034 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1035 xfs_buf_relse(bp);
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001036 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001037}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001039void
1040xfs_bdwrite(
1041 void *mp,
1042 struct xfs_buf *bp)
1043{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001044 trace_xfs_buf_bdwrite(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001046 bp->b_flags &= ~XBF_READ;
1047 bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
1048
1049 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Christoph Hellwig4e234712010-01-13 22:17:56 +00001052/*
1053 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001054 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001055 * so that the proper iodone callbacks get called.
1056 */
1057STATIC int
1058xfs_bioerror(
1059 xfs_buf_t *bp)
1060{
1061#ifdef XFSERRORDEBUG
1062 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1063#endif
1064
1065 /*
1066 * No need to wait until the buffer is unpinned, we aren't flushing it.
1067 */
1068 XFS_BUF_ERROR(bp, EIO);
1069
1070 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001071 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001072 */
1073 XFS_BUF_UNREAD(bp);
1074 XFS_BUF_UNDELAYWRITE(bp);
1075 XFS_BUF_UNDONE(bp);
1076 XFS_BUF_STALE(bp);
1077
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001078 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001079
1080 return EIO;
1081}
1082
1083/*
1084 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001085 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001086 * This is meant for userdata errors; metadata bufs come with
1087 * iodone functions attached, so that we can track down errors.
1088 */
1089STATIC int
1090xfs_bioerror_relse(
1091 struct xfs_buf *bp)
1092{
1093 int64_t fl = XFS_BUF_BFLAGS(bp);
1094 /*
1095 * No need to wait until the buffer is unpinned.
1096 * We aren't flushing it.
1097 *
1098 * chunkhold expects B_DONE to be set, whether
1099 * we actually finish the I/O or not. We don't want to
1100 * change that interface.
1101 */
1102 XFS_BUF_UNREAD(bp);
1103 XFS_BUF_UNDELAYWRITE(bp);
1104 XFS_BUF_DONE(bp);
1105 XFS_BUF_STALE(bp);
1106 XFS_BUF_CLR_IODONE_FUNC(bp);
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001107 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001108 /*
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_ERROR(bp, EIO);
1115 XFS_BUF_FINISH_IOWAIT(bp);
1116 } else {
1117 xfs_buf_relse(bp);
1118 }
1119
1120 return EIO;
1121}
1122
1123
1124/*
1125 * All xfs metadata buffers except log state machine buffers
1126 * get this attached as their b_bdstrat callback function.
1127 * This is so that we can catch a buffer
1128 * after prematurely unpinning it to forcibly shutdown the filesystem.
1129 */
1130int
1131xfs_bdstrat_cb(
1132 struct xfs_buf *bp)
1133{
Dave Chinnerebad8612010-09-22 10:47:20 +10001134 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001135 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1136 /*
1137 * Metadata write that didn't get logged but
1138 * written delayed anyway. These aren't associated
1139 * with a transaction, and can be ignored.
1140 */
1141 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1142 return xfs_bioerror_relse(bp);
1143 else
1144 return xfs_bioerror(bp);
1145 }
1146
1147 xfs_buf_iorequest(bp);
1148 return 0;
1149}
1150
1151/*
1152 * Wrapper around bdstrat so that we can stop data from going to disk in case
1153 * we are shutting down the filesystem. Typically user data goes thru this
1154 * path; one of the exceptions is the superblock.
1155 */
1156void
1157xfsbdstrat(
1158 struct xfs_mount *mp,
1159 struct xfs_buf *bp)
1160{
1161 if (XFS_FORCED_SHUTDOWN(mp)) {
1162 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1163 xfs_bioerror_relse(bp);
1164 return;
1165 }
1166
1167 xfs_buf_iorequest(bp);
1168}
1169
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001170STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001171_xfs_buf_ioend(
1172 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 int schedule)
1174{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001175 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001176 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Al Viro782e3b32007-10-12 07:17:47 +01001179STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001180xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 int error)
1183{
Nathan Scottce8e9222006-01-11 15:39:08 +11001184 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001186 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
James Bottomley73c77e22010-01-25 11:42:24 -06001188 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1189 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1190
Nathan Scottce8e9222006-01-11 15:39:08 +11001191 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
1195STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001196_xfs_buf_ioapply(
1197 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001199 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001201 int offset = bp->b_offset;
1202 int size = bp->b_count_desired;
1203 sector_t sector = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Nathan Scottce8e9222006-01-11 15:39:08 +11001205 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 map_i = 0;
1207
Nathan Scottce8e9222006-01-11 15:39:08 +11001208 if (bp->b_flags & XBF_ORDERED) {
1209 ASSERT(!(bp->b_flags & XBF_READ));
Christoph Hellwig80f6c292010-08-18 05:29:11 -04001210 rw = WRITE_FLUSH_FUA;
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001211 } else if (bp->b_flags & XBF_LOG_BUFFER) {
Nathan Scott51bdd702006-09-28 11:01:57 +10001212 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1213 bp->b_flags &= ~_XBF_RUN_QUEUES;
1214 rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
Dave Chinner2ee1aba2009-11-24 18:03:15 +00001215 } else if (bp->b_flags & _XBF_RUN_QUEUES) {
1216 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1217 bp->b_flags &= ~_XBF_RUN_QUEUES;
1218 rw = (bp->b_flags & XBF_WRITE) ? WRITE_META : READ_META;
Nathan Scott51bdd702006-09-28 11:01:57 +10001219 } else {
1220 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1221 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001222 }
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001226 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1228 if (nr_pages > total_nr_pages)
1229 nr_pages = total_nr_pages;
1230
1231 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001232 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001234 bio->bi_end_io = xfs_buf_bio_end_io;
1235 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Dave Chinner0e6e8472011-03-26 09:16:45 +11001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 for (; size && nr_pages; nr_pages--, map_i++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001239 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 if (nbytes > size)
1242 nbytes = size;
1243
Nathan Scottce8e9222006-01-11 15:39:08 +11001244 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1245 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 break;
1247
1248 offset = 0;
1249 sector += nbytes >> BBSHIFT;
1250 size -= nbytes;
1251 total_nr_pages--;
1252 }
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001255 if (xfs_buf_is_vmapped(bp)) {
1256 flush_kernel_vmap_range(bp->b_addr,
1257 xfs_buf_vmap_len(bp));
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 submit_bio(rw, bio);
1260 if (size)
1261 goto next_chunk;
1262 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001263 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001264 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268int
Nathan Scottce8e9222006-01-11 15:39:08 +11001269xfs_buf_iorequest(
1270 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001272 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Nathan Scottce8e9222006-01-11 15:39:08 +11001274 if (bp->b_flags & XBF_DELWRI) {
1275 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return 0;
1277 }
1278
Nathan Scottce8e9222006-01-11 15:39:08 +11001279 if (bp->b_flags & XBF_WRITE) {
1280 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
1282
Nathan Scottce8e9222006-01-11 15:39:08 +11001283 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 /* Set the count to 1 initially, this will stop an I/O
1286 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001287 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001289 atomic_set(&bp->b_io_remaining, 1);
1290 _xfs_buf_ioapply(bp);
1291 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Nathan Scottce8e9222006-01-11 15:39:08 +11001293 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return 0;
1295}
1296
1297/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001298 * Waits for I/O to complete on the buffer supplied.
1299 * It returns immediately if no I/O is pending.
1300 * It returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 */
1302int
Nathan Scottce8e9222006-01-11 15:39:08 +11001303xfs_buf_iowait(
1304 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001306 trace_xfs_buf_iowait(bp, _RET_IP_);
1307
Nathan Scottce8e9222006-01-11 15:39:08 +11001308 if (atomic_read(&bp->b_io_remaining))
Jens Axboe7eaceac2011-03-10 08:52:07 +01001309 blk_flush_plug(current);
David Chinnerb4dd3302008-08-13 16:36:11 +10001310 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001311
1312 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001313 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Nathan Scottce8e9222006-01-11 15:39:08 +11001316xfs_caddr_t
1317xfs_buf_offset(
1318 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 size_t offset)
1320{
1321 struct page *page;
1322
Nathan Scottce8e9222006-01-11 15:39:08 +11001323 if (bp->b_flags & XBF_MAPPED)
1324 return XFS_BUF_PTR(bp) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Nathan Scottce8e9222006-01-11 15:39:08 +11001326 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001327 page = bp->b_pages[offset >> PAGE_SHIFT];
1328 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 * Move data into or out of a buffer.
1333 */
1334void
Nathan Scottce8e9222006-01-11 15:39:08 +11001335xfs_buf_iomove(
1336 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 size_t boff, /* starting buffer offset */
1338 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001339 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001340 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 size_t bend, cpoff, csize;
1343 struct page *page;
1344
1345 bend = boff + bsize;
1346 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001347 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1348 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 csize = min_t(size_t,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001350 PAGE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Dave Chinner0e6e8472011-03-26 09:16:45 +11001352 ASSERT(((csize + cpoff) <= PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001355 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 memset(page_address(page) + cpoff, 0, csize);
1357 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001358 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 memcpy(data, page_address(page) + cpoff, csize);
1360 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001361 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 memcpy(page_address(page) + cpoff, data, csize);
1363 }
1364
1365 boff += csize;
1366 data += csize;
1367 }
1368}
1369
1370/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001371 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 */
1373
1374/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001375 * Wait for any bufs with callbacks that have been submitted but have not yet
1376 * returned. These buffers will have an elevated hold count, so wait on those
1377 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 */
1379void
1380xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001381 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Dave Chinner430cbeb2010-12-02 16:30:55 +11001383 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Dave Chinner430cbeb2010-12-02 16:30:55 +11001385restart:
1386 spin_lock(&btp->bt_lru_lock);
1387 while (!list_empty(&btp->bt_lru)) {
1388 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1389 if (atomic_read(&bp->b_hold) > 1) {
1390 spin_unlock(&btp->bt_lru_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001391 delay(100);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001392 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001394 /*
1395 * clear the LRU reference count so the bufer doesn't get
1396 * ignored in xfs_buf_rele().
1397 */
1398 atomic_set(&bp->b_lru_ref, 0);
1399 spin_unlock(&btp->bt_lru_lock);
1400 xfs_buf_rele(bp);
1401 spin_lock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001403 spin_unlock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Dave Chinnerff57ab22010-11-30 17:27:57 +11001406int
1407xfs_buftarg_shrink(
1408 struct shrinker *shrink,
1409 int nr_to_scan,
1410 gfp_t mask)
David Chinnera6867a62006-01-11 15:37:58 +11001411{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001412 struct xfs_buftarg *btp = container_of(shrink,
1413 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001414 struct xfs_buf *bp;
1415 LIST_HEAD(dispose);
1416
1417 if (!nr_to_scan)
1418 return btp->bt_lru_nr;
1419
1420 spin_lock(&btp->bt_lru_lock);
1421 while (!list_empty(&btp->bt_lru)) {
1422 if (nr_to_scan-- <= 0)
1423 break;
1424
1425 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1426
1427 /*
1428 * Decrement the b_lru_ref count unless the value is already
1429 * zero. If the value is already zero, we need to reclaim the
1430 * buffer, otherwise it gets another trip through the LRU.
1431 */
1432 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1433 list_move_tail(&bp->b_lru, &btp->bt_lru);
1434 continue;
1435 }
1436
1437 /*
1438 * remove the buffer from the LRU now to avoid needing another
1439 * lock round trip inside xfs_buf_rele().
1440 */
1441 list_move(&bp->b_lru, &dispose);
1442 btp->bt_lru_nr--;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001443 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001444 spin_unlock(&btp->bt_lru_lock);
1445
1446 while (!list_empty(&dispose)) {
1447 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1448 list_del_init(&bp->b_lru);
1449 xfs_buf_rele(bp);
1450 }
1451
1452 return btp->bt_lru_nr;
David Chinnera6867a62006-01-11 15:37:58 +11001453}
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455void
1456xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001457 struct xfs_mount *mp,
1458 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001460 unregister_shrinker(&btp->bt_shrinker);
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 xfs_flush_buftarg(btp, 1);
Christoph Hellwigb7963132009-03-03 14:48:37 -05001463 if (mp->m_flags & XFS_MOUNT_BARRIER)
1464 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001465
David Chinnera6867a62006-01-11 15:37:58 +11001466 kthread_stop(btp->bt_task);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001467 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470STATIC int
1471xfs_setsize_buftarg_flags(
1472 xfs_buftarg_t *btp,
1473 unsigned int blocksize,
1474 unsigned int sectorsize,
1475 int verbose)
1476{
Nathan Scottce8e9222006-01-11 15:39:08 +11001477 btp->bt_bsize = blocksize;
1478 btp->bt_sshift = ffs(sectorsize) - 1;
1479 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Nathan Scottce8e9222006-01-11 15:39:08 +11001481 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001482 xfs_warn(btp->bt_mount,
1483 "Cannot set_blocksize to %u on device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 sectorsize, XFS_BUFTARG_NAME(btp));
1485 return EINVAL;
1486 }
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return 0;
1489}
1490
1491/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001492 * When allocating the initial buffer target we have not yet
1493 * read in the superblock, so don't know what sized sectors
1494 * are being used is at this early stage. Play safe.
1495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496STATIC int
1497xfs_setsize_buftarg_early(
1498 xfs_buftarg_t *btp,
1499 struct block_device *bdev)
1500{
1501 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001502 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
1505int
1506xfs_setsize_buftarg(
1507 xfs_buftarg_t *btp,
1508 unsigned int blocksize,
1509 unsigned int sectorsize)
1510{
1511 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1512}
1513
1514STATIC int
David Chinnera6867a62006-01-11 15:37:58 +11001515xfs_alloc_delwrite_queue(
Jan Engelhardte2a07812010-03-23 09:52:55 +11001516 xfs_buftarg_t *btp,
1517 const char *fsname)
David Chinnera6867a62006-01-11 15:37:58 +11001518{
David Chinnera6867a62006-01-11 15:37:58 +11001519 INIT_LIST_HEAD(&btp->bt_delwrite_queue);
Eric Sandeen007c61c2007-10-11 17:43:56 +10001520 spin_lock_init(&btp->bt_delwrite_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001521 btp->bt_flags = 0;
Jan Engelhardte2a07812010-03-23 09:52:55 +11001522 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd/%s", fsname);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001523 if (IS_ERR(btp->bt_task))
1524 return PTR_ERR(btp->bt_task);
1525 return 0;
David Chinnera6867a62006-01-11 15:37:58 +11001526}
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528xfs_buftarg_t *
1529xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001530 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001532 int external,
1533 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 xfs_buftarg_t *btp;
1536
1537 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1538
Dave Chinnerebad8612010-09-22 10:47:20 +10001539 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001540 btp->bt_dev = bdev->bd_dev;
1541 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001542 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1543 if (!btp->bt_bdi)
1544 goto error;
1545
Dave Chinner430cbeb2010-12-02 16:30:55 +11001546 INIT_LIST_HEAD(&btp->bt_lru);
1547 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (xfs_setsize_buftarg_early(btp, bdev))
1549 goto error;
Jan Engelhardte2a07812010-03-23 09:52:55 +11001550 if (xfs_alloc_delwrite_queue(btp, fsname))
David Chinnera6867a62006-01-11 15:37:58 +11001551 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001552 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1553 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1554 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return btp;
1556
1557error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001558 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return NULL;
1560}
1561
1562
1563/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001564 * Delayed write buffer handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001567xfs_buf_delwri_queue(
1568 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 int unlock)
1570{
Nathan Scottce8e9222006-01-11 15:39:08 +11001571 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1572 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
David Chinnera6867a62006-01-11 15:37:58 +11001573
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001574 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1575
Nathan Scottce8e9222006-01-11 15:39:08 +11001576 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
David Chinnera6867a62006-01-11 15:37:58 +11001578 spin_lock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 /* If already in the queue, dequeue and place at tail */
Nathan Scottce8e9222006-01-11 15:39:08 +11001580 if (!list_empty(&bp->b_list)) {
1581 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1582 if (unlock)
1583 atomic_dec(&bp->b_hold);
1584 list_del(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
Dave Chinnerc9c12972010-01-11 11:49:59 +00001587 if (list_empty(dwq)) {
1588 /* start xfsbufd as it is about to have something to do */
1589 wake_up_process(bp->b_target->bt_task);
1590 }
1591
Nathan Scottce8e9222006-01-11 15:39:08 +11001592 bp->b_flags |= _XBF_DELWRI_Q;
1593 list_add_tail(&bp->b_list, dwq);
1594 bp->b_queuetime = jiffies;
David Chinnera6867a62006-01-11 15:37:58 +11001595 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 if (unlock)
Nathan Scottce8e9222006-01-11 15:39:08 +11001598 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
1601void
Nathan Scottce8e9222006-01-11 15:39:08 +11001602xfs_buf_delwri_dequeue(
1603 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Nathan Scottce8e9222006-01-11 15:39:08 +11001605 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 int dequeued = 0;
1607
David Chinnera6867a62006-01-11 15:37:58 +11001608 spin_lock(dwlk);
Nathan Scottce8e9222006-01-11 15:39:08 +11001609 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1610 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1611 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 dequeued = 1;
1613 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001614 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
David Chinnera6867a62006-01-11 15:37:58 +11001615 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 if (dequeued)
Nathan Scottce8e9222006-01-11 15:39:08 +11001618 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001620 trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
Dave Chinnerd808f612010-02-02 10:13:42 +11001623/*
1624 * If a delwri buffer needs to be pushed before it has aged out, then promote
1625 * it to the head of the delwri queue so that it will be flushed on the next
1626 * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
1627 * than the age currently needed to flush the buffer. Hence the next time the
1628 * xfsbufd sees it is guaranteed to be considered old enough to flush.
1629 */
1630void
1631xfs_buf_delwri_promote(
1632 struct xfs_buf *bp)
1633{
1634 struct xfs_buftarg *btp = bp->b_target;
1635 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
1636
1637 ASSERT(bp->b_flags & XBF_DELWRI);
1638 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1639
1640 /*
1641 * Check the buffer age before locking the delayed write queue as we
1642 * don't need to promote buffers that are already past the flush age.
1643 */
1644 if (bp->b_queuetime < jiffies - age)
1645 return;
1646 bp->b_queuetime = jiffies - age;
1647 spin_lock(&btp->bt_delwrite_lock);
1648 list_move(&bp->b_list, &btp->bt_delwrite_queue);
1649 spin_unlock(&btp->bt_delwrite_lock);
1650}
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001653xfs_buf_runall_queues(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 struct workqueue_struct *queue)
1655{
1656 flush_workqueue(queue);
1657}
1658
David Chinner585e6d82007-02-10 18:32:29 +11001659/*
1660 * Move as many buffers as specified to the supplied list
1661 * idicating if we skipped any buffers to prevent deadlocks.
1662 */
1663STATIC int
1664xfs_buf_delwri_split(
1665 xfs_buftarg_t *target,
1666 struct list_head *list,
David Chinner5e6a07d2007-02-10 18:34:49 +11001667 unsigned long age)
David Chinner585e6d82007-02-10 18:32:29 +11001668{
1669 xfs_buf_t *bp, *n;
1670 struct list_head *dwq = &target->bt_delwrite_queue;
1671 spinlock_t *dwlk = &target->bt_delwrite_lock;
1672 int skipped = 0;
David Chinner5e6a07d2007-02-10 18:34:49 +11001673 int force;
David Chinner585e6d82007-02-10 18:32:29 +11001674
David Chinner5e6a07d2007-02-10 18:34:49 +11001675 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
David Chinner585e6d82007-02-10 18:32:29 +11001676 INIT_LIST_HEAD(list);
1677 spin_lock(dwlk);
1678 list_for_each_entry_safe(bp, n, dwq, b_list) {
David Chinner585e6d82007-02-10 18:32:29 +11001679 ASSERT(bp->b_flags & XBF_DELWRI);
1680
Christoph Hellwig4d16e922010-06-23 18:11:15 +10001681 if (!XFS_BUF_ISPINNED(bp) && !xfs_buf_cond_lock(bp)) {
David Chinner5e6a07d2007-02-10 18:34:49 +11001682 if (!force &&
David Chinner585e6d82007-02-10 18:32:29 +11001683 time_before(jiffies, bp->b_queuetime + age)) {
1684 xfs_buf_unlock(bp);
1685 break;
1686 }
1687
1688 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1689 _XBF_RUN_QUEUES);
1690 bp->b_flags |= XBF_WRITE;
1691 list_move_tail(&bp->b_list, list);
Dave Chinnerbfe27412010-11-08 08:55:05 +00001692 trace_xfs_buf_delwri_split(bp, _RET_IP_);
David Chinner585e6d82007-02-10 18:32:29 +11001693 } else
1694 skipped++;
1695 }
1696 spin_unlock(dwlk);
1697
1698 return skipped;
1699
1700}
1701
Dave Chinner089716a2010-01-26 15:13:25 +11001702/*
1703 * Compare function is more complex than it needs to be because
1704 * the return value is only 32 bits and we are doing comparisons
1705 * on 64 bit values
1706 */
1707static int
1708xfs_buf_cmp(
1709 void *priv,
1710 struct list_head *a,
1711 struct list_head *b)
1712{
1713 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1714 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1715 xfs_daddr_t diff;
1716
1717 diff = ap->b_bn - bp->b_bn;
1718 if (diff < 0)
1719 return -1;
1720 if (diff > 0)
1721 return 1;
1722 return 0;
1723}
1724
1725void
1726xfs_buf_delwri_sort(
1727 xfs_buftarg_t *target,
1728 struct list_head *list)
1729{
1730 list_sort(NULL, list, xfs_buf_cmp);
1731}
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001734xfsbufd(
David Chinner585e6d82007-02-10 18:32:29 +11001735 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Dave Chinner089716a2010-01-26 15:13:25 +11001737 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 current->flags |= PF_MEMALLOC;
1740
Rafael J. Wysocki978c7b22007-12-07 14:09:02 +11001741 set_freezable();
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 do {
Dave Chinnerc9c12972010-01-11 11:49:59 +00001744 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
1745 long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
Dave Chinner089716a2010-01-26 15:13:25 +11001746 int count = 0;
1747 struct list_head tmp;
Dave Chinnerc9c12972010-01-11 11:49:59 +00001748
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001749 if (unlikely(freezing(current))) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001750 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001751 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001752 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001753 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Nathan Scottabd0cf72005-05-05 13:30:13 -07001754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Dave Chinnerc9c12972010-01-11 11:49:59 +00001756 /* sleep for a long time if there is nothing to do. */
1757 if (list_empty(&target->bt_delwrite_queue))
1758 tout = MAX_SCHEDULE_TIMEOUT;
1759 schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Dave Chinnerc9c12972010-01-11 11:49:59 +00001761 xfs_buf_delwri_split(target, &tmp, age);
Dave Chinner089716a2010-01-26 15:13:25 +11001762 list_sort(NULL, &tmp, xfs_buf_cmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 while (!list_empty(&tmp)) {
Dave Chinner089716a2010-01-26 15:13:25 +11001764 struct xfs_buf *bp;
1765 bp = list_first_entry(&tmp, struct xfs_buf, b_list);
Nathan Scottce8e9222006-01-11 15:39:08 +11001766 list_del_init(&bp->b_list);
Christoph Hellwig939d7232010-07-20 17:51:16 +10001767 xfs_bdstrat_cb(bp);
David Chinner585e6d82007-02-10 18:32:29 +11001768 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
Nathan Scottf07c2252006-09-28 10:52:15 +10001770 if (count)
Jens Axboe7eaceac2011-03-10 08:52:07 +01001771 blk_flush_plug(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001773 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001775 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
1778/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001779 * Go through all incore buffers, and release buffers if they belong to
1780 * the given device. This is used in filesystem error handling to
1781 * preserve the consistency of its metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 */
1783int
1784xfs_flush_buftarg(
David Chinner585e6d82007-02-10 18:32:29 +11001785 xfs_buftarg_t *target,
1786 int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Dave Chinner089716a2010-01-26 15:13:25 +11001788 xfs_buf_t *bp;
David Chinner585e6d82007-02-10 18:32:29 +11001789 int pincount = 0;
Dave Chinner089716a2010-01-26 15:13:25 +11001790 LIST_HEAD(tmp_list);
1791 LIST_HEAD(wait_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Dave Chinnerc626d172009-04-06 18:42:11 +02001793 xfs_buf_runall_queues(xfsconvertd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001794 xfs_buf_runall_queues(xfsdatad_workqueue);
1795 xfs_buf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
David Chinner5e6a07d2007-02-10 18:34:49 +11001797 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
Dave Chinner089716a2010-01-26 15:13:25 +11001798 pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 /*
Dave Chinner089716a2010-01-26 15:13:25 +11001801 * Dropped the delayed write list lock, now walk the temporary list.
1802 * All I/O is issued async and then if we need to wait for completion
1803 * we do that after issuing all the IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 */
Dave Chinner089716a2010-01-26 15:13:25 +11001805 list_sort(NULL, &tmp_list, xfs_buf_cmp);
1806 while (!list_empty(&tmp_list)) {
1807 bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
David Chinner585e6d82007-02-10 18:32:29 +11001808 ASSERT(target == bp->b_target);
Dave Chinner089716a2010-01-26 15:13:25 +11001809 list_del_init(&bp->b_list);
1810 if (wait) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001811 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner089716a2010-01-26 15:13:25 +11001812 list_add(&bp->b_list, &wait_list);
1813 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001814 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816
Dave Chinner089716a2010-01-26 15:13:25 +11001817 if (wait) {
1818 /* Expedite and wait for IO to complete. */
Jens Axboe7eaceac2011-03-10 08:52:07 +01001819 blk_flush_plug(current);
Dave Chinner089716a2010-01-26 15:13:25 +11001820 while (!list_empty(&wait_list)) {
1821 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Nathan Scottf07c2252006-09-28 10:52:15 +10001822
Dave Chinner089716a2010-01-26 15:13:25 +11001823 list_del_init(&bp->b_list);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001824 xfs_buf_iowait(bp);
Dave Chinner089716a2010-01-26 15:13:25 +11001825 xfs_buf_relse(bp);
1826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return pincount;
1830}
1831
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001832int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001833xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Nathan Scott87582802006-03-14 13:18:19 +11001835 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1836 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001837 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001838 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001839
Dave Chinner51749e42010-09-08 09:00:22 +00001840 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001841 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001842 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001843 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Tejun Heo83e75902011-02-01 11:42:43 +01001845 xfsdatad_workqueue = alloc_workqueue("xfsdatad", WQ_MEM_RECLAIM, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001846 if (!xfsdatad_workqueue)
1847 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Tejun Heo83e75902011-02-01 11:42:43 +01001849 xfsconvertd_workqueue = alloc_workqueue("xfsconvertd",
1850 WQ_MEM_RECLAIM, 1);
Dave Chinnerc626d172009-04-06 18:42:11 +02001851 if (!xfsconvertd_workqueue)
1852 goto out_destroy_xfsdatad_workqueue;
1853
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Dave Chinnerc626d172009-04-06 18:42:11 +02001856 out_destroy_xfsdatad_workqueue:
1857 destroy_workqueue(xfsdatad_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001858 out_destroy_xfslogd_workqueue:
1859 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001860 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001861 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001862 out:
Nathan Scott87582802006-03-14 13:18:19 +11001863 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866void
Nathan Scottce8e9222006-01-11 15:39:08 +11001867xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Dave Chinnerc626d172009-04-06 18:42:11 +02001869 destroy_workqueue(xfsconvertd_workqueue);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001870 destroy_workqueue(xfsdatad_workqueue);
1871 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001872 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001874
1875#ifdef CONFIG_KDB_MODULES
1876struct list_head *
1877xfs_get_buftarg_list(void)
1878{
1879 return &xfs_buftarg_list;
1880}
1881#endif