blob: 8366348aa1f9195ef1db67e77219ee70b528b8a0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scottf07c2252006-09-28 10:52:15 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Vlad Apostolov93c189c2006-11-11 18:03:49 +110018#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/stddef.h>
20#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
23#include <linux/init.h>
24#include <linux/vmalloc.h>
25#include <linux/bio.h>
26#include <linux/sysctl.h>
27#include <linux/proc_fs.h>
28#include <linux/workqueue.h>
29#include <linux/percpu.h>
30#include <linux/blkdev.h>
31#include <linux/hash.h>
Christoph Hellwig4df08c52005-09-05 08:34:18 +100032#include <linux/kthread.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080033#include <linux/migrate.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070034#include <linux/backing-dev.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Christoph Hellwigb7963132009-03-03 14:48:37 -050037#include "xfs_sb.h"
38#include "xfs_inum.h"
Dave Chinnered3b4d62010-05-21 12:07:08 +100039#include "xfs_log.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050040#include "xfs_ag.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050041#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050043
David Chinner7989cb82007-02-10 18:34:56 +110044static kmem_zone_t *xfs_buf_zone;
Christoph Hellwig23ea4032005-06-21 15:14:01 +100045
David Chinner7989cb82007-02-10 18:34:56 +110046static struct workqueue_struct *xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Nathan Scottce8e9222006-01-11 15:39:08 +110048#ifdef XFS_BUF_LOCK_TRACKING
49# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
50# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
51# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#else
Nathan Scottce8e9222006-01-11 15:39:08 +110053# define XB_SET_OWNER(bp) do { } while (0)
54# define XB_CLEAR_OWNER(bp) do { } while (0)
55# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57
Nathan Scottce8e9222006-01-11 15:39:08 +110058#define xb_to_gfp(flags) \
59 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
60 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Nathan Scottce8e9222006-01-11 15:39:08 +110062#define xb_to_km(flags) \
63 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
James Bottomley73c77e22010-01-25 11:42:24 -060066static inline int
67xfs_buf_is_vmapped(
68 struct xfs_buf *bp)
69{
70 /*
71 * Return true if the buffer is vmapped.
72 *
73 * The XBF_MAPPED flag is set if the buffer should be mapped, but the
74 * code is clever enough to know it doesn't have to map a single page,
75 * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
76 */
77 return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
78}
79
80static inline int
81xfs_buf_vmap_len(
82 struct xfs_buf *bp)
83{
84 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
85}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
Dave Chinner430cbeb2010-12-02 16:30:55 +110088 * xfs_buf_lru_add - add a buffer to the LRU.
89 *
90 * The LRU takes a new reference to the buffer so that it will only be freed
91 * once the shrinker takes the buffer off the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
Dave Chinner430cbeb2010-12-02 16:30:55 +110093STATIC void
94xfs_buf_lru_add(
95 struct xfs_buf *bp)
96{
97 struct xfs_buftarg *btp = bp->b_target;
98
99 spin_lock(&btp->bt_lru_lock);
100 if (list_empty(&bp->b_lru)) {
101 atomic_inc(&bp->b_hold);
102 list_add_tail(&bp->b_lru, &btp->bt_lru);
103 btp->bt_lru_nr++;
104 }
105 spin_unlock(&btp->bt_lru_lock);
106}
107
108/*
109 * xfs_buf_lru_del - remove a buffer from the LRU
110 *
111 * The unlocked check is safe here because it only occurs when there are not
112 * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there
113 * to optimise the shrinker removing the buffer from the LRU and calling
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300114 * xfs_buf_free(). i.e. it removes an unnecessary round trip on the
Dave Chinner430cbeb2010-12-02 16:30:55 +1100115 * bt_lru_lock.
116 */
117STATIC void
118xfs_buf_lru_del(
119 struct xfs_buf *bp)
120{
121 struct xfs_buftarg *btp = bp->b_target;
122
123 if (list_empty(&bp->b_lru))
124 return;
125
126 spin_lock(&btp->bt_lru_lock);
127 if (!list_empty(&bp->b_lru)) {
128 list_del_init(&bp->b_lru);
129 btp->bt_lru_nr--;
130 }
131 spin_unlock(&btp->bt_lru_lock);
132}
133
134/*
135 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
136 * b_lru_ref count so that the buffer is freed immediately when the buffer
137 * reference count falls to zero. If the buffer is already on the LRU, we need
138 * to remove the reference that LRU holds on the buffer.
139 *
140 * This prevents build-up of stale buffers on the LRU.
141 */
142void
143xfs_buf_stale(
144 struct xfs_buf *bp)
145{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000146 ASSERT(xfs_buf_islocked(bp));
147
Dave Chinner430cbeb2010-12-02 16:30:55 +1100148 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000149
150 /*
151 * Clear the delwri status so that a delwri queue walker will not
152 * flush this buffer to disk now that it is stale. The delwri queue has
153 * a reference to the buffer, so this is safe to do.
154 */
155 bp->b_flags &= ~_XBF_DELWRI_Q;
156
Dave Chinner430cbeb2010-12-02 16:30:55 +1100157 atomic_set(&(bp)->b_lru_ref, 0);
158 if (!list_empty(&bp->b_lru)) {
159 struct xfs_buftarg *btp = bp->b_target;
160
161 spin_lock(&btp->bt_lru_lock);
162 if (!list_empty(&bp->b_lru)) {
163 list_del_init(&bp->b_lru);
164 btp->bt_lru_nr--;
165 atomic_dec(&bp->b_hold);
166 }
167 spin_unlock(&btp->bt_lru_lock);
168 }
169 ASSERT(atomic_read(&bp->b_hold) >= 1);
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000172struct xfs_buf *
173xfs_buf_alloc(
174 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000175 xfs_daddr_t blkno,
176 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100177 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000179 struct xfs_buf *bp;
180
Dave Chinnerbf813cd2012-04-23 15:58:48 +1000181 bp = kmem_zone_zalloc(xfs_buf_zone, xb_to_km(flags));
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000182 if (unlikely(!bp))
183 return NULL;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100186 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
Dave Chinnera8acad72012-04-23 15:58:54 +1000188 flags &= ~(XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Nathan Scottce8e9222006-01-11 15:39:08 +1100190 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100191 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000192 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100193 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100194 INIT_LIST_HEAD(&bp->b_list);
Dave Chinner74f75a02010-09-24 19:59:04 +1000195 RB_CLEAR_NODE(&bp->b_rbnode);
Thomas Gleixnera731cd12010-09-07 14:33:15 +0000196 sema_init(&bp->b_sema, 0); /* held, no waiters */
Nathan Scottce8e9222006-01-11 15:39:08 +1100197 XB_SET_OWNER(bp);
198 bp->b_target = target;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000201 * Set length and io_length to the same value initially.
202 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * most cases but may be reset (e.g. XFS recovery).
204 */
Dave Chinner4e94b712012-04-23 15:58:51 +1000205 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000206 bp->b_io_length = numblks;
Nathan Scottce8e9222006-01-11 15:39:08 +1100207 bp->b_flags = flags;
Dave Chinnere70b73f2012-04-23 15:58:49 +1000208
209 /*
210 * We do not set the block number here in the buffer because we have not
211 * finished initialising the buffer. We insert the buffer into the cache
212 * in this state, so this ensures that we are unable to do IO on a
213 * buffer that hasn't been fully initialised.
214 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100215 bp->b_bn = XFS_BUF_DADDR_NULL;
216 atomic_set(&bp->b_pin_count, 0);
217 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Nathan Scottce8e9222006-01-11 15:39:08 +1100219 XFS_STATS_INC(xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000220 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000221
222 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100226 * Allocate a page array capable of holding a specified number
227 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
229STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100230_xfs_buf_get_pages(
231 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100233 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100236 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100237 bp->b_page_count = page_count;
238 if (page_count <= XB_PAGES) {
239 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100241 bp->b_pages = kmem_alloc(sizeof(struct page *) *
242 page_count, xb_to_km(flags));
243 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -ENOMEM;
245 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100246 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248 return 0;
249}
250
251/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100252 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
254STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100255_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 xfs_buf_t *bp)
257{
Nathan Scottce8e9222006-01-11 15:39:08 +1100258 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000259 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000260 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262}
263
264/*
265 * Releases the specified buffer.
266 *
267 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100268 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * hashed and refcounted buffers
270 */
271void
Nathan Scottce8e9222006-01-11 15:39:08 +1100272xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 xfs_buf_t *bp)
274{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000275 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Dave Chinner430cbeb2010-12-02 16:30:55 +1100277 ASSERT(list_empty(&bp->b_lru));
278
Dave Chinner0e6e8472011-03-26 09:16:45 +1100279 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 uint i;
281
James Bottomley73c77e22010-01-25 11:42:24 -0600282 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000283 vm_unmap_ram(bp->b_addr - bp->b_offset,
284 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Nathan Scott948ecdb2006-09-28 11:03:13 +1000286 for (i = 0; i < bp->b_page_count; i++) {
287 struct page *page = bp->b_pages[i];
288
Dave Chinner0e6e8472011-03-26 09:16:45 +1100289 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000290 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100291 } else if (bp->b_flags & _XBF_KMEM)
292 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000293 _xfs_buf_free_pages(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000294 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100298 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
300STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100301xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 xfs_buf_t *bp,
303 uint flags)
304{
Dave Chinneraa0e8832012-04-23 15:58:52 +1000305 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100307 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 unsigned short page_count, i;
Dave Chinner795cac72012-04-23 15:58:53 +1000309 xfs_off_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 int error;
311
Dave Chinner0e6e8472011-03-26 09:16:45 +1100312 /*
313 * for buffers that are contained within a single page, just allocate
314 * the memory from the heap - there's no need for the complexity of
315 * page arrays to keep allocation down to order 0.
316 */
Dave Chinner795cac72012-04-23 15:58:53 +1000317 size = BBTOB(bp->b_length);
318 if (size < PAGE_SIZE) {
319 bp->b_addr = kmem_alloc(size, xb_to_km(flags));
Dave Chinner0e6e8472011-03-26 09:16:45 +1100320 if (!bp->b_addr) {
321 /* low memory - use alloc_page loop instead */
322 goto use_alloc_page;
323 }
324
Dave Chinner795cac72012-04-23 15:58:53 +1000325 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100326 ((unsigned long)bp->b_addr & PAGE_MASK)) {
327 /* b_addr spans two pages - use alloc_page instead */
328 kmem_free(bp->b_addr);
329 bp->b_addr = NULL;
330 goto use_alloc_page;
331 }
332 bp->b_offset = offset_in_page(bp->b_addr);
333 bp->b_pages = bp->b_page_array;
334 bp->b_pages[0] = virt_to_page(bp->b_addr);
335 bp->b_page_count = 1;
336 bp->b_flags |= XBF_MAPPED | _XBF_KMEM;
337 return 0;
338 }
339
340use_alloc_page:
Dave Chinner795cac72012-04-23 15:58:53 +1000341 start = BBTOB(bp->b_bn) >> PAGE_SHIFT;
342 end = (BBTOB(bp->b_bn + bp->b_length) + PAGE_SIZE - 1) >> PAGE_SHIFT;
343 page_count = end - start;
Nathan Scottce8e9222006-01-11 15:39:08 +1100344 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (unlikely(error))
346 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Nathan Scottce8e9222006-01-11 15:39:08 +1100348 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100349 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Nathan Scottce8e9222006-01-11 15:39:08 +1100351 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct page *page;
353 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100354retry:
355 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100357 if (flags & XBF_READ_AHEAD) {
358 bp->b_page_count = i;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100359 error = ENOMEM;
360 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
363 /*
364 * This could deadlock.
365 *
366 * But until all the XFS lowlevel code is revamped to
367 * handle buffer allocation failures we can't do much.
368 */
369 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100370 xfs_err(NULL,
371 "possible memory allocation deadlock in %s (mode:0x%x)",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000372 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Nathan Scottce8e9222006-01-11 15:39:08 +1100374 XFS_STATS_INC(xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200375 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 goto retry;
377 }
378
Nathan Scottce8e9222006-01-11 15:39:08 +1100379 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Dave Chinner0e6e8472011-03-26 09:16:45 +1100381 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100383 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 offset = 0;
385 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Dave Chinner0e6e8472011-03-26 09:16:45 +1100388out_free_pages:
389 for (i = 0; i < bp->b_page_count; i++)
390 __free_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return error;
392}
393
394/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300395 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
397STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100398_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 xfs_buf_t *bp,
400 uint flags)
401{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100402 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100403 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100404 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100405 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
406 bp->b_flags |= XBF_MAPPED;
407 } else if (flags & XBF_MAPPED) {
Dave Chinnera19fb382011-03-26 09:13:42 +1100408 int retried = 0;
409
410 do {
411 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
412 -1, PAGE_KERNEL);
413 if (bp->b_addr)
414 break;
415 vm_unmap_aliases();
416 } while (retried++ <= 1);
417
418 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100420 bp->b_addr += bp->b_offset;
421 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
424 return 0;
425}
426
427/*
428 * Finding and Reading Buffers
429 */
430
431/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100432 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * a given range of an inode. The buffer is returned
Chandra Seetharamaneabbaf12011-09-08 20:18:50 +0000434 * locked. No I/O is implied by this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 */
436xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100437_xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000438 struct xfs_buftarg *btp,
439 xfs_daddr_t blkno,
440 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100441 xfs_buf_flags_t flags,
442 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000444 size_t numbytes;
Dave Chinner74f75a02010-09-24 19:59:04 +1000445 struct xfs_perag *pag;
446 struct rb_node **rbp;
447 struct rb_node *parent;
448 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Dave Chinnere70b73f2012-04-23 15:58:49 +1000450 numbytes = BBTOB(numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /* Check for IOs smaller than the sector size / not sector aligned */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000453 ASSERT(!(numbytes < (1 << btp->bt_sshift)));
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000454 ASSERT(!(BBTOB(blkno) & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dave Chinner74f75a02010-09-24 19:59:04 +1000456 /* get tree root */
457 pag = xfs_perag_get(btp->bt_mount,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000458 xfs_daddr_to_agno(btp->bt_mount, blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Dave Chinner74f75a02010-09-24 19:59:04 +1000460 /* walk tree */
461 spin_lock(&pag->pag_buf_lock);
462 rbp = &pag->pag_buf_tree.rb_node;
463 parent = NULL;
464 bp = NULL;
465 while (*rbp) {
466 parent = *rbp;
467 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000469 if (blkno < bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000470 rbp = &(*rbp)->rb_left;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000471 else if (blkno > bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000472 rbp = &(*rbp)->rb_right;
473 else {
474 /*
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000475 * found a block number match. If the range doesn't
Dave Chinner74f75a02010-09-24 19:59:04 +1000476 * match, the only way this is allowed is if the buffer
477 * in the cache is stale and the transaction that made
478 * it stale has not yet committed. i.e. we are
479 * reallocating a busy extent. Skip this buffer and
480 * continue searching to the right for an exact match.
481 */
Dave Chinner4e94b712012-04-23 15:58:51 +1000482 if (bp->b_length != numblks) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000483 ASSERT(bp->b_flags & XBF_STALE);
484 rbp = &(*rbp)->rb_right;
485 continue;
486 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100487 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 goto found;
489 }
490 }
491
492 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100493 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000494 rb_link_node(&new_bp->b_rbnode, parent, rbp);
495 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
496 /* the buffer keeps the perag reference until it is freed */
497 new_bp->b_pag = pag;
498 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100500 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000501 spin_unlock(&pag->pag_buf_lock);
502 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100504 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000507 spin_unlock(&pag->pag_buf_lock);
508 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200510 if (!xfs_buf_trylock(bp)) {
511 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100512 xfs_buf_rele(bp);
513 XFS_STATS_INC(xb_busy_locked);
514 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200516 xfs_buf_lock(bp);
517 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
Dave Chinner0e6e8472011-03-26 09:16:45 +1100520 /*
521 * if the buffer is stale, clear all the external state associated with
522 * it. We need to keep flags such as how we allocated the buffer memory
523 * intact here.
524 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100525 if (bp->b_flags & XBF_STALE) {
526 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100527 bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
David Chinner2f926582005-09-05 08:33:35 +1000528 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000529
530 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100531 XFS_STATS_INC(xb_get_locked);
532 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535/*
Dave Chinner38158322011-09-30 04:45:02 +0000536 * Assembles a buffer covering the specified range. The code is optimised for
537 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
538 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
Dave Chinner38158322011-09-30 04:45:02 +0000540struct xfs_buf *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000541xfs_buf_get(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000542 xfs_buftarg_t *target,
543 xfs_daddr_t blkno,
544 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100545 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Dave Chinner38158322011-09-30 04:45:02 +0000547 struct xfs_buf *bp;
548 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100549 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Dave Chinnere70b73f2012-04-23 15:58:49 +1000551 bp = _xfs_buf_find(target, blkno, numblks, flags, NULL);
Dave Chinner38158322011-09-30 04:45:02 +0000552 if (likely(bp))
553 goto found;
554
Dave Chinnere70b73f2012-04-23 15:58:49 +1000555 new_bp = xfs_buf_alloc(target, blkno, numblks, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100556 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return NULL;
558
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000559 error = xfs_buf_allocate_memory(new_bp, flags);
560 if (error) {
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000561 kmem_zone_free(xfs_buf_zone, new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000562 return NULL;
563 }
564
Dave Chinnere70b73f2012-04-23 15:58:49 +1000565 bp = _xfs_buf_find(target, blkno, numblks, flags, new_bp);
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000566 if (!bp) {
567 xfs_buf_free(new_bp);
568 return NULL;
569 }
570
571 if (bp != new_bp)
572 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Dave Chinner38158322011-09-30 04:45:02 +0000574 /*
575 * Now we have a workable buffer, fill in the block number so
576 * that we can do IO on it.
577 */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000578 bp->b_bn = blkno;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000579 bp->b_io_length = bp->b_length;
Dave Chinner38158322011-09-30 04:45:02 +0000580
581found:
Nathan Scottce8e9222006-01-11 15:39:08 +1100582 if (!(bp->b_flags & XBF_MAPPED)) {
583 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100585 xfs_warn(target->bt_mount,
586 "%s: failed to map pages\n", __func__);
Dave Chinnera8acad72012-04-23 15:58:54 +1000587 xfs_buf_relse(bp);
588 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590 }
591
Nathan Scottce8e9222006-01-11 15:39:08 +1100592 XFS_STATS_INC(xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000593 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100594 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100597STATIC int
598_xfs_buf_read(
599 xfs_buf_t *bp,
600 xfs_buf_flags_t flags)
601{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000602 ASSERT(!(flags & XBF_WRITE));
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100603 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
604
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000605 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200606 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100607
Dave Chinner0e95f192012-04-23 15:58:46 +1000608 xfs_buf_iorequest(bp);
609 if (flags & XBF_ASYNC)
610 return 0;
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000611 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100612}
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000615xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 xfs_buftarg_t *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000617 xfs_daddr_t blkno,
618 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100619 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Nathan Scottce8e9222006-01-11 15:39:08 +1100621 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Nathan Scottce8e9222006-01-11 15:39:08 +1100623 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Dave Chinnere70b73f2012-04-23 15:58:49 +1000625 bp = xfs_buf_get(target, blkno, numblks, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100626 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000627 trace_xfs_buf_read(bp, flags, _RET_IP_);
628
Nathan Scottce8e9222006-01-11 15:39:08 +1100629 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100630 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100631 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100632 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /*
634 * Read ahead call which is already satisfied,
635 * drop the buffer
636 */
Dave Chinnera8acad72012-04-23 15:58:54 +1000637 xfs_buf_relse(bp);
638 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 } 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
648/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100649 * If we are not low on memory then do the readahead in a deadlock
650 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 */
652void
Nathan Scottce8e9222006-01-11 15:39:08 +1100653xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 xfs_buftarg_t *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000655 xfs_daddr_t blkno,
656 size_t numblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100658 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return;
660
Dave Chinnere70b73f2012-04-23 15:58:49 +1000661 xfs_buf_read(target, blkno, numblks,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000662 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Dave Chinner5adc94c2010-09-24 21:58:31 +1000665/*
666 * Read an uncached buffer from disk. Allocates and returns a locked
667 * buffer containing the disk contents or nothing.
668 */
669struct xfs_buf *
670xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000671 struct xfs_buftarg *target,
672 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000673 size_t numblks,
Dave Chinner5adc94c2010-09-24 21:58:31 +1000674 int flags)
675{
676 xfs_buf_t *bp;
677 int error;
678
Dave Chinnere70b73f2012-04-23 15:58:49 +1000679 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000680 if (!bp)
681 return NULL;
682
683 /* set up the buffer for a read IO */
Dave Chinner5adc94c2010-09-24 21:58:31 +1000684 XFS_BUF_SET_ADDR(bp, daddr);
685 XFS_BUF_READ(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000686
Dave Chinnere70b73f2012-04-23 15:58:49 +1000687 xfsbdstrat(target->bt_mount, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000688 error = xfs_buf_iowait(bp);
Dave Chinner0e95f192012-04-23 15:58:46 +1000689 if (error) {
Dave Chinner5adc94c2010-09-24 21:58:31 +1000690 xfs_buf_relse(bp);
691 return NULL;
692 }
693 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Dave Chinner44396472011-04-21 09:34:27 +0000696/*
697 * Return a buffer allocated as an empty buffer and associated to external
698 * memory via xfs_buf_associate_memory() back to it's empty state.
699 */
700void
701xfs_buf_set_empty(
702 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000703 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000704{
705 if (bp->b_pages)
706 _xfs_buf_free_pages(bp);
707
708 bp->b_pages = NULL;
709 bp->b_page_count = 0;
710 bp->b_addr = NULL;
Dave Chinner4e94b712012-04-23 15:58:51 +1000711 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000712 bp->b_io_length = numblks;
Dave Chinner44396472011-04-21 09:34:27 +0000713 bp->b_bn = XFS_BUF_DADDR_NULL;
714 bp->b_flags &= ~XBF_MAPPED;
715}
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717static inline struct page *
718mem_to_page(
719 void *addr)
720{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800721 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return virt_to_page(addr);
723 } else {
724 return vmalloc_to_page(addr);
725 }
726}
727
728int
Nathan Scottce8e9222006-01-11 15:39:08 +1100729xfs_buf_associate_memory(
730 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 void *mem,
732 size_t len)
733{
734 int rval;
735 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100736 unsigned long pageaddr;
737 unsigned long offset;
738 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int page_count;
740
Dave Chinner0e6e8472011-03-26 09:16:45 +1100741 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100742 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100743 buflen = PAGE_ALIGN(len + offset);
744 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100747 if (bp->b_pages)
748 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Nathan Scottce8e9222006-01-11 15:39:08 +1100750 bp->b_pages = NULL;
751 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Christoph Hellwig36fae172009-07-18 18:14:58 -0400753 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (rval)
755 return rval;
756
Nathan Scottce8e9222006-01-11 15:39:08 +1100757 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100759 for (i = 0; i < bp->b_page_count; i++) {
760 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100761 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Dave Chinneraa0e8832012-04-23 15:58:52 +1000764 bp->b_io_length = BTOBB(len);
Dave Chinner4e94b712012-04-23 15:58:51 +1000765 bp->b_length = BTOBB(buflen);
Nathan Scottce8e9222006-01-11 15:39:08 +1100766 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 return 0;
769}
770
771xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000772xfs_buf_get_uncached(
773 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000774 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000775 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000777 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000778 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Dave Chinnere70b73f2012-04-23 15:58:49 +1000781 bp = xfs_buf_alloc(target, 0, numblks, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (unlikely(bp == NULL))
783 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Dave Chinnere70b73f2012-04-23 15:58:49 +1000785 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000786 error = _xfs_buf_get_pages(bp, page_count, 0);
787 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 goto fail_free_buf;
789
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000790 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000791 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000792 if (!bp->b_pages[i])
793 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000795 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000797 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
798 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100799 xfs_warn(target->bt_mount,
800 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Dave Chinner686865f2010-09-24 20:07:47 +1000804 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000808 while (--i >= 0)
809 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000810 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 fail_free_buf:
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000812 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 fail:
814 return NULL;
815}
816
817/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 * Increment reference count on buffer, to hold the buffer concurrently
819 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 * Must hold the buffer already to call this function.
821 */
822void
Nathan Scottce8e9222006-01-11 15:39:08 +1100823xfs_buf_hold(
824 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000826 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100827 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
830/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100831 * Releases a hold on the specified buffer. If the
832 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
834void
Nathan Scottce8e9222006-01-11 15:39:08 +1100835xfs_buf_rele(
836 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Dave Chinner74f75a02010-09-24 19:59:04 +1000838 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000840 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Dave Chinner74f75a02010-09-24 19:59:04 +1000842 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100843 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000844 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100845 if (atomic_dec_and_test(&bp->b_hold))
846 xfs_buf_free(bp);
847 return;
848 }
849
Dave Chinner74f75a02010-09-24 19:59:04 +1000850 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100851
Lachlan McIlroy37906892008-08-13 15:42:10 +1000852 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000853 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000854 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100855 atomic_read(&bp->b_lru_ref)) {
856 xfs_buf_lru_add(bp);
857 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100858 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100859 xfs_buf_lru_del(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000860 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner74f75a02010-09-24 19:59:04 +1000861 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
862 spin_unlock(&pag->pag_buf_lock);
863 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100864 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 }
867}
868
869
870/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100871 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100872 *
873 * If we come across a stale, pinned, locked buffer, we know that we are
874 * being asked to lock a buffer that has been reallocated. Because it is
875 * pinned, we know that the log has not been pushed to disk and hence it
876 * will still be locked. Rather than continuing to have trylock attempts
877 * fail until someone else pushes the log, push it ourselves before
878 * returning. This means that the xfsaild will not get stuck trying
879 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 */
881int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200882xfs_buf_trylock(
883 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 int locked;
886
Nathan Scottce8e9222006-01-11 15:39:08 +1100887 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000888 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100889 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100890 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
891 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000892
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200893 trace_xfs_buf_trylock(bp, _RET_IP_);
894 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100898 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000899 *
900 * If we come across a stale, pinned, locked buffer, we know that we
901 * are being asked to lock a buffer that has been reallocated. Because
902 * it is pinned, we know that the log has not been pushed to disk and
903 * hence it will still be locked. Rather than sleeping until someone
904 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100906void
907xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200908 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000910 trace_xfs_buf_lock(bp, _RET_IP_);
911
Dave Chinnered3b4d62010-05-21 12:07:08 +1000912 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000913 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100914 down(&bp->b_sema);
915 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000916
917 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920void
Nathan Scottce8e9222006-01-11 15:39:08 +1100921xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200922 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Nathan Scottce8e9222006-01-11 15:39:08 +1100924 XB_CLEAR_OWNER(bp);
925 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000926
927 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
Nathan Scottce8e9222006-01-11 15:39:08 +1100930STATIC void
931xfs_buf_wait_unpin(
932 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 DECLARE_WAITQUEUE (wait, current);
935
Nathan Scottce8e9222006-01-11 15:39:08 +1100936 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return;
938
Nathan Scottce8e9222006-01-11 15:39:08 +1100939 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 for (;;) {
941 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100942 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100944 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100946 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 set_current_state(TASK_RUNNING);
948}
949
950/*
951 * Buffer Utility Routines
952 */
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100955xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000956 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
David Howellsc4028952006-11-22 14:57:56 +0000958 xfs_buf_t *bp =
959 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400961 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100962 (*(bp->b_iodone))(bp);
963 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 xfs_buf_relse(bp);
965}
966
967void
Nathan Scottce8e9222006-01-11 15:39:08 +1100968xfs_buf_ioend(
969 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int schedule)
971{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000972 trace_xfs_buf_iodone(bp, _RET_IP_);
973
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100974 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100975 if (bp->b_error == 0)
976 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Nathan Scottce8e9222006-01-11 15:39:08 +1100978 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000980 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100981 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 } else {
David Howellsc4028952006-11-22 14:57:56 +0000983 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +1000986 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988}
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990void
Nathan Scottce8e9222006-01-11 15:39:08 +1100991xfs_buf_ioerror(
992 xfs_buf_t *bp,
993 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +1100996 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000997 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
Christoph Hellwig901796a2011-10-10 16:52:49 +00001000void
1001xfs_buf_ioerror_alert(
1002 struct xfs_buf *bp,
1003 const char *func)
1004{
1005 xfs_alert(bp->b_target->bt_mount,
Dave Chinneraa0e8832012-04-23 15:58:52 +10001006"metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
1007 (__uint64_t)XFS_BUF_ADDR(bp), func, bp->b_error, bp->b_length);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001011xfs_bwrite(
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001012 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001014 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001016 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001018 bp->b_flags |= XBF_WRITE;
1019 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
1020
Christoph Hellwig939d7232010-07-20 17:51:16 +10001021 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Christoph Hellwig8c383662010-03-12 10:59:40 +00001023 error = xfs_buf_iowait(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001024 if (error) {
1025 xfs_force_shutdown(bp->b_target->bt_mount,
1026 SHUTDOWN_META_IO_ERROR);
1027 }
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001028 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001029}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Christoph Hellwig4e234712010-01-13 22:17:56 +00001031/*
1032 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001033 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001034 * so that the proper iodone callbacks get called.
1035 */
1036STATIC int
1037xfs_bioerror(
1038 xfs_buf_t *bp)
1039{
1040#ifdef XFSERRORDEBUG
1041 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1042#endif
1043
1044 /*
1045 * No need to wait until the buffer is unpinned, we aren't flushing it.
1046 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001047 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001048
1049 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001050 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001051 */
1052 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001053 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001054 xfs_buf_stale(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001055
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001056 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001057
1058 return EIO;
1059}
1060
1061/*
1062 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001063 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001064 * This is meant for userdata errors; metadata bufs come with
1065 * iodone functions attached, so that we can track down errors.
1066 */
1067STATIC int
1068xfs_bioerror_relse(
1069 struct xfs_buf *bp)
1070{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001071 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001072 /*
1073 * No need to wait until the buffer is unpinned.
1074 * We aren't flushing it.
1075 *
1076 * chunkhold expects B_DONE to be set, whether
1077 * we actually finish the I/O or not. We don't want to
1078 * change that interface.
1079 */
1080 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001081 XFS_BUF_DONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001082 xfs_buf_stale(bp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001083 bp->b_iodone = NULL;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001084 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001085 /*
1086 * Mark b_error and B_ERROR _both_.
1087 * Lot's of chunkcache code assumes that.
1088 * There's no reason to mark error for
1089 * ASYNC buffers.
1090 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001091 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig5fde0322011-10-10 16:52:44 +00001092 complete(&bp->b_iowait);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001093 } else {
1094 xfs_buf_relse(bp);
1095 }
1096
1097 return EIO;
1098}
1099
1100
1101/*
1102 * All xfs metadata buffers except log state machine buffers
1103 * get this attached as their b_bdstrat callback function.
1104 * This is so that we can catch a buffer
1105 * after prematurely unpinning it to forcibly shutdown the filesystem.
1106 */
1107int
1108xfs_bdstrat_cb(
1109 struct xfs_buf *bp)
1110{
Dave Chinnerebad8612010-09-22 10:47:20 +10001111 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001112 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1113 /*
1114 * Metadata write that didn't get logged but
1115 * written delayed anyway. These aren't associated
1116 * with a transaction, and can be ignored.
1117 */
1118 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1119 return xfs_bioerror_relse(bp);
1120 else
1121 return xfs_bioerror(bp);
1122 }
1123
1124 xfs_buf_iorequest(bp);
1125 return 0;
1126}
1127
1128/*
1129 * Wrapper around bdstrat so that we can stop data from going to disk in case
1130 * we are shutting down the filesystem. Typically user data goes thru this
1131 * path; one of the exceptions is the superblock.
1132 */
1133void
1134xfsbdstrat(
1135 struct xfs_mount *mp,
1136 struct xfs_buf *bp)
1137{
1138 if (XFS_FORCED_SHUTDOWN(mp)) {
1139 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1140 xfs_bioerror_relse(bp);
1141 return;
1142 }
1143
1144 xfs_buf_iorequest(bp);
1145}
1146
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001147STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001148_xfs_buf_ioend(
1149 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 int schedule)
1151{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001152 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001153 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Al Viro782e3b32007-10-12 07:17:47 +01001156STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001157xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int error)
1160{
Nathan Scottce8e9222006-01-11 15:39:08 +11001161 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001163 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
James Bottomley73c77e22010-01-25 11:42:24 -06001165 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1166 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1167
Nathan Scottce8e9222006-01-11 15:39:08 +11001168 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
1172STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001173_xfs_buf_ioapply(
1174 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001176 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001178 int offset = bp->b_offset;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001179 int size = BBTOB(bp->b_io_length);
Nathan Scottce8e9222006-01-11 15:39:08 +11001180 sector_t sector = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Nathan Scottce8e9222006-01-11 15:39:08 +11001182 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 map_i = 0;
1184
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001185 if (bp->b_flags & XBF_WRITE) {
1186 if (bp->b_flags & XBF_SYNCIO)
1187 rw = WRITE_SYNC;
1188 else
1189 rw = WRITE;
1190 if (bp->b_flags & XBF_FUA)
1191 rw |= REQ_FUA;
1192 if (bp->b_flags & XBF_FLUSH)
1193 rw |= REQ_FLUSH;
1194 } else if (bp->b_flags & XBF_READ_AHEAD) {
1195 rw = READA;
Nathan Scott51bdd702006-09-28 11:01:57 +10001196 } else {
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001197 rw = READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001198 }
1199
Christoph Hellwig34951f52011-07-26 15:06:44 +00001200 /* we only use the buffer cache for meta-data */
1201 rw |= REQ_META;
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001204 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1206 if (nr_pages > total_nr_pages)
1207 nr_pages = total_nr_pages;
1208
1209 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001210 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001212 bio->bi_end_io = xfs_buf_bio_end_io;
1213 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Dave Chinner0e6e8472011-03-26 09:16:45 +11001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 for (; size && nr_pages; nr_pages--, map_i++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001217 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 if (nbytes > size)
1220 nbytes = size;
1221
Nathan Scottce8e9222006-01-11 15:39:08 +11001222 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1223 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 break;
1225
1226 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001227 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 size -= nbytes;
1229 total_nr_pages--;
1230 }
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001233 if (xfs_buf_is_vmapped(bp)) {
1234 flush_kernel_vmap_range(bp->b_addr,
1235 xfs_buf_vmap_len(bp));
1236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 submit_bio(rw, bio);
1238 if (size)
1239 goto next_chunk;
1240 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001241 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001242 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244}
1245
Dave Chinner0e95f192012-04-23 15:58:46 +10001246void
Nathan Scottce8e9222006-01-11 15:39:08 +11001247xfs_buf_iorequest(
1248 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001250 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001252 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Christoph Hellwig375ec69d2011-08-23 08:28:03 +00001254 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001255 xfs_buf_wait_unpin(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001256 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 /* Set the count to 1 initially, this will stop an I/O
1259 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001260 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001262 atomic_set(&bp->b_io_remaining, 1);
1263 _xfs_buf_ioapply(bp);
1264 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Nathan Scottce8e9222006-01-11 15:39:08 +11001266 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
1269/*
Dave Chinner0e95f192012-04-23 15:58:46 +10001270 * Waits for I/O to complete on the buffer supplied. It returns immediately if
1271 * no I/O is pending or there is already a pending error on the buffer. It
1272 * returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
1274int
Nathan Scottce8e9222006-01-11 15:39:08 +11001275xfs_buf_iowait(
1276 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001278 trace_xfs_buf_iowait(bp, _RET_IP_);
1279
Dave Chinner0e95f192012-04-23 15:58:46 +10001280 if (!bp->b_error)
1281 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001282
1283 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001284 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Nathan Scottce8e9222006-01-11 15:39:08 +11001287xfs_caddr_t
1288xfs_buf_offset(
1289 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 size_t offset)
1291{
1292 struct page *page;
1293
Nathan Scottce8e9222006-01-11 15:39:08 +11001294 if (bp->b_flags & XBF_MAPPED)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001295 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Nathan Scottce8e9222006-01-11 15:39:08 +11001297 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001298 page = bp->b_pages[offset >> PAGE_SHIFT];
1299 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
1302/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 * Move data into or out of a buffer.
1304 */
1305void
Nathan Scottce8e9222006-01-11 15:39:08 +11001306xfs_buf_iomove(
1307 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 size_t boff, /* starting buffer offset */
1309 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001310 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001311 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Dave Chinner795cac72012-04-23 15:58:53 +10001313 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 bend = boff + bsize;
1316 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001317 struct page *page;
1318 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Dave Chinner795cac72012-04-23 15:58:53 +10001320 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1321 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1322 page = bp->b_pages[page_index];
1323 csize = min_t(size_t, PAGE_SIZE - page_offset,
1324 BBTOB(bp->b_io_length) - boff);
1325
1326 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001329 case XBRW_ZERO:
Dave Chinner795cac72012-04-23 15:58:53 +10001330 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001332 case XBRW_READ:
Dave Chinner795cac72012-04-23 15:58:53 +10001333 memcpy(data, page_address(page) + page_offset, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001335 case XBRW_WRITE:
Dave Chinner795cac72012-04-23 15:58:53 +10001336 memcpy(page_address(page) + page_offset, data, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338
1339 boff += csize;
1340 data += csize;
1341 }
1342}
1343
1344/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001345 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 */
1347
1348/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001349 * Wait for any bufs with callbacks that have been submitted but have not yet
1350 * returned. These buffers will have an elevated hold count, so wait on those
1351 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 */
1353void
1354xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001355 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Dave Chinner430cbeb2010-12-02 16:30:55 +11001357 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Dave Chinner430cbeb2010-12-02 16:30:55 +11001359restart:
1360 spin_lock(&btp->bt_lru_lock);
1361 while (!list_empty(&btp->bt_lru)) {
1362 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1363 if (atomic_read(&bp->b_hold) > 1) {
1364 spin_unlock(&btp->bt_lru_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001365 delay(100);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001366 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001368 /*
Paul Bolle90802ed2011-12-05 13:00:34 +01001369 * clear the LRU reference count so the buffer doesn't get
Dave Chinner430cbeb2010-12-02 16:30:55 +11001370 * ignored in xfs_buf_rele().
1371 */
1372 atomic_set(&bp->b_lru_ref, 0);
1373 spin_unlock(&btp->bt_lru_lock);
1374 xfs_buf_rele(bp);
1375 spin_lock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001377 spin_unlock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Dave Chinnerff57ab22010-11-30 17:27:57 +11001380int
1381xfs_buftarg_shrink(
1382 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001383 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001384{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001385 struct xfs_buftarg *btp = container_of(shrink,
1386 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001387 struct xfs_buf *bp;
Ying Han1495f232011-05-24 17:12:27 -07001388 int nr_to_scan = sc->nr_to_scan;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001389 LIST_HEAD(dispose);
1390
1391 if (!nr_to_scan)
1392 return btp->bt_lru_nr;
1393
1394 spin_lock(&btp->bt_lru_lock);
1395 while (!list_empty(&btp->bt_lru)) {
1396 if (nr_to_scan-- <= 0)
1397 break;
1398
1399 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1400
1401 /*
1402 * Decrement the b_lru_ref count unless the value is already
1403 * zero. If the value is already zero, we need to reclaim the
1404 * buffer, otherwise it gets another trip through the LRU.
1405 */
1406 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1407 list_move_tail(&bp->b_lru, &btp->bt_lru);
1408 continue;
1409 }
1410
1411 /*
1412 * remove the buffer from the LRU now to avoid needing another
1413 * lock round trip inside xfs_buf_rele().
1414 */
1415 list_move(&bp->b_lru, &dispose);
1416 btp->bt_lru_nr--;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001417 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001418 spin_unlock(&btp->bt_lru_lock);
1419
1420 while (!list_empty(&dispose)) {
1421 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1422 list_del_init(&bp->b_lru);
1423 xfs_buf_rele(bp);
1424 }
1425
1426 return btp->bt_lru_nr;
David Chinnera6867a62006-01-11 15:37:58 +11001427}
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429void
1430xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001431 struct xfs_mount *mp,
1432 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001434 unregister_shrinker(&btp->bt_shrinker);
1435
Christoph Hellwigb7963132009-03-03 14:48:37 -05001436 if (mp->m_flags & XFS_MOUNT_BARRIER)
1437 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001438
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001439 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442STATIC int
1443xfs_setsize_buftarg_flags(
1444 xfs_buftarg_t *btp,
1445 unsigned int blocksize,
1446 unsigned int sectorsize,
1447 int verbose)
1448{
Nathan Scottce8e9222006-01-11 15:39:08 +11001449 btp->bt_bsize = blocksize;
1450 btp->bt_sshift = ffs(sectorsize) - 1;
1451 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Nathan Scottce8e9222006-01-11 15:39:08 +11001453 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001454 char name[BDEVNAME_SIZE];
1455
1456 bdevname(btp->bt_bdev, name);
1457
Dave Chinner4f107002011-03-07 10:00:35 +11001458 xfs_warn(btp->bt_mount,
1459 "Cannot set_blocksize to %u on device %s\n",
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001460 sectorsize, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return EINVAL;
1462 }
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return 0;
1465}
1466
1467/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001468 * When allocating the initial buffer target we have not yet
1469 * read in the superblock, so don't know what sized sectors
1470 * are being used is at this early stage. Play safe.
1471 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472STATIC int
1473xfs_setsize_buftarg_early(
1474 xfs_buftarg_t *btp,
1475 struct block_device *bdev)
1476{
1477 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001478 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
1481int
1482xfs_setsize_buftarg(
1483 xfs_buftarg_t *btp,
1484 unsigned int blocksize,
1485 unsigned int sectorsize)
1486{
1487 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1488}
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490xfs_buftarg_t *
1491xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001492 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001494 int external,
1495 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 xfs_buftarg_t *btp;
1498
1499 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1500
Dave Chinnerebad8612010-09-22 10:47:20 +10001501 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001502 btp->bt_dev = bdev->bd_dev;
1503 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001504 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1505 if (!btp->bt_bdi)
1506 goto error;
1507
Dave Chinner430cbeb2010-12-02 16:30:55 +11001508 INIT_LIST_HEAD(&btp->bt_lru);
1509 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (xfs_setsize_buftarg_early(btp, bdev))
1511 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001512 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1513 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1514 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return btp;
1516
1517error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001518 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return NULL;
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001523 * Add a buffer to the delayed write list.
1524 *
1525 * This queues a buffer for writeout if it hasn't already been. Note that
1526 * neither this routine nor the buffer list submission functions perform
1527 * any internal synchronization. It is expected that the lists are thread-local
1528 * to the callers.
1529 *
1530 * Returns true if we queued up the buffer, or false if it already had
1531 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001533bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001534xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001535 struct xfs_buf *bp,
1536 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001538 ASSERT(xfs_buf_islocked(bp));
1539 ASSERT(!(bp->b_flags & XBF_READ));
1540
1541 /*
1542 * If the buffer is already marked delwri it already is queued up
1543 * by someone else for imediate writeout. Just ignore it in that
1544 * case.
1545 */
1546 if (bp->b_flags & _XBF_DELWRI_Q) {
1547 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1548 return false;
1549 }
David Chinnera6867a62006-01-11 15:37:58 +11001550
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001551 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1552
Dave Chinnerd808f612010-02-02 10:13:42 +11001553 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001554 * If a buffer gets written out synchronously or marked stale while it
1555 * is on a delwri list we lazily remove it. To do this, the other party
1556 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1557 * It remains referenced and on the list. In a rare corner case it
1558 * might get readded to a delwri list after the synchronous writeout, in
1559 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001560 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001561 bp->b_flags |= _XBF_DELWRI_Q;
1562 if (list_empty(&bp->b_list)) {
1563 atomic_inc(&bp->b_hold);
1564 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001565 }
David Chinner585e6d82007-02-10 18:32:29 +11001566
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001567 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001568}
1569
Dave Chinner089716a2010-01-26 15:13:25 +11001570/*
1571 * Compare function is more complex than it needs to be because
1572 * the return value is only 32 bits and we are doing comparisons
1573 * on 64 bit values
1574 */
1575static int
1576xfs_buf_cmp(
1577 void *priv,
1578 struct list_head *a,
1579 struct list_head *b)
1580{
1581 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1582 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1583 xfs_daddr_t diff;
1584
1585 diff = ap->b_bn - bp->b_bn;
1586 if (diff < 0)
1587 return -1;
1588 if (diff > 0)
1589 return 1;
1590 return 0;
1591}
1592
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001593static int
1594__xfs_buf_delwri_submit(
1595 struct list_head *buffer_list,
1596 struct list_head *io_list,
1597 bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001599 struct blk_plug plug;
1600 struct xfs_buf *bp, *n;
1601 int pinned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001603 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
1604 if (!wait) {
1605 if (xfs_buf_ispinned(bp)) {
1606 pinned++;
1607 continue;
1608 }
1609 if (!xfs_buf_trylock(bp))
1610 continue;
1611 } else {
1612 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001615 /*
1616 * Someone else might have written the buffer synchronously or
1617 * marked it stale in the meantime. In that case only the
1618 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1619 * reference and remove it from the list here.
1620 */
1621 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1622 list_del_init(&bp->b_list);
1623 xfs_buf_relse(bp);
1624 continue;
1625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001627 list_move_tail(&bp->b_list, io_list);
1628 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001631 list_sort(NULL, io_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001632
1633 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001634 list_for_each_entry_safe(bp, n, io_list, b_list) {
1635 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
1636 bp->b_flags |= XBF_WRITE;
1637
1638 if (!wait) {
1639 bp->b_flags |= XBF_ASYNC;
1640 list_del_init(&bp->b_list);
Dave Chinner089716a2010-01-26 15:13:25 +11001641 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001642 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001644 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001646 return pinned;
1647}
Nathan Scottf07c2252006-09-28 10:52:15 +10001648
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001649/*
1650 * Write out a buffer list asynchronously.
1651 *
1652 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1653 * out and not wait for I/O completion on any of the buffers. This interface
1654 * is only safely useable for callers that can track I/O completion by higher
1655 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1656 * function.
1657 */
1658int
1659xfs_buf_delwri_submit_nowait(
1660 struct list_head *buffer_list)
1661{
1662 LIST_HEAD (io_list);
1663 return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
1664}
1665
1666/*
1667 * Write out a buffer list synchronously.
1668 *
1669 * This will take the @buffer_list, write all buffers out and wait for I/O
1670 * completion on all of the buffers. @buffer_list is consumed by the function,
1671 * so callers must have some other way of tracking buffers if they require such
1672 * functionality.
1673 */
1674int
1675xfs_buf_delwri_submit(
1676 struct list_head *buffer_list)
1677{
1678 LIST_HEAD (io_list);
1679 int error = 0, error2;
1680 struct xfs_buf *bp;
1681
1682 __xfs_buf_delwri_submit(buffer_list, &io_list, true);
1683
1684 /* Wait for IO to complete. */
1685 while (!list_empty(&io_list)) {
1686 bp = list_first_entry(&io_list, struct xfs_buf, b_list);
1687
1688 list_del_init(&bp->b_list);
1689 error2 = xfs_buf_iowait(bp);
1690 xfs_buf_relse(bp);
1691 if (!error)
1692 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001695 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001698int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001699xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Nathan Scott87582802006-03-14 13:18:19 +11001701 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1702 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001703 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001704 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001705
Dave Chinner51749e42010-09-08 09:00:22 +00001706 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001707 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001708 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001709 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001713 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001714 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001715 out:
Nathan Scott87582802006-03-14 13:18:19 +11001716 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719void
Nathan Scottce8e9222006-01-11 15:39:08 +11001720xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001722 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001723 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}