blob: 44f4a8ac95bd5a3a0f7742291c4827a27e27b3b5 [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>
Ingo Molnar5b3cc152017-02-02 20:43:54 +010014#include <linux/sched/mm.h>
Asaf Vertzf4953392015-01-06 15:44:15 +020015#include <linux/jiffies.h>
Mikulas Patocka95d402f2011-10-31 20:19:09 +000016#include <linux/vmalloc.h>
Mikulas Patocka95d402f2011-10-31 20:19:09 +000017#include <linux/shrinker.h>
Stephen Rothwell6f662632011-11-01 18:30:49 +110018#include <linux/module.h>
Joe Thornber4e420c42014-10-06 13:48:51 +010019#include <linux/rbtree.h>
Mikulas Patocka86bad0c2015-11-23 19:20:06 -050020#include <linux/stacktrace.h>
Mikulas Patocka95d402f2011-10-31 20:19:09 +000021
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 */
Joe Thornber33096a72014-10-09 11:10:25 +010041#define DM_BUFIO_WORK_TIMER_SECS 30
Mikulas Patocka95d402f2011-10-31 20:19:09 +000042
43/*
44 * Free buffers when they are older than this (seconds)
45 */
Joe Thornber33096a72014-10-09 11:10:25 +010046#define DM_BUFIO_DEFAULT_AGE_SECS 300
47
48/*
49 * The nr of bytes of cached data to keep around.
50 */
51#define DM_BUFIO_DEFAULT_RETAIN_BYTES (256 * 1024)
Mikulas Patocka95d402f2011-10-31 20:19:09 +000052
53/*
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.
56 */
57#define DM_BUFIO_INLINE_VECS 16
58
59/*
Mikulas Patocka95d402f2011-10-31 20:19:09 +000060 * Don't try to use kmem_cache_alloc for blocks larger than this.
61 * For explanation, see alloc_buffer_data below.
62 */
63#define DM_BUFIO_BLOCK_SIZE_SLAB_LIMIT (PAGE_SIZE >> 1)
64#define DM_BUFIO_BLOCK_SIZE_GFP_LIMIT (PAGE_SIZE << (MAX_ORDER - 1))
65
66/*
67 * dm_buffer->list_mode
68 */
69#define LIST_CLEAN 0
70#define LIST_DIRTY 1
71#define LIST_SIZE 2
72
73/*
74 * Linking of buffers:
75 * All buffers are linked to cache_hash with their hash_list field.
76 *
77 * Clean buffers that are not being written (B_WRITING not set)
78 * are linked to lru[LIST_CLEAN] with their lru_list field.
79 *
80 * Dirty and clean buffers that are being written are linked to
81 * lru[LIST_DIRTY] with their lru_list field. When the write
82 * finishes, the buffer cannot be relinked immediately (because we
83 * are in an interrupt context and relinking requires process
84 * context), so some clean-not-writing buffers can be held on
85 * dirty_lru too. They are later added to lru in the process
86 * context.
87 */
88struct dm_bufio_client {
89 struct mutex lock;
90
91 struct list_head lru[LIST_SIZE];
92 unsigned long n_buffers[LIST_SIZE];
93
94 struct block_device *bdev;
95 unsigned block_size;
96 unsigned char sectors_per_block_bits;
97 unsigned char pages_per_block_bits;
98 unsigned char blocks_per_page_bits;
99 unsigned aux_size;
100 void (*alloc_callback)(struct dm_buffer *);
101 void (*write_callback)(struct dm_buffer *);
102
103 struct dm_io_client *dm_io;
104
105 struct list_head reserved_buffers;
106 unsigned need_reserved_buffers;
107
Mikulas Patocka55b082e2014-01-13 19:13:05 -0500108 unsigned minimum_buffers;
109
Joe Thornber4e420c42014-10-06 13:48:51 +0100110 struct rb_root buffer_tree;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000111 wait_queue_head_t free_buffer_wait;
112
Mikulas Patocka400a0be2017-01-04 20:23:52 +0100113 sector_t start;
114
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000115 int async_write_error;
116
117 struct list_head client_list;
118 struct shrinker shrinker;
119};
120
121/*
122 * Buffer state bits.
123 */
124#define B_READING 0
125#define B_WRITING 1
126#define B_DIRTY 2
127
128/*
129 * Describes how the block was allocated:
130 * kmem_cache_alloc(), __get_free_pages() or vmalloc().
131 * See the comment at alloc_buffer_data.
132 */
133enum data_mode {
134 DATA_MODE_SLAB = 0,
135 DATA_MODE_GET_FREE_PAGES = 1,
136 DATA_MODE_VMALLOC = 2,
137 DATA_MODE_LIMIT = 3
138};
139
140struct dm_buffer {
Joe Thornber4e420c42014-10-06 13:48:51 +0100141 struct rb_node node;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000142 struct list_head lru_list;
143 sector_t block;
144 void *data;
145 enum data_mode data_mode;
146 unsigned char list_mode; /* LIST_* */
147 unsigned hold_count;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200148 blk_status_t read_error;
149 blk_status_t write_error;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000150 unsigned long state;
151 unsigned long last_accessed;
152 struct dm_bufio_client *c;
Mikulas Patocka24809452013-07-10 23:41:18 +0100153 struct list_head write_list;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000154 struct bio bio;
155 struct bio_vec bio_vec[DM_BUFIO_INLINE_VECS];
Mikulas Patocka86bad0c2015-11-23 19:20:06 -0500156#ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
157#define MAX_STACK 10
158 struct stack_trace stack_trace;
159 unsigned long stack_entries[MAX_STACK];
160#endif
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000161};
162
163/*----------------------------------------------------------------*/
164
165static struct kmem_cache *dm_bufio_caches[PAGE_SHIFT - SECTOR_SHIFT];
166static char *dm_bufio_cache_names[PAGE_SHIFT - SECTOR_SHIFT];
167
168static inline int dm_bufio_cache_index(struct dm_bufio_client *c)
169{
170 unsigned ret = c->blocks_per_page_bits - 1;
171
172 BUG_ON(ret >= ARRAY_SIZE(dm_bufio_caches));
173
174 return ret;
175}
176
177#define DM_BUFIO_CACHE(c) (dm_bufio_caches[dm_bufio_cache_index(c)])
178#define DM_BUFIO_CACHE_NAME(c) (dm_bufio_cache_names[dm_bufio_cache_index(c)])
179
180#define dm_bufio_in_request() (!!current->bio_list)
181
182static void dm_bufio_lock(struct dm_bufio_client *c)
183{
184 mutex_lock_nested(&c->lock, dm_bufio_in_request());
185}
186
187static int dm_bufio_trylock(struct dm_bufio_client *c)
188{
189 return mutex_trylock(&c->lock);
190}
191
192static void dm_bufio_unlock(struct dm_bufio_client *c)
193{
194 mutex_unlock(&c->lock);
195}
196
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000197/*----------------------------------------------------------------*/
198
199/*
200 * Default cache size: available memory divided by the ratio.
201 */
202static unsigned long dm_bufio_default_cache_size;
203
204/*
205 * Total cache size set by the user.
206 */
207static unsigned long dm_bufio_cache_size;
208
209/*
210 * A copy of dm_bufio_cache_size because dm_bufio_cache_size can change
211 * at any time. If it disagrees, the user has changed cache size.
212 */
213static unsigned long dm_bufio_cache_size_latch;
214
215static DEFINE_SPINLOCK(param_spinlock);
216
217/*
218 * Buffers are freed after this timeout
219 */
220static unsigned dm_bufio_max_age = DM_BUFIO_DEFAULT_AGE_SECS;
Mikulas Patocka13840d32017-04-30 17:32:28 -0400221static unsigned long dm_bufio_retain_bytes = DM_BUFIO_DEFAULT_RETAIN_BYTES;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000222
223static unsigned long dm_bufio_peak_allocated;
224static unsigned long dm_bufio_allocated_kmem_cache;
225static unsigned long dm_bufio_allocated_get_free_pages;
226static unsigned long dm_bufio_allocated_vmalloc;
227static unsigned long dm_bufio_current_allocated;
228
229/*----------------------------------------------------------------*/
230
231/*
232 * Per-client cache: dm_bufio_cache_size / dm_bufio_client_count
233 */
234static unsigned long dm_bufio_cache_size_per_client;
235
236/*
237 * The current number of clients.
238 */
239static int dm_bufio_client_count;
240
241/*
242 * The list of all clients.
243 */
244static LIST_HEAD(dm_bufio_all_clients);
245
246/*
247 * This mutex protects dm_bufio_cache_size_latch,
248 * dm_bufio_cache_size_per_client and dm_bufio_client_count
249 */
250static DEFINE_MUTEX(dm_bufio_clients_lock);
251
Mikulas Patocka86bad0c2015-11-23 19:20:06 -0500252#ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
253static void buffer_record_stack(struct dm_buffer *b)
254{
255 b->stack_trace.nr_entries = 0;
256 b->stack_trace.max_entries = MAX_STACK;
257 b->stack_trace.entries = b->stack_entries;
258 b->stack_trace.skip = 2;
259 save_stack_trace(&b->stack_trace);
260}
261#endif
262
Joe Thornber4e420c42014-10-06 13:48:51 +0100263/*----------------------------------------------------------------
264 * A red/black tree acts as an index for all the buffers.
265 *--------------------------------------------------------------*/
266static struct dm_buffer *__find(struct dm_bufio_client *c, sector_t block)
267{
268 struct rb_node *n = c->buffer_tree.rb_node;
269 struct dm_buffer *b;
270
271 while (n) {
272 b = container_of(n, struct dm_buffer, node);
273
274 if (b->block == block)
275 return b;
276
277 n = (b->block < block) ? n->rb_left : n->rb_right;
278 }
279
280 return NULL;
281}
282
283static void __insert(struct dm_bufio_client *c, struct dm_buffer *b)
284{
285 struct rb_node **new = &c->buffer_tree.rb_node, *parent = NULL;
286 struct dm_buffer *found;
287
288 while (*new) {
289 found = container_of(*new, struct dm_buffer, node);
290
291 if (found->block == b->block) {
292 BUG_ON(found != b);
293 return;
294 }
295
296 parent = *new;
297 new = (found->block < b->block) ?
298 &((*new)->rb_left) : &((*new)->rb_right);
299 }
300
301 rb_link_node(&b->node, parent, new);
302 rb_insert_color(&b->node, &c->buffer_tree);
303}
304
305static void __remove(struct dm_bufio_client *c, struct dm_buffer *b)
306{
307 rb_erase(&b->node, &c->buffer_tree);
308}
309
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000310/*----------------------------------------------------------------*/
311
312static void adjust_total_allocated(enum data_mode data_mode, long diff)
313{
314 static unsigned long * const class_ptr[DATA_MODE_LIMIT] = {
315 &dm_bufio_allocated_kmem_cache,
316 &dm_bufio_allocated_get_free_pages,
317 &dm_bufio_allocated_vmalloc,
318 };
319
320 spin_lock(&param_spinlock);
321
322 *class_ptr[data_mode] += diff;
323
324 dm_bufio_current_allocated += diff;
325
326 if (dm_bufio_current_allocated > dm_bufio_peak_allocated)
327 dm_bufio_peak_allocated = dm_bufio_current_allocated;
328
329 spin_unlock(&param_spinlock);
330}
331
332/*
333 * Change the number of clients and recalculate per-client limit.
334 */
335static void __cache_size_refresh(void)
336{
337 BUG_ON(!mutex_is_locked(&dm_bufio_clients_lock));
338 BUG_ON(dm_bufio_client_count < 0);
339
Mikulas Patockafe5fe902012-10-12 16:59:46 +0100340 dm_bufio_cache_size_latch = ACCESS_ONCE(dm_bufio_cache_size);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000341
342 /*
343 * Use default if set to 0 and report the actual cache size used.
344 */
345 if (!dm_bufio_cache_size_latch) {
346 (void)cmpxchg(&dm_bufio_cache_size, 0,
347 dm_bufio_default_cache_size);
348 dm_bufio_cache_size_latch = dm_bufio_default_cache_size;
349 }
350
351 dm_bufio_cache_size_per_client = dm_bufio_cache_size_latch /
352 (dm_bufio_client_count ? : 1);
353}
354
355/*
356 * Allocating buffer data.
357 *
358 * Small buffers are allocated with kmem_cache, to use space optimally.
359 *
360 * For large buffers, we choose between get_free_pages and vmalloc.
361 * Each has advantages and disadvantages.
362 *
363 * __get_free_pages can randomly fail if the memory is fragmented.
364 * __vmalloc won't randomly fail, but vmalloc space is limited (it may be
365 * as low as 128M) so using it for caching is not appropriate.
366 *
367 * If the allocation may fail we use __get_free_pages. Memory fragmentation
368 * won't have a fatal effect here, but it just causes flushes of some other
369 * buffers and more I/O will be performed. Don't use __get_free_pages if it
370 * always fails (i.e. order >= MAX_ORDER).
371 *
372 * If the allocation shouldn't fail we use __vmalloc. This is only for the
373 * initial reserve allocation, so there's no risk of wasting all vmalloc
374 * space.
375 */
376static void *alloc_buffer_data(struct dm_bufio_client *c, gfp_t gfp_mask,
377 enum data_mode *data_mode)
378{
Mikulas Patocka502624b2013-05-10 14:37:15 +0100379 unsigned noio_flag;
380 void *ptr;
381
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000382 if (c->block_size <= DM_BUFIO_BLOCK_SIZE_SLAB_LIMIT) {
383 *data_mode = DATA_MODE_SLAB;
384 return kmem_cache_alloc(DM_BUFIO_CACHE(c), gfp_mask);
385 }
386
387 if (c->block_size <= DM_BUFIO_BLOCK_SIZE_GFP_LIMIT &&
388 gfp_mask & __GFP_NORETRY) {
389 *data_mode = DATA_MODE_GET_FREE_PAGES;
390 return (void *)__get_free_pages(gfp_mask,
391 c->pages_per_block_bits);
392 }
393
394 *data_mode = DATA_MODE_VMALLOC;
Mikulas Patocka502624b2013-05-10 14:37:15 +0100395
396 /*
397 * __vmalloc allocates the data pages and auxiliary structures with
398 * gfp_flags that were specified, but pagetables are always allocated
399 * with GFP_KERNEL, no matter what was specified as gfp_mask.
400 *
401 * Consequently, we must set per-process flag PF_MEMALLOC_NOIO so that
402 * all allocations done by this process (including pagetables) are done
403 * as if GFP_NOIO was specified.
404 */
405
406 if (gfp_mask & __GFP_NORETRY)
407 noio_flag = memalloc_noio_save();
408
Michal Hocko19809c22017-05-08 15:57:44 -0700409 ptr = __vmalloc(c->block_size, gfp_mask, PAGE_KERNEL);
Mikulas Patocka502624b2013-05-10 14:37:15 +0100410
411 if (gfp_mask & __GFP_NORETRY)
412 memalloc_noio_restore(noio_flag);
413
414 return ptr;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000415}
416
417/*
418 * Free buffer's data.
419 */
420static void free_buffer_data(struct dm_bufio_client *c,
421 void *data, enum data_mode data_mode)
422{
423 switch (data_mode) {
424 case DATA_MODE_SLAB:
425 kmem_cache_free(DM_BUFIO_CACHE(c), data);
426 break;
427
428 case DATA_MODE_GET_FREE_PAGES:
429 free_pages((unsigned long)data, c->pages_per_block_bits);
430 break;
431
432 case DATA_MODE_VMALLOC:
433 vfree(data);
434 break;
435
436 default:
437 DMCRIT("dm_bufio_free_buffer_data: bad data mode: %d",
438 data_mode);
439 BUG();
440 }
441}
442
443/*
444 * Allocate buffer and its data.
445 */
446static struct dm_buffer *alloc_buffer(struct dm_bufio_client *c, gfp_t gfp_mask)
447{
448 struct dm_buffer *b = kmalloc(sizeof(struct dm_buffer) + c->aux_size,
449 gfp_mask);
450
451 if (!b)
452 return NULL;
453
454 b->c = c;
455
456 b->data = alloc_buffer_data(c, gfp_mask, &b->data_mode);
457 if (!b->data) {
458 kfree(b);
459 return NULL;
460 }
461
462 adjust_total_allocated(b->data_mode, (long)c->block_size);
463
Mikulas Patocka86bad0c2015-11-23 19:20:06 -0500464#ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
465 memset(&b->stack_trace, 0, sizeof(b->stack_trace));
466#endif
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000467 return b;
468}
469
470/*
471 * Free buffer and its data.
472 */
473static void free_buffer(struct dm_buffer *b)
474{
475 struct dm_bufio_client *c = b->c;
476
477 adjust_total_allocated(b->data_mode, -(long)c->block_size);
478
479 free_buffer_data(c, b->data, b->data_mode);
480 kfree(b);
481}
482
483/*
484 * Link buffer to the hash list and clean or dirty queue.
485 */
486static void __link_buffer(struct dm_buffer *b, sector_t block, int dirty)
487{
488 struct dm_bufio_client *c = b->c;
489
490 c->n_buffers[dirty]++;
491 b->block = block;
492 b->list_mode = dirty;
493 list_add(&b->lru_list, &c->lru[dirty]);
Joe Thornber4e420c42014-10-06 13:48:51 +0100494 __insert(b->c, b);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000495 b->last_accessed = jiffies;
496}
497
498/*
499 * Unlink buffer from the hash list and dirty or clean queue.
500 */
501static void __unlink_buffer(struct dm_buffer *b)
502{
503 struct dm_bufio_client *c = b->c;
504
505 BUG_ON(!c->n_buffers[b->list_mode]);
506
507 c->n_buffers[b->list_mode]--;
Joe Thornber4e420c42014-10-06 13:48:51 +0100508 __remove(b->c, b);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000509 list_del(&b->lru_list);
510}
511
512/*
513 * Place the buffer to the head of dirty or clean LRU queue.
514 */
515static void __relink_lru(struct dm_buffer *b, int dirty)
516{
517 struct dm_bufio_client *c = b->c;
518
519 BUG_ON(!c->n_buffers[b->list_mode]);
520
521 c->n_buffers[b->list_mode]--;
522 c->n_buffers[dirty]++;
523 b->list_mode = dirty;
Wei Yongjun54499af2012-10-12 16:59:44 +0100524 list_move(&b->lru_list, &c->lru[dirty]);
Joe Thornbereb76faf2014-09-30 09:32:46 +0100525 b->last_accessed = jiffies;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000526}
527
528/*----------------------------------------------------------------
529 * Submit I/O on the buffer.
530 *
531 * Bio interface is faster but it has some problems:
532 * the vector list is limited (increasing this limit increases
533 * memory-consumption per buffer, so it is not viable);
534 *
535 * the memory must be direct-mapped, not vmalloced;
536 *
537 * the I/O driver can reject requests spuriously if it thinks that
538 * the requests are too big for the device or if they cross a
539 * controller-defined memory boundary.
540 *
541 * If the buffer is small enough (up to DM_BUFIO_INLINE_VECS pages) and
542 * it is not vmalloced, try using the bio interface.
543 *
544 * If the buffer is big, if it is vmalloced or if the underlying device
545 * rejects the bio because it is too large, use dm-io layer to do the I/O.
546 * The dm-io layer splits the I/O into multiple requests, avoiding the above
547 * shortcomings.
548 *--------------------------------------------------------------*/
549
550/*
551 * dm-io completion routine. It just calls b->bio.bi_end_io, pretending
552 * that the request was handled directly with bio interface.
553 */
554static void dmio_complete(unsigned long error, void *context)
555{
556 struct dm_buffer *b = context;
557
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200558 b->bio.bi_status = error ? BLK_STS_IOERR : 0;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200559 b->bio.bi_end_io(&b->bio);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000560}
561
Mikulas Patocka400a0be2017-01-04 20:23:52 +0100562static void use_dmio(struct dm_buffer *b, int rw, sector_t sector,
563 unsigned n_sectors, bio_end_io_t *end_io)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000564{
565 int r;
566 struct dm_io_request io_req = {
Mike Christiee6047142016-06-05 14:32:04 -0500567 .bi_op = rw,
568 .bi_op_flags = 0,
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000569 .notify.fn = dmio_complete,
570 .notify.context = b,
571 .client = b->c->dm_io,
572 };
573 struct dm_io_region region = {
574 .bdev = b->c->bdev,
Mikulas Patocka400a0be2017-01-04 20:23:52 +0100575 .sector = sector,
576 .count = n_sectors,
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000577 };
578
579 if (b->data_mode != DATA_MODE_VMALLOC) {
580 io_req.mem.type = DM_IO_KMEM;
581 io_req.mem.ptr.addr = b->data;
582 } else {
583 io_req.mem.type = DM_IO_VMA;
584 io_req.mem.ptr.vma = b->data;
585 }
586
587 b->bio.bi_end_io = end_io;
588
589 r = dm_io(&io_req, 1, &region, NULL);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200590 if (r) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200591 b->bio.bi_status = errno_to_blk_status(r);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200592 end_io(&b->bio);
593 }
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000594}
595
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200596static void inline_endio(struct bio *bio)
Darrick J. Wong445559c2014-11-25 17:45:15 -0800597{
598 bio_end_io_t *end_fn = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200599 blk_status_t status = bio->bi_status;
Darrick J. Wong445559c2014-11-25 17:45:15 -0800600
601 /*
602 * Reset the bio to free any attached resources
603 * (e.g. bio integrity profiles).
604 */
605 bio_reset(bio);
606
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200607 bio->bi_status = status;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200608 end_fn(bio);
Darrick J. Wong445559c2014-11-25 17:45:15 -0800609}
610
Mikulas Patocka400a0be2017-01-04 20:23:52 +0100611static void use_inline_bio(struct dm_buffer *b, int rw, sector_t sector,
612 unsigned n_sectors, bio_end_io_t *end_io)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000613{
614 char *ptr;
615 int len;
616
Ming Lei3a83f462016-11-22 08:57:21 -0700617 bio_init(&b->bio, b->bio_vec, DM_BUFIO_INLINE_VECS);
Mikulas Patocka400a0be2017-01-04 20:23:52 +0100618 b->bio.bi_iter.bi_sector = sector;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000619 b->bio.bi_bdev = b->c->bdev;
Darrick J. Wong445559c2014-11-25 17:45:15 -0800620 b->bio.bi_end_io = inline_endio;
621 /*
622 * Use of .bi_private isn't a problem here because
623 * the dm_buffer's inline bio is local to bufio.
624 */
625 b->bio.bi_private = end_io;
Mike Christiee6047142016-06-05 14:32:04 -0500626 bio_set_op_attrs(&b->bio, rw, 0);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000627
628 /*
629 * We assume that if len >= PAGE_SIZE ptr is page-aligned.
630 * If len < PAGE_SIZE the buffer doesn't cross page boundary.
631 */
632 ptr = b->data;
Mikulas Patocka400a0be2017-01-04 20:23:52 +0100633 len = n_sectors << SECTOR_SHIFT;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000634
635 if (len >= PAGE_SIZE)
636 BUG_ON((unsigned long)ptr & (PAGE_SIZE - 1));
637 else
638 BUG_ON((unsigned long)ptr & (len - 1));
639
640 do {
641 if (!bio_add_page(&b->bio, virt_to_page(ptr),
642 len < PAGE_SIZE ? len : PAGE_SIZE,
Al Viro756d0972016-01-02 12:45:27 -0500643 offset_in_page(ptr))) {
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000644 BUG_ON(b->c->block_size <= PAGE_SIZE);
Mikulas Patocka400a0be2017-01-04 20:23:52 +0100645 use_dmio(b, rw, sector, n_sectors, end_io);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000646 return;
647 }
648
649 len -= PAGE_SIZE;
650 ptr += PAGE_SIZE;
651 } while (len > 0);
652
Mike Christie4e49ea42016-06-05 14:31:41 -0500653 submit_bio(&b->bio);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000654}
655
Mikulas Patocka400a0be2017-01-04 20:23:52 +0100656static void submit_io(struct dm_buffer *b, int rw, bio_end_io_t *end_io)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000657{
Mikulas Patocka400a0be2017-01-04 20:23:52 +0100658 unsigned n_sectors;
659 sector_t sector;
660
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000661 if (rw == WRITE && b->c->write_callback)
662 b->c->write_callback(b);
663
Mikulas Patocka400a0be2017-01-04 20:23:52 +0100664 sector = (b->block << b->c->sectors_per_block_bits) + b->c->start;
665 n_sectors = 1 << b->c->sectors_per_block_bits;
666
667 if (n_sectors <= ((DM_BUFIO_INLINE_VECS * PAGE_SIZE) >> SECTOR_SHIFT) &&
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000668 b->data_mode != DATA_MODE_VMALLOC)
Mikulas Patocka400a0be2017-01-04 20:23:52 +0100669 use_inline_bio(b, rw, sector, n_sectors, end_io);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000670 else
Mikulas Patocka400a0be2017-01-04 20:23:52 +0100671 use_dmio(b, rw, sector, n_sectors, end_io);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000672}
673
674/*----------------------------------------------------------------
675 * Writing dirty buffers
676 *--------------------------------------------------------------*/
677
678/*
679 * The endio routine for write.
680 *
681 * Set the error, clear B_WRITING bit and wake anyone who was waiting on
682 * it.
683 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200684static void write_endio(struct bio *bio)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000685{
686 struct dm_buffer *b = container_of(bio, struct dm_buffer, bio);
687
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200688 b->write_error = bio->bi_status;
689 if (unlikely(bio->bi_status)) {
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000690 struct dm_bufio_client *c = b->c;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200691
692 (void)cmpxchg(&c->async_write_error, 0,
693 blk_status_to_errno(bio->bi_status));
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000694 }
695
696 BUG_ON(!test_bit(B_WRITING, &b->state));
697
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100698 smp_mb__before_atomic();
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000699 clear_bit(B_WRITING, &b->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100700 smp_mb__after_atomic();
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000701
702 wake_up_bit(&b->state, B_WRITING);
703}
704
705/*
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000706 * Initiate a write on a dirty buffer, but don't wait for it.
707 *
708 * - If the buffer is not dirty, exit.
709 * - If there some previous write going on, wait for it to finish (we can't
710 * have two writes on the same buffer simultaneously).
711 * - Submit our write and don't wait on it. We set B_WRITING indicating
712 * that there is a write in progress.
713 */
Mikulas Patocka24809452013-07-10 23:41:18 +0100714static void __write_dirty_buffer(struct dm_buffer *b,
715 struct list_head *write_list)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000716{
717 if (!test_bit(B_DIRTY, &b->state))
718 return;
719
720 clear_bit(B_DIRTY, &b->state);
NeilBrown74316202014-07-07 15:16:04 +1000721 wait_on_bit_lock_io(&b->state, B_WRITING, TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000722
Mikulas Patocka24809452013-07-10 23:41:18 +0100723 if (!write_list)
Mikulas Patocka400a0be2017-01-04 20:23:52 +0100724 submit_io(b, WRITE, write_endio);
Mikulas Patocka24809452013-07-10 23:41:18 +0100725 else
726 list_add_tail(&b->write_list, write_list);
727}
728
729static void __flush_write_list(struct list_head *write_list)
730{
731 struct blk_plug plug;
732 blk_start_plug(&plug);
733 while (!list_empty(write_list)) {
734 struct dm_buffer *b =
735 list_entry(write_list->next, struct dm_buffer, write_list);
736 list_del(&b->write_list);
Mikulas Patocka400a0be2017-01-04 20:23:52 +0100737 submit_io(b, WRITE, write_endio);
Peter Zijlstra7cd32672016-09-13 10:45:20 +0200738 cond_resched();
Mikulas Patocka24809452013-07-10 23:41:18 +0100739 }
740 blk_finish_plug(&plug);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000741}
742
743/*
744 * Wait until any activity on the buffer finishes. Possibly write the
745 * buffer if it is dirty. When this function finishes, there is no I/O
746 * running on the buffer and the buffer is not dirty.
747 */
748static void __make_buffer_clean(struct dm_buffer *b)
749{
750 BUG_ON(b->hold_count);
751
752 if (!b->state) /* fast case */
753 return;
754
NeilBrown74316202014-07-07 15:16:04 +1000755 wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE);
Mikulas Patocka24809452013-07-10 23:41:18 +0100756 __write_dirty_buffer(b, NULL);
NeilBrown74316202014-07-07 15:16:04 +1000757 wait_on_bit_io(&b->state, B_WRITING, TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000758}
759
760/*
761 * Find some buffer that is not held by anybody, clean it, unlink it and
762 * return it.
763 */
764static struct dm_buffer *__get_unclaimed_buffer(struct dm_bufio_client *c)
765{
766 struct dm_buffer *b;
767
768 list_for_each_entry_reverse(b, &c->lru[LIST_CLEAN], lru_list) {
769 BUG_ON(test_bit(B_WRITING, &b->state));
770 BUG_ON(test_bit(B_DIRTY, &b->state));
771
772 if (!b->hold_count) {
773 __make_buffer_clean(b);
774 __unlink_buffer(b);
775 return b;
776 }
Peter Zijlstra7cd32672016-09-13 10:45:20 +0200777 cond_resched();
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000778 }
779
780 list_for_each_entry_reverse(b, &c->lru[LIST_DIRTY], lru_list) {
781 BUG_ON(test_bit(B_READING, &b->state));
782
783 if (!b->hold_count) {
784 __make_buffer_clean(b);
785 __unlink_buffer(b);
786 return b;
787 }
Peter Zijlstra7cd32672016-09-13 10:45:20 +0200788 cond_resched();
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000789 }
790
791 return NULL;
792}
793
794/*
795 * Wait until some other threads free some buffer or release hold count on
796 * some buffer.
797 *
798 * This function is entered with c->lock held, drops it and regains it
799 * before exiting.
800 */
801static void __wait_for_free_buffer(struct dm_bufio_client *c)
802{
803 DECLARE_WAITQUEUE(wait, current);
804
805 add_wait_queue(&c->free_buffer_wait, &wait);
Davidlohr Bueso642fa442017-01-03 13:43:14 -0800806 set_current_state(TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000807 dm_bufio_unlock(c);
808
809 io_schedule();
810
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000811 remove_wait_queue(&c->free_buffer_wait, &wait);
812
813 dm_bufio_lock(c);
814}
815
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100816enum new_flag {
817 NF_FRESH = 0,
818 NF_READ = 1,
819 NF_GET = 2,
820 NF_PREFETCH = 3
821};
822
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000823/*
824 * Allocate a new buffer. If the allocation is not possible, wait until
825 * some other thread frees a buffer.
826 *
827 * May drop the lock and regain it.
828 */
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100829static struct dm_buffer *__alloc_buffer_wait_no_callback(struct dm_bufio_client *c, enum new_flag nf)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000830{
831 struct dm_buffer *b;
Mikulas Patocka41c73a42016-11-23 17:04:00 -0500832 bool tried_noio_alloc = false;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000833
834 /*
835 * dm-bufio is resistant to allocation failures (it just keeps
836 * one buffer reserved in cases all the allocations fail).
837 * So set flags to not try too hard:
Douglas Anderson9ea61ca2016-11-17 11:24:20 -0800838 * GFP_NOWAIT: don't wait; if we need to sleep we'll release our
839 * mutex and wait ourselves.
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000840 * __GFP_NORETRY: don't retry and rather return failure
841 * __GFP_NOMEMALLOC: don't use emergency reserves
842 * __GFP_NOWARN: don't print a warning in case of failure
843 *
844 * For debugging, if we set the cache size to 1, no new buffers will
845 * be allocated.
846 */
847 while (1) {
848 if (dm_bufio_cache_size_latch != 1) {
Douglas Anderson9ea61ca2016-11-17 11:24:20 -0800849 b = alloc_buffer(c, GFP_NOWAIT | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000850 if (b)
851 return b;
852 }
853
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100854 if (nf == NF_PREFETCH)
855 return NULL;
856
Mikulas Patocka41c73a42016-11-23 17:04:00 -0500857 if (dm_bufio_cache_size_latch != 1 && !tried_noio_alloc) {
858 dm_bufio_unlock(c);
859 b = alloc_buffer(c, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
860 dm_bufio_lock(c);
861 if (b)
862 return b;
863 tried_noio_alloc = true;
864 }
865
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000866 if (!list_empty(&c->reserved_buffers)) {
867 b = list_entry(c->reserved_buffers.next,
868 struct dm_buffer, lru_list);
869 list_del(&b->lru_list);
870 c->need_reserved_buffers++;
871
872 return b;
873 }
874
875 b = __get_unclaimed_buffer(c);
876 if (b)
877 return b;
878
879 __wait_for_free_buffer(c);
880 }
881}
882
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100883static struct dm_buffer *__alloc_buffer_wait(struct dm_bufio_client *c, enum new_flag nf)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000884{
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100885 struct dm_buffer *b = __alloc_buffer_wait_no_callback(c, nf);
886
887 if (!b)
888 return NULL;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000889
890 if (c->alloc_callback)
891 c->alloc_callback(b);
892
893 return b;
894}
895
896/*
897 * Free a buffer and wake other threads waiting for free buffers.
898 */
899static void __free_buffer_wake(struct dm_buffer *b)
900{
901 struct dm_bufio_client *c = b->c;
902
903 if (!c->need_reserved_buffers)
904 free_buffer(b);
905 else {
906 list_add(&b->lru_list, &c->reserved_buffers);
907 c->need_reserved_buffers--;
908 }
909
910 wake_up(&c->free_buffer_wait);
911}
912
Mikulas Patocka24809452013-07-10 23:41:18 +0100913static void __write_dirty_buffers_async(struct dm_bufio_client *c, int no_wait,
914 struct list_head *write_list)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000915{
916 struct dm_buffer *b, *tmp;
917
918 list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) {
919 BUG_ON(test_bit(B_READING, &b->state));
920
921 if (!test_bit(B_DIRTY, &b->state) &&
922 !test_bit(B_WRITING, &b->state)) {
923 __relink_lru(b, LIST_CLEAN);
924 continue;
925 }
926
927 if (no_wait && test_bit(B_WRITING, &b->state))
928 return;
929
Mikulas Patocka24809452013-07-10 23:41:18 +0100930 __write_dirty_buffer(b, write_list);
Peter Zijlstra7cd32672016-09-13 10:45:20 +0200931 cond_resched();
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000932 }
933}
934
935/*
936 * Get writeback threshold and buffer limit for a given client.
937 */
938static void __get_memory_limit(struct dm_bufio_client *c,
939 unsigned long *threshold_buffers,
940 unsigned long *limit_buffers)
941{
942 unsigned long buffers;
943
Mikulas Patocka1b0fb5a2017-04-30 17:33:26 -0400944 if (unlikely(ACCESS_ONCE(dm_bufio_cache_size) != dm_bufio_cache_size_latch)) {
945 if (mutex_trylock(&dm_bufio_clients_lock)) {
946 __cache_size_refresh();
947 mutex_unlock(&dm_bufio_clients_lock);
948 }
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000949 }
950
951 buffers = dm_bufio_cache_size_per_client >>
952 (c->sectors_per_block_bits + SECTOR_SHIFT);
953
Mikulas Patocka55b082e2014-01-13 19:13:05 -0500954 if (buffers < c->minimum_buffers)
955 buffers = c->minimum_buffers;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000956
957 *limit_buffers = buffers;
958 *threshold_buffers = buffers * DM_BUFIO_WRITEBACK_PERCENT / 100;
959}
960
961/*
962 * Check if we're over watermark.
963 * If we are over threshold_buffers, start freeing buffers.
964 * If we're over "limit_buffers", block until we get under the limit.
965 */
Mikulas Patocka24809452013-07-10 23:41:18 +0100966static void __check_watermark(struct dm_bufio_client *c,
967 struct list_head *write_list)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000968{
969 unsigned long threshold_buffers, limit_buffers;
970
971 __get_memory_limit(c, &threshold_buffers, &limit_buffers);
972
973 while (c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY] >
974 limit_buffers) {
975
976 struct dm_buffer *b = __get_unclaimed_buffer(c);
977
978 if (!b)
979 return;
980
981 __free_buffer_wake(b);
Peter Zijlstra7cd32672016-09-13 10:45:20 +0200982 cond_resched();
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000983 }
984
985 if (c->n_buffers[LIST_DIRTY] > threshold_buffers)
Mikulas Patocka24809452013-07-10 23:41:18 +0100986 __write_dirty_buffers_async(c, 1, write_list);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000987}
988
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000989/*----------------------------------------------------------------
990 * Getting a buffer
991 *--------------------------------------------------------------*/
992
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000993static struct dm_buffer *__bufio_new(struct dm_bufio_client *c, sector_t block,
Mikulas Patocka24809452013-07-10 23:41:18 +0100994 enum new_flag nf, int *need_submit,
995 struct list_head *write_list)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000996{
997 struct dm_buffer *b, *new_b = NULL;
998
999 *need_submit = 0;
1000
1001 b = __find(c, block);
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001002 if (b)
1003 goto found_buffer;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001004
1005 if (nf == NF_GET)
1006 return NULL;
1007
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001008 new_b = __alloc_buffer_wait(c, nf);
1009 if (!new_b)
1010 return NULL;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001011
1012 /*
1013 * We've had a period where the mutex was unlocked, so need to
1014 * recheck the hash table.
1015 */
1016 b = __find(c, block);
1017 if (b) {
1018 __free_buffer_wake(new_b);
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001019 goto found_buffer;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001020 }
1021
Mikulas Patocka24809452013-07-10 23:41:18 +01001022 __check_watermark(c, write_list);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001023
1024 b = new_b;
1025 b->hold_count = 1;
1026 b->read_error = 0;
1027 b->write_error = 0;
1028 __link_buffer(b, block, LIST_CLEAN);
1029
1030 if (nf == NF_FRESH) {
1031 b->state = 0;
1032 return b;
1033 }
1034
1035 b->state = 1 << B_READING;
1036 *need_submit = 1;
1037
1038 return b;
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001039
1040found_buffer:
1041 if (nf == NF_PREFETCH)
1042 return NULL;
1043 /*
1044 * Note: it is essential that we don't wait for the buffer to be
1045 * read if dm_bufio_get function is used. Both dm_bufio_get and
1046 * dm_bufio_prefetch can be used in the driver request routine.
1047 * If the user called both dm_bufio_prefetch and dm_bufio_get on
1048 * the same buffer, it would deadlock if we waited.
1049 */
1050 if (nf == NF_GET && unlikely(test_bit(B_READING, &b->state)))
1051 return NULL;
1052
1053 b->hold_count++;
1054 __relink_lru(b, test_bit(B_DIRTY, &b->state) ||
1055 test_bit(B_WRITING, &b->state));
1056 return b;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001057}
1058
1059/*
1060 * The endio routine for reading: set the error, clear the bit and wake up
1061 * anyone waiting on the buffer.
1062 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001063static void read_endio(struct bio *bio)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001064{
1065 struct dm_buffer *b = container_of(bio, struct dm_buffer, bio);
1066
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001067 b->read_error = bio->bi_status;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001068
1069 BUG_ON(!test_bit(B_READING, &b->state));
1070
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001071 smp_mb__before_atomic();
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001072 clear_bit(B_READING, &b->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001073 smp_mb__after_atomic();
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001074
1075 wake_up_bit(&b->state, B_READING);
1076}
1077
1078/*
1079 * A common routine for dm_bufio_new and dm_bufio_read. Operation of these
1080 * functions is similar except that dm_bufio_new doesn't read the
1081 * buffer from the disk (assuming that the caller overwrites all the data
1082 * and uses dm_bufio_mark_buffer_dirty to write new data back).
1083 */
1084static void *new_read(struct dm_bufio_client *c, sector_t block,
1085 enum new_flag nf, struct dm_buffer **bp)
1086{
1087 int need_submit;
1088 struct dm_buffer *b;
1089
Mikulas Patocka24809452013-07-10 23:41:18 +01001090 LIST_HEAD(write_list);
1091
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001092 dm_bufio_lock(c);
Mikulas Patocka24809452013-07-10 23:41:18 +01001093 b = __bufio_new(c, block, nf, &need_submit, &write_list);
Mikulas Patocka86bad0c2015-11-23 19:20:06 -05001094#ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
1095 if (b && b->hold_count == 1)
1096 buffer_record_stack(b);
1097#endif
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001098 dm_bufio_unlock(c);
1099
Mikulas Patocka24809452013-07-10 23:41:18 +01001100 __flush_write_list(&write_list);
1101
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001102 if (!b)
Mikulas Patockaf98c8f72015-11-23 19:11:32 -05001103 return NULL;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001104
1105 if (need_submit)
Mikulas Patocka400a0be2017-01-04 20:23:52 +01001106 submit_io(b, READ, read_endio);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001107
NeilBrown74316202014-07-07 15:16:04 +10001108 wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001109
1110 if (b->read_error) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001111 int error = blk_status_to_errno(b->read_error);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001112
1113 dm_bufio_release(b);
1114
1115 return ERR_PTR(error);
1116 }
1117
1118 *bp = b;
1119
1120 return b->data;
1121}
1122
1123void *dm_bufio_get(struct dm_bufio_client *c, sector_t block,
1124 struct dm_buffer **bp)
1125{
1126 return new_read(c, block, NF_GET, bp);
1127}
1128EXPORT_SYMBOL_GPL(dm_bufio_get);
1129
1130void *dm_bufio_read(struct dm_bufio_client *c, sector_t block,
1131 struct dm_buffer **bp)
1132{
1133 BUG_ON(dm_bufio_in_request());
1134
1135 return new_read(c, block, NF_READ, bp);
1136}
1137EXPORT_SYMBOL_GPL(dm_bufio_read);
1138
1139void *dm_bufio_new(struct dm_bufio_client *c, sector_t block,
1140 struct dm_buffer **bp)
1141{
1142 BUG_ON(dm_bufio_in_request());
1143
1144 return new_read(c, block, NF_FRESH, bp);
1145}
1146EXPORT_SYMBOL_GPL(dm_bufio_new);
1147
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001148void dm_bufio_prefetch(struct dm_bufio_client *c,
1149 sector_t block, unsigned n_blocks)
1150{
1151 struct blk_plug plug;
1152
Mikulas Patocka24809452013-07-10 23:41:18 +01001153 LIST_HEAD(write_list);
1154
Mikulas Patocka3b6b7812013-03-20 17:21:25 +00001155 BUG_ON(dm_bufio_in_request());
1156
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001157 blk_start_plug(&plug);
1158 dm_bufio_lock(c);
1159
1160 for (; n_blocks--; block++) {
1161 int need_submit;
1162 struct dm_buffer *b;
Mikulas Patocka24809452013-07-10 23:41:18 +01001163 b = __bufio_new(c, block, NF_PREFETCH, &need_submit,
1164 &write_list);
1165 if (unlikely(!list_empty(&write_list))) {
1166 dm_bufio_unlock(c);
1167 blk_finish_plug(&plug);
1168 __flush_write_list(&write_list);
1169 blk_start_plug(&plug);
1170 dm_bufio_lock(c);
1171 }
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001172 if (unlikely(b != NULL)) {
1173 dm_bufio_unlock(c);
1174
1175 if (need_submit)
Mikulas Patocka400a0be2017-01-04 20:23:52 +01001176 submit_io(b, READ, read_endio);
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001177 dm_bufio_release(b);
1178
Peter Zijlstra7cd32672016-09-13 10:45:20 +02001179 cond_resched();
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001180
1181 if (!n_blocks)
1182 goto flush_plug;
1183 dm_bufio_lock(c);
1184 }
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001185 }
1186
1187 dm_bufio_unlock(c);
1188
1189flush_plug:
1190 blk_finish_plug(&plug);
1191}
1192EXPORT_SYMBOL_GPL(dm_bufio_prefetch);
1193
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001194void dm_bufio_release(struct dm_buffer *b)
1195{
1196 struct dm_bufio_client *c = b->c;
1197
1198 dm_bufio_lock(c);
1199
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001200 BUG_ON(!b->hold_count);
1201
1202 b->hold_count--;
1203 if (!b->hold_count) {
1204 wake_up(&c->free_buffer_wait);
1205
1206 /*
1207 * If there were errors on the buffer, and the buffer is not
1208 * to be written, free the buffer. There is no point in caching
1209 * invalid buffer.
1210 */
1211 if ((b->read_error || b->write_error) &&
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001212 !test_bit(B_READING, &b->state) &&
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001213 !test_bit(B_WRITING, &b->state) &&
1214 !test_bit(B_DIRTY, &b->state)) {
1215 __unlink_buffer(b);
1216 __free_buffer_wake(b);
1217 }
1218 }
1219
1220 dm_bufio_unlock(c);
1221}
1222EXPORT_SYMBOL_GPL(dm_bufio_release);
1223
1224void dm_bufio_mark_buffer_dirty(struct dm_buffer *b)
1225{
1226 struct dm_bufio_client *c = b->c;
1227
1228 dm_bufio_lock(c);
1229
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001230 BUG_ON(test_bit(B_READING, &b->state));
1231
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001232 if (!test_and_set_bit(B_DIRTY, &b->state))
1233 __relink_lru(b, LIST_DIRTY);
1234
1235 dm_bufio_unlock(c);
1236}
1237EXPORT_SYMBOL_GPL(dm_bufio_mark_buffer_dirty);
1238
1239void dm_bufio_write_dirty_buffers_async(struct dm_bufio_client *c)
1240{
Mikulas Patocka24809452013-07-10 23:41:18 +01001241 LIST_HEAD(write_list);
1242
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001243 BUG_ON(dm_bufio_in_request());
1244
1245 dm_bufio_lock(c);
Mikulas Patocka24809452013-07-10 23:41:18 +01001246 __write_dirty_buffers_async(c, 0, &write_list);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001247 dm_bufio_unlock(c);
Mikulas Patocka24809452013-07-10 23:41:18 +01001248 __flush_write_list(&write_list);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001249}
1250EXPORT_SYMBOL_GPL(dm_bufio_write_dirty_buffers_async);
1251
1252/*
1253 * For performance, it is essential that the buffers are written asynchronously
1254 * and simultaneously (so that the block layer can merge the writes) and then
1255 * waited upon.
1256 *
1257 * Finally, we flush hardware disk cache.
1258 */
1259int dm_bufio_write_dirty_buffers(struct dm_bufio_client *c)
1260{
Dan Carpenteredc11d42017-07-12 10:26:34 +03001261 int a, f;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001262 unsigned long buffers_processed = 0;
1263 struct dm_buffer *b, *tmp;
1264
Mikulas Patocka24809452013-07-10 23:41:18 +01001265 LIST_HEAD(write_list);
1266
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001267 dm_bufio_lock(c);
Mikulas Patocka24809452013-07-10 23:41:18 +01001268 __write_dirty_buffers_async(c, 0, &write_list);
1269 dm_bufio_unlock(c);
1270 __flush_write_list(&write_list);
1271 dm_bufio_lock(c);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001272
1273again:
1274 list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) {
1275 int dropped_lock = 0;
1276
1277 if (buffers_processed < c->n_buffers[LIST_DIRTY])
1278 buffers_processed++;
1279
1280 BUG_ON(test_bit(B_READING, &b->state));
1281
1282 if (test_bit(B_WRITING, &b->state)) {
1283 if (buffers_processed < c->n_buffers[LIST_DIRTY]) {
1284 dropped_lock = 1;
1285 b->hold_count++;
1286 dm_bufio_unlock(c);
NeilBrown74316202014-07-07 15:16:04 +10001287 wait_on_bit_io(&b->state, B_WRITING,
1288 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001289 dm_bufio_lock(c);
1290 b->hold_count--;
1291 } else
NeilBrown74316202014-07-07 15:16:04 +10001292 wait_on_bit_io(&b->state, B_WRITING,
1293 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001294 }
1295
1296 if (!test_bit(B_DIRTY, &b->state) &&
1297 !test_bit(B_WRITING, &b->state))
1298 __relink_lru(b, LIST_CLEAN);
1299
Peter Zijlstra7cd32672016-09-13 10:45:20 +02001300 cond_resched();
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001301
1302 /*
1303 * If we dropped the lock, the list is no longer consistent,
1304 * so we must restart the search.
1305 *
1306 * In the most common case, the buffer just processed is
1307 * relinked to the clean list, so we won't loop scanning the
1308 * same buffer again and again.
1309 *
1310 * This may livelock if there is another thread simultaneously
1311 * dirtying buffers, so we count the number of buffers walked
1312 * and if it exceeds the total number of buffers, it means that
1313 * someone is doing some writes simultaneously with us. In
1314 * this case, stop, dropping the lock.
1315 */
1316 if (dropped_lock)
1317 goto again;
1318 }
1319 wake_up(&c->free_buffer_wait);
1320 dm_bufio_unlock(c);
1321
1322 a = xchg(&c->async_write_error, 0);
1323 f = dm_bufio_issue_flush(c);
1324 if (a)
1325 return a;
1326
1327 return f;
1328}
1329EXPORT_SYMBOL_GPL(dm_bufio_write_dirty_buffers);
1330
1331/*
1332 * Use dm-io to send and empty barrier flush the device.
1333 */
1334int dm_bufio_issue_flush(struct dm_bufio_client *c)
1335{
1336 struct dm_io_request io_req = {
Mike Christiee6047142016-06-05 14:32:04 -05001337 .bi_op = REQ_OP_WRITE,
Jan Karaff0361b2017-05-31 09:44:32 +02001338 .bi_op_flags = REQ_PREFLUSH | REQ_SYNC,
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001339 .mem.type = DM_IO_KMEM,
1340 .mem.ptr.addr = NULL,
1341 .client = c->dm_io,
1342 };
1343 struct dm_io_region io_reg = {
1344 .bdev = c->bdev,
1345 .sector = 0,
1346 .count = 0,
1347 };
1348
1349 BUG_ON(dm_bufio_in_request());
1350
1351 return dm_io(&io_req, 1, &io_reg, NULL);
1352}
1353EXPORT_SYMBOL_GPL(dm_bufio_issue_flush);
1354
1355/*
1356 * We first delete any other buffer that may be at that new location.
1357 *
1358 * Then, we write the buffer to the original location if it was dirty.
1359 *
1360 * Then, if we are the only one who is holding the buffer, relink the buffer
1361 * in the hash queue for the new location.
1362 *
1363 * If there was someone else holding the buffer, we write it to the new
1364 * location but not relink it, because that other user needs to have the buffer
1365 * at the same place.
1366 */
1367void dm_bufio_release_move(struct dm_buffer *b, sector_t new_block)
1368{
1369 struct dm_bufio_client *c = b->c;
1370 struct dm_buffer *new;
1371
1372 BUG_ON(dm_bufio_in_request());
1373
1374 dm_bufio_lock(c);
1375
1376retry:
1377 new = __find(c, new_block);
1378 if (new) {
1379 if (new->hold_count) {
1380 __wait_for_free_buffer(c);
1381 goto retry;
1382 }
1383
1384 /*
1385 * FIXME: Is there any point waiting for a write that's going
1386 * to be overwritten in a bit?
1387 */
1388 __make_buffer_clean(new);
1389 __unlink_buffer(new);
1390 __free_buffer_wake(new);
1391 }
1392
1393 BUG_ON(!b->hold_count);
1394 BUG_ON(test_bit(B_READING, &b->state));
1395
Mikulas Patocka24809452013-07-10 23:41:18 +01001396 __write_dirty_buffer(b, NULL);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001397 if (b->hold_count == 1) {
NeilBrown74316202014-07-07 15:16:04 +10001398 wait_on_bit_io(&b->state, B_WRITING,
1399 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001400 set_bit(B_DIRTY, &b->state);
1401 __unlink_buffer(b);
1402 __link_buffer(b, new_block, LIST_DIRTY);
1403 } else {
1404 sector_t old_block;
NeilBrown74316202014-07-07 15:16:04 +10001405 wait_on_bit_lock_io(&b->state, B_WRITING,
1406 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001407 /*
1408 * Relink buffer to "new_block" so that write_callback
1409 * sees "new_block" as a block number.
1410 * After the write, link the buffer back to old_block.
1411 * All this must be done in bufio lock, so that block number
1412 * change isn't visible to other threads.
1413 */
1414 old_block = b->block;
1415 __unlink_buffer(b);
1416 __link_buffer(b, new_block, b->list_mode);
Mikulas Patocka400a0be2017-01-04 20:23:52 +01001417 submit_io(b, WRITE, write_endio);
NeilBrown74316202014-07-07 15:16:04 +10001418 wait_on_bit_io(&b->state, B_WRITING,
1419 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001420 __unlink_buffer(b);
1421 __link_buffer(b, old_block, b->list_mode);
1422 }
1423
1424 dm_bufio_unlock(c);
1425 dm_bufio_release(b);
1426}
1427EXPORT_SYMBOL_GPL(dm_bufio_release_move);
1428
Mikulas Patocka55494bf2014-01-13 19:12:36 -05001429/*
1430 * Free the given buffer.
1431 *
1432 * This is just a hint, if the buffer is in use or dirty, this function
1433 * does nothing.
1434 */
1435void dm_bufio_forget(struct dm_bufio_client *c, sector_t block)
1436{
1437 struct dm_buffer *b;
1438
1439 dm_bufio_lock(c);
1440
1441 b = __find(c, block);
1442 if (b && likely(!b->hold_count) && likely(!b->state)) {
1443 __unlink_buffer(b);
1444 __free_buffer_wake(b);
1445 }
1446
1447 dm_bufio_unlock(c);
1448}
1449EXPORT_SYMBOL(dm_bufio_forget);
1450
Mikulas Patocka55b082e2014-01-13 19:13:05 -05001451void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned n)
1452{
1453 c->minimum_buffers = n;
1454}
1455EXPORT_SYMBOL(dm_bufio_set_minimum_buffers);
1456
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001457unsigned dm_bufio_get_block_size(struct dm_bufio_client *c)
1458{
1459 return c->block_size;
1460}
1461EXPORT_SYMBOL_GPL(dm_bufio_get_block_size);
1462
1463sector_t dm_bufio_get_device_size(struct dm_bufio_client *c)
1464{
1465 return i_size_read(c->bdev->bd_inode) >>
1466 (SECTOR_SHIFT + c->sectors_per_block_bits);
1467}
1468EXPORT_SYMBOL_GPL(dm_bufio_get_device_size);
1469
1470sector_t dm_bufio_get_block_number(struct dm_buffer *b)
1471{
1472 return b->block;
1473}
1474EXPORT_SYMBOL_GPL(dm_bufio_get_block_number);
1475
1476void *dm_bufio_get_block_data(struct dm_buffer *b)
1477{
1478 return b->data;
1479}
1480EXPORT_SYMBOL_GPL(dm_bufio_get_block_data);
1481
1482void *dm_bufio_get_aux_data(struct dm_buffer *b)
1483{
1484 return b + 1;
1485}
1486EXPORT_SYMBOL_GPL(dm_bufio_get_aux_data);
1487
1488struct dm_bufio_client *dm_bufio_get_client(struct dm_buffer *b)
1489{
1490 return b->c;
1491}
1492EXPORT_SYMBOL_GPL(dm_bufio_get_client);
1493
1494static void drop_buffers(struct dm_bufio_client *c)
1495{
1496 struct dm_buffer *b;
1497 int i;
Mikulas Patocka86bad0c2015-11-23 19:20:06 -05001498 bool warned = false;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001499
1500 BUG_ON(dm_bufio_in_request());
1501
1502 /*
1503 * An optimization so that the buffers are not written one-by-one.
1504 */
1505 dm_bufio_write_dirty_buffers_async(c);
1506
1507 dm_bufio_lock(c);
1508
1509 while ((b = __get_unclaimed_buffer(c)))
1510 __free_buffer_wake(b);
1511
1512 for (i = 0; i < LIST_SIZE; i++)
Mikulas Patocka86bad0c2015-11-23 19:20:06 -05001513 list_for_each_entry(b, &c->lru[i], lru_list) {
1514 WARN_ON(!warned);
1515 warned = true;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001516 DMERR("leaked buffer %llx, hold count %u, list %d",
1517 (unsigned long long)b->block, b->hold_count, i);
Mikulas Patocka86bad0c2015-11-23 19:20:06 -05001518#ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
1519 print_stack_trace(&b->stack_trace, 1);
1520 b->hold_count = 0; /* mark unclaimed to avoid BUG_ON below */
1521#endif
1522 }
1523
1524#ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
1525 while ((b = __get_unclaimed_buffer(c)))
1526 __free_buffer_wake(b);
1527#endif
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001528
1529 for (i = 0; i < LIST_SIZE; i++)
1530 BUG_ON(!list_empty(&c->lru[i]));
1531
1532 dm_bufio_unlock(c);
1533}
1534
1535/*
Joe Thornber33096a72014-10-09 11:10:25 +01001536 * We may not be able to evict this buffer if IO pending or the client
1537 * is still using it. Caller is expected to know buffer is too old.
1538 *
Mikulas Patocka9d28eb12014-10-16 14:45:20 -04001539 * And if GFP_NOFS is used, we must not do any I/O because we hold
1540 * dm_bufio_clients_lock and we would risk deadlock if the I/O gets
1541 * rerouted to different bufio client.
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001542 */
Joe Thornber33096a72014-10-09 11:10:25 +01001543static bool __try_evict_buffer(struct dm_buffer *b, gfp_t gfp)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001544{
Mikulas Patocka9d28eb12014-10-16 14:45:20 -04001545 if (!(gfp & __GFP_FS)) {
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001546 if (test_bit(B_READING, &b->state) ||
1547 test_bit(B_WRITING, &b->state) ||
1548 test_bit(B_DIRTY, &b->state))
Joe Thornber33096a72014-10-09 11:10:25 +01001549 return false;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001550 }
1551
1552 if (b->hold_count)
Joe Thornber33096a72014-10-09 11:10:25 +01001553 return false;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001554
1555 __make_buffer_clean(b);
1556 __unlink_buffer(b);
1557 __free_buffer_wake(b);
1558
Joe Thornber33096a72014-10-09 11:10:25 +01001559 return true;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001560}
1561
Mikulas Patocka13840d32017-04-30 17:32:28 -04001562static unsigned long get_retain_buffers(struct dm_bufio_client *c)
Joe Thornber33096a72014-10-09 11:10:25 +01001563{
Mikulas Patocka13840d32017-04-30 17:32:28 -04001564 unsigned long retain_bytes = ACCESS_ONCE(dm_bufio_retain_bytes);
1565 return retain_bytes >> (c->sectors_per_block_bits + SECTOR_SHIFT);
Joe Thornber33096a72014-10-09 11:10:25 +01001566}
1567
1568static unsigned long __scan(struct dm_bufio_client *c, unsigned long nr_to_scan,
1569 gfp_t gfp_mask)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001570{
1571 int l;
1572 struct dm_buffer *b, *tmp;
Joe Thornber33096a72014-10-09 11:10:25 +01001573 unsigned long freed = 0;
1574 unsigned long count = nr_to_scan;
Mikulas Patocka13840d32017-04-30 17:32:28 -04001575 unsigned long retain_target = get_retain_buffers(c);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001576
1577 for (l = 0; l < LIST_SIZE; l++) {
Dave Chinner7dc19d52013-08-28 10:18:11 +10001578 list_for_each_entry_safe_reverse(b, tmp, &c->lru[l], lru_list) {
Joe Thornber33096a72014-10-09 11:10:25 +01001579 if (__try_evict_buffer(b, gfp_mask))
1580 freed++;
1581 if (!--nr_to_scan || ((count - freed) <= retain_target))
Mikulas Patocka0e825862014-10-01 13:29:48 -04001582 return freed;
Peter Zijlstra7cd32672016-09-13 10:45:20 +02001583 cond_resched();
Dave Chinner7dc19d52013-08-28 10:18:11 +10001584 }
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001585 }
Dave Chinner7dc19d52013-08-28 10:18:11 +10001586 return freed;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001587}
1588
Dave Chinner7dc19d52013-08-28 10:18:11 +10001589static unsigned long
1590dm_bufio_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001591{
Dave Chinner7dc19d52013-08-28 10:18:11 +10001592 struct dm_bufio_client *c;
1593 unsigned long freed;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001594
Dave Chinner7dc19d52013-08-28 10:18:11 +10001595 c = container_of(shrink, struct dm_bufio_client, shrinker);
Mikulas Patocka9d28eb12014-10-16 14:45:20 -04001596 if (sc->gfp_mask & __GFP_FS)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001597 dm_bufio_lock(c);
1598 else if (!dm_bufio_trylock(c))
Dave Chinner7dc19d52013-08-28 10:18:11 +10001599 return SHRINK_STOP;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001600
Dave Chinner7dc19d52013-08-28 10:18:11 +10001601 freed = __scan(c, sc->nr_to_scan, sc->gfp_mask);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001602 dm_bufio_unlock(c);
Dave Chinner7dc19d52013-08-28 10:18:11 +10001603 return freed;
1604}
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001605
Dave Chinner7dc19d52013-08-28 10:18:11 +10001606static unsigned long
1607dm_bufio_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1608{
Mikulas Patockad12067f2016-11-23 16:52:01 -05001609 struct dm_bufio_client *c = container_of(shrink, struct dm_bufio_client, shrinker);
Dave Chinner7dc19d52013-08-28 10:18:11 +10001610
Mikulas Patockad12067f2016-11-23 16:52:01 -05001611 return ACCESS_ONCE(c->n_buffers[LIST_CLEAN]) + ACCESS_ONCE(c->n_buffers[LIST_DIRTY]);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001612}
1613
1614/*
1615 * Create the buffering interface
1616 */
1617struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsigned block_size,
1618 unsigned reserved_buffers, unsigned aux_size,
1619 void (*alloc_callback)(struct dm_buffer *),
1620 void (*write_callback)(struct dm_buffer *))
1621{
1622 int r;
1623 struct dm_bufio_client *c;
1624 unsigned i;
1625
1626 BUG_ON(block_size < 1 << SECTOR_SHIFT ||
1627 (block_size & (block_size - 1)));
1628
Greg Thelend8c712e2014-07-31 09:07:19 -07001629 c = kzalloc(sizeof(*c), GFP_KERNEL);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001630 if (!c) {
1631 r = -ENOMEM;
1632 goto bad_client;
1633 }
Joe Thornber4e420c42014-10-06 13:48:51 +01001634 c->buffer_tree = RB_ROOT;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001635
1636 c->bdev = bdev;
1637 c->block_size = block_size;
Mikulas Patockaa3d939a2015-10-02 11:21:24 -04001638 c->sectors_per_block_bits = __ffs(block_size) - SECTOR_SHIFT;
1639 c->pages_per_block_bits = (__ffs(block_size) >= PAGE_SHIFT) ?
1640 __ffs(block_size) - PAGE_SHIFT : 0;
1641 c->blocks_per_page_bits = (__ffs(block_size) < PAGE_SHIFT ?
1642 PAGE_SHIFT - __ffs(block_size) : 0);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001643
1644 c->aux_size = aux_size;
1645 c->alloc_callback = alloc_callback;
1646 c->write_callback = write_callback;
1647
1648 for (i = 0; i < LIST_SIZE; i++) {
1649 INIT_LIST_HEAD(&c->lru[i]);
1650 c->n_buffers[i] = 0;
1651 }
1652
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001653 mutex_init(&c->lock);
1654 INIT_LIST_HEAD(&c->reserved_buffers);
1655 c->need_reserved_buffers = reserved_buffers;
1656
Mikulas Patocka55b082e2014-01-13 19:13:05 -05001657 c->minimum_buffers = DM_BUFIO_MIN_BUFFERS;
1658
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001659 init_waitqueue_head(&c->free_buffer_wait);
1660 c->async_write_error = 0;
1661
1662 c->dm_io = dm_io_client_create();
1663 if (IS_ERR(c->dm_io)) {
1664 r = PTR_ERR(c->dm_io);
1665 goto bad_dm_io;
1666 }
1667
1668 mutex_lock(&dm_bufio_clients_lock);
1669 if (c->blocks_per_page_bits) {
1670 if (!DM_BUFIO_CACHE_NAME(c)) {
1671 DM_BUFIO_CACHE_NAME(c) = kasprintf(GFP_KERNEL, "dm_bufio_cache-%u", c->block_size);
1672 if (!DM_BUFIO_CACHE_NAME(c)) {
1673 r = -ENOMEM;
1674 mutex_unlock(&dm_bufio_clients_lock);
1675 goto bad_cache;
1676 }
1677 }
1678
1679 if (!DM_BUFIO_CACHE(c)) {
1680 DM_BUFIO_CACHE(c) = kmem_cache_create(DM_BUFIO_CACHE_NAME(c),
1681 c->block_size,
1682 c->block_size, 0, NULL);
1683 if (!DM_BUFIO_CACHE(c)) {
1684 r = -ENOMEM;
1685 mutex_unlock(&dm_bufio_clients_lock);
1686 goto bad_cache;
1687 }
1688 }
1689 }
1690 mutex_unlock(&dm_bufio_clients_lock);
1691
1692 while (c->need_reserved_buffers) {
1693 struct dm_buffer *b = alloc_buffer(c, GFP_KERNEL);
1694
1695 if (!b) {
1696 r = -ENOMEM;
1697 goto bad_buffer;
1698 }
1699 __free_buffer_wake(b);
1700 }
1701
1702 mutex_lock(&dm_bufio_clients_lock);
1703 dm_bufio_client_count++;
1704 list_add(&c->client_list, &dm_bufio_all_clients);
1705 __cache_size_refresh();
1706 mutex_unlock(&dm_bufio_clients_lock);
1707
Dave Chinner7dc19d52013-08-28 10:18:11 +10001708 c->shrinker.count_objects = dm_bufio_shrink_count;
1709 c->shrinker.scan_objects = dm_bufio_shrink_scan;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001710 c->shrinker.seeks = 1;
1711 c->shrinker.batch = 0;
1712 register_shrinker(&c->shrinker);
1713
1714 return c;
1715
1716bad_buffer:
1717bad_cache:
1718 while (!list_empty(&c->reserved_buffers)) {
1719 struct dm_buffer *b = list_entry(c->reserved_buffers.next,
1720 struct dm_buffer, lru_list);
1721 list_del(&b->lru_list);
1722 free_buffer(b);
1723 }
1724 dm_io_client_destroy(c->dm_io);
1725bad_dm_io:
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001726 kfree(c);
1727bad_client:
1728 return ERR_PTR(r);
1729}
1730EXPORT_SYMBOL_GPL(dm_bufio_client_create);
1731
1732/*
1733 * Free the buffering interface.
1734 * It is required that there are no references on any buffers.
1735 */
1736void dm_bufio_client_destroy(struct dm_bufio_client *c)
1737{
1738 unsigned i;
1739
1740 drop_buffers(c);
1741
1742 unregister_shrinker(&c->shrinker);
1743
1744 mutex_lock(&dm_bufio_clients_lock);
1745
1746 list_del(&c->client_list);
1747 dm_bufio_client_count--;
1748 __cache_size_refresh();
1749
1750 mutex_unlock(&dm_bufio_clients_lock);
1751
Joe Thornber4e420c42014-10-06 13:48:51 +01001752 BUG_ON(!RB_EMPTY_ROOT(&c->buffer_tree));
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001753 BUG_ON(c->need_reserved_buffers);
1754
1755 while (!list_empty(&c->reserved_buffers)) {
1756 struct dm_buffer *b = list_entry(c->reserved_buffers.next,
1757 struct dm_buffer, lru_list);
1758 list_del(&b->lru_list);
1759 free_buffer(b);
1760 }
1761
1762 for (i = 0; i < LIST_SIZE; i++)
1763 if (c->n_buffers[i])
1764 DMERR("leaked buffer count %d: %ld", i, c->n_buffers[i]);
1765
1766 for (i = 0; i < LIST_SIZE; i++)
1767 BUG_ON(c->n_buffers[i]);
1768
1769 dm_io_client_destroy(c->dm_io);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001770 kfree(c);
1771}
1772EXPORT_SYMBOL_GPL(dm_bufio_client_destroy);
1773
Mikulas Patocka400a0be2017-01-04 20:23:52 +01001774void dm_bufio_set_sector_offset(struct dm_bufio_client *c, sector_t start)
1775{
1776 c->start = start;
1777}
1778EXPORT_SYMBOL_GPL(dm_bufio_set_sector_offset);
1779
Joe Thornber33096a72014-10-09 11:10:25 +01001780static unsigned get_max_age_hz(void)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001781{
Joe Thornber33096a72014-10-09 11:10:25 +01001782 unsigned max_age = ACCESS_ONCE(dm_bufio_max_age);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001783
Joe Thornber33096a72014-10-09 11:10:25 +01001784 if (max_age > UINT_MAX / HZ)
1785 max_age = UINT_MAX / HZ;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001786
Joe Thornber33096a72014-10-09 11:10:25 +01001787 return max_age * HZ;
1788}
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001789
Joe Thornber33096a72014-10-09 11:10:25 +01001790static bool older_than(struct dm_buffer *b, unsigned long age_hz)
1791{
Asaf Vertzf4953392015-01-06 15:44:15 +02001792 return time_after_eq(jiffies, b->last_accessed + age_hz);
Joe Thornber33096a72014-10-09 11:10:25 +01001793}
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001794
Joe Thornber33096a72014-10-09 11:10:25 +01001795static void __evict_old_buffers(struct dm_bufio_client *c, unsigned long age_hz)
1796{
1797 struct dm_buffer *b, *tmp;
Mikulas Patocka13840d32017-04-30 17:32:28 -04001798 unsigned long retain_target = get_retain_buffers(c);
1799 unsigned long count;
Mikulas Patocka390020a2017-04-30 17:34:53 -04001800 LIST_HEAD(write_list);
Joe Thornber33096a72014-10-09 11:10:25 +01001801
1802 dm_bufio_lock(c);
1803
Mikulas Patocka390020a2017-04-30 17:34:53 -04001804 __check_watermark(c, &write_list);
1805 if (unlikely(!list_empty(&write_list))) {
1806 dm_bufio_unlock(c);
1807 __flush_write_list(&write_list);
1808 dm_bufio_lock(c);
1809 }
1810
Joe Thornber33096a72014-10-09 11:10:25 +01001811 count = c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY];
1812 list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_CLEAN], lru_list) {
1813 if (count <= retain_target)
1814 break;
1815
1816 if (!older_than(b, age_hz))
1817 break;
1818
1819 if (__try_evict_buffer(b, 0))
1820 count--;
1821
Peter Zijlstra7cd32672016-09-13 10:45:20 +02001822 cond_resched();
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001823 }
Joe Thornber33096a72014-10-09 11:10:25 +01001824
1825 dm_bufio_unlock(c);
1826}
1827
1828static void cleanup_old_buffers(void)
1829{
1830 unsigned long max_age_hz = get_max_age_hz();
1831 struct dm_bufio_client *c;
1832
1833 mutex_lock(&dm_bufio_clients_lock);
1834
Mikulas Patocka390020a2017-04-30 17:34:53 -04001835 __cache_size_refresh();
1836
Joe Thornber33096a72014-10-09 11:10:25 +01001837 list_for_each_entry(c, &dm_bufio_all_clients, client_list)
1838 __evict_old_buffers(c, max_age_hz);
1839
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001840 mutex_unlock(&dm_bufio_clients_lock);
1841}
1842
1843static struct workqueue_struct *dm_bufio_wq;
1844static struct delayed_work dm_bufio_work;
1845
1846static void work_fn(struct work_struct *w)
1847{
1848 cleanup_old_buffers();
1849
1850 queue_delayed_work(dm_bufio_wq, &dm_bufio_work,
1851 DM_BUFIO_WORK_TIMER_SECS * HZ);
1852}
1853
1854/*----------------------------------------------------------------
1855 * Module setup
1856 *--------------------------------------------------------------*/
1857
1858/*
1859 * This is called only once for the whole dm_bufio module.
1860 * It initializes memory limit.
1861 */
1862static int __init dm_bufio_init(void)
1863{
1864 __u64 mem;
1865
Mikulas Patocka4cb57ab2013-12-05 17:33:29 -05001866 dm_bufio_allocated_kmem_cache = 0;
1867 dm_bufio_allocated_get_free_pages = 0;
1868 dm_bufio_allocated_vmalloc = 0;
1869 dm_bufio_current_allocated = 0;
1870
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001871 memset(&dm_bufio_caches, 0, sizeof dm_bufio_caches);
1872 memset(&dm_bufio_cache_names, 0, sizeof dm_bufio_cache_names);
1873
1874 mem = (__u64)((totalram_pages - totalhigh_pages) *
1875 DM_BUFIO_MEMORY_PERCENT / 100) << PAGE_SHIFT;
1876
1877 if (mem > ULONG_MAX)
1878 mem = ULONG_MAX;
1879
1880#ifdef CONFIG_MMU
1881 /*
1882 * Get the size of vmalloc space the same way as VMALLOC_TOTAL
1883 * in fs/proc/internal.h
1884 */
1885 if (mem > (VMALLOC_END - VMALLOC_START) * DM_BUFIO_VMALLOC_PERCENT / 100)
1886 mem = (VMALLOC_END - VMALLOC_START) * DM_BUFIO_VMALLOC_PERCENT / 100;
1887#endif
1888
1889 dm_bufio_default_cache_size = mem;
1890
1891 mutex_lock(&dm_bufio_clients_lock);
1892 __cache_size_refresh();
1893 mutex_unlock(&dm_bufio_clients_lock);
1894
Bhaktipriya Shridharedd1ea22016-08-30 22:19:11 +05301895 dm_bufio_wq = alloc_workqueue("dm_bufio_cache", WQ_MEM_RECLAIM, 0);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001896 if (!dm_bufio_wq)
1897 return -ENOMEM;
1898
1899 INIT_DELAYED_WORK(&dm_bufio_work, work_fn);
1900 queue_delayed_work(dm_bufio_wq, &dm_bufio_work,
1901 DM_BUFIO_WORK_TIMER_SECS * HZ);
1902
1903 return 0;
1904}
1905
1906/*
1907 * This is called once when unloading the dm_bufio module.
1908 */
1909static void __exit dm_bufio_exit(void)
1910{
1911 int bug = 0;
1912 int i;
1913
1914 cancel_delayed_work_sync(&dm_bufio_work);
1915 destroy_workqueue(dm_bufio_wq);
1916
Julia Lawall6f659852015-09-13 14:15:05 +02001917 for (i = 0; i < ARRAY_SIZE(dm_bufio_caches); i++)
1918 kmem_cache_destroy(dm_bufio_caches[i]);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001919
1920 for (i = 0; i < ARRAY_SIZE(dm_bufio_cache_names); i++)
1921 kfree(dm_bufio_cache_names[i]);
1922
1923 if (dm_bufio_client_count) {
1924 DMCRIT("%s: dm_bufio_client_count leaked: %d",
1925 __func__, dm_bufio_client_count);
1926 bug = 1;
1927 }
1928
1929 if (dm_bufio_current_allocated) {
1930 DMCRIT("%s: dm_bufio_current_allocated leaked: %lu",
1931 __func__, dm_bufio_current_allocated);
1932 bug = 1;
1933 }
1934
1935 if (dm_bufio_allocated_get_free_pages) {
1936 DMCRIT("%s: dm_bufio_allocated_get_free_pages leaked: %lu",
1937 __func__, dm_bufio_allocated_get_free_pages);
1938 bug = 1;
1939 }
1940
1941 if (dm_bufio_allocated_vmalloc) {
1942 DMCRIT("%s: dm_bufio_vmalloc leaked: %lu",
1943 __func__, dm_bufio_allocated_vmalloc);
1944 bug = 1;
1945 }
1946
Anup Limbu86a49e22015-11-25 15:46:05 +05301947 BUG_ON(bug);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001948}
1949
1950module_init(dm_bufio_init)
1951module_exit(dm_bufio_exit)
1952
1953module_param_named(max_cache_size_bytes, dm_bufio_cache_size, ulong, S_IRUGO | S_IWUSR);
1954MODULE_PARM_DESC(max_cache_size_bytes, "Size of metadata cache");
1955
1956module_param_named(max_age_seconds, dm_bufio_max_age, uint, S_IRUGO | S_IWUSR);
1957MODULE_PARM_DESC(max_age_seconds, "Max age of a buffer in seconds");
1958
Mikulas Patocka13840d32017-04-30 17:32:28 -04001959module_param_named(retain_bytes, dm_bufio_retain_bytes, ulong, S_IRUGO | S_IWUSR);
Joe Thornber33096a72014-10-09 11:10:25 +01001960MODULE_PARM_DESC(retain_bytes, "Try to keep at least this many bytes cached in memory");
1961
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001962module_param_named(peak_allocated_bytes, dm_bufio_peak_allocated, ulong, S_IRUGO | S_IWUSR);
1963MODULE_PARM_DESC(peak_allocated_bytes, "Tracks the maximum allocated memory");
1964
1965module_param_named(allocated_kmem_cache_bytes, dm_bufio_allocated_kmem_cache, ulong, S_IRUGO);
1966MODULE_PARM_DESC(allocated_kmem_cache_bytes, "Memory allocated with kmem_cache_alloc");
1967
1968module_param_named(allocated_get_free_pages_bytes, dm_bufio_allocated_get_free_pages, ulong, S_IRUGO);
1969MODULE_PARM_DESC(allocated_get_free_pages_bytes, "Memory allocated with get_free_pages");
1970
1971module_param_named(allocated_vmalloc_bytes, dm_bufio_allocated_vmalloc, ulong, S_IRUGO);
1972MODULE_PARM_DESC(allocated_vmalloc_bytes, "Memory allocated with vmalloc");
1973
1974module_param_named(current_allocated_bytes, dm_bufio_current_allocated, ulong, S_IRUGO);
1975MODULE_PARM_DESC(current_allocated_bytes, "Memory currently used by the cache");
1976
1977MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
1978MODULE_DESCRIPTION(DM_NAME " buffered I/O library");
1979MODULE_LICENSE("GPL");