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