dm-bufio.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  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. unsigned noio_flag;
  308. void *ptr;
  309. if (c->block_size <= DM_BUFIO_BLOCK_SIZE_SLAB_LIMIT) {
  310. *data_mode = DATA_MODE_SLAB;
  311. return kmem_cache_alloc(DM_BUFIO_CACHE(c), gfp_mask);
  312. }
  313. if (c->block_size <= DM_BUFIO_BLOCK_SIZE_GFP_LIMIT &&
  314. gfp_mask & __GFP_NORETRY) {
  315. *data_mode = DATA_MODE_GET_FREE_PAGES;
  316. return (void *)__get_free_pages(gfp_mask,
  317. c->pages_per_block_bits);
  318. }
  319. *data_mode = DATA_MODE_VMALLOC;
  320. /*
  321. * __vmalloc allocates the data pages and auxiliary structures with
  322. * gfp_flags that were specified, but pagetables are always allocated
  323. * with GFP_KERNEL, no matter what was specified as gfp_mask.
  324. *
  325. * Consequently, we must set per-process flag PF_MEMALLOC_NOIO so that
  326. * all allocations done by this process (including pagetables) are done
  327. * as if GFP_NOIO was specified.
  328. */
  329. if (gfp_mask & __GFP_NORETRY)
  330. noio_flag = memalloc_noio_save();
  331. ptr = __vmalloc(c->block_size, gfp_mask | __GFP_HIGHMEM, PAGE_KERNEL);
  332. if (gfp_mask & __GFP_NORETRY)
  333. memalloc_noio_restore(noio_flag);
  334. return ptr;
  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. /*
  683. * dm-bufio is resistant to allocation failures (it just keeps
  684. * one buffer reserved in cases all the allocations fail).
  685. * So set flags to not try too hard:
  686. * GFP_NOIO: don't recurse into the I/O layer
  687. * __GFP_NORETRY: don't retry and rather return failure
  688. * __GFP_NOMEMALLOC: don't use emergency reserves
  689. * __GFP_NOWARN: don't print a warning in case of failure
  690. *
  691. * For debugging, if we set the cache size to 1, no new buffers will
  692. * be allocated.
  693. */
  694. while (1) {
  695. if (dm_bufio_cache_size_latch != 1) {
  696. b = alloc_buffer(c, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  697. if (b)
  698. return b;
  699. }
  700. if (nf == NF_PREFETCH)
  701. return NULL;
  702. if (!list_empty(&c->reserved_buffers)) {
  703. b = list_entry(c->reserved_buffers.next,
  704. struct dm_buffer, lru_list);
  705. list_del(&b->lru_list);
  706. c->need_reserved_buffers++;
  707. return b;
  708. }
  709. b = __get_unclaimed_buffer(c);
  710. if (b)
  711. return b;
  712. __wait_for_free_buffer(c);
  713. }
  714. }
  715. static struct dm_buffer *__alloc_buffer_wait(struct dm_bufio_client *c, enum new_flag nf)
  716. {
  717. struct dm_buffer *b = __alloc_buffer_wait_no_callback(c, nf);
  718. if (!b)
  719. return NULL;
  720. if (c->alloc_callback)
  721. c->alloc_callback(b);
  722. return b;
  723. }
  724. /*
  725. * Free a buffer and wake other threads waiting for free buffers.
  726. */
  727. static void __free_buffer_wake(struct dm_buffer *b)
  728. {
  729. struct dm_bufio_client *c = b->c;
  730. if (!c->need_reserved_buffers)
  731. free_buffer(b);
  732. else {
  733. list_add(&b->lru_list, &c->reserved_buffers);
  734. c->need_reserved_buffers--;
  735. }
  736. wake_up(&c->free_buffer_wait);
  737. }
  738. static void __write_dirty_buffers_async(struct dm_bufio_client *c, int no_wait,
  739. struct list_head *write_list)
  740. {
  741. struct dm_buffer *b, *tmp;
  742. list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) {
  743. BUG_ON(test_bit(B_READING, &b->state));
  744. if (!test_bit(B_DIRTY, &b->state) &&
  745. !test_bit(B_WRITING, &b->state)) {
  746. __relink_lru(b, LIST_CLEAN);
  747. continue;
  748. }
  749. if (no_wait && test_bit(B_WRITING, &b->state))
  750. return;
  751. __write_dirty_buffer(b, write_list);
  752. cond_resched();
  753. }
  754. }
  755. /*
  756. * Get writeback threshold and buffer limit for a given client.
  757. */
  758. static void __get_memory_limit(struct dm_bufio_client *c,
  759. unsigned long *threshold_buffers,
  760. unsigned long *limit_buffers)
  761. {
  762. unsigned long buffers;
  763. if (unlikely(ACCESS_ONCE(dm_bufio_cache_size) != dm_bufio_cache_size_latch)) {
  764. if (mutex_trylock(&dm_bufio_clients_lock)) {
  765. __cache_size_refresh();
  766. mutex_unlock(&dm_bufio_clients_lock);
  767. }
  768. }
  769. buffers = dm_bufio_cache_size_per_client >>
  770. (c->sectors_per_block_bits + SECTOR_SHIFT);
  771. if (buffers < c->minimum_buffers)
  772. buffers = c->minimum_buffers;
  773. *limit_buffers = buffers;
  774. *threshold_buffers = buffers * DM_BUFIO_WRITEBACK_PERCENT / 100;
  775. }
  776. /*
  777. * Check if we're over watermark.
  778. * If we are over threshold_buffers, start freeing buffers.
  779. * If we're over "limit_buffers", block until we get under the limit.
  780. */
  781. static void __check_watermark(struct dm_bufio_client *c,
  782. struct list_head *write_list)
  783. {
  784. unsigned long threshold_buffers, limit_buffers;
  785. __get_memory_limit(c, &threshold_buffers, &limit_buffers);
  786. while (c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY] >
  787. limit_buffers) {
  788. struct dm_buffer *b = __get_unclaimed_buffer(c);
  789. if (!b)
  790. return;
  791. __free_buffer_wake(b);
  792. cond_resched();
  793. }
  794. if (c->n_buffers[LIST_DIRTY] > threshold_buffers)
  795. __write_dirty_buffers_async(c, 1, write_list);
  796. }
  797. /*----------------------------------------------------------------
  798. * Getting a buffer
  799. *--------------------------------------------------------------*/
  800. static struct dm_buffer *__bufio_new(struct dm_bufio_client *c, sector_t block,
  801. enum new_flag nf, int *need_submit,
  802. struct list_head *write_list)
  803. {
  804. struct dm_buffer *b, *new_b = NULL;
  805. *need_submit = 0;
  806. b = __find(c, block);
  807. if (b)
  808. goto found_buffer;
  809. if (nf == NF_GET)
  810. return NULL;
  811. new_b = __alloc_buffer_wait(c, nf);
  812. if (!new_b)
  813. return NULL;
  814. /*
  815. * We've had a period where the mutex was unlocked, so need to
  816. * recheck the hash table.
  817. */
  818. b = __find(c, block);
  819. if (b) {
  820. __free_buffer_wake(new_b);
  821. goto found_buffer;
  822. }
  823. __check_watermark(c, write_list);
  824. b = new_b;
  825. b->hold_count = 1;
  826. b->read_error = 0;
  827. b->write_error = 0;
  828. __link_buffer(b, block, LIST_CLEAN);
  829. if (nf == NF_FRESH) {
  830. b->state = 0;
  831. return b;
  832. }
  833. b->state = 1 << B_READING;
  834. *need_submit = 1;
  835. return b;
  836. found_buffer:
  837. if (nf == NF_PREFETCH)
  838. return NULL;
  839. /*
  840. * Note: it is essential that we don't wait for the buffer to be
  841. * read if dm_bufio_get function is used. Both dm_bufio_get and
  842. * dm_bufio_prefetch can be used in the driver request routine.
  843. * If the user called both dm_bufio_prefetch and dm_bufio_get on
  844. * the same buffer, it would deadlock if we waited.
  845. */
  846. if (nf == NF_GET && unlikely(test_bit(B_READING, &b->state)))
  847. return NULL;
  848. b->hold_count++;
  849. __relink_lru(b, test_bit(B_DIRTY, &b->state) ||
  850. test_bit(B_WRITING, &b->state));
  851. return b;
  852. }
  853. /*
  854. * The endio routine for reading: set the error, clear the bit and wake up
  855. * anyone waiting on the buffer.
  856. */
  857. static void read_endio(struct bio *bio)
  858. {
  859. struct dm_buffer *b = container_of(bio, struct dm_buffer, bio);
  860. b->read_error = bio->bi_error;
  861. BUG_ON(!test_bit(B_READING, &b->state));
  862. smp_mb__before_atomic();
  863. clear_bit(B_READING, &b->state);
  864. smp_mb__after_atomic();
  865. wake_up_bit(&b->state, B_READING);
  866. }
  867. /*
  868. * A common routine for dm_bufio_new and dm_bufio_read. Operation of these
  869. * functions is similar except that dm_bufio_new doesn't read the
  870. * buffer from the disk (assuming that the caller overwrites all the data
  871. * and uses dm_bufio_mark_buffer_dirty to write new data back).
  872. */
  873. static void *new_read(struct dm_bufio_client *c, sector_t block,
  874. enum new_flag nf, struct dm_buffer **bp)
  875. {
  876. int need_submit;
  877. struct dm_buffer *b;
  878. LIST_HEAD(write_list);
  879. dm_bufio_lock(c);
  880. b = __bufio_new(c, block, nf, &need_submit, &write_list);
  881. #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
  882. if (b && b->hold_count == 1)
  883. buffer_record_stack(b);
  884. #endif
  885. dm_bufio_unlock(c);
  886. __flush_write_list(&write_list);
  887. if (!b)
  888. return NULL;
  889. if (need_submit)
  890. submit_io(b, READ, b->block, read_endio);
  891. wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE);
  892. if (b->read_error) {
  893. int error = b->read_error;
  894. dm_bufio_release(b);
  895. return ERR_PTR(error);
  896. }
  897. *bp = b;
  898. return b->data;
  899. }
  900. void *dm_bufio_get(struct dm_bufio_client *c, sector_t block,
  901. struct dm_buffer **bp)
  902. {
  903. return new_read(c, block, NF_GET, bp);
  904. }
  905. EXPORT_SYMBOL_GPL(dm_bufio_get);
  906. void *dm_bufio_read(struct dm_bufio_client *c, sector_t block,
  907. struct dm_buffer **bp)
  908. {
  909. BUG_ON(dm_bufio_in_request());
  910. return new_read(c, block, NF_READ, bp);
  911. }
  912. EXPORT_SYMBOL_GPL(dm_bufio_read);
  913. void *dm_bufio_new(struct dm_bufio_client *c, sector_t block,
  914. struct dm_buffer **bp)
  915. {
  916. BUG_ON(dm_bufio_in_request());
  917. return new_read(c, block, NF_FRESH, bp);
  918. }
  919. EXPORT_SYMBOL_GPL(dm_bufio_new);
  920. void dm_bufio_prefetch(struct dm_bufio_client *c,
  921. sector_t block, unsigned n_blocks)
  922. {
  923. struct blk_plug plug;
  924. LIST_HEAD(write_list);
  925. BUG_ON(dm_bufio_in_request());
  926. blk_start_plug(&plug);
  927. dm_bufio_lock(c);
  928. for (; n_blocks--; block++) {
  929. int need_submit;
  930. struct dm_buffer *b;
  931. b = __bufio_new(c, block, NF_PREFETCH, &need_submit,
  932. &write_list);
  933. if (unlikely(!list_empty(&write_list))) {
  934. dm_bufio_unlock(c);
  935. blk_finish_plug(&plug);
  936. __flush_write_list(&write_list);
  937. blk_start_plug(&plug);
  938. dm_bufio_lock(c);
  939. }
  940. if (unlikely(b != NULL)) {
  941. dm_bufio_unlock(c);
  942. if (need_submit)
  943. submit_io(b, READ, b->block, read_endio);
  944. dm_bufio_release(b);
  945. cond_resched();
  946. if (!n_blocks)
  947. goto flush_plug;
  948. dm_bufio_lock(c);
  949. }
  950. }
  951. dm_bufio_unlock(c);
  952. flush_plug:
  953. blk_finish_plug(&plug);
  954. }
  955. EXPORT_SYMBOL_GPL(dm_bufio_prefetch);
  956. void dm_bufio_release(struct dm_buffer *b)
  957. {
  958. struct dm_bufio_client *c = b->c;
  959. dm_bufio_lock(c);
  960. BUG_ON(!b->hold_count);
  961. b->hold_count--;
  962. if (!b->hold_count) {
  963. wake_up(&c->free_buffer_wait);
  964. /*
  965. * If there were errors on the buffer, and the buffer is not
  966. * to be written, free the buffer. There is no point in caching
  967. * invalid buffer.
  968. */
  969. if ((b->read_error || b->write_error) &&
  970. !test_bit(B_READING, &b->state) &&
  971. !test_bit(B_WRITING, &b->state) &&
  972. !test_bit(B_DIRTY, &b->state)) {
  973. __unlink_buffer(b);
  974. __free_buffer_wake(b);
  975. }
  976. }
  977. dm_bufio_unlock(c);
  978. }
  979. EXPORT_SYMBOL_GPL(dm_bufio_release);
  980. void dm_bufio_mark_buffer_dirty(struct dm_buffer *b)
  981. {
  982. struct dm_bufio_client *c = b->c;
  983. dm_bufio_lock(c);
  984. BUG_ON(test_bit(B_READING, &b->state));
  985. if (!test_and_set_bit(B_DIRTY, &b->state))
  986. __relink_lru(b, LIST_DIRTY);
  987. dm_bufio_unlock(c);
  988. }
  989. EXPORT_SYMBOL_GPL(dm_bufio_mark_buffer_dirty);
  990. void dm_bufio_write_dirty_buffers_async(struct dm_bufio_client *c)
  991. {
  992. LIST_HEAD(write_list);
  993. BUG_ON(dm_bufio_in_request());
  994. dm_bufio_lock(c);
  995. __write_dirty_buffers_async(c, 0, &write_list);
  996. dm_bufio_unlock(c);
  997. __flush_write_list(&write_list);
  998. }
  999. EXPORT_SYMBOL_GPL(dm_bufio_write_dirty_buffers_async);
  1000. /*
  1001. * For performance, it is essential that the buffers are written asynchronously
  1002. * and simultaneously (so that the block layer can merge the writes) and then
  1003. * waited upon.
  1004. *
  1005. * Finally, we flush hardware disk cache.
  1006. */
  1007. int dm_bufio_write_dirty_buffers(struct dm_bufio_client *c)
  1008. {
  1009. int a, f;
  1010. unsigned long buffers_processed = 0;
  1011. struct dm_buffer *b, *tmp;
  1012. LIST_HEAD(write_list);
  1013. dm_bufio_lock(c);
  1014. __write_dirty_buffers_async(c, 0, &write_list);
  1015. dm_bufio_unlock(c);
  1016. __flush_write_list(&write_list);
  1017. dm_bufio_lock(c);
  1018. again:
  1019. list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) {
  1020. int dropped_lock = 0;
  1021. if (buffers_processed < c->n_buffers[LIST_DIRTY])
  1022. buffers_processed++;
  1023. BUG_ON(test_bit(B_READING, &b->state));
  1024. if (test_bit(B_WRITING, &b->state)) {
  1025. if (buffers_processed < c->n_buffers[LIST_DIRTY]) {
  1026. dropped_lock = 1;
  1027. b->hold_count++;
  1028. dm_bufio_unlock(c);
  1029. wait_on_bit_io(&b->state, B_WRITING,
  1030. TASK_UNINTERRUPTIBLE);
  1031. dm_bufio_lock(c);
  1032. b->hold_count--;
  1033. } else
  1034. wait_on_bit_io(&b->state, B_WRITING,
  1035. TASK_UNINTERRUPTIBLE);
  1036. }
  1037. if (!test_bit(B_DIRTY, &b->state) &&
  1038. !test_bit(B_WRITING, &b->state))
  1039. __relink_lru(b, LIST_CLEAN);
  1040. cond_resched();
  1041. /*
  1042. * If we dropped the lock, the list is no longer consistent,
  1043. * so we must restart the search.
  1044. *
  1045. * In the most common case, the buffer just processed is
  1046. * relinked to the clean list, so we won't loop scanning the
  1047. * same buffer again and again.
  1048. *
  1049. * This may livelock if there is another thread simultaneously
  1050. * dirtying buffers, so we count the number of buffers walked
  1051. * and if it exceeds the total number of buffers, it means that
  1052. * someone is doing some writes simultaneously with us. In
  1053. * this case, stop, dropping the lock.
  1054. */
  1055. if (dropped_lock)
  1056. goto again;
  1057. }
  1058. wake_up(&c->free_buffer_wait);
  1059. dm_bufio_unlock(c);
  1060. a = xchg(&c->async_write_error, 0);
  1061. f = dm_bufio_issue_flush(c);
  1062. if (a)
  1063. return a;
  1064. return f;
  1065. }
  1066. EXPORT_SYMBOL_GPL(dm_bufio_write_dirty_buffers);
  1067. /*
  1068. * Use dm-io to send and empty barrier flush the device.
  1069. */
  1070. int dm_bufio_issue_flush(struct dm_bufio_client *c)
  1071. {
  1072. struct dm_io_request io_req = {
  1073. .bi_op = REQ_OP_WRITE,
  1074. .bi_op_flags = WRITE_FLUSH,
  1075. .mem.type = DM_IO_KMEM,
  1076. .mem.ptr.addr = NULL,
  1077. .client = c->dm_io,
  1078. };
  1079. struct dm_io_region io_reg = {
  1080. .bdev = c->bdev,
  1081. .sector = 0,
  1082. .count = 0,
  1083. };
  1084. BUG_ON(dm_bufio_in_request());
  1085. return dm_io(&io_req, 1, &io_reg, NULL);
  1086. }
  1087. EXPORT_SYMBOL_GPL(dm_bufio_issue_flush);
  1088. /*
  1089. * We first delete any other buffer that may be at that new location.
  1090. *
  1091. * Then, we write the buffer to the original location if it was dirty.
  1092. *
  1093. * Then, if we are the only one who is holding the buffer, relink the buffer
  1094. * in the hash queue for the new location.
  1095. *
  1096. * If there was someone else holding the buffer, we write it to the new
  1097. * location but not relink it, because that other user needs to have the buffer
  1098. * at the same place.
  1099. */
  1100. void dm_bufio_release_move(struct dm_buffer *b, sector_t new_block)
  1101. {
  1102. struct dm_bufio_client *c = b->c;
  1103. struct dm_buffer *new;
  1104. BUG_ON(dm_bufio_in_request());
  1105. dm_bufio_lock(c);
  1106. retry:
  1107. new = __find(c, new_block);
  1108. if (new) {
  1109. if (new->hold_count) {
  1110. __wait_for_free_buffer(c);
  1111. goto retry;
  1112. }
  1113. /*
  1114. * FIXME: Is there any point waiting for a write that's going
  1115. * to be overwritten in a bit?
  1116. */
  1117. __make_buffer_clean(new);
  1118. __unlink_buffer(new);
  1119. __free_buffer_wake(new);
  1120. }
  1121. BUG_ON(!b->hold_count);
  1122. BUG_ON(test_bit(B_READING, &b->state));
  1123. __write_dirty_buffer(b, NULL);
  1124. if (b->hold_count == 1) {
  1125. wait_on_bit_io(&b->state, B_WRITING,
  1126. TASK_UNINTERRUPTIBLE);
  1127. set_bit(B_DIRTY, &b->state);
  1128. __unlink_buffer(b);
  1129. __link_buffer(b, new_block, LIST_DIRTY);
  1130. } else {
  1131. sector_t old_block;
  1132. wait_on_bit_lock_io(&b->state, B_WRITING,
  1133. TASK_UNINTERRUPTIBLE);
  1134. /*
  1135. * Relink buffer to "new_block" so that write_callback
  1136. * sees "new_block" as a block number.
  1137. * After the write, link the buffer back to old_block.
  1138. * All this must be done in bufio lock, so that block number
  1139. * change isn't visible to other threads.
  1140. */
  1141. old_block = b->block;
  1142. __unlink_buffer(b);
  1143. __link_buffer(b, new_block, b->list_mode);
  1144. submit_io(b, WRITE, new_block, write_endio);
  1145. wait_on_bit_io(&b->state, B_WRITING,
  1146. TASK_UNINTERRUPTIBLE);
  1147. __unlink_buffer(b);
  1148. __link_buffer(b, old_block, b->list_mode);
  1149. }
  1150. dm_bufio_unlock(c);
  1151. dm_bufio_release(b);
  1152. }
  1153. EXPORT_SYMBOL_GPL(dm_bufio_release_move);
  1154. /*
  1155. * Free the given buffer.
  1156. *
  1157. * This is just a hint, if the buffer is in use or dirty, this function
  1158. * does nothing.
  1159. */
  1160. void dm_bufio_forget(struct dm_bufio_client *c, sector_t block)
  1161. {
  1162. struct dm_buffer *b;
  1163. dm_bufio_lock(c);
  1164. b = __find(c, block);
  1165. if (b && likely(!b->hold_count) && likely(!b->state)) {
  1166. __unlink_buffer(b);
  1167. __free_buffer_wake(b);
  1168. }
  1169. dm_bufio_unlock(c);
  1170. }
  1171. EXPORT_SYMBOL(dm_bufio_forget);
  1172. void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned n)
  1173. {
  1174. c->minimum_buffers = n;
  1175. }
  1176. EXPORT_SYMBOL(dm_bufio_set_minimum_buffers);
  1177. unsigned dm_bufio_get_block_size(struct dm_bufio_client *c)
  1178. {
  1179. return c->block_size;
  1180. }
  1181. EXPORT_SYMBOL_GPL(dm_bufio_get_block_size);
  1182. sector_t dm_bufio_get_device_size(struct dm_bufio_client *c)
  1183. {
  1184. return i_size_read(c->bdev->bd_inode) >>
  1185. (SECTOR_SHIFT + c->sectors_per_block_bits);
  1186. }
  1187. EXPORT_SYMBOL_GPL(dm_bufio_get_device_size);
  1188. sector_t dm_bufio_get_block_number(struct dm_buffer *b)
  1189. {
  1190. return b->block;
  1191. }
  1192. EXPORT_SYMBOL_GPL(dm_bufio_get_block_number);
  1193. void *dm_bufio_get_block_data(struct dm_buffer *b)
  1194. {
  1195. return b->data;
  1196. }
  1197. EXPORT_SYMBOL_GPL(dm_bufio_get_block_data);
  1198. void *dm_bufio_get_aux_data(struct dm_buffer *b)
  1199. {
  1200. return b + 1;
  1201. }
  1202. EXPORT_SYMBOL_GPL(dm_bufio_get_aux_data);
  1203. struct dm_bufio_client *dm_bufio_get_client(struct dm_buffer *b)
  1204. {
  1205. return b->c;
  1206. }
  1207. EXPORT_SYMBOL_GPL(dm_bufio_get_client);
  1208. static void drop_buffers(struct dm_bufio_client *c)
  1209. {
  1210. struct dm_buffer *b;
  1211. int i;
  1212. bool warned = false;
  1213. BUG_ON(dm_bufio_in_request());
  1214. /*
  1215. * An optimization so that the buffers are not written one-by-one.
  1216. */
  1217. dm_bufio_write_dirty_buffers_async(c);
  1218. dm_bufio_lock(c);
  1219. while ((b = __get_unclaimed_buffer(c)))
  1220. __free_buffer_wake(b);
  1221. for (i = 0; i < LIST_SIZE; i++)
  1222. list_for_each_entry(b, &c->lru[i], lru_list) {
  1223. WARN_ON(!warned);
  1224. warned = true;
  1225. DMERR("leaked buffer %llx, hold count %u, list %d",
  1226. (unsigned long long)b->block, b->hold_count, i);
  1227. #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
  1228. print_stack_trace(&b->stack_trace, 1);
  1229. b->hold_count = 0; /* mark unclaimed to avoid BUG_ON below */
  1230. #endif
  1231. }
  1232. #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
  1233. while ((b = __get_unclaimed_buffer(c)))
  1234. __free_buffer_wake(b);
  1235. #endif
  1236. for (i = 0; i < LIST_SIZE; i++)
  1237. BUG_ON(!list_empty(&c->lru[i]));
  1238. dm_bufio_unlock(c);
  1239. }
  1240. /*
  1241. * We may not be able to evict this buffer if IO pending or the client
  1242. * is still using it. Caller is expected to know buffer is too old.
  1243. *
  1244. * And if GFP_NOFS is used, we must not do any I/O because we hold
  1245. * dm_bufio_clients_lock and we would risk deadlock if the I/O gets
  1246. * rerouted to different bufio client.
  1247. */
  1248. static bool __try_evict_buffer(struct dm_buffer *b, gfp_t gfp)
  1249. {
  1250. if (!(gfp & __GFP_FS)) {
  1251. if (test_bit(B_READING, &b->state) ||
  1252. test_bit(B_WRITING, &b->state) ||
  1253. test_bit(B_DIRTY, &b->state))
  1254. return false;
  1255. }
  1256. if (b->hold_count)
  1257. return false;
  1258. __make_buffer_clean(b);
  1259. __unlink_buffer(b);
  1260. __free_buffer_wake(b);
  1261. return true;
  1262. }
  1263. static unsigned long get_retain_buffers(struct dm_bufio_client *c)
  1264. {
  1265. unsigned long retain_bytes = ACCESS_ONCE(dm_bufio_retain_bytes);
  1266. return retain_bytes >> (c->sectors_per_block_bits + SECTOR_SHIFT);
  1267. }
  1268. static unsigned long __scan(struct dm_bufio_client *c, unsigned long nr_to_scan,
  1269. gfp_t gfp_mask)
  1270. {
  1271. int l;
  1272. struct dm_buffer *b, *tmp;
  1273. unsigned long freed = 0;
  1274. unsigned long count = nr_to_scan;
  1275. unsigned long retain_target = get_retain_buffers(c);
  1276. for (l = 0; l < LIST_SIZE; l++) {
  1277. list_for_each_entry_safe_reverse(b, tmp, &c->lru[l], lru_list) {
  1278. if (__try_evict_buffer(b, gfp_mask))
  1279. freed++;
  1280. if (!--nr_to_scan || ((count - freed) <= retain_target))
  1281. return freed;
  1282. cond_resched();
  1283. }
  1284. }
  1285. return freed;
  1286. }
  1287. static unsigned long
  1288. dm_bufio_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  1289. {
  1290. struct dm_bufio_client *c;
  1291. unsigned long freed;
  1292. c = container_of(shrink, struct dm_bufio_client, shrinker);
  1293. if (sc->gfp_mask & __GFP_FS)
  1294. dm_bufio_lock(c);
  1295. else if (!dm_bufio_trylock(c))
  1296. return SHRINK_STOP;
  1297. freed = __scan(c, sc->nr_to_scan, sc->gfp_mask);
  1298. dm_bufio_unlock(c);
  1299. return freed;
  1300. }
  1301. static unsigned long
  1302. dm_bufio_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  1303. {
  1304. struct dm_bufio_client *c;
  1305. unsigned long count;
  1306. c = container_of(shrink, struct dm_bufio_client, shrinker);
  1307. if (sc->gfp_mask & __GFP_FS)
  1308. dm_bufio_lock(c);
  1309. else if (!dm_bufio_trylock(c))
  1310. return 0;
  1311. count = c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY];
  1312. dm_bufio_unlock(c);
  1313. return count;
  1314. }
  1315. /*
  1316. * Create the buffering interface
  1317. */
  1318. struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsigned block_size,
  1319. unsigned reserved_buffers, unsigned aux_size,
  1320. void (*alloc_callback)(struct dm_buffer *),
  1321. void (*write_callback)(struct dm_buffer *))
  1322. {
  1323. int r;
  1324. struct dm_bufio_client *c;
  1325. unsigned i;
  1326. BUG_ON(block_size < 1 << SECTOR_SHIFT ||
  1327. (block_size & (block_size - 1)));
  1328. c = kzalloc(sizeof(*c), GFP_KERNEL);
  1329. if (!c) {
  1330. r = -ENOMEM;
  1331. goto bad_client;
  1332. }
  1333. c->buffer_tree = RB_ROOT;
  1334. c->bdev = bdev;
  1335. c->block_size = block_size;
  1336. c->sectors_per_block_bits = __ffs(block_size) - SECTOR_SHIFT;
  1337. c->pages_per_block_bits = (__ffs(block_size) >= PAGE_SHIFT) ?
  1338. __ffs(block_size) - PAGE_SHIFT : 0;
  1339. c->blocks_per_page_bits = (__ffs(block_size) < PAGE_SHIFT ?
  1340. PAGE_SHIFT - __ffs(block_size) : 0);
  1341. c->aux_size = aux_size;
  1342. c->alloc_callback = alloc_callback;
  1343. c->write_callback = write_callback;
  1344. for (i = 0; i < LIST_SIZE; i++) {
  1345. INIT_LIST_HEAD(&c->lru[i]);
  1346. c->n_buffers[i] = 0;
  1347. }
  1348. mutex_init(&c->lock);
  1349. INIT_LIST_HEAD(&c->reserved_buffers);
  1350. c->need_reserved_buffers = reserved_buffers;
  1351. c->minimum_buffers = DM_BUFIO_MIN_BUFFERS;
  1352. init_waitqueue_head(&c->free_buffer_wait);
  1353. c->async_write_error = 0;
  1354. c->dm_io = dm_io_client_create();
  1355. if (IS_ERR(c->dm_io)) {
  1356. r = PTR_ERR(c->dm_io);
  1357. goto bad_dm_io;
  1358. }
  1359. mutex_lock(&dm_bufio_clients_lock);
  1360. if (c->blocks_per_page_bits) {
  1361. if (!DM_BUFIO_CACHE_NAME(c)) {
  1362. DM_BUFIO_CACHE_NAME(c) = kasprintf(GFP_KERNEL, "dm_bufio_cache-%u", c->block_size);
  1363. if (!DM_BUFIO_CACHE_NAME(c)) {
  1364. r = -ENOMEM;
  1365. mutex_unlock(&dm_bufio_clients_lock);
  1366. goto bad_cache;
  1367. }
  1368. }
  1369. if (!DM_BUFIO_CACHE(c)) {
  1370. DM_BUFIO_CACHE(c) = kmem_cache_create(DM_BUFIO_CACHE_NAME(c),
  1371. c->block_size,
  1372. c->block_size, 0, NULL);
  1373. if (!DM_BUFIO_CACHE(c)) {
  1374. r = -ENOMEM;
  1375. mutex_unlock(&dm_bufio_clients_lock);
  1376. goto bad_cache;
  1377. }
  1378. }
  1379. }
  1380. mutex_unlock(&dm_bufio_clients_lock);
  1381. while (c->need_reserved_buffers) {
  1382. struct dm_buffer *b = alloc_buffer(c, GFP_KERNEL);
  1383. if (!b) {
  1384. r = -ENOMEM;
  1385. goto bad_buffer;
  1386. }
  1387. __free_buffer_wake(b);
  1388. }
  1389. mutex_lock(&dm_bufio_clients_lock);
  1390. dm_bufio_client_count++;
  1391. list_add(&c->client_list, &dm_bufio_all_clients);
  1392. __cache_size_refresh();
  1393. mutex_unlock(&dm_bufio_clients_lock);
  1394. c->shrinker.count_objects = dm_bufio_shrink_count;
  1395. c->shrinker.scan_objects = dm_bufio_shrink_scan;
  1396. c->shrinker.seeks = 1;
  1397. c->shrinker.batch = 0;
  1398. register_shrinker(&c->shrinker);
  1399. return c;
  1400. bad_buffer:
  1401. bad_cache:
  1402. while (!list_empty(&c->reserved_buffers)) {
  1403. struct dm_buffer *b = list_entry(c->reserved_buffers.next,
  1404. struct dm_buffer, lru_list);
  1405. list_del(&b->lru_list);
  1406. free_buffer(b);
  1407. }
  1408. dm_io_client_destroy(c->dm_io);
  1409. bad_dm_io:
  1410. kfree(c);
  1411. bad_client:
  1412. return ERR_PTR(r);
  1413. }
  1414. EXPORT_SYMBOL_GPL(dm_bufio_client_create);
  1415. /*
  1416. * Free the buffering interface.
  1417. * It is required that there are no references on any buffers.
  1418. */
  1419. void dm_bufio_client_destroy(struct dm_bufio_client *c)
  1420. {
  1421. unsigned i;
  1422. drop_buffers(c);
  1423. unregister_shrinker(&c->shrinker);
  1424. mutex_lock(&dm_bufio_clients_lock);
  1425. list_del(&c->client_list);
  1426. dm_bufio_client_count--;
  1427. __cache_size_refresh();
  1428. mutex_unlock(&dm_bufio_clients_lock);
  1429. BUG_ON(!RB_EMPTY_ROOT(&c->buffer_tree));
  1430. BUG_ON(c->need_reserved_buffers);
  1431. while (!list_empty(&c->reserved_buffers)) {
  1432. struct dm_buffer *b = list_entry(c->reserved_buffers.next,
  1433. struct dm_buffer, lru_list);
  1434. list_del(&b->lru_list);
  1435. free_buffer(b);
  1436. }
  1437. for (i = 0; i < LIST_SIZE; i++)
  1438. if (c->n_buffers[i])
  1439. DMERR("leaked buffer count %d: %ld", i, c->n_buffers[i]);
  1440. for (i = 0; i < LIST_SIZE; i++)
  1441. BUG_ON(c->n_buffers[i]);
  1442. dm_io_client_destroy(c->dm_io);
  1443. kfree(c);
  1444. }
  1445. EXPORT_SYMBOL_GPL(dm_bufio_client_destroy);
  1446. static unsigned get_max_age_hz(void)
  1447. {
  1448. unsigned max_age = ACCESS_ONCE(dm_bufio_max_age);
  1449. if (max_age > UINT_MAX / HZ)
  1450. max_age = UINT_MAX / HZ;
  1451. return max_age * HZ;
  1452. }
  1453. static bool older_than(struct dm_buffer *b, unsigned long age_hz)
  1454. {
  1455. return time_after_eq(jiffies, b->last_accessed + age_hz);
  1456. }
  1457. static void __evict_old_buffers(struct dm_bufio_client *c, unsigned long age_hz)
  1458. {
  1459. struct dm_buffer *b, *tmp;
  1460. unsigned long retain_target = get_retain_buffers(c);
  1461. unsigned long count;
  1462. LIST_HEAD(write_list);
  1463. dm_bufio_lock(c);
  1464. __check_watermark(c, &write_list);
  1465. if (unlikely(!list_empty(&write_list))) {
  1466. dm_bufio_unlock(c);
  1467. __flush_write_list(&write_list);
  1468. dm_bufio_lock(c);
  1469. }
  1470. count = c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY];
  1471. list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_CLEAN], lru_list) {
  1472. if (count <= retain_target)
  1473. break;
  1474. if (!older_than(b, age_hz))
  1475. break;
  1476. if (__try_evict_buffer(b, 0))
  1477. count--;
  1478. cond_resched();
  1479. }
  1480. dm_bufio_unlock(c);
  1481. }
  1482. static void cleanup_old_buffers(void)
  1483. {
  1484. unsigned long max_age_hz = get_max_age_hz();
  1485. struct dm_bufio_client *c;
  1486. mutex_lock(&dm_bufio_clients_lock);
  1487. __cache_size_refresh();
  1488. list_for_each_entry(c, &dm_bufio_all_clients, client_list)
  1489. __evict_old_buffers(c, max_age_hz);
  1490. mutex_unlock(&dm_bufio_clients_lock);
  1491. }
  1492. static struct workqueue_struct *dm_bufio_wq;
  1493. static struct delayed_work dm_bufio_work;
  1494. static void work_fn(struct work_struct *w)
  1495. {
  1496. cleanup_old_buffers();
  1497. queue_delayed_work(dm_bufio_wq, &dm_bufio_work,
  1498. DM_BUFIO_WORK_TIMER_SECS * HZ);
  1499. }
  1500. /*----------------------------------------------------------------
  1501. * Module setup
  1502. *--------------------------------------------------------------*/
  1503. /*
  1504. * This is called only once for the whole dm_bufio module.
  1505. * It initializes memory limit.
  1506. */
  1507. static int __init dm_bufio_init(void)
  1508. {
  1509. __u64 mem;
  1510. dm_bufio_allocated_kmem_cache = 0;
  1511. dm_bufio_allocated_get_free_pages = 0;
  1512. dm_bufio_allocated_vmalloc = 0;
  1513. dm_bufio_current_allocated = 0;
  1514. memset(&dm_bufio_caches, 0, sizeof dm_bufio_caches);
  1515. memset(&dm_bufio_cache_names, 0, sizeof dm_bufio_cache_names);
  1516. mem = (__u64)((totalram_pages - totalhigh_pages) *
  1517. DM_BUFIO_MEMORY_PERCENT / 100) << PAGE_SHIFT;
  1518. if (mem > ULONG_MAX)
  1519. mem = ULONG_MAX;
  1520. #ifdef CONFIG_MMU
  1521. /*
  1522. * Get the size of vmalloc space the same way as VMALLOC_TOTAL
  1523. * in fs/proc/internal.h
  1524. */
  1525. if (mem > (VMALLOC_END - VMALLOC_START) * DM_BUFIO_VMALLOC_PERCENT / 100)
  1526. mem = (VMALLOC_END - VMALLOC_START) * DM_BUFIO_VMALLOC_PERCENT / 100;
  1527. #endif
  1528. dm_bufio_default_cache_size = mem;
  1529. mutex_lock(&dm_bufio_clients_lock);
  1530. __cache_size_refresh();
  1531. mutex_unlock(&dm_bufio_clients_lock);
  1532. dm_bufio_wq = alloc_workqueue("dm_bufio_cache", WQ_MEM_RECLAIM, 0);
  1533. if (!dm_bufio_wq)
  1534. return -ENOMEM;
  1535. INIT_DELAYED_WORK(&dm_bufio_work, work_fn);
  1536. queue_delayed_work(dm_bufio_wq, &dm_bufio_work,
  1537. DM_BUFIO_WORK_TIMER_SECS * HZ);
  1538. return 0;
  1539. }
  1540. /*
  1541. * This is called once when unloading the dm_bufio module.
  1542. */
  1543. static void __exit dm_bufio_exit(void)
  1544. {
  1545. int bug = 0;
  1546. int i;
  1547. cancel_delayed_work_sync(&dm_bufio_work);
  1548. destroy_workqueue(dm_bufio_wq);
  1549. for (i = 0; i < ARRAY_SIZE(dm_bufio_caches); i++)
  1550. kmem_cache_destroy(dm_bufio_caches[i]);
  1551. for (i = 0; i < ARRAY_SIZE(dm_bufio_cache_names); i++)
  1552. kfree(dm_bufio_cache_names[i]);
  1553. if (dm_bufio_client_count) {
  1554. DMCRIT("%s: dm_bufio_client_count leaked: %d",
  1555. __func__, dm_bufio_client_count);
  1556. bug = 1;
  1557. }
  1558. if (dm_bufio_current_allocated) {
  1559. DMCRIT("%s: dm_bufio_current_allocated leaked: %lu",
  1560. __func__, dm_bufio_current_allocated);
  1561. bug = 1;
  1562. }
  1563. if (dm_bufio_allocated_get_free_pages) {
  1564. DMCRIT("%s: dm_bufio_allocated_get_free_pages leaked: %lu",
  1565. __func__, dm_bufio_allocated_get_free_pages);
  1566. bug = 1;
  1567. }
  1568. if (dm_bufio_allocated_vmalloc) {
  1569. DMCRIT("%s: dm_bufio_vmalloc leaked: %lu",
  1570. __func__, dm_bufio_allocated_vmalloc);
  1571. bug = 1;
  1572. }
  1573. BUG_ON(bug);
  1574. }
  1575. module_init(dm_bufio_init)
  1576. module_exit(dm_bufio_exit)
  1577. module_param_named(max_cache_size_bytes, dm_bufio_cache_size, ulong, S_IRUGO | S_IWUSR);
  1578. MODULE_PARM_DESC(max_cache_size_bytes, "Size of metadata cache");
  1579. module_param_named(max_age_seconds, dm_bufio_max_age, uint, S_IRUGO | S_IWUSR);
  1580. MODULE_PARM_DESC(max_age_seconds, "Max age of a buffer in seconds");
  1581. module_param_named(retain_bytes, dm_bufio_retain_bytes, ulong, S_IRUGO | S_IWUSR);
  1582. MODULE_PARM_DESC(retain_bytes, "Try to keep at least this many bytes cached in memory");
  1583. module_param_named(peak_allocated_bytes, dm_bufio_peak_allocated, ulong, S_IRUGO | S_IWUSR);
  1584. MODULE_PARM_DESC(peak_allocated_bytes, "Tracks the maximum allocated memory");
  1585. module_param_named(allocated_kmem_cache_bytes, dm_bufio_allocated_kmem_cache, ulong, S_IRUGO);
  1586. MODULE_PARM_DESC(allocated_kmem_cache_bytes, "Memory allocated with kmem_cache_alloc");
  1587. module_param_named(allocated_get_free_pages_bytes, dm_bufio_allocated_get_free_pages, ulong, S_IRUGO);
  1588. MODULE_PARM_DESC(allocated_get_free_pages_bytes, "Memory allocated with get_free_pages");
  1589. module_param_named(allocated_vmalloc_bytes, dm_bufio_allocated_vmalloc, ulong, S_IRUGO);
  1590. MODULE_PARM_DESC(allocated_vmalloc_bytes, "Memory allocated with vmalloc");
  1591. module_param_named(current_allocated_bytes, dm_bufio_current_allocated, ulong, S_IRUGO);
  1592. MODULE_PARM_DESC(current_allocated_bytes, "Memory currently used by the cache");
  1593. MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
  1594. MODULE_DESCRIPTION(DM_NAME " buffered I/O library");
  1595. MODULE_LICENSE("GPL");