blob: d3a1974c91d5fe1c8a8ac663bb743aaa2c92df31 [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;
Christoph Hellwig23ea4032005-06-21 15:14:01 +100045
David Chinner7989cb82007-02-10 18:34:56 +110046static struct workqueue_struct *xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Nathan Scottce8e9222006-01-11 15:39:08 +110048#ifdef XFS_BUF_LOCK_TRACKING
49# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
50# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
51# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#else
Nathan Scottce8e9222006-01-11 15:39:08 +110053# define XB_SET_OWNER(bp) do { } while (0)
54# define XB_CLEAR_OWNER(bp) do { } while (0)
55# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57
Nathan Scottce8e9222006-01-11 15:39:08 +110058#define xb_to_gfp(flags) \
59 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
60 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Nathan Scottce8e9222006-01-11 15:39:08 +110062#define xb_to_km(flags) \
63 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
James Bottomley73c77e22010-01-25 11:42:24 -060066static inline int
67xfs_buf_is_vmapped(
68 struct xfs_buf *bp)
69{
70 /*
71 * Return true if the buffer is vmapped.
72 *
73 * The XBF_MAPPED flag is set if the buffer should be mapped, but the
74 * code is clever enough to know it doesn't have to map a single page,
75 * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
76 */
77 return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
78}
79
80static inline int
81xfs_buf_vmap_len(
82 struct xfs_buf *bp)
83{
84 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
85}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
Dave Chinner430cbeb2010-12-02 16:30:55 +110088 * xfs_buf_lru_add - add a buffer to the LRU.
89 *
90 * The LRU takes a new reference to the buffer so that it will only be freed
91 * once the shrinker takes the buffer off the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
Dave Chinner430cbeb2010-12-02 16:30:55 +110093STATIC void
94xfs_buf_lru_add(
95 struct xfs_buf *bp)
96{
97 struct xfs_buftarg *btp = bp->b_target;
98
99 spin_lock(&btp->bt_lru_lock);
100 if (list_empty(&bp->b_lru)) {
101 atomic_inc(&bp->b_hold);
102 list_add_tail(&bp->b_lru, &btp->bt_lru);
103 btp->bt_lru_nr++;
104 }
105 spin_unlock(&btp->bt_lru_lock);
106}
107
108/*
109 * xfs_buf_lru_del - remove a buffer from the LRU
110 *
111 * The unlocked check is safe here because it only occurs when there are not
112 * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there
113 * to optimise the shrinker removing the buffer from the LRU and calling
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300114 * xfs_buf_free(). i.e. it removes an unnecessary round trip on the
Dave Chinner430cbeb2010-12-02 16:30:55 +1100115 * bt_lru_lock.
116 */
117STATIC void
118xfs_buf_lru_del(
119 struct xfs_buf *bp)
120{
121 struct xfs_buftarg *btp = bp->b_target;
122
123 if (list_empty(&bp->b_lru))
124 return;
125
126 spin_lock(&btp->bt_lru_lock);
127 if (!list_empty(&bp->b_lru)) {
128 list_del_init(&bp->b_lru);
129 btp->bt_lru_nr--;
130 }
131 spin_unlock(&btp->bt_lru_lock);
132}
133
134/*
135 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
136 * b_lru_ref count so that the buffer is freed immediately when the buffer
137 * reference count falls to zero. If the buffer is already on the LRU, we need
138 * to remove the reference that LRU holds on the buffer.
139 *
140 * This prevents build-up of stale buffers on the LRU.
141 */
142void
143xfs_buf_stale(
144 struct xfs_buf *bp)
145{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000146 ASSERT(xfs_buf_islocked(bp));
147
Dave Chinner430cbeb2010-12-02 16:30:55 +1100148 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000149
150 /*
151 * Clear the delwri status so that a delwri queue walker will not
152 * flush this buffer to disk now that it is stale. The delwri queue has
153 * a reference to the buffer, so this is safe to do.
154 */
155 bp->b_flags &= ~_XBF_DELWRI_Q;
156
Dave Chinner430cbeb2010-12-02 16:30:55 +1100157 atomic_set(&(bp)->b_lru_ref, 0);
158 if (!list_empty(&bp->b_lru)) {
159 struct xfs_buftarg *btp = bp->b_target;
160
161 spin_lock(&btp->bt_lru_lock);
162 if (!list_empty(&bp->b_lru)) {
163 list_del_init(&bp->b_lru);
164 btp->bt_lru_nr--;
165 atomic_dec(&bp->b_hold);
166 }
167 spin_unlock(&btp->bt_lru_lock);
168 }
169 ASSERT(atomic_read(&bp->b_hold) >= 1);
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000172struct xfs_buf *
173xfs_buf_alloc(
174 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000175 xfs_daddr_t blkno,
176 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100177 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000179 struct xfs_buf *bp;
180
Dave Chinnerbf813cd2012-04-23 15:58:48 +1000181 bp = kmem_zone_zalloc(xfs_buf_zone, xb_to_km(flags));
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000182 if (unlikely(!bp))
183 return NULL;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100186 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100188 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Nathan Scottce8e9222006-01-11 15:39:08 +1100190 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100191 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000192 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100193 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100194 INIT_LIST_HEAD(&bp->b_list);
Dave Chinner74f75a02010-09-24 19:59:04 +1000195 RB_CLEAR_NODE(&bp->b_rbnode);
Thomas Gleixnera731cd12010-09-07 14:33:15 +0000196 sema_init(&bp->b_sema, 0); /* held, no waiters */
Nathan Scottce8e9222006-01-11 15:39:08 +1100197 XB_SET_OWNER(bp);
198 bp->b_target = target;
Dave Chinnere70b73f2012-04-23 15:58:49 +1000199 bp->b_file_offset = blkno << BBSHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /*
201 * Set buffer_length and count_desired to the same value initially.
202 * I/O routines should use count_desired, which will be the same in
203 * most cases but may be reset (e.g. XFS recovery).
204 */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000205 bp->b_buffer_length = bp->b_count_desired = numblks << BBSHIFT;
Nathan Scottce8e9222006-01-11 15:39:08 +1100206 bp->b_flags = flags;
Dave Chinnere70b73f2012-04-23 15:58:49 +1000207
208 /*
209 * We do not set the block number here in the buffer because we have not
210 * finished initialising the buffer. We insert the buffer into the cache
211 * in this state, so this ensures that we are unable to do IO on a
212 * buffer that hasn't been fully initialised.
213 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100214 bp->b_bn = XFS_BUF_DADDR_NULL;
215 atomic_set(&bp->b_pin_count, 0);
216 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Nathan Scottce8e9222006-01-11 15:39:08 +1100218 XFS_STATS_INC(xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000219 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000220
221 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100225 * Allocate a page array capable of holding a specified number
226 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
228STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100229_xfs_buf_get_pages(
230 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100232 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100235 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100236 bp->b_page_count = page_count;
237 if (page_count <= XB_PAGES) {
238 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100240 bp->b_pages = kmem_alloc(sizeof(struct page *) *
241 page_count, xb_to_km(flags));
242 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return -ENOMEM;
244 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100245 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 return 0;
248}
249
250/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100251 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 */
253STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100254_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 xfs_buf_t *bp)
256{
Nathan Scottce8e9222006-01-11 15:39:08 +1100257 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000258 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000259 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261}
262
263/*
264 * Releases the specified buffer.
265 *
266 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100267 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * hashed and refcounted buffers
269 */
270void
Nathan Scottce8e9222006-01-11 15:39:08 +1100271xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 xfs_buf_t *bp)
273{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000274 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Dave Chinner430cbeb2010-12-02 16:30:55 +1100276 ASSERT(list_empty(&bp->b_lru));
277
Dave Chinner0e6e8472011-03-26 09:16:45 +1100278 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 uint i;
280
James Bottomley73c77e22010-01-25 11:42:24 -0600281 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000282 vm_unmap_ram(bp->b_addr - bp->b_offset,
283 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Nathan Scott948ecdb2006-09-28 11:03:13 +1000285 for (i = 0; i < bp->b_page_count; i++) {
286 struct page *page = bp->b_pages[i];
287
Dave Chinner0e6e8472011-03-26 09:16:45 +1100288 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000289 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100290 } else if (bp->b_flags & _XBF_KMEM)
291 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000292 _xfs_buf_free_pages(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000293 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100297 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
299STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100300xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 xfs_buf_t *bp,
302 uint flags)
303{
Nathan Scottce8e9222006-01-11 15:39:08 +1100304 size_t size = bp->b_count_desired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100306 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 unsigned short page_count, i;
Nathan Scott204ab252006-01-11 20:50:22 +1100308 xfs_off_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 int error;
310
Dave Chinner0e6e8472011-03-26 09:16:45 +1100311 /*
312 * for buffers that are contained within a single page, just allocate
313 * the memory from the heap - there's no need for the complexity of
314 * page arrays to keep allocation down to order 0.
315 */
316 if (bp->b_buffer_length < PAGE_SIZE) {
317 bp->b_addr = kmem_alloc(bp->b_buffer_length, xb_to_km(flags));
318 if (!bp->b_addr) {
319 /* low memory - use alloc_page loop instead */
320 goto use_alloc_page;
321 }
322
323 if (((unsigned long)(bp->b_addr + bp->b_buffer_length - 1) &
324 PAGE_MASK) !=
325 ((unsigned long)bp->b_addr & PAGE_MASK)) {
326 /* b_addr spans two pages - use alloc_page instead */
327 kmem_free(bp->b_addr);
328 bp->b_addr = NULL;
329 goto use_alloc_page;
330 }
331 bp->b_offset = offset_in_page(bp->b_addr);
332 bp->b_pages = bp->b_page_array;
333 bp->b_pages[0] = virt_to_page(bp->b_addr);
334 bp->b_page_count = 1;
335 bp->b_flags |= XBF_MAPPED | _XBF_KMEM;
336 return 0;
337 }
338
339use_alloc_page:
Nathan Scottce8e9222006-01-11 15:39:08 +1100340 end = bp->b_file_offset + bp->b_buffer_length;
341 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
Nathan Scottce8e9222006-01-11 15:39:08 +1100342 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (unlikely(error))
344 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Nathan Scottce8e9222006-01-11 15:39:08 +1100346 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100347 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Nathan Scottce8e9222006-01-11 15:39:08 +1100349 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct page *page;
351 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100352retry:
353 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100355 if (flags & XBF_READ_AHEAD) {
356 bp->b_page_count = i;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100357 error = ENOMEM;
358 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360
361 /*
362 * This could deadlock.
363 *
364 * But until all the XFS lowlevel code is revamped to
365 * handle buffer allocation failures we can't do much.
366 */
367 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100368 xfs_err(NULL,
369 "possible memory allocation deadlock in %s (mode:0x%x)",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000370 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Nathan Scottce8e9222006-01-11 15:39:08 +1100372 XFS_STATS_INC(xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200373 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 goto retry;
375 }
376
Nathan Scottce8e9222006-01-11 15:39:08 +1100377 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Dave Chinner0e6e8472011-03-26 09:16:45 +1100379 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100381 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 offset = 0;
383 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Dave Chinner0e6e8472011-03-26 09:16:45 +1100386out_free_pages:
387 for (i = 0; i < bp->b_page_count; i++)
388 __free_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return error;
390}
391
392/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300393 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 */
395STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100396_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 xfs_buf_t *bp,
398 uint flags)
399{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100400 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100401 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100402 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100403 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
404 bp->b_flags |= XBF_MAPPED;
405 } else if (flags & XBF_MAPPED) {
Dave Chinnera19fb382011-03-26 09:13:42 +1100406 int retried = 0;
407
408 do {
409 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
410 -1, PAGE_KERNEL);
411 if (bp->b_addr)
412 break;
413 vm_unmap_aliases();
414 } while (retried++ <= 1);
415
416 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100418 bp->b_addr += bp->b_offset;
419 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 return 0;
423}
424
425/*
426 * Finding and Reading Buffers
427 */
428
429/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100430 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * a given range of an inode. The buffer is returned
Chandra Seetharamaneabbaf12011-09-08 20:18:50 +0000432 * locked. No I/O is implied by this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
434xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100435_xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000436 struct xfs_buftarg *btp,
437 xfs_daddr_t blkno,
438 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100439 xfs_buf_flags_t flags,
440 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000442 xfs_off_t offset;
443 size_t numbytes;
Dave Chinner74f75a02010-09-24 19:59:04 +1000444 struct xfs_perag *pag;
445 struct rb_node **rbp;
446 struct rb_node *parent;
447 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Dave Chinnere70b73f2012-04-23 15:58:49 +1000449 offset = BBTOB(blkno);
450 numbytes = BBTOB(numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /* Check for IOs smaller than the sector size / not sector aligned */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000453 ASSERT(!(numbytes < (1 << btp->bt_sshift)));
454 ASSERT(!(offset & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dave Chinner74f75a02010-09-24 19:59:04 +1000456 /* get tree root */
457 pag = xfs_perag_get(btp->bt_mount,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000458 xfs_daddr_to_agno(btp->bt_mount, blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Dave Chinner74f75a02010-09-24 19:59:04 +1000460 /* walk tree */
461 spin_lock(&pag->pag_buf_lock);
462 rbp = &pag->pag_buf_tree.rb_node;
463 parent = NULL;
464 bp = NULL;
465 while (*rbp) {
466 parent = *rbp;
467 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Dave Chinnere70b73f2012-04-23 15:58:49 +1000469 if (offset < bp->b_file_offset)
Dave Chinner74f75a02010-09-24 19:59:04 +1000470 rbp = &(*rbp)->rb_left;
Dave Chinnere70b73f2012-04-23 15:58:49 +1000471 else if (offset > bp->b_file_offset)
Dave Chinner74f75a02010-09-24 19:59:04 +1000472 rbp = &(*rbp)->rb_right;
473 else {
474 /*
475 * found a block offset match. If the range doesn't
476 * match, the only way this is allowed is if the buffer
477 * in the cache is stale and the transaction that made
478 * it stale has not yet committed. i.e. we are
479 * reallocating a busy extent. Skip this buffer and
480 * continue searching to the right for an exact match.
481 */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000482 if (bp->b_buffer_length != numbytes) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000483 ASSERT(bp->b_flags & XBF_STALE);
484 rbp = &(*rbp)->rb_right;
485 continue;
486 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100487 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 goto found;
489 }
490 }
491
492 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100493 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000494 rb_link_node(&new_bp->b_rbnode, parent, rbp);
495 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
496 /* the buffer keeps the perag reference until it is freed */
497 new_bp->b_pag = pag;
498 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100500 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000501 spin_unlock(&pag->pag_buf_lock);
502 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100504 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000507 spin_unlock(&pag->pag_buf_lock);
508 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200510 if (!xfs_buf_trylock(bp)) {
511 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100512 xfs_buf_rele(bp);
513 XFS_STATS_INC(xb_busy_locked);
514 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200516 xfs_buf_lock(bp);
517 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
Dave Chinner0e6e8472011-03-26 09:16:45 +1100520 /*
521 * if the buffer is stale, clear all the external state associated with
522 * it. We need to keep flags such as how we allocated the buffer memory
523 * intact here.
524 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100525 if (bp->b_flags & XBF_STALE) {
526 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100527 bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
David Chinner2f926582005-09-05 08:33:35 +1000528 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000529
530 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100531 XFS_STATS_INC(xb_get_locked);
532 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535/*
Dave Chinner38158322011-09-30 04:45:02 +0000536 * Assembles a buffer covering the specified range. The code is optimised for
537 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
538 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
Dave Chinner38158322011-09-30 04:45:02 +0000540struct xfs_buf *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000541xfs_buf_get(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000542 xfs_buftarg_t *target,
543 xfs_daddr_t blkno,
544 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100545 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Dave Chinner38158322011-09-30 04:45:02 +0000547 struct xfs_buf *bp;
548 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100549 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Dave Chinnere70b73f2012-04-23 15:58:49 +1000551 bp = _xfs_buf_find(target, blkno, numblks, flags, NULL);
Dave Chinner38158322011-09-30 04:45:02 +0000552 if (likely(bp))
553 goto found;
554
Dave Chinnere70b73f2012-04-23 15:58:49 +1000555 new_bp = xfs_buf_alloc(target, blkno, numblks, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100556 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return NULL;
558
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000559 error = xfs_buf_allocate_memory(new_bp, flags);
560 if (error) {
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000561 kmem_zone_free(xfs_buf_zone, new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000562 return NULL;
563 }
564
Dave Chinnere70b73f2012-04-23 15:58:49 +1000565 bp = _xfs_buf_find(target, blkno, numblks, flags, new_bp);
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000566 if (!bp) {
567 xfs_buf_free(new_bp);
568 return NULL;
569 }
570
571 if (bp != new_bp)
572 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Dave Chinner38158322011-09-30 04:45:02 +0000574 /*
575 * Now we have a workable buffer, fill in the block number so
576 * that we can do IO on it.
577 */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000578 bp->b_bn = blkno;
Dave Chinner38158322011-09-30 04:45:02 +0000579 bp->b_count_desired = bp->b_buffer_length;
580
581found:
Nathan Scottce8e9222006-01-11 15:39:08 +1100582 if (!(bp->b_flags & XBF_MAPPED)) {
583 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100585 xfs_warn(target->bt_mount,
586 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 goto no_buffer;
588 }
589 }
590
Nathan Scottce8e9222006-01-11 15:39:08 +1100591 XFS_STATS_INC(xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000592 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100593 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Dave Chinner38158322011-09-30 04:45:02 +0000595no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100596 if (flags & (XBF_LOCK | XBF_TRYLOCK))
597 xfs_buf_unlock(bp);
598 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return NULL;
600}
601
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100602STATIC int
603_xfs_buf_read(
604 xfs_buf_t *bp,
605 xfs_buf_flags_t flags)
606{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000607 ASSERT(!(flags & XBF_WRITE));
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100608 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
609
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000610 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200611 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100612
Dave Chinner0e95f192012-04-23 15:58:46 +1000613 xfs_buf_iorequest(bp);
614 if (flags & XBF_ASYNC)
615 return 0;
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000616 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000620xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 xfs_buftarg_t *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000622 xfs_daddr_t blkno,
623 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100624 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Nathan Scottce8e9222006-01-11 15:39:08 +1100626 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Nathan Scottce8e9222006-01-11 15:39:08 +1100628 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Dave Chinnere70b73f2012-04-23 15:58:49 +1000630 bp = xfs_buf_get(target, blkno, numblks, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100631 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000632 trace_xfs_buf_read(bp, flags, _RET_IP_);
633
Nathan Scottce8e9222006-01-11 15:39:08 +1100634 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100635 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100636 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100637 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /*
639 * Read ahead call which is already satisfied,
640 * drop the buffer
641 */
642 goto no_buffer;
643 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100645 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647 }
648
Nathan Scottce8e9222006-01-11 15:39:08 +1100649 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100652 if (flags & (XBF_LOCK | XBF_TRYLOCK))
653 xfs_buf_unlock(bp);
654 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return NULL;
656}
657
658/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100659 * If we are not low on memory then do the readahead in a deadlock
660 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 */
662void
Nathan Scottce8e9222006-01-11 15:39:08 +1100663xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 xfs_buftarg_t *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000665 xfs_daddr_t blkno,
666 size_t numblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100668 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return;
670
Dave Chinnere70b73f2012-04-23 15:58:49 +1000671 xfs_buf_read(target, blkno, numblks,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000672 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Dave Chinner5adc94c2010-09-24 21:58:31 +1000675/*
676 * Read an uncached buffer from disk. Allocates and returns a locked
677 * buffer containing the disk contents or nothing.
678 */
679struct xfs_buf *
680xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000681 struct xfs_buftarg *target,
682 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000683 size_t numblks,
Dave Chinner5adc94c2010-09-24 21:58:31 +1000684 int flags)
685{
686 xfs_buf_t *bp;
687 int error;
688
Dave Chinnere70b73f2012-04-23 15:58:49 +1000689 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000690 if (!bp)
691 return NULL;
692
693 /* set up the buffer for a read IO */
Dave Chinner5adc94c2010-09-24 21:58:31 +1000694 XFS_BUF_SET_ADDR(bp, daddr);
695 XFS_BUF_READ(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000696
Dave Chinnere70b73f2012-04-23 15:58:49 +1000697 xfsbdstrat(target->bt_mount, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000698 error = xfs_buf_iowait(bp);
Dave Chinner0e95f192012-04-23 15:58:46 +1000699 if (error) {
Dave Chinner5adc94c2010-09-24 21:58:31 +1000700 xfs_buf_relse(bp);
701 return NULL;
702 }
703 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Dave Chinner44396472011-04-21 09:34:27 +0000706/*
707 * Return a buffer allocated as an empty buffer and associated to external
708 * memory via xfs_buf_associate_memory() back to it's empty state.
709 */
710void
711xfs_buf_set_empty(
712 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000713 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000714{
715 if (bp->b_pages)
716 _xfs_buf_free_pages(bp);
717
718 bp->b_pages = NULL;
719 bp->b_page_count = 0;
720 bp->b_addr = NULL;
721 bp->b_file_offset = 0;
Dave Chinnere70b73f2012-04-23 15:58:49 +1000722 bp->b_buffer_length = bp->b_count_desired = numblks << BBSHIFT;
Dave Chinner44396472011-04-21 09:34:27 +0000723 bp->b_bn = XFS_BUF_DADDR_NULL;
724 bp->b_flags &= ~XBF_MAPPED;
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727static inline struct page *
728mem_to_page(
729 void *addr)
730{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800731 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return virt_to_page(addr);
733 } else {
734 return vmalloc_to_page(addr);
735 }
736}
737
738int
Nathan Scottce8e9222006-01-11 15:39:08 +1100739xfs_buf_associate_memory(
740 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 void *mem,
742 size_t len)
743{
744 int rval;
745 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100746 unsigned long pageaddr;
747 unsigned long offset;
748 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 int page_count;
750
Dave Chinner0e6e8472011-03-26 09:16:45 +1100751 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100752 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100753 buflen = PAGE_ALIGN(len + offset);
754 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100757 if (bp->b_pages)
758 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Nathan Scottce8e9222006-01-11 15:39:08 +1100760 bp->b_pages = NULL;
761 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Christoph Hellwig36fae172009-07-18 18:14:58 -0400763 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (rval)
765 return rval;
766
Nathan Scottce8e9222006-01-11 15:39:08 +1100767 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100769 for (i = 0; i < bp->b_page_count; i++) {
770 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100771 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100774 bp->b_count_desired = len;
775 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100776 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 return 0;
779}
780
781xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000782xfs_buf_get_uncached(
783 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000784 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000785 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000787 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000788 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Dave Chinnere70b73f2012-04-23 15:58:49 +1000791 bp = xfs_buf_alloc(target, 0, numblks, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (unlikely(bp == NULL))
793 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Dave Chinnere70b73f2012-04-23 15:58:49 +1000795 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000796 error = _xfs_buf_get_pages(bp, page_count, 0);
797 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto fail_free_buf;
799
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000800 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000801 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000802 if (!bp->b_pages[i])
803 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000805 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000807 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
808 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100809 xfs_warn(target->bt_mount,
810 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Dave Chinner686865f2010-09-24 20:07:47 +1000814 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000818 while (--i >= 0)
819 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000820 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 fail_free_buf:
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000822 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 fail:
824 return NULL;
825}
826
827/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 * Increment reference count on buffer, to hold the buffer concurrently
829 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 * Must hold the buffer already to call this function.
831 */
832void
Nathan Scottce8e9222006-01-11 15:39:08 +1100833xfs_buf_hold(
834 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000836 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100837 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
840/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100841 * Releases a hold on the specified buffer. If the
842 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 */
844void
Nathan Scottce8e9222006-01-11 15:39:08 +1100845xfs_buf_rele(
846 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Dave Chinner74f75a02010-09-24 19:59:04 +1000848 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000850 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Dave Chinner74f75a02010-09-24 19:59:04 +1000852 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100853 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000854 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100855 if (atomic_dec_and_test(&bp->b_hold))
856 xfs_buf_free(bp);
857 return;
858 }
859
Dave Chinner74f75a02010-09-24 19:59:04 +1000860 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100861
Lachlan McIlroy37906892008-08-13 15:42:10 +1000862 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000863 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000864 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100865 atomic_read(&bp->b_lru_ref)) {
866 xfs_buf_lru_add(bp);
867 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100868 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100869 xfs_buf_lru_del(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000870 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner74f75a02010-09-24 19:59:04 +1000871 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
872 spin_unlock(&pag->pag_buf_lock);
873 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100874 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 }
877}
878
879
880/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100881 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100882 *
883 * If we come across a stale, pinned, locked buffer, we know that we are
884 * being asked to lock a buffer that has been reallocated. Because it is
885 * pinned, we know that the log has not been pushed to disk and hence it
886 * will still be locked. Rather than continuing to have trylock attempts
887 * fail until someone else pushes the log, push it ourselves before
888 * returning. This means that the xfsaild will not get stuck trying
889 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
891int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200892xfs_buf_trylock(
893 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 int locked;
896
Nathan Scottce8e9222006-01-11 15:39:08 +1100897 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000898 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100899 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100900 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
901 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000902
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200903 trace_xfs_buf_trylock(bp, _RET_IP_);
904 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100908 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000909 *
910 * If we come across a stale, pinned, locked buffer, we know that we
911 * are being asked to lock a buffer that has been reallocated. Because
912 * it is pinned, we know that the log has not been pushed to disk and
913 * hence it will still be locked. Rather than sleeping until someone
914 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100916void
917xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200918 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000920 trace_xfs_buf_lock(bp, _RET_IP_);
921
Dave Chinnered3b4d62010-05-21 12:07:08 +1000922 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000923 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100924 down(&bp->b_sema);
925 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000926
927 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930void
Nathan Scottce8e9222006-01-11 15:39:08 +1100931xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200932 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Nathan Scottce8e9222006-01-11 15:39:08 +1100934 XB_CLEAR_OWNER(bp);
935 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000936
937 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Nathan Scottce8e9222006-01-11 15:39:08 +1100940STATIC void
941xfs_buf_wait_unpin(
942 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 DECLARE_WAITQUEUE (wait, current);
945
Nathan Scottce8e9222006-01-11 15:39:08 +1100946 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return;
948
Nathan Scottce8e9222006-01-11 15:39:08 +1100949 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 for (;;) {
951 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100952 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100954 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100956 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 set_current_state(TASK_RUNNING);
958}
959
960/*
961 * Buffer Utility Routines
962 */
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100965xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000966 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
David Howellsc4028952006-11-22 14:57:56 +0000968 xfs_buf_t *bp =
969 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400971 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100972 (*(bp->b_iodone))(bp);
973 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 xfs_buf_relse(bp);
975}
976
977void
Nathan Scottce8e9222006-01-11 15:39:08 +1100978xfs_buf_ioend(
979 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int schedule)
981{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000982 trace_xfs_buf_iodone(bp, _RET_IP_);
983
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100984 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100985 if (bp->b_error == 0)
986 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Nathan Scottce8e9222006-01-11 15:39:08 +1100988 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000990 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100991 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 } else {
David Howellsc4028952006-11-22 14:57:56 +0000993 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +1000996 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998}
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000void
Nathan Scottce8e9222006-01-11 15:39:08 +11001001xfs_buf_ioerror(
1002 xfs_buf_t *bp,
1003 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001006 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001007 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Christoph Hellwig901796a2011-10-10 16:52:49 +00001010void
1011xfs_buf_ioerror_alert(
1012 struct xfs_buf *bp,
1013 const char *func)
1014{
1015 xfs_alert(bp->b_target->bt_mount,
1016"metadata I/O error: block 0x%llx (\"%s\") error %d buf count %zd",
1017 (__uint64_t)XFS_BUF_ADDR(bp), func,
1018 bp->b_error, XFS_BUF_COUNT(bp));
1019}
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001022xfs_bwrite(
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001023 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001025 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001027 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001029 bp->b_flags |= XBF_WRITE;
1030 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
1031
Christoph Hellwig939d7232010-07-20 17:51:16 +10001032 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Christoph Hellwig8c383662010-03-12 10:59:40 +00001034 error = xfs_buf_iowait(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001035 if (error) {
1036 xfs_force_shutdown(bp->b_target->bt_mount,
1037 SHUTDOWN_META_IO_ERROR);
1038 }
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001039 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Christoph Hellwig4e234712010-01-13 22:17:56 +00001042/*
1043 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001044 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001045 * so that the proper iodone callbacks get called.
1046 */
1047STATIC int
1048xfs_bioerror(
1049 xfs_buf_t *bp)
1050{
1051#ifdef XFSERRORDEBUG
1052 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1053#endif
1054
1055 /*
1056 * No need to wait until the buffer is unpinned, we aren't flushing it.
1057 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001058 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001059
1060 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001061 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001062 */
1063 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001064 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001065 xfs_buf_stale(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001066
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001067 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001068
1069 return EIO;
1070}
1071
1072/*
1073 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001074 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001075 * This is meant for userdata errors; metadata bufs come with
1076 * iodone functions attached, so that we can track down errors.
1077 */
1078STATIC int
1079xfs_bioerror_relse(
1080 struct xfs_buf *bp)
1081{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001082 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001083 /*
1084 * No need to wait until the buffer is unpinned.
1085 * We aren't flushing it.
1086 *
1087 * chunkhold expects B_DONE to be set, whether
1088 * we actually finish the I/O or not. We don't want to
1089 * change that interface.
1090 */
1091 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001092 XFS_BUF_DONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001093 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 Hellwig5fde0322011-10-10 16:52:44 +00001103 complete(&bp->b_iowait);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001104 } 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
Dave Chinner0e95f192012-04-23 15:58:46 +10001257void
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 Hellwig43ff2122012-04-23 15:58:39 +10001263 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
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}
1279
1280/*
Dave Chinner0e95f192012-04-23 15:58:46 +10001281 * Waits for I/O to complete on the buffer supplied. It returns immediately if
1282 * no I/O is pending or there is already a pending error on the buffer. It
1283 * returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 */
1285int
Nathan Scottce8e9222006-01-11 15:39:08 +11001286xfs_buf_iowait(
1287 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001289 trace_xfs_buf_iowait(bp, _RET_IP_);
1290
Dave Chinner0e95f192012-04-23 15:58:46 +10001291 if (!bp->b_error)
1292 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 /*
Paul Bolle90802ed2011-12-05 13:00:34 +01001377 * clear the LRU reference count so the buffer doesn't get
Dave Chinner430cbeb2010-12-02 16:30:55 +11001378 * 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
Christoph Hellwigb7963132009-03-03 14:48:37 -05001444 if (mp->m_flags & XFS_MOUNT_BARRIER)
1445 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001446
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001447 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450STATIC int
1451xfs_setsize_buftarg_flags(
1452 xfs_buftarg_t *btp,
1453 unsigned int blocksize,
1454 unsigned int sectorsize,
1455 int verbose)
1456{
Nathan Scottce8e9222006-01-11 15:39:08 +11001457 btp->bt_bsize = blocksize;
1458 btp->bt_sshift = ffs(sectorsize) - 1;
1459 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Nathan Scottce8e9222006-01-11 15:39:08 +11001461 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001462 char name[BDEVNAME_SIZE];
1463
1464 bdevname(btp->bt_bdev, name);
1465
Dave Chinner4f107002011-03-07 10:00:35 +11001466 xfs_warn(btp->bt_mount,
1467 "Cannot set_blocksize to %u on device %s\n",
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001468 sectorsize, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return EINVAL;
1470 }
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return 0;
1473}
1474
1475/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001476 * When allocating the initial buffer target we have not yet
1477 * read in the superblock, so don't know what sized sectors
1478 * are being used is at this early stage. Play safe.
1479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480STATIC int
1481xfs_setsize_buftarg_early(
1482 xfs_buftarg_t *btp,
1483 struct block_device *bdev)
1484{
1485 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001486 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
1489int
1490xfs_setsize_buftarg(
1491 xfs_buftarg_t *btp,
1492 unsigned int blocksize,
1493 unsigned int sectorsize)
1494{
1495 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1496}
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498xfs_buftarg_t *
1499xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001500 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001502 int external,
1503 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 xfs_buftarg_t *btp;
1506
1507 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1508
Dave Chinnerebad8612010-09-22 10:47:20 +10001509 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001510 btp->bt_dev = bdev->bd_dev;
1511 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001512 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1513 if (!btp->bt_bdi)
1514 goto error;
1515
Dave Chinner430cbeb2010-12-02 16:30:55 +11001516 INIT_LIST_HEAD(&btp->bt_lru);
1517 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (xfs_setsize_buftarg_early(btp, bdev))
1519 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001520 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1521 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1522 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return btp;
1524
1525error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001526 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return NULL;
1528}
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001531 * Add a buffer to the delayed write list.
1532 *
1533 * This queues a buffer for writeout if it hasn't already been. Note that
1534 * neither this routine nor the buffer list submission functions perform
1535 * any internal synchronization. It is expected that the lists are thread-local
1536 * to the callers.
1537 *
1538 * Returns true if we queued up the buffer, or false if it already had
1539 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001541bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001542xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001543 struct xfs_buf *bp,
1544 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001546 ASSERT(xfs_buf_islocked(bp));
1547 ASSERT(!(bp->b_flags & XBF_READ));
1548
1549 /*
1550 * If the buffer is already marked delwri it already is queued up
1551 * by someone else for imediate writeout. Just ignore it in that
1552 * case.
1553 */
1554 if (bp->b_flags & _XBF_DELWRI_Q) {
1555 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1556 return false;
1557 }
David Chinnera6867a62006-01-11 15:37:58 +11001558
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001559 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1560
Dave Chinnerd808f612010-02-02 10:13:42 +11001561 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001562 * If a buffer gets written out synchronously or marked stale while it
1563 * is on a delwri list we lazily remove it. To do this, the other party
1564 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1565 * It remains referenced and on the list. In a rare corner case it
1566 * might get readded to a delwri list after the synchronous writeout, in
1567 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001568 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001569 bp->b_flags |= _XBF_DELWRI_Q;
1570 if (list_empty(&bp->b_list)) {
1571 atomic_inc(&bp->b_hold);
1572 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001573 }
David Chinner585e6d82007-02-10 18:32:29 +11001574
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001575 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001576}
1577
Dave Chinner089716a2010-01-26 15:13:25 +11001578/*
1579 * Compare function is more complex than it needs to be because
1580 * the return value is only 32 bits and we are doing comparisons
1581 * on 64 bit values
1582 */
1583static int
1584xfs_buf_cmp(
1585 void *priv,
1586 struct list_head *a,
1587 struct list_head *b)
1588{
1589 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1590 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1591 xfs_daddr_t diff;
1592
1593 diff = ap->b_bn - bp->b_bn;
1594 if (diff < 0)
1595 return -1;
1596 if (diff > 0)
1597 return 1;
1598 return 0;
1599}
1600
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001601static int
1602__xfs_buf_delwri_submit(
1603 struct list_head *buffer_list,
1604 struct list_head *io_list,
1605 bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001607 struct blk_plug plug;
1608 struct xfs_buf *bp, *n;
1609 int pinned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001611 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
1612 if (!wait) {
1613 if (xfs_buf_ispinned(bp)) {
1614 pinned++;
1615 continue;
1616 }
1617 if (!xfs_buf_trylock(bp))
1618 continue;
1619 } else {
1620 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001623 /*
1624 * Someone else might have written the buffer synchronously or
1625 * marked it stale in the meantime. In that case only the
1626 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1627 * reference and remove it from the list here.
1628 */
1629 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1630 list_del_init(&bp->b_list);
1631 xfs_buf_relse(bp);
1632 continue;
1633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001635 list_move_tail(&bp->b_list, io_list);
1636 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001639 list_sort(NULL, io_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001640
1641 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001642 list_for_each_entry_safe(bp, n, io_list, b_list) {
1643 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
1644 bp->b_flags |= XBF_WRITE;
1645
1646 if (!wait) {
1647 bp->b_flags |= XBF_ASYNC;
1648 list_del_init(&bp->b_list);
Dave Chinner089716a2010-01-26 15:13:25 +11001649 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001650 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001652 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001654 return pinned;
1655}
Nathan Scottf07c2252006-09-28 10:52:15 +10001656
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001657/*
1658 * Write out a buffer list asynchronously.
1659 *
1660 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1661 * out and not wait for I/O completion on any of the buffers. This interface
1662 * is only safely useable for callers that can track I/O completion by higher
1663 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1664 * function.
1665 */
1666int
1667xfs_buf_delwri_submit_nowait(
1668 struct list_head *buffer_list)
1669{
1670 LIST_HEAD (io_list);
1671 return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
1672}
1673
1674/*
1675 * Write out a buffer list synchronously.
1676 *
1677 * This will take the @buffer_list, write all buffers out and wait for I/O
1678 * completion on all of the buffers. @buffer_list is consumed by the function,
1679 * so callers must have some other way of tracking buffers if they require such
1680 * functionality.
1681 */
1682int
1683xfs_buf_delwri_submit(
1684 struct list_head *buffer_list)
1685{
1686 LIST_HEAD (io_list);
1687 int error = 0, error2;
1688 struct xfs_buf *bp;
1689
1690 __xfs_buf_delwri_submit(buffer_list, &io_list, true);
1691
1692 /* Wait for IO to complete. */
1693 while (!list_empty(&io_list)) {
1694 bp = list_first_entry(&io_list, struct xfs_buf, b_list);
1695
1696 list_del_init(&bp->b_list);
1697 error2 = xfs_buf_iowait(bp);
1698 xfs_buf_relse(bp);
1699 if (!error)
1700 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
1702
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001703 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001706int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001707xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Nathan Scott87582802006-03-14 13:18:19 +11001709 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1710 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001711 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001712 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001713
Dave Chinner51749e42010-09-08 09:00:22 +00001714 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001715 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001716 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001717 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001719 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001721 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001722 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001723 out:
Nathan Scott87582802006-03-14 13:18:19 +11001724 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727void
Nathan Scottce8e9222006-01-11 15:39:08 +11001728xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001730 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001731 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}