dm-bufio.c 41 KB

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