blob: 854b27a8e7765de6ccaf83510b7f818c06d22e3b [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 Chinnerde1cbee2012-04-23 15:58:50 +1000199
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:
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000340 end = BBTOB(bp->b_bn) + bp->b_buffer_length;
341 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(BBTOB(bp->b_bn));
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 size_t numbytes;
Dave Chinner74f75a02010-09-24 19:59:04 +1000443 struct xfs_perag *pag;
444 struct rb_node **rbp;
445 struct rb_node *parent;
446 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Dave Chinnere70b73f2012-04-23 15:58:49 +1000448 numbytes = BBTOB(numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 /* Check for IOs smaller than the sector size / not sector aligned */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000451 ASSERT(!(numbytes < (1 << btp->bt_sshift)));
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000452 ASSERT(!(BBTOB(blkno) & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Dave Chinner74f75a02010-09-24 19:59:04 +1000454 /* get tree root */
455 pag = xfs_perag_get(btp->bt_mount,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000456 xfs_daddr_to_agno(btp->bt_mount, blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Dave Chinner74f75a02010-09-24 19:59:04 +1000458 /* walk tree */
459 spin_lock(&pag->pag_buf_lock);
460 rbp = &pag->pag_buf_tree.rb_node;
461 parent = NULL;
462 bp = NULL;
463 while (*rbp) {
464 parent = *rbp;
465 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000467 if (blkno < bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000468 rbp = &(*rbp)->rb_left;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000469 else if (blkno > bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000470 rbp = &(*rbp)->rb_right;
471 else {
472 /*
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000473 * found a block number match. If the range doesn't
Dave Chinner74f75a02010-09-24 19:59:04 +1000474 * match, the only way this is allowed is if the buffer
475 * in the cache is stale and the transaction that made
476 * it stale has not yet committed. i.e. we are
477 * reallocating a busy extent. Skip this buffer and
478 * continue searching to the right for an exact match.
479 */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000480 if (bp->b_buffer_length != numbytes) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000481 ASSERT(bp->b_flags & XBF_STALE);
482 rbp = &(*rbp)->rb_right;
483 continue;
484 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100485 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 goto found;
487 }
488 }
489
490 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100491 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000492 rb_link_node(&new_bp->b_rbnode, parent, rbp);
493 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
494 /* the buffer keeps the perag reference until it is freed */
495 new_bp->b_pag = pag;
496 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100498 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000499 spin_unlock(&pag->pag_buf_lock);
500 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100502 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000505 spin_unlock(&pag->pag_buf_lock);
506 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200508 if (!xfs_buf_trylock(bp)) {
509 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100510 xfs_buf_rele(bp);
511 XFS_STATS_INC(xb_busy_locked);
512 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200514 xfs_buf_lock(bp);
515 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
Dave Chinner0e6e8472011-03-26 09:16:45 +1100518 /*
519 * if the buffer is stale, clear all the external state associated with
520 * it. We need to keep flags such as how we allocated the buffer memory
521 * intact here.
522 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100523 if (bp->b_flags & XBF_STALE) {
524 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100525 bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
David Chinner2f926582005-09-05 08:33:35 +1000526 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000527
528 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100529 XFS_STATS_INC(xb_get_locked);
530 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533/*
Dave Chinner38158322011-09-30 04:45:02 +0000534 * Assembles a buffer covering the specified range. The code is optimised for
535 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
536 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
Dave Chinner38158322011-09-30 04:45:02 +0000538struct xfs_buf *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000539xfs_buf_get(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000540 xfs_buftarg_t *target,
541 xfs_daddr_t blkno,
542 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100543 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Dave Chinner38158322011-09-30 04:45:02 +0000545 struct xfs_buf *bp;
546 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100547 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Dave Chinnere70b73f2012-04-23 15:58:49 +1000549 bp = _xfs_buf_find(target, blkno, numblks, flags, NULL);
Dave Chinner38158322011-09-30 04:45:02 +0000550 if (likely(bp))
551 goto found;
552
Dave Chinnere70b73f2012-04-23 15:58:49 +1000553 new_bp = xfs_buf_alloc(target, blkno, numblks, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100554 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return NULL;
556
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000557 error = xfs_buf_allocate_memory(new_bp, flags);
558 if (error) {
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000559 kmem_zone_free(xfs_buf_zone, new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000560 return NULL;
561 }
562
Dave Chinnere70b73f2012-04-23 15:58:49 +1000563 bp = _xfs_buf_find(target, blkno, numblks, flags, new_bp);
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000564 if (!bp) {
565 xfs_buf_free(new_bp);
566 return NULL;
567 }
568
569 if (bp != new_bp)
570 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Dave Chinner38158322011-09-30 04:45:02 +0000572 /*
573 * Now we have a workable buffer, fill in the block number so
574 * that we can do IO on it.
575 */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000576 bp->b_bn = blkno;
Dave Chinner38158322011-09-30 04:45:02 +0000577 bp->b_count_desired = bp->b_buffer_length;
578
579found:
Nathan Scottce8e9222006-01-11 15:39:08 +1100580 if (!(bp->b_flags & XBF_MAPPED)) {
581 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100583 xfs_warn(target->bt_mount,
584 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 goto no_buffer;
586 }
587 }
588
Nathan Scottce8e9222006-01-11 15:39:08 +1100589 XFS_STATS_INC(xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000590 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100591 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Dave Chinner38158322011-09-30 04:45:02 +0000593no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100594 if (flags & (XBF_LOCK | XBF_TRYLOCK))
595 xfs_buf_unlock(bp);
596 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return NULL;
598}
599
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100600STATIC int
601_xfs_buf_read(
602 xfs_buf_t *bp,
603 xfs_buf_flags_t flags)
604{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000605 ASSERT(!(flags & XBF_WRITE));
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100606 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
607
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000608 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200609 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100610
Dave Chinner0e95f192012-04-23 15:58:46 +1000611 xfs_buf_iorequest(bp);
612 if (flags & XBF_ASYNC)
613 return 0;
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000614 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000618xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 xfs_buftarg_t *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000620 xfs_daddr_t blkno,
621 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100622 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Nathan Scottce8e9222006-01-11 15:39:08 +1100624 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Nathan Scottce8e9222006-01-11 15:39:08 +1100626 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Dave Chinnere70b73f2012-04-23 15:58:49 +1000628 bp = xfs_buf_get(target, blkno, numblks, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100629 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000630 trace_xfs_buf_read(bp, flags, _RET_IP_);
631
Nathan Scottce8e9222006-01-11 15:39:08 +1100632 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100633 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100634 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100635 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /*
637 * Read ahead call which is already satisfied,
638 * drop the buffer
639 */
640 goto no_buffer;
641 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100643 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645 }
646
Nathan Scottce8e9222006-01-11 15:39:08 +1100647 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100650 if (flags & (XBF_LOCK | XBF_TRYLOCK))
651 xfs_buf_unlock(bp);
652 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return NULL;
654}
655
656/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100657 * If we are not low on memory then do the readahead in a deadlock
658 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 */
660void
Nathan Scottce8e9222006-01-11 15:39:08 +1100661xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 xfs_buftarg_t *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000663 xfs_daddr_t blkno,
664 size_t numblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100666 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return;
668
Dave Chinnere70b73f2012-04-23 15:58:49 +1000669 xfs_buf_read(target, blkno, numblks,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000670 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Dave Chinner5adc94c2010-09-24 21:58:31 +1000673/*
674 * Read an uncached buffer from disk. Allocates and returns a locked
675 * buffer containing the disk contents or nothing.
676 */
677struct xfs_buf *
678xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000679 struct xfs_buftarg *target,
680 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000681 size_t numblks,
Dave Chinner5adc94c2010-09-24 21:58:31 +1000682 int flags)
683{
684 xfs_buf_t *bp;
685 int error;
686
Dave Chinnere70b73f2012-04-23 15:58:49 +1000687 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000688 if (!bp)
689 return NULL;
690
691 /* set up the buffer for a read IO */
Dave Chinner5adc94c2010-09-24 21:58:31 +1000692 XFS_BUF_SET_ADDR(bp, daddr);
693 XFS_BUF_READ(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000694
Dave Chinnere70b73f2012-04-23 15:58:49 +1000695 xfsbdstrat(target->bt_mount, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000696 error = xfs_buf_iowait(bp);
Dave Chinner0e95f192012-04-23 15:58:46 +1000697 if (error) {
Dave Chinner5adc94c2010-09-24 21:58:31 +1000698 xfs_buf_relse(bp);
699 return NULL;
700 }
701 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Dave Chinner44396472011-04-21 09:34:27 +0000704/*
705 * Return a buffer allocated as an empty buffer and associated to external
706 * memory via xfs_buf_associate_memory() back to it's empty state.
707 */
708void
709xfs_buf_set_empty(
710 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000711 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000712{
713 if (bp->b_pages)
714 _xfs_buf_free_pages(bp);
715
716 bp->b_pages = NULL;
717 bp->b_page_count = 0;
718 bp->b_addr = NULL;
Dave Chinnere70b73f2012-04-23 15:58:49 +1000719 bp->b_buffer_length = bp->b_count_desired = numblks << BBSHIFT;
Dave Chinner44396472011-04-21 09:34:27 +0000720 bp->b_bn = XFS_BUF_DADDR_NULL;
721 bp->b_flags &= ~XBF_MAPPED;
722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724static inline struct page *
725mem_to_page(
726 void *addr)
727{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800728 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return virt_to_page(addr);
730 } else {
731 return vmalloc_to_page(addr);
732 }
733}
734
735int
Nathan Scottce8e9222006-01-11 15:39:08 +1100736xfs_buf_associate_memory(
737 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 void *mem,
739 size_t len)
740{
741 int rval;
742 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100743 unsigned long pageaddr;
744 unsigned long offset;
745 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int page_count;
747
Dave Chinner0e6e8472011-03-26 09:16:45 +1100748 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100749 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100750 buflen = PAGE_ALIGN(len + offset);
751 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100754 if (bp->b_pages)
755 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Nathan Scottce8e9222006-01-11 15:39:08 +1100757 bp->b_pages = NULL;
758 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Christoph Hellwig36fae172009-07-18 18:14:58 -0400760 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (rval)
762 return rval;
763
Nathan Scottce8e9222006-01-11 15:39:08 +1100764 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100766 for (i = 0; i < bp->b_page_count; i++) {
767 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100768 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100771 bp->b_count_desired = len;
772 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100773 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 return 0;
776}
777
778xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000779xfs_buf_get_uncached(
780 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000781 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000782 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000784 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000785 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Dave Chinnere70b73f2012-04-23 15:58:49 +1000788 bp = xfs_buf_alloc(target, 0, numblks, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (unlikely(bp == NULL))
790 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Dave Chinnere70b73f2012-04-23 15:58:49 +1000792 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000793 error = _xfs_buf_get_pages(bp, page_count, 0);
794 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 goto fail_free_buf;
796
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000797 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000798 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000799 if (!bp->b_pages[i])
800 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000802 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000804 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
805 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100806 xfs_warn(target->bt_mount,
807 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Dave Chinner686865f2010-09-24 20:07:47 +1000811 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000815 while (--i >= 0)
816 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000817 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 fail_free_buf:
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000819 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 fail:
821 return NULL;
822}
823
824/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 * Increment reference count on buffer, to hold the buffer concurrently
826 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 * Must hold the buffer already to call this function.
828 */
829void
Nathan Scottce8e9222006-01-11 15:39:08 +1100830xfs_buf_hold(
831 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000833 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100834 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
837/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100838 * Releases a hold on the specified buffer. If the
839 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 */
841void
Nathan Scottce8e9222006-01-11 15:39:08 +1100842xfs_buf_rele(
843 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Dave Chinner74f75a02010-09-24 19:59:04 +1000845 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000847 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Dave Chinner74f75a02010-09-24 19:59:04 +1000849 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100850 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000851 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100852 if (atomic_dec_and_test(&bp->b_hold))
853 xfs_buf_free(bp);
854 return;
855 }
856
Dave Chinner74f75a02010-09-24 19:59:04 +1000857 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100858
Lachlan McIlroy37906892008-08-13 15:42:10 +1000859 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000860 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000861 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100862 atomic_read(&bp->b_lru_ref)) {
863 xfs_buf_lru_add(bp);
864 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100865 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100866 xfs_buf_lru_del(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000867 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner74f75a02010-09-24 19:59:04 +1000868 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
869 spin_unlock(&pag->pag_buf_lock);
870 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100871 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873 }
874}
875
876
877/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100878 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100879 *
880 * If we come across a stale, pinned, locked buffer, we know that we are
881 * being asked to lock a buffer that has been reallocated. Because it is
882 * pinned, we know that the log has not been pushed to disk and hence it
883 * will still be locked. Rather than continuing to have trylock attempts
884 * fail until someone else pushes the log, push it ourselves before
885 * returning. This means that the xfsaild will not get stuck trying
886 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
888int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200889xfs_buf_trylock(
890 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 int locked;
893
Nathan Scottce8e9222006-01-11 15:39:08 +1100894 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000895 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100896 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100897 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
898 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000899
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200900 trace_xfs_buf_trylock(bp, _RET_IP_);
901 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100905 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000906 *
907 * If we come across a stale, pinned, locked buffer, we know that we
908 * are being asked to lock a buffer that has been reallocated. Because
909 * it is pinned, we know that the log has not been pushed to disk and
910 * hence it will still be locked. Rather than sleeping until someone
911 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100913void
914xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200915 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000917 trace_xfs_buf_lock(bp, _RET_IP_);
918
Dave Chinnered3b4d62010-05-21 12:07:08 +1000919 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000920 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100921 down(&bp->b_sema);
922 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000923
924 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927void
Nathan Scottce8e9222006-01-11 15:39:08 +1100928xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200929 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Nathan Scottce8e9222006-01-11 15:39:08 +1100931 XB_CLEAR_OWNER(bp);
932 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000933
934 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Nathan Scottce8e9222006-01-11 15:39:08 +1100937STATIC void
938xfs_buf_wait_unpin(
939 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 DECLARE_WAITQUEUE (wait, current);
942
Nathan Scottce8e9222006-01-11 15:39:08 +1100943 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return;
945
Nathan Scottce8e9222006-01-11 15:39:08 +1100946 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 for (;;) {
948 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100949 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100951 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100953 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 set_current_state(TASK_RUNNING);
955}
956
957/*
958 * Buffer Utility Routines
959 */
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100962xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000963 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
David Howellsc4028952006-11-22 14:57:56 +0000965 xfs_buf_t *bp =
966 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400968 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100969 (*(bp->b_iodone))(bp);
970 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 xfs_buf_relse(bp);
972}
973
974void
Nathan Scottce8e9222006-01-11 15:39:08 +1100975xfs_buf_ioend(
976 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 int schedule)
978{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000979 trace_xfs_buf_iodone(bp, _RET_IP_);
980
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100981 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100982 if (bp->b_error == 0)
983 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Nathan Scottce8e9222006-01-11 15:39:08 +1100985 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000987 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100988 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 } else {
David Howellsc4028952006-11-22 14:57:56 +0000990 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +1000993 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995}
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997void
Nathan Scottce8e9222006-01-11 15:39:08 +1100998xfs_buf_ioerror(
999 xfs_buf_t *bp,
1000 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001003 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001004 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
Christoph Hellwig901796a2011-10-10 16:52:49 +00001007void
1008xfs_buf_ioerror_alert(
1009 struct xfs_buf *bp,
1010 const char *func)
1011{
1012 xfs_alert(bp->b_target->bt_mount,
1013"metadata I/O error: block 0x%llx (\"%s\") error %d buf count %zd",
1014 (__uint64_t)XFS_BUF_ADDR(bp), func,
1015 bp->b_error, XFS_BUF_COUNT(bp));
1016}
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001019xfs_bwrite(
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001020 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001022 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001024 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001026 bp->b_flags |= XBF_WRITE;
1027 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
1028
Christoph Hellwig939d7232010-07-20 17:51:16 +10001029 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Christoph Hellwig8c383662010-03-12 10:59:40 +00001031 error = xfs_buf_iowait(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001032 if (error) {
1033 xfs_force_shutdown(bp->b_target->bt_mount,
1034 SHUTDOWN_META_IO_ERROR);
1035 }
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001036 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001037}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Christoph Hellwig4e234712010-01-13 22:17:56 +00001039/*
1040 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001041 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001042 * so that the proper iodone callbacks get called.
1043 */
1044STATIC int
1045xfs_bioerror(
1046 xfs_buf_t *bp)
1047{
1048#ifdef XFSERRORDEBUG
1049 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1050#endif
1051
1052 /*
1053 * No need to wait until the buffer is unpinned, we aren't flushing it.
1054 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001055 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001056
1057 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001058 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001059 */
1060 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001061 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001062 xfs_buf_stale(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001063
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001064 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001065
1066 return EIO;
1067}
1068
1069/*
1070 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001071 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001072 * This is meant for userdata errors; metadata bufs come with
1073 * iodone functions attached, so that we can track down errors.
1074 */
1075STATIC int
1076xfs_bioerror_relse(
1077 struct xfs_buf *bp)
1078{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001079 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001080 /*
1081 * No need to wait until the buffer is unpinned.
1082 * We aren't flushing it.
1083 *
1084 * chunkhold expects B_DONE to be set, whether
1085 * we actually finish the I/O or not. We don't want to
1086 * change that interface.
1087 */
1088 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001089 XFS_BUF_DONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001090 xfs_buf_stale(bp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001091 bp->b_iodone = NULL;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001092 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001093 /*
1094 * Mark b_error and B_ERROR _both_.
1095 * Lot's of chunkcache code assumes that.
1096 * There's no reason to mark error for
1097 * ASYNC buffers.
1098 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001099 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig5fde0322011-10-10 16:52:44 +00001100 complete(&bp->b_iowait);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001101 } else {
1102 xfs_buf_relse(bp);
1103 }
1104
1105 return EIO;
1106}
1107
1108
1109/*
1110 * All xfs metadata buffers except log state machine buffers
1111 * get this attached as their b_bdstrat callback function.
1112 * This is so that we can catch a buffer
1113 * after prematurely unpinning it to forcibly shutdown the filesystem.
1114 */
1115int
1116xfs_bdstrat_cb(
1117 struct xfs_buf *bp)
1118{
Dave Chinnerebad8612010-09-22 10:47:20 +10001119 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001120 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1121 /*
1122 * Metadata write that didn't get logged but
1123 * written delayed anyway. These aren't associated
1124 * with a transaction, and can be ignored.
1125 */
1126 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1127 return xfs_bioerror_relse(bp);
1128 else
1129 return xfs_bioerror(bp);
1130 }
1131
1132 xfs_buf_iorequest(bp);
1133 return 0;
1134}
1135
1136/*
1137 * Wrapper around bdstrat so that we can stop data from going to disk in case
1138 * we are shutting down the filesystem. Typically user data goes thru this
1139 * path; one of the exceptions is the superblock.
1140 */
1141void
1142xfsbdstrat(
1143 struct xfs_mount *mp,
1144 struct xfs_buf *bp)
1145{
1146 if (XFS_FORCED_SHUTDOWN(mp)) {
1147 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1148 xfs_bioerror_relse(bp);
1149 return;
1150 }
1151
1152 xfs_buf_iorequest(bp);
1153}
1154
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001155STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001156_xfs_buf_ioend(
1157 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 int schedule)
1159{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001160 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001161 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Al Viro782e3b32007-10-12 07:17:47 +01001164STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001165xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 int error)
1168{
Nathan Scottce8e9222006-01-11 15:39:08 +11001169 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001171 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
James Bottomley73c77e22010-01-25 11:42:24 -06001173 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1174 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1175
Nathan Scottce8e9222006-01-11 15:39:08 +11001176 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
1180STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001181_xfs_buf_ioapply(
1182 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001184 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001186 int offset = bp->b_offset;
1187 int size = bp->b_count_desired;
1188 sector_t sector = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Nathan Scottce8e9222006-01-11 15:39:08 +11001190 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 map_i = 0;
1192
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001193 if (bp->b_flags & XBF_WRITE) {
1194 if (bp->b_flags & XBF_SYNCIO)
1195 rw = WRITE_SYNC;
1196 else
1197 rw = WRITE;
1198 if (bp->b_flags & XBF_FUA)
1199 rw |= REQ_FUA;
1200 if (bp->b_flags & XBF_FLUSH)
1201 rw |= REQ_FLUSH;
1202 } else if (bp->b_flags & XBF_READ_AHEAD) {
1203 rw = READA;
Nathan Scott51bdd702006-09-28 11:01:57 +10001204 } else {
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001205 rw = READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001206 }
1207
Christoph Hellwig34951f52011-07-26 15:06:44 +00001208 /* we only use the buffer cache for meta-data */
1209 rw |= REQ_META;
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001212 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1214 if (nr_pages > total_nr_pages)
1215 nr_pages = total_nr_pages;
1216
1217 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001218 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001220 bio->bi_end_io = xfs_buf_bio_end_io;
1221 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Dave Chinner0e6e8472011-03-26 09:16:45 +11001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 for (; size && nr_pages; nr_pages--, map_i++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001225 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 if (nbytes > size)
1228 nbytes = size;
1229
Nathan Scottce8e9222006-01-11 15:39:08 +11001230 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1231 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 break;
1233
1234 offset = 0;
1235 sector += nbytes >> BBSHIFT;
1236 size -= nbytes;
1237 total_nr_pages--;
1238 }
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001241 if (xfs_buf_is_vmapped(bp)) {
1242 flush_kernel_vmap_range(bp->b_addr,
1243 xfs_buf_vmap_len(bp));
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 submit_bio(rw, bio);
1246 if (size)
1247 goto next_chunk;
1248 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001249 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001250 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252}
1253
Dave Chinner0e95f192012-04-23 15:58:46 +10001254void
Nathan Scottce8e9222006-01-11 15:39:08 +11001255xfs_buf_iorequest(
1256 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001258 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001260 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Christoph Hellwig375ec69d2011-08-23 08:28:03 +00001262 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001263 xfs_buf_wait_unpin(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001264 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 /* Set the count to 1 initially, this will stop an I/O
1267 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001268 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001270 atomic_set(&bp->b_io_remaining, 1);
1271 _xfs_buf_ioapply(bp);
1272 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Nathan Scottce8e9222006-01-11 15:39:08 +11001274 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
1277/*
Dave Chinner0e95f192012-04-23 15:58:46 +10001278 * Waits for I/O to complete on the buffer supplied. It returns immediately if
1279 * no I/O is pending or there is already a pending error on the buffer. It
1280 * returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 */
1282int
Nathan Scottce8e9222006-01-11 15:39:08 +11001283xfs_buf_iowait(
1284 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001286 trace_xfs_buf_iowait(bp, _RET_IP_);
1287
Dave Chinner0e95f192012-04-23 15:58:46 +10001288 if (!bp->b_error)
1289 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001290
1291 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001292 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Nathan Scottce8e9222006-01-11 15:39:08 +11001295xfs_caddr_t
1296xfs_buf_offset(
1297 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 size_t offset)
1299{
1300 struct page *page;
1301
Nathan Scottce8e9222006-01-11 15:39:08 +11001302 if (bp->b_flags & XBF_MAPPED)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001303 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Nathan Scottce8e9222006-01-11 15:39:08 +11001305 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001306 page = bp->b_pages[offset >> PAGE_SHIFT];
1307 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
1310/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 * Move data into or out of a buffer.
1312 */
1313void
Nathan Scottce8e9222006-01-11 15:39:08 +11001314xfs_buf_iomove(
1315 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 size_t boff, /* starting buffer offset */
1317 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001318 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001319 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 size_t bend, cpoff, csize;
1322 struct page *page;
1323
1324 bend = boff + bsize;
1325 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001326 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1327 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 csize = min_t(size_t,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001329 PAGE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Dave Chinner0e6e8472011-03-26 09:16:45 +11001331 ASSERT(((csize + cpoff) <= PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001334 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 memset(page_address(page) + cpoff, 0, csize);
1336 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001337 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 memcpy(data, page_address(page) + cpoff, csize);
1339 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001340 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 memcpy(page_address(page) + cpoff, data, csize);
1342 }
1343
1344 boff += csize;
1345 data += csize;
1346 }
1347}
1348
1349/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001350 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 */
1352
1353/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001354 * Wait for any bufs with callbacks that have been submitted but have not yet
1355 * returned. These buffers will have an elevated hold count, so wait on those
1356 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
1358void
1359xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001360 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Dave Chinner430cbeb2010-12-02 16:30:55 +11001362 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Dave Chinner430cbeb2010-12-02 16:30:55 +11001364restart:
1365 spin_lock(&btp->bt_lru_lock);
1366 while (!list_empty(&btp->bt_lru)) {
1367 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1368 if (atomic_read(&bp->b_hold) > 1) {
1369 spin_unlock(&btp->bt_lru_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001370 delay(100);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001371 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001373 /*
Paul Bolle90802ed2011-12-05 13:00:34 +01001374 * clear the LRU reference count so the buffer doesn't get
Dave Chinner430cbeb2010-12-02 16:30:55 +11001375 * ignored in xfs_buf_rele().
1376 */
1377 atomic_set(&bp->b_lru_ref, 0);
1378 spin_unlock(&btp->bt_lru_lock);
1379 xfs_buf_rele(bp);
1380 spin_lock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001382 spin_unlock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
Dave Chinnerff57ab22010-11-30 17:27:57 +11001385int
1386xfs_buftarg_shrink(
1387 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001388 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001389{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001390 struct xfs_buftarg *btp = container_of(shrink,
1391 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001392 struct xfs_buf *bp;
Ying Han1495f232011-05-24 17:12:27 -07001393 int nr_to_scan = sc->nr_to_scan;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001394 LIST_HEAD(dispose);
1395
1396 if (!nr_to_scan)
1397 return btp->bt_lru_nr;
1398
1399 spin_lock(&btp->bt_lru_lock);
1400 while (!list_empty(&btp->bt_lru)) {
1401 if (nr_to_scan-- <= 0)
1402 break;
1403
1404 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1405
1406 /*
1407 * Decrement the b_lru_ref count unless the value is already
1408 * zero. If the value is already zero, we need to reclaim the
1409 * buffer, otherwise it gets another trip through the LRU.
1410 */
1411 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1412 list_move_tail(&bp->b_lru, &btp->bt_lru);
1413 continue;
1414 }
1415
1416 /*
1417 * remove the buffer from the LRU now to avoid needing another
1418 * lock round trip inside xfs_buf_rele().
1419 */
1420 list_move(&bp->b_lru, &dispose);
1421 btp->bt_lru_nr--;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001422 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001423 spin_unlock(&btp->bt_lru_lock);
1424
1425 while (!list_empty(&dispose)) {
1426 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1427 list_del_init(&bp->b_lru);
1428 xfs_buf_rele(bp);
1429 }
1430
1431 return btp->bt_lru_nr;
David Chinnera6867a62006-01-11 15:37:58 +11001432}
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434void
1435xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001436 struct xfs_mount *mp,
1437 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001439 unregister_shrinker(&btp->bt_shrinker);
1440
Christoph Hellwigb7963132009-03-03 14:48:37 -05001441 if (mp->m_flags & XFS_MOUNT_BARRIER)
1442 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001443
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001444 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447STATIC int
1448xfs_setsize_buftarg_flags(
1449 xfs_buftarg_t *btp,
1450 unsigned int blocksize,
1451 unsigned int sectorsize,
1452 int verbose)
1453{
Nathan Scottce8e9222006-01-11 15:39:08 +11001454 btp->bt_bsize = blocksize;
1455 btp->bt_sshift = ffs(sectorsize) - 1;
1456 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Nathan Scottce8e9222006-01-11 15:39:08 +11001458 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001459 char name[BDEVNAME_SIZE];
1460
1461 bdevname(btp->bt_bdev, name);
1462
Dave Chinner4f107002011-03-07 10:00:35 +11001463 xfs_warn(btp->bt_mount,
1464 "Cannot set_blocksize to %u on device %s\n",
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001465 sectorsize, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return EINVAL;
1467 }
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return 0;
1470}
1471
1472/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001473 * When allocating the initial buffer target we have not yet
1474 * read in the superblock, so don't know what sized sectors
1475 * are being used is at this early stage. Play safe.
1476 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477STATIC int
1478xfs_setsize_buftarg_early(
1479 xfs_buftarg_t *btp,
1480 struct block_device *bdev)
1481{
1482 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001483 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
1486int
1487xfs_setsize_buftarg(
1488 xfs_buftarg_t *btp,
1489 unsigned int blocksize,
1490 unsigned int sectorsize)
1491{
1492 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1493}
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495xfs_buftarg_t *
1496xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001497 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001499 int external,
1500 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 xfs_buftarg_t *btp;
1503
1504 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1505
Dave Chinnerebad8612010-09-22 10:47:20 +10001506 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001507 btp->bt_dev = bdev->bd_dev;
1508 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001509 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1510 if (!btp->bt_bdi)
1511 goto error;
1512
Dave Chinner430cbeb2010-12-02 16:30:55 +11001513 INIT_LIST_HEAD(&btp->bt_lru);
1514 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (xfs_setsize_buftarg_early(btp, bdev))
1516 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001517 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1518 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1519 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return btp;
1521
1522error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001523 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return NULL;
1525}
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001528 * Add a buffer to the delayed write list.
1529 *
1530 * This queues a buffer for writeout if it hasn't already been. Note that
1531 * neither this routine nor the buffer list submission functions perform
1532 * any internal synchronization. It is expected that the lists are thread-local
1533 * to the callers.
1534 *
1535 * Returns true if we queued up the buffer, or false if it already had
1536 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001538bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001539xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001540 struct xfs_buf *bp,
1541 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001543 ASSERT(xfs_buf_islocked(bp));
1544 ASSERT(!(bp->b_flags & XBF_READ));
1545
1546 /*
1547 * If the buffer is already marked delwri it already is queued up
1548 * by someone else for imediate writeout. Just ignore it in that
1549 * case.
1550 */
1551 if (bp->b_flags & _XBF_DELWRI_Q) {
1552 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1553 return false;
1554 }
David Chinnera6867a62006-01-11 15:37:58 +11001555
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001556 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1557
Dave Chinnerd808f612010-02-02 10:13:42 +11001558 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001559 * If a buffer gets written out synchronously or marked stale while it
1560 * is on a delwri list we lazily remove it. To do this, the other party
1561 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1562 * It remains referenced and on the list. In a rare corner case it
1563 * might get readded to a delwri list after the synchronous writeout, in
1564 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001565 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001566 bp->b_flags |= _XBF_DELWRI_Q;
1567 if (list_empty(&bp->b_list)) {
1568 atomic_inc(&bp->b_hold);
1569 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001570 }
David Chinner585e6d82007-02-10 18:32:29 +11001571
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001572 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001573}
1574
Dave Chinner089716a2010-01-26 15:13:25 +11001575/*
1576 * Compare function is more complex than it needs to be because
1577 * the return value is only 32 bits and we are doing comparisons
1578 * on 64 bit values
1579 */
1580static int
1581xfs_buf_cmp(
1582 void *priv,
1583 struct list_head *a,
1584 struct list_head *b)
1585{
1586 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1587 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1588 xfs_daddr_t diff;
1589
1590 diff = ap->b_bn - bp->b_bn;
1591 if (diff < 0)
1592 return -1;
1593 if (diff > 0)
1594 return 1;
1595 return 0;
1596}
1597
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001598static int
1599__xfs_buf_delwri_submit(
1600 struct list_head *buffer_list,
1601 struct list_head *io_list,
1602 bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001604 struct blk_plug plug;
1605 struct xfs_buf *bp, *n;
1606 int pinned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001608 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
1609 if (!wait) {
1610 if (xfs_buf_ispinned(bp)) {
1611 pinned++;
1612 continue;
1613 }
1614 if (!xfs_buf_trylock(bp))
1615 continue;
1616 } else {
1617 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001620 /*
1621 * Someone else might have written the buffer synchronously or
1622 * marked it stale in the meantime. In that case only the
1623 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1624 * reference and remove it from the list here.
1625 */
1626 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1627 list_del_init(&bp->b_list);
1628 xfs_buf_relse(bp);
1629 continue;
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001632 list_move_tail(&bp->b_list, io_list);
1633 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001636 list_sort(NULL, io_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001637
1638 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001639 list_for_each_entry_safe(bp, n, io_list, b_list) {
1640 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
1641 bp->b_flags |= XBF_WRITE;
1642
1643 if (!wait) {
1644 bp->b_flags |= XBF_ASYNC;
1645 list_del_init(&bp->b_list);
Dave Chinner089716a2010-01-26 15:13:25 +11001646 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001647 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001649 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001651 return pinned;
1652}
Nathan Scottf07c2252006-09-28 10:52:15 +10001653
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001654/*
1655 * Write out a buffer list asynchronously.
1656 *
1657 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1658 * out and not wait for I/O completion on any of the buffers. This interface
1659 * is only safely useable for callers that can track I/O completion by higher
1660 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1661 * function.
1662 */
1663int
1664xfs_buf_delwri_submit_nowait(
1665 struct list_head *buffer_list)
1666{
1667 LIST_HEAD (io_list);
1668 return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
1669}
1670
1671/*
1672 * Write out a buffer list synchronously.
1673 *
1674 * This will take the @buffer_list, write all buffers out and wait for I/O
1675 * completion on all of the buffers. @buffer_list is consumed by the function,
1676 * so callers must have some other way of tracking buffers if they require such
1677 * functionality.
1678 */
1679int
1680xfs_buf_delwri_submit(
1681 struct list_head *buffer_list)
1682{
1683 LIST_HEAD (io_list);
1684 int error = 0, error2;
1685 struct xfs_buf *bp;
1686
1687 __xfs_buf_delwri_submit(buffer_list, &io_list, true);
1688
1689 /* Wait for IO to complete. */
1690 while (!list_empty(&io_list)) {
1691 bp = list_first_entry(&io_list, struct xfs_buf, b_list);
1692
1693 list_del_init(&bp->b_list);
1694 error2 = xfs_buf_iowait(bp);
1695 xfs_buf_relse(bp);
1696 if (!error)
1697 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001700 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001703int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001704xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Nathan Scott87582802006-03-14 13:18:19 +11001706 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1707 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001708 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001709 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001710
Dave Chinner51749e42010-09-08 09:00:22 +00001711 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001712 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001713 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001714 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001718 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001719 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001720 out:
Nathan Scott87582802006-03-14 13:18:19 +11001721 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724void
Nathan Scottce8e9222006-01-11 15:39:08 +11001725xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001727 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001728 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}