blob: d3c2b58d7d702523445bf06a5e76aa12d11f2db6 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Christoph Hellwigb7963132009-03-03 14:48:37 -050037#include "xfs_sb.h"
38#include "xfs_inum.h"
Dave Chinnered3b4d62010-05-21 12:07:08 +100039#include "xfs_log.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050040#include "xfs_ag.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050041#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050043
David Chinner7989cb82007-02-10 18:34:56 +110044static kmem_zone_t *xfs_buf_zone;
David Chinnera6867a62006-01-11 15:37:58 +110045STATIC int xfsbufd(void *);
Christoph Hellwig23ea4032005-06-21 15:14:01 +100046
David Chinner7989cb82007-02-10 18:34:56 +110047static struct workqueue_struct *xfslogd_workqueue;
Christoph Hellwig0829c362005-09-02 16:58:49 +100048struct workqueue_struct *xfsdatad_workqueue;
Dave Chinnerc626d172009-04-06 18:42:11 +020049struct workqueue_struct *xfsconvertd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Nathan Scottce8e9222006-01-11 15:39:08 +110051#ifdef XFS_BUF_LOCK_TRACKING
52# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
53# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
54# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#else
Nathan Scottce8e9222006-01-11 15:39:08 +110056# define XB_SET_OWNER(bp) do { } while (0)
57# define XB_CLEAR_OWNER(bp) do { } while (0)
58# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
Nathan Scottce8e9222006-01-11 15:39:08 +110061#define xb_to_gfp(flags) \
62 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
63 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Nathan Scottce8e9222006-01-11 15:39:08 +110065#define xb_to_km(flags) \
66 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Nathan Scottce8e9222006-01-11 15:39:08 +110068#define xfs_buf_allocate(flags) \
69 kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
70#define xfs_buf_deallocate(bp) \
71 kmem_zone_free(xfs_buf_zone, (bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
James Bottomley73c77e22010-01-25 11:42:24 -060073static inline int
74xfs_buf_is_vmapped(
75 struct xfs_buf *bp)
76{
77 /*
78 * Return true if the buffer is vmapped.
79 *
80 * The XBF_MAPPED flag is set if the buffer should be mapped, but the
81 * code is clever enough to know it doesn't have to map a single page,
82 * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
83 */
84 return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
85}
86
87static inline int
88xfs_buf_vmap_len(
89 struct xfs_buf *bp)
90{
91 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
92}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
Dave Chinner430cbeb2010-12-02 16:30:55 +110095 * xfs_buf_lru_add - add a buffer to the LRU.
96 *
97 * The LRU takes a new reference to the buffer so that it will only be freed
98 * once the shrinker takes the buffer off the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Dave Chinner430cbeb2010-12-02 16:30:55 +1100100STATIC void
101xfs_buf_lru_add(
102 struct xfs_buf *bp)
103{
104 struct xfs_buftarg *btp = bp->b_target;
105
106 spin_lock(&btp->bt_lru_lock);
107 if (list_empty(&bp->b_lru)) {
108 atomic_inc(&bp->b_hold);
109 list_add_tail(&bp->b_lru, &btp->bt_lru);
110 btp->bt_lru_nr++;
111 }
112 spin_unlock(&btp->bt_lru_lock);
113}
114
115/*
116 * xfs_buf_lru_del - remove a buffer from the LRU
117 *
118 * The unlocked check is safe here because it only occurs when there are not
119 * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there
120 * to optimise the shrinker removing the buffer from the LRU and calling
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300121 * xfs_buf_free(). i.e. it removes an unnecessary round trip on the
Dave Chinner430cbeb2010-12-02 16:30:55 +1100122 * bt_lru_lock.
123 */
124STATIC void
125xfs_buf_lru_del(
126 struct xfs_buf *bp)
127{
128 struct xfs_buftarg *btp = bp->b_target;
129
130 if (list_empty(&bp->b_lru))
131 return;
132
133 spin_lock(&btp->bt_lru_lock);
134 if (!list_empty(&bp->b_lru)) {
135 list_del_init(&bp->b_lru);
136 btp->bt_lru_nr--;
137 }
138 spin_unlock(&btp->bt_lru_lock);
139}
140
141/*
142 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
143 * b_lru_ref count so that the buffer is freed immediately when the buffer
144 * reference count falls to zero. If the buffer is already on the LRU, we need
145 * to remove the reference that LRU holds on the buffer.
146 *
147 * This prevents build-up of stale buffers on the LRU.
148 */
149void
150xfs_buf_stale(
151 struct xfs_buf *bp)
152{
153 bp->b_flags |= XBF_STALE;
154 atomic_set(&(bp)->b_lru_ref, 0);
155 if (!list_empty(&bp->b_lru)) {
156 struct xfs_buftarg *btp = bp->b_target;
157
158 spin_lock(&btp->bt_lru_lock);
159 if (!list_empty(&bp->b_lru)) {
160 list_del_init(&bp->b_lru);
161 btp->bt_lru_nr--;
162 atomic_dec(&bp->b_hold);
163 }
164 spin_unlock(&btp->bt_lru_lock);
165 }
166 ASSERT(atomic_read(&bp->b_hold) >= 1);
167}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100170_xfs_buf_initialize(
171 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100173 xfs_off_t range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 size_t range_length,
Nathan Scottce8e9222006-01-11 15:39:08 +1100175 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100178 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100180 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Nathan Scottce8e9222006-01-11 15:39:08 +1100182 memset(bp, 0, sizeof(xfs_buf_t));
183 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100184 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000185 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100186 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100187 INIT_LIST_HEAD(&bp->b_list);
Dave Chinner74f75a02010-09-24 19:59:04 +1000188 RB_CLEAR_NODE(&bp->b_rbnode);
Thomas Gleixnera731cd12010-09-07 14:33:15 +0000189 sema_init(&bp->b_sema, 0); /* held, no waiters */
Nathan Scottce8e9222006-01-11 15:39:08 +1100190 XB_SET_OWNER(bp);
191 bp->b_target = target;
192 bp->b_file_offset = range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /*
194 * Set buffer_length and count_desired to the same value initially.
195 * I/O routines should use count_desired, which will be the same in
196 * most cases but may be reset (e.g. XFS recovery).
197 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100198 bp->b_buffer_length = bp->b_count_desired = range_length;
199 bp->b_flags = flags;
200 bp->b_bn = XFS_BUF_DADDR_NULL;
201 atomic_set(&bp->b_pin_count, 0);
202 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Nathan Scottce8e9222006-01-11 15:39:08 +1100204 XFS_STATS_INC(xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000205
206 trace_xfs_buf_init(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100210 * Allocate a page array capable of holding a specified number
211 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 */
213STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100214_xfs_buf_get_pages(
215 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100217 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100220 if (bp->b_pages == NULL) {
221 bp->b_offset = xfs_buf_poff(bp->b_file_offset);
222 bp->b_page_count = page_count;
223 if (page_count <= XB_PAGES) {
224 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100226 bp->b_pages = kmem_alloc(sizeof(struct page *) *
227 page_count, xb_to_km(flags));
228 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -ENOMEM;
230 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100231 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 return 0;
234}
235
236/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100237 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
239STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100240_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 xfs_buf_t *bp)
242{
Nathan Scottce8e9222006-01-11 15:39:08 +1100243 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000244 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000245 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247}
248
249/*
250 * Releases the specified buffer.
251 *
252 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100253 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 * hashed and refcounted buffers
255 */
256void
Nathan Scottce8e9222006-01-11 15:39:08 +1100257xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 xfs_buf_t *bp)
259{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000260 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Dave Chinner430cbeb2010-12-02 16:30:55 +1100262 ASSERT(list_empty(&bp->b_lru));
263
Dave Chinner0e6e8472011-03-26 09:16:45 +1100264 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 uint i;
266
James Bottomley73c77e22010-01-25 11:42:24 -0600267 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000268 vm_unmap_ram(bp->b_addr - bp->b_offset,
269 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Nathan Scott948ecdb2006-09-28 11:03:13 +1000271 for (i = 0; i < bp->b_page_count; i++) {
272 struct page *page = bp->b_pages[i];
273
Dave Chinner0e6e8472011-03-26 09:16:45 +1100274 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000275 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100276 } else if (bp->b_flags & _XBF_KMEM)
277 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000278 _xfs_buf_free_pages(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +1100279 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100283 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
285STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100286xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 xfs_buf_t *bp,
288 uint flags)
289{
Nathan Scottce8e9222006-01-11 15:39:08 +1100290 size_t size = bp->b_count_desired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100292 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 unsigned short page_count, i;
Nathan Scott204ab252006-01-11 20:50:22 +1100294 xfs_off_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int error;
296
Dave Chinner0e6e8472011-03-26 09:16:45 +1100297 /*
298 * for buffers that are contained within a single page, just allocate
299 * the memory from the heap - there's no need for the complexity of
300 * page arrays to keep allocation down to order 0.
301 */
302 if (bp->b_buffer_length < PAGE_SIZE) {
303 bp->b_addr = kmem_alloc(bp->b_buffer_length, xb_to_km(flags));
304 if (!bp->b_addr) {
305 /* low memory - use alloc_page loop instead */
306 goto use_alloc_page;
307 }
308
309 if (((unsigned long)(bp->b_addr + bp->b_buffer_length - 1) &
310 PAGE_MASK) !=
311 ((unsigned long)bp->b_addr & PAGE_MASK)) {
312 /* b_addr spans two pages - use alloc_page instead */
313 kmem_free(bp->b_addr);
314 bp->b_addr = NULL;
315 goto use_alloc_page;
316 }
317 bp->b_offset = offset_in_page(bp->b_addr);
318 bp->b_pages = bp->b_page_array;
319 bp->b_pages[0] = virt_to_page(bp->b_addr);
320 bp->b_page_count = 1;
321 bp->b_flags |= XBF_MAPPED | _XBF_KMEM;
322 return 0;
323 }
324
325use_alloc_page:
Nathan Scottce8e9222006-01-11 15:39:08 +1100326 end = bp->b_file_offset + bp->b_buffer_length;
327 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
Nathan Scottce8e9222006-01-11 15:39:08 +1100328 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (unlikely(error))
330 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Nathan Scottce8e9222006-01-11 15:39:08 +1100332 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100333 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Nathan Scottce8e9222006-01-11 15:39:08 +1100335 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct page *page;
337 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100338retry:
339 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100341 if (flags & XBF_READ_AHEAD) {
342 bp->b_page_count = i;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100343 error = ENOMEM;
344 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
347 /*
348 * This could deadlock.
349 *
350 * But until all the XFS lowlevel code is revamped to
351 * handle buffer allocation failures we can't do much.
352 */
353 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100354 xfs_err(NULL,
355 "possible memory allocation deadlock in %s (mode:0x%x)",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000356 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Nathan Scottce8e9222006-01-11 15:39:08 +1100358 XFS_STATS_INC(xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200359 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 goto retry;
361 }
362
Nathan Scottce8e9222006-01-11 15:39:08 +1100363 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Dave Chinner0e6e8472011-03-26 09:16:45 +1100365 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100367 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 offset = 0;
369 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Dave Chinner0e6e8472011-03-26 09:16:45 +1100372out_free_pages:
373 for (i = 0; i < bp->b_page_count; i++)
374 __free_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return error;
376}
377
378/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300379 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
381STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100382_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 xfs_buf_t *bp,
384 uint flags)
385{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100386 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100387 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100388 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100389 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
390 bp->b_flags |= XBF_MAPPED;
391 } else if (flags & XBF_MAPPED) {
Dave Chinnera19fb382011-03-26 09:13:42 +1100392 int retried = 0;
393
394 do {
395 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
396 -1, PAGE_KERNEL);
397 if (bp->b_addr)
398 break;
399 vm_unmap_aliases();
400 } while (retried++ <= 1);
401
402 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100404 bp->b_addr += bp->b_offset;
405 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
408 return 0;
409}
410
411/*
412 * Finding and Reading Buffers
413 */
414
415/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100416 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * a given range of an inode. The buffer is returned
418 * locked. If other overlapping buffers exist, they are
419 * released before the new buffer is created and locked,
420 * which may imply that this call will block until those buffers
421 * are unlocked. No I/O is implied by this call.
422 */
423xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100424_xfs_buf_find(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 xfs_buftarg_t *btp, /* block device target */
Nathan Scott204ab252006-01-11 20:50:22 +1100426 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100428 xfs_buf_flags_t flags,
429 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Nathan Scott204ab252006-01-11 20:50:22 +1100431 xfs_off_t range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 size_t range_length;
Dave Chinner74f75a02010-09-24 19:59:04 +1000433 struct xfs_perag *pag;
434 struct rb_node **rbp;
435 struct rb_node *parent;
436 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 range_base = (ioff << BBSHIFT);
439 range_length = (isize << BBSHIFT);
440
441 /* Check for IOs smaller than the sector size / not sector aligned */
Nathan Scottce8e9222006-01-11 15:39:08 +1100442 ASSERT(!(range_length < (1 << btp->bt_sshift)));
Nathan Scott204ab252006-01-11 20:50:22 +1100443 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dave Chinner74f75a02010-09-24 19:59:04 +1000445 /* get tree root */
446 pag = xfs_perag_get(btp->bt_mount,
447 xfs_daddr_to_agno(btp->bt_mount, ioff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Dave Chinner74f75a02010-09-24 19:59:04 +1000449 /* walk tree */
450 spin_lock(&pag->pag_buf_lock);
451 rbp = &pag->pag_buf_tree.rb_node;
452 parent = NULL;
453 bp = NULL;
454 while (*rbp) {
455 parent = *rbp;
456 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Dave Chinner74f75a02010-09-24 19:59:04 +1000458 if (range_base < bp->b_file_offset)
459 rbp = &(*rbp)->rb_left;
460 else if (range_base > bp->b_file_offset)
461 rbp = &(*rbp)->rb_right;
462 else {
463 /*
464 * found a block offset match. If the range doesn't
465 * match, the only way this is allowed is if the buffer
466 * in the cache is stale and the transaction that made
467 * it stale has not yet committed. i.e. we are
468 * reallocating a busy extent. Skip this buffer and
469 * continue searching to the right for an exact match.
470 */
471 if (bp->b_buffer_length != range_length) {
472 ASSERT(bp->b_flags & XBF_STALE);
473 rbp = &(*rbp)->rb_right;
474 continue;
475 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100476 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 goto found;
478 }
479 }
480
481 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100482 if (new_bp) {
483 _xfs_buf_initialize(new_bp, btp, range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 range_length, flags);
Dave Chinner74f75a02010-09-24 19:59:04 +1000485 rb_link_node(&new_bp->b_rbnode, parent, rbp);
486 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
487 /* the buffer keeps the perag reference until it is freed */
488 new_bp->b_pag = pag;
489 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100491 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000492 spin_unlock(&pag->pag_buf_lock);
493 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100495 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000498 spin_unlock(&pag->pag_buf_lock);
499 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200501 if (!xfs_buf_trylock(bp)) {
502 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100503 xfs_buf_rele(bp);
504 XFS_STATS_INC(xb_busy_locked);
505 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200507 xfs_buf_lock(bp);
508 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
Dave Chinner0e6e8472011-03-26 09:16:45 +1100511 /*
512 * if the buffer is stale, clear all the external state associated with
513 * it. We need to keep flags such as how we allocated the buffer memory
514 * intact here.
515 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100516 if (bp->b_flags & XBF_STALE) {
517 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100518 bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
David Chinner2f926582005-09-05 08:33:35 +1000519 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000520
521 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100522 XFS_STATS_INC(xb_get_locked);
523 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100527 * Assembles a buffer covering the specified range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * Storage in memory for all portions of the buffer will be allocated,
529 * although backing storage may not be.
530 */
531xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000532xfs_buf_get(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100534 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100536 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Nathan Scottce8e9222006-01-11 15:39:08 +1100538 xfs_buf_t *bp, *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100539 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Nathan Scottce8e9222006-01-11 15:39:08 +1100541 new_bp = xfs_buf_allocate(flags);
542 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return NULL;
544
Nathan Scottce8e9222006-01-11 15:39:08 +1100545 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
546 if (bp == new_bp) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100547 error = xfs_buf_allocate_memory(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (error)
549 goto no_buffer;
550 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100551 xfs_buf_deallocate(new_bp);
552 if (unlikely(bp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return NULL;
554 }
555
Nathan Scottce8e9222006-01-11 15:39:08 +1100556 if (!(bp->b_flags & XBF_MAPPED)) {
557 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100559 xfs_warn(target->bt_mount,
560 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 goto no_buffer;
562 }
563 }
564
Nathan Scottce8e9222006-01-11 15:39:08 +1100565 XFS_STATS_INC(xb_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /*
568 * Always fill in the block number now, the mapped cases can do
569 * their own overlay of this later.
570 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100571 bp->b_bn = ioff;
572 bp->b_count_desired = bp->b_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000574 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100575 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100578 if (flags & (XBF_LOCK | XBF_TRYLOCK))
579 xfs_buf_unlock(bp);
580 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return NULL;
582}
583
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100584STATIC int
585_xfs_buf_read(
586 xfs_buf_t *bp,
587 xfs_buf_flags_t flags)
588{
589 int status;
590
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100591 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
592 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
593
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200594 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | XBF_READ_AHEAD);
595 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100596
597 status = xfs_buf_iorequest(bp);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000598 if (status || bp->b_error || (flags & XBF_ASYNC))
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000599 return status;
600 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000604xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100606 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100608 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Nathan Scottce8e9222006-01-11 15:39:08 +1100610 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Nathan Scottce8e9222006-01-11 15:39:08 +1100612 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000614 bp = xfs_buf_get(target, ioff, isize, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100615 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000616 trace_xfs_buf_read(bp, flags, _RET_IP_);
617
Nathan Scottce8e9222006-01-11 15:39:08 +1100618 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100619 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100620 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100621 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
623 * Read ahead call which is already satisfied,
624 * drop the buffer
625 */
626 goto no_buffer;
627 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100629 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631 }
632
Nathan Scottce8e9222006-01-11 15:39:08 +1100633 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100636 if (flags & (XBF_LOCK | XBF_TRYLOCK))
637 xfs_buf_unlock(bp);
638 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return NULL;
640}
641
642/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100643 * If we are not low on memory then do the readahead in a deadlock
644 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 */
646void
Nathan Scottce8e9222006-01-11 15:39:08 +1100647xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100649 xfs_off_t ioff,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000650 size_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100652 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return;
654
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000655 xfs_buf_read(target, ioff, isize,
656 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Dave Chinner5adc94c2010-09-24 21:58:31 +1000659/*
660 * Read an uncached buffer from disk. Allocates and returns a locked
661 * buffer containing the disk contents or nothing.
662 */
663struct xfs_buf *
664xfs_buf_read_uncached(
665 struct xfs_mount *mp,
666 struct xfs_buftarg *target,
667 xfs_daddr_t daddr,
668 size_t length,
669 int flags)
670{
671 xfs_buf_t *bp;
672 int error;
673
674 bp = xfs_buf_get_uncached(target, length, flags);
675 if (!bp)
676 return NULL;
677
678 /* set up the buffer for a read IO */
Dave Chinner5adc94c2010-09-24 21:58:31 +1000679 XFS_BUF_SET_ADDR(bp, daddr);
680 XFS_BUF_READ(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000681
682 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000683 error = xfs_buf_iowait(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000684 if (error || bp->b_error) {
685 xfs_buf_relse(bp);
686 return NULL;
687 }
688 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100692xfs_buf_get_empty(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 size_t len,
694 xfs_buftarg_t *target)
695{
Nathan Scottce8e9222006-01-11 15:39:08 +1100696 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Nathan Scottce8e9222006-01-11 15:39:08 +1100698 bp = xfs_buf_allocate(0);
699 if (bp)
700 _xfs_buf_initialize(bp, target, 0, len, 0);
701 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Dave Chinner44396472011-04-21 09:34:27 +0000704/*
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 */
708void
709xfs_buf_set_empty(
710 struct xfs_buf *bp,
711 size_t len)
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_file_offset = 0;
720 bp->b_buffer_length = bp->b_count_desired = len;
721 bp->b_bn = XFS_BUF_DADDR_NULL;
722 bp->b_flags &= ~XBF_MAPPED;
723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725static inline struct page *
726mem_to_page(
727 void *addr)
728{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800729 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return virt_to_page(addr);
731 } else {
732 return vmalloc_to_page(addr);
733 }
734}
735
736int
Nathan Scottce8e9222006-01-11 15:39:08 +1100737xfs_buf_associate_memory(
738 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 void *mem,
740 size_t len)
741{
742 int rval;
743 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100744 unsigned long pageaddr;
745 unsigned long offset;
746 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 int page_count;
748
Dave Chinner0e6e8472011-03-26 09:16:45 +1100749 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100750 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100751 buflen = PAGE_ALIGN(len + offset);
752 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100755 if (bp->b_pages)
756 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Nathan Scottce8e9222006-01-11 15:39:08 +1100758 bp->b_pages = NULL;
759 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Christoph Hellwig36fae172009-07-18 18:14:58 -0400761 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (rval)
763 return rval;
764
Nathan Scottce8e9222006-01-11 15:39:08 +1100765 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100767 for (i = 0; i < bp->b_page_count; i++) {
768 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100769 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100772 bp->b_count_desired = len;
773 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100774 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 return 0;
777}
778
779xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000780xfs_buf_get_uncached(
781 struct xfs_buftarg *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 size_t len,
Dave Chinner686865f2010-09-24 20:07:47 +1000783 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000785 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
786 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Nathan Scottce8e9222006-01-11 15:39:08 +1100789 bp = xfs_buf_allocate(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (unlikely(bp == NULL))
791 goto fail;
Nathan Scottce8e9222006-01-11 15:39:08 +1100792 _xfs_buf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000794 error = _xfs_buf_get_pages(bp, page_count, 0);
795 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 goto fail_free_buf;
797
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000798 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000799 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000800 if (!bp->b_pages[i])
801 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000803 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000805 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
806 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100807 xfs_warn(target->bt_mount,
808 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Dave Chinner686865f2010-09-24 20:07:47 +1000812 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000816 while (--i >= 0)
817 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000818 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 fail_free_buf:
Christoph Hellwigca165b82007-05-24 15:21:11 +1000820 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 fail:
822 return NULL;
823}
824
825/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * Increment reference count on buffer, to hold the buffer concurrently
827 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 * Must hold the buffer already to call this function.
829 */
830void
Nathan Scottce8e9222006-01-11 15:39:08 +1100831xfs_buf_hold(
832 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000834 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100835 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100839 * Releases a hold on the specified buffer. If the
840 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 */
842void
Nathan Scottce8e9222006-01-11 15:39:08 +1100843xfs_buf_rele(
844 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Dave Chinner74f75a02010-09-24 19:59:04 +1000846 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000848 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Dave Chinner74f75a02010-09-24 19:59:04 +1000850 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100851 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000852 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100853 if (atomic_dec_and_test(&bp->b_hold))
854 xfs_buf_free(bp);
855 return;
856 }
857
Dave Chinner74f75a02010-09-24 19:59:04 +1000858 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100859
Lachlan McIlroy37906892008-08-13 15:42:10 +1000860 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000861 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000862 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100863 atomic_read(&bp->b_lru_ref)) {
864 xfs_buf_lru_add(bp);
865 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100866 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100867 xfs_buf_lru_del(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +1100868 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
Dave Chinner74f75a02010-09-24 19:59:04 +1000869 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
870 spin_unlock(&pag->pag_buf_lock);
871 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100872 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874 }
875}
876
877
878/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100879 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100880 *
881 * If we come across a stale, pinned, locked buffer, we know that we are
882 * being asked to lock a buffer that has been reallocated. Because it is
883 * pinned, we know that the log has not been pushed to disk and hence it
884 * will still be locked. Rather than continuing to have trylock attempts
885 * fail until someone else pushes the log, push it ourselves before
886 * returning. This means that the xfsaild will not get stuck trying
887 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
889int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200890xfs_buf_trylock(
891 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 int locked;
894
Nathan Scottce8e9222006-01-11 15:39:08 +1100895 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000896 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100897 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100898 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
899 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000900
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200901 trace_xfs_buf_trylock(bp, _RET_IP_);
902 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100906 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000907 *
908 * If we come across a stale, pinned, locked buffer, we know that we
909 * are being asked to lock a buffer that has been reallocated. Because
910 * it is pinned, we know that the log has not been pushed to disk and
911 * hence it will still be locked. Rather than sleeping until someone
912 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100914void
915xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200916 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000918 trace_xfs_buf_lock(bp, _RET_IP_);
919
Dave Chinnered3b4d62010-05-21 12:07:08 +1000920 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000921 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100922 down(&bp->b_sema);
923 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000924
925 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
928/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100929 * Releases the lock on the buffer object.
David Chinner2f926582005-09-05 08:33:35 +1000930 * If the buffer is marked delwri but is not queued, do so before we
Nathan Scottce8e9222006-01-11 15:39:08 +1100931 * unlock the buffer as we need to set flags correctly. We also need to
David Chinner2f926582005-09-05 08:33:35 +1000932 * take a reference for the delwri queue because the unlocker is going to
933 * drop their's and they don't know we just queued it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 */
935void
Nathan Scottce8e9222006-01-11 15:39:08 +1100936xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200937 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Nathan Scottce8e9222006-01-11 15:39:08 +1100939 XB_CLEAR_OWNER(bp);
940 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000941
942 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Nathan Scottce8e9222006-01-11 15:39:08 +1100945STATIC void
946xfs_buf_wait_unpin(
947 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 DECLARE_WAITQUEUE (wait, current);
950
Nathan Scottce8e9222006-01-11 15:39:08 +1100951 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return;
953
Nathan Scottce8e9222006-01-11 15:39:08 +1100954 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 for (;;) {
956 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100957 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100959 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100961 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 set_current_state(TASK_RUNNING);
963}
964
965/*
966 * Buffer Utility Routines
967 */
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100970xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000971 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
David Howellsc4028952006-11-22 14:57:56 +0000973 xfs_buf_t *bp =
974 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400976 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100977 (*(bp->b_iodone))(bp);
978 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 xfs_buf_relse(bp);
980}
981
982void
Nathan Scottce8e9222006-01-11 15:39:08 +1100983xfs_buf_ioend(
984 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int schedule)
986{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000987 trace_xfs_buf_iodone(bp, _RET_IP_);
988
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100989 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100990 if (bp->b_error == 0)
991 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Nathan Scottce8e9222006-01-11 15:39:08 +1100993 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000995 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100996 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 } else {
David Howellsc4028952006-11-22 14:57:56 +0000998 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +10001001 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003}
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005void
Nathan Scottce8e9222006-01-11 15:39:08 +11001006xfs_buf_ioerror(
1007 xfs_buf_t *bp,
1008 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001011 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001012 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001016xfs_bwrite(
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001017 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001019 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001021 bp->b_flags |= XBF_WRITE;
Christoph Hellwig8c383662010-03-12 10:59:40 +00001022 bp->b_flags &= ~(XBF_ASYNC | XBF_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001024 xfs_buf_delwri_dequeue(bp);
Christoph Hellwig939d7232010-07-20 17:51:16 +10001025 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Christoph Hellwig8c383662010-03-12 10:59:40 +00001027 error = xfs_buf_iowait(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001028 if (error) {
1029 xfs_force_shutdown(bp->b_target->bt_mount,
1030 SHUTDOWN_META_IO_ERROR);
1031 }
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001032 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001033}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Christoph Hellwig4e234712010-01-13 22:17:56 +00001035/*
1036 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001037 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001038 * so that the proper iodone callbacks get called.
1039 */
1040STATIC int
1041xfs_bioerror(
1042 xfs_buf_t *bp)
1043{
1044#ifdef XFSERRORDEBUG
1045 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1046#endif
1047
1048 /*
1049 * No need to wait until the buffer is unpinned, we aren't flushing it.
1050 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001051 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001052
1053 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001054 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001055 */
1056 XFS_BUF_UNREAD(bp);
Christoph Hellwig61551f12011-08-23 08:28:06 +00001057 xfs_buf_delwri_dequeue(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001058 XFS_BUF_UNDONE(bp);
1059 XFS_BUF_STALE(bp);
1060
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001061 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001062
1063 return EIO;
1064}
1065
1066/*
1067 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001068 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001069 * This is meant for userdata errors; metadata bufs come with
1070 * iodone functions attached, so that we can track down errors.
1071 */
1072STATIC int
1073xfs_bioerror_relse(
1074 struct xfs_buf *bp)
1075{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001076 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001077 /*
1078 * No need to wait until the buffer is unpinned.
1079 * We aren't flushing it.
1080 *
1081 * chunkhold expects B_DONE to be set, whether
1082 * we actually finish the I/O or not. We don't want to
1083 * change that interface.
1084 */
1085 XFS_BUF_UNREAD(bp);
Christoph Hellwig61551f12011-08-23 08:28:06 +00001086 xfs_buf_delwri_dequeue(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001087 XFS_BUF_DONE(bp);
1088 XFS_BUF_STALE(bp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001089 bp->b_iodone = NULL;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001090 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001091 /*
1092 * Mark b_error and B_ERROR _both_.
1093 * Lot's of chunkcache code assumes that.
1094 * There's no reason to mark error for
1095 * ASYNC buffers.
1096 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001097 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001098 XFS_BUF_FINISH_IOWAIT(bp);
1099 } else {
1100 xfs_buf_relse(bp);
1101 }
1102
1103 return EIO;
1104}
1105
1106
1107/*
1108 * All xfs metadata buffers except log state machine buffers
1109 * get this attached as their b_bdstrat callback function.
1110 * This is so that we can catch a buffer
1111 * after prematurely unpinning it to forcibly shutdown the filesystem.
1112 */
1113int
1114xfs_bdstrat_cb(
1115 struct xfs_buf *bp)
1116{
Dave Chinnerebad8612010-09-22 10:47:20 +10001117 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001118 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1119 /*
1120 * Metadata write that didn't get logged but
1121 * written delayed anyway. These aren't associated
1122 * with a transaction, and can be ignored.
1123 */
1124 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1125 return xfs_bioerror_relse(bp);
1126 else
1127 return xfs_bioerror(bp);
1128 }
1129
1130 xfs_buf_iorequest(bp);
1131 return 0;
1132}
1133
1134/*
1135 * Wrapper around bdstrat so that we can stop data from going to disk in case
1136 * we are shutting down the filesystem. Typically user data goes thru this
1137 * path; one of the exceptions is the superblock.
1138 */
1139void
1140xfsbdstrat(
1141 struct xfs_mount *mp,
1142 struct xfs_buf *bp)
1143{
1144 if (XFS_FORCED_SHUTDOWN(mp)) {
1145 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1146 xfs_bioerror_relse(bp);
1147 return;
1148 }
1149
1150 xfs_buf_iorequest(bp);
1151}
1152
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001153STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001154_xfs_buf_ioend(
1155 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 int schedule)
1157{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001158 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001159 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Al Viro782e3b32007-10-12 07:17:47 +01001162STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001163xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int error)
1166{
Nathan Scottce8e9222006-01-11 15:39:08 +11001167 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001169 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
James Bottomley73c77e22010-01-25 11:42:24 -06001171 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1172 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1173
Nathan Scottce8e9222006-01-11 15:39:08 +11001174 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
1178STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001179_xfs_buf_ioapply(
1180 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001182 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001184 int offset = bp->b_offset;
1185 int size = bp->b_count_desired;
1186 sector_t sector = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Nathan Scottce8e9222006-01-11 15:39:08 +11001188 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 map_i = 0;
1190
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001191 if (bp->b_flags & XBF_WRITE) {
1192 if (bp->b_flags & XBF_SYNCIO)
1193 rw = WRITE_SYNC;
1194 else
1195 rw = WRITE;
1196 if (bp->b_flags & XBF_FUA)
1197 rw |= REQ_FUA;
1198 if (bp->b_flags & XBF_FLUSH)
1199 rw |= REQ_FLUSH;
1200 } else if (bp->b_flags & XBF_READ_AHEAD) {
1201 rw = READA;
Nathan Scott51bdd702006-09-28 11:01:57 +10001202 } else {
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001203 rw = READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001204 }
1205
Christoph Hellwig34951f52011-07-26 15:06:44 +00001206 /* we only use the buffer cache for meta-data */
1207 rw |= REQ_META;
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001210 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1212 if (nr_pages > total_nr_pages)
1213 nr_pages = total_nr_pages;
1214
1215 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001216 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001218 bio->bi_end_io = xfs_buf_bio_end_io;
1219 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Dave Chinner0e6e8472011-03-26 09:16:45 +11001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 for (; size && nr_pages; nr_pages--, map_i++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001223 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 if (nbytes > size)
1226 nbytes = size;
1227
Nathan Scottce8e9222006-01-11 15:39:08 +11001228 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1229 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 break;
1231
1232 offset = 0;
1233 sector += nbytes >> BBSHIFT;
1234 size -= nbytes;
1235 total_nr_pages--;
1236 }
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001239 if (xfs_buf_is_vmapped(bp)) {
1240 flush_kernel_vmap_range(bp->b_addr,
1241 xfs_buf_vmap_len(bp));
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 submit_bio(rw, bio);
1244 if (size)
1245 goto next_chunk;
1246 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001247 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001248 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250}
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252int
Nathan Scottce8e9222006-01-11 15:39:08 +11001253xfs_buf_iorequest(
1254 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001256 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Christoph Hellwig375ec69d2011-08-23 08:28:03 +00001258 ASSERT(!(bp->b_flags & XBF_DELWRI));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Christoph Hellwig375ec69d2011-08-23 08:28:03 +00001260 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001261 xfs_buf_wait_unpin(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001262 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /* Set the count to 1 initially, this will stop an I/O
1265 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001266 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001268 atomic_set(&bp->b_io_remaining, 1);
1269 _xfs_buf_ioapply(bp);
1270 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Nathan Scottce8e9222006-01-11 15:39:08 +11001272 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return 0;
1274}
1275
1276/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001277 * Waits for I/O to complete on the buffer supplied.
1278 * It returns immediately if no I/O is pending.
1279 * It returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 */
1281int
Nathan Scottce8e9222006-01-11 15:39:08 +11001282xfs_buf_iowait(
1283 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001285 trace_xfs_buf_iowait(bp, _RET_IP_);
1286
David Chinnerb4dd3302008-08-13 16:36:11 +10001287 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001288
1289 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001290 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
Nathan Scottce8e9222006-01-11 15:39:08 +11001293xfs_caddr_t
1294xfs_buf_offset(
1295 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 size_t offset)
1297{
1298 struct page *page;
1299
Nathan Scottce8e9222006-01-11 15:39:08 +11001300 if (bp->b_flags & XBF_MAPPED)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001301 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Nathan Scottce8e9222006-01-11 15:39:08 +11001303 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001304 page = bp->b_pages[offset >> PAGE_SHIFT];
1305 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 * Move data into or out of a buffer.
1310 */
1311void
Nathan Scottce8e9222006-01-11 15:39:08 +11001312xfs_buf_iomove(
1313 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 size_t boff, /* starting buffer offset */
1315 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001316 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001317 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 size_t bend, cpoff, csize;
1320 struct page *page;
1321
1322 bend = boff + bsize;
1323 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001324 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1325 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 csize = min_t(size_t,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001327 PAGE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Dave Chinner0e6e8472011-03-26 09:16:45 +11001329 ASSERT(((csize + cpoff) <= PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001332 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 memset(page_address(page) + cpoff, 0, csize);
1334 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001335 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 memcpy(data, page_address(page) + cpoff, csize);
1337 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001338 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 memcpy(page_address(page) + cpoff, data, csize);
1340 }
1341
1342 boff += csize;
1343 data += csize;
1344 }
1345}
1346
1347/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001348 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 */
1350
1351/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001352 * Wait for any bufs with callbacks that have been submitted but have not yet
1353 * returned. These buffers will have an elevated hold count, so wait on those
1354 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 */
1356void
1357xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001358 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Dave Chinner430cbeb2010-12-02 16:30:55 +11001360 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Dave Chinner430cbeb2010-12-02 16:30:55 +11001362restart:
1363 spin_lock(&btp->bt_lru_lock);
1364 while (!list_empty(&btp->bt_lru)) {
1365 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1366 if (atomic_read(&bp->b_hold) > 1) {
1367 spin_unlock(&btp->bt_lru_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001368 delay(100);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001369 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001371 /*
1372 * clear the LRU reference count so the bufer doesn't get
1373 * ignored in xfs_buf_rele().
1374 */
1375 atomic_set(&bp->b_lru_ref, 0);
1376 spin_unlock(&btp->bt_lru_lock);
1377 xfs_buf_rele(bp);
1378 spin_lock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001380 spin_unlock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
Dave Chinnerff57ab22010-11-30 17:27:57 +11001383int
1384xfs_buftarg_shrink(
1385 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001386 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001387{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001388 struct xfs_buftarg *btp = container_of(shrink,
1389 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001390 struct xfs_buf *bp;
Ying Han1495f232011-05-24 17:12:27 -07001391 int nr_to_scan = sc->nr_to_scan;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001392 LIST_HEAD(dispose);
1393
1394 if (!nr_to_scan)
1395 return btp->bt_lru_nr;
1396
1397 spin_lock(&btp->bt_lru_lock);
1398 while (!list_empty(&btp->bt_lru)) {
1399 if (nr_to_scan-- <= 0)
1400 break;
1401
1402 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1403
1404 /*
1405 * Decrement the b_lru_ref count unless the value is already
1406 * zero. If the value is already zero, we need to reclaim the
1407 * buffer, otherwise it gets another trip through the LRU.
1408 */
1409 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1410 list_move_tail(&bp->b_lru, &btp->bt_lru);
1411 continue;
1412 }
1413
1414 /*
1415 * remove the buffer from the LRU now to avoid needing another
1416 * lock round trip inside xfs_buf_rele().
1417 */
1418 list_move(&bp->b_lru, &dispose);
1419 btp->bt_lru_nr--;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001420 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001421 spin_unlock(&btp->bt_lru_lock);
1422
1423 while (!list_empty(&dispose)) {
1424 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1425 list_del_init(&bp->b_lru);
1426 xfs_buf_rele(bp);
1427 }
1428
1429 return btp->bt_lru_nr;
David Chinnera6867a62006-01-11 15:37:58 +11001430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432void
1433xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001434 struct xfs_mount *mp,
1435 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001437 unregister_shrinker(&btp->bt_shrinker);
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 xfs_flush_buftarg(btp, 1);
Christoph Hellwigb7963132009-03-03 14:48:37 -05001440 if (mp->m_flags & XFS_MOUNT_BARRIER)
1441 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001442
David Chinnera6867a62006-01-11 15:37:58 +11001443 kthread_stop(btp->bt_task);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001444 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447STATIC int
1448xfs_setsize_buftarg_flags(
1449 xfs_buftarg_t *btp,
1450 unsigned int blocksize,
1451 unsigned int sectorsize,
1452 int verbose)
1453{
Nathan Scottce8e9222006-01-11 15:39:08 +11001454 btp->bt_bsize = blocksize;
1455 btp->bt_sshift = ffs(sectorsize) - 1;
1456 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Nathan Scottce8e9222006-01-11 15:39:08 +11001458 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001459 xfs_warn(btp->bt_mount,
1460 "Cannot set_blocksize to %u on device %s\n",
Chandra Seetharamanc35a5492011-07-22 23:40:46 +00001461 sectorsize, xfs_buf_target_name(btp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return EINVAL;
1463 }
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return 0;
1466}
1467
1468/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001469 * When allocating the initial buffer target we have not yet
1470 * read in the superblock, so don't know what sized sectors
1471 * are being used is at this early stage. Play safe.
1472 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473STATIC int
1474xfs_setsize_buftarg_early(
1475 xfs_buftarg_t *btp,
1476 struct block_device *bdev)
1477{
1478 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001479 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482int
1483xfs_setsize_buftarg(
1484 xfs_buftarg_t *btp,
1485 unsigned int blocksize,
1486 unsigned int sectorsize)
1487{
1488 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1489}
1490
1491STATIC int
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001492xfs_alloc_delwri_queue(
Jan Engelhardte2a07812010-03-23 09:52:55 +11001493 xfs_buftarg_t *btp,
1494 const char *fsname)
David Chinnera6867a62006-01-11 15:37:58 +11001495{
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001496 INIT_LIST_HEAD(&btp->bt_delwri_queue);
1497 spin_lock_init(&btp->bt_delwri_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001498 btp->bt_flags = 0;
Jan Engelhardte2a07812010-03-23 09:52:55 +11001499 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd/%s", fsname);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001500 if (IS_ERR(btp->bt_task))
1501 return PTR_ERR(btp->bt_task);
1502 return 0;
David Chinnera6867a62006-01-11 15:37:58 +11001503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505xfs_buftarg_t *
1506xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001507 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001509 int external,
1510 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 xfs_buftarg_t *btp;
1513
1514 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1515
Dave Chinnerebad8612010-09-22 10:47:20 +10001516 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001517 btp->bt_dev = bdev->bd_dev;
1518 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001519 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1520 if (!btp->bt_bdi)
1521 goto error;
1522
Dave Chinner430cbeb2010-12-02 16:30:55 +11001523 INIT_LIST_HEAD(&btp->bt_lru);
1524 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (xfs_setsize_buftarg_early(btp, bdev))
1526 goto error;
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001527 if (xfs_alloc_delwri_queue(btp, fsname))
David Chinnera6867a62006-01-11 15:37:58 +11001528 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001529 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1530 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1531 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return btp;
1533
1534error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001535 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return NULL;
1537}
1538
1539
1540/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001541 * Delayed write buffer handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 */
Christoph Hellwig61551f12011-08-23 08:28:06 +00001543void
Nathan Scottce8e9222006-01-11 15:39:08 +11001544xfs_buf_delwri_queue(
Christoph Hellwig527cfdf2011-08-23 08:28:04 +00001545 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001547 struct xfs_buftarg *btp = bp->b_target;
David Chinnera6867a62006-01-11 15:37:58 +11001548
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001549 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1550
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001551 ASSERT(!(bp->b_flags & XBF_READ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001553 spin_lock(&btp->bt_delwri_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +11001554 if (!list_empty(&bp->b_list)) {
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001555 /* if already in the queue, move it to the tail */
Nathan Scottce8e9222006-01-11 15:39:08 +11001556 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001557 list_move_tail(&bp->b_list, &btp->bt_delwri_queue);
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001558 } else {
Dave Chinnerc9c12972010-01-11 11:49:59 +00001559 /* start xfsbufd as it is about to have something to do */
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001560 if (list_empty(&btp->bt_delwri_queue))
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001561 wake_up_process(bp->b_target->bt_task);
Dave Chinnerc9c12972010-01-11 11:49:59 +00001562
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001563 atomic_inc(&bp->b_hold);
1564 bp->b_flags |= XBF_DELWRI | _XBF_DELWRI_Q | XBF_ASYNC;
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001565 list_add_tail(&bp->b_list, &btp->bt_delwri_queue);
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001566 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001567 bp->b_queuetime = jiffies;
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001568 spin_unlock(&btp->bt_delwri_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
1571void
Nathan Scottce8e9222006-01-11 15:39:08 +11001572xfs_buf_delwri_dequeue(
1573 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 int dequeued = 0;
1576
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001577 spin_lock(&bp->b_target->bt_delwri_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +11001578 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1579 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1580 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 dequeued = 1;
1582 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001583 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001584 spin_unlock(&bp->b_target->bt_delwri_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 if (dequeued)
Nathan Scottce8e9222006-01-11 15:39:08 +11001587 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001589 trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
Dave Chinnerd808f612010-02-02 10:13:42 +11001592/*
1593 * If a delwri buffer needs to be pushed before it has aged out, then promote
1594 * it to the head of the delwri queue so that it will be flushed on the next
1595 * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
1596 * than the age currently needed to flush the buffer. Hence the next time the
1597 * xfsbufd sees it is guaranteed to be considered old enough to flush.
1598 */
1599void
1600xfs_buf_delwri_promote(
1601 struct xfs_buf *bp)
1602{
1603 struct xfs_buftarg *btp = bp->b_target;
1604 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
1605
1606 ASSERT(bp->b_flags & XBF_DELWRI);
1607 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1608
1609 /*
1610 * Check the buffer age before locking the delayed write queue as we
1611 * don't need to promote buffers that are already past the flush age.
1612 */
1613 if (bp->b_queuetime < jiffies - age)
1614 return;
1615 bp->b_queuetime = jiffies - age;
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001616 spin_lock(&btp->bt_delwri_lock);
1617 list_move(&bp->b_list, &btp->bt_delwri_queue);
1618 spin_unlock(&btp->bt_delwri_lock);
Dave Chinnerd808f612010-02-02 10:13:42 +11001619}
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001622xfs_buf_runall_queues(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 struct workqueue_struct *queue)
1624{
1625 flush_workqueue(queue);
1626}
1627
David Chinner585e6d82007-02-10 18:32:29 +11001628/*
1629 * Move as many buffers as specified to the supplied list
1630 * idicating if we skipped any buffers to prevent deadlocks.
1631 */
1632STATIC int
1633xfs_buf_delwri_split(
1634 xfs_buftarg_t *target,
1635 struct list_head *list,
David Chinner5e6a07d2007-02-10 18:34:49 +11001636 unsigned long age)
David Chinner585e6d82007-02-10 18:32:29 +11001637{
1638 xfs_buf_t *bp, *n;
David Chinner585e6d82007-02-10 18:32:29 +11001639 int skipped = 0;
David Chinner5e6a07d2007-02-10 18:34:49 +11001640 int force;
David Chinner585e6d82007-02-10 18:32:29 +11001641
David Chinner5e6a07d2007-02-10 18:34:49 +11001642 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
David Chinner585e6d82007-02-10 18:32:29 +11001643 INIT_LIST_HEAD(list);
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001644 spin_lock(&target->bt_delwri_lock);
1645 list_for_each_entry_safe(bp, n, &target->bt_delwri_queue, b_list) {
David Chinner585e6d82007-02-10 18:32:29 +11001646 ASSERT(bp->b_flags & XBF_DELWRI);
1647
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00001648 if (!xfs_buf_ispinned(bp) && xfs_buf_trylock(bp)) {
David Chinner5e6a07d2007-02-10 18:34:49 +11001649 if (!force &&
David Chinner585e6d82007-02-10 18:32:29 +11001650 time_before(jiffies, bp->b_queuetime + age)) {
1651 xfs_buf_unlock(bp);
1652 break;
1653 }
1654
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001655 bp->b_flags &= ~(XBF_DELWRI | _XBF_DELWRI_Q);
David Chinner585e6d82007-02-10 18:32:29 +11001656 bp->b_flags |= XBF_WRITE;
1657 list_move_tail(&bp->b_list, list);
Dave Chinnerbfe27412010-11-08 08:55:05 +00001658 trace_xfs_buf_delwri_split(bp, _RET_IP_);
David Chinner585e6d82007-02-10 18:32:29 +11001659 } else
1660 skipped++;
1661 }
David Chinner585e6d82007-02-10 18:32:29 +11001662
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001663 spin_unlock(&target->bt_delwri_lock);
David Chinner585e6d82007-02-10 18:32:29 +11001664 return skipped;
David Chinner585e6d82007-02-10 18:32:29 +11001665}
1666
Dave Chinner089716a2010-01-26 15:13:25 +11001667/*
1668 * Compare function is more complex than it needs to be because
1669 * the return value is only 32 bits and we are doing comparisons
1670 * on 64 bit values
1671 */
1672static int
1673xfs_buf_cmp(
1674 void *priv,
1675 struct list_head *a,
1676 struct list_head *b)
1677{
1678 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1679 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1680 xfs_daddr_t diff;
1681
1682 diff = ap->b_bn - bp->b_bn;
1683 if (diff < 0)
1684 return -1;
1685 if (diff > 0)
1686 return 1;
1687 return 0;
1688}
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001691xfsbufd(
David Chinner585e6d82007-02-10 18:32:29 +11001692 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Dave Chinner089716a2010-01-26 15:13:25 +11001694 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 current->flags |= PF_MEMALLOC;
1697
Rafael J. Wysocki978c7b22007-12-07 14:09:02 +11001698 set_freezable();
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 do {
Dave Chinnerc9c12972010-01-11 11:49:59 +00001701 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
1702 long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
Dave Chinner089716a2010-01-26 15:13:25 +11001703 struct list_head tmp;
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001704 struct blk_plug plug;
Dave Chinnerc9c12972010-01-11 11:49:59 +00001705
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001706 if (unlikely(freezing(current))) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001707 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001708 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001709 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001710 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Nathan Scottabd0cf72005-05-05 13:30:13 -07001711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Dave Chinnerc9c12972010-01-11 11:49:59 +00001713 /* sleep for a long time if there is nothing to do. */
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001714 if (list_empty(&target->bt_delwri_queue))
Dave Chinnerc9c12972010-01-11 11:49:59 +00001715 tout = MAX_SCHEDULE_TIMEOUT;
1716 schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Dave Chinnerc9c12972010-01-11 11:49:59 +00001718 xfs_buf_delwri_split(target, &tmp, age);
Dave Chinner089716a2010-01-26 15:13:25 +11001719 list_sort(NULL, &tmp, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001720
1721 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 while (!list_empty(&tmp)) {
Dave Chinner089716a2010-01-26 15:13:25 +11001723 struct xfs_buf *bp;
1724 bp = list_first_entry(&tmp, struct xfs_buf, b_list);
Nathan Scottce8e9222006-01-11 15:39:08 +11001725 list_del_init(&bp->b_list);
Christoph Hellwig939d7232010-07-20 17:51:16 +10001726 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001728 blk_finish_plug(&plug);
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001729 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001731 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
1734/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001735 * Go through all incore buffers, and release buffers if they belong to
1736 * the given device. This is used in filesystem error handling to
1737 * preserve the consistency of its metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
1739int
1740xfs_flush_buftarg(
David Chinner585e6d82007-02-10 18:32:29 +11001741 xfs_buftarg_t *target,
1742 int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Dave Chinner089716a2010-01-26 15:13:25 +11001744 xfs_buf_t *bp;
David Chinner585e6d82007-02-10 18:32:29 +11001745 int pincount = 0;
Dave Chinner089716a2010-01-26 15:13:25 +11001746 LIST_HEAD(tmp_list);
1747 LIST_HEAD(wait_list);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001748 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Dave Chinnerc626d172009-04-06 18:42:11 +02001750 xfs_buf_runall_queues(xfsconvertd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001751 xfs_buf_runall_queues(xfsdatad_workqueue);
1752 xfs_buf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
David Chinner5e6a07d2007-02-10 18:34:49 +11001754 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
Dave Chinner089716a2010-01-26 15:13:25 +11001755 pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 /*
Dave Chinner089716a2010-01-26 15:13:25 +11001758 * Dropped the delayed write list lock, now walk the temporary list.
1759 * All I/O is issued async and then if we need to wait for completion
1760 * we do that after issuing all the IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 */
Dave Chinner089716a2010-01-26 15:13:25 +11001762 list_sort(NULL, &tmp_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001763
1764 blk_start_plug(&plug);
Dave Chinner089716a2010-01-26 15:13:25 +11001765 while (!list_empty(&tmp_list)) {
1766 bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
David Chinner585e6d82007-02-10 18:32:29 +11001767 ASSERT(target == bp->b_target);
Dave Chinner089716a2010-01-26 15:13:25 +11001768 list_del_init(&bp->b_list);
1769 if (wait) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001770 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner089716a2010-01-26 15:13:25 +11001771 list_add(&bp->b_list, &wait_list);
1772 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001773 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001775 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Dave Chinner089716a2010-01-26 15:13:25 +11001777 if (wait) {
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001778 /* Wait for IO to complete. */
Dave Chinner089716a2010-01-26 15:13:25 +11001779 while (!list_empty(&wait_list)) {
1780 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Nathan Scottf07c2252006-09-28 10:52:15 +10001781
Dave Chinner089716a2010-01-26 15:13:25 +11001782 list_del_init(&bp->b_list);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001783 xfs_buf_iowait(bp);
Dave Chinner089716a2010-01-26 15:13:25 +11001784 xfs_buf_relse(bp);
1785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return pincount;
1789}
1790
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001791int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001792xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Nathan Scott87582802006-03-14 13:18:19 +11001794 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1795 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001796 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001797 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001798
Dave Chinner51749e42010-09-08 09:00:22 +00001799 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001800 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001801 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001802 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Tejun Heo83e75902011-02-01 11:42:43 +01001804 xfsdatad_workqueue = alloc_workqueue("xfsdatad", WQ_MEM_RECLAIM, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001805 if (!xfsdatad_workqueue)
1806 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Tejun Heo83e75902011-02-01 11:42:43 +01001808 xfsconvertd_workqueue = alloc_workqueue("xfsconvertd",
1809 WQ_MEM_RECLAIM, 1);
Dave Chinnerc626d172009-04-06 18:42:11 +02001810 if (!xfsconvertd_workqueue)
1811 goto out_destroy_xfsdatad_workqueue;
1812
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Dave Chinnerc626d172009-04-06 18:42:11 +02001815 out_destroy_xfsdatad_workqueue:
1816 destroy_workqueue(xfsdatad_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001817 out_destroy_xfslogd_workqueue:
1818 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001819 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001820 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001821 out:
Nathan Scott87582802006-03-14 13:18:19 +11001822 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825void
Nathan Scottce8e9222006-01-11 15:39:08 +11001826xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Dave Chinnerc626d172009-04-06 18:42:11 +02001828 destroy_workqueue(xfsconvertd_workqueue);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001829 destroy_workqueue(xfsdatad_workqueue);
1830 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001831 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001833
1834#ifdef CONFIG_KDB_MODULES
1835struct list_head *
1836xfs_get_buftarg_list(void)
1837{
1838 return &xfs_buftarg_list;
1839}
1840#endif