dm-bufio.c 44 KB

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