blob: 86d9af70ab3bea23da4a67e33eab54c9214c20b4 [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{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000603 ASSERT(!(flags & XBF_WRITE));
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100604 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
605
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000606 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200607 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100608
Dave Chinner0e95f192012-04-23 15:58:46 +1000609 xfs_buf_iorequest(bp);
610 if (flags & XBF_ASYNC)
611 return 0;
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000612 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000616xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100618 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100620 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Nathan Scottce8e9222006-01-11 15:39:08 +1100622 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Nathan Scottce8e9222006-01-11 15:39:08 +1100624 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000626 bp = xfs_buf_get(target, ioff, isize, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100627 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000628 trace_xfs_buf_read(bp, flags, _RET_IP_);
629
Nathan Scottce8e9222006-01-11 15:39:08 +1100630 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100631 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100632 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100633 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /*
635 * Read ahead call which is already satisfied,
636 * drop the buffer
637 */
638 goto no_buffer;
639 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100641 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 }
644
Nathan Scottce8e9222006-01-11 15:39:08 +1100645 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100648 if (flags & (XBF_LOCK | XBF_TRYLOCK))
649 xfs_buf_unlock(bp);
650 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return NULL;
652}
653
654/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100655 * If we are not low on memory then do the readahead in a deadlock
656 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
658void
Nathan Scottce8e9222006-01-11 15:39:08 +1100659xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100661 xfs_off_t ioff,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000662 size_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100664 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return;
666
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000667 xfs_buf_read(target, ioff, isize,
668 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Dave Chinner5adc94c2010-09-24 21:58:31 +1000671/*
672 * Read an uncached buffer from disk. Allocates and returns a locked
673 * buffer containing the disk contents or nothing.
674 */
675struct xfs_buf *
676xfs_buf_read_uncached(
677 struct xfs_mount *mp,
678 struct xfs_buftarg *target,
679 xfs_daddr_t daddr,
680 size_t length,
681 int flags)
682{
683 xfs_buf_t *bp;
684 int error;
685
686 bp = xfs_buf_get_uncached(target, length, flags);
687 if (!bp)
688 return NULL;
689
690 /* set up the buffer for a read IO */
Dave Chinner5adc94c2010-09-24 21:58:31 +1000691 XFS_BUF_SET_ADDR(bp, daddr);
692 XFS_BUF_READ(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000693
694 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000695 error = xfs_buf_iowait(bp);
Dave Chinner0e95f192012-04-23 15:58:46 +1000696 if (error) {
Dave Chinner5adc94c2010-09-24 21:58:31 +1000697 xfs_buf_relse(bp);
698 return NULL;
699 }
700 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Dave Chinner44396472011-04-21 09:34:27 +0000703/*
704 * Return a buffer allocated as an empty buffer and associated to external
705 * memory via xfs_buf_associate_memory() back to it's empty state.
706 */
707void
708xfs_buf_set_empty(
709 struct xfs_buf *bp,
710 size_t len)
711{
712 if (bp->b_pages)
713 _xfs_buf_free_pages(bp);
714
715 bp->b_pages = NULL;
716 bp->b_page_count = 0;
717 bp->b_addr = NULL;
718 bp->b_file_offset = 0;
719 bp->b_buffer_length = bp->b_count_desired = len;
720 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 size_t len,
Dave Chinner686865f2010-09-24 20:07:47 +1000782 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000784 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
785 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000788 bp = xfs_buf_alloc(target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (unlikely(bp == NULL))
790 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000792 error = _xfs_buf_get_pages(bp, page_count, 0);
793 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 goto fail_free_buf;
795
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000796 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000797 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000798 if (!bp->b_pages[i])
799 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000801 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000803 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
804 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100805 xfs_warn(target->bt_mount,
806 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Dave Chinner686865f2010-09-24 20:07:47 +1000810 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000814 while (--i >= 0)
815 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000816 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 fail_free_buf:
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000818 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 fail:
820 return NULL;
821}
822
823/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 * Increment reference count on buffer, to hold the buffer concurrently
825 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * Must hold the buffer already to call this function.
827 */
828void
Nathan Scottce8e9222006-01-11 15:39:08 +1100829xfs_buf_hold(
830 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000832 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100833 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
836/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100837 * Releases a hold on the specified buffer. If the
838 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 */
840void
Nathan Scottce8e9222006-01-11 15:39:08 +1100841xfs_buf_rele(
842 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Dave Chinner74f75a02010-09-24 19:59:04 +1000844 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000846 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Dave Chinner74f75a02010-09-24 19:59:04 +1000848 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100849 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000850 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100851 if (atomic_dec_and_test(&bp->b_hold))
852 xfs_buf_free(bp);
853 return;
854 }
855
Dave Chinner74f75a02010-09-24 19:59:04 +1000856 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100857
Lachlan McIlroy37906892008-08-13 15:42:10 +1000858 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000859 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000860 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100861 atomic_read(&bp->b_lru_ref)) {
862 xfs_buf_lru_add(bp);
863 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100864 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100865 xfs_buf_lru_del(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000866 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner74f75a02010-09-24 19:59:04 +1000867 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
868 spin_unlock(&pag->pag_buf_lock);
869 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100870 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872 }
873}
874
875
876/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100877 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100878 *
879 * If we come across a stale, pinned, locked buffer, we know that we are
880 * being asked to lock a buffer that has been reallocated. Because it is
881 * pinned, we know that the log has not been pushed to disk and hence it
882 * will still be locked. Rather than continuing to have trylock attempts
883 * fail until someone else pushes the log, push it ourselves before
884 * returning. This means that the xfsaild will not get stuck trying
885 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 */
887int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200888xfs_buf_trylock(
889 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 int locked;
892
Nathan Scottce8e9222006-01-11 15:39:08 +1100893 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000894 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100895 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100896 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
897 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000898
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200899 trace_xfs_buf_trylock(bp, _RET_IP_);
900 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100904 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000905 *
906 * If we come across a stale, pinned, locked buffer, we know that we
907 * are being asked to lock a buffer that has been reallocated. Because
908 * it is pinned, we know that the log has not been pushed to disk and
909 * hence it will still be locked. Rather than sleeping until someone
910 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100912void
913xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200914 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000916 trace_xfs_buf_lock(bp, _RET_IP_);
917
Dave Chinnered3b4d62010-05-21 12:07:08 +1000918 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000919 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100920 down(&bp->b_sema);
921 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000922
923 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926void
Nathan Scottce8e9222006-01-11 15:39:08 +1100927xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200928 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Nathan Scottce8e9222006-01-11 15:39:08 +1100930 XB_CLEAR_OWNER(bp);
931 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000932
933 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Nathan Scottce8e9222006-01-11 15:39:08 +1100936STATIC void
937xfs_buf_wait_unpin(
938 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 DECLARE_WAITQUEUE (wait, current);
941
Nathan Scottce8e9222006-01-11 15:39:08 +1100942 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return;
944
Nathan Scottce8e9222006-01-11 15:39:08 +1100945 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 for (;;) {
947 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100948 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100950 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100952 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 set_current_state(TASK_RUNNING);
954}
955
956/*
957 * Buffer Utility Routines
958 */
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100961xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000962 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
David Howellsc4028952006-11-22 14:57:56 +0000964 xfs_buf_t *bp =
965 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400967 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100968 (*(bp->b_iodone))(bp);
969 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 xfs_buf_relse(bp);
971}
972
973void
Nathan Scottce8e9222006-01-11 15:39:08 +1100974xfs_buf_ioend(
975 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 int schedule)
977{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000978 trace_xfs_buf_iodone(bp, _RET_IP_);
979
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100980 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100981 if (bp->b_error == 0)
982 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Nathan Scottce8e9222006-01-11 15:39:08 +1100984 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000986 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100987 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 } else {
David Howellsc4028952006-11-22 14:57:56 +0000989 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +1000992 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994}
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996void
Nathan Scottce8e9222006-01-11 15:39:08 +1100997xfs_buf_ioerror(
998 xfs_buf_t *bp,
999 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001002 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001003 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
Christoph Hellwig901796a2011-10-10 16:52:49 +00001006void
1007xfs_buf_ioerror_alert(
1008 struct xfs_buf *bp,
1009 const char *func)
1010{
1011 xfs_alert(bp->b_target->bt_mount,
1012"metadata I/O error: block 0x%llx (\"%s\") error %d buf count %zd",
1013 (__uint64_t)XFS_BUF_ADDR(bp), func,
1014 bp->b_error, XFS_BUF_COUNT(bp));
1015}
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001018xfs_bwrite(
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001019 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001021 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001023 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001025 bp->b_flags |= XBF_WRITE;
1026 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
1027
Christoph Hellwig939d7232010-07-20 17:51:16 +10001028 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Christoph Hellwig8c383662010-03-12 10:59:40 +00001030 error = xfs_buf_iowait(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001031 if (error) {
1032 xfs_force_shutdown(bp->b_target->bt_mount,
1033 SHUTDOWN_META_IO_ERROR);
1034 }
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001035 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001036}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Christoph Hellwig4e234712010-01-13 22:17:56 +00001038/*
1039 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001040 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001041 * so that the proper iodone callbacks get called.
1042 */
1043STATIC int
1044xfs_bioerror(
1045 xfs_buf_t *bp)
1046{
1047#ifdef XFSERRORDEBUG
1048 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1049#endif
1050
1051 /*
1052 * No need to wait until the buffer is unpinned, we aren't flushing it.
1053 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001054 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001055
1056 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001057 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001058 */
1059 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001060 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001061 xfs_buf_stale(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001062
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001063 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001064
1065 return EIO;
1066}
1067
1068/*
1069 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001070 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001071 * This is meant for userdata errors; metadata bufs come with
1072 * iodone functions attached, so that we can track down errors.
1073 */
1074STATIC int
1075xfs_bioerror_relse(
1076 struct xfs_buf *bp)
1077{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001078 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001079 /*
1080 * No need to wait until the buffer is unpinned.
1081 * We aren't flushing it.
1082 *
1083 * chunkhold expects B_DONE to be set, whether
1084 * we actually finish the I/O or not. We don't want to
1085 * change that interface.
1086 */
1087 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001088 XFS_BUF_DONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001089 xfs_buf_stale(bp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001090 bp->b_iodone = NULL;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001091 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001092 /*
1093 * Mark b_error and B_ERROR _both_.
1094 * Lot's of chunkcache code assumes that.
1095 * There's no reason to mark error for
1096 * ASYNC buffers.
1097 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001098 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig5fde0322011-10-10 16:52:44 +00001099 complete(&bp->b_iowait);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001100 } else {
1101 xfs_buf_relse(bp);
1102 }
1103
1104 return EIO;
1105}
1106
1107
1108/*
1109 * All xfs metadata buffers except log state machine buffers
1110 * get this attached as their b_bdstrat callback function.
1111 * This is so that we can catch a buffer
1112 * after prematurely unpinning it to forcibly shutdown the filesystem.
1113 */
1114int
1115xfs_bdstrat_cb(
1116 struct xfs_buf *bp)
1117{
Dave Chinnerebad8612010-09-22 10:47:20 +10001118 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001119 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1120 /*
1121 * Metadata write that didn't get logged but
1122 * written delayed anyway. These aren't associated
1123 * with a transaction, and can be ignored.
1124 */
1125 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1126 return xfs_bioerror_relse(bp);
1127 else
1128 return xfs_bioerror(bp);
1129 }
1130
1131 xfs_buf_iorequest(bp);
1132 return 0;
1133}
1134
1135/*
1136 * Wrapper around bdstrat so that we can stop data from going to disk in case
1137 * we are shutting down the filesystem. Typically user data goes thru this
1138 * path; one of the exceptions is the superblock.
1139 */
1140void
1141xfsbdstrat(
1142 struct xfs_mount *mp,
1143 struct xfs_buf *bp)
1144{
1145 if (XFS_FORCED_SHUTDOWN(mp)) {
1146 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1147 xfs_bioerror_relse(bp);
1148 return;
1149 }
1150
1151 xfs_buf_iorequest(bp);
1152}
1153
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001154STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001155_xfs_buf_ioend(
1156 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int schedule)
1158{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001159 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001160 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Al Viro782e3b32007-10-12 07:17:47 +01001163STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001164xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 int error)
1167{
Nathan Scottce8e9222006-01-11 15:39:08 +11001168 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001170 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
James Bottomley73c77e22010-01-25 11:42:24 -06001172 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1173 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1174
Nathan Scottce8e9222006-01-11 15:39:08 +11001175 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001180_xfs_buf_ioapply(
1181 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001183 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001185 int offset = bp->b_offset;
1186 int size = bp->b_count_desired;
1187 sector_t sector = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Nathan Scottce8e9222006-01-11 15:39:08 +11001189 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 map_i = 0;
1191
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001192 if (bp->b_flags & XBF_WRITE) {
1193 if (bp->b_flags & XBF_SYNCIO)
1194 rw = WRITE_SYNC;
1195 else
1196 rw = WRITE;
1197 if (bp->b_flags & XBF_FUA)
1198 rw |= REQ_FUA;
1199 if (bp->b_flags & XBF_FLUSH)
1200 rw |= REQ_FLUSH;
1201 } else if (bp->b_flags & XBF_READ_AHEAD) {
1202 rw = READA;
Nathan Scott51bdd702006-09-28 11:01:57 +10001203 } else {
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001204 rw = READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001205 }
1206
Christoph Hellwig34951f52011-07-26 15:06:44 +00001207 /* we only use the buffer cache for meta-data */
1208 rw |= REQ_META;
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001211 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1213 if (nr_pages > total_nr_pages)
1214 nr_pages = total_nr_pages;
1215
1216 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001217 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001219 bio->bi_end_io = xfs_buf_bio_end_io;
1220 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Dave Chinner0e6e8472011-03-26 09:16:45 +11001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 for (; size && nr_pages; nr_pages--, map_i++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001224 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 if (nbytes > size)
1227 nbytes = size;
1228
Nathan Scottce8e9222006-01-11 15:39:08 +11001229 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1230 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 break;
1232
1233 offset = 0;
1234 sector += nbytes >> BBSHIFT;
1235 size -= nbytes;
1236 total_nr_pages--;
1237 }
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001240 if (xfs_buf_is_vmapped(bp)) {
1241 flush_kernel_vmap_range(bp->b_addr,
1242 xfs_buf_vmap_len(bp));
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 submit_bio(rw, bio);
1245 if (size)
1246 goto next_chunk;
1247 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001248 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001249 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251}
1252
Dave Chinner0e95f192012-04-23 15:58:46 +10001253void
Nathan Scottce8e9222006-01-11 15:39:08 +11001254xfs_buf_iorequest(
1255 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001257 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001259 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Christoph Hellwig375ec69d2011-08-23 08:28:03 +00001261 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001262 xfs_buf_wait_unpin(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001263 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 /* Set the count to 1 initially, this will stop an I/O
1266 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001267 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001269 atomic_set(&bp->b_io_remaining, 1);
1270 _xfs_buf_ioapply(bp);
1271 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Nathan Scottce8e9222006-01-11 15:39:08 +11001273 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
1276/*
Dave Chinner0e95f192012-04-23 15:58:46 +10001277 * Waits for I/O to complete on the buffer supplied. It returns immediately if
1278 * no I/O is pending or there is already a pending error on the buffer. It
1279 * returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 */
1281int
Nathan Scottce8e9222006-01-11 15:39:08 +11001282xfs_buf_iowait(
1283 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001285 trace_xfs_buf_iowait(bp, _RET_IP_);
1286
Dave Chinner0e95f192012-04-23 15:58:46 +10001287 if (!bp->b_error)
1288 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001289
1290 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001291 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Nathan Scottce8e9222006-01-11 15:39:08 +11001294xfs_caddr_t
1295xfs_buf_offset(
1296 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 size_t offset)
1298{
1299 struct page *page;
1300
Nathan Scottce8e9222006-01-11 15:39:08 +11001301 if (bp->b_flags & XBF_MAPPED)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001302 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Nathan Scottce8e9222006-01-11 15:39:08 +11001304 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001305 page = bp->b_pages[offset >> PAGE_SHIFT];
1306 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
1309/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 * Move data into or out of a buffer.
1311 */
1312void
Nathan Scottce8e9222006-01-11 15:39:08 +11001313xfs_buf_iomove(
1314 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 size_t boff, /* starting buffer offset */
1316 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001317 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001318 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 size_t bend, cpoff, csize;
1321 struct page *page;
1322
1323 bend = boff + bsize;
1324 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001325 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1326 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 csize = min_t(size_t,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001328 PAGE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Dave Chinner0e6e8472011-03-26 09:16:45 +11001330 ASSERT(((csize + cpoff) <= PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001333 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 memset(page_address(page) + cpoff, 0, csize);
1335 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001336 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 memcpy(data, page_address(page) + cpoff, csize);
1338 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001339 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 memcpy(page_address(page) + cpoff, data, csize);
1341 }
1342
1343 boff += csize;
1344 data += csize;
1345 }
1346}
1347
1348/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001349 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 */
1351
1352/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001353 * Wait for any bufs with callbacks that have been submitted but have not yet
1354 * returned. These buffers will have an elevated hold count, so wait on those
1355 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 */
1357void
1358xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001359 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Dave Chinner430cbeb2010-12-02 16:30:55 +11001361 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Dave Chinner430cbeb2010-12-02 16:30:55 +11001363restart:
1364 spin_lock(&btp->bt_lru_lock);
1365 while (!list_empty(&btp->bt_lru)) {
1366 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1367 if (atomic_read(&bp->b_hold) > 1) {
1368 spin_unlock(&btp->bt_lru_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001369 delay(100);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001370 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001372 /*
Paul Bolle90802ed2011-12-05 13:00:34 +01001373 * clear the LRU reference count so the buffer doesn't get
Dave Chinner430cbeb2010-12-02 16:30:55 +11001374 * ignored in xfs_buf_rele().
1375 */
1376 atomic_set(&bp->b_lru_ref, 0);
1377 spin_unlock(&btp->bt_lru_lock);
1378 xfs_buf_rele(bp);
1379 spin_lock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001381 spin_unlock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
Dave Chinnerff57ab22010-11-30 17:27:57 +11001384int
1385xfs_buftarg_shrink(
1386 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001387 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001388{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001389 struct xfs_buftarg *btp = container_of(shrink,
1390 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001391 struct xfs_buf *bp;
Ying Han1495f232011-05-24 17:12:27 -07001392 int nr_to_scan = sc->nr_to_scan;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001393 LIST_HEAD(dispose);
1394
1395 if (!nr_to_scan)
1396 return btp->bt_lru_nr;
1397
1398 spin_lock(&btp->bt_lru_lock);
1399 while (!list_empty(&btp->bt_lru)) {
1400 if (nr_to_scan-- <= 0)
1401 break;
1402
1403 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1404
1405 /*
1406 * Decrement the b_lru_ref count unless the value is already
1407 * zero. If the value is already zero, we need to reclaim the
1408 * buffer, otherwise it gets another trip through the LRU.
1409 */
1410 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1411 list_move_tail(&bp->b_lru, &btp->bt_lru);
1412 continue;
1413 }
1414
1415 /*
1416 * remove the buffer from the LRU now to avoid needing another
1417 * lock round trip inside xfs_buf_rele().
1418 */
1419 list_move(&bp->b_lru, &dispose);
1420 btp->bt_lru_nr--;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001421 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001422 spin_unlock(&btp->bt_lru_lock);
1423
1424 while (!list_empty(&dispose)) {
1425 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1426 list_del_init(&bp->b_lru);
1427 xfs_buf_rele(bp);
1428 }
1429
1430 return btp->bt_lru_nr;
David Chinnera6867a62006-01-11 15:37:58 +11001431}
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433void
1434xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001435 struct xfs_mount *mp,
1436 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001438 unregister_shrinker(&btp->bt_shrinker);
1439
Christoph Hellwigb7963132009-03-03 14:48:37 -05001440 if (mp->m_flags & XFS_MOUNT_BARRIER)
1441 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001442
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001443 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446STATIC int
1447xfs_setsize_buftarg_flags(
1448 xfs_buftarg_t *btp,
1449 unsigned int blocksize,
1450 unsigned int sectorsize,
1451 int verbose)
1452{
Nathan Scottce8e9222006-01-11 15:39:08 +11001453 btp->bt_bsize = blocksize;
1454 btp->bt_sshift = ffs(sectorsize) - 1;
1455 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Nathan Scottce8e9222006-01-11 15:39:08 +11001457 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001458 char name[BDEVNAME_SIZE];
1459
1460 bdevname(btp->bt_bdev, name);
1461
Dave Chinner4f107002011-03-07 10:00:35 +11001462 xfs_warn(btp->bt_mount,
1463 "Cannot set_blocksize to %u on device %s\n",
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001464 sectorsize, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return EINVAL;
1466 }
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return 0;
1469}
1470
1471/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001472 * When allocating the initial buffer target we have not yet
1473 * read in the superblock, so don't know what sized sectors
1474 * are being used is at this early stage. Play safe.
1475 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476STATIC int
1477xfs_setsize_buftarg_early(
1478 xfs_buftarg_t *btp,
1479 struct block_device *bdev)
1480{
1481 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001482 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
1485int
1486xfs_setsize_buftarg(
1487 xfs_buftarg_t *btp,
1488 unsigned int blocksize,
1489 unsigned int sectorsize)
1490{
1491 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1492}
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494xfs_buftarg_t *
1495xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001496 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001498 int external,
1499 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 xfs_buftarg_t *btp;
1502
1503 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1504
Dave Chinnerebad8612010-09-22 10:47:20 +10001505 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001506 btp->bt_dev = bdev->bd_dev;
1507 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001508 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1509 if (!btp->bt_bdi)
1510 goto error;
1511
Dave Chinner430cbeb2010-12-02 16:30:55 +11001512 INIT_LIST_HEAD(&btp->bt_lru);
1513 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (xfs_setsize_buftarg_early(btp, bdev))
1515 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001516 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1517 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1518 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return btp;
1520
1521error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001522 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return NULL;
1524}
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001527 * Add a buffer to the delayed write list.
1528 *
1529 * This queues a buffer for writeout if it hasn't already been. Note that
1530 * neither this routine nor the buffer list submission functions perform
1531 * any internal synchronization. It is expected that the lists are thread-local
1532 * to the callers.
1533 *
1534 * Returns true if we queued up the buffer, or false if it already had
1535 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001537bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001538xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001539 struct xfs_buf *bp,
1540 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001542 ASSERT(xfs_buf_islocked(bp));
1543 ASSERT(!(bp->b_flags & XBF_READ));
1544
1545 /*
1546 * If the buffer is already marked delwri it already is queued up
1547 * by someone else for imediate writeout. Just ignore it in that
1548 * case.
1549 */
1550 if (bp->b_flags & _XBF_DELWRI_Q) {
1551 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1552 return false;
1553 }
David Chinnera6867a62006-01-11 15:37:58 +11001554
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001555 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1556
Dave Chinnerd808f612010-02-02 10:13:42 +11001557 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001558 * If a buffer gets written out synchronously or marked stale while it
1559 * is on a delwri list we lazily remove it. To do this, the other party
1560 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1561 * It remains referenced and on the list. In a rare corner case it
1562 * might get readded to a delwri list after the synchronous writeout, in
1563 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001564 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001565 bp->b_flags |= _XBF_DELWRI_Q;
1566 if (list_empty(&bp->b_list)) {
1567 atomic_inc(&bp->b_hold);
1568 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001569 }
David Chinner585e6d82007-02-10 18:32:29 +11001570
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001571 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001572}
1573
Dave Chinner089716a2010-01-26 15:13:25 +11001574/*
1575 * Compare function is more complex than it needs to be because
1576 * the return value is only 32 bits and we are doing comparisons
1577 * on 64 bit values
1578 */
1579static int
1580xfs_buf_cmp(
1581 void *priv,
1582 struct list_head *a,
1583 struct list_head *b)
1584{
1585 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1586 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1587 xfs_daddr_t diff;
1588
1589 diff = ap->b_bn - bp->b_bn;
1590 if (diff < 0)
1591 return -1;
1592 if (diff > 0)
1593 return 1;
1594 return 0;
1595}
1596
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001597static int
1598__xfs_buf_delwri_submit(
1599 struct list_head *buffer_list,
1600 struct list_head *io_list,
1601 bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001603 struct blk_plug plug;
1604 struct xfs_buf *bp, *n;
1605 int pinned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001607 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
1608 if (!wait) {
1609 if (xfs_buf_ispinned(bp)) {
1610 pinned++;
1611 continue;
1612 }
1613 if (!xfs_buf_trylock(bp))
1614 continue;
1615 } else {
1616 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001619 /*
1620 * Someone else might have written the buffer synchronously or
1621 * marked it stale in the meantime. In that case only the
1622 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1623 * reference and remove it from the list here.
1624 */
1625 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1626 list_del_init(&bp->b_list);
1627 xfs_buf_relse(bp);
1628 continue;
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001631 list_move_tail(&bp->b_list, io_list);
1632 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001635 list_sort(NULL, io_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001636
1637 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001638 list_for_each_entry_safe(bp, n, io_list, b_list) {
1639 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
1640 bp->b_flags |= XBF_WRITE;
1641
1642 if (!wait) {
1643 bp->b_flags |= XBF_ASYNC;
1644 list_del_init(&bp->b_list);
Dave Chinner089716a2010-01-26 15:13:25 +11001645 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001646 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001648 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001650 return pinned;
1651}
Nathan Scottf07c2252006-09-28 10:52:15 +10001652
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001653/*
1654 * Write out a buffer list asynchronously.
1655 *
1656 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1657 * out and not wait for I/O completion on any of the buffers. This interface
1658 * is only safely useable for callers that can track I/O completion by higher
1659 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1660 * function.
1661 */
1662int
1663xfs_buf_delwri_submit_nowait(
1664 struct list_head *buffer_list)
1665{
1666 LIST_HEAD (io_list);
1667 return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
1668}
1669
1670/*
1671 * Write out a buffer list synchronously.
1672 *
1673 * This will take the @buffer_list, write all buffers out and wait for I/O
1674 * completion on all of the buffers. @buffer_list is consumed by the function,
1675 * so callers must have some other way of tracking buffers if they require such
1676 * functionality.
1677 */
1678int
1679xfs_buf_delwri_submit(
1680 struct list_head *buffer_list)
1681{
1682 LIST_HEAD (io_list);
1683 int error = 0, error2;
1684 struct xfs_buf *bp;
1685
1686 __xfs_buf_delwri_submit(buffer_list, &io_list, true);
1687
1688 /* Wait for IO to complete. */
1689 while (!list_empty(&io_list)) {
1690 bp = list_first_entry(&io_list, struct xfs_buf, b_list);
1691
1692 list_del_init(&bp->b_list);
1693 error2 = xfs_buf_iowait(bp);
1694 xfs_buf_relse(bp);
1695 if (!error)
1696 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
1698
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001699 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001702int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001703xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Nathan Scott87582802006-03-14 13:18:19 +11001705 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1706 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001707 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001708 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001709
Dave Chinner51749e42010-09-08 09:00:22 +00001710 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001711 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001712 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001713 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001717 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001718 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001719 out:
Nathan Scott87582802006-03-14 13:18:19 +11001720 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723void
Nathan Scottce8e9222006-01-11 15:39:08 +11001724xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001726 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001727 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}