Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
f07c2250 | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
7b718769 | 3 | * All Rights Reserved. |
1da177e4 | 4 | * |
7b718769 NS |
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 | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
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. | |
1da177e4 | 13 | * |
7b718769 NS |
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 | |
1da177e4 | 17 | */ |
93c189c1 | 18 | #include "xfs.h" |
1da177e4 LT |
19 | #include <linux/stddef.h> |
20 | #include <linux/errno.h> | |
5a0e3ad6 | 21 | #include <linux/gfp.h> |
1da177e4 LT |
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> | |
4df08c52 | 32 | #include <linux/kthread.h> |
b20a3503 | 33 | #include <linux/migrate.h> |
3fcfab16 | 34 | #include <linux/backing-dev.h> |
7dfb7103 | 35 | #include <linux/freezer.h> |
1da177e4 | 36 | |
b7963133 CH |
37 | #include "xfs_sb.h" |
38 | #include "xfs_inum.h" | |
ed3b4d6c | 39 | #include "xfs_log.h" |
b7963133 | 40 | #include "xfs_ag.h" |
b7963133 | 41 | #include "xfs_mount.h" |
0b1b213f | 42 | #include "xfs_trace.h" |
b7963133 | 43 | |
7989cb8e | 44 | static kmem_zone_t *xfs_buf_zone; |
23ea4032 | 45 | |
7989cb8e | 46 | static struct workqueue_struct *xfslogd_workqueue; |
1da177e4 | 47 | |
ce8e922c NS |
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) | |
1da177e4 | 52 | #else |
ce8e922c NS |
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) | |
1da177e4 LT |
56 | #endif |
57 | ||
ce8e922c NS |
58 | #define xb_to_gfp(flags) \ |
59 | ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \ | |
60 | ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN) | |
1da177e4 | 61 | |
ce8e922c NS |
62 | #define xb_to_km(flags) \ |
63 | (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP) | |
1da177e4 | 64 | |
1da177e4 | 65 | |
73c77e2c JB |
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 | ||
1da177e4 | 87 | /* |
430cbeb8 DC |
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. | |
92 | */ | |
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 | |
25985edc | 114 | * xfs_buf_free(). i.e. it removes an unnecessary round trip on the |
430cbeb8 | 115 | * bt_lru_lock. |
1da177e4 | 116 | */ |
430cbeb8 DC |
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 | { | |
43ff2122 CH |
146 | ASSERT(xfs_buf_islocked(bp)); |
147 | ||
430cbeb8 | 148 | bp->b_flags |= XBF_STALE; |
43ff2122 CH |
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 | ||
430cbeb8 DC |
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 | } | |
1da177e4 | 171 | |
4347b9d7 CH |
172 | struct xfs_buf * |
173 | xfs_buf_alloc( | |
174 | struct xfs_buftarg *target, | |
e70b73f8 DC |
175 | xfs_daddr_t blkno, |
176 | size_t numblks, | |
ce8e922c | 177 | xfs_buf_flags_t flags) |
1da177e4 | 178 | { |
4347b9d7 CH |
179 | struct xfs_buf *bp; |
180 | ||
bf813cdd | 181 | bp = kmem_zone_zalloc(xfs_buf_zone, xb_to_km(flags)); |
4347b9d7 CH |
182 | if (unlikely(!bp)) |
183 | return NULL; | |
184 | ||
1da177e4 | 185 | /* |
ce8e922c | 186 | * We don't want certain flags to appear in b_flags. |
1da177e4 | 187 | */ |
ce8e922c NS |
188 | flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD); |
189 | ||
ce8e922c | 190 | atomic_set(&bp->b_hold, 1); |
430cbeb8 | 191 | atomic_set(&bp->b_lru_ref, 1); |
b4dd330b | 192 | init_completion(&bp->b_iowait); |
430cbeb8 | 193 | INIT_LIST_HEAD(&bp->b_lru); |
ce8e922c | 194 | INIT_LIST_HEAD(&bp->b_list); |
74f75a0c | 195 | RB_CLEAR_NODE(&bp->b_rbnode); |
a731cd11 | 196 | sema_init(&bp->b_sema, 0); /* held, no waiters */ |
ce8e922c NS |
197 | XB_SET_OWNER(bp); |
198 | bp->b_target = target; | |
de1cbee4 | 199 | |
1da177e4 LT |
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 | */ | |
e70b73f8 | 205 | bp->b_buffer_length = bp->b_count_desired = numblks << BBSHIFT; |
ce8e922c | 206 | bp->b_flags = flags; |
e70b73f8 DC |
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 | */ | |
ce8e922c NS |
214 | bp->b_bn = XFS_BUF_DADDR_NULL; |
215 | atomic_set(&bp->b_pin_count, 0); | |
216 | init_waitqueue_head(&bp->b_waiters); | |
217 | ||
218 | XFS_STATS_INC(xb_create); | |
0b1b213f | 219 | trace_xfs_buf_init(bp, _RET_IP_); |
4347b9d7 CH |
220 | |
221 | return bp; | |
1da177e4 LT |
222 | } |
223 | ||
224 | /* | |
ce8e922c NS |
225 | * Allocate a page array capable of holding a specified number |
226 | * of pages, and point the page buf at it. | |
1da177e4 LT |
227 | */ |
228 | STATIC int | |
ce8e922c NS |
229 | _xfs_buf_get_pages( |
230 | xfs_buf_t *bp, | |
1da177e4 | 231 | int page_count, |
ce8e922c | 232 | xfs_buf_flags_t flags) |
1da177e4 LT |
233 | { |
234 | /* Make sure that we have a page list */ | |
ce8e922c | 235 | if (bp->b_pages == NULL) { |
ce8e922c NS |
236 | bp->b_page_count = page_count; |
237 | if (page_count <= XB_PAGES) { | |
238 | bp->b_pages = bp->b_page_array; | |
1da177e4 | 239 | } else { |
ce8e922c NS |
240 | bp->b_pages = kmem_alloc(sizeof(struct page *) * |
241 | page_count, xb_to_km(flags)); | |
242 | if (bp->b_pages == NULL) | |
1da177e4 LT |
243 | return -ENOMEM; |
244 | } | |
ce8e922c | 245 | memset(bp->b_pages, 0, sizeof(struct page *) * page_count); |
1da177e4 LT |
246 | } |
247 | return 0; | |
248 | } | |
249 | ||
250 | /* | |
ce8e922c | 251 | * Frees b_pages if it was allocated. |
1da177e4 LT |
252 | */ |
253 | STATIC void | |
ce8e922c | 254 | _xfs_buf_free_pages( |
1da177e4 LT |
255 | xfs_buf_t *bp) |
256 | { | |
ce8e922c | 257 | if (bp->b_pages != bp->b_page_array) { |
f0e2d93c | 258 | kmem_free(bp->b_pages); |
3fc98b1a | 259 | bp->b_pages = NULL; |
1da177e4 LT |
260 | } |
261 | } | |
262 | ||
263 | /* | |
264 | * Releases the specified buffer. | |
265 | * | |
266 | * The modification state of any associated pages is left unchanged. | |
ce8e922c | 267 | * The buffer most not be on any hash - use xfs_buf_rele instead for |
1da177e4 LT |
268 | * hashed and refcounted buffers |
269 | */ | |
270 | void | |
ce8e922c | 271 | xfs_buf_free( |
1da177e4 LT |
272 | xfs_buf_t *bp) |
273 | { | |
0b1b213f | 274 | trace_xfs_buf_free(bp, _RET_IP_); |
1da177e4 | 275 | |
430cbeb8 DC |
276 | ASSERT(list_empty(&bp->b_lru)); |
277 | ||
0e6e847f | 278 | if (bp->b_flags & _XBF_PAGES) { |
1da177e4 LT |
279 | uint i; |
280 | ||
73c77e2c | 281 | if (xfs_buf_is_vmapped(bp)) |
8a262e57 AE |
282 | vm_unmap_ram(bp->b_addr - bp->b_offset, |
283 | bp->b_page_count); | |
1da177e4 | 284 | |
948ecdb4 NS |
285 | for (i = 0; i < bp->b_page_count; i++) { |
286 | struct page *page = bp->b_pages[i]; | |
287 | ||
0e6e847f | 288 | __free_page(page); |
948ecdb4 | 289 | } |
0e6e847f DC |
290 | } else if (bp->b_flags & _XBF_KMEM) |
291 | kmem_free(bp->b_addr); | |
3fc98b1a | 292 | _xfs_buf_free_pages(bp); |
4347b9d7 | 293 | kmem_zone_free(xfs_buf_zone, bp); |
1da177e4 LT |
294 | } |
295 | ||
296 | /* | |
0e6e847f | 297 | * Allocates all the pages for buffer in question and builds it's page list. |
1da177e4 LT |
298 | */ |
299 | STATIC int | |
0e6e847f | 300 | xfs_buf_allocate_memory( |
1da177e4 LT |
301 | xfs_buf_t *bp, |
302 | uint flags) | |
303 | { | |
ce8e922c | 304 | size_t size = bp->b_count_desired; |
1da177e4 | 305 | size_t nbytes, offset; |
ce8e922c | 306 | gfp_t gfp_mask = xb_to_gfp(flags); |
1da177e4 | 307 | unsigned short page_count, i; |
204ab25f | 308 | xfs_off_t end; |
1da177e4 LT |
309 | int error; |
310 | ||
0e6e847f DC |
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: | |
de1cbee4 DC |
340 | end = BBTOB(bp->b_bn) + bp->b_buffer_length; |
341 | page_count = xfs_buf_btoc(end) - xfs_buf_btoct(BBTOB(bp->b_bn)); | |
ce8e922c | 342 | error = _xfs_buf_get_pages(bp, page_count, flags); |
1da177e4 LT |
343 | if (unlikely(error)) |
344 | return error; | |
1da177e4 | 345 | |
ce8e922c | 346 | offset = bp->b_offset; |
0e6e847f | 347 | bp->b_flags |= _XBF_PAGES; |
1da177e4 | 348 | |
ce8e922c | 349 | for (i = 0; i < bp->b_page_count; i++) { |
1da177e4 LT |
350 | struct page *page; |
351 | uint retries = 0; | |
0e6e847f DC |
352 | retry: |
353 | page = alloc_page(gfp_mask); | |
1da177e4 | 354 | if (unlikely(page == NULL)) { |
ce8e922c NS |
355 | if (flags & XBF_READ_AHEAD) { |
356 | bp->b_page_count = i; | |
0e6e847f DC |
357 | error = ENOMEM; |
358 | goto out_free_pages; | |
1da177e4 LT |
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)) | |
4f10700a DC |
368 | xfs_err(NULL, |
369 | "possible memory allocation deadlock in %s (mode:0x%x)", | |
34a622b2 | 370 | __func__, gfp_mask); |
1da177e4 | 371 | |
ce8e922c | 372 | XFS_STATS_INC(xb_page_retries); |
8aa7e847 | 373 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
1da177e4 LT |
374 | goto retry; |
375 | } | |
376 | ||
ce8e922c | 377 | XFS_STATS_INC(xb_page_found); |
1da177e4 | 378 | |
0e6e847f | 379 | nbytes = min_t(size_t, size, PAGE_SIZE - offset); |
1da177e4 | 380 | size -= nbytes; |
ce8e922c | 381 | bp->b_pages[i] = page; |
1da177e4 LT |
382 | offset = 0; |
383 | } | |
0e6e847f | 384 | return 0; |
1da177e4 | 385 | |
0e6e847f DC |
386 | out_free_pages: |
387 | for (i = 0; i < bp->b_page_count; i++) | |
388 | __free_page(bp->b_pages[i]); | |
1da177e4 LT |
389 | return error; |
390 | } | |
391 | ||
392 | /* | |
25985edc | 393 | * Map buffer into kernel address-space if necessary. |
1da177e4 LT |
394 | */ |
395 | STATIC int | |
ce8e922c | 396 | _xfs_buf_map_pages( |
1da177e4 LT |
397 | xfs_buf_t *bp, |
398 | uint flags) | |
399 | { | |
0e6e847f | 400 | ASSERT(bp->b_flags & _XBF_PAGES); |
ce8e922c | 401 | if (bp->b_page_count == 1) { |
0e6e847f | 402 | /* A single page buffer is always mappable */ |
ce8e922c NS |
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) { | |
a19fb380 DC |
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) | |
1da177e4 | 417 | return -ENOMEM; |
ce8e922c NS |
418 | bp->b_addr += bp->b_offset; |
419 | bp->b_flags |= XBF_MAPPED; | |
1da177e4 LT |
420 | } |
421 | ||
422 | return 0; | |
423 | } | |
424 | ||
425 | /* | |
426 | * Finding and Reading Buffers | |
427 | */ | |
428 | ||
429 | /* | |
ce8e922c | 430 | * Look up, and creates if absent, a lockable buffer for |
1da177e4 | 431 | * a given range of an inode. The buffer is returned |
eabbaf11 | 432 | * locked. No I/O is implied by this call. |
1da177e4 LT |
433 | */ |
434 | xfs_buf_t * | |
ce8e922c | 435 | _xfs_buf_find( |
e70b73f8 DC |
436 | struct xfs_buftarg *btp, |
437 | xfs_daddr_t blkno, | |
438 | size_t numblks, | |
ce8e922c NS |
439 | xfs_buf_flags_t flags, |
440 | xfs_buf_t *new_bp) | |
1da177e4 | 441 | { |
e70b73f8 | 442 | size_t numbytes; |
74f75a0c DC |
443 | struct xfs_perag *pag; |
444 | struct rb_node **rbp; | |
445 | struct rb_node *parent; | |
446 | xfs_buf_t *bp; | |
1da177e4 | 447 | |
e70b73f8 | 448 | numbytes = BBTOB(numblks); |
1da177e4 LT |
449 | |
450 | /* Check for IOs smaller than the sector size / not sector aligned */ | |
e70b73f8 | 451 | ASSERT(!(numbytes < (1 << btp->bt_sshift))); |
de1cbee4 | 452 | ASSERT(!(BBTOB(blkno) & (xfs_off_t)btp->bt_smask)); |
1da177e4 | 453 | |
74f75a0c DC |
454 | /* get tree root */ |
455 | pag = xfs_perag_get(btp->bt_mount, | |
e70b73f8 | 456 | xfs_daddr_to_agno(btp->bt_mount, blkno)); |
74f75a0c DC |
457 | |
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); | |
466 | ||
de1cbee4 | 467 | if (blkno < bp->b_bn) |
74f75a0c | 468 | rbp = &(*rbp)->rb_left; |
de1cbee4 | 469 | else if (blkno > bp->b_bn) |
74f75a0c DC |
470 | rbp = &(*rbp)->rb_right; |
471 | else { | |
472 | /* | |
de1cbee4 | 473 | * found a block number match. If the range doesn't |
74f75a0c DC |
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 | */ | |
e70b73f8 | 480 | if (bp->b_buffer_length != numbytes) { |
74f75a0c DC |
481 | ASSERT(bp->b_flags & XBF_STALE); |
482 | rbp = &(*rbp)->rb_right; | |
483 | continue; | |
484 | } | |
ce8e922c | 485 | atomic_inc(&bp->b_hold); |
1da177e4 LT |
486 | goto found; |
487 | } | |
488 | } | |
489 | ||
490 | /* No match found */ | |
ce8e922c | 491 | if (new_bp) { |
74f75a0c DC |
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); | |
1da177e4 | 497 | } else { |
ce8e922c | 498 | XFS_STATS_INC(xb_miss_locked); |
74f75a0c DC |
499 | spin_unlock(&pag->pag_buf_lock); |
500 | xfs_perag_put(pag); | |
1da177e4 | 501 | } |
ce8e922c | 502 | return new_bp; |
1da177e4 LT |
503 | |
504 | found: | |
74f75a0c DC |
505 | spin_unlock(&pag->pag_buf_lock); |
506 | xfs_perag_put(pag); | |
1da177e4 | 507 | |
0c842ad4 CH |
508 | if (!xfs_buf_trylock(bp)) { |
509 | if (flags & XBF_TRYLOCK) { | |
ce8e922c NS |
510 | xfs_buf_rele(bp); |
511 | XFS_STATS_INC(xb_busy_locked); | |
512 | return NULL; | |
1da177e4 | 513 | } |
0c842ad4 CH |
514 | xfs_buf_lock(bp); |
515 | XFS_STATS_INC(xb_get_locked_waited); | |
1da177e4 LT |
516 | } |
517 | ||
0e6e847f DC |
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 | */ | |
ce8e922c NS |
523 | if (bp->b_flags & XBF_STALE) { |
524 | ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0); | |
0e6e847f | 525 | bp->b_flags &= XBF_MAPPED | _XBF_KMEM | _XBF_PAGES; |
2f926587 | 526 | } |
0b1b213f CH |
527 | |
528 | trace_xfs_buf_find(bp, flags, _RET_IP_); | |
ce8e922c NS |
529 | XFS_STATS_INC(xb_get_locked); |
530 | return bp; | |
1da177e4 LT |
531 | } |
532 | ||
533 | /* | |
3815832a DC |
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. | |
1da177e4 | 537 | */ |
3815832a | 538 | struct xfs_buf * |
6ad112bf | 539 | xfs_buf_get( |
e70b73f8 DC |
540 | xfs_buftarg_t *target, |
541 | xfs_daddr_t blkno, | |
542 | size_t numblks, | |
ce8e922c | 543 | xfs_buf_flags_t flags) |
1da177e4 | 544 | { |
3815832a DC |
545 | struct xfs_buf *bp; |
546 | struct xfs_buf *new_bp; | |
0e6e847f | 547 | int error = 0; |
1da177e4 | 548 | |
e70b73f8 | 549 | bp = _xfs_buf_find(target, blkno, numblks, flags, NULL); |
3815832a DC |
550 | if (likely(bp)) |
551 | goto found; | |
552 | ||
e70b73f8 | 553 | new_bp = xfs_buf_alloc(target, blkno, numblks, flags); |
ce8e922c | 554 | if (unlikely(!new_bp)) |
1da177e4 LT |
555 | return NULL; |
556 | ||
fe2429b0 DC |
557 | error = xfs_buf_allocate_memory(new_bp, flags); |
558 | if (error) { | |
559 | kmem_zone_free(xfs_buf_zone, new_bp); | |
560 | return NULL; | |
561 | } | |
562 | ||
e70b73f8 | 563 | bp = _xfs_buf_find(target, blkno, numblks, flags, new_bp); |
3815832a | 564 | if (!bp) { |
fe2429b0 | 565 | xfs_buf_free(new_bp); |
3815832a DC |
566 | return NULL; |
567 | } | |
568 | ||
fe2429b0 DC |
569 | if (bp != new_bp) |
570 | xfs_buf_free(new_bp); | |
1da177e4 | 571 | |
3815832a DC |
572 | /* |
573 | * Now we have a workable buffer, fill in the block number so | |
574 | * that we can do IO on it. | |
575 | */ | |
e70b73f8 | 576 | bp->b_bn = blkno; |
3815832a DC |
577 | bp->b_count_desired = bp->b_buffer_length; |
578 | ||
579 | found: | |
ce8e922c NS |
580 | if (!(bp->b_flags & XBF_MAPPED)) { |
581 | error = _xfs_buf_map_pages(bp, flags); | |
1da177e4 | 582 | if (unlikely(error)) { |
4f10700a DC |
583 | xfs_warn(target->bt_mount, |
584 | "%s: failed to map pages\n", __func__); | |
1da177e4 LT |
585 | goto no_buffer; |
586 | } | |
587 | } | |
588 | ||
ce8e922c | 589 | XFS_STATS_INC(xb_get); |
0b1b213f | 590 | trace_xfs_buf_get(bp, flags, _RET_IP_); |
ce8e922c | 591 | return bp; |
1da177e4 | 592 | |
3815832a | 593 | no_buffer: |
ce8e922c NS |
594 | if (flags & (XBF_LOCK | XBF_TRYLOCK)) |
595 | xfs_buf_unlock(bp); | |
596 | xfs_buf_rele(bp); | |
1da177e4 LT |
597 | return NULL; |
598 | } | |
599 | ||
5d765b97 CH |
600 | STATIC int |
601 | _xfs_buf_read( | |
602 | xfs_buf_t *bp, | |
603 | xfs_buf_flags_t flags) | |
604 | { | |
43ff2122 | 605 | ASSERT(!(flags & XBF_WRITE)); |
5d765b97 CH |
606 | ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL); |
607 | ||
43ff2122 | 608 | bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD); |
1d5ae5df | 609 | bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD); |
5d765b97 | 610 | |
0e95f19a DC |
611 | xfs_buf_iorequest(bp); |
612 | if (flags & XBF_ASYNC) | |
613 | return 0; | |
ec53d1db | 614 | return xfs_buf_iowait(bp); |
5d765b97 CH |
615 | } |
616 | ||
1da177e4 | 617 | xfs_buf_t * |
6ad112bf | 618 | xfs_buf_read( |
1da177e4 | 619 | xfs_buftarg_t *target, |
e70b73f8 DC |
620 | xfs_daddr_t blkno, |
621 | size_t numblks, | |
ce8e922c | 622 | xfs_buf_flags_t flags) |
1da177e4 | 623 | { |
ce8e922c NS |
624 | xfs_buf_t *bp; |
625 | ||
626 | flags |= XBF_READ; | |
627 | ||
e70b73f8 | 628 | bp = xfs_buf_get(target, blkno, numblks, flags); |
ce8e922c | 629 | if (bp) { |
0b1b213f CH |
630 | trace_xfs_buf_read(bp, flags, _RET_IP_); |
631 | ||
ce8e922c | 632 | if (!XFS_BUF_ISDONE(bp)) { |
ce8e922c | 633 | XFS_STATS_INC(xb_get_read); |
5d765b97 | 634 | _xfs_buf_read(bp, flags); |
ce8e922c | 635 | } else if (flags & XBF_ASYNC) { |
1da177e4 LT |
636 | /* |
637 | * Read ahead call which is already satisfied, | |
638 | * drop the buffer | |
639 | */ | |
640 | goto no_buffer; | |
641 | } else { | |
1da177e4 | 642 | /* We do not want read in the flags */ |
ce8e922c | 643 | bp->b_flags &= ~XBF_READ; |
1da177e4 LT |
644 | } |
645 | } | |
646 | ||
ce8e922c | 647 | return bp; |
1da177e4 LT |
648 | |
649 | no_buffer: | |
ce8e922c NS |
650 | if (flags & (XBF_LOCK | XBF_TRYLOCK)) |
651 | xfs_buf_unlock(bp); | |
652 | xfs_buf_rele(bp); | |
1da177e4 LT |
653 | return NULL; |
654 | } | |
655 | ||
1da177e4 | 656 | /* |
ce8e922c NS |
657 | * If we are not low on memory then do the readahead in a deadlock |
658 | * safe manner. | |
1da177e4 LT |
659 | */ |
660 | void | |
ce8e922c | 661 | xfs_buf_readahead( |
1da177e4 | 662 | xfs_buftarg_t *target, |
e70b73f8 DC |
663 | xfs_daddr_t blkno, |
664 | size_t numblks) | |
1da177e4 | 665 | { |
0e6e847f | 666 | if (bdi_read_congested(target->bt_bdi)) |
1da177e4 LT |
667 | return; |
668 | ||
e70b73f8 | 669 | xfs_buf_read(target, blkno, numblks, |
1a1a3e97 | 670 | XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD|XBF_DONT_BLOCK); |
1da177e4 LT |
671 | } |
672 | ||
5adc94c2 DC |
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( | |
5adc94c2 DC |
679 | struct xfs_buftarg *target, |
680 | xfs_daddr_t daddr, | |
e70b73f8 | 681 | size_t numblks, |
5adc94c2 DC |
682 | int flags) |
683 | { | |
684 | xfs_buf_t *bp; | |
685 | int error; | |
686 | ||
e70b73f8 | 687 | bp = xfs_buf_get_uncached(target, numblks, flags); |
5adc94c2 DC |
688 | if (!bp) |
689 | return NULL; | |
690 | ||
691 | /* set up the buffer for a read IO */ | |
5adc94c2 DC |
692 | XFS_BUF_SET_ADDR(bp, daddr); |
693 | XFS_BUF_READ(bp); | |
5adc94c2 | 694 | |
e70b73f8 | 695 | xfsbdstrat(target->bt_mount, bp); |
1a1a3e97 | 696 | error = xfs_buf_iowait(bp); |
0e95f19a | 697 | if (error) { |
5adc94c2 DC |
698 | xfs_buf_relse(bp); |
699 | return NULL; | |
700 | } | |
701 | return bp; | |
1da177e4 LT |
702 | } |
703 | ||
44396476 DC |
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, | |
e70b73f8 | 711 | size_t numblks) |
44396476 DC |
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; | |
e70b73f8 | 719 | bp->b_buffer_length = bp->b_count_desired = numblks << BBSHIFT; |
44396476 DC |
720 | bp->b_bn = XFS_BUF_DADDR_NULL; |
721 | bp->b_flags &= ~XBF_MAPPED; | |
722 | } | |
723 | ||
1da177e4 LT |
724 | static inline struct page * |
725 | mem_to_page( | |
726 | void *addr) | |
727 | { | |
9e2779fa | 728 | if ((!is_vmalloc_addr(addr))) { |
1da177e4 LT |
729 | return virt_to_page(addr); |
730 | } else { | |
731 | return vmalloc_to_page(addr); | |
732 | } | |
733 | } | |
734 | ||
735 | int | |
ce8e922c NS |
736 | xfs_buf_associate_memory( |
737 | xfs_buf_t *bp, | |
1da177e4 LT |
738 | void *mem, |
739 | size_t len) | |
740 | { | |
741 | int rval; | |
742 | int i = 0; | |
d1afb678 LM |
743 | unsigned long pageaddr; |
744 | unsigned long offset; | |
745 | size_t buflen; | |
1da177e4 LT |
746 | int page_count; |
747 | ||
0e6e847f | 748 | pageaddr = (unsigned long)mem & PAGE_MASK; |
d1afb678 | 749 | offset = (unsigned long)mem - pageaddr; |
0e6e847f DC |
750 | buflen = PAGE_ALIGN(len + offset); |
751 | page_count = buflen >> PAGE_SHIFT; | |
1da177e4 LT |
752 | |
753 | /* Free any previous set of page pointers */ | |
ce8e922c NS |
754 | if (bp->b_pages) |
755 | _xfs_buf_free_pages(bp); | |
1da177e4 | 756 | |
ce8e922c NS |
757 | bp->b_pages = NULL; |
758 | bp->b_addr = mem; | |
1da177e4 | 759 | |
36fae17a | 760 | rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK); |
1da177e4 LT |
761 | if (rval) |
762 | return rval; | |
763 | ||
ce8e922c | 764 | bp->b_offset = offset; |
d1afb678 LM |
765 | |
766 | for (i = 0; i < bp->b_page_count; i++) { | |
767 | bp->b_pages[i] = mem_to_page((void *)pageaddr); | |
0e6e847f | 768 | pageaddr += PAGE_SIZE; |
1da177e4 | 769 | } |
1da177e4 | 770 | |
d1afb678 LM |
771 | bp->b_count_desired = len; |
772 | bp->b_buffer_length = buflen; | |
ce8e922c | 773 | bp->b_flags |= XBF_MAPPED; |
1da177e4 LT |
774 | |
775 | return 0; | |
776 | } | |
777 | ||
778 | xfs_buf_t * | |
686865f7 DC |
779 | xfs_buf_get_uncached( |
780 | struct xfs_buftarg *target, | |
e70b73f8 | 781 | size_t numblks, |
686865f7 | 782 | int flags) |
1da177e4 | 783 | { |
e70b73f8 | 784 | unsigned long page_count; |
1fa40b01 | 785 | int error, i; |
1da177e4 | 786 | xfs_buf_t *bp; |
1da177e4 | 787 | |
e70b73f8 | 788 | bp = xfs_buf_alloc(target, 0, numblks, 0); |
1da177e4 LT |
789 | if (unlikely(bp == NULL)) |
790 | goto fail; | |
1da177e4 | 791 | |
e70b73f8 | 792 | page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT; |
1fa40b01 CH |
793 | error = _xfs_buf_get_pages(bp, page_count, 0); |
794 | if (error) | |
1da177e4 LT |
795 | goto fail_free_buf; |
796 | ||
1fa40b01 | 797 | for (i = 0; i < page_count; i++) { |
686865f7 | 798 | bp->b_pages[i] = alloc_page(xb_to_gfp(flags)); |
1fa40b01 CH |
799 | if (!bp->b_pages[i]) |
800 | goto fail_free_mem; | |
1da177e4 | 801 | } |
1fa40b01 | 802 | bp->b_flags |= _XBF_PAGES; |
1da177e4 | 803 | |
1fa40b01 CH |
804 | error = _xfs_buf_map_pages(bp, XBF_MAPPED); |
805 | if (unlikely(error)) { | |
4f10700a DC |
806 | xfs_warn(target->bt_mount, |
807 | "%s: failed to map pages\n", __func__); | |
1da177e4 | 808 | goto fail_free_mem; |
1fa40b01 | 809 | } |
1da177e4 | 810 | |
686865f7 | 811 | trace_xfs_buf_get_uncached(bp, _RET_IP_); |
1da177e4 | 812 | return bp; |
1fa40b01 | 813 | |
1da177e4 | 814 | fail_free_mem: |
1fa40b01 CH |
815 | while (--i >= 0) |
816 | __free_page(bp->b_pages[i]); | |
ca165b88 | 817 | _xfs_buf_free_pages(bp); |
1da177e4 | 818 | fail_free_buf: |
4347b9d7 | 819 | kmem_zone_free(xfs_buf_zone, bp); |
1da177e4 LT |
820 | fail: |
821 | return NULL; | |
822 | } | |
823 | ||
824 | /* | |
1da177e4 LT |
825 | * Increment reference count on buffer, to hold the buffer concurrently |
826 | * with another thread which may release (free) the buffer asynchronously. | |
1da177e4 LT |
827 | * Must hold the buffer already to call this function. |
828 | */ | |
829 | void | |
ce8e922c NS |
830 | xfs_buf_hold( |
831 | xfs_buf_t *bp) | |
1da177e4 | 832 | { |
0b1b213f | 833 | trace_xfs_buf_hold(bp, _RET_IP_); |
ce8e922c | 834 | atomic_inc(&bp->b_hold); |
1da177e4 LT |
835 | } |
836 | ||
837 | /* | |
ce8e922c NS |
838 | * Releases a hold on the specified buffer. If the |
839 | * the hold count is 1, calls xfs_buf_free. | |
1da177e4 LT |
840 | */ |
841 | void | |
ce8e922c NS |
842 | xfs_buf_rele( |
843 | xfs_buf_t *bp) | |
1da177e4 | 844 | { |
74f75a0c | 845 | struct xfs_perag *pag = bp->b_pag; |
1da177e4 | 846 | |
0b1b213f | 847 | trace_xfs_buf_rele(bp, _RET_IP_); |
1da177e4 | 848 | |
74f75a0c | 849 | if (!pag) { |
430cbeb8 | 850 | ASSERT(list_empty(&bp->b_lru)); |
74f75a0c | 851 | ASSERT(RB_EMPTY_NODE(&bp->b_rbnode)); |
fad3aa1e NS |
852 | if (atomic_dec_and_test(&bp->b_hold)) |
853 | xfs_buf_free(bp); | |
854 | return; | |
855 | } | |
856 | ||
74f75a0c | 857 | ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode)); |
430cbeb8 | 858 | |
3790689f | 859 | ASSERT(atomic_read(&bp->b_hold) > 0); |
74f75a0c | 860 | if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) { |
bfc60177 | 861 | if (!(bp->b_flags & XBF_STALE) && |
430cbeb8 DC |
862 | atomic_read(&bp->b_lru_ref)) { |
863 | xfs_buf_lru_add(bp); | |
864 | spin_unlock(&pag->pag_buf_lock); | |
1da177e4 | 865 | } else { |
430cbeb8 | 866 | xfs_buf_lru_del(bp); |
43ff2122 | 867 | ASSERT(!(bp->b_flags & _XBF_DELWRI_Q)); |
74f75a0c DC |
868 | rb_erase(&bp->b_rbnode, &pag->pag_buf_tree); |
869 | spin_unlock(&pag->pag_buf_lock); | |
870 | xfs_perag_put(pag); | |
ce8e922c | 871 | xfs_buf_free(bp); |
1da177e4 LT |
872 | } |
873 | } | |
874 | } | |
875 | ||
876 | ||
877 | /* | |
0e6e847f | 878 | * Lock a buffer object, if it is not already locked. |
90810b9e DC |
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. | |
1da177e4 LT |
887 | */ |
888 | int | |
0c842ad4 CH |
889 | xfs_buf_trylock( |
890 | struct xfs_buf *bp) | |
1da177e4 LT |
891 | { |
892 | int locked; | |
893 | ||
ce8e922c | 894 | locked = down_trylock(&bp->b_sema) == 0; |
0b1b213f | 895 | if (locked) |
ce8e922c | 896 | XB_SET_OWNER(bp); |
90810b9e DC |
897 | else if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE)) |
898 | xfs_log_force(bp->b_target->bt_mount, 0); | |
0b1b213f | 899 | |
0c842ad4 CH |
900 | trace_xfs_buf_trylock(bp, _RET_IP_); |
901 | return locked; | |
1da177e4 | 902 | } |
1da177e4 LT |
903 | |
904 | /* | |
0e6e847f | 905 | * Lock a buffer object. |
ed3b4d6c DC |
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. | |
1da177e4 | 912 | */ |
ce8e922c NS |
913 | void |
914 | xfs_buf_lock( | |
0c842ad4 | 915 | struct xfs_buf *bp) |
1da177e4 | 916 | { |
0b1b213f CH |
917 | trace_xfs_buf_lock(bp, _RET_IP_); |
918 | ||
ed3b4d6c | 919 | if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE)) |
ebad861b | 920 | xfs_log_force(bp->b_target->bt_mount, 0); |
ce8e922c NS |
921 | down(&bp->b_sema); |
922 | XB_SET_OWNER(bp); | |
0b1b213f CH |
923 | |
924 | trace_xfs_buf_lock_done(bp, _RET_IP_); | |
1da177e4 LT |
925 | } |
926 | ||
1da177e4 | 927 | void |
ce8e922c | 928 | xfs_buf_unlock( |
0c842ad4 | 929 | struct xfs_buf *bp) |
1da177e4 | 930 | { |
ce8e922c NS |
931 | XB_CLEAR_OWNER(bp); |
932 | up(&bp->b_sema); | |
0b1b213f CH |
933 | |
934 | trace_xfs_buf_unlock(bp, _RET_IP_); | |
1da177e4 LT |
935 | } |
936 | ||
ce8e922c NS |
937 | STATIC void |
938 | xfs_buf_wait_unpin( | |
939 | xfs_buf_t *bp) | |
1da177e4 LT |
940 | { |
941 | DECLARE_WAITQUEUE (wait, current); | |
942 | ||
ce8e922c | 943 | if (atomic_read(&bp->b_pin_count) == 0) |
1da177e4 LT |
944 | return; |
945 | ||
ce8e922c | 946 | add_wait_queue(&bp->b_waiters, &wait); |
1da177e4 LT |
947 | for (;;) { |
948 | set_current_state(TASK_UNINTERRUPTIBLE); | |
ce8e922c | 949 | if (atomic_read(&bp->b_pin_count) == 0) |
1da177e4 | 950 | break; |
7eaceacc | 951 | io_schedule(); |
1da177e4 | 952 | } |
ce8e922c | 953 | remove_wait_queue(&bp->b_waiters, &wait); |
1da177e4 LT |
954 | set_current_state(TASK_RUNNING); |
955 | } | |
956 | ||
957 | /* | |
958 | * Buffer Utility Routines | |
959 | */ | |
960 | ||
1da177e4 | 961 | STATIC void |
ce8e922c | 962 | xfs_buf_iodone_work( |
c4028958 | 963 | struct work_struct *work) |
1da177e4 | 964 | { |
c4028958 DH |
965 | xfs_buf_t *bp = |
966 | container_of(work, xfs_buf_t, b_iodone_work); | |
1da177e4 | 967 | |
80f6c29d | 968 | if (bp->b_iodone) |
ce8e922c NS |
969 | (*(bp->b_iodone))(bp); |
970 | else if (bp->b_flags & XBF_ASYNC) | |
1da177e4 LT |
971 | xfs_buf_relse(bp); |
972 | } | |
973 | ||
974 | void | |
ce8e922c NS |
975 | xfs_buf_ioend( |
976 | xfs_buf_t *bp, | |
1da177e4 LT |
977 | int schedule) |
978 | { | |
0b1b213f CH |
979 | trace_xfs_buf_iodone(bp, _RET_IP_); |
980 | ||
77be55a5 | 981 | bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD); |
ce8e922c NS |
982 | if (bp->b_error == 0) |
983 | bp->b_flags |= XBF_DONE; | |
1da177e4 | 984 | |
ce8e922c | 985 | if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) { |
1da177e4 | 986 | if (schedule) { |
c4028958 | 987 | INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work); |
ce8e922c | 988 | queue_work(xfslogd_workqueue, &bp->b_iodone_work); |
1da177e4 | 989 | } else { |
c4028958 | 990 | xfs_buf_iodone_work(&bp->b_iodone_work); |
1da177e4 LT |
991 | } |
992 | } else { | |
b4dd330b | 993 | complete(&bp->b_iowait); |
1da177e4 LT |
994 | } |
995 | } | |
996 | ||
1da177e4 | 997 | void |
ce8e922c NS |
998 | xfs_buf_ioerror( |
999 | xfs_buf_t *bp, | |
1000 | int error) | |
1da177e4 LT |
1001 | { |
1002 | ASSERT(error >= 0 && error <= 0xffff); | |
ce8e922c | 1003 | bp->b_error = (unsigned short)error; |
0b1b213f | 1004 | trace_xfs_buf_ioerror(bp, error, _RET_IP_); |
1da177e4 LT |
1005 | } |
1006 | ||
901796af CH |
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 | ||
1da177e4 | 1018 | int |
64e0bc7d | 1019 | xfs_bwrite( |
5d765b97 | 1020 | struct xfs_buf *bp) |
1da177e4 | 1021 | { |
8c38366f | 1022 | int error; |
1da177e4 | 1023 | |
43ff2122 CH |
1024 | ASSERT(xfs_buf_islocked(bp)); |
1025 | ||
64e0bc7d | 1026 | bp->b_flags |= XBF_WRITE; |
43ff2122 | 1027 | bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q); |
1da177e4 | 1028 | |
939d723b | 1029 | xfs_bdstrat_cb(bp); |
1da177e4 | 1030 | |
8c38366f | 1031 | error = xfs_buf_iowait(bp); |
c2b006c1 CH |
1032 | if (error) { |
1033 | xfs_force_shutdown(bp->b_target->bt_mount, | |
1034 | SHUTDOWN_META_IO_ERROR); | |
1035 | } | |
64e0bc7d | 1036 | return error; |
5d765b97 | 1037 | } |
1da177e4 | 1038 | |
4e23471a CH |
1039 | /* |
1040 | * Called when we want to stop a buffer from getting written or read. | |
1a1a3e97 | 1041 | * We attach the EIO error, muck with its flags, and call xfs_buf_ioend |
4e23471a CH |
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 | */ | |
5a52c2a5 | 1055 | xfs_buf_ioerror(bp, EIO); |
4e23471a CH |
1056 | |
1057 | /* | |
1a1a3e97 | 1058 | * We're calling xfs_buf_ioend, so delete XBF_DONE flag. |
4e23471a CH |
1059 | */ |
1060 | XFS_BUF_UNREAD(bp); | |
4e23471a | 1061 | XFS_BUF_UNDONE(bp); |
c867cb61 | 1062 | xfs_buf_stale(bp); |
4e23471a | 1063 | |
1a1a3e97 | 1064 | xfs_buf_ioend(bp, 0); |
4e23471a CH |
1065 | |
1066 | return EIO; | |
1067 | } | |
1068 | ||
1069 | /* | |
1070 | * Same as xfs_bioerror, except that we are releasing the buffer | |
1a1a3e97 | 1071 | * here ourselves, and avoiding the xfs_buf_ioend call. |
4e23471a CH |
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 | { | |
ed43233b | 1079 | int64_t fl = bp->b_flags; |
4e23471a CH |
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); | |
4e23471a | 1089 | XFS_BUF_DONE(bp); |
c867cb61 | 1090 | xfs_buf_stale(bp); |
cb669ca5 | 1091 | bp->b_iodone = NULL; |
0cadda1c | 1092 | if (!(fl & XBF_ASYNC)) { |
4e23471a CH |
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 | */ | |
5a52c2a5 | 1099 | xfs_buf_ioerror(bp, EIO); |
5fde0326 | 1100 | complete(&bp->b_iowait); |
4e23471a CH |
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 | { | |
ebad861b | 1119 | if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) { |
4e23471a CH |
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 | ||
b8f82a4a | 1155 | STATIC void |
ce8e922c NS |
1156 | _xfs_buf_ioend( |
1157 | xfs_buf_t *bp, | |
1da177e4 LT |
1158 | int schedule) |
1159 | { | |
0e6e847f | 1160 | if (atomic_dec_and_test(&bp->b_io_remaining) == 1) |
ce8e922c | 1161 | xfs_buf_ioend(bp, schedule); |
1da177e4 LT |
1162 | } |
1163 | ||
782e3b3b | 1164 | STATIC void |
ce8e922c | 1165 | xfs_buf_bio_end_io( |
1da177e4 | 1166 | struct bio *bio, |
1da177e4 LT |
1167 | int error) |
1168 | { | |
ce8e922c | 1169 | xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private; |
1da177e4 | 1170 | |
cfbe5267 | 1171 | xfs_buf_ioerror(bp, -error); |
1da177e4 | 1172 | |
73c77e2c JB |
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 | ||
ce8e922c | 1176 | _xfs_buf_ioend(bp, 1); |
1da177e4 | 1177 | bio_put(bio); |
1da177e4 LT |
1178 | } |
1179 | ||
1180 | STATIC void | |
ce8e922c NS |
1181 | _xfs_buf_ioapply( |
1182 | xfs_buf_t *bp) | |
1da177e4 | 1183 | { |
a9759f2d | 1184 | int rw, map_i, total_nr_pages, nr_pages; |
1da177e4 | 1185 | struct bio *bio; |
ce8e922c NS |
1186 | int offset = bp->b_offset; |
1187 | int size = bp->b_count_desired; | |
1188 | sector_t sector = bp->b_bn; | |
1da177e4 | 1189 | |
ce8e922c | 1190 | total_nr_pages = bp->b_page_count; |
1da177e4 LT |
1191 | map_i = 0; |
1192 | ||
1d5ae5df CH |
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; | |
51bdd706 | 1204 | } else { |
1d5ae5df | 1205 | rw = READ; |
f538d4da CH |
1206 | } |
1207 | ||
34951f5c CH |
1208 | /* we only use the buffer cache for meta-data */ |
1209 | rw |= REQ_META; | |
1210 | ||
1da177e4 | 1211 | next_chunk: |
ce8e922c | 1212 | atomic_inc(&bp->b_io_remaining); |
1da177e4 LT |
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); | |
ce8e922c | 1218 | bio->bi_bdev = bp->b_target->bt_bdev; |
1da177e4 | 1219 | bio->bi_sector = sector; |
ce8e922c NS |
1220 | bio->bi_end_io = xfs_buf_bio_end_io; |
1221 | bio->bi_private = bp; | |
1da177e4 | 1222 | |
0e6e847f | 1223 | |
1da177e4 | 1224 | for (; size && nr_pages; nr_pages--, map_i++) { |
0e6e847f | 1225 | int rbytes, nbytes = PAGE_SIZE - offset; |
1da177e4 LT |
1226 | |
1227 | if (nbytes > size) | |
1228 | nbytes = size; | |
1229 | ||
ce8e922c NS |
1230 | rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset); |
1231 | if (rbytes < nbytes) | |
1da177e4 LT |
1232 | break; |
1233 | ||
1234 | offset = 0; | |
1235 | sector += nbytes >> BBSHIFT; | |
1236 | size -= nbytes; | |
1237 | total_nr_pages--; | |
1238 | } | |
1239 | ||
1da177e4 | 1240 | if (likely(bio->bi_size)) { |
73c77e2c JB |
1241 | if (xfs_buf_is_vmapped(bp)) { |
1242 | flush_kernel_vmap_range(bp->b_addr, | |
1243 | xfs_buf_vmap_len(bp)); | |
1244 | } | |
1da177e4 LT |
1245 | submit_bio(rw, bio); |
1246 | if (size) | |
1247 | goto next_chunk; | |
1248 | } else { | |
ce8e922c | 1249 | xfs_buf_ioerror(bp, EIO); |
ec53d1db | 1250 | bio_put(bio); |
1da177e4 LT |
1251 | } |
1252 | } | |
1253 | ||
0e95f19a | 1254 | void |
ce8e922c NS |
1255 | xfs_buf_iorequest( |
1256 | xfs_buf_t *bp) | |
1da177e4 | 1257 | { |
0b1b213f | 1258 | trace_xfs_buf_iorequest(bp, _RET_IP_); |
1da177e4 | 1259 | |
43ff2122 | 1260 | ASSERT(!(bp->b_flags & _XBF_DELWRI_Q)); |
1da177e4 | 1261 | |
375ec69d | 1262 | if (bp->b_flags & XBF_WRITE) |
ce8e922c | 1263 | xfs_buf_wait_unpin(bp); |
ce8e922c | 1264 | xfs_buf_hold(bp); |
1da177e4 LT |
1265 | |
1266 | /* Set the count to 1 initially, this will stop an I/O | |
1267 | * completion callout which happens before we have started | |
ce8e922c | 1268 | * all the I/O from calling xfs_buf_ioend too early. |
1da177e4 | 1269 | */ |
ce8e922c NS |
1270 | atomic_set(&bp->b_io_remaining, 1); |
1271 | _xfs_buf_ioapply(bp); | |
1272 | _xfs_buf_ioend(bp, 0); | |
1da177e4 | 1273 | |
ce8e922c | 1274 | xfs_buf_rele(bp); |
1da177e4 LT |
1275 | } |
1276 | ||
1277 | /* | |
0e95f19a DC |
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. | |
1da177e4 LT |
1281 | */ |
1282 | int | |
ce8e922c NS |
1283 | xfs_buf_iowait( |
1284 | xfs_buf_t *bp) | |
1da177e4 | 1285 | { |
0b1b213f CH |
1286 | trace_xfs_buf_iowait(bp, _RET_IP_); |
1287 | ||
0e95f19a DC |
1288 | if (!bp->b_error) |
1289 | wait_for_completion(&bp->b_iowait); | |
0b1b213f CH |
1290 | |
1291 | trace_xfs_buf_iowait_done(bp, _RET_IP_); | |
ce8e922c | 1292 | return bp->b_error; |
1da177e4 LT |
1293 | } |
1294 | ||
ce8e922c NS |
1295 | xfs_caddr_t |
1296 | xfs_buf_offset( | |
1297 | xfs_buf_t *bp, | |
1da177e4 LT |
1298 | size_t offset) |
1299 | { | |
1300 | struct page *page; | |
1301 | ||
ce8e922c | 1302 | if (bp->b_flags & XBF_MAPPED) |
62926044 | 1303 | return bp->b_addr + offset; |
1da177e4 | 1304 | |
ce8e922c | 1305 | offset += bp->b_offset; |
0e6e847f DC |
1306 | page = bp->b_pages[offset >> PAGE_SHIFT]; |
1307 | return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1)); | |
1da177e4 LT |
1308 | } |
1309 | ||
1310 | /* | |
1da177e4 LT |
1311 | * Move data into or out of a buffer. |
1312 | */ | |
1313 | void | |
ce8e922c NS |
1314 | xfs_buf_iomove( |
1315 | xfs_buf_t *bp, /* buffer to process */ | |
1da177e4 LT |
1316 | size_t boff, /* starting buffer offset */ |
1317 | size_t bsize, /* length to copy */ | |
b9c48649 | 1318 | void *data, /* data address */ |
ce8e922c | 1319 | xfs_buf_rw_t mode) /* read/write/zero flag */ |
1da177e4 LT |
1320 | { |
1321 | size_t bend, cpoff, csize; | |
1322 | struct page *page; | |
1323 | ||
1324 | bend = boff + bsize; | |
1325 | while (boff < bend) { | |
ce8e922c NS |
1326 | page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)]; |
1327 | cpoff = xfs_buf_poff(boff + bp->b_offset); | |
1da177e4 | 1328 | csize = min_t(size_t, |
0e6e847f | 1329 | PAGE_SIZE-cpoff, bp->b_count_desired-boff); |
1da177e4 | 1330 | |
0e6e847f | 1331 | ASSERT(((csize + cpoff) <= PAGE_SIZE)); |
1da177e4 LT |
1332 | |
1333 | switch (mode) { | |
ce8e922c | 1334 | case XBRW_ZERO: |
1da177e4 LT |
1335 | memset(page_address(page) + cpoff, 0, csize); |
1336 | break; | |
ce8e922c | 1337 | case XBRW_READ: |
1da177e4 LT |
1338 | memcpy(data, page_address(page) + cpoff, csize); |
1339 | break; | |
ce8e922c | 1340 | case XBRW_WRITE: |
1da177e4 LT |
1341 | memcpy(page_address(page) + cpoff, data, csize); |
1342 | } | |
1343 | ||
1344 | boff += csize; | |
1345 | data += csize; | |
1346 | } | |
1347 | } | |
1348 | ||
1349 | /* | |
ce8e922c | 1350 | * Handling of buffer targets (buftargs). |
1da177e4 LT |
1351 | */ |
1352 | ||
1353 | /* | |
430cbeb8 DC |
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. | |
1da177e4 LT |
1357 | */ |
1358 | void | |
1359 | xfs_wait_buftarg( | |
74f75a0c | 1360 | struct xfs_buftarg *btp) |
1da177e4 | 1361 | { |
430cbeb8 DC |
1362 | struct xfs_buf *bp; |
1363 | ||
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); | |
26af6552 | 1370 | delay(100); |
430cbeb8 | 1371 | goto restart; |
1da177e4 | 1372 | } |
430cbeb8 | 1373 | /* |
90802ed9 | 1374 | * clear the LRU reference count so the buffer doesn't get |
430cbeb8 DC |
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); | |
1da177e4 | 1381 | } |
430cbeb8 | 1382 | spin_unlock(&btp->bt_lru_lock); |
1da177e4 LT |
1383 | } |
1384 | ||
ff57ab21 DC |
1385 | int |
1386 | xfs_buftarg_shrink( | |
1387 | struct shrinker *shrink, | |
1495f230 | 1388 | struct shrink_control *sc) |
a6867a68 | 1389 | { |
ff57ab21 DC |
1390 | struct xfs_buftarg *btp = container_of(shrink, |
1391 | struct xfs_buftarg, bt_shrinker); | |
430cbeb8 | 1392 | struct xfs_buf *bp; |
1495f230 | 1393 | int nr_to_scan = sc->nr_to_scan; |
430cbeb8 DC |
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--; | |
ff57ab21 | 1422 | } |
430cbeb8 DC |
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; | |
a6867a68 DC |
1432 | } |
1433 | ||
1da177e4 LT |
1434 | void |
1435 | xfs_free_buftarg( | |
b7963133 CH |
1436 | struct xfs_mount *mp, |
1437 | struct xfs_buftarg *btp) | |
1da177e4 | 1438 | { |
ff57ab21 DC |
1439 | unregister_shrinker(&btp->bt_shrinker); |
1440 | ||
b7963133 CH |
1441 | if (mp->m_flags & XFS_MOUNT_BARRIER) |
1442 | xfs_blkdev_issue_flush(btp); | |
a6867a68 | 1443 | |
f0e2d93c | 1444 | kmem_free(btp); |
1da177e4 LT |
1445 | } |
1446 | ||
1da177e4 LT |
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 | { | |
ce8e922c NS |
1454 | btp->bt_bsize = blocksize; |
1455 | btp->bt_sshift = ffs(sectorsize) - 1; | |
1456 | btp->bt_smask = sectorsize - 1; | |
1da177e4 | 1457 | |
ce8e922c | 1458 | if (set_blocksize(btp->bt_bdev, sectorsize)) { |
02b102df CH |
1459 | char name[BDEVNAME_SIZE]; |
1460 | ||
1461 | bdevname(btp->bt_bdev, name); | |
1462 | ||
4f10700a DC |
1463 | xfs_warn(btp->bt_mount, |
1464 | "Cannot set_blocksize to %u on device %s\n", | |
02b102df | 1465 | sectorsize, name); |
1da177e4 LT |
1466 | return EINVAL; |
1467 | } | |
1468 | ||
1da177e4 LT |
1469 | return 0; |
1470 | } | |
1471 | ||
1472 | /* | |
ce8e922c NS |
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 | */ | |
1da177e4 LT |
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, | |
0e6e847f | 1483 | PAGE_SIZE, bdev_logical_block_size(bdev), 0); |
1da177e4 LT |
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 | ||
1da177e4 LT |
1495 | xfs_buftarg_t * |
1496 | xfs_alloc_buftarg( | |
ebad861b | 1497 | struct xfs_mount *mp, |
1da177e4 | 1498 | struct block_device *bdev, |
e2a07812 JE |
1499 | int external, |
1500 | const char *fsname) | |
1da177e4 LT |
1501 | { |
1502 | xfs_buftarg_t *btp; | |
1503 | ||
1504 | btp = kmem_zalloc(sizeof(*btp), KM_SLEEP); | |
1505 | ||
ebad861b | 1506 | btp->bt_mount = mp; |
ce8e922c NS |
1507 | btp->bt_dev = bdev->bd_dev; |
1508 | btp->bt_bdev = bdev; | |
0e6e847f DC |
1509 | btp->bt_bdi = blk_get_backing_dev_info(bdev); |
1510 | if (!btp->bt_bdi) | |
1511 | goto error; | |
1512 | ||
430cbeb8 DC |
1513 | INIT_LIST_HEAD(&btp->bt_lru); |
1514 | spin_lock_init(&btp->bt_lru_lock); | |
1da177e4 LT |
1515 | if (xfs_setsize_buftarg_early(btp, bdev)) |
1516 | goto error; | |
ff57ab21 DC |
1517 | btp->bt_shrinker.shrink = xfs_buftarg_shrink; |
1518 | btp->bt_shrinker.seeks = DEFAULT_SEEKS; | |
1519 | register_shrinker(&btp->bt_shrinker); | |
1da177e4 LT |
1520 | return btp; |
1521 | ||
1522 | error: | |
f0e2d93c | 1523 | kmem_free(btp); |
1da177e4 LT |
1524 | return NULL; |
1525 | } | |
1526 | ||
1da177e4 | 1527 | /* |
43ff2122 CH |
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. | |
1da177e4 | 1537 | */ |
43ff2122 | 1538 | bool |
ce8e922c | 1539 | xfs_buf_delwri_queue( |
43ff2122 CH |
1540 | struct xfs_buf *bp, |
1541 | struct list_head *list) | |
1da177e4 | 1542 | { |
43ff2122 | 1543 | ASSERT(xfs_buf_islocked(bp)); |
5a8ee6ba | 1544 | ASSERT(!(bp->b_flags & XBF_READ)); |
1da177e4 | 1545 | |
43ff2122 CH |
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; | |
1da177e4 | 1554 | } |
1da177e4 | 1555 | |
43ff2122 | 1556 | trace_xfs_buf_delwri_queue(bp, _RET_IP_); |
d808f617 DC |
1557 | |
1558 | /* | |
43ff2122 CH |
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. | |
d808f617 | 1565 | */ |
43ff2122 CH |
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); | |
585e6d88 | 1570 | } |
585e6d88 | 1571 | |
43ff2122 | 1572 | return true; |
585e6d88 DC |
1573 | } |
1574 | ||
089716aa DC |
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 | ||
43ff2122 CH |
1598 | static int |
1599 | __xfs_buf_delwri_submit( | |
1600 | struct list_head *buffer_list, | |
1601 | struct list_head *io_list, | |
1602 | bool wait) | |
1da177e4 | 1603 | { |
43ff2122 CH |
1604 | struct blk_plug plug; |
1605 | struct xfs_buf *bp, *n; | |
1606 | int pinned = 0; | |
1607 | ||
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); | |
1618 | } | |
978c7b2f | 1619 | |
43ff2122 CH |
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 | } | |
c9c12971 | 1631 | |
43ff2122 CH |
1632 | list_move_tail(&bp->b_list, io_list); |
1633 | trace_xfs_buf_delwri_split(bp, _RET_IP_); | |
1634 | } | |
1da177e4 | 1635 | |
43ff2122 | 1636 | list_sort(NULL, io_list, xfs_buf_cmp); |
1da177e4 | 1637 | |
43ff2122 CH |
1638 | blk_start_plug(&plug); |
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; | |
a1b7ea5d | 1642 | |
43ff2122 CH |
1643 | if (!wait) { |
1644 | bp->b_flags |= XBF_ASYNC; | |
ce8e922c | 1645 | list_del_init(&bp->b_list); |
1da177e4 | 1646 | } |
43ff2122 CH |
1647 | xfs_bdstrat_cb(bp); |
1648 | } | |
1649 | blk_finish_plug(&plug); | |
1da177e4 | 1650 | |
43ff2122 | 1651 | return pinned; |
1da177e4 LT |
1652 | } |
1653 | ||
1654 | /* | |
43ff2122 CH |
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. | |
1da177e4 LT |
1662 | */ |
1663 | int | |
43ff2122 CH |
1664 | xfs_buf_delwri_submit_nowait( |
1665 | struct list_head *buffer_list) | |
1da177e4 | 1666 | { |
43ff2122 CH |
1667 | LIST_HEAD (io_list); |
1668 | return __xfs_buf_delwri_submit(buffer_list, &io_list, false); | |
1669 | } | |
1da177e4 | 1670 | |
43ff2122 CH |
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; | |
1da177e4 | 1686 | |
43ff2122 | 1687 | __xfs_buf_delwri_submit(buffer_list, &io_list, true); |
1da177e4 | 1688 | |
43ff2122 CH |
1689 | /* Wait for IO to complete. */ |
1690 | while (!list_empty(&io_list)) { | |
1691 | bp = list_first_entry(&io_list, struct xfs_buf, b_list); | |
a1b7ea5d | 1692 | |
089716aa | 1693 | list_del_init(&bp->b_list); |
43ff2122 CH |
1694 | error2 = xfs_buf_iowait(bp); |
1695 | xfs_buf_relse(bp); | |
1696 | if (!error) | |
1697 | error = error2; | |
1da177e4 LT |
1698 | } |
1699 | ||
43ff2122 | 1700 | return error; |
1da177e4 LT |
1701 | } |
1702 | ||
04d8b284 | 1703 | int __init |
ce8e922c | 1704 | xfs_buf_init(void) |
1da177e4 | 1705 | { |
8758280f NS |
1706 | xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf", |
1707 | KM_ZONE_HWALIGN, NULL); | |
ce8e922c | 1708 | if (!xfs_buf_zone) |
0b1b213f | 1709 | goto out; |
04d8b284 | 1710 | |
51749e47 | 1711 | xfslogd_workqueue = alloc_workqueue("xfslogd", |
6370a6ad | 1712 | WQ_MEM_RECLAIM | WQ_HIGHPRI, 1); |
23ea4032 | 1713 | if (!xfslogd_workqueue) |
04d8b284 | 1714 | goto out_free_buf_zone; |
1da177e4 | 1715 | |
23ea4032 | 1716 | return 0; |
1da177e4 | 1717 | |
23ea4032 | 1718 | out_free_buf_zone: |
ce8e922c | 1719 | kmem_zone_destroy(xfs_buf_zone); |
0b1b213f | 1720 | out: |
8758280f | 1721 | return -ENOMEM; |
1da177e4 LT |
1722 | } |
1723 | ||
1da177e4 | 1724 | void |
ce8e922c | 1725 | xfs_buf_terminate(void) |
1da177e4 | 1726 | { |
04d8b284 | 1727 | destroy_workqueue(xfslogd_workqueue); |
ce8e922c | 1728 | kmem_zone_destroy(xfs_buf_zone); |
1da177e4 | 1729 | } |