dm-bufio.c 41 KB

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