blob: 59e39160d379d9186c3a6680d630fca87213c7eb [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) \
Dave Chinneraa5c1582012-04-23 15:58:56 +100059 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
James Bottomley73c77e22010-01-25 11:42:24 -060062static inline int
63xfs_buf_is_vmapped(
64 struct xfs_buf *bp)
65{
66 /*
67 * Return true if the buffer is vmapped.
68 *
69 * The XBF_MAPPED flag is set if the buffer should be mapped, but the
70 * code is clever enough to know it doesn't have to map a single page,
71 * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
72 */
73 return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
74}
75
76static inline int
77xfs_buf_vmap_len(
78 struct xfs_buf *bp)
79{
80 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
81}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
Dave Chinner430cbeb2010-12-02 16:30:55 +110084 * xfs_buf_lru_add - add a buffer to the LRU.
85 *
86 * The LRU takes a new reference to the buffer so that it will only be freed
87 * once the shrinker takes the buffer off the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Dave Chinner430cbeb2010-12-02 16:30:55 +110089STATIC void
90xfs_buf_lru_add(
91 struct xfs_buf *bp)
92{
93 struct xfs_buftarg *btp = bp->b_target;
94
95 spin_lock(&btp->bt_lru_lock);
96 if (list_empty(&bp->b_lru)) {
97 atomic_inc(&bp->b_hold);
98 list_add_tail(&bp->b_lru, &btp->bt_lru);
99 btp->bt_lru_nr++;
100 }
101 spin_unlock(&btp->bt_lru_lock);
102}
103
104/*
105 * xfs_buf_lru_del - remove a buffer from the LRU
106 *
107 * The unlocked check is safe here because it only occurs when there are not
108 * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there
109 * to optimise the shrinker removing the buffer from the LRU and calling
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300110 * xfs_buf_free(). i.e. it removes an unnecessary round trip on the
Dave Chinner430cbeb2010-12-02 16:30:55 +1100111 * bt_lru_lock.
112 */
113STATIC void
114xfs_buf_lru_del(
115 struct xfs_buf *bp)
116{
117 struct xfs_buftarg *btp = bp->b_target;
118
119 if (list_empty(&bp->b_lru))
120 return;
121
122 spin_lock(&btp->bt_lru_lock);
123 if (!list_empty(&bp->b_lru)) {
124 list_del_init(&bp->b_lru);
125 btp->bt_lru_nr--;
126 }
127 spin_unlock(&btp->bt_lru_lock);
128}
129
130/*
131 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
132 * b_lru_ref count so that the buffer is freed immediately when the buffer
133 * reference count falls to zero. If the buffer is already on the LRU, we need
134 * to remove the reference that LRU holds on the buffer.
135 *
136 * This prevents build-up of stale buffers on the LRU.
137 */
138void
139xfs_buf_stale(
140 struct xfs_buf *bp)
141{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000142 ASSERT(xfs_buf_islocked(bp));
143
Dave Chinner430cbeb2010-12-02 16:30:55 +1100144 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000145
146 /*
147 * Clear the delwri status so that a delwri queue walker will not
148 * flush this buffer to disk now that it is stale. The delwri queue has
149 * a reference to the buffer, so this is safe to do.
150 */
151 bp->b_flags &= ~_XBF_DELWRI_Q;
152
Dave Chinner430cbeb2010-12-02 16:30:55 +1100153 atomic_set(&(bp)->b_lru_ref, 0);
154 if (!list_empty(&bp->b_lru)) {
155 struct xfs_buftarg *btp = bp->b_target;
156
157 spin_lock(&btp->bt_lru_lock);
158 if (!list_empty(&bp->b_lru)) {
159 list_del_init(&bp->b_lru);
160 btp->bt_lru_nr--;
161 atomic_dec(&bp->b_hold);
162 }
163 spin_unlock(&btp->bt_lru_lock);
164 }
165 ASSERT(atomic_read(&bp->b_hold) >= 1);
166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000168struct xfs_buf *
169xfs_buf_alloc(
170 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000171 xfs_daddr_t blkno,
172 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100173 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000175 struct xfs_buf *bp;
176
Dave Chinneraa5c1582012-04-23 15:58:56 +1000177 bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000178 if (unlikely(!bp))
179 return NULL;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100182 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 */
Dave Chinneraa5c1582012-04-23 15:58:56 +1000184 flags &= ~(XBF_MAPPED|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Nathan Scottce8e9222006-01-11 15:39:08 +1100186 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100187 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000188 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100189 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100190 INIT_LIST_HEAD(&bp->b_list);
Dave Chinner74f75a02010-09-24 19:59:04 +1000191 RB_CLEAR_NODE(&bp->b_rbnode);
Thomas Gleixnera731cd12010-09-07 14:33:15 +0000192 sema_init(&bp->b_sema, 0); /* held, no waiters */
Nathan Scottce8e9222006-01-11 15:39:08 +1100193 XB_SET_OWNER(bp);
194 bp->b_target = target;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000197 * Set length and io_length to the same value initially.
198 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * most cases but may be reset (e.g. XFS recovery).
200 */
Dave Chinner4e94b712012-04-23 15:58:51 +1000201 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000202 bp->b_io_length = numblks;
Nathan Scottce8e9222006-01-11 15:39:08 +1100203 bp->b_flags = flags;
Dave Chinnere70b73f2012-04-23 15:58:49 +1000204
205 /*
206 * We do not set the block number here in the buffer because we have not
207 * finished initialising the buffer. We insert the buffer into the cache
208 * in this state, so this ensures that we are unable to do IO on a
209 * buffer that hasn't been fully initialised.
210 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100211 bp->b_bn = XFS_BUF_DADDR_NULL;
212 atomic_set(&bp->b_pin_count, 0);
213 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Nathan Scottce8e9222006-01-11 15:39:08 +1100215 XFS_STATS_INC(xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000216 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000217
218 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100222 * Allocate a page array capable of holding a specified number
223 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
225STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100226_xfs_buf_get_pages(
227 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100229 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100232 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100233 bp->b_page_count = page_count;
234 if (page_count <= XB_PAGES) {
235 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100237 bp->b_pages = kmem_alloc(sizeof(struct page *) *
Dave Chinneraa5c1582012-04-23 15:58:56 +1000238 page_count, KM_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +1100239 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return -ENOMEM;
241 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100242 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 return 0;
245}
246
247/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100248 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
250STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100251_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 xfs_buf_t *bp)
253{
Nathan Scottce8e9222006-01-11 15:39:08 +1100254 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000255 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000256 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258}
259
260/*
261 * Releases the specified buffer.
262 *
263 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100264 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 * hashed and refcounted buffers
266 */
267void
Nathan Scottce8e9222006-01-11 15:39:08 +1100268xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 xfs_buf_t *bp)
270{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000271 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Dave Chinner430cbeb2010-12-02 16:30:55 +1100273 ASSERT(list_empty(&bp->b_lru));
274
Dave Chinner0e6e8472011-03-26 09:16:45 +1100275 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 uint i;
277
James Bottomley73c77e22010-01-25 11:42:24 -0600278 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000279 vm_unmap_ram(bp->b_addr - bp->b_offset,
280 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Nathan Scott948ecdb2006-09-28 11:03:13 +1000282 for (i = 0; i < bp->b_page_count; i++) {
283 struct page *page = bp->b_pages[i];
284
Dave Chinner0e6e8472011-03-26 09:16:45 +1100285 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000286 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100287 } else if (bp->b_flags & _XBF_KMEM)
288 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000289 _xfs_buf_free_pages(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000290 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100294 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 */
296STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100297xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 xfs_buf_t *bp,
299 uint flags)
300{
Dave Chinneraa0e8832012-04-23 15:58:52 +1000301 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100303 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 unsigned short page_count, i;
Dave Chinner795cac72012-04-23 15:58:53 +1000305 xfs_off_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int error;
307
Dave Chinner0e6e8472011-03-26 09:16:45 +1100308 /*
309 * for buffers that are contained within a single page, just allocate
310 * the memory from the heap - there's no need for the complexity of
311 * page arrays to keep allocation down to order 0.
312 */
Dave Chinner795cac72012-04-23 15:58:53 +1000313 size = BBTOB(bp->b_length);
314 if (size < PAGE_SIZE) {
Dave Chinneraa5c1582012-04-23 15:58:56 +1000315 bp->b_addr = kmem_alloc(size, KM_NOFS);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100316 if (!bp->b_addr) {
317 /* low memory - use alloc_page loop instead */
318 goto use_alloc_page;
319 }
320
Dave Chinner795cac72012-04-23 15:58:53 +1000321 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100322 ((unsigned long)bp->b_addr & PAGE_MASK)) {
323 /* b_addr spans two pages - use alloc_page instead */
324 kmem_free(bp->b_addr);
325 bp->b_addr = NULL;
326 goto use_alloc_page;
327 }
328 bp->b_offset = offset_in_page(bp->b_addr);
329 bp->b_pages = bp->b_page_array;
330 bp->b_pages[0] = virt_to_page(bp->b_addr);
331 bp->b_page_count = 1;
332 bp->b_flags |= XBF_MAPPED | _XBF_KMEM;
333 return 0;
334 }
335
336use_alloc_page:
Dave Chinner795cac72012-04-23 15:58:53 +1000337 start = BBTOB(bp->b_bn) >> PAGE_SHIFT;
338 end = (BBTOB(bp->b_bn + bp->b_length) + PAGE_SIZE - 1) >> PAGE_SHIFT;
339 page_count = end - start;
Nathan Scottce8e9222006-01-11 15:39:08 +1100340 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (unlikely(error))
342 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Nathan Scottce8e9222006-01-11 15:39:08 +1100344 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100345 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Nathan Scottce8e9222006-01-11 15:39:08 +1100347 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct page *page;
349 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100350retry:
351 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100353 if (flags & XBF_READ_AHEAD) {
354 bp->b_page_count = i;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100355 error = ENOMEM;
356 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
359 /*
360 * This could deadlock.
361 *
362 * But until all the XFS lowlevel code is revamped to
363 * handle buffer allocation failures we can't do much.
364 */
365 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100366 xfs_err(NULL,
367 "possible memory allocation deadlock in %s (mode:0x%x)",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000368 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Nathan Scottce8e9222006-01-11 15:39:08 +1100370 XFS_STATS_INC(xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200371 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 goto retry;
373 }
374
Nathan Scottce8e9222006-01-11 15:39:08 +1100375 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Dave Chinner0e6e8472011-03-26 09:16:45 +1100377 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100379 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 offset = 0;
381 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Dave Chinner0e6e8472011-03-26 09:16:45 +1100384out_free_pages:
385 for (i = 0; i < bp->b_page_count; i++)
386 __free_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return error;
388}
389
390/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300391 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
393STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100394_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 xfs_buf_t *bp,
396 uint flags)
397{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100398 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100399 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100400 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100401 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
402 bp->b_flags |= XBF_MAPPED;
403 } else if (flags & XBF_MAPPED) {
Dave Chinnera19fb382011-03-26 09:13:42 +1100404 int retried = 0;
405
406 do {
407 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
408 -1, PAGE_KERNEL);
409 if (bp->b_addr)
410 break;
411 vm_unmap_aliases();
412 } while (retried++ <= 1);
413
414 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100416 bp->b_addr += bp->b_offset;
417 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
420 return 0;
421}
422
423/*
424 * Finding and Reading Buffers
425 */
426
427/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100428 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * a given range of an inode. The buffer is returned
Chandra Seetharamaneabbaf12011-09-08 20:18:50 +0000430 * locked. No I/O is implied by this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 */
432xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100433_xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000434 struct xfs_buftarg *btp,
435 xfs_daddr_t blkno,
436 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100437 xfs_buf_flags_t flags,
438 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000440 size_t numbytes;
Dave Chinner74f75a02010-09-24 19:59:04 +1000441 struct xfs_perag *pag;
442 struct rb_node **rbp;
443 struct rb_node *parent;
444 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Dave Chinnere70b73f2012-04-23 15:58:49 +1000446 numbytes = BBTOB(numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 /* Check for IOs smaller than the sector size / not sector aligned */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000449 ASSERT(!(numbytes < (1 << btp->bt_sshift)));
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000450 ASSERT(!(BBTOB(blkno) & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Dave Chinner74f75a02010-09-24 19:59:04 +1000452 /* get tree root */
453 pag = xfs_perag_get(btp->bt_mount,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000454 xfs_daddr_to_agno(btp->bt_mount, blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dave Chinner74f75a02010-09-24 19:59:04 +1000456 /* walk tree */
457 spin_lock(&pag->pag_buf_lock);
458 rbp = &pag->pag_buf_tree.rb_node;
459 parent = NULL;
460 bp = NULL;
461 while (*rbp) {
462 parent = *rbp;
463 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000465 if (blkno < bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000466 rbp = &(*rbp)->rb_left;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000467 else if (blkno > bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000468 rbp = &(*rbp)->rb_right;
469 else {
470 /*
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000471 * found a block number match. If the range doesn't
Dave Chinner74f75a02010-09-24 19:59:04 +1000472 * match, the only way this is allowed is if the buffer
473 * in the cache is stale and the transaction that made
474 * it stale has not yet committed. i.e. we are
475 * reallocating a busy extent. Skip this buffer and
476 * continue searching to the right for an exact match.
477 */
Dave Chinner4e94b712012-04-23 15:58:51 +1000478 if (bp->b_length != numblks) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000479 ASSERT(bp->b_flags & XBF_STALE);
480 rbp = &(*rbp)->rb_right;
481 continue;
482 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100483 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 goto found;
485 }
486 }
487
488 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100489 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000490 rb_link_node(&new_bp->b_rbnode, parent, rbp);
491 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
492 /* the buffer keeps the perag reference until it is freed */
493 new_bp->b_pag = pag;
494 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100496 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000497 spin_unlock(&pag->pag_buf_lock);
498 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100500 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000503 spin_unlock(&pag->pag_buf_lock);
504 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200506 if (!xfs_buf_trylock(bp)) {
507 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100508 xfs_buf_rele(bp);
509 XFS_STATS_INC(xb_busy_locked);
510 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200512 xfs_buf_lock(bp);
513 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
Dave Chinner0e6e8472011-03-26 09:16:45 +1100516 /*
517 * if the buffer is stale, clear all the external state associated with
518 * it. We need to keep flags such as how we allocated the buffer memory
519 * intact here.
520 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100521 if (bp->b_flags & XBF_STALE) {
522 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100523 bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
David Chinner2f926582005-09-05 08:33:35 +1000524 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000525
526 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100527 XFS_STATS_INC(xb_get_locked);
528 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531/*
Dave Chinner38158322011-09-30 04:45:02 +0000532 * Assembles a buffer covering the specified range. The code is optimised for
533 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
534 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 */
Dave Chinner38158322011-09-30 04:45:02 +0000536struct xfs_buf *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000537xfs_buf_get(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000538 xfs_buftarg_t *target,
539 xfs_daddr_t blkno,
540 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100541 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Dave Chinner38158322011-09-30 04:45:02 +0000543 struct xfs_buf *bp;
544 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100545 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Dave Chinnere70b73f2012-04-23 15:58:49 +1000547 bp = _xfs_buf_find(target, blkno, numblks, flags, NULL);
Dave Chinner38158322011-09-30 04:45:02 +0000548 if (likely(bp))
549 goto found;
550
Dave Chinnere70b73f2012-04-23 15:58:49 +1000551 new_bp = xfs_buf_alloc(target, blkno, numblks, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100552 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return NULL;
554
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000555 error = xfs_buf_allocate_memory(new_bp, flags);
556 if (error) {
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000557 kmem_zone_free(xfs_buf_zone, new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000558 return NULL;
559 }
560
Dave Chinnere70b73f2012-04-23 15:58:49 +1000561 bp = _xfs_buf_find(target, blkno, numblks, flags, new_bp);
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000562 if (!bp) {
563 xfs_buf_free(new_bp);
564 return NULL;
565 }
566
567 if (bp != new_bp)
568 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Dave Chinner38158322011-09-30 04:45:02 +0000570 /*
571 * Now we have a workable buffer, fill in the block number so
572 * that we can do IO on it.
573 */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000574 bp->b_bn = blkno;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000575 bp->b_io_length = bp->b_length;
Dave Chinner38158322011-09-30 04:45:02 +0000576
577found:
Nathan Scottce8e9222006-01-11 15:39:08 +1100578 if (!(bp->b_flags & XBF_MAPPED)) {
579 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100581 xfs_warn(target->bt_mount,
582 "%s: failed to map pages\n", __func__);
Dave Chinnera8acad72012-04-23 15:58:54 +1000583 xfs_buf_relse(bp);
584 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586 }
587
Nathan Scottce8e9222006-01-11 15:39:08 +1100588 XFS_STATS_INC(xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000589 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100590 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100593STATIC int
594_xfs_buf_read(
595 xfs_buf_t *bp,
596 xfs_buf_flags_t flags)
597{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000598 ASSERT(!(flags & XBF_WRITE));
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100599 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
600
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000601 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200602 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100603
Dave Chinner0e95f192012-04-23 15:58:46 +1000604 xfs_buf_iorequest(bp);
605 if (flags & XBF_ASYNC)
606 return 0;
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000607 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000611xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 xfs_buftarg_t *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000613 xfs_daddr_t blkno,
614 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100615 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Nathan Scottce8e9222006-01-11 15:39:08 +1100617 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Nathan Scottce8e9222006-01-11 15:39:08 +1100619 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Dave Chinnere70b73f2012-04-23 15:58:49 +1000621 bp = xfs_buf_get(target, blkno, numblks, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100622 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000623 trace_xfs_buf_read(bp, flags, _RET_IP_);
624
Nathan Scottce8e9222006-01-11 15:39:08 +1100625 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100626 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100627 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100628 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /*
630 * Read ahead call which is already satisfied,
631 * drop the buffer
632 */
Dave Chinnera8acad72012-04-23 15:58:54 +1000633 xfs_buf_relse(bp);
634 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100637 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639 }
640
Nathan Scottce8e9222006-01-11 15:39:08 +1100641 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100645 * If we are not low on memory then do the readahead in a deadlock
646 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 */
648void
Nathan Scottce8e9222006-01-11 15:39:08 +1100649xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 xfs_buftarg_t *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000651 xfs_daddr_t blkno,
652 size_t numblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100654 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return;
656
Dave Chinnere70b73f2012-04-23 15:58:49 +1000657 xfs_buf_read(target, blkno, numblks,
Dave Chinneraa5c1582012-04-23 15:58:56 +1000658 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Dave Chinner5adc94c2010-09-24 21:58:31 +1000661/*
662 * Read an uncached buffer from disk. Allocates and returns a locked
663 * buffer containing the disk contents or nothing.
664 */
665struct xfs_buf *
666xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000667 struct xfs_buftarg *target,
668 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000669 size_t numblks,
Dave Chinner5adc94c2010-09-24 21:58:31 +1000670 int flags)
671{
672 xfs_buf_t *bp;
673 int error;
674
Dave Chinnere70b73f2012-04-23 15:58:49 +1000675 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000676 if (!bp)
677 return NULL;
678
679 /* set up the buffer for a read IO */
Dave Chinner5adc94c2010-09-24 21:58:31 +1000680 XFS_BUF_SET_ADDR(bp, daddr);
681 XFS_BUF_READ(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000682
Dave Chinnere70b73f2012-04-23 15:58:49 +1000683 xfsbdstrat(target->bt_mount, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000684 error = xfs_buf_iowait(bp);
Dave Chinner0e95f192012-04-23 15:58:46 +1000685 if (error) {
Dave Chinner5adc94c2010-09-24 21:58:31 +1000686 xfs_buf_relse(bp);
687 return NULL;
688 }
689 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Dave Chinner44396472011-04-21 09:34:27 +0000692/*
693 * Return a buffer allocated as an empty buffer and associated to external
694 * memory via xfs_buf_associate_memory() back to it's empty state.
695 */
696void
697xfs_buf_set_empty(
698 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000699 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000700{
701 if (bp->b_pages)
702 _xfs_buf_free_pages(bp);
703
704 bp->b_pages = NULL;
705 bp->b_page_count = 0;
706 bp->b_addr = NULL;
Dave Chinner4e94b712012-04-23 15:58:51 +1000707 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000708 bp->b_io_length = numblks;
Dave Chinner44396472011-04-21 09:34:27 +0000709 bp->b_bn = XFS_BUF_DADDR_NULL;
710 bp->b_flags &= ~XBF_MAPPED;
711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713static inline struct page *
714mem_to_page(
715 void *addr)
716{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800717 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return virt_to_page(addr);
719 } else {
720 return vmalloc_to_page(addr);
721 }
722}
723
724int
Nathan Scottce8e9222006-01-11 15:39:08 +1100725xfs_buf_associate_memory(
726 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 void *mem,
728 size_t len)
729{
730 int rval;
731 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100732 unsigned long pageaddr;
733 unsigned long offset;
734 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int page_count;
736
Dave Chinner0e6e8472011-03-26 09:16:45 +1100737 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100738 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100739 buflen = PAGE_ALIGN(len + offset);
740 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100743 if (bp->b_pages)
744 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Nathan Scottce8e9222006-01-11 15:39:08 +1100746 bp->b_pages = NULL;
747 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Dave Chinneraa5c1582012-04-23 15:58:56 +1000749 rval = _xfs_buf_get_pages(bp, page_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (rval)
751 return rval;
752
Nathan Scottce8e9222006-01-11 15:39:08 +1100753 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100755 for (i = 0; i < bp->b_page_count; i++) {
756 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100757 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Dave Chinneraa0e8832012-04-23 15:58:52 +1000760 bp->b_io_length = BTOBB(len);
Dave Chinner4e94b712012-04-23 15:58:51 +1000761 bp->b_length = BTOBB(buflen);
Nathan Scottce8e9222006-01-11 15:39:08 +1100762 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 return 0;
765}
766
767xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000768xfs_buf_get_uncached(
769 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000770 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000771 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000773 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000774 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Dave Chinnere70b73f2012-04-23 15:58:49 +1000777 bp = xfs_buf_alloc(target, 0, numblks, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (unlikely(bp == NULL))
779 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Dave Chinnere70b73f2012-04-23 15:58:49 +1000781 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000782 error = _xfs_buf_get_pages(bp, page_count, 0);
783 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 goto fail_free_buf;
785
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000786 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000787 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000788 if (!bp->b_pages[i])
789 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000791 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000793 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
794 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100795 xfs_warn(target->bt_mount,
796 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Dave Chinner686865f2010-09-24 20:07:47 +1000800 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000804 while (--i >= 0)
805 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000806 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 fail_free_buf:
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000808 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 fail:
810 return NULL;
811}
812
813/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 * Increment reference count on buffer, to hold the buffer concurrently
815 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * Must hold the buffer already to call this function.
817 */
818void
Nathan Scottce8e9222006-01-11 15:39:08 +1100819xfs_buf_hold(
820 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000822 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100823 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
826/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100827 * Releases a hold on the specified buffer. If the
828 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 */
830void
Nathan Scottce8e9222006-01-11 15:39:08 +1100831xfs_buf_rele(
832 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Dave Chinner74f75a02010-09-24 19:59:04 +1000834 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000836 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Dave Chinner74f75a02010-09-24 19:59:04 +1000838 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100839 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000840 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100841 if (atomic_dec_and_test(&bp->b_hold))
842 xfs_buf_free(bp);
843 return;
844 }
845
Dave Chinner74f75a02010-09-24 19:59:04 +1000846 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100847
Lachlan McIlroy37906892008-08-13 15:42:10 +1000848 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000849 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000850 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100851 atomic_read(&bp->b_lru_ref)) {
852 xfs_buf_lru_add(bp);
853 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100854 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100855 xfs_buf_lru_del(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000856 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner74f75a02010-09-24 19:59:04 +1000857 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
858 spin_unlock(&pag->pag_buf_lock);
859 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100860 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862 }
863}
864
865
866/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100867 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100868 *
869 * If we come across a stale, pinned, locked buffer, we know that we are
870 * being asked to lock a buffer that has been reallocated. Because it is
871 * pinned, we know that the log has not been pushed to disk and hence it
872 * will still be locked. Rather than continuing to have trylock attempts
873 * fail until someone else pushes the log, push it ourselves before
874 * returning. This means that the xfsaild will not get stuck trying
875 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 */
877int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200878xfs_buf_trylock(
879 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 int locked;
882
Nathan Scottce8e9222006-01-11 15:39:08 +1100883 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000884 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100885 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100886 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
887 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000888
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200889 trace_xfs_buf_trylock(bp, _RET_IP_);
890 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100894 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000895 *
896 * If we come across a stale, pinned, locked buffer, we know that we
897 * are being asked to lock a buffer that has been reallocated. Because
898 * it is pinned, we know that the log has not been pushed to disk and
899 * hence it will still be locked. Rather than sleeping until someone
900 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100902void
903xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200904 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000906 trace_xfs_buf_lock(bp, _RET_IP_);
907
Dave Chinnered3b4d62010-05-21 12:07:08 +1000908 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000909 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100910 down(&bp->b_sema);
911 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000912
913 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916void
Nathan Scottce8e9222006-01-11 15:39:08 +1100917xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200918 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Nathan Scottce8e9222006-01-11 15:39:08 +1100920 XB_CLEAR_OWNER(bp);
921 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000922
923 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Nathan Scottce8e9222006-01-11 15:39:08 +1100926STATIC void
927xfs_buf_wait_unpin(
928 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 DECLARE_WAITQUEUE (wait, current);
931
Nathan Scottce8e9222006-01-11 15:39:08 +1100932 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return;
934
Nathan Scottce8e9222006-01-11 15:39:08 +1100935 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 for (;;) {
937 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100938 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100940 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100942 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 set_current_state(TASK_RUNNING);
944}
945
946/*
947 * Buffer Utility Routines
948 */
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100951xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000952 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
David Howellsc4028952006-11-22 14:57:56 +0000954 xfs_buf_t *bp =
955 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400957 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100958 (*(bp->b_iodone))(bp);
959 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 xfs_buf_relse(bp);
961}
962
963void
Nathan Scottce8e9222006-01-11 15:39:08 +1100964xfs_buf_ioend(
965 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int schedule)
967{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000968 trace_xfs_buf_iodone(bp, _RET_IP_);
969
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100970 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100971 if (bp->b_error == 0)
972 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Nathan Scottce8e9222006-01-11 15:39:08 +1100974 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000976 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100977 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 } else {
David Howellsc4028952006-11-22 14:57:56 +0000979 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +1000982 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986void
Nathan Scottce8e9222006-01-11 15:39:08 +1100987xfs_buf_ioerror(
988 xfs_buf_t *bp,
989 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +1100992 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000993 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Christoph Hellwig901796a2011-10-10 16:52:49 +0000996void
997xfs_buf_ioerror_alert(
998 struct xfs_buf *bp,
999 const char *func)
1000{
1001 xfs_alert(bp->b_target->bt_mount,
Dave Chinneraa0e8832012-04-23 15:58:52 +10001002"metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
1003 (__uint64_t)XFS_BUF_ADDR(bp), func, bp->b_error, bp->b_length);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001007xfs_bwrite(
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001008 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001010 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001012 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001014 bp->b_flags |= XBF_WRITE;
1015 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
1016
Christoph Hellwig939d7232010-07-20 17:51:16 +10001017 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Christoph Hellwig8c383662010-03-12 10:59:40 +00001019 error = xfs_buf_iowait(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001020 if (error) {
1021 xfs_force_shutdown(bp->b_target->bt_mount,
1022 SHUTDOWN_META_IO_ERROR);
1023 }
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001024 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001025}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Christoph Hellwig4e234712010-01-13 22:17:56 +00001027/*
1028 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001029 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001030 * so that the proper iodone callbacks get called.
1031 */
1032STATIC int
1033xfs_bioerror(
1034 xfs_buf_t *bp)
1035{
1036#ifdef XFSERRORDEBUG
1037 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1038#endif
1039
1040 /*
1041 * No need to wait until the buffer is unpinned, we aren't flushing it.
1042 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001043 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001044
1045 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001046 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001047 */
1048 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001049 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001050 xfs_buf_stale(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001051
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001052 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001053
1054 return EIO;
1055}
1056
1057/*
1058 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001059 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001060 * This is meant for userdata errors; metadata bufs come with
1061 * iodone functions attached, so that we can track down errors.
1062 */
1063STATIC int
1064xfs_bioerror_relse(
1065 struct xfs_buf *bp)
1066{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001067 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001068 /*
1069 * No need to wait until the buffer is unpinned.
1070 * We aren't flushing it.
1071 *
1072 * chunkhold expects B_DONE to be set, whether
1073 * we actually finish the I/O or not. We don't want to
1074 * change that interface.
1075 */
1076 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001077 XFS_BUF_DONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001078 xfs_buf_stale(bp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001079 bp->b_iodone = NULL;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001080 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001081 /*
1082 * Mark b_error and B_ERROR _both_.
1083 * Lot's of chunkcache code assumes that.
1084 * There's no reason to mark error for
1085 * ASYNC buffers.
1086 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001087 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig5fde0322011-10-10 16:52:44 +00001088 complete(&bp->b_iowait);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001089 } else {
1090 xfs_buf_relse(bp);
1091 }
1092
1093 return EIO;
1094}
1095
1096
1097/*
1098 * All xfs metadata buffers except log state machine buffers
1099 * get this attached as their b_bdstrat callback function.
1100 * This is so that we can catch a buffer
1101 * after prematurely unpinning it to forcibly shutdown the filesystem.
1102 */
1103int
1104xfs_bdstrat_cb(
1105 struct xfs_buf *bp)
1106{
Dave Chinnerebad8612010-09-22 10:47:20 +10001107 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001108 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1109 /*
1110 * Metadata write that didn't get logged but
1111 * written delayed anyway. These aren't associated
1112 * with a transaction, and can be ignored.
1113 */
1114 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1115 return xfs_bioerror_relse(bp);
1116 else
1117 return xfs_bioerror(bp);
1118 }
1119
1120 xfs_buf_iorequest(bp);
1121 return 0;
1122}
1123
1124/*
1125 * Wrapper around bdstrat so that we can stop data from going to disk in case
1126 * we are shutting down the filesystem. Typically user data goes thru this
1127 * path; one of the exceptions is the superblock.
1128 */
1129void
1130xfsbdstrat(
1131 struct xfs_mount *mp,
1132 struct xfs_buf *bp)
1133{
1134 if (XFS_FORCED_SHUTDOWN(mp)) {
1135 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1136 xfs_bioerror_relse(bp);
1137 return;
1138 }
1139
1140 xfs_buf_iorequest(bp);
1141}
1142
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001143STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001144_xfs_buf_ioend(
1145 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 int schedule)
1147{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001148 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001149 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
Al Viro782e3b32007-10-12 07:17:47 +01001152STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001153xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 int error)
1156{
Nathan Scottce8e9222006-01-11 15:39:08 +11001157 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001159 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
James Bottomley73c77e22010-01-25 11:42:24 -06001161 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1162 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1163
Nathan Scottce8e9222006-01-11 15:39:08 +11001164 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
1168STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001169_xfs_buf_ioapply(
1170 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001172 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001174 int offset = bp->b_offset;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001175 int size = BBTOB(bp->b_io_length);
Nathan Scottce8e9222006-01-11 15:39:08 +11001176 sector_t sector = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Nathan Scottce8e9222006-01-11 15:39:08 +11001178 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 map_i = 0;
1180
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001181 if (bp->b_flags & XBF_WRITE) {
1182 if (bp->b_flags & XBF_SYNCIO)
1183 rw = WRITE_SYNC;
1184 else
1185 rw = WRITE;
1186 if (bp->b_flags & XBF_FUA)
1187 rw |= REQ_FUA;
1188 if (bp->b_flags & XBF_FLUSH)
1189 rw |= REQ_FLUSH;
1190 } else if (bp->b_flags & XBF_READ_AHEAD) {
1191 rw = READA;
Nathan Scott51bdd702006-09-28 11:01:57 +10001192 } else {
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001193 rw = READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001194 }
1195
Christoph Hellwig34951f52011-07-26 15:06:44 +00001196 /* we only use the buffer cache for meta-data */
1197 rw |= REQ_META;
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001200 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1202 if (nr_pages > total_nr_pages)
1203 nr_pages = total_nr_pages;
1204
1205 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001206 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001208 bio->bi_end_io = xfs_buf_bio_end_io;
1209 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Dave Chinner0e6e8472011-03-26 09:16:45 +11001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 for (; size && nr_pages; nr_pages--, map_i++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001213 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 if (nbytes > size)
1216 nbytes = size;
1217
Nathan Scottce8e9222006-01-11 15:39:08 +11001218 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1219 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 break;
1221
1222 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001223 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 size -= nbytes;
1225 total_nr_pages--;
1226 }
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001229 if (xfs_buf_is_vmapped(bp)) {
1230 flush_kernel_vmap_range(bp->b_addr,
1231 xfs_buf_vmap_len(bp));
1232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 submit_bio(rw, bio);
1234 if (size)
1235 goto next_chunk;
1236 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001237 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001238 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240}
1241
Dave Chinner0e95f192012-04-23 15:58:46 +10001242void
Nathan Scottce8e9222006-01-11 15:39:08 +11001243xfs_buf_iorequest(
1244 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001246 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001248 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Christoph Hellwig375ec69d2011-08-23 08:28:03 +00001250 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001251 xfs_buf_wait_unpin(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001252 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /* Set the count to 1 initially, this will stop an I/O
1255 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001256 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001258 atomic_set(&bp->b_io_remaining, 1);
1259 _xfs_buf_ioapply(bp);
1260 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Nathan Scottce8e9222006-01-11 15:39:08 +11001262 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
1265/*
Dave Chinner0e95f192012-04-23 15:58:46 +10001266 * Waits for I/O to complete on the buffer supplied. It returns immediately if
1267 * no I/O is pending or there is already a pending error on the buffer. It
1268 * returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 */
1270int
Nathan Scottce8e9222006-01-11 15:39:08 +11001271xfs_buf_iowait(
1272 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001274 trace_xfs_buf_iowait(bp, _RET_IP_);
1275
Dave Chinner0e95f192012-04-23 15:58:46 +10001276 if (!bp->b_error)
1277 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001278
1279 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001280 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Nathan Scottce8e9222006-01-11 15:39:08 +11001283xfs_caddr_t
1284xfs_buf_offset(
1285 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 size_t offset)
1287{
1288 struct page *page;
1289
Nathan Scottce8e9222006-01-11 15:39:08 +11001290 if (bp->b_flags & XBF_MAPPED)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001291 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Nathan Scottce8e9222006-01-11 15:39:08 +11001293 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001294 page = bp->b_pages[offset >> PAGE_SHIFT];
1295 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
1298/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 * Move data into or out of a buffer.
1300 */
1301void
Nathan Scottce8e9222006-01-11 15:39:08 +11001302xfs_buf_iomove(
1303 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 size_t boff, /* starting buffer offset */
1305 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001306 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001307 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Dave Chinner795cac72012-04-23 15:58:53 +10001309 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 bend = boff + bsize;
1312 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001313 struct page *page;
1314 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Dave Chinner795cac72012-04-23 15:58:53 +10001316 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1317 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1318 page = bp->b_pages[page_index];
1319 csize = min_t(size_t, PAGE_SIZE - page_offset,
1320 BBTOB(bp->b_io_length) - boff);
1321
1322 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001325 case XBRW_ZERO:
Dave Chinner795cac72012-04-23 15:58:53 +10001326 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001328 case XBRW_READ:
Dave Chinner795cac72012-04-23 15:58:53 +10001329 memcpy(data, page_address(page) + page_offset, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001331 case XBRW_WRITE:
Dave Chinner795cac72012-04-23 15:58:53 +10001332 memcpy(page_address(page) + page_offset, data, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
1334
1335 boff += csize;
1336 data += csize;
1337 }
1338}
1339
1340/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001341 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 */
1343
1344/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001345 * Wait for any bufs with callbacks that have been submitted but have not yet
1346 * returned. These buffers will have an elevated hold count, so wait on those
1347 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 */
1349void
1350xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001351 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Dave Chinner430cbeb2010-12-02 16:30:55 +11001353 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Dave Chinner430cbeb2010-12-02 16:30:55 +11001355restart:
1356 spin_lock(&btp->bt_lru_lock);
1357 while (!list_empty(&btp->bt_lru)) {
1358 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1359 if (atomic_read(&bp->b_hold) > 1) {
1360 spin_unlock(&btp->bt_lru_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001361 delay(100);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001362 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001364 /*
Paul Bolle90802ed2011-12-05 13:00:34 +01001365 * clear the LRU reference count so the buffer doesn't get
Dave Chinner430cbeb2010-12-02 16:30:55 +11001366 * ignored in xfs_buf_rele().
1367 */
1368 atomic_set(&bp->b_lru_ref, 0);
1369 spin_unlock(&btp->bt_lru_lock);
1370 xfs_buf_rele(bp);
1371 spin_lock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001373 spin_unlock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Dave Chinnerff57ab22010-11-30 17:27:57 +11001376int
1377xfs_buftarg_shrink(
1378 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001379 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001380{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001381 struct xfs_buftarg *btp = container_of(shrink,
1382 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001383 struct xfs_buf *bp;
Ying Han1495f232011-05-24 17:12:27 -07001384 int nr_to_scan = sc->nr_to_scan;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001385 LIST_HEAD(dispose);
1386
1387 if (!nr_to_scan)
1388 return btp->bt_lru_nr;
1389
1390 spin_lock(&btp->bt_lru_lock);
1391 while (!list_empty(&btp->bt_lru)) {
1392 if (nr_to_scan-- <= 0)
1393 break;
1394
1395 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1396
1397 /*
1398 * Decrement the b_lru_ref count unless the value is already
1399 * zero. If the value is already zero, we need to reclaim the
1400 * buffer, otherwise it gets another trip through the LRU.
1401 */
1402 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1403 list_move_tail(&bp->b_lru, &btp->bt_lru);
1404 continue;
1405 }
1406
1407 /*
1408 * remove the buffer from the LRU now to avoid needing another
1409 * lock round trip inside xfs_buf_rele().
1410 */
1411 list_move(&bp->b_lru, &dispose);
1412 btp->bt_lru_nr--;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001413 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001414 spin_unlock(&btp->bt_lru_lock);
1415
1416 while (!list_empty(&dispose)) {
1417 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1418 list_del_init(&bp->b_lru);
1419 xfs_buf_rele(bp);
1420 }
1421
1422 return btp->bt_lru_nr;
David Chinnera6867a62006-01-11 15:37:58 +11001423}
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425void
1426xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001427 struct xfs_mount *mp,
1428 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001430 unregister_shrinker(&btp->bt_shrinker);
1431
Christoph Hellwigb7963132009-03-03 14:48:37 -05001432 if (mp->m_flags & XFS_MOUNT_BARRIER)
1433 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001434
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001435 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438STATIC int
1439xfs_setsize_buftarg_flags(
1440 xfs_buftarg_t *btp,
1441 unsigned int blocksize,
1442 unsigned int sectorsize,
1443 int verbose)
1444{
Nathan Scottce8e9222006-01-11 15:39:08 +11001445 btp->bt_bsize = blocksize;
1446 btp->bt_sshift = ffs(sectorsize) - 1;
1447 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Nathan Scottce8e9222006-01-11 15:39:08 +11001449 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001450 char name[BDEVNAME_SIZE];
1451
1452 bdevname(btp->bt_bdev, name);
1453
Dave Chinner4f107002011-03-07 10:00:35 +11001454 xfs_warn(btp->bt_mount,
1455 "Cannot set_blocksize to %u on device %s\n",
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001456 sectorsize, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return EINVAL;
1458 }
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return 0;
1461}
1462
1463/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001464 * When allocating the initial buffer target we have not yet
1465 * read in the superblock, so don't know what sized sectors
1466 * are being used is at this early stage. Play safe.
1467 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468STATIC int
1469xfs_setsize_buftarg_early(
1470 xfs_buftarg_t *btp,
1471 struct block_device *bdev)
1472{
1473 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001474 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
1477int
1478xfs_setsize_buftarg(
1479 xfs_buftarg_t *btp,
1480 unsigned int blocksize,
1481 unsigned int sectorsize)
1482{
1483 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1484}
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486xfs_buftarg_t *
1487xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001488 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001490 int external,
1491 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 xfs_buftarg_t *btp;
1494
1495 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1496
Dave Chinnerebad8612010-09-22 10:47:20 +10001497 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001498 btp->bt_dev = bdev->bd_dev;
1499 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001500 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1501 if (!btp->bt_bdi)
1502 goto error;
1503
Dave Chinner430cbeb2010-12-02 16:30:55 +11001504 INIT_LIST_HEAD(&btp->bt_lru);
1505 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (xfs_setsize_buftarg_early(btp, bdev))
1507 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001508 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1509 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1510 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return btp;
1512
1513error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001514 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return NULL;
1516}
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001519 * Add a buffer to the delayed write list.
1520 *
1521 * This queues a buffer for writeout if it hasn't already been. Note that
1522 * neither this routine nor the buffer list submission functions perform
1523 * any internal synchronization. It is expected that the lists are thread-local
1524 * to the callers.
1525 *
1526 * Returns true if we queued up the buffer, or false if it already had
1527 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001529bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001530xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001531 struct xfs_buf *bp,
1532 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001534 ASSERT(xfs_buf_islocked(bp));
1535 ASSERT(!(bp->b_flags & XBF_READ));
1536
1537 /*
1538 * If the buffer is already marked delwri it already is queued up
1539 * by someone else for imediate writeout. Just ignore it in that
1540 * case.
1541 */
1542 if (bp->b_flags & _XBF_DELWRI_Q) {
1543 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1544 return false;
1545 }
David Chinnera6867a62006-01-11 15:37:58 +11001546
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001547 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1548
Dave Chinnerd808f612010-02-02 10:13:42 +11001549 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001550 * If a buffer gets written out synchronously or marked stale while it
1551 * is on a delwri list we lazily remove it. To do this, the other party
1552 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1553 * It remains referenced and on the list. In a rare corner case it
1554 * might get readded to a delwri list after the synchronous writeout, in
1555 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001556 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001557 bp->b_flags |= _XBF_DELWRI_Q;
1558 if (list_empty(&bp->b_list)) {
1559 atomic_inc(&bp->b_hold);
1560 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001561 }
David Chinner585e6d82007-02-10 18:32:29 +11001562
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001563 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001564}
1565
Dave Chinner089716a2010-01-26 15:13:25 +11001566/*
1567 * Compare function is more complex than it needs to be because
1568 * the return value is only 32 bits and we are doing comparisons
1569 * on 64 bit values
1570 */
1571static int
1572xfs_buf_cmp(
1573 void *priv,
1574 struct list_head *a,
1575 struct list_head *b)
1576{
1577 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1578 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1579 xfs_daddr_t diff;
1580
1581 diff = ap->b_bn - bp->b_bn;
1582 if (diff < 0)
1583 return -1;
1584 if (diff > 0)
1585 return 1;
1586 return 0;
1587}
1588
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001589static int
1590__xfs_buf_delwri_submit(
1591 struct list_head *buffer_list,
1592 struct list_head *io_list,
1593 bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001595 struct blk_plug plug;
1596 struct xfs_buf *bp, *n;
1597 int pinned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001599 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
1600 if (!wait) {
1601 if (xfs_buf_ispinned(bp)) {
1602 pinned++;
1603 continue;
1604 }
1605 if (!xfs_buf_trylock(bp))
1606 continue;
1607 } else {
1608 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001611 /*
1612 * Someone else might have written the buffer synchronously or
1613 * marked it stale in the meantime. In that case only the
1614 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1615 * reference and remove it from the list here.
1616 */
1617 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1618 list_del_init(&bp->b_list);
1619 xfs_buf_relse(bp);
1620 continue;
1621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001623 list_move_tail(&bp->b_list, io_list);
1624 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001627 list_sort(NULL, io_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001628
1629 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001630 list_for_each_entry_safe(bp, n, io_list, b_list) {
1631 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
1632 bp->b_flags |= XBF_WRITE;
1633
1634 if (!wait) {
1635 bp->b_flags |= XBF_ASYNC;
1636 list_del_init(&bp->b_list);
Dave Chinner089716a2010-01-26 15:13:25 +11001637 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001638 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001640 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001642 return pinned;
1643}
Nathan Scottf07c2252006-09-28 10:52:15 +10001644
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001645/*
1646 * Write out a buffer list asynchronously.
1647 *
1648 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1649 * out and not wait for I/O completion on any of the buffers. This interface
1650 * is only safely useable for callers that can track I/O completion by higher
1651 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1652 * function.
1653 */
1654int
1655xfs_buf_delwri_submit_nowait(
1656 struct list_head *buffer_list)
1657{
1658 LIST_HEAD (io_list);
1659 return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
1660}
1661
1662/*
1663 * Write out a buffer list synchronously.
1664 *
1665 * This will take the @buffer_list, write all buffers out and wait for I/O
1666 * completion on all of the buffers. @buffer_list is consumed by the function,
1667 * so callers must have some other way of tracking buffers if they require such
1668 * functionality.
1669 */
1670int
1671xfs_buf_delwri_submit(
1672 struct list_head *buffer_list)
1673{
1674 LIST_HEAD (io_list);
1675 int error = 0, error2;
1676 struct xfs_buf *bp;
1677
1678 __xfs_buf_delwri_submit(buffer_list, &io_list, true);
1679
1680 /* Wait for IO to complete. */
1681 while (!list_empty(&io_list)) {
1682 bp = list_first_entry(&io_list, struct xfs_buf, b_list);
1683
1684 list_del_init(&bp->b_list);
1685 error2 = xfs_buf_iowait(bp);
1686 xfs_buf_relse(bp);
1687 if (!error)
1688 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001691 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001694int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001695xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Nathan Scott87582802006-03-14 13:18:19 +11001697 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1698 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001699 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001700 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001701
Dave Chinner51749e42010-09-08 09:00:22 +00001702 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001703 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001704 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001705 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001709 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001710 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001711 out:
Nathan Scott87582802006-03-14 13:18:19 +11001712 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715void
Nathan Scottce8e9222006-01-11 15:39:08 +11001716xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001718 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001719 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}