blob: 118ce71d00d2bbabcc5f26d9f8160df2816e898d [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 Chinner7fd36c42013-08-12 20:49:32 +100038#include "xfs_trans_resv.h"
Dave Chinnered3b4d62010-05-21 12:07:08 +100039#include "xfs_log.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050040#include "xfs_ag.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050041#include "xfs_mount.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Christoph Hellwigb7963132009-03-03 14:48:37 -050043
David Chinner7989cb82007-02-10 18:34:56 +110044static kmem_zone_t *xfs_buf_zone;
Christoph Hellwig23ea4032005-06-21 15:14:01 +100045
David Chinner7989cb82007-02-10 18:34:56 +110046static struct workqueue_struct *xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Nathan Scottce8e9222006-01-11 15:39:08 +110048#ifdef XFS_BUF_LOCK_TRACKING
49# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
50# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
51# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#else
Nathan Scottce8e9222006-01-11 15:39:08 +110053# define XB_SET_OWNER(bp) do { } while (0)
54# define XB_CLEAR_OWNER(bp) do { } while (0)
55# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57
Nathan Scottce8e9222006-01-11 15:39:08 +110058#define xb_to_gfp(flags) \
Dave Chinneraa5c1582012-04-23 15:58:56 +100059 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
James Bottomley73c77e22010-01-25 11:42:24 -060062static inline int
63xfs_buf_is_vmapped(
64 struct xfs_buf *bp)
65{
66 /*
67 * Return true if the buffer is vmapped.
68 *
Dave Chinner611c9942012-04-23 15:59:07 +100069 * b_addr is null if the buffer is not mapped, but the code is clever
70 * enough to know it doesn't have to map a single page, so the check has
71 * to be both for b_addr and bp->b_page_count > 1.
James Bottomley73c77e22010-01-25 11:42:24 -060072 */
Dave Chinner611c9942012-04-23 15:59:07 +100073 return bp->b_addr && bp->b_page_count > 1;
James Bottomley73c77e22010-01-25 11:42:24 -060074}
75
76static inline int
77xfs_buf_vmap_len(
78 struct xfs_buf *bp)
79{
80 return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
81}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
Dave Chinner430cbeb2010-12-02 16:30:55 +110084 * xfs_buf_lru_add - add a buffer to the LRU.
85 *
86 * The LRU takes a new reference to the buffer so that it will only be freed
87 * once the shrinker takes the buffer off the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Dave Chinner430cbeb2010-12-02 16:30:55 +110089STATIC void
90xfs_buf_lru_add(
91 struct xfs_buf *bp)
92{
93 struct xfs_buftarg *btp = bp->b_target;
94
95 spin_lock(&btp->bt_lru_lock);
96 if (list_empty(&bp->b_lru)) {
97 atomic_inc(&bp->b_hold);
98 list_add_tail(&bp->b_lru, &btp->bt_lru);
99 btp->bt_lru_nr++;
Carlos Maiolino6fb8a902012-08-10 15:01:51 -0300100 bp->b_lru_flags &= ~_XBF_LRU_DISPOSE;
Dave Chinner430cbeb2010-12-02 16:30:55 +1100101 }
102 spin_unlock(&btp->bt_lru_lock);
103}
104
105/*
106 * xfs_buf_lru_del - remove a buffer from the LRU
107 *
108 * The unlocked check is safe here because it only occurs when there are not
109 * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there
110 * to optimise the shrinker removing the buffer from the LRU and calling
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300111 * xfs_buf_free(). i.e. it removes an unnecessary round trip on the
Dave Chinner430cbeb2010-12-02 16:30:55 +1100112 * bt_lru_lock.
113 */
114STATIC void
115xfs_buf_lru_del(
116 struct xfs_buf *bp)
117{
118 struct xfs_buftarg *btp = bp->b_target;
119
120 if (list_empty(&bp->b_lru))
121 return;
122
123 spin_lock(&btp->bt_lru_lock);
124 if (!list_empty(&bp->b_lru)) {
125 list_del_init(&bp->b_lru);
126 btp->bt_lru_nr--;
127 }
128 spin_unlock(&btp->bt_lru_lock);
129}
130
131/*
132 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
133 * b_lru_ref count so that the buffer is freed immediately when the buffer
134 * reference count falls to zero. If the buffer is already on the LRU, we need
135 * to remove the reference that LRU holds on the buffer.
136 *
137 * This prevents build-up of stale buffers on the LRU.
138 */
139void
140xfs_buf_stale(
141 struct xfs_buf *bp)
142{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000143 ASSERT(xfs_buf_islocked(bp));
144
Dave Chinner430cbeb2010-12-02 16:30:55 +1100145 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000146
147 /*
148 * Clear the delwri status so that a delwri queue walker will not
149 * flush this buffer to disk now that it is stale. The delwri queue has
150 * a reference to the buffer, so this is safe to do.
151 */
152 bp->b_flags &= ~_XBF_DELWRI_Q;
153
Dave Chinner430cbeb2010-12-02 16:30:55 +1100154 atomic_set(&(bp)->b_lru_ref, 0);
155 if (!list_empty(&bp->b_lru)) {
156 struct xfs_buftarg *btp = bp->b_target;
157
158 spin_lock(&btp->bt_lru_lock);
Carlos Maiolino6fb8a902012-08-10 15:01:51 -0300159 if (!list_empty(&bp->b_lru) &&
160 !(bp->b_lru_flags & _XBF_LRU_DISPOSE)) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100161 list_del_init(&bp->b_lru);
162 btp->bt_lru_nr--;
163 atomic_dec(&bp->b_hold);
164 }
165 spin_unlock(&btp->bt_lru_lock);
166 }
167 ASSERT(atomic_read(&bp->b_hold) >= 1);
168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Dave Chinner3e85c862012-06-22 18:50:09 +1000170static int
171xfs_buf_get_maps(
172 struct xfs_buf *bp,
173 int map_count)
174{
175 ASSERT(bp->b_maps == NULL);
176 bp->b_map_count = map_count;
177
178 if (map_count == 1) {
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600179 bp->b_maps = &bp->__b_map;
Dave Chinner3e85c862012-06-22 18:50:09 +1000180 return 0;
181 }
182
183 bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
184 KM_NOFS);
185 if (!bp->b_maps)
186 return ENOMEM;
187 return 0;
188}
189
190/*
191 * Frees b_pages if it was allocated.
192 */
193static void
194xfs_buf_free_maps(
195 struct xfs_buf *bp)
196{
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600197 if (bp->b_maps != &bp->__b_map) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000198 kmem_free(bp->b_maps);
199 bp->b_maps = NULL;
200 }
201}
202
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000203struct xfs_buf *
Dave Chinner3e85c862012-06-22 18:50:09 +1000204_xfs_buf_alloc(
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000205 struct xfs_buftarg *target,
Dave Chinner3e85c862012-06-22 18:50:09 +1000206 struct xfs_buf_map *map,
207 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100208 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000210 struct xfs_buf *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000211 int error;
212 int i;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000213
Dave Chinneraa5c1582012-04-23 15:58:56 +1000214 bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000215 if (unlikely(!bp))
216 return NULL;
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 /*
Dave Chinner12bcb3f2012-04-23 15:59:05 +1000219 * We don't want certain flags to appear in b_flags unless they are
220 * specifically set by later operations on the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 */
Dave Chinner611c9942012-04-23 15:59:07 +1000222 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Nathan Scottce8e9222006-01-11 15:39:08 +1100224 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100225 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000226 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100227 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100228 INIT_LIST_HEAD(&bp->b_list);
Dave Chinner74f75a02010-09-24 19:59:04 +1000229 RB_CLEAR_NODE(&bp->b_rbnode);
Thomas Gleixnera731cd112010-09-07 14:33:15 +0000230 sema_init(&bp->b_sema, 0); /* held, no waiters */
Nathan Scottce8e9222006-01-11 15:39:08 +1100231 XB_SET_OWNER(bp);
232 bp->b_target = target;
Dave Chinner3e85c862012-06-22 18:50:09 +1000233 bp->b_flags = flags;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000236 * Set length and io_length to the same value initially.
237 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * most cases but may be reset (e.g. XFS recovery).
239 */
Dave Chinner3e85c862012-06-22 18:50:09 +1000240 error = xfs_buf_get_maps(bp, nmaps);
241 if (error) {
242 kmem_zone_free(xfs_buf_zone, bp);
243 return NULL;
244 }
245
246 bp->b_bn = map[0].bm_bn;
247 bp->b_length = 0;
248 for (i = 0; i < nmaps; i++) {
249 bp->b_maps[i].bm_bn = map[i].bm_bn;
250 bp->b_maps[i].bm_len = map[i].bm_len;
251 bp->b_length += map[i].bm_len;
252 }
253 bp->b_io_length = bp->b_length;
254
Nathan Scottce8e9222006-01-11 15:39:08 +1100255 atomic_set(&bp->b_pin_count, 0);
256 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Nathan Scottce8e9222006-01-11 15:39:08 +1100258 XFS_STATS_INC(xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000259 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000260
261 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100265 * Allocate a page array capable of holding a specified number
266 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
268STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100269_xfs_buf_get_pages(
270 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100272 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100275 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100276 bp->b_page_count = page_count;
277 if (page_count <= XB_PAGES) {
278 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100280 bp->b_pages = kmem_alloc(sizeof(struct page *) *
Dave Chinneraa5c1582012-04-23 15:58:56 +1000281 page_count, KM_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +1100282 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return -ENOMEM;
284 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100285 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 return 0;
288}
289
290/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100291 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
293STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100294_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 xfs_buf_t *bp)
296{
Nathan Scottce8e9222006-01-11 15:39:08 +1100297 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000298 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000299 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301}
302
303/*
304 * Releases the specified buffer.
305 *
306 * The modification state of any associated pages is left unchanged.
Zhi Yong Wub46fe822013-08-07 10:10:59 +0000307 * The buffer must not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * hashed and refcounted buffers
309 */
310void
Nathan Scottce8e9222006-01-11 15:39:08 +1100311xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 xfs_buf_t *bp)
313{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000314 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Dave Chinner430cbeb2010-12-02 16:30:55 +1100316 ASSERT(list_empty(&bp->b_lru));
317
Dave Chinner0e6e8472011-03-26 09:16:45 +1100318 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 uint i;
320
James Bottomley73c77e22010-01-25 11:42:24 -0600321 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000322 vm_unmap_ram(bp->b_addr - bp->b_offset,
323 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Nathan Scott948ecdb2006-09-28 11:03:13 +1000325 for (i = 0; i < bp->b_page_count; i++) {
326 struct page *page = bp->b_pages[i];
327
Dave Chinner0e6e8472011-03-26 09:16:45 +1100328 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000329 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100330 } else if (bp->b_flags & _XBF_KMEM)
331 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000332 _xfs_buf_free_pages(bp);
Dave Chinner3e85c862012-06-22 18:50:09 +1000333 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000334 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100338 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
340STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100341xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 xfs_buf_t *bp,
343 uint flags)
344{
Dave Chinneraa0e8832012-04-23 15:58:52 +1000345 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100347 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned short page_count, i;
Dave Chinner795cac72012-04-23 15:58:53 +1000349 xfs_off_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int error;
351
Dave Chinner0e6e8472011-03-26 09:16:45 +1100352 /*
353 * for buffers that are contained within a single page, just allocate
354 * the memory from the heap - there's no need for the complexity of
355 * page arrays to keep allocation down to order 0.
356 */
Dave Chinner795cac72012-04-23 15:58:53 +1000357 size = BBTOB(bp->b_length);
358 if (size < PAGE_SIZE) {
Dave Chinneraa5c1582012-04-23 15:58:56 +1000359 bp->b_addr = kmem_alloc(size, KM_NOFS);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100360 if (!bp->b_addr) {
361 /* low memory - use alloc_page loop instead */
362 goto use_alloc_page;
363 }
364
Dave Chinner795cac72012-04-23 15:58:53 +1000365 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100366 ((unsigned long)bp->b_addr & PAGE_MASK)) {
367 /* b_addr spans two pages - use alloc_page instead */
368 kmem_free(bp->b_addr);
369 bp->b_addr = NULL;
370 goto use_alloc_page;
371 }
372 bp->b_offset = offset_in_page(bp->b_addr);
373 bp->b_pages = bp->b_page_array;
374 bp->b_pages[0] = virt_to_page(bp->b_addr);
375 bp->b_page_count = 1;
Dave Chinner611c9942012-04-23 15:59:07 +1000376 bp->b_flags |= _XBF_KMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100377 return 0;
378 }
379
380use_alloc_page:
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600381 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
382 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000383 >> PAGE_SHIFT;
Dave Chinner795cac72012-04-23 15:58:53 +1000384 page_count = end - start;
Nathan Scottce8e9222006-01-11 15:39:08 +1100385 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (unlikely(error))
387 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Nathan Scottce8e9222006-01-11 15:39:08 +1100389 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100390 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Nathan Scottce8e9222006-01-11 15:39:08 +1100392 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct page *page;
394 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100395retry:
396 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100398 if (flags & XBF_READ_AHEAD) {
399 bp->b_page_count = i;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100400 error = ENOMEM;
401 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
404 /*
405 * This could deadlock.
406 *
407 * But until all the XFS lowlevel code is revamped to
408 * handle buffer allocation failures we can't do much.
409 */
410 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100411 xfs_err(NULL,
412 "possible memory allocation deadlock in %s (mode:0x%x)",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000413 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Nathan Scottce8e9222006-01-11 15:39:08 +1100415 XFS_STATS_INC(xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200416 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 goto retry;
418 }
419
Nathan Scottce8e9222006-01-11 15:39:08 +1100420 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Dave Chinner0e6e8472011-03-26 09:16:45 +1100422 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100424 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 offset = 0;
426 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Dave Chinner0e6e8472011-03-26 09:16:45 +1100429out_free_pages:
430 for (i = 0; i < bp->b_page_count; i++)
431 __free_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return error;
433}
434
435/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300436 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 */
438STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100439_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 xfs_buf_t *bp,
441 uint flags)
442{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100443 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100444 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100445 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100446 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
Dave Chinner611c9942012-04-23 15:59:07 +1000447 } else if (flags & XBF_UNMAPPED) {
448 bp->b_addr = NULL;
449 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100450 int retried = 0;
451
452 do {
453 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
454 -1, PAGE_KERNEL);
455 if (bp->b_addr)
456 break;
457 vm_unmap_aliases();
458 } while (retried++ <= 1);
459
460 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100462 bp->b_addr += bp->b_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464
465 return 0;
466}
467
468/*
469 * Finding and Reading Buffers
470 */
471
472/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100473 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 * a given range of an inode. The buffer is returned
Chandra Seetharamaneabbaf12011-09-08 20:18:50 +0000475 * locked. No I/O is implied by this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 */
477xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100478_xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000479 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000480 struct xfs_buf_map *map,
481 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100482 xfs_buf_flags_t flags,
483 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000485 size_t numbytes;
Dave Chinner74f75a02010-09-24 19:59:04 +1000486 struct xfs_perag *pag;
487 struct rb_node **rbp;
488 struct rb_node *parent;
489 xfs_buf_t *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000490 xfs_daddr_t blkno = map[0].bm_bn;
Dave Chinner10616b82013-01-21 23:53:52 +1100491 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000492 int numblks = 0;
493 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Dave Chinner3e85c862012-06-22 18:50:09 +1000495 for (i = 0; i < nmaps; i++)
496 numblks += map[i].bm_len;
Dave Chinnere70b73f2012-04-23 15:58:49 +1000497 numbytes = BBTOB(numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* Check for IOs smaller than the sector size / not sector aligned */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000500 ASSERT(!(numbytes < (1 << btp->bt_sshift)));
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000501 ASSERT(!(BBTOB(blkno) & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Dave Chinner10616b82013-01-21 23:53:52 +1100503 /*
504 * Corrupted block numbers can get through to here, unfortunately, so we
505 * have to check that the buffer falls within the filesystem bounds.
506 */
507 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
508 if (blkno >= eofs) {
509 /*
510 * XXX (dgc): we should really be returning EFSCORRUPTED here,
511 * but none of the higher level infrastructure supports
512 * returning a specific error on buffer lookup failures.
513 */
514 xfs_alert(btp->bt_mount,
515 "%s: Block out of range: block 0x%llx, EOFS 0x%llx ",
516 __func__, blkno, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000517 WARN_ON(1);
Dave Chinner10616b82013-01-21 23:53:52 +1100518 return NULL;
519 }
520
Dave Chinner74f75a02010-09-24 19:59:04 +1000521 /* get tree root */
522 pag = xfs_perag_get(btp->bt_mount,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000523 xfs_daddr_to_agno(btp->bt_mount, blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Dave Chinner74f75a02010-09-24 19:59:04 +1000525 /* walk tree */
526 spin_lock(&pag->pag_buf_lock);
527 rbp = &pag->pag_buf_tree.rb_node;
528 parent = NULL;
529 bp = NULL;
530 while (*rbp) {
531 parent = *rbp;
532 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000534 if (blkno < bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000535 rbp = &(*rbp)->rb_left;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000536 else if (blkno > bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000537 rbp = &(*rbp)->rb_right;
538 else {
539 /*
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000540 * found a block number match. If the range doesn't
Dave Chinner74f75a02010-09-24 19:59:04 +1000541 * match, the only way this is allowed is if the buffer
542 * in the cache is stale and the transaction that made
543 * it stale has not yet committed. i.e. we are
544 * reallocating a busy extent. Skip this buffer and
545 * continue searching to the right for an exact match.
546 */
Dave Chinner4e94b712012-04-23 15:58:51 +1000547 if (bp->b_length != numblks) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000548 ASSERT(bp->b_flags & XBF_STALE);
549 rbp = &(*rbp)->rb_right;
550 continue;
551 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100552 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto found;
554 }
555 }
556
557 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100558 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000559 rb_link_node(&new_bp->b_rbnode, parent, rbp);
560 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
561 /* the buffer keeps the perag reference until it is freed */
562 new_bp->b_pag = pag;
563 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100565 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000566 spin_unlock(&pag->pag_buf_lock);
567 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100569 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000572 spin_unlock(&pag->pag_buf_lock);
573 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200575 if (!xfs_buf_trylock(bp)) {
576 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100577 xfs_buf_rele(bp);
578 XFS_STATS_INC(xb_busy_locked);
579 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200581 xfs_buf_lock(bp);
582 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
Dave Chinner0e6e8472011-03-26 09:16:45 +1100585 /*
586 * if the buffer is stale, clear all the external state associated with
587 * it. We need to keep flags such as how we allocated the buffer memory
588 * intact here.
589 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100590 if (bp->b_flags & XBF_STALE) {
591 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinnercfb02852012-11-12 22:54:19 +1100592 ASSERT(bp->b_iodone == NULL);
Dave Chinner611c9942012-04-23 15:59:07 +1000593 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100594 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000595 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000596
597 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100598 XFS_STATS_INC(xb_get_locked);
599 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602/*
Dave Chinner38158322011-09-30 04:45:02 +0000603 * Assembles a buffer covering the specified range. The code is optimised for
604 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
605 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
Dave Chinner38158322011-09-30 04:45:02 +0000607struct xfs_buf *
Dave Chinner6dde2702012-06-22 18:50:10 +1000608xfs_buf_get_map(
609 struct xfs_buftarg *target,
610 struct xfs_buf_map *map,
611 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100612 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Dave Chinner38158322011-09-30 04:45:02 +0000614 struct xfs_buf *bp;
615 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100616 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Dave Chinner6dde2702012-06-22 18:50:10 +1000618 bp = _xfs_buf_find(target, map, nmaps, flags, NULL);
Dave Chinner38158322011-09-30 04:45:02 +0000619 if (likely(bp))
620 goto found;
621
Dave Chinner6dde2702012-06-22 18:50:10 +1000622 new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100623 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return NULL;
625
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000626 error = xfs_buf_allocate_memory(new_bp, flags);
627 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000628 xfs_buf_free(new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000629 return NULL;
630 }
631
Dave Chinner6dde2702012-06-22 18:50:10 +1000632 bp = _xfs_buf_find(target, map, nmaps, flags, new_bp);
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000633 if (!bp) {
634 xfs_buf_free(new_bp);
635 return NULL;
636 }
637
638 if (bp != new_bp)
639 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Dave Chinner38158322011-09-30 04:45:02 +0000641found:
Dave Chinner611c9942012-04-23 15:59:07 +1000642 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100643 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100645 xfs_warn(target->bt_mount,
646 "%s: failed to map pages\n", __func__);
Dave Chinnera8acad72012-04-23 15:58:54 +1000647 xfs_buf_relse(bp);
648 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 }
651
Nathan Scottce8e9222006-01-11 15:39:08 +1100652 XFS_STATS_INC(xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000653 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100654 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100657STATIC int
658_xfs_buf_read(
659 xfs_buf_t *bp,
660 xfs_buf_flags_t flags)
661{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000662 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600663 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100664
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000665 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200666 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100667
Dave Chinner0e95f192012-04-23 15:58:46 +1000668 xfs_buf_iorequest(bp);
669 if (flags & XBF_ASYNC)
670 return 0;
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000671 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674xfs_buf_t *
Dave Chinner6dde2702012-06-22 18:50:10 +1000675xfs_buf_read_map(
676 struct xfs_buftarg *target,
677 struct xfs_buf_map *map,
678 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100679 xfs_buf_flags_t flags,
Dave Chinner1813dd62012-11-14 17:54:40 +1100680 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Dave Chinner6dde2702012-06-22 18:50:10 +1000682 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Nathan Scottce8e9222006-01-11 15:39:08 +1100684 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Dave Chinner6dde2702012-06-22 18:50:10 +1000686 bp = xfs_buf_get_map(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100687 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000688 trace_xfs_buf_read(bp, flags, _RET_IP_);
689
Nathan Scottce8e9222006-01-11 15:39:08 +1100690 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100691 XFS_STATS_INC(xb_get_read);
Dave Chinner1813dd62012-11-14 17:54:40 +1100692 bp->b_ops = ops;
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100693 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100694 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /*
696 * Read ahead call which is already satisfied,
697 * drop the buffer
698 */
Dave Chinnera8acad72012-04-23 15:58:54 +1000699 xfs_buf_relse(bp);
700 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100703 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705 }
706
Nathan Scottce8e9222006-01-11 15:39:08 +1100707 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100711 * If we are not low on memory then do the readahead in a deadlock
712 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
714void
Dave Chinner6dde2702012-06-22 18:50:10 +1000715xfs_buf_readahead_map(
716 struct xfs_buftarg *target,
717 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100718 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100719 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100721 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return;
723
Dave Chinner6dde2702012-06-22 18:50:10 +1000724 xfs_buf_read_map(target, map, nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100725 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
Dave Chinner5adc94c2010-09-24 21:58:31 +1000728/*
729 * Read an uncached buffer from disk. Allocates and returns a locked
730 * buffer containing the disk contents or nothing.
731 */
732struct xfs_buf *
733xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000734 struct xfs_buftarg *target,
735 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000736 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100737 int flags,
Dave Chinner1813dd62012-11-14 17:54:40 +1100738 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000739{
Dave Chinnereab4e632012-11-12 22:54:02 +1100740 struct xfs_buf *bp;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000741
Dave Chinnere70b73f2012-04-23 15:58:49 +1000742 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000743 if (!bp)
744 return NULL;
745
746 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000747 ASSERT(bp->b_map_count == 1);
748 bp->b_bn = daddr;
749 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000750 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100751 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000752
Dave Chinnere70b73f2012-04-23 15:58:49 +1000753 xfsbdstrat(target->bt_mount, bp);
Dave Chinnereab4e632012-11-12 22:54:02 +1100754 xfs_buf_iowait(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000755 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Dave Chinner44396472011-04-21 09:34:27 +0000758/*
759 * Return a buffer allocated as an empty buffer and associated to external
760 * memory via xfs_buf_associate_memory() back to it's empty state.
761 */
762void
763xfs_buf_set_empty(
764 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000765 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000766{
767 if (bp->b_pages)
768 _xfs_buf_free_pages(bp);
769
770 bp->b_pages = NULL;
771 bp->b_page_count = 0;
772 bp->b_addr = NULL;
Dave Chinner4e94b712012-04-23 15:58:51 +1000773 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000774 bp->b_io_length = numblks;
Dave Chinner3e85c862012-06-22 18:50:09 +1000775
776 ASSERT(bp->b_map_count == 1);
Dave Chinner44396472011-04-21 09:34:27 +0000777 bp->b_bn = XFS_BUF_DADDR_NULL;
Dave Chinner3e85c862012-06-22 18:50:09 +1000778 bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL;
779 bp->b_maps[0].bm_len = bp->b_length;
Dave Chinner44396472011-04-21 09:34:27 +0000780}
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782static inline struct page *
783mem_to_page(
784 void *addr)
785{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800786 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return virt_to_page(addr);
788 } else {
789 return vmalloc_to_page(addr);
790 }
791}
792
793int
Nathan Scottce8e9222006-01-11 15:39:08 +1100794xfs_buf_associate_memory(
795 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 void *mem,
797 size_t len)
798{
799 int rval;
800 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100801 unsigned long pageaddr;
802 unsigned long offset;
803 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 int page_count;
805
Dave Chinner0e6e8472011-03-26 09:16:45 +1100806 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100807 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100808 buflen = PAGE_ALIGN(len + offset);
809 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100812 if (bp->b_pages)
813 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Nathan Scottce8e9222006-01-11 15:39:08 +1100815 bp->b_pages = NULL;
816 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Dave Chinneraa5c1582012-04-23 15:58:56 +1000818 rval = _xfs_buf_get_pages(bp, page_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (rval)
820 return rval;
821
Nathan Scottce8e9222006-01-11 15:39:08 +1100822 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100824 for (i = 0; i < bp->b_page_count; i++) {
825 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100826 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Dave Chinneraa0e8832012-04-23 15:58:52 +1000829 bp->b_io_length = BTOBB(len);
Dave Chinner4e94b712012-04-23 15:58:51 +1000830 bp->b_length = BTOBB(buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 return 0;
833}
834
835xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000836xfs_buf_get_uncached(
837 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000838 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000839 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000841 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000842 int error, i;
Dave Chinner3e85c862012-06-22 18:50:09 +1000843 struct xfs_buf *bp;
844 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Dave Chinner3e85c862012-06-22 18:50:09 +1000846 bp = _xfs_buf_alloc(target, &map, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (unlikely(bp == NULL))
848 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Dave Chinnere70b73f2012-04-23 15:58:49 +1000850 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000851 error = _xfs_buf_get_pages(bp, page_count, 0);
852 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 goto fail_free_buf;
854
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000855 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000856 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000857 if (!bp->b_pages[i])
858 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000860 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Dave Chinner611c9942012-04-23 15:59:07 +1000862 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000863 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100864 xfs_warn(target->bt_mount,
865 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Dave Chinner686865f2010-09-24 20:07:47 +1000869 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000873 while (--i >= 0)
874 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000875 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 fail_free_buf:
Dave Chinner3e85c862012-06-22 18:50:09 +1000877 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000878 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 fail:
880 return NULL;
881}
882
883/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 * Increment reference count on buffer, to hold the buffer concurrently
885 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * Must hold the buffer already to call this function.
887 */
888void
Nathan Scottce8e9222006-01-11 15:39:08 +1100889xfs_buf_hold(
890 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000892 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100893 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100897 * Releases a hold on the specified buffer. If the
898 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 */
900void
Nathan Scottce8e9222006-01-11 15:39:08 +1100901xfs_buf_rele(
902 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Dave Chinner74f75a02010-09-24 19:59:04 +1000904 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000906 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Dave Chinner74f75a02010-09-24 19:59:04 +1000908 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100909 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000910 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100911 if (atomic_dec_and_test(&bp->b_hold))
912 xfs_buf_free(bp);
913 return;
914 }
915
Dave Chinner74f75a02010-09-24 19:59:04 +1000916 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100917
Lachlan McIlroy37906892008-08-13 15:42:10 +1000918 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000919 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000920 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100921 atomic_read(&bp->b_lru_ref)) {
922 xfs_buf_lru_add(bp);
923 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100924 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100925 xfs_buf_lru_del(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000926 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner74f75a02010-09-24 19:59:04 +1000927 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
928 spin_unlock(&pag->pag_buf_lock);
929 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100930 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 }
933}
934
935
936/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100937 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100938 *
939 * If we come across a stale, pinned, locked buffer, we know that we are
940 * being asked to lock a buffer that has been reallocated. Because it is
941 * pinned, we know that the log has not been pushed to disk and hence it
942 * will still be locked. Rather than continuing to have trylock attempts
943 * fail until someone else pushes the log, push it ourselves before
944 * returning. This means that the xfsaild will not get stuck trying
945 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 */
947int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200948xfs_buf_trylock(
949 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 int locked;
952
Nathan Scottce8e9222006-01-11 15:39:08 +1100953 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000954 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100955 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000956
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200957 trace_xfs_buf_trylock(bp, _RET_IP_);
958 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100962 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000963 *
964 * If we come across a stale, pinned, locked buffer, we know that we
965 * are being asked to lock a buffer that has been reallocated. Because
966 * it is pinned, we know that the log has not been pushed to disk and
967 * hence it will still be locked. Rather than sleeping until someone
968 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100970void
971xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200972 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000974 trace_xfs_buf_lock(bp, _RET_IP_);
975
Dave Chinnered3b4d62010-05-21 12:07:08 +1000976 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000977 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100978 down(&bp->b_sema);
979 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000980
981 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984void
Nathan Scottce8e9222006-01-11 15:39:08 +1100985xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200986 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Nathan Scottce8e9222006-01-11 15:39:08 +1100988 XB_CLEAR_OWNER(bp);
989 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000990
991 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Nathan Scottce8e9222006-01-11 15:39:08 +1100994STATIC void
995xfs_buf_wait_unpin(
996 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 DECLARE_WAITQUEUE (wait, current);
999
Nathan Scottce8e9222006-01-11 15:39:08 +11001000 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return;
1002
Nathan Scottce8e9222006-01-11 15:39:08 +11001003 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 for (;;) {
1005 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001006 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001008 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001010 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 set_current_state(TASK_RUNNING);
1012}
1013
1014/*
1015 * Buffer Utility Routines
1016 */
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001019xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +00001020 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Dave Chinner1813dd62012-11-14 17:54:40 +11001022 struct xfs_buf *bp =
David Howellsc4028952006-11-22 14:57:56 +00001023 container_of(work, xfs_buf_t, b_iodone_work);
Dave Chinner1813dd62012-11-14 17:54:40 +11001024 bool read = !!(bp->b_flags & XBF_READ);
1025
1026 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Dave Chinnerd5929de2013-02-27 13:25:54 +11001027
1028 /* only validate buffers that were read without errors */
1029 if (read && bp->b_ops && !bp->b_error && (bp->b_flags & XBF_DONE))
Dave Chinner1813dd62012-11-14 17:54:40 +11001030 bp->b_ops->verify_read(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Christoph Hellwig80f6c292010-08-18 05:29:11 -04001032 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001033 (*(bp->b_iodone))(bp);
1034 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 xfs_buf_relse(bp);
Dave Chinner1813dd62012-11-14 17:54:40 +11001036 else {
1037 ASSERT(read && bp->b_ops);
1038 complete(&bp->b_iowait);
1039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042void
Nathan Scottce8e9222006-01-11 15:39:08 +11001043xfs_buf_ioend(
Dave Chinner1813dd62012-11-14 17:54:40 +11001044 struct xfs_buf *bp,
1045 int schedule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Dave Chinner1813dd62012-11-14 17:54:40 +11001047 bool read = !!(bp->b_flags & XBF_READ);
1048
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001049 trace_xfs_buf_iodone(bp, _RET_IP_);
1050
Nathan Scottce8e9222006-01-11 15:39:08 +11001051 if (bp->b_error == 0)
1052 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Dave Chinner1813dd62012-11-14 17:54:40 +11001054 if (bp->b_iodone || (read && bp->b_ops) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +00001056 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +11001057 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 } else {
David Howellsc4028952006-11-22 14:57:56 +00001059 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061 } else {
Dave Chinner1813dd62012-11-14 17:54:40 +11001062 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
David Chinnerb4dd3302008-08-13 16:36:11 +10001063 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065}
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067void
Nathan Scottce8e9222006-01-11 15:39:08 +11001068xfs_buf_ioerror(
1069 xfs_buf_t *bp,
1070 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001073 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001074 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Christoph Hellwig901796a2011-10-10 16:52:49 +00001077void
1078xfs_buf_ioerror_alert(
1079 struct xfs_buf *bp,
1080 const char *func)
1081{
1082 xfs_alert(bp->b_target->bt_mount,
Dave Chinneraa0e8832012-04-23 15:58:52 +10001083"metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
1084 (__uint64_t)XFS_BUF_ADDR(bp), func, bp->b_error, bp->b_length);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001085}
1086
Christoph Hellwig4e234712010-01-13 22:17:56 +00001087/*
1088 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001089 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001090 * so that the proper iodone callbacks get called.
1091 */
1092STATIC int
1093xfs_bioerror(
1094 xfs_buf_t *bp)
1095{
1096#ifdef XFSERRORDEBUG
1097 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1098#endif
1099
1100 /*
1101 * No need to wait until the buffer is unpinned, we aren't flushing it.
1102 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001103 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001104
1105 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001106 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001107 */
1108 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001109 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001110 xfs_buf_stale(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001111
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001112 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001113
1114 return EIO;
1115}
1116
1117/*
1118 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001119 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001120 * This is meant for userdata errors; metadata bufs come with
1121 * iodone functions attached, so that we can track down errors.
1122 */
1123STATIC int
1124xfs_bioerror_relse(
1125 struct xfs_buf *bp)
1126{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001127 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001128 /*
1129 * No need to wait until the buffer is unpinned.
1130 * We aren't flushing it.
1131 *
1132 * chunkhold expects B_DONE to be set, whether
1133 * we actually finish the I/O or not. We don't want to
1134 * change that interface.
1135 */
1136 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001137 XFS_BUF_DONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001138 xfs_buf_stale(bp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001139 bp->b_iodone = NULL;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001140 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001141 /*
1142 * Mark b_error and B_ERROR _both_.
1143 * Lot's of chunkcache code assumes that.
1144 * There's no reason to mark error for
1145 * ASYNC buffers.
1146 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001147 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig5fde0322011-10-10 16:52:44 +00001148 complete(&bp->b_iowait);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001149 } else {
1150 xfs_buf_relse(bp);
1151 }
1152
1153 return EIO;
1154}
1155
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001156STATIC int
Christoph Hellwig4e234712010-01-13 22:17:56 +00001157xfs_bdstrat_cb(
1158 struct xfs_buf *bp)
1159{
Dave Chinnerebad8612010-09-22 10:47:20 +10001160 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001161 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1162 /*
1163 * Metadata write that didn't get logged but
1164 * written delayed anyway. These aren't associated
1165 * with a transaction, and can be ignored.
1166 */
1167 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1168 return xfs_bioerror_relse(bp);
1169 else
1170 return xfs_bioerror(bp);
1171 }
1172
1173 xfs_buf_iorequest(bp);
1174 return 0;
1175}
1176
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001177int
1178xfs_bwrite(
1179 struct xfs_buf *bp)
1180{
1181 int error;
1182
1183 ASSERT(xfs_buf_islocked(bp));
1184
1185 bp->b_flags |= XBF_WRITE;
1186 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
1187
1188 xfs_bdstrat_cb(bp);
1189
1190 error = xfs_buf_iowait(bp);
1191 if (error) {
1192 xfs_force_shutdown(bp->b_target->bt_mount,
1193 SHUTDOWN_META_IO_ERROR);
1194 }
1195 return error;
1196}
1197
Christoph Hellwig4e234712010-01-13 22:17:56 +00001198/*
1199 * Wrapper around bdstrat so that we can stop data from going to disk in case
1200 * we are shutting down the filesystem. Typically user data goes thru this
1201 * path; one of the exceptions is the superblock.
1202 */
1203void
1204xfsbdstrat(
1205 struct xfs_mount *mp,
1206 struct xfs_buf *bp)
1207{
1208 if (XFS_FORCED_SHUTDOWN(mp)) {
1209 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1210 xfs_bioerror_relse(bp);
1211 return;
1212 }
1213
1214 xfs_buf_iorequest(bp);
1215}
1216
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001217STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001218_xfs_buf_ioend(
1219 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int schedule)
1221{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001222 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001223 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Al Viro782e3b32007-10-12 07:17:47 +01001226STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001227xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int error)
1230{
Nathan Scottce8e9222006-01-11 15:39:08 +11001231 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Dave Chinner37eb17e2012-11-12 22:09:46 +11001233 /*
1234 * don't overwrite existing errors - otherwise we can lose errors on
1235 * buffers that require multiple bios to complete.
1236 */
1237 if (!bp->b_error)
1238 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Dave Chinner37eb17e2012-11-12 22:09:46 +11001240 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001241 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1242
Nathan Scottce8e9222006-01-11 15:39:08 +11001243 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
Dave Chinner3e85c862012-06-22 18:50:09 +10001247static void
1248xfs_buf_ioapply_map(
1249 struct xfs_buf *bp,
1250 int map,
1251 int *buf_offset,
1252 int *count,
1253 int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Dave Chinner3e85c862012-06-22 18:50:09 +10001255 int page_index;
1256 int total_nr_pages = bp->b_page_count;
1257 int nr_pages;
1258 struct bio *bio;
1259 sector_t sector = bp->b_maps[map].bm_bn;
1260 int size;
1261 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Nathan Scottce8e9222006-01-11 15:39:08 +11001263 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Dave Chinner3e85c862012-06-22 18:50:09 +10001265 /* skip the pages in the buffer before the start offset */
1266 page_index = 0;
1267 offset = *buf_offset;
1268 while (offset >= PAGE_SIZE) {
1269 page_index++;
1270 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001271 }
1272
Dave Chinner3e85c862012-06-22 18:50:09 +10001273 /*
1274 * Limit the IO size to the length of the current vector, and update the
1275 * remaining IO count for the next time around.
1276 */
1277 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1278 *count -= size;
1279 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001282 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1284 if (nr_pages > total_nr_pages)
1285 nr_pages = total_nr_pages;
1286
1287 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001288 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001290 bio->bi_end_io = xfs_buf_bio_end_io;
1291 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Dave Chinner0e6e8472011-03-26 09:16:45 +11001293
Dave Chinner3e85c862012-06-22 18:50:09 +10001294 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001295 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 if (nbytes > size)
1298 nbytes = size;
1299
Dave Chinner3e85c862012-06-22 18:50:09 +10001300 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1301 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001302 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 break;
1304
1305 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001306 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 size -= nbytes;
1308 total_nr_pages--;
1309 }
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001312 if (xfs_buf_is_vmapped(bp)) {
1313 flush_kernel_vmap_range(bp->b_addr,
1314 xfs_buf_vmap_len(bp));
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 submit_bio(rw, bio);
1317 if (size)
1318 goto next_chunk;
1319 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001320 /*
1321 * This is guaranteed not to be the last io reference count
1322 * because the caller (xfs_buf_iorequest) holds a count itself.
1323 */
1324 atomic_dec(&bp->b_io_remaining);
Nathan Scottce8e9222006-01-11 15:39:08 +11001325 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001326 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001328
1329}
1330
1331STATIC void
1332_xfs_buf_ioapply(
1333 struct xfs_buf *bp)
1334{
1335 struct blk_plug plug;
1336 int rw;
1337 int offset;
1338 int size;
1339 int i;
1340
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001341 /*
1342 * Make sure we capture only current IO errors rather than stale errors
1343 * left over from previous use of the buffer (e.g. failed readahead).
1344 */
1345 bp->b_error = 0;
1346
Dave Chinner3e85c862012-06-22 18:50:09 +10001347 if (bp->b_flags & XBF_WRITE) {
1348 if (bp->b_flags & XBF_SYNCIO)
1349 rw = WRITE_SYNC;
1350 else
1351 rw = WRITE;
1352 if (bp->b_flags & XBF_FUA)
1353 rw |= REQ_FUA;
1354 if (bp->b_flags & XBF_FLUSH)
1355 rw |= REQ_FLUSH;
Dave Chinner1813dd62012-11-14 17:54:40 +11001356
1357 /*
1358 * Run the write verifier callback function if it exists. If
1359 * this function fails it will mark the buffer with an error and
1360 * the IO should not be dispatched.
1361 */
1362 if (bp->b_ops) {
1363 bp->b_ops->verify_write(bp);
1364 if (bp->b_error) {
1365 xfs_force_shutdown(bp->b_target->bt_mount,
1366 SHUTDOWN_CORRUPT_INCORE);
1367 return;
1368 }
1369 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001370 } else if (bp->b_flags & XBF_READ_AHEAD) {
1371 rw = READA;
1372 } else {
1373 rw = READ;
1374 }
1375
1376 /* we only use the buffer cache for meta-data */
1377 rw |= REQ_META;
1378
1379 /*
1380 * Walk all the vectors issuing IO on them. Set up the initial offset
1381 * into the buffer and the desired IO size before we start -
1382 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1383 * subsequent call.
1384 */
1385 offset = bp->b_offset;
1386 size = BBTOB(bp->b_io_length);
1387 blk_start_plug(&plug);
1388 for (i = 0; i < bp->b_map_count; i++) {
1389 xfs_buf_ioapply_map(bp, i, &offset, &size, rw);
1390 if (bp->b_error)
1391 break;
1392 if (size <= 0)
1393 break; /* all done */
1394 }
1395 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
Dave Chinner0e95f192012-04-23 15:58:46 +10001398void
Nathan Scottce8e9222006-01-11 15:39:08 +11001399xfs_buf_iorequest(
1400 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001402 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001404 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Christoph Hellwig375ec692011-08-23 08:28:03 +00001406 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001407 xfs_buf_wait_unpin(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001408 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /* Set the count to 1 initially, this will stop an I/O
1411 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001412 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001414 atomic_set(&bp->b_io_remaining, 1);
1415 _xfs_buf_ioapply(bp);
Christoph Hellwig08023d62012-07-02 06:00:04 -04001416 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Nathan Scottce8e9222006-01-11 15:39:08 +11001418 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
1421/*
Dave Chinner0e95f192012-04-23 15:58:46 +10001422 * Waits for I/O to complete on the buffer supplied. It returns immediately if
1423 * no I/O is pending or there is already a pending error on the buffer. It
1424 * returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 */
1426int
Nathan Scottce8e9222006-01-11 15:39:08 +11001427xfs_buf_iowait(
1428 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001430 trace_xfs_buf_iowait(bp, _RET_IP_);
1431
Dave Chinner0e95f192012-04-23 15:58:46 +10001432 if (!bp->b_error)
1433 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001434
1435 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001436 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
Nathan Scottce8e9222006-01-11 15:39:08 +11001439xfs_caddr_t
1440xfs_buf_offset(
1441 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 size_t offset)
1443{
1444 struct page *page;
1445
Dave Chinner611c9942012-04-23 15:59:07 +10001446 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001447 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Nathan Scottce8e9222006-01-11 15:39:08 +11001449 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001450 page = bp->b_pages[offset >> PAGE_SHIFT];
1451 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
1454/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 * Move data into or out of a buffer.
1456 */
1457void
Nathan Scottce8e9222006-01-11 15:39:08 +11001458xfs_buf_iomove(
1459 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 size_t boff, /* starting buffer offset */
1461 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001462 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001463 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Dave Chinner795cac72012-04-23 15:58:53 +10001465 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 bend = boff + bsize;
1468 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001469 struct page *page;
1470 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Dave Chinner795cac72012-04-23 15:58:53 +10001472 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1473 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1474 page = bp->b_pages[page_index];
1475 csize = min_t(size_t, PAGE_SIZE - page_offset,
1476 BBTOB(bp->b_io_length) - boff);
1477
1478 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001481 case XBRW_ZERO:
Dave Chinner795cac72012-04-23 15:58:53 +10001482 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001484 case XBRW_READ:
Dave Chinner795cac72012-04-23 15:58:53 +10001485 memcpy(data, page_address(page) + page_offset, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001487 case XBRW_WRITE:
Dave Chinner795cac72012-04-23 15:58:53 +10001488 memcpy(page_address(page) + page_offset, data, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490
1491 boff += csize;
1492 data += csize;
1493 }
1494}
1495
1496/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001497 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 */
1499
1500/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001501 * Wait for any bufs with callbacks that have been submitted but have not yet
1502 * returned. These buffers will have an elevated hold count, so wait on those
1503 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 */
1505void
1506xfs_wait_buftarg(
Dave Chinner74f75a02010-09-24 19:59:04 +10001507 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Dave Chinner430cbeb2010-12-02 16:30:55 +11001509 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Dave Chinner430cbeb2010-12-02 16:30:55 +11001511restart:
1512 spin_lock(&btp->bt_lru_lock);
1513 while (!list_empty(&btp->bt_lru)) {
1514 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1515 if (atomic_read(&bp->b_hold) > 1) {
Dave Chinner3b190342013-01-21 23:53:55 +11001516 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
1517 list_move_tail(&bp->b_lru, &btp->bt_lru);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001518 spin_unlock(&btp->bt_lru_lock);
Dave Chinner26af6552010-09-22 10:47:20 +10001519 delay(100);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001520 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001522 /*
Paul Bolle90802ed2011-12-05 13:00:34 +01001523 * clear the LRU reference count so the buffer doesn't get
Dave Chinner430cbeb2010-12-02 16:30:55 +11001524 * ignored in xfs_buf_rele().
1525 */
1526 atomic_set(&bp->b_lru_ref, 0);
1527 spin_unlock(&btp->bt_lru_lock);
1528 xfs_buf_rele(bp);
1529 spin_lock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001531 spin_unlock(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Dave Chinnerff57ab22010-11-30 17:27:57 +11001534int
1535xfs_buftarg_shrink(
1536 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001537 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001538{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001539 struct xfs_buftarg *btp = container_of(shrink,
1540 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001541 struct xfs_buf *bp;
Ying Han1495f232011-05-24 17:12:27 -07001542 int nr_to_scan = sc->nr_to_scan;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001543 LIST_HEAD(dispose);
1544
1545 if (!nr_to_scan)
1546 return btp->bt_lru_nr;
1547
1548 spin_lock(&btp->bt_lru_lock);
1549 while (!list_empty(&btp->bt_lru)) {
1550 if (nr_to_scan-- <= 0)
1551 break;
1552
1553 bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru);
1554
1555 /*
1556 * Decrement the b_lru_ref count unless the value is already
1557 * zero. If the value is already zero, we need to reclaim the
1558 * buffer, otherwise it gets another trip through the LRU.
1559 */
1560 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
1561 list_move_tail(&bp->b_lru, &btp->bt_lru);
1562 continue;
1563 }
1564
1565 /*
1566 * remove the buffer from the LRU now to avoid needing another
1567 * lock round trip inside xfs_buf_rele().
1568 */
1569 list_move(&bp->b_lru, &dispose);
1570 btp->bt_lru_nr--;
Carlos Maiolino6fb8a902012-08-10 15:01:51 -03001571 bp->b_lru_flags |= _XBF_LRU_DISPOSE;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001572 }
Dave Chinner430cbeb2010-12-02 16:30:55 +11001573 spin_unlock(&btp->bt_lru_lock);
1574
1575 while (!list_empty(&dispose)) {
1576 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1577 list_del_init(&bp->b_lru);
1578 xfs_buf_rele(bp);
1579 }
1580
1581 return btp->bt_lru_nr;
David Chinnera6867a62006-01-11 15:37:58 +11001582}
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584void
1585xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001586 struct xfs_mount *mp,
1587 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001589 unregister_shrinker(&btp->bt_shrinker);
1590
Christoph Hellwigb7963132009-03-03 14:48:37 -05001591 if (mp->m_flags & XFS_MOUNT_BARRIER)
1592 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001593
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001594 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597STATIC int
1598xfs_setsize_buftarg_flags(
1599 xfs_buftarg_t *btp,
1600 unsigned int blocksize,
1601 unsigned int sectorsize,
1602 int verbose)
1603{
Nathan Scottce8e9222006-01-11 15:39:08 +11001604 btp->bt_bsize = blocksize;
1605 btp->bt_sshift = ffs(sectorsize) - 1;
1606 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Nathan Scottce8e9222006-01-11 15:39:08 +11001608 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001609 char name[BDEVNAME_SIZE];
1610
1611 bdevname(btp->bt_bdev, name);
1612
Dave Chinner4f107002011-03-07 10:00:35 +11001613 xfs_warn(btp->bt_mount,
1614 "Cannot set_blocksize to %u on device %s\n",
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001615 sectorsize, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return EINVAL;
1617 }
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return 0;
1620}
1621
1622/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001623 * When allocating the initial buffer target we have not yet
1624 * read in the superblock, so don't know what sized sectors
1625 * are being used is at this early stage. Play safe.
1626 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627STATIC int
1628xfs_setsize_buftarg_early(
1629 xfs_buftarg_t *btp,
1630 struct block_device *bdev)
1631{
1632 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001633 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
1636int
1637xfs_setsize_buftarg(
1638 xfs_buftarg_t *btp,
1639 unsigned int blocksize,
1640 unsigned int sectorsize)
1641{
1642 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1643}
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645xfs_buftarg_t *
1646xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001647 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001649 int external,
1650 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 xfs_buftarg_t *btp;
1653
Dave Chinnerb17cb362013-05-20 09:51:12 +10001654 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Dave Chinnerebad8612010-09-22 10:47:20 +10001656 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001657 btp->bt_dev = bdev->bd_dev;
1658 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001659 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1660 if (!btp->bt_bdi)
1661 goto error;
1662
Dave Chinner430cbeb2010-12-02 16:30:55 +11001663 INIT_LIST_HEAD(&btp->bt_lru);
1664 spin_lock_init(&btp->bt_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (xfs_setsize_buftarg_early(btp, bdev))
1666 goto error;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001667 btp->bt_shrinker.shrink = xfs_buftarg_shrink;
1668 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
1669 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return btp;
1671
1672error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001673 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return NULL;
1675}
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001678 * Add a buffer to the delayed write list.
1679 *
1680 * This queues a buffer for writeout if it hasn't already been. Note that
1681 * neither this routine nor the buffer list submission functions perform
1682 * any internal synchronization. It is expected that the lists are thread-local
1683 * to the callers.
1684 *
1685 * Returns true if we queued up the buffer, or false if it already had
1686 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001688bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001689xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001690 struct xfs_buf *bp,
1691 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001693 ASSERT(xfs_buf_islocked(bp));
1694 ASSERT(!(bp->b_flags & XBF_READ));
1695
1696 /*
1697 * If the buffer is already marked delwri it already is queued up
1698 * by someone else for imediate writeout. Just ignore it in that
1699 * case.
1700 */
1701 if (bp->b_flags & _XBF_DELWRI_Q) {
1702 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1703 return false;
1704 }
David Chinnera6867a62006-01-11 15:37:58 +11001705
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001706 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1707
Dave Chinnerd808f612010-02-02 10:13:42 +11001708 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001709 * If a buffer gets written out synchronously or marked stale while it
1710 * is on a delwri list we lazily remove it. To do this, the other party
1711 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1712 * It remains referenced and on the list. In a rare corner case it
1713 * might get readded to a delwri list after the synchronous writeout, in
1714 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001715 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001716 bp->b_flags |= _XBF_DELWRI_Q;
1717 if (list_empty(&bp->b_list)) {
1718 atomic_inc(&bp->b_hold);
1719 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001720 }
David Chinner585e6d82007-02-10 18:32:29 +11001721
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001722 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001723}
1724
Dave Chinner089716a2010-01-26 15:13:25 +11001725/*
1726 * Compare function is more complex than it needs to be because
1727 * the return value is only 32 bits and we are doing comparisons
1728 * on 64 bit values
1729 */
1730static int
1731xfs_buf_cmp(
1732 void *priv,
1733 struct list_head *a,
1734 struct list_head *b)
1735{
1736 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1737 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1738 xfs_daddr_t diff;
1739
Mark Tinguelyf4b42422012-12-04 17:18:02 -06001740 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11001741 if (diff < 0)
1742 return -1;
1743 if (diff > 0)
1744 return 1;
1745 return 0;
1746}
1747
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001748static int
1749__xfs_buf_delwri_submit(
1750 struct list_head *buffer_list,
1751 struct list_head *io_list,
1752 bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001754 struct blk_plug plug;
1755 struct xfs_buf *bp, *n;
1756 int pinned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001758 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
1759 if (!wait) {
1760 if (xfs_buf_ispinned(bp)) {
1761 pinned++;
1762 continue;
1763 }
1764 if (!xfs_buf_trylock(bp))
1765 continue;
1766 } else {
1767 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001770 /*
1771 * Someone else might have written the buffer synchronously or
1772 * marked it stale in the meantime. In that case only the
1773 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1774 * reference and remove it from the list here.
1775 */
1776 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1777 list_del_init(&bp->b_list);
1778 xfs_buf_relse(bp);
1779 continue;
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001782 list_move_tail(&bp->b_list, io_list);
1783 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001786 list_sort(NULL, io_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001787
1788 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001789 list_for_each_entry_safe(bp, n, io_list, b_list) {
1790 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
1791 bp->b_flags |= XBF_WRITE;
1792
1793 if (!wait) {
1794 bp->b_flags |= XBF_ASYNC;
1795 list_del_init(&bp->b_list);
Dave Chinner089716a2010-01-26 15:13:25 +11001796 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001797 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001799 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001801 return pinned;
1802}
Nathan Scottf07c2252006-09-28 10:52:15 +10001803
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001804/*
1805 * Write out a buffer list asynchronously.
1806 *
1807 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1808 * out and not wait for I/O completion on any of the buffers. This interface
1809 * is only safely useable for callers that can track I/O completion by higher
1810 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1811 * function.
1812 */
1813int
1814xfs_buf_delwri_submit_nowait(
1815 struct list_head *buffer_list)
1816{
1817 LIST_HEAD (io_list);
1818 return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
1819}
1820
1821/*
1822 * Write out a buffer list synchronously.
1823 *
1824 * This will take the @buffer_list, write all buffers out and wait for I/O
1825 * completion on all of the buffers. @buffer_list is consumed by the function,
1826 * so callers must have some other way of tracking buffers if they require such
1827 * functionality.
1828 */
1829int
1830xfs_buf_delwri_submit(
1831 struct list_head *buffer_list)
1832{
1833 LIST_HEAD (io_list);
1834 int error = 0, error2;
1835 struct xfs_buf *bp;
1836
1837 __xfs_buf_delwri_submit(buffer_list, &io_list, true);
1838
1839 /* Wait for IO to complete. */
1840 while (!list_empty(&io_list)) {
1841 bp = list_first_entry(&io_list, struct xfs_buf, b_list);
1842
1843 list_del_init(&bp->b_list);
1844 error2 = xfs_buf_iowait(bp);
1845 xfs_buf_relse(bp);
1846 if (!error)
1847 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
1849
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001850 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001853int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001854xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Nathan Scott87582802006-03-14 13:18:19 +11001856 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1857 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001858 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001859 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001860
Dave Chinner51749e42010-09-08 09:00:22 +00001861 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001862 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001863 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001864 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001868 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001869 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001870 out:
Nathan Scott87582802006-03-14 13:18:19 +11001871 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874void
Nathan Scottce8e9222006-01-11 15:39:08 +11001875xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001877 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001878 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}