dm-bufio.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925
  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_end_io(&b->bio, error ? -EIO : 0);
  448. }
  449. static void use_dmio(struct dm_buffer *b, int rw, sector_t block,
  450. bio_end_io_t *end_io)
  451. {
  452. int r;
  453. struct dm_io_request io_req = {
  454. .bi_rw = rw,
  455. .notify.fn = dmio_complete,
  456. .notify.context = b,
  457. .client = b->c->dm_io,
  458. };
  459. struct dm_io_region region = {
  460. .bdev = b->c->bdev,
  461. .sector = block << b->c->sectors_per_block_bits,
  462. .count = b->c->block_size >> SECTOR_SHIFT,
  463. };
  464. if (b->data_mode != DATA_MODE_VMALLOC) {
  465. io_req.mem.type = DM_IO_KMEM;
  466. io_req.mem.ptr.addr = b->data;
  467. } else {
  468. io_req.mem.type = DM_IO_VMA;
  469. io_req.mem.ptr.vma = b->data;
  470. }
  471. b->bio.bi_end_io = end_io;
  472. r = dm_io(&io_req, 1, &region, NULL);
  473. if (r)
  474. end_io(&b->bio, r);
  475. }
  476. static void inline_endio(struct bio *bio, int error)
  477. {
  478. bio_end_io_t *end_fn = bio->bi_private;
  479. /*
  480. * Reset the bio to free any attached resources
  481. * (e.g. bio integrity profiles).
  482. */
  483. bio_reset(bio);
  484. end_fn(bio, error);
  485. }
  486. static void use_inline_bio(struct dm_buffer *b, int rw, sector_t block,
  487. bio_end_io_t *end_io)
  488. {
  489. char *ptr;
  490. int len;
  491. bio_init(&b->bio);
  492. b->bio.bi_io_vec = b->bio_vec;
  493. b->bio.bi_max_vecs = DM_BUFIO_INLINE_VECS;
  494. b->bio.bi_iter.bi_sector = block << b->c->sectors_per_block_bits;
  495. b->bio.bi_bdev = b->c->bdev;
  496. b->bio.bi_end_io = inline_endio;
  497. /*
  498. * Use of .bi_private isn't a problem here because
  499. * the dm_buffer's inline bio is local to bufio.
  500. */
  501. b->bio.bi_private = end_io;
  502. /*
  503. * We assume that if len >= PAGE_SIZE ptr is page-aligned.
  504. * If len < PAGE_SIZE the buffer doesn't cross page boundary.
  505. */
  506. ptr = b->data;
  507. len = b->c->block_size;
  508. if (len >= PAGE_SIZE)
  509. BUG_ON((unsigned long)ptr & (PAGE_SIZE - 1));
  510. else
  511. BUG_ON((unsigned long)ptr & (len - 1));
  512. do {
  513. if (!bio_add_page(&b->bio, virt_to_page(ptr),
  514. len < PAGE_SIZE ? len : PAGE_SIZE,
  515. virt_to_phys(ptr) & (PAGE_SIZE - 1))) {
  516. BUG_ON(b->c->block_size <= PAGE_SIZE);
  517. use_dmio(b, rw, block, end_io);
  518. return;
  519. }
  520. len -= PAGE_SIZE;
  521. ptr += PAGE_SIZE;
  522. } while (len > 0);
  523. submit_bio(rw, &b->bio);
  524. }
  525. static void submit_io(struct dm_buffer *b, int rw, sector_t block,
  526. bio_end_io_t *end_io)
  527. {
  528. if (rw == WRITE && b->c->write_callback)
  529. b->c->write_callback(b);
  530. if (b->c->block_size <= DM_BUFIO_INLINE_VECS * PAGE_SIZE &&
  531. b->data_mode != DATA_MODE_VMALLOC)
  532. use_inline_bio(b, rw, block, end_io);
  533. else
  534. use_dmio(b, rw, block, end_io);
  535. }
  536. /*----------------------------------------------------------------
  537. * Writing dirty buffers
  538. *--------------------------------------------------------------*/
  539. /*
  540. * The endio routine for write.
  541. *
  542. * Set the error, clear B_WRITING bit and wake anyone who was waiting on
  543. * it.
  544. */
  545. static void write_endio(struct bio *bio, int error)
  546. {
  547. struct dm_buffer *b = container_of(bio, struct dm_buffer, bio);
  548. b->write_error = error;
  549. if (unlikely(error)) {
  550. struct dm_bufio_client *c = b->c;
  551. (void)cmpxchg(&c->async_write_error, 0, error);
  552. }
  553. BUG_ON(!test_bit(B_WRITING, &b->state));
  554. smp_mb__before_atomic();
  555. clear_bit(B_WRITING, &b->state);
  556. smp_mb__after_atomic();
  557. wake_up_bit(&b->state, B_WRITING);
  558. }
  559. /*
  560. * Initiate a write on a dirty buffer, but don't wait for it.
  561. *
  562. * - If the buffer is not dirty, exit.
  563. * - If there some previous write going on, wait for it to finish (we can't
  564. * have two writes on the same buffer simultaneously).
  565. * - Submit our write and don't wait on it. We set B_WRITING indicating
  566. * that there is a write in progress.
  567. */
  568. static void __write_dirty_buffer(struct dm_buffer *b,
  569. struct list_head *write_list)
  570. {
  571. if (!test_bit(B_DIRTY, &b->state))
  572. return;
  573. clear_bit(B_DIRTY, &b->state);
  574. wait_on_bit_lock_io(&b->state, B_WRITING, TASK_UNINTERRUPTIBLE);
  575. if (!write_list)
  576. submit_io(b, WRITE, b->block, write_endio);
  577. else
  578. list_add_tail(&b->write_list, write_list);
  579. }
  580. static void __flush_write_list(struct list_head *write_list)
  581. {
  582. struct blk_plug plug;
  583. blk_start_plug(&plug);
  584. while (!list_empty(write_list)) {
  585. struct dm_buffer *b =
  586. list_entry(write_list->next, struct dm_buffer, write_list);
  587. list_del(&b->write_list);
  588. submit_io(b, WRITE, b->block, write_endio);
  589. dm_bufio_cond_resched();
  590. }
  591. blk_finish_plug(&plug);
  592. }
  593. /*
  594. * Wait until any activity on the buffer finishes. Possibly write the
  595. * buffer if it is dirty. When this function finishes, there is no I/O
  596. * running on the buffer and the buffer is not dirty.
  597. */
  598. static void __make_buffer_clean(struct dm_buffer *b)
  599. {
  600. BUG_ON(b->hold_count);
  601. if (!b->state) /* fast case */
  602. return;
  603. wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE);
  604. __write_dirty_buffer(b, NULL);
  605. wait_on_bit_io(&b->state, B_WRITING, TASK_UNINTERRUPTIBLE);
  606. }
  607. /*
  608. * Find some buffer that is not held by anybody, clean it, unlink it and
  609. * return it.
  610. */
  611. static struct dm_buffer *__get_unclaimed_buffer(struct dm_bufio_client *c)
  612. {
  613. struct dm_buffer *b;
  614. list_for_each_entry_reverse(b, &c->lru[LIST_CLEAN], lru_list) {
  615. BUG_ON(test_bit(B_WRITING, &b->state));
  616. BUG_ON(test_bit(B_DIRTY, &b->state));
  617. if (!b->hold_count) {
  618. __make_buffer_clean(b);
  619. __unlink_buffer(b);
  620. return b;
  621. }
  622. dm_bufio_cond_resched();
  623. }
  624. list_for_each_entry_reverse(b, &c->lru[LIST_DIRTY], lru_list) {
  625. BUG_ON(test_bit(B_READING, &b->state));
  626. if (!b->hold_count) {
  627. __make_buffer_clean(b);
  628. __unlink_buffer(b);
  629. return b;
  630. }
  631. dm_bufio_cond_resched();
  632. }
  633. return NULL;
  634. }
  635. /*
  636. * Wait until some other threads free some buffer or release hold count on
  637. * some buffer.
  638. *
  639. * This function is entered with c->lock held, drops it and regains it
  640. * before exiting.
  641. */
  642. static void __wait_for_free_buffer(struct dm_bufio_client *c)
  643. {
  644. DECLARE_WAITQUEUE(wait, current);
  645. add_wait_queue(&c->free_buffer_wait, &wait);
  646. set_task_state(current, TASK_UNINTERRUPTIBLE);
  647. dm_bufio_unlock(c);
  648. io_schedule();
  649. remove_wait_queue(&c->free_buffer_wait, &wait);
  650. dm_bufio_lock(c);
  651. }
  652. enum new_flag {
  653. NF_FRESH = 0,
  654. NF_READ = 1,
  655. NF_GET = 2,
  656. NF_PREFETCH = 3
  657. };
  658. /*
  659. * Allocate a new buffer. If the allocation is not possible, wait until
  660. * some other thread frees a buffer.
  661. *
  662. * May drop the lock and regain it.
  663. */
  664. static struct dm_buffer *__alloc_buffer_wait_no_callback(struct dm_bufio_client *c, enum new_flag nf)
  665. {
  666. struct dm_buffer *b;
  667. /*
  668. * dm-bufio is resistant to allocation failures (it just keeps
  669. * one buffer reserved in cases all the allocations fail).
  670. * So set flags to not try too hard:
  671. * GFP_NOIO: don't recurse into the I/O layer
  672. * __GFP_NORETRY: don't retry and rather return failure
  673. * __GFP_NOMEMALLOC: don't use emergency reserves
  674. * __GFP_NOWARN: don't print a warning in case of failure
  675. *
  676. * For debugging, if we set the cache size to 1, no new buffers will
  677. * be allocated.
  678. */
  679. while (1) {
  680. if (dm_bufio_cache_size_latch != 1) {
  681. b = alloc_buffer(c, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  682. if (b)
  683. return b;
  684. }
  685. if (nf == NF_PREFETCH)
  686. return NULL;
  687. if (!list_empty(&c->reserved_buffers)) {
  688. b = list_entry(c->reserved_buffers.next,
  689. struct dm_buffer, lru_list);
  690. list_del(&b->lru_list);
  691. c->need_reserved_buffers++;
  692. return b;
  693. }
  694. b = __get_unclaimed_buffer(c);
  695. if (b)
  696. return b;
  697. __wait_for_free_buffer(c);
  698. }
  699. }
  700. static struct dm_buffer *__alloc_buffer_wait(struct dm_bufio_client *c, enum new_flag nf)
  701. {
  702. struct dm_buffer *b = __alloc_buffer_wait_no_callback(c, nf);
  703. if (!b)
  704. return NULL;
  705. if (c->alloc_callback)
  706. c->alloc_callback(b);
  707. return b;
  708. }
  709. /*
  710. * Free a buffer and wake other threads waiting for free buffers.
  711. */
  712. static void __free_buffer_wake(struct dm_buffer *b)
  713. {
  714. struct dm_bufio_client *c = b->c;
  715. if (!c->need_reserved_buffers)
  716. free_buffer(b);
  717. else {
  718. list_add(&b->lru_list, &c->reserved_buffers);
  719. c->need_reserved_buffers--;
  720. }
  721. wake_up(&c->free_buffer_wait);
  722. }
  723. static void __write_dirty_buffers_async(struct dm_bufio_client *c, int no_wait,
  724. struct list_head *write_list)
  725. {
  726. struct dm_buffer *b, *tmp;
  727. list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) {
  728. BUG_ON(test_bit(B_READING, &b->state));
  729. if (!test_bit(B_DIRTY, &b->state) &&
  730. !test_bit(B_WRITING, &b->state)) {
  731. __relink_lru(b, LIST_CLEAN);
  732. continue;
  733. }
  734. if (no_wait && test_bit(B_WRITING, &b->state))
  735. return;
  736. __write_dirty_buffer(b, write_list);
  737. dm_bufio_cond_resched();
  738. }
  739. }
  740. /*
  741. * Get writeback threshold and buffer limit for a given client.
  742. */
  743. static void __get_memory_limit(struct dm_bufio_client *c,
  744. unsigned long *threshold_buffers,
  745. unsigned long *limit_buffers)
  746. {
  747. unsigned long buffers;
  748. if (ACCESS_ONCE(dm_bufio_cache_size) != dm_bufio_cache_size_latch) {
  749. mutex_lock(&dm_bufio_clients_lock);
  750. __cache_size_refresh();
  751. mutex_unlock(&dm_bufio_clients_lock);
  752. }
  753. buffers = dm_bufio_cache_size_per_client >>
  754. (c->sectors_per_block_bits + SECTOR_SHIFT);
  755. if (buffers < c->minimum_buffers)
  756. buffers = c->minimum_buffers;
  757. *limit_buffers = buffers;
  758. *threshold_buffers = buffers * DM_BUFIO_WRITEBACK_PERCENT / 100;
  759. }
  760. /*
  761. * Check if we're over watermark.
  762. * If we are over threshold_buffers, start freeing buffers.
  763. * If we're over "limit_buffers", block until we get under the limit.
  764. */
  765. static void __check_watermark(struct dm_bufio_client *c,
  766. struct list_head *write_list)
  767. {
  768. unsigned long threshold_buffers, limit_buffers;
  769. __get_memory_limit(c, &threshold_buffers, &limit_buffers);
  770. while (c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY] >
  771. limit_buffers) {
  772. struct dm_buffer *b = __get_unclaimed_buffer(c);
  773. if (!b)
  774. return;
  775. __free_buffer_wake(b);
  776. dm_bufio_cond_resched();
  777. }
  778. if (c->n_buffers[LIST_DIRTY] > threshold_buffers)
  779. __write_dirty_buffers_async(c, 1, write_list);
  780. }
  781. /*----------------------------------------------------------------
  782. * Getting a buffer
  783. *--------------------------------------------------------------*/
  784. static struct dm_buffer *__bufio_new(struct dm_bufio_client *c, sector_t block,
  785. enum new_flag nf, int *need_submit,
  786. struct list_head *write_list)
  787. {
  788. struct dm_buffer *b, *new_b = NULL;
  789. *need_submit = 0;
  790. b = __find(c, block);
  791. if (b)
  792. goto found_buffer;
  793. if (nf == NF_GET)
  794. return NULL;
  795. new_b = __alloc_buffer_wait(c, nf);
  796. if (!new_b)
  797. return NULL;
  798. /*
  799. * We've had a period where the mutex was unlocked, so need to
  800. * recheck the hash table.
  801. */
  802. b = __find(c, block);
  803. if (b) {
  804. __free_buffer_wake(new_b);
  805. goto found_buffer;
  806. }
  807. __check_watermark(c, write_list);
  808. b = new_b;
  809. b->hold_count = 1;
  810. b->read_error = 0;
  811. b->write_error = 0;
  812. __link_buffer(b, block, LIST_CLEAN);
  813. if (nf == NF_FRESH) {
  814. b->state = 0;
  815. return b;
  816. }
  817. b->state = 1 << B_READING;
  818. *need_submit = 1;
  819. return b;
  820. found_buffer:
  821. if (nf == NF_PREFETCH)
  822. return NULL;
  823. /*
  824. * Note: it is essential that we don't wait for the buffer to be
  825. * read if dm_bufio_get function is used. Both dm_bufio_get and
  826. * dm_bufio_prefetch can be used in the driver request routine.
  827. * If the user called both dm_bufio_prefetch and dm_bufio_get on
  828. * the same buffer, it would deadlock if we waited.
  829. */
  830. if (nf == NF_GET && unlikely(test_bit(B_READING, &b->state)))
  831. return NULL;
  832. b->hold_count++;
  833. __relink_lru(b, test_bit(B_DIRTY, &b->state) ||
  834. test_bit(B_WRITING, &b->state));
  835. return b;
  836. }
  837. /*
  838. * The endio routine for reading: set the error, clear the bit and wake up
  839. * anyone waiting on the buffer.
  840. */
  841. static void read_endio(struct bio *bio, int error)
  842. {
  843. struct dm_buffer *b = container_of(bio, struct dm_buffer, bio);
  844. b->read_error = error;
  845. BUG_ON(!test_bit(B_READING, &b->state));
  846. smp_mb__before_atomic();
  847. clear_bit(B_READING, &b->state);
  848. smp_mb__after_atomic();
  849. wake_up_bit(&b->state, B_READING);
  850. }
  851. /*
  852. * A common routine for dm_bufio_new and dm_bufio_read. Operation of these
  853. * functions is similar except that dm_bufio_new doesn't read the
  854. * buffer from the disk (assuming that the caller overwrites all the data
  855. * and uses dm_bufio_mark_buffer_dirty to write new data back).
  856. */
  857. static void *new_read(struct dm_bufio_client *c, sector_t block,
  858. enum new_flag nf, struct dm_buffer **bp)
  859. {
  860. int need_submit;
  861. struct dm_buffer *b;
  862. LIST_HEAD(write_list);
  863. dm_bufio_lock(c);
  864. b = __bufio_new(c, block, nf, &need_submit, &write_list);
  865. dm_bufio_unlock(c);
  866. __flush_write_list(&write_list);
  867. if (!b)
  868. return b;
  869. if (need_submit)
  870. submit_io(b, READ, b->block, read_endio);
  871. wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE);
  872. if (b->read_error) {
  873. int error = b->read_error;
  874. dm_bufio_release(b);
  875. return ERR_PTR(error);
  876. }
  877. *bp = b;
  878. return b->data;
  879. }
  880. void *dm_bufio_get(struct dm_bufio_client *c, sector_t block,
  881. struct dm_buffer **bp)
  882. {
  883. return new_read(c, block, NF_GET, bp);
  884. }
  885. EXPORT_SYMBOL_GPL(dm_bufio_get);
  886. void *dm_bufio_read(struct dm_bufio_client *c, sector_t block,
  887. struct dm_buffer **bp)
  888. {
  889. BUG_ON(dm_bufio_in_request());
  890. return new_read(c, block, NF_READ, bp);
  891. }
  892. EXPORT_SYMBOL_GPL(dm_bufio_read);
  893. void *dm_bufio_new(struct dm_bufio_client *c, sector_t block,
  894. struct dm_buffer **bp)
  895. {
  896. BUG_ON(dm_bufio_in_request());
  897. return new_read(c, block, NF_FRESH, bp);
  898. }
  899. EXPORT_SYMBOL_GPL(dm_bufio_new);
  900. void dm_bufio_prefetch(struct dm_bufio_client *c,
  901. sector_t block, unsigned n_blocks)
  902. {
  903. struct blk_plug plug;
  904. LIST_HEAD(write_list);
  905. BUG_ON(dm_bufio_in_request());
  906. blk_start_plug(&plug);
  907. dm_bufio_lock(c);
  908. for (; n_blocks--; block++) {
  909. int need_submit;
  910. struct dm_buffer *b;
  911. b = __bufio_new(c, block, NF_PREFETCH, &need_submit,
  912. &write_list);
  913. if (unlikely(!list_empty(&write_list))) {
  914. dm_bufio_unlock(c);
  915. blk_finish_plug(&plug);
  916. __flush_write_list(&write_list);
  917. blk_start_plug(&plug);
  918. dm_bufio_lock(c);
  919. }
  920. if (unlikely(b != NULL)) {
  921. dm_bufio_unlock(c);
  922. if (need_submit)
  923. submit_io(b, READ, b->block, read_endio);
  924. dm_bufio_release(b);
  925. dm_bufio_cond_resched();
  926. if (!n_blocks)
  927. goto flush_plug;
  928. dm_bufio_lock(c);
  929. }
  930. }
  931. dm_bufio_unlock(c);
  932. flush_plug:
  933. blk_finish_plug(&plug);
  934. }
  935. EXPORT_SYMBOL_GPL(dm_bufio_prefetch);
  936. void dm_bufio_release(struct dm_buffer *b)
  937. {
  938. struct dm_bufio_client *c = b->c;
  939. dm_bufio_lock(c);
  940. BUG_ON(!b->hold_count);
  941. b->hold_count--;
  942. if (!b->hold_count) {
  943. wake_up(&c->free_buffer_wait);
  944. /*
  945. * If there were errors on the buffer, and the buffer is not
  946. * to be written, free the buffer. There is no point in caching
  947. * invalid buffer.
  948. */
  949. if ((b->read_error || b->write_error) &&
  950. !test_bit(B_READING, &b->state) &&
  951. !test_bit(B_WRITING, &b->state) &&
  952. !test_bit(B_DIRTY, &b->state)) {
  953. __unlink_buffer(b);
  954. __free_buffer_wake(b);
  955. }
  956. }
  957. dm_bufio_unlock(c);
  958. }
  959. EXPORT_SYMBOL_GPL(dm_bufio_release);
  960. void dm_bufio_mark_buffer_dirty(struct dm_buffer *b)
  961. {
  962. struct dm_bufio_client *c = b->c;
  963. dm_bufio_lock(c);
  964. BUG_ON(test_bit(B_READING, &b->state));
  965. if (!test_and_set_bit(B_DIRTY, &b->state))
  966. __relink_lru(b, LIST_DIRTY);
  967. dm_bufio_unlock(c);
  968. }
  969. EXPORT_SYMBOL_GPL(dm_bufio_mark_buffer_dirty);
  970. void dm_bufio_write_dirty_buffers_async(struct dm_bufio_client *c)
  971. {
  972. LIST_HEAD(write_list);
  973. BUG_ON(dm_bufio_in_request());
  974. dm_bufio_lock(c);
  975. __write_dirty_buffers_async(c, 0, &write_list);
  976. dm_bufio_unlock(c);
  977. __flush_write_list(&write_list);
  978. }
  979. EXPORT_SYMBOL_GPL(dm_bufio_write_dirty_buffers_async);
  980. /*
  981. * For performance, it is essential that the buffers are written asynchronously
  982. * and simultaneously (so that the block layer can merge the writes) and then
  983. * waited upon.
  984. *
  985. * Finally, we flush hardware disk cache.
  986. */
  987. int dm_bufio_write_dirty_buffers(struct dm_bufio_client *c)
  988. {
  989. int a, f;
  990. unsigned long buffers_processed = 0;
  991. struct dm_buffer *b, *tmp;
  992. LIST_HEAD(write_list);
  993. dm_bufio_lock(c);
  994. __write_dirty_buffers_async(c, 0, &write_list);
  995. dm_bufio_unlock(c);
  996. __flush_write_list(&write_list);
  997. dm_bufio_lock(c);
  998. again:
  999. list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) {
  1000. int dropped_lock = 0;
  1001. if (buffers_processed < c->n_buffers[LIST_DIRTY])
  1002. buffers_processed++;
  1003. BUG_ON(test_bit(B_READING, &b->state));
  1004. if (test_bit(B_WRITING, &b->state)) {
  1005. if (buffers_processed < c->n_buffers[LIST_DIRTY]) {
  1006. dropped_lock = 1;
  1007. b->hold_count++;
  1008. dm_bufio_unlock(c);
  1009. wait_on_bit_io(&b->state, B_WRITING,
  1010. TASK_UNINTERRUPTIBLE);
  1011. dm_bufio_lock(c);
  1012. b->hold_count--;
  1013. } else
  1014. wait_on_bit_io(&b->state, B_WRITING,
  1015. TASK_UNINTERRUPTIBLE);
  1016. }
  1017. if (!test_bit(B_DIRTY, &b->state) &&
  1018. !test_bit(B_WRITING, &b->state))
  1019. __relink_lru(b, LIST_CLEAN);
  1020. dm_bufio_cond_resched();
  1021. /*
  1022. * If we dropped the lock, the list is no longer consistent,
  1023. * so we must restart the search.
  1024. *
  1025. * In the most common case, the buffer just processed is
  1026. * relinked to the clean list, so we won't loop scanning the
  1027. * same buffer again and again.
  1028. *
  1029. * This may livelock if there is another thread simultaneously
  1030. * dirtying buffers, so we count the number of buffers walked
  1031. * and if it exceeds the total number of buffers, it means that
  1032. * someone is doing some writes simultaneously with us. In
  1033. * this case, stop, dropping the lock.
  1034. */
  1035. if (dropped_lock)
  1036. goto again;
  1037. }
  1038. wake_up(&c->free_buffer_wait);
  1039. dm_bufio_unlock(c);
  1040. a = xchg(&c->async_write_error, 0);
  1041. f = dm_bufio_issue_flush(c);
  1042. if (a)
  1043. return a;
  1044. return f;
  1045. }
  1046. EXPORT_SYMBOL_GPL(dm_bufio_write_dirty_buffers);
  1047. /*
  1048. * Use dm-io to send and empty barrier flush the device.
  1049. */
  1050. int dm_bufio_issue_flush(struct dm_bufio_client *c)
  1051. {
  1052. struct dm_io_request io_req = {
  1053. .bi_rw = WRITE_FLUSH,
  1054. .mem.type = DM_IO_KMEM,
  1055. .mem.ptr.addr = NULL,
  1056. .client = c->dm_io,
  1057. };
  1058. struct dm_io_region io_reg = {
  1059. .bdev = c->bdev,
  1060. .sector = 0,
  1061. .count = 0,
  1062. };
  1063. BUG_ON(dm_bufio_in_request());
  1064. return dm_io(&io_req, 1, &io_reg, NULL);
  1065. }
  1066. EXPORT_SYMBOL_GPL(dm_bufio_issue_flush);
  1067. /*
  1068. * We first delete any other buffer that may be at that new location.
  1069. *
  1070. * Then, we write the buffer to the original location if it was dirty.
  1071. *
  1072. * Then, if we are the only one who is holding the buffer, relink the buffer
  1073. * in the hash queue for the new location.
  1074. *
  1075. * If there was someone else holding the buffer, we write it to the new
  1076. * location but not relink it, because that other user needs to have the buffer
  1077. * at the same place.
  1078. */
  1079. void dm_bufio_release_move(struct dm_buffer *b, sector_t new_block)
  1080. {
  1081. struct dm_bufio_client *c = b->c;
  1082. struct dm_buffer *new;
  1083. BUG_ON(dm_bufio_in_request());
  1084. dm_bufio_lock(c);
  1085. retry:
  1086. new = __find(c, new_block);
  1087. if (new) {
  1088. if (new->hold_count) {
  1089. __wait_for_free_buffer(c);
  1090. goto retry;
  1091. }
  1092. /*
  1093. * FIXME: Is there any point waiting for a write that's going
  1094. * to be overwritten in a bit?
  1095. */
  1096. __make_buffer_clean(new);
  1097. __unlink_buffer(new);
  1098. __free_buffer_wake(new);
  1099. }
  1100. BUG_ON(!b->hold_count);
  1101. BUG_ON(test_bit(B_READING, &b->state));
  1102. __write_dirty_buffer(b, NULL);
  1103. if (b->hold_count == 1) {
  1104. wait_on_bit_io(&b->state, B_WRITING,
  1105. TASK_UNINTERRUPTIBLE);
  1106. set_bit(B_DIRTY, &b->state);
  1107. __unlink_buffer(b);
  1108. __link_buffer(b, new_block, LIST_DIRTY);
  1109. } else {
  1110. sector_t old_block;
  1111. wait_on_bit_lock_io(&b->state, B_WRITING,
  1112. TASK_UNINTERRUPTIBLE);
  1113. /*
  1114. * Relink buffer to "new_block" so that write_callback
  1115. * sees "new_block" as a block number.
  1116. * After the write, link the buffer back to old_block.
  1117. * All this must be done in bufio lock, so that block number
  1118. * change isn't visible to other threads.
  1119. */
  1120. old_block = b->block;
  1121. __unlink_buffer(b);
  1122. __link_buffer(b, new_block, b->list_mode);
  1123. submit_io(b, WRITE, new_block, write_endio);
  1124. wait_on_bit_io(&b->state, B_WRITING,
  1125. TASK_UNINTERRUPTIBLE);
  1126. __unlink_buffer(b);
  1127. __link_buffer(b, old_block, b->list_mode);
  1128. }
  1129. dm_bufio_unlock(c);
  1130. dm_bufio_release(b);
  1131. }
  1132. EXPORT_SYMBOL_GPL(dm_bufio_release_move);
  1133. /*
  1134. * Free the given buffer.
  1135. *
  1136. * This is just a hint, if the buffer is in use or dirty, this function
  1137. * does nothing.
  1138. */
  1139. void dm_bufio_forget(struct dm_bufio_client *c, sector_t block)
  1140. {
  1141. struct dm_buffer *b;
  1142. dm_bufio_lock(c);
  1143. b = __find(c, block);
  1144. if (b && likely(!b->hold_count) && likely(!b->state)) {
  1145. __unlink_buffer(b);
  1146. __free_buffer_wake(b);
  1147. }
  1148. dm_bufio_unlock(c);
  1149. }
  1150. EXPORT_SYMBOL(dm_bufio_forget);
  1151. void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned n)
  1152. {
  1153. c->minimum_buffers = n;
  1154. }
  1155. EXPORT_SYMBOL(dm_bufio_set_minimum_buffers);
  1156. unsigned dm_bufio_get_block_size(struct dm_bufio_client *c)
  1157. {
  1158. return c->block_size;
  1159. }
  1160. EXPORT_SYMBOL_GPL(dm_bufio_get_block_size);
  1161. sector_t dm_bufio_get_device_size(struct dm_bufio_client *c)
  1162. {
  1163. return i_size_read(c->bdev->bd_inode) >>
  1164. (SECTOR_SHIFT + c->sectors_per_block_bits);
  1165. }
  1166. EXPORT_SYMBOL_GPL(dm_bufio_get_device_size);
  1167. sector_t dm_bufio_get_block_number(struct dm_buffer *b)
  1168. {
  1169. return b->block;
  1170. }
  1171. EXPORT_SYMBOL_GPL(dm_bufio_get_block_number);
  1172. void *dm_bufio_get_block_data(struct dm_buffer *b)
  1173. {
  1174. return b->data;
  1175. }
  1176. EXPORT_SYMBOL_GPL(dm_bufio_get_block_data);
  1177. void *dm_bufio_get_aux_data(struct dm_buffer *b)
  1178. {
  1179. return b + 1;
  1180. }
  1181. EXPORT_SYMBOL_GPL(dm_bufio_get_aux_data);
  1182. struct dm_bufio_client *dm_bufio_get_client(struct dm_buffer *b)
  1183. {
  1184. return b->c;
  1185. }
  1186. EXPORT_SYMBOL_GPL(dm_bufio_get_client);
  1187. static void drop_buffers(struct dm_bufio_client *c)
  1188. {
  1189. struct dm_buffer *b;
  1190. int i;
  1191. BUG_ON(dm_bufio_in_request());
  1192. /*
  1193. * An optimization so that the buffers are not written one-by-one.
  1194. */
  1195. dm_bufio_write_dirty_buffers_async(c);
  1196. dm_bufio_lock(c);
  1197. while ((b = __get_unclaimed_buffer(c)))
  1198. __free_buffer_wake(b);
  1199. for (i = 0; i < LIST_SIZE; i++)
  1200. list_for_each_entry(b, &c->lru[i], lru_list)
  1201. DMERR("leaked buffer %llx, hold count %u, list %d",
  1202. (unsigned long long)b->block, b->hold_count, i);
  1203. for (i = 0; i < LIST_SIZE; i++)
  1204. BUG_ON(!list_empty(&c->lru[i]));
  1205. dm_bufio_unlock(c);
  1206. }
  1207. /*
  1208. * We may not be able to evict this buffer if IO pending or the client
  1209. * is still using it. Caller is expected to know buffer is too old.
  1210. *
  1211. * And if GFP_NOFS is used, we must not do any I/O because we hold
  1212. * dm_bufio_clients_lock and we would risk deadlock if the I/O gets
  1213. * rerouted to different bufio client.
  1214. */
  1215. static bool __try_evict_buffer(struct dm_buffer *b, gfp_t gfp)
  1216. {
  1217. if (!(gfp & __GFP_FS)) {
  1218. if (test_bit(B_READING, &b->state) ||
  1219. test_bit(B_WRITING, &b->state) ||
  1220. test_bit(B_DIRTY, &b->state))
  1221. return false;
  1222. }
  1223. if (b->hold_count)
  1224. return false;
  1225. __make_buffer_clean(b);
  1226. __unlink_buffer(b);
  1227. __free_buffer_wake(b);
  1228. return true;
  1229. }
  1230. static unsigned get_retain_buffers(struct dm_bufio_client *c)
  1231. {
  1232. unsigned retain_bytes = ACCESS_ONCE(dm_bufio_retain_bytes);
  1233. return retain_bytes / c->block_size;
  1234. }
  1235. static unsigned long __scan(struct dm_bufio_client *c, unsigned long nr_to_scan,
  1236. gfp_t gfp_mask)
  1237. {
  1238. int l;
  1239. struct dm_buffer *b, *tmp;
  1240. unsigned long freed = 0;
  1241. unsigned long count = nr_to_scan;
  1242. unsigned retain_target = get_retain_buffers(c);
  1243. for (l = 0; l < LIST_SIZE; l++) {
  1244. list_for_each_entry_safe_reverse(b, tmp, &c->lru[l], lru_list) {
  1245. if (__try_evict_buffer(b, gfp_mask))
  1246. freed++;
  1247. if (!--nr_to_scan || ((count - freed) <= retain_target))
  1248. return freed;
  1249. dm_bufio_cond_resched();
  1250. }
  1251. }
  1252. return freed;
  1253. }
  1254. static unsigned long
  1255. dm_bufio_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  1256. {
  1257. struct dm_bufio_client *c;
  1258. unsigned long freed;
  1259. c = container_of(shrink, struct dm_bufio_client, shrinker);
  1260. if (sc->gfp_mask & __GFP_FS)
  1261. dm_bufio_lock(c);
  1262. else if (!dm_bufio_trylock(c))
  1263. return SHRINK_STOP;
  1264. freed = __scan(c, sc->nr_to_scan, sc->gfp_mask);
  1265. dm_bufio_unlock(c);
  1266. return freed;
  1267. }
  1268. static unsigned long
  1269. dm_bufio_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  1270. {
  1271. struct dm_bufio_client *c;
  1272. unsigned long count;
  1273. c = container_of(shrink, struct dm_bufio_client, shrinker);
  1274. if (sc->gfp_mask & __GFP_FS)
  1275. dm_bufio_lock(c);
  1276. else if (!dm_bufio_trylock(c))
  1277. return 0;
  1278. count = c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY];
  1279. dm_bufio_unlock(c);
  1280. return count;
  1281. }
  1282. /*
  1283. * Create the buffering interface
  1284. */
  1285. struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsigned block_size,
  1286. unsigned reserved_buffers, unsigned aux_size,
  1287. void (*alloc_callback)(struct dm_buffer *),
  1288. void (*write_callback)(struct dm_buffer *))
  1289. {
  1290. int r;
  1291. struct dm_bufio_client *c;
  1292. unsigned i;
  1293. BUG_ON(block_size < 1 << SECTOR_SHIFT ||
  1294. (block_size & (block_size - 1)));
  1295. c = kzalloc(sizeof(*c), GFP_KERNEL);
  1296. if (!c) {
  1297. r = -ENOMEM;
  1298. goto bad_client;
  1299. }
  1300. c->buffer_tree = RB_ROOT;
  1301. c->bdev = bdev;
  1302. c->block_size = block_size;
  1303. c->sectors_per_block_bits = ffs(block_size) - 1 - SECTOR_SHIFT;
  1304. c->pages_per_block_bits = (ffs(block_size) - 1 >= PAGE_SHIFT) ?
  1305. ffs(block_size) - 1 - PAGE_SHIFT : 0;
  1306. c->blocks_per_page_bits = (ffs(block_size) - 1 < PAGE_SHIFT ?
  1307. PAGE_SHIFT - (ffs(block_size) - 1) : 0);
  1308. c->aux_size = aux_size;
  1309. c->alloc_callback = alloc_callback;
  1310. c->write_callback = write_callback;
  1311. for (i = 0; i < LIST_SIZE; i++) {
  1312. INIT_LIST_HEAD(&c->lru[i]);
  1313. c->n_buffers[i] = 0;
  1314. }
  1315. mutex_init(&c->lock);
  1316. INIT_LIST_HEAD(&c->reserved_buffers);
  1317. c->need_reserved_buffers = reserved_buffers;
  1318. c->minimum_buffers = DM_BUFIO_MIN_BUFFERS;
  1319. init_waitqueue_head(&c->free_buffer_wait);
  1320. c->async_write_error = 0;
  1321. c->dm_io = dm_io_client_create();
  1322. if (IS_ERR(c->dm_io)) {
  1323. r = PTR_ERR(c->dm_io);
  1324. goto bad_dm_io;
  1325. }
  1326. mutex_lock(&dm_bufio_clients_lock);
  1327. if (c->blocks_per_page_bits) {
  1328. if (!DM_BUFIO_CACHE_NAME(c)) {
  1329. DM_BUFIO_CACHE_NAME(c) = kasprintf(GFP_KERNEL, "dm_bufio_cache-%u", c->block_size);
  1330. if (!DM_BUFIO_CACHE_NAME(c)) {
  1331. r = -ENOMEM;
  1332. mutex_unlock(&dm_bufio_clients_lock);
  1333. goto bad_cache;
  1334. }
  1335. }
  1336. if (!DM_BUFIO_CACHE(c)) {
  1337. DM_BUFIO_CACHE(c) = kmem_cache_create(DM_BUFIO_CACHE_NAME(c),
  1338. c->block_size,
  1339. c->block_size, 0, NULL);
  1340. if (!DM_BUFIO_CACHE(c)) {
  1341. r = -ENOMEM;
  1342. mutex_unlock(&dm_bufio_clients_lock);
  1343. goto bad_cache;
  1344. }
  1345. }
  1346. }
  1347. mutex_unlock(&dm_bufio_clients_lock);
  1348. while (c->need_reserved_buffers) {
  1349. struct dm_buffer *b = alloc_buffer(c, GFP_KERNEL);
  1350. if (!b) {
  1351. r = -ENOMEM;
  1352. goto bad_buffer;
  1353. }
  1354. __free_buffer_wake(b);
  1355. }
  1356. mutex_lock(&dm_bufio_clients_lock);
  1357. dm_bufio_client_count++;
  1358. list_add(&c->client_list, &dm_bufio_all_clients);
  1359. __cache_size_refresh();
  1360. mutex_unlock(&dm_bufio_clients_lock);
  1361. c->shrinker.count_objects = dm_bufio_shrink_count;
  1362. c->shrinker.scan_objects = dm_bufio_shrink_scan;
  1363. c->shrinker.seeks = 1;
  1364. c->shrinker.batch = 0;
  1365. register_shrinker(&c->shrinker);
  1366. return c;
  1367. bad_buffer:
  1368. bad_cache:
  1369. while (!list_empty(&c->reserved_buffers)) {
  1370. struct dm_buffer *b = list_entry(c->reserved_buffers.next,
  1371. struct dm_buffer, lru_list);
  1372. list_del(&b->lru_list);
  1373. free_buffer(b);
  1374. }
  1375. dm_io_client_destroy(c->dm_io);
  1376. bad_dm_io:
  1377. kfree(c);
  1378. bad_client:
  1379. return ERR_PTR(r);
  1380. }
  1381. EXPORT_SYMBOL_GPL(dm_bufio_client_create);
  1382. /*
  1383. * Free the buffering interface.
  1384. * It is required that there are no references on any buffers.
  1385. */
  1386. void dm_bufio_client_destroy(struct dm_bufio_client *c)
  1387. {
  1388. unsigned i;
  1389. drop_buffers(c);
  1390. unregister_shrinker(&c->shrinker);
  1391. mutex_lock(&dm_bufio_clients_lock);
  1392. list_del(&c->client_list);
  1393. dm_bufio_client_count--;
  1394. __cache_size_refresh();
  1395. mutex_unlock(&dm_bufio_clients_lock);
  1396. BUG_ON(!RB_EMPTY_ROOT(&c->buffer_tree));
  1397. BUG_ON(c->need_reserved_buffers);
  1398. while (!list_empty(&c->reserved_buffers)) {
  1399. struct dm_buffer *b = list_entry(c->reserved_buffers.next,
  1400. struct dm_buffer, lru_list);
  1401. list_del(&b->lru_list);
  1402. free_buffer(b);
  1403. }
  1404. for (i = 0; i < LIST_SIZE; i++)
  1405. if (c->n_buffers[i])
  1406. DMERR("leaked buffer count %d: %ld", i, c->n_buffers[i]);
  1407. for (i = 0; i < LIST_SIZE; i++)
  1408. BUG_ON(c->n_buffers[i]);
  1409. dm_io_client_destroy(c->dm_io);
  1410. kfree(c);
  1411. }
  1412. EXPORT_SYMBOL_GPL(dm_bufio_client_destroy);
  1413. static unsigned get_max_age_hz(void)
  1414. {
  1415. unsigned max_age = ACCESS_ONCE(dm_bufio_max_age);
  1416. if (max_age > UINT_MAX / HZ)
  1417. max_age = UINT_MAX / HZ;
  1418. return max_age * HZ;
  1419. }
  1420. static bool older_than(struct dm_buffer *b, unsigned long age_hz)
  1421. {
  1422. return time_after_eq(jiffies, b->last_accessed + age_hz);
  1423. }
  1424. static void __evict_old_buffers(struct dm_bufio_client *c, unsigned long age_hz)
  1425. {
  1426. struct dm_buffer *b, *tmp;
  1427. unsigned retain_target = get_retain_buffers(c);
  1428. unsigned count;
  1429. dm_bufio_lock(c);
  1430. count = c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY];
  1431. list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_CLEAN], lru_list) {
  1432. if (count <= retain_target)
  1433. break;
  1434. if (!older_than(b, age_hz))
  1435. break;
  1436. if (__try_evict_buffer(b, 0))
  1437. count--;
  1438. dm_bufio_cond_resched();
  1439. }
  1440. dm_bufio_unlock(c);
  1441. }
  1442. static void cleanup_old_buffers(void)
  1443. {
  1444. unsigned long max_age_hz = get_max_age_hz();
  1445. struct dm_bufio_client *c;
  1446. mutex_lock(&dm_bufio_clients_lock);
  1447. list_for_each_entry(c, &dm_bufio_all_clients, client_list)
  1448. __evict_old_buffers(c, max_age_hz);
  1449. mutex_unlock(&dm_bufio_clients_lock);
  1450. }
  1451. static struct workqueue_struct *dm_bufio_wq;
  1452. static struct delayed_work dm_bufio_work;
  1453. static void work_fn(struct work_struct *w)
  1454. {
  1455. cleanup_old_buffers();
  1456. queue_delayed_work(dm_bufio_wq, &dm_bufio_work,
  1457. DM_BUFIO_WORK_TIMER_SECS * HZ);
  1458. }
  1459. /*----------------------------------------------------------------
  1460. * Module setup
  1461. *--------------------------------------------------------------*/
  1462. /*
  1463. * This is called only once for the whole dm_bufio module.
  1464. * It initializes memory limit.
  1465. */
  1466. static int __init dm_bufio_init(void)
  1467. {
  1468. __u64 mem;
  1469. dm_bufio_allocated_kmem_cache = 0;
  1470. dm_bufio_allocated_get_free_pages = 0;
  1471. dm_bufio_allocated_vmalloc = 0;
  1472. dm_bufio_current_allocated = 0;
  1473. memset(&dm_bufio_caches, 0, sizeof dm_bufio_caches);
  1474. memset(&dm_bufio_cache_names, 0, sizeof dm_bufio_cache_names);
  1475. mem = (__u64)((totalram_pages - totalhigh_pages) *
  1476. DM_BUFIO_MEMORY_PERCENT / 100) << PAGE_SHIFT;
  1477. if (mem > ULONG_MAX)
  1478. mem = ULONG_MAX;
  1479. #ifdef CONFIG_MMU
  1480. /*
  1481. * Get the size of vmalloc space the same way as VMALLOC_TOTAL
  1482. * in fs/proc/internal.h
  1483. */
  1484. if (mem > (VMALLOC_END - VMALLOC_START) * DM_BUFIO_VMALLOC_PERCENT / 100)
  1485. mem = (VMALLOC_END - VMALLOC_START) * DM_BUFIO_VMALLOC_PERCENT / 100;
  1486. #endif
  1487. dm_bufio_default_cache_size = mem;
  1488. mutex_lock(&dm_bufio_clients_lock);
  1489. __cache_size_refresh();
  1490. mutex_unlock(&dm_bufio_clients_lock);
  1491. dm_bufio_wq = create_singlethread_workqueue("dm_bufio_cache");
  1492. if (!dm_bufio_wq)
  1493. return -ENOMEM;
  1494. INIT_DELAYED_WORK(&dm_bufio_work, work_fn);
  1495. queue_delayed_work(dm_bufio_wq, &dm_bufio_work,
  1496. DM_BUFIO_WORK_TIMER_SECS * HZ);
  1497. return 0;
  1498. }
  1499. /*
  1500. * This is called once when unloading the dm_bufio module.
  1501. */
  1502. static void __exit dm_bufio_exit(void)
  1503. {
  1504. int bug = 0;
  1505. int i;
  1506. cancel_delayed_work_sync(&dm_bufio_work);
  1507. destroy_workqueue(dm_bufio_wq);
  1508. for (i = 0; i < ARRAY_SIZE(dm_bufio_caches); i++) {
  1509. struct kmem_cache *kc = dm_bufio_caches[i];
  1510. if (kc)
  1511. kmem_cache_destroy(kc);
  1512. }
  1513. for (i = 0; i < ARRAY_SIZE(dm_bufio_cache_names); i++)
  1514. kfree(dm_bufio_cache_names[i]);
  1515. if (dm_bufio_client_count) {
  1516. DMCRIT("%s: dm_bufio_client_count leaked: %d",
  1517. __func__, dm_bufio_client_count);
  1518. bug = 1;
  1519. }
  1520. if (dm_bufio_current_allocated) {
  1521. DMCRIT("%s: dm_bufio_current_allocated leaked: %lu",
  1522. __func__, dm_bufio_current_allocated);
  1523. bug = 1;
  1524. }
  1525. if (dm_bufio_allocated_get_free_pages) {
  1526. DMCRIT("%s: dm_bufio_allocated_get_free_pages leaked: %lu",
  1527. __func__, dm_bufio_allocated_get_free_pages);
  1528. bug = 1;
  1529. }
  1530. if (dm_bufio_allocated_vmalloc) {
  1531. DMCRIT("%s: dm_bufio_vmalloc leaked: %lu",
  1532. __func__, dm_bufio_allocated_vmalloc);
  1533. bug = 1;
  1534. }
  1535. if (bug)
  1536. BUG();
  1537. }
  1538. module_init(dm_bufio_init)
  1539. module_exit(dm_bufio_exit)
  1540. module_param_named(max_cache_size_bytes, dm_bufio_cache_size, ulong, S_IRUGO | S_IWUSR);
  1541. MODULE_PARM_DESC(max_cache_size_bytes, "Size of metadata cache");
  1542. module_param_named(max_age_seconds, dm_bufio_max_age, uint, S_IRUGO | S_IWUSR);
  1543. MODULE_PARM_DESC(max_age_seconds, "Max age of a buffer in seconds");
  1544. module_param_named(retain_bytes, dm_bufio_retain_bytes, uint, S_IRUGO | S_IWUSR);
  1545. MODULE_PARM_DESC(retain_bytes, "Try to keep at least this many bytes cached in memory");
  1546. module_param_named(peak_allocated_bytes, dm_bufio_peak_allocated, ulong, S_IRUGO | S_IWUSR);
  1547. MODULE_PARM_DESC(peak_allocated_bytes, "Tracks the maximum allocated memory");
  1548. module_param_named(allocated_kmem_cache_bytes, dm_bufio_allocated_kmem_cache, ulong, S_IRUGO);
  1549. MODULE_PARM_DESC(allocated_kmem_cache_bytes, "Memory allocated with kmem_cache_alloc");
  1550. module_param_named(allocated_get_free_pages_bytes, dm_bufio_allocated_get_free_pages, ulong, S_IRUGO);
  1551. MODULE_PARM_DESC(allocated_get_free_pages_bytes, "Memory allocated with get_free_pages");
  1552. module_param_named(allocated_vmalloc_bytes, dm_bufio_allocated_vmalloc, ulong, S_IRUGO);
  1553. MODULE_PARM_DESC(allocated_vmalloc_bytes, "Memory allocated with vmalloc");
  1554. module_param_named(current_allocated_bytes, dm_bufio_current_allocated, ulong, S_IRUGO);
  1555. MODULE_PARM_DESC(current_allocated_bytes, "Memory currently used by the cache");
  1556. MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
  1557. MODULE_DESCRIPTION(DM_NAME " buffered I/O library");
  1558. MODULE_LICENSE("GPL");