dm-bufio.c 49 KB

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