blob: ab3c4491777b0a61f02b5de79ab744b827e294dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scottf07c2252006-09-28 10:52:15 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Vlad Apostolov93c189c2006-11-11 18:03:49 +110018#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/stddef.h>
20#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
23#include <linux/init.h>
24#include <linux/vmalloc.h>
25#include <linux/bio.h>
26#include <linux/sysctl.h>
27#include <linux/proc_fs.h>
28#include <linux/workqueue.h>
29#include <linux/percpu.h>
30#include <linux/blkdev.h>
31#include <linux/hash.h>
Christoph Hellwig4df08c52005-09-05 08:34:18 +100032#include <linux/kthread.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080033#include <linux/migrate.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070034#include <linux/backing-dev.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Christoph Hellwigb7963132009-03-03 14:48:37 -050037#include "xfs_sb.h"
38#include "xfs_inum.h"
Dave Chinnered3b4d62010-05-21 12:07:08 +100039#include "xfs_log.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050040#include "xfs_ag.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050041#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050043
David Chinner7989cb82007-02-10 18:34:56 +110044static kmem_zone_t *xfs_buf_zone;
Christoph Hellwig23ea4032005-06-21 15:14:01 +100045
David Chinner7989cb82007-02-10 18:34:56 +110046static struct workqueue_struct *xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Nathan Scottce8e9222006-01-11 15:39:08 +110048#ifdef XFS_BUF_LOCK_TRACKING
49# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
50# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
51# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#else
Nathan Scottce8e9222006-01-11 15:39:08 +110053# define XB_SET_OWNER(bp) do { } while (0)
54# define XB_CLEAR_OWNER(bp) do { } while (0)
55# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57
Nathan Scottce8e9222006-01-11 15:39:08 +110058#define xb_to_gfp(flags) \
59 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
60 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Nathan Scottce8e9222006-01-11 15:39:08 +110062#define xb_to_km(flags) \
63 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
James Bottomley73c77e22010-01-25 11:42:24 -060066static inline int
67xfs_buf_is_vmapped(
68 struct xfs_buf *bp)
69{
70 /*
71 * Return true if the buffer is vmapped.
72 *
73 * The XBF_MAPPED flag is set if the buffer should be mapped, but the
74 * code is clever enough to know it doesn't have to map a single page,
75 * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
76 */
77 return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
78}
79
80static inline int
81xfs_buf_vmap_len(
82 struct xfs_buf *bp)
83{
84 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
85}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
Dave Chinner430cbeb2010-12-02 16:30:55 +110088 * xfs_buf_lru_add - add a buffer to the LRU.
89 *
90 * The LRU takes a new reference to the buffer so that it will only be freed
91 * once the shrinker takes the buffer off the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
Dave Chinner430cbeb2010-12-02 16:30:55 +110093STATIC void
94xfs_buf_lru_add(
95 struct xfs_buf *bp)
96{
97 struct xfs_buftarg *btp = bp->b_target;
98
99 spin_lock(&btp->bt_lru_lock);
100 if (list_empty(&bp->b_lru)) {
101 atomic_inc(&bp->b_hold);
102 list_add_tail(&bp->b_lru, &btp->bt_lru);
103 btp->bt_lru_nr++;
104 }
105 spin_unlock(&btp->bt_lru_lock);
106}
107
108/*
109 * xfs_buf_lru_del - remove a buffer from the LRU
110 *
111 * The unlocked check is safe here because it only occurs when there are not
112 * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there
113 * to optimise the shrinker removing the buffer from the LRU and calling
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300114 * xfs_buf_free(). i.e. it removes an unnecessary round trip on the
Dave Chinner430cbeb2010-12-02 16:30:55 +1100115 * bt_lru_lock.
116 */
117STATIC void
118xfs_buf_lru_del(
119 struct xfs_buf *bp)
120{
121 struct xfs_buftarg *btp = bp->b_target;
122
123 if (list_empty(&bp->b_lru))
124 return;
125
126 spin_lock(&btp->bt_lru_lock);
127 if (!list_empty(&bp->b_lru)) {
128 list_del_init(&bp->b_lru);
129 btp->bt_lru_nr--;
130 }
131 spin_unlock(&btp->bt_lru_lock);
132}
133
134/*
135 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
136 * b_lru_ref count so that the buffer is freed immediately when the buffer
137 * reference count falls to zero. If the buffer is already on the LRU, we need
138 * to remove the reference that LRU holds on the buffer.
139 *
140 * This prevents build-up of stale buffers on the LRU.
141 */
142void
143xfs_buf_stale(
144 struct xfs_buf *bp)
145{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000146 ASSERT(xfs_buf_islocked(bp));
147
Dave Chinner430cbeb2010-12-02 16:30:55 +1100148 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000149
150 /*
151 * Clear the delwri status so that a delwri queue walker will not
152 * flush this buffer to disk now that it is stale. The delwri queue has
153 * a reference to the buffer, so this is safe to do.
154 */
155 bp->b_flags &= ~_XBF_DELWRI_Q;
156
Dave Chinner430cbeb2010-12-02 16:30:55 +1100157 atomic_set(&(bp)->b_lru_ref, 0);
158 if (!list_empty(&bp->b_lru)) {
159 struct xfs_buftarg *btp = bp->b_target;
160
161 spin_lock(&btp->bt_lru_lock);
162 if (!list_empty(&bp->b_lru)) {
163 list_del_init(&bp->b_lru);
164 btp->bt_lru_nr--;
165 atomic_dec(&bp->b_hold);
166 }
167 spin_unlock(&btp->bt_lru_lock);
168 }
169 ASSERT(atomic_read(&bp->b_hold) >= 1);
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000172struct xfs_buf *
173xfs_buf_alloc(
174 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000175 xfs_daddr_t blkno,
176 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100177 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000179 struct xfs_buf *bp;
180
Dave Chinnerbf813cd2012-04-23 15:58:48 +1000181 bp = kmem_zone_zalloc(xfs_buf_zone, xb_to_km(flags));
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000182 if (unlikely(!bp))
183 return NULL;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100186 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100188 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Nathan Scottce8e9222006-01-11 15:39:08 +1100190 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100191 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000192 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100193 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100194 INIT_LIST_HEAD(&bp->b_list);
Dave Chinner74f75a02010-09-24 19:59:04 +1000195 RB_CLEAR_NODE(&bp->b_rbnode);
Thomas Gleixnera731cd12010-09-07 14:33:15 +0000196 sema_init(&bp->b_sema, 0); /* held, no waiters */
Nathan Scottce8e9222006-01-11 15:39:08 +1100197 XB_SET_OWNER(bp);
198 bp->b_target = target;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /*
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;
Nathan Scott204ab25f2006-01-11 20:50:22 +1100309 xfs_off_t 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 Chinner4e94b712012-04-23 15:58:51 +1000317 if (bp->b_length < BTOBB(PAGE_SIZE)) {
318 bp->b_addr = kmem_alloc(BBTOB(bp->b_length), xb_to_km(flags));
Dave Chinner0e6e8472011-03-26 09:16:45 +1100319 if (!bp->b_addr) {
320 /* low memory - use alloc_page loop instead */
321 goto use_alloc_page;
322 }
323
Dave Chinner4e94b712012-04-23 15:58:51 +1000324 if (((unsigned long)(bp->b_addr + BBTOB(bp->b_length) - 1) &
Dave Chinner0e6e8472011-03-26 09:16:45 +1100325 PAGE_MASK) !=
326 ((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 Chinner4e94b712012-04-23 15:58:51 +1000341 end = BBTOB(bp->b_bn + bp->b_length);
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000342 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(BBTOB(bp->b_bn));
Nathan Scottce8e9222006-01-11 15:39:08 +1100343 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (unlikely(error))
345 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Nathan Scottce8e9222006-01-11 15:39:08 +1100347 offset = bp->b_offset;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000348 size = BBTOB(bp->b_length);
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__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 goto no_buffer;
588 }
589 }
590
Nathan Scottce8e9222006-01-11 15:39:08 +1100591 XFS_STATS_INC(xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000592 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100593 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Dave Chinner38158322011-09-30 04:45:02 +0000595no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100596 if (flags & (XBF_LOCK | XBF_TRYLOCK))
597 xfs_buf_unlock(bp);
598 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return NULL;
600}
601
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100602STATIC int
603_xfs_buf_read(
604 xfs_buf_t *bp,
605 xfs_buf_flags_t flags)
606{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000607 ASSERT(!(flags & XBF_WRITE));
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100608 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
609
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000610 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200611 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100612
Dave Chinner0e95f192012-04-23 15:58:46 +1000613 xfs_buf_iorequest(bp);
614 if (flags & XBF_ASYNC)
615 return 0;
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000616 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000620xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 xfs_buftarg_t *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000622 xfs_daddr_t blkno,
623 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100624 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Nathan Scottce8e9222006-01-11 15:39:08 +1100626 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Nathan Scottce8e9222006-01-11 15:39:08 +1100628 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Dave Chinnere70b73f2012-04-23 15:58:49 +1000630 bp = xfs_buf_get(target, blkno, numblks, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100631 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000632 trace_xfs_buf_read(bp, flags, _RET_IP_);
633
Nathan Scottce8e9222006-01-11 15:39:08 +1100634 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100635 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100636 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100637 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /*
639 * Read ahead call which is already satisfied,
640 * drop the buffer
641 */
642 goto no_buffer;
643 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100645 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647 }
648
Nathan Scottce8e9222006-01-11 15:39:08 +1100649 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100652 if (flags & (XBF_LOCK | XBF_TRYLOCK))
653 xfs_buf_unlock(bp);
654 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return NULL;
656}
657
658/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100659 * If we are not low on memory then do the readahead in a deadlock
660 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 */
662void
Nathan Scottce8e9222006-01-11 15:39:08 +1100663xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 xfs_buftarg_t *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000665 xfs_daddr_t blkno,
666 size_t numblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100668 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return;
670
Dave Chinnere70b73f2012-04-23 15:58:49 +1000671 xfs_buf_read(target, blkno, numblks,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000672 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Dave Chinner5adc94c2010-09-24 21:58:31 +1000675/*
676 * Read an uncached buffer from disk. Allocates and returns a locked
677 * buffer containing the disk contents or nothing.
678 */
679struct xfs_buf *
680xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000681 struct xfs_buftarg *target,
682 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000683 size_t numblks,
Dave Chinner5adc94c2010-09-24 21:58:31 +1000684 int flags)
685{
686 xfs_buf_t *bp;
687 int error;
688
Dave Chinnere70b73f2012-04-23 15:58:49 +1000689 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000690 if (!bp)
691 return NULL;
692
693 /* set up the buffer for a read IO */
Dave Chinner5adc94c2010-09-24 21:58:31 +1000694 XFS_BUF_SET_ADDR(bp, daddr);
695 XFS_BUF_READ(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000696
Dave Chinnere70b73f2012-04-23 15:58:49 +1000697 xfsbdstrat(target->bt_mount, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000698 error = xfs_buf_iowait(bp);
Dave Chinner0e95f192012-04-23 15:58:46 +1000699 if (error) {
Dave Chinner5adc94c2010-09-24 21:58:31 +1000700 xfs_buf_relse(bp);
701 return NULL;
702 }
703 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Dave Chinner44396472011-04-21 09:34:27 +0000706/*
707 * Return a buffer allocated as an empty buffer and associated to external
708 * memory via xfs_buf_associate_memory() back to it's empty state.
709 */
710void
711xfs_buf_set_empty(
712 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000713 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000714{
715 if (bp->b_pages)
716 _xfs_buf_free_pages(bp);
717
718 bp->b_pages = NULL;
719 bp->b_page_count = 0;
720 bp->b_addr = NULL;
Dave Chinner4e94b712012-04-23 15:58:51 +1000721 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000722 bp->b_io_length = numblks;
Dave Chinner44396472011-04-21 09:34:27 +0000723 bp->b_bn = XFS_BUF_DADDR_NULL;
724 bp->b_flags &= ~XBF_MAPPED;
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727static inline struct page *
728mem_to_page(
729 void *addr)
730{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800731 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return virt_to_page(addr);
733 } else {
734 return vmalloc_to_page(addr);
735 }
736}
737
738int
Nathan Scottce8e9222006-01-11 15:39:08 +1100739xfs_buf_associate_memory(
740 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 void *mem,
742 size_t len)
743{
744 int rval;
745 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100746 unsigned long pageaddr;
747 unsigned long offset;
748 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 int page_count;
750
Dave Chinner0e6e8472011-03-26 09:16:45 +1100751 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100752 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100753 buflen = PAGE_ALIGN(len + offset);
754 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100757 if (bp->b_pages)
758 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Nathan Scottce8e9222006-01-11 15:39:08 +1100760 bp->b_pages = NULL;
761 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Christoph Hellwig36fae172009-07-18 18:14:58 -0400763 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (rval)
765 return rval;
766
Nathan Scottce8e9222006-01-11 15:39:08 +1100767 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100769 for (i = 0; i < bp->b_page_count; i++) {
770 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100771 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Dave Chinneraa0e8832012-04-23 15:58:52 +1000774 bp->b_io_length = BTOBB(len);
Dave Chinner4e94b712012-04-23 15:58:51 +1000775 bp->b_length = BTOBB(buflen);
Nathan Scottce8e9222006-01-11 15:39:08 +1100776 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 return 0;
779}
780
781xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000782xfs_buf_get_uncached(
783 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000784 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000785 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000787 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000788 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Dave Chinnere70b73f2012-04-23 15:58:49 +1000791 bp = xfs_buf_alloc(target, 0, numblks, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (unlikely(bp == NULL))
793 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Dave Chinnere70b73f2012-04-23 15:58:49 +1000795 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000796 error = _xfs_buf_get_pages(bp, page_count, 0);
797 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto fail_free_buf;
799
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000800 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000801 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000802 if (!bp->b_pages[i])
803 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000805 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000807 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
808 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100809 xfs_warn(target->bt_mount,
810 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Dave Chinner686865f2010-09-24 20:07:47 +1000814 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000818 while (--i >= 0)
819 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000820 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 fail_free_buf:
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000822 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 fail:
824 return NULL;
825}
826
827/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 * Increment reference count on buffer, to hold the buffer concurrently
829 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 * Must hold the buffer already to call this function.
831 */
832void
Nathan Scottce8e9222006-01-11 15:39:08 +1100833xfs_buf_hold(
834 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000836 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100837 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
840/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100841 * Releases a hold on the specified buffer. If the
842 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 */
844void
Nathan Scottce8e9222006-01-11 15:39:08 +1100845xfs_buf_rele(
846 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Dave Chinner74f75a02010-09-24 19:59:04 +1000848 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000850 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Dave Chinner74f75a02010-09-24 19:59:04 +1000852 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100853 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000854 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100855 if (atomic_dec_and_test(&bp->b_hold))
856 xfs_buf_free(bp);
857 return;
858 }
859
Dave Chinner74f75a02010-09-24 19:59:04 +1000860 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100861
Lachlan McIlroy37906892008-08-13 15:42:10 +1000862 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000863 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000864 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100865 atomic_read(&bp->b_lru_ref)) {
866 xfs_buf_lru_add(bp);
867 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100868 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100869 xfs_buf_lru_del(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000870 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner74f75a02010-09-24 19:59:04 +1000871 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
872 spin_unlock(&pag->pag_buf_lock);
873 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100874 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 }
877}
878
879
880/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100881 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100882 *
883 * If we come across a stale, pinned, locked buffer, we know that we are
884 * being asked to lock a buffer that has been reallocated. Because it is
885 * pinned, we know that the log has not been pushed to disk and hence it
886 * will still be locked. Rather than continuing to have trylock attempts
887 * fail until someone else pushes the log, push it ourselves before
888 * returning. This means that the xfsaild will not get stuck trying
889 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
891int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200892xfs_buf_trylock(
893 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 int locked;
896
Nathan Scottce8e9222006-01-11 15:39:08 +1100897 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000898 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100899 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100900 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
901 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000902
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200903 trace_xfs_buf_trylock(bp, _RET_IP_);
904 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100908 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000909 *
910 * If we come across a stale, pinned, locked buffer, we know that we
911 * are being asked to lock a buffer that has been reallocated. Because
912 * it is pinned, we know that the log has not been pushed to disk and
913 * hence it will still be locked. Rather than sleeping until someone
914 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100916void
917xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200918 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000920 trace_xfs_buf_lock(bp, _RET_IP_);
921
Dave Chinnered3b4d62010-05-21 12:07:08 +1000922 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000923 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100924 down(&bp->b_sema);
925 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000926
927 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930void
Nathan Scottce8e9222006-01-11 15:39:08 +1100931xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200932 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Nathan Scottce8e9222006-01-11 15:39:08 +1100934 XB_CLEAR_OWNER(bp);
935 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000936
937 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Nathan Scottce8e9222006-01-11 15:39:08 +1100940STATIC void
941xfs_buf_wait_unpin(
942 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 DECLARE_WAITQUEUE (wait, current);
945
Nathan Scottce8e9222006-01-11 15:39:08 +1100946 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return;
948
Nathan Scottce8e9222006-01-11 15:39:08 +1100949 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 for (;;) {
951 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100952 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100954 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100956 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 set_current_state(TASK_RUNNING);
958}
959
960/*
961 * Buffer Utility Routines
962 */
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100965xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000966 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
David Howellsc4028952006-11-22 14:57:56 +0000968 xfs_buf_t *bp =
969 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400971 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100972 (*(bp->b_iodone))(bp);
973 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 xfs_buf_relse(bp);
975}
976
977void
Nathan Scottce8e9222006-01-11 15:39:08 +1100978xfs_buf_ioend(
979 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int schedule)
981{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000982 trace_xfs_buf_iodone(bp, _RET_IP_);
983
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100984 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100985 if (bp->b_error == 0)
986 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Nathan Scottce8e9222006-01-11 15:39:08 +1100988 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000990 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100991 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 } else {
David Howellsc4028952006-11-22 14:57:56 +0000993 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +1000996 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998}
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000void
Nathan Scottce8e9222006-01-11 15:39:08 +11001001xfs_buf_ioerror(
1002 xfs_buf_t *bp,
1003 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001006 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001007 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Christoph Hellwig901796a2011-10-10 16:52:49 +00001010void
1011xfs_buf_ioerror_alert(
1012 struct xfs_buf *bp,
1013 const char *func)
1014{
1015 xfs_alert(bp->b_target->bt_mount,
Dave Chinneraa0e8832012-04-23 15:58:52 +10001016"metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
1017 (__uint64_t)XFS_BUF_ADDR(bp), func, bp->b_error, bp->b_length);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001018}
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001021xfs_bwrite(
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001022 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001024 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001026 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001028 bp->b_flags |= XBF_WRITE;
1029 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
1030
Christoph Hellwig939d7232010-07-20 17:51:16 +10001031 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Christoph Hellwig8c383662010-03-12 10:59:40 +00001033 error = xfs_buf_iowait(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001034 if (error) {
1035 xfs_force_shutdown(bp->b_target->bt_mount,
1036 SHUTDOWN_META_IO_ERROR);
1037 }
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001038 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001039}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Christoph Hellwig4e234712010-01-13 22:17:56 +00001041/*
1042 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001043 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001044 * so that the proper iodone callbacks get called.
1045 */
1046STATIC int
1047xfs_bioerror(
1048 xfs_buf_t *bp)
1049{
1050#ifdef XFSERRORDEBUG
1051 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1052#endif
1053
1054 /*
1055 * No need to wait until the buffer is unpinned, we aren't flushing it.
1056 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001057 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001058
1059 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001060 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001061 */
1062 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001063 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001064 xfs_buf_stale(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001065
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001066 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001067
1068 return EIO;
1069}
1070
1071/*
1072 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001073 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001074 * This is meant for userdata errors; metadata bufs come with
1075 * iodone functions attached, so that we can track down errors.
1076 */
1077STATIC int
1078xfs_bioerror_relse(
1079 struct xfs_buf *bp)
1080{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001081 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001082 /*
1083 * No need to wait until the buffer is unpinned.
1084 * We aren't flushing it.
1085 *
1086 * chunkhold expects B_DONE to be set, whether
1087 * we actually finish the I/O or not. We don't want to
1088 * change that interface.
1089 */
1090 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001091 XFS_BUF_DONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001092 xfs_buf_stale(bp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001093 bp->b_iodone = NULL;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001094 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001095 /*
1096 * Mark b_error and B_ERROR _both_.
1097 * Lot's of chunkcache code assumes that.
1098 * There's no reason to mark error for
1099 * ASYNC buffers.
1100 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001101 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig5fde0322011-10-10 16:52:44 +00001102 complete(&bp->b_iowait);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001103 } else {
1104 xfs_buf_relse(bp);
1105 }
1106
1107 return EIO;
1108}
1109
1110
1111/*
1112 * All xfs metadata buffers except log state machine buffers
1113 * get this attached as their b_bdstrat callback function.
1114 * This is so that we can catch a buffer
1115 * after prematurely unpinning it to forcibly shutdown the filesystem.
1116 */
1117int
1118xfs_bdstrat_cb(
1119 struct xfs_buf *bp)
1120{
Dave Chinnerebad8612010-09-22 10:47:20 +10001121 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001122 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1123 /*
1124 * Metadata write that didn't get logged but
1125 * written delayed anyway. These aren't associated
1126 * with a transaction, and can be ignored.
1127 */
1128 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1129 return xfs_bioerror_relse(bp);
1130 else
1131 return xfs_bioerror(bp);
1132 }
1133
1134 xfs_buf_iorequest(bp);
1135 return 0;
1136}
1137
1138/*
1139 * Wrapper around bdstrat so that we can stop data from going to disk in case
1140 * we are shutting down the filesystem. Typically user data goes thru this
1141 * path; one of the exceptions is the superblock.
1142 */
1143void
1144xfsbdstrat(
1145 struct xfs_mount *mp,
1146 struct xfs_buf *bp)
1147{
1148 if (XFS_FORCED_SHUTDOWN(mp)) {
1149 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1150 xfs_bioerror_relse(bp);
1151 return;
1152 }
1153
1154 xfs_buf_iorequest(bp);
1155}
1156
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001157STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001158_xfs_buf_ioend(
1159 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 int schedule)
1161{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001162 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001163 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Al Viro782e3b32007-10-12 07:17:47 +01001166STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001167xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int error)
1170{
Nathan Scottce8e9222006-01-11 15:39:08 +11001171 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001173 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
James Bottomley73c77e22010-01-25 11:42:24 -06001175 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1176 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1177
Nathan Scottce8e9222006-01-11 15:39:08 +11001178 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
1182STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001183_xfs_buf_ioapply(
1184 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001186 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001188 int offset = bp->b_offset;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001189 int size = BBTOB(bp->b_io_length);
Nathan Scottce8e9222006-01-11 15:39:08 +11001190 sector_t sector = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Nathan Scottce8e9222006-01-11 15:39:08 +11001192 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 map_i = 0;
1194
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001195 if (bp->b_flags & XBF_WRITE) {
1196 if (bp->b_flags & XBF_SYNCIO)
1197 rw = WRITE_SYNC;
1198 else
1199 rw = WRITE;
1200 if (bp->b_flags & XBF_FUA)
1201 rw |= REQ_FUA;
1202 if (bp->b_flags & XBF_FLUSH)
1203 rw |= REQ_FLUSH;
1204 } else if (bp->b_flags & XBF_READ_AHEAD) {
1205 rw = READA;
Nathan Scott51bdd702006-09-28 11:01:57 +10001206 } else {
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001207 rw = READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001208 }
1209
Christoph Hellwig34951f52011-07-26 15:06:44 +00001210 /* we only use the buffer cache for meta-data */
1211 rw |= REQ_META;
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001214 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1216 if (nr_pages > total_nr_pages)
1217 nr_pages = total_nr_pages;
1218
1219 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001220 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001222 bio->bi_end_io = xfs_buf_bio_end_io;
1223 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Dave Chinner0e6e8472011-03-26 09:16:45 +11001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 for (; size && nr_pages; nr_pages--, map_i++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001227 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if (nbytes > size)
1230 nbytes = size;
1231
Nathan Scottce8e9222006-01-11 15:39:08 +11001232 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1233 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 break;
1235
1236 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001237 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 size -= nbytes;
1239 total_nr_pages--;
1240 }
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001243 if (xfs_buf_is_vmapped(bp)) {
1244 flush_kernel_vmap_range(bp->b_addr,
1245 xfs_buf_vmap_len(bp));
1246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 submit_bio(rw, bio);
1248 if (size)
1249 goto next_chunk;
1250 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001251 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001252 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254}
1255
Dave Chinner0e95f192012-04-23 15:58:46 +10001256void
Nathan Scottce8e9222006-01-11 15:39:08 +11001257xfs_buf_iorequest(
1258 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001260 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001262 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Christoph Hellwig375ec692011-08-23 08:28:03 +00001264 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001265 xfs_buf_wait_unpin(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001266 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /* Set the count to 1 initially, this will stop an I/O
1269 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001270 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001272 atomic_set(&bp->b_io_remaining, 1);
1273 _xfs_buf_ioapply(bp);
1274 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Nathan Scottce8e9222006-01-11 15:39:08 +11001276 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
1279/*
Dave Chinner0e95f192012-04-23 15:58:46 +10001280 * Waits for I/O to complete on the buffer supplied. It returns immediately if
1281 * no I/O is pending or there is already a pending error on the buffer. It
1282 * returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 */
1284int
Nathan Scottce8e9222006-01-11 15:39:08 +11001285xfs_buf_iowait(
1286 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001288 trace_xfs_buf_iowait(bp, _RET_IP_);
1289
Dave Chinner0e95f192012-04-23 15:58:46 +10001290 if (!bp->b_error)
1291 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001292
1293 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001294 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
Nathan Scottce8e9222006-01-11 15:39:08 +11001297xfs_caddr_t
1298xfs_buf_offset(
1299 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 size_t offset)
1301{
1302 struct page *page;
1303
Nathan Scottce8e9222006-01-11 15:39:08 +11001304 if (bp->b_flags & XBF_MAPPED)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001305 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Nathan Scottce8e9222006-01-11 15:39:08 +11001307 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001308 page = bp->b_pages[offset >> PAGE_SHIFT];
1309 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
1312/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 * Move data into or out of a buffer.
1314 */
1315void
Nathan Scottce8e9222006-01-11 15:39:08 +11001316xfs_buf_iomove(
1317 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 size_t boff, /* starting buffer offset */
1319 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001320 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001321 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 size_t bend, cpoff, csize;
1324 struct page *page;
1325
1326 bend = boff + bsize;
1327 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001328 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1329 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 csize = min_t(size_t,
Dave Chinneraa0e8832012-04-23 15:58:52 +10001331 PAGE_SIZE - cpoff, BBTOB(bp->b_io_length) - boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Dave Chinner0e6e8472011-03-26 09:16:45 +11001333 ASSERT(((csize + cpoff) <= PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001336 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 memset(page_address(page) + cpoff, 0, csize);
1338 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001339 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 memcpy(data, page_address(page) + cpoff, csize);
1341 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001342 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 memcpy(page_address(page) + cpoff, data, csize);
1344 }
1345
1346 boff += csize;
1347 data += csize;
1348 }
1349}
1350
1351/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001352 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
1354
1355/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001356 * Wait for any bufs with callbacks that have been submitted but have not yet
1357 * returned. These buffers will have an elevated hold count, so wait on those
1358 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 */
1360void
1361xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001362 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Dave Chinner430cbeb2010-12-02 16:30:55 +11001364 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Dave Chinner430cbeb2010-12-02 16:30:55 +11001366restart:
1367 spin_lock(&btp->bt_lru_lock);
1368 while (!list_empty(&btp->bt_lru)) {
1369 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1370 if (atomic_read(&bp->b_hold) > 1) {
1371 spin_unlock(&btp->bt_lru_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001372 delay(100);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001373 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001375 /*
Paul Bolle90802ed2011-12-05 13:00:34 +01001376 * clear the LRU reference count so the buffer doesn't get
Dave Chinner430cbeb2010-12-02 16:30:55 +11001377 * ignored in xfs_buf_rele().
1378 */
1379 atomic_set(&bp->b_lru_ref, 0);
1380 spin_unlock(&btp->bt_lru_lock);
1381 xfs_buf_rele(bp);
1382 spin_lock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001384 spin_unlock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
Dave Chinnerff57ab22010-11-30 17:27:57 +11001387int
1388xfs_buftarg_shrink(
1389 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001390 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001391{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001392 struct xfs_buftarg *btp = container_of(shrink,
1393 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001394 struct xfs_buf *bp;
Ying Han1495f232011-05-24 17:12:27 -07001395 int nr_to_scan = sc->nr_to_scan;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001396 LIST_HEAD(dispose);
1397
1398 if (!nr_to_scan)
1399 return btp->bt_lru_nr;
1400
1401 spin_lock(&btp->bt_lru_lock);
1402 while (!list_empty(&btp->bt_lru)) {
1403 if (nr_to_scan-- <= 0)
1404 break;
1405
1406 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1407
1408 /*
1409 * Decrement the b_lru_ref count unless the value is already
1410 * zero. If the value is already zero, we need to reclaim the
1411 * buffer, otherwise it gets another trip through the LRU.
1412 */
1413 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1414 list_move_tail(&bp->b_lru, &btp->bt_lru);
1415 continue;
1416 }
1417
1418 /*
1419 * remove the buffer from the LRU now to avoid needing another
1420 * lock round trip inside xfs_buf_rele().
1421 */
1422 list_move(&bp->b_lru, &dispose);
1423 btp->bt_lru_nr--;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001424 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001425 spin_unlock(&btp->bt_lru_lock);
1426
1427 while (!list_empty(&dispose)) {
1428 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1429 list_del_init(&bp->b_lru);
1430 xfs_buf_rele(bp);
1431 }
1432
1433 return btp->bt_lru_nr;
David Chinnera6867a62006-01-11 15:37:58 +11001434}
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436void
1437xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001438 struct xfs_mount *mp,
1439 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001441 unregister_shrinker(&btp->bt_shrinker);
1442
Christoph Hellwigb7963132009-03-03 14:48:37 -05001443 if (mp->m_flags & XFS_MOUNT_BARRIER)
1444 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001445
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001446 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449STATIC int
1450xfs_setsize_buftarg_flags(
1451 xfs_buftarg_t *btp,
1452 unsigned int blocksize,
1453 unsigned int sectorsize,
1454 int verbose)
1455{
Nathan Scottce8e9222006-01-11 15:39:08 +11001456 btp->bt_bsize = blocksize;
1457 btp->bt_sshift = ffs(sectorsize) - 1;
1458 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Nathan Scottce8e9222006-01-11 15:39:08 +11001460 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001461 char name[BDEVNAME_SIZE];
1462
1463 bdevname(btp->bt_bdev, name);
1464
Dave Chinner4f107002011-03-07 10:00:35 +11001465 xfs_warn(btp->bt_mount,
1466 "Cannot set_blocksize to %u on device %s\n",
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001467 sectorsize, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return EINVAL;
1469 }
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return 0;
1472}
1473
1474/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001475 * When allocating the initial buffer target we have not yet
1476 * read in the superblock, so don't know what sized sectors
1477 * are being used is at this early stage. Play safe.
1478 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479STATIC int
1480xfs_setsize_buftarg_early(
1481 xfs_buftarg_t *btp,
1482 struct block_device *bdev)
1483{
1484 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001485 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488int
1489xfs_setsize_buftarg(
1490 xfs_buftarg_t *btp,
1491 unsigned int blocksize,
1492 unsigned int sectorsize)
1493{
1494 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1495}
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497xfs_buftarg_t *
1498xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001499 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001501 int external,
1502 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 xfs_buftarg_t *btp;
1505
1506 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1507
Dave Chinnerebad8612010-09-22 10:47:20 +10001508 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001509 btp->bt_dev = bdev->bd_dev;
1510 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001511 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1512 if (!btp->bt_bdi)
1513 goto error;
1514
Dave Chinner430cbeb2010-12-02 16:30:55 +11001515 INIT_LIST_HEAD(&btp->bt_lru);
1516 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (xfs_setsize_buftarg_early(btp, bdev))
1518 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001519 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1520 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1521 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return btp;
1523
1524error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001525 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return NULL;
1527}
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001530 * Add a buffer to the delayed write list.
1531 *
1532 * This queues a buffer for writeout if it hasn't already been. Note that
1533 * neither this routine nor the buffer list submission functions perform
1534 * any internal synchronization. It is expected that the lists are thread-local
1535 * to the callers.
1536 *
1537 * Returns true if we queued up the buffer, or false if it already had
1538 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001540bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001541xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001542 struct xfs_buf *bp,
1543 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001545 ASSERT(xfs_buf_islocked(bp));
1546 ASSERT(!(bp->b_flags & XBF_READ));
1547
1548 /*
1549 * If the buffer is already marked delwri it already is queued up
1550 * by someone else for imediate writeout. Just ignore it in that
1551 * case.
1552 */
1553 if (bp->b_flags & _XBF_DELWRI_Q) {
1554 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1555 return false;
1556 }
David Chinnera6867a62006-01-11 15:37:58 +11001557
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001558 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1559
Dave Chinnerd808f612010-02-02 10:13:42 +11001560 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001561 * If a buffer gets written out synchronously or marked stale while it
1562 * is on a delwri list we lazily remove it. To do this, the other party
1563 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1564 * It remains referenced and on the list. In a rare corner case it
1565 * might get readded to a delwri list after the synchronous writeout, in
1566 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001567 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001568 bp->b_flags |= _XBF_DELWRI_Q;
1569 if (list_empty(&bp->b_list)) {
1570 atomic_inc(&bp->b_hold);
1571 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001572 }
David Chinner585e6d82007-02-10 18:32:29 +11001573
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001574 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001575}
1576
Dave Chinner089716a2010-01-26 15:13:25 +11001577/*
1578 * Compare function is more complex than it needs to be because
1579 * the return value is only 32 bits and we are doing comparisons
1580 * on 64 bit values
1581 */
1582static int
1583xfs_buf_cmp(
1584 void *priv,
1585 struct list_head *a,
1586 struct list_head *b)
1587{
1588 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1589 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1590 xfs_daddr_t diff;
1591
1592 diff = ap->b_bn - bp->b_bn;
1593 if (diff < 0)
1594 return -1;
1595 if (diff > 0)
1596 return 1;
1597 return 0;
1598}
1599
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001600static int
1601__xfs_buf_delwri_submit(
1602 struct list_head *buffer_list,
1603 struct list_head *io_list,
1604 bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001606 struct blk_plug plug;
1607 struct xfs_buf *bp, *n;
1608 int pinned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001610 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
1611 if (!wait) {
1612 if (xfs_buf_ispinned(bp)) {
1613 pinned++;
1614 continue;
1615 }
1616 if (!xfs_buf_trylock(bp))
1617 continue;
1618 } else {
1619 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001622 /*
1623 * Someone else might have written the buffer synchronously or
1624 * marked it stale in the meantime. In that case only the
1625 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1626 * reference and remove it from the list here.
1627 */
1628 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1629 list_del_init(&bp->b_list);
1630 xfs_buf_relse(bp);
1631 continue;
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001634 list_move_tail(&bp->b_list, io_list);
1635 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001638 list_sort(NULL, io_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001639
1640 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001641 list_for_each_entry_safe(bp, n, io_list, b_list) {
1642 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
1643 bp->b_flags |= XBF_WRITE;
1644
1645 if (!wait) {
1646 bp->b_flags |= XBF_ASYNC;
1647 list_del_init(&bp->b_list);
Dave Chinner089716a2010-01-26 15:13:25 +11001648 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001649 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001651 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001653 return pinned;
1654}
Nathan Scottf07c2252006-09-28 10:52:15 +10001655
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001656/*
1657 * Write out a buffer list asynchronously.
1658 *
1659 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1660 * out and not wait for I/O completion on any of the buffers. This interface
1661 * is only safely useable for callers that can track I/O completion by higher
1662 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1663 * function.
1664 */
1665int
1666xfs_buf_delwri_submit_nowait(
1667 struct list_head *buffer_list)
1668{
1669 LIST_HEAD (io_list);
1670 return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
1671}
1672
1673/*
1674 * Write out a buffer list synchronously.
1675 *
1676 * This will take the @buffer_list, write all buffers out and wait for I/O
1677 * completion on all of the buffers. @buffer_list is consumed by the function,
1678 * so callers must have some other way of tracking buffers if they require such
1679 * functionality.
1680 */
1681int
1682xfs_buf_delwri_submit(
1683 struct list_head *buffer_list)
1684{
1685 LIST_HEAD (io_list);
1686 int error = 0, error2;
1687 struct xfs_buf *bp;
1688
1689 __xfs_buf_delwri_submit(buffer_list, &io_list, true);
1690
1691 /* Wait for IO to complete. */
1692 while (!list_empty(&io_list)) {
1693 bp = list_first_entry(&io_list, struct xfs_buf, b_list);
1694
1695 list_del_init(&bp->b_list);
1696 error2 = xfs_buf_iowait(bp);
1697 xfs_buf_relse(bp);
1698 if (!error)
1699 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001702 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001705int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001706xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Nathan Scott87582802006-03-14 13:18:19 +11001708 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1709 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001710 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001711 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001712
Dave Chinner51749e42010-09-08 09:00:22 +00001713 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001714 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001715 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001716 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001720 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001721 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001722 out:
Nathan Scott87582802006-03-14 13:18:19 +11001723 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726void
Nathan Scottce8e9222006-01-11 15:39:08 +11001727xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001729 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001730 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}