blob: a843873b0954591aafbcc37b1ba12be46856edca [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"
Dave Chinnered3b4d62010-05-21 12:07:08 +100038#include "xfs_log.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050039#include "xfs_ag.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050040#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000041#include "xfs_trace.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050042
David Chinner7989cb82007-02-10 18:34:56 +110043static kmem_zone_t *xfs_buf_zone;
Christoph Hellwig23ea4032005-06-21 15:14:01 +100044
David Chinner7989cb82007-02-10 18:34:56 +110045static struct workqueue_struct *xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Nathan Scottce8e9222006-01-11 15:39:08 +110047#ifdef XFS_BUF_LOCK_TRACKING
48# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
49# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
50# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#else
Nathan Scottce8e9222006-01-11 15:39:08 +110052# define XB_SET_OWNER(bp) do { } while (0)
53# define XB_CLEAR_OWNER(bp) do { } while (0)
54# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#endif
56
Nathan Scottce8e9222006-01-11 15:39:08 +110057#define xb_to_gfp(flags) \
Dave Chinneraa5c1582012-04-23 15:58:56 +100058 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
James Bottomley73c77e22010-01-25 11:42:24 -060061static inline int
62xfs_buf_is_vmapped(
63 struct xfs_buf *bp)
64{
65 /*
66 * Return true if the buffer is vmapped.
67 *
Dave Chinner611c9942012-04-23 15:59:07 +100068 * b_addr is null if the buffer is not mapped, but the code is clever
69 * enough to know it doesn't have to map a single page, so the check has
70 * to be both for b_addr and bp->b_page_count > 1.
James Bottomley73c77e22010-01-25 11:42:24 -060071 */
Dave Chinner611c9942012-04-23 15:59:07 +100072 return bp->b_addr && bp->b_page_count > 1;
James Bottomley73c77e22010-01-25 11:42:24 -060073}
74
75static inline int
76xfs_buf_vmap_len(
77 struct xfs_buf *bp)
78{
79 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
Dave Chinner430cbeb2010-12-02 16:30:55 +110083 * xfs_buf_lru_add - add a buffer to the LRU.
84 *
85 * The LRU takes a new reference to the buffer so that it will only be freed
86 * once the shrinker takes the buffer off the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
Dave Chinner430cbeb2010-12-02 16:30:55 +110088STATIC void
89xfs_buf_lru_add(
90 struct xfs_buf *bp)
91{
92 struct xfs_buftarg *btp = bp->b_target;
93
94 spin_lock(&btp->bt_lru_lock);
95 if (list_empty(&bp->b_lru)) {
96 atomic_inc(&bp->b_hold);
97 list_add_tail(&bp->b_lru, &btp->bt_lru);
98 btp->bt_lru_nr++;
99 }
100 spin_unlock(&btp->bt_lru_lock);
101}
102
103/*
104 * xfs_buf_lru_del - remove a buffer from the LRU
105 *
106 * The unlocked check is safe here because it only occurs when there are not
107 * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there
108 * to optimise the shrinker removing the buffer from the LRU and calling
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300109 * xfs_buf_free(). i.e. it removes an unnecessary round trip on the
Dave Chinner430cbeb2010-12-02 16:30:55 +1100110 * bt_lru_lock.
111 */
112STATIC void
113xfs_buf_lru_del(
114 struct xfs_buf *bp)
115{
116 struct xfs_buftarg *btp = bp->b_target;
117
118 if (list_empty(&bp->b_lru))
119 return;
120
121 spin_lock(&btp->bt_lru_lock);
122 if (!list_empty(&bp->b_lru)) {
123 list_del_init(&bp->b_lru);
124 btp->bt_lru_nr--;
125 }
126 spin_unlock(&btp->bt_lru_lock);
127}
128
129/*
130 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
131 * b_lru_ref count so that the buffer is freed immediately when the buffer
132 * reference count falls to zero. If the buffer is already on the LRU, we need
133 * to remove the reference that LRU holds on the buffer.
134 *
135 * This prevents build-up of stale buffers on the LRU.
136 */
137void
138xfs_buf_stale(
139 struct xfs_buf *bp)
140{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000141 ASSERT(xfs_buf_islocked(bp));
142
Dave Chinner430cbeb2010-12-02 16:30:55 +1100143 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000144
145 /*
146 * Clear the delwri status so that a delwri queue walker will not
147 * flush this buffer to disk now that it is stale. The delwri queue has
148 * a reference to the buffer, so this is safe to do.
149 */
150 bp->b_flags &= ~_XBF_DELWRI_Q;
151
Dave Chinner430cbeb2010-12-02 16:30:55 +1100152 atomic_set(&(bp)->b_lru_ref, 0);
153 if (!list_empty(&bp->b_lru)) {
154 struct xfs_buftarg *btp = bp->b_target;
155
156 spin_lock(&btp->bt_lru_lock);
157 if (!list_empty(&bp->b_lru)) {
158 list_del_init(&bp->b_lru);
159 btp->bt_lru_nr--;
160 atomic_dec(&bp->b_hold);
161 }
162 spin_unlock(&btp->bt_lru_lock);
163 }
164 ASSERT(atomic_read(&bp->b_hold) >= 1);
165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000167struct xfs_buf *
168xfs_buf_alloc(
169 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000170 xfs_daddr_t blkno,
171 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100172 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000174 struct xfs_buf *bp;
175
Dave Chinneraa5c1582012-04-23 15:58:56 +1000176 bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000177 if (unlikely(!bp))
178 return NULL;
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /*
Dave Chinner12bcb3f2012-04-23 15:59:05 +1000181 * We don't want certain flags to appear in b_flags unless they are
182 * specifically set by later operations on the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 */
Dave Chinner611c9942012-04-23 15:59:07 +1000184 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Nathan Scottce8e9222006-01-11 15:39:08 +1100186 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100187 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000188 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100189 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100190 INIT_LIST_HEAD(&bp->b_list);
Dave Chinner74f75a02010-09-24 19:59:04 +1000191 RB_CLEAR_NODE(&bp->b_rbnode);
Thomas Gleixnera731cd12010-09-07 14:33:15 +0000192 sema_init(&bp->b_sema, 0); /* held, no waiters */
Nathan Scottce8e9222006-01-11 15:39:08 +1100193 XB_SET_OWNER(bp);
194 bp->b_target = target;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000197 * Set length and io_length to the same value initially.
198 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * most cases but may be reset (e.g. XFS recovery).
200 */
Dave Chinner4e94b712012-04-23 15:58:51 +1000201 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000202 bp->b_io_length = numblks;
Nathan Scottce8e9222006-01-11 15:39:08 +1100203 bp->b_flags = flags;
Jan Karabcf62ab2012-06-06 00:32:26 +0200204 bp->b_bn = blkno;
Dave Chinnercbb7baab2012-06-22 18:50:08 +1000205 bp->b_map.bm_bn = blkno;
206 bp->b_map.bm_len = numblks;
Nathan Scottce8e9222006-01-11 15:39:08 +1100207 atomic_set(&bp->b_pin_count, 0);
208 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Nathan Scottce8e9222006-01-11 15:39:08 +1100210 XFS_STATS_INC(xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000211 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000212
213 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100217 * Allocate a page array capable of holding a specified number
218 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
220STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100221_xfs_buf_get_pages(
222 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100224 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100227 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100228 bp->b_page_count = page_count;
229 if (page_count <= XB_PAGES) {
230 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100232 bp->b_pages = kmem_alloc(sizeof(struct page *) *
Dave Chinneraa5c1582012-04-23 15:58:56 +1000233 page_count, KM_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +1100234 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -ENOMEM;
236 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100237 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 return 0;
240}
241
242/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100243 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
245STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100246_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 xfs_buf_t *bp)
248{
Nathan Scottce8e9222006-01-11 15:39:08 +1100249 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000250 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000251 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253}
254
255/*
256 * Releases the specified buffer.
257 *
258 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100259 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 * hashed and refcounted buffers
261 */
262void
Nathan Scottce8e9222006-01-11 15:39:08 +1100263xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 xfs_buf_t *bp)
265{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000266 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Dave Chinner430cbeb2010-12-02 16:30:55 +1100268 ASSERT(list_empty(&bp->b_lru));
269
Dave Chinner0e6e8472011-03-26 09:16:45 +1100270 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 uint i;
272
James Bottomley73c77e22010-01-25 11:42:24 -0600273 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000274 vm_unmap_ram(bp->b_addr - bp->b_offset,
275 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Nathan Scott948ecdb2006-09-28 11:03:13 +1000277 for (i = 0; i < bp->b_page_count; i++) {
278 struct page *page = bp->b_pages[i];
279
Dave Chinner0e6e8472011-03-26 09:16:45 +1100280 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000281 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100282 } else if (bp->b_flags & _XBF_KMEM)
283 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000284 _xfs_buf_free_pages(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000285 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100289 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
291STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100292xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 xfs_buf_t *bp,
294 uint flags)
295{
Dave Chinneraa0e8832012-04-23 15:58:52 +1000296 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100298 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 unsigned short page_count, i;
Dave Chinner795cac72012-04-23 15:58:53 +1000300 xfs_off_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int error;
302
Dave Chinner0e6e8472011-03-26 09:16:45 +1100303 /*
304 * for buffers that are contained within a single page, just allocate
305 * the memory from the heap - there's no need for the complexity of
306 * page arrays to keep allocation down to order 0.
307 */
Dave Chinner795cac72012-04-23 15:58:53 +1000308 size = BBTOB(bp->b_length);
309 if (size < PAGE_SIZE) {
Dave Chinneraa5c1582012-04-23 15:58:56 +1000310 bp->b_addr = kmem_alloc(size, KM_NOFS);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100311 if (!bp->b_addr) {
312 /* low memory - use alloc_page loop instead */
313 goto use_alloc_page;
314 }
315
Dave Chinner795cac72012-04-23 15:58:53 +1000316 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100317 ((unsigned long)bp->b_addr & PAGE_MASK)) {
318 /* b_addr spans two pages - use alloc_page instead */
319 kmem_free(bp->b_addr);
320 bp->b_addr = NULL;
321 goto use_alloc_page;
322 }
323 bp->b_offset = offset_in_page(bp->b_addr);
324 bp->b_pages = bp->b_page_array;
325 bp->b_pages[0] = virt_to_page(bp->b_addr);
326 bp->b_page_count = 1;
Dave Chinner611c9942012-04-23 15:59:07 +1000327 bp->b_flags |= _XBF_KMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100328 return 0;
329 }
330
331use_alloc_page:
Dave Chinnercbb7baab2012-06-22 18:50:08 +1000332 start = BBTOB(bp->b_map.bm_bn) >> PAGE_SHIFT;
333 end = (BBTOB(bp->b_map.bm_bn + bp->b_length) + PAGE_SIZE - 1)
334 >> PAGE_SHIFT;
Dave Chinner795cac72012-04-23 15:58:53 +1000335 page_count = end - start;
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;
Dave Chinner611c9942012-04-23 15:59:07 +1000398 } else if (flags & XBF_UNMAPPED) {
399 bp->b_addr = NULL;
400 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100401 int retried = 0;
402
403 do {
404 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
405 -1, PAGE_KERNEL);
406 if (bp->b_addr)
407 break;
408 vm_unmap_aliases();
409 } while (retried++ <= 1);
410
411 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100413 bp->b_addr += bp->b_offset;
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(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000430 struct xfs_buftarg *btp,
431 xfs_daddr_t blkno,
432 size_t numblks,
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{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000436 size_t numbytes;
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
Dave Chinnere70b73f2012-04-23 15:58:49 +1000442 numbytes = BBTOB(numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /* Check for IOs smaller than the sector size / not sector aligned */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000445 ASSERT(!(numbytes < (1 << btp->bt_sshift)));
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000446 ASSERT(!(BBTOB(blkno) & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Dave Chinner74f75a02010-09-24 19:59:04 +1000448 /* get tree root */
449 pag = xfs_perag_get(btp->bt_mount,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000450 xfs_daddr_to_agno(btp->bt_mount, blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Dave Chinner74f75a02010-09-24 19:59:04 +1000452 /* walk tree */
453 spin_lock(&pag->pag_buf_lock);
454 rbp = &pag->pag_buf_tree.rb_node;
455 parent = NULL;
456 bp = NULL;
457 while (*rbp) {
458 parent = *rbp;
459 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000461 if (blkno < bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000462 rbp = &(*rbp)->rb_left;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000463 else if (blkno > bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000464 rbp = &(*rbp)->rb_right;
465 else {
466 /*
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000467 * found a block number match. If the range doesn't
Dave Chinner74f75a02010-09-24 19:59:04 +1000468 * match, the only way this is allowed is if the buffer
469 * in the cache is stale and the transaction that made
470 * it stale has not yet committed. i.e. we are
471 * reallocating a busy extent. Skip this buffer and
472 * continue searching to the right for an exact match.
473 */
Dave Chinner4e94b712012-04-23 15:58:51 +1000474 if (bp->b_length != numblks) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000475 ASSERT(bp->b_flags & XBF_STALE);
476 rbp = &(*rbp)->rb_right;
477 continue;
478 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100479 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 goto found;
481 }
482 }
483
484 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100485 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000486 rb_link_node(&new_bp->b_rbnode, parent, rbp);
487 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
488 /* the buffer keeps the perag reference until it is freed */
489 new_bp->b_pag = pag;
490 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100492 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000493 spin_unlock(&pag->pag_buf_lock);
494 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100496 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000499 spin_unlock(&pag->pag_buf_lock);
500 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200502 if (!xfs_buf_trylock(bp)) {
503 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100504 xfs_buf_rele(bp);
505 XFS_STATS_INC(xb_busy_locked);
506 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200508 xfs_buf_lock(bp);
509 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
Dave Chinner0e6e8472011-03-26 09:16:45 +1100512 /*
513 * if the buffer is stale, clear all the external state associated with
514 * it. We need to keep flags such as how we allocated the buffer memory
515 * intact here.
516 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100517 if (bp->b_flags & XBF_STALE) {
518 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinner611c9942012-04-23 15:59:07 +1000519 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
David Chinner2f926582005-09-05 08:33:35 +1000520 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000521
522 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100523 XFS_STATS_INC(xb_get_locked);
524 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527/*
Dave Chinner38158322011-09-30 04:45:02 +0000528 * Assembles a buffer covering the specified range. The code is optimised for
529 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
530 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
Dave Chinner38158322011-09-30 04:45:02 +0000532struct xfs_buf *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000533xfs_buf_get(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000534 xfs_buftarg_t *target,
535 xfs_daddr_t blkno,
536 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100537 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Dave Chinner38158322011-09-30 04:45:02 +0000539 struct xfs_buf *bp;
540 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100541 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Dave Chinnere70b73f2012-04-23 15:58:49 +1000543 bp = _xfs_buf_find(target, blkno, numblks, flags, NULL);
Dave Chinner38158322011-09-30 04:45:02 +0000544 if (likely(bp))
545 goto found;
546
Dave Chinnere70b73f2012-04-23 15:58:49 +1000547 new_bp = xfs_buf_alloc(target, blkno, numblks, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100548 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return NULL;
550
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000551 error = xfs_buf_allocate_memory(new_bp, flags);
552 if (error) {
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000553 kmem_zone_free(xfs_buf_zone, new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000554 return NULL;
555 }
556
Dave Chinnere70b73f2012-04-23 15:58:49 +1000557 bp = _xfs_buf_find(target, blkno, numblks, flags, new_bp);
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000558 if (!bp) {
559 xfs_buf_free(new_bp);
560 return NULL;
561 }
562
563 if (bp != new_bp)
564 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Dave Chinner38158322011-09-30 04:45:02 +0000566found:
Dave Chinner611c9942012-04-23 15:59:07 +1000567 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100568 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100570 xfs_warn(target->bt_mount,
571 "%s: failed to map pages\n", __func__);
Dave Chinnera8acad72012-04-23 15:58:54 +1000572 xfs_buf_relse(bp);
573 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 }
576
Nathan Scottce8e9222006-01-11 15:39:08 +1100577 XFS_STATS_INC(xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000578 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100579 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100582STATIC int
583_xfs_buf_read(
584 xfs_buf_t *bp,
585 xfs_buf_flags_t flags)
586{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000587 ASSERT(!(flags & XBF_WRITE));
Dave Chinnercbb7baab2012-06-22 18:50:08 +1000588 ASSERT(bp->b_map.bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100589
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000590 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200591 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100592
Dave Chinner0e95f192012-04-23 15:58:46 +1000593 xfs_buf_iorequest(bp);
594 if (flags & XBF_ASYNC)
595 return 0;
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000596 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599xfs_buf_t *
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000600xfs_buf_read(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 xfs_buftarg_t *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000602 xfs_daddr_t blkno,
603 size_t numblks,
Nathan Scottce8e9222006-01-11 15:39:08 +1100604 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Nathan Scottce8e9222006-01-11 15:39:08 +1100606 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Nathan Scottce8e9222006-01-11 15:39:08 +1100608 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Dave Chinnere70b73f2012-04-23 15:58:49 +1000610 bp = xfs_buf_get(target, blkno, numblks, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100611 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000612 trace_xfs_buf_read(bp, flags, _RET_IP_);
613
Nathan Scottce8e9222006-01-11 15:39:08 +1100614 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100615 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100616 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100617 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /*
619 * Read ahead call which is already satisfied,
620 * drop the buffer
621 */
Dave Chinnera8acad72012-04-23 15:58:54 +1000622 xfs_buf_relse(bp);
623 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100626 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628 }
629
Nathan Scottce8e9222006-01-11 15:39:08 +1100630 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100634 * If we are not low on memory then do the readahead in a deadlock
635 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
637void
Nathan Scottce8e9222006-01-11 15:39:08 +1100638xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 xfs_buftarg_t *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000640 xfs_daddr_t blkno,
641 size_t numblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100643 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return;
645
Dave Chinnere70b73f2012-04-23 15:58:49 +1000646 xfs_buf_read(target, blkno, numblks,
Dave Chinneraa5c1582012-04-23 15:58:56 +1000647 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Dave Chinner5adc94c2010-09-24 21:58:31 +1000650/*
651 * Read an uncached buffer from disk. Allocates and returns a locked
652 * buffer containing the disk contents or nothing.
653 */
654struct xfs_buf *
655xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000656 struct xfs_buftarg *target,
657 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000658 size_t numblks,
Dave Chinner5adc94c2010-09-24 21:58:31 +1000659 int flags)
660{
661 xfs_buf_t *bp;
662 int error;
663
Dave Chinnere70b73f2012-04-23 15:58:49 +1000664 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000665 if (!bp)
666 return NULL;
667
668 /* set up the buffer for a read IO */
Dave Chinnercbb7baab2012-06-22 18:50:08 +1000669 bp->b_map.bm_bn = daddr;
670 bp->b_flags |= XBF_READ;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000671
Dave Chinnere70b73f2012-04-23 15:58:49 +1000672 xfsbdstrat(target->bt_mount, bp);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000673 error = xfs_buf_iowait(bp);
Dave Chinner0e95f192012-04-23 15:58:46 +1000674 if (error) {
Dave Chinner5adc94c2010-09-24 21:58:31 +1000675 xfs_buf_relse(bp);
676 return NULL;
677 }
678 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Dave Chinner44396472011-04-21 09:34:27 +0000681/*
682 * Return a buffer allocated as an empty buffer and associated to external
683 * memory via xfs_buf_associate_memory() back to it's empty state.
684 */
685void
686xfs_buf_set_empty(
687 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000688 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000689{
690 if (bp->b_pages)
691 _xfs_buf_free_pages(bp);
692
693 bp->b_pages = NULL;
694 bp->b_page_count = 0;
695 bp->b_addr = NULL;
Dave Chinner4e94b712012-04-23 15:58:51 +1000696 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000697 bp->b_io_length = numblks;
Dave Chinner44396472011-04-21 09:34:27 +0000698 bp->b_bn = XFS_BUF_DADDR_NULL;
Dave Chinnercbb7baab2012-06-22 18:50:08 +1000699 bp->b_map.bm_bn = XFS_BUF_DADDR_NULL;
700 bp->b_map.bm_len = bp->b_length;
Dave Chinner44396472011-04-21 09:34:27 +0000701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703static inline struct page *
704mem_to_page(
705 void *addr)
706{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800707 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return virt_to_page(addr);
709 } else {
710 return vmalloc_to_page(addr);
711 }
712}
713
714int
Nathan Scottce8e9222006-01-11 15:39:08 +1100715xfs_buf_associate_memory(
716 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 void *mem,
718 size_t len)
719{
720 int rval;
721 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100722 unsigned long pageaddr;
723 unsigned long offset;
724 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int page_count;
726
Dave Chinner0e6e8472011-03-26 09:16:45 +1100727 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100728 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100729 buflen = PAGE_ALIGN(len + offset);
730 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100733 if (bp->b_pages)
734 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Nathan Scottce8e9222006-01-11 15:39:08 +1100736 bp->b_pages = NULL;
737 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Dave Chinneraa5c1582012-04-23 15:58:56 +1000739 rval = _xfs_buf_get_pages(bp, page_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (rval)
741 return rval;
742
Nathan Scottce8e9222006-01-11 15:39:08 +1100743 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100745 for (i = 0; i < bp->b_page_count; i++) {
746 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100747 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Dave Chinneraa0e8832012-04-23 15:58:52 +1000750 bp->b_io_length = BTOBB(len);
Dave Chinner4e94b712012-04-23 15:58:51 +1000751 bp->b_length = BTOBB(buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 return 0;
754}
755
756xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000757xfs_buf_get_uncached(
758 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000759 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000760 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000762 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000763 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Jan Karabcf62ab2012-06-06 00:32:26 +0200766 bp = xfs_buf_alloc(target, XFS_BUF_DADDR_NULL, numblks, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (unlikely(bp == NULL))
768 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Dave Chinnere70b73f2012-04-23 15:58:49 +1000770 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000771 error = _xfs_buf_get_pages(bp, page_count, 0);
772 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 goto fail_free_buf;
774
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000775 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000776 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000777 if (!bp->b_pages[i])
778 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000780 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Dave Chinner611c9942012-04-23 15:59:07 +1000782 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000783 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100784 xfs_warn(target->bt_mount,
785 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Dave Chinner686865f2010-09-24 20:07:47 +1000789 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000793 while (--i >= 0)
794 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000795 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 fail_free_buf:
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000797 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 fail:
799 return NULL;
800}
801
802/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * Increment reference count on buffer, to hold the buffer concurrently
804 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * Must hold the buffer already to call this function.
806 */
807void
Nathan Scottce8e9222006-01-11 15:39:08 +1100808xfs_buf_hold(
809 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000811 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100812 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100816 * Releases a hold on the specified buffer. If the
817 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 */
819void
Nathan Scottce8e9222006-01-11 15:39:08 +1100820xfs_buf_rele(
821 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Dave Chinner74f75a02010-09-24 19:59:04 +1000823 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000825 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Dave Chinner74f75a02010-09-24 19:59:04 +1000827 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100828 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000829 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100830 if (atomic_dec_and_test(&bp->b_hold))
831 xfs_buf_free(bp);
832 return;
833 }
834
Dave Chinner74f75a02010-09-24 19:59:04 +1000835 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100836
Lachlan McIlroy37906892008-08-13 15:42:10 +1000837 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000838 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000839 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100840 atomic_read(&bp->b_lru_ref)) {
841 xfs_buf_lru_add(bp);
842 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100843 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100844 xfs_buf_lru_del(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000845 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner74f75a02010-09-24 19:59:04 +1000846 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
847 spin_unlock(&pag->pag_buf_lock);
848 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100849 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851 }
852}
853
854
855/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100856 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100857 *
858 * If we come across a stale, pinned, locked buffer, we know that we are
859 * being asked to lock a buffer that has been reallocated. Because it is
860 * pinned, we know that the log has not been pushed to disk and hence it
861 * will still be locked. Rather than continuing to have trylock attempts
862 * fail until someone else pushes the log, push it ourselves before
863 * returning. This means that the xfsaild will not get stuck trying
864 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 */
866int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200867xfs_buf_trylock(
868 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 int locked;
871
Nathan Scottce8e9222006-01-11 15:39:08 +1100872 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000873 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100874 XB_SET_OWNER(bp);
Dave Chinner90810b92010-11-30 15:16:16 +1100875 else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
876 xfs_log_force(bp->b_target->bt_mount, 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000877
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200878 trace_xfs_buf_trylock(bp, _RET_IP_);
879 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100883 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000884 *
885 * If we come across a stale, pinned, locked buffer, we know that we
886 * are being asked to lock a buffer that has been reallocated. Because
887 * it is pinned, we know that the log has not been pushed to disk and
888 * hence it will still be locked. Rather than sleeping until someone
889 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100891void
892xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200893 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000895 trace_xfs_buf_lock(bp, _RET_IP_);
896
Dave Chinnered3b4d62010-05-21 12:07:08 +1000897 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000898 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100899 down(&bp->b_sema);
900 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000901
902 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905void
Nathan Scottce8e9222006-01-11 15:39:08 +1100906xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200907 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Nathan Scottce8e9222006-01-11 15:39:08 +1100909 XB_CLEAR_OWNER(bp);
910 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000911
912 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Nathan Scottce8e9222006-01-11 15:39:08 +1100915STATIC void
916xfs_buf_wait_unpin(
917 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 DECLARE_WAITQUEUE (wait, current);
920
Nathan Scottce8e9222006-01-11 15:39:08 +1100921 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return;
923
Nathan Scottce8e9222006-01-11 15:39:08 +1100924 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 for (;;) {
926 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100927 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100929 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100931 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 set_current_state(TASK_RUNNING);
933}
934
935/*
936 * Buffer Utility Routines
937 */
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100940xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000941 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
David Howellsc4028952006-11-22 14:57:56 +0000943 xfs_buf_t *bp =
944 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Christoph Hellwig80f6c292010-08-18 05:29:11 -0400946 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +1100947 (*(bp->b_iodone))(bp);
948 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 xfs_buf_relse(bp);
950}
951
952void
Nathan Scottce8e9222006-01-11 15:39:08 +1100953xfs_buf_ioend(
954 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int schedule)
956{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000957 trace_xfs_buf_iodone(bp, _RET_IP_);
958
Lachlan McIlroy77be55a2007-11-23 16:31:00 +1100959 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +1100960 if (bp->b_error == 0)
961 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Nathan Scottce8e9222006-01-11 15:39:08 +1100963 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +0000965 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +1100966 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 } else {
David Howellsc4028952006-11-22 14:57:56 +0000968 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +1000971 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975void
Nathan Scottce8e9222006-01-11 15:39:08 +1100976xfs_buf_ioerror(
977 xfs_buf_t *bp,
978 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +1100981 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000982 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Christoph Hellwig901796a2011-10-10 16:52:49 +0000985void
986xfs_buf_ioerror_alert(
987 struct xfs_buf *bp,
988 const char *func)
989{
990 xfs_alert(bp->b_target->bt_mount,
Dave Chinneraa0e8832012-04-23 15:58:52 +1000991"metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
992 (__uint64_t)XFS_BUF_ADDR(bp), func, bp->b_error, bp->b_length);
Christoph Hellwig901796a2011-10-10 16:52:49 +0000993}
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995int
Christoph Hellwig64e0bc72010-01-13 22:17:58 +0000996xfs_bwrite(
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100997 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Christoph Hellwig8c383662010-03-12 10:59:40 +0000999 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001001 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001003 bp->b_flags |= XBF_WRITE;
1004 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
1005
Christoph Hellwig939d7232010-07-20 17:51:16 +10001006 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Christoph Hellwig8c383662010-03-12 10:59:40 +00001008 error = xfs_buf_iowait(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001009 if (error) {
1010 xfs_force_shutdown(bp->b_target->bt_mount,
1011 SHUTDOWN_META_IO_ERROR);
1012 }
Christoph Hellwig64e0bc72010-01-13 22:17:58 +00001013 return error;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001014}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Christoph Hellwig4e234712010-01-13 22:17:56 +00001016/*
1017 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001018 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001019 * so that the proper iodone callbacks get called.
1020 */
1021STATIC int
1022xfs_bioerror(
1023 xfs_buf_t *bp)
1024{
1025#ifdef XFSERRORDEBUG
1026 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1027#endif
1028
1029 /*
1030 * No need to wait until the buffer is unpinned, we aren't flushing it.
1031 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001032 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001033
1034 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001035 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001036 */
1037 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001038 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001039 xfs_buf_stale(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001040
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001041 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001042
1043 return EIO;
1044}
1045
1046/*
1047 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001048 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001049 * This is meant for userdata errors; metadata bufs come with
1050 * iodone functions attached, so that we can track down errors.
1051 */
1052STATIC int
1053xfs_bioerror_relse(
1054 struct xfs_buf *bp)
1055{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001056 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001057 /*
1058 * No need to wait until the buffer is unpinned.
1059 * We aren't flushing it.
1060 *
1061 * chunkhold expects B_DONE to be set, whether
1062 * we actually finish the I/O or not. We don't want to
1063 * change that interface.
1064 */
1065 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001066 XFS_BUF_DONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001067 xfs_buf_stale(bp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001068 bp->b_iodone = NULL;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001069 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001070 /*
1071 * Mark b_error and B_ERROR _both_.
1072 * Lot's of chunkcache code assumes that.
1073 * There's no reason to mark error for
1074 * ASYNC buffers.
1075 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001076 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig5fde0322011-10-10 16:52:44 +00001077 complete(&bp->b_iowait);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001078 } else {
1079 xfs_buf_relse(bp);
1080 }
1081
1082 return EIO;
1083}
1084
1085
1086/*
1087 * All xfs metadata buffers except log state machine buffers
1088 * get this attached as their b_bdstrat callback function.
1089 * This is so that we can catch a buffer
1090 * after prematurely unpinning it to forcibly shutdown the filesystem.
1091 */
1092int
1093xfs_bdstrat_cb(
1094 struct xfs_buf *bp)
1095{
Dave Chinnerebad8612010-09-22 10:47:20 +10001096 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001097 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1098 /*
1099 * Metadata write that didn't get logged but
1100 * written delayed anyway. These aren't associated
1101 * with a transaction, and can be ignored.
1102 */
1103 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1104 return xfs_bioerror_relse(bp);
1105 else
1106 return xfs_bioerror(bp);
1107 }
1108
1109 xfs_buf_iorequest(bp);
1110 return 0;
1111}
1112
1113/*
1114 * Wrapper around bdstrat so that we can stop data from going to disk in case
1115 * we are shutting down the filesystem. Typically user data goes thru this
1116 * path; one of the exceptions is the superblock.
1117 */
1118void
1119xfsbdstrat(
1120 struct xfs_mount *mp,
1121 struct xfs_buf *bp)
1122{
1123 if (XFS_FORCED_SHUTDOWN(mp)) {
1124 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1125 xfs_bioerror_relse(bp);
1126 return;
1127 }
1128
1129 xfs_buf_iorequest(bp);
1130}
1131
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001132STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001133_xfs_buf_ioend(
1134 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 int schedule)
1136{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001137 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001138 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Al Viro782e3b32007-10-12 07:17:47 +01001141STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001142xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 int error)
1145{
Nathan Scottce8e9222006-01-11 15:39:08 +11001146 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001148 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
James Bottomley73c77e22010-01-25 11:42:24 -06001150 if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
1151 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1152
Nathan Scottce8e9222006-01-11 15:39:08 +11001153 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
1157STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001158_xfs_buf_ioapply(
1159 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001161 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001163 int offset = bp->b_offset;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001164 int size = BBTOB(bp->b_io_length);
Dave Chinnercbb7baab2012-06-22 18:50:08 +10001165 sector_t sector = bp->b_map.bm_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Nathan Scottce8e9222006-01-11 15:39:08 +11001167 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 map_i = 0;
1169
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001170 if (bp->b_flags & XBF_WRITE) {
1171 if (bp->b_flags & XBF_SYNCIO)
1172 rw = WRITE_SYNC;
1173 else
1174 rw = WRITE;
1175 if (bp->b_flags & XBF_FUA)
1176 rw |= REQ_FUA;
1177 if (bp->b_flags & XBF_FLUSH)
1178 rw |= REQ_FLUSH;
1179 } else if (bp->b_flags & XBF_READ_AHEAD) {
1180 rw = READA;
Nathan Scott51bdd702006-09-28 11:01:57 +10001181 } else {
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +02001182 rw = READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001183 }
1184
Christoph Hellwig34951f52011-07-26 15:06:44 +00001185 /* we only use the buffer cache for meta-data */
1186 rw |= REQ_META;
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001189 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1191 if (nr_pages > total_nr_pages)
1192 nr_pages = total_nr_pages;
1193
1194 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001195 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001197 bio->bi_end_io = xfs_buf_bio_end_io;
1198 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Dave Chinner0e6e8472011-03-26 09:16:45 +11001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 for (; size && nr_pages; nr_pages--, map_i++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001202 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 if (nbytes > size)
1205 nbytes = size;
1206
Nathan Scottce8e9222006-01-11 15:39:08 +11001207 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1208 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 break;
1210
1211 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001212 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 size -= nbytes;
1214 total_nr_pages--;
1215 }
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001218 if (xfs_buf_is_vmapped(bp)) {
1219 flush_kernel_vmap_range(bp->b_addr,
1220 xfs_buf_vmap_len(bp));
1221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 submit_bio(rw, bio);
1223 if (size)
1224 goto next_chunk;
1225 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001226 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001227 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229}
1230
Dave Chinner0e95f192012-04-23 15:58:46 +10001231void
Nathan Scottce8e9222006-01-11 15:39:08 +11001232xfs_buf_iorequest(
1233 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001235 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001237 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Christoph Hellwig375ec69d2011-08-23 08:28:03 +00001239 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001240 xfs_buf_wait_unpin(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001241 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /* Set the count to 1 initially, this will stop an I/O
1244 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001245 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001247 atomic_set(&bp->b_io_remaining, 1);
1248 _xfs_buf_ioapply(bp);
1249 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Nathan Scottce8e9222006-01-11 15:39:08 +11001251 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
1254/*
Dave Chinner0e95f192012-04-23 15:58:46 +10001255 * Waits for I/O to complete on the buffer supplied. It returns immediately if
1256 * no I/O is pending or there is already a pending error on the buffer. It
1257 * returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 */
1259int
Nathan Scottce8e9222006-01-11 15:39:08 +11001260xfs_buf_iowait(
1261 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001263 trace_xfs_buf_iowait(bp, _RET_IP_);
1264
Dave Chinner0e95f192012-04-23 15:58:46 +10001265 if (!bp->b_error)
1266 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001267
1268 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001269 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Nathan Scottce8e9222006-01-11 15:39:08 +11001272xfs_caddr_t
1273xfs_buf_offset(
1274 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 size_t offset)
1276{
1277 struct page *page;
1278
Dave Chinner611c9942012-04-23 15:59:07 +10001279 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001280 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Nathan Scottce8e9222006-01-11 15:39:08 +11001282 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001283 page = bp->b_pages[offset >> PAGE_SHIFT];
1284 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
1287/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 * Move data into or out of a buffer.
1289 */
1290void
Nathan Scottce8e9222006-01-11 15:39:08 +11001291xfs_buf_iomove(
1292 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 size_t boff, /* starting buffer offset */
1294 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001295 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001296 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Dave Chinner795cac72012-04-23 15:58:53 +10001298 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 bend = boff + bsize;
1301 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001302 struct page *page;
1303 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Dave Chinner795cac72012-04-23 15:58:53 +10001305 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1306 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1307 page = bp->b_pages[page_index];
1308 csize = min_t(size_t, PAGE_SIZE - page_offset,
1309 BBTOB(bp->b_io_length) - boff);
1310
1311 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001314 case XBRW_ZERO:
Dave Chinner795cac72012-04-23 15:58:53 +10001315 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001317 case XBRW_READ:
Dave Chinner795cac72012-04-23 15:58:53 +10001318 memcpy(data, page_address(page) + page_offset, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001320 case XBRW_WRITE:
Dave Chinner795cac72012-04-23 15:58:53 +10001321 memcpy(page_address(page) + page_offset, data, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323
1324 boff += csize;
1325 data += csize;
1326 }
1327}
1328
1329/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001330 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 */
1332
1333/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001334 * Wait for any bufs with callbacks that have been submitted but have not yet
1335 * returned. These buffers will have an elevated hold count, so wait on those
1336 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
1338void
1339xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001340 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Dave Chinner430cbeb2010-12-02 16:30:55 +11001342 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Dave Chinner430cbeb2010-12-02 16:30:55 +11001344restart:
1345 spin_lock(&btp->bt_lru_lock);
1346 while (!list_empty(&btp->bt_lru)) {
1347 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1348 if (atomic_read(&bp->b_hold) > 1) {
1349 spin_unlock(&btp->bt_lru_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001350 delay(100);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001351 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001353 /*
Paul Bolle90802ed2011-12-05 13:00:34 +01001354 * clear the LRU reference count so the buffer doesn't get
Dave Chinner430cbeb2010-12-02 16:30:55 +11001355 * ignored in xfs_buf_rele().
1356 */
1357 atomic_set(&bp->b_lru_ref, 0);
1358 spin_unlock(&btp->bt_lru_lock);
1359 xfs_buf_rele(bp);
1360 spin_lock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001362 spin_unlock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Dave Chinnerff57ab22010-11-30 17:27:57 +11001365int
1366xfs_buftarg_shrink(
1367 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001368 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001369{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001370 struct xfs_buftarg *btp = container_of(shrink,
1371 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001372 struct xfs_buf *bp;
Ying Han1495f232011-05-24 17:12:27 -07001373 int nr_to_scan = sc->nr_to_scan;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001374 LIST_HEAD(dispose);
1375
1376 if (!nr_to_scan)
1377 return btp->bt_lru_nr;
1378
1379 spin_lock(&btp->bt_lru_lock);
1380 while (!list_empty(&btp->bt_lru)) {
1381 if (nr_to_scan-- <= 0)
1382 break;
1383
1384 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1385
1386 /*
1387 * Decrement the b_lru_ref count unless the value is already
1388 * zero. If the value is already zero, we need to reclaim the
1389 * buffer, otherwise it gets another trip through the LRU.
1390 */
1391 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1392 list_move_tail(&bp->b_lru, &btp->bt_lru);
1393 continue;
1394 }
1395
1396 /*
1397 * remove the buffer from the LRU now to avoid needing another
1398 * lock round trip inside xfs_buf_rele().
1399 */
1400 list_move(&bp->b_lru, &dispose);
1401 btp->bt_lru_nr--;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001402 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001403 spin_unlock(&btp->bt_lru_lock);
1404
1405 while (!list_empty(&dispose)) {
1406 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1407 list_del_init(&bp->b_lru);
1408 xfs_buf_rele(bp);
1409 }
1410
1411 return btp->bt_lru_nr;
David Chinnera6867a62006-01-11 15:37:58 +11001412}
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414void
1415xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001416 struct xfs_mount *mp,
1417 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001419 unregister_shrinker(&btp->bt_shrinker);
1420
Christoph Hellwigb7963132009-03-03 14:48:37 -05001421 if (mp->m_flags & XFS_MOUNT_BARRIER)
1422 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001423
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001424 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427STATIC int
1428xfs_setsize_buftarg_flags(
1429 xfs_buftarg_t *btp,
1430 unsigned int blocksize,
1431 unsigned int sectorsize,
1432 int verbose)
1433{
Nathan Scottce8e9222006-01-11 15:39:08 +11001434 btp->bt_bsize = blocksize;
1435 btp->bt_sshift = ffs(sectorsize) - 1;
1436 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Nathan Scottce8e9222006-01-11 15:39:08 +11001438 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001439 char name[BDEVNAME_SIZE];
1440
1441 bdevname(btp->bt_bdev, name);
1442
Dave Chinner4f107002011-03-07 10:00:35 +11001443 xfs_warn(btp->bt_mount,
1444 "Cannot set_blocksize to %u on device %s\n",
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001445 sectorsize, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return EINVAL;
1447 }
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return 0;
1450}
1451
1452/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001453 * When allocating the initial buffer target we have not yet
1454 * read in the superblock, so don't know what sized sectors
1455 * are being used is at this early stage. Play safe.
1456 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457STATIC int
1458xfs_setsize_buftarg_early(
1459 xfs_buftarg_t *btp,
1460 struct block_device *bdev)
1461{
1462 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001463 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466int
1467xfs_setsize_buftarg(
1468 xfs_buftarg_t *btp,
1469 unsigned int blocksize,
1470 unsigned int sectorsize)
1471{
1472 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1473}
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475xfs_buftarg_t *
1476xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001477 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001479 int external,
1480 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 xfs_buftarg_t *btp;
1483
1484 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1485
Dave Chinnerebad8612010-09-22 10:47:20 +10001486 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001487 btp->bt_dev = bdev->bd_dev;
1488 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001489 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1490 if (!btp->bt_bdi)
1491 goto error;
1492
Dave Chinner430cbeb2010-12-02 16:30:55 +11001493 INIT_LIST_HEAD(&btp->bt_lru);
1494 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (xfs_setsize_buftarg_early(btp, bdev))
1496 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001497 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1498 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1499 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return btp;
1501
1502error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001503 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return NULL;
1505}
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001508 * Add a buffer to the delayed write list.
1509 *
1510 * This queues a buffer for writeout if it hasn't already been. Note that
1511 * neither this routine nor the buffer list submission functions perform
1512 * any internal synchronization. It is expected that the lists are thread-local
1513 * to the callers.
1514 *
1515 * Returns true if we queued up the buffer, or false if it already had
1516 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001518bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001519xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001520 struct xfs_buf *bp,
1521 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001523 ASSERT(xfs_buf_islocked(bp));
1524 ASSERT(!(bp->b_flags & XBF_READ));
1525
1526 /*
1527 * If the buffer is already marked delwri it already is queued up
1528 * by someone else for imediate writeout. Just ignore it in that
1529 * case.
1530 */
1531 if (bp->b_flags & _XBF_DELWRI_Q) {
1532 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1533 return false;
1534 }
David Chinnera6867a62006-01-11 15:37:58 +11001535
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001536 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1537
Dave Chinnerd808f612010-02-02 10:13:42 +11001538 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001539 * If a buffer gets written out synchronously or marked stale while it
1540 * is on a delwri list we lazily remove it. To do this, the other party
1541 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1542 * It remains referenced and on the list. In a rare corner case it
1543 * might get readded to a delwri list after the synchronous writeout, in
1544 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001545 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001546 bp->b_flags |= _XBF_DELWRI_Q;
1547 if (list_empty(&bp->b_list)) {
1548 atomic_inc(&bp->b_hold);
1549 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001550 }
David Chinner585e6d82007-02-10 18:32:29 +11001551
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001552 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001553}
1554
Dave Chinner089716a2010-01-26 15:13:25 +11001555/*
1556 * Compare function is more complex than it needs to be because
1557 * the return value is only 32 bits and we are doing comparisons
1558 * on 64 bit values
1559 */
1560static int
1561xfs_buf_cmp(
1562 void *priv,
1563 struct list_head *a,
1564 struct list_head *b)
1565{
1566 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1567 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1568 xfs_daddr_t diff;
1569
Dave Chinnercbb7baab2012-06-22 18:50:08 +10001570 diff = ap->b_map.bm_bn - bp->b_map.bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11001571 if (diff < 0)
1572 return -1;
1573 if (diff > 0)
1574 return 1;
1575 return 0;
1576}
1577
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001578static int
1579__xfs_buf_delwri_submit(
1580 struct list_head *buffer_list,
1581 struct list_head *io_list,
1582 bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001584 struct blk_plug plug;
1585 struct xfs_buf *bp, *n;
1586 int pinned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001588 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
1589 if (!wait) {
1590 if (xfs_buf_ispinned(bp)) {
1591 pinned++;
1592 continue;
1593 }
1594 if (!xfs_buf_trylock(bp))
1595 continue;
1596 } else {
1597 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001600 /*
1601 * Someone else might have written the buffer synchronously or
1602 * marked it stale in the meantime. In that case only the
1603 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1604 * reference and remove it from the list here.
1605 */
1606 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1607 list_del_init(&bp->b_list);
1608 xfs_buf_relse(bp);
1609 continue;
1610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001612 list_move_tail(&bp->b_list, io_list);
1613 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001616 list_sort(NULL, io_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001617
1618 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001619 list_for_each_entry_safe(bp, n, io_list, b_list) {
1620 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
1621 bp->b_flags |= XBF_WRITE;
1622
1623 if (!wait) {
1624 bp->b_flags |= XBF_ASYNC;
1625 list_del_init(&bp->b_list);
Dave Chinner089716a2010-01-26 15:13:25 +11001626 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001627 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001629 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001631 return pinned;
1632}
Nathan Scottf07c2252006-09-28 10:52:15 +10001633
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001634/*
1635 * Write out a buffer list asynchronously.
1636 *
1637 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1638 * out and not wait for I/O completion on any of the buffers. This interface
1639 * is only safely useable for callers that can track I/O completion by higher
1640 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1641 * function.
1642 */
1643int
1644xfs_buf_delwri_submit_nowait(
1645 struct list_head *buffer_list)
1646{
1647 LIST_HEAD (io_list);
1648 return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
1649}
1650
1651/*
1652 * Write out a buffer list synchronously.
1653 *
1654 * This will take the @buffer_list, write all buffers out and wait for I/O
1655 * completion on all of the buffers. @buffer_list is consumed by the function,
1656 * so callers must have some other way of tracking buffers if they require such
1657 * functionality.
1658 */
1659int
1660xfs_buf_delwri_submit(
1661 struct list_head *buffer_list)
1662{
1663 LIST_HEAD (io_list);
1664 int error = 0, error2;
1665 struct xfs_buf *bp;
1666
1667 __xfs_buf_delwri_submit(buffer_list, &io_list, true);
1668
1669 /* Wait for IO to complete. */
1670 while (!list_empty(&io_list)) {
1671 bp = list_first_entry(&io_list, struct xfs_buf, b_list);
1672
1673 list_del_init(&bp->b_list);
1674 error2 = xfs_buf_iowait(bp);
1675 xfs_buf_relse(bp);
1676 if (!error)
1677 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
1679
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001680 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001683int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001684xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Nathan Scott87582802006-03-14 13:18:19 +11001686 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1687 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001688 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001689 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001690
Dave Chinner51749e42010-09-08 09:00:22 +00001691 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001692 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001693 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001694 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001698 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001699 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001700 out:
Nathan Scott87582802006-03-14 13:18:19 +11001701 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704void
Nathan Scottce8e9222006-01-11 15:39:08 +11001705xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001707 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001708 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}