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