dm-bufio.c 46 KB

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