Commit | Line | Data |
---|---|---|
95d402f0 MP |
1 | /* |
2 | * Copyright (C) 2009-2011 Red Hat, Inc. | |
3 | * | |
4 | * Author: Mikulas Patocka <mpatocka@redhat.com> | |
5 | * | |
6 | * This file is released under the GPL. | |
7 | */ | |
8 | ||
afa53df8 | 9 | #include <linux/dm-bufio.h> |
95d402f0 MP |
10 | |
11 | #include <linux/device-mapper.h> | |
12 | #include <linux/dm-io.h> | |
13 | #include <linux/slab.h> | |
5b3cc15a | 14 | #include <linux/sched/mm.h> |
f495339c | 15 | #include <linux/jiffies.h> |
95d402f0 | 16 | #include <linux/vmalloc.h> |
95d402f0 | 17 | #include <linux/shrinker.h> |
6f66263f | 18 | #include <linux/module.h> |
4e420c45 | 19 | #include <linux/rbtree.h> |
86bad0c7 | 20 | #include <linux/stacktrace.h> |
95d402f0 MP |
21 | |
22 | #define DM_MSG_PREFIX "bufio" | |
23 | ||
24 | /* | |
25 | * Memory management policy: | |
26 | * Limit the number of buffers to DM_BUFIO_MEMORY_PERCENT of main memory | |
27 | * or DM_BUFIO_VMALLOC_PERCENT of vmalloc memory (whichever is lower). | |
28 | * Always allocate at least DM_BUFIO_MIN_BUFFERS buffers. | |
29 | * Start background writeback when there are DM_BUFIO_WRITEBACK_PERCENT | |
30 | * dirty buffers. | |
31 | */ | |
32 | #define DM_BUFIO_MIN_BUFFERS 8 | |
33 | ||
34 | #define DM_BUFIO_MEMORY_PERCENT 2 | |
35 | #define DM_BUFIO_VMALLOC_PERCENT 25 | |
36 | #define DM_BUFIO_WRITEBACK_PERCENT 75 | |
37 | ||
38 | /* | |
39 | * Check buffer ages in this interval (seconds) | |
40 | */ | |
33096a78 | 41 | #define DM_BUFIO_WORK_TIMER_SECS 30 |
95d402f0 MP |
42 | |
43 | /* | |
44 | * Free buffers when they are older than this (seconds) | |
45 | */ | |
33096a78 | 46 | #define DM_BUFIO_DEFAULT_AGE_SECS 300 |
95d402f0 MP |
47 | |
48 | /* | |
33096a78 | 49 | * The nr of bytes of cached data to keep around. |
95d402f0 | 50 | */ |
33096a78 | 51 | #define DM_BUFIO_DEFAULT_RETAIN_BYTES (256 * 1024) |
95d402f0 | 52 | |
1e3b21c6 MP |
53 | /* |
54 | * Align buffer writes to this boundary. | |
55 | * Tests show that SSDs have the highest IOPS when using 4k writes. | |
56 | */ | |
57 | #define DM_BUFIO_WRITE_ALIGN 4096 | |
58 | ||
95d402f0 MP |
59 | /* |
60 | * dm_buffer->list_mode | |
61 | */ | |
62 | #define LIST_CLEAN 0 | |
63 | #define LIST_DIRTY 1 | |
64 | #define LIST_SIZE 2 | |
65 | ||
66 | /* | |
67 | * Linking of buffers: | |
ef992373 | 68 | * All buffers are linked to buffer_tree with their node field. |
95d402f0 MP |
69 | * |
70 | * Clean buffers that are not being written (B_WRITING not set) | |
71 | * are linked to lru[LIST_CLEAN] with their lru_list field. | |
72 | * | |
73 | * Dirty and clean buffers that are being written are linked to | |
74 | * lru[LIST_DIRTY] with their lru_list field. When the write | |
75 | * finishes, the buffer cannot be relinked immediately (because we | |
76 | * are in an interrupt context and relinking requires process | |
77 | * context), so some clean-not-writing buffers can be held on | |
78 | * dirty_lru too. They are later added to lru in the process | |
79 | * context. | |
80 | */ | |
81 | struct dm_bufio_client { | |
82 | struct mutex lock; | |
83 | ||
84 | struct list_head lru[LIST_SIZE]; | |
85 | unsigned long n_buffers[LIST_SIZE]; | |
86 | ||
87 | struct block_device *bdev; | |
88 | unsigned block_size; | |
f51f2e0a | 89 | s8 sectors_per_block_bits; |
95d402f0 MP |
90 | void (*alloc_callback)(struct dm_buffer *); |
91 | void (*write_callback)(struct dm_buffer *); | |
92 | ||
359dbf19 | 93 | struct kmem_cache *slab_buffer; |
21bb1327 | 94 | struct kmem_cache *slab_cache; |
95d402f0 MP |
95 | struct dm_io_client *dm_io; |
96 | ||
97 | struct list_head reserved_buffers; | |
98 | unsigned need_reserved_buffers; | |
99 | ||
55b082e6 MP |
100 | unsigned minimum_buffers; |
101 | ||
4e420c45 | 102 | struct rb_root buffer_tree; |
95d402f0 MP |
103 | wait_queue_head_t free_buffer_wait; |
104 | ||
400a0bef MP |
105 | sector_t start; |
106 | ||
95d402f0 MP |
107 | int async_write_error; |
108 | ||
109 | struct list_head client_list; | |
110 | struct shrinker shrinker; | |
111 | }; | |
112 | ||
113 | /* | |
114 | * Buffer state bits. | |
115 | */ | |
116 | #define B_READING 0 | |
117 | #define B_WRITING 1 | |
118 | #define B_DIRTY 2 | |
119 | ||
120 | /* | |
121 | * Describes how the block was allocated: | |
122 | * kmem_cache_alloc(), __get_free_pages() or vmalloc(). | |
123 | * See the comment at alloc_buffer_data. | |
124 | */ | |
125 | enum data_mode { | |
126 | DATA_MODE_SLAB = 0, | |
127 | DATA_MODE_GET_FREE_PAGES = 1, | |
128 | DATA_MODE_VMALLOC = 2, | |
129 | DATA_MODE_LIMIT = 3 | |
130 | }; | |
131 | ||
132 | struct dm_buffer { | |
4e420c45 | 133 | struct rb_node node; |
95d402f0 MP |
134 | struct list_head lru_list; |
135 | sector_t block; | |
136 | void *data; | |
03b02939 | 137 | unsigned char data_mode; /* DATA_MODE_* */ |
95d402f0 | 138 | unsigned char list_mode; /* LIST_* */ |
4e4cbee9 CH |
139 | blk_status_t read_error; |
140 | blk_status_t write_error; | |
03b02939 | 141 | unsigned hold_count; |
95d402f0 MP |
142 | unsigned long state; |
143 | unsigned long last_accessed; | |
1e3b21c6 MP |
144 | unsigned dirty_start; |
145 | unsigned dirty_end; | |
146 | unsigned write_start; | |
147 | unsigned write_end; | |
95d402f0 | 148 | struct dm_bufio_client *c; |
2480945c | 149 | struct list_head write_list; |
45354f1e | 150 | void (*end_io)(struct dm_buffer *, blk_status_t); |
86bad0c7 MP |
151 | #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING |
152 | #define MAX_STACK 10 | |
741b58f3 | 153 | unsigned int stack_len; |
86bad0c7 MP |
154 | unsigned long stack_entries[MAX_STACK]; |
155 | #endif | |
95d402f0 MP |
156 | }; |
157 | ||
158 | /*----------------------------------------------------------------*/ | |
159 | ||
95d402f0 MP |
160 | #define dm_bufio_in_request() (!!current->bio_list) |
161 | ||
162 | static void dm_bufio_lock(struct dm_bufio_client *c) | |
163 | { | |
164 | mutex_lock_nested(&c->lock, dm_bufio_in_request()); | |
165 | } | |
166 | ||
167 | static int dm_bufio_trylock(struct dm_bufio_client *c) | |
168 | { | |
169 | return mutex_trylock(&c->lock); | |
170 | } | |
171 | ||
172 | static void dm_bufio_unlock(struct dm_bufio_client *c) | |
173 | { | |
174 | mutex_unlock(&c->lock); | |
175 | } | |
176 | ||
95d402f0 MP |
177 | /*----------------------------------------------------------------*/ |
178 | ||
179 | /* | |
180 | * Default cache size: available memory divided by the ratio. | |
181 | */ | |
182 | static unsigned long dm_bufio_default_cache_size; | |
183 | ||
184 | /* | |
185 | * Total cache size set by the user. | |
186 | */ | |
187 | static unsigned long dm_bufio_cache_size; | |
188 | ||
189 | /* | |
190 | * A copy of dm_bufio_cache_size because dm_bufio_cache_size can change | |
191 | * at any time. If it disagrees, the user has changed cache size. | |
192 | */ | |
193 | static unsigned long dm_bufio_cache_size_latch; | |
194 | ||
195 | static DEFINE_SPINLOCK(param_spinlock); | |
196 | ||
197 | /* | |
198 | * Buffers are freed after this timeout | |
199 | */ | |
200 | static unsigned dm_bufio_max_age = DM_BUFIO_DEFAULT_AGE_SECS; | |
13840d38 | 201 | static unsigned long dm_bufio_retain_bytes = DM_BUFIO_DEFAULT_RETAIN_BYTES; |
95d402f0 MP |
202 | |
203 | static unsigned long dm_bufio_peak_allocated; | |
204 | static unsigned long dm_bufio_allocated_kmem_cache; | |
205 | static unsigned long dm_bufio_allocated_get_free_pages; | |
206 | static unsigned long dm_bufio_allocated_vmalloc; | |
207 | static unsigned long dm_bufio_current_allocated; | |
208 | ||
209 | /*----------------------------------------------------------------*/ | |
210 | ||
211 | /* | |
212 | * Per-client cache: dm_bufio_cache_size / dm_bufio_client_count | |
213 | */ | |
214 | static unsigned long dm_bufio_cache_size_per_client; | |
215 | ||
216 | /* | |
217 | * The current number of clients. | |
218 | */ | |
219 | static int dm_bufio_client_count; | |
220 | ||
221 | /* | |
222 | * The list of all clients. | |
223 | */ | |
224 | static LIST_HEAD(dm_bufio_all_clients); | |
225 | ||
226 | /* | |
227 | * This mutex protects dm_bufio_cache_size_latch, | |
228 | * dm_bufio_cache_size_per_client and dm_bufio_client_count | |
229 | */ | |
230 | static DEFINE_MUTEX(dm_bufio_clients_lock); | |
231 | ||
86bad0c7 MP |
232 | #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING |
233 | static void buffer_record_stack(struct dm_buffer *b) | |
234 | { | |
741b58f3 | 235 | b->stack_len = stack_trace_save(b->stack_entries, MAX_STACK, 2); |
86bad0c7 MP |
236 | } |
237 | #endif | |
238 | ||
4e420c45 JT |
239 | /*---------------------------------------------------------------- |
240 | * A red/black tree acts as an index for all the buffers. | |
241 | *--------------------------------------------------------------*/ | |
242 | static struct dm_buffer *__find(struct dm_bufio_client *c, sector_t block) | |
243 | { | |
244 | struct rb_node *n = c->buffer_tree.rb_node; | |
245 | struct dm_buffer *b; | |
246 | ||
247 | while (n) { | |
248 | b = container_of(n, struct dm_buffer, node); | |
249 | ||
250 | if (b->block == block) | |
251 | return b; | |
252 | ||
253 | n = (b->block < block) ? n->rb_left : n->rb_right; | |
254 | } | |
255 | ||
256 | return NULL; | |
257 | } | |
258 | ||
259 | static void __insert(struct dm_bufio_client *c, struct dm_buffer *b) | |
260 | { | |
261 | struct rb_node **new = &c->buffer_tree.rb_node, *parent = NULL; | |
262 | struct dm_buffer *found; | |
263 | ||
264 | while (*new) { | |
265 | found = container_of(*new, struct dm_buffer, node); | |
266 | ||
267 | if (found->block == b->block) { | |
268 | BUG_ON(found != b); | |
269 | return; | |
270 | } | |
271 | ||
272 | parent = *new; | |
273 | new = (found->block < b->block) ? | |
274 | &((*new)->rb_left) : &((*new)->rb_right); | |
275 | } | |
276 | ||
277 | rb_link_node(&b->node, parent, new); | |
278 | rb_insert_color(&b->node, &c->buffer_tree); | |
279 | } | |
280 | ||
281 | static void __remove(struct dm_bufio_client *c, struct dm_buffer *b) | |
282 | { | |
283 | rb_erase(&b->node, &c->buffer_tree); | |
284 | } | |
285 | ||
95d402f0 MP |
286 | /*----------------------------------------------------------------*/ |
287 | ||
d0a328a3 | 288 | static void adjust_total_allocated(struct dm_buffer *b, bool unlink) |
95d402f0 | 289 | { |
d0a328a3 MP |
290 | unsigned char data_mode; |
291 | long diff; | |
292 | ||
95d402f0 MP |
293 | static unsigned long * const class_ptr[DATA_MODE_LIMIT] = { |
294 | &dm_bufio_allocated_kmem_cache, | |
295 | &dm_bufio_allocated_get_free_pages, | |
296 | &dm_bufio_allocated_vmalloc, | |
297 | }; | |
298 | ||
d0a328a3 MP |
299 | data_mode = b->data_mode; |
300 | diff = (long)b->c->block_size; | |
301 | if (unlink) | |
302 | diff = -diff; | |
303 | ||
95d402f0 MP |
304 | spin_lock(¶m_spinlock); |
305 | ||
306 | *class_ptr[data_mode] += diff; | |
307 | ||
308 | dm_bufio_current_allocated += diff; | |
309 | ||
310 | if (dm_bufio_current_allocated > dm_bufio_peak_allocated) | |
311 | dm_bufio_peak_allocated = dm_bufio_current_allocated; | |
312 | ||
313 | spin_unlock(¶m_spinlock); | |
314 | } | |
315 | ||
316 | /* | |
317 | * Change the number of clients and recalculate per-client limit. | |
318 | */ | |
319 | static void __cache_size_refresh(void) | |
320 | { | |
321 | BUG_ON(!mutex_is_locked(&dm_bufio_clients_lock)); | |
322 | BUG_ON(dm_bufio_client_count < 0); | |
323 | ||
6aa7de05 | 324 | dm_bufio_cache_size_latch = READ_ONCE(dm_bufio_cache_size); |
95d402f0 MP |
325 | |
326 | /* | |
327 | * Use default if set to 0 and report the actual cache size used. | |
328 | */ | |
329 | if (!dm_bufio_cache_size_latch) { | |
330 | (void)cmpxchg(&dm_bufio_cache_size, 0, | |
331 | dm_bufio_default_cache_size); | |
332 | dm_bufio_cache_size_latch = dm_bufio_default_cache_size; | |
333 | } | |
334 | ||
335 | dm_bufio_cache_size_per_client = dm_bufio_cache_size_latch / | |
336 | (dm_bufio_client_count ? : 1); | |
337 | } | |
338 | ||
339 | /* | |
340 | * Allocating buffer data. | |
341 | * | |
342 | * Small buffers are allocated with kmem_cache, to use space optimally. | |
343 | * | |
344 | * For large buffers, we choose between get_free_pages and vmalloc. | |
345 | * Each has advantages and disadvantages. | |
346 | * | |
347 | * __get_free_pages can randomly fail if the memory is fragmented. | |
348 | * __vmalloc won't randomly fail, but vmalloc space is limited (it may be | |
349 | * as low as 128M) so using it for caching is not appropriate. | |
350 | * | |
351 | * If the allocation may fail we use __get_free_pages. Memory fragmentation | |
352 | * won't have a fatal effect here, but it just causes flushes of some other | |
353 | * buffers and more I/O will be performed. Don't use __get_free_pages if it | |
354 | * always fails (i.e. order >= MAX_ORDER). | |
355 | * | |
356 | * If the allocation shouldn't fail we use __vmalloc. This is only for the | |
357 | * initial reserve allocation, so there's no risk of wasting all vmalloc | |
358 | * space. | |
359 | */ | |
360 | static void *alloc_buffer_data(struct dm_bufio_client *c, gfp_t gfp_mask, | |
03b02939 | 361 | unsigned char *data_mode) |
95d402f0 | 362 | { |
21bb1327 | 363 | if (unlikely(c->slab_cache != NULL)) { |
95d402f0 | 364 | *data_mode = DATA_MODE_SLAB; |
21bb1327 | 365 | return kmem_cache_alloc(c->slab_cache, gfp_mask); |
95d402f0 MP |
366 | } |
367 | ||
f51f2e0a | 368 | if (c->block_size <= KMALLOC_MAX_SIZE && |
95d402f0 MP |
369 | gfp_mask & __GFP_NORETRY) { |
370 | *data_mode = DATA_MODE_GET_FREE_PAGES; | |
371 | return (void *)__get_free_pages(gfp_mask, | |
f51f2e0a | 372 | c->sectors_per_block_bits - (PAGE_SHIFT - SECTOR_SHIFT)); |
95d402f0 MP |
373 | } |
374 | ||
375 | *data_mode = DATA_MODE_VMALLOC; | |
502624bd MP |
376 | |
377 | /* | |
378 | * __vmalloc allocates the data pages and auxiliary structures with | |
379 | * gfp_flags that were specified, but pagetables are always allocated | |
380 | * with GFP_KERNEL, no matter what was specified as gfp_mask. | |
381 | * | |
382 | * Consequently, we must set per-process flag PF_MEMALLOC_NOIO so that | |
383 | * all allocations done by this process (including pagetables) are done | |
384 | * as if GFP_NOIO was specified. | |
385 | */ | |
590347e4 AB |
386 | if (gfp_mask & __GFP_NORETRY) { |
387 | unsigned noio_flag = memalloc_noio_save(); | |
388 | void *ptr = __vmalloc(c->block_size, gfp_mask, PAGE_KERNEL); | |
502624bd | 389 | |
502624bd | 390 | memalloc_noio_restore(noio_flag); |
590347e4 AB |
391 | return ptr; |
392 | } | |
502624bd | 393 | |
590347e4 | 394 | return __vmalloc(c->block_size, gfp_mask, PAGE_KERNEL); |
95d402f0 MP |
395 | } |
396 | ||
397 | /* | |
398 | * Free buffer's data. | |
399 | */ | |
400 | static void free_buffer_data(struct dm_bufio_client *c, | |
03b02939 | 401 | void *data, unsigned char data_mode) |
95d402f0 MP |
402 | { |
403 | switch (data_mode) { | |
404 | case DATA_MODE_SLAB: | |
21bb1327 | 405 | kmem_cache_free(c->slab_cache, data); |
95d402f0 MP |
406 | break; |
407 | ||
408 | case DATA_MODE_GET_FREE_PAGES: | |
f51f2e0a MP |
409 | free_pages((unsigned long)data, |
410 | c->sectors_per_block_bits - (PAGE_SHIFT - SECTOR_SHIFT)); | |
95d402f0 MP |
411 | break; |
412 | ||
413 | case DATA_MODE_VMALLOC: | |
414 | vfree(data); | |
415 | break; | |
416 | ||
417 | default: | |
418 | DMCRIT("dm_bufio_free_buffer_data: bad data mode: %d", | |
419 | data_mode); | |
420 | BUG(); | |
421 | } | |
422 | } | |
423 | ||
424 | /* | |
425 | * Allocate buffer and its data. | |
426 | */ | |
427 | static struct dm_buffer *alloc_buffer(struct dm_bufio_client *c, gfp_t gfp_mask) | |
428 | { | |
359dbf19 | 429 | struct dm_buffer *b = kmem_cache_alloc(c->slab_buffer, gfp_mask); |
95d402f0 MP |
430 | |
431 | if (!b) | |
432 | return NULL; | |
433 | ||
434 | b->c = c; | |
435 | ||
436 | b->data = alloc_buffer_data(c, gfp_mask, &b->data_mode); | |
437 | if (!b->data) { | |
359dbf19 | 438 | kmem_cache_free(c->slab_buffer, b); |
95d402f0 MP |
439 | return NULL; |
440 | } | |
441 | ||
86bad0c7 | 442 | #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING |
741b58f3 | 443 | b->stack_len = 0; |
86bad0c7 | 444 | #endif |
95d402f0 MP |
445 | return b; |
446 | } | |
447 | ||
448 | /* | |
449 | * Free buffer and its data. | |
450 | */ | |
451 | static void free_buffer(struct dm_buffer *b) | |
452 | { | |
453 | struct dm_bufio_client *c = b->c; | |
454 | ||
95d402f0 | 455 | free_buffer_data(c, b->data, b->data_mode); |
359dbf19 | 456 | kmem_cache_free(c->slab_buffer, b); |
95d402f0 MP |
457 | } |
458 | ||
459 | /* | |
ef992373 | 460 | * Link buffer to the buffer tree and clean or dirty queue. |
95d402f0 MP |
461 | */ |
462 | static void __link_buffer(struct dm_buffer *b, sector_t block, int dirty) | |
463 | { | |
464 | struct dm_bufio_client *c = b->c; | |
465 | ||
466 | c->n_buffers[dirty]++; | |
467 | b->block = block; | |
468 | b->list_mode = dirty; | |
469 | list_add(&b->lru_list, &c->lru[dirty]); | |
4e420c45 | 470 | __insert(b->c, b); |
95d402f0 | 471 | b->last_accessed = jiffies; |
26d2ef0c | 472 | |
d0a328a3 | 473 | adjust_total_allocated(b, false); |
95d402f0 MP |
474 | } |
475 | ||
476 | /* | |
ef992373 | 477 | * Unlink buffer from the buffer tree and dirty or clean queue. |
95d402f0 MP |
478 | */ |
479 | static void __unlink_buffer(struct dm_buffer *b) | |
480 | { | |
481 | struct dm_bufio_client *c = b->c; | |
482 | ||
483 | BUG_ON(!c->n_buffers[b->list_mode]); | |
484 | ||
485 | c->n_buffers[b->list_mode]--; | |
4e420c45 | 486 | __remove(b->c, b); |
95d402f0 | 487 | list_del(&b->lru_list); |
26d2ef0c | 488 | |
d0a328a3 | 489 | adjust_total_allocated(b, true); |
95d402f0 MP |
490 | } |
491 | ||
492 | /* | |
493 | * Place the buffer to the head of dirty or clean LRU queue. | |
494 | */ | |
495 | static void __relink_lru(struct dm_buffer *b, int dirty) | |
496 | { | |
497 | struct dm_bufio_client *c = b->c; | |
498 | ||
499 | BUG_ON(!c->n_buffers[b->list_mode]); | |
500 | ||
501 | c->n_buffers[b->list_mode]--; | |
502 | c->n_buffers[dirty]++; | |
503 | b->list_mode = dirty; | |
54499afb | 504 | list_move(&b->lru_list, &c->lru[dirty]); |
eb76faf5 | 505 | b->last_accessed = jiffies; |
95d402f0 MP |
506 | } |
507 | ||
508 | /*---------------------------------------------------------------- | |
509 | * Submit I/O on the buffer. | |
510 | * | |
511 | * Bio interface is faster but it has some problems: | |
512 | * the vector list is limited (increasing this limit increases | |
513 | * memory-consumption per buffer, so it is not viable); | |
514 | * | |
515 | * the memory must be direct-mapped, not vmalloced; | |
516 | * | |
95d402f0 MP |
517 | * If the buffer is small enough (up to DM_BUFIO_INLINE_VECS pages) and |
518 | * it is not vmalloced, try using the bio interface. | |
519 | * | |
520 | * If the buffer is big, if it is vmalloced or if the underlying device | |
521 | * rejects the bio because it is too large, use dm-io layer to do the I/O. | |
522 | * The dm-io layer splits the I/O into multiple requests, avoiding the above | |
523 | * shortcomings. | |
524 | *--------------------------------------------------------------*/ | |
525 | ||
526 | /* | |
527 | * dm-io completion routine. It just calls b->bio.bi_end_io, pretending | |
528 | * that the request was handled directly with bio interface. | |
529 | */ | |
530 | static void dmio_complete(unsigned long error, void *context) | |
531 | { | |
532 | struct dm_buffer *b = context; | |
533 | ||
45354f1e | 534 | b->end_io(b, unlikely(error != 0) ? BLK_STS_IOERR : 0); |
95d402f0 MP |
535 | } |
536 | ||
400a0bef | 537 | static void use_dmio(struct dm_buffer *b, int rw, sector_t sector, |
45354f1e | 538 | unsigned n_sectors, unsigned offset) |
95d402f0 MP |
539 | { |
540 | int r; | |
541 | struct dm_io_request io_req = { | |
e6047149 MC |
542 | .bi_op = rw, |
543 | .bi_op_flags = 0, | |
95d402f0 MP |
544 | .notify.fn = dmio_complete, |
545 | .notify.context = b, | |
546 | .client = b->c->dm_io, | |
547 | }; | |
548 | struct dm_io_region region = { | |
549 | .bdev = b->c->bdev, | |
400a0bef MP |
550 | .sector = sector, |
551 | .count = n_sectors, | |
95d402f0 MP |
552 | }; |
553 | ||
554 | if (b->data_mode != DATA_MODE_VMALLOC) { | |
555 | io_req.mem.type = DM_IO_KMEM; | |
1e3b21c6 | 556 | io_req.mem.ptr.addr = (char *)b->data + offset; |
95d402f0 MP |
557 | } else { |
558 | io_req.mem.type = DM_IO_VMA; | |
1e3b21c6 | 559 | io_req.mem.ptr.vma = (char *)b->data + offset; |
95d402f0 MP |
560 | } |
561 | ||
95d402f0 | 562 | r = dm_io(&io_req, 1, ®ion, NULL); |
45354f1e MP |
563 | if (unlikely(r)) |
564 | b->end_io(b, errno_to_blk_status(r)); | |
95d402f0 MP |
565 | } |
566 | ||
45354f1e | 567 | static void bio_complete(struct bio *bio) |
445559cd | 568 | { |
45354f1e | 569 | struct dm_buffer *b = bio->bi_private; |
4e4cbee9 | 570 | blk_status_t status = bio->bi_status; |
45354f1e MP |
571 | bio_put(bio); |
572 | b->end_io(b, status); | |
445559cd DW |
573 | } |
574 | ||
45354f1e MP |
575 | static void use_bio(struct dm_buffer *b, int rw, sector_t sector, |
576 | unsigned n_sectors, unsigned offset) | |
95d402f0 | 577 | { |
45354f1e | 578 | struct bio *bio; |
95d402f0 | 579 | char *ptr; |
45354f1e | 580 | unsigned vec_size, len; |
95d402f0 | 581 | |
45354f1e MP |
582 | vec_size = b->c->block_size >> PAGE_SHIFT; |
583 | if (unlikely(b->c->sectors_per_block_bits < PAGE_SHIFT - SECTOR_SHIFT)) | |
584 | vec_size += 2; | |
585 | ||
586 | bio = bio_kmalloc(GFP_NOWAIT | __GFP_NORETRY | __GFP_NOWARN, vec_size); | |
587 | if (!bio) { | |
588 | dmio: | |
589 | use_dmio(b, rw, sector, n_sectors, offset); | |
590 | return; | |
591 | } | |
592 | ||
593 | bio->bi_iter.bi_sector = sector; | |
594 | bio_set_dev(bio, b->c->bdev); | |
595 | bio_set_op_attrs(bio, rw, 0); | |
596 | bio->bi_end_io = bio_complete; | |
597 | bio->bi_private = b; | |
95d402f0 | 598 | |
1e3b21c6 | 599 | ptr = (char *)b->data + offset; |
400a0bef | 600 | len = n_sectors << SECTOR_SHIFT; |
95d402f0 | 601 | |
95d402f0 | 602 | do { |
1e3b21c6 | 603 | unsigned this_step = min((unsigned)(PAGE_SIZE - offset_in_page(ptr)), len); |
45354f1e | 604 | if (!bio_add_page(bio, virt_to_page(ptr), this_step, |
756d097b | 605 | offset_in_page(ptr))) { |
45354f1e MP |
606 | bio_put(bio); |
607 | goto dmio; | |
95d402f0 MP |
608 | } |
609 | ||
1e3b21c6 MP |
610 | len -= this_step; |
611 | ptr += this_step; | |
95d402f0 MP |
612 | } while (len > 0); |
613 | ||
45354f1e | 614 | submit_bio(bio); |
95d402f0 MP |
615 | } |
616 | ||
45354f1e | 617 | static void submit_io(struct dm_buffer *b, int rw, void (*end_io)(struct dm_buffer *, blk_status_t)) |
95d402f0 | 618 | { |
400a0bef MP |
619 | unsigned n_sectors; |
620 | sector_t sector; | |
1e3b21c6 | 621 | unsigned offset, end; |
95d402f0 | 622 | |
45354f1e MP |
623 | b->end_io = end_io; |
624 | ||
f51f2e0a MP |
625 | if (likely(b->c->sectors_per_block_bits >= 0)) |
626 | sector = b->block << b->c->sectors_per_block_bits; | |
627 | else | |
628 | sector = b->block * (b->c->block_size >> SECTOR_SHIFT); | |
629 | sector += b->c->start; | |
1e3b21c6 | 630 | |
905be0a1 | 631 | if (rw != REQ_OP_WRITE) { |
f51f2e0a | 632 | n_sectors = b->c->block_size >> SECTOR_SHIFT; |
1e3b21c6 MP |
633 | offset = 0; |
634 | } else { | |
635 | if (b->c->write_callback) | |
636 | b->c->write_callback(b); | |
637 | offset = b->write_start; | |
638 | end = b->write_end; | |
639 | offset &= -DM_BUFIO_WRITE_ALIGN; | |
640 | end += DM_BUFIO_WRITE_ALIGN - 1; | |
641 | end &= -DM_BUFIO_WRITE_ALIGN; | |
642 | if (unlikely(end > b->c->block_size)) | |
643 | end = b->c->block_size; | |
644 | ||
645 | sector += offset >> SECTOR_SHIFT; | |
646 | n_sectors = (end - offset) >> SECTOR_SHIFT; | |
647 | } | |
400a0bef | 648 | |
45354f1e MP |
649 | if (b->data_mode != DATA_MODE_VMALLOC) |
650 | use_bio(b, rw, sector, n_sectors, offset); | |
95d402f0 | 651 | else |
45354f1e | 652 | use_dmio(b, rw, sector, n_sectors, offset); |
95d402f0 MP |
653 | } |
654 | ||
655 | /*---------------------------------------------------------------- | |
656 | * Writing dirty buffers | |
657 | *--------------------------------------------------------------*/ | |
658 | ||
659 | /* | |
660 | * The endio routine for write. | |
661 | * | |
662 | * Set the error, clear B_WRITING bit and wake anyone who was waiting on | |
663 | * it. | |
664 | */ | |
45354f1e | 665 | static void write_endio(struct dm_buffer *b, blk_status_t status) |
95d402f0 | 666 | { |
45354f1e MP |
667 | b->write_error = status; |
668 | if (unlikely(status)) { | |
95d402f0 | 669 | struct dm_bufio_client *c = b->c; |
4e4cbee9 CH |
670 | |
671 | (void)cmpxchg(&c->async_write_error, 0, | |
45354f1e | 672 | blk_status_to_errno(status)); |
95d402f0 MP |
673 | } |
674 | ||
675 | BUG_ON(!test_bit(B_WRITING, &b->state)); | |
676 | ||
4e857c58 | 677 | smp_mb__before_atomic(); |
95d402f0 | 678 | clear_bit(B_WRITING, &b->state); |
4e857c58 | 679 | smp_mb__after_atomic(); |
95d402f0 MP |
680 | |
681 | wake_up_bit(&b->state, B_WRITING); | |
682 | } | |
683 | ||
95d402f0 MP |
684 | /* |
685 | * Initiate a write on a dirty buffer, but don't wait for it. | |
686 | * | |
687 | * - If the buffer is not dirty, exit. | |
688 | * - If there some previous write going on, wait for it to finish (we can't | |
689 | * have two writes on the same buffer simultaneously). | |
690 | * - Submit our write and don't wait on it. We set B_WRITING indicating | |
691 | * that there is a write in progress. | |
692 | */ | |
2480945c MP |
693 | static void __write_dirty_buffer(struct dm_buffer *b, |
694 | struct list_head *write_list) | |
95d402f0 MP |
695 | { |
696 | if (!test_bit(B_DIRTY, &b->state)) | |
697 | return; | |
698 | ||
699 | clear_bit(B_DIRTY, &b->state); | |
74316201 | 700 | wait_on_bit_lock_io(&b->state, B_WRITING, TASK_UNINTERRUPTIBLE); |
95d402f0 | 701 | |
1e3b21c6 MP |
702 | b->write_start = b->dirty_start; |
703 | b->write_end = b->dirty_end; | |
704 | ||
2480945c | 705 | if (!write_list) |
905be0a1 | 706 | submit_io(b, REQ_OP_WRITE, write_endio); |
2480945c MP |
707 | else |
708 | list_add_tail(&b->write_list, write_list); | |
709 | } | |
710 | ||
711 | static void __flush_write_list(struct list_head *write_list) | |
712 | { | |
713 | struct blk_plug plug; | |
714 | blk_start_plug(&plug); | |
715 | while (!list_empty(write_list)) { | |
716 | struct dm_buffer *b = | |
717 | list_entry(write_list->next, struct dm_buffer, write_list); | |
718 | list_del(&b->write_list); | |
905be0a1 | 719 | submit_io(b, REQ_OP_WRITE, write_endio); |
7cd32674 | 720 | cond_resched(); |
2480945c MP |
721 | } |
722 | blk_finish_plug(&plug); | |
95d402f0 MP |
723 | } |
724 | ||
725 | /* | |
726 | * Wait until any activity on the buffer finishes. Possibly write the | |
727 | * buffer if it is dirty. When this function finishes, there is no I/O | |
728 | * running on the buffer and the buffer is not dirty. | |
729 | */ | |
730 | static void __make_buffer_clean(struct dm_buffer *b) | |
731 | { | |
732 | BUG_ON(b->hold_count); | |
733 | ||
734 | if (!b->state) /* fast case */ | |
735 | return; | |
736 | ||
74316201 | 737 | wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE); |
2480945c | 738 | __write_dirty_buffer(b, NULL); |
74316201 | 739 | wait_on_bit_io(&b->state, B_WRITING, TASK_UNINTERRUPTIBLE); |
95d402f0 MP |
740 | } |
741 | ||
742 | /* | |
743 | * Find some buffer that is not held by anybody, clean it, unlink it and | |
744 | * return it. | |
745 | */ | |
746 | static struct dm_buffer *__get_unclaimed_buffer(struct dm_bufio_client *c) | |
747 | { | |
748 | struct dm_buffer *b; | |
749 | ||
750 | list_for_each_entry_reverse(b, &c->lru[LIST_CLEAN], lru_list) { | |
751 | BUG_ON(test_bit(B_WRITING, &b->state)); | |
752 | BUG_ON(test_bit(B_DIRTY, &b->state)); | |
753 | ||
754 | if (!b->hold_count) { | |
755 | __make_buffer_clean(b); | |
756 | __unlink_buffer(b); | |
757 | return b; | |
758 | } | |
7cd32674 | 759 | cond_resched(); |
95d402f0 MP |
760 | } |
761 | ||
762 | list_for_each_entry_reverse(b, &c->lru[LIST_DIRTY], lru_list) { | |
763 | BUG_ON(test_bit(B_READING, &b->state)); | |
764 | ||
765 | if (!b->hold_count) { | |
766 | __make_buffer_clean(b); | |
767 | __unlink_buffer(b); | |
768 | return b; | |
769 | } | |
7cd32674 | 770 | cond_resched(); |
95d402f0 MP |
771 | } |
772 | ||
773 | return NULL; | |
774 | } | |
775 | ||
776 | /* | |
777 | * Wait until some other threads free some buffer or release hold count on | |
778 | * some buffer. | |
779 | * | |
780 | * This function is entered with c->lock held, drops it and regains it | |
781 | * before exiting. | |
782 | */ | |
783 | static void __wait_for_free_buffer(struct dm_bufio_client *c) | |
784 | { | |
785 | DECLARE_WAITQUEUE(wait, current); | |
786 | ||
787 | add_wait_queue(&c->free_buffer_wait, &wait); | |
642fa448 | 788 | set_current_state(TASK_UNINTERRUPTIBLE); |
95d402f0 MP |
789 | dm_bufio_unlock(c); |
790 | ||
791 | io_schedule(); | |
792 | ||
95d402f0 MP |
793 | remove_wait_queue(&c->free_buffer_wait, &wait); |
794 | ||
795 | dm_bufio_lock(c); | |
796 | } | |
797 | ||
a66cc28f MP |
798 | enum new_flag { |
799 | NF_FRESH = 0, | |
800 | NF_READ = 1, | |
801 | NF_GET = 2, | |
802 | NF_PREFETCH = 3 | |
803 | }; | |
804 | ||
95d402f0 MP |
805 | /* |
806 | * Allocate a new buffer. If the allocation is not possible, wait until | |
807 | * some other thread frees a buffer. | |
808 | * | |
809 | * May drop the lock and regain it. | |
810 | */ | |
a66cc28f | 811 | static struct dm_buffer *__alloc_buffer_wait_no_callback(struct dm_bufio_client *c, enum new_flag nf) |
95d402f0 MP |
812 | { |
813 | struct dm_buffer *b; | |
41c73a49 | 814 | bool tried_noio_alloc = false; |
95d402f0 MP |
815 | |
816 | /* | |
817 | * dm-bufio is resistant to allocation failures (it just keeps | |
818 | * one buffer reserved in cases all the allocations fail). | |
819 | * So set flags to not try too hard: | |
9ea61cac DA |
820 | * GFP_NOWAIT: don't wait; if we need to sleep we'll release our |
821 | * mutex and wait ourselves. | |
95d402f0 MP |
822 | * __GFP_NORETRY: don't retry and rather return failure |
823 | * __GFP_NOMEMALLOC: don't use emergency reserves | |
824 | * __GFP_NOWARN: don't print a warning in case of failure | |
825 | * | |
826 | * For debugging, if we set the cache size to 1, no new buffers will | |
827 | * be allocated. | |
828 | */ | |
829 | while (1) { | |
830 | if (dm_bufio_cache_size_latch != 1) { | |
9ea61cac | 831 | b = alloc_buffer(c, GFP_NOWAIT | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN); |
95d402f0 MP |
832 | if (b) |
833 | return b; | |
834 | } | |
835 | ||
a66cc28f MP |
836 | if (nf == NF_PREFETCH) |
837 | return NULL; | |
838 | ||
41c73a49 MP |
839 | if (dm_bufio_cache_size_latch != 1 && !tried_noio_alloc) { |
840 | dm_bufio_unlock(c); | |
841 | b = alloc_buffer(c, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN); | |
842 | dm_bufio_lock(c); | |
843 | if (b) | |
844 | return b; | |
845 | tried_noio_alloc = true; | |
846 | } | |
847 | ||
95d402f0 MP |
848 | if (!list_empty(&c->reserved_buffers)) { |
849 | b = list_entry(c->reserved_buffers.next, | |
850 | struct dm_buffer, lru_list); | |
851 | list_del(&b->lru_list); | |
852 | c->need_reserved_buffers++; | |
853 | ||
854 | return b; | |
855 | } | |
856 | ||
857 | b = __get_unclaimed_buffer(c); | |
858 | if (b) | |
859 | return b; | |
860 | ||
861 | __wait_for_free_buffer(c); | |
862 | } | |
863 | } | |
864 | ||
a66cc28f | 865 | static struct dm_buffer *__alloc_buffer_wait(struct dm_bufio_client *c, enum new_flag nf) |
95d402f0 | 866 | { |
a66cc28f MP |
867 | struct dm_buffer *b = __alloc_buffer_wait_no_callback(c, nf); |
868 | ||
869 | if (!b) | |
870 | return NULL; | |
95d402f0 MP |
871 | |
872 | if (c->alloc_callback) | |
873 | c->alloc_callback(b); | |
874 | ||
875 | return b; | |
876 | } | |
877 | ||
878 | /* | |
879 | * Free a buffer and wake other threads waiting for free buffers. | |
880 | */ | |
881 | static void __free_buffer_wake(struct dm_buffer *b) | |
882 | { | |
883 | struct dm_bufio_client *c = b->c; | |
884 | ||
885 | if (!c->need_reserved_buffers) | |
886 | free_buffer(b); | |
887 | else { | |
888 | list_add(&b->lru_list, &c->reserved_buffers); | |
889 | c->need_reserved_buffers--; | |
890 | } | |
891 | ||
892 | wake_up(&c->free_buffer_wait); | |
893 | } | |
894 | ||
2480945c MP |
895 | static void __write_dirty_buffers_async(struct dm_bufio_client *c, int no_wait, |
896 | struct list_head *write_list) | |
95d402f0 MP |
897 | { |
898 | struct dm_buffer *b, *tmp; | |
899 | ||
900 | list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) { | |
901 | BUG_ON(test_bit(B_READING, &b->state)); | |
902 | ||
903 | if (!test_bit(B_DIRTY, &b->state) && | |
904 | !test_bit(B_WRITING, &b->state)) { | |
905 | __relink_lru(b, LIST_CLEAN); | |
906 | continue; | |
907 | } | |
908 | ||
909 | if (no_wait && test_bit(B_WRITING, &b->state)) | |
910 | return; | |
911 | ||
2480945c | 912 | __write_dirty_buffer(b, write_list); |
7cd32674 | 913 | cond_resched(); |
95d402f0 MP |
914 | } |
915 | } | |
916 | ||
917 | /* | |
918 | * Get writeback threshold and buffer limit for a given client. | |
919 | */ | |
920 | static void __get_memory_limit(struct dm_bufio_client *c, | |
921 | unsigned long *threshold_buffers, | |
922 | unsigned long *limit_buffers) | |
923 | { | |
924 | unsigned long buffers; | |
925 | ||
6aa7de05 | 926 | if (unlikely(READ_ONCE(dm_bufio_cache_size) != dm_bufio_cache_size_latch)) { |
1b0fb5a5 MP |
927 | if (mutex_trylock(&dm_bufio_clients_lock)) { |
928 | __cache_size_refresh(); | |
929 | mutex_unlock(&dm_bufio_clients_lock); | |
930 | } | |
95d402f0 MP |
931 | } |
932 | ||
f51f2e0a MP |
933 | buffers = dm_bufio_cache_size_per_client; |
934 | if (likely(c->sectors_per_block_bits >= 0)) | |
935 | buffers >>= c->sectors_per_block_bits + SECTOR_SHIFT; | |
936 | else | |
937 | buffers /= c->block_size; | |
95d402f0 | 938 | |
55b082e6 MP |
939 | if (buffers < c->minimum_buffers) |
940 | buffers = c->minimum_buffers; | |
95d402f0 MP |
941 | |
942 | *limit_buffers = buffers; | |
74d4108d EB |
943 | *threshold_buffers = mult_frac(buffers, |
944 | DM_BUFIO_WRITEBACK_PERCENT, 100); | |
95d402f0 MP |
945 | } |
946 | ||
947 | /* | |
948 | * Check if we're over watermark. | |
949 | * If we are over threshold_buffers, start freeing buffers. | |
950 | * If we're over "limit_buffers", block until we get under the limit. | |
951 | */ | |
2480945c MP |
952 | static void __check_watermark(struct dm_bufio_client *c, |
953 | struct list_head *write_list) | |
95d402f0 MP |
954 | { |
955 | unsigned long threshold_buffers, limit_buffers; | |
956 | ||
957 | __get_memory_limit(c, &threshold_buffers, &limit_buffers); | |
958 | ||
959 | while (c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY] > | |
960 | limit_buffers) { | |
961 | ||
962 | struct dm_buffer *b = __get_unclaimed_buffer(c); | |
963 | ||
964 | if (!b) | |
965 | return; | |
966 | ||
967 | __free_buffer_wake(b); | |
7cd32674 | 968 | cond_resched(); |
95d402f0 MP |
969 | } |
970 | ||
971 | if (c->n_buffers[LIST_DIRTY] > threshold_buffers) | |
2480945c | 972 | __write_dirty_buffers_async(c, 1, write_list); |
95d402f0 MP |
973 | } |
974 | ||
95d402f0 MP |
975 | /*---------------------------------------------------------------- |
976 | * Getting a buffer | |
977 | *--------------------------------------------------------------*/ | |
978 | ||
95d402f0 | 979 | static struct dm_buffer *__bufio_new(struct dm_bufio_client *c, sector_t block, |
2480945c MP |
980 | enum new_flag nf, int *need_submit, |
981 | struct list_head *write_list) | |
95d402f0 MP |
982 | { |
983 | struct dm_buffer *b, *new_b = NULL; | |
984 | ||
985 | *need_submit = 0; | |
986 | ||
987 | b = __find(c, block); | |
a66cc28f MP |
988 | if (b) |
989 | goto found_buffer; | |
95d402f0 MP |
990 | |
991 | if (nf == NF_GET) | |
992 | return NULL; | |
993 | ||
a66cc28f MP |
994 | new_b = __alloc_buffer_wait(c, nf); |
995 | if (!new_b) | |
996 | return NULL; | |
95d402f0 MP |
997 | |
998 | /* | |
999 | * We've had a period where the mutex was unlocked, so need to | |
ef992373 | 1000 | * recheck the buffer tree. |
95d402f0 MP |
1001 | */ |
1002 | b = __find(c, block); | |
1003 | if (b) { | |
1004 | __free_buffer_wake(new_b); | |
a66cc28f | 1005 | goto found_buffer; |
95d402f0 MP |
1006 | } |
1007 | ||
2480945c | 1008 | __check_watermark(c, write_list); |
95d402f0 MP |
1009 | |
1010 | b = new_b; | |
1011 | b->hold_count = 1; | |
1012 | b->read_error = 0; | |
1013 | b->write_error = 0; | |
1014 | __link_buffer(b, block, LIST_CLEAN); | |
1015 | ||
1016 | if (nf == NF_FRESH) { | |
1017 | b->state = 0; | |
1018 | return b; | |
1019 | } | |
1020 | ||
1021 | b->state = 1 << B_READING; | |
1022 | *need_submit = 1; | |
1023 | ||
1024 | return b; | |
a66cc28f MP |
1025 | |
1026 | found_buffer: | |
1027 | if (nf == NF_PREFETCH) | |
1028 | return NULL; | |
1029 | /* | |
1030 | * Note: it is essential that we don't wait for the buffer to be | |
1031 | * read if dm_bufio_get function is used. Both dm_bufio_get and | |
1032 | * dm_bufio_prefetch can be used in the driver request routine. | |
1033 | * If the user called both dm_bufio_prefetch and dm_bufio_get on | |
1034 | * the same buffer, it would deadlock if we waited. | |
1035 | */ | |
1036 | if (nf == NF_GET && unlikely(test_bit(B_READING, &b->state))) | |
1037 | return NULL; | |
1038 | ||
1039 | b->hold_count++; | |
1040 | __relink_lru(b, test_bit(B_DIRTY, &b->state) || | |
1041 | test_bit(B_WRITING, &b->state)); | |
1042 | return b; | |
95d402f0 MP |
1043 | } |
1044 | ||
1045 | /* | |
1046 | * The endio routine for reading: set the error, clear the bit and wake up | |
1047 | * anyone waiting on the buffer. | |
1048 | */ | |
45354f1e | 1049 | static void read_endio(struct dm_buffer *b, blk_status_t status) |
95d402f0 | 1050 | { |
45354f1e | 1051 | b->read_error = status; |
95d402f0 MP |
1052 | |
1053 | BUG_ON(!test_bit(B_READING, &b->state)); | |
1054 | ||
4e857c58 | 1055 | smp_mb__before_atomic(); |
95d402f0 | 1056 | clear_bit(B_READING, &b->state); |
4e857c58 | 1057 | smp_mb__after_atomic(); |
95d402f0 MP |
1058 | |
1059 | wake_up_bit(&b->state, B_READING); | |
1060 | } | |
1061 | ||
1062 | /* | |
1063 | * A common routine for dm_bufio_new and dm_bufio_read. Operation of these | |
1064 | * functions is similar except that dm_bufio_new doesn't read the | |
1065 | * buffer from the disk (assuming that the caller overwrites all the data | |
1066 | * and uses dm_bufio_mark_buffer_dirty to write new data back). | |
1067 | */ | |
1068 | static void *new_read(struct dm_bufio_client *c, sector_t block, | |
1069 | enum new_flag nf, struct dm_buffer **bp) | |
1070 | { | |
1071 | int need_submit; | |
1072 | struct dm_buffer *b; | |
1073 | ||
2480945c MP |
1074 | LIST_HEAD(write_list); |
1075 | ||
95d402f0 | 1076 | dm_bufio_lock(c); |
2480945c | 1077 | b = __bufio_new(c, block, nf, &need_submit, &write_list); |
86bad0c7 MP |
1078 | #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING |
1079 | if (b && b->hold_count == 1) | |
1080 | buffer_record_stack(b); | |
1081 | #endif | |
95d402f0 MP |
1082 | dm_bufio_unlock(c); |
1083 | ||
2480945c MP |
1084 | __flush_write_list(&write_list); |
1085 | ||
a66cc28f | 1086 | if (!b) |
f98c8f79 | 1087 | return NULL; |
95d402f0 MP |
1088 | |
1089 | if (need_submit) | |
905be0a1 | 1090 | submit_io(b, REQ_OP_READ, read_endio); |
95d402f0 | 1091 | |
74316201 | 1092 | wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE); |
95d402f0 MP |
1093 | |
1094 | if (b->read_error) { | |
4e4cbee9 | 1095 | int error = blk_status_to_errno(b->read_error); |
95d402f0 MP |
1096 | |
1097 | dm_bufio_release(b); | |
1098 | ||
1099 | return ERR_PTR(error); | |
1100 | } | |
1101 | ||
1102 | *bp = b; | |
1103 | ||
1104 | return b->data; | |
1105 | } | |
1106 | ||
1107 | void *dm_bufio_get(struct dm_bufio_client *c, sector_t block, | |
1108 | struct dm_buffer **bp) | |
1109 | { | |
1110 | return new_read(c, block, NF_GET, bp); | |
1111 | } | |
1112 | EXPORT_SYMBOL_GPL(dm_bufio_get); | |
1113 | ||
1114 | void *dm_bufio_read(struct dm_bufio_client *c, sector_t block, | |
1115 | struct dm_buffer **bp) | |
1116 | { | |
1117 | BUG_ON(dm_bufio_in_request()); | |
1118 | ||
1119 | return new_read(c, block, NF_READ, bp); | |
1120 | } | |
1121 | EXPORT_SYMBOL_GPL(dm_bufio_read); | |
1122 | ||
1123 | void *dm_bufio_new(struct dm_bufio_client *c, sector_t block, | |
1124 | struct dm_buffer **bp) | |
1125 | { | |
1126 | BUG_ON(dm_bufio_in_request()); | |
1127 | ||
1128 | return new_read(c, block, NF_FRESH, bp); | |
1129 | } | |
1130 | EXPORT_SYMBOL_GPL(dm_bufio_new); | |
1131 | ||
a66cc28f MP |
1132 | void dm_bufio_prefetch(struct dm_bufio_client *c, |
1133 | sector_t block, unsigned n_blocks) | |
1134 | { | |
1135 | struct blk_plug plug; | |
1136 | ||
2480945c MP |
1137 | LIST_HEAD(write_list); |
1138 | ||
3b6b7813 MP |
1139 | BUG_ON(dm_bufio_in_request()); |
1140 | ||
a66cc28f MP |
1141 | blk_start_plug(&plug); |
1142 | dm_bufio_lock(c); | |
1143 | ||
1144 | for (; n_blocks--; block++) { | |
1145 | int need_submit; | |
1146 | struct dm_buffer *b; | |
2480945c MP |
1147 | b = __bufio_new(c, block, NF_PREFETCH, &need_submit, |
1148 | &write_list); | |
1149 | if (unlikely(!list_empty(&write_list))) { | |
1150 | dm_bufio_unlock(c); | |
1151 | blk_finish_plug(&plug); | |
1152 | __flush_write_list(&write_list); | |
1153 | blk_start_plug(&plug); | |
1154 | dm_bufio_lock(c); | |
1155 | } | |
a66cc28f MP |
1156 | if (unlikely(b != NULL)) { |
1157 | dm_bufio_unlock(c); | |
1158 | ||
1159 | if (need_submit) | |
905be0a1 | 1160 | submit_io(b, REQ_OP_READ, read_endio); |
a66cc28f MP |
1161 | dm_bufio_release(b); |
1162 | ||
7cd32674 | 1163 | cond_resched(); |
a66cc28f MP |
1164 | |
1165 | if (!n_blocks) | |
1166 | goto flush_plug; | |
1167 | dm_bufio_lock(c); | |
1168 | } | |
a66cc28f MP |
1169 | } |
1170 | ||
1171 | dm_bufio_unlock(c); | |
1172 | ||
1173 | flush_plug: | |
1174 | blk_finish_plug(&plug); | |
1175 | } | |
1176 | EXPORT_SYMBOL_GPL(dm_bufio_prefetch); | |
1177 | ||
95d402f0 MP |
1178 | void dm_bufio_release(struct dm_buffer *b) |
1179 | { | |
1180 | struct dm_bufio_client *c = b->c; | |
1181 | ||
1182 | dm_bufio_lock(c); | |
1183 | ||
95d402f0 MP |
1184 | BUG_ON(!b->hold_count); |
1185 | ||
1186 | b->hold_count--; | |
1187 | if (!b->hold_count) { | |
1188 | wake_up(&c->free_buffer_wait); | |
1189 | ||
1190 | /* | |
1191 | * If there were errors on the buffer, and the buffer is not | |
1192 | * to be written, free the buffer. There is no point in caching | |
1193 | * invalid buffer. | |
1194 | */ | |
1195 | if ((b->read_error || b->write_error) && | |
a66cc28f | 1196 | !test_bit(B_READING, &b->state) && |
95d402f0 MP |
1197 | !test_bit(B_WRITING, &b->state) && |
1198 | !test_bit(B_DIRTY, &b->state)) { | |
1199 | __unlink_buffer(b); | |
1200 | __free_buffer_wake(b); | |
1201 | } | |
1202 | } | |
1203 | ||
1204 | dm_bufio_unlock(c); | |
1205 | } | |
1206 | EXPORT_SYMBOL_GPL(dm_bufio_release); | |
1207 | ||
1e3b21c6 MP |
1208 | void dm_bufio_mark_partial_buffer_dirty(struct dm_buffer *b, |
1209 | unsigned start, unsigned end) | |
95d402f0 MP |
1210 | { |
1211 | struct dm_bufio_client *c = b->c; | |
1212 | ||
1e3b21c6 MP |
1213 | BUG_ON(start >= end); |
1214 | BUG_ON(end > b->c->block_size); | |
1215 | ||
95d402f0 MP |
1216 | dm_bufio_lock(c); |
1217 | ||
a66cc28f MP |
1218 | BUG_ON(test_bit(B_READING, &b->state)); |
1219 | ||
1e3b21c6 MP |
1220 | if (!test_and_set_bit(B_DIRTY, &b->state)) { |
1221 | b->dirty_start = start; | |
1222 | b->dirty_end = end; | |
95d402f0 | 1223 | __relink_lru(b, LIST_DIRTY); |
1e3b21c6 MP |
1224 | } else { |
1225 | if (start < b->dirty_start) | |
1226 | b->dirty_start = start; | |
1227 | if (end > b->dirty_end) | |
1228 | b->dirty_end = end; | |
1229 | } | |
95d402f0 MP |
1230 | |
1231 | dm_bufio_unlock(c); | |
1232 | } | |
1e3b21c6 MP |
1233 | EXPORT_SYMBOL_GPL(dm_bufio_mark_partial_buffer_dirty); |
1234 | ||
1235 | void dm_bufio_mark_buffer_dirty(struct dm_buffer *b) | |
1236 | { | |
1237 | dm_bufio_mark_partial_buffer_dirty(b, 0, b->c->block_size); | |
1238 | } | |
95d402f0 MP |
1239 | EXPORT_SYMBOL_GPL(dm_bufio_mark_buffer_dirty); |
1240 | ||
1241 | void dm_bufio_write_dirty_buffers_async(struct dm_bufio_client *c) | |
1242 | { | |
2480945c MP |
1243 | LIST_HEAD(write_list); |
1244 | ||
95d402f0 MP |
1245 | BUG_ON(dm_bufio_in_request()); |
1246 | ||
1247 | dm_bufio_lock(c); | |
2480945c | 1248 | __write_dirty_buffers_async(c, 0, &write_list); |
95d402f0 | 1249 | dm_bufio_unlock(c); |
2480945c | 1250 | __flush_write_list(&write_list); |
95d402f0 MP |
1251 | } |
1252 | EXPORT_SYMBOL_GPL(dm_bufio_write_dirty_buffers_async); | |
1253 | ||
1254 | /* | |
1255 | * For performance, it is essential that the buffers are written asynchronously | |
1256 | * and simultaneously (so that the block layer can merge the writes) and then | |
1257 | * waited upon. | |
1258 | * | |
1259 | * Finally, we flush hardware disk cache. | |
1260 | */ | |
1261 | int dm_bufio_write_dirty_buffers(struct dm_bufio_client *c) | |
1262 | { | |
edc11d49 | 1263 | int a, f; |
95d402f0 MP |
1264 | unsigned long buffers_processed = 0; |
1265 | struct dm_buffer *b, *tmp; | |
1266 | ||
2480945c MP |
1267 | LIST_HEAD(write_list); |
1268 | ||
1269 | dm_bufio_lock(c); | |
1270 | __write_dirty_buffers_async(c, 0, &write_list); | |
1271 | dm_bufio_unlock(c); | |
1272 | __flush_write_list(&write_list); | |
95d402f0 | 1273 | dm_bufio_lock(c); |
95d402f0 MP |
1274 | |
1275 | again: | |
1276 | list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) { | |
1277 | int dropped_lock = 0; | |
1278 | ||
1279 | if (buffers_processed < c->n_buffers[LIST_DIRTY]) | |
1280 | buffers_processed++; | |
1281 | ||
1282 | BUG_ON(test_bit(B_READING, &b->state)); | |
1283 | ||
1284 | if (test_bit(B_WRITING, &b->state)) { | |
1285 | if (buffers_processed < c->n_buffers[LIST_DIRTY]) { | |
1286 | dropped_lock = 1; | |
1287 | b->hold_count++; | |
1288 | dm_bufio_unlock(c); | |
74316201 N |
1289 | wait_on_bit_io(&b->state, B_WRITING, |
1290 | TASK_UNINTERRUPTIBLE); | |
95d402f0 MP |
1291 | dm_bufio_lock(c); |
1292 | b->hold_count--; | |
1293 | } else | |
74316201 N |
1294 | wait_on_bit_io(&b->state, B_WRITING, |
1295 | TASK_UNINTERRUPTIBLE); | |
95d402f0 MP |
1296 | } |
1297 | ||
1298 | if (!test_bit(B_DIRTY, &b->state) && | |
1299 | !test_bit(B_WRITING, &b->state)) | |
1300 | __relink_lru(b, LIST_CLEAN); | |
1301 | ||
7cd32674 | 1302 | cond_resched(); |
95d402f0 MP |
1303 | |
1304 | /* | |
1305 | * If we dropped the lock, the list is no longer consistent, | |
1306 | * so we must restart the search. | |
1307 | * | |
1308 | * In the most common case, the buffer just processed is | |
1309 | * relinked to the clean list, so we won't loop scanning the | |
1310 | * same buffer again and again. | |
1311 | * | |
1312 | * This may livelock if there is another thread simultaneously | |
1313 | * dirtying buffers, so we count the number of buffers walked | |
1314 | * and if it exceeds the total number of buffers, it means that | |
1315 | * someone is doing some writes simultaneously with us. In | |
1316 | * this case, stop, dropping the lock. | |
1317 | */ | |
1318 | if (dropped_lock) | |
1319 | goto again; | |
1320 | } | |
1321 | wake_up(&c->free_buffer_wait); | |
1322 | dm_bufio_unlock(c); | |
1323 | ||
1324 | a = xchg(&c->async_write_error, 0); | |
1325 | f = dm_bufio_issue_flush(c); | |
1326 | if (a) | |
1327 | return a; | |
1328 | ||
1329 | return f; | |
1330 | } | |
1331 | EXPORT_SYMBOL_GPL(dm_bufio_write_dirty_buffers); | |
1332 | ||
1333 | /* | |
ef992373 | 1334 | * Use dm-io to send an empty barrier to flush the device. |
95d402f0 MP |
1335 | */ |
1336 | int dm_bufio_issue_flush(struct dm_bufio_client *c) | |
1337 | { | |
1338 | struct dm_io_request io_req = { | |
e6047149 | 1339 | .bi_op = REQ_OP_WRITE, |
ff0361b3 | 1340 | .bi_op_flags = REQ_PREFLUSH | REQ_SYNC, |
95d402f0 MP |
1341 | .mem.type = DM_IO_KMEM, |
1342 | .mem.ptr.addr = NULL, | |
1343 | .client = c->dm_io, | |
1344 | }; | |
1345 | struct dm_io_region io_reg = { | |
1346 | .bdev = c->bdev, | |
1347 | .sector = 0, | |
1348 | .count = 0, | |
1349 | }; | |
1350 | ||
1351 | BUG_ON(dm_bufio_in_request()); | |
1352 | ||
1353 | return dm_io(&io_req, 1, &io_reg, NULL); | |
1354 | } | |
1355 | EXPORT_SYMBOL_GPL(dm_bufio_issue_flush); | |
1356 | ||
1357 | /* | |
1358 | * We first delete any other buffer that may be at that new location. | |
1359 | * | |
1360 | * Then, we write the buffer to the original location if it was dirty. | |
1361 | * | |
1362 | * Then, if we are the only one who is holding the buffer, relink the buffer | |
ef992373 | 1363 | * in the buffer tree for the new location. |
95d402f0 MP |
1364 | * |
1365 | * If there was someone else holding the buffer, we write it to the new | |
1366 | * location but not relink it, because that other user needs to have the buffer | |
1367 | * at the same place. | |
1368 | */ | |
1369 | void dm_bufio_release_move(struct dm_buffer *b, sector_t new_block) | |
1370 | { | |
1371 | struct dm_bufio_client *c = b->c; | |
1372 | struct dm_buffer *new; | |
1373 | ||
1374 | BUG_ON(dm_bufio_in_request()); | |
1375 | ||
1376 | dm_bufio_lock(c); | |
1377 | ||
1378 | retry: | |
1379 | new = __find(c, new_block); | |
1380 | if (new) { | |
1381 | if (new->hold_count) { | |
1382 | __wait_for_free_buffer(c); | |
1383 | goto retry; | |
1384 | } | |
1385 | ||
1386 | /* | |
1387 | * FIXME: Is there any point waiting for a write that's going | |
1388 | * to be overwritten in a bit? | |
1389 | */ | |
1390 | __make_buffer_clean(new); | |
1391 | __unlink_buffer(new); | |
1392 | __free_buffer_wake(new); | |
1393 | } | |
1394 | ||
1395 | BUG_ON(!b->hold_count); | |
1396 | BUG_ON(test_bit(B_READING, &b->state)); | |
1397 | ||
2480945c | 1398 | __write_dirty_buffer(b, NULL); |
95d402f0 | 1399 | if (b->hold_count == 1) { |
74316201 N |
1400 | wait_on_bit_io(&b->state, B_WRITING, |
1401 | TASK_UNINTERRUPTIBLE); | |
95d402f0 | 1402 | set_bit(B_DIRTY, &b->state); |
1e3b21c6 MP |
1403 | b->dirty_start = 0; |
1404 | b->dirty_end = c->block_size; | |
95d402f0 MP |
1405 | __unlink_buffer(b); |
1406 | __link_buffer(b, new_block, LIST_DIRTY); | |
1407 | } else { | |
1408 | sector_t old_block; | |
74316201 N |
1409 | wait_on_bit_lock_io(&b->state, B_WRITING, |
1410 | TASK_UNINTERRUPTIBLE); | |
95d402f0 MP |
1411 | /* |
1412 | * Relink buffer to "new_block" so that write_callback | |
1413 | * sees "new_block" as a block number. | |
1414 | * After the write, link the buffer back to old_block. | |
1415 | * All this must be done in bufio lock, so that block number | |
1416 | * change isn't visible to other threads. | |
1417 | */ | |
1418 | old_block = b->block; | |
1419 | __unlink_buffer(b); | |
1420 | __link_buffer(b, new_block, b->list_mode); | |
905be0a1 | 1421 | submit_io(b, REQ_OP_WRITE, write_endio); |
74316201 N |
1422 | wait_on_bit_io(&b->state, B_WRITING, |
1423 | TASK_UNINTERRUPTIBLE); | |
95d402f0 MP |
1424 | __unlink_buffer(b); |
1425 | __link_buffer(b, old_block, b->list_mode); | |
1426 | } | |
1427 | ||
1428 | dm_bufio_unlock(c); | |
1429 | dm_bufio_release(b); | |
1430 | } | |
1431 | EXPORT_SYMBOL_GPL(dm_bufio_release_move); | |
1432 | ||
55494bf2 MP |
1433 | /* |
1434 | * Free the given buffer. | |
1435 | * | |
1436 | * This is just a hint, if the buffer is in use or dirty, this function | |
1437 | * does nothing. | |
1438 | */ | |
1439 | void dm_bufio_forget(struct dm_bufio_client *c, sector_t block) | |
1440 | { | |
1441 | struct dm_buffer *b; | |
1442 | ||
1443 | dm_bufio_lock(c); | |
1444 | ||
1445 | b = __find(c, block); | |
1446 | if (b && likely(!b->hold_count) && likely(!b->state)) { | |
1447 | __unlink_buffer(b); | |
1448 | __free_buffer_wake(b); | |
1449 | } | |
1450 | ||
1451 | dm_bufio_unlock(c); | |
1452 | } | |
afa53df8 | 1453 | EXPORT_SYMBOL_GPL(dm_bufio_forget); |
55494bf2 | 1454 | |
55b082e6 MP |
1455 | void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned n) |
1456 | { | |
1457 | c->minimum_buffers = n; | |
1458 | } | |
afa53df8 | 1459 | EXPORT_SYMBOL_GPL(dm_bufio_set_minimum_buffers); |
55b082e6 | 1460 | |
95d402f0 MP |
1461 | unsigned dm_bufio_get_block_size(struct dm_bufio_client *c) |
1462 | { | |
1463 | return c->block_size; | |
1464 | } | |
1465 | EXPORT_SYMBOL_GPL(dm_bufio_get_block_size); | |
1466 | ||
1467 | sector_t dm_bufio_get_device_size(struct dm_bufio_client *c) | |
1468 | { | |
f51f2e0a MP |
1469 | sector_t s = i_size_read(c->bdev->bd_inode) >> SECTOR_SHIFT; |
1470 | if (likely(c->sectors_per_block_bits >= 0)) | |
1471 | s >>= c->sectors_per_block_bits; | |
1472 | else | |
1473 | sector_div(s, c->block_size >> SECTOR_SHIFT); | |
1474 | return s; | |
95d402f0 MP |
1475 | } |
1476 | EXPORT_SYMBOL_GPL(dm_bufio_get_device_size); | |
1477 | ||
1478 | sector_t dm_bufio_get_block_number(struct dm_buffer *b) | |
1479 | { | |
1480 | return b->block; | |
1481 | } | |
1482 | EXPORT_SYMBOL_GPL(dm_bufio_get_block_number); | |
1483 | ||
1484 | void *dm_bufio_get_block_data(struct dm_buffer *b) | |
1485 | { | |
1486 | return b->data; | |
1487 | } | |
1488 | EXPORT_SYMBOL_GPL(dm_bufio_get_block_data); | |
1489 | ||
1490 | void *dm_bufio_get_aux_data(struct dm_buffer *b) | |
1491 | { | |
1492 | return b + 1; | |
1493 | } | |
1494 | EXPORT_SYMBOL_GPL(dm_bufio_get_aux_data); | |
1495 | ||
1496 | struct dm_bufio_client *dm_bufio_get_client(struct dm_buffer *b) | |
1497 | { | |
1498 | return b->c; | |
1499 | } | |
1500 | EXPORT_SYMBOL_GPL(dm_bufio_get_client); | |
1501 | ||
1502 | static void drop_buffers(struct dm_bufio_client *c) | |
1503 | { | |
1504 | struct dm_buffer *b; | |
1505 | int i; | |
86bad0c7 | 1506 | bool warned = false; |
95d402f0 MP |
1507 | |
1508 | BUG_ON(dm_bufio_in_request()); | |
1509 | ||
1510 | /* | |
1511 | * An optimization so that the buffers are not written one-by-one. | |
1512 | */ | |
1513 | dm_bufio_write_dirty_buffers_async(c); | |
1514 | ||
1515 | dm_bufio_lock(c); | |
1516 | ||
1517 | while ((b = __get_unclaimed_buffer(c))) | |
1518 | __free_buffer_wake(b); | |
1519 | ||
1520 | for (i = 0; i < LIST_SIZE; i++) | |
86bad0c7 MP |
1521 | list_for_each_entry(b, &c->lru[i], lru_list) { |
1522 | WARN_ON(!warned); | |
1523 | warned = true; | |
95d402f0 MP |
1524 | DMERR("leaked buffer %llx, hold count %u, list %d", |
1525 | (unsigned long long)b->block, b->hold_count, i); | |
86bad0c7 | 1526 | #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING |
741b58f3 TG |
1527 | stack_trace_print(b->stack_entries, b->stack_len, 1); |
1528 | /* mark unclaimed to avoid BUG_ON below */ | |
1529 | b->hold_count = 0; | |
86bad0c7 MP |
1530 | #endif |
1531 | } | |
1532 | ||
1533 | #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING | |
1534 | while ((b = __get_unclaimed_buffer(c))) | |
1535 | __free_buffer_wake(b); | |
1536 | #endif | |
95d402f0 MP |
1537 | |
1538 | for (i = 0; i < LIST_SIZE; i++) | |
1539 | BUG_ON(!list_empty(&c->lru[i])); | |
1540 | ||
1541 | dm_bufio_unlock(c); | |
1542 | } | |
1543 | ||
1544 | /* | |
33096a78 JT |
1545 | * We may not be able to evict this buffer if IO pending or the client |
1546 | * is still using it. Caller is expected to know buffer is too old. | |
1547 | * | |
9d28eb12 MP |
1548 | * And if GFP_NOFS is used, we must not do any I/O because we hold |
1549 | * dm_bufio_clients_lock and we would risk deadlock if the I/O gets | |
1550 | * rerouted to different bufio client. | |
95d402f0 | 1551 | */ |
33096a78 | 1552 | static bool __try_evict_buffer(struct dm_buffer *b, gfp_t gfp) |
95d402f0 | 1553 | { |
9d28eb12 | 1554 | if (!(gfp & __GFP_FS)) { |
95d402f0 MP |
1555 | if (test_bit(B_READING, &b->state) || |
1556 | test_bit(B_WRITING, &b->state) || | |
1557 | test_bit(B_DIRTY, &b->state)) | |
33096a78 | 1558 | return false; |
95d402f0 MP |
1559 | } |
1560 | ||
1561 | if (b->hold_count) | |
33096a78 | 1562 | return false; |
95d402f0 MP |
1563 | |
1564 | __make_buffer_clean(b); | |
1565 | __unlink_buffer(b); | |
1566 | __free_buffer_wake(b); | |
1567 | ||
33096a78 | 1568 | return true; |
95d402f0 MP |
1569 | } |
1570 | ||
13840d38 | 1571 | static unsigned long get_retain_buffers(struct dm_bufio_client *c) |
33096a78 | 1572 | { |
f51f2e0a MP |
1573 | unsigned long retain_bytes = READ_ONCE(dm_bufio_retain_bytes); |
1574 | if (likely(c->sectors_per_block_bits >= 0)) | |
1575 | retain_bytes >>= c->sectors_per_block_bits + SECTOR_SHIFT; | |
1576 | else | |
1577 | retain_bytes /= c->block_size; | |
1578 | return retain_bytes; | |
33096a78 JT |
1579 | } |
1580 | ||
1581 | static unsigned long __scan(struct dm_bufio_client *c, unsigned long nr_to_scan, | |
1582 | gfp_t gfp_mask) | |
95d402f0 MP |
1583 | { |
1584 | int l; | |
1585 | struct dm_buffer *b, *tmp; | |
33096a78 | 1586 | unsigned long freed = 0; |
fbc7c07e SB |
1587 | unsigned long count = c->n_buffers[LIST_CLEAN] + |
1588 | c->n_buffers[LIST_DIRTY]; | |
13840d38 | 1589 | unsigned long retain_target = get_retain_buffers(c); |
95d402f0 MP |
1590 | |
1591 | for (l = 0; l < LIST_SIZE; l++) { | |
7dc19d5a | 1592 | list_for_each_entry_safe_reverse(b, tmp, &c->lru[l], lru_list) { |
33096a78 JT |
1593 | if (__try_evict_buffer(b, gfp_mask)) |
1594 | freed++; | |
1595 | if (!--nr_to_scan || ((count - freed) <= retain_target)) | |
0e825862 | 1596 | return freed; |
7cd32674 | 1597 | cond_resched(); |
7dc19d5a | 1598 | } |
95d402f0 | 1599 | } |
7dc19d5a | 1600 | return freed; |
95d402f0 MP |
1601 | } |
1602 | ||
7dc19d5a DC |
1603 | static unsigned long |
1604 | dm_bufio_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) | |
95d402f0 | 1605 | { |
7dc19d5a DC |
1606 | struct dm_bufio_client *c; |
1607 | unsigned long freed; | |
95d402f0 | 1608 | |
7dc19d5a | 1609 | c = container_of(shrink, struct dm_bufio_client, shrinker); |
cf3591ef MP |
1610 | if (sc->gfp_mask & __GFP_FS) |
1611 | dm_bufio_lock(c); | |
1612 | else if (!dm_bufio_trylock(c)) | |
7dc19d5a | 1613 | return SHRINK_STOP; |
95d402f0 | 1614 | |
7dc19d5a DC |
1615 | freed = __scan(c, sc->nr_to_scan, sc->gfp_mask); |
1616 | dm_bufio_unlock(c); | |
1617 | return freed; | |
1618 | } | |
95d402f0 | 1619 | |
7dc19d5a DC |
1620 | static unsigned long |
1621 | dm_bufio_shrink_count(struct shrinker *shrink, struct shrink_control *sc) | |
1622 | { | |
d12067f4 | 1623 | struct dm_bufio_client *c = container_of(shrink, struct dm_bufio_client, shrinker); |
fbc7c07e SB |
1624 | unsigned long count = READ_ONCE(c->n_buffers[LIST_CLEAN]) + |
1625 | READ_ONCE(c->n_buffers[LIST_DIRTY]); | |
1626 | unsigned long retain_target = get_retain_buffers(c); | |
95d402f0 | 1627 | |
fbc7c07e | 1628 | return (count < retain_target) ? 0 : (count - retain_target); |
95d402f0 MP |
1629 | } |
1630 | ||
1631 | /* | |
1632 | * Create the buffering interface | |
1633 | */ | |
1634 | struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsigned block_size, | |
1635 | unsigned reserved_buffers, unsigned aux_size, | |
1636 | void (*alloc_callback)(struct dm_buffer *), | |
1637 | void (*write_callback)(struct dm_buffer *)) | |
1638 | { | |
1639 | int r; | |
1640 | struct dm_bufio_client *c; | |
1641 | unsigned i; | |
359dbf19 | 1642 | char slab_name[27]; |
95d402f0 | 1643 | |
f51f2e0a MP |
1644 | if (!block_size || block_size & ((1 << SECTOR_SHIFT) - 1)) { |
1645 | DMERR("%s: block size not specified or is not multiple of 512b", __func__); | |
1646 | r = -EINVAL; | |
1647 | goto bad_client; | |
1648 | } | |
95d402f0 | 1649 | |
d8c712ea | 1650 | c = kzalloc(sizeof(*c), GFP_KERNEL); |
95d402f0 MP |
1651 | if (!c) { |
1652 | r = -ENOMEM; | |
1653 | goto bad_client; | |
1654 | } | |
4e420c45 | 1655 | c->buffer_tree = RB_ROOT; |
95d402f0 MP |
1656 | |
1657 | c->bdev = bdev; | |
1658 | c->block_size = block_size; | |
f51f2e0a MP |
1659 | if (is_power_of_2(block_size)) |
1660 | c->sectors_per_block_bits = __ffs(block_size) - SECTOR_SHIFT; | |
1661 | else | |
1662 | c->sectors_per_block_bits = -1; | |
95d402f0 | 1663 | |
95d402f0 MP |
1664 | c->alloc_callback = alloc_callback; |
1665 | c->write_callback = write_callback; | |
1666 | ||
1667 | for (i = 0; i < LIST_SIZE; i++) { | |
1668 | INIT_LIST_HEAD(&c->lru[i]); | |
1669 | c->n_buffers[i] = 0; | |
1670 | } | |
1671 | ||
95d402f0 MP |
1672 | mutex_init(&c->lock); |
1673 | INIT_LIST_HEAD(&c->reserved_buffers); | |
1674 | c->need_reserved_buffers = reserved_buffers; | |
1675 | ||
afa53df8 | 1676 | dm_bufio_set_minimum_buffers(c, DM_BUFIO_MIN_BUFFERS); |
55b082e6 | 1677 | |
95d402f0 MP |
1678 | init_waitqueue_head(&c->free_buffer_wait); |
1679 | c->async_write_error = 0; | |
1680 | ||
1681 | c->dm_io = dm_io_client_create(); | |
1682 | if (IS_ERR(c->dm_io)) { | |
1683 | r = PTR_ERR(c->dm_io); | |
1684 | goto bad_dm_io; | |
1685 | } | |
1686 | ||
f51f2e0a MP |
1687 | if (block_size <= KMALLOC_MAX_SIZE && |
1688 | (block_size < PAGE_SIZE || !is_power_of_2(block_size))) { | |
f7879b4c MP |
1689 | unsigned align = min(1U << __ffs(block_size), (unsigned)PAGE_SIZE); |
1690 | snprintf(slab_name, sizeof slab_name, "dm_bufio_cache-%u", block_size); | |
1691 | c->slab_cache = kmem_cache_create(slab_name, block_size, align, | |
6b5e718c | 1692 | SLAB_RECLAIM_ACCOUNT, NULL); |
21bb1327 MP |
1693 | if (!c->slab_cache) { |
1694 | r = -ENOMEM; | |
1695 | goto bad; | |
95d402f0 MP |
1696 | } |
1697 | } | |
359dbf19 MP |
1698 | if (aux_size) |
1699 | snprintf(slab_name, sizeof slab_name, "dm_bufio_buffer-%u", aux_size); | |
1700 | else | |
1701 | snprintf(slab_name, sizeof slab_name, "dm_bufio_buffer"); | |
1702 | c->slab_buffer = kmem_cache_create(slab_name, sizeof(struct dm_buffer) + aux_size, | |
1703 | 0, SLAB_RECLAIM_ACCOUNT, NULL); | |
1704 | if (!c->slab_buffer) { | |
1705 | r = -ENOMEM; | |
1706 | goto bad; | |
1707 | } | |
95d402f0 MP |
1708 | |
1709 | while (c->need_reserved_buffers) { | |
1710 | struct dm_buffer *b = alloc_buffer(c, GFP_KERNEL); | |
1711 | ||
1712 | if (!b) { | |
1713 | r = -ENOMEM; | |
0e696d38 | 1714 | goto bad; |
95d402f0 MP |
1715 | } |
1716 | __free_buffer_wake(b); | |
1717 | } | |
1718 | ||
46898e9a AK |
1719 | c->shrinker.count_objects = dm_bufio_shrink_count; |
1720 | c->shrinker.scan_objects = dm_bufio_shrink_scan; | |
1721 | c->shrinker.seeks = 1; | |
1722 | c->shrinker.batch = 0; | |
1723 | r = register_shrinker(&c->shrinker); | |
1724 | if (r) | |
0e696d38 | 1725 | goto bad; |
46898e9a | 1726 | |
95d402f0 MP |
1727 | mutex_lock(&dm_bufio_clients_lock); |
1728 | dm_bufio_client_count++; | |
1729 | list_add(&c->client_list, &dm_bufio_all_clients); | |
1730 | __cache_size_refresh(); | |
1731 | mutex_unlock(&dm_bufio_clients_lock); | |
1732 | ||
95d402f0 MP |
1733 | return c; |
1734 | ||
0e696d38 | 1735 | bad: |
95d402f0 MP |
1736 | while (!list_empty(&c->reserved_buffers)) { |
1737 | struct dm_buffer *b = list_entry(c->reserved_buffers.next, | |
1738 | struct dm_buffer, lru_list); | |
1739 | list_del(&b->lru_list); | |
1740 | free_buffer(b); | |
1741 | } | |
21bb1327 | 1742 | kmem_cache_destroy(c->slab_cache); |
359dbf19 | 1743 | kmem_cache_destroy(c->slab_buffer); |
95d402f0 MP |
1744 | dm_io_client_destroy(c->dm_io); |
1745 | bad_dm_io: | |
bde14184 | 1746 | mutex_destroy(&c->lock); |
95d402f0 MP |
1747 | kfree(c); |
1748 | bad_client: | |
1749 | return ERR_PTR(r); | |
1750 | } | |
1751 | EXPORT_SYMBOL_GPL(dm_bufio_client_create); | |
1752 | ||
1753 | /* | |
1754 | * Free the buffering interface. | |
1755 | * It is required that there are no references on any buffers. | |
1756 | */ | |
1757 | void dm_bufio_client_destroy(struct dm_bufio_client *c) | |
1758 | { | |
1759 | unsigned i; | |
1760 | ||
1761 | drop_buffers(c); | |
1762 | ||
1763 | unregister_shrinker(&c->shrinker); | |
1764 | ||
1765 | mutex_lock(&dm_bufio_clients_lock); | |
1766 | ||
1767 | list_del(&c->client_list); | |
1768 | dm_bufio_client_count--; | |
1769 | __cache_size_refresh(); | |
1770 | ||
1771 | mutex_unlock(&dm_bufio_clients_lock); | |
1772 | ||
4e420c45 | 1773 | BUG_ON(!RB_EMPTY_ROOT(&c->buffer_tree)); |
95d402f0 MP |
1774 | BUG_ON(c->need_reserved_buffers); |
1775 | ||
1776 | while (!list_empty(&c->reserved_buffers)) { | |
1777 | struct dm_buffer *b = list_entry(c->reserved_buffers.next, | |
1778 | struct dm_buffer, lru_list); | |
1779 | list_del(&b->lru_list); | |
1780 | free_buffer(b); | |
1781 | } | |
1782 | ||
1783 | for (i = 0; i < LIST_SIZE; i++) | |
1784 | if (c->n_buffers[i]) | |
1785 | DMERR("leaked buffer count %d: %ld", i, c->n_buffers[i]); | |
1786 | ||
1787 | for (i = 0; i < LIST_SIZE; i++) | |
1788 | BUG_ON(c->n_buffers[i]); | |
1789 | ||
21bb1327 | 1790 | kmem_cache_destroy(c->slab_cache); |
359dbf19 | 1791 | kmem_cache_destroy(c->slab_buffer); |
95d402f0 | 1792 | dm_io_client_destroy(c->dm_io); |
bde14184 | 1793 | mutex_destroy(&c->lock); |
95d402f0 MP |
1794 | kfree(c); |
1795 | } | |
1796 | EXPORT_SYMBOL_GPL(dm_bufio_client_destroy); | |
1797 | ||
400a0bef MP |
1798 | void dm_bufio_set_sector_offset(struct dm_bufio_client *c, sector_t start) |
1799 | { | |
1800 | c->start = start; | |
1801 | } | |
1802 | EXPORT_SYMBOL_GPL(dm_bufio_set_sector_offset); | |
1803 | ||
33096a78 | 1804 | static unsigned get_max_age_hz(void) |
95d402f0 | 1805 | { |
6aa7de05 | 1806 | unsigned max_age = READ_ONCE(dm_bufio_max_age); |
95d402f0 | 1807 | |
33096a78 JT |
1808 | if (max_age > UINT_MAX / HZ) |
1809 | max_age = UINT_MAX / HZ; | |
95d402f0 | 1810 | |
33096a78 JT |
1811 | return max_age * HZ; |
1812 | } | |
95d402f0 | 1813 | |
33096a78 JT |
1814 | static bool older_than(struct dm_buffer *b, unsigned long age_hz) |
1815 | { | |
f495339c | 1816 | return time_after_eq(jiffies, b->last_accessed + age_hz); |
33096a78 JT |
1817 | } |
1818 | ||
1819 | static void __evict_old_buffers(struct dm_bufio_client *c, unsigned long age_hz) | |
1820 | { | |
1821 | struct dm_buffer *b, *tmp; | |
13840d38 MP |
1822 | unsigned long retain_target = get_retain_buffers(c); |
1823 | unsigned long count; | |
390020ad | 1824 | LIST_HEAD(write_list); |
33096a78 JT |
1825 | |
1826 | dm_bufio_lock(c); | |
1827 | ||
390020ad MP |
1828 | __check_watermark(c, &write_list); |
1829 | if (unlikely(!list_empty(&write_list))) { | |
1830 | dm_bufio_unlock(c); | |
1831 | __flush_write_list(&write_list); | |
1832 | dm_bufio_lock(c); | |
1833 | } | |
1834 | ||
33096a78 JT |
1835 | count = c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY]; |
1836 | list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_CLEAN], lru_list) { | |
1837 | if (count <= retain_target) | |
1838 | break; | |
1839 | ||
1840 | if (!older_than(b, age_hz)) | |
1841 | break; | |
1842 | ||
1843 | if (__try_evict_buffer(b, 0)) | |
1844 | count--; | |
95d402f0 | 1845 | |
7cd32674 | 1846 | cond_resched(); |
95d402f0 | 1847 | } |
33096a78 JT |
1848 | |
1849 | dm_bufio_unlock(c); | |
1850 | } | |
1851 | ||
1852 | static void cleanup_old_buffers(void) | |
1853 | { | |
1854 | unsigned long max_age_hz = get_max_age_hz(); | |
1855 | struct dm_bufio_client *c; | |
1856 | ||
1857 | mutex_lock(&dm_bufio_clients_lock); | |
1858 | ||
390020ad MP |
1859 | __cache_size_refresh(); |
1860 | ||
33096a78 JT |
1861 | list_for_each_entry(c, &dm_bufio_all_clients, client_list) |
1862 | __evict_old_buffers(c, max_age_hz); | |
1863 | ||
95d402f0 MP |
1864 | mutex_unlock(&dm_bufio_clients_lock); |
1865 | } | |
1866 | ||
1867 | static struct workqueue_struct *dm_bufio_wq; | |
1868 | static struct delayed_work dm_bufio_work; | |
1869 | ||
1870 | static void work_fn(struct work_struct *w) | |
1871 | { | |
1872 | cleanup_old_buffers(); | |
1873 | ||
1874 | queue_delayed_work(dm_bufio_wq, &dm_bufio_work, | |
1875 | DM_BUFIO_WORK_TIMER_SECS * HZ); | |
1876 | } | |
1877 | ||
1878 | /*---------------------------------------------------------------- | |
1879 | * Module setup | |
1880 | *--------------------------------------------------------------*/ | |
1881 | ||
1882 | /* | |
1883 | * This is called only once for the whole dm_bufio module. | |
1884 | * It initializes memory limit. | |
1885 | */ | |
1886 | static int __init dm_bufio_init(void) | |
1887 | { | |
1888 | __u64 mem; | |
1889 | ||
4cb57ab4 MP |
1890 | dm_bufio_allocated_kmem_cache = 0; |
1891 | dm_bufio_allocated_get_free_pages = 0; | |
1892 | dm_bufio_allocated_vmalloc = 0; | |
1893 | dm_bufio_current_allocated = 0; | |
1894 | ||
ca79b0c2 | 1895 | mem = (__u64)mult_frac(totalram_pages() - totalhigh_pages(), |
74d4108d | 1896 | DM_BUFIO_MEMORY_PERCENT, 100) << PAGE_SHIFT; |
95d402f0 MP |
1897 | |
1898 | if (mem > ULONG_MAX) | |
1899 | mem = ULONG_MAX; | |
1900 | ||
1901 | #ifdef CONFIG_MMU | |
74d4108d EB |
1902 | if (mem > mult_frac(VMALLOC_TOTAL, DM_BUFIO_VMALLOC_PERCENT, 100)) |
1903 | mem = mult_frac(VMALLOC_TOTAL, DM_BUFIO_VMALLOC_PERCENT, 100); | |
95d402f0 MP |
1904 | #endif |
1905 | ||
1906 | dm_bufio_default_cache_size = mem; | |
1907 | ||
1908 | mutex_lock(&dm_bufio_clients_lock); | |
1909 | __cache_size_refresh(); | |
1910 | mutex_unlock(&dm_bufio_clients_lock); | |
1911 | ||
edd1ea2a | 1912 | dm_bufio_wq = alloc_workqueue("dm_bufio_cache", WQ_MEM_RECLAIM, 0); |
95d402f0 MP |
1913 | if (!dm_bufio_wq) |
1914 | return -ENOMEM; | |
1915 | ||
1916 | INIT_DELAYED_WORK(&dm_bufio_work, work_fn); | |
1917 | queue_delayed_work(dm_bufio_wq, &dm_bufio_work, | |
1918 | DM_BUFIO_WORK_TIMER_SECS * HZ); | |
1919 | ||
1920 | return 0; | |
1921 | } | |
1922 | ||
1923 | /* | |
1924 | * This is called once when unloading the dm_bufio module. | |
1925 | */ | |
1926 | static void __exit dm_bufio_exit(void) | |
1927 | { | |
1928 | int bug = 0; | |
95d402f0 MP |
1929 | |
1930 | cancel_delayed_work_sync(&dm_bufio_work); | |
1931 | destroy_workqueue(dm_bufio_wq); | |
1932 | ||
95d402f0 MP |
1933 | if (dm_bufio_client_count) { |
1934 | DMCRIT("%s: dm_bufio_client_count leaked: %d", | |
1935 | __func__, dm_bufio_client_count); | |
1936 | bug = 1; | |
1937 | } | |
1938 | ||
1939 | if (dm_bufio_current_allocated) { | |
1940 | DMCRIT("%s: dm_bufio_current_allocated leaked: %lu", | |
1941 | __func__, dm_bufio_current_allocated); | |
1942 | bug = 1; | |
1943 | } | |
1944 | ||
1945 | if (dm_bufio_allocated_get_free_pages) { | |
1946 | DMCRIT("%s: dm_bufio_allocated_get_free_pages leaked: %lu", | |
1947 | __func__, dm_bufio_allocated_get_free_pages); | |
1948 | bug = 1; | |
1949 | } | |
1950 | ||
1951 | if (dm_bufio_allocated_vmalloc) { | |
1952 | DMCRIT("%s: dm_bufio_vmalloc leaked: %lu", | |
1953 | __func__, dm_bufio_allocated_vmalloc); | |
1954 | bug = 1; | |
1955 | } | |
1956 | ||
86a49e2d | 1957 | BUG_ON(bug); |
95d402f0 MP |
1958 | } |
1959 | ||
1960 | module_init(dm_bufio_init) | |
1961 | module_exit(dm_bufio_exit) | |
1962 | ||
1963 | module_param_named(max_cache_size_bytes, dm_bufio_cache_size, ulong, S_IRUGO | S_IWUSR); | |
1964 | MODULE_PARM_DESC(max_cache_size_bytes, "Size of metadata cache"); | |
1965 | ||
1966 | module_param_named(max_age_seconds, dm_bufio_max_age, uint, S_IRUGO | S_IWUSR); | |
1967 | MODULE_PARM_DESC(max_age_seconds, "Max age of a buffer in seconds"); | |
33096a78 | 1968 | |
13840d38 | 1969 | module_param_named(retain_bytes, dm_bufio_retain_bytes, ulong, S_IRUGO | S_IWUSR); |
33096a78 | 1970 | MODULE_PARM_DESC(retain_bytes, "Try to keep at least this many bytes cached in memory"); |
95d402f0 MP |
1971 | |
1972 | module_param_named(peak_allocated_bytes, dm_bufio_peak_allocated, ulong, S_IRUGO | S_IWUSR); | |
1973 | MODULE_PARM_DESC(peak_allocated_bytes, "Tracks the maximum allocated memory"); | |
1974 | ||
1975 | module_param_named(allocated_kmem_cache_bytes, dm_bufio_allocated_kmem_cache, ulong, S_IRUGO); | |
1976 | MODULE_PARM_DESC(allocated_kmem_cache_bytes, "Memory allocated with kmem_cache_alloc"); | |
1977 | ||
1978 | module_param_named(allocated_get_free_pages_bytes, dm_bufio_allocated_get_free_pages, ulong, S_IRUGO); | |
1979 | MODULE_PARM_DESC(allocated_get_free_pages_bytes, "Memory allocated with get_free_pages"); | |
1980 | ||
1981 | module_param_named(allocated_vmalloc_bytes, dm_bufio_allocated_vmalloc, ulong, S_IRUGO); | |
1982 | MODULE_PARM_DESC(allocated_vmalloc_bytes, "Memory allocated with vmalloc"); | |
1983 | ||
1984 | module_param_named(current_allocated_bytes, dm_bufio_current_allocated, ulong, S_IRUGO); | |
1985 | MODULE_PARM_DESC(current_allocated_bytes, "Memory currently used by the cache"); | |
1986 | ||
1987 | MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>"); | |
1988 | MODULE_DESCRIPTION(DM_NAME " buffered I/O library"); | |
1989 | MODULE_LICENSE("GPL"); |