blob: ddfc58f405064655417d5e72d87ca627be9d38c0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scottf07c2252006-09-28 10:52:15 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Vlad Apostolov93c189c2006-11-11 18:03:49 +110018#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/stddef.h>
20#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
23#include <linux/init.h>
24#include <linux/vmalloc.h>
25#include <linux/bio.h>
26#include <linux/sysctl.h>
27#include <linux/proc_fs.h>
28#include <linux/workqueue.h>
29#include <linux/percpu.h>
30#include <linux/blkdev.h>
31#include <linux/hash.h>
Christoph Hellwig4df08c52005-09-05 08:34:18 +100032#include <linux/kthread.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080033#include <linux/migrate.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070034#include <linux/backing-dev.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Christoph Hellwigb7963132009-03-03 14:48:37 -050037#include "xfs_sb.h"
38#include "xfs_inum.h"
Dave Chinnered3b4d62010-05-21 12:07:08 +100039#include "xfs_log.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050040#include "xfs_ag.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050041#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050043
David Chinner7989cb82007-02-10 18:34:56 +110044static kmem_zone_t *xfs_buf_zone;
Christoph Hellwig23ea4032005-06-21 15:14:01 +100045
David Chinner7989cb82007-02-10 18:34:56 +110046static struct workqueue_struct *xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Nathan Scottce8e9222006-01-11 15:39:08 +110048#ifdef XFS_BUF_LOCK_TRACKING
49# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
50# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
51# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#else
Nathan Scottce8e9222006-01-11 15:39:08 +110053# define XB_SET_OWNER(bp) do { } while (0)
54# define XB_CLEAR_OWNER(bp) do { } while (0)
55# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57
Nathan Scottce8e9222006-01-11 15:39:08 +110058#define xb_to_gfp(flags) \
59 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
60 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Nathan Scottce8e9222006-01-11 15:39:08 +110062#define xb_to_km(flags) \
63 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
James Bottomley73c77e22010-01-25 11:42:24 -060066static inline int
67xfs_buf_is_vmapped(
68 struct xfs_buf *bp)
69{
70 /*
71 * Return true if the buffer is vmapped.
72 *
73 * The XBF_MAPPED flag is set if the buffer should be mapped, but the
74 * code is clever enough to know it doesn't have to map a single page,
75 * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
76 */
77 return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
78}
79
80static inline int
81xfs_buf_vmap_len(
82 struct xfs_buf *bp)
83{
84 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
85}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
Dave Chinner430cbeb2010-12-02 16:30:55 +110088 * xfs_buf_lru_add - add a buffer to the LRU.
89 *
90 * The LRU takes a new reference to the buffer so that it will only be freed
91 * once the shrinker takes the buffer off the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
Dave Chinner430cbeb2010-12-02 16:30:55 +110093STATIC void
94xfs_buf_lru_add(
95 struct xfs_buf *bp)
96{
97 struct xfs_buftarg *btp = bp->b_target;
98
99 spin_lock(&btp->bt_lru_lock);
100 if (list_empty(&bp->b_lru)) {
101 atomic_inc(&bp->b_hold);
102 list_add_tail(&bp->b_lru, &btp->bt_lru);
103 btp->bt_lru_nr++;
104 }
105 spin_unlock(&btp->bt_lru_lock);
106}
107
108/*
109 * xfs_buf_lru_del - remove a buffer from the LRU
110 *
111 * The unlocked check is safe here because it only occurs when there are not
112 * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there
113 * to optimise the shrinker removing the buffer from the LRU and calling
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300114 * xfs_buf_free(). i.e. it removes an unnecessary round trip on the
Dave Chinner430cbeb2010-12-02 16:30:55 +1100115 * bt_lru_lock.
116 */
117STATIC void
118xfs_buf_lru_del(
119 struct xfs_buf *bp)
120{
121 struct xfs_buftarg *btp = bp->b_target;
122
123 if (list_empty(&bp->b_lru))
124 return;
125
126 spin_lock(&btp->bt_lru_lock);
127 if (!list_empty(&bp->b_lru)) {
128 list_del_init(&bp->b_lru);
129 btp->bt_lru_nr--;
130 }
131 spin_unlock(&btp->bt_lru_lock);
132}
133
134/*
135 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
136 * b_lru_ref count so that the buffer is freed immediately when the buffer
137 * reference count falls to zero. If the buffer is already on the LRU, we need
138 * to remove the reference that LRU holds on the buffer.
139 *
140 * This prevents build-up of stale buffers on the LRU.
141 */
142void
143xfs_buf_stale(
144 struct xfs_buf *bp)
145{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000146 ASSERT(xfs_buf_islocked(bp));
147
Dave Chinner430cbeb2010-12-02 16:30:55 +1100148 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000149
150 /*
151 * Clear the delwri status so that a delwri queue walker will not
152 * flush this buffer to disk now that it is stale. The delwri queue has
153 * a reference to the buffer, so this is safe to do.
154 */
155 bp->b_flags &= ~_XBF_DELWRI_Q;
156
Dave Chinner430cbeb2010-12-02 16:30:55 +1100157 atomic_set(&(bp)->b_lru_ref, 0);
158 if (!list_empty(&bp->b_lru)) {
159 struct xfs_buftarg *btp = bp->b_target;
160
161 spin_lock(&btp->bt_lru_lock);
162 if (!list_empty(&bp->b_lru)) {
163 list_del_init(&bp->b_lru);
164 btp->bt_lru_nr--;
165 atomic_dec(&bp->b_hold);
166 }
167 spin_unlock(&btp->bt_lru_lock);
168 }
169 ASSERT(atomic_read(&bp->b_hold) >= 1);
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000172struct xfs_buf *
173xfs_buf_alloc(
174 struct xfs_buftarg *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100175 xfs_off_t range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 size_t range_length,
Nathan Scottce8e9222006-01-11 15:39:08 +1100177 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000179 struct xfs_buf *bp;
180
181 bp = kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags));
182 if (unlikely(!bp))
183 return NULL;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100186 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100188 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Nathan Scottce8e9222006-01-11 15:39:08 +1100190 memset(bp, 0, sizeof(xfs_buf_t));
191 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100192 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000193 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100194 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100195 INIT_LIST_HEAD(&bp->b_list);
Dave Chinner74f75a02010-09-24 19:59:04 +1000196 RB_CLEAR_NODE(&bp->b_rbnode);
Thomas Gleixnera731cd12010-09-07 14:33:15 +0000197 sema_init(&bp->b_sema, 0); /* held, no waiters */
Nathan Scottce8e9222006-01-11 15:39:08 +1100198 XB_SET_OWNER(bp);
199 bp->b_target = target;
200 bp->b_file_offset = range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /*
202 * Set buffer_length and count_desired to the same value initially.
203 * I/O routines should use count_desired, which will be the same in
204 * most cases but may be reset (e.g. XFS recovery).
205 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100206 bp->b_buffer_length = bp->b_count_desired = range_length;
207 bp->b_flags = flags;
208 bp->b_bn = XFS_BUF_DADDR_NULL;
209 atomic_set(&bp->b_pin_count, 0);
210 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Nathan Scottce8e9222006-01-11 15:39:08 +1100212 XFS_STATS_INC(xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000213 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000214
215 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100219 * Allocate a page array capable of holding a specified number
220 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 */
222STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100223_xfs_buf_get_pages(
224 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100226 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100229 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100230 bp->b_page_count = page_count;
231 if (page_count <= XB_PAGES) {
232 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100234 bp->b_pages = kmem_alloc(sizeof(struct page *) *
235 page_count, xb_to_km(flags));
236 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return -ENOMEM;
238 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100239 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 return 0;
242}
243
244/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100245 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 */
247STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100248_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 xfs_buf_t *bp)
250{
Nathan Scottce8e9222006-01-11 15:39:08 +1100251 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000252 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000253 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255}
256
257/*
258 * Releases the specified buffer.
259 *
260 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100261 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 * hashed and refcounted buffers
263 */
264void
Nathan Scottce8e9222006-01-11 15:39:08 +1100265xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 xfs_buf_t *bp)
267{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000268 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Dave Chinner430cbeb2010-12-02 16:30:55 +1100270 ASSERT(list_empty(&bp->b_lru));
271
Dave Chinner0e6e8472011-03-26 09:16:45 +1100272 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 uint i;
274
James Bottomley73c77e22010-01-25 11:42:24 -0600275 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000276 vm_unmap_ram(bp->b_addr - bp->b_offset,
277 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Nathan Scott948ecdb2006-09-28 11:03:13 +1000279 for (i = 0; i < bp->b_page_count; i++) {
280 struct page *page = bp->b_pages[i];
281
Dave Chinner0e6e8472011-03-26 09:16:45 +1100282 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000283 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100284 } else if (bp->b_flags & _XBF_KMEM)
285 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000286 _xfs_buf_free_pages(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000287 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100291 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
293STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100294xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 xfs_buf_t *bp,
296 uint flags)
297{
Nathan Scottce8e9222006-01-11 15:39:08 +1100298 size_t size = bp->b_count_desired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100300 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 unsigned short page_count, i;
Nathan Scott204ab252006-01-11 20:50:22 +1100302 xfs_off_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 int error;
304
Dave Chinner0e6e8472011-03-26 09:16:45 +1100305 /*
306 * for buffers that are contained within a single page, just allocate
307 * the memory from the heap - there's no need for the complexity of
308 * page arrays to keep allocation down to order 0.
309 */
310 if (bp->b_buffer_length < PAGE_SIZE) {
311 bp->b_addr = kmem_alloc(bp->b_buffer_length, xb_to_km(flags));
312 if (!bp->b_addr) {
313 /* low memory - use alloc_page loop instead */
314 goto use_alloc_page;
315 }
316
317 if (((unsigned long)(bp->b_addr + bp->b_buffer_length - 1) &
318 PAGE_MASK) !=
319 ((unsigned long)bp->b_addr & PAGE_MASK)) {
320 /* b_addr spans two pages - use alloc_page instead */
321 kmem_free(bp->b_addr);
322 bp->b_addr = NULL;
323 goto use_alloc_page;
324 }
325 bp->b_offset = offset_in_page(bp->b_addr);
326 bp->b_pages = bp->b_page_array;
327 bp->b_pages[0] = virt_to_page(bp->b_addr);
328 bp->b_page_count = 1;
329 bp->b_flags |= XBF_MAPPED | _XBF_KMEM;
330 return 0;
331 }
332
333use_alloc_page:
Nathan Scottce8e9222006-01-11 15:39:08 +1100334 end = bp->b_file_offset + bp->b_buffer_length;
335 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
Nathan Scottce8e9222006-01-11 15:39:08 +1100336 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (unlikely(error))
338 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Nathan Scottce8e9222006-01-11 15:39:08 +1100340 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100341 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Nathan Scottce8e9222006-01-11 15:39:08 +1100343 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct page *page;
345 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100346retry:
347 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100349 if (flags & XBF_READ_AHEAD) {
350 bp->b_page_count = i;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100351 error = ENOMEM;
352 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
355 /*
356 * This could deadlock.
357 *
358 * But until all the XFS lowlevel code is revamped to
359 * handle buffer allocation failures we can't do much.
360 */
361 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100362 xfs_err(NULL,
363 "possible memory allocation deadlock in %s (mode:0x%x)",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000364 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Nathan Scottce8e9222006-01-11 15:39:08 +1100366 XFS_STATS_INC(xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200367 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 goto retry;
369 }
370
Nathan Scottce8e9222006-01-11 15:39:08 +1100371 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Dave Chinner0e6e8472011-03-26 09:16:45 +1100373 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100375 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 offset = 0;
377 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dave Chinner0e6e8472011-03-26 09:16:45 +1100380out_free_pages:
381 for (i = 0; i < bp->b_page_count; i++)
382 __free_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return error;
384}
385
386/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300387 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 */
389STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100390_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 xfs_buf_t *bp,
392 uint flags)
393{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100394 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100395 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100396 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100397 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
398 bp->b_flags |= XBF_MAPPED;
399 } else if (flags & XBF_MAPPED) {
Dave Chinnera19fb382011-03-26 09:13:42 +1100400 int retried = 0;
401
402 do {
403 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
404 -1, PAGE_KERNEL);
405 if (bp->b_addr)
406 break;
407 vm_unmap_aliases();
408 } while (retried++ <= 1);
409
410 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100412 bp->b_addr += bp->b_offset;
413 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
416 return 0;
417}
418
419/*
420 * Finding and Reading Buffers
421 */
422
423/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100424 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * a given range of an inode. The buffer is returned
Chandra Seetharamaneabbaf12011-09-08 20:18:50 +0000426 * locked. No I/O is implied by this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
428xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100429_xfs_buf_find(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 xfs_buftarg_t *btp, /* block device target */
Nathan Scott204ab252006-01-11 20:50:22 +1100431 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100433 xfs_buf_flags_t flags,
434 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Nathan Scott204ab252006-01-11 20:50:22 +1100436 xfs_off_t range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 size_t range_length;
Dave Chinner74f75a02010-09-24 19:59:04 +1000438 struct xfs_perag *pag;
439 struct rb_node **rbp;
440 struct rb_node *parent;
441 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 range_base = (ioff << BBSHIFT);
444 range_length = (isize << BBSHIFT);
445
446 /* Check for IOs smaller than the sector size / not sector aligned */
Nathan Scottce8e9222006-01-11 15:39:08 +1100447 ASSERT(!(range_length < (1 << btp->bt_sshift)));
Nathan Scott204ab252006-01-11 20:50:22 +1100448 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Dave Chinner74f75a02010-09-24 19:59:04 +1000450 /* get tree root */
451 pag = xfs_perag_get(btp->bt_mount,
452 xfs_daddr_to_agno(btp->bt_mount, ioff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Dave Chinner74f75a02010-09-24 19:59:04 +1000454 /* walk tree */
455 spin_lock(&pag->pag_buf_lock);
456 rbp = &pag->pag_buf_tree.rb_node;
457 parent = NULL;
458 bp = NULL;
459 while (*rbp) {
460 parent = *rbp;
461 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Dave Chinner74f75a02010-09-24 19:59:04 +1000463 if (range_base < bp->b_file_offset)
464 rbp = &(*rbp)->rb_left;
465 else if (range_base > bp->b_file_offset)
466 rbp = &(*rbp)->rb_right;
467 else {
468 /*
469 * found a block offset match. If the range doesn't
470 * match, the only way this is allowed is if the buffer
471 * in the cache is stale and the transaction that made
472 * it stale has not yet committed. i.e. we are
473 * reallocating a busy extent. Skip this buffer and
474 * continue searching to the right for an exact match.
475 */
476 if (bp->b_buffer_length != range_length) {
477 ASSERT(bp->b_flags & XBF_STALE);
478 rbp = &(*rbp)->rb_right;
479 continue;
480 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100481 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto found;
483 }
484 }
485
486 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100487 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000488 rb_link_node(&new_bp->b_rbnode, parent, rbp);
489 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
490 /* the buffer keeps the perag reference until it is freed */
491 new_bp->b_pag = pag;
492 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100494 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000495 spin_unlock(&pag->pag_buf_lock);
496 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100498 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000501 spin_unlock(&pag->pag_buf_lock);
502 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200504 if (!xfs_buf_trylock(bp)) {
505 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100506 xfs_buf_rele(bp);
507 XFS_STATS_INC(xb_busy_locked);
508 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200510 xfs_buf_lock(bp);
511 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
Dave Chinner0e6e8472011-03-26 09:16:45 +1100514 /*
515 * if the buffer is stale, clear all the external state associated with
516 * it. We need to keep flags such as how we allocated the buffer memory
517 * intact here.
518 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100519 if (bp->b_flags & XBF_STALE) {
520 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100521 bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
David Chinner2f926582005-09-05 08:33:35 +1000522 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000523
524 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100525 XFS_STATS_INC(xb_get_locked);
526 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529/*
Dave Chinner38158322011-09-30 04:45:02 +0000530 * Assembles a buffer covering the specified range. The code is optimised for
531 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
532 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 */
Dave Chinner38158322011-09-30 04:45:02 +0000534struct xfs_buf *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000535xfs_buf_get(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100537 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100539 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Dave Chinner38158322011-09-30 04:45:02 +0000541 struct xfs_buf *bp;
542 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100543 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Dave Chinner38158322011-09-30 04:45:02 +0000545 bp = _xfs_buf_find(target, ioff, isize, flags, NULL);
546 if (likely(bp))
547 goto found;
548
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000549 new_bp = xfs_buf_alloc(target, ioff << BBSHIFT, isize << BBSHIFT,
550 flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100551 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return NULL;
553
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000554 error = xfs_buf_allocate_memory(new_bp, flags);
555 if (error) {
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000556 kmem_zone_free(xfs_buf_zone, new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000557 return NULL;
558 }
559
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000560 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
561 if (!bp) {
562 xfs_buf_free(new_bp);
563 return NULL;
564 }
565
566 if (bp != new_bp)
567 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Dave Chinner38158322011-09-30 04:45:02 +0000569 /*
570 * Now we have a workable buffer, fill in the block number so
571 * that we can do IO on it.
572 */
573 bp->b_bn = ioff;
574 bp->b_count_desired = bp->b_buffer_length;
575
576found:
Nathan Scottce8e9222006-01-11 15:39:08 +1100577 if (!(bp->b_flags & XBF_MAPPED)) {
578 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100580 xfs_warn(target->bt_mount,
581 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto no_buffer;
583 }
584 }
585
Nathan Scottce8e9222006-01-11 15:39:08 +1100586 XFS_STATS_INC(xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000587 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100588 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Dave Chinner38158322011-09-30 04:45:02 +0000590no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100591 if (flags & (XBF_LOCK | XBF_TRYLOCK))
592 xfs_buf_unlock(bp);
593 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return NULL;
595}
596
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100597STATIC int
598_xfs_buf_read(
599 xfs_buf_t *bp,
600 xfs_buf_flags_t flags)
601{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000602 ASSERT(!(flags & XBF_WRITE));
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100603 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
604
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000605 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200606 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100607
Dave Chinner0e95f192012-04-23 15:58:46 +1000608 xfs_buf_iorequest(bp);
609 if (flags & XBF_ASYNC)
610 return 0;
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000611 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100612}
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000615xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100617 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100619 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Nathan Scottce8e9222006-01-11 15:39:08 +1100621 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Nathan Scottce8e9222006-01-11 15:39:08 +1100623 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000625 bp = xfs_buf_get(target, ioff, isize, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100626 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000627 trace_xfs_buf_read(bp, flags, _RET_IP_);
628
Nathan Scottce8e9222006-01-11 15:39:08 +1100629 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100630 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100631 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100632 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /*
634 * Read ahead call which is already satisfied,
635 * drop the buffer
636 */
637 goto no_buffer;
638 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100640 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642 }
643
Nathan Scottce8e9222006-01-11 15:39:08 +1100644 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100647 if (flags & (XBF_LOCK | XBF_TRYLOCK))
648 xfs_buf_unlock(bp);
649 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return NULL;
651}
652
653/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100654 * If we are not low on memory then do the readahead in a deadlock
655 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
657void
Nathan Scottce8e9222006-01-11 15:39:08 +1100658xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100660 xfs_off_t ioff,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000661 size_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100663 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return;
665
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000666 xfs_buf_read(target, ioff, isize,
667 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Dave Chinner5adc94c2010-09-24 21:58:31 +1000670/*
671 * Read an uncached buffer from disk. Allocates and returns a locked
672 * buffer containing the disk contents or nothing.
673 */
674struct xfs_buf *
675xfs_buf_read_uncached(
676 struct xfs_mount *mp,
677 struct xfs_buftarg *target,
678 xfs_daddr_t daddr,
679 size_t length,
680 int flags)
681{
682 xfs_buf_t *bp;
683 int error;
684
685 bp = xfs_buf_get_uncached(target, length, flags);
686 if (!bp)
687 return NULL;
688
689 /* set up the buffer for a read IO */
Dave Chinner5adc94c2010-09-24 21:58:31 +1000690 XFS_BUF_SET_ADDR(bp, daddr);
691 XFS_BUF_READ(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000692
693 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000694 error = xfs_buf_iowait(bp);
Dave Chinner0e95f192012-04-23 15:58:46 +1000695 if (error) {
Dave Chinner5adc94c2010-09-24 21:58:31 +1000696 xfs_buf_relse(bp);
697 return NULL;
698 }
699 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Dave Chinner44396472011-04-21 09:34:27 +0000702/*
703 * Return a buffer allocated as an empty buffer and associated to external
704 * memory via xfs_buf_associate_memory() back to it's empty state.
705 */
706void
707xfs_buf_set_empty(
708 struct xfs_buf *bp,
709 size_t len)
710{
711 if (bp->b_pages)
712 _xfs_buf_free_pages(bp);
713
714 bp->b_pages = NULL;
715 bp->b_page_count = 0;
716 bp->b_addr = NULL;
717 bp->b_file_offset = 0;
718 bp->b_buffer_length = bp->b_count_desired = len;
719 bp->b_bn = XFS_BUF_DADDR_NULL;
720 bp->b_flags &= ~XBF_MAPPED;
721}
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723static inline struct page *
724mem_to_page(
725 void *addr)
726{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800727 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return virt_to_page(addr);
729 } else {
730 return vmalloc_to_page(addr);
731 }
732}
733
734int
Nathan Scottce8e9222006-01-11 15:39:08 +1100735xfs_buf_associate_memory(
736 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 void *mem,
738 size_t len)
739{
740 int rval;
741 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100742 unsigned long pageaddr;
743 unsigned long offset;
744 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 int page_count;
746
Dave Chinner0e6e8472011-03-26 09:16:45 +1100747 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100748 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100749 buflen = PAGE_ALIGN(len + offset);
750 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100753 if (bp->b_pages)
754 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Nathan Scottce8e9222006-01-11 15:39:08 +1100756 bp->b_pages = NULL;
757 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Christoph Hellwig36fae172009-07-18 18:14:58 -0400759 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (rval)
761 return rval;
762
Nathan Scottce8e9222006-01-11 15:39:08 +1100763 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100765 for (i = 0; i < bp->b_page_count; i++) {
766 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100767 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100770 bp->b_count_desired = len;
771 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100772 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 return 0;
775}
776
777xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000778xfs_buf_get_uncached(
779 struct xfs_buftarg *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 size_t len,
Dave Chinner686865f2010-09-24 20:07:47 +1000781 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000783 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
784 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000787 bp = xfs_buf_alloc(target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (unlikely(bp == NULL))
789 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000791 error = _xfs_buf_get_pages(bp, page_count, 0);
792 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 goto fail_free_buf;
794
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000795 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000796 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000797 if (!bp->b_pages[i])
798 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000800 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000802 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
803 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100804 xfs_warn(target->bt_mount,
805 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Dave Chinner686865f2010-09-24 20:07:47 +1000809 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000813 while (--i >= 0)
814 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000815 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 fail_free_buf:
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000817 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 fail:
819 return NULL;
820}
821
822/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 * Increment reference count on buffer, to hold the buffer concurrently
824 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 * Must hold the buffer already to call this function.
826 */
827void
Nathan Scottce8e9222006-01-11 15:39:08 +1100828xfs_buf_hold(
829 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000831 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100832 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100836 * Releases a hold on the specified buffer. If the
837 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 */
839void
Nathan Scottce8e9222006-01-11 15:39:08 +1100840xfs_buf_rele(
841 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Dave Chinner74f75a02010-09-24 19:59:04 +1000843 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000845 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Dave Chinner74f75a02010-09-24 19:59:04 +1000847 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100848 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000849 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100850 if (atomic_dec_and_test(&bp->b_hold))
851 xfs_buf_free(bp);
852 return;
853 }
854
Dave Chinner74f75a02010-09-24 19:59:04 +1000855 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100856
Lachlan McIlroy37906892008-08-13 15:42:10 +1000857 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000858 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000859 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100860 atomic_read(&bp->b_lru_ref)) {
861 xfs_buf_lru_add(bp);
862 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100863 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100864 xfs_buf_lru_del(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000865 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner74f75a02010-09-24 19:59:04 +1000866 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
867 spin_unlock(&pag->pag_buf_lock);
868 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100869 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871 }
872}
873
874
875/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100876 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100877 *
878 * If we come across a stale, pinned, locked buffer, we know that we are
879 * being asked to lock a buffer that has been reallocated. Because it is
880 * pinned, we know that the log has not been pushed to disk and hence it
881 * will still be locked. Rather than continuing to have trylock attempts
882 * fail until someone else pushes the log, push it ourselves before
883 * returning. This means that the xfsaild will not get stuck trying
884 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 */
886int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200887xfs_buf_trylock(
888 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 int locked;
891
Nathan Scottce8e9222006-01-11 15:39:08 +1100892 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000893 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100894 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100895 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
896 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000897
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200898 trace_xfs_buf_trylock(bp, _RET_IP_);
899 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100903 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000904 *
905 * If we come across a stale, pinned, locked buffer, we know that we
906 * are being asked to lock a buffer that has been reallocated. Because
907 * it is pinned, we know that the log has not been pushed to disk and
908 * hence it will still be locked. Rather than sleeping until someone
909 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100911void
912xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200913 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000915 trace_xfs_buf_lock(bp, _RET_IP_);
916
Dave Chinnered3b4d62010-05-21 12:07:08 +1000917 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000918 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100919 down(&bp->b_sema);
920 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000921
922 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925void
Nathan Scottce8e9222006-01-11 15:39:08 +1100926xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200927 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Nathan Scottce8e9222006-01-11 15:39:08 +1100929 XB_CLEAR_OWNER(bp);
930 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000931
932 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Nathan Scottce8e9222006-01-11 15:39:08 +1100935STATIC void
936xfs_buf_wait_unpin(
937 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 DECLARE_WAITQUEUE (wait, current);
940
Nathan Scottce8e9222006-01-11 15:39:08 +1100941 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return;
943
Nathan Scottce8e9222006-01-11 15:39:08 +1100944 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 for (;;) {
946 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100947 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100949 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100951 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 set_current_state(TASK_RUNNING);
953}
954
955/*
956 * Buffer Utility Routines
957 */
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100960xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000961 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
David Howellsc4028952006-11-22 14:57:56 +0000963 xfs_buf_t *bp =
964 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400966 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100967 (*(bp->b_iodone))(bp);
968 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 xfs_buf_relse(bp);
970}
971
972void
Nathan Scottce8e9222006-01-11 15:39:08 +1100973xfs_buf_ioend(
974 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 int schedule)
976{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000977 trace_xfs_buf_iodone(bp, _RET_IP_);
978
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100979 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100980 if (bp->b_error == 0)
981 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Nathan Scottce8e9222006-01-11 15:39:08 +1100983 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000985 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100986 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 } else {
David Howellsc4028952006-11-22 14:57:56 +0000988 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +1000991 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993}
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995void
Nathan Scottce8e9222006-01-11 15:39:08 +1100996xfs_buf_ioerror(
997 xfs_buf_t *bp,
998 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001001 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001002 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
Christoph Hellwig901796a2011-10-10 16:52:49 +00001005void
1006xfs_buf_ioerror_alert(
1007 struct xfs_buf *bp,
1008 const char *func)
1009{
1010 xfs_alert(bp->b_target->bt_mount,
1011"metadata I/O error: block 0x%llx (\"%s\") error %d buf count %zd",
1012 (__uint64_t)XFS_BUF_ADDR(bp), func,
1013 bp->b_error, XFS_BUF_COUNT(bp));
1014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001017xfs_bwrite(
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001018 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001020 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001022 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001024 bp->b_flags |= XBF_WRITE;
1025 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
1026
Christoph Hellwig939d7232010-07-20 17:51:16 +10001027 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Christoph Hellwig8c383662010-03-12 10:59:40 +00001029 error = xfs_buf_iowait(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001030 if (error) {
1031 xfs_force_shutdown(bp->b_target->bt_mount,
1032 SHUTDOWN_META_IO_ERROR);
1033 }
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001034 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001035}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Christoph Hellwig4e234712010-01-13 22:17:56 +00001037/*
1038 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001039 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001040 * so that the proper iodone callbacks get called.
1041 */
1042STATIC int
1043xfs_bioerror(
1044 xfs_buf_t *bp)
1045{
1046#ifdef XFSERRORDEBUG
1047 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1048#endif
1049
1050 /*
1051 * No need to wait until the buffer is unpinned, we aren't flushing it.
1052 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001053 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001054
1055 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001056 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001057 */
1058 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001059 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001060 xfs_buf_stale(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001061
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001062 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001063
1064 return EIO;
1065}
1066
1067/*
1068 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001069 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001070 * This is meant for userdata errors; metadata bufs come with
1071 * iodone functions attached, so that we can track down errors.
1072 */
1073STATIC int
1074xfs_bioerror_relse(
1075 struct xfs_buf *bp)
1076{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001077 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001078 /*
1079 * No need to wait until the buffer is unpinned.
1080 * We aren't flushing it.
1081 *
1082 * chunkhold expects B_DONE to be set, whether
1083 * we actually finish the I/O or not. We don't want to
1084 * change that interface.
1085 */
1086 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001087 XFS_BUF_DONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001088 xfs_buf_stale(bp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001089 bp->b_iodone = NULL;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001090 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001091 /*
1092 * Mark b_error and B_ERROR _both_.
1093 * Lot's of chunkcache code assumes that.
1094 * There's no reason to mark error for
1095 * ASYNC buffers.
1096 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001097 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig5fde0322011-10-10 16:52:44 +00001098 complete(&bp->b_iowait);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001099 } else {
1100 xfs_buf_relse(bp);
1101 }
1102
1103 return EIO;
1104}
1105
1106
1107/*
1108 * All xfs metadata buffers except log state machine buffers
1109 * get this attached as their b_bdstrat callback function.
1110 * This is so that we can catch a buffer
1111 * after prematurely unpinning it to forcibly shutdown the filesystem.
1112 */
1113int
1114xfs_bdstrat_cb(
1115 struct xfs_buf *bp)
1116{
Dave Chinnerebad8612010-09-22 10:47:20 +10001117 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001118 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1119 /*
1120 * Metadata write that didn't get logged but
1121 * written delayed anyway. These aren't associated
1122 * with a transaction, and can be ignored.
1123 */
1124 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1125 return xfs_bioerror_relse(bp);
1126 else
1127 return xfs_bioerror(bp);
1128 }
1129
1130 xfs_buf_iorequest(bp);
1131 return 0;
1132}
1133
1134/*
1135 * Wrapper around bdstrat so that we can stop data from going to disk in case
1136 * we are shutting down the filesystem. Typically user data goes thru this
1137 * path; one of the exceptions is the superblock.
1138 */
1139void
1140xfsbdstrat(
1141 struct xfs_mount *mp,
1142 struct xfs_buf *bp)
1143{
1144 if (XFS_FORCED_SHUTDOWN(mp)) {
1145 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1146 xfs_bioerror_relse(bp);
1147 return;
1148 }
1149
1150 xfs_buf_iorequest(bp);
1151}
1152
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001153STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001154_xfs_buf_ioend(
1155 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 int schedule)
1157{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001158 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001159 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Al Viro782e3b32007-10-12 07:17:47 +01001162STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001163xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int error)
1166{
Nathan Scottce8e9222006-01-11 15:39:08 +11001167 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001169 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
James Bottomley73c77e22010-01-25 11:42:24 -06001171 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1172 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1173
Nathan Scottce8e9222006-01-11 15:39:08 +11001174 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
1178STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001179_xfs_buf_ioapply(
1180 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001182 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001184 int offset = bp->b_offset;
1185 int size = bp->b_count_desired;
1186 sector_t sector = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Nathan Scottce8e9222006-01-11 15:39:08 +11001188 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 map_i = 0;
1190
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001191 if (bp->b_flags & XBF_WRITE) {
1192 if (bp->b_flags & XBF_SYNCIO)
1193 rw = WRITE_SYNC;
1194 else
1195 rw = WRITE;
1196 if (bp->b_flags & XBF_FUA)
1197 rw |= REQ_FUA;
1198 if (bp->b_flags & XBF_FLUSH)
1199 rw |= REQ_FLUSH;
1200 } else if (bp->b_flags & XBF_READ_AHEAD) {
1201 rw = READA;
Nathan Scott51bdd702006-09-28 11:01:57 +10001202 } else {
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001203 rw = READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001204 }
1205
Christoph Hellwig34951f52011-07-26 15:06:44 +00001206 /* we only use the buffer cache for meta-data */
1207 rw |= REQ_META;
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001210 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1212 if (nr_pages > total_nr_pages)
1213 nr_pages = total_nr_pages;
1214
1215 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001216 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001218 bio->bi_end_io = xfs_buf_bio_end_io;
1219 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Dave Chinner0e6e8472011-03-26 09:16:45 +11001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 for (; size && nr_pages; nr_pages--, map_i++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001223 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 if (nbytes > size)
1226 nbytes = size;
1227
Nathan Scottce8e9222006-01-11 15:39:08 +11001228 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1229 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 break;
1231
1232 offset = 0;
1233 sector += nbytes >> BBSHIFT;
1234 size -= nbytes;
1235 total_nr_pages--;
1236 }
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001239 if (xfs_buf_is_vmapped(bp)) {
1240 flush_kernel_vmap_range(bp->b_addr,
1241 xfs_buf_vmap_len(bp));
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 submit_bio(rw, bio);
1244 if (size)
1245 goto next_chunk;
1246 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001247 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001248 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250}
1251
Dave Chinner0e95f192012-04-23 15:58:46 +10001252void
Nathan Scottce8e9222006-01-11 15:39:08 +11001253xfs_buf_iorequest(
1254 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001256 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001258 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Christoph Hellwig375ec69d2011-08-23 08:28:03 +00001260 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001261 xfs_buf_wait_unpin(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001262 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /* Set the count to 1 initially, this will stop an I/O
1265 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001266 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001268 atomic_set(&bp->b_io_remaining, 1);
1269 _xfs_buf_ioapply(bp);
1270 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Nathan Scottce8e9222006-01-11 15:39:08 +11001272 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
1275/*
Dave Chinner0e95f192012-04-23 15:58:46 +10001276 * Waits for I/O to complete on the buffer supplied. It returns immediately if
1277 * no I/O is pending or there is already a pending error on the buffer. It
1278 * returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 */
1280int
Nathan Scottce8e9222006-01-11 15:39:08 +11001281xfs_buf_iowait(
1282 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001284 trace_xfs_buf_iowait(bp, _RET_IP_);
1285
Dave Chinner0e95f192012-04-23 15:58:46 +10001286 if (!bp->b_error)
1287 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001288
1289 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001290 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
Nathan Scottce8e9222006-01-11 15:39:08 +11001293xfs_caddr_t
1294xfs_buf_offset(
1295 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 size_t offset)
1297{
1298 struct page *page;
1299
Nathan Scottce8e9222006-01-11 15:39:08 +11001300 if (bp->b_flags & XBF_MAPPED)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001301 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Nathan Scottce8e9222006-01-11 15:39:08 +11001303 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001304 page = bp->b_pages[offset >> PAGE_SHIFT];
1305 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 * Move data into or out of a buffer.
1310 */
1311void
Nathan Scottce8e9222006-01-11 15:39:08 +11001312xfs_buf_iomove(
1313 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 size_t boff, /* starting buffer offset */
1315 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001316 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001317 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 size_t bend, cpoff, csize;
1320 struct page *page;
1321
1322 bend = boff + bsize;
1323 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001324 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1325 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 csize = min_t(size_t,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001327 PAGE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Dave Chinner0e6e8472011-03-26 09:16:45 +11001329 ASSERT(((csize + cpoff) <= PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001332 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 memset(page_address(page) + cpoff, 0, csize);
1334 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001335 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 memcpy(data, page_address(page) + cpoff, csize);
1337 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001338 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 memcpy(page_address(page) + cpoff, data, csize);
1340 }
1341
1342 boff += csize;
1343 data += csize;
1344 }
1345}
1346
1347/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001348 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 */
1350
1351/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001352 * Wait for any bufs with callbacks that have been submitted but have not yet
1353 * returned. These buffers will have an elevated hold count, so wait on those
1354 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 */
1356void
1357xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001358 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Dave Chinner430cbeb2010-12-02 16:30:55 +11001360 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Dave Chinner430cbeb2010-12-02 16:30:55 +11001362restart:
1363 spin_lock(&btp->bt_lru_lock);
1364 while (!list_empty(&btp->bt_lru)) {
1365 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1366 if (atomic_read(&bp->b_hold) > 1) {
1367 spin_unlock(&btp->bt_lru_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001368 delay(100);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001369 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001371 /*
Paul Bolle90802ed2011-12-05 13:00:34 +01001372 * clear the LRU reference count so the buffer doesn't get
Dave Chinner430cbeb2010-12-02 16:30:55 +11001373 * ignored in xfs_buf_rele().
1374 */
1375 atomic_set(&bp->b_lru_ref, 0);
1376 spin_unlock(&btp->bt_lru_lock);
1377 xfs_buf_rele(bp);
1378 spin_lock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001380 spin_unlock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
Dave Chinnerff57ab22010-11-30 17:27:57 +11001383int
1384xfs_buftarg_shrink(
1385 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001386 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001387{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001388 struct xfs_buftarg *btp = container_of(shrink,
1389 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001390 struct xfs_buf *bp;
Ying Han1495f232011-05-24 17:12:27 -07001391 int nr_to_scan = sc->nr_to_scan;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001392 LIST_HEAD(dispose);
1393
1394 if (!nr_to_scan)
1395 return btp->bt_lru_nr;
1396
1397 spin_lock(&btp->bt_lru_lock);
1398 while (!list_empty(&btp->bt_lru)) {
1399 if (nr_to_scan-- <= 0)
1400 break;
1401
1402 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1403
1404 /*
1405 * Decrement the b_lru_ref count unless the value is already
1406 * zero. If the value is already zero, we need to reclaim the
1407 * buffer, otherwise it gets another trip through the LRU.
1408 */
1409 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1410 list_move_tail(&bp->b_lru, &btp->bt_lru);
1411 continue;
1412 }
1413
1414 /*
1415 * remove the buffer from the LRU now to avoid needing another
1416 * lock round trip inside xfs_buf_rele().
1417 */
1418 list_move(&bp->b_lru, &dispose);
1419 btp->bt_lru_nr--;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001420 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001421 spin_unlock(&btp->bt_lru_lock);
1422
1423 while (!list_empty(&dispose)) {
1424 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1425 list_del_init(&bp->b_lru);
1426 xfs_buf_rele(bp);
1427 }
1428
1429 return btp->bt_lru_nr;
David Chinnera6867a62006-01-11 15:37:58 +11001430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432void
1433xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001434 struct xfs_mount *mp,
1435 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001437 unregister_shrinker(&btp->bt_shrinker);
1438
Christoph Hellwigb7963132009-03-03 14:48:37 -05001439 if (mp->m_flags & XFS_MOUNT_BARRIER)
1440 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001441
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001442 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445STATIC int
1446xfs_setsize_buftarg_flags(
1447 xfs_buftarg_t *btp,
1448 unsigned int blocksize,
1449 unsigned int sectorsize,
1450 int verbose)
1451{
Nathan Scottce8e9222006-01-11 15:39:08 +11001452 btp->bt_bsize = blocksize;
1453 btp->bt_sshift = ffs(sectorsize) - 1;
1454 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Nathan Scottce8e9222006-01-11 15:39:08 +11001456 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001457 char name[BDEVNAME_SIZE];
1458
1459 bdevname(btp->bt_bdev, name);
1460
Dave Chinner4f107002011-03-07 10:00:35 +11001461 xfs_warn(btp->bt_mount,
1462 "Cannot set_blocksize to %u on device %s\n",
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001463 sectorsize, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return EINVAL;
1465 }
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return 0;
1468}
1469
1470/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001471 * When allocating the initial buffer target we have not yet
1472 * read in the superblock, so don't know what sized sectors
1473 * are being used is at this early stage. Play safe.
1474 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475STATIC int
1476xfs_setsize_buftarg_early(
1477 xfs_buftarg_t *btp,
1478 struct block_device *bdev)
1479{
1480 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001481 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
1484int
1485xfs_setsize_buftarg(
1486 xfs_buftarg_t *btp,
1487 unsigned int blocksize,
1488 unsigned int sectorsize)
1489{
1490 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1491}
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493xfs_buftarg_t *
1494xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001495 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001497 int external,
1498 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 xfs_buftarg_t *btp;
1501
1502 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1503
Dave Chinnerebad8612010-09-22 10:47:20 +10001504 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001505 btp->bt_dev = bdev->bd_dev;
1506 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001507 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1508 if (!btp->bt_bdi)
1509 goto error;
1510
Dave Chinner430cbeb2010-12-02 16:30:55 +11001511 INIT_LIST_HEAD(&btp->bt_lru);
1512 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (xfs_setsize_buftarg_early(btp, bdev))
1514 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001515 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1516 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1517 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return btp;
1519
1520error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001521 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return NULL;
1523}
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001526 * Add a buffer to the delayed write list.
1527 *
1528 * This queues a buffer for writeout if it hasn't already been. Note that
1529 * neither this routine nor the buffer list submission functions perform
1530 * any internal synchronization. It is expected that the lists are thread-local
1531 * to the callers.
1532 *
1533 * Returns true if we queued up the buffer, or false if it already had
1534 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001536bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001537xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001538 struct xfs_buf *bp,
1539 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001541 ASSERT(xfs_buf_islocked(bp));
1542 ASSERT(!(bp->b_flags & XBF_READ));
1543
1544 /*
1545 * If the buffer is already marked delwri it already is queued up
1546 * by someone else for imediate writeout. Just ignore it in that
1547 * case.
1548 */
1549 if (bp->b_flags & _XBF_DELWRI_Q) {
1550 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1551 return false;
1552 }
David Chinnera6867a62006-01-11 15:37:58 +11001553
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001554 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1555
Dave Chinnerd808f612010-02-02 10:13:42 +11001556 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001557 * If a buffer gets written out synchronously or marked stale while it
1558 * is on a delwri list we lazily remove it. To do this, the other party
1559 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1560 * It remains referenced and on the list. In a rare corner case it
1561 * might get readded to a delwri list after the synchronous writeout, in
1562 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001563 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001564 bp->b_flags |= _XBF_DELWRI_Q;
1565 if (list_empty(&bp->b_list)) {
1566 atomic_inc(&bp->b_hold);
1567 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001568 }
David Chinner585e6d82007-02-10 18:32:29 +11001569
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001570 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001571}
1572
Dave Chinner089716a2010-01-26 15:13:25 +11001573/*
1574 * Compare function is more complex than it needs to be because
1575 * the return value is only 32 bits and we are doing comparisons
1576 * on 64 bit values
1577 */
1578static int
1579xfs_buf_cmp(
1580 void *priv,
1581 struct list_head *a,
1582 struct list_head *b)
1583{
1584 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1585 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1586 xfs_daddr_t diff;
1587
1588 diff = ap->b_bn - bp->b_bn;
1589 if (diff < 0)
1590 return -1;
1591 if (diff > 0)
1592 return 1;
1593 return 0;
1594}
1595
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001596static int
1597__xfs_buf_delwri_submit(
1598 struct list_head *buffer_list,
1599 struct list_head *io_list,
1600 bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001602 struct blk_plug plug;
1603 struct xfs_buf *bp, *n;
1604 int pinned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001606 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
1607 if (!wait) {
1608 if (xfs_buf_ispinned(bp)) {
1609 pinned++;
1610 continue;
1611 }
1612 if (!xfs_buf_trylock(bp))
1613 continue;
1614 } else {
1615 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001618 /*
1619 * Someone else might have written the buffer synchronously or
1620 * marked it stale in the meantime. In that case only the
1621 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1622 * reference and remove it from the list here.
1623 */
1624 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1625 list_del_init(&bp->b_list);
1626 xfs_buf_relse(bp);
1627 continue;
1628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001630 list_move_tail(&bp->b_list, io_list);
1631 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001634 list_sort(NULL, io_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001635
1636 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001637 list_for_each_entry_safe(bp, n, io_list, b_list) {
1638 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
1639 bp->b_flags |= XBF_WRITE;
1640
1641 if (!wait) {
1642 bp->b_flags |= XBF_ASYNC;
1643 list_del_init(&bp->b_list);
Dave Chinner089716a2010-01-26 15:13:25 +11001644 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001645 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001647 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001649 return pinned;
1650}
Nathan Scottf07c2252006-09-28 10:52:15 +10001651
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001652/*
1653 * Write out a buffer list asynchronously.
1654 *
1655 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1656 * out and not wait for I/O completion on any of the buffers. This interface
1657 * is only safely useable for callers that can track I/O completion by higher
1658 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1659 * function.
1660 */
1661int
1662xfs_buf_delwri_submit_nowait(
1663 struct list_head *buffer_list)
1664{
1665 LIST_HEAD (io_list);
1666 return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
1667}
1668
1669/*
1670 * Write out a buffer list synchronously.
1671 *
1672 * This will take the @buffer_list, write all buffers out and wait for I/O
1673 * completion on all of the buffers. @buffer_list is consumed by the function,
1674 * so callers must have some other way of tracking buffers if they require such
1675 * functionality.
1676 */
1677int
1678xfs_buf_delwri_submit(
1679 struct list_head *buffer_list)
1680{
1681 LIST_HEAD (io_list);
1682 int error = 0, error2;
1683 struct xfs_buf *bp;
1684
1685 __xfs_buf_delwri_submit(buffer_list, &io_list, true);
1686
1687 /* Wait for IO to complete. */
1688 while (!list_empty(&io_list)) {
1689 bp = list_first_entry(&io_list, struct xfs_buf, b_list);
1690
1691 list_del_init(&bp->b_list);
1692 error2 = xfs_buf_iowait(bp);
1693 xfs_buf_relse(bp);
1694 if (!error)
1695 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001698 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001701int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001702xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Nathan Scott87582802006-03-14 13:18:19 +11001704 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1705 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001706 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001707 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001708
Dave Chinner51749e42010-09-08 09:00:22 +00001709 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001710 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001711 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001712 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001716 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001717 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001718 out:
Nathan Scott87582802006-03-14 13:18:19 +11001719 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722void
Nathan Scottce8e9222006-01-11 15:39:08 +11001723xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001725 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001726 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}