dm-bufio.c 48 KB

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