blob: e3af85095ddbaa0de0a59e0eb93739481ec0694b [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
Chandra Seetharamaneabbaf12011-09-08 20:18:50 +0000418 * locked. No I/O is implied by this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
420xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100421_xfs_buf_find(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 xfs_buftarg_t *btp, /* block device target */
Nathan Scott204ab252006-01-11 20:50:22 +1100423 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100425 xfs_buf_flags_t flags,
426 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Nathan Scott204ab252006-01-11 20:50:22 +1100428 xfs_off_t range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 size_t range_length;
Dave Chinner74f75a02010-09-24 19:59:04 +1000430 struct xfs_perag *pag;
431 struct rb_node **rbp;
432 struct rb_node *parent;
433 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 range_base = (ioff << BBSHIFT);
436 range_length = (isize << BBSHIFT);
437
438 /* Check for IOs smaller than the sector size / not sector aligned */
Nathan Scottce8e9222006-01-11 15:39:08 +1100439 ASSERT(!(range_length < (1 << btp->bt_sshift)));
Nathan Scott204ab252006-01-11 20:50:22 +1100440 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Dave Chinner74f75a02010-09-24 19:59:04 +1000442 /* get tree root */
443 pag = xfs_perag_get(btp->bt_mount,
444 xfs_daddr_to_agno(btp->bt_mount, ioff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Dave Chinner74f75a02010-09-24 19:59:04 +1000446 /* walk tree */
447 spin_lock(&pag->pag_buf_lock);
448 rbp = &pag->pag_buf_tree.rb_node;
449 parent = NULL;
450 bp = NULL;
451 while (*rbp) {
452 parent = *rbp;
453 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Dave Chinner74f75a02010-09-24 19:59:04 +1000455 if (range_base < bp->b_file_offset)
456 rbp = &(*rbp)->rb_left;
457 else if (range_base > bp->b_file_offset)
458 rbp = &(*rbp)->rb_right;
459 else {
460 /*
461 * found a block offset match. If the range doesn't
462 * match, the only way this is allowed is if the buffer
463 * in the cache is stale and the transaction that made
464 * it stale has not yet committed. i.e. we are
465 * reallocating a busy extent. Skip this buffer and
466 * continue searching to the right for an exact match.
467 */
468 if (bp->b_buffer_length != range_length) {
469 ASSERT(bp->b_flags & XBF_STALE);
470 rbp = &(*rbp)->rb_right;
471 continue;
472 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100473 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 goto found;
475 }
476 }
477
478 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100479 if (new_bp) {
480 _xfs_buf_initialize(new_bp, btp, range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 range_length, flags);
Dave Chinner74f75a02010-09-24 19:59:04 +1000482 rb_link_node(&new_bp->b_rbnode, parent, rbp);
483 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
484 /* the buffer keeps the perag reference until it is freed */
485 new_bp->b_pag = pag;
486 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100488 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000489 spin_unlock(&pag->pag_buf_lock);
490 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100492 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000495 spin_unlock(&pag->pag_buf_lock);
496 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200498 if (!xfs_buf_trylock(bp)) {
499 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100500 xfs_buf_rele(bp);
501 XFS_STATS_INC(xb_busy_locked);
502 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200504 xfs_buf_lock(bp);
505 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507
Dave Chinner0e6e8472011-03-26 09:16:45 +1100508 /*
509 * if the buffer is stale, clear all the external state associated with
510 * it. We need to keep flags such as how we allocated the buffer memory
511 * intact here.
512 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100513 if (bp->b_flags & XBF_STALE) {
514 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100515 bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
David Chinner2f926582005-09-05 08:33:35 +1000516 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000517
518 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100519 XFS_STATS_INC(xb_get_locked);
520 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100524 * Assembles a buffer covering the specified range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 * Storage in memory for all portions of the buffer will be allocated,
526 * although backing storage may not be.
527 */
528xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000529xfs_buf_get(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100531 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100533 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Nathan Scottce8e9222006-01-11 15:39:08 +1100535 xfs_buf_t *bp, *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100536 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Nathan Scottce8e9222006-01-11 15:39:08 +1100538 new_bp = xfs_buf_allocate(flags);
539 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return NULL;
541
Nathan Scottce8e9222006-01-11 15:39:08 +1100542 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
543 if (bp == new_bp) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100544 error = xfs_buf_allocate_memory(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (error)
546 goto no_buffer;
547 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100548 xfs_buf_deallocate(new_bp);
549 if (unlikely(bp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return NULL;
551 }
552
Nathan Scottce8e9222006-01-11 15:39:08 +1100553 if (!(bp->b_flags & XBF_MAPPED)) {
554 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100556 xfs_warn(target->bt_mount,
557 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto no_buffer;
559 }
560 }
561
Nathan Scottce8e9222006-01-11 15:39:08 +1100562 XFS_STATS_INC(xb_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /*
565 * Always fill in the block number now, the mapped cases can do
566 * their own overlay of this later.
567 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100568 bp->b_bn = ioff;
569 bp->b_count_desired = bp->b_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000571 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100572 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100575 if (flags & (XBF_LOCK | XBF_TRYLOCK))
576 xfs_buf_unlock(bp);
577 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return NULL;
579}
580
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100581STATIC int
582_xfs_buf_read(
583 xfs_buf_t *bp,
584 xfs_buf_flags_t flags)
585{
586 int status;
587
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100588 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
589 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
590
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200591 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | XBF_READ_AHEAD);
592 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100593
594 status = xfs_buf_iorequest(bp);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000595 if (status || bp->b_error || (flags & XBF_ASYNC))
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000596 return status;
597 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000601xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100603 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100605 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Nathan Scottce8e9222006-01-11 15:39:08 +1100607 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Nathan Scottce8e9222006-01-11 15:39:08 +1100609 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000611 bp = xfs_buf_get(target, ioff, isize, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100612 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000613 trace_xfs_buf_read(bp, flags, _RET_IP_);
614
Nathan Scottce8e9222006-01-11 15:39:08 +1100615 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100616 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100617 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100618 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /*
620 * Read ahead call which is already satisfied,
621 * drop the buffer
622 */
623 goto no_buffer;
624 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100626 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628 }
629
Nathan Scottce8e9222006-01-11 15:39:08 +1100630 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100633 if (flags & (XBF_LOCK | XBF_TRYLOCK))
634 xfs_buf_unlock(bp);
635 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return NULL;
637}
638
639/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100640 * If we are not low on memory then do the readahead in a deadlock
641 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
643void
Nathan Scottce8e9222006-01-11 15:39:08 +1100644xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100646 xfs_off_t ioff,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000647 size_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100649 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return;
651
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000652 xfs_buf_read(target, ioff, isize,
653 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Dave Chinner5adc94c2010-09-24 21:58:31 +1000656/*
657 * Read an uncached buffer from disk. Allocates and returns a locked
658 * buffer containing the disk contents or nothing.
659 */
660struct xfs_buf *
661xfs_buf_read_uncached(
662 struct xfs_mount *mp,
663 struct xfs_buftarg *target,
664 xfs_daddr_t daddr,
665 size_t length,
666 int flags)
667{
668 xfs_buf_t *bp;
669 int error;
670
671 bp = xfs_buf_get_uncached(target, length, flags);
672 if (!bp)
673 return NULL;
674
675 /* set up the buffer for a read IO */
Dave Chinner5adc94c2010-09-24 21:58:31 +1000676 XFS_BUF_SET_ADDR(bp, daddr);
677 XFS_BUF_READ(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000678
679 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000680 error = xfs_buf_iowait(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000681 if (error || bp->b_error) {
682 xfs_buf_relse(bp);
683 return NULL;
684 }
685 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100689xfs_buf_get_empty(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 size_t len,
691 xfs_buftarg_t *target)
692{
Nathan Scottce8e9222006-01-11 15:39:08 +1100693 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Nathan Scottce8e9222006-01-11 15:39:08 +1100695 bp = xfs_buf_allocate(0);
696 if (bp)
697 _xfs_buf_initialize(bp, target, 0, len, 0);
698 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Dave Chinner44396472011-04-21 09:34:27 +0000701/*
702 * Return a buffer allocated as an empty buffer and associated to external
703 * memory via xfs_buf_associate_memory() back to it's empty state.
704 */
705void
706xfs_buf_set_empty(
707 struct xfs_buf *bp,
708 size_t len)
709{
710 if (bp->b_pages)
711 _xfs_buf_free_pages(bp);
712
713 bp->b_pages = NULL;
714 bp->b_page_count = 0;
715 bp->b_addr = NULL;
716 bp->b_file_offset = 0;
717 bp->b_buffer_length = bp->b_count_desired = len;
718 bp->b_bn = XFS_BUF_DADDR_NULL;
719 bp->b_flags &= ~XBF_MAPPED;
720}
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722static inline struct page *
723mem_to_page(
724 void *addr)
725{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800726 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return virt_to_page(addr);
728 } else {
729 return vmalloc_to_page(addr);
730 }
731}
732
733int
Nathan Scottce8e9222006-01-11 15:39:08 +1100734xfs_buf_associate_memory(
735 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 void *mem,
737 size_t len)
738{
739 int rval;
740 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100741 unsigned long pageaddr;
742 unsigned long offset;
743 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 int page_count;
745
Dave Chinner0e6e8472011-03-26 09:16:45 +1100746 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100747 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100748 buflen = PAGE_ALIGN(len + offset);
749 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100752 if (bp->b_pages)
753 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Nathan Scottce8e9222006-01-11 15:39:08 +1100755 bp->b_pages = NULL;
756 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Christoph Hellwig36fae172009-07-18 18:14:58 -0400758 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (rval)
760 return rval;
761
Nathan Scottce8e9222006-01-11 15:39:08 +1100762 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100764 for (i = 0; i < bp->b_page_count; i++) {
765 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100766 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100769 bp->b_count_desired = len;
770 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100771 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 return 0;
774}
775
776xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000777xfs_buf_get_uncached(
778 struct xfs_buftarg *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 size_t len,
Dave Chinner686865f2010-09-24 20:07:47 +1000780 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000782 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
783 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Nathan Scottce8e9222006-01-11 15:39:08 +1100786 bp = xfs_buf_allocate(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (unlikely(bp == NULL))
788 goto fail;
Nathan Scottce8e9222006-01-11 15:39:08 +1100789 _xfs_buf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000791 error = _xfs_buf_get_pages(bp, page_count, 0);
792 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 goto fail_free_buf;
794
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000795 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000796 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000797 if (!bp->b_pages[i])
798 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000800 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000802 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
803 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100804 xfs_warn(target->bt_mount,
805 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Dave Chinner686865f2010-09-24 20:07:47 +1000809 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000813 while (--i >= 0)
814 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000815 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 fail_free_buf:
Christoph Hellwigca165b82007-05-24 15:21:11 +1000817 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 fail:
819 return NULL;
820}
821
822/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 * Increment reference count on buffer, to hold the buffer concurrently
824 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 * Must hold the buffer already to call this function.
826 */
827void
Nathan Scottce8e9222006-01-11 15:39:08 +1100828xfs_buf_hold(
829 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000831 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100832 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100836 * Releases a hold on the specified buffer. If the
837 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 */
839void
Nathan Scottce8e9222006-01-11 15:39:08 +1100840xfs_buf_rele(
841 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Dave Chinner74f75a02010-09-24 19:59:04 +1000843 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000845 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Dave Chinner74f75a02010-09-24 19:59:04 +1000847 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100848 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000849 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100850 if (atomic_dec_and_test(&bp->b_hold))
851 xfs_buf_free(bp);
852 return;
853 }
854
Dave Chinner74f75a02010-09-24 19:59:04 +1000855 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100856
Lachlan McIlroy37906892008-08-13 15:42:10 +1000857 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000858 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000859 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100860 atomic_read(&bp->b_lru_ref)) {
861 xfs_buf_lru_add(bp);
862 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100863 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100864 xfs_buf_lru_del(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +1100865 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
Dave Chinner74f75a02010-09-24 19:59:04 +1000866 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
867 spin_unlock(&pag->pag_buf_lock);
868 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100869 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871 }
872}
873
874
875/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100876 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100877 *
878 * If we come across a stale, pinned, locked buffer, we know that we are
879 * being asked to lock a buffer that has been reallocated. Because it is
880 * pinned, we know that the log has not been pushed to disk and hence it
881 * will still be locked. Rather than continuing to have trylock attempts
882 * fail until someone else pushes the log, push it ourselves before
883 * returning. This means that the xfsaild will not get stuck trying
884 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 */
886int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200887xfs_buf_trylock(
888 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 int locked;
891
Nathan Scottce8e9222006-01-11 15:39:08 +1100892 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000893 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100894 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100895 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
896 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000897
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200898 trace_xfs_buf_trylock(bp, _RET_IP_);
899 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100903 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000904 *
905 * If we come across a stale, pinned, locked buffer, we know that we
906 * are being asked to lock a buffer that has been reallocated. Because
907 * it is pinned, we know that the log has not been pushed to disk and
908 * hence it will still be locked. Rather than sleeping until someone
909 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100911void
912xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200913 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000915 trace_xfs_buf_lock(bp, _RET_IP_);
916
Dave Chinnered3b4d62010-05-21 12:07:08 +1000917 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000918 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100919 down(&bp->b_sema);
920 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000921
922 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
925/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100926 * Releases the lock on the buffer object.
David Chinner2f926582005-09-05 08:33:35 +1000927 * If the buffer is marked delwri but is not queued, do so before we
Nathan Scottce8e9222006-01-11 15:39:08 +1100928 * unlock the buffer as we need to set flags correctly. We also need to
David Chinner2f926582005-09-05 08:33:35 +1000929 * take a reference for the delwri queue because the unlocker is going to
930 * drop their's and they don't know we just queued it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 */
932void
Nathan Scottce8e9222006-01-11 15:39:08 +1100933xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200934 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Nathan Scottce8e9222006-01-11 15:39:08 +1100936 XB_CLEAR_OWNER(bp);
937 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000938
939 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
Nathan Scottce8e9222006-01-11 15:39:08 +1100942STATIC void
943xfs_buf_wait_unpin(
944 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 DECLARE_WAITQUEUE (wait, current);
947
Nathan Scottce8e9222006-01-11 15:39:08 +1100948 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return;
950
Nathan Scottce8e9222006-01-11 15:39:08 +1100951 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 for (;;) {
953 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100954 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100956 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100958 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 set_current_state(TASK_RUNNING);
960}
961
962/*
963 * Buffer Utility Routines
964 */
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100967xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000968 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
David Howellsc4028952006-11-22 14:57:56 +0000970 xfs_buf_t *bp =
971 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400973 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100974 (*(bp->b_iodone))(bp);
975 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 xfs_buf_relse(bp);
977}
978
979void
Nathan Scottce8e9222006-01-11 15:39:08 +1100980xfs_buf_ioend(
981 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int schedule)
983{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000984 trace_xfs_buf_iodone(bp, _RET_IP_);
985
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100986 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100987 if (bp->b_error == 0)
988 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Nathan Scottce8e9222006-01-11 15:39:08 +1100990 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000992 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100993 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 } else {
David Howellsc4028952006-11-22 14:57:56 +0000995 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +1000998 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000}
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002void
Nathan Scottce8e9222006-01-11 15:39:08 +11001003xfs_buf_ioerror(
1004 xfs_buf_t *bp,
1005 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001008 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001009 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001013xfs_bwrite(
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001014 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001016 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001018 bp->b_flags |= XBF_WRITE;
Christoph Hellwig8c383662010-03-12 10:59:40 +00001019 bp->b_flags &= ~(XBF_ASYNC | XBF_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001021 xfs_buf_delwri_dequeue(bp);
Christoph Hellwig939d7232010-07-20 17:51:16 +10001022 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Christoph Hellwig8c383662010-03-12 10:59:40 +00001024 error = xfs_buf_iowait(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001025 if (error) {
1026 xfs_force_shutdown(bp->b_target->bt_mount,
1027 SHUTDOWN_META_IO_ERROR);
1028 }
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001029 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001030}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Christoph Hellwig4e234712010-01-13 22:17:56 +00001032/*
1033 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001034 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001035 * so that the proper iodone callbacks get called.
1036 */
1037STATIC int
1038xfs_bioerror(
1039 xfs_buf_t *bp)
1040{
1041#ifdef XFSERRORDEBUG
1042 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1043#endif
1044
1045 /*
1046 * No need to wait until the buffer is unpinned, we aren't flushing it.
1047 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001048 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001049
1050 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001051 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001052 */
1053 XFS_BUF_UNREAD(bp);
Christoph Hellwig61551f12011-08-23 08:28:06 +00001054 xfs_buf_delwri_dequeue(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001055 XFS_BUF_UNDONE(bp);
1056 XFS_BUF_STALE(bp);
1057
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001058 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001059
1060 return EIO;
1061}
1062
1063/*
1064 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001065 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001066 * This is meant for userdata errors; metadata bufs come with
1067 * iodone functions attached, so that we can track down errors.
1068 */
1069STATIC int
1070xfs_bioerror_relse(
1071 struct xfs_buf *bp)
1072{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001073 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001074 /*
1075 * No need to wait until the buffer is unpinned.
1076 * We aren't flushing it.
1077 *
1078 * chunkhold expects B_DONE to be set, whether
1079 * we actually finish the I/O or not. We don't want to
1080 * change that interface.
1081 */
1082 XFS_BUF_UNREAD(bp);
Christoph Hellwig61551f12011-08-23 08:28:06 +00001083 xfs_buf_delwri_dequeue(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001084 XFS_BUF_DONE(bp);
1085 XFS_BUF_STALE(bp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001086 bp->b_iodone = NULL;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001087 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001088 /*
1089 * Mark b_error and B_ERROR _both_.
1090 * Lot's of chunkcache code assumes that.
1091 * There's no reason to mark error for
1092 * ASYNC buffers.
1093 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001094 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001095 XFS_BUF_FINISH_IOWAIT(bp);
1096 } else {
1097 xfs_buf_relse(bp);
1098 }
1099
1100 return EIO;
1101}
1102
1103
1104/*
1105 * All xfs metadata buffers except log state machine buffers
1106 * get this attached as their b_bdstrat callback function.
1107 * This is so that we can catch a buffer
1108 * after prematurely unpinning it to forcibly shutdown the filesystem.
1109 */
1110int
1111xfs_bdstrat_cb(
1112 struct xfs_buf *bp)
1113{
Dave Chinnerebad8612010-09-22 10:47:20 +10001114 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001115 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1116 /*
1117 * Metadata write that didn't get logged but
1118 * written delayed anyway. These aren't associated
1119 * with a transaction, and can be ignored.
1120 */
1121 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1122 return xfs_bioerror_relse(bp);
1123 else
1124 return xfs_bioerror(bp);
1125 }
1126
1127 xfs_buf_iorequest(bp);
1128 return 0;
1129}
1130
1131/*
1132 * Wrapper around bdstrat so that we can stop data from going to disk in case
1133 * we are shutting down the filesystem. Typically user data goes thru this
1134 * path; one of the exceptions is the superblock.
1135 */
1136void
1137xfsbdstrat(
1138 struct xfs_mount *mp,
1139 struct xfs_buf *bp)
1140{
1141 if (XFS_FORCED_SHUTDOWN(mp)) {
1142 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1143 xfs_bioerror_relse(bp);
1144 return;
1145 }
1146
1147 xfs_buf_iorequest(bp);
1148}
1149
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001150STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001151_xfs_buf_ioend(
1152 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 int schedule)
1154{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001155 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001156 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
Al Viro782e3b32007-10-12 07:17:47 +01001159STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001160xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 int error)
1163{
Nathan Scottce8e9222006-01-11 15:39:08 +11001164 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001166 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
James Bottomley73c77e22010-01-25 11:42:24 -06001168 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1169 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1170
Nathan Scottce8e9222006-01-11 15:39:08 +11001171 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001176_xfs_buf_ioapply(
1177 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001179 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001181 int offset = bp->b_offset;
1182 int size = bp->b_count_desired;
1183 sector_t sector = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Nathan Scottce8e9222006-01-11 15:39:08 +11001185 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 map_i = 0;
1187
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001188 if (bp->b_flags & XBF_WRITE) {
1189 if (bp->b_flags & XBF_SYNCIO)
1190 rw = WRITE_SYNC;
1191 else
1192 rw = WRITE;
1193 if (bp->b_flags & XBF_FUA)
1194 rw |= REQ_FUA;
1195 if (bp->b_flags & XBF_FLUSH)
1196 rw |= REQ_FLUSH;
1197 } else if (bp->b_flags & XBF_READ_AHEAD) {
1198 rw = READA;
Nathan Scott51bdd702006-09-28 11:01:57 +10001199 } else {
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001200 rw = READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001201 }
1202
Christoph Hellwig34951f52011-07-26 15:06:44 +00001203 /* we only use the buffer cache for meta-data */
1204 rw |= REQ_META;
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001207 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1209 if (nr_pages > total_nr_pages)
1210 nr_pages = total_nr_pages;
1211
1212 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001213 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001215 bio->bi_end_io = xfs_buf_bio_end_io;
1216 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Dave Chinner0e6e8472011-03-26 09:16:45 +11001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 for (; size && nr_pages; nr_pages--, map_i++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001220 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 if (nbytes > size)
1223 nbytes = size;
1224
Nathan Scottce8e9222006-01-11 15:39:08 +11001225 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1226 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 break;
1228
1229 offset = 0;
1230 sector += nbytes >> BBSHIFT;
1231 size -= nbytes;
1232 total_nr_pages--;
1233 }
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001236 if (xfs_buf_is_vmapped(bp)) {
1237 flush_kernel_vmap_range(bp->b_addr,
1238 xfs_buf_vmap_len(bp));
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 submit_bio(rw, bio);
1241 if (size)
1242 goto next_chunk;
1243 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001244 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001245 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247}
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249int
Nathan Scottce8e9222006-01-11 15:39:08 +11001250xfs_buf_iorequest(
1251 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001253 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Christoph Hellwig375ec692011-08-23 08:28:03 +00001255 ASSERT(!(bp->b_flags & XBF_DELWRI));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Christoph Hellwig375ec692011-08-23 08:28:03 +00001257 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001258 xfs_buf_wait_unpin(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001259 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 /* Set the count to 1 initially, this will stop an I/O
1262 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001263 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001265 atomic_set(&bp->b_io_remaining, 1);
1266 _xfs_buf_ioapply(bp);
1267 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Nathan Scottce8e9222006-01-11 15:39:08 +11001269 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return 0;
1271}
1272
1273/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001274 * Waits for I/O to complete on the buffer supplied.
1275 * It returns immediately if no I/O is pending.
1276 * It returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 */
1278int
Nathan Scottce8e9222006-01-11 15:39:08 +11001279xfs_buf_iowait(
1280 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001282 trace_xfs_buf_iowait(bp, _RET_IP_);
1283
David Chinnerb4dd3302008-08-13 16:36:11 +10001284 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001285
1286 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001287 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Nathan Scottce8e9222006-01-11 15:39:08 +11001290xfs_caddr_t
1291xfs_buf_offset(
1292 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 size_t offset)
1294{
1295 struct page *page;
1296
Nathan Scottce8e9222006-01-11 15:39:08 +11001297 if (bp->b_flags & XBF_MAPPED)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001298 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Nathan Scottce8e9222006-01-11 15:39:08 +11001300 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001301 page = bp->b_pages[offset >> PAGE_SHIFT];
1302 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
1305/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 * Move data into or out of a buffer.
1307 */
1308void
Nathan Scottce8e9222006-01-11 15:39:08 +11001309xfs_buf_iomove(
1310 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 size_t boff, /* starting buffer offset */
1312 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001313 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001314 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 size_t bend, cpoff, csize;
1317 struct page *page;
1318
1319 bend = boff + bsize;
1320 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001321 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1322 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 csize = min_t(size_t,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001324 PAGE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Dave Chinner0e6e8472011-03-26 09:16:45 +11001326 ASSERT(((csize + cpoff) <= PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001329 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 memset(page_address(page) + cpoff, 0, csize);
1331 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001332 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 memcpy(data, page_address(page) + cpoff, csize);
1334 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001335 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 memcpy(page_address(page) + cpoff, data, csize);
1337 }
1338
1339 boff += csize;
1340 data += csize;
1341 }
1342}
1343
1344/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001345 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 */
1347
1348/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001349 * Wait for any bufs with callbacks that have been submitted but have not yet
1350 * returned. These buffers will have an elevated hold count, so wait on those
1351 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 */
1353void
1354xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001355 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Dave Chinner430cbeb2010-12-02 16:30:55 +11001357 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Dave Chinner430cbeb2010-12-02 16:30:55 +11001359restart:
1360 spin_lock(&btp->bt_lru_lock);
1361 while (!list_empty(&btp->bt_lru)) {
1362 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1363 if (atomic_read(&bp->b_hold) > 1) {
1364 spin_unlock(&btp->bt_lru_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001365 delay(100);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001366 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001368 /*
1369 * clear the LRU reference count so the bufer doesn't get
1370 * ignored in xfs_buf_rele().
1371 */
1372 atomic_set(&bp->b_lru_ref, 0);
1373 spin_unlock(&btp->bt_lru_lock);
1374 xfs_buf_rele(bp);
1375 spin_lock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001377 spin_unlock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Dave Chinnerff57ab22010-11-30 17:27:57 +11001380int
1381xfs_buftarg_shrink(
1382 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001383 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001384{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001385 struct xfs_buftarg *btp = container_of(shrink,
1386 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001387 struct xfs_buf *bp;
Ying Han1495f232011-05-24 17:12:27 -07001388 int nr_to_scan = sc->nr_to_scan;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001389 LIST_HEAD(dispose);
1390
1391 if (!nr_to_scan)
1392 return btp->bt_lru_nr;
1393
1394 spin_lock(&btp->bt_lru_lock);
1395 while (!list_empty(&btp->bt_lru)) {
1396 if (nr_to_scan-- <= 0)
1397 break;
1398
1399 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1400
1401 /*
1402 * Decrement the b_lru_ref count unless the value is already
1403 * zero. If the value is already zero, we need to reclaim the
1404 * buffer, otherwise it gets another trip through the LRU.
1405 */
1406 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1407 list_move_tail(&bp->b_lru, &btp->bt_lru);
1408 continue;
1409 }
1410
1411 /*
1412 * remove the buffer from the LRU now to avoid needing another
1413 * lock round trip inside xfs_buf_rele().
1414 */
1415 list_move(&bp->b_lru, &dispose);
1416 btp->bt_lru_nr--;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001417 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001418 spin_unlock(&btp->bt_lru_lock);
1419
1420 while (!list_empty(&dispose)) {
1421 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1422 list_del_init(&bp->b_lru);
1423 xfs_buf_rele(bp);
1424 }
1425
1426 return btp->bt_lru_nr;
David Chinnera6867a62006-01-11 15:37:58 +11001427}
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429void
1430xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001431 struct xfs_mount *mp,
1432 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001434 unregister_shrinker(&btp->bt_shrinker);
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 xfs_flush_buftarg(btp, 1);
Christoph Hellwigb7963132009-03-03 14:48:37 -05001437 if (mp->m_flags & XFS_MOUNT_BARRIER)
1438 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001439
David Chinnera6867a62006-01-11 15:37:58 +11001440 kthread_stop(btp->bt_task);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001441 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444STATIC int
1445xfs_setsize_buftarg_flags(
1446 xfs_buftarg_t *btp,
1447 unsigned int blocksize,
1448 unsigned int sectorsize,
1449 int verbose)
1450{
Nathan Scottce8e9222006-01-11 15:39:08 +11001451 btp->bt_bsize = blocksize;
1452 btp->bt_sshift = ffs(sectorsize) - 1;
1453 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Nathan Scottce8e9222006-01-11 15:39:08 +11001455 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001456 xfs_warn(btp->bt_mount,
1457 "Cannot set_blocksize to %u on device %s\n",
Chandra Seetharamanc35a5492011-07-22 23:40:46 +00001458 sectorsize, xfs_buf_target_name(btp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return EINVAL;
1460 }
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return 0;
1463}
1464
1465/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001466 * When allocating the initial buffer target we have not yet
1467 * read in the superblock, so don't know what sized sectors
1468 * are being used is at this early stage. Play safe.
1469 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470STATIC int
1471xfs_setsize_buftarg_early(
1472 xfs_buftarg_t *btp,
1473 struct block_device *bdev)
1474{
1475 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001476 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
1479int
1480xfs_setsize_buftarg(
1481 xfs_buftarg_t *btp,
1482 unsigned int blocksize,
1483 unsigned int sectorsize)
1484{
1485 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1486}
1487
1488STATIC int
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001489xfs_alloc_delwri_queue(
Jan Engelhardte2a07812010-03-23 09:52:55 +11001490 xfs_buftarg_t *btp,
1491 const char *fsname)
David Chinnera6867a62006-01-11 15:37:58 +11001492{
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001493 INIT_LIST_HEAD(&btp->bt_delwri_queue);
1494 spin_lock_init(&btp->bt_delwri_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001495 btp->bt_flags = 0;
Jan Engelhardte2a07812010-03-23 09:52:55 +11001496 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd/%s", fsname);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001497 if (IS_ERR(btp->bt_task))
1498 return PTR_ERR(btp->bt_task);
1499 return 0;
David Chinnera6867a62006-01-11 15:37:58 +11001500}
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502xfs_buftarg_t *
1503xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001504 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001506 int external,
1507 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 xfs_buftarg_t *btp;
1510
1511 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1512
Dave Chinnerebad8612010-09-22 10:47:20 +10001513 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001514 btp->bt_dev = bdev->bd_dev;
1515 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001516 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1517 if (!btp->bt_bdi)
1518 goto error;
1519
Dave Chinner430cbeb2010-12-02 16:30:55 +11001520 INIT_LIST_HEAD(&btp->bt_lru);
1521 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (xfs_setsize_buftarg_early(btp, bdev))
1523 goto error;
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001524 if (xfs_alloc_delwri_queue(btp, fsname))
David Chinnera6867a62006-01-11 15:37:58 +11001525 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001526 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1527 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1528 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return btp;
1530
1531error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001532 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return NULL;
1534}
1535
1536
1537/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001538 * Delayed write buffer handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 */
Christoph Hellwig61551f12011-08-23 08:28:06 +00001540void
Nathan Scottce8e9222006-01-11 15:39:08 +11001541xfs_buf_delwri_queue(
Christoph Hellwig527cfdf2011-08-23 08:28:04 +00001542 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001544 struct xfs_buftarg *btp = bp->b_target;
David Chinnera6867a62006-01-11 15:37:58 +11001545
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001546 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1547
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001548 ASSERT(!(bp->b_flags & XBF_READ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001550 spin_lock(&btp->bt_delwri_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +11001551 if (!list_empty(&bp->b_list)) {
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001552 /* if already in the queue, move it to the tail */
Nathan Scottce8e9222006-01-11 15:39:08 +11001553 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001554 list_move_tail(&bp->b_list, &btp->bt_delwri_queue);
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001555 } else {
Dave Chinnerc9c12972010-01-11 11:49:59 +00001556 /* start xfsbufd as it is about to have something to do */
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001557 if (list_empty(&btp->bt_delwri_queue))
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001558 wake_up_process(bp->b_target->bt_task);
Dave Chinnerc9c12972010-01-11 11:49:59 +00001559
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001560 atomic_inc(&bp->b_hold);
1561 bp->b_flags |= XBF_DELWRI | _XBF_DELWRI_Q | XBF_ASYNC;
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001562 list_add_tail(&bp->b_list, &btp->bt_delwri_queue);
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001563 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001564 bp->b_queuetime = jiffies;
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001565 spin_unlock(&btp->bt_delwri_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
1568void
Nathan Scottce8e9222006-01-11 15:39:08 +11001569xfs_buf_delwri_dequeue(
1570 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 int dequeued = 0;
1573
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001574 spin_lock(&bp->b_target->bt_delwri_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +11001575 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1576 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1577 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 dequeued = 1;
1579 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001580 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001581 spin_unlock(&bp->b_target->bt_delwri_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 if (dequeued)
Nathan Scottce8e9222006-01-11 15:39:08 +11001584 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001586 trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
Dave Chinnerd808f612010-02-02 10:13:42 +11001589/*
1590 * If a delwri buffer needs to be pushed before it has aged out, then promote
1591 * it to the head of the delwri queue so that it will be flushed on the next
1592 * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
1593 * than the age currently needed to flush the buffer. Hence the next time the
1594 * xfsbufd sees it is guaranteed to be considered old enough to flush.
1595 */
1596void
1597xfs_buf_delwri_promote(
1598 struct xfs_buf *bp)
1599{
1600 struct xfs_buftarg *btp = bp->b_target;
1601 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
1602
1603 ASSERT(bp->b_flags & XBF_DELWRI);
1604 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1605
1606 /*
1607 * Check the buffer age before locking the delayed write queue as we
1608 * don't need to promote buffers that are already past the flush age.
1609 */
1610 if (bp->b_queuetime < jiffies - age)
1611 return;
1612 bp->b_queuetime = jiffies - age;
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001613 spin_lock(&btp->bt_delwri_lock);
1614 list_move(&bp->b_list, &btp->bt_delwri_queue);
1615 spin_unlock(&btp->bt_delwri_lock);
Dave Chinnerd808f612010-02-02 10:13:42 +11001616}
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001619xfs_buf_runall_queues(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 struct workqueue_struct *queue)
1621{
1622 flush_workqueue(queue);
1623}
1624
David Chinner585e6d82007-02-10 18:32:29 +11001625/*
1626 * Move as many buffers as specified to the supplied list
1627 * idicating if we skipped any buffers to prevent deadlocks.
1628 */
1629STATIC int
1630xfs_buf_delwri_split(
1631 xfs_buftarg_t *target,
1632 struct list_head *list,
David Chinner5e6a07d2007-02-10 18:34:49 +11001633 unsigned long age)
David Chinner585e6d82007-02-10 18:32:29 +11001634{
1635 xfs_buf_t *bp, *n;
David Chinner585e6d82007-02-10 18:32:29 +11001636 int skipped = 0;
David Chinner5e6a07d2007-02-10 18:34:49 +11001637 int force;
David Chinner585e6d82007-02-10 18:32:29 +11001638
David Chinner5e6a07d2007-02-10 18:34:49 +11001639 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
David Chinner585e6d82007-02-10 18:32:29 +11001640 INIT_LIST_HEAD(list);
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001641 spin_lock(&target->bt_delwri_lock);
1642 list_for_each_entry_safe(bp, n, &target->bt_delwri_queue, b_list) {
David Chinner585e6d82007-02-10 18:32:29 +11001643 ASSERT(bp->b_flags & XBF_DELWRI);
1644
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00001645 if (!xfs_buf_ispinned(bp) && xfs_buf_trylock(bp)) {
David Chinner5e6a07d2007-02-10 18:34:49 +11001646 if (!force &&
David Chinner585e6d82007-02-10 18:32:29 +11001647 time_before(jiffies, bp->b_queuetime + age)) {
1648 xfs_buf_unlock(bp);
1649 break;
1650 }
1651
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001652 bp->b_flags &= ~(XBF_DELWRI | _XBF_DELWRI_Q);
David Chinner585e6d82007-02-10 18:32:29 +11001653 bp->b_flags |= XBF_WRITE;
1654 list_move_tail(&bp->b_list, list);
Dave Chinnerbfe27412010-11-08 08:55:05 +00001655 trace_xfs_buf_delwri_split(bp, _RET_IP_);
David Chinner585e6d82007-02-10 18:32:29 +11001656 } else
1657 skipped++;
1658 }
David Chinner585e6d82007-02-10 18:32:29 +11001659
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001660 spin_unlock(&target->bt_delwri_lock);
David Chinner585e6d82007-02-10 18:32:29 +11001661 return skipped;
David Chinner585e6d82007-02-10 18:32:29 +11001662}
1663
Dave Chinner089716a2010-01-26 15:13:25 +11001664/*
1665 * Compare function is more complex than it needs to be because
1666 * the return value is only 32 bits and we are doing comparisons
1667 * on 64 bit values
1668 */
1669static int
1670xfs_buf_cmp(
1671 void *priv,
1672 struct list_head *a,
1673 struct list_head *b)
1674{
1675 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1676 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1677 xfs_daddr_t diff;
1678
1679 diff = ap->b_bn - bp->b_bn;
1680 if (diff < 0)
1681 return -1;
1682 if (diff > 0)
1683 return 1;
1684 return 0;
1685}
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001688xfsbufd(
David Chinner585e6d82007-02-10 18:32:29 +11001689 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Dave Chinner089716a2010-01-26 15:13:25 +11001691 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 current->flags |= PF_MEMALLOC;
1694
Rafael J. Wysocki978c7b22007-12-07 14:09:02 +11001695 set_freezable();
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 do {
Dave Chinnerc9c12972010-01-11 11:49:59 +00001698 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
1699 long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
Dave Chinner089716a2010-01-26 15:13:25 +11001700 struct list_head tmp;
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001701 struct blk_plug plug;
Dave Chinnerc9c12972010-01-11 11:49:59 +00001702
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001703 if (unlikely(freezing(current))) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001704 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001705 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001706 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001707 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Nathan Scottabd0cf72005-05-05 13:30:13 -07001708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Dave Chinnerc9c12972010-01-11 11:49:59 +00001710 /* sleep for a long time if there is nothing to do. */
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001711 if (list_empty(&target->bt_delwri_queue))
Dave Chinnerc9c12972010-01-11 11:49:59 +00001712 tout = MAX_SCHEDULE_TIMEOUT;
1713 schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Dave Chinnerc9c12972010-01-11 11:49:59 +00001715 xfs_buf_delwri_split(target, &tmp, age);
Dave Chinner089716a2010-01-26 15:13:25 +11001716 list_sort(NULL, &tmp, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001717
1718 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 while (!list_empty(&tmp)) {
Dave Chinner089716a2010-01-26 15:13:25 +11001720 struct xfs_buf *bp;
1721 bp = list_first_entry(&tmp, struct xfs_buf, b_list);
Nathan Scottce8e9222006-01-11 15:39:08 +11001722 list_del_init(&bp->b_list);
Christoph Hellwig939d7232010-07-20 17:51:16 +10001723 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001725 blk_finish_plug(&plug);
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001726 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
1731/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001732 * Go through all incore buffers, and release buffers if they belong to
1733 * the given device. This is used in filesystem error handling to
1734 * preserve the consistency of its metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
1736int
1737xfs_flush_buftarg(
David Chinner585e6d82007-02-10 18:32:29 +11001738 xfs_buftarg_t *target,
1739 int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Dave Chinner089716a2010-01-26 15:13:25 +11001741 xfs_buf_t *bp;
David Chinner585e6d82007-02-10 18:32:29 +11001742 int pincount = 0;
Dave Chinner089716a2010-01-26 15:13:25 +11001743 LIST_HEAD(tmp_list);
1744 LIST_HEAD(wait_list);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001745 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Dave Chinnerc626d172009-04-06 18:42:11 +02001747 xfs_buf_runall_queues(xfsconvertd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001748 xfs_buf_runall_queues(xfsdatad_workqueue);
1749 xfs_buf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
David Chinner5e6a07d2007-02-10 18:34:49 +11001751 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
Dave Chinner089716a2010-01-26 15:13:25 +11001752 pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 /*
Dave Chinner089716a2010-01-26 15:13:25 +11001755 * Dropped the delayed write list lock, now walk the temporary list.
1756 * All I/O is issued async and then if we need to wait for completion
1757 * we do that after issuing all the IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 */
Dave Chinner089716a2010-01-26 15:13:25 +11001759 list_sort(NULL, &tmp_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001760
1761 blk_start_plug(&plug);
Dave Chinner089716a2010-01-26 15:13:25 +11001762 while (!list_empty(&tmp_list)) {
1763 bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
David Chinner585e6d82007-02-10 18:32:29 +11001764 ASSERT(target == bp->b_target);
Dave Chinner089716a2010-01-26 15:13:25 +11001765 list_del_init(&bp->b_list);
1766 if (wait) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001767 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner089716a2010-01-26 15:13:25 +11001768 list_add(&bp->b_list, &wait_list);
1769 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001770 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001772 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Dave Chinner089716a2010-01-26 15:13:25 +11001774 if (wait) {
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001775 /* Wait for IO to complete. */
Dave Chinner089716a2010-01-26 15:13:25 +11001776 while (!list_empty(&wait_list)) {
1777 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Nathan Scottf07c2252006-09-28 10:52:15 +10001778
Dave Chinner089716a2010-01-26 15:13:25 +11001779 list_del_init(&bp->b_list);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001780 xfs_buf_iowait(bp);
Dave Chinner089716a2010-01-26 15:13:25 +11001781 xfs_buf_relse(bp);
1782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return pincount;
1786}
1787
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001788int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001789xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Nathan Scott87582802006-03-14 13:18:19 +11001791 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1792 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001793 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001794 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001795
Dave Chinner51749e42010-09-08 09:00:22 +00001796 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001797 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001798 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001799 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Tejun Heo83e75902011-02-01 11:42:43 +01001801 xfsdatad_workqueue = alloc_workqueue("xfsdatad", WQ_MEM_RECLAIM, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001802 if (!xfsdatad_workqueue)
1803 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Tejun Heo83e75902011-02-01 11:42:43 +01001805 xfsconvertd_workqueue = alloc_workqueue("xfsconvertd",
1806 WQ_MEM_RECLAIM, 1);
Dave Chinnerc626d172009-04-06 18:42:11 +02001807 if (!xfsconvertd_workqueue)
1808 goto out_destroy_xfsdatad_workqueue;
1809
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001810 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Dave Chinnerc626d172009-04-06 18:42:11 +02001812 out_destroy_xfsdatad_workqueue:
1813 destroy_workqueue(xfsdatad_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001814 out_destroy_xfslogd_workqueue:
1815 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001816 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001817 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001818 out:
Nathan Scott87582802006-03-14 13:18:19 +11001819 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822void
Nathan Scottce8e9222006-01-11 15:39:08 +11001823xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Dave Chinnerc626d172009-04-06 18:42:11 +02001825 destroy_workqueue(xfsconvertd_workqueue);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001826 destroy_workqueue(xfsdatad_workqueue);
1827 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001828 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001830
1831#ifdef CONFIG_KDB_MODULES
1832struct list_head *
1833xfs_get_buftarg_list(void)
1834{
1835 return &xfs_buftarg_list;
1836}
1837#endif