blob: 382c49a42ac2ac31d0e197196fa7ac7b509740da [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 Chinner4e94b712012-04-23 15:58:51 +1000201 * Set length and count_desired to the same value initially.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * I/O routines should use count_desired, which will be the same in
203 * most cases but may be reset (e.g. XFS recovery).
204 */
Dave Chinner4e94b712012-04-23 15:58:51 +1000205 bp->b_length = numblks;
206 bp->b_count_desired = numblks << BBSHIFT;
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{
Nathan Scottce8e9222006-01-11 15:39:08 +1100305 size_t size = bp->b_count_desired;
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 Scott204ab252006-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 Chinner0e6e8472011-03-26 09:16:45 +1100348 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Nathan Scottce8e9222006-01-11 15:39:08 +1100350 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct page *page;
352 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100353retry:
354 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100356 if (flags & XBF_READ_AHEAD) {
357 bp->b_page_count = i;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100358 error = ENOMEM;
359 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361
362 /*
363 * This could deadlock.
364 *
365 * But until all the XFS lowlevel code is revamped to
366 * handle buffer allocation failures we can't do much.
367 */
368 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100369 xfs_err(NULL,
370 "possible memory allocation deadlock in %s (mode:0x%x)",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000371 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Nathan Scottce8e9222006-01-11 15:39:08 +1100373 XFS_STATS_INC(xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200374 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 goto retry;
376 }
377
Nathan Scottce8e9222006-01-11 15:39:08 +1100378 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dave Chinner0e6e8472011-03-26 09:16:45 +1100380 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100382 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 offset = 0;
384 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Dave Chinner0e6e8472011-03-26 09:16:45 +1100387out_free_pages:
388 for (i = 0; i < bp->b_page_count; i++)
389 __free_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return error;
391}
392
393/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300394 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
396STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100397_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 xfs_buf_t *bp,
399 uint flags)
400{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100401 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100402 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100403 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100404 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
405 bp->b_flags |= XBF_MAPPED;
406 } else if (flags & XBF_MAPPED) {
Dave Chinnera19fb382011-03-26 09:13:42 +1100407 int retried = 0;
408
409 do {
410 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
411 -1, PAGE_KERNEL);
412 if (bp->b_addr)
413 break;
414 vm_unmap_aliases();
415 } while (retried++ <= 1);
416
417 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100419 bp->b_addr += bp->b_offset;
420 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
423 return 0;
424}
425
426/*
427 * Finding and Reading Buffers
428 */
429
430/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100431 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * a given range of an inode. The buffer is returned
Chandra Seetharamaneabbaf12011-09-08 20:18:50 +0000433 * locked. No I/O is implied by this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
435xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100436_xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000437 struct xfs_buftarg *btp,
438 xfs_daddr_t blkno,
439 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100440 xfs_buf_flags_t flags,
441 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000443 size_t numbytes;
Dave Chinner74f75a02010-09-24 19:59:04 +1000444 struct xfs_perag *pag;
445 struct rb_node **rbp;
446 struct rb_node *parent;
447 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Dave Chinnere70b73f2012-04-23 15:58:49 +1000449 numbytes = BBTOB(numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /* Check for IOs smaller than the sector size / not sector aligned */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000452 ASSERT(!(numbytes < (1 << btp->bt_sshift)));
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000453 ASSERT(!(BBTOB(blkno) & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Dave Chinner74f75a02010-09-24 19:59:04 +1000455 /* get tree root */
456 pag = xfs_perag_get(btp->bt_mount,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000457 xfs_daddr_to_agno(btp->bt_mount, blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Dave Chinner74f75a02010-09-24 19:59:04 +1000459 /* walk tree */
460 spin_lock(&pag->pag_buf_lock);
461 rbp = &pag->pag_buf_tree.rb_node;
462 parent = NULL;
463 bp = NULL;
464 while (*rbp) {
465 parent = *rbp;
466 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000468 if (blkno < bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000469 rbp = &(*rbp)->rb_left;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000470 else if (blkno > bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000471 rbp = &(*rbp)->rb_right;
472 else {
473 /*
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000474 * found a block number match. If the range doesn't
Dave Chinner74f75a02010-09-24 19:59:04 +1000475 * match, the only way this is allowed is if the buffer
476 * in the cache is stale and the transaction that made
477 * it stale has not yet committed. i.e. we are
478 * reallocating a busy extent. Skip this buffer and
479 * continue searching to the right for an exact match.
480 */
Dave Chinner4e94b712012-04-23 15:58:51 +1000481 if (bp->b_length != numblks) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000482 ASSERT(bp->b_flags & XBF_STALE);
483 rbp = &(*rbp)->rb_right;
484 continue;
485 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100486 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 goto found;
488 }
489 }
490
491 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100492 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000493 rb_link_node(&new_bp->b_rbnode, parent, rbp);
494 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
495 /* the buffer keeps the perag reference until it is freed */
496 new_bp->b_pag = pag;
497 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100499 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000500 spin_unlock(&pag->pag_buf_lock);
501 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100503 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000506 spin_unlock(&pag->pag_buf_lock);
507 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200509 if (!xfs_buf_trylock(bp)) {
510 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100511 xfs_buf_rele(bp);
512 XFS_STATS_INC(xb_busy_locked);
513 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200515 xfs_buf_lock(bp);
516 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518
Dave Chinner0e6e8472011-03-26 09:16:45 +1100519 /*
520 * if the buffer is stale, clear all the external state associated with
521 * it. We need to keep flags such as how we allocated the buffer memory
522 * intact here.
523 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100524 if (bp->b_flags & XBF_STALE) {
525 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100526 bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
David Chinner2f926582005-09-05 08:33:35 +1000527 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000528
529 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100530 XFS_STATS_INC(xb_get_locked);
531 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534/*
Dave Chinner38158322011-09-30 04:45:02 +0000535 * Assembles a buffer covering the specified range. The code is optimised for
536 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
537 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 */
Dave Chinner38158322011-09-30 04:45:02 +0000539struct xfs_buf *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000540xfs_buf_get(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000541 xfs_buftarg_t *target,
542 xfs_daddr_t blkno,
543 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100544 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Dave Chinner38158322011-09-30 04:45:02 +0000546 struct xfs_buf *bp;
547 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100548 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Dave Chinnere70b73f2012-04-23 15:58:49 +1000550 bp = _xfs_buf_find(target, blkno, numblks, flags, NULL);
Dave Chinner38158322011-09-30 04:45:02 +0000551 if (likely(bp))
552 goto found;
553
Dave Chinnere70b73f2012-04-23 15:58:49 +1000554 new_bp = xfs_buf_alloc(target, blkno, numblks, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100555 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return NULL;
557
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000558 error = xfs_buf_allocate_memory(new_bp, flags);
559 if (error) {
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000560 kmem_zone_free(xfs_buf_zone, new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000561 return NULL;
562 }
563
Dave Chinnere70b73f2012-04-23 15:58:49 +1000564 bp = _xfs_buf_find(target, blkno, numblks, flags, new_bp);
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000565 if (!bp) {
566 xfs_buf_free(new_bp);
567 return NULL;
568 }
569
570 if (bp != new_bp)
571 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Dave Chinner38158322011-09-30 04:45:02 +0000573 /*
574 * Now we have a workable buffer, fill in the block number so
575 * that we can do IO on it.
576 */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000577 bp->b_bn = blkno;
Dave Chinner4e94b712012-04-23 15:58:51 +1000578 bp->b_count_desired = BBTOB(bp->b_length);
Dave Chinner38158322011-09-30 04:45:02 +0000579
580found:
Nathan Scottce8e9222006-01-11 15:39:08 +1100581 if (!(bp->b_flags & XBF_MAPPED)) {
582 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100584 xfs_warn(target->bt_mount,
585 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto no_buffer;
587 }
588 }
589
Nathan Scottce8e9222006-01-11 15:39:08 +1100590 XFS_STATS_INC(xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000591 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100592 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Dave Chinner38158322011-09-30 04:45:02 +0000594no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100595 if (flags & (XBF_LOCK | XBF_TRYLOCK))
596 xfs_buf_unlock(bp);
597 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return NULL;
599}
600
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100601STATIC int
602_xfs_buf_read(
603 xfs_buf_t *bp,
604 xfs_buf_flags_t flags)
605{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000606 ASSERT(!(flags & XBF_WRITE));
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100607 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
608
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000609 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200610 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100611
Dave Chinner0e95f192012-04-23 15:58:46 +1000612 xfs_buf_iorequest(bp);
613 if (flags & XBF_ASYNC)
614 return 0;
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000615 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000619xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 xfs_buftarg_t *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000621 xfs_daddr_t blkno,
622 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100623 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Nathan Scottce8e9222006-01-11 15:39:08 +1100625 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Nathan Scottce8e9222006-01-11 15:39:08 +1100627 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Dave Chinnere70b73f2012-04-23 15:58:49 +1000629 bp = xfs_buf_get(target, blkno, numblks, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100630 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000631 trace_xfs_buf_read(bp, flags, _RET_IP_);
632
Nathan Scottce8e9222006-01-11 15:39:08 +1100633 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100634 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100635 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100636 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /*
638 * Read ahead call which is already satisfied,
639 * drop the buffer
640 */
641 goto no_buffer;
642 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100644 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 }
647
Nathan Scottce8e9222006-01-11 15:39:08 +1100648 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100651 if (flags & (XBF_LOCK | XBF_TRYLOCK))
652 xfs_buf_unlock(bp);
653 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return NULL;
655}
656
657/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100658 * If we are not low on memory then do the readahead in a deadlock
659 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 */
661void
Nathan Scottce8e9222006-01-11 15:39:08 +1100662xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 xfs_buftarg_t *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000664 xfs_daddr_t blkno,
665 size_t numblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100667 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return;
669
Dave Chinnere70b73f2012-04-23 15:58:49 +1000670 xfs_buf_read(target, blkno, numblks,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000671 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
Dave Chinner5adc94c2010-09-24 21:58:31 +1000674/*
675 * Read an uncached buffer from disk. Allocates and returns a locked
676 * buffer containing the disk contents or nothing.
677 */
678struct xfs_buf *
679xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000680 struct xfs_buftarg *target,
681 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000682 size_t numblks,
Dave Chinner5adc94c2010-09-24 21:58:31 +1000683 int flags)
684{
685 xfs_buf_t *bp;
686 int error;
687
Dave Chinnere70b73f2012-04-23 15:58:49 +1000688 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000689 if (!bp)
690 return NULL;
691
692 /* set up the buffer for a read IO */
Dave Chinner5adc94c2010-09-24 21:58:31 +1000693 XFS_BUF_SET_ADDR(bp, daddr);
694 XFS_BUF_READ(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000695
Dave Chinnere70b73f2012-04-23 15:58:49 +1000696 xfsbdstrat(target->bt_mount, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000697 error = xfs_buf_iowait(bp);
Dave Chinner0e95f192012-04-23 15:58:46 +1000698 if (error) {
Dave Chinner5adc94c2010-09-24 21:58:31 +1000699 xfs_buf_relse(bp);
700 return NULL;
701 }
702 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Dave Chinner44396472011-04-21 09:34:27 +0000705/*
706 * Return a buffer allocated as an empty buffer and associated to external
707 * memory via xfs_buf_associate_memory() back to it's empty state.
708 */
709void
710xfs_buf_set_empty(
711 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000712 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000713{
714 if (bp->b_pages)
715 _xfs_buf_free_pages(bp);
716
717 bp->b_pages = NULL;
718 bp->b_page_count = 0;
719 bp->b_addr = NULL;
Dave Chinner4e94b712012-04-23 15:58:51 +1000720 bp->b_length = numblks;
721 bp->b_count_desired = numblks << BBSHIFT;
Dave Chinner44396472011-04-21 09:34:27 +0000722 bp->b_bn = XFS_BUF_DADDR_NULL;
723 bp->b_flags &= ~XBF_MAPPED;
724}
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726static inline struct page *
727mem_to_page(
728 void *addr)
729{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800730 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return virt_to_page(addr);
732 } else {
733 return vmalloc_to_page(addr);
734 }
735}
736
737int
Nathan Scottce8e9222006-01-11 15:39:08 +1100738xfs_buf_associate_memory(
739 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 void *mem,
741 size_t len)
742{
743 int rval;
744 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100745 unsigned long pageaddr;
746 unsigned long offset;
747 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int page_count;
749
Dave Chinner0e6e8472011-03-26 09:16:45 +1100750 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100751 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100752 buflen = PAGE_ALIGN(len + offset);
753 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100756 if (bp->b_pages)
757 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Nathan Scottce8e9222006-01-11 15:39:08 +1100759 bp->b_pages = NULL;
760 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Christoph Hellwig36fae172009-07-18 18:14:58 -0400762 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (rval)
764 return rval;
765
Nathan Scottce8e9222006-01-11 15:39:08 +1100766 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100768 for (i = 0; i < bp->b_page_count; i++) {
769 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100770 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100773 bp->b_count_desired = len;
Dave Chinner4e94b712012-04-23 15:58:51 +1000774 bp->b_length = BTOBB(buflen);
Nathan Scottce8e9222006-01-11 15:39:08 +1100775 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 return 0;
778}
779
780xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000781xfs_buf_get_uncached(
782 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000783 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000784 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000786 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000787 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Dave Chinnere70b73f2012-04-23 15:58:49 +1000790 bp = xfs_buf_alloc(target, 0, numblks, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (unlikely(bp == NULL))
792 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Dave Chinnere70b73f2012-04-23 15:58:49 +1000794 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000795 error = _xfs_buf_get_pages(bp, page_count, 0);
796 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 goto fail_free_buf;
798
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000799 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000800 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000801 if (!bp->b_pages[i])
802 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000804 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000806 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
807 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100808 xfs_warn(target->bt_mount,
809 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Dave Chinner686865f2010-09-24 20:07:47 +1000813 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000817 while (--i >= 0)
818 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000819 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 fail_free_buf:
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000821 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 fail:
823 return NULL;
824}
825
826/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 * Increment reference count on buffer, to hold the buffer concurrently
828 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 * Must hold the buffer already to call this function.
830 */
831void
Nathan Scottce8e9222006-01-11 15:39:08 +1100832xfs_buf_hold(
833 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000835 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100836 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100840 * Releases a hold on the specified buffer. If the
841 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 */
843void
Nathan Scottce8e9222006-01-11 15:39:08 +1100844xfs_buf_rele(
845 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Dave Chinner74f75a02010-09-24 19:59:04 +1000847 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000849 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Dave Chinner74f75a02010-09-24 19:59:04 +1000851 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100852 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000853 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100854 if (atomic_dec_and_test(&bp->b_hold))
855 xfs_buf_free(bp);
856 return;
857 }
858
Dave Chinner74f75a02010-09-24 19:59:04 +1000859 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100860
Lachlan McIlroy37906892008-08-13 15:42:10 +1000861 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000862 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000863 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100864 atomic_read(&bp->b_lru_ref)) {
865 xfs_buf_lru_add(bp);
866 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100867 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100868 xfs_buf_lru_del(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000869 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner74f75a02010-09-24 19:59:04 +1000870 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
871 spin_unlock(&pag->pag_buf_lock);
872 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100873 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875 }
876}
877
878
879/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100880 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100881 *
882 * If we come across a stale, pinned, locked buffer, we know that we are
883 * being asked to lock a buffer that has been reallocated. Because it is
884 * pinned, we know that the log has not been pushed to disk and hence it
885 * will still be locked. Rather than continuing to have trylock attempts
886 * fail until someone else pushes the log, push it ourselves before
887 * returning. This means that the xfsaild will not get stuck trying
888 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 */
890int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200891xfs_buf_trylock(
892 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 int locked;
895
Nathan Scottce8e9222006-01-11 15:39:08 +1100896 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000897 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100898 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100899 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
900 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000901
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200902 trace_xfs_buf_trylock(bp, _RET_IP_);
903 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100907 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000908 *
909 * If we come across a stale, pinned, locked buffer, we know that we
910 * are being asked to lock a buffer that has been reallocated. Because
911 * it is pinned, we know that the log has not been pushed to disk and
912 * hence it will still be locked. Rather than sleeping until someone
913 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100915void
916xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200917 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000919 trace_xfs_buf_lock(bp, _RET_IP_);
920
Dave Chinnered3b4d62010-05-21 12:07:08 +1000921 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000922 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100923 down(&bp->b_sema);
924 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000925
926 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929void
Nathan Scottce8e9222006-01-11 15:39:08 +1100930xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200931 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Nathan Scottce8e9222006-01-11 15:39:08 +1100933 XB_CLEAR_OWNER(bp);
934 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000935
936 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Nathan Scottce8e9222006-01-11 15:39:08 +1100939STATIC void
940xfs_buf_wait_unpin(
941 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 DECLARE_WAITQUEUE (wait, current);
944
Nathan Scottce8e9222006-01-11 15:39:08 +1100945 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return;
947
Nathan Scottce8e9222006-01-11 15:39:08 +1100948 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 for (;;) {
950 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100951 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100953 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100955 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 set_current_state(TASK_RUNNING);
957}
958
959/*
960 * Buffer Utility Routines
961 */
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100964xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000965 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
David Howellsc4028952006-11-22 14:57:56 +0000967 xfs_buf_t *bp =
968 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400970 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100971 (*(bp->b_iodone))(bp);
972 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 xfs_buf_relse(bp);
974}
975
976void
Nathan Scottce8e9222006-01-11 15:39:08 +1100977xfs_buf_ioend(
978 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 int schedule)
980{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000981 trace_xfs_buf_iodone(bp, _RET_IP_);
982
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100983 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100984 if (bp->b_error == 0)
985 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Nathan Scottce8e9222006-01-11 15:39:08 +1100987 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000989 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100990 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 } else {
David Howellsc4028952006-11-22 14:57:56 +0000992 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +1000995 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999void
Nathan Scottce8e9222006-01-11 15:39:08 +11001000xfs_buf_ioerror(
1001 xfs_buf_t *bp,
1002 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001005 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001006 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Christoph Hellwig901796a2011-10-10 16:52:49 +00001009void
1010xfs_buf_ioerror_alert(
1011 struct xfs_buf *bp,
1012 const char *func)
1013{
1014 xfs_alert(bp->b_target->bt_mount,
1015"metadata I/O error: block 0x%llx (\"%s\") error %d buf count %zd",
1016 (__uint64_t)XFS_BUF_ADDR(bp), func,
1017 bp->b_error, XFS_BUF_COUNT(bp));
1018}
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;
1189 int size = bp->b_count_desired;
1190 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;
1237 sector += nbytes >> BBSHIFT;
1238 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 Hellwig375ec69d2011-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 Chinner0e6e8472011-03-26 09:16:45 +11001331 PAGE_SIZE-cpoff, bp->b_count_desired-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}