blob: 99579c81ae0a225ebee51d2ff1efb34b13183718 [file] [log] [blame]
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001/*
2 * Copyright (C) 2009-2011 Red Hat, Inc.
3 *
4 * Author: Mikulas Patocka <mpatocka@redhat.com>
5 *
6 * This file is released under the GPL.
7 */
8
9#include "dm-bufio.h"
10
11#include <linux/device-mapper.h>
12#include <linux/dm-io.h>
13#include <linux/slab.h>
14#include <linux/vmalloc.h>
Mikulas Patocka95d402f2011-10-31 20:19:09 +000015#include <linux/shrinker.h>
Stephen Rothwell6f662632011-11-01 18:30:49 +110016#include <linux/module.h>
Joe Thornber4e420c42014-10-06 13:48:51 +010017#include <linux/rbtree.h>
Mikulas Patocka95d402f2011-10-31 20:19:09 +000018
19#define DM_MSG_PREFIX "bufio"
20
21/*
22 * Memory management policy:
23 * Limit the number of buffers to DM_BUFIO_MEMORY_PERCENT of main memory
24 * or DM_BUFIO_VMALLOC_PERCENT of vmalloc memory (whichever is lower).
25 * Always allocate at least DM_BUFIO_MIN_BUFFERS buffers.
26 * Start background writeback when there are DM_BUFIO_WRITEBACK_PERCENT
27 * dirty buffers.
28 */
29#define DM_BUFIO_MIN_BUFFERS 8
30
31#define DM_BUFIO_MEMORY_PERCENT 2
32#define DM_BUFIO_VMALLOC_PERCENT 25
33#define DM_BUFIO_WRITEBACK_PERCENT 75
34
35/*
36 * Check buffer ages in this interval (seconds)
37 */
Joe Thornber33096a72014-10-09 11:10:25 +010038#define DM_BUFIO_WORK_TIMER_SECS 30
Mikulas Patocka95d402f2011-10-31 20:19:09 +000039
40/*
41 * Free buffers when they are older than this (seconds)
42 */
Joe Thornber33096a72014-10-09 11:10:25 +010043#define DM_BUFIO_DEFAULT_AGE_SECS 300
44
45/*
46 * The nr of bytes of cached data to keep around.
47 */
48#define DM_BUFIO_DEFAULT_RETAIN_BYTES (256 * 1024)
Mikulas Patocka95d402f2011-10-31 20:19:09 +000049
50/*
51 * The number of bvec entries that are embedded directly in the buffer.
52 * If the chunk size is larger, dm-io is used to do the io.
53 */
54#define DM_BUFIO_INLINE_VECS 16
55
56/*
Mikulas Patocka95d402f2011-10-31 20:19:09 +000057 * Don't try to use kmem_cache_alloc for blocks larger than this.
58 * For explanation, see alloc_buffer_data below.
59 */
60#define DM_BUFIO_BLOCK_SIZE_SLAB_LIMIT (PAGE_SIZE >> 1)
61#define DM_BUFIO_BLOCK_SIZE_GFP_LIMIT (PAGE_SIZE << (MAX_ORDER - 1))
62
63/*
64 * dm_buffer->list_mode
65 */
66#define LIST_CLEAN 0
67#define LIST_DIRTY 1
68#define LIST_SIZE 2
69
70/*
71 * Linking of buffers:
72 * All buffers are linked to cache_hash with their hash_list field.
73 *
74 * Clean buffers that are not being written (B_WRITING not set)
75 * are linked to lru[LIST_CLEAN] with their lru_list field.
76 *
77 * Dirty and clean buffers that are being written are linked to
78 * lru[LIST_DIRTY] with their lru_list field. When the write
79 * finishes, the buffer cannot be relinked immediately (because we
80 * are in an interrupt context and relinking requires process
81 * context), so some clean-not-writing buffers can be held on
82 * dirty_lru too. They are later added to lru in the process
83 * context.
84 */
85struct dm_bufio_client {
86 struct mutex lock;
87
88 struct list_head lru[LIST_SIZE];
89 unsigned long n_buffers[LIST_SIZE];
90
91 struct block_device *bdev;
92 unsigned block_size;
93 unsigned char sectors_per_block_bits;
94 unsigned char pages_per_block_bits;
95 unsigned char blocks_per_page_bits;
96 unsigned aux_size;
97 void (*alloc_callback)(struct dm_buffer *);
98 void (*write_callback)(struct dm_buffer *);
99
100 struct dm_io_client *dm_io;
101
102 struct list_head reserved_buffers;
103 unsigned need_reserved_buffers;
104
Mikulas Patocka55b082e2014-01-13 19:13:05 -0500105 unsigned minimum_buffers;
106
Joe Thornber4e420c42014-10-06 13:48:51 +0100107 struct rb_root buffer_tree;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000108 wait_queue_head_t free_buffer_wait;
109
110 int async_write_error;
111
112 struct list_head client_list;
113 struct shrinker shrinker;
114};
115
116/*
117 * Buffer state bits.
118 */
119#define B_READING 0
120#define B_WRITING 1
121#define B_DIRTY 2
122
123/*
124 * Describes how the block was allocated:
125 * kmem_cache_alloc(), __get_free_pages() or vmalloc().
126 * See the comment at alloc_buffer_data.
127 */
128enum data_mode {
129 DATA_MODE_SLAB = 0,
130 DATA_MODE_GET_FREE_PAGES = 1,
131 DATA_MODE_VMALLOC = 2,
132 DATA_MODE_LIMIT = 3
133};
134
135struct dm_buffer {
Joe Thornber4e420c42014-10-06 13:48:51 +0100136 struct rb_node node;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000137 struct list_head lru_list;
138 sector_t block;
139 void *data;
140 enum data_mode data_mode;
141 unsigned char list_mode; /* LIST_* */
142 unsigned hold_count;
143 int read_error;
144 int write_error;
145 unsigned long state;
146 unsigned long last_accessed;
147 struct dm_bufio_client *c;
Mikulas Patocka24809452013-07-10 23:41:18 +0100148 struct list_head write_list;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000149 struct bio bio;
150 struct bio_vec bio_vec[DM_BUFIO_INLINE_VECS];
151};
152
153/*----------------------------------------------------------------*/
154
155static struct kmem_cache *dm_bufio_caches[PAGE_SHIFT - SECTOR_SHIFT];
156static char *dm_bufio_cache_names[PAGE_SHIFT - SECTOR_SHIFT];
157
158static inline int dm_bufio_cache_index(struct dm_bufio_client *c)
159{
160 unsigned ret = c->blocks_per_page_bits - 1;
161
162 BUG_ON(ret >= ARRAY_SIZE(dm_bufio_caches));
163
164 return ret;
165}
166
167#define DM_BUFIO_CACHE(c) (dm_bufio_caches[dm_bufio_cache_index(c)])
168#define DM_BUFIO_CACHE_NAME(c) (dm_bufio_cache_names[dm_bufio_cache_index(c)])
169
170#define dm_bufio_in_request() (!!current->bio_list)
171
172static void dm_bufio_lock(struct dm_bufio_client *c)
173{
174 mutex_lock_nested(&c->lock, dm_bufio_in_request());
175}
176
177static int dm_bufio_trylock(struct dm_bufio_client *c)
178{
179 return mutex_trylock(&c->lock);
180}
181
182static void dm_bufio_unlock(struct dm_bufio_client *c)
183{
184 mutex_unlock(&c->lock);
185}
186
187/*
188 * FIXME Move to sched.h?
189 */
190#ifdef CONFIG_PREEMPT_VOLUNTARY
191# define dm_bufio_cond_resched() \
192do { \
193 if (unlikely(need_resched())) \
194 _cond_resched(); \
195} while (0)
196#else
197# define dm_bufio_cond_resched() do { } while (0)
198#endif
199
200/*----------------------------------------------------------------*/
201
202/*
203 * Default cache size: available memory divided by the ratio.
204 */
205static unsigned long dm_bufio_default_cache_size;
206
207/*
208 * Total cache size set by the user.
209 */
210static unsigned long dm_bufio_cache_size;
211
212/*
213 * A copy of dm_bufio_cache_size because dm_bufio_cache_size can change
214 * at any time. If it disagrees, the user has changed cache size.
215 */
216static unsigned long dm_bufio_cache_size_latch;
217
218static DEFINE_SPINLOCK(param_spinlock);
219
220/*
221 * Buffers are freed after this timeout
222 */
223static unsigned dm_bufio_max_age = DM_BUFIO_DEFAULT_AGE_SECS;
Joe Thornber33096a72014-10-09 11:10:25 +0100224static unsigned dm_bufio_retain_bytes = DM_BUFIO_DEFAULT_RETAIN_BYTES;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000225
226static unsigned long dm_bufio_peak_allocated;
227static unsigned long dm_bufio_allocated_kmem_cache;
228static unsigned long dm_bufio_allocated_get_free_pages;
229static unsigned long dm_bufio_allocated_vmalloc;
230static unsigned long dm_bufio_current_allocated;
231
232/*----------------------------------------------------------------*/
233
234/*
235 * Per-client cache: dm_bufio_cache_size / dm_bufio_client_count
236 */
237static unsigned long dm_bufio_cache_size_per_client;
238
239/*
240 * The current number of clients.
241 */
242static int dm_bufio_client_count;
243
244/*
245 * The list of all clients.
246 */
247static LIST_HEAD(dm_bufio_all_clients);
248
249/*
250 * This mutex protects dm_bufio_cache_size_latch,
251 * dm_bufio_cache_size_per_client and dm_bufio_client_count
252 */
253static DEFINE_MUTEX(dm_bufio_clients_lock);
254
Joe Thornber4e420c42014-10-06 13:48:51 +0100255/*----------------------------------------------------------------
256 * A red/black tree acts as an index for all the buffers.
257 *--------------------------------------------------------------*/
258static struct dm_buffer *__find(struct dm_bufio_client *c, sector_t block)
259{
260 struct rb_node *n = c->buffer_tree.rb_node;
261 struct dm_buffer *b;
262
263 while (n) {
264 b = container_of(n, struct dm_buffer, node);
265
266 if (b->block == block)
267 return b;
268
269 n = (b->block < block) ? n->rb_left : n->rb_right;
270 }
271
272 return NULL;
273}
274
275static void __insert(struct dm_bufio_client *c, struct dm_buffer *b)
276{
277 struct rb_node **new = &c->buffer_tree.rb_node, *parent = NULL;
278 struct dm_buffer *found;
279
280 while (*new) {
281 found = container_of(*new, struct dm_buffer, node);
282
283 if (found->block == b->block) {
284 BUG_ON(found != b);
285 return;
286 }
287
288 parent = *new;
289 new = (found->block < b->block) ?
290 &((*new)->rb_left) : &((*new)->rb_right);
291 }
292
293 rb_link_node(&b->node, parent, new);
294 rb_insert_color(&b->node, &c->buffer_tree);
295}
296
297static void __remove(struct dm_bufio_client *c, struct dm_buffer *b)
298{
299 rb_erase(&b->node, &c->buffer_tree);
300}
301
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000302/*----------------------------------------------------------------*/
303
304static void adjust_total_allocated(enum data_mode data_mode, long diff)
305{
306 static unsigned long * const class_ptr[DATA_MODE_LIMIT] = {
307 &dm_bufio_allocated_kmem_cache,
308 &dm_bufio_allocated_get_free_pages,
309 &dm_bufio_allocated_vmalloc,
310 };
311
312 spin_lock(&param_spinlock);
313
314 *class_ptr[data_mode] += diff;
315
316 dm_bufio_current_allocated += diff;
317
318 if (dm_bufio_current_allocated > dm_bufio_peak_allocated)
319 dm_bufio_peak_allocated = dm_bufio_current_allocated;
320
321 spin_unlock(&param_spinlock);
322}
323
324/*
325 * Change the number of clients and recalculate per-client limit.
326 */
327static void __cache_size_refresh(void)
328{
329 BUG_ON(!mutex_is_locked(&dm_bufio_clients_lock));
330 BUG_ON(dm_bufio_client_count < 0);
331
Mikulas Patockafe5fe902012-10-12 16:59:46 +0100332 dm_bufio_cache_size_latch = ACCESS_ONCE(dm_bufio_cache_size);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000333
334 /*
335 * Use default if set to 0 and report the actual cache size used.
336 */
337 if (!dm_bufio_cache_size_latch) {
338 (void)cmpxchg(&dm_bufio_cache_size, 0,
339 dm_bufio_default_cache_size);
340 dm_bufio_cache_size_latch = dm_bufio_default_cache_size;
341 }
342
343 dm_bufio_cache_size_per_client = dm_bufio_cache_size_latch /
344 (dm_bufio_client_count ? : 1);
345}
346
347/*
348 * Allocating buffer data.
349 *
350 * Small buffers are allocated with kmem_cache, to use space optimally.
351 *
352 * For large buffers, we choose between get_free_pages and vmalloc.
353 * Each has advantages and disadvantages.
354 *
355 * __get_free_pages can randomly fail if the memory is fragmented.
356 * __vmalloc won't randomly fail, but vmalloc space is limited (it may be
357 * as low as 128M) so using it for caching is not appropriate.
358 *
359 * If the allocation may fail we use __get_free_pages. Memory fragmentation
360 * won't have a fatal effect here, but it just causes flushes of some other
361 * buffers and more I/O will be performed. Don't use __get_free_pages if it
362 * always fails (i.e. order >= MAX_ORDER).
363 *
364 * If the allocation shouldn't fail we use __vmalloc. This is only for the
365 * initial reserve allocation, so there's no risk of wasting all vmalloc
366 * space.
367 */
368static void *alloc_buffer_data(struct dm_bufio_client *c, gfp_t gfp_mask,
369 enum data_mode *data_mode)
370{
Mikulas Patocka502624b2013-05-10 14:37:15 +0100371 unsigned noio_flag;
372 void *ptr;
373
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000374 if (c->block_size <= DM_BUFIO_BLOCK_SIZE_SLAB_LIMIT) {
375 *data_mode = DATA_MODE_SLAB;
376 return kmem_cache_alloc(DM_BUFIO_CACHE(c), gfp_mask);
377 }
378
379 if (c->block_size <= DM_BUFIO_BLOCK_SIZE_GFP_LIMIT &&
380 gfp_mask & __GFP_NORETRY) {
381 *data_mode = DATA_MODE_GET_FREE_PAGES;
382 return (void *)__get_free_pages(gfp_mask,
383 c->pages_per_block_bits);
384 }
385
386 *data_mode = DATA_MODE_VMALLOC;
Mikulas Patocka502624b2013-05-10 14:37:15 +0100387
388 /*
389 * __vmalloc allocates the data pages and auxiliary structures with
390 * gfp_flags that were specified, but pagetables are always allocated
391 * with GFP_KERNEL, no matter what was specified as gfp_mask.
392 *
393 * Consequently, we must set per-process flag PF_MEMALLOC_NOIO so that
394 * all allocations done by this process (including pagetables) are done
395 * as if GFP_NOIO was specified.
396 */
397
398 if (gfp_mask & __GFP_NORETRY)
399 noio_flag = memalloc_noio_save();
400
Mikulas Patocka220cd052013-07-10 23:41:16 +0100401 ptr = __vmalloc(c->block_size, gfp_mask | __GFP_HIGHMEM, PAGE_KERNEL);
Mikulas Patocka502624b2013-05-10 14:37:15 +0100402
403 if (gfp_mask & __GFP_NORETRY)
404 memalloc_noio_restore(noio_flag);
405
406 return ptr;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000407}
408
409/*
410 * Free buffer's data.
411 */
412static void free_buffer_data(struct dm_bufio_client *c,
413 void *data, enum data_mode data_mode)
414{
415 switch (data_mode) {
416 case DATA_MODE_SLAB:
417 kmem_cache_free(DM_BUFIO_CACHE(c), data);
418 break;
419
420 case DATA_MODE_GET_FREE_PAGES:
421 free_pages((unsigned long)data, c->pages_per_block_bits);
422 break;
423
424 case DATA_MODE_VMALLOC:
425 vfree(data);
426 break;
427
428 default:
429 DMCRIT("dm_bufio_free_buffer_data: bad data mode: %d",
430 data_mode);
431 BUG();
432 }
433}
434
435/*
436 * Allocate buffer and its data.
437 */
438static struct dm_buffer *alloc_buffer(struct dm_bufio_client *c, gfp_t gfp_mask)
439{
440 struct dm_buffer *b = kmalloc(sizeof(struct dm_buffer) + c->aux_size,
441 gfp_mask);
442
443 if (!b)
444 return NULL;
445
446 b->c = c;
447
448 b->data = alloc_buffer_data(c, gfp_mask, &b->data_mode);
449 if (!b->data) {
450 kfree(b);
451 return NULL;
452 }
453
454 adjust_total_allocated(b->data_mode, (long)c->block_size);
455
456 return b;
457}
458
459/*
460 * Free buffer and its data.
461 */
462static 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);
469 kfree(b);
470}
471
472/*
473 * Link buffer to the hash list and clean or dirty queue.
474 */
475static 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]);
Joe Thornber4e420c42014-10-06 13:48:51 +0100483 __insert(b->c, b);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000484 b->last_accessed = jiffies;
485}
486
487/*
488 * Unlink buffer from the hash list and dirty or clean queue.
489 */
490static 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]--;
Joe Thornber4e420c42014-10-06 13:48:51 +0100497 __remove(b->c, b);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000498 list_del(&b->lru_list);
499}
500
501/*
502 * Place the buffer to the head of dirty or clean LRU queue.
503 */
504static 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;
Wei Yongjun54499af2012-10-12 16:59:44 +0100513 list_move(&b->lru_list, &c->lru[dirty]);
Joe Thornbereb76faf2014-09-30 09:32:46 +0100514 b->last_accessed = jiffies;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000515}
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 *
526 * the I/O driver can reject requests spuriously if it thinks that
527 * the requests are too big for the device or if they cross a
528 * controller-defined memory boundary.
529 *
530 * If the buffer is small enough (up to DM_BUFIO_INLINE_VECS pages) and
531 * it is not vmalloced, try using the bio interface.
532 *
533 * If the buffer is big, if it is vmalloced or if the underlying device
534 * rejects the bio because it is too large, use dm-io layer to do the I/O.
535 * The dm-io layer splits the I/O into multiple requests, avoiding the above
536 * shortcomings.
537 *--------------------------------------------------------------*/
538
539/*
540 * dm-io completion routine. It just calls b->bio.bi_end_io, pretending
541 * that the request was handled directly with bio interface.
542 */
543static void dmio_complete(unsigned long error, void *context)
544{
545 struct dm_buffer *b = context;
546
547 b->bio.bi_end_io(&b->bio, error ? -EIO : 0);
548}
549
550static void use_dmio(struct dm_buffer *b, int rw, sector_t block,
551 bio_end_io_t *end_io)
552{
553 int r;
554 struct dm_io_request io_req = {
555 .bi_rw = rw,
556 .notify.fn = dmio_complete,
557 .notify.context = b,
558 .client = b->c->dm_io,
559 };
560 struct dm_io_region region = {
561 .bdev = b->c->bdev,
562 .sector = block << b->c->sectors_per_block_bits,
563 .count = b->c->block_size >> SECTOR_SHIFT,
564 };
565
566 if (b->data_mode != DATA_MODE_VMALLOC) {
567 io_req.mem.type = DM_IO_KMEM;
568 io_req.mem.ptr.addr = b->data;
569 } else {
570 io_req.mem.type = DM_IO_VMA;
571 io_req.mem.ptr.vma = b->data;
572 }
573
574 b->bio.bi_end_io = end_io;
575
576 r = dm_io(&io_req, 1, &region, NULL);
577 if (r)
578 end_io(&b->bio, r);
579}
580
581static void use_inline_bio(struct dm_buffer *b, int rw, sector_t block,
582 bio_end_io_t *end_io)
583{
584 char *ptr;
585 int len;
586
587 bio_init(&b->bio);
588 b->bio.bi_io_vec = b->bio_vec;
589 b->bio.bi_max_vecs = DM_BUFIO_INLINE_VECS;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700590 b->bio.bi_iter.bi_sector = block << b->c->sectors_per_block_bits;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000591 b->bio.bi_bdev = b->c->bdev;
592 b->bio.bi_end_io = end_io;
593
594 /*
595 * We assume that if len >= PAGE_SIZE ptr is page-aligned.
596 * If len < PAGE_SIZE the buffer doesn't cross page boundary.
597 */
598 ptr = b->data;
599 len = b->c->block_size;
600
601 if (len >= PAGE_SIZE)
602 BUG_ON((unsigned long)ptr & (PAGE_SIZE - 1));
603 else
604 BUG_ON((unsigned long)ptr & (len - 1));
605
606 do {
607 if (!bio_add_page(&b->bio, virt_to_page(ptr),
608 len < PAGE_SIZE ? len : PAGE_SIZE,
609 virt_to_phys(ptr) & (PAGE_SIZE - 1))) {
610 BUG_ON(b->c->block_size <= PAGE_SIZE);
611 use_dmio(b, rw, block, end_io);
612 return;
613 }
614
615 len -= PAGE_SIZE;
616 ptr += PAGE_SIZE;
617 } while (len > 0);
618
619 submit_bio(rw, &b->bio);
620}
621
622static void submit_io(struct dm_buffer *b, int rw, sector_t block,
623 bio_end_io_t *end_io)
624{
625 if (rw == WRITE && b->c->write_callback)
626 b->c->write_callback(b);
627
628 if (b->c->block_size <= DM_BUFIO_INLINE_VECS * PAGE_SIZE &&
629 b->data_mode != DATA_MODE_VMALLOC)
630 use_inline_bio(b, rw, block, end_io);
631 else
632 use_dmio(b, rw, block, end_io);
633}
634
635/*----------------------------------------------------------------
636 * Writing dirty buffers
637 *--------------------------------------------------------------*/
638
639/*
640 * The endio routine for write.
641 *
642 * Set the error, clear B_WRITING bit and wake anyone who was waiting on
643 * it.
644 */
645static void write_endio(struct bio *bio, int error)
646{
647 struct dm_buffer *b = container_of(bio, struct dm_buffer, bio);
648
649 b->write_error = error;
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100650 if (unlikely(error)) {
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000651 struct dm_bufio_client *c = b->c;
652 (void)cmpxchg(&c->async_write_error, 0, error);
653 }
654
655 BUG_ON(!test_bit(B_WRITING, &b->state));
656
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100657 smp_mb__before_atomic();
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000658 clear_bit(B_WRITING, &b->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100659 smp_mb__after_atomic();
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000660
661 wake_up_bit(&b->state, B_WRITING);
662}
663
664/*
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000665 * Initiate a write on a dirty buffer, but don't wait for it.
666 *
667 * - If the buffer is not dirty, exit.
668 * - If there some previous write going on, wait for it to finish (we can't
669 * have two writes on the same buffer simultaneously).
670 * - Submit our write and don't wait on it. We set B_WRITING indicating
671 * that there is a write in progress.
672 */
Mikulas Patocka24809452013-07-10 23:41:18 +0100673static void __write_dirty_buffer(struct dm_buffer *b,
674 struct list_head *write_list)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000675{
676 if (!test_bit(B_DIRTY, &b->state))
677 return;
678
679 clear_bit(B_DIRTY, &b->state);
NeilBrown74316202014-07-07 15:16:04 +1000680 wait_on_bit_lock_io(&b->state, B_WRITING, TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000681
Mikulas Patocka24809452013-07-10 23:41:18 +0100682 if (!write_list)
683 submit_io(b, WRITE, b->block, write_endio);
684 else
685 list_add_tail(&b->write_list, write_list);
686}
687
688static void __flush_write_list(struct list_head *write_list)
689{
690 struct blk_plug plug;
691 blk_start_plug(&plug);
692 while (!list_empty(write_list)) {
693 struct dm_buffer *b =
694 list_entry(write_list->next, struct dm_buffer, write_list);
695 list_del(&b->write_list);
696 submit_io(b, WRITE, b->block, write_endio);
697 dm_bufio_cond_resched();
698 }
699 blk_finish_plug(&plug);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000700}
701
702/*
703 * Wait until any activity on the buffer finishes. Possibly write the
704 * buffer if it is dirty. When this function finishes, there is no I/O
705 * running on the buffer and the buffer is not dirty.
706 */
707static void __make_buffer_clean(struct dm_buffer *b)
708{
709 BUG_ON(b->hold_count);
710
711 if (!b->state) /* fast case */
712 return;
713
NeilBrown74316202014-07-07 15:16:04 +1000714 wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE);
Mikulas Patocka24809452013-07-10 23:41:18 +0100715 __write_dirty_buffer(b, NULL);
NeilBrown74316202014-07-07 15:16:04 +1000716 wait_on_bit_io(&b->state, B_WRITING, TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000717}
718
719/*
720 * Find some buffer that is not held by anybody, clean it, unlink it and
721 * return it.
722 */
723static struct dm_buffer *__get_unclaimed_buffer(struct dm_bufio_client *c)
724{
725 struct dm_buffer *b;
726
727 list_for_each_entry_reverse(b, &c->lru[LIST_CLEAN], lru_list) {
728 BUG_ON(test_bit(B_WRITING, &b->state));
729 BUG_ON(test_bit(B_DIRTY, &b->state));
730
731 if (!b->hold_count) {
732 __make_buffer_clean(b);
733 __unlink_buffer(b);
734 return b;
735 }
736 dm_bufio_cond_resched();
737 }
738
739 list_for_each_entry_reverse(b, &c->lru[LIST_DIRTY], lru_list) {
740 BUG_ON(test_bit(B_READING, &b->state));
741
742 if (!b->hold_count) {
743 __make_buffer_clean(b);
744 __unlink_buffer(b);
745 return b;
746 }
747 dm_bufio_cond_resched();
748 }
749
750 return NULL;
751}
752
753/*
754 * Wait until some other threads free some buffer or release hold count on
755 * some buffer.
756 *
757 * This function is entered with c->lock held, drops it and regains it
758 * before exiting.
759 */
760static void __wait_for_free_buffer(struct dm_bufio_client *c)
761{
762 DECLARE_WAITQUEUE(wait, current);
763
764 add_wait_queue(&c->free_buffer_wait, &wait);
765 set_task_state(current, TASK_UNINTERRUPTIBLE);
766 dm_bufio_unlock(c);
767
768 io_schedule();
769
770 set_task_state(current, TASK_RUNNING);
771 remove_wait_queue(&c->free_buffer_wait, &wait);
772
773 dm_bufio_lock(c);
774}
775
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100776enum new_flag {
777 NF_FRESH = 0,
778 NF_READ = 1,
779 NF_GET = 2,
780 NF_PREFETCH = 3
781};
782
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000783/*
784 * Allocate a new buffer. If the allocation is not possible, wait until
785 * some other thread frees a buffer.
786 *
787 * May drop the lock and regain it.
788 */
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100789static struct dm_buffer *__alloc_buffer_wait_no_callback(struct dm_bufio_client *c, enum new_flag nf)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000790{
791 struct dm_buffer *b;
792
793 /*
794 * dm-bufio is resistant to allocation failures (it just keeps
795 * one buffer reserved in cases all the allocations fail).
796 * So set flags to not try too hard:
797 * GFP_NOIO: don't recurse into the I/O layer
798 * __GFP_NORETRY: don't retry and rather return failure
799 * __GFP_NOMEMALLOC: don't use emergency reserves
800 * __GFP_NOWARN: don't print a warning in case of failure
801 *
802 * For debugging, if we set the cache size to 1, no new buffers will
803 * be allocated.
804 */
805 while (1) {
806 if (dm_bufio_cache_size_latch != 1) {
807 b = alloc_buffer(c, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
808 if (b)
809 return b;
810 }
811
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100812 if (nf == NF_PREFETCH)
813 return NULL;
814
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000815 if (!list_empty(&c->reserved_buffers)) {
816 b = list_entry(c->reserved_buffers.next,
817 struct dm_buffer, lru_list);
818 list_del(&b->lru_list);
819 c->need_reserved_buffers++;
820
821 return b;
822 }
823
824 b = __get_unclaimed_buffer(c);
825 if (b)
826 return b;
827
828 __wait_for_free_buffer(c);
829 }
830}
831
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100832static struct dm_buffer *__alloc_buffer_wait(struct dm_bufio_client *c, enum new_flag nf)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000833{
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100834 struct dm_buffer *b = __alloc_buffer_wait_no_callback(c, nf);
835
836 if (!b)
837 return NULL;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000838
839 if (c->alloc_callback)
840 c->alloc_callback(b);
841
842 return b;
843}
844
845/*
846 * Free a buffer and wake other threads waiting for free buffers.
847 */
848static void __free_buffer_wake(struct dm_buffer *b)
849{
850 struct dm_bufio_client *c = b->c;
851
852 if (!c->need_reserved_buffers)
853 free_buffer(b);
854 else {
855 list_add(&b->lru_list, &c->reserved_buffers);
856 c->need_reserved_buffers--;
857 }
858
859 wake_up(&c->free_buffer_wait);
860}
861
Mikulas Patocka24809452013-07-10 23:41:18 +0100862static void __write_dirty_buffers_async(struct dm_bufio_client *c, int no_wait,
863 struct list_head *write_list)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000864{
865 struct dm_buffer *b, *tmp;
866
867 list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) {
868 BUG_ON(test_bit(B_READING, &b->state));
869
870 if (!test_bit(B_DIRTY, &b->state) &&
871 !test_bit(B_WRITING, &b->state)) {
872 __relink_lru(b, LIST_CLEAN);
873 continue;
874 }
875
876 if (no_wait && test_bit(B_WRITING, &b->state))
877 return;
878
Mikulas Patocka24809452013-07-10 23:41:18 +0100879 __write_dirty_buffer(b, write_list);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000880 dm_bufio_cond_resched();
881 }
882}
883
884/*
885 * Get writeback threshold and buffer limit for a given client.
886 */
887static void __get_memory_limit(struct dm_bufio_client *c,
888 unsigned long *threshold_buffers,
889 unsigned long *limit_buffers)
890{
891 unsigned long buffers;
892
Mikulas Patockafe5fe902012-10-12 16:59:46 +0100893 if (ACCESS_ONCE(dm_bufio_cache_size) != dm_bufio_cache_size_latch) {
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000894 mutex_lock(&dm_bufio_clients_lock);
895 __cache_size_refresh();
896 mutex_unlock(&dm_bufio_clients_lock);
897 }
898
899 buffers = dm_bufio_cache_size_per_client >>
900 (c->sectors_per_block_bits + SECTOR_SHIFT);
901
Mikulas Patocka55b082e2014-01-13 19:13:05 -0500902 if (buffers < c->minimum_buffers)
903 buffers = c->minimum_buffers;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000904
905 *limit_buffers = buffers;
906 *threshold_buffers = buffers * DM_BUFIO_WRITEBACK_PERCENT / 100;
907}
908
909/*
910 * Check if we're over watermark.
911 * If we are over threshold_buffers, start freeing buffers.
912 * If we're over "limit_buffers", block until we get under the limit.
913 */
Mikulas Patocka24809452013-07-10 23:41:18 +0100914static void __check_watermark(struct dm_bufio_client *c,
915 struct list_head *write_list)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000916{
917 unsigned long threshold_buffers, limit_buffers;
918
919 __get_memory_limit(c, &threshold_buffers, &limit_buffers);
920
921 while (c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY] >
922 limit_buffers) {
923
924 struct dm_buffer *b = __get_unclaimed_buffer(c);
925
926 if (!b)
927 return;
928
929 __free_buffer_wake(b);
930 dm_bufio_cond_resched();
931 }
932
933 if (c->n_buffers[LIST_DIRTY] > threshold_buffers)
Mikulas Patocka24809452013-07-10 23:41:18 +0100934 __write_dirty_buffers_async(c, 1, write_list);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000935}
936
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000937/*----------------------------------------------------------------
938 * Getting a buffer
939 *--------------------------------------------------------------*/
940
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000941static struct dm_buffer *__bufio_new(struct dm_bufio_client *c, sector_t block,
Mikulas Patocka24809452013-07-10 23:41:18 +0100942 enum new_flag nf, int *need_submit,
943 struct list_head *write_list)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000944{
945 struct dm_buffer *b, *new_b = NULL;
946
947 *need_submit = 0;
948
949 b = __find(c, block);
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100950 if (b)
951 goto found_buffer;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000952
953 if (nf == NF_GET)
954 return NULL;
955
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100956 new_b = __alloc_buffer_wait(c, nf);
957 if (!new_b)
958 return NULL;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000959
960 /*
961 * We've had a period where the mutex was unlocked, so need to
962 * recheck the hash table.
963 */
964 b = __find(c, block);
965 if (b) {
966 __free_buffer_wake(new_b);
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100967 goto found_buffer;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000968 }
969
Mikulas Patocka24809452013-07-10 23:41:18 +0100970 __check_watermark(c, write_list);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000971
972 b = new_b;
973 b->hold_count = 1;
974 b->read_error = 0;
975 b->write_error = 0;
976 __link_buffer(b, block, LIST_CLEAN);
977
978 if (nf == NF_FRESH) {
979 b->state = 0;
980 return b;
981 }
982
983 b->state = 1 << B_READING;
984 *need_submit = 1;
985
986 return b;
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100987
988found_buffer:
989 if (nf == NF_PREFETCH)
990 return NULL;
991 /*
992 * Note: it is essential that we don't wait for the buffer to be
993 * read if dm_bufio_get function is used. Both dm_bufio_get and
994 * dm_bufio_prefetch can be used in the driver request routine.
995 * If the user called both dm_bufio_prefetch and dm_bufio_get on
996 * the same buffer, it would deadlock if we waited.
997 */
998 if (nf == NF_GET && unlikely(test_bit(B_READING, &b->state)))
999 return NULL;
1000
1001 b->hold_count++;
1002 __relink_lru(b, test_bit(B_DIRTY, &b->state) ||
1003 test_bit(B_WRITING, &b->state));
1004 return b;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001005}
1006
1007/*
1008 * The endio routine for reading: set the error, clear the bit and wake up
1009 * anyone waiting on the buffer.
1010 */
1011static void read_endio(struct bio *bio, int error)
1012{
1013 struct dm_buffer *b = container_of(bio, struct dm_buffer, bio);
1014
1015 b->read_error = error;
1016
1017 BUG_ON(!test_bit(B_READING, &b->state));
1018
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001019 smp_mb__before_atomic();
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001020 clear_bit(B_READING, &b->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001021 smp_mb__after_atomic();
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001022
1023 wake_up_bit(&b->state, B_READING);
1024}
1025
1026/*
1027 * A common routine for dm_bufio_new and dm_bufio_read. Operation of these
1028 * functions is similar except that dm_bufio_new doesn't read the
1029 * buffer from the disk (assuming that the caller overwrites all the data
1030 * and uses dm_bufio_mark_buffer_dirty to write new data back).
1031 */
1032static void *new_read(struct dm_bufio_client *c, sector_t block,
1033 enum new_flag nf, struct dm_buffer **bp)
1034{
1035 int need_submit;
1036 struct dm_buffer *b;
1037
Mikulas Patocka24809452013-07-10 23:41:18 +01001038 LIST_HEAD(write_list);
1039
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001040 dm_bufio_lock(c);
Mikulas Patocka24809452013-07-10 23:41:18 +01001041 b = __bufio_new(c, block, nf, &need_submit, &write_list);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001042 dm_bufio_unlock(c);
1043
Mikulas Patocka24809452013-07-10 23:41:18 +01001044 __flush_write_list(&write_list);
1045
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001046 if (!b)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001047 return b;
1048
1049 if (need_submit)
1050 submit_io(b, READ, b->block, read_endio);
1051
NeilBrown74316202014-07-07 15:16:04 +10001052 wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001053
1054 if (b->read_error) {
1055 int error = b->read_error;
1056
1057 dm_bufio_release(b);
1058
1059 return ERR_PTR(error);
1060 }
1061
1062 *bp = b;
1063
1064 return b->data;
1065}
1066
1067void *dm_bufio_get(struct dm_bufio_client *c, sector_t block,
1068 struct dm_buffer **bp)
1069{
1070 return new_read(c, block, NF_GET, bp);
1071}
1072EXPORT_SYMBOL_GPL(dm_bufio_get);
1073
1074void *dm_bufio_read(struct dm_bufio_client *c, sector_t block,
1075 struct dm_buffer **bp)
1076{
1077 BUG_ON(dm_bufio_in_request());
1078
1079 return new_read(c, block, NF_READ, bp);
1080}
1081EXPORT_SYMBOL_GPL(dm_bufio_read);
1082
1083void *dm_bufio_new(struct dm_bufio_client *c, sector_t block,
1084 struct dm_buffer **bp)
1085{
1086 BUG_ON(dm_bufio_in_request());
1087
1088 return new_read(c, block, NF_FRESH, bp);
1089}
1090EXPORT_SYMBOL_GPL(dm_bufio_new);
1091
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001092void dm_bufio_prefetch(struct dm_bufio_client *c,
1093 sector_t block, unsigned n_blocks)
1094{
1095 struct blk_plug plug;
1096
Mikulas Patocka24809452013-07-10 23:41:18 +01001097 LIST_HEAD(write_list);
1098
Mikulas Patocka3b6b7812013-03-20 17:21:25 +00001099 BUG_ON(dm_bufio_in_request());
1100
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001101 blk_start_plug(&plug);
1102 dm_bufio_lock(c);
1103
1104 for (; n_blocks--; block++) {
1105 int need_submit;
1106 struct dm_buffer *b;
Mikulas Patocka24809452013-07-10 23:41:18 +01001107 b = __bufio_new(c, block, NF_PREFETCH, &need_submit,
1108 &write_list);
1109 if (unlikely(!list_empty(&write_list))) {
1110 dm_bufio_unlock(c);
1111 blk_finish_plug(&plug);
1112 __flush_write_list(&write_list);
1113 blk_start_plug(&plug);
1114 dm_bufio_lock(c);
1115 }
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001116 if (unlikely(b != NULL)) {
1117 dm_bufio_unlock(c);
1118
1119 if (need_submit)
1120 submit_io(b, READ, b->block, read_endio);
1121 dm_bufio_release(b);
1122
1123 dm_bufio_cond_resched();
1124
1125 if (!n_blocks)
1126 goto flush_plug;
1127 dm_bufio_lock(c);
1128 }
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001129 }
1130
1131 dm_bufio_unlock(c);
1132
1133flush_plug:
1134 blk_finish_plug(&plug);
1135}
1136EXPORT_SYMBOL_GPL(dm_bufio_prefetch);
1137
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001138void dm_bufio_release(struct dm_buffer *b)
1139{
1140 struct dm_bufio_client *c = b->c;
1141
1142 dm_bufio_lock(c);
1143
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001144 BUG_ON(!b->hold_count);
1145
1146 b->hold_count--;
1147 if (!b->hold_count) {
1148 wake_up(&c->free_buffer_wait);
1149
1150 /*
1151 * If there were errors on the buffer, and the buffer is not
1152 * to be written, free the buffer. There is no point in caching
1153 * invalid buffer.
1154 */
1155 if ((b->read_error || b->write_error) &&
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001156 !test_bit(B_READING, &b->state) &&
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001157 !test_bit(B_WRITING, &b->state) &&
1158 !test_bit(B_DIRTY, &b->state)) {
1159 __unlink_buffer(b);
1160 __free_buffer_wake(b);
1161 }
1162 }
1163
1164 dm_bufio_unlock(c);
1165}
1166EXPORT_SYMBOL_GPL(dm_bufio_release);
1167
1168void dm_bufio_mark_buffer_dirty(struct dm_buffer *b)
1169{
1170 struct dm_bufio_client *c = b->c;
1171
1172 dm_bufio_lock(c);
1173
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001174 BUG_ON(test_bit(B_READING, &b->state));
1175
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001176 if (!test_and_set_bit(B_DIRTY, &b->state))
1177 __relink_lru(b, LIST_DIRTY);
1178
1179 dm_bufio_unlock(c);
1180}
1181EXPORT_SYMBOL_GPL(dm_bufio_mark_buffer_dirty);
1182
1183void dm_bufio_write_dirty_buffers_async(struct dm_bufio_client *c)
1184{
Mikulas Patocka24809452013-07-10 23:41:18 +01001185 LIST_HEAD(write_list);
1186
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001187 BUG_ON(dm_bufio_in_request());
1188
1189 dm_bufio_lock(c);
Mikulas Patocka24809452013-07-10 23:41:18 +01001190 __write_dirty_buffers_async(c, 0, &write_list);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001191 dm_bufio_unlock(c);
Mikulas Patocka24809452013-07-10 23:41:18 +01001192 __flush_write_list(&write_list);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001193}
1194EXPORT_SYMBOL_GPL(dm_bufio_write_dirty_buffers_async);
1195
1196/*
1197 * For performance, it is essential that the buffers are written asynchronously
1198 * and simultaneously (so that the block layer can merge the writes) and then
1199 * waited upon.
1200 *
1201 * Finally, we flush hardware disk cache.
1202 */
1203int dm_bufio_write_dirty_buffers(struct dm_bufio_client *c)
1204{
1205 int a, f;
1206 unsigned long buffers_processed = 0;
1207 struct dm_buffer *b, *tmp;
1208
Mikulas Patocka24809452013-07-10 23:41:18 +01001209 LIST_HEAD(write_list);
1210
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001211 dm_bufio_lock(c);
Mikulas Patocka24809452013-07-10 23:41:18 +01001212 __write_dirty_buffers_async(c, 0, &write_list);
1213 dm_bufio_unlock(c);
1214 __flush_write_list(&write_list);
1215 dm_bufio_lock(c);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001216
1217again:
1218 list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) {
1219 int dropped_lock = 0;
1220
1221 if (buffers_processed < c->n_buffers[LIST_DIRTY])
1222 buffers_processed++;
1223
1224 BUG_ON(test_bit(B_READING, &b->state));
1225
1226 if (test_bit(B_WRITING, &b->state)) {
1227 if (buffers_processed < c->n_buffers[LIST_DIRTY]) {
1228 dropped_lock = 1;
1229 b->hold_count++;
1230 dm_bufio_unlock(c);
NeilBrown74316202014-07-07 15:16:04 +10001231 wait_on_bit_io(&b->state, B_WRITING,
1232 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001233 dm_bufio_lock(c);
1234 b->hold_count--;
1235 } else
NeilBrown74316202014-07-07 15:16:04 +10001236 wait_on_bit_io(&b->state, B_WRITING,
1237 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001238 }
1239
1240 if (!test_bit(B_DIRTY, &b->state) &&
1241 !test_bit(B_WRITING, &b->state))
1242 __relink_lru(b, LIST_CLEAN);
1243
1244 dm_bufio_cond_resched();
1245
1246 /*
1247 * If we dropped the lock, the list is no longer consistent,
1248 * so we must restart the search.
1249 *
1250 * In the most common case, the buffer just processed is
1251 * relinked to the clean list, so we won't loop scanning the
1252 * same buffer again and again.
1253 *
1254 * This may livelock if there is another thread simultaneously
1255 * dirtying buffers, so we count the number of buffers walked
1256 * and if it exceeds the total number of buffers, it means that
1257 * someone is doing some writes simultaneously with us. In
1258 * this case, stop, dropping the lock.
1259 */
1260 if (dropped_lock)
1261 goto again;
1262 }
1263 wake_up(&c->free_buffer_wait);
1264 dm_bufio_unlock(c);
1265
1266 a = xchg(&c->async_write_error, 0);
1267 f = dm_bufio_issue_flush(c);
1268 if (a)
1269 return a;
1270
1271 return f;
1272}
1273EXPORT_SYMBOL_GPL(dm_bufio_write_dirty_buffers);
1274
1275/*
1276 * Use dm-io to send and empty barrier flush the device.
1277 */
1278int dm_bufio_issue_flush(struct dm_bufio_client *c)
1279{
1280 struct dm_io_request io_req = {
Mikulas Patocka3daec3b2013-03-01 22:45:45 +00001281 .bi_rw = WRITE_FLUSH,
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001282 .mem.type = DM_IO_KMEM,
1283 .mem.ptr.addr = NULL,
1284 .client = c->dm_io,
1285 };
1286 struct dm_io_region io_reg = {
1287 .bdev = c->bdev,
1288 .sector = 0,
1289 .count = 0,
1290 };
1291
1292 BUG_ON(dm_bufio_in_request());
1293
1294 return dm_io(&io_req, 1, &io_reg, NULL);
1295}
1296EXPORT_SYMBOL_GPL(dm_bufio_issue_flush);
1297
1298/*
1299 * We first delete any other buffer that may be at that new location.
1300 *
1301 * Then, we write the buffer to the original location if it was dirty.
1302 *
1303 * Then, if we are the only one who is holding the buffer, relink the buffer
1304 * in the hash queue for the new location.
1305 *
1306 * If there was someone else holding the buffer, we write it to the new
1307 * location but not relink it, because that other user needs to have the buffer
1308 * at the same place.
1309 */
1310void dm_bufio_release_move(struct dm_buffer *b, sector_t new_block)
1311{
1312 struct dm_bufio_client *c = b->c;
1313 struct dm_buffer *new;
1314
1315 BUG_ON(dm_bufio_in_request());
1316
1317 dm_bufio_lock(c);
1318
1319retry:
1320 new = __find(c, new_block);
1321 if (new) {
1322 if (new->hold_count) {
1323 __wait_for_free_buffer(c);
1324 goto retry;
1325 }
1326
1327 /*
1328 * FIXME: Is there any point waiting for a write that's going
1329 * to be overwritten in a bit?
1330 */
1331 __make_buffer_clean(new);
1332 __unlink_buffer(new);
1333 __free_buffer_wake(new);
1334 }
1335
1336 BUG_ON(!b->hold_count);
1337 BUG_ON(test_bit(B_READING, &b->state));
1338
Mikulas Patocka24809452013-07-10 23:41:18 +01001339 __write_dirty_buffer(b, NULL);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001340 if (b->hold_count == 1) {
NeilBrown74316202014-07-07 15:16:04 +10001341 wait_on_bit_io(&b->state, B_WRITING,
1342 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001343 set_bit(B_DIRTY, &b->state);
1344 __unlink_buffer(b);
1345 __link_buffer(b, new_block, LIST_DIRTY);
1346 } else {
1347 sector_t old_block;
NeilBrown74316202014-07-07 15:16:04 +10001348 wait_on_bit_lock_io(&b->state, B_WRITING,
1349 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001350 /*
1351 * Relink buffer to "new_block" so that write_callback
1352 * sees "new_block" as a block number.
1353 * After the write, link the buffer back to old_block.
1354 * All this must be done in bufio lock, so that block number
1355 * change isn't visible to other threads.
1356 */
1357 old_block = b->block;
1358 __unlink_buffer(b);
1359 __link_buffer(b, new_block, b->list_mode);
1360 submit_io(b, WRITE, new_block, write_endio);
NeilBrown74316202014-07-07 15:16:04 +10001361 wait_on_bit_io(&b->state, B_WRITING,
1362 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001363 __unlink_buffer(b);
1364 __link_buffer(b, old_block, b->list_mode);
1365 }
1366
1367 dm_bufio_unlock(c);
1368 dm_bufio_release(b);
1369}
1370EXPORT_SYMBOL_GPL(dm_bufio_release_move);
1371
Mikulas Patocka55494bf2014-01-13 19:12:36 -05001372/*
1373 * Free the given buffer.
1374 *
1375 * This is just a hint, if the buffer is in use or dirty, this function
1376 * does nothing.
1377 */
1378void dm_bufio_forget(struct dm_bufio_client *c, sector_t block)
1379{
1380 struct dm_buffer *b;
1381
1382 dm_bufio_lock(c);
1383
1384 b = __find(c, block);
1385 if (b && likely(!b->hold_count) && likely(!b->state)) {
1386 __unlink_buffer(b);
1387 __free_buffer_wake(b);
1388 }
1389
1390 dm_bufio_unlock(c);
1391}
1392EXPORT_SYMBOL(dm_bufio_forget);
1393
Mikulas Patocka55b082e2014-01-13 19:13:05 -05001394void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned n)
1395{
1396 c->minimum_buffers = n;
1397}
1398EXPORT_SYMBOL(dm_bufio_set_minimum_buffers);
1399
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001400unsigned dm_bufio_get_block_size(struct dm_bufio_client *c)
1401{
1402 return c->block_size;
1403}
1404EXPORT_SYMBOL_GPL(dm_bufio_get_block_size);
1405
1406sector_t dm_bufio_get_device_size(struct dm_bufio_client *c)
1407{
1408 return i_size_read(c->bdev->bd_inode) >>
1409 (SECTOR_SHIFT + c->sectors_per_block_bits);
1410}
1411EXPORT_SYMBOL_GPL(dm_bufio_get_device_size);
1412
1413sector_t dm_bufio_get_block_number(struct dm_buffer *b)
1414{
1415 return b->block;
1416}
1417EXPORT_SYMBOL_GPL(dm_bufio_get_block_number);
1418
1419void *dm_bufio_get_block_data(struct dm_buffer *b)
1420{
1421 return b->data;
1422}
1423EXPORT_SYMBOL_GPL(dm_bufio_get_block_data);
1424
1425void *dm_bufio_get_aux_data(struct dm_buffer *b)
1426{
1427 return b + 1;
1428}
1429EXPORT_SYMBOL_GPL(dm_bufio_get_aux_data);
1430
1431struct dm_bufio_client *dm_bufio_get_client(struct dm_buffer *b)
1432{
1433 return b->c;
1434}
1435EXPORT_SYMBOL_GPL(dm_bufio_get_client);
1436
1437static void drop_buffers(struct dm_bufio_client *c)
1438{
1439 struct dm_buffer *b;
1440 int i;
1441
1442 BUG_ON(dm_bufio_in_request());
1443
1444 /*
1445 * An optimization so that the buffers are not written one-by-one.
1446 */
1447 dm_bufio_write_dirty_buffers_async(c);
1448
1449 dm_bufio_lock(c);
1450
1451 while ((b = __get_unclaimed_buffer(c)))
1452 __free_buffer_wake(b);
1453
1454 for (i = 0; i < LIST_SIZE; i++)
1455 list_for_each_entry(b, &c->lru[i], lru_list)
1456 DMERR("leaked buffer %llx, hold count %u, list %d",
1457 (unsigned long long)b->block, b->hold_count, i);
1458
1459 for (i = 0; i < LIST_SIZE; i++)
1460 BUG_ON(!list_empty(&c->lru[i]));
1461
1462 dm_bufio_unlock(c);
1463}
1464
1465/*
Joe Thornber33096a72014-10-09 11:10:25 +01001466 * We may not be able to evict this buffer if IO pending or the client
1467 * is still using it. Caller is expected to know buffer is too old.
1468 *
Mikulas Patocka9d28eb12014-10-16 14:45:20 -04001469 * And if GFP_NOFS is used, we must not do any I/O because we hold
1470 * dm_bufio_clients_lock and we would risk deadlock if the I/O gets
1471 * rerouted to different bufio client.
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001472 */
Joe Thornber33096a72014-10-09 11:10:25 +01001473static bool __try_evict_buffer(struct dm_buffer *b, gfp_t gfp)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001474{
Mikulas Patocka9d28eb12014-10-16 14:45:20 -04001475 if (!(gfp & __GFP_FS)) {
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001476 if (test_bit(B_READING, &b->state) ||
1477 test_bit(B_WRITING, &b->state) ||
1478 test_bit(B_DIRTY, &b->state))
Joe Thornber33096a72014-10-09 11:10:25 +01001479 return false;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001480 }
1481
1482 if (b->hold_count)
Joe Thornber33096a72014-10-09 11:10:25 +01001483 return false;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001484
1485 __make_buffer_clean(b);
1486 __unlink_buffer(b);
1487 __free_buffer_wake(b);
1488
Joe Thornber33096a72014-10-09 11:10:25 +01001489 return true;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001490}
1491
Joe Thornber33096a72014-10-09 11:10:25 +01001492static unsigned get_retain_buffers(struct dm_bufio_client *c)
1493{
1494 unsigned retain_bytes = ACCESS_ONCE(dm_bufio_retain_bytes);
1495 return retain_bytes / c->block_size;
1496}
1497
1498static unsigned long __scan(struct dm_bufio_client *c, unsigned long nr_to_scan,
1499 gfp_t gfp_mask)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001500{
1501 int l;
1502 struct dm_buffer *b, *tmp;
Joe Thornber33096a72014-10-09 11:10:25 +01001503 unsigned long freed = 0;
1504 unsigned long count = nr_to_scan;
1505 unsigned retain_target = get_retain_buffers(c);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001506
1507 for (l = 0; l < LIST_SIZE; l++) {
Dave Chinner7dc19d52013-08-28 10:18:11 +10001508 list_for_each_entry_safe_reverse(b, tmp, &c->lru[l], lru_list) {
Joe Thornber33096a72014-10-09 11:10:25 +01001509 if (__try_evict_buffer(b, gfp_mask))
1510 freed++;
1511 if (!--nr_to_scan || ((count - freed) <= retain_target))
Mikulas Patocka0e825862014-10-01 13:29:48 -04001512 return freed;
1513 dm_bufio_cond_resched();
Dave Chinner7dc19d52013-08-28 10:18:11 +10001514 }
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001515 }
Dave Chinner7dc19d52013-08-28 10:18:11 +10001516 return freed;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001517}
1518
Dave Chinner7dc19d52013-08-28 10:18:11 +10001519static unsigned long
1520dm_bufio_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001521{
Dave Chinner7dc19d52013-08-28 10:18:11 +10001522 struct dm_bufio_client *c;
1523 unsigned long freed;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001524
Dave Chinner7dc19d52013-08-28 10:18:11 +10001525 c = container_of(shrink, struct dm_bufio_client, shrinker);
Mikulas Patocka9d28eb12014-10-16 14:45:20 -04001526 if (sc->gfp_mask & __GFP_FS)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001527 dm_bufio_lock(c);
1528 else if (!dm_bufio_trylock(c))
Dave Chinner7dc19d52013-08-28 10:18:11 +10001529 return SHRINK_STOP;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001530
Dave Chinner7dc19d52013-08-28 10:18:11 +10001531 freed = __scan(c, sc->nr_to_scan, sc->gfp_mask);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001532 dm_bufio_unlock(c);
Dave Chinner7dc19d52013-08-28 10:18:11 +10001533 return freed;
1534}
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001535
Dave Chinner7dc19d52013-08-28 10:18:11 +10001536static unsigned long
1537dm_bufio_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1538{
1539 struct dm_bufio_client *c;
1540 unsigned long count;
1541
1542 c = container_of(shrink, struct dm_bufio_client, shrinker);
Mikulas Patocka9d28eb12014-10-16 14:45:20 -04001543 if (sc->gfp_mask & __GFP_FS)
Dave Chinner7dc19d52013-08-28 10:18:11 +10001544 dm_bufio_lock(c);
1545 else if (!dm_bufio_trylock(c))
1546 return 0;
1547
1548 count = c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY];
1549 dm_bufio_unlock(c);
1550 return count;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001551}
1552
1553/*
1554 * Create the buffering interface
1555 */
1556struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsigned block_size,
1557 unsigned reserved_buffers, unsigned aux_size,
1558 void (*alloc_callback)(struct dm_buffer *),
1559 void (*write_callback)(struct dm_buffer *))
1560{
1561 int r;
1562 struct dm_bufio_client *c;
1563 unsigned i;
1564
1565 BUG_ON(block_size < 1 << SECTOR_SHIFT ||
1566 (block_size & (block_size - 1)));
1567
Greg Thelend8c712e2014-07-31 09:07:19 -07001568 c = kzalloc(sizeof(*c), GFP_KERNEL);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001569 if (!c) {
1570 r = -ENOMEM;
1571 goto bad_client;
1572 }
Joe Thornber4e420c42014-10-06 13:48:51 +01001573 c->buffer_tree = RB_ROOT;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001574
1575 c->bdev = bdev;
1576 c->block_size = block_size;
1577 c->sectors_per_block_bits = ffs(block_size) - 1 - SECTOR_SHIFT;
1578 c->pages_per_block_bits = (ffs(block_size) - 1 >= PAGE_SHIFT) ?
1579 ffs(block_size) - 1 - PAGE_SHIFT : 0;
1580 c->blocks_per_page_bits = (ffs(block_size) - 1 < PAGE_SHIFT ?
1581 PAGE_SHIFT - (ffs(block_size) - 1) : 0);
1582
1583 c->aux_size = aux_size;
1584 c->alloc_callback = alloc_callback;
1585 c->write_callback = write_callback;
1586
1587 for (i = 0; i < LIST_SIZE; i++) {
1588 INIT_LIST_HEAD(&c->lru[i]);
1589 c->n_buffers[i] = 0;
1590 }
1591
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001592 mutex_init(&c->lock);
1593 INIT_LIST_HEAD(&c->reserved_buffers);
1594 c->need_reserved_buffers = reserved_buffers;
1595
Mikulas Patocka55b082e2014-01-13 19:13:05 -05001596 c->minimum_buffers = DM_BUFIO_MIN_BUFFERS;
1597
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001598 init_waitqueue_head(&c->free_buffer_wait);
1599 c->async_write_error = 0;
1600
1601 c->dm_io = dm_io_client_create();
1602 if (IS_ERR(c->dm_io)) {
1603 r = PTR_ERR(c->dm_io);
1604 goto bad_dm_io;
1605 }
1606
1607 mutex_lock(&dm_bufio_clients_lock);
1608 if (c->blocks_per_page_bits) {
1609 if (!DM_BUFIO_CACHE_NAME(c)) {
1610 DM_BUFIO_CACHE_NAME(c) = kasprintf(GFP_KERNEL, "dm_bufio_cache-%u", c->block_size);
1611 if (!DM_BUFIO_CACHE_NAME(c)) {
1612 r = -ENOMEM;
1613 mutex_unlock(&dm_bufio_clients_lock);
1614 goto bad_cache;
1615 }
1616 }
1617
1618 if (!DM_BUFIO_CACHE(c)) {
1619 DM_BUFIO_CACHE(c) = kmem_cache_create(DM_BUFIO_CACHE_NAME(c),
1620 c->block_size,
1621 c->block_size, 0, NULL);
1622 if (!DM_BUFIO_CACHE(c)) {
1623 r = -ENOMEM;
1624 mutex_unlock(&dm_bufio_clients_lock);
1625 goto bad_cache;
1626 }
1627 }
1628 }
1629 mutex_unlock(&dm_bufio_clients_lock);
1630
1631 while (c->need_reserved_buffers) {
1632 struct dm_buffer *b = alloc_buffer(c, GFP_KERNEL);
1633
1634 if (!b) {
1635 r = -ENOMEM;
1636 goto bad_buffer;
1637 }
1638 __free_buffer_wake(b);
1639 }
1640
1641 mutex_lock(&dm_bufio_clients_lock);
1642 dm_bufio_client_count++;
1643 list_add(&c->client_list, &dm_bufio_all_clients);
1644 __cache_size_refresh();
1645 mutex_unlock(&dm_bufio_clients_lock);
1646
Dave Chinner7dc19d52013-08-28 10:18:11 +10001647 c->shrinker.count_objects = dm_bufio_shrink_count;
1648 c->shrinker.scan_objects = dm_bufio_shrink_scan;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001649 c->shrinker.seeks = 1;
1650 c->shrinker.batch = 0;
1651 register_shrinker(&c->shrinker);
1652
1653 return c;
1654
1655bad_buffer:
1656bad_cache:
1657 while (!list_empty(&c->reserved_buffers)) {
1658 struct dm_buffer *b = list_entry(c->reserved_buffers.next,
1659 struct dm_buffer, lru_list);
1660 list_del(&b->lru_list);
1661 free_buffer(b);
1662 }
1663 dm_io_client_destroy(c->dm_io);
1664bad_dm_io:
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001665 kfree(c);
1666bad_client:
1667 return ERR_PTR(r);
1668}
1669EXPORT_SYMBOL_GPL(dm_bufio_client_create);
1670
1671/*
1672 * Free the buffering interface.
1673 * It is required that there are no references on any buffers.
1674 */
1675void dm_bufio_client_destroy(struct dm_bufio_client *c)
1676{
1677 unsigned i;
1678
1679 drop_buffers(c);
1680
1681 unregister_shrinker(&c->shrinker);
1682
1683 mutex_lock(&dm_bufio_clients_lock);
1684
1685 list_del(&c->client_list);
1686 dm_bufio_client_count--;
1687 __cache_size_refresh();
1688
1689 mutex_unlock(&dm_bufio_clients_lock);
1690
Joe Thornber4e420c42014-10-06 13:48:51 +01001691 BUG_ON(!RB_EMPTY_ROOT(&c->buffer_tree));
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001692 BUG_ON(c->need_reserved_buffers);
1693
1694 while (!list_empty(&c->reserved_buffers)) {
1695 struct dm_buffer *b = list_entry(c->reserved_buffers.next,
1696 struct dm_buffer, lru_list);
1697 list_del(&b->lru_list);
1698 free_buffer(b);
1699 }
1700
1701 for (i = 0; i < LIST_SIZE; i++)
1702 if (c->n_buffers[i])
1703 DMERR("leaked buffer count %d: %ld", i, c->n_buffers[i]);
1704
1705 for (i = 0; i < LIST_SIZE; i++)
1706 BUG_ON(c->n_buffers[i]);
1707
1708 dm_io_client_destroy(c->dm_io);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001709 kfree(c);
1710}
1711EXPORT_SYMBOL_GPL(dm_bufio_client_destroy);
1712
Joe Thornber33096a72014-10-09 11:10:25 +01001713static unsigned get_max_age_hz(void)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001714{
Joe Thornber33096a72014-10-09 11:10:25 +01001715 unsigned max_age = ACCESS_ONCE(dm_bufio_max_age);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001716
Joe Thornber33096a72014-10-09 11:10:25 +01001717 if (max_age > UINT_MAX / HZ)
1718 max_age = UINT_MAX / HZ;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001719
Joe Thornber33096a72014-10-09 11:10:25 +01001720 return max_age * HZ;
1721}
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001722
Joe Thornber33096a72014-10-09 11:10:25 +01001723static bool older_than(struct dm_buffer *b, unsigned long age_hz)
1724{
1725 return (jiffies - b->last_accessed) >= age_hz;
1726}
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001727
Joe Thornber33096a72014-10-09 11:10:25 +01001728static void __evict_old_buffers(struct dm_bufio_client *c, unsigned long age_hz)
1729{
1730 struct dm_buffer *b, *tmp;
1731 unsigned retain_target = get_retain_buffers(c);
1732 unsigned count;
1733
1734 dm_bufio_lock(c);
1735
1736 count = c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY];
1737 list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_CLEAN], lru_list) {
1738 if (count <= retain_target)
1739 break;
1740
1741 if (!older_than(b, age_hz))
1742 break;
1743
1744 if (__try_evict_buffer(b, 0))
1745 count--;
1746
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001747 dm_bufio_cond_resched();
1748 }
Joe Thornber33096a72014-10-09 11:10:25 +01001749
1750 dm_bufio_unlock(c);
1751}
1752
1753static void cleanup_old_buffers(void)
1754{
1755 unsigned long max_age_hz = get_max_age_hz();
1756 struct dm_bufio_client *c;
1757
1758 mutex_lock(&dm_bufio_clients_lock);
1759
1760 list_for_each_entry(c, &dm_bufio_all_clients, client_list)
1761 __evict_old_buffers(c, max_age_hz);
1762
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001763 mutex_unlock(&dm_bufio_clients_lock);
1764}
1765
1766static struct workqueue_struct *dm_bufio_wq;
1767static struct delayed_work dm_bufio_work;
1768
1769static void work_fn(struct work_struct *w)
1770{
1771 cleanup_old_buffers();
1772
1773 queue_delayed_work(dm_bufio_wq, &dm_bufio_work,
1774 DM_BUFIO_WORK_TIMER_SECS * HZ);
1775}
1776
1777/*----------------------------------------------------------------
1778 * Module setup
1779 *--------------------------------------------------------------*/
1780
1781/*
1782 * This is called only once for the whole dm_bufio module.
1783 * It initializes memory limit.
1784 */
1785static int __init dm_bufio_init(void)
1786{
1787 __u64 mem;
1788
Mikulas Patocka4cb57ab2013-12-05 17:33:29 -05001789 dm_bufio_allocated_kmem_cache = 0;
1790 dm_bufio_allocated_get_free_pages = 0;
1791 dm_bufio_allocated_vmalloc = 0;
1792 dm_bufio_current_allocated = 0;
1793
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001794 memset(&dm_bufio_caches, 0, sizeof dm_bufio_caches);
1795 memset(&dm_bufio_cache_names, 0, sizeof dm_bufio_cache_names);
1796
1797 mem = (__u64)((totalram_pages - totalhigh_pages) *
1798 DM_BUFIO_MEMORY_PERCENT / 100) << PAGE_SHIFT;
1799
1800 if (mem > ULONG_MAX)
1801 mem = ULONG_MAX;
1802
1803#ifdef CONFIG_MMU
1804 /*
1805 * Get the size of vmalloc space the same way as VMALLOC_TOTAL
1806 * in fs/proc/internal.h
1807 */
1808 if (mem > (VMALLOC_END - VMALLOC_START) * DM_BUFIO_VMALLOC_PERCENT / 100)
1809 mem = (VMALLOC_END - VMALLOC_START) * DM_BUFIO_VMALLOC_PERCENT / 100;
1810#endif
1811
1812 dm_bufio_default_cache_size = mem;
1813
1814 mutex_lock(&dm_bufio_clients_lock);
1815 __cache_size_refresh();
1816 mutex_unlock(&dm_bufio_clients_lock);
1817
1818 dm_bufio_wq = create_singlethread_workqueue("dm_bufio_cache");
1819 if (!dm_bufio_wq)
1820 return -ENOMEM;
1821
1822 INIT_DELAYED_WORK(&dm_bufio_work, work_fn);
1823 queue_delayed_work(dm_bufio_wq, &dm_bufio_work,
1824 DM_BUFIO_WORK_TIMER_SECS * HZ);
1825
1826 return 0;
1827}
1828
1829/*
1830 * This is called once when unloading the dm_bufio module.
1831 */
1832static void __exit dm_bufio_exit(void)
1833{
1834 int bug = 0;
1835 int i;
1836
1837 cancel_delayed_work_sync(&dm_bufio_work);
1838 destroy_workqueue(dm_bufio_wq);
1839
1840 for (i = 0; i < ARRAY_SIZE(dm_bufio_caches); i++) {
1841 struct kmem_cache *kc = dm_bufio_caches[i];
1842
1843 if (kc)
1844 kmem_cache_destroy(kc);
1845 }
1846
1847 for (i = 0; i < ARRAY_SIZE(dm_bufio_cache_names); i++)
1848 kfree(dm_bufio_cache_names[i]);
1849
1850 if (dm_bufio_client_count) {
1851 DMCRIT("%s: dm_bufio_client_count leaked: %d",
1852 __func__, dm_bufio_client_count);
1853 bug = 1;
1854 }
1855
1856 if (dm_bufio_current_allocated) {
1857 DMCRIT("%s: dm_bufio_current_allocated leaked: %lu",
1858 __func__, dm_bufio_current_allocated);
1859 bug = 1;
1860 }
1861
1862 if (dm_bufio_allocated_get_free_pages) {
1863 DMCRIT("%s: dm_bufio_allocated_get_free_pages leaked: %lu",
1864 __func__, dm_bufio_allocated_get_free_pages);
1865 bug = 1;
1866 }
1867
1868 if (dm_bufio_allocated_vmalloc) {
1869 DMCRIT("%s: dm_bufio_vmalloc leaked: %lu",
1870 __func__, dm_bufio_allocated_vmalloc);
1871 bug = 1;
1872 }
1873
1874 if (bug)
1875 BUG();
1876}
1877
1878module_init(dm_bufio_init)
1879module_exit(dm_bufio_exit)
1880
1881module_param_named(max_cache_size_bytes, dm_bufio_cache_size, ulong, S_IRUGO | S_IWUSR);
1882MODULE_PARM_DESC(max_cache_size_bytes, "Size of metadata cache");
1883
1884module_param_named(max_age_seconds, dm_bufio_max_age, uint, S_IRUGO | S_IWUSR);
1885MODULE_PARM_DESC(max_age_seconds, "Max age of a buffer in seconds");
1886
Joe Thornber33096a72014-10-09 11:10:25 +01001887module_param_named(retain_bytes, dm_bufio_retain_bytes, uint, S_IRUGO | S_IWUSR);
1888MODULE_PARM_DESC(retain_bytes, "Try to keep at least this many bytes cached in memory");
1889
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001890module_param_named(peak_allocated_bytes, dm_bufio_peak_allocated, ulong, S_IRUGO | S_IWUSR);
1891MODULE_PARM_DESC(peak_allocated_bytes, "Tracks the maximum allocated memory");
1892
1893module_param_named(allocated_kmem_cache_bytes, dm_bufio_allocated_kmem_cache, ulong, S_IRUGO);
1894MODULE_PARM_DESC(allocated_kmem_cache_bytes, "Memory allocated with kmem_cache_alloc");
1895
1896module_param_named(allocated_get_free_pages_bytes, dm_bufio_allocated_get_free_pages, ulong, S_IRUGO);
1897MODULE_PARM_DESC(allocated_get_free_pages_bytes, "Memory allocated with get_free_pages");
1898
1899module_param_named(allocated_vmalloc_bytes, dm_bufio_allocated_vmalloc, ulong, S_IRUGO);
1900MODULE_PARM_DESC(allocated_vmalloc_bytes, "Memory allocated with vmalloc");
1901
1902module_param_named(current_allocated_bytes, dm_bufio_current_allocated, ulong, S_IRUGO);
1903MODULE_PARM_DESC(current_allocated_bytes, "Memory currently used by the cache");
1904
1905MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
1906MODULE_DESCRIPTION(DM_NAME " buffered I/O library");
1907MODULE_LICENSE("GPL");