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