blob: da2541e5ff81847e705a6f34f648ec18c5cb2289 [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,
Nathan Scott204ab252006-01-11 20:50:22 +1100175 xfs_off_t range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 size_t range_length,
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
181 bp = kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags));
182 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 memset(bp, 0, sizeof(xfs_buf_t));
191 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100192 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000193 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100194 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100195 INIT_LIST_HEAD(&bp->b_list);
Dave Chinner74f75a02010-09-24 19:59:04 +1000196 RB_CLEAR_NODE(&bp->b_rbnode);
Thomas Gleixnera731cd12010-09-07 14:33:15 +0000197 sema_init(&bp->b_sema, 0); /* held, no waiters */
Nathan Scottce8e9222006-01-11 15:39:08 +1100198 XB_SET_OWNER(bp);
199 bp->b_target = target;
200 bp->b_file_offset = range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /*
202 * Set buffer_length and count_desired to the same value initially.
203 * I/O routines should use count_desired, which will be the same in
204 * most cases but may be reset (e.g. XFS recovery).
205 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100206 bp->b_buffer_length = bp->b_count_desired = range_length;
207 bp->b_flags = flags;
208 bp->b_bn = XFS_BUF_DADDR_NULL;
209 atomic_set(&bp->b_pin_count, 0);
210 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Nathan Scottce8e9222006-01-11 15:39:08 +1100212 XFS_STATS_INC(xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000213 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000214
215 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100219 * Allocate a page array capable of holding a specified number
220 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 */
222STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100223_xfs_buf_get_pages(
224 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100226 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100229 if (bp->b_pages == NULL) {
230 bp->b_offset = xfs_buf_poff(bp->b_file_offset);
231 bp->b_page_count = page_count;
232 if (page_count <= XB_PAGES) {
233 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100235 bp->b_pages = kmem_alloc(sizeof(struct page *) *
236 page_count, xb_to_km(flags));
237 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -ENOMEM;
239 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100240 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242 return 0;
243}
244
245/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100246 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 */
248STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100249_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 xfs_buf_t *bp)
251{
Nathan Scottce8e9222006-01-11 15:39:08 +1100252 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000253 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000254 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256}
257
258/*
259 * Releases the specified buffer.
260 *
261 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100262 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 * hashed and refcounted buffers
264 */
265void
Nathan Scottce8e9222006-01-11 15:39:08 +1100266xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 xfs_buf_t *bp)
268{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000269 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Dave Chinner430cbeb2010-12-02 16:30:55 +1100271 ASSERT(list_empty(&bp->b_lru));
272
Dave Chinner0e6e8472011-03-26 09:16:45 +1100273 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 uint i;
275
James Bottomley73c77e22010-01-25 11:42:24 -0600276 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000277 vm_unmap_ram(bp->b_addr - bp->b_offset,
278 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Nathan Scott948ecdb2006-09-28 11:03:13 +1000280 for (i = 0; i < bp->b_page_count; i++) {
281 struct page *page = bp->b_pages[i];
282
Dave Chinner0e6e8472011-03-26 09:16:45 +1100283 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000284 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100285 } else if (bp->b_flags & _XBF_KMEM)
286 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000287 _xfs_buf_free_pages(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000288 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100292 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
294STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100295xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 xfs_buf_t *bp,
297 uint flags)
298{
Nathan Scottce8e9222006-01-11 15:39:08 +1100299 size_t size = bp->b_count_desired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100301 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 unsigned short page_count, i;
Nathan Scott204ab252006-01-11 20:50:22 +1100303 xfs_off_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 int error;
305
Dave Chinner0e6e8472011-03-26 09:16:45 +1100306 /*
307 * for buffers that are contained within a single page, just allocate
308 * the memory from the heap - there's no need for the complexity of
309 * page arrays to keep allocation down to order 0.
310 */
311 if (bp->b_buffer_length < PAGE_SIZE) {
312 bp->b_addr = kmem_alloc(bp->b_buffer_length, xb_to_km(flags));
313 if (!bp->b_addr) {
314 /* low memory - use alloc_page loop instead */
315 goto use_alloc_page;
316 }
317
318 if (((unsigned long)(bp->b_addr + bp->b_buffer_length - 1) &
319 PAGE_MASK) !=
320 ((unsigned long)bp->b_addr & PAGE_MASK)) {
321 /* b_addr spans two pages - use alloc_page instead */
322 kmem_free(bp->b_addr);
323 bp->b_addr = NULL;
324 goto use_alloc_page;
325 }
326 bp->b_offset = offset_in_page(bp->b_addr);
327 bp->b_pages = bp->b_page_array;
328 bp->b_pages[0] = virt_to_page(bp->b_addr);
329 bp->b_page_count = 1;
330 bp->b_flags |= XBF_MAPPED | _XBF_KMEM;
331 return 0;
332 }
333
334use_alloc_page:
Nathan Scottce8e9222006-01-11 15:39:08 +1100335 end = bp->b_file_offset + bp->b_buffer_length;
336 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
Nathan Scottce8e9222006-01-11 15:39:08 +1100337 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (unlikely(error))
339 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Nathan Scottce8e9222006-01-11 15:39:08 +1100341 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100342 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Nathan Scottce8e9222006-01-11 15:39:08 +1100344 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct page *page;
346 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100347retry:
348 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100350 if (flags & XBF_READ_AHEAD) {
351 bp->b_page_count = i;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100352 error = ENOMEM;
353 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 /*
357 * This could deadlock.
358 *
359 * But until all the XFS lowlevel code is revamped to
360 * handle buffer allocation failures we can't do much.
361 */
362 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100363 xfs_err(NULL,
364 "possible memory allocation deadlock in %s (mode:0x%x)",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000365 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Nathan Scottce8e9222006-01-11 15:39:08 +1100367 XFS_STATS_INC(xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200368 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 goto retry;
370 }
371
Nathan Scottce8e9222006-01-11 15:39:08 +1100372 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Dave Chinner0e6e8472011-03-26 09:16:45 +1100374 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100376 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 offset = 0;
378 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Dave Chinner0e6e8472011-03-26 09:16:45 +1100381out_free_pages:
382 for (i = 0; i < bp->b_page_count; i++)
383 __free_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return error;
385}
386
387/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300388 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
390STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100391_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 xfs_buf_t *bp,
393 uint flags)
394{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100395 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100396 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100397 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100398 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
399 bp->b_flags |= XBF_MAPPED;
400 } else if (flags & XBF_MAPPED) {
Dave Chinnera19fb382011-03-26 09:13:42 +1100401 int retried = 0;
402
403 do {
404 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
405 -1, PAGE_KERNEL);
406 if (bp->b_addr)
407 break;
408 vm_unmap_aliases();
409 } while (retried++ <= 1);
410
411 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100413 bp->b_addr += bp->b_offset;
414 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
417 return 0;
418}
419
420/*
421 * Finding and Reading Buffers
422 */
423
424/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100425 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * a given range of an inode. The buffer is returned
Chandra Seetharamaneabbaf12011-09-08 20:18:50 +0000427 * locked. No I/O is implied by this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
429xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100430_xfs_buf_find(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 xfs_buftarg_t *btp, /* block device target */
Nathan Scott204ab252006-01-11 20:50:22 +1100432 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100434 xfs_buf_flags_t flags,
435 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Nathan Scott204ab252006-01-11 20:50:22 +1100437 xfs_off_t range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 size_t range_length;
Dave Chinner74f75a02010-09-24 19:59:04 +1000439 struct xfs_perag *pag;
440 struct rb_node **rbp;
441 struct rb_node *parent;
442 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 range_base = (ioff << BBSHIFT);
445 range_length = (isize << BBSHIFT);
446
447 /* Check for IOs smaller than the sector size / not sector aligned */
Nathan Scottce8e9222006-01-11 15:39:08 +1100448 ASSERT(!(range_length < (1 << btp->bt_sshift)));
Nathan Scott204ab252006-01-11 20:50:22 +1100449 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Dave Chinner74f75a02010-09-24 19:59:04 +1000451 /* get tree root */
452 pag = xfs_perag_get(btp->bt_mount,
453 xfs_daddr_to_agno(btp->bt_mount, ioff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Dave Chinner74f75a02010-09-24 19:59:04 +1000455 /* walk tree */
456 spin_lock(&pag->pag_buf_lock);
457 rbp = &pag->pag_buf_tree.rb_node;
458 parent = NULL;
459 bp = NULL;
460 while (*rbp) {
461 parent = *rbp;
462 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Dave Chinner74f75a02010-09-24 19:59:04 +1000464 if (range_base < bp->b_file_offset)
465 rbp = &(*rbp)->rb_left;
466 else if (range_base > bp->b_file_offset)
467 rbp = &(*rbp)->rb_right;
468 else {
469 /*
470 * found a block offset match. If the range doesn't
471 * match, the only way this is allowed is if the buffer
472 * in the cache is stale and the transaction that made
473 * it stale has not yet committed. i.e. we are
474 * reallocating a busy extent. Skip this buffer and
475 * continue searching to the right for an exact match.
476 */
477 if (bp->b_buffer_length != range_length) {
478 ASSERT(bp->b_flags & XBF_STALE);
479 rbp = &(*rbp)->rb_right;
480 continue;
481 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100482 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 goto found;
484 }
485 }
486
487 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100488 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000489 rb_link_node(&new_bp->b_rbnode, parent, rbp);
490 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
491 /* the buffer keeps the perag reference until it is freed */
492 new_bp->b_pag = pag;
493 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100495 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000496 spin_unlock(&pag->pag_buf_lock);
497 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100499 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000502 spin_unlock(&pag->pag_buf_lock);
503 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200505 if (!xfs_buf_trylock(bp)) {
506 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100507 xfs_buf_rele(bp);
508 XFS_STATS_INC(xb_busy_locked);
509 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200511 xfs_buf_lock(bp);
512 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
Dave Chinner0e6e8472011-03-26 09:16:45 +1100515 /*
516 * if the buffer is stale, clear all the external state associated with
517 * it. We need to keep flags such as how we allocated the buffer memory
518 * intact here.
519 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100520 if (bp->b_flags & XBF_STALE) {
521 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100522 bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
David Chinner2f926582005-09-05 08:33:35 +1000523 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000524
525 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100526 XFS_STATS_INC(xb_get_locked);
527 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530/*
Dave Chinner38158322011-09-30 04:45:02 +0000531 * Assembles a buffer covering the specified range. The code is optimised for
532 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
533 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
Dave Chinner38158322011-09-30 04:45:02 +0000535struct xfs_buf *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000536xfs_buf_get(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100538 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100540 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Dave Chinner38158322011-09-30 04:45:02 +0000542 struct xfs_buf *bp;
543 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100544 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Dave Chinner38158322011-09-30 04:45:02 +0000546 bp = _xfs_buf_find(target, ioff, isize, flags, NULL);
547 if (likely(bp))
548 goto found;
549
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000550 new_bp = xfs_buf_alloc(target, ioff << BBSHIFT, isize << BBSHIFT,
551 flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100552 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return NULL;
554
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000555 error = xfs_buf_allocate_memory(new_bp, flags);
556 if (error) {
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000557 kmem_zone_free(xfs_buf_zone, new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000558 return NULL;
559 }
560
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000561 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
562 if (!bp) {
563 xfs_buf_free(new_bp);
564 return NULL;
565 }
566
567 if (bp != new_bp)
568 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Dave Chinner38158322011-09-30 04:45:02 +0000570 /*
571 * Now we have a workable buffer, fill in the block number so
572 * that we can do IO on it.
573 */
574 bp->b_bn = ioff;
575 bp->b_count_desired = bp->b_buffer_length;
576
577found:
Nathan Scottce8e9222006-01-11 15:39:08 +1100578 if (!(bp->b_flags & XBF_MAPPED)) {
579 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100581 xfs_warn(target->bt_mount,
582 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 goto no_buffer;
584 }
585 }
586
Nathan Scottce8e9222006-01-11 15:39:08 +1100587 XFS_STATS_INC(xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000588 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100589 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Dave Chinner38158322011-09-30 04:45:02 +0000591no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100592 if (flags & (XBF_LOCK | XBF_TRYLOCK))
593 xfs_buf_unlock(bp);
594 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return NULL;
596}
597
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100598STATIC int
599_xfs_buf_read(
600 xfs_buf_t *bp,
601 xfs_buf_flags_t flags)
602{
603 int status;
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
611 status = xfs_buf_iorequest(bp);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000612 if (status || bp->b_error || (flags & XBF_ASYNC))
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000613 return status;
614 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,
Nathan Scott204ab252006-01-11 20:50:22 +1100620 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 size_t isize,
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
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000628 bp = xfs_buf_get(target, ioff, isize, 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,
Nathan Scott204ab252006-01-11 20:50:22 +1100663 xfs_off_t ioff,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000664 size_t isize)
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
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000669 xfs_buf_read(target, ioff, isize,
670 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(
679 struct xfs_mount *mp,
680 struct xfs_buftarg *target,
681 xfs_daddr_t daddr,
682 size_t length,
683 int flags)
684{
685 xfs_buf_t *bp;
686 int error;
687
688 bp = xfs_buf_get_uncached(target, length, flags);
689 if (!bp)
690 return NULL;
691
692 /* set up the buffer for a read IO */
Dave Chinner5adc94c2010-09-24 21:58:31 +1000693 XFS_BUF_SET_ADDR(bp, daddr);
694 XFS_BUF_READ(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000695
696 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000697 error = xfs_buf_iowait(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000698 if (error || bp->b_error) {
699 xfs_buf_relse(bp);
700 return NULL;
701 }
702 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Dave Chinner44396472011-04-21 09:34:27 +0000705/*
706 * Return a buffer allocated as an empty buffer and associated to external
707 * memory via xfs_buf_associate_memory() back to it's empty state.
708 */
709void
710xfs_buf_set_empty(
711 struct xfs_buf *bp,
712 size_t len)
713{
714 if (bp->b_pages)
715 _xfs_buf_free_pages(bp);
716
717 bp->b_pages = NULL;
718 bp->b_page_count = 0;
719 bp->b_addr = NULL;
720 bp->b_file_offset = 0;
721 bp->b_buffer_length = bp->b_count_desired = len;
722 bp->b_bn = XFS_BUF_DADDR_NULL;
723 bp->b_flags &= ~XBF_MAPPED;
724}
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726static inline struct page *
727mem_to_page(
728 void *addr)
729{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800730 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return virt_to_page(addr);
732 } else {
733 return vmalloc_to_page(addr);
734 }
735}
736
737int
Nathan Scottce8e9222006-01-11 15:39:08 +1100738xfs_buf_associate_memory(
739 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 void *mem,
741 size_t len)
742{
743 int rval;
744 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100745 unsigned long pageaddr;
746 unsigned long offset;
747 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int page_count;
749
Dave Chinner0e6e8472011-03-26 09:16:45 +1100750 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100751 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100752 buflen = PAGE_ALIGN(len + offset);
753 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100756 if (bp->b_pages)
757 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Nathan Scottce8e9222006-01-11 15:39:08 +1100759 bp->b_pages = NULL;
760 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Christoph Hellwig36fae172009-07-18 18:14:58 -0400762 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (rval)
764 return rval;
765
Nathan Scottce8e9222006-01-11 15:39:08 +1100766 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100768 for (i = 0; i < bp->b_page_count; i++) {
769 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100770 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100773 bp->b_count_desired = len;
774 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100775 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 return 0;
778}
779
780xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000781xfs_buf_get_uncached(
782 struct xfs_buftarg *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 size_t len,
Dave Chinner686865f2010-09-24 20:07:47 +1000784 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000786 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
787 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000790 bp = xfs_buf_alloc(target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (unlikely(bp == NULL))
792 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000794 error = _xfs_buf_get_pages(bp, page_count, 0);
795 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 goto fail_free_buf;
797
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000798 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000799 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000800 if (!bp->b_pages[i])
801 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000803 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000805 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
806 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100807 xfs_warn(target->bt_mount,
808 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Dave Chinner686865f2010-09-24 20:07:47 +1000812 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000816 while (--i >= 0)
817 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000818 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 fail_free_buf:
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000820 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 fail:
822 return NULL;
823}
824
825/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * Increment reference count on buffer, to hold the buffer concurrently
827 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 * Must hold the buffer already to call this function.
829 */
830void
Nathan Scottce8e9222006-01-11 15:39:08 +1100831xfs_buf_hold(
832 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000834 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100835 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100839 * Releases a hold on the specified buffer. If the
840 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 */
842void
Nathan Scottce8e9222006-01-11 15:39:08 +1100843xfs_buf_rele(
844 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Dave Chinner74f75a02010-09-24 19:59:04 +1000846 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000848 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Dave Chinner74f75a02010-09-24 19:59:04 +1000850 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100851 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000852 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100853 if (atomic_dec_and_test(&bp->b_hold))
854 xfs_buf_free(bp);
855 return;
856 }
857
Dave Chinner74f75a02010-09-24 19:59:04 +1000858 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100859
Lachlan McIlroy37906892008-08-13 15:42:10 +1000860 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000861 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000862 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100863 atomic_read(&bp->b_lru_ref)) {
864 xfs_buf_lru_add(bp);
865 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100866 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100867 xfs_buf_lru_del(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000868 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner74f75a02010-09-24 19:59:04 +1000869 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
870 spin_unlock(&pag->pag_buf_lock);
871 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100872 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874 }
875}
876
877
878/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100879 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100880 *
881 * If we come across a stale, pinned, locked buffer, we know that we are
882 * being asked to lock a buffer that has been reallocated. Because it is
883 * pinned, we know that the log has not been pushed to disk and hence it
884 * will still be locked. Rather than continuing to have trylock attempts
885 * fail until someone else pushes the log, push it ourselves before
886 * returning. This means that the xfsaild will not get stuck trying
887 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
889int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200890xfs_buf_trylock(
891 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 int locked;
894
Nathan Scottce8e9222006-01-11 15:39:08 +1100895 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000896 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100897 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100898 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
899 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000900
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200901 trace_xfs_buf_trylock(bp, _RET_IP_);
902 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100906 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000907 *
908 * If we come across a stale, pinned, locked buffer, we know that we
909 * are being asked to lock a buffer that has been reallocated. Because
910 * it is pinned, we know that the log has not been pushed to disk and
911 * hence it will still be locked. Rather than sleeping until someone
912 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100914void
915xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200916 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000918 trace_xfs_buf_lock(bp, _RET_IP_);
919
Dave Chinnered3b4d62010-05-21 12:07:08 +1000920 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000921 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100922 down(&bp->b_sema);
923 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000924
925 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928void
Nathan Scottce8e9222006-01-11 15:39:08 +1100929xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200930 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Nathan Scottce8e9222006-01-11 15:39:08 +1100932 XB_CLEAR_OWNER(bp);
933 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000934
935 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
Nathan Scottce8e9222006-01-11 15:39:08 +1100938STATIC void
939xfs_buf_wait_unpin(
940 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 DECLARE_WAITQUEUE (wait, current);
943
Nathan Scottce8e9222006-01-11 15:39:08 +1100944 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return;
946
Nathan Scottce8e9222006-01-11 15:39:08 +1100947 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 for (;;) {
949 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100950 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100952 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100954 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 set_current_state(TASK_RUNNING);
956}
957
958/*
959 * Buffer Utility Routines
960 */
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100963xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000964 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
David Howellsc4028952006-11-22 14:57:56 +0000966 xfs_buf_t *bp =
967 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400969 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100970 (*(bp->b_iodone))(bp);
971 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 xfs_buf_relse(bp);
973}
974
975void
Nathan Scottce8e9222006-01-11 15:39:08 +1100976xfs_buf_ioend(
977 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 int schedule)
979{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000980 trace_xfs_buf_iodone(bp, _RET_IP_);
981
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100982 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100983 if (bp->b_error == 0)
984 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Nathan Scottce8e9222006-01-11 15:39:08 +1100986 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000988 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100989 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 } else {
David Howellsc4028952006-11-22 14:57:56 +0000991 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +1000994 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998void
Nathan Scottce8e9222006-01-11 15:39:08 +1100999xfs_buf_ioerror(
1000 xfs_buf_t *bp,
1001 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001004 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001005 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
Christoph Hellwig901796a2011-10-10 16:52:49 +00001008void
1009xfs_buf_ioerror_alert(
1010 struct xfs_buf *bp,
1011 const char *func)
1012{
1013 xfs_alert(bp->b_target->bt_mount,
1014"metadata I/O error: block 0x%llx (\"%s\") error %d buf count %zd",
1015 (__uint64_t)XFS_BUF_ADDR(bp), func,
1016 bp->b_error, XFS_BUF_COUNT(bp));
1017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001020xfs_bwrite(
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001021 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001023 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001025 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001027 bp->b_flags |= XBF_WRITE;
1028 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
1029
Christoph Hellwig939d7232010-07-20 17:51:16 +10001030 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Christoph Hellwig8c383662010-03-12 10:59:40 +00001032 error = xfs_buf_iowait(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001033 if (error) {
1034 xfs_force_shutdown(bp->b_target->bt_mount,
1035 SHUTDOWN_META_IO_ERROR);
1036 }
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001037 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001038}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Christoph Hellwig4e234712010-01-13 22:17:56 +00001040/*
1041 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001042 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001043 * so that the proper iodone callbacks get called.
1044 */
1045STATIC int
1046xfs_bioerror(
1047 xfs_buf_t *bp)
1048{
1049#ifdef XFSERRORDEBUG
1050 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1051#endif
1052
1053 /*
1054 * No need to wait until the buffer is unpinned, we aren't flushing it.
1055 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001056 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001057
1058 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001059 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001060 */
1061 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001062 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001063 xfs_buf_stale(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001064
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001065 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001066
1067 return EIO;
1068}
1069
1070/*
1071 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001072 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001073 * This is meant for userdata errors; metadata bufs come with
1074 * iodone functions attached, so that we can track down errors.
1075 */
1076STATIC int
1077xfs_bioerror_relse(
1078 struct xfs_buf *bp)
1079{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001080 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001081 /*
1082 * No need to wait until the buffer is unpinned.
1083 * We aren't flushing it.
1084 *
1085 * chunkhold expects B_DONE to be set, whether
1086 * we actually finish the I/O or not. We don't want to
1087 * change that interface.
1088 */
1089 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001090 XFS_BUF_DONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001091 xfs_buf_stale(bp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001092 bp->b_iodone = NULL;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001093 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001094 /*
1095 * Mark b_error and B_ERROR _both_.
1096 * Lot's of chunkcache code assumes that.
1097 * There's no reason to mark error for
1098 * ASYNC buffers.
1099 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001100 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig5fde0322011-10-10 16:52:44 +00001101 complete(&bp->b_iowait);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001102 } else {
1103 xfs_buf_relse(bp);
1104 }
1105
1106 return EIO;
1107}
1108
1109
1110/*
1111 * All xfs metadata buffers except log state machine buffers
1112 * get this attached as their b_bdstrat callback function.
1113 * This is so that we can catch a buffer
1114 * after prematurely unpinning it to forcibly shutdown the filesystem.
1115 */
1116int
1117xfs_bdstrat_cb(
1118 struct xfs_buf *bp)
1119{
Dave Chinnerebad8612010-09-22 10:47:20 +10001120 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001121 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1122 /*
1123 * Metadata write that didn't get logged but
1124 * written delayed anyway. These aren't associated
1125 * with a transaction, and can be ignored.
1126 */
1127 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1128 return xfs_bioerror_relse(bp);
1129 else
1130 return xfs_bioerror(bp);
1131 }
1132
1133 xfs_buf_iorequest(bp);
1134 return 0;
1135}
1136
1137/*
1138 * Wrapper around bdstrat so that we can stop data from going to disk in case
1139 * we are shutting down the filesystem. Typically user data goes thru this
1140 * path; one of the exceptions is the superblock.
1141 */
1142void
1143xfsbdstrat(
1144 struct xfs_mount *mp,
1145 struct xfs_buf *bp)
1146{
1147 if (XFS_FORCED_SHUTDOWN(mp)) {
1148 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1149 xfs_bioerror_relse(bp);
1150 return;
1151 }
1152
1153 xfs_buf_iorequest(bp);
1154}
1155
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001156STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001157_xfs_buf_ioend(
1158 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int schedule)
1160{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001161 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001162 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Al Viro782e3b32007-10-12 07:17:47 +01001165STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001166xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 int error)
1169{
Nathan Scottce8e9222006-01-11 15:39:08 +11001170 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001172 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
James Bottomley73c77e22010-01-25 11:42:24 -06001174 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1175 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1176
Nathan Scottce8e9222006-01-11 15:39:08 +11001177 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001182_xfs_buf_ioapply(
1183 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001185 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001187 int offset = bp->b_offset;
1188 int size = bp->b_count_desired;
1189 sector_t sector = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Nathan Scottce8e9222006-01-11 15:39:08 +11001191 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 map_i = 0;
1193
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001194 if (bp->b_flags & XBF_WRITE) {
1195 if (bp->b_flags & XBF_SYNCIO)
1196 rw = WRITE_SYNC;
1197 else
1198 rw = WRITE;
1199 if (bp->b_flags & XBF_FUA)
1200 rw |= REQ_FUA;
1201 if (bp->b_flags & XBF_FLUSH)
1202 rw |= REQ_FLUSH;
1203 } else if (bp->b_flags & XBF_READ_AHEAD) {
1204 rw = READA;
Nathan Scott51bdd702006-09-28 11:01:57 +10001205 } else {
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001206 rw = READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001207 }
1208
Christoph Hellwig34951f52011-07-26 15:06:44 +00001209 /* we only use the buffer cache for meta-data */
1210 rw |= REQ_META;
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001213 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1215 if (nr_pages > total_nr_pages)
1216 nr_pages = total_nr_pages;
1217
1218 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001219 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001221 bio->bi_end_io = xfs_buf_bio_end_io;
1222 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Dave Chinner0e6e8472011-03-26 09:16:45 +11001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 for (; size && nr_pages; nr_pages--, map_i++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001226 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 if (nbytes > size)
1229 nbytes = size;
1230
Nathan Scottce8e9222006-01-11 15:39:08 +11001231 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1232 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
1234
1235 offset = 0;
1236 sector += nbytes >> BBSHIFT;
1237 size -= nbytes;
1238 total_nr_pages--;
1239 }
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001242 if (xfs_buf_is_vmapped(bp)) {
1243 flush_kernel_vmap_range(bp->b_addr,
1244 xfs_buf_vmap_len(bp));
1245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 submit_bio(rw, bio);
1247 if (size)
1248 goto next_chunk;
1249 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001250 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001251 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253}
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255int
Nathan Scottce8e9222006-01-11 15:39:08 +11001256xfs_buf_iorequest(
1257 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001259 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001261 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Christoph Hellwig375ec692011-08-23 08:28:03 +00001263 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001264 xfs_buf_wait_unpin(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001265 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /* Set the count to 1 initially, this will stop an I/O
1268 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001269 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001271 atomic_set(&bp->b_io_remaining, 1);
1272 _xfs_buf_ioapply(bp);
1273 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Nathan Scottce8e9222006-01-11 15:39:08 +11001275 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return 0;
1277}
1278
1279/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001280 * Waits for I/O to complete on the buffer supplied.
1281 * It returns immediately if no I/O is pending.
1282 * It returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 */
1284int
Nathan Scottce8e9222006-01-11 15:39:08 +11001285xfs_buf_iowait(
1286 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001288 trace_xfs_buf_iowait(bp, _RET_IP_);
1289
David Chinnerb4dd3302008-08-13 16:36:11 +10001290 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001291
1292 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001293 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Nathan Scottce8e9222006-01-11 15:39:08 +11001296xfs_caddr_t
1297xfs_buf_offset(
1298 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 size_t offset)
1300{
1301 struct page *page;
1302
Nathan Scottce8e9222006-01-11 15:39:08 +11001303 if (bp->b_flags & XBF_MAPPED)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001304 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Nathan Scottce8e9222006-01-11 15:39:08 +11001306 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001307 page = bp->b_pages[offset >> PAGE_SHIFT];
1308 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
1311/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 * Move data into or out of a buffer.
1313 */
1314void
Nathan Scottce8e9222006-01-11 15:39:08 +11001315xfs_buf_iomove(
1316 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 size_t boff, /* starting buffer offset */
1318 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001319 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001320 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 size_t bend, cpoff, csize;
1323 struct page *page;
1324
1325 bend = boff + bsize;
1326 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001327 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1328 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 csize = min_t(size_t,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001330 PAGE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Dave Chinner0e6e8472011-03-26 09:16:45 +11001332 ASSERT(((csize + cpoff) <= PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001335 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 memset(page_address(page) + cpoff, 0, csize);
1337 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001338 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 memcpy(data, page_address(page) + cpoff, csize);
1340 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001341 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 memcpy(page_address(page) + cpoff, data, csize);
1343 }
1344
1345 boff += csize;
1346 data += csize;
1347 }
1348}
1349
1350/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001351 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 */
1353
1354/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001355 * Wait for any bufs with callbacks that have been submitted but have not yet
1356 * returned. These buffers will have an elevated hold count, so wait on those
1357 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 */
1359void
1360xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001361 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Dave Chinner430cbeb2010-12-02 16:30:55 +11001363 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Dave Chinner430cbeb2010-12-02 16:30:55 +11001365restart:
1366 spin_lock(&btp->bt_lru_lock);
1367 while (!list_empty(&btp->bt_lru)) {
1368 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1369 if (atomic_read(&bp->b_hold) > 1) {
1370 spin_unlock(&btp->bt_lru_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001371 delay(100);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001372 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001374 /*
Paul Bolle90802ed2011-12-05 13:00:34 +01001375 * clear the LRU reference count so the buffer doesn't get
Dave Chinner430cbeb2010-12-02 16:30:55 +11001376 * ignored in xfs_buf_rele().
1377 */
1378 atomic_set(&bp->b_lru_ref, 0);
1379 spin_unlock(&btp->bt_lru_lock);
1380 xfs_buf_rele(bp);
1381 spin_lock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001383 spin_unlock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
Dave Chinnerff57ab22010-11-30 17:27:57 +11001386int
1387xfs_buftarg_shrink(
1388 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001389 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001390{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001391 struct xfs_buftarg *btp = container_of(shrink,
1392 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001393 struct xfs_buf *bp;
Ying Han1495f232011-05-24 17:12:27 -07001394 int nr_to_scan = sc->nr_to_scan;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001395 LIST_HEAD(dispose);
1396
1397 if (!nr_to_scan)
1398 return btp->bt_lru_nr;
1399
1400 spin_lock(&btp->bt_lru_lock);
1401 while (!list_empty(&btp->bt_lru)) {
1402 if (nr_to_scan-- <= 0)
1403 break;
1404
1405 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1406
1407 /*
1408 * Decrement the b_lru_ref count unless the value is already
1409 * zero. If the value is already zero, we need to reclaim the
1410 * buffer, otherwise it gets another trip through the LRU.
1411 */
1412 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1413 list_move_tail(&bp->b_lru, &btp->bt_lru);
1414 continue;
1415 }
1416
1417 /*
1418 * remove the buffer from the LRU now to avoid needing another
1419 * lock round trip inside xfs_buf_rele().
1420 */
1421 list_move(&bp->b_lru, &dispose);
1422 btp->bt_lru_nr--;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001423 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001424 spin_unlock(&btp->bt_lru_lock);
1425
1426 while (!list_empty(&dispose)) {
1427 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1428 list_del_init(&bp->b_lru);
1429 xfs_buf_rele(bp);
1430 }
1431
1432 return btp->bt_lru_nr;
David Chinnera6867a62006-01-11 15:37:58 +11001433}
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435void
1436xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001437 struct xfs_mount *mp,
1438 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001440 unregister_shrinker(&btp->bt_shrinker);
1441
Christoph Hellwigb7963132009-03-03 14:48:37 -05001442 if (mp->m_flags & XFS_MOUNT_BARRIER)
1443 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001444
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001445 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448STATIC int
1449xfs_setsize_buftarg_flags(
1450 xfs_buftarg_t *btp,
1451 unsigned int blocksize,
1452 unsigned int sectorsize,
1453 int verbose)
1454{
Nathan Scottce8e9222006-01-11 15:39:08 +11001455 btp->bt_bsize = blocksize;
1456 btp->bt_sshift = ffs(sectorsize) - 1;
1457 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Nathan Scottce8e9222006-01-11 15:39:08 +11001459 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001460 char name[BDEVNAME_SIZE];
1461
1462 bdevname(btp->bt_bdev, name);
1463
Dave Chinner4f107002011-03-07 10:00:35 +11001464 xfs_warn(btp->bt_mount,
1465 "Cannot set_blocksize to %u on device %s\n",
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001466 sectorsize, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return EINVAL;
1468 }
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return 0;
1471}
1472
1473/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001474 * When allocating the initial buffer target we have not yet
1475 * read in the superblock, so don't know what sized sectors
1476 * are being used is at this early stage. Play safe.
1477 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478STATIC int
1479xfs_setsize_buftarg_early(
1480 xfs_buftarg_t *btp,
1481 struct block_device *bdev)
1482{
1483 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001484 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487int
1488xfs_setsize_buftarg(
1489 xfs_buftarg_t *btp,
1490 unsigned int blocksize,
1491 unsigned int sectorsize)
1492{
1493 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1494}
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496xfs_buftarg_t *
1497xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001498 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001500 int external,
1501 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 xfs_buftarg_t *btp;
1504
1505 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1506
Dave Chinnerebad8612010-09-22 10:47:20 +10001507 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001508 btp->bt_dev = bdev->bd_dev;
1509 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001510 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1511 if (!btp->bt_bdi)
1512 goto error;
1513
Dave Chinner430cbeb2010-12-02 16:30:55 +11001514 INIT_LIST_HEAD(&btp->bt_lru);
1515 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (xfs_setsize_buftarg_early(btp, bdev))
1517 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001518 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1519 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1520 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return btp;
1522
1523error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001524 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return NULL;
1526}
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001529 * Add a buffer to the delayed write list.
1530 *
1531 * This queues a buffer for writeout if it hasn't already been. Note that
1532 * neither this routine nor the buffer list submission functions perform
1533 * any internal synchronization. It is expected that the lists are thread-local
1534 * to the callers.
1535 *
1536 * Returns true if we queued up the buffer, or false if it already had
1537 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001539bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001540xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001541 struct xfs_buf *bp,
1542 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001544 ASSERT(xfs_buf_islocked(bp));
1545 ASSERT(!(bp->b_flags & XBF_READ));
1546
1547 /*
1548 * If the buffer is already marked delwri it already is queued up
1549 * by someone else for imediate writeout. Just ignore it in that
1550 * case.
1551 */
1552 if (bp->b_flags & _XBF_DELWRI_Q) {
1553 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1554 return false;
1555 }
David Chinnera6867a62006-01-11 15:37:58 +11001556
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001557 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1558
Dave Chinnerd808f612010-02-02 10:13:42 +11001559 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001560 * If a buffer gets written out synchronously or marked stale while it
1561 * is on a delwri list we lazily remove it. To do this, the other party
1562 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1563 * It remains referenced and on the list. In a rare corner case it
1564 * might get readded to a delwri list after the synchronous writeout, in
1565 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001566 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001567 bp->b_flags |= _XBF_DELWRI_Q;
1568 if (list_empty(&bp->b_list)) {
1569 atomic_inc(&bp->b_hold);
1570 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001571 }
David Chinner585e6d82007-02-10 18:32:29 +11001572
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001573 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001574}
1575
Dave Chinner089716a2010-01-26 15:13:25 +11001576/*
1577 * Compare function is more complex than it needs to be because
1578 * the return value is only 32 bits and we are doing comparisons
1579 * on 64 bit values
1580 */
1581static int
1582xfs_buf_cmp(
1583 void *priv,
1584 struct list_head *a,
1585 struct list_head *b)
1586{
1587 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1588 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1589 xfs_daddr_t diff;
1590
1591 diff = ap->b_bn - bp->b_bn;
1592 if (diff < 0)
1593 return -1;
1594 if (diff > 0)
1595 return 1;
1596 return 0;
1597}
1598
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001599static int
1600__xfs_buf_delwri_submit(
1601 struct list_head *buffer_list,
1602 struct list_head *io_list,
1603 bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001605 struct blk_plug plug;
1606 struct xfs_buf *bp, *n;
1607 int pinned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001609 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
1610 if (!wait) {
1611 if (xfs_buf_ispinned(bp)) {
1612 pinned++;
1613 continue;
1614 }
1615 if (!xfs_buf_trylock(bp))
1616 continue;
1617 } else {
1618 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001621 /*
1622 * Someone else might have written the buffer synchronously or
1623 * marked it stale in the meantime. In that case only the
1624 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1625 * reference and remove it from the list here.
1626 */
1627 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1628 list_del_init(&bp->b_list);
1629 xfs_buf_relse(bp);
1630 continue;
1631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001633 list_move_tail(&bp->b_list, io_list);
1634 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001637 list_sort(NULL, io_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001638
1639 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001640 list_for_each_entry_safe(bp, n, io_list, b_list) {
1641 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
1642 bp->b_flags |= XBF_WRITE;
1643
1644 if (!wait) {
1645 bp->b_flags |= XBF_ASYNC;
1646 list_del_init(&bp->b_list);
Dave Chinner089716a2010-01-26 15:13:25 +11001647 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001648 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001650 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001652 return pinned;
1653}
Nathan Scottf07c2252006-09-28 10:52:15 +10001654
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001655/*
1656 * Write out a buffer list asynchronously.
1657 *
1658 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1659 * out and not wait for I/O completion on any of the buffers. This interface
1660 * is only safely useable for callers that can track I/O completion by higher
1661 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1662 * function.
1663 */
1664int
1665xfs_buf_delwri_submit_nowait(
1666 struct list_head *buffer_list)
1667{
1668 LIST_HEAD (io_list);
1669 return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
1670}
1671
1672/*
1673 * Write out a buffer list synchronously.
1674 *
1675 * This will take the @buffer_list, write all buffers out and wait for I/O
1676 * completion on all of the buffers. @buffer_list is consumed by the function,
1677 * so callers must have some other way of tracking buffers if they require such
1678 * functionality.
1679 */
1680int
1681xfs_buf_delwri_submit(
1682 struct list_head *buffer_list)
1683{
1684 LIST_HEAD (io_list);
1685 int error = 0, error2;
1686 struct xfs_buf *bp;
1687
1688 __xfs_buf_delwri_submit(buffer_list, &io_list, true);
1689
1690 /* Wait for IO to complete. */
1691 while (!list_empty(&io_list)) {
1692 bp = list_first_entry(&io_list, struct xfs_buf, b_list);
1693
1694 list_del_init(&bp->b_list);
1695 error2 = xfs_buf_iowait(bp);
1696 xfs_buf_relse(bp);
1697 if (!error)
1698 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001701 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001704int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001705xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Nathan Scott87582802006-03-14 13:18:19 +11001707 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1708 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001709 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001710 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001711
Dave Chinner51749e42010-09-08 09:00:22 +00001712 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001713 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001714 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001715 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001717 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001719 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001720 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001721 out:
Nathan Scott87582802006-03-14 13:18:19 +11001722 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725void
Nathan Scottce8e9222006-01-11 15:39:08 +11001726xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001728 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001729 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}