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