blob: f463dce42515e61f40b9a99b8ef1cf4185e8819f [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 Scott204ab25f2006-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
Dave Chinnerbf813cd2012-04-23 15:58:48 +1000181 bp = kmem_zone_zalloc(xfs_buf_zone, xb_to_km(flags));
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000182 if (unlikely(!bp))
183 return NULL;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100186 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100188 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Nathan Scottce8e9222006-01-11 15:39:08 +1100190 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100191 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000192 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100193 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100194 INIT_LIST_HEAD(&bp->b_list);
Dave Chinner74f75a02010-09-24 19:59:04 +1000195 RB_CLEAR_NODE(&bp->b_rbnode);
Thomas Gleixnera731cd12010-09-07 14:33:15 +0000196 sema_init(&bp->b_sema, 0); /* held, no waiters */
Nathan Scottce8e9222006-01-11 15:39:08 +1100197 XB_SET_OWNER(bp);
198 bp->b_target = target;
199 bp->b_file_offset = range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /*
201 * Set buffer_length and count_desired to the same value initially.
202 * I/O routines should use count_desired, which will be the same in
203 * most cases but may be reset (e.g. XFS recovery).
204 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100205 bp->b_buffer_length = bp->b_count_desired = range_length;
206 bp->b_flags = flags;
207 bp->b_bn = XFS_BUF_DADDR_NULL;
208 atomic_set(&bp->b_pin_count, 0);
209 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Nathan Scottce8e9222006-01-11 15:39:08 +1100211 XFS_STATS_INC(xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000212 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000213
214 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100218 * Allocate a page array capable of holding a specified number
219 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
221STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100222_xfs_buf_get_pages(
223 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100225 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100228 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100229 bp->b_page_count = page_count;
230 if (page_count <= XB_PAGES) {
231 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100233 bp->b_pages = kmem_alloc(sizeof(struct page *) *
234 page_count, xb_to_km(flags));
235 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return -ENOMEM;
237 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100238 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240 return 0;
241}
242
243/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100244 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
246STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100247_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 xfs_buf_t *bp)
249{
Nathan Scottce8e9222006-01-11 15:39:08 +1100250 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000251 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000252 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254}
255
256/*
257 * Releases the specified buffer.
258 *
259 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100260 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * hashed and refcounted buffers
262 */
263void
Nathan Scottce8e9222006-01-11 15:39:08 +1100264xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 xfs_buf_t *bp)
266{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000267 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Dave Chinner430cbeb2010-12-02 16:30:55 +1100269 ASSERT(list_empty(&bp->b_lru));
270
Dave Chinner0e6e8472011-03-26 09:16:45 +1100271 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 uint i;
273
James Bottomley73c77e22010-01-25 11:42:24 -0600274 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000275 vm_unmap_ram(bp->b_addr - bp->b_offset,
276 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Nathan Scott948ecdb2006-09-28 11:03:13 +1000278 for (i = 0; i < bp->b_page_count; i++) {
279 struct page *page = bp->b_pages[i];
280
Dave Chinner0e6e8472011-03-26 09:16:45 +1100281 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000282 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100283 } else if (bp->b_flags & _XBF_KMEM)
284 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000285 _xfs_buf_free_pages(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000286 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100290 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
292STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100293xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 xfs_buf_t *bp,
295 uint flags)
296{
Nathan Scottce8e9222006-01-11 15:39:08 +1100297 size_t size = bp->b_count_desired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100299 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 unsigned short page_count, i;
Nathan Scott204ab25f2006-01-11 20:50:22 +1100301 xfs_off_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 int error;
303
Dave Chinner0e6e8472011-03-26 09:16:45 +1100304 /*
305 * for buffers that are contained within a single page, just allocate
306 * the memory from the heap - there's no need for the complexity of
307 * page arrays to keep allocation down to order 0.
308 */
309 if (bp->b_buffer_length < PAGE_SIZE) {
310 bp->b_addr = kmem_alloc(bp->b_buffer_length, xb_to_km(flags));
311 if (!bp->b_addr) {
312 /* low memory - use alloc_page loop instead */
313 goto use_alloc_page;
314 }
315
316 if (((unsigned long)(bp->b_addr + bp->b_buffer_length - 1) &
317 PAGE_MASK) !=
318 ((unsigned long)bp->b_addr & PAGE_MASK)) {
319 /* b_addr spans two pages - use alloc_page instead */
320 kmem_free(bp->b_addr);
321 bp->b_addr = NULL;
322 goto use_alloc_page;
323 }
324 bp->b_offset = offset_in_page(bp->b_addr);
325 bp->b_pages = bp->b_page_array;
326 bp->b_pages[0] = virt_to_page(bp->b_addr);
327 bp->b_page_count = 1;
328 bp->b_flags |= XBF_MAPPED | _XBF_KMEM;
329 return 0;
330 }
331
332use_alloc_page:
Nathan Scottce8e9222006-01-11 15:39:08 +1100333 end = bp->b_file_offset + bp->b_buffer_length;
334 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
Nathan Scottce8e9222006-01-11 15:39:08 +1100335 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (unlikely(error))
337 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Nathan Scottce8e9222006-01-11 15:39:08 +1100339 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100340 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Nathan Scottce8e9222006-01-11 15:39:08 +1100342 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct page *page;
344 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100345retry:
346 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100348 if (flags & XBF_READ_AHEAD) {
349 bp->b_page_count = i;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100350 error = ENOMEM;
351 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
354 /*
355 * This could deadlock.
356 *
357 * But until all the XFS lowlevel code is revamped to
358 * handle buffer allocation failures we can't do much.
359 */
360 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100361 xfs_err(NULL,
362 "possible memory allocation deadlock in %s (mode:0x%x)",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000363 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Nathan Scottce8e9222006-01-11 15:39:08 +1100365 XFS_STATS_INC(xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200366 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 goto retry;
368 }
369
Nathan Scottce8e9222006-01-11 15:39:08 +1100370 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Dave Chinner0e6e8472011-03-26 09:16:45 +1100372 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100374 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 offset = 0;
376 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Dave Chinner0e6e8472011-03-26 09:16:45 +1100379out_free_pages:
380 for (i = 0; i < bp->b_page_count; i++)
381 __free_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return error;
383}
384
385/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300386 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
388STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100389_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 xfs_buf_t *bp,
391 uint flags)
392{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100393 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100394 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100395 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100396 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
397 bp->b_flags |= XBF_MAPPED;
398 } else if (flags & XBF_MAPPED) {
Dave Chinnera19fb382011-03-26 09:13:42 +1100399 int retried = 0;
400
401 do {
402 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
403 -1, PAGE_KERNEL);
404 if (bp->b_addr)
405 break;
406 vm_unmap_aliases();
407 } while (retried++ <= 1);
408
409 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100411 bp->b_addr += bp->b_offset;
412 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 return 0;
416}
417
418/*
419 * Finding and Reading Buffers
420 */
421
422/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100423 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * a given range of an inode. The buffer is returned
Chandra Seetharamaneabbaf12011-09-08 20:18:50 +0000425 * locked. No I/O is implied by this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
427xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100428_xfs_buf_find(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 xfs_buftarg_t *btp, /* block device target */
Nathan Scott204ab25f2006-01-11 20:50:22 +1100430 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100432 xfs_buf_flags_t flags,
433 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Nathan Scott204ab25f2006-01-11 20:50:22 +1100435 xfs_off_t range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 size_t range_length;
Dave Chinner74f75a02010-09-24 19:59:04 +1000437 struct xfs_perag *pag;
438 struct rb_node **rbp;
439 struct rb_node *parent;
440 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 range_base = (ioff << BBSHIFT);
443 range_length = (isize << BBSHIFT);
444
445 /* Check for IOs smaller than the sector size / not sector aligned */
Nathan Scottce8e9222006-01-11 15:39:08 +1100446 ASSERT(!(range_length < (1 << btp->bt_sshift)));
Nathan Scott204ab25f2006-01-11 20:50:22 +1100447 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Dave Chinner74f75a02010-09-24 19:59:04 +1000449 /* get tree root */
450 pag = xfs_perag_get(btp->bt_mount,
451 xfs_daddr_to_agno(btp->bt_mount, ioff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Dave Chinner74f75a02010-09-24 19:59:04 +1000453 /* walk tree */
454 spin_lock(&pag->pag_buf_lock);
455 rbp = &pag->pag_buf_tree.rb_node;
456 parent = NULL;
457 bp = NULL;
458 while (*rbp) {
459 parent = *rbp;
460 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Dave Chinner74f75a02010-09-24 19:59:04 +1000462 if (range_base < bp->b_file_offset)
463 rbp = &(*rbp)->rb_left;
464 else if (range_base > bp->b_file_offset)
465 rbp = &(*rbp)->rb_right;
466 else {
467 /*
468 * found a block offset match. If the range doesn't
469 * match, the only way this is allowed is if the buffer
470 * in the cache is stale and the transaction that made
471 * it stale has not yet committed. i.e. we are
472 * reallocating a busy extent. Skip this buffer and
473 * continue searching to the right for an exact match.
474 */
475 if (bp->b_buffer_length != range_length) {
476 ASSERT(bp->b_flags & XBF_STALE);
477 rbp = &(*rbp)->rb_right;
478 continue;
479 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100480 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 goto found;
482 }
483 }
484
485 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100486 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000487 rb_link_node(&new_bp->b_rbnode, parent, rbp);
488 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
489 /* the buffer keeps the perag reference until it is freed */
490 new_bp->b_pag = pag;
491 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100493 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000494 spin_unlock(&pag->pag_buf_lock);
495 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100497 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000500 spin_unlock(&pag->pag_buf_lock);
501 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200503 if (!xfs_buf_trylock(bp)) {
504 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100505 xfs_buf_rele(bp);
506 XFS_STATS_INC(xb_busy_locked);
507 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200509 xfs_buf_lock(bp);
510 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
Dave Chinner0e6e8472011-03-26 09:16:45 +1100513 /*
514 * if the buffer is stale, clear all the external state associated with
515 * it. We need to keep flags such as how we allocated the buffer memory
516 * intact here.
517 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100518 if (bp->b_flags & XBF_STALE) {
519 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100520 bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES;
David Chinner2f926582005-09-05 08:33:35 +1000521 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000522
523 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100524 XFS_STATS_INC(xb_get_locked);
525 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528/*
Dave Chinner38158322011-09-30 04:45:02 +0000529 * Assembles a buffer covering the specified range. The code is optimised for
530 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
531 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
Dave Chinner38158322011-09-30 04:45:02 +0000533struct xfs_buf *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000534xfs_buf_get(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab25f2006-01-11 20:50:22 +1100536 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100538 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Dave Chinner38158322011-09-30 04:45:02 +0000540 struct xfs_buf *bp;
541 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100542 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Dave Chinner38158322011-09-30 04:45:02 +0000544 bp = _xfs_buf_find(target, ioff, isize, flags, NULL);
545 if (likely(bp))
546 goto found;
547
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000548 new_bp = xfs_buf_alloc(target, ioff << BBSHIFT, isize << BBSHIFT,
549 flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100550 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return NULL;
552
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000553 error = xfs_buf_allocate_memory(new_bp, flags);
554 if (error) {
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000555 kmem_zone_free(xfs_buf_zone, new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000556 return NULL;
557 }
558
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000559 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
560 if (!bp) {
561 xfs_buf_free(new_bp);
562 return NULL;
563 }
564
565 if (bp != new_bp)
566 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Dave Chinner38158322011-09-30 04:45:02 +0000568 /*
569 * Now we have a workable buffer, fill in the block number so
570 * that we can do IO on it.
571 */
572 bp->b_bn = ioff;
573 bp->b_count_desired = bp->b_buffer_length;
574
575found:
Nathan Scottce8e9222006-01-11 15:39:08 +1100576 if (!(bp->b_flags & XBF_MAPPED)) {
577 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100579 xfs_warn(target->bt_mount,
580 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto no_buffer;
582 }
583 }
584
Nathan Scottce8e9222006-01-11 15:39:08 +1100585 XFS_STATS_INC(xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000586 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100587 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Dave Chinner38158322011-09-30 04:45:02 +0000589no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100590 if (flags & (XBF_LOCK | XBF_TRYLOCK))
591 xfs_buf_unlock(bp);
592 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return NULL;
594}
595
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100596STATIC int
597_xfs_buf_read(
598 xfs_buf_t *bp,
599 xfs_buf_flags_t flags)
600{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000601 ASSERT(!(flags & XBF_WRITE));
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100602 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
603
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000604 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200605 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100606
Dave Chinner0e95f192012-04-23 15:58:46 +1000607 xfs_buf_iorequest(bp);
608 if (flags & XBF_ASYNC)
609 return 0;
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000610 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100611}
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000614xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 xfs_buftarg_t *target,
Nathan Scott204ab25f2006-01-11 20:50:22 +1100616 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100618 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Nathan Scottce8e9222006-01-11 15:39:08 +1100620 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Nathan Scottce8e9222006-01-11 15:39:08 +1100622 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000624 bp = xfs_buf_get(target, ioff, isize, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100625 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000626 trace_xfs_buf_read(bp, flags, _RET_IP_);
627
Nathan Scottce8e9222006-01-11 15:39:08 +1100628 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100629 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100630 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100631 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * Read ahead call which is already satisfied,
634 * drop the buffer
635 */
636 goto no_buffer;
637 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100639 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 }
642
Nathan Scottce8e9222006-01-11 15:39:08 +1100643 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100646 if (flags & (XBF_LOCK | XBF_TRYLOCK))
647 xfs_buf_unlock(bp);
648 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return NULL;
650}
651
652/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100653 * If we are not low on memory then do the readahead in a deadlock
654 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 */
656void
Nathan Scottce8e9222006-01-11 15:39:08 +1100657xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 xfs_buftarg_t *target,
Nathan Scott204ab25f2006-01-11 20:50:22 +1100659 xfs_off_t ioff,
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000660 size_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100662 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return;
664
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000665 xfs_buf_read(target, ioff, isize,
666 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Dave Chinner5adc94c2010-09-24 21:58:31 +1000669/*
670 * Read an uncached buffer from disk. Allocates and returns a locked
671 * buffer containing the disk contents or nothing.
672 */
673struct xfs_buf *
674xfs_buf_read_uncached(
675 struct xfs_mount *mp,
676 struct xfs_buftarg *target,
677 xfs_daddr_t daddr,
678 size_t length,
679 int flags)
680{
681 xfs_buf_t *bp;
682 int error;
683
684 bp = xfs_buf_get_uncached(target, length, flags);
685 if (!bp)
686 return NULL;
687
688 /* set up the buffer for a read IO */
Dave Chinner5adc94c2010-09-24 21:58:31 +1000689 XFS_BUF_SET_ADDR(bp, daddr);
690 XFS_BUF_READ(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000691
692 xfsbdstrat(mp, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000693 error = xfs_buf_iowait(bp);
Dave Chinner0e95f192012-04-23 15:58:46 +1000694 if (error) {
Dave Chinner5adc94c2010-09-24 21:58:31 +1000695 xfs_buf_relse(bp);
696 return NULL;
697 }
698 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Dave Chinner44396472011-04-21 09:34:27 +0000701/*
702 * Return a buffer allocated as an empty buffer and associated to external
703 * memory via xfs_buf_associate_memory() back to it's empty state.
704 */
705void
706xfs_buf_set_empty(
707 struct xfs_buf *bp,
708 size_t len)
709{
710 if (bp->b_pages)
711 _xfs_buf_free_pages(bp);
712
713 bp->b_pages = NULL;
714 bp->b_page_count = 0;
715 bp->b_addr = NULL;
716 bp->b_file_offset = 0;
717 bp->b_buffer_length = bp->b_count_desired = len;
718 bp->b_bn = XFS_BUF_DADDR_NULL;
719 bp->b_flags &= ~XBF_MAPPED;
720}
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722static inline struct page *
723mem_to_page(
724 void *addr)
725{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800726 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return virt_to_page(addr);
728 } else {
729 return vmalloc_to_page(addr);
730 }
731}
732
733int
Nathan Scottce8e9222006-01-11 15:39:08 +1100734xfs_buf_associate_memory(
735 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 void *mem,
737 size_t len)
738{
739 int rval;
740 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100741 unsigned long pageaddr;
742 unsigned long offset;
743 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 int page_count;
745
Dave Chinner0e6e8472011-03-26 09:16:45 +1100746 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100747 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100748 buflen = PAGE_ALIGN(len + offset);
749 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100752 if (bp->b_pages)
753 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Nathan Scottce8e9222006-01-11 15:39:08 +1100755 bp->b_pages = NULL;
756 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Christoph Hellwig36fae172009-07-18 18:14:58 -0400758 rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (rval)
760 return rval;
761
Nathan Scottce8e9222006-01-11 15:39:08 +1100762 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100764 for (i = 0; i < bp->b_page_count; i++) {
765 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100766 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100769 bp->b_count_desired = len;
770 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100771 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 return 0;
774}
775
776xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000777xfs_buf_get_uncached(
778 struct xfs_buftarg *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 size_t len,
Dave Chinner686865f2010-09-24 20:07:47 +1000780 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000782 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
783 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000786 bp = xfs_buf_alloc(target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (unlikely(bp == NULL))
788 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000790 error = _xfs_buf_get_pages(bp, page_count, 0);
791 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 goto fail_free_buf;
793
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000794 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000795 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000796 if (!bp->b_pages[i])
797 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000799 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000801 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
802 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100803 xfs_warn(target->bt_mount,
804 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Dave Chinner686865f2010-09-24 20:07:47 +1000808 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000812 while (--i >= 0)
813 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000814 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 fail_free_buf:
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000816 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 fail:
818 return NULL;
819}
820
821/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 * Increment reference count on buffer, to hold the buffer concurrently
823 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 * Must hold the buffer already to call this function.
825 */
826void
Nathan Scottce8e9222006-01-11 15:39:08 +1100827xfs_buf_hold(
828 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000830 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100831 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100835 * Releases a hold on the specified buffer. If the
836 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 */
838void
Nathan Scottce8e9222006-01-11 15:39:08 +1100839xfs_buf_rele(
840 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Dave Chinner74f75a02010-09-24 19:59:04 +1000842 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000844 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Dave Chinner74f75a02010-09-24 19:59:04 +1000846 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100847 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000848 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100849 if (atomic_dec_and_test(&bp->b_hold))
850 xfs_buf_free(bp);
851 return;
852 }
853
Dave Chinner74f75a02010-09-24 19:59:04 +1000854 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100855
Lachlan McIlroy37906892008-08-13 15:42:10 +1000856 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000857 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000858 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100859 atomic_read(&bp->b_lru_ref)) {
860 xfs_buf_lru_add(bp);
861 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100862 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100863 xfs_buf_lru_del(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000864 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner74f75a02010-09-24 19:59:04 +1000865 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
866 spin_unlock(&pag->pag_buf_lock);
867 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100868 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 }
871}
872
873
874/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100875 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100876 *
877 * If we come across a stale, pinned, locked buffer, we know that we are
878 * being asked to lock a buffer that has been reallocated. Because it is
879 * pinned, we know that the log has not been pushed to disk and hence it
880 * will still be locked. Rather than continuing to have trylock attempts
881 * fail until someone else pushes the log, push it ourselves before
882 * returning. This means that the xfsaild will not get stuck trying
883 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 */
885int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200886xfs_buf_trylock(
887 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 int locked;
890
Nathan Scottce8e9222006-01-11 15:39:08 +1100891 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000892 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100893 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100894 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
895 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000896
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200897 trace_xfs_buf_trylock(bp, _RET_IP_);
898 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100902 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000903 *
904 * If we come across a stale, pinned, locked buffer, we know that we
905 * are being asked to lock a buffer that has been reallocated. Because
906 * it is pinned, we know that the log has not been pushed to disk and
907 * hence it will still be locked. Rather than sleeping until someone
908 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100910void
911xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200912 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000914 trace_xfs_buf_lock(bp, _RET_IP_);
915
Dave Chinnered3b4d62010-05-21 12:07:08 +1000916 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000917 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100918 down(&bp->b_sema);
919 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000920
921 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924void
Nathan Scottce8e9222006-01-11 15:39:08 +1100925xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200926 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Nathan Scottce8e9222006-01-11 15:39:08 +1100928 XB_CLEAR_OWNER(bp);
929 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000930
931 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Nathan Scottce8e9222006-01-11 15:39:08 +1100934STATIC void
935xfs_buf_wait_unpin(
936 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 DECLARE_WAITQUEUE (wait, current);
939
Nathan Scottce8e9222006-01-11 15:39:08 +1100940 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return;
942
Nathan Scottce8e9222006-01-11 15:39:08 +1100943 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 for (;;) {
945 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100946 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100948 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100950 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 set_current_state(TASK_RUNNING);
952}
953
954/*
955 * Buffer Utility Routines
956 */
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100959xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000960 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
David Howellsc4028952006-11-22 14:57:56 +0000962 xfs_buf_t *bp =
963 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400965 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100966 (*(bp->b_iodone))(bp);
967 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 xfs_buf_relse(bp);
969}
970
971void
Nathan Scottce8e9222006-01-11 15:39:08 +1100972xfs_buf_ioend(
973 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 int schedule)
975{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000976 trace_xfs_buf_iodone(bp, _RET_IP_);
977
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100978 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100979 if (bp->b_error == 0)
980 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Nathan Scottce8e9222006-01-11 15:39:08 +1100982 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000984 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100985 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 } else {
David Howellsc4028952006-11-22 14:57:56 +0000987 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +1000990 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994void
Nathan Scottce8e9222006-01-11 15:39:08 +1100995xfs_buf_ioerror(
996 xfs_buf_t *bp,
997 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001000 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001001 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Christoph Hellwig901796a2011-10-10 16:52:49 +00001004void
1005xfs_buf_ioerror_alert(
1006 struct xfs_buf *bp,
1007 const char *func)
1008{
1009 xfs_alert(bp->b_target->bt_mount,
1010"metadata I/O error: block 0x%llx (\"%s\") error %d buf count %zd",
1011 (__uint64_t)XFS_BUF_ADDR(bp), func,
1012 bp->b_error, XFS_BUF_COUNT(bp));
1013}
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001016xfs_bwrite(
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001017 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Christoph Hellwig8c383662010-03-12 10:59:40 +00001019 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001021 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001023 bp->b_flags |= XBF_WRITE;
1024 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
1025
Christoph Hellwig939d7232010-07-20 17:51:16 +10001026 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Christoph Hellwig8c383662010-03-12 10:59:40 +00001028 error = xfs_buf_iowait(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001029 if (error) {
1030 xfs_force_shutdown(bp->b_target->bt_mount,
1031 SHUTDOWN_META_IO_ERROR);
1032 }
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001033 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001034}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Christoph Hellwig4e234712010-01-13 22:17:56 +00001036/*
1037 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001038 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001039 * so that the proper iodone callbacks get called.
1040 */
1041STATIC int
1042xfs_bioerror(
1043 xfs_buf_t *bp)
1044{
1045#ifdef XFSERRORDEBUG
1046 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1047#endif
1048
1049 /*
1050 * No need to wait until the buffer is unpinned, we aren't flushing it.
1051 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001052 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001053
1054 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001055 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001056 */
1057 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001058 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001059 xfs_buf_stale(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001060
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001061 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001062
1063 return EIO;
1064}
1065
1066/*
1067 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001068 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001069 * This is meant for userdata errors; metadata bufs come with
1070 * iodone functions attached, so that we can track down errors.
1071 */
1072STATIC int
1073xfs_bioerror_relse(
1074 struct xfs_buf *bp)
1075{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001076 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001077 /*
1078 * No need to wait until the buffer is unpinned.
1079 * We aren't flushing it.
1080 *
1081 * chunkhold expects B_DONE to be set, whether
1082 * we actually finish the I/O or not. We don't want to
1083 * change that interface.
1084 */
1085 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001086 XFS_BUF_DONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001087 xfs_buf_stale(bp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001088 bp->b_iodone = NULL;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001089 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001090 /*
1091 * Mark b_error and B_ERROR _both_.
1092 * Lot's of chunkcache code assumes that.
1093 * There's no reason to mark error for
1094 * ASYNC buffers.
1095 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001096 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig5fde0322011-10-10 16:52:44 +00001097 complete(&bp->b_iowait);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001098 } else {
1099 xfs_buf_relse(bp);
1100 }
1101
1102 return EIO;
1103}
1104
1105
1106/*
1107 * All xfs metadata buffers except log state machine buffers
1108 * get this attached as their b_bdstrat callback function.
1109 * This is so that we can catch a buffer
1110 * after prematurely unpinning it to forcibly shutdown the filesystem.
1111 */
1112int
1113xfs_bdstrat_cb(
1114 struct xfs_buf *bp)
1115{
Dave Chinnerebad8612010-09-22 10:47:20 +10001116 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001117 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1118 /*
1119 * Metadata write that didn't get logged but
1120 * written delayed anyway. These aren't associated
1121 * with a transaction, and can be ignored.
1122 */
1123 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1124 return xfs_bioerror_relse(bp);
1125 else
1126 return xfs_bioerror(bp);
1127 }
1128
1129 xfs_buf_iorequest(bp);
1130 return 0;
1131}
1132
1133/*
1134 * Wrapper around bdstrat so that we can stop data from going to disk in case
1135 * we are shutting down the filesystem. Typically user data goes thru this
1136 * path; one of the exceptions is the superblock.
1137 */
1138void
1139xfsbdstrat(
1140 struct xfs_mount *mp,
1141 struct xfs_buf *bp)
1142{
1143 if (XFS_FORCED_SHUTDOWN(mp)) {
1144 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1145 xfs_bioerror_relse(bp);
1146 return;
1147 }
1148
1149 xfs_buf_iorequest(bp);
1150}
1151
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001152STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001153_xfs_buf_ioend(
1154 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 int schedule)
1156{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001157 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001158 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
Al Viro782e3b32007-10-12 07:17:47 +01001161STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001162xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 int error)
1165{
Nathan Scottce8e9222006-01-11 15:39:08 +11001166 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001168 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
James Bottomley73c77e22010-01-25 11:42:24 -06001170 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1171 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1172
Nathan Scottce8e9222006-01-11 15:39:08 +11001173 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001178_xfs_buf_ioapply(
1179 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001181 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001183 int offset = bp->b_offset;
1184 int size = bp->b_count_desired;
1185 sector_t sector = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Nathan Scottce8e9222006-01-11 15:39:08 +11001187 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 map_i = 0;
1189
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001190 if (bp->b_flags & XBF_WRITE) {
1191 if (bp->b_flags & XBF_SYNCIO)
1192 rw = WRITE_SYNC;
1193 else
1194 rw = WRITE;
1195 if (bp->b_flags & XBF_FUA)
1196 rw |= REQ_FUA;
1197 if (bp->b_flags & XBF_FLUSH)
1198 rw |= REQ_FLUSH;
1199 } else if (bp->b_flags & XBF_READ_AHEAD) {
1200 rw = READA;
Nathan Scott51bdd702006-09-28 11:01:57 +10001201 } else {
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001202 rw = READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001203 }
1204
Christoph Hellwig34951f52011-07-26 15:06:44 +00001205 /* we only use the buffer cache for meta-data */
1206 rw |= REQ_META;
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001209 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1211 if (nr_pages > total_nr_pages)
1212 nr_pages = total_nr_pages;
1213
1214 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001215 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001217 bio->bi_end_io = xfs_buf_bio_end_io;
1218 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Dave Chinner0e6e8472011-03-26 09:16:45 +11001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 for (; size && nr_pages; nr_pages--, map_i++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001222 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 if (nbytes > size)
1225 nbytes = size;
1226
Nathan Scottce8e9222006-01-11 15:39:08 +11001227 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1228 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 break;
1230
1231 offset = 0;
1232 sector += nbytes >> BBSHIFT;
1233 size -= nbytes;
1234 total_nr_pages--;
1235 }
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001238 if (xfs_buf_is_vmapped(bp)) {
1239 flush_kernel_vmap_range(bp->b_addr,
1240 xfs_buf_vmap_len(bp));
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 submit_bio(rw, bio);
1243 if (size)
1244 goto next_chunk;
1245 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001246 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001247 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249}
1250
Dave Chinner0e95f192012-04-23 15:58:46 +10001251void
Nathan Scottce8e9222006-01-11 15:39:08 +11001252xfs_buf_iorequest(
1253 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001255 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001257 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Christoph Hellwig375ec692011-08-23 08:28:03 +00001259 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001260 xfs_buf_wait_unpin(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001261 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 /* Set the count to 1 initially, this will stop an I/O
1264 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001265 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001267 atomic_set(&bp->b_io_remaining, 1);
1268 _xfs_buf_ioapply(bp);
1269 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Nathan Scottce8e9222006-01-11 15:39:08 +11001271 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
1274/*
Dave Chinner0e95f192012-04-23 15:58:46 +10001275 * Waits for I/O to complete on the buffer supplied. It returns immediately if
1276 * no I/O is pending or there is already a pending error on the buffer. It
1277 * returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 */
1279int
Nathan Scottce8e9222006-01-11 15:39:08 +11001280xfs_buf_iowait(
1281 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001283 trace_xfs_buf_iowait(bp, _RET_IP_);
1284
Dave Chinner0e95f192012-04-23 15:58:46 +10001285 if (!bp->b_error)
1286 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001287
1288 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001289 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Nathan Scottce8e9222006-01-11 15:39:08 +11001292xfs_caddr_t
1293xfs_buf_offset(
1294 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 size_t offset)
1296{
1297 struct page *page;
1298
Nathan Scottce8e9222006-01-11 15:39:08 +11001299 if (bp->b_flags & XBF_MAPPED)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001300 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Nathan Scottce8e9222006-01-11 15:39:08 +11001302 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001303 page = bp->b_pages[offset >> PAGE_SHIFT];
1304 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
1307/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 * Move data into or out of a buffer.
1309 */
1310void
Nathan Scottce8e9222006-01-11 15:39:08 +11001311xfs_buf_iomove(
1312 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 size_t boff, /* starting buffer offset */
1314 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001315 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001316 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 size_t bend, cpoff, csize;
1319 struct page *page;
1320
1321 bend = boff + bsize;
1322 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001323 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1324 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 csize = min_t(size_t,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001326 PAGE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Dave Chinner0e6e8472011-03-26 09:16:45 +11001328 ASSERT(((csize + cpoff) <= PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001331 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 memset(page_address(page) + cpoff, 0, csize);
1333 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001334 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 memcpy(data, page_address(page) + cpoff, csize);
1336 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001337 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 memcpy(page_address(page) + cpoff, data, csize);
1339 }
1340
1341 boff += csize;
1342 data += csize;
1343 }
1344}
1345
1346/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001347 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 */
1349
1350/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001351 * Wait for any bufs with callbacks that have been submitted but have not yet
1352 * returned. These buffers will have an elevated hold count, so wait on those
1353 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 */
1355void
1356xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001357 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Dave Chinner430cbeb2010-12-02 16:30:55 +11001359 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Dave Chinner430cbeb2010-12-02 16:30:55 +11001361restart:
1362 spin_lock(&btp->bt_lru_lock);
1363 while (!list_empty(&btp->bt_lru)) {
1364 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1365 if (atomic_read(&bp->b_hold) > 1) {
1366 spin_unlock(&btp->bt_lru_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001367 delay(100);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001368 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001370 /*
Paul Bolle90802ed2011-12-05 13:00:34 +01001371 * clear the LRU reference count so the buffer doesn't get
Dave Chinner430cbeb2010-12-02 16:30:55 +11001372 * ignored in xfs_buf_rele().
1373 */
1374 atomic_set(&bp->b_lru_ref, 0);
1375 spin_unlock(&btp->bt_lru_lock);
1376 xfs_buf_rele(bp);
1377 spin_lock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001379 spin_unlock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
Dave Chinnerff57ab22010-11-30 17:27:57 +11001382int
1383xfs_buftarg_shrink(
1384 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001385 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001386{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001387 struct xfs_buftarg *btp = container_of(shrink,
1388 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001389 struct xfs_buf *bp;
Ying Han1495f232011-05-24 17:12:27 -07001390 int nr_to_scan = sc->nr_to_scan;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001391 LIST_HEAD(dispose);
1392
1393 if (!nr_to_scan)
1394 return btp->bt_lru_nr;
1395
1396 spin_lock(&btp->bt_lru_lock);
1397 while (!list_empty(&btp->bt_lru)) {
1398 if (nr_to_scan-- <= 0)
1399 break;
1400
1401 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1402
1403 /*
1404 * Decrement the b_lru_ref count unless the value is already
1405 * zero. If the value is already zero, we need to reclaim the
1406 * buffer, otherwise it gets another trip through the LRU.
1407 */
1408 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1409 list_move_tail(&bp->b_lru, &btp->bt_lru);
1410 continue;
1411 }
1412
1413 /*
1414 * remove the buffer from the LRU now to avoid needing another
1415 * lock round trip inside xfs_buf_rele().
1416 */
1417 list_move(&bp->b_lru, &dispose);
1418 btp->bt_lru_nr--;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001419 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001420 spin_unlock(&btp->bt_lru_lock);
1421
1422 while (!list_empty(&dispose)) {
1423 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1424 list_del_init(&bp->b_lru);
1425 xfs_buf_rele(bp);
1426 }
1427
1428 return btp->bt_lru_nr;
David Chinnera6867a62006-01-11 15:37:58 +11001429}
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431void
1432xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001433 struct xfs_mount *mp,
1434 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001436 unregister_shrinker(&btp->bt_shrinker);
1437
Christoph Hellwigb7963132009-03-03 14:48:37 -05001438 if (mp->m_flags & XFS_MOUNT_BARRIER)
1439 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001440
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001441 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444STATIC int
1445xfs_setsize_buftarg_flags(
1446 xfs_buftarg_t *btp,
1447 unsigned int blocksize,
1448 unsigned int sectorsize,
1449 int verbose)
1450{
Nathan Scottce8e9222006-01-11 15:39:08 +11001451 btp->bt_bsize = blocksize;
1452 btp->bt_sshift = ffs(sectorsize) - 1;
1453 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Nathan Scottce8e9222006-01-11 15:39:08 +11001455 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001456 char name[BDEVNAME_SIZE];
1457
1458 bdevname(btp->bt_bdev, name);
1459
Dave Chinner4f107002011-03-07 10:00:35 +11001460 xfs_warn(btp->bt_mount,
1461 "Cannot set_blocksize to %u on device %s\n",
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001462 sectorsize, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return EINVAL;
1464 }
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return 0;
1467}
1468
1469/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001470 * When allocating the initial buffer target we have not yet
1471 * read in the superblock, so don't know what sized sectors
1472 * are being used is at this early stage. Play safe.
1473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474STATIC int
1475xfs_setsize_buftarg_early(
1476 xfs_buftarg_t *btp,
1477 struct block_device *bdev)
1478{
1479 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001480 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
1483int
1484xfs_setsize_buftarg(
1485 xfs_buftarg_t *btp,
1486 unsigned int blocksize,
1487 unsigned int sectorsize)
1488{
1489 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1490}
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492xfs_buftarg_t *
1493xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001494 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001496 int external,
1497 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 xfs_buftarg_t *btp;
1500
1501 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1502
Dave Chinnerebad8612010-09-22 10:47:20 +10001503 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001504 btp->bt_dev = bdev->bd_dev;
1505 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001506 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1507 if (!btp->bt_bdi)
1508 goto error;
1509
Dave Chinner430cbeb2010-12-02 16:30:55 +11001510 INIT_LIST_HEAD(&btp->bt_lru);
1511 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (xfs_setsize_buftarg_early(btp, bdev))
1513 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001514 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1515 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1516 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return btp;
1518
1519error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001520 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return NULL;
1522}
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001525 * Add a buffer to the delayed write list.
1526 *
1527 * This queues a buffer for writeout if it hasn't already been. Note that
1528 * neither this routine nor the buffer list submission functions perform
1529 * any internal synchronization. It is expected that the lists are thread-local
1530 * to the callers.
1531 *
1532 * Returns true if we queued up the buffer, or false if it already had
1533 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001535bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001536xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001537 struct xfs_buf *bp,
1538 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001540 ASSERT(xfs_buf_islocked(bp));
1541 ASSERT(!(bp->b_flags & XBF_READ));
1542
1543 /*
1544 * If the buffer is already marked delwri it already is queued up
1545 * by someone else for imediate writeout. Just ignore it in that
1546 * case.
1547 */
1548 if (bp->b_flags & _XBF_DELWRI_Q) {
1549 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1550 return false;
1551 }
David Chinnera6867a62006-01-11 15:37:58 +11001552
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001553 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1554
Dave Chinnerd808f612010-02-02 10:13:42 +11001555 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001556 * If a buffer gets written out synchronously or marked stale while it
1557 * is on a delwri list we lazily remove it. To do this, the other party
1558 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1559 * It remains referenced and on the list. In a rare corner case it
1560 * might get readded to a delwri list after the synchronous writeout, in
1561 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001562 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001563 bp->b_flags |= _XBF_DELWRI_Q;
1564 if (list_empty(&bp->b_list)) {
1565 atomic_inc(&bp->b_hold);
1566 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001567 }
David Chinner585e6d82007-02-10 18:32:29 +11001568
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001569 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001570}
1571
Dave Chinner089716a2010-01-26 15:13:25 +11001572/*
1573 * Compare function is more complex than it needs to be because
1574 * the return value is only 32 bits and we are doing comparisons
1575 * on 64 bit values
1576 */
1577static int
1578xfs_buf_cmp(
1579 void *priv,
1580 struct list_head *a,
1581 struct list_head *b)
1582{
1583 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1584 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1585 xfs_daddr_t diff;
1586
1587 diff = ap->b_bn - bp->b_bn;
1588 if (diff < 0)
1589 return -1;
1590 if (diff > 0)
1591 return 1;
1592 return 0;
1593}
1594
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001595static int
1596__xfs_buf_delwri_submit(
1597 struct list_head *buffer_list,
1598 struct list_head *io_list,
1599 bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001601 struct blk_plug plug;
1602 struct xfs_buf *bp, *n;
1603 int pinned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001605 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
1606 if (!wait) {
1607 if (xfs_buf_ispinned(bp)) {
1608 pinned++;
1609 continue;
1610 }
1611 if (!xfs_buf_trylock(bp))
1612 continue;
1613 } else {
1614 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001617 /*
1618 * Someone else might have written the buffer synchronously or
1619 * marked it stale in the meantime. In that case only the
1620 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1621 * reference and remove it from the list here.
1622 */
1623 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1624 list_del_init(&bp->b_list);
1625 xfs_buf_relse(bp);
1626 continue;
1627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001629 list_move_tail(&bp->b_list, io_list);
1630 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001633 list_sort(NULL, io_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001634
1635 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001636 list_for_each_entry_safe(bp, n, io_list, b_list) {
1637 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
1638 bp->b_flags |= XBF_WRITE;
1639
1640 if (!wait) {
1641 bp->b_flags |= XBF_ASYNC;
1642 list_del_init(&bp->b_list);
Dave Chinner089716a2010-01-26 15:13:25 +11001643 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001644 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001646 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001648 return pinned;
1649}
Nathan Scottf07c2252006-09-28 10:52:15 +10001650
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001651/*
1652 * Write out a buffer list asynchronously.
1653 *
1654 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1655 * out and not wait for I/O completion on any of the buffers. This interface
1656 * is only safely useable for callers that can track I/O completion by higher
1657 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1658 * function.
1659 */
1660int
1661xfs_buf_delwri_submit_nowait(
1662 struct list_head *buffer_list)
1663{
1664 LIST_HEAD (io_list);
1665 return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
1666}
1667
1668/*
1669 * Write out a buffer list synchronously.
1670 *
1671 * This will take the @buffer_list, write all buffers out and wait for I/O
1672 * completion on all of the buffers. @buffer_list is consumed by the function,
1673 * so callers must have some other way of tracking buffers if they require such
1674 * functionality.
1675 */
1676int
1677xfs_buf_delwri_submit(
1678 struct list_head *buffer_list)
1679{
1680 LIST_HEAD (io_list);
1681 int error = 0, error2;
1682 struct xfs_buf *bp;
1683
1684 __xfs_buf_delwri_submit(buffer_list, &io_list, true);
1685
1686 /* Wait for IO to complete. */
1687 while (!list_empty(&io_list)) {
1688 bp = list_first_entry(&io_list, struct xfs_buf, b_list);
1689
1690 list_del_init(&bp->b_list);
1691 error2 = xfs_buf_iowait(bp);
1692 xfs_buf_relse(bp);
1693 if (!error)
1694 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001697 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001700int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001701xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Nathan Scott87582802006-03-14 13:18:19 +11001703 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1704 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001705 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001706 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001707
Dave Chinner51749e42010-09-08 09:00:22 +00001708 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001709 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001710 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001711 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001715 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001716 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001717 out:
Nathan Scott87582802006-03-14 13:18:19 +11001718 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721void
Nathan Scottce8e9222006-01-11 15:39:08 +11001722xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001724 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001725 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}