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