blob: 665ff792d665bf3840e4e9eac35285b3a49a51aa [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 Chinnere80dfa12013-08-28 10:18:05 +100089static void
Dave Chinner430cbeb2010-12-02 16:30:55 +110090xfs_buf_lru_add(
91 struct xfs_buf *bp)
92{
Dave Chinnere80dfa12013-08-28 10:18:05 +100093 if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
Carlos Maiolino6fb8a902012-08-10 15:01:51 -030094 bp->b_lru_flags &= ~_XBF_LRU_DISPOSE;
Dave Chinnere80dfa12013-08-28 10:18:05 +100095 atomic_inc(&bp->b_hold);
Dave Chinner430cbeb2010-12-02 16:30:55 +110096 }
Dave Chinner430cbeb2010-12-02 16:30:55 +110097}
98
99/*
100 * xfs_buf_lru_del - remove a buffer from the LRU
101 *
102 * The unlocked check is safe here because it only occurs when there are not
103 * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there
104 * to optimise the shrinker removing the buffer from the LRU and calling
Dave Chinnere80dfa12013-08-28 10:18:05 +1000105 * xfs_buf_free().
Dave Chinner430cbeb2010-12-02 16:30:55 +1100106 */
Dave Chinnere80dfa12013-08-28 10:18:05 +1000107static void
Dave Chinner430cbeb2010-12-02 16:30:55 +1100108xfs_buf_lru_del(
109 struct xfs_buf *bp)
110{
Dave Chinnere80dfa12013-08-28 10:18:05 +1000111 list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100112}
113
114/*
115 * When we mark a buffer stale, we remove the buffer from the LRU and clear the
116 * b_lru_ref count so that the buffer is freed immediately when the buffer
117 * reference count falls to zero. If the buffer is already on the LRU, we need
118 * to remove the reference that LRU holds on the buffer.
119 *
120 * This prevents build-up of stale buffers on the LRU.
121 */
122void
123xfs_buf_stale(
124 struct xfs_buf *bp)
125{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000126 ASSERT(xfs_buf_islocked(bp));
127
Dave Chinner430cbeb2010-12-02 16:30:55 +1100128 bp->b_flags |= XBF_STALE;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000129
130 /*
131 * Clear the delwri status so that a delwri queue walker will not
132 * flush this buffer to disk now that it is stale. The delwri queue has
133 * a reference to the buffer, so this is safe to do.
134 */
135 bp->b_flags &= ~_XBF_DELWRI_Q;
136
Dave Chinner430cbeb2010-12-02 16:30:55 +1100137 atomic_set(&(bp)->b_lru_ref, 0);
Dave Chinnere80dfa12013-08-28 10:18:05 +1000138 if (!(bp->b_lru_flags & _XBF_LRU_DISPOSE) &&
139 (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
140 atomic_dec(&bp->b_hold);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100141
Dave Chinner430cbeb2010-12-02 16:30:55 +1100142 ASSERT(atomic_read(&bp->b_hold) >= 1);
143}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Dave Chinner3e85c862012-06-22 18:50:09 +1000145static int
146xfs_buf_get_maps(
147 struct xfs_buf *bp,
148 int map_count)
149{
150 ASSERT(bp->b_maps == NULL);
151 bp->b_map_count = map_count;
152
153 if (map_count == 1) {
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600154 bp->b_maps = &bp->__b_map;
Dave Chinner3e85c862012-06-22 18:50:09 +1000155 return 0;
156 }
157
158 bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
159 KM_NOFS);
160 if (!bp->b_maps)
161 return ENOMEM;
162 return 0;
163}
164
165/*
166 * Frees b_pages if it was allocated.
167 */
168static void
169xfs_buf_free_maps(
170 struct xfs_buf *bp)
171{
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600172 if (bp->b_maps != &bp->__b_map) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000173 kmem_free(bp->b_maps);
174 bp->b_maps = NULL;
175 }
176}
177
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000178struct xfs_buf *
Dave Chinner3e85c862012-06-22 18:50:09 +1000179_xfs_buf_alloc(
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000180 struct xfs_buftarg *target,
Dave Chinner3e85c862012-06-22 18:50:09 +1000181 struct xfs_buf_map *map,
182 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100183 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000185 struct xfs_buf *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000186 int error;
187 int i;
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000188
Dave Chinneraa5c1582012-04-23 15:58:56 +1000189 bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000190 if (unlikely(!bp))
191 return NULL;
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /*
Dave Chinner12bcb3f2012-04-23 15:59:05 +1000194 * We don't want certain flags to appear in b_flags unless they are
195 * specifically set by later operations on the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
Dave Chinner611c9942012-04-23 15:59:07 +1000197 flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Nathan Scottce8e9222006-01-11 15:39:08 +1100199 atomic_set(&bp->b_hold, 1);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100200 atomic_set(&bp->b_lru_ref, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000201 init_completion(&bp->b_iowait);
Dave Chinner430cbeb2010-12-02 16:30:55 +1100202 INIT_LIST_HEAD(&bp->b_lru);
Nathan Scottce8e9222006-01-11 15:39:08 +1100203 INIT_LIST_HEAD(&bp->b_list);
Dave Chinner74f75a02010-09-24 19:59:04 +1000204 RB_CLEAR_NODE(&bp->b_rbnode);
Thomas Gleixnera731cd112010-09-07 14:33:15 +0000205 sema_init(&bp->b_sema, 0); /* held, no waiters */
Nathan Scottce8e9222006-01-11 15:39:08 +1100206 XB_SET_OWNER(bp);
207 bp->b_target = target;
Dave Chinner3e85c862012-06-22 18:50:09 +1000208 bp->b_flags = flags;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /*
Dave Chinneraa0e8832012-04-23 15:58:52 +1000211 * Set length and io_length to the same value initially.
212 * I/O routines should use io_length, which will be the same in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * most cases but may be reset (e.g. XFS recovery).
214 */
Dave Chinner3e85c862012-06-22 18:50:09 +1000215 error = xfs_buf_get_maps(bp, nmaps);
216 if (error) {
217 kmem_zone_free(xfs_buf_zone, bp);
218 return NULL;
219 }
220
221 bp->b_bn = map[0].bm_bn;
222 bp->b_length = 0;
223 for (i = 0; i < nmaps; i++) {
224 bp->b_maps[i].bm_bn = map[i].bm_bn;
225 bp->b_maps[i].bm_len = map[i].bm_len;
226 bp->b_length += map[i].bm_len;
227 }
228 bp->b_io_length = bp->b_length;
229
Nathan Scottce8e9222006-01-11 15:39:08 +1100230 atomic_set(&bp->b_pin_count, 0);
231 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Nathan Scottce8e9222006-01-11 15:39:08 +1100233 XFS_STATS_INC(xb_create);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000234 trace_xfs_buf_init(bp, _RET_IP_);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000235
236 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100240 * Allocate a page array capable of holding a specified number
241 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100244_xfs_buf_get_pages(
245 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100247 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100250 if (bp->b_pages == NULL) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100251 bp->b_page_count = page_count;
252 if (page_count <= XB_PAGES) {
253 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100255 bp->b_pages = kmem_alloc(sizeof(struct page *) *
Dave Chinneraa5c1582012-04-23 15:58:56 +1000256 page_count, KM_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +1100257 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return -ENOMEM;
259 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100260 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262 return 0;
263}
264
265/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100266 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
268STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100269_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 xfs_buf_t *bp)
271{
Nathan Scottce8e9222006-01-11 15:39:08 +1100272 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000273 kmem_free(bp->b_pages);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000274 bp->b_pages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276}
277
278/*
279 * Releases the specified buffer.
280 *
281 * The modification state of any associated pages is left unchanged.
Zhi Yong Wub46fe822013-08-07 10:10:59 +0000282 * The buffer must not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * hashed and refcounted buffers
284 */
285void
Nathan Scottce8e9222006-01-11 15:39:08 +1100286xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 xfs_buf_t *bp)
288{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000289 trace_xfs_buf_free(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Dave Chinner430cbeb2010-12-02 16:30:55 +1100291 ASSERT(list_empty(&bp->b_lru));
292
Dave Chinner0e6e8472011-03-26 09:16:45 +1100293 if (bp->b_flags & _XBF_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 uint i;
295
James Bottomley73c77e22010-01-25 11:42:24 -0600296 if (xfs_buf_is_vmapped(bp))
Alex Elder8a262e52010-03-16 18:55:56 +0000297 vm_unmap_ram(bp->b_addr - bp->b_offset,
298 bp->b_page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Nathan Scott948ecdb2006-09-28 11:03:13 +1000300 for (i = 0; i < bp->b_page_count; i++) {
301 struct page *page = bp->b_pages[i];
302
Dave Chinner0e6e8472011-03-26 09:16:45 +1100303 __free_page(page);
Nathan Scott948ecdb2006-09-28 11:03:13 +1000304 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100305 } else if (bp->b_flags & _XBF_KMEM)
306 kmem_free(bp->b_addr);
Dave Chinner3fc98b12009-12-14 23:11:57 +0000307 _xfs_buf_free_pages(bp);
Dave Chinner3e85c862012-06-22 18:50:09 +1000308 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000309 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100313 * Allocates all the pages for buffer in question and builds it's page list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
315STATIC int
Dave Chinner0e6e8472011-03-26 09:16:45 +1100316xfs_buf_allocate_memory(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 xfs_buf_t *bp,
318 uint flags)
319{
Dave Chinneraa0e8832012-04-23 15:58:52 +1000320 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100322 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unsigned short page_count, i;
Dave Chinner795cac72012-04-23 15:58:53 +1000324 xfs_off_t start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int error;
326
Dave Chinner0e6e8472011-03-26 09:16:45 +1100327 /*
328 * for buffers that are contained within a single page, just allocate
329 * the memory from the heap - there's no need for the complexity of
330 * page arrays to keep allocation down to order 0.
331 */
Dave Chinner795cac72012-04-23 15:58:53 +1000332 size = BBTOB(bp->b_length);
333 if (size < PAGE_SIZE) {
Dave Chinneraa5c1582012-04-23 15:58:56 +1000334 bp->b_addr = kmem_alloc(size, KM_NOFS);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100335 if (!bp->b_addr) {
336 /* low memory - use alloc_page loop instead */
337 goto use_alloc_page;
338 }
339
Dave Chinner795cac72012-04-23 15:58:53 +1000340 if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
Dave Chinner0e6e8472011-03-26 09:16:45 +1100341 ((unsigned long)bp->b_addr & PAGE_MASK)) {
342 /* b_addr spans two pages - use alloc_page instead */
343 kmem_free(bp->b_addr);
344 bp->b_addr = NULL;
345 goto use_alloc_page;
346 }
347 bp->b_offset = offset_in_page(bp->b_addr);
348 bp->b_pages = bp->b_page_array;
349 bp->b_pages[0] = virt_to_page(bp->b_addr);
350 bp->b_page_count = 1;
Dave Chinner611c9942012-04-23 15:59:07 +1000351 bp->b_flags |= _XBF_KMEM;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100352 return 0;
353 }
354
355use_alloc_page:
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600356 start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
357 end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000358 >> PAGE_SHIFT;
Dave Chinner795cac72012-04-23 15:58:53 +1000359 page_count = end - start;
Nathan Scottce8e9222006-01-11 15:39:08 +1100360 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (unlikely(error))
362 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Nathan Scottce8e9222006-01-11 15:39:08 +1100364 offset = bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100365 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Nathan Scottce8e9222006-01-11 15:39:08 +1100367 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct page *page;
369 uint retries = 0;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100370retry:
371 page = alloc_page(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100373 if (flags & XBF_READ_AHEAD) {
374 bp->b_page_count = i;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100375 error = ENOMEM;
376 goto out_free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378
379 /*
380 * This could deadlock.
381 *
382 * But until all the XFS lowlevel code is revamped to
383 * handle buffer allocation failures we can't do much.
384 */
385 if (!(++retries % 100))
Dave Chinner4f107002011-03-07 10:00:35 +1100386 xfs_err(NULL,
387 "possible memory allocation deadlock in %s (mode:0x%x)",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000388 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Nathan Scottce8e9222006-01-11 15:39:08 +1100390 XFS_STATS_INC(xb_page_retries);
Jens Axboe8aa7e842009-07-09 14:52:32 +0200391 congestion_wait(BLK_RW_ASYNC, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 goto retry;
393 }
394
Nathan Scottce8e9222006-01-11 15:39:08 +1100395 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Dave Chinner0e6e8472011-03-26 09:16:45 +1100397 nbytes = min_t(size_t, size, PAGE_SIZE - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 size -= nbytes;
Nathan Scottce8e9222006-01-11 15:39:08 +1100399 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 offset = 0;
401 }
Dave Chinner0e6e8472011-03-26 09:16:45 +1100402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Dave Chinner0e6e8472011-03-26 09:16:45 +1100404out_free_pages:
405 for (i = 0; i < bp->b_page_count; i++)
406 __free_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return error;
408}
409
410/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300411 * Map buffer into kernel address-space if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
413STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100414_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 xfs_buf_t *bp,
416 uint flags)
417{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100418 ASSERT(bp->b_flags & _XBF_PAGES);
Nathan Scottce8e9222006-01-11 15:39:08 +1100419 if (bp->b_page_count == 1) {
Dave Chinner0e6e8472011-03-26 09:16:45 +1100420 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100421 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
Dave Chinner611c9942012-04-23 15:59:07 +1000422 } else if (flags & XBF_UNMAPPED) {
423 bp->b_addr = NULL;
424 } else {
Dave Chinnera19fb382011-03-26 09:13:42 +1100425 int retried = 0;
426
427 do {
428 bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
429 -1, PAGE_KERNEL);
430 if (bp->b_addr)
431 break;
432 vm_unmap_aliases();
433 } while (retried++ <= 1);
434
435 if (!bp->b_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100437 bp->b_addr += bp->b_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
440 return 0;
441}
442
443/*
444 * Finding and Reading Buffers
445 */
446
447/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100448 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * a given range of an inode. The buffer is returned
Chandra Seetharamaneabbaf12011-09-08 20:18:50 +0000450 * locked. No I/O is implied by this call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 */
452xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100453_xfs_buf_find(
Dave Chinnere70b73f2012-04-23 15:58:49 +1000454 struct xfs_buftarg *btp,
Dave Chinner3e85c862012-06-22 18:50:09 +1000455 struct xfs_buf_map *map,
456 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100457 xfs_buf_flags_t flags,
458 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000460 size_t numbytes;
Dave Chinner74f75a02010-09-24 19:59:04 +1000461 struct xfs_perag *pag;
462 struct rb_node **rbp;
463 struct rb_node *parent;
464 xfs_buf_t *bp;
Dave Chinner3e85c862012-06-22 18:50:09 +1000465 xfs_daddr_t blkno = map[0].bm_bn;
Dave Chinner10616b82013-01-21 23:53:52 +1100466 xfs_daddr_t eofs;
Dave Chinner3e85c862012-06-22 18:50:09 +1000467 int numblks = 0;
468 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Dave Chinner3e85c862012-06-22 18:50:09 +1000470 for (i = 0; i < nmaps; i++)
471 numblks += map[i].bm_len;
Dave Chinnere70b73f2012-04-23 15:58:49 +1000472 numbytes = BBTOB(numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /* Check for IOs smaller than the sector size / not sector aligned */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000475 ASSERT(!(numbytes < (1 << btp->bt_sshift)));
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000476 ASSERT(!(BBTOB(blkno) & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Dave Chinner10616b82013-01-21 23:53:52 +1100478 /*
479 * Corrupted block numbers can get through to here, unfortunately, so we
480 * have to check that the buffer falls within the filesystem bounds.
481 */
482 eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
483 if (blkno >= eofs) {
484 /*
485 * XXX (dgc): we should really be returning EFSCORRUPTED here,
486 * but none of the higher level infrastructure supports
487 * returning a specific error on buffer lookup failures.
488 */
489 xfs_alert(btp->bt_mount,
490 "%s: Block out of range: block 0x%llx, EOFS 0x%llx ",
491 __func__, blkno, eofs);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000492 WARN_ON(1);
Dave Chinner10616b82013-01-21 23:53:52 +1100493 return NULL;
494 }
495
Dave Chinner74f75a02010-09-24 19:59:04 +1000496 /* get tree root */
497 pag = xfs_perag_get(btp->bt_mount,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000498 xfs_daddr_to_agno(btp->bt_mount, blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Dave Chinner74f75a02010-09-24 19:59:04 +1000500 /* walk tree */
501 spin_lock(&pag->pag_buf_lock);
502 rbp = &pag->pag_buf_tree.rb_node;
503 parent = NULL;
504 bp = NULL;
505 while (*rbp) {
506 parent = *rbp;
507 bp = rb_entry(parent, struct xfs_buf, b_rbnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000509 if (blkno < bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000510 rbp = &(*rbp)->rb_left;
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000511 else if (blkno > bp->b_bn)
Dave Chinner74f75a02010-09-24 19:59:04 +1000512 rbp = &(*rbp)->rb_right;
513 else {
514 /*
Dave Chinnerde1cbee2012-04-23 15:58:50 +1000515 * found a block number match. If the range doesn't
Dave Chinner74f75a02010-09-24 19:59:04 +1000516 * match, the only way this is allowed is if the buffer
517 * in the cache is stale and the transaction that made
518 * it stale has not yet committed. i.e. we are
519 * reallocating a busy extent. Skip this buffer and
520 * continue searching to the right for an exact match.
521 */
Dave Chinner4e94b712012-04-23 15:58:51 +1000522 if (bp->b_length != numblks) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000523 ASSERT(bp->b_flags & XBF_STALE);
524 rbp = &(*rbp)->rb_right;
525 continue;
526 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100527 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 goto found;
529 }
530 }
531
532 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100533 if (new_bp) {
Dave Chinner74f75a02010-09-24 19:59:04 +1000534 rb_link_node(&new_bp->b_rbnode, parent, rbp);
535 rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
536 /* the buffer keeps the perag reference until it is freed */
537 new_bp->b_pag = pag;
538 spin_unlock(&pag->pag_buf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100540 XFS_STATS_INC(xb_miss_locked);
Dave Chinner74f75a02010-09-24 19:59:04 +1000541 spin_unlock(&pag->pag_buf_lock);
542 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100544 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546found:
Dave Chinner74f75a02010-09-24 19:59:04 +1000547 spin_unlock(&pag->pag_buf_lock);
548 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200550 if (!xfs_buf_trylock(bp)) {
551 if (flags & XBF_TRYLOCK) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100552 xfs_buf_rele(bp);
553 XFS_STATS_INC(xb_busy_locked);
554 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200556 xfs_buf_lock(bp);
557 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
Dave Chinner0e6e8472011-03-26 09:16:45 +1100560 /*
561 * if the buffer is stale, clear all the external state associated with
562 * it. We need to keep flags such as how we allocated the buffer memory
563 * intact here.
564 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100565 if (bp->b_flags & XBF_STALE) {
566 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
Dave Chinnercfb02852012-11-12 22:54:19 +1100567 ASSERT(bp->b_iodone == NULL);
Dave Chinner611c9942012-04-23 15:59:07 +1000568 bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
Dave Chinner1813dd62012-11-14 17:54:40 +1100569 bp->b_ops = NULL;
David Chinner2f926582005-09-05 08:33:35 +1000570 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000571
572 trace_xfs_buf_find(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100573 XFS_STATS_INC(xb_get_locked);
574 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577/*
Dave Chinner38158322011-09-30 04:45:02 +0000578 * Assembles a buffer covering the specified range. The code is optimised for
579 * cache hits, as metadata intensive workloads will see 3 orders of magnitude
580 * more hits than misses.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
Dave Chinner38158322011-09-30 04:45:02 +0000582struct xfs_buf *
Dave Chinner6dde2702012-06-22 18:50:10 +1000583xfs_buf_get_map(
584 struct xfs_buftarg *target,
585 struct xfs_buf_map *map,
586 int nmaps,
Nathan Scottce8e9222006-01-11 15:39:08 +1100587 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Dave Chinner38158322011-09-30 04:45:02 +0000589 struct xfs_buf *bp;
590 struct xfs_buf *new_bp;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100591 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Dave Chinner6dde2702012-06-22 18:50:10 +1000593 bp = _xfs_buf_find(target, map, nmaps, flags, NULL);
Dave Chinner38158322011-09-30 04:45:02 +0000594 if (likely(bp))
595 goto found;
596
Dave Chinner6dde2702012-06-22 18:50:10 +1000597 new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100598 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return NULL;
600
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000601 error = xfs_buf_allocate_memory(new_bp, flags);
602 if (error) {
Dave Chinner3e85c862012-06-22 18:50:09 +1000603 xfs_buf_free(new_bp);
Dave Chinner38158322011-09-30 04:45:02 +0000604 return NULL;
605 }
606
Dave Chinner6dde2702012-06-22 18:50:10 +1000607 bp = _xfs_buf_find(target, map, nmaps, flags, new_bp);
Dave Chinnerfe2429b2012-04-23 15:58:45 +1000608 if (!bp) {
609 xfs_buf_free(new_bp);
610 return NULL;
611 }
612
613 if (bp != new_bp)
614 xfs_buf_free(new_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Dave Chinner38158322011-09-30 04:45:02 +0000616found:
Dave Chinner611c9942012-04-23 15:59:07 +1000617 if (!bp->b_addr) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100618 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100620 xfs_warn(target->bt_mount,
621 "%s: failed to map pages\n", __func__);
Dave Chinnera8acad72012-04-23 15:58:54 +1000622 xfs_buf_relse(bp);
623 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625 }
626
Nathan Scottce8e9222006-01-11 15:39:08 +1100627 XFS_STATS_INC(xb_get);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000628 trace_xfs_buf_get(bp, flags, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100629 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100632STATIC int
633_xfs_buf_read(
634 xfs_buf_t *bp,
635 xfs_buf_flags_t flags)
636{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000637 ASSERT(!(flags & XBF_WRITE));
Mark Tinguelyf4b42422012-12-04 17:18:02 -0600638 ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100639
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000640 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig1d5ae5d2011-07-08 14:36:32 +0200641 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100642
Dave Chinner0e95f192012-04-23 15:58:46 +1000643 xfs_buf_iorequest(bp);
644 if (flags & XBF_ASYNC)
645 return 0;
Dave Chinnerec53d1d2010-07-20 17:52:59 +1000646 return xfs_buf_iowait(bp);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100647}
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649xfs_buf_t *
Dave Chinner6dde2702012-06-22 18:50:10 +1000650xfs_buf_read_map(
651 struct xfs_buftarg *target,
652 struct xfs_buf_map *map,
653 int nmaps,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100654 xfs_buf_flags_t flags,
Dave Chinner1813dd62012-11-14 17:54:40 +1100655 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Dave Chinner6dde2702012-06-22 18:50:10 +1000657 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Nathan Scottce8e9222006-01-11 15:39:08 +1100659 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Dave Chinner6dde2702012-06-22 18:50:10 +1000661 bp = xfs_buf_get_map(target, map, nmaps, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100662 if (bp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000663 trace_xfs_buf_read(bp, flags, _RET_IP_);
664
Nathan Scottce8e9222006-01-11 15:39:08 +1100665 if (!XFS_BUF_ISDONE(bp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100666 XFS_STATS_INC(xb_get_read);
Dave Chinner1813dd62012-11-14 17:54:40 +1100667 bp->b_ops = ops;
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100668 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100669 } else if (flags & XBF_ASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /*
671 * Read ahead call which is already satisfied,
672 * drop the buffer
673 */
Dave Chinnera8acad72012-04-23 15:58:54 +1000674 xfs_buf_relse(bp);
675 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100678 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 }
681
Nathan Scottce8e9222006-01-11 15:39:08 +1100682 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100686 * If we are not low on memory then do the readahead in a deadlock
687 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
689void
Dave Chinner6dde2702012-06-22 18:50:10 +1000690xfs_buf_readahead_map(
691 struct xfs_buftarg *target,
692 struct xfs_buf_map *map,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100693 int nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100694 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Dave Chinner0e6e8472011-03-26 09:16:45 +1100696 if (bdi_read_congested(target->bt_bdi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return;
698
Dave Chinner6dde2702012-06-22 18:50:10 +1000699 xfs_buf_read_map(target, map, nmaps,
Dave Chinner1813dd62012-11-14 17:54:40 +1100700 XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Dave Chinner5adc94c2010-09-24 21:58:31 +1000703/*
704 * Read an uncached buffer from disk. Allocates and returns a locked
705 * buffer containing the disk contents or nothing.
706 */
707struct xfs_buf *
708xfs_buf_read_uncached(
Dave Chinner5adc94c2010-09-24 21:58:31 +1000709 struct xfs_buftarg *target,
710 xfs_daddr_t daddr,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000711 size_t numblks,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100712 int flags,
Dave Chinner1813dd62012-11-14 17:54:40 +1100713 const struct xfs_buf_ops *ops)
Dave Chinner5adc94c2010-09-24 21:58:31 +1000714{
Dave Chinnereab4e632012-11-12 22:54:02 +1100715 struct xfs_buf *bp;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000716
Dave Chinnere70b73f2012-04-23 15:58:49 +1000717 bp = xfs_buf_get_uncached(target, numblks, flags);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000718 if (!bp)
719 return NULL;
720
721 /* set up the buffer for a read IO */
Dave Chinner3e85c862012-06-22 18:50:09 +1000722 ASSERT(bp->b_map_count == 1);
723 bp->b_bn = daddr;
724 bp->b_maps[0].bm_bn = daddr;
Dave Chinnercbb7baa2012-06-22 18:50:08 +1000725 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +1100726 bp->b_ops = ops;
Dave Chinner5adc94c2010-09-24 21:58:31 +1000727
Dave Chinnere70b73f2012-04-23 15:58:49 +1000728 xfsbdstrat(target->bt_mount, bp);
Dave Chinnereab4e632012-11-12 22:54:02 +1100729 xfs_buf_iowait(bp);
Dave Chinner5adc94c2010-09-24 21:58:31 +1000730 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Dave Chinner44396472011-04-21 09:34:27 +0000733/*
734 * Return a buffer allocated as an empty buffer and associated to external
735 * memory via xfs_buf_associate_memory() back to it's empty state.
736 */
737void
738xfs_buf_set_empty(
739 struct xfs_buf *bp,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000740 size_t numblks)
Dave Chinner44396472011-04-21 09:34:27 +0000741{
742 if (bp->b_pages)
743 _xfs_buf_free_pages(bp);
744
745 bp->b_pages = NULL;
746 bp->b_page_count = 0;
747 bp->b_addr = NULL;
Dave Chinner4e94b712012-04-23 15:58:51 +1000748 bp->b_length = numblks;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000749 bp->b_io_length = numblks;
Dave Chinner3e85c862012-06-22 18:50:09 +1000750
751 ASSERT(bp->b_map_count == 1);
Dave Chinner44396472011-04-21 09:34:27 +0000752 bp->b_bn = XFS_BUF_DADDR_NULL;
Dave Chinner3e85c862012-06-22 18:50:09 +1000753 bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL;
754 bp->b_maps[0].bm_len = bp->b_length;
Dave Chinner44396472011-04-21 09:34:27 +0000755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757static inline struct page *
758mem_to_page(
759 void *addr)
760{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800761 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return virt_to_page(addr);
763 } else {
764 return vmalloc_to_page(addr);
765 }
766}
767
768int
Nathan Scottce8e9222006-01-11 15:39:08 +1100769xfs_buf_associate_memory(
770 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 void *mem,
772 size_t len)
773{
774 int rval;
775 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100776 unsigned long pageaddr;
777 unsigned long offset;
778 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int page_count;
780
Dave Chinner0e6e8472011-03-26 09:16:45 +1100781 pageaddr = (unsigned long)mem & PAGE_MASK;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100782 offset = (unsigned long)mem - pageaddr;
Dave Chinner0e6e8472011-03-26 09:16:45 +1100783 buflen = PAGE_ALIGN(len + offset);
784 page_count = buflen >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100787 if (bp->b_pages)
788 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Nathan Scottce8e9222006-01-11 15:39:08 +1100790 bp->b_pages = NULL;
791 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Dave Chinneraa5c1582012-04-23 15:58:56 +1000793 rval = _xfs_buf_get_pages(bp, page_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (rval)
795 return rval;
796
Nathan Scottce8e9222006-01-11 15:39:08 +1100797 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100799 for (i = 0; i < bp->b_page_count; i++) {
800 bp->b_pages[i] = mem_to_page((void *)pageaddr);
Dave Chinner0e6e8472011-03-26 09:16:45 +1100801 pageaddr += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Dave Chinneraa0e8832012-04-23 15:58:52 +1000804 bp->b_io_length = BTOBB(len);
Dave Chinner4e94b712012-04-23 15:58:51 +1000805 bp->b_length = BTOBB(buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 return 0;
808}
809
810xfs_buf_t *
Dave Chinner686865f2010-09-24 20:07:47 +1000811xfs_buf_get_uncached(
812 struct xfs_buftarg *target,
Dave Chinnere70b73f2012-04-23 15:58:49 +1000813 size_t numblks,
Dave Chinner686865f2010-09-24 20:07:47 +1000814 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Dave Chinnere70b73f2012-04-23 15:58:49 +1000816 unsigned long page_count;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000817 int error, i;
Dave Chinner3e85c862012-06-22 18:50:09 +1000818 struct xfs_buf *bp;
819 DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Dave Chinner3e85c862012-06-22 18:50:09 +1000821 bp = _xfs_buf_alloc(target, &map, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (unlikely(bp == NULL))
823 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Dave Chinnere70b73f2012-04-23 15:58:49 +1000825 page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000826 error = _xfs_buf_get_pages(bp, page_count, 0);
827 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 goto fail_free_buf;
829
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000830 for (i = 0; i < page_count; i++) {
Dave Chinner686865f2010-09-24 20:07:47 +1000831 bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000832 if (!bp->b_pages[i])
833 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000835 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Dave Chinner611c9942012-04-23 15:59:07 +1000837 error = _xfs_buf_map_pages(bp, 0);
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000838 if (unlikely(error)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100839 xfs_warn(target->bt_mount,
840 "%s: failed to map pages\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Dave Chinner686865f2010-09-24 20:07:47 +1000844 trace_xfs_buf_get_uncached(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000848 while (--i >= 0)
849 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000850 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 fail_free_buf:
Dave Chinner3e85c862012-06-22 18:50:09 +1000852 xfs_buf_free_maps(bp);
Christoph Hellwig4347b9d2011-10-10 16:52:48 +0000853 kmem_zone_free(xfs_buf_zone, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 fail:
855 return NULL;
856}
857
858/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 * Increment reference count on buffer, to hold the buffer concurrently
860 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 * Must hold the buffer already to call this function.
862 */
863void
Nathan Scottce8e9222006-01-11 15:39:08 +1100864xfs_buf_hold(
865 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000867 trace_xfs_buf_hold(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +1100868 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100872 * Releases a hold on the specified buffer. If the
873 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 */
875void
Nathan Scottce8e9222006-01-11 15:39:08 +1100876xfs_buf_rele(
877 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Dave Chinner74f75a02010-09-24 19:59:04 +1000879 struct xfs_perag *pag = bp->b_pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000881 trace_xfs_buf_rele(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Dave Chinner74f75a02010-09-24 19:59:04 +1000883 if (!pag) {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100884 ASSERT(list_empty(&bp->b_lru));
Dave Chinner74f75a02010-09-24 19:59:04 +1000885 ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
Nathan Scottfad3aa12006-02-01 12:14:52 +1100886 if (atomic_dec_and_test(&bp->b_hold))
887 xfs_buf_free(bp);
888 return;
889 }
890
Dave Chinner74f75a02010-09-24 19:59:04 +1000891 ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
Dave Chinner430cbeb2010-12-02 16:30:55 +1100892
Lachlan McIlroy37906892008-08-13 15:42:10 +1000893 ASSERT(atomic_read(&bp->b_hold) > 0);
Dave Chinner74f75a02010-09-24 19:59:04 +1000894 if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000895 if (!(bp->b_flags & XBF_STALE) &&
Dave Chinner430cbeb2010-12-02 16:30:55 +1100896 atomic_read(&bp->b_lru_ref)) {
897 xfs_buf_lru_add(bp);
898 spin_unlock(&pag->pag_buf_lock);
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100899 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +1100900 xfs_buf_lru_del(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000901 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Dave Chinner74f75a02010-09-24 19:59:04 +1000902 rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
903 spin_unlock(&pag->pag_buf_lock);
904 xfs_perag_put(pag);
Nathan Scottce8e9222006-01-11 15:39:08 +1100905 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907 }
908}
909
910
911/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100912 * Lock a buffer object, if it is not already locked.
Dave Chinner90810b92010-11-30 15:16:16 +1100913 *
914 * If we come across a stale, pinned, locked buffer, we know that we are
915 * being asked to lock a buffer that has been reallocated. Because it is
916 * pinned, we know that the log has not been pushed to disk and hence it
917 * will still be locked. Rather than continuing to have trylock attempts
918 * fail until someone else pushes the log, push it ourselves before
919 * returning. This means that the xfsaild will not get stuck trying
920 * to push on stale inode buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 */
922int
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200923xfs_buf_trylock(
924 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 int locked;
927
Nathan Scottce8e9222006-01-11 15:39:08 +1100928 locked = down_trylock(&bp->b_sema) == 0;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000929 if (locked)
Nathan Scottce8e9222006-01-11 15:39:08 +1100930 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000931
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200932 trace_xfs_buf_trylock(bp, _RET_IP_);
933 return locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936/*
Dave Chinner0e6e8472011-03-26 09:16:45 +1100937 * Lock a buffer object.
Dave Chinnered3b4d62010-05-21 12:07:08 +1000938 *
939 * If we come across a stale, pinned, locked buffer, we know that we
940 * are being asked to lock a buffer that has been reallocated. Because
941 * it is pinned, we know that the log has not been pushed to disk and
942 * hence it will still be locked. Rather than sleeping until someone
943 * else pushes the log, push it ourselves before trying to get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100945void
946xfs_buf_lock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200947 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000949 trace_xfs_buf_lock(bp, _RET_IP_);
950
Dave Chinnered3b4d62010-05-21 12:07:08 +1000951 if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
Dave Chinnerebad8612010-09-22 10:47:20 +1000952 xfs_log_force(bp->b_target->bt_mount, 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100953 down(&bp->b_sema);
954 XB_SET_OWNER(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000955
956 trace_xfs_buf_lock_done(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959void
Nathan Scottce8e9222006-01-11 15:39:08 +1100960xfs_buf_unlock(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200961 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Nathan Scottce8e9222006-01-11 15:39:08 +1100963 XB_CLEAR_OWNER(bp);
964 up(&bp->b_sema);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000965
966 trace_xfs_buf_unlock(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Nathan Scottce8e9222006-01-11 15:39:08 +1100969STATIC void
970xfs_buf_wait_unpin(
971 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 DECLARE_WAITQUEUE (wait, current);
974
Nathan Scottce8e9222006-01-11 15:39:08 +1100975 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return;
977
Nathan Scottce8e9222006-01-11 15:39:08 +1100978 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 for (;;) {
980 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +1100981 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 break;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100983 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100985 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 set_current_state(TASK_RUNNING);
987}
988
989/*
990 * Buffer Utility Routines
991 */
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100994xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +0000995 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Dave Chinner1813dd62012-11-14 17:54:40 +1100997 struct xfs_buf *bp =
David Howellsc4028952006-11-22 14:57:56 +0000998 container_of(work, xfs_buf_t, b_iodone_work);
Dave Chinner1813dd62012-11-14 17:54:40 +1100999 bool read = !!(bp->b_flags & XBF_READ);
1000
1001 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Dave Chinnerd5929de2013-02-27 13:25:54 +11001002
1003 /* only validate buffers that were read without errors */
1004 if (read && bp->b_ops && !bp->b_error && (bp->b_flags & XBF_DONE))
Dave Chinner1813dd62012-11-14 17:54:40 +11001005 bp->b_ops->verify_read(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Christoph Hellwig80f6c292010-08-18 05:29:11 -04001007 if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001008 (*(bp->b_iodone))(bp);
1009 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 xfs_buf_relse(bp);
Dave Chinner1813dd62012-11-14 17:54:40 +11001011 else {
1012 ASSERT(read && bp->b_ops);
1013 complete(&bp->b_iowait);
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017void
Nathan Scottce8e9222006-01-11 15:39:08 +11001018xfs_buf_ioend(
Dave Chinner1813dd62012-11-14 17:54:40 +11001019 struct xfs_buf *bp,
1020 int schedule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Dave Chinner1813dd62012-11-14 17:54:40 +11001022 bool read = !!(bp->b_flags & XBF_READ);
1023
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001024 trace_xfs_buf_iodone(bp, _RET_IP_);
1025
Nathan Scottce8e9222006-01-11 15:39:08 +11001026 if (bp->b_error == 0)
1027 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Dave Chinner1813dd62012-11-14 17:54:40 +11001029 if (bp->b_iodone || (read && bp->b_ops) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +00001031 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +11001032 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 } else {
David Howellsc4028952006-11-22 14:57:56 +00001034 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036 } else {
Dave Chinner1813dd62012-11-14 17:54:40 +11001037 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
David Chinnerb4dd3302008-08-13 16:36:11 +10001038 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040}
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042void
Nathan Scottce8e9222006-01-11 15:39:08 +11001043xfs_buf_ioerror(
1044 xfs_buf_t *bp,
1045 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001048 bp->b_error = (unsigned short)error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001049 trace_xfs_buf_ioerror(bp, error, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Christoph Hellwig901796a2011-10-10 16:52:49 +00001052void
1053xfs_buf_ioerror_alert(
1054 struct xfs_buf *bp,
1055 const char *func)
1056{
1057 xfs_alert(bp->b_target->bt_mount,
Dave Chinneraa0e8832012-04-23 15:58:52 +10001058"metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
1059 (__uint64_t)XFS_BUF_ADDR(bp), func, bp->b_error, bp->b_length);
Christoph Hellwig901796a2011-10-10 16:52:49 +00001060}
1061
Christoph Hellwig4e234712010-01-13 22:17:56 +00001062/*
1063 * Called when we want to stop a buffer from getting written or read.
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001064 * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
Christoph Hellwig4e234712010-01-13 22:17:56 +00001065 * so that the proper iodone callbacks get called.
1066 */
1067STATIC int
1068xfs_bioerror(
1069 xfs_buf_t *bp)
1070{
1071#ifdef XFSERRORDEBUG
1072 ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
1073#endif
1074
1075 /*
1076 * No need to wait until the buffer is unpinned, we aren't flushing it.
1077 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001078 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001079
1080 /*
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001081 * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001082 */
1083 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001084 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001085 xfs_buf_stale(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001086
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001087 xfs_buf_ioend(bp, 0);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001088
1089 return EIO;
1090}
1091
1092/*
1093 * Same as xfs_bioerror, except that we are releasing the buffer
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00001094 * here ourselves, and avoiding the xfs_buf_ioend call.
Christoph Hellwig4e234712010-01-13 22:17:56 +00001095 * This is meant for userdata errors; metadata bufs come with
1096 * iodone functions attached, so that we can track down errors.
1097 */
1098STATIC int
1099xfs_bioerror_relse(
1100 struct xfs_buf *bp)
1101{
Chandra Seetharamaned432332011-07-22 23:39:39 +00001102 int64_t fl = bp->b_flags;
Christoph Hellwig4e234712010-01-13 22:17:56 +00001103 /*
1104 * No need to wait until the buffer is unpinned.
1105 * We aren't flushing it.
1106 *
1107 * chunkhold expects B_DONE to be set, whether
1108 * we actually finish the I/O or not. We don't want to
1109 * change that interface.
1110 */
1111 XFS_BUF_UNREAD(bp);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001112 XFS_BUF_DONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00001113 xfs_buf_stale(bp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02001114 bp->b_iodone = NULL;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001115 if (!(fl & XBF_ASYNC)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001116 /*
1117 * Mark b_error and B_ERROR _both_.
1118 * Lot's of chunkcache code assumes that.
1119 * There's no reason to mark error for
1120 * ASYNC buffers.
1121 */
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001122 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig5fde0322011-10-10 16:52:44 +00001123 complete(&bp->b_iowait);
Christoph Hellwig4e234712010-01-13 22:17:56 +00001124 } else {
1125 xfs_buf_relse(bp);
1126 }
1127
1128 return EIO;
1129}
1130
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001131STATIC int
Christoph Hellwig4e234712010-01-13 22:17:56 +00001132xfs_bdstrat_cb(
1133 struct xfs_buf *bp)
1134{
Dave Chinnerebad8612010-09-22 10:47:20 +10001135 if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
Christoph Hellwig4e234712010-01-13 22:17:56 +00001136 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1137 /*
1138 * Metadata write that didn't get logged but
1139 * written delayed anyway. These aren't associated
1140 * with a transaction, and can be ignored.
1141 */
1142 if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
1143 return xfs_bioerror_relse(bp);
1144 else
1145 return xfs_bioerror(bp);
1146 }
1147
1148 xfs_buf_iorequest(bp);
1149 return 0;
1150}
1151
Christoph Hellwiga2dcf5d2012-07-13 02:24:10 -04001152int
1153xfs_bwrite(
1154 struct xfs_buf *bp)
1155{
1156 int error;
1157
1158 ASSERT(xfs_buf_islocked(bp));
1159
1160 bp->b_flags |= XBF_WRITE;
1161 bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
1162
1163 xfs_bdstrat_cb(bp);
1164
1165 error = xfs_buf_iowait(bp);
1166 if (error) {
1167 xfs_force_shutdown(bp->b_target->bt_mount,
1168 SHUTDOWN_META_IO_ERROR);
1169 }
1170 return error;
1171}
1172
Christoph Hellwig4e234712010-01-13 22:17:56 +00001173/*
1174 * Wrapper around bdstrat so that we can stop data from going to disk in case
1175 * we are shutting down the filesystem. Typically user data goes thru this
1176 * path; one of the exceptions is the superblock.
1177 */
1178void
1179xfsbdstrat(
1180 struct xfs_mount *mp,
1181 struct xfs_buf *bp)
1182{
1183 if (XFS_FORCED_SHUTDOWN(mp)) {
1184 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1185 xfs_bioerror_relse(bp);
1186 return;
1187 }
1188
1189 xfs_buf_iorequest(bp);
1190}
1191
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00001192STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001193_xfs_buf_ioend(
1194 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 int schedule)
1196{
Dave Chinner0e6e8472011-03-26 09:16:45 +11001197 if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
Nathan Scottce8e9222006-01-11 15:39:08 +11001198 xfs_buf_ioend(bp, schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Al Viro782e3b32007-10-12 07:17:47 +01001201STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001202xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 int error)
1205{
Nathan Scottce8e9222006-01-11 15:39:08 +11001206 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Dave Chinner37eb17e2012-11-12 22:09:46 +11001208 /*
1209 * don't overwrite existing errors - otherwise we can lose errors on
1210 * buffers that require multiple bios to complete.
1211 */
1212 if (!bp->b_error)
1213 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Dave Chinner37eb17e2012-11-12 22:09:46 +11001215 if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
James Bottomley73c77e22010-01-25 11:42:24 -06001216 invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
1217
Nathan Scottce8e9222006-01-11 15:39:08 +11001218 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Dave Chinner3e85c862012-06-22 18:50:09 +10001222static void
1223xfs_buf_ioapply_map(
1224 struct xfs_buf *bp,
1225 int map,
1226 int *buf_offset,
1227 int *count,
1228 int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Dave Chinner3e85c862012-06-22 18:50:09 +10001230 int page_index;
1231 int total_nr_pages = bp->b_page_count;
1232 int nr_pages;
1233 struct bio *bio;
1234 sector_t sector = bp->b_maps[map].bm_bn;
1235 int size;
1236 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Nathan Scottce8e9222006-01-11 15:39:08 +11001238 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Dave Chinner3e85c862012-06-22 18:50:09 +10001240 /* skip the pages in the buffer before the start offset */
1241 page_index = 0;
1242 offset = *buf_offset;
1243 while (offset >= PAGE_SIZE) {
1244 page_index++;
1245 offset -= PAGE_SIZE;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001246 }
1247
Dave Chinner3e85c862012-06-22 18:50:09 +10001248 /*
1249 * Limit the IO size to the length of the current vector, and update the
1250 * remaining IO count for the next time around.
1251 */
1252 size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
1253 *count -= size;
1254 *buf_offset += size;
Christoph Hellwig34951f52011-07-26 15:06:44 +00001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001257 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1259 if (nr_pages > total_nr_pages)
1260 nr_pages = total_nr_pages;
1261
1262 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001263 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001265 bio->bi_end_io = xfs_buf_bio_end_io;
1266 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Dave Chinner0e6e8472011-03-26 09:16:45 +11001268
Dave Chinner3e85c862012-06-22 18:50:09 +10001269 for (; size && nr_pages; nr_pages--, page_index++) {
Dave Chinner0e6e8472011-03-26 09:16:45 +11001270 int rbytes, nbytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 if (nbytes > size)
1273 nbytes = size;
1274
Dave Chinner3e85c862012-06-22 18:50:09 +10001275 rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
1276 offset);
Nathan Scottce8e9222006-01-11 15:39:08 +11001277 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 break;
1279
1280 offset = 0;
Dave Chinneraa0e8832012-04-23 15:58:52 +10001281 sector += BTOBB(nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 size -= nbytes;
1283 total_nr_pages--;
1284 }
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (likely(bio->bi_size)) {
James Bottomley73c77e22010-01-25 11:42:24 -06001287 if (xfs_buf_is_vmapped(bp)) {
1288 flush_kernel_vmap_range(bp->b_addr,
1289 xfs_buf_vmap_len(bp));
1290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 submit_bio(rw, bio);
1292 if (size)
1293 goto next_chunk;
1294 } else {
Dave Chinner37eb17e2012-11-12 22:09:46 +11001295 /*
1296 * This is guaranteed not to be the last io reference count
1297 * because the caller (xfs_buf_iorequest) holds a count itself.
1298 */
1299 atomic_dec(&bp->b_io_remaining);
Nathan Scottce8e9222006-01-11 15:39:08 +11001300 xfs_buf_ioerror(bp, EIO);
Dave Chinnerec53d1d2010-07-20 17:52:59 +10001301 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001303
1304}
1305
1306STATIC void
1307_xfs_buf_ioapply(
1308 struct xfs_buf *bp)
1309{
1310 struct blk_plug plug;
1311 int rw;
1312 int offset;
1313 int size;
1314 int i;
1315
Dave Chinnerc163f9a2013-03-12 23:30:34 +11001316 /*
1317 * Make sure we capture only current IO errors rather than stale errors
1318 * left over from previous use of the buffer (e.g. failed readahead).
1319 */
1320 bp->b_error = 0;
1321
Dave Chinner3e85c862012-06-22 18:50:09 +10001322 if (bp->b_flags & XBF_WRITE) {
1323 if (bp->b_flags & XBF_SYNCIO)
1324 rw = WRITE_SYNC;
1325 else
1326 rw = WRITE;
1327 if (bp->b_flags & XBF_FUA)
1328 rw |= REQ_FUA;
1329 if (bp->b_flags & XBF_FLUSH)
1330 rw |= REQ_FLUSH;
Dave Chinner1813dd62012-11-14 17:54:40 +11001331
1332 /*
1333 * Run the write verifier callback function if it exists. If
1334 * this function fails it will mark the buffer with an error and
1335 * the IO should not be dispatched.
1336 */
1337 if (bp->b_ops) {
1338 bp->b_ops->verify_write(bp);
1339 if (bp->b_error) {
1340 xfs_force_shutdown(bp->b_target->bt_mount,
1341 SHUTDOWN_CORRUPT_INCORE);
1342 return;
1343 }
1344 }
Dave Chinner3e85c862012-06-22 18:50:09 +10001345 } else if (bp->b_flags & XBF_READ_AHEAD) {
1346 rw = READA;
1347 } else {
1348 rw = READ;
1349 }
1350
1351 /* we only use the buffer cache for meta-data */
1352 rw |= REQ_META;
1353
1354 /*
1355 * Walk all the vectors issuing IO on them. Set up the initial offset
1356 * into the buffer and the desired IO size before we start -
1357 * _xfs_buf_ioapply_vec() will modify them appropriately for each
1358 * subsequent call.
1359 */
1360 offset = bp->b_offset;
1361 size = BBTOB(bp->b_io_length);
1362 blk_start_plug(&plug);
1363 for (i = 0; i < bp->b_map_count; i++) {
1364 xfs_buf_ioapply_map(bp, i, &offset, &size, rw);
1365 if (bp->b_error)
1366 break;
1367 if (size <= 0)
1368 break; /* all done */
1369 }
1370 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
Dave Chinner0e95f192012-04-23 15:58:46 +10001373void
Nathan Scottce8e9222006-01-11 15:39:08 +11001374xfs_buf_iorequest(
1375 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001377 trace_xfs_buf_iorequest(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001379 ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Christoph Hellwig375ec692011-08-23 08:28:03 +00001381 if (bp->b_flags & XBF_WRITE)
Nathan Scottce8e9222006-01-11 15:39:08 +11001382 xfs_buf_wait_unpin(bp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001383 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /* Set the count to 1 initially, this will stop an I/O
1386 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001387 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001389 atomic_set(&bp->b_io_remaining, 1);
1390 _xfs_buf_ioapply(bp);
Christoph Hellwig08023d62012-07-02 06:00:04 -04001391 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Nathan Scottce8e9222006-01-11 15:39:08 +11001393 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
1396/*
Dave Chinner0e95f192012-04-23 15:58:46 +10001397 * Waits for I/O to complete on the buffer supplied. It returns immediately if
1398 * no I/O is pending or there is already a pending error on the buffer. It
1399 * returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 */
1401int
Nathan Scottce8e9222006-01-11 15:39:08 +11001402xfs_buf_iowait(
1403 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001405 trace_xfs_buf_iowait(bp, _RET_IP_);
1406
Dave Chinner0e95f192012-04-23 15:58:46 +10001407 if (!bp->b_error)
1408 wait_for_completion(&bp->b_iowait);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001409
1410 trace_xfs_buf_iowait_done(bp, _RET_IP_);
Nathan Scottce8e9222006-01-11 15:39:08 +11001411 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Nathan Scottce8e9222006-01-11 15:39:08 +11001414xfs_caddr_t
1415xfs_buf_offset(
1416 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 size_t offset)
1418{
1419 struct page *page;
1420
Dave Chinner611c9942012-04-23 15:59:07 +10001421 if (bp->b_addr)
Chandra Seetharaman62926042011-07-22 23:40:15 +00001422 return bp->b_addr + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Nathan Scottce8e9222006-01-11 15:39:08 +11001424 offset += bp->b_offset;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001425 page = bp->b_pages[offset >> PAGE_SHIFT];
1426 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
1429/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 * Move data into or out of a buffer.
1431 */
1432void
Nathan Scottce8e9222006-01-11 15:39:08 +11001433xfs_buf_iomove(
1434 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 size_t boff, /* starting buffer offset */
1436 size_t bsize, /* length to copy */
Dave Chinnerb9c48642010-01-20 10:47:39 +11001437 void *data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001438 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Dave Chinner795cac72012-04-23 15:58:53 +10001440 size_t bend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 bend = boff + bsize;
1443 while (boff < bend) {
Dave Chinner795cac72012-04-23 15:58:53 +10001444 struct page *page;
1445 int page_index, page_offset, csize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Dave Chinner795cac72012-04-23 15:58:53 +10001447 page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
1448 page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
1449 page = bp->b_pages[page_index];
1450 csize = min_t(size_t, PAGE_SIZE - page_offset,
1451 BBTOB(bp->b_io_length) - boff);
1452
1453 ASSERT((csize + page_offset) <= PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001456 case XBRW_ZERO:
Dave Chinner795cac72012-04-23 15:58:53 +10001457 memset(page_address(page) + page_offset, 0, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001459 case XBRW_READ:
Dave Chinner795cac72012-04-23 15:58:53 +10001460 memcpy(data, page_address(page) + page_offset, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001462 case XBRW_WRITE:
Dave Chinner795cac72012-04-23 15:58:53 +10001463 memcpy(page_address(page) + page_offset, data, csize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465
1466 boff += csize;
1467 data += csize;
1468 }
1469}
1470
1471/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001472 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 */
1474
1475/*
Dave Chinner430cbeb2010-12-02 16:30:55 +11001476 * Wait for any bufs with callbacks that have been submitted but have not yet
1477 * returned. These buffers will have an elevated hold count, so wait on those
1478 * while freeing all the buffers only held by the LRU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 */
Dave Chinnere80dfa12013-08-28 10:18:05 +10001480static enum lru_status
1481xfs_buftarg_wait_rele(
1482 struct list_head *item,
1483 spinlock_t *lru_lock,
1484 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Dave Chinnere80dfa12013-08-28 10:18:05 +10001486{
1487 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1488
1489 if (atomic_read(&bp->b_hold) > 1) {
1490 /* need to wait */
1491 trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
1492 spin_unlock(lru_lock);
1493 delay(100);
1494 } else {
Dave Chinner430cbeb2010-12-02 16:30:55 +11001495 /*
Paul Bolle90802ed2011-12-05 13:00:34 +01001496 * clear the LRU reference count so the buffer doesn't get
Dave Chinner430cbeb2010-12-02 16:30:55 +11001497 * ignored in xfs_buf_rele().
1498 */
1499 atomic_set(&bp->b_lru_ref, 0);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001500 spin_unlock(lru_lock);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001501 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
Dave Chinnere80dfa12013-08-28 10:18:05 +10001503
1504 spin_lock(lru_lock);
1505 return LRU_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
Dave Chinnere80dfa12013-08-28 10:18:05 +10001508void
1509xfs_wait_buftarg(
1510 struct xfs_buftarg *btp)
1511{
1512 while (list_lru_count(&btp->bt_lru))
1513 list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
1514 NULL, LONG_MAX);
1515}
1516
1517static enum lru_status
1518xfs_buftarg_isolate(
1519 struct list_head *item,
1520 spinlock_t *lru_lock,
1521 void *arg)
1522{
1523 struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
1524 struct list_head *dispose = arg;
1525
1526 /*
1527 * Decrement the b_lru_ref count unless the value is already
1528 * zero. If the value is already zero, we need to reclaim the
1529 * buffer, otherwise it gets another trip through the LRU.
1530 */
1531 if (!atomic_add_unless(&bp->b_lru_ref, -1, 0))
1532 return LRU_ROTATE;
1533
1534 bp->b_lru_flags |= _XBF_LRU_DISPOSE;
1535 list_move(item, dispose);
1536 return LRU_REMOVED;
1537}
1538
1539static long
1540xfs_buftarg_shrink_scan(
Dave Chinnerff57ab22010-11-30 17:27:57 +11001541 struct shrinker *shrink,
Ying Han1495f232011-05-24 17:12:27 -07001542 struct shrink_control *sc)
David Chinnera6867a62006-01-11 15:37:58 +11001543{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001544 struct xfs_buftarg *btp = container_of(shrink,
1545 struct xfs_buftarg, bt_shrinker);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001546 LIST_HEAD(dispose);
Dave Chinnere80dfa12013-08-28 10:18:05 +10001547 long freed;
1548 unsigned long nr_to_scan = sc->nr_to_scan;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001549
Dave Chinnere80dfa12013-08-28 10:18:05 +10001550 freed = list_lru_walk_node(&btp->bt_lru, sc->nid, xfs_buftarg_isolate,
1551 &dispose, &nr_to_scan);
Dave Chinner430cbeb2010-12-02 16:30:55 +11001552
1553 while (!list_empty(&dispose)) {
Dave Chinnere80dfa12013-08-28 10:18:05 +10001554 struct xfs_buf *bp;
Dave Chinner430cbeb2010-12-02 16:30:55 +11001555 bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
1556 list_del_init(&bp->b_lru);
1557 xfs_buf_rele(bp);
1558 }
1559
Dave Chinnere80dfa12013-08-28 10:18:05 +10001560 return freed;
1561}
1562
1563static long
1564xfs_buftarg_shrink_count(
1565 struct shrinker *shrink,
1566 struct shrink_control *sc)
1567{
1568 struct xfs_buftarg *btp = container_of(shrink,
1569 struct xfs_buftarg, bt_shrinker);
1570 return list_lru_count_node(&btp->bt_lru, sc->nid);
David Chinnera6867a62006-01-11 15:37:58 +11001571}
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573void
1574xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001575 struct xfs_mount *mp,
1576 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Dave Chinnerff57ab22010-11-30 17:27:57 +11001578 unregister_shrinker(&btp->bt_shrinker);
1579
Christoph Hellwigb7963132009-03-03 14:48:37 -05001580 if (mp->m_flags & XFS_MOUNT_BARRIER)
1581 xfs_blkdev_issue_flush(btp);
David Chinnera6867a62006-01-11 15:37:58 +11001582
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001583 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586STATIC int
1587xfs_setsize_buftarg_flags(
1588 xfs_buftarg_t *btp,
1589 unsigned int blocksize,
1590 unsigned int sectorsize,
1591 int verbose)
1592{
Nathan Scottce8e9222006-01-11 15:39:08 +11001593 btp->bt_bsize = blocksize;
1594 btp->bt_sshift = ffs(sectorsize) - 1;
1595 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Nathan Scottce8e9222006-01-11 15:39:08 +11001597 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001598 char name[BDEVNAME_SIZE];
1599
1600 bdevname(btp->bt_bdev, name);
1601
Dave Chinner4f107002011-03-07 10:00:35 +11001602 xfs_warn(btp->bt_mount,
1603 "Cannot set_blocksize to %u on device %s\n",
Christoph Hellwig02b102d2011-10-10 16:52:51 +00001604 sectorsize, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return EINVAL;
1606 }
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return 0;
1609}
1610
1611/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001612 * When allocating the initial buffer target we have not yet
1613 * read in the superblock, so don't know what sized sectors
Zhi Yong Wu8b4ad792013-08-12 03:14:56 +00001614 * are being used at this early stage. Play safe.
Nathan Scottce8e9222006-01-11 15:39:08 +11001615 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616STATIC int
1617xfs_setsize_buftarg_early(
1618 xfs_buftarg_t *btp,
1619 struct block_device *bdev)
1620{
1621 return xfs_setsize_buftarg_flags(btp,
Dave Chinner0e6e8472011-03-26 09:16:45 +11001622 PAGE_SIZE, bdev_logical_block_size(bdev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
1625int
1626xfs_setsize_buftarg(
1627 xfs_buftarg_t *btp,
1628 unsigned int blocksize,
1629 unsigned int sectorsize)
1630{
1631 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1632}
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634xfs_buftarg_t *
1635xfs_alloc_buftarg(
Dave Chinnerebad8612010-09-22 10:47:20 +10001636 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 struct block_device *bdev,
Jan Engelhardte2a07812010-03-23 09:52:55 +11001638 int external,
1639 const char *fsname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 xfs_buftarg_t *btp;
1642
Dave Chinnerb17cb362013-05-20 09:51:12 +10001643 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Dave Chinnerebad8612010-09-22 10:47:20 +10001645 btp->bt_mount = mp;
Nathan Scottce8e9222006-01-11 15:39:08 +11001646 btp->bt_dev = bdev->bd_dev;
1647 btp->bt_bdev = bdev;
Dave Chinner0e6e8472011-03-26 09:16:45 +11001648 btp->bt_bdi = blk_get_backing_dev_info(bdev);
1649 if (!btp->bt_bdi)
1650 goto error;
1651
Dave Chinnere80dfa12013-08-28 10:18:05 +10001652 list_lru_init(&btp->bt_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (xfs_setsize_buftarg_early(btp, bdev))
1654 goto error;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001655 btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
1656 btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001657 btp->bt_shrinker.seeks = DEFAULT_SEEKS;
Dave Chinnere80dfa12013-08-28 10:18:05 +10001658 btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
Dave Chinnerff57ab22010-11-30 17:27:57 +11001659 register_shrinker(&btp->bt_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return btp;
1661
1662error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001663 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return NULL;
1665}
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001668 * Add a buffer to the delayed write list.
1669 *
1670 * This queues a buffer for writeout if it hasn't already been. Note that
1671 * neither this routine nor the buffer list submission functions perform
1672 * any internal synchronization. It is expected that the lists are thread-local
1673 * to the callers.
1674 *
1675 * Returns true if we queued up the buffer, or false if it already had
1676 * been on the buffer list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001678bool
Nathan Scottce8e9222006-01-11 15:39:08 +11001679xfs_buf_delwri_queue(
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001680 struct xfs_buf *bp,
1681 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001683 ASSERT(xfs_buf_islocked(bp));
1684 ASSERT(!(bp->b_flags & XBF_READ));
1685
1686 /*
1687 * If the buffer is already marked delwri it already is queued up
1688 * by someone else for imediate writeout. Just ignore it in that
1689 * case.
1690 */
1691 if (bp->b_flags & _XBF_DELWRI_Q) {
1692 trace_xfs_buf_delwri_queued(bp, _RET_IP_);
1693 return false;
1694 }
David Chinnera6867a62006-01-11 15:37:58 +11001695
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001696 trace_xfs_buf_delwri_queue(bp, _RET_IP_);
1697
Dave Chinnerd808f612010-02-02 10:13:42 +11001698 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001699 * If a buffer gets written out synchronously or marked stale while it
1700 * is on a delwri list we lazily remove it. To do this, the other party
1701 * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
1702 * It remains referenced and on the list. In a rare corner case it
1703 * might get readded to a delwri list after the synchronous writeout, in
1704 * which case we need just need to re-add the flag here.
Dave Chinnerd808f612010-02-02 10:13:42 +11001705 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001706 bp->b_flags |= _XBF_DELWRI_Q;
1707 if (list_empty(&bp->b_list)) {
1708 atomic_inc(&bp->b_hold);
1709 list_add_tail(&bp->b_list, list);
David Chinner585e6d82007-02-10 18:32:29 +11001710 }
David Chinner585e6d82007-02-10 18:32:29 +11001711
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001712 return true;
David Chinner585e6d82007-02-10 18:32:29 +11001713}
1714
Dave Chinner089716a2010-01-26 15:13:25 +11001715/*
1716 * Compare function is more complex than it needs to be because
1717 * the return value is only 32 bits and we are doing comparisons
1718 * on 64 bit values
1719 */
1720static int
1721xfs_buf_cmp(
1722 void *priv,
1723 struct list_head *a,
1724 struct list_head *b)
1725{
1726 struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
1727 struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
1728 xfs_daddr_t diff;
1729
Mark Tinguelyf4b42422012-12-04 17:18:02 -06001730 diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
Dave Chinner089716a2010-01-26 15:13:25 +11001731 if (diff < 0)
1732 return -1;
1733 if (diff > 0)
1734 return 1;
1735 return 0;
1736}
1737
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001738static int
1739__xfs_buf_delwri_submit(
1740 struct list_head *buffer_list,
1741 struct list_head *io_list,
1742 bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001744 struct blk_plug plug;
1745 struct xfs_buf *bp, *n;
1746 int pinned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001748 list_for_each_entry_safe(bp, n, buffer_list, b_list) {
1749 if (!wait) {
1750 if (xfs_buf_ispinned(bp)) {
1751 pinned++;
1752 continue;
1753 }
1754 if (!xfs_buf_trylock(bp))
1755 continue;
1756 } else {
1757 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001760 /*
1761 * Someone else might have written the buffer synchronously or
1762 * marked it stale in the meantime. In that case only the
1763 * _XBF_DELWRI_Q flag got cleared, and we have to drop the
1764 * reference and remove it from the list here.
1765 */
1766 if (!(bp->b_flags & _XBF_DELWRI_Q)) {
1767 list_del_init(&bp->b_list);
1768 xfs_buf_relse(bp);
1769 continue;
1770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001772 list_move_tail(&bp->b_list, io_list);
1773 trace_xfs_buf_delwri_split(bp, _RET_IP_);
1774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001776 list_sort(NULL, io_list, xfs_buf_cmp);
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001777
1778 blk_start_plug(&plug);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001779 list_for_each_entry_safe(bp, n, io_list, b_list) {
1780 bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
1781 bp->b_flags |= XBF_WRITE;
1782
1783 if (!wait) {
1784 bp->b_flags |= XBF_ASYNC;
1785 list_del_init(&bp->b_list);
Dave Chinner089716a2010-01-26 15:13:25 +11001786 }
Christoph Hellwig939d7232010-07-20 17:51:16 +10001787 xfs_bdstrat_cb(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
Christoph Hellwiga1b7ea52011-03-30 11:05:09 +00001789 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001791 return pinned;
1792}
Nathan Scottf07c2252006-09-28 10:52:15 +10001793
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001794/*
1795 * Write out a buffer list asynchronously.
1796 *
1797 * This will take the @buffer_list, write all non-locked and non-pinned buffers
1798 * out and not wait for I/O completion on any of the buffers. This interface
1799 * is only safely useable for callers that can track I/O completion by higher
1800 * level means, e.g. AIL pushing as the @buffer_list is consumed in this
1801 * function.
1802 */
1803int
1804xfs_buf_delwri_submit_nowait(
1805 struct list_head *buffer_list)
1806{
1807 LIST_HEAD (io_list);
1808 return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
1809}
1810
1811/*
1812 * Write out a buffer list synchronously.
1813 *
1814 * This will take the @buffer_list, write all buffers out and wait for I/O
1815 * completion on all of the buffers. @buffer_list is consumed by the function,
1816 * so callers must have some other way of tracking buffers if they require such
1817 * functionality.
1818 */
1819int
1820xfs_buf_delwri_submit(
1821 struct list_head *buffer_list)
1822{
1823 LIST_HEAD (io_list);
1824 int error = 0, error2;
1825 struct xfs_buf *bp;
1826
1827 __xfs_buf_delwri_submit(buffer_list, &io_list, true);
1828
1829 /* Wait for IO to complete. */
1830 while (!list_empty(&io_list)) {
1831 bp = list_first_entry(&io_list, struct xfs_buf, b_list);
1832
1833 list_del_init(&bp->b_list);
1834 error2 = xfs_buf_iowait(bp);
1835 xfs_buf_relse(bp);
1836 if (!error)
1837 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839
Christoph Hellwig43ff2122012-04-23 15:58:39 +10001840 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001843int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001844xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Nathan Scott87582802006-03-14 13:18:19 +11001846 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1847 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001848 if (!xfs_buf_zone)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001849 goto out;
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001850
Dave Chinner51749e42010-09-08 09:00:22 +00001851 xfslogd_workqueue = alloc_workqueue("xfslogd",
Tejun Heo6370a6a2010-10-11 15:12:27 +02001852 WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001853 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001854 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001858 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001859 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001860 out:
Nathan Scott87582802006-03-14 13:18:19 +11001861 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864void
Nathan Scottce8e9222006-01-11 15:39:08 +11001865xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001867 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001868 kmem_zone_destroy(xfs_buf_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}