blob: 6785b7bd952d1bc6bde7f960995ca0746059c955 [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) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000480 rb_link_node(&new_bp->b_rbnode, parent, rbp);
481 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
482 /* the buffer keeps the perag reference until it is freed */
483 new_bp->b_pag = pag;
484 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100486 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000487 spin_unlock(&pag->pag_buf_lock);
488 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100490 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000493 spin_unlock(&pag->pag_buf_lock);
494 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200496 if (!xfs_buf_trylock(bp)) {
497 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100498 xfs_buf_rele(bp);
499 XFS_STATS_INC(xb_busy_locked);
500 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200502 xfs_buf_lock(bp);
503 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
Dave Chinner0e6e8472011-03-26 09:16:45 +1100506 /*
507 * if the buffer is stale, clear all the external state associated with
508 * it. We need to keep flags such as how we allocated the buffer memory
509 * intact here.
510 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100511 if (bp->b_flags & XBF_STALE) {
512 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100513 bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
David Chinner2f926582005-09-05 08:33:35 +1000514 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000515
516 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100517 XFS_STATS_INC(xb_get_locked);
518 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521/*
Dave Chinner38158322011-09-30 04:45:02 +0000522 * Assembles a buffer covering the specified range. The code is optimised for
523 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
524 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
Dave Chinner38158322011-09-30 04:45:02 +0000526struct xfs_buf *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000527xfs_buf_get(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100529 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100531 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Dave Chinner38158322011-09-30 04:45:02 +0000533 struct xfs_buf *bp;
534 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100535 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Dave Chinner38158322011-09-30 04:45:02 +0000537 bp = _xfs_buf_find(target, ioff, isize, flags, NULL);
538 if (likely(bp))
539 goto found;
540
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
Dave Chinner38158322011-09-30 04:45:02 +0000545 _xfs_buf_initialize(new_bp, target,
546 ioff << BBSHIFT, isize << BBSHIFT, flags);
547
Nathan Scottce8e9222006-01-11 15:39:08 +1100548 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000549 if (!bp) {
550 xfs_buf_deallocate(new_bp);
551 return NULL;
552 }
553
Nathan Scottce8e9222006-01-11 15:39:08 +1100554 if (bp == new_bp) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100555 error = xfs_buf_allocate_memory(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (error)
557 goto no_buffer;
Dave Chinner38158322011-09-30 04:45:02 +0000558 } else
Nathan Scottce8e9222006-01-11 15:39:08 +1100559 xfs_buf_deallocate(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Dave Chinner38158322011-09-30 04:45:02 +0000561 /*
562 * Now we have a workable buffer, fill in the block number so
563 * that we can do IO on it.
564 */
565 bp->b_bn = ioff;
566 bp->b_count_desired = bp->b_buffer_length;
567
568found:
Nathan Scottce8e9222006-01-11 15:39:08 +1100569 if (!(bp->b_flags & XBF_MAPPED)) {
570 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100572 xfs_warn(target->bt_mount,
573 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto no_buffer;
575 }
576 }
577
Nathan Scottce8e9222006-01-11 15:39:08 +1100578 XFS_STATS_INC(xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000579 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100580 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Dave Chinner38158322011-09-30 04:45:02 +0000582no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100583 if (flags & (XBF_LOCK | XBF_TRYLOCK))
584 xfs_buf_unlock(bp);
585 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return NULL;
587}
588
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100589STATIC int
590_xfs_buf_read(
591 xfs_buf_t *bp,
592 xfs_buf_flags_t flags)
593{
594 int status;
595
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100596 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
597 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
598
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200599 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | XBF_READ_AHEAD);
600 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100601
602 status = xfs_buf_iorequest(bp);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000603 if (status || bp->b_error || (flags & XBF_ASYNC))
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000604 return status;
605 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000609xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100611 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100613 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Nathan Scottce8e9222006-01-11 15:39:08 +1100615 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Nathan Scottce8e9222006-01-11 15:39:08 +1100617 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000619 bp = xfs_buf_get(target, ioff, isize, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100620 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000621 trace_xfs_buf_read(bp, flags, _RET_IP_);
622
Nathan Scottce8e9222006-01-11 15:39:08 +1100623 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100624 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100625 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100626 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /*
628 * Read ahead call which is already satisfied,
629 * drop the buffer
630 */
631 goto no_buffer;
632 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100634 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636 }
637
Nathan Scottce8e9222006-01-11 15:39:08 +1100638 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100641 if (flags & (XBF_LOCK | XBF_TRYLOCK))
642 xfs_buf_unlock(bp);
643 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return NULL;
645}
646
647/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100648 * If we are not low on memory then do the readahead in a deadlock
649 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
651void
Nathan Scottce8e9222006-01-11 15:39:08 +1100652xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100654 xfs_off_t ioff,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000655 size_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100657 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return;
659
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000660 xfs_buf_read(target, ioff, isize,
661 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Dave Chinner5adc94c2010-09-24 21:58:31 +1000664/*
665 * Read an uncached buffer from disk. Allocates and returns a locked
666 * buffer containing the disk contents or nothing.
667 */
668struct xfs_buf *
669xfs_buf_read_uncached(
670 struct xfs_mount *mp,
671 struct xfs_buftarg *target,
672 xfs_daddr_t daddr,
673 size_t length,
674 int flags)
675{
676 xfs_buf_t *bp;
677 int error;
678
679 bp = xfs_buf_get_uncached(target, length, flags);
680 if (!bp)
681 return NULL;
682
683 /* set up the buffer for a read IO */
Dave Chinner5adc94c2010-09-24 21:58:31 +1000684 XFS_BUF_SET_ADDR(bp, daddr);
685 XFS_BUF_READ(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000686
687 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000688 error = xfs_buf_iowait(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000689 if (error || bp->b_error) {
690 xfs_buf_relse(bp);
691 return NULL;
692 }
693 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100697xfs_buf_get_empty(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 size_t len,
699 xfs_buftarg_t *target)
700{
Nathan Scottce8e9222006-01-11 15:39:08 +1100701 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Nathan Scottce8e9222006-01-11 15:39:08 +1100703 bp = xfs_buf_allocate(0);
704 if (bp)
705 _xfs_buf_initialize(bp, target, 0, len, 0);
706 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Dave Chinner44396472011-04-21 09:34:27 +0000709/*
710 * Return a buffer allocated as an empty buffer and associated to external
711 * memory via xfs_buf_associate_memory() back to it's empty state.
712 */
713void
714xfs_buf_set_empty(
715 struct xfs_buf *bp,
716 size_t len)
717{
718 if (bp->b_pages)
719 _xfs_buf_free_pages(bp);
720
721 bp->b_pages = NULL;
722 bp->b_page_count = 0;
723 bp->b_addr = NULL;
724 bp->b_file_offset = 0;
725 bp->b_buffer_length = bp->b_count_desired = len;
726 bp->b_bn = XFS_BUF_DADDR_NULL;
727 bp->b_flags &= ~XBF_MAPPED;
728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730static inline struct page *
731mem_to_page(
732 void *addr)
733{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800734 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return virt_to_page(addr);
736 } else {
737 return vmalloc_to_page(addr);
738 }
739}
740
741int
Nathan Scottce8e9222006-01-11 15:39:08 +1100742xfs_buf_associate_memory(
743 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 void *mem,
745 size_t len)
746{
747 int rval;
748 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100749 unsigned long pageaddr;
750 unsigned long offset;
751 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 int page_count;
753
Dave Chinner0e6e8472011-03-26 09:16:45 +1100754 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100755 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100756 buflen = PAGE_ALIGN(len + offset);
757 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100760 if (bp->b_pages)
761 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Nathan Scottce8e9222006-01-11 15:39:08 +1100763 bp->b_pages = NULL;
764 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Christoph Hellwig36fae172009-07-18 18:14:58 -0400766 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (rval)
768 return rval;
769
Nathan Scottce8e9222006-01-11 15:39:08 +1100770 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100772 for (i = 0; i < bp->b_page_count; i++) {
773 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100774 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100777 bp->b_count_desired = len;
778 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100779 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 return 0;
782}
783
784xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000785xfs_buf_get_uncached(
786 struct xfs_buftarg *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 size_t len,
Dave Chinner686865f2010-09-24 20:07:47 +1000788 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000790 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
791 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Nathan Scottce8e9222006-01-11 15:39:08 +1100794 bp = xfs_buf_allocate(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (unlikely(bp == NULL))
796 goto fail;
Nathan Scottce8e9222006-01-11 15:39:08 +1100797 _xfs_buf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000799 error = _xfs_buf_get_pages(bp, page_count, 0);
800 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 goto fail_free_buf;
802
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000803 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000804 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000805 if (!bp->b_pages[i])
806 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000808 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000810 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
811 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100812 xfs_warn(target->bt_mount,
813 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Dave Chinner686865f2010-09-24 20:07:47 +1000817 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000821 while (--i >= 0)
822 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000823 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 fail_free_buf:
Christoph Hellwigca165b82007-05-24 15:21:11 +1000825 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 fail:
827 return NULL;
828}
829
830/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * Increment reference count on buffer, to hold the buffer concurrently
832 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 * Must hold the buffer already to call this function.
834 */
835void
Nathan Scottce8e9222006-01-11 15:39:08 +1100836xfs_buf_hold(
837 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000839 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100840 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100844 * Releases a hold on the specified buffer. If the
845 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 */
847void
Nathan Scottce8e9222006-01-11 15:39:08 +1100848xfs_buf_rele(
849 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Dave Chinner74f75a02010-09-24 19:59:04 +1000851 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000853 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Dave Chinner74f75a02010-09-24 19:59:04 +1000855 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100856 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000857 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100858 if (atomic_dec_and_test(&bp->b_hold))
859 xfs_buf_free(bp);
860 return;
861 }
862
Dave Chinner74f75a02010-09-24 19:59:04 +1000863 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100864
Lachlan McIlroy37906892008-08-13 15:42:10 +1000865 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000866 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000867 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100868 atomic_read(&bp->b_lru_ref)) {
869 xfs_buf_lru_add(bp);
870 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100871 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100872 xfs_buf_lru_del(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +1100873 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
Dave Chinner74f75a02010-09-24 19:59:04 +1000874 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
875 spin_unlock(&pag->pag_buf_lock);
876 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100877 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879 }
880}
881
882
883/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100884 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100885 *
886 * If we come across a stale, pinned, locked buffer, we know that we are
887 * being asked to lock a buffer that has been reallocated. Because it is
888 * pinned, we know that the log has not been pushed to disk and hence it
889 * will still be locked. Rather than continuing to have trylock attempts
890 * fail until someone else pushes the log, push it ourselves before
891 * returning. This means that the xfsaild will not get stuck trying
892 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 */
894int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200895xfs_buf_trylock(
896 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 int locked;
899
Nathan Scottce8e9222006-01-11 15:39:08 +1100900 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000901 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100902 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100903 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
904 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000905
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200906 trace_xfs_buf_trylock(bp, _RET_IP_);
907 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100911 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000912 *
913 * If we come across a stale, pinned, locked buffer, we know that we
914 * are being asked to lock a buffer that has been reallocated. Because
915 * it is pinned, we know that the log has not been pushed to disk and
916 * hence it will still be locked. Rather than sleeping until someone
917 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100919void
920xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200921 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000923 trace_xfs_buf_lock(bp, _RET_IP_);
924
Dave Chinnered3b4d62010-05-21 12:07:08 +1000925 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000926 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100927 down(&bp->b_sema);
928 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000929
930 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
933/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100934 * Releases the lock on the buffer object.
David Chinner2f926582005-09-05 08:33:35 +1000935 * If the buffer is marked delwri but is not queued, do so before we
Nathan Scottce8e9222006-01-11 15:39:08 +1100936 * unlock the buffer as we need to set flags correctly. We also need to
David Chinner2f926582005-09-05 08:33:35 +1000937 * take a reference for the delwri queue because the unlocker is going to
938 * drop their's and they don't know we just queued it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 */
940void
Nathan Scottce8e9222006-01-11 15:39:08 +1100941xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200942 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Nathan Scottce8e9222006-01-11 15:39:08 +1100944 XB_CLEAR_OWNER(bp);
945 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000946
947 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Nathan Scottce8e9222006-01-11 15:39:08 +1100950STATIC void
951xfs_buf_wait_unpin(
952 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 DECLARE_WAITQUEUE (wait, current);
955
Nathan Scottce8e9222006-01-11 15:39:08 +1100956 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return;
958
Nathan Scottce8e9222006-01-11 15:39:08 +1100959 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 for (;;) {
961 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100962 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100964 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100966 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 set_current_state(TASK_RUNNING);
968}
969
970/*
971 * Buffer Utility Routines
972 */
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100975xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000976 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
David Howellsc4028952006-11-22 14:57:56 +0000978 xfs_buf_t *bp =
979 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400981 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100982 (*(bp->b_iodone))(bp);
983 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 xfs_buf_relse(bp);
985}
986
987void
Nathan Scottce8e9222006-01-11 15:39:08 +1100988xfs_buf_ioend(
989 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 int schedule)
991{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000992 trace_xfs_buf_iodone(bp, _RET_IP_);
993
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100994 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100995 if (bp->b_error == 0)
996 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Nathan Scottce8e9222006-01-11 15:39:08 +1100998 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +00001000 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +11001001 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 } else {
David Howellsc4028952006-11-22 14:57:56 +00001003 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +10001006 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010void
Nathan Scottce8e9222006-01-11 15:39:08 +11001011xfs_buf_ioerror(
1012 xfs_buf_t *bp,
1013 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001016 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001017 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001021xfs_bwrite(
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001022 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001024 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001026 bp->b_flags |= XBF_WRITE;
Christoph Hellwig8c383662010-03-12 10:59:40 +00001027 bp->b_flags &= ~(XBF_ASYNC | XBF_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001029 xfs_buf_delwri_dequeue(bp);
Christoph Hellwig939d7232010-07-20 17:51:16 +10001030 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Christoph Hellwig8c383662010-03-12 10:59:40 +00001032 error = xfs_buf_iowait(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001033 if (error) {
1034 xfs_force_shutdown(bp->b_target->bt_mount,
1035 SHUTDOWN_META_IO_ERROR);
1036 }
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001037 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001038}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Christoph Hellwig4e234712010-01-13 22:17:56 +00001040/*
1041 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001042 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001043 * so that the proper iodone callbacks get called.
1044 */
1045STATIC int
1046xfs_bioerror(
1047 xfs_buf_t *bp)
1048{
1049#ifdef XFSERRORDEBUG
1050 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1051#endif
1052
1053 /*
1054 * No need to wait until the buffer is unpinned, we aren't flushing it.
1055 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001056 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001057
1058 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001059 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001060 */
1061 XFS_BUF_UNREAD(bp);
Christoph Hellwig61551f12011-08-23 08:28:06 +00001062 xfs_buf_delwri_dequeue(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001063 XFS_BUF_UNDONE(bp);
1064 XFS_BUF_STALE(bp);
1065
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001066 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001067
1068 return EIO;
1069}
1070
1071/*
1072 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001073 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001074 * This is meant for userdata errors; metadata bufs come with
1075 * iodone functions attached, so that we can track down errors.
1076 */
1077STATIC int
1078xfs_bioerror_relse(
1079 struct xfs_buf *bp)
1080{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001081 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001082 /*
1083 * No need to wait until the buffer is unpinned.
1084 * We aren't flushing it.
1085 *
1086 * chunkhold expects B_DONE to be set, whether
1087 * we actually finish the I/O or not. We don't want to
1088 * change that interface.
1089 */
1090 XFS_BUF_UNREAD(bp);
Christoph Hellwig61551f12011-08-23 08:28:06 +00001091 xfs_buf_delwri_dequeue(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001092 XFS_BUF_DONE(bp);
1093 XFS_BUF_STALE(bp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001094 bp->b_iodone = NULL;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001095 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001096 /*
1097 * Mark b_error and B_ERROR _both_.
1098 * Lot's of chunkcache code assumes that.
1099 * There's no reason to mark error for
1100 * ASYNC buffers.
1101 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001102 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001103 XFS_BUF_FINISH_IOWAIT(bp);
1104 } else {
1105 xfs_buf_relse(bp);
1106 }
1107
1108 return EIO;
1109}
1110
1111
1112/*
1113 * All xfs metadata buffers except log state machine buffers
1114 * get this attached as their b_bdstrat callback function.
1115 * This is so that we can catch a buffer
1116 * after prematurely unpinning it to forcibly shutdown the filesystem.
1117 */
1118int
1119xfs_bdstrat_cb(
1120 struct xfs_buf *bp)
1121{
Dave Chinnerebad8612010-09-22 10:47:20 +10001122 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001123 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1124 /*
1125 * Metadata write that didn't get logged but
1126 * written delayed anyway. These aren't associated
1127 * with a transaction, and can be ignored.
1128 */
1129 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1130 return xfs_bioerror_relse(bp);
1131 else
1132 return xfs_bioerror(bp);
1133 }
1134
1135 xfs_buf_iorequest(bp);
1136 return 0;
1137}
1138
1139/*
1140 * Wrapper around bdstrat so that we can stop data from going to disk in case
1141 * we are shutting down the filesystem. Typically user data goes thru this
1142 * path; one of the exceptions is the superblock.
1143 */
1144void
1145xfsbdstrat(
1146 struct xfs_mount *mp,
1147 struct xfs_buf *bp)
1148{
1149 if (XFS_FORCED_SHUTDOWN(mp)) {
1150 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1151 xfs_bioerror_relse(bp);
1152 return;
1153 }
1154
1155 xfs_buf_iorequest(bp);
1156}
1157
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001158STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001159_xfs_buf_ioend(
1160 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 int schedule)
1162{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001163 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001164 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
Al Viro782e3b32007-10-12 07:17:47 +01001167STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001168xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 int error)
1171{
Nathan Scottce8e9222006-01-11 15:39:08 +11001172 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001174 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
James Bottomley73c77e22010-01-25 11:42:24 -06001176 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1177 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1178
Nathan Scottce8e9222006-01-11 15:39:08 +11001179 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
1183STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001184_xfs_buf_ioapply(
1185 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001187 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001189 int offset = bp->b_offset;
1190 int size = bp->b_count_desired;
1191 sector_t sector = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Nathan Scottce8e9222006-01-11 15:39:08 +11001193 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 map_i = 0;
1195
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001196 if (bp->b_flags & XBF_WRITE) {
1197 if (bp->b_flags & XBF_SYNCIO)
1198 rw = WRITE_SYNC;
1199 else
1200 rw = WRITE;
1201 if (bp->b_flags & XBF_FUA)
1202 rw |= REQ_FUA;
1203 if (bp->b_flags & XBF_FLUSH)
1204 rw |= REQ_FLUSH;
1205 } else if (bp->b_flags & XBF_READ_AHEAD) {
1206 rw = READA;
Nathan Scott51bdd702006-09-28 11:01:57 +10001207 } else {
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001208 rw = READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001209 }
1210
Christoph Hellwig34951f52011-07-26 15:06:44 +00001211 /* we only use the buffer cache for meta-data */
1212 rw |= REQ_META;
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001215 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1217 if (nr_pages > total_nr_pages)
1218 nr_pages = total_nr_pages;
1219
1220 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001221 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001223 bio->bi_end_io = xfs_buf_bio_end_io;
1224 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Dave Chinner0e6e8472011-03-26 09:16:45 +11001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 for (; size && nr_pages; nr_pages--, map_i++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001228 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 if (nbytes > size)
1231 nbytes = size;
1232
Nathan Scottce8e9222006-01-11 15:39:08 +11001233 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1234 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 break;
1236
1237 offset = 0;
1238 sector += nbytes >> BBSHIFT;
1239 size -= nbytes;
1240 total_nr_pages--;
1241 }
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001244 if (xfs_buf_is_vmapped(bp)) {
1245 flush_kernel_vmap_range(bp->b_addr,
1246 xfs_buf_vmap_len(bp));
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 submit_bio(rw, bio);
1249 if (size)
1250 goto next_chunk;
1251 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001252 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001253 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255}
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257int
Nathan Scottce8e9222006-01-11 15:39:08 +11001258xfs_buf_iorequest(
1259 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001261 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Christoph Hellwig375ec69d2011-08-23 08:28:03 +00001263 ASSERT(!(bp->b_flags & XBF_DELWRI));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Christoph Hellwig375ec69d2011-08-23 08:28:03 +00001265 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001266 xfs_buf_wait_unpin(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001267 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 /* Set the count to 1 initially, this will stop an I/O
1270 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001271 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001273 atomic_set(&bp->b_io_remaining, 1);
1274 _xfs_buf_ioapply(bp);
1275 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Nathan Scottce8e9222006-01-11 15:39:08 +11001277 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return 0;
1279}
1280
1281/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001282 * Waits for I/O to complete on the buffer supplied.
1283 * It returns immediately if no I/O is pending.
1284 * It returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 */
1286int
Nathan Scottce8e9222006-01-11 15:39:08 +11001287xfs_buf_iowait(
1288 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001290 trace_xfs_buf_iowait(bp, _RET_IP_);
1291
David Chinnerb4dd3302008-08-13 16:36:11 +10001292 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001293
1294 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001295 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Nathan Scottce8e9222006-01-11 15:39:08 +11001298xfs_caddr_t
1299xfs_buf_offset(
1300 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 size_t offset)
1302{
1303 struct page *page;
1304
Nathan Scottce8e9222006-01-11 15:39:08 +11001305 if (bp->b_flags & XBF_MAPPED)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001306 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Nathan Scottce8e9222006-01-11 15:39:08 +11001308 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001309 page = bp->b_pages[offset >> PAGE_SHIFT];
1310 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * Move data into or out of a buffer.
1315 */
1316void
Nathan Scottce8e9222006-01-11 15:39:08 +11001317xfs_buf_iomove(
1318 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 size_t boff, /* starting buffer offset */
1320 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001321 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001322 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 size_t bend, cpoff, csize;
1325 struct page *page;
1326
1327 bend = boff + bsize;
1328 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001329 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1330 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 csize = min_t(size_t,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001332 PAGE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Dave Chinner0e6e8472011-03-26 09:16:45 +11001334 ASSERT(((csize + cpoff) <= PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001337 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 memset(page_address(page) + cpoff, 0, csize);
1339 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001340 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 memcpy(data, page_address(page) + cpoff, csize);
1342 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001343 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 memcpy(page_address(page) + cpoff, data, csize);
1345 }
1346
1347 boff += csize;
1348 data += csize;
1349 }
1350}
1351
1352/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001353 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 */
1355
1356/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001357 * Wait for any bufs with callbacks that have been submitted but have not yet
1358 * returned. These buffers will have an elevated hold count, so wait on those
1359 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 */
1361void
1362xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001363 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Dave Chinner430cbeb2010-12-02 16:30:55 +11001365 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Dave Chinner430cbeb2010-12-02 16:30:55 +11001367restart:
1368 spin_lock(&btp->bt_lru_lock);
1369 while (!list_empty(&btp->bt_lru)) {
1370 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1371 if (atomic_read(&bp->b_hold) > 1) {
1372 spin_unlock(&btp->bt_lru_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001373 delay(100);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001374 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001376 /*
1377 * clear the LRU reference count so the bufer doesn't get
1378 * ignored in xfs_buf_rele().
1379 */
1380 atomic_set(&bp->b_lru_ref, 0);
1381 spin_unlock(&btp->bt_lru_lock);
1382 xfs_buf_rele(bp);
1383 spin_lock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001385 spin_unlock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Dave Chinnerff57ab22010-11-30 17:27:57 +11001388int
1389xfs_buftarg_shrink(
1390 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001391 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001392{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001393 struct xfs_buftarg *btp = container_of(shrink,
1394 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001395 struct xfs_buf *bp;
Ying Han1495f232011-05-24 17:12:27 -07001396 int nr_to_scan = sc->nr_to_scan;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001397 LIST_HEAD(dispose);
1398
1399 if (!nr_to_scan)
1400 return btp->bt_lru_nr;
1401
1402 spin_lock(&btp->bt_lru_lock);
1403 while (!list_empty(&btp->bt_lru)) {
1404 if (nr_to_scan-- <= 0)
1405 break;
1406
1407 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1408
1409 /*
1410 * Decrement the b_lru_ref count unless the value is already
1411 * zero. If the value is already zero, we need to reclaim the
1412 * buffer, otherwise it gets another trip through the LRU.
1413 */
1414 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1415 list_move_tail(&bp->b_lru, &btp->bt_lru);
1416 continue;
1417 }
1418
1419 /*
1420 * remove the buffer from the LRU now to avoid needing another
1421 * lock round trip inside xfs_buf_rele().
1422 */
1423 list_move(&bp->b_lru, &dispose);
1424 btp->bt_lru_nr--;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001425 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001426 spin_unlock(&btp->bt_lru_lock);
1427
1428 while (!list_empty(&dispose)) {
1429 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1430 list_del_init(&bp->b_lru);
1431 xfs_buf_rele(bp);
1432 }
1433
1434 return btp->bt_lru_nr;
David Chinnera6867a62006-01-11 15:37:58 +11001435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437void
1438xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001439 struct xfs_mount *mp,
1440 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001442 unregister_shrinker(&btp->bt_shrinker);
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 xfs_flush_buftarg(btp, 1);
Christoph Hellwigb7963132009-03-03 14:48:37 -05001445 if (mp->m_flags & XFS_MOUNT_BARRIER)
1446 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001447
David Chinnera6867a62006-01-11 15:37:58 +11001448 kthread_stop(btp->bt_task);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001449 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452STATIC int
1453xfs_setsize_buftarg_flags(
1454 xfs_buftarg_t *btp,
1455 unsigned int blocksize,
1456 unsigned int sectorsize,
1457 int verbose)
1458{
Nathan Scottce8e9222006-01-11 15:39:08 +11001459 btp->bt_bsize = blocksize;
1460 btp->bt_sshift = ffs(sectorsize) - 1;
1461 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Nathan Scottce8e9222006-01-11 15:39:08 +11001463 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001464 xfs_warn(btp->bt_mount,
1465 "Cannot set_blocksize to %u on device %s\n",
Chandra Seetharamanc35a5492011-07-22 23:40:46 +00001466 sectorsize, xfs_buf_target_name(btp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return EINVAL;
1468 }
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return 0;
1471}
1472
1473/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001474 * When allocating the initial buffer target we have not yet
1475 * read in the superblock, so don't know what sized sectors
1476 * are being used is at this early stage. Play safe.
1477 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478STATIC int
1479xfs_setsize_buftarg_early(
1480 xfs_buftarg_t *btp,
1481 struct block_device *bdev)
1482{
1483 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001484 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487int
1488xfs_setsize_buftarg(
1489 xfs_buftarg_t *btp,
1490 unsigned int blocksize,
1491 unsigned int sectorsize)
1492{
1493 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1494}
1495
1496STATIC int
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001497xfs_alloc_delwri_queue(
Jan Engelhardte2a07812010-03-23 09:52:55 +11001498 xfs_buftarg_t *btp,
1499 const char *fsname)
David Chinnera6867a62006-01-11 15:37:58 +11001500{
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001501 INIT_LIST_HEAD(&btp->bt_delwri_queue);
1502 spin_lock_init(&btp->bt_delwri_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001503 btp->bt_flags = 0;
Jan Engelhardte2a07812010-03-23 09:52:55 +11001504 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd/%s", fsname);
Dave Chinnerff57ab22010-11-30 17:27:57 +11001505 if (IS_ERR(btp->bt_task))
1506 return PTR_ERR(btp->bt_task);
1507 return 0;
David Chinnera6867a62006-01-11 15:37:58 +11001508}
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510xfs_buftarg_t *
1511xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001512 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001514 int external,
1515 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 xfs_buftarg_t *btp;
1518
1519 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1520
Dave Chinnerebad8612010-09-22 10:47:20 +10001521 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001522 btp->bt_dev = bdev->bd_dev;
1523 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001524 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1525 if (!btp->bt_bdi)
1526 goto error;
1527
Dave Chinner430cbeb2010-12-02 16:30:55 +11001528 INIT_LIST_HEAD(&btp->bt_lru);
1529 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (xfs_setsize_buftarg_early(btp, bdev))
1531 goto error;
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001532 if (xfs_alloc_delwri_queue(btp, fsname))
David Chinnera6867a62006-01-11 15:37:58 +11001533 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001534 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1535 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1536 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return btp;
1538
1539error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001540 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return NULL;
1542}
1543
1544
1545/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001546 * Delayed write buffer handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 */
Christoph Hellwig61551f12011-08-23 08:28:06 +00001548void
Nathan Scottce8e9222006-01-11 15:39:08 +11001549xfs_buf_delwri_queue(
Christoph Hellwig527cfdf2011-08-23 08:28:04 +00001550 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001552 struct xfs_buftarg *btp = bp->b_target;
David Chinnera6867a62006-01-11 15:37:58 +11001553
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001554 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1555
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001556 ASSERT(!(bp->b_flags & XBF_READ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001558 spin_lock(&btp->bt_delwri_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +11001559 if (!list_empty(&bp->b_list)) {
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001560 /* if already in the queue, move it to the tail */
Nathan Scottce8e9222006-01-11 15:39:08 +11001561 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001562 list_move_tail(&bp->b_list, &btp->bt_delwri_queue);
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001563 } else {
Dave Chinnerc9c12972010-01-11 11:49:59 +00001564 /* start xfsbufd as it is about to have something to do */
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001565 if (list_empty(&btp->bt_delwri_queue))
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001566 wake_up_process(bp->b_target->bt_task);
Dave Chinnerc9c12972010-01-11 11:49:59 +00001567
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001568 atomic_inc(&bp->b_hold);
1569 bp->b_flags |= XBF_DELWRI | _XBF_DELWRI_Q | XBF_ASYNC;
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001570 list_add_tail(&bp->b_list, &btp->bt_delwri_queue);
Christoph Hellwig5a8ee6b2011-08-23 08:28:05 +00001571 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001572 bp->b_queuetime = jiffies;
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001573 spin_unlock(&btp->bt_delwri_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
1576void
Nathan Scottce8e9222006-01-11 15:39:08 +11001577xfs_buf_delwri_dequeue(
1578 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 int dequeued = 0;
1581
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001582 spin_lock(&bp->b_target->bt_delwri_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +11001583 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1584 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1585 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 dequeued = 1;
1587 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001588 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001589 spin_unlock(&bp->b_target->bt_delwri_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 if (dequeued)
Nathan Scottce8e9222006-01-11 15:39:08 +11001592 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001594 trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
Dave Chinnerd808f612010-02-02 10:13:42 +11001597/*
1598 * If a delwri buffer needs to be pushed before it has aged out, then promote
1599 * it to the head of the delwri queue so that it will be flushed on the next
1600 * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
1601 * than the age currently needed to flush the buffer. Hence the next time the
1602 * xfsbufd sees it is guaranteed to be considered old enough to flush.
1603 */
1604void
1605xfs_buf_delwri_promote(
1606 struct xfs_buf *bp)
1607{
1608 struct xfs_buftarg *btp = bp->b_target;
1609 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
1610
1611 ASSERT(bp->b_flags & XBF_DELWRI);
1612 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1613
1614 /*
1615 * Check the buffer age before locking the delayed write queue as we
1616 * don't need to promote buffers that are already past the flush age.
1617 */
1618 if (bp->b_queuetime < jiffies - age)
1619 return;
1620 bp->b_queuetime = jiffies - age;
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001621 spin_lock(&btp->bt_delwri_lock);
1622 list_move(&bp->b_list, &btp->bt_delwri_queue);
1623 spin_unlock(&btp->bt_delwri_lock);
Dave Chinnerd808f612010-02-02 10:13:42 +11001624}
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001627xfs_buf_runall_queues(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 struct workqueue_struct *queue)
1629{
1630 flush_workqueue(queue);
1631}
1632
David Chinner585e6d82007-02-10 18:32:29 +11001633/*
1634 * Move as many buffers as specified to the supplied list
1635 * idicating if we skipped any buffers to prevent deadlocks.
1636 */
1637STATIC int
1638xfs_buf_delwri_split(
1639 xfs_buftarg_t *target,
1640 struct list_head *list,
David Chinner5e6a07d2007-02-10 18:34:49 +11001641 unsigned long age)
David Chinner585e6d82007-02-10 18:32:29 +11001642{
1643 xfs_buf_t *bp, *n;
David Chinner585e6d82007-02-10 18:32:29 +11001644 int skipped = 0;
David Chinner5e6a07d2007-02-10 18:34:49 +11001645 int force;
David Chinner585e6d82007-02-10 18:32:29 +11001646
David Chinner5e6a07d2007-02-10 18:34:49 +11001647 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
David Chinner585e6d82007-02-10 18:32:29 +11001648 INIT_LIST_HEAD(list);
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001649 spin_lock(&target->bt_delwri_lock);
1650 list_for_each_entry_safe(bp, n, &target->bt_delwri_queue, b_list) {
David Chinner585e6d82007-02-10 18:32:29 +11001651 ASSERT(bp->b_flags & XBF_DELWRI);
1652
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00001653 if (!xfs_buf_ispinned(bp) && xfs_buf_trylock(bp)) {
David Chinner5e6a07d2007-02-10 18:34:49 +11001654 if (!force &&
David Chinner585e6d82007-02-10 18:32:29 +11001655 time_before(jiffies, bp->b_queuetime + age)) {
1656 xfs_buf_unlock(bp);
1657 break;
1658 }
1659
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001660 bp->b_flags &= ~(XBF_DELWRI | _XBF_DELWRI_Q);
David Chinner585e6d82007-02-10 18:32:29 +11001661 bp->b_flags |= XBF_WRITE;
1662 list_move_tail(&bp->b_list, list);
Dave Chinnerbfe27412010-11-08 08:55:05 +00001663 trace_xfs_buf_delwri_split(bp, _RET_IP_);
David Chinner585e6d82007-02-10 18:32:29 +11001664 } else
1665 skipped++;
1666 }
David Chinner585e6d82007-02-10 18:32:29 +11001667
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001668 spin_unlock(&target->bt_delwri_lock);
David Chinner585e6d82007-02-10 18:32:29 +11001669 return skipped;
David Chinner585e6d82007-02-10 18:32:29 +11001670}
1671
Dave Chinner089716a2010-01-26 15:13:25 +11001672/*
1673 * Compare function is more complex than it needs to be because
1674 * the return value is only 32 bits and we are doing comparisons
1675 * on 64 bit values
1676 */
1677static int
1678xfs_buf_cmp(
1679 void *priv,
1680 struct list_head *a,
1681 struct list_head *b)
1682{
1683 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1684 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1685 xfs_daddr_t diff;
1686
1687 diff = ap->b_bn - bp->b_bn;
1688 if (diff < 0)
1689 return -1;
1690 if (diff > 0)
1691 return 1;
1692 return 0;
1693}
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001696xfsbufd(
David Chinner585e6d82007-02-10 18:32:29 +11001697 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Dave Chinner089716a2010-01-26 15:13:25 +11001699 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 current->flags |= PF_MEMALLOC;
1702
Rafael J. Wysocki978c7b22007-12-07 14:09:02 +11001703 set_freezable();
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 do {
Dave Chinnerc9c12972010-01-11 11:49:59 +00001706 long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
1707 long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
Dave Chinner089716a2010-01-26 15:13:25 +11001708 struct list_head tmp;
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001709 struct blk_plug plug;
Dave Chinnerc9c12972010-01-11 11:49:59 +00001710
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001711 if (unlikely(freezing(current))) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001712 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001713 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001714 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001715 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Nathan Scottabd0cf72005-05-05 13:30:13 -07001716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Dave Chinnerc9c12972010-01-11 11:49:59 +00001718 /* sleep for a long time if there is nothing to do. */
Christoph Hellwigc4e1c092011-08-23 08:28:08 +00001719 if (list_empty(&target->bt_delwri_queue))
Dave Chinnerc9c12972010-01-11 11:49:59 +00001720 tout = MAX_SCHEDULE_TIMEOUT;
1721 schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Dave Chinnerc9c12972010-01-11 11:49:59 +00001723 xfs_buf_delwri_split(target, &tmp, age);
Dave Chinner089716a2010-01-26 15:13:25 +11001724 list_sort(NULL, &tmp, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001725
1726 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 while (!list_empty(&tmp)) {
Dave Chinner089716a2010-01-26 15:13:25 +11001728 struct xfs_buf *bp;
1729 bp = list_first_entry(&tmp, struct xfs_buf, b_list);
Nathan Scottce8e9222006-01-11 15:39:08 +11001730 list_del_init(&bp->b_list);
Christoph Hellwig939d7232010-07-20 17:51:16 +10001731 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001733 blk_finish_plug(&plug);
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001734 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
1739/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001740 * Go through all incore buffers, and release buffers if they belong to
1741 * the given device. This is used in filesystem error handling to
1742 * preserve the consistency of its metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
1744int
1745xfs_flush_buftarg(
David Chinner585e6d82007-02-10 18:32:29 +11001746 xfs_buftarg_t *target,
1747 int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Dave Chinner089716a2010-01-26 15:13:25 +11001749 xfs_buf_t *bp;
David Chinner585e6d82007-02-10 18:32:29 +11001750 int pincount = 0;
Dave Chinner089716a2010-01-26 15:13:25 +11001751 LIST_HEAD(tmp_list);
1752 LIST_HEAD(wait_list);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001753 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Dave Chinnerc626d172009-04-06 18:42:11 +02001755 xfs_buf_runall_queues(xfsconvertd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001756 xfs_buf_runall_queues(xfsdatad_workqueue);
1757 xfs_buf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
David Chinner5e6a07d2007-02-10 18:34:49 +11001759 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
Dave Chinner089716a2010-01-26 15:13:25 +11001760 pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 /*
Dave Chinner089716a2010-01-26 15:13:25 +11001763 * Dropped the delayed write list lock, now walk the temporary list.
1764 * All I/O is issued async and then if we need to wait for completion
1765 * we do that after issuing all the IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 */
Dave Chinner089716a2010-01-26 15:13:25 +11001767 list_sort(NULL, &tmp_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001768
1769 blk_start_plug(&plug);
Dave Chinner089716a2010-01-26 15:13:25 +11001770 while (!list_empty(&tmp_list)) {
1771 bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
David Chinner585e6d82007-02-10 18:32:29 +11001772 ASSERT(target == bp->b_target);
Dave Chinner089716a2010-01-26 15:13:25 +11001773 list_del_init(&bp->b_list);
1774 if (wait) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001775 bp->b_flags &= ~XBF_ASYNC;
Dave Chinner089716a2010-01-26 15:13:25 +11001776 list_add(&bp->b_list, &wait_list);
1777 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001778 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001780 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Dave Chinner089716a2010-01-26 15:13:25 +11001782 if (wait) {
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001783 /* Wait for IO to complete. */
Dave Chinner089716a2010-01-26 15:13:25 +11001784 while (!list_empty(&wait_list)) {
1785 bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
Nathan Scottf07c2252006-09-28 10:52:15 +10001786
Dave Chinner089716a2010-01-26 15:13:25 +11001787 list_del_init(&bp->b_list);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001788 xfs_buf_iowait(bp);
Dave Chinner089716a2010-01-26 15:13:25 +11001789 xfs_buf_relse(bp);
1790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return pincount;
1794}
1795
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001796int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001797xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Nathan Scott87582802006-03-14 13:18:19 +11001799 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1800 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001801 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001802 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001803
Dave Chinner51749e42010-09-08 09:00:22 +00001804 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001805 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001806 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001807 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Tejun Heo83e75902011-02-01 11:42:43 +01001809 xfsdatad_workqueue = alloc_workqueue("xfsdatad", WQ_MEM_RECLAIM, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001810 if (!xfsdatad_workqueue)
1811 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Tejun Heo83e75902011-02-01 11:42:43 +01001813 xfsconvertd_workqueue = alloc_workqueue("xfsconvertd",
1814 WQ_MEM_RECLAIM, 1);
Dave Chinnerc626d172009-04-06 18:42:11 +02001815 if (!xfsconvertd_workqueue)
1816 goto out_destroy_xfsdatad_workqueue;
1817
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Dave Chinnerc626d172009-04-06 18:42:11 +02001820 out_destroy_xfsdatad_workqueue:
1821 destroy_workqueue(xfsdatad_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001822 out_destroy_xfslogd_workqueue:
1823 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001824 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001825 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001826 out:
Nathan Scott87582802006-03-14 13:18:19 +11001827 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830void
Nathan Scottce8e9222006-01-11 15:39:08 +11001831xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Dave Chinnerc626d172009-04-06 18:42:11 +02001833 destroy_workqueue(xfsconvertd_workqueue);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001834 destroy_workqueue(xfsdatad_workqueue);
1835 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001836 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001838
1839#ifdef CONFIG_KDB_MODULES
1840struct list_head *
1841xfs_get_buftarg_list(void)
1842{
1843 return &xfs_buftarg_list;
1844}
1845#endif