Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | f07c225 | 2006-09-28 10:52:15 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Vlad Apostolov | 93c189c | 2006-11-11 18:03:49 +1100 | [diff] [blame] | 18 | #include "xfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/stddef.h> |
| 20 | #include <linux/errno.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Hellwig | 4df08c5 | 2005-09-05 08:34:18 +1000 | [diff] [blame] | 32 | #include <linux/kthread.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 33 | #include <linux/migrate.h> |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 34 | #include <linux/backing-dev.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 35 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Christoph Hellwig | b796313 | 2009-03-03 14:48:37 -0500 | [diff] [blame] | 37 | #include "xfs_sb.h" |
| 38 | #include "xfs_inum.h" |
Dave Chinner | ed3b4d6 | 2010-05-21 12:07:08 +1000 | [diff] [blame] | 39 | #include "xfs_log.h" |
Christoph Hellwig | b796313 | 2009-03-03 14:48:37 -0500 | [diff] [blame] | 40 | #include "xfs_ag.h" |
Christoph Hellwig | b796313 | 2009-03-03 14:48:37 -0500 | [diff] [blame] | 41 | #include "xfs_mount.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 42 | #include "xfs_trace.h" |
Christoph Hellwig | b796313 | 2009-03-03 14:48:37 -0500 | [diff] [blame] | 43 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 44 | static kmem_zone_t *xfs_buf_zone; |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 45 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 46 | static struct workqueue_struct *xfslogd_workqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 48 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #else |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 53 | # 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #endif |
| 57 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 58 | #define xb_to_gfp(flags) \ |
| 59 | ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \ |
| 60 | ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 62 | #define xb_to_km(flags) \ |
| 63 | (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
James Bottomley | 73c77e2 | 2010-01-25 11:42:24 -0600 | [diff] [blame] | 66 | static inline int |
| 67 | xfs_buf_is_vmapped( |
| 68 | struct xfs_buf *bp) |
| 69 | { |
| 70 | /* |
| 71 | * Return true if the buffer is vmapped. |
| 72 | * |
| 73 | * The XBF_MAPPED flag is set if the buffer should be mapped, but the |
| 74 | * code is clever enough to know it doesn't have to map a single page, |
| 75 | * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1. |
| 76 | */ |
| 77 | return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1; |
| 78 | } |
| 79 | |
| 80 | static inline int |
| 81 | xfs_buf_vmap_len( |
| 82 | struct xfs_buf *bp) |
| 83 | { |
| 84 | return (bp->b_page_count * PAGE_SIZE) - bp->b_offset; |
| 85 | } |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 88 | * xfs_buf_lru_add - add a buffer to the LRU. |
| 89 | * |
| 90 | * The LRU takes a new reference to the buffer so that it will only be freed |
| 91 | * once the shrinker takes the buffer off the LRU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | */ |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 93 | STATIC void |
| 94 | xfs_buf_lru_add( |
| 95 | struct xfs_buf *bp) |
| 96 | { |
| 97 | struct xfs_buftarg *btp = bp->b_target; |
| 98 | |
| 99 | spin_lock(&btp->bt_lru_lock); |
| 100 | if (list_empty(&bp->b_lru)) { |
| 101 | atomic_inc(&bp->b_hold); |
| 102 | list_add_tail(&bp->b_lru, &btp->bt_lru); |
| 103 | btp->bt_lru_nr++; |
| 104 | } |
| 105 | spin_unlock(&btp->bt_lru_lock); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * xfs_buf_lru_del - remove a buffer from the LRU |
| 110 | * |
| 111 | * The unlocked check is safe here because it only occurs when there are not |
| 112 | * b_lru_ref counts left on the inode under the pag->pag_buf_lock. it is there |
| 113 | * to optimise the shrinker removing the buffer from the LRU and calling |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 114 | * xfs_buf_free(). i.e. it removes an unnecessary round trip on the |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 115 | * bt_lru_lock. |
| 116 | */ |
| 117 | STATIC void |
| 118 | xfs_buf_lru_del( |
| 119 | struct xfs_buf *bp) |
| 120 | { |
| 121 | struct xfs_buftarg *btp = bp->b_target; |
| 122 | |
| 123 | if (list_empty(&bp->b_lru)) |
| 124 | return; |
| 125 | |
| 126 | spin_lock(&btp->bt_lru_lock); |
| 127 | if (!list_empty(&bp->b_lru)) { |
| 128 | list_del_init(&bp->b_lru); |
| 129 | btp->bt_lru_nr--; |
| 130 | } |
| 131 | spin_unlock(&btp->bt_lru_lock); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * When we mark a buffer stale, we remove the buffer from the LRU and clear the |
| 136 | * b_lru_ref count so that the buffer is freed immediately when the buffer |
| 137 | * reference count falls to zero. If the buffer is already on the LRU, we need |
| 138 | * to remove the reference that LRU holds on the buffer. |
| 139 | * |
| 140 | * This prevents build-up of stale buffers on the LRU. |
| 141 | */ |
| 142 | void |
| 143 | xfs_buf_stale( |
| 144 | struct xfs_buf *bp) |
| 145 | { |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 146 | ASSERT(xfs_buf_islocked(bp)); |
| 147 | |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 148 | bp->b_flags |= XBF_STALE; |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * Clear the delwri status so that a delwri queue walker will not |
| 152 | * flush this buffer to disk now that it is stale. The delwri queue has |
| 153 | * a reference to the buffer, so this is safe to do. |
| 154 | */ |
| 155 | bp->b_flags &= ~_XBF_DELWRI_Q; |
| 156 | |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 157 | atomic_set(&(bp)->b_lru_ref, 0); |
| 158 | if (!list_empty(&bp->b_lru)) { |
| 159 | struct xfs_buftarg *btp = bp->b_target; |
| 160 | |
| 161 | spin_lock(&btp->bt_lru_lock); |
| 162 | if (!list_empty(&bp->b_lru)) { |
| 163 | list_del_init(&bp->b_lru); |
| 164 | btp->bt_lru_nr--; |
| 165 | atomic_dec(&bp->b_hold); |
| 166 | } |
| 167 | spin_unlock(&btp->bt_lru_lock); |
| 168 | } |
| 169 | ASSERT(atomic_read(&bp->b_hold) >= 1); |
| 170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 172 | struct xfs_buf * |
| 173 | xfs_buf_alloc( |
| 174 | struct xfs_buftarg *target, |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 175 | xfs_off_t range_base, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | size_t range_length, |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 177 | xfs_buf_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 179 | struct xfs_buf *bp; |
| 180 | |
| 181 | bp = kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags)); |
| 182 | if (unlikely(!bp)) |
| 183 | return NULL; |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 186 | * We don't want certain flags to appear in b_flags. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 188 | flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 190 | memset(bp, 0, sizeof(xfs_buf_t)); |
| 191 | atomic_set(&bp->b_hold, 1); |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 192 | atomic_set(&bp->b_lru_ref, 1); |
David Chinner | b4dd330 | 2008-08-13 16:36:11 +1000 | [diff] [blame] | 193 | init_completion(&bp->b_iowait); |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 194 | INIT_LIST_HEAD(&bp->b_lru); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 195 | INIT_LIST_HEAD(&bp->b_list); |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 196 | RB_CLEAR_NODE(&bp->b_rbnode); |
Thomas Gleixner | a731cd1 | 2010-09-07 14:33:15 +0000 | [diff] [blame] | 197 | sema_init(&bp->b_sema, 0); /* held, no waiters */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 198 | XB_SET_OWNER(bp); |
| 199 | bp->b_target = target; |
| 200 | bp->b_file_offset = range_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | /* |
| 202 | * Set buffer_length and count_desired to the same value initially. |
| 203 | * I/O routines should use count_desired, which will be the same in |
| 204 | * most cases but may be reset (e.g. XFS recovery). |
| 205 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 206 | bp->b_buffer_length = bp->b_count_desired = range_length; |
| 207 | bp->b_flags = flags; |
| 208 | bp->b_bn = XFS_BUF_DADDR_NULL; |
| 209 | atomic_set(&bp->b_pin_count, 0); |
| 210 | init_waitqueue_head(&bp->b_waiters); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 212 | XFS_STATS_INC(xb_create); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 213 | trace_xfs_buf_init(bp, _RET_IP_); |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 214 | |
| 215 | return bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 219 | * Allocate a page array capable of holding a specified number |
| 220 | * of pages, and point the page buf at it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | */ |
| 222 | STATIC int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 223 | _xfs_buf_get_pages( |
| 224 | xfs_buf_t *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | int page_count, |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 226 | xfs_buf_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | { |
| 228 | /* Make sure that we have a page list */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 229 | if (bp->b_pages == NULL) { |
| 230 | bp->b_offset = xfs_buf_poff(bp->b_file_offset); |
| 231 | bp->b_page_count = page_count; |
| 232 | if (page_count <= XB_PAGES) { |
| 233 | bp->b_pages = bp->b_page_array; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } else { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 235 | bp->b_pages = kmem_alloc(sizeof(struct page *) * |
| 236 | page_count, xb_to_km(flags)); |
| 237 | if (bp->b_pages == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | return -ENOMEM; |
| 239 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 240 | memset(bp->b_pages, 0, sizeof(struct page *) * page_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 246 | * Frees b_pages if it was allocated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | */ |
| 248 | STATIC void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 249 | _xfs_buf_free_pages( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | xfs_buf_t *bp) |
| 251 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 252 | if (bp->b_pages != bp->b_page_array) { |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 253 | kmem_free(bp->b_pages); |
Dave Chinner | 3fc98b1 | 2009-12-14 23:11:57 +0000 | [diff] [blame] | 254 | bp->b_pages = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Releases the specified buffer. |
| 260 | * |
| 261 | * The modification state of any associated pages is left unchanged. |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 262 | * The buffer most not be on any hash - use xfs_buf_rele instead for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | * hashed and refcounted buffers |
| 264 | */ |
| 265 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 266 | xfs_buf_free( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | xfs_buf_t *bp) |
| 268 | { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 269 | trace_xfs_buf_free(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 271 | ASSERT(list_empty(&bp->b_lru)); |
| 272 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 273 | if (bp->b_flags & _XBF_PAGES) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | uint i; |
| 275 | |
James Bottomley | 73c77e2 | 2010-01-25 11:42:24 -0600 | [diff] [blame] | 276 | if (xfs_buf_is_vmapped(bp)) |
Alex Elder | 8a262e5 | 2010-03-16 18:55:56 +0000 | [diff] [blame] | 277 | vm_unmap_ram(bp->b_addr - bp->b_offset, |
| 278 | bp->b_page_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Nathan Scott | 948ecdb | 2006-09-28 11:03:13 +1000 | [diff] [blame] | 280 | for (i = 0; i < bp->b_page_count; i++) { |
| 281 | struct page *page = bp->b_pages[i]; |
| 282 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 283 | __free_page(page); |
Nathan Scott | 948ecdb | 2006-09-28 11:03:13 +1000 | [diff] [blame] | 284 | } |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 285 | } else if (bp->b_flags & _XBF_KMEM) |
| 286 | kmem_free(bp->b_addr); |
Dave Chinner | 3fc98b1 | 2009-12-14 23:11:57 +0000 | [diff] [blame] | 287 | _xfs_buf_free_pages(bp); |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 288 | kmem_zone_free(xfs_buf_zone, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 292 | * Allocates all the pages for buffer in question and builds it's page list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | */ |
| 294 | STATIC int |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 295 | xfs_buf_allocate_memory( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | xfs_buf_t *bp, |
| 297 | uint flags) |
| 298 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 299 | size_t size = bp->b_count_desired; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | size_t nbytes, offset; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 301 | gfp_t gfp_mask = xb_to_gfp(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | unsigned short page_count, i; |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 303 | xfs_off_t end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | int error; |
| 305 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 306 | /* |
| 307 | * for buffers that are contained within a single page, just allocate |
| 308 | * the memory from the heap - there's no need for the complexity of |
| 309 | * page arrays to keep allocation down to order 0. |
| 310 | */ |
| 311 | if (bp->b_buffer_length < PAGE_SIZE) { |
| 312 | bp->b_addr = kmem_alloc(bp->b_buffer_length, xb_to_km(flags)); |
| 313 | if (!bp->b_addr) { |
| 314 | /* low memory - use alloc_page loop instead */ |
| 315 | goto use_alloc_page; |
| 316 | } |
| 317 | |
| 318 | if (((unsigned long)(bp->b_addr + bp->b_buffer_length - 1) & |
| 319 | PAGE_MASK) != |
| 320 | ((unsigned long)bp->b_addr & PAGE_MASK)) { |
| 321 | /* b_addr spans two pages - use alloc_page instead */ |
| 322 | kmem_free(bp->b_addr); |
| 323 | bp->b_addr = NULL; |
| 324 | goto use_alloc_page; |
| 325 | } |
| 326 | bp->b_offset = offset_in_page(bp->b_addr); |
| 327 | bp->b_pages = bp->b_page_array; |
| 328 | bp->b_pages[0] = virt_to_page(bp->b_addr); |
| 329 | bp->b_page_count = 1; |
| 330 | bp->b_flags |= XBF_MAPPED | _XBF_KMEM; |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | use_alloc_page: |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 335 | end = bp->b_file_offset + bp->b_buffer_length; |
| 336 | page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 337 | error = _xfs_buf_get_pages(bp, page_count, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | if (unlikely(error)) |
| 339 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 341 | offset = bp->b_offset; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 342 | bp->b_flags |= _XBF_PAGES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 344 | for (i = 0; i < bp->b_page_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | struct page *page; |
| 346 | uint retries = 0; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 347 | retry: |
| 348 | page = alloc_page(gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | if (unlikely(page == NULL)) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 350 | if (flags & XBF_READ_AHEAD) { |
| 351 | bp->b_page_count = i; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 352 | error = ENOMEM; |
| 353 | goto out_free_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* |
| 357 | * This could deadlock. |
| 358 | * |
| 359 | * But until all the XFS lowlevel code is revamped to |
| 360 | * handle buffer allocation failures we can't do much. |
| 361 | */ |
| 362 | if (!(++retries % 100)) |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 363 | xfs_err(NULL, |
| 364 | "possible memory allocation deadlock in %s (mode:0x%x)", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 365 | __func__, gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 367 | XFS_STATS_INC(xb_page_retries); |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 368 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | goto retry; |
| 370 | } |
| 371 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 372 | XFS_STATS_INC(xb_page_found); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 374 | nbytes = min_t(size_t, size, PAGE_SIZE - offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | size -= nbytes; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 376 | bp->b_pages[i] = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | offset = 0; |
| 378 | } |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 379 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 381 | out_free_pages: |
| 382 | for (i = 0; i < bp->b_page_count; i++) |
| 383 | __free_page(bp->b_pages[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | return error; |
| 385 | } |
| 386 | |
| 387 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 388 | * Map buffer into kernel address-space if necessary. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | */ |
| 390 | STATIC int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 391 | _xfs_buf_map_pages( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | xfs_buf_t *bp, |
| 393 | uint flags) |
| 394 | { |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 395 | ASSERT(bp->b_flags & _XBF_PAGES); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 396 | if (bp->b_page_count == 1) { |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 397 | /* A single page buffer is always mappable */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 398 | bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset; |
| 399 | bp->b_flags |= XBF_MAPPED; |
| 400 | } else if (flags & XBF_MAPPED) { |
Dave Chinner | a19fb38 | 2011-03-26 09:13:42 +1100 | [diff] [blame] | 401 | int retried = 0; |
| 402 | |
| 403 | do { |
| 404 | bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count, |
| 405 | -1, PAGE_KERNEL); |
| 406 | if (bp->b_addr) |
| 407 | break; |
| 408 | vm_unmap_aliases(); |
| 409 | } while (retried++ <= 1); |
| 410 | |
| 411 | if (!bp->b_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | return -ENOMEM; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 413 | bp->b_addr += bp->b_offset; |
| 414 | bp->b_flags |= XBF_MAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Finding and Reading Buffers |
| 422 | */ |
| 423 | |
| 424 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 425 | * Look up, and creates if absent, a lockable buffer for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | * a given range of an inode. The buffer is returned |
Chandra Seetharaman | eabbaf1 | 2011-09-08 20:18:50 +0000 | [diff] [blame] | 427 | * locked. No I/O is implied by this call. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | */ |
| 429 | xfs_buf_t * |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 430 | _xfs_buf_find( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | xfs_buftarg_t *btp, /* block device target */ |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 432 | xfs_off_t ioff, /* starting offset of range */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | size_t isize, /* length of range */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 434 | xfs_buf_flags_t flags, |
| 435 | xfs_buf_t *new_bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 437 | xfs_off_t range_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | size_t range_length; |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 439 | struct xfs_perag *pag; |
| 440 | struct rb_node **rbp; |
| 441 | struct rb_node *parent; |
| 442 | xfs_buf_t *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
| 444 | range_base = (ioff << BBSHIFT); |
| 445 | range_length = (isize << BBSHIFT); |
| 446 | |
| 447 | /* Check for IOs smaller than the sector size / not sector aligned */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 448 | ASSERT(!(range_length < (1 << btp->bt_sshift))); |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 449 | ASSERT(!(range_base & (xfs_off_t)btp->bt_smask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 451 | /* get tree root */ |
| 452 | pag = xfs_perag_get(btp->bt_mount, |
| 453 | xfs_daddr_to_agno(btp->bt_mount, ioff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 455 | /* walk tree */ |
| 456 | spin_lock(&pag->pag_buf_lock); |
| 457 | rbp = &pag->pag_buf_tree.rb_node; |
| 458 | parent = NULL; |
| 459 | bp = NULL; |
| 460 | while (*rbp) { |
| 461 | parent = *rbp; |
| 462 | bp = rb_entry(parent, struct xfs_buf, b_rbnode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 464 | if (range_base < bp->b_file_offset) |
| 465 | rbp = &(*rbp)->rb_left; |
| 466 | else if (range_base > bp->b_file_offset) |
| 467 | rbp = &(*rbp)->rb_right; |
| 468 | else { |
| 469 | /* |
| 470 | * found a block offset match. If the range doesn't |
| 471 | * match, the only way this is allowed is if the buffer |
| 472 | * in the cache is stale and the transaction that made |
| 473 | * it stale has not yet committed. i.e. we are |
| 474 | * reallocating a busy extent. Skip this buffer and |
| 475 | * continue searching to the right for an exact match. |
| 476 | */ |
| 477 | if (bp->b_buffer_length != range_length) { |
| 478 | ASSERT(bp->b_flags & XBF_STALE); |
| 479 | rbp = &(*rbp)->rb_right; |
| 480 | continue; |
| 481 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 482 | atomic_inc(&bp->b_hold); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | goto found; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /* No match found */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 488 | if (new_bp) { |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 489 | rb_link_node(&new_bp->b_rbnode, parent, rbp); |
| 490 | rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree); |
| 491 | /* the buffer keeps the perag reference until it is freed */ |
| 492 | new_bp->b_pag = pag; |
| 493 | spin_unlock(&pag->pag_buf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } else { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 495 | XFS_STATS_INC(xb_miss_locked); |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 496 | spin_unlock(&pag->pag_buf_lock); |
| 497 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 499 | return new_bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | found: |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 502 | spin_unlock(&pag->pag_buf_lock); |
| 503 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 505 | if (!xfs_buf_trylock(bp)) { |
| 506 | if (flags & XBF_TRYLOCK) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 507 | xfs_buf_rele(bp); |
| 508 | XFS_STATS_INC(xb_busy_locked); |
| 509 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 511 | xfs_buf_lock(bp); |
| 512 | XFS_STATS_INC(xb_get_locked_waited); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 515 | /* |
| 516 | * if the buffer is stale, clear all the external state associated with |
| 517 | * it. We need to keep flags such as how we allocated the buffer memory |
| 518 | * intact here. |
| 519 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 520 | if (bp->b_flags & XBF_STALE) { |
| 521 | ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0); |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 522 | bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES; |
David Chinner | 2f92658 | 2005-09-05 08:33:35 +1000 | [diff] [blame] | 523 | } |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 524 | |
| 525 | trace_xfs_buf_find(bp, flags, _RET_IP_); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 526 | XFS_STATS_INC(xb_get_locked); |
| 527 | return bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /* |
Dave Chinner | 3815832 | 2011-09-30 04:45:02 +0000 | [diff] [blame] | 531 | * Assembles a buffer covering the specified range. The code is optimised for |
| 532 | * cache hits, as metadata intensive workloads will see 3 orders of magnitude |
| 533 | * more hits than misses. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | */ |
Dave Chinner | 3815832 | 2011-09-30 04:45:02 +0000 | [diff] [blame] | 535 | struct xfs_buf * |
Christoph Hellwig | 6ad112b | 2009-11-24 18:02:23 +0000 | [diff] [blame] | 536 | xfs_buf_get( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | xfs_buftarg_t *target,/* target for buffer */ |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 538 | xfs_off_t ioff, /* starting offset of range */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | size_t isize, /* length of range */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 540 | xfs_buf_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | { |
Dave Chinner | 3815832 | 2011-09-30 04:45:02 +0000 | [diff] [blame] | 542 | struct xfs_buf *bp; |
| 543 | struct xfs_buf *new_bp; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 544 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
Dave Chinner | 3815832 | 2011-09-30 04:45:02 +0000 | [diff] [blame] | 546 | bp = _xfs_buf_find(target, ioff, isize, flags, NULL); |
| 547 | if (likely(bp)) |
| 548 | goto found; |
| 549 | |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 550 | new_bp = xfs_buf_alloc(target, ioff << BBSHIFT, isize << BBSHIFT, |
| 551 | flags); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 552 | if (unlikely(!new_bp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | return NULL; |
| 554 | |
Dave Chinner | fe2429b | 2012-04-23 15:58:45 +1000 | [diff] [blame^] | 555 | error = xfs_buf_allocate_memory(new_bp, flags); |
| 556 | if (error) { |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 557 | kmem_zone_free(xfs_buf_zone, new_bp); |
Dave Chinner | 3815832 | 2011-09-30 04:45:02 +0000 | [diff] [blame] | 558 | return NULL; |
| 559 | } |
| 560 | |
Dave Chinner | fe2429b | 2012-04-23 15:58:45 +1000 | [diff] [blame^] | 561 | bp = _xfs_buf_find(target, ioff, isize, flags, new_bp); |
| 562 | if (!bp) { |
| 563 | xfs_buf_free(new_bp); |
| 564 | return NULL; |
| 565 | } |
| 566 | |
| 567 | if (bp != new_bp) |
| 568 | xfs_buf_free(new_bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
Dave Chinner | 3815832 | 2011-09-30 04:45:02 +0000 | [diff] [blame] | 570 | /* |
| 571 | * Now we have a workable buffer, fill in the block number so |
| 572 | * that we can do IO on it. |
| 573 | */ |
| 574 | bp->b_bn = ioff; |
| 575 | bp->b_count_desired = bp->b_buffer_length; |
| 576 | |
| 577 | found: |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 578 | if (!(bp->b_flags & XBF_MAPPED)) { |
| 579 | error = _xfs_buf_map_pages(bp, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | if (unlikely(error)) { |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 581 | xfs_warn(target->bt_mount, |
| 582 | "%s: failed to map pages\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | goto no_buffer; |
| 584 | } |
| 585 | } |
| 586 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 587 | XFS_STATS_INC(xb_get); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 588 | trace_xfs_buf_get(bp, flags, _RET_IP_); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 589 | return bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
Dave Chinner | 3815832 | 2011-09-30 04:45:02 +0000 | [diff] [blame] | 591 | no_buffer: |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 592 | if (flags & (XBF_LOCK | XBF_TRYLOCK)) |
| 593 | xfs_buf_unlock(bp); |
| 594 | xfs_buf_rele(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | return NULL; |
| 596 | } |
| 597 | |
Christoph Hellwig | 5d765b9 | 2008-12-03 12:20:26 +0100 | [diff] [blame] | 598 | STATIC int |
| 599 | _xfs_buf_read( |
| 600 | xfs_buf_t *bp, |
| 601 | xfs_buf_flags_t flags) |
| 602 | { |
| 603 | int status; |
| 604 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 605 | ASSERT(!(flags & XBF_WRITE)); |
Christoph Hellwig | 5d765b9 | 2008-12-03 12:20:26 +0100 | [diff] [blame] | 606 | ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL); |
| 607 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 608 | bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD); |
Christoph Hellwig | 1d5ae5d | 2011-07-08 14:36:32 +0200 | [diff] [blame] | 609 | bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD); |
Christoph Hellwig | 5d765b9 | 2008-12-03 12:20:26 +0100 | [diff] [blame] | 610 | |
| 611 | status = xfs_buf_iorequest(bp); |
Chandra Seetharaman | 5a52c2a58 | 2011-07-22 23:39:51 +0000 | [diff] [blame] | 612 | if (status || bp->b_error || (flags & XBF_ASYNC)) |
Dave Chinner | ec53d1d | 2010-07-20 17:52:59 +1000 | [diff] [blame] | 613 | return status; |
| 614 | return xfs_buf_iowait(bp); |
Christoph Hellwig | 5d765b9 | 2008-12-03 12:20:26 +0100 | [diff] [blame] | 615 | } |
| 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | xfs_buf_t * |
Christoph Hellwig | 6ad112b | 2009-11-24 18:02:23 +0000 | [diff] [blame] | 618 | xfs_buf_read( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | xfs_buftarg_t *target, |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 620 | xfs_off_t ioff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | size_t isize, |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 622 | xfs_buf_flags_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 624 | xfs_buf_t *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 626 | flags |= XBF_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
Christoph Hellwig | 6ad112b | 2009-11-24 18:02:23 +0000 | [diff] [blame] | 628 | bp = xfs_buf_get(target, ioff, isize, flags); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 629 | if (bp) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 630 | trace_xfs_buf_read(bp, flags, _RET_IP_); |
| 631 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 632 | if (!XFS_BUF_ISDONE(bp)) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 633 | XFS_STATS_INC(xb_get_read); |
Christoph Hellwig | 5d765b9 | 2008-12-03 12:20:26 +0100 | [diff] [blame] | 634 | _xfs_buf_read(bp, flags); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 635 | } else if (flags & XBF_ASYNC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | /* |
| 637 | * Read ahead call which is already satisfied, |
| 638 | * drop the buffer |
| 639 | */ |
| 640 | goto no_buffer; |
| 641 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | /* We do not want read in the flags */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 643 | bp->b_flags &= ~XBF_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 647 | return bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
| 649 | no_buffer: |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 650 | if (flags & (XBF_LOCK | XBF_TRYLOCK)) |
| 651 | xfs_buf_unlock(bp); |
| 652 | xfs_buf_rele(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | return NULL; |
| 654 | } |
| 655 | |
| 656 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 657 | * If we are not low on memory then do the readahead in a deadlock |
| 658 | * safe manner. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | */ |
| 660 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 661 | xfs_buf_readahead( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | xfs_buftarg_t *target, |
Nathan Scott | 204ab25 | 2006-01-11 20:50:22 +1100 | [diff] [blame] | 663 | xfs_off_t ioff, |
Christoph Hellwig | 1a1a3e9 | 2010-10-06 18:41:18 +0000 | [diff] [blame] | 664 | size_t isize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | { |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 666 | if (bdi_read_congested(target->bt_bdi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | return; |
| 668 | |
Christoph Hellwig | 1a1a3e9 | 2010-10-06 18:41:18 +0000 | [diff] [blame] | 669 | xfs_buf_read(target, ioff, isize, |
| 670 | XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Dave Chinner | 5adc94c | 2010-09-24 21:58:31 +1000 | [diff] [blame] | 673 | /* |
| 674 | * Read an uncached buffer from disk. Allocates and returns a locked |
| 675 | * buffer containing the disk contents or nothing. |
| 676 | */ |
| 677 | struct xfs_buf * |
| 678 | xfs_buf_read_uncached( |
| 679 | struct xfs_mount *mp, |
| 680 | struct xfs_buftarg *target, |
| 681 | xfs_daddr_t daddr, |
| 682 | size_t length, |
| 683 | int flags) |
| 684 | { |
| 685 | xfs_buf_t *bp; |
| 686 | int error; |
| 687 | |
| 688 | bp = xfs_buf_get_uncached(target, length, flags); |
| 689 | if (!bp) |
| 690 | return NULL; |
| 691 | |
| 692 | /* set up the buffer for a read IO */ |
Dave Chinner | 5adc94c | 2010-09-24 21:58:31 +1000 | [diff] [blame] | 693 | XFS_BUF_SET_ADDR(bp, daddr); |
| 694 | XFS_BUF_READ(bp); |
Dave Chinner | 5adc94c | 2010-09-24 21:58:31 +1000 | [diff] [blame] | 695 | |
| 696 | xfsbdstrat(mp, bp); |
Christoph Hellwig | 1a1a3e9 | 2010-10-06 18:41:18 +0000 | [diff] [blame] | 697 | error = xfs_buf_iowait(bp); |
Dave Chinner | 5adc94c | 2010-09-24 21:58:31 +1000 | [diff] [blame] | 698 | if (error || bp->b_error) { |
| 699 | xfs_buf_relse(bp); |
| 700 | return NULL; |
| 701 | } |
| 702 | return bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Dave Chinner | 4439647 | 2011-04-21 09:34:27 +0000 | [diff] [blame] | 705 | /* |
| 706 | * Return a buffer allocated as an empty buffer and associated to external |
| 707 | * memory via xfs_buf_associate_memory() back to it's empty state. |
| 708 | */ |
| 709 | void |
| 710 | xfs_buf_set_empty( |
| 711 | struct xfs_buf *bp, |
| 712 | size_t len) |
| 713 | { |
| 714 | if (bp->b_pages) |
| 715 | _xfs_buf_free_pages(bp); |
| 716 | |
| 717 | bp->b_pages = NULL; |
| 718 | bp->b_page_count = 0; |
| 719 | bp->b_addr = NULL; |
| 720 | bp->b_file_offset = 0; |
| 721 | bp->b_buffer_length = bp->b_count_desired = len; |
| 722 | bp->b_bn = XFS_BUF_DADDR_NULL; |
| 723 | bp->b_flags &= ~XBF_MAPPED; |
| 724 | } |
| 725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | static inline struct page * |
| 727 | mem_to_page( |
| 728 | void *addr) |
| 729 | { |
Christoph Lameter | 9e2779f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 730 | if ((!is_vmalloc_addr(addr))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | return virt_to_page(addr); |
| 732 | } else { |
| 733 | return vmalloc_to_page(addr); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 738 | xfs_buf_associate_memory( |
| 739 | xfs_buf_t *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | void *mem, |
| 741 | size_t len) |
| 742 | { |
| 743 | int rval; |
| 744 | int i = 0; |
Lachlan McIlroy | d1afb67 | 2007-11-27 17:01:24 +1100 | [diff] [blame] | 745 | unsigned long pageaddr; |
| 746 | unsigned long offset; |
| 747 | size_t buflen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | int page_count; |
| 749 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 750 | pageaddr = (unsigned long)mem & PAGE_MASK; |
Lachlan McIlroy | d1afb67 | 2007-11-27 17:01:24 +1100 | [diff] [blame] | 751 | offset = (unsigned long)mem - pageaddr; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 752 | buflen = PAGE_ALIGN(len + offset); |
| 753 | page_count = buflen >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
| 755 | /* Free any previous set of page pointers */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 756 | if (bp->b_pages) |
| 757 | _xfs_buf_free_pages(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 759 | bp->b_pages = NULL; |
| 760 | bp->b_addr = mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
Christoph Hellwig | 36fae17 | 2009-07-18 18:14:58 -0400 | [diff] [blame] | 762 | rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | if (rval) |
| 764 | return rval; |
| 765 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 766 | bp->b_offset = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
Lachlan McIlroy | d1afb67 | 2007-11-27 17:01:24 +1100 | [diff] [blame] | 768 | for (i = 0; i < bp->b_page_count; i++) { |
| 769 | bp->b_pages[i] = mem_to_page((void *)pageaddr); |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 770 | pageaddr += PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
Lachlan McIlroy | d1afb67 | 2007-11-27 17:01:24 +1100 | [diff] [blame] | 773 | bp->b_count_desired = len; |
| 774 | bp->b_buffer_length = buflen; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 775 | bp->b_flags |= XBF_MAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | xfs_buf_t * |
Dave Chinner | 686865f | 2010-09-24 20:07:47 +1000 | [diff] [blame] | 781 | xfs_buf_get_uncached( |
| 782 | struct xfs_buftarg *target, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | size_t len, |
Dave Chinner | 686865f | 2010-09-24 20:07:47 +1000 | [diff] [blame] | 784 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | { |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 786 | unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT; |
| 787 | int error, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | xfs_buf_t *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 790 | bp = xfs_buf_alloc(target, 0, len, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | if (unlikely(bp == NULL)) |
| 792 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 794 | error = _xfs_buf_get_pages(bp, page_count, 0); |
| 795 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | goto fail_free_buf; |
| 797 | |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 798 | for (i = 0; i < page_count; i++) { |
Dave Chinner | 686865f | 2010-09-24 20:07:47 +1000 | [diff] [blame] | 799 | bp->b_pages[i] = alloc_page(xb_to_gfp(flags)); |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 800 | if (!bp->b_pages[i]) |
| 801 | goto fail_free_mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 803 | bp->b_flags |= _XBF_PAGES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 805 | error = _xfs_buf_map_pages(bp, XBF_MAPPED); |
| 806 | if (unlikely(error)) { |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 807 | xfs_warn(target->bt_mount, |
| 808 | "%s: failed to map pages\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | goto fail_free_mem; |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 810 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | |
Dave Chinner | 686865f | 2010-09-24 20:07:47 +1000 | [diff] [blame] | 812 | trace_xfs_buf_get_uncached(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | return bp; |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | fail_free_mem: |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 816 | while (--i >= 0) |
| 817 | __free_page(bp->b_pages[i]); |
Christoph Hellwig | ca165b8 | 2007-05-24 15:21:11 +1000 | [diff] [blame] | 818 | _xfs_buf_free_pages(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | fail_free_buf: |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 820 | kmem_zone_free(xfs_buf_zone, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | fail: |
| 822 | return NULL; |
| 823 | } |
| 824 | |
| 825 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | * Increment reference count on buffer, to hold the buffer concurrently |
| 827 | * with another thread which may release (free) the buffer asynchronously. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | * Must hold the buffer already to call this function. |
| 829 | */ |
| 830 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 831 | xfs_buf_hold( |
| 832 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 834 | trace_xfs_buf_hold(bp, _RET_IP_); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 835 | atomic_inc(&bp->b_hold); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 839 | * Releases a hold on the specified buffer. If the |
| 840 | * the hold count is 1, calls xfs_buf_free. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | */ |
| 842 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 843 | xfs_buf_rele( |
| 844 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | { |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 846 | struct xfs_perag *pag = bp->b_pag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 848 | trace_xfs_buf_rele(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 850 | if (!pag) { |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 851 | ASSERT(list_empty(&bp->b_lru)); |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 852 | ASSERT(RB_EMPTY_NODE(&bp->b_rbnode)); |
Nathan Scott | fad3aa1 | 2006-02-01 12:14:52 +1100 | [diff] [blame] | 853 | if (atomic_dec_and_test(&bp->b_hold)) |
| 854 | xfs_buf_free(bp); |
| 855 | return; |
| 856 | } |
| 857 | |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 858 | ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode)); |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 859 | |
Lachlan McIlroy | 3790689 | 2008-08-13 15:42:10 +1000 | [diff] [blame] | 860 | ASSERT(atomic_read(&bp->b_hold) > 0); |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 861 | if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) { |
Christoph Hellwig | bfc6017 | 2011-01-07 13:02:23 +0000 | [diff] [blame] | 862 | if (!(bp->b_flags & XBF_STALE) && |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 863 | atomic_read(&bp->b_lru_ref)) { |
| 864 | xfs_buf_lru_add(bp); |
| 865 | spin_unlock(&pag->pag_buf_lock); |
Christoph Hellwig | 7f14d0a | 2005-11-02 15:09:35 +1100 | [diff] [blame] | 866 | } else { |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 867 | xfs_buf_lru_del(bp); |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 868 | ASSERT(!(bp->b_flags & _XBF_DELWRI_Q)); |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 869 | rb_erase(&bp->b_rbnode, &pag->pag_buf_tree); |
| 870 | spin_unlock(&pag->pag_buf_lock); |
| 871 | xfs_perag_put(pag); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 872 | xfs_buf_free(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | |
| 878 | /* |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 879 | * Lock a buffer object, if it is not already locked. |
Dave Chinner | 90810b9 | 2010-11-30 15:16:16 +1100 | [diff] [blame] | 880 | * |
| 881 | * If we come across a stale, pinned, locked buffer, we know that we are |
| 882 | * being asked to lock a buffer that has been reallocated. Because it is |
| 883 | * pinned, we know that the log has not been pushed to disk and hence it |
| 884 | * will still be locked. Rather than continuing to have trylock attempts |
| 885 | * fail until someone else pushes the log, push it ourselves before |
| 886 | * returning. This means that the xfsaild will not get stuck trying |
| 887 | * to push on stale inode buffers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | */ |
| 889 | int |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 890 | xfs_buf_trylock( |
| 891 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | { |
| 893 | int locked; |
| 894 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 895 | locked = down_trylock(&bp->b_sema) == 0; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 896 | if (locked) |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 897 | XB_SET_OWNER(bp); |
Dave Chinner | 90810b9 | 2010-11-30 15:16:16 +1100 | [diff] [blame] | 898 | else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE)) |
| 899 | xfs_log_force(bp->b_target->bt_mount, 0); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 900 | |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 901 | trace_xfs_buf_trylock(bp, _RET_IP_); |
| 902 | return locked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | |
| 905 | /* |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 906 | * Lock a buffer object. |
Dave Chinner | ed3b4d6 | 2010-05-21 12:07:08 +1000 | [diff] [blame] | 907 | * |
| 908 | * If we come across a stale, pinned, locked buffer, we know that we |
| 909 | * are being asked to lock a buffer that has been reallocated. Because |
| 910 | * it is pinned, we know that the log has not been pushed to disk and |
| 911 | * hence it will still be locked. Rather than sleeping until someone |
| 912 | * else pushes the log, push it ourselves before trying to get the lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 914 | void |
| 915 | xfs_buf_lock( |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 916 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 918 | trace_xfs_buf_lock(bp, _RET_IP_); |
| 919 | |
Dave Chinner | ed3b4d6 | 2010-05-21 12:07:08 +1000 | [diff] [blame] | 920 | if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE)) |
Dave Chinner | ebad861 | 2010-09-22 10:47:20 +1000 | [diff] [blame] | 921 | xfs_log_force(bp->b_target->bt_mount, 0); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 922 | down(&bp->b_sema); |
| 923 | XB_SET_OWNER(bp); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 924 | |
| 925 | trace_xfs_buf_lock_done(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 929 | xfs_buf_unlock( |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 930 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 932 | XB_CLEAR_OWNER(bp); |
| 933 | up(&bp->b_sema); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 934 | |
| 935 | trace_xfs_buf_unlock(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 938 | STATIC void |
| 939 | xfs_buf_wait_unpin( |
| 940 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | { |
| 942 | DECLARE_WAITQUEUE (wait, current); |
| 943 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 944 | if (atomic_read(&bp->b_pin_count) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | return; |
| 946 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 947 | add_wait_queue(&bp->b_waiters, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | for (;;) { |
| 949 | set_current_state(TASK_UNINTERRUPTIBLE); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 950 | if (atomic_read(&bp->b_pin_count) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | break; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 952 | io_schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | } |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 954 | remove_wait_queue(&bp->b_waiters, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | set_current_state(TASK_RUNNING); |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * Buffer Utility Routines |
| 960 | */ |
| 961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | STATIC void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 963 | xfs_buf_iodone_work( |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 964 | struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 966 | xfs_buf_t *bp = |
| 967 | container_of(work, xfs_buf_t, b_iodone_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
Christoph Hellwig | 80f6c29 | 2010-08-18 05:29:11 -0400 | [diff] [blame] | 969 | if (bp->b_iodone) |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 970 | (*(bp->b_iodone))(bp); |
| 971 | else if (bp->b_flags & XBF_ASYNC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | xfs_buf_relse(bp); |
| 973 | } |
| 974 | |
| 975 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 976 | xfs_buf_ioend( |
| 977 | xfs_buf_t *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | int schedule) |
| 979 | { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 980 | trace_xfs_buf_iodone(bp, _RET_IP_); |
| 981 | |
Lachlan McIlroy | 77be55a | 2007-11-23 16:31:00 +1100 | [diff] [blame] | 982 | bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 983 | if (bp->b_error == 0) |
| 984 | bp->b_flags |= XBF_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 986 | if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | if (schedule) { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 988 | INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 989 | queue_work(xfslogd_workqueue, &bp->b_iodone_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | } else { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 991 | xfs_buf_iodone_work(&bp->b_iodone_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | } |
| 993 | } else { |
David Chinner | b4dd330 | 2008-08-13 16:36:11 +1000 | [diff] [blame] | 994 | complete(&bp->b_iowait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 999 | xfs_buf_ioerror( |
| 1000 | xfs_buf_t *bp, |
| 1001 | int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | { |
| 1003 | ASSERT(error >= 0 && error <= 0xffff); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1004 | bp->b_error = (unsigned short)error; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1005 | trace_xfs_buf_ioerror(bp, error, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Christoph Hellwig | 901796a | 2011-10-10 16:52:49 +0000 | [diff] [blame] | 1008 | void |
| 1009 | xfs_buf_ioerror_alert( |
| 1010 | struct xfs_buf *bp, |
| 1011 | const char *func) |
| 1012 | { |
| 1013 | xfs_alert(bp->b_target->bt_mount, |
| 1014 | "metadata I/O error: block 0x%llx (\"%s\") error %d buf count %zd", |
| 1015 | (__uint64_t)XFS_BUF_ADDR(bp), func, |
| 1016 | bp->b_error, XFS_BUF_COUNT(bp)); |
| 1017 | } |
| 1018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | int |
Christoph Hellwig | 64e0bc7 | 2010-01-13 22:17:58 +0000 | [diff] [blame] | 1020 | xfs_bwrite( |
Christoph Hellwig | 5d765b9 | 2008-12-03 12:20:26 +0100 | [diff] [blame] | 1021 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | { |
Christoph Hellwig | 8c38366 | 2010-03-12 10:59:40 +0000 | [diff] [blame] | 1023 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1025 | ASSERT(xfs_buf_islocked(bp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1027 | bp->b_flags |= XBF_WRITE; |
| 1028 | bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q); |
| 1029 | |
Christoph Hellwig | 939d723 | 2010-07-20 17:51:16 +1000 | [diff] [blame] | 1030 | xfs_bdstrat_cb(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
Christoph Hellwig | 8c38366 | 2010-03-12 10:59:40 +0000 | [diff] [blame] | 1032 | error = xfs_buf_iowait(bp); |
Christoph Hellwig | c2b006c | 2011-08-23 08:28:07 +0000 | [diff] [blame] | 1033 | if (error) { |
| 1034 | xfs_force_shutdown(bp->b_target->bt_mount, |
| 1035 | SHUTDOWN_META_IO_ERROR); |
| 1036 | } |
Christoph Hellwig | 64e0bc7 | 2010-01-13 22:17:58 +0000 | [diff] [blame] | 1037 | return error; |
Christoph Hellwig | 5d765b9 | 2008-12-03 12:20:26 +0100 | [diff] [blame] | 1038 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
Christoph Hellwig | 4e23471 | 2010-01-13 22:17:56 +0000 | [diff] [blame] | 1040 | /* |
| 1041 | * Called when we want to stop a buffer from getting written or read. |
Christoph Hellwig | 1a1a3e9 | 2010-10-06 18:41:18 +0000 | [diff] [blame] | 1042 | * We attach the EIO error, muck with its flags, and call xfs_buf_ioend |
Christoph Hellwig | 4e23471 | 2010-01-13 22:17:56 +0000 | [diff] [blame] | 1043 | * so that the proper iodone callbacks get called. |
| 1044 | */ |
| 1045 | STATIC int |
| 1046 | xfs_bioerror( |
| 1047 | xfs_buf_t *bp) |
| 1048 | { |
| 1049 | #ifdef XFSERRORDEBUG |
| 1050 | ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone); |
| 1051 | #endif |
| 1052 | |
| 1053 | /* |
| 1054 | * No need to wait until the buffer is unpinned, we aren't flushing it. |
| 1055 | */ |
Chandra Seetharaman | 5a52c2a58 | 2011-07-22 23:39:51 +0000 | [diff] [blame] | 1056 | xfs_buf_ioerror(bp, EIO); |
Christoph Hellwig | 4e23471 | 2010-01-13 22:17:56 +0000 | [diff] [blame] | 1057 | |
| 1058 | /* |
Christoph Hellwig | 1a1a3e9 | 2010-10-06 18:41:18 +0000 | [diff] [blame] | 1059 | * We're calling xfs_buf_ioend, so delete XBF_DONE flag. |
Christoph Hellwig | 4e23471 | 2010-01-13 22:17:56 +0000 | [diff] [blame] | 1060 | */ |
| 1061 | XFS_BUF_UNREAD(bp); |
Christoph Hellwig | 4e23471 | 2010-01-13 22:17:56 +0000 | [diff] [blame] | 1062 | XFS_BUF_UNDONE(bp); |
Christoph Hellwig | c867cb6 | 2011-10-10 16:52:46 +0000 | [diff] [blame] | 1063 | xfs_buf_stale(bp); |
Christoph Hellwig | 4e23471 | 2010-01-13 22:17:56 +0000 | [diff] [blame] | 1064 | |
Christoph Hellwig | 1a1a3e9 | 2010-10-06 18:41:18 +0000 | [diff] [blame] | 1065 | xfs_buf_ioend(bp, 0); |
Christoph Hellwig | 4e23471 | 2010-01-13 22:17:56 +0000 | [diff] [blame] | 1066 | |
| 1067 | return EIO; |
| 1068 | } |
| 1069 | |
| 1070 | /* |
| 1071 | * Same as xfs_bioerror, except that we are releasing the buffer |
Christoph Hellwig | 1a1a3e9 | 2010-10-06 18:41:18 +0000 | [diff] [blame] | 1072 | * here ourselves, and avoiding the xfs_buf_ioend call. |
Christoph Hellwig | 4e23471 | 2010-01-13 22:17:56 +0000 | [diff] [blame] | 1073 | * This is meant for userdata errors; metadata bufs come with |
| 1074 | * iodone functions attached, so that we can track down errors. |
| 1075 | */ |
| 1076 | STATIC int |
| 1077 | xfs_bioerror_relse( |
| 1078 | struct xfs_buf *bp) |
| 1079 | { |
Chandra Seetharaman | ed43233 | 2011-07-22 23:39:39 +0000 | [diff] [blame] | 1080 | int64_t fl = bp->b_flags; |
Christoph Hellwig | 4e23471 | 2010-01-13 22:17:56 +0000 | [diff] [blame] | 1081 | /* |
| 1082 | * No need to wait until the buffer is unpinned. |
| 1083 | * We aren't flushing it. |
| 1084 | * |
| 1085 | * chunkhold expects B_DONE to be set, whether |
| 1086 | * we actually finish the I/O or not. We don't want to |
| 1087 | * change that interface. |
| 1088 | */ |
| 1089 | XFS_BUF_UNREAD(bp); |
Christoph Hellwig | 4e23471 | 2010-01-13 22:17:56 +0000 | [diff] [blame] | 1090 | XFS_BUF_DONE(bp); |
Christoph Hellwig | c867cb6 | 2011-10-10 16:52:46 +0000 | [diff] [blame] | 1091 | xfs_buf_stale(bp); |
Christoph Hellwig | cb669ca | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 1092 | bp->b_iodone = NULL; |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 1093 | if (!(fl & XBF_ASYNC)) { |
Christoph Hellwig | 4e23471 | 2010-01-13 22:17:56 +0000 | [diff] [blame] | 1094 | /* |
| 1095 | * Mark b_error and B_ERROR _both_. |
| 1096 | * Lot's of chunkcache code assumes that. |
| 1097 | * There's no reason to mark error for |
| 1098 | * ASYNC buffers. |
| 1099 | */ |
Chandra Seetharaman | 5a52c2a58 | 2011-07-22 23:39:51 +0000 | [diff] [blame] | 1100 | xfs_buf_ioerror(bp, EIO); |
Christoph Hellwig | 5fde032 | 2011-10-10 16:52:44 +0000 | [diff] [blame] | 1101 | complete(&bp->b_iowait); |
Christoph Hellwig | 4e23471 | 2010-01-13 22:17:56 +0000 | [diff] [blame] | 1102 | } else { |
| 1103 | xfs_buf_relse(bp); |
| 1104 | } |
| 1105 | |
| 1106 | return EIO; |
| 1107 | } |
| 1108 | |
| 1109 | |
| 1110 | /* |
| 1111 | * All xfs metadata buffers except log state machine buffers |
| 1112 | * get this attached as their b_bdstrat callback function. |
| 1113 | * This is so that we can catch a buffer |
| 1114 | * after prematurely unpinning it to forcibly shutdown the filesystem. |
| 1115 | */ |
| 1116 | int |
| 1117 | xfs_bdstrat_cb( |
| 1118 | struct xfs_buf *bp) |
| 1119 | { |
Dave Chinner | ebad861 | 2010-09-22 10:47:20 +1000 | [diff] [blame] | 1120 | if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) { |
Christoph Hellwig | 4e23471 | 2010-01-13 22:17:56 +0000 | [diff] [blame] | 1121 | trace_xfs_bdstrat_shut(bp, _RET_IP_); |
| 1122 | /* |
| 1123 | * Metadata write that didn't get logged but |
| 1124 | * written delayed anyway. These aren't associated |
| 1125 | * with a transaction, and can be ignored. |
| 1126 | */ |
| 1127 | if (!bp->b_iodone && !XFS_BUF_ISREAD(bp)) |
| 1128 | return xfs_bioerror_relse(bp); |
| 1129 | else |
| 1130 | return xfs_bioerror(bp); |
| 1131 | } |
| 1132 | |
| 1133 | xfs_buf_iorequest(bp); |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * Wrapper around bdstrat so that we can stop data from going to disk in case |
| 1139 | * we are shutting down the filesystem. Typically user data goes thru this |
| 1140 | * path; one of the exceptions is the superblock. |
| 1141 | */ |
| 1142 | void |
| 1143 | xfsbdstrat( |
| 1144 | struct xfs_mount *mp, |
| 1145 | struct xfs_buf *bp) |
| 1146 | { |
| 1147 | if (XFS_FORCED_SHUTDOWN(mp)) { |
| 1148 | trace_xfs_bdstrat_shut(bp, _RET_IP_); |
| 1149 | xfs_bioerror_relse(bp); |
| 1150 | return; |
| 1151 | } |
| 1152 | |
| 1153 | xfs_buf_iorequest(bp); |
| 1154 | } |
| 1155 | |
Christoph Hellwig | b8f82a4 | 2009-11-14 16:17:22 +0000 | [diff] [blame] | 1156 | STATIC void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1157 | _xfs_buf_ioend( |
| 1158 | xfs_buf_t *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | int schedule) |
| 1160 | { |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 1161 | if (atomic_dec_and_test(&bp->b_io_remaining) == 1) |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1162 | xfs_buf_ioend(bp, schedule); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Al Viro | 782e3b3 | 2007-10-12 07:17:47 +0100 | [diff] [blame] | 1165 | STATIC void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1166 | xfs_buf_bio_end_io( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | struct bio *bio, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | int error) |
| 1169 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1170 | xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | |
Lachlan McIlroy | cfbe526 | 2008-12-12 15:27:25 +1100 | [diff] [blame] | 1172 | xfs_buf_ioerror(bp, -error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | |
James Bottomley | 73c77e2 | 2010-01-25 11:42:24 -0600 | [diff] [blame] | 1174 | if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ)) |
| 1175 | invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp)); |
| 1176 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1177 | _xfs_buf_ioend(bp, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | STATIC void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1182 | _xfs_buf_ioapply( |
| 1183 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | { |
Christoph Hellwig | a9759f2 | 2007-12-07 14:07:08 +1100 | [diff] [blame] | 1185 | int rw, map_i, total_nr_pages, nr_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | struct bio *bio; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1187 | int offset = bp->b_offset; |
| 1188 | int size = bp->b_count_desired; |
| 1189 | sector_t sector = bp->b_bn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1191 | total_nr_pages = bp->b_page_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | map_i = 0; |
| 1193 | |
Christoph Hellwig | 1d5ae5d | 2011-07-08 14:36:32 +0200 | [diff] [blame] | 1194 | if (bp->b_flags & XBF_WRITE) { |
| 1195 | if (bp->b_flags & XBF_SYNCIO) |
| 1196 | rw = WRITE_SYNC; |
| 1197 | else |
| 1198 | rw = WRITE; |
| 1199 | if (bp->b_flags & XBF_FUA) |
| 1200 | rw |= REQ_FUA; |
| 1201 | if (bp->b_flags & XBF_FLUSH) |
| 1202 | rw |= REQ_FLUSH; |
| 1203 | } else if (bp->b_flags & XBF_READ_AHEAD) { |
| 1204 | rw = READA; |
Nathan Scott | 51bdd70 | 2006-09-28 11:01:57 +1000 | [diff] [blame] | 1205 | } else { |
Christoph Hellwig | 1d5ae5d | 2011-07-08 14:36:32 +0200 | [diff] [blame] | 1206 | rw = READ; |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 1207 | } |
| 1208 | |
Christoph Hellwig | 34951f5 | 2011-07-26 15:06:44 +0000 | [diff] [blame] | 1209 | /* we only use the buffer cache for meta-data */ |
| 1210 | rw |= REQ_META; |
| 1211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | next_chunk: |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1213 | atomic_inc(&bp->b_io_remaining); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | 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 Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1219 | bio->bi_bdev = bp->b_target->bt_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | bio->bi_sector = sector; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1221 | bio->bi_end_io = xfs_buf_bio_end_io; |
| 1222 | bio->bi_private = bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 1224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | for (; size && nr_pages; nr_pages--, map_i++) { |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 1226 | int rbytes, nbytes = PAGE_SIZE - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | |
| 1228 | if (nbytes > size) |
| 1229 | nbytes = size; |
| 1230 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1231 | rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset); |
| 1232 | if (rbytes < nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | break; |
| 1234 | |
| 1235 | offset = 0; |
| 1236 | sector += nbytes >> BBSHIFT; |
| 1237 | size -= nbytes; |
| 1238 | total_nr_pages--; |
| 1239 | } |
| 1240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | if (likely(bio->bi_size)) { |
James Bottomley | 73c77e2 | 2010-01-25 11:42:24 -0600 | [diff] [blame] | 1242 | if (xfs_buf_is_vmapped(bp)) { |
| 1243 | flush_kernel_vmap_range(bp->b_addr, |
| 1244 | xfs_buf_vmap_len(bp)); |
| 1245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | submit_bio(rw, bio); |
| 1247 | if (size) |
| 1248 | goto next_chunk; |
| 1249 | } else { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1250 | xfs_buf_ioerror(bp, EIO); |
Dave Chinner | ec53d1d | 2010-07-20 17:52:59 +1000 | [diff] [blame] | 1251 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | } |
| 1253 | } |
| 1254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1256 | xfs_buf_iorequest( |
| 1257 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1259 | trace_xfs_buf_iorequest(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1261 | ASSERT(!(bp->b_flags & _XBF_DELWRI_Q)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
Christoph Hellwig | 375ec69 | 2011-08-23 08:28:03 +0000 | [diff] [blame] | 1263 | if (bp->b_flags & XBF_WRITE) |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1264 | xfs_buf_wait_unpin(bp); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1265 | xfs_buf_hold(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | |
| 1267 | /* Set the count to 1 initially, this will stop an I/O |
| 1268 | * completion callout which happens before we have started |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1269 | * all the I/O from calling xfs_buf_ioend too early. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1271 | atomic_set(&bp->b_io_remaining, 1); |
| 1272 | _xfs_buf_ioapply(bp); |
| 1273 | _xfs_buf_ioend(bp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1275 | xfs_buf_rele(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | return 0; |
| 1277 | } |
| 1278 | |
| 1279 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1280 | * Waits for I/O to complete on the buffer supplied. |
| 1281 | * It returns immediately if no I/O is pending. |
| 1282 | * It returns the I/O error code, if any, or 0 if there was no error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | */ |
| 1284 | int |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1285 | xfs_buf_iowait( |
| 1286 | xfs_buf_t *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1288 | trace_xfs_buf_iowait(bp, _RET_IP_); |
| 1289 | |
David Chinner | b4dd330 | 2008-08-13 16:36:11 +1000 | [diff] [blame] | 1290 | wait_for_completion(&bp->b_iowait); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1291 | |
| 1292 | trace_xfs_buf_iowait_done(bp, _RET_IP_); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1293 | return bp->b_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1296 | xfs_caddr_t |
| 1297 | xfs_buf_offset( |
| 1298 | xfs_buf_t *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | size_t offset) |
| 1300 | { |
| 1301 | struct page *page; |
| 1302 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1303 | if (bp->b_flags & XBF_MAPPED) |
Chandra Seetharaman | 6292604 | 2011-07-22 23:40:15 +0000 | [diff] [blame] | 1304 | return bp->b_addr + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1306 | offset += bp->b_offset; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 1307 | page = bp->b_pages[offset >> PAGE_SHIFT]; |
| 1308 | return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | * Move data into or out of a buffer. |
| 1313 | */ |
| 1314 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1315 | xfs_buf_iomove( |
| 1316 | xfs_buf_t *bp, /* buffer to process */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | size_t boff, /* starting buffer offset */ |
| 1318 | size_t bsize, /* length to copy */ |
Dave Chinner | b9c4864 | 2010-01-20 10:47:39 +1100 | [diff] [blame] | 1319 | void *data, /* data address */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1320 | xfs_buf_rw_t mode) /* read/write/zero flag */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | { |
| 1322 | size_t bend, cpoff, csize; |
| 1323 | struct page *page; |
| 1324 | |
| 1325 | bend = boff + bsize; |
| 1326 | while (boff < bend) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1327 | page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)]; |
| 1328 | cpoff = xfs_buf_poff(boff + bp->b_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | csize = min_t(size_t, |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 1330 | PAGE_SIZE-cpoff, bp->b_count_desired-boff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 1332 | ASSERT(((csize + cpoff) <= PAGE_SIZE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | |
| 1334 | switch (mode) { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1335 | case XBRW_ZERO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | memset(page_address(page) + cpoff, 0, csize); |
| 1337 | break; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1338 | case XBRW_READ: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | memcpy(data, page_address(page) + cpoff, csize); |
| 1340 | break; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1341 | case XBRW_WRITE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | memcpy(page_address(page) + cpoff, data, csize); |
| 1343 | } |
| 1344 | |
| 1345 | boff += csize; |
| 1346 | data += csize; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1351 | * Handling of buffer targets (buftargs). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | */ |
| 1353 | |
| 1354 | /* |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1355 | * Wait for any bufs with callbacks that have been submitted but have not yet |
| 1356 | * returned. These buffers will have an elevated hold count, so wait on those |
| 1357 | * while freeing all the buffers only held by the LRU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | */ |
| 1359 | void |
| 1360 | xfs_wait_buftarg( |
Dave Chinner | 74f75a0 | 2010-09-24 19:59:04 +1000 | [diff] [blame] | 1361 | struct xfs_buftarg *btp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | { |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1363 | struct xfs_buf *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1365 | restart: |
| 1366 | spin_lock(&btp->bt_lru_lock); |
| 1367 | while (!list_empty(&btp->bt_lru)) { |
| 1368 | bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru); |
| 1369 | if (atomic_read(&bp->b_hold) > 1) { |
| 1370 | spin_unlock(&btp->bt_lru_lock); |
Dave Chinner | 26af655 | 2010-09-22 10:47:20 +1000 | [diff] [blame] | 1371 | delay(100); |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1372 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | } |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1374 | /* |
Paul Bolle | 90802ed | 2011-12-05 13:00:34 +0100 | [diff] [blame] | 1375 | * clear the LRU reference count so the buffer doesn't get |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1376 | * ignored in xfs_buf_rele(). |
| 1377 | */ |
| 1378 | atomic_set(&bp->b_lru_ref, 0); |
| 1379 | spin_unlock(&btp->bt_lru_lock); |
| 1380 | xfs_buf_rele(bp); |
| 1381 | spin_lock(&btp->bt_lru_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | } |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1383 | spin_unlock(&btp->bt_lru_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
Dave Chinner | ff57ab2 | 2010-11-30 17:27:57 +1100 | [diff] [blame] | 1386 | int |
| 1387 | xfs_buftarg_shrink( |
| 1388 | struct shrinker *shrink, |
Ying Han | 1495f23 | 2011-05-24 17:12:27 -0700 | [diff] [blame] | 1389 | struct shrink_control *sc) |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1390 | { |
Dave Chinner | ff57ab2 | 2010-11-30 17:27:57 +1100 | [diff] [blame] | 1391 | struct xfs_buftarg *btp = container_of(shrink, |
| 1392 | struct xfs_buftarg, bt_shrinker); |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1393 | struct xfs_buf *bp; |
Ying Han | 1495f23 | 2011-05-24 17:12:27 -0700 | [diff] [blame] | 1394 | int nr_to_scan = sc->nr_to_scan; |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1395 | LIST_HEAD(dispose); |
| 1396 | |
| 1397 | if (!nr_to_scan) |
| 1398 | return btp->bt_lru_nr; |
| 1399 | |
| 1400 | spin_lock(&btp->bt_lru_lock); |
| 1401 | while (!list_empty(&btp->bt_lru)) { |
| 1402 | if (nr_to_scan-- <= 0) |
| 1403 | break; |
| 1404 | |
| 1405 | bp = list_first_entry(&btp->bt_lru, struct xfs_buf, b_lru); |
| 1406 | |
| 1407 | /* |
| 1408 | * Decrement the b_lru_ref count unless the value is already |
| 1409 | * zero. If the value is already zero, we need to reclaim the |
| 1410 | * buffer, otherwise it gets another trip through the LRU. |
| 1411 | */ |
| 1412 | if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) { |
| 1413 | list_move_tail(&bp->b_lru, &btp->bt_lru); |
| 1414 | continue; |
| 1415 | } |
| 1416 | |
| 1417 | /* |
| 1418 | * remove the buffer from the LRU now to avoid needing another |
| 1419 | * lock round trip inside xfs_buf_rele(). |
| 1420 | */ |
| 1421 | list_move(&bp->b_lru, &dispose); |
| 1422 | btp->bt_lru_nr--; |
Dave Chinner | ff57ab2 | 2010-11-30 17:27:57 +1100 | [diff] [blame] | 1423 | } |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1424 | spin_unlock(&btp->bt_lru_lock); |
| 1425 | |
| 1426 | while (!list_empty(&dispose)) { |
| 1427 | bp = list_first_entry(&dispose, struct xfs_buf, b_lru); |
| 1428 | list_del_init(&bp->b_lru); |
| 1429 | xfs_buf_rele(bp); |
| 1430 | } |
| 1431 | |
| 1432 | return btp->bt_lru_nr; |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1433 | } |
| 1434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | void |
| 1436 | xfs_free_buftarg( |
Christoph Hellwig | b796313 | 2009-03-03 14:48:37 -0500 | [diff] [blame] | 1437 | struct xfs_mount *mp, |
| 1438 | struct xfs_buftarg *btp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | { |
Dave Chinner | ff57ab2 | 2010-11-30 17:27:57 +1100 | [diff] [blame] | 1440 | unregister_shrinker(&btp->bt_shrinker); |
| 1441 | |
Christoph Hellwig | b796313 | 2009-03-03 14:48:37 -0500 | [diff] [blame] | 1442 | if (mp->m_flags & XFS_MOUNT_BARRIER) |
| 1443 | xfs_blkdev_issue_flush(btp); |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1444 | |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1445 | kmem_free(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | STATIC int |
| 1449 | xfs_setsize_buftarg_flags( |
| 1450 | xfs_buftarg_t *btp, |
| 1451 | unsigned int blocksize, |
| 1452 | unsigned int sectorsize, |
| 1453 | int verbose) |
| 1454 | { |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1455 | btp->bt_bsize = blocksize; |
| 1456 | btp->bt_sshift = ffs(sectorsize) - 1; |
| 1457 | btp->bt_smask = sectorsize - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1459 | if (set_blocksize(btp->bt_bdev, sectorsize)) { |
Christoph Hellwig | 02b102d | 2011-10-10 16:52:51 +0000 | [diff] [blame] | 1460 | char name[BDEVNAME_SIZE]; |
| 1461 | |
| 1462 | bdevname(btp->bt_bdev, name); |
| 1463 | |
Dave Chinner | 4f10700 | 2011-03-07 10:00:35 +1100 | [diff] [blame] | 1464 | xfs_warn(btp->bt_mount, |
| 1465 | "Cannot set_blocksize to %u on device %s\n", |
Christoph Hellwig | 02b102d | 2011-10-10 16:52:51 +0000 | [diff] [blame] | 1466 | sectorsize, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | return EINVAL; |
| 1468 | } |
| 1469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | return 0; |
| 1471 | } |
| 1472 | |
| 1473 | /* |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1474 | * When allocating the initial buffer target we have not yet |
| 1475 | * read in the superblock, so don't know what sized sectors |
| 1476 | * are being used is at this early stage. Play safe. |
| 1477 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | STATIC int |
| 1479 | xfs_setsize_buftarg_early( |
| 1480 | xfs_buftarg_t *btp, |
| 1481 | struct block_device *bdev) |
| 1482 | { |
| 1483 | return xfs_setsize_buftarg_flags(btp, |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 1484 | PAGE_SIZE, bdev_logical_block_size(bdev), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | int |
| 1488 | xfs_setsize_buftarg( |
| 1489 | xfs_buftarg_t *btp, |
| 1490 | unsigned int blocksize, |
| 1491 | unsigned int sectorsize) |
| 1492 | { |
| 1493 | return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1); |
| 1494 | } |
| 1495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | xfs_buftarg_t * |
| 1497 | xfs_alloc_buftarg( |
Dave Chinner | ebad861 | 2010-09-22 10:47:20 +1000 | [diff] [blame] | 1498 | struct xfs_mount *mp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | struct block_device *bdev, |
Jan Engelhardt | e2a0781 | 2010-03-23 09:52:55 +1100 | [diff] [blame] | 1500 | int external, |
| 1501 | const char *fsname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | { |
| 1503 | xfs_buftarg_t *btp; |
| 1504 | |
| 1505 | btp = kmem_zalloc(sizeof(*btp), KM_SLEEP); |
| 1506 | |
Dave Chinner | ebad861 | 2010-09-22 10:47:20 +1000 | [diff] [blame] | 1507 | btp->bt_mount = mp; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1508 | btp->bt_dev = bdev->bd_dev; |
| 1509 | btp->bt_bdev = bdev; |
Dave Chinner | 0e6e847 | 2011-03-26 09:16:45 +1100 | [diff] [blame] | 1510 | btp->bt_bdi = blk_get_backing_dev_info(bdev); |
| 1511 | if (!btp->bt_bdi) |
| 1512 | goto error; |
| 1513 | |
Dave Chinner | 430cbeb | 2010-12-02 16:30:55 +1100 | [diff] [blame] | 1514 | INIT_LIST_HEAD(&btp->bt_lru); |
| 1515 | spin_lock_init(&btp->bt_lru_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | if (xfs_setsize_buftarg_early(btp, bdev)) |
| 1517 | goto error; |
Dave Chinner | ff57ab2 | 2010-11-30 17:27:57 +1100 | [diff] [blame] | 1518 | btp->bt_shrinker.shrink = xfs_buftarg_shrink; |
| 1519 | btp->bt_shrinker.seeks = DEFAULT_SEEKS; |
| 1520 | register_shrinker(&btp->bt_shrinker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | return btp; |
| 1522 | |
| 1523 | error: |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1524 | kmem_free(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | return NULL; |
| 1526 | } |
| 1527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | /* |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1529 | * Add a buffer to the delayed write list. |
| 1530 | * |
| 1531 | * This queues a buffer for writeout if it hasn't already been. Note that |
| 1532 | * neither this routine nor the buffer list submission functions perform |
| 1533 | * any internal synchronization. It is expected that the lists are thread-local |
| 1534 | * to the callers. |
| 1535 | * |
| 1536 | * Returns true if we queued up the buffer, or false if it already had |
| 1537 | * been on the buffer list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | */ |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1539 | bool |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1540 | xfs_buf_delwri_queue( |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1541 | struct xfs_buf *bp, |
| 1542 | struct list_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | { |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1544 | ASSERT(xfs_buf_islocked(bp)); |
| 1545 | ASSERT(!(bp->b_flags & XBF_READ)); |
| 1546 | |
| 1547 | /* |
| 1548 | * If the buffer is already marked delwri it already is queued up |
| 1549 | * by someone else for imediate writeout. Just ignore it in that |
| 1550 | * case. |
| 1551 | */ |
| 1552 | if (bp->b_flags & _XBF_DELWRI_Q) { |
| 1553 | trace_xfs_buf_delwri_queued(bp, _RET_IP_); |
| 1554 | return false; |
| 1555 | } |
David Chinner | a6867a6 | 2006-01-11 15:37:58 +1100 | [diff] [blame] | 1556 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1557 | trace_xfs_buf_delwri_queue(bp, _RET_IP_); |
| 1558 | |
Dave Chinner | d808f61 | 2010-02-02 10:13:42 +1100 | [diff] [blame] | 1559 | /* |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1560 | * If a buffer gets written out synchronously or marked stale while it |
| 1561 | * is on a delwri list we lazily remove it. To do this, the other party |
| 1562 | * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone. |
| 1563 | * It remains referenced and on the list. In a rare corner case it |
| 1564 | * might get readded to a delwri list after the synchronous writeout, in |
| 1565 | * which case we need just need to re-add the flag here. |
Dave Chinner | d808f61 | 2010-02-02 10:13:42 +1100 | [diff] [blame] | 1566 | */ |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1567 | bp->b_flags |= _XBF_DELWRI_Q; |
| 1568 | if (list_empty(&bp->b_list)) { |
| 1569 | atomic_inc(&bp->b_hold); |
| 1570 | list_add_tail(&bp->b_list, list); |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1571 | } |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1572 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1573 | return true; |
David Chinner | 585e6d8 | 2007-02-10 18:32:29 +1100 | [diff] [blame] | 1574 | } |
| 1575 | |
Dave Chinner | 089716a | 2010-01-26 15:13:25 +1100 | [diff] [blame] | 1576 | /* |
| 1577 | * Compare function is more complex than it needs to be because |
| 1578 | * the return value is only 32 bits and we are doing comparisons |
| 1579 | * on 64 bit values |
| 1580 | */ |
| 1581 | static int |
| 1582 | xfs_buf_cmp( |
| 1583 | void *priv, |
| 1584 | struct list_head *a, |
| 1585 | struct list_head *b) |
| 1586 | { |
| 1587 | struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list); |
| 1588 | struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list); |
| 1589 | xfs_daddr_t diff; |
| 1590 | |
| 1591 | diff = ap->b_bn - bp->b_bn; |
| 1592 | if (diff < 0) |
| 1593 | return -1; |
| 1594 | if (diff > 0) |
| 1595 | return 1; |
| 1596 | return 0; |
| 1597 | } |
| 1598 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1599 | static int |
| 1600 | __xfs_buf_delwri_submit( |
| 1601 | struct list_head *buffer_list, |
| 1602 | struct list_head *io_list, |
| 1603 | bool wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | { |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1605 | struct blk_plug plug; |
| 1606 | struct xfs_buf *bp, *n; |
| 1607 | int pinned = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1609 | list_for_each_entry_safe(bp, n, buffer_list, b_list) { |
| 1610 | if (!wait) { |
| 1611 | if (xfs_buf_ispinned(bp)) { |
| 1612 | pinned++; |
| 1613 | continue; |
| 1614 | } |
| 1615 | if (!xfs_buf_trylock(bp)) |
| 1616 | continue; |
| 1617 | } else { |
| 1618 | xfs_buf_lock(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1621 | /* |
| 1622 | * Someone else might have written the buffer synchronously or |
| 1623 | * marked it stale in the meantime. In that case only the |
| 1624 | * _XBF_DELWRI_Q flag got cleared, and we have to drop the |
| 1625 | * reference and remove it from the list here. |
| 1626 | */ |
| 1627 | if (!(bp->b_flags & _XBF_DELWRI_Q)) { |
| 1628 | list_del_init(&bp->b_list); |
| 1629 | xfs_buf_relse(bp); |
| 1630 | continue; |
| 1631 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1633 | list_move_tail(&bp->b_list, io_list); |
| 1634 | trace_xfs_buf_delwri_split(bp, _RET_IP_); |
| 1635 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1637 | list_sort(NULL, io_list, xfs_buf_cmp); |
Christoph Hellwig | a1b7ea5 | 2011-03-30 11:05:09 +0000 | [diff] [blame] | 1638 | |
| 1639 | blk_start_plug(&plug); |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1640 | list_for_each_entry_safe(bp, n, io_list, b_list) { |
| 1641 | bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC); |
| 1642 | bp->b_flags |= XBF_WRITE; |
| 1643 | |
| 1644 | if (!wait) { |
| 1645 | bp->b_flags |= XBF_ASYNC; |
| 1646 | list_del_init(&bp->b_list); |
Dave Chinner | 089716a | 2010-01-26 15:13:25 +1100 | [diff] [blame] | 1647 | } |
Christoph Hellwig | 939d723 | 2010-07-20 17:51:16 +1000 | [diff] [blame] | 1648 | xfs_bdstrat_cb(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | } |
Christoph Hellwig | a1b7ea5 | 2011-03-30 11:05:09 +0000 | [diff] [blame] | 1650 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1652 | return pinned; |
| 1653 | } |
Nathan Scott | f07c225 | 2006-09-28 10:52:15 +1000 | [diff] [blame] | 1654 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1655 | /* |
| 1656 | * Write out a buffer list asynchronously. |
| 1657 | * |
| 1658 | * This will take the @buffer_list, write all non-locked and non-pinned buffers |
| 1659 | * out and not wait for I/O completion on any of the buffers. This interface |
| 1660 | * is only safely useable for callers that can track I/O completion by higher |
| 1661 | * level means, e.g. AIL pushing as the @buffer_list is consumed in this |
| 1662 | * function. |
| 1663 | */ |
| 1664 | int |
| 1665 | xfs_buf_delwri_submit_nowait( |
| 1666 | struct list_head *buffer_list) |
| 1667 | { |
| 1668 | LIST_HEAD (io_list); |
| 1669 | return __xfs_buf_delwri_submit(buffer_list, &io_list, false); |
| 1670 | } |
| 1671 | |
| 1672 | /* |
| 1673 | * Write out a buffer list synchronously. |
| 1674 | * |
| 1675 | * This will take the @buffer_list, write all buffers out and wait for I/O |
| 1676 | * completion on all of the buffers. @buffer_list is consumed by the function, |
| 1677 | * so callers must have some other way of tracking buffers if they require such |
| 1678 | * functionality. |
| 1679 | */ |
| 1680 | int |
| 1681 | xfs_buf_delwri_submit( |
| 1682 | struct list_head *buffer_list) |
| 1683 | { |
| 1684 | LIST_HEAD (io_list); |
| 1685 | int error = 0, error2; |
| 1686 | struct xfs_buf *bp; |
| 1687 | |
| 1688 | __xfs_buf_delwri_submit(buffer_list, &io_list, true); |
| 1689 | |
| 1690 | /* Wait for IO to complete. */ |
| 1691 | while (!list_empty(&io_list)) { |
| 1692 | bp = list_first_entry(&io_list, struct xfs_buf, b_list); |
| 1693 | |
| 1694 | list_del_init(&bp->b_list); |
| 1695 | error2 = xfs_buf_iowait(bp); |
| 1696 | xfs_buf_relse(bp); |
| 1697 | if (!error) |
| 1698 | error = error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | } |
| 1700 | |
Christoph Hellwig | 43ff212 | 2012-04-23 15:58:39 +1000 | [diff] [blame] | 1701 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | } |
| 1703 | |
Christoph Hellwig | 04d8b28 | 2005-11-02 10:15:05 +1100 | [diff] [blame] | 1704 | int __init |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1705 | xfs_buf_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | { |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 1707 | xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf", |
| 1708 | KM_ZONE_HWALIGN, NULL); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1709 | if (!xfs_buf_zone) |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1710 | goto out; |
Christoph Hellwig | 04d8b28 | 2005-11-02 10:15:05 +1100 | [diff] [blame] | 1711 | |
Dave Chinner | 51749e4 | 2010-09-08 09:00:22 +0000 | [diff] [blame] | 1712 | xfslogd_workqueue = alloc_workqueue("xfslogd", |
Tejun Heo | 6370a6a | 2010-10-11 15:12:27 +0200 | [diff] [blame] | 1713 | WQ_MEM_RECLAIM | WQ_HIGHPRI, 1); |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 1714 | if (!xfslogd_workqueue) |
Christoph Hellwig | 04d8b28 | 2005-11-02 10:15:05 +1100 | [diff] [blame] | 1715 | goto out_free_buf_zone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 1717 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | |
Christoph Hellwig | 23ea403 | 2005-06-21 15:14:01 +1000 | [diff] [blame] | 1719 | out_free_buf_zone: |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1720 | kmem_zone_destroy(xfs_buf_zone); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1721 | out: |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 1722 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | void |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1726 | xfs_buf_terminate(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | { |
Christoph Hellwig | 04d8b28 | 2005-11-02 10:15:05 +1100 | [diff] [blame] | 1728 | destroy_workqueue(xfslogd_workqueue); |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1729 | kmem_zone_destroy(xfs_buf_zone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | } |