dm-bufio.c 47 KB

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