dm-bufio.c 43 KB

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