dm-bufio.c 43 KB

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