dm-bufio.c 48 KB

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