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