dm-bufio.c 44 KB

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