dm-bufio.c 45 KB

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