dm-bufio.c 46 KB

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