blob: aa1016bb913405e4e4f77c5bee28e7f38db2b44c [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>
21#include <linux/slab.h>
22#include <linux/pagemap.h>
23#include <linux/init.h>
24#include <linux/vmalloc.h>
25#include <linux/bio.h>
26#include <linux/sysctl.h>
27#include <linux/proc_fs.h>
28#include <linux/workqueue.h>
29#include <linux/percpu.h>
30#include <linux/blkdev.h>
31#include <linux/hash.h>
Christoph Hellwig4df08c52005-09-05 08:34:18 +100032#include <linux/kthread.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080033#include <linux/migrate.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070034#include <linux/backing-dev.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Christoph Hellwigb7963132009-03-03 14:48:37 -050037#include "xfs_sb.h"
38#include "xfs_inum.h"
39#include "xfs_ag.h"
40#include "xfs_dmapi.h"
41#include "xfs_mount.h"
42
David Chinner7989cb82007-02-10 18:34:56 +110043static kmem_zone_t *xfs_buf_zone;
David Chinnera6867a62006-01-11 15:37:58 +110044STATIC int xfsbufd(void *);
Al Viro27496a82005-10-21 03:20:48 -040045STATIC int xfsbufd_wakeup(int, gfp_t);
Nathan Scottce8e9222006-01-11 15:39:08 +110046STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
Rusty Russell8e1f9362007-07-17 04:03:17 -070047static struct shrinker xfs_buf_shake = {
48 .shrink = xfsbufd_wakeup,
49 .seeks = DEFAULT_SEEKS,
50};
Christoph Hellwig23ea4032005-06-21 15:14:01 +100051
David Chinner7989cb82007-02-10 18:34:56 +110052static struct workqueue_struct *xfslogd_workqueue;
Christoph Hellwig0829c362005-09-02 16:58:49 +100053struct workqueue_struct *xfsdatad_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Nathan Scottce8e9222006-01-11 15:39:08 +110055#ifdef XFS_BUF_TRACE
Linus Torvalds1da177e2005-04-16 15:20:36 -070056void
Nathan Scottce8e9222006-01-11 15:39:08 +110057xfs_buf_trace(
58 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 char *id,
60 void *data,
61 void *ra)
62{
Nathan Scottce8e9222006-01-11 15:39:08 +110063 ktrace_enter(xfs_buf_trace_buf,
64 bp, id,
65 (void *)(unsigned long)bp->b_flags,
66 (void *)(unsigned long)bp->b_hold.counter,
Lachlan McIlroyd63f1542008-08-13 16:28:40 +100067 (void *)(unsigned long)bp->b_sema.count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 (void *)current,
69 data, ra,
Nathan Scottce8e9222006-01-11 15:39:08 +110070 (void *)(unsigned long)((bp->b_file_offset>>32) & 0xffffffff),
71 (void *)(unsigned long)(bp->b_file_offset & 0xffffffff),
72 (void *)(unsigned long)bp->b_buffer_length,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 NULL, NULL, NULL, NULL, NULL);
74}
Nathan Scottce8e9222006-01-11 15:39:08 +110075ktrace_t *xfs_buf_trace_buf;
76#define XFS_BUF_TRACE_SIZE 4096
77#define XB_TRACE(bp, id, data) \
78 xfs_buf_trace(bp, id, (void *)data, (void *)__builtin_return_address(0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#else
Nathan Scottce8e9222006-01-11 15:39:08 +110080#define XB_TRACE(bp, id, data) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#endif
82
Nathan Scottce8e9222006-01-11 15:39:08 +110083#ifdef XFS_BUF_LOCK_TRACKING
84# define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
85# define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
86# define XB_GET_OWNER(bp) ((bp)->b_last_holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#else
Nathan Scottce8e9222006-01-11 15:39:08 +110088# define XB_SET_OWNER(bp) do { } while (0)
89# define XB_CLEAR_OWNER(bp) do { } while (0)
90# define XB_GET_OWNER(bp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#endif
92
Nathan Scottce8e9222006-01-11 15:39:08 +110093#define xb_to_gfp(flags) \
94 ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
95 ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Nathan Scottce8e9222006-01-11 15:39:08 +110097#define xb_to_km(flags) \
98 (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Nathan Scottce8e9222006-01-11 15:39:08 +1100100#define xfs_buf_allocate(flags) \
101 kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
102#define xfs_buf_deallocate(bp) \
103 kmem_zone_free(xfs_buf_zone, (bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100106 * Page Region interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100108 * For pages in filesystems where the blocksize is smaller than the
109 * pagesize, we use the page->private field (long) to hold a bitmap
110 * of uptodate regions within the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100112 * Each such region is "bytes per page / bits per long" bytes long.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 *
Nathan Scottce8e9222006-01-11 15:39:08 +1100114 * NBPPR == number-of-bytes-per-page-region
115 * BTOPR == bytes-to-page-region (rounded up)
116 * BTOPRT == bytes-to-page-region-truncated (rounded down)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118#if (BITS_PER_LONG == 32)
119#define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
120#elif (BITS_PER_LONG == 64)
121#define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
122#else
123#error BITS_PER_LONG must be 32 or 64
124#endif
125#define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
126#define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
127#define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
128
129STATIC unsigned long
130page_region_mask(
131 size_t offset,
132 size_t length)
133{
134 unsigned long mask;
135 int first, final;
136
137 first = BTOPR(offset);
138 final = BTOPRT(offset + length - 1);
139 first = min(first, final);
140
141 mask = ~0UL;
142 mask <<= BITS_PER_LONG - (final - first);
143 mask >>= BITS_PER_LONG - (final);
144
145 ASSERT(offset + length <= PAGE_CACHE_SIZE);
146 ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
147
148 return mask;
149}
150
David Chinner7989cb82007-02-10 18:34:56 +1100151STATIC_INLINE void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152set_page_region(
153 struct page *page,
154 size_t offset,
155 size_t length)
156{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700157 set_page_private(page,
158 page_private(page) | page_region_mask(offset, length));
159 if (page_private(page) == ~0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 SetPageUptodate(page);
161}
162
David Chinner7989cb82007-02-10 18:34:56 +1100163STATIC_INLINE int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164test_page_region(
165 struct page *page,
166 size_t offset,
167 size_t length)
168{
169 unsigned long mask = page_region_mask(offset, length);
170
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700171 return (mask && (page_private(page) & mask) == mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174/*
Felix Blyakher3a011a12009-02-18 15:56:51 -0600175 * Mapping of multi-page buffers into contiguous virtual space
176 */
177
178typedef struct a_list {
179 void *vm_addr;
180 struct a_list *next;
181} a_list_t;
182
183static a_list_t *as_free_head;
184static int as_list_len;
185static DEFINE_SPINLOCK(as_lock);
186
187/*
188 * Try to batch vunmaps because they are costly.
189 */
190STATIC void
191free_address(
192 void *addr)
193{
194 a_list_t *aentry;
195
196#ifdef CONFIG_XEN
197 /*
198 * Xen needs to be able to make sure it can get an exclusive
199 * RO mapping of pages it wants to turn into a pagetable. If
200 * a newly allocated page is also still being vmap()ed by xfs,
201 * it will cause pagetable construction to fail. This is a
202 * quick workaround to always eagerly unmap pages so that Xen
203 * is happy.
204 */
205 vunmap(addr);
206 return;
207#endif
208
209 aentry = kmalloc(sizeof(a_list_t), GFP_NOWAIT);
210 if (likely(aentry)) {
211 spin_lock(&as_lock);
212 aentry->next = as_free_head;
213 aentry->vm_addr = addr;
214 as_free_head = aentry;
215 as_list_len++;
216 spin_unlock(&as_lock);
217 } else {
218 vunmap(addr);
219 }
220}
221
222STATIC void
223purge_addresses(void)
224{
225 a_list_t *aentry, *old;
226
227 if (as_free_head == NULL)
228 return;
229
230 spin_lock(&as_lock);
231 aentry = as_free_head;
232 as_free_head = NULL;
233 as_list_len = 0;
234 spin_unlock(&as_lock);
235
236 while ((old = aentry) != NULL) {
237 vunmap(aentry->vm_addr);
238 aentry = aentry->next;
239 kfree(old);
240 }
241}
242
243/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100244 * Internal xfs_buf_t object manipulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
246
247STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100248_xfs_buf_initialize(
249 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100251 xfs_off_t range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 size_t range_length,
Nathan Scottce8e9222006-01-11 15:39:08 +1100253 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100256 * We don't want certain flags to appear in b_flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100258 flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Nathan Scottce8e9222006-01-11 15:39:08 +1100260 memset(bp, 0, sizeof(xfs_buf_t));
261 atomic_set(&bp->b_hold, 1);
David Chinnerb4dd3302008-08-13 16:36:11 +1000262 init_completion(&bp->b_iowait);
Nathan Scottce8e9222006-01-11 15:39:08 +1100263 INIT_LIST_HEAD(&bp->b_list);
264 INIT_LIST_HEAD(&bp->b_hash_list);
265 init_MUTEX_LOCKED(&bp->b_sema); /* held, no waiters */
266 XB_SET_OWNER(bp);
267 bp->b_target = target;
268 bp->b_file_offset = range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /*
270 * Set buffer_length and count_desired to the same value initially.
271 * I/O routines should use count_desired, which will be the same in
272 * most cases but may be reset (e.g. XFS recovery).
273 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100274 bp->b_buffer_length = bp->b_count_desired = range_length;
275 bp->b_flags = flags;
276 bp->b_bn = XFS_BUF_DADDR_NULL;
277 atomic_set(&bp->b_pin_count, 0);
278 init_waitqueue_head(&bp->b_waiters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Nathan Scottce8e9222006-01-11 15:39:08 +1100280 XFS_STATS_INC(xb_create);
281 XB_TRACE(bp, "initialize", target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100285 * Allocate a page array capable of holding a specified number
286 * of pages, and point the page buf at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
288STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100289_xfs_buf_get_pages(
290 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int page_count,
Nathan Scottce8e9222006-01-11 15:39:08 +1100292 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 /* Make sure that we have a page list */
Nathan Scottce8e9222006-01-11 15:39:08 +1100295 if (bp->b_pages == NULL) {
296 bp->b_offset = xfs_buf_poff(bp->b_file_offset);
297 bp->b_page_count = page_count;
298 if (page_count <= XB_PAGES) {
299 bp->b_pages = bp->b_page_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100301 bp->b_pages = kmem_alloc(sizeof(struct page *) *
302 page_count, xb_to_km(flags));
303 if (bp->b_pages == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return -ENOMEM;
305 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100306 memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 return 0;
309}
310
311/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100312 * Frees b_pages if it was allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
314STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +1100315_xfs_buf_free_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 xfs_buf_t *bp)
317{
Nathan Scottce8e9222006-01-11 15:39:08 +1100318 if (bp->b_pages != bp->b_page_array) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000319 kmem_free(bp->b_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321}
322
323/*
324 * Releases the specified buffer.
325 *
326 * The modification state of any associated pages is left unchanged.
Nathan Scottce8e9222006-01-11 15:39:08 +1100327 * The buffer most not be on any hash - use xfs_buf_rele instead for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * hashed and refcounted buffers
329 */
330void
Nathan Scottce8e9222006-01-11 15:39:08 +1100331xfs_buf_free(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xfs_buf_t *bp)
333{
Nathan Scottce8e9222006-01-11 15:39:08 +1100334 XB_TRACE(bp, "free", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Nathan Scottce8e9222006-01-11 15:39:08 +1100336 ASSERT(list_empty(&bp->b_hash_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000338 if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 uint i;
340
Nathan Scottce8e9222006-01-11 15:39:08 +1100341 if ((bp->b_flags & XBF_MAPPED) && (bp->b_page_count > 1))
Felix Blyakher3a011a12009-02-18 15:56:51 -0600342 free_address(bp->b_addr - bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Nathan Scott948ecdb2006-09-28 11:03:13 +1000344 for (i = 0; i < bp->b_page_count; i++) {
345 struct page *page = bp->b_pages[i];
346
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000347 if (bp->b_flags & _XBF_PAGE_CACHE)
348 ASSERT(!PagePrivate(page));
Nathan Scott948ecdb2006-09-28 11:03:13 +1000349 page_cache_release(page);
350 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100351 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
Nathan Scottce8e9222006-01-11 15:39:08 +1100354 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357/*
358 * Finds all pages for buffer in question and builds it's page list.
359 */
360STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100361_xfs_buf_lookup_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 xfs_buf_t *bp,
363 uint flags)
364{
Nathan Scottce8e9222006-01-11 15:39:08 +1100365 struct address_space *mapping = bp->b_target->bt_mapping;
366 size_t blocksize = bp->b_target->bt_bsize;
367 size_t size = bp->b_count_desired;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 size_t nbytes, offset;
Nathan Scottce8e9222006-01-11 15:39:08 +1100369 gfp_t gfp_mask = xb_to_gfp(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned short page_count, i;
371 pgoff_t first;
Nathan Scott204ab252006-01-11 20:50:22 +1100372 xfs_off_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int error;
374
Nathan Scottce8e9222006-01-11 15:39:08 +1100375 end = bp->b_file_offset + bp->b_buffer_length;
376 page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Nathan Scottce8e9222006-01-11 15:39:08 +1100378 error = _xfs_buf_get_pages(bp, page_count, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (unlikely(error))
380 return error;
Nathan Scottce8e9222006-01-11 15:39:08 +1100381 bp->b_flags |= _XBF_PAGE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Nathan Scottce8e9222006-01-11 15:39:08 +1100383 offset = bp->b_offset;
384 first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Nathan Scottce8e9222006-01-11 15:39:08 +1100386 for (i = 0; i < bp->b_page_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct page *page;
388 uint retries = 0;
389
390 retry:
391 page = find_or_create_page(mapping, first + i, gfp_mask);
392 if (unlikely(page == NULL)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100393 if (flags & XBF_READ_AHEAD) {
394 bp->b_page_count = i;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000395 for (i = 0; i < bp->b_page_count; i++)
396 unlock_page(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return -ENOMEM;
398 }
399
400 /*
401 * This could deadlock.
402 *
403 * But until all the XFS lowlevel code is revamped to
404 * handle buffer allocation failures we can't do much.
405 */
406 if (!(++retries % 100))
407 printk(KERN_ERR
408 "XFS: possible memory allocation "
409 "deadlock in %s (mode:0x%x)\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000410 __func__, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Nathan Scottce8e9222006-01-11 15:39:08 +1100412 XFS_STATS_INC(xb_page_retries);
Christoph Hellwig23ea4032005-06-21 15:14:01 +1000413 xfsbufd_wakeup(0, gfp_mask);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700414 congestion_wait(WRITE, HZ/50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 goto retry;
416 }
417
Nathan Scottce8e9222006-01-11 15:39:08 +1100418 XFS_STATS_INC(xb_page_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
421 size -= nbytes;
422
Nathan Scott948ecdb2006-09-28 11:03:13 +1000423 ASSERT(!PagePrivate(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (!PageUptodate(page)) {
425 page_count--;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000426 if (blocksize >= PAGE_CACHE_SIZE) {
427 if (flags & XBF_READ)
428 bp->b_flags |= _XBF_PAGE_LOCKED;
429 } else if (!PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (test_page_region(page, offset, nbytes))
431 page_count++;
432 }
433 }
434
Nathan Scottce8e9222006-01-11 15:39:08 +1100435 bp->b_pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 offset = 0;
437 }
438
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000439 if (!(bp->b_flags & _XBF_PAGE_LOCKED)) {
440 for (i = 0; i < bp->b_page_count; i++)
441 unlock_page(bp->b_pages[i]);
442 }
443
Nathan Scottce8e9222006-01-11 15:39:08 +1100444 if (page_count == bp->b_page_count)
445 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Nathan Scottce8e9222006-01-11 15:39:08 +1100447 XB_TRACE(bp, "lookup_pages", (long)page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return error;
449}
450
451/*
452 * Map buffer into kernel address-space if nessecary.
453 */
454STATIC int
Nathan Scottce8e9222006-01-11 15:39:08 +1100455_xfs_buf_map_pages(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 xfs_buf_t *bp,
457 uint flags)
458{
459 /* A single page buffer is always mappable */
Nathan Scottce8e9222006-01-11 15:39:08 +1100460 if (bp->b_page_count == 1) {
461 bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
462 bp->b_flags |= XBF_MAPPED;
463 } else if (flags & XBF_MAPPED) {
Felix Blyakher3a011a12009-02-18 15:56:51 -0600464 if (as_list_len > 64)
465 purge_addresses();
Felix Blyakhercf7dab82009-02-18 15:41:28 -0600466 bp->b_addr = vmap(bp->b_pages, bp->b_page_count,
467 VM_MAP, PAGE_KERNEL);
Nathan Scottce8e9222006-01-11 15:39:08 +1100468 if (unlikely(bp->b_addr == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return -ENOMEM;
Nathan Scottce8e9222006-01-11 15:39:08 +1100470 bp->b_addr += bp->b_offset;
471 bp->b_flags |= XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
474 return 0;
475}
476
477/*
478 * Finding and Reading Buffers
479 */
480
481/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100482 * Look up, and creates if absent, a lockable buffer for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 * a given range of an inode. The buffer is returned
484 * locked. If other overlapping buffers exist, they are
485 * released before the new buffer is created and locked,
486 * which may imply that this call will block until those buffers
487 * are unlocked. No I/O is implied by this call.
488 */
489xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100490_xfs_buf_find(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 xfs_buftarg_t *btp, /* block device target */
Nathan Scott204ab252006-01-11 20:50:22 +1100492 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100494 xfs_buf_flags_t flags,
495 xfs_buf_t *new_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Nathan Scott204ab252006-01-11 20:50:22 +1100497 xfs_off_t range_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 size_t range_length;
499 xfs_bufhash_t *hash;
Nathan Scottce8e9222006-01-11 15:39:08 +1100500 xfs_buf_t *bp, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 range_base = (ioff << BBSHIFT);
503 range_length = (isize << BBSHIFT);
504
505 /* Check for IOs smaller than the sector size / not sector aligned */
Nathan Scottce8e9222006-01-11 15:39:08 +1100506 ASSERT(!(range_length < (1 << btp->bt_sshift)));
Nathan Scott204ab252006-01-11 20:50:22 +1100507 ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
510
511 spin_lock(&hash->bh_lock);
512
Nathan Scottce8e9222006-01-11 15:39:08 +1100513 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
514 ASSERT(btp == bp->b_target);
515 if (bp->b_file_offset == range_base &&
516 bp->b_buffer_length == range_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
Nathan Scottce8e9222006-01-11 15:39:08 +1100518 * If we look at something, bring it to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 * front of the list for next time.
520 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100521 atomic_inc(&bp->b_hold);
522 list_move(&bp->b_hash_list, &hash->bh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto found;
524 }
525 }
526
527 /* No match found */
Nathan Scottce8e9222006-01-11 15:39:08 +1100528 if (new_bp) {
529 _xfs_buf_initialize(new_bp, btp, range_base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 range_length, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100531 new_bp->b_hash = hash;
532 list_add(&new_bp->b_hash_list, &hash->bh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100534 XFS_STATS_INC(xb_miss_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536
537 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100538 return new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540found:
541 spin_unlock(&hash->bh_lock);
542
543 /* Attempt to get the semaphore without sleeping,
544 * if this does not work then we need to drop the
545 * spinlock and do a hard attempt on the semaphore.
546 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100547 if (down_trylock(&bp->b_sema)) {
548 if (!(flags & XBF_TRYLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* wait for buffer ownership */
Nathan Scottce8e9222006-01-11 15:39:08 +1100550 XB_TRACE(bp, "get_lock", 0);
551 xfs_buf_lock(bp);
552 XFS_STATS_INC(xb_get_locked_waited);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 } else {
554 /* We asked for a trylock and failed, no need
555 * to look at file offset and length here, we
Nathan Scottce8e9222006-01-11 15:39:08 +1100556 * know that this buffer at least overlaps our
557 * buffer and is locked, therefore our buffer
558 * either does not exist, or is this buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100560 xfs_buf_rele(bp);
561 XFS_STATS_INC(xb_busy_locked);
562 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564 } else {
565 /* trylock worked */
Nathan Scottce8e9222006-01-11 15:39:08 +1100566 XB_SET_OWNER(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
Nathan Scottce8e9222006-01-11 15:39:08 +1100569 if (bp->b_flags & XBF_STALE) {
570 ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
571 bp->b_flags &= XBF_MAPPED;
David Chinner2f926582005-09-05 08:33:35 +1000572 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100573 XB_TRACE(bp, "got_lock", 0);
574 XFS_STATS_INC(xb_get_locked);
575 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100579 * Assembles a buffer covering the specified range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * Storage in memory for all portions of the buffer will be allocated,
581 * although backing storage may not be.
582 */
583xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100584xfs_buf_get_flags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 xfs_buftarg_t *target,/* target for buffer */
Nathan Scott204ab252006-01-11 20:50:22 +1100586 xfs_off_t ioff, /* starting offset of range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 size_t isize, /* length of range */
Nathan Scottce8e9222006-01-11 15:39:08 +1100588 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Nathan Scottce8e9222006-01-11 15:39:08 +1100590 xfs_buf_t *bp, *new_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int error = 0, i;
592
Nathan Scottce8e9222006-01-11 15:39:08 +1100593 new_bp = xfs_buf_allocate(flags);
594 if (unlikely(!new_bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return NULL;
596
Nathan Scottce8e9222006-01-11 15:39:08 +1100597 bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
598 if (bp == new_bp) {
599 error = _xfs_buf_lookup_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (error)
601 goto no_buffer;
602 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100603 xfs_buf_deallocate(new_bp);
604 if (unlikely(bp == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return NULL;
606 }
607
Nathan Scottce8e9222006-01-11 15:39:08 +1100608 for (i = 0; i < bp->b_page_count; i++)
609 mark_page_accessed(bp->b_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Nathan Scottce8e9222006-01-11 15:39:08 +1100611 if (!(bp->b_flags & XBF_MAPPED)) {
612 error = _xfs_buf_map_pages(bp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (unlikely(error)) {
614 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000615 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 goto no_buffer;
617 }
618 }
619
Nathan Scottce8e9222006-01-11 15:39:08 +1100620 XFS_STATS_INC(xb_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /*
623 * Always fill in the block number now, the mapped cases can do
624 * their own overlay of this later.
625 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100626 bp->b_bn = ioff;
627 bp->b_count_desired = bp->b_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Nathan Scottce8e9222006-01-11 15:39:08 +1100629 XB_TRACE(bp, "get", (unsigned long)flags);
630 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100633 if (flags & (XBF_LOCK | XBF_TRYLOCK))
634 xfs_buf_unlock(bp);
635 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return NULL;
637}
638
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100639STATIC int
640_xfs_buf_read(
641 xfs_buf_t *bp,
642 xfs_buf_flags_t flags)
643{
644 int status;
645
646 XB_TRACE(bp, "_xfs_buf_read", (unsigned long)flags);
647
648 ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
649 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
650
651 bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
652 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
653 bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
654 XBF_READ_AHEAD | _XBF_RUN_QUEUES);
655
656 status = xfs_buf_iorequest(bp);
657 if (!status && !(flags & XBF_ASYNC))
658 status = xfs_buf_iowait(bp);
659 return status;
660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662xfs_buf_t *
663xfs_buf_read_flags(
664 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100665 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100667 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Nathan Scottce8e9222006-01-11 15:39:08 +1100669 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Nathan Scottce8e9222006-01-11 15:39:08 +1100671 flags |= XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Nathan Scottce8e9222006-01-11 15:39:08 +1100673 bp = xfs_buf_get_flags(target, ioff, isize, flags);
674 if (bp) {
675 if (!XFS_BUF_ISDONE(bp)) {
676 XB_TRACE(bp, "read", (unsigned long)flags);
677 XFS_STATS_INC(xb_get_read);
Christoph Hellwig5d765b92008-12-03 12:20:26 +0100678 _xfs_buf_read(bp, flags);
Nathan Scottce8e9222006-01-11 15:39:08 +1100679 } else if (flags & XBF_ASYNC) {
680 XB_TRACE(bp, "read_async", (unsigned long)flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /*
682 * Read ahead call which is already satisfied,
683 * drop the buffer
684 */
685 goto no_buffer;
686 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100687 XB_TRACE(bp, "read_done", (unsigned long)flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* We do not want read in the flags */
Nathan Scottce8e9222006-01-11 15:39:08 +1100689 bp->b_flags &= ~XBF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691 }
692
Nathan Scottce8e9222006-01-11 15:39:08 +1100693 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 no_buffer:
Nathan Scottce8e9222006-01-11 15:39:08 +1100696 if (flags & (XBF_LOCK | XBF_TRYLOCK))
697 xfs_buf_unlock(bp);
698 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return NULL;
700}
701
702/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100703 * If we are not low on memory then do the readahead in a deadlock
704 * safe manner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 */
706void
Nathan Scottce8e9222006-01-11 15:39:08 +1100707xfs_buf_readahead(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 xfs_buftarg_t *target,
Nathan Scott204ab252006-01-11 20:50:22 +1100709 xfs_off_t ioff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 size_t isize,
Nathan Scottce8e9222006-01-11 15:39:08 +1100711 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 struct backing_dev_info *bdi;
714
Nathan Scottce8e9222006-01-11 15:39:08 +1100715 bdi = target->bt_mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (bdi_read_congested(bdi))
717 return;
718
Nathan Scottce8e9222006-01-11 15:39:08 +1100719 flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 xfs_buf_read_flags(target, ioff, isize, flags);
721}
722
723xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100724xfs_buf_get_empty(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 size_t len,
726 xfs_buftarg_t *target)
727{
Nathan Scottce8e9222006-01-11 15:39:08 +1100728 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Nathan Scottce8e9222006-01-11 15:39:08 +1100730 bp = xfs_buf_allocate(0);
731 if (bp)
732 _xfs_buf_initialize(bp, target, 0, len, 0);
733 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
736static inline struct page *
737mem_to_page(
738 void *addr)
739{
Christoph Lameter9e2779f2008-02-04 22:28:34 -0800740 if ((!is_vmalloc_addr(addr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return virt_to_page(addr);
742 } else {
743 return vmalloc_to_page(addr);
744 }
745}
746
747int
Nathan Scottce8e9222006-01-11 15:39:08 +1100748xfs_buf_associate_memory(
749 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 void *mem,
751 size_t len)
752{
753 int rval;
754 int i = 0;
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100755 unsigned long pageaddr;
756 unsigned long offset;
757 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 int page_count;
759
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100760 pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
761 offset = (unsigned long)mem - pageaddr;
762 buflen = PAGE_CACHE_ALIGN(len + offset);
763 page_count = buflen >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 /* Free any previous set of page pointers */
Nathan Scottce8e9222006-01-11 15:39:08 +1100766 if (bp->b_pages)
767 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Nathan Scottce8e9222006-01-11 15:39:08 +1100769 bp->b_pages = NULL;
770 bp->b_addr = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Nathan Scottce8e9222006-01-11 15:39:08 +1100772 rval = _xfs_buf_get_pages(bp, page_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (rval)
774 return rval;
775
Nathan Scottce8e9222006-01-11 15:39:08 +1100776 bp->b_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100778 for (i = 0; i < bp->b_page_count; i++) {
779 bp->b_pages[i] = mem_to_page((void *)pageaddr);
780 pageaddr += PAGE_CACHE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Lachlan McIlroyd1afb672007-11-27 17:01:24 +1100783 bp->b_count_desired = len;
784 bp->b_buffer_length = buflen;
Nathan Scottce8e9222006-01-11 15:39:08 +1100785 bp->b_flags |= XBF_MAPPED;
Christoph Hellwig6ab455e2008-05-19 16:34:42 +1000786 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 return 0;
789}
790
791xfs_buf_t *
Nathan Scottce8e9222006-01-11 15:39:08 +1100792xfs_buf_get_noaddr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 size_t len,
794 xfs_buftarg_t *target)
795{
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000796 unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
797 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Nathan Scottce8e9222006-01-11 15:39:08 +1100800 bp = xfs_buf_allocate(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (unlikely(bp == NULL))
802 goto fail;
Nathan Scottce8e9222006-01-11 15:39:08 +1100803 _xfs_buf_initialize(bp, target, 0, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000805 error = _xfs_buf_get_pages(bp, page_count, 0);
806 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 goto fail_free_buf;
808
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000809 for (i = 0; i < page_count; i++) {
810 bp->b_pages[i] = alloc_page(GFP_KERNEL);
811 if (!bp->b_pages[i])
812 goto fail_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000814 bp->b_flags |= _XBF_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000816 error = _xfs_buf_map_pages(bp, XBF_MAPPED);
817 if (unlikely(error)) {
818 printk(KERN_WARNING "%s: failed to map pages\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +1000819 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 goto fail_free_mem;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Nathan Scottce8e9222006-01-11 15:39:08 +1100823 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000825 XB_TRACE(bp, "no_daddr", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return bp;
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 fail_free_mem:
Christoph Hellwig1fa40b02007-05-14 18:23:50 +1000829 while (--i >= 0)
830 __free_page(bp->b_pages[i]);
Christoph Hellwigca165b82007-05-24 15:21:11 +1000831 _xfs_buf_free_pages(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 fail_free_buf:
Christoph Hellwigca165b82007-05-24 15:21:11 +1000833 xfs_buf_deallocate(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 fail:
835 return NULL;
836}
837
838/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 * Increment reference count on buffer, to hold the buffer concurrently
840 * with another thread which may release (free) the buffer asynchronously.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * Must hold the buffer already to call this function.
842 */
843void
Nathan Scottce8e9222006-01-11 15:39:08 +1100844xfs_buf_hold(
845 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Nathan Scottce8e9222006-01-11 15:39:08 +1100847 atomic_inc(&bp->b_hold);
848 XB_TRACE(bp, "hold", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100852 * Releases a hold on the specified buffer. If the
853 * the hold count is 1, calls xfs_buf_free.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
855void
Nathan Scottce8e9222006-01-11 15:39:08 +1100856xfs_buf_rele(
857 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Nathan Scottce8e9222006-01-11 15:39:08 +1100859 xfs_bufhash_t *hash = bp->b_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Nathan Scottce8e9222006-01-11 15:39:08 +1100861 XB_TRACE(bp, "rele", bp->b_relse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Nathan Scottfad3aa12006-02-01 12:14:52 +1100863 if (unlikely(!hash)) {
864 ASSERT(!bp->b_relse);
865 if (atomic_dec_and_test(&bp->b_hold))
866 xfs_buf_free(bp);
867 return;
868 }
869
Lachlan McIlroy37906892008-08-13 15:42:10 +1000870 ASSERT(atomic_read(&bp->b_hold) > 0);
Nathan Scottce8e9222006-01-11 15:39:08 +1100871 if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
872 if (bp->b_relse) {
873 atomic_inc(&bp->b_hold);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100875 (*(bp->b_relse)) (bp);
876 } else if (bp->b_flags & XBF_FS_MANAGED) {
Christoph Hellwig7f14d0a2005-11-02 15:09:35 +1100877 spin_unlock(&hash->bh_lock);
878 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +1100879 ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
880 list_del_init(&bp->b_hash_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 spin_unlock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +1100882 xfs_buf_free(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884 }
885}
886
887
888/*
889 * Mutual exclusion on buffers. Locking model:
890 *
891 * Buffers associated with inodes for which buffer locking
892 * is not enabled are not protected by semaphores, and are
893 * assumed to be exclusively owned by the caller. There is a
894 * spinlock in the buffer, used by the caller when concurrent
895 * access is possible.
896 */
897
898/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100899 * Locks a buffer object, if it is not already locked.
900 * Note that this in no way locks the underlying pages, so it is only
901 * useful for synchronizing concurrent use of buffer objects, not for
902 * synchronizing independent access to the underlying pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
904int
Nathan Scottce8e9222006-01-11 15:39:08 +1100905xfs_buf_cond_lock(
906 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 int locked;
909
Nathan Scottce8e9222006-01-11 15:39:08 +1100910 locked = down_trylock(&bp->b_sema) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (locked) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100912 XB_SET_OWNER(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Nathan Scottce8e9222006-01-11 15:39:08 +1100914 XB_TRACE(bp, "cond_lock", (long)locked);
915 return locked ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918#if defined(DEBUG) || defined(XFS_BLI_TRACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919int
Nathan Scottce8e9222006-01-11 15:39:08 +1100920xfs_buf_lock_value(
921 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Stephen Rothwelladaa6932008-04-22 15:26:13 +1000923 return bp->b_sema.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925#endif
926
927/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100928 * Locks a buffer object.
929 * Note that this in no way locks the underlying pages, so it is only
930 * useful for synchronizing concurrent use of buffer objects, not for
931 * synchronizing independent access to the underlying pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
Nathan Scottce8e9222006-01-11 15:39:08 +1100933void
934xfs_buf_lock(
935 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Nathan Scottce8e9222006-01-11 15:39:08 +1100937 XB_TRACE(bp, "lock", 0);
938 if (atomic_read(&bp->b_io_remaining))
939 blk_run_address_space(bp->b_target->bt_mapping);
940 down(&bp->b_sema);
941 XB_SET_OWNER(bp);
942 XB_TRACE(bp, "locked", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
945/*
Nathan Scottce8e9222006-01-11 15:39:08 +1100946 * Releases the lock on the buffer object.
David Chinner2f926582005-09-05 08:33:35 +1000947 * If the buffer is marked delwri but is not queued, do so before we
Nathan Scottce8e9222006-01-11 15:39:08 +1100948 * unlock the buffer as we need to set flags correctly. We also need to
David Chinner2f926582005-09-05 08:33:35 +1000949 * take a reference for the delwri queue because the unlocker is going to
950 * drop their's and they don't know we just queued it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
952void
Nathan Scottce8e9222006-01-11 15:39:08 +1100953xfs_buf_unlock(
954 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Nathan Scottce8e9222006-01-11 15:39:08 +1100956 if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
957 atomic_inc(&bp->b_hold);
958 bp->b_flags |= XBF_ASYNC;
959 xfs_buf_delwri_queue(bp, 0);
David Chinner2f926582005-09-05 08:33:35 +1000960 }
961
Nathan Scottce8e9222006-01-11 15:39:08 +1100962 XB_CLEAR_OWNER(bp);
963 up(&bp->b_sema);
964 XB_TRACE(bp, "unlock", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
967
968/*
969 * Pinning Buffer Storage in Memory
Nathan Scottce8e9222006-01-11 15:39:08 +1100970 * Ensure that no attempt to force a buffer to disk will succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 */
972void
Nathan Scottce8e9222006-01-11 15:39:08 +1100973xfs_buf_pin(
974 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Nathan Scottce8e9222006-01-11 15:39:08 +1100976 atomic_inc(&bp->b_pin_count);
977 XB_TRACE(bp, "pin", (long)bp->b_pin_count.counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980void
Nathan Scottce8e9222006-01-11 15:39:08 +1100981xfs_buf_unpin(
982 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Nathan Scottce8e9222006-01-11 15:39:08 +1100984 if (atomic_dec_and_test(&bp->b_pin_count))
985 wake_up_all(&bp->b_waiters);
986 XB_TRACE(bp, "unpin", (long)bp->b_pin_count.counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989int
Nathan Scottce8e9222006-01-11 15:39:08 +1100990xfs_buf_ispin(
991 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Nathan Scottce8e9222006-01-11 15:39:08 +1100993 return atomic_read(&bp->b_pin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Nathan Scottce8e9222006-01-11 15:39:08 +1100996STATIC void
997xfs_buf_wait_unpin(
998 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 DECLARE_WAITQUEUE (wait, current);
1001
Nathan Scottce8e9222006-01-11 15:39:08 +11001002 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return;
1004
Nathan Scottce8e9222006-01-11 15:39:08 +11001005 add_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 for (;;) {
1007 set_current_state(TASK_UNINTERRUPTIBLE);
Nathan Scottce8e9222006-01-11 15:39:08 +11001008 if (atomic_read(&bp->b_pin_count) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001010 if (atomic_read(&bp->b_io_remaining))
1011 blk_run_address_space(bp->b_target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 schedule();
1013 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001014 remove_wait_queue(&bp->b_waiters, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 set_current_state(TASK_RUNNING);
1016}
1017
1018/*
1019 * Buffer Utility Routines
1020 */
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001023xfs_buf_iodone_work(
David Howellsc4028952006-11-22 14:57:56 +00001024 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
David Howellsc4028952006-11-22 14:57:56 +00001026 xfs_buf_t *bp =
1027 container_of(work, xfs_buf_t, b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
David Chinner0bfefc42007-05-14 18:24:23 +10001029 /*
1030 * We can get an EOPNOTSUPP to ordered writes. Here we clear the
1031 * ordered flag and reissue them. Because we can't tell the higher
1032 * layers directly that they should not issue ordered I/O anymore, they
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001033 * need to check if the _XFS_BARRIER_FAILED flag was set during I/O completion.
David Chinner0bfefc42007-05-14 18:24:23 +10001034 */
1035 if ((bp->b_error == EOPNOTSUPP) &&
1036 (bp->b_flags & (XBF_ORDERED|XBF_ASYNC)) == (XBF_ORDERED|XBF_ASYNC)) {
1037 XB_TRACE(bp, "ordered_retry", bp->b_iodone);
1038 bp->b_flags &= ~XBF_ORDERED;
Christoph Hellwig73f6aa42008-10-10 17:28:29 +11001039 bp->b_flags |= _XFS_BARRIER_FAILED;
David Chinner0bfefc42007-05-14 18:24:23 +10001040 xfs_buf_iorequest(bp);
1041 } else if (bp->b_iodone)
Nathan Scottce8e9222006-01-11 15:39:08 +11001042 (*(bp->b_iodone))(bp);
1043 else if (bp->b_flags & XBF_ASYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 xfs_buf_relse(bp);
1045}
1046
1047void
Nathan Scottce8e9222006-01-11 15:39:08 +11001048xfs_buf_ioend(
1049 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 int schedule)
1051{
Lachlan McIlroy77be55a2007-11-23 16:31:00 +11001052 bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
Nathan Scottce8e9222006-01-11 15:39:08 +11001053 if (bp->b_error == 0)
1054 bp->b_flags |= XBF_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Nathan Scottce8e9222006-01-11 15:39:08 +11001056 XB_TRACE(bp, "iodone", bp->b_iodone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Nathan Scottce8e9222006-01-11 15:39:08 +11001058 if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (schedule) {
David Howellsc4028952006-11-22 14:57:56 +00001060 INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
Nathan Scottce8e9222006-01-11 15:39:08 +11001061 queue_work(xfslogd_workqueue, &bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 } else {
David Howellsc4028952006-11-22 14:57:56 +00001063 xfs_buf_iodone_work(&bp->b_iodone_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065 } else {
David Chinnerb4dd3302008-08-13 16:36:11 +10001066 complete(&bp->b_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068}
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070void
Nathan Scottce8e9222006-01-11 15:39:08 +11001071xfs_buf_ioerror(
1072 xfs_buf_t *bp,
1073 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 ASSERT(error >= 0 && error <= 0xffff);
Nathan Scottce8e9222006-01-11 15:39:08 +11001076 bp->b_error = (unsigned short)error;
1077 XB_TRACE(bp, "ioerror", (unsigned long)error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080int
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001081xfs_bawrite(
1082 void *mp,
1083 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001085 XB_TRACE(bp, "bawrite", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001087 ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001089 xfs_buf_delwri_dequeue(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001091 bp->b_flags &= ~(XBF_READ | XBF_DELWRI | XBF_READ_AHEAD);
1092 bp->b_flags |= (XBF_WRITE | XBF_ASYNC | _XBF_RUN_QUEUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05001094 bp->b_mount = mp;
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001095 bp->b_strat = xfs_bdstrat_cb;
1096 return xfs_bdstrat_cb(bp);
1097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001099void
1100xfs_bdwrite(
1101 void *mp,
1102 struct xfs_buf *bp)
1103{
1104 XB_TRACE(bp, "bdwrite", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001106 bp->b_strat = xfs_bdstrat_cb;
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05001107 bp->b_mount = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Christoph Hellwig5d765b92008-12-03 12:20:26 +01001109 bp->b_flags &= ~XBF_READ;
1110 bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
1111
1112 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
David Chinner7989cb82007-02-10 18:34:56 +11001115STATIC_INLINE void
Nathan Scottce8e9222006-01-11 15:39:08 +11001116_xfs_buf_ioend(
1117 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int schedule)
1119{
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001120 if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
1121 bp->b_flags &= ~_XBF_PAGE_LOCKED;
Nathan Scottce8e9222006-01-11 15:39:08 +11001122 xfs_buf_ioend(bp, schedule);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
Al Viro782e3b32007-10-12 07:17:47 +01001126STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001127xfs_buf_bio_end_io(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 int error)
1130{
Nathan Scottce8e9222006-01-11 15:39:08 +11001131 xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
1132 unsigned int blocksize = bp->b_target->bt_bsize;
Nathan Scotteedb5532005-09-02 16:39:56 +10001133 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Lachlan McIlroycfbe5262008-12-12 15:27:25 +11001135 xfs_buf_ioerror(bp, -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Nathan Scotteedb5532005-09-02 16:39:56 +10001137 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 struct page *page = bvec->bv_page;
1139
Nathan Scott948ecdb2006-09-28 11:03:13 +10001140 ASSERT(!PagePrivate(page));
Nathan Scottce8e9222006-01-11 15:39:08 +11001141 if (unlikely(bp->b_error)) {
1142 if (bp->b_flags & XBF_READ)
Nathan Scotteedb5532005-09-02 16:39:56 +10001143 ClearPageUptodate(page);
Nathan Scottce8e9222006-01-11 15:39:08 +11001144 } else if (blocksize >= PAGE_CACHE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 SetPageUptodate(page);
1146 } else if (!PagePrivate(page) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001147 (bp->b_flags & _XBF_PAGE_CACHE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 set_page_region(page, bvec->bv_offset, bvec->bv_len);
1149 }
1150
Nathan Scotteedb5532005-09-02 16:39:56 +10001151 if (--bvec >= bio->bi_io_vec)
1152 prefetchw(&bvec->bv_page->flags);
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001153
1154 if (bp->b_flags & _XBF_PAGE_LOCKED)
1155 unlock_page(page);
Nathan Scotteedb5532005-09-02 16:39:56 +10001156 } while (bvec >= bio->bi_io_vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Nathan Scottce8e9222006-01-11 15:39:08 +11001158 _xfs_buf_ioend(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
1162STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001163_xfs_buf_ioapply(
1164 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Christoph Hellwiga9759f22007-12-07 14:07:08 +11001166 int rw, map_i, total_nr_pages, nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 struct bio *bio;
Nathan Scottce8e9222006-01-11 15:39:08 +11001168 int offset = bp->b_offset;
1169 int size = bp->b_count_desired;
1170 sector_t sector = bp->b_bn;
1171 unsigned int blocksize = bp->b_target->bt_bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Nathan Scottce8e9222006-01-11 15:39:08 +11001173 total_nr_pages = bp->b_page_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 map_i = 0;
1175
Nathan Scottce8e9222006-01-11 15:39:08 +11001176 if (bp->b_flags & XBF_ORDERED) {
1177 ASSERT(!(bp->b_flags & XBF_READ));
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001178 rw = WRITE_BARRIER;
Nathan Scott51bdd702006-09-28 11:01:57 +10001179 } else if (bp->b_flags & _XBF_RUN_QUEUES) {
1180 ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
1181 bp->b_flags &= ~_XBF_RUN_QUEUES;
1182 rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
1183 } else {
1184 rw = (bp->b_flags & XBF_WRITE) ? WRITE :
1185 (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001186 }
1187
Nathan Scottce8e9222006-01-11 15:39:08 +11001188 /* Special code path for reading a sub page size buffer in --
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 * we populate up the whole page, and hence the other metadata
1190 * in the same page. This optimization is only valid when the
Nathan Scottce8e9222006-01-11 15:39:08 +11001191 * filesystem block size is not smaller than the page size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001193 if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
Christoph Hellwig6ab455e2008-05-19 16:34:42 +10001194 ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
1195 (XBF_READ|_XBF_PAGE_LOCKED)) &&
Nathan Scottce8e9222006-01-11 15:39:08 +11001196 (blocksize >= PAGE_CACHE_SIZE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 bio = bio_alloc(GFP_NOIO, 1);
1198
Nathan Scottce8e9222006-01-11 15:39:08 +11001199 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 bio->bi_sector = sector - (offset >> BBSHIFT);
Nathan Scottce8e9222006-01-11 15:39:08 +11001201 bio->bi_end_io = xfs_buf_bio_end_io;
1202 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Nathan Scottce8e9222006-01-11 15:39:08 +11001204 bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 size = 0;
1206
Nathan Scottce8e9222006-01-11 15:39:08 +11001207 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 goto submit_io;
1210 }
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212next_chunk:
Nathan Scottce8e9222006-01-11 15:39:08 +11001213 atomic_inc(&bp->b_io_remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
1215 if (nr_pages > total_nr_pages)
1216 nr_pages = total_nr_pages;
1217
1218 bio = bio_alloc(GFP_NOIO, nr_pages);
Nathan Scottce8e9222006-01-11 15:39:08 +11001219 bio->bi_bdev = bp->b_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 bio->bi_sector = sector;
Nathan Scottce8e9222006-01-11 15:39:08 +11001221 bio->bi_end_io = xfs_buf_bio_end_io;
1222 bio->bi_private = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 for (; size && nr_pages; nr_pages--, map_i++) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001225 int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 if (nbytes > size)
1228 nbytes = size;
1229
Nathan Scottce8e9222006-01-11 15:39:08 +11001230 rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
1231 if (rbytes < nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 break;
1233
1234 offset = 0;
1235 sector += nbytes >> BBSHIFT;
1236 size -= nbytes;
1237 total_nr_pages--;
1238 }
1239
1240submit_io:
1241 if (likely(bio->bi_size)) {
1242 submit_bio(rw, bio);
1243 if (size)
1244 goto next_chunk;
1245 } else {
1246 bio_put(bio);
Nathan Scottce8e9222006-01-11 15:39:08 +11001247 xfs_buf_ioerror(bp, EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249}
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251int
Nathan Scottce8e9222006-01-11 15:39:08 +11001252xfs_buf_iorequest(
1253 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Nathan Scottce8e9222006-01-11 15:39:08 +11001255 XB_TRACE(bp, "iorequest", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Nathan Scottce8e9222006-01-11 15:39:08 +11001257 if (bp->b_flags & XBF_DELWRI) {
1258 xfs_buf_delwri_queue(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return 0;
1260 }
1261
Nathan Scottce8e9222006-01-11 15:39:08 +11001262 if (bp->b_flags & XBF_WRITE) {
1263 xfs_buf_wait_unpin(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
Nathan Scottce8e9222006-01-11 15:39:08 +11001266 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /* Set the count to 1 initially, this will stop an I/O
1269 * completion callout which happens before we have started
Nathan Scottce8e9222006-01-11 15:39:08 +11001270 * all the I/O from calling xfs_buf_ioend too early.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001272 atomic_set(&bp->b_io_remaining, 1);
1273 _xfs_buf_ioapply(bp);
1274 _xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Nathan Scottce8e9222006-01-11 15:39:08 +11001276 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return 0;
1278}
1279
1280/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001281 * Waits for I/O to complete on the buffer supplied.
1282 * It returns immediately if no I/O is pending.
1283 * It returns the I/O error code, if any, or 0 if there was no error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 */
1285int
Nathan Scottce8e9222006-01-11 15:39:08 +11001286xfs_buf_iowait(
1287 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Nathan Scottce8e9222006-01-11 15:39:08 +11001289 XB_TRACE(bp, "iowait", 0);
1290 if (atomic_read(&bp->b_io_remaining))
1291 blk_run_address_space(bp->b_target->bt_mapping);
David Chinnerb4dd3302008-08-13 16:36:11 +10001292 wait_for_completion(&bp->b_iowait);
Nathan Scottce8e9222006-01-11 15:39:08 +11001293 XB_TRACE(bp, "iowaited", (long)bp->b_error);
1294 return bp->b_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
Nathan Scottce8e9222006-01-11 15:39:08 +11001297xfs_caddr_t
1298xfs_buf_offset(
1299 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 size_t offset)
1301{
1302 struct page *page;
1303
Nathan Scottce8e9222006-01-11 15:39:08 +11001304 if (bp->b_flags & XBF_MAPPED)
1305 return XFS_BUF_PTR(bp) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Nathan Scottce8e9222006-01-11 15:39:08 +11001307 offset += bp->b_offset;
1308 page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
1309 return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
1312/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 * Move data into or out of a buffer.
1314 */
1315void
Nathan Scottce8e9222006-01-11 15:39:08 +11001316xfs_buf_iomove(
1317 xfs_buf_t *bp, /* buffer to process */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 size_t boff, /* starting buffer offset */
1319 size_t bsize, /* length to copy */
1320 caddr_t data, /* data address */
Nathan Scottce8e9222006-01-11 15:39:08 +11001321 xfs_buf_rw_t mode) /* read/write/zero flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 size_t bend, cpoff, csize;
1324 struct page *page;
1325
1326 bend = boff + bsize;
1327 while (boff < bend) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001328 page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
1329 cpoff = xfs_buf_poff(boff + bp->b_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 csize = min_t(size_t,
Nathan Scottce8e9222006-01-11 15:39:08 +11001331 PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
1334
1335 switch (mode) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001336 case XBRW_ZERO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 memset(page_address(page) + cpoff, 0, csize);
1338 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001339 case XBRW_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 memcpy(data, page_address(page) + cpoff, csize);
1341 break;
Nathan Scottce8e9222006-01-11 15:39:08 +11001342 case XBRW_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 memcpy(page_address(page) + cpoff, data, csize);
1344 }
1345
1346 boff += csize;
1347 data += csize;
1348 }
1349}
1350
1351/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001352 * Handling of buffer targets (buftargs).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
1354
1355/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001356 * Wait for any bufs with callbacks that have been submitted but
1357 * have not yet returned... walk the hash list for the target.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 */
1359void
1360xfs_wait_buftarg(
1361 xfs_buftarg_t *btp)
1362{
1363 xfs_buf_t *bp, *n;
1364 xfs_bufhash_t *hash;
1365 uint i;
1366
1367 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1368 hash = &btp->bt_hash[i];
1369again:
1370 spin_lock(&hash->bh_lock);
Nathan Scottce8e9222006-01-11 15:39:08 +11001371 list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
1372 ASSERT(btp == bp->b_target);
1373 if (!(bp->b_flags & XBF_FS_MANAGED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 spin_unlock(&hash->bh_lock);
David Chinner2f926582005-09-05 08:33:35 +10001375 /*
1376 * Catch superblock reference count leaks
1377 * immediately
1378 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001379 BUG_ON(bp->b_bn == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 delay(100);
1381 goto again;
1382 }
1383 }
1384 spin_unlock(&hash->bh_lock);
1385 }
1386}
1387
1388/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001389 * Allocate buffer hash table for a given target.
1390 * For devices containing metadata (i.e. not the log/realtime devices)
1391 * we need to allocate a much larger hash table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 */
1393STATIC void
1394xfs_alloc_bufhash(
1395 xfs_buftarg_t *btp,
1396 int external)
1397{
1398 unsigned int i;
1399
1400 btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
1401 btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
1402 btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) *
Vlad Apostolov93c189c2006-11-11 18:03:49 +11001403 sizeof(xfs_bufhash_t), KM_SLEEP | KM_LARGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 for (i = 0; i < (1 << btp->bt_hashshift); i++) {
1405 spin_lock_init(&btp->bt_hash[i].bh_lock);
1406 INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
1407 }
1408}
1409
1410STATIC void
1411xfs_free_bufhash(
1412 xfs_buftarg_t *btp)
1413{
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001414 kmem_free(btp->bt_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 btp->bt_hash = NULL;
1416}
1417
David Chinnera6867a62006-01-11 15:37:58 +11001418/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001419 * buftarg list for delwrite queue processing
David Chinnera6867a62006-01-11 15:37:58 +11001420 */
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001421static LIST_HEAD(xfs_buftarg_list);
David Chinner7989cb82007-02-10 18:34:56 +11001422static DEFINE_SPINLOCK(xfs_buftarg_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001423
1424STATIC void
1425xfs_register_buftarg(
1426 xfs_buftarg_t *btp)
1427{
1428 spin_lock(&xfs_buftarg_lock);
1429 list_add(&btp->bt_list, &xfs_buftarg_list);
1430 spin_unlock(&xfs_buftarg_lock);
1431}
1432
1433STATIC void
1434xfs_unregister_buftarg(
1435 xfs_buftarg_t *btp)
1436{
1437 spin_lock(&xfs_buftarg_lock);
1438 list_del(&btp->bt_list);
1439 spin_unlock(&xfs_buftarg_lock);
1440}
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442void
1443xfs_free_buftarg(
Christoph Hellwigb7963132009-03-03 14:48:37 -05001444 struct xfs_mount *mp,
1445 struct xfs_buftarg *btp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 xfs_flush_buftarg(btp, 1);
Christoph Hellwigb7963132009-03-03 14:48:37 -05001448 if (mp->m_flags & XFS_MOUNT_BARRIER)
1449 xfs_blkdev_issue_flush(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 xfs_free_bufhash(btp);
Nathan Scottce8e9222006-01-11 15:39:08 +11001451 iput(btp->bt_mapping->host);
David Chinnera6867a62006-01-11 15:37:58 +11001452
Nathan Scottce8e9222006-01-11 15:39:08 +11001453 /* Unregister the buftarg first so that we don't get a
1454 * wakeup finding a non-existent task
1455 */
David Chinnera6867a62006-01-11 15:37:58 +11001456 xfs_unregister_buftarg(btp);
1457 kthread_stop(btp->bt_task);
1458
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001459 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462STATIC int
1463xfs_setsize_buftarg_flags(
1464 xfs_buftarg_t *btp,
1465 unsigned int blocksize,
1466 unsigned int sectorsize,
1467 int verbose)
1468{
Nathan Scottce8e9222006-01-11 15:39:08 +11001469 btp->bt_bsize = blocksize;
1470 btp->bt_sshift = ffs(sectorsize) - 1;
1471 btp->bt_smask = sectorsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Nathan Scottce8e9222006-01-11 15:39:08 +11001473 if (set_blocksize(btp->bt_bdev, sectorsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 printk(KERN_WARNING
1475 "XFS: Cannot set_blocksize to %u on device %s\n",
1476 sectorsize, XFS_BUFTARG_NAME(btp));
1477 return EINVAL;
1478 }
1479
1480 if (verbose &&
1481 (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
1482 printk(KERN_WARNING
1483 "XFS: %u byte sectors in use on device %s. "
1484 "This is suboptimal; %u or greater is ideal.\n",
1485 sectorsize, XFS_BUFTARG_NAME(btp),
1486 (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
1487 }
1488
1489 return 0;
1490}
1491
1492/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001493 * When allocating the initial buffer target we have not yet
1494 * read in the superblock, so don't know what sized sectors
1495 * are being used is at this early stage. Play safe.
1496 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497STATIC int
1498xfs_setsize_buftarg_early(
1499 xfs_buftarg_t *btp,
1500 struct block_device *bdev)
1501{
1502 return xfs_setsize_buftarg_flags(btp,
1503 PAGE_CACHE_SIZE, bdev_hardsect_size(bdev), 0);
1504}
1505
1506int
1507xfs_setsize_buftarg(
1508 xfs_buftarg_t *btp,
1509 unsigned int blocksize,
1510 unsigned int sectorsize)
1511{
1512 return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
1513}
1514
1515STATIC int
1516xfs_mapping_buftarg(
1517 xfs_buftarg_t *btp,
1518 struct block_device *bdev)
1519{
1520 struct backing_dev_info *bdi;
1521 struct inode *inode;
1522 struct address_space *mapping;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001523 static const struct address_space_operations mapping_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 .sync_page = block_sync_page,
Christoph Lametere965f962006-02-01 03:05:41 -08001525 .migratepage = fail_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 };
1527
1528 inode = new_inode(bdev->bd_inode->i_sb);
1529 if (!inode) {
1530 printk(KERN_WARNING
1531 "XFS: Cannot allocate mapping inode for device %s\n",
1532 XFS_BUFTARG_NAME(btp));
1533 return ENOMEM;
1534 }
1535 inode->i_mode = S_IFBLK;
1536 inode->i_bdev = bdev;
1537 inode->i_rdev = bdev->bd_dev;
1538 bdi = blk_get_backing_dev_info(bdev);
1539 if (!bdi)
1540 bdi = &default_backing_dev_info;
1541 mapping = &inode->i_data;
1542 mapping->a_ops = &mapping_aops;
1543 mapping->backing_dev_info = bdi;
1544 mapping_set_gfp_mask(mapping, GFP_NOFS);
Nathan Scottce8e9222006-01-11 15:39:08 +11001545 btp->bt_mapping = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return 0;
1547}
1548
David Chinnera6867a62006-01-11 15:37:58 +11001549STATIC int
1550xfs_alloc_delwrite_queue(
1551 xfs_buftarg_t *btp)
1552{
1553 int error = 0;
1554
1555 INIT_LIST_HEAD(&btp->bt_list);
1556 INIT_LIST_HEAD(&btp->bt_delwrite_queue);
Eric Sandeen007c61c2007-10-11 17:43:56 +10001557 spin_lock_init(&btp->bt_delwrite_lock);
David Chinnera6867a62006-01-11 15:37:58 +11001558 btp->bt_flags = 0;
1559 btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd");
1560 if (IS_ERR(btp->bt_task)) {
1561 error = PTR_ERR(btp->bt_task);
1562 goto out_error;
1563 }
1564 xfs_register_buftarg(btp);
1565out_error:
1566 return error;
1567}
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569xfs_buftarg_t *
1570xfs_alloc_buftarg(
1571 struct block_device *bdev,
1572 int external)
1573{
1574 xfs_buftarg_t *btp;
1575
1576 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
1577
Nathan Scottce8e9222006-01-11 15:39:08 +11001578 btp->bt_dev = bdev->bd_dev;
1579 btp->bt_bdev = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (xfs_setsize_buftarg_early(btp, bdev))
1581 goto error;
1582 if (xfs_mapping_buftarg(btp, bdev))
1583 goto error;
David Chinnera6867a62006-01-11 15:37:58 +11001584 if (xfs_alloc_delwrite_queue(btp))
1585 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 xfs_alloc_bufhash(btp, external);
1587 return btp;
1588
1589error:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001590 kmem_free(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return NULL;
1592}
1593
1594
1595/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001596 * Delayed write buffer handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001599xfs_buf_delwri_queue(
1600 xfs_buf_t *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 int unlock)
1602{
Nathan Scottce8e9222006-01-11 15:39:08 +11001603 struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
1604 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
David Chinnera6867a62006-01-11 15:37:58 +11001605
Nathan Scottce8e9222006-01-11 15:39:08 +11001606 XB_TRACE(bp, "delwri_q", (long)unlock);
1607 ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
David Chinnera6867a62006-01-11 15:37:58 +11001609 spin_lock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /* If already in the queue, dequeue and place at tail */
Nathan Scottce8e9222006-01-11 15:39:08 +11001611 if (!list_empty(&bp->b_list)) {
1612 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1613 if (unlock)
1614 atomic_dec(&bp->b_hold);
1615 list_del(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
Nathan Scottce8e9222006-01-11 15:39:08 +11001618 bp->b_flags |= _XBF_DELWRI_Q;
1619 list_add_tail(&bp->b_list, dwq);
1620 bp->b_queuetime = jiffies;
David Chinnera6867a62006-01-11 15:37:58 +11001621 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 if (unlock)
Nathan Scottce8e9222006-01-11 15:39:08 +11001624 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
1627void
Nathan Scottce8e9222006-01-11 15:39:08 +11001628xfs_buf_delwri_dequeue(
1629 xfs_buf_t *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Nathan Scottce8e9222006-01-11 15:39:08 +11001631 spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 int dequeued = 0;
1633
David Chinnera6867a62006-01-11 15:37:58 +11001634 spin_lock(dwlk);
Nathan Scottce8e9222006-01-11 15:39:08 +11001635 if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
1636 ASSERT(bp->b_flags & _XBF_DELWRI_Q);
1637 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 dequeued = 1;
1639 }
Nathan Scottce8e9222006-01-11 15:39:08 +11001640 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
David Chinnera6867a62006-01-11 15:37:58 +11001641 spin_unlock(dwlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 if (dequeued)
Nathan Scottce8e9222006-01-11 15:39:08 +11001644 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Nathan Scottce8e9222006-01-11 15:39:08 +11001646 XB_TRACE(bp, "delwri_dq", (long)dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
1649STATIC void
Nathan Scottce8e9222006-01-11 15:39:08 +11001650xfs_buf_runall_queues(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 struct workqueue_struct *queue)
1652{
1653 flush_workqueue(queue);
1654}
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001657xfsbufd_wakeup(
Nathan Scott15c84a42005-11-04 10:51:01 +11001658 int priority,
1659 gfp_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001661 xfs_buftarg_t *btp;
David Chinnera6867a62006-01-11 15:37:58 +11001662
1663 spin_lock(&xfs_buftarg_lock);
Christoph Hellwigda7f93e2006-01-11 20:49:57 +11001664 list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001665 if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
David Chinnera6867a62006-01-11 15:37:58 +11001666 continue;
Nathan Scottce8e9222006-01-11 15:39:08 +11001667 set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
David Chinnera6867a62006-01-11 15:37:58 +11001668 wake_up_process(btp->bt_task);
1669 }
1670 spin_unlock(&xfs_buftarg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return 0;
1672}
1673
David Chinner585e6d82007-02-10 18:32:29 +11001674/*
1675 * Move as many buffers as specified to the supplied list
1676 * idicating if we skipped any buffers to prevent deadlocks.
1677 */
1678STATIC int
1679xfs_buf_delwri_split(
1680 xfs_buftarg_t *target,
1681 struct list_head *list,
David Chinner5e6a07d2007-02-10 18:34:49 +11001682 unsigned long age)
David Chinner585e6d82007-02-10 18:32:29 +11001683{
1684 xfs_buf_t *bp, *n;
1685 struct list_head *dwq = &target->bt_delwrite_queue;
1686 spinlock_t *dwlk = &target->bt_delwrite_lock;
1687 int skipped = 0;
David Chinner5e6a07d2007-02-10 18:34:49 +11001688 int force;
David Chinner585e6d82007-02-10 18:32:29 +11001689
David Chinner5e6a07d2007-02-10 18:34:49 +11001690 force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
David Chinner585e6d82007-02-10 18:32:29 +11001691 INIT_LIST_HEAD(list);
1692 spin_lock(dwlk);
1693 list_for_each_entry_safe(bp, n, dwq, b_list) {
1694 XB_TRACE(bp, "walkq1", (long)xfs_buf_ispin(bp));
1695 ASSERT(bp->b_flags & XBF_DELWRI);
1696
1697 if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
David Chinner5e6a07d2007-02-10 18:34:49 +11001698 if (!force &&
David Chinner585e6d82007-02-10 18:32:29 +11001699 time_before(jiffies, bp->b_queuetime + age)) {
1700 xfs_buf_unlock(bp);
1701 break;
1702 }
1703
1704 bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
1705 _XBF_RUN_QUEUES);
1706 bp->b_flags |= XBF_WRITE;
1707 list_move_tail(&bp->b_list, list);
1708 } else
1709 skipped++;
1710 }
1711 spin_unlock(dwlk);
1712
1713 return skipped;
1714
1715}
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717STATIC int
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001718xfsbufd(
David Chinner585e6d82007-02-10 18:32:29 +11001719 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
David Chinner585e6d82007-02-10 18:32:29 +11001721 struct list_head tmp;
1722 xfs_buftarg_t *target = (xfs_buftarg_t *)data;
1723 int count;
1724 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 current->flags |= PF_MEMALLOC;
1727
Rafael J. Wysocki978c7b22007-12-07 14:09:02 +11001728 set_freezable();
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 do {
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001731 if (unlikely(freezing(current))) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001732 set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001733 refrigerator();
Nathan Scottabd0cf72005-05-05 13:30:13 -07001734 } else {
Nathan Scottce8e9222006-01-11 15:39:08 +11001735 clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
Nathan Scottabd0cf72005-05-05 13:30:13 -07001736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Nathan Scott15c84a42005-11-04 10:51:01 +11001738 schedule_timeout_interruptible(
1739 xfs_buf_timer_centisecs * msecs_to_jiffies(10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
David Chinner585e6d82007-02-10 18:32:29 +11001741 xfs_buf_delwri_split(target, &tmp,
David Chinner5e6a07d2007-02-10 18:34:49 +11001742 xfs_buf_age_centisecs * msecs_to_jiffies(10));
David Chinner585e6d82007-02-10 18:32:29 +11001743
Nathan Scottf07c2252006-09-28 10:52:15 +10001744 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 while (!list_empty(&tmp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001746 bp = list_entry(tmp.next, xfs_buf_t, b_list);
1747 ASSERT(target == bp->b_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Nathan Scottce8e9222006-01-11 15:39:08 +11001749 list_del_init(&bp->b_list);
1750 xfs_buf_iostrategy(bp);
David Chinner585e6d82007-02-10 18:32:29 +11001751 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
1753
Felix Blyakher3a011a12009-02-18 15:56:51 -06001754 if (as_list_len > 0)
1755 purge_addresses();
Nathan Scottf07c2252006-09-28 10:52:15 +10001756 if (count)
1757 blk_run_address_space(target->bt_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001759 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Christoph Hellwig4df08c52005-09-05 08:34:18 +10001761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
1764/*
Nathan Scottce8e9222006-01-11 15:39:08 +11001765 * Go through all incore buffers, and release buffers if they belong to
1766 * the given device. This is used in filesystem error handling to
1767 * preserve the consistency of its metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 */
1769int
1770xfs_flush_buftarg(
David Chinner585e6d82007-02-10 18:32:29 +11001771 xfs_buftarg_t *target,
1772 int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
David Chinner585e6d82007-02-10 18:32:29 +11001774 struct list_head tmp;
1775 xfs_buf_t *bp, *n;
1776 int pincount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Nathan Scottce8e9222006-01-11 15:39:08 +11001778 xfs_buf_runall_queues(xfsdatad_workqueue);
1779 xfs_buf_runall_queues(xfslogd_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
David Chinner5e6a07d2007-02-10 18:34:49 +11001781 set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
1782 pincount = xfs_buf_delwri_split(target, &tmp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 /*
1785 * Dropped the delayed write list lock, now walk the temporary list
1786 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001787 list_for_each_entry_safe(bp, n, &tmp, b_list) {
David Chinner585e6d82007-02-10 18:32:29 +11001788 ASSERT(target == bp->b_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (wait)
Nathan Scottce8e9222006-01-11 15:39:08 +11001790 bp->b_flags &= ~XBF_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 else
Nathan Scottce8e9222006-01-11 15:39:08 +11001792 list_del_init(&bp->b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Nathan Scottce8e9222006-01-11 15:39:08 +11001794 xfs_buf_iostrategy(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796
Nathan Scottf07c2252006-09-28 10:52:15 +10001797 if (wait)
1798 blk_run_address_space(target->bt_mapping);
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 /*
1801 * Remaining list items must be flushed before returning
1802 */
1803 while (!list_empty(&tmp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +11001804 bp = list_entry(tmp.next, xfs_buf_t, b_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Nathan Scottce8e9222006-01-11 15:39:08 +11001806 list_del_init(&bp->b_list);
1807 xfs_iowait(bp);
1808 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return pincount;
1812}
1813
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001814int __init
Nathan Scottce8e9222006-01-11 15:39:08 +11001815xfs_buf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Nathan Scottce8e9222006-01-11 15:39:08 +11001817#ifdef XFS_BUF_TRACE
Lachlan McIlroy5695ef462008-08-13 16:51:57 +10001818 xfs_buf_trace_buf = ktrace_alloc(XFS_BUF_TRACE_SIZE, KM_NOFS);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001819#endif
1820
Nathan Scott87582802006-03-14 13:18:19 +11001821 xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
1822 KM_ZONE_HWALIGN, NULL);
Nathan Scottce8e9222006-01-11 15:39:08 +11001823 if (!xfs_buf_zone)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001824 goto out_free_trace_buf;
1825
Rafael J. Wysockib4337692007-03-22 00:11:27 -08001826 xfslogd_workqueue = create_workqueue("xfslogd");
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001827 if (!xfslogd_workqueue)
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001828 goto out_free_buf_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Rafael J. Wysockib4337692007-03-22 00:11:27 -08001830 xfsdatad_workqueue = create_workqueue("xfsdatad");
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001831 if (!xfsdatad_workqueue)
1832 goto out_destroy_xfslogd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Rusty Russell8e1f9362007-07-17 04:03:17 -07001834 register_shrinker(&xfs_buf_shake);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001837 out_destroy_xfslogd_workqueue:
1838 destroy_workqueue(xfslogd_workqueue);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001839 out_free_buf_zone:
Nathan Scottce8e9222006-01-11 15:39:08 +11001840 kmem_zone_destroy(xfs_buf_zone);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001841 out_free_trace_buf:
Nathan Scottce8e9222006-01-11 15:39:08 +11001842#ifdef XFS_BUF_TRACE
1843 ktrace_free(xfs_buf_trace_buf);
Christoph Hellwig23ea4032005-06-21 15:14:01 +10001844#endif
Nathan Scott87582802006-03-14 13:18:19 +11001845 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848void
Nathan Scottce8e9222006-01-11 15:39:08 +11001849xfs_buf_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Rusty Russell8e1f9362007-07-17 04:03:17 -07001851 unregister_shrinker(&xfs_buf_shake);
Christoph Hellwig04d8b282005-11-02 10:15:05 +11001852 destroy_workqueue(xfsdatad_workqueue);
1853 destroy_workqueue(xfslogd_workqueue);
Nathan Scottce8e9222006-01-11 15:39:08 +11001854 kmem_zone_destroy(xfs_buf_zone);
1855#ifdef XFS_BUF_TRACE
1856 ktrace_free(xfs_buf_trace_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
Tim Shimmine6a0e9c2007-05-08 13:49:59 +10001859
1860#ifdef CONFIG_KDB_MODULES
1861struct list_head *
1862xfs_get_buftarg_list(void)
1863{
1864 return &xfs_buftarg_list;
1865}
1866#endif