bitmap.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200
  1. /*
  2. * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
  3. *
  4. * bitmap_create - sets up the bitmap structure
  5. * bitmap_destroy - destroys the bitmap structure
  6. *
  7. * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.:
  8. * - added disk storage for bitmap
  9. * - changes to allow various bitmap chunk sizes
  10. */
  11. /*
  12. * Still to do:
  13. *
  14. * flush after percent set rather than just time based. (maybe both).
  15. */
  16. #include <linux/blkdev.h>
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/slab.h>
  20. #include <linux/init.h>
  21. #include <linux/timer.h>
  22. #include <linux/sched.h>
  23. #include <linux/list.h>
  24. #include <linux/file.h>
  25. #include <linux/mount.h>
  26. #include <linux/buffer_head.h>
  27. #include "md.h"
  28. #include "bitmap.h"
  29. #include <linux/dm-dirty-log.h>
  30. /* debug macros */
  31. #define DEBUG 0
  32. #if DEBUG
  33. /* these are for debugging purposes only! */
  34. /* define one and only one of these */
  35. #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
  36. #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
  37. #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
  38. #define INJECT_FAULTS_4 0 /* undef */
  39. #define INJECT_FAULTS_5 0 /* undef */
  40. #define INJECT_FAULTS_6 0
  41. /* if these are defined, the driver will fail! debug only */
  42. #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
  43. #define INJECT_FATAL_FAULT_2 0 /* undef */
  44. #define INJECT_FATAL_FAULT_3 0 /* undef */
  45. #endif
  46. #ifndef PRINTK
  47. # if DEBUG > 0
  48. # define PRINTK(x...) printk(KERN_DEBUG x)
  49. # else
  50. # define PRINTK(x...)
  51. # endif
  52. #endif
  53. static inline char *bmname(struct bitmap *bitmap)
  54. {
  55. return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
  56. }
  57. /*
  58. * just a placeholder - calls kmalloc for bitmap pages
  59. */
  60. static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
  61. {
  62. unsigned char *page;
  63. #ifdef INJECT_FAULTS_1
  64. page = NULL;
  65. #else
  66. page = kzalloc(PAGE_SIZE, GFP_NOIO);
  67. #endif
  68. if (!page)
  69. printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
  70. else
  71. PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
  72. bmname(bitmap), page);
  73. return page;
  74. }
  75. /*
  76. * for now just a placeholder -- just calls kfree for bitmap pages
  77. */
  78. static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
  79. {
  80. PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
  81. kfree(page);
  82. }
  83. /*
  84. * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
  85. *
  86. * 1) check to see if this page is allocated, if it's not then try to alloc
  87. * 2) if the alloc fails, set the page's hijacked flag so we'll use the
  88. * page pointer directly as a counter
  89. *
  90. * if we find our page, we increment the page's refcount so that it stays
  91. * allocated while we're using it
  92. */
  93. static int bitmap_checkpage(struct bitmap *bitmap,
  94. unsigned long page, int create)
  95. __releases(bitmap->lock)
  96. __acquires(bitmap->lock)
  97. {
  98. unsigned char *mappage;
  99. if (page >= bitmap->pages) {
  100. /* This can happen if bitmap_start_sync goes beyond
  101. * End-of-device while looking for a whole page.
  102. * It is harmless.
  103. */
  104. return -EINVAL;
  105. }
  106. if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
  107. return 0;
  108. if (bitmap->bp[page].map) /* page is already allocated, just return */
  109. return 0;
  110. if (!create)
  111. return -ENOENT;
  112. /* this page has not been allocated yet */
  113. spin_unlock_irq(&bitmap->lock);
  114. mappage = bitmap_alloc_page(bitmap);
  115. spin_lock_irq(&bitmap->lock);
  116. if (mappage == NULL) {
  117. PRINTK("%s: bitmap map page allocation failed, hijacking\n",
  118. bmname(bitmap));
  119. /* failed - set the hijacked flag so that we can use the
  120. * pointer as a counter */
  121. if (!bitmap->bp[page].map)
  122. bitmap->bp[page].hijacked = 1;
  123. } else if (bitmap->bp[page].map ||
  124. bitmap->bp[page].hijacked) {
  125. /* somebody beat us to getting the page */
  126. bitmap_free_page(bitmap, mappage);
  127. return 0;
  128. } else {
  129. /* no page was in place and we have one, so install it */
  130. bitmap->bp[page].map = mappage;
  131. bitmap->missing_pages--;
  132. }
  133. return 0;
  134. }
  135. /* if page is completely empty, put it back on the free list, or dealloc it */
  136. /* if page was hijacked, unmark the flag so it might get alloced next time */
  137. /* Note: lock should be held when calling this */
  138. static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
  139. {
  140. char *ptr;
  141. if (bitmap->bp[page].count) /* page is still busy */
  142. return;
  143. /* page is no longer in use, it can be released */
  144. if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
  145. bitmap->bp[page].hijacked = 0;
  146. bitmap->bp[page].map = NULL;
  147. } else {
  148. /* normal case, free the page */
  149. ptr = bitmap->bp[page].map;
  150. bitmap->bp[page].map = NULL;
  151. bitmap->missing_pages++;
  152. bitmap_free_page(bitmap, ptr);
  153. }
  154. }
  155. /*
  156. * bitmap file handling - read and write the bitmap file and its superblock
  157. */
  158. /*
  159. * basic page I/O operations
  160. */
  161. /* IO operations when bitmap is stored near all superblocks */
  162. static struct page *read_sb_page(mddev_t *mddev, loff_t offset,
  163. struct page *page,
  164. unsigned long index, int size)
  165. {
  166. /* choose a good rdev and read the page from there */
  167. mdk_rdev_t *rdev;
  168. sector_t target;
  169. int did_alloc = 0;
  170. if (!page) {
  171. page = alloc_page(GFP_KERNEL);
  172. if (!page)
  173. return ERR_PTR(-ENOMEM);
  174. did_alloc = 1;
  175. }
  176. list_for_each_entry(rdev, &mddev->disks, same_set) {
  177. if (! test_bit(In_sync, &rdev->flags)
  178. || test_bit(Faulty, &rdev->flags))
  179. continue;
  180. target = offset + index * (PAGE_SIZE/512);
  181. if (sync_page_io(rdev, target,
  182. roundup(size, bdev_logical_block_size(rdev->bdev)),
  183. page, READ, true)) {
  184. page->index = index;
  185. attach_page_buffers(page, NULL); /* so that free_buffer will
  186. * quietly no-op */
  187. return page;
  188. }
  189. }
  190. if (did_alloc)
  191. put_page(page);
  192. return ERR_PTR(-EIO);
  193. }
  194. static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
  195. {
  196. /* Iterate the disks of an mddev, using rcu to protect access to the
  197. * linked list, and raising the refcount of devices we return to ensure
  198. * they don't disappear while in use.
  199. * As devices are only added or removed when raid_disk is < 0 and
  200. * nr_pending is 0 and In_sync is clear, the entries we return will
  201. * still be in the same position on the list when we re-enter
  202. * list_for_each_continue_rcu.
  203. */
  204. struct list_head *pos;
  205. rcu_read_lock();
  206. if (rdev == NULL)
  207. /* start at the beginning */
  208. pos = &mddev->disks;
  209. else {
  210. /* release the previous rdev and start from there. */
  211. rdev_dec_pending(rdev, mddev);
  212. pos = &rdev->same_set;
  213. }
  214. list_for_each_continue_rcu(pos, &mddev->disks) {
  215. rdev = list_entry(pos, mdk_rdev_t, same_set);
  216. if (rdev->raid_disk >= 0 &&
  217. !test_bit(Faulty, &rdev->flags)) {
  218. /* this is a usable devices */
  219. atomic_inc(&rdev->nr_pending);
  220. rcu_read_unlock();
  221. return rdev;
  222. }
  223. }
  224. rcu_read_unlock();
  225. return NULL;
  226. }
  227. static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
  228. {
  229. mdk_rdev_t *rdev = NULL;
  230. struct block_device *bdev;
  231. mddev_t *mddev = bitmap->mddev;
  232. while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
  233. int size = PAGE_SIZE;
  234. loff_t offset = mddev->bitmap_info.offset;
  235. bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
  236. if (page->index == bitmap->file_pages-1)
  237. size = roundup(bitmap->last_page_size,
  238. bdev_logical_block_size(bdev));
  239. /* Just make sure we aren't corrupting data or
  240. * metadata
  241. */
  242. if (mddev->external) {
  243. /* Bitmap could be anywhere. */
  244. if (rdev->sb_start + offset + (page->index
  245. * (PAGE_SIZE/512))
  246. > rdev->data_offset
  247. &&
  248. rdev->sb_start + offset
  249. < (rdev->data_offset + mddev->dev_sectors
  250. + (PAGE_SIZE/512)))
  251. goto bad_alignment;
  252. } else if (offset < 0) {
  253. /* DATA BITMAP METADATA */
  254. if (offset
  255. + (long)(page->index * (PAGE_SIZE/512))
  256. + size/512 > 0)
  257. /* bitmap runs in to metadata */
  258. goto bad_alignment;
  259. if (rdev->data_offset + mddev->dev_sectors
  260. > rdev->sb_start + offset)
  261. /* data runs in to bitmap */
  262. goto bad_alignment;
  263. } else if (rdev->sb_start < rdev->data_offset) {
  264. /* METADATA BITMAP DATA */
  265. if (rdev->sb_start
  266. + offset
  267. + page->index*(PAGE_SIZE/512) + size/512
  268. > rdev->data_offset)
  269. /* bitmap runs in to data */
  270. goto bad_alignment;
  271. } else {
  272. /* DATA METADATA BITMAP - no problems */
  273. }
  274. md_super_write(mddev, rdev,
  275. rdev->sb_start + offset
  276. + page->index * (PAGE_SIZE/512),
  277. size,
  278. page);
  279. }
  280. if (wait)
  281. md_super_wait(mddev);
  282. return 0;
  283. bad_alignment:
  284. return -EINVAL;
  285. }
  286. static void bitmap_file_kick(struct bitmap *bitmap);
  287. /*
  288. * write out a page to a file
  289. */
  290. static void write_page(struct bitmap *bitmap, struct page *page, int wait)
  291. {
  292. struct buffer_head *bh;
  293. if (bitmap->file == NULL) {
  294. switch (write_sb_page(bitmap, page, wait)) {
  295. case -EINVAL:
  296. bitmap->flags |= BITMAP_WRITE_ERROR;
  297. }
  298. } else {
  299. bh = page_buffers(page);
  300. while (bh && bh->b_blocknr) {
  301. atomic_inc(&bitmap->pending_writes);
  302. set_buffer_locked(bh);
  303. set_buffer_mapped(bh);
  304. submit_bh(WRITE | REQ_SYNC, bh);
  305. bh = bh->b_this_page;
  306. }
  307. if (wait)
  308. wait_event(bitmap->write_wait,
  309. atomic_read(&bitmap->pending_writes)==0);
  310. }
  311. if (bitmap->flags & BITMAP_WRITE_ERROR)
  312. bitmap_file_kick(bitmap);
  313. }
  314. static void end_bitmap_write(struct buffer_head *bh, int uptodate)
  315. {
  316. struct bitmap *bitmap = bh->b_private;
  317. unsigned long flags;
  318. if (!uptodate) {
  319. spin_lock_irqsave(&bitmap->lock, flags);
  320. bitmap->flags |= BITMAP_WRITE_ERROR;
  321. spin_unlock_irqrestore(&bitmap->lock, flags);
  322. }
  323. if (atomic_dec_and_test(&bitmap->pending_writes))
  324. wake_up(&bitmap->write_wait);
  325. }
  326. /* copied from buffer.c */
  327. static void
  328. __clear_page_buffers(struct page *page)
  329. {
  330. ClearPagePrivate(page);
  331. set_page_private(page, 0);
  332. page_cache_release(page);
  333. }
  334. static void free_buffers(struct page *page)
  335. {
  336. struct buffer_head *bh = page_buffers(page);
  337. while (bh) {
  338. struct buffer_head *next = bh->b_this_page;
  339. free_buffer_head(bh);
  340. bh = next;
  341. }
  342. __clear_page_buffers(page);
  343. put_page(page);
  344. }
  345. /* read a page from a file.
  346. * We both read the page, and attach buffers to the page to record the
  347. * address of each block (using bmap). These addresses will be used
  348. * to write the block later, completely bypassing the filesystem.
  349. * This usage is similar to how swap files are handled, and allows us
  350. * to write to a file with no concerns of memory allocation failing.
  351. */
  352. static struct page *read_page(struct file *file, unsigned long index,
  353. struct bitmap *bitmap,
  354. unsigned long count)
  355. {
  356. struct page *page = NULL;
  357. struct inode *inode = file->f_path.dentry->d_inode;
  358. struct buffer_head *bh;
  359. sector_t block;
  360. PRINTK("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
  361. (unsigned long long)index << PAGE_SHIFT);
  362. page = alloc_page(GFP_KERNEL);
  363. if (!page)
  364. page = ERR_PTR(-ENOMEM);
  365. if (IS_ERR(page))
  366. goto out;
  367. bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
  368. if (!bh) {
  369. put_page(page);
  370. page = ERR_PTR(-ENOMEM);
  371. goto out;
  372. }
  373. attach_page_buffers(page, bh);
  374. block = index << (PAGE_SHIFT - inode->i_blkbits);
  375. while (bh) {
  376. if (count == 0)
  377. bh->b_blocknr = 0;
  378. else {
  379. bh->b_blocknr = bmap(inode, block);
  380. if (bh->b_blocknr == 0) {
  381. /* Cannot use this file! */
  382. free_buffers(page);
  383. page = ERR_PTR(-EINVAL);
  384. goto out;
  385. }
  386. bh->b_bdev = inode->i_sb->s_bdev;
  387. if (count < (1<<inode->i_blkbits))
  388. count = 0;
  389. else
  390. count -= (1<<inode->i_blkbits);
  391. bh->b_end_io = end_bitmap_write;
  392. bh->b_private = bitmap;
  393. atomic_inc(&bitmap->pending_writes);
  394. set_buffer_locked(bh);
  395. set_buffer_mapped(bh);
  396. submit_bh(READ, bh);
  397. }
  398. block++;
  399. bh = bh->b_this_page;
  400. }
  401. page->index = index;
  402. wait_event(bitmap->write_wait,
  403. atomic_read(&bitmap->pending_writes)==0);
  404. if (bitmap->flags & BITMAP_WRITE_ERROR) {
  405. free_buffers(page);
  406. page = ERR_PTR(-EIO);
  407. }
  408. out:
  409. if (IS_ERR(page))
  410. printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n",
  411. (int)PAGE_SIZE,
  412. (unsigned long long)index << PAGE_SHIFT,
  413. PTR_ERR(page));
  414. return page;
  415. }
  416. /*
  417. * bitmap file superblock operations
  418. */
  419. /* update the event counter and sync the superblock to disk */
  420. void bitmap_update_sb(struct bitmap *bitmap)
  421. {
  422. bitmap_super_t *sb;
  423. unsigned long flags;
  424. if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
  425. return;
  426. if (bitmap->mddev->bitmap_info.external)
  427. return;
  428. spin_lock_irqsave(&bitmap->lock, flags);
  429. if (!bitmap->sb_page) { /* no superblock */
  430. spin_unlock_irqrestore(&bitmap->lock, flags);
  431. return;
  432. }
  433. spin_unlock_irqrestore(&bitmap->lock, flags);
  434. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  435. sb->events = cpu_to_le64(bitmap->mddev->events);
  436. if (bitmap->mddev->events < bitmap->events_cleared)
  437. /* rocking back to read-only */
  438. bitmap->events_cleared = bitmap->mddev->events;
  439. sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
  440. sb->state = cpu_to_le32(bitmap->flags);
  441. /* Just in case these have been changed via sysfs: */
  442. sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
  443. sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
  444. kunmap_atomic(sb, KM_USER0);
  445. write_page(bitmap, bitmap->sb_page, 1);
  446. }
  447. /* print out the bitmap file superblock */
  448. void bitmap_print_sb(struct bitmap *bitmap)
  449. {
  450. bitmap_super_t *sb;
  451. if (!bitmap || !bitmap->sb_page)
  452. return;
  453. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  454. printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
  455. printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
  456. printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
  457. printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
  458. *(__u32 *)(sb->uuid+0),
  459. *(__u32 *)(sb->uuid+4),
  460. *(__u32 *)(sb->uuid+8),
  461. *(__u32 *)(sb->uuid+12));
  462. printk(KERN_DEBUG " events: %llu\n",
  463. (unsigned long long) le64_to_cpu(sb->events));
  464. printk(KERN_DEBUG "events cleared: %llu\n",
  465. (unsigned long long) le64_to_cpu(sb->events_cleared));
  466. printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
  467. printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
  468. printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
  469. printk(KERN_DEBUG " sync size: %llu KB\n",
  470. (unsigned long long)le64_to_cpu(sb->sync_size)/2);
  471. printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
  472. kunmap_atomic(sb, KM_USER0);
  473. }
  474. /*
  475. * bitmap_new_disk_sb
  476. * @bitmap
  477. *
  478. * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
  479. * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
  480. * This function verifies 'bitmap_info' and populates the on-disk bitmap
  481. * structure, which is to be written to disk.
  482. *
  483. * Returns: 0 on success, -Exxx on error
  484. */
  485. static int bitmap_new_disk_sb(struct bitmap *bitmap)
  486. {
  487. bitmap_super_t *sb;
  488. unsigned long chunksize, daemon_sleep, write_behind;
  489. int err = -EINVAL;
  490. bitmap->sb_page = alloc_page(GFP_KERNEL);
  491. if (IS_ERR(bitmap->sb_page)) {
  492. err = PTR_ERR(bitmap->sb_page);
  493. bitmap->sb_page = NULL;
  494. return err;
  495. }
  496. bitmap->sb_page->index = 0;
  497. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  498. sb->magic = cpu_to_le32(BITMAP_MAGIC);
  499. sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
  500. chunksize = bitmap->mddev->bitmap_info.chunksize;
  501. BUG_ON(!chunksize);
  502. if (!is_power_of_2(chunksize)) {
  503. kunmap_atomic(sb, KM_USER0);
  504. printk(KERN_ERR "bitmap chunksize not a power of 2\n");
  505. return -EINVAL;
  506. }
  507. sb->chunksize = cpu_to_le32(chunksize);
  508. daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
  509. if (!daemon_sleep ||
  510. (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
  511. printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n");
  512. daemon_sleep = 5 * HZ;
  513. }
  514. sb->daemon_sleep = cpu_to_le32(daemon_sleep);
  515. bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
  516. /*
  517. * FIXME: write_behind for RAID1. If not specified, what
  518. * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
  519. */
  520. write_behind = bitmap->mddev->bitmap_info.max_write_behind;
  521. if (write_behind > COUNTER_MAX)
  522. write_behind = COUNTER_MAX / 2;
  523. sb->write_behind = cpu_to_le32(write_behind);
  524. bitmap->mddev->bitmap_info.max_write_behind = write_behind;
  525. /* keep the array size field of the bitmap superblock up to date */
  526. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  527. memcpy(sb->uuid, bitmap->mddev->uuid, 16);
  528. bitmap->flags |= BITMAP_STALE;
  529. sb->state |= cpu_to_le32(BITMAP_STALE);
  530. bitmap->events_cleared = bitmap->mddev->events;
  531. sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
  532. bitmap->flags |= BITMAP_HOSTENDIAN;
  533. sb->version = cpu_to_le32(BITMAP_MAJOR_HOSTENDIAN);
  534. kunmap_atomic(sb, KM_USER0);
  535. return 0;
  536. }
  537. /* read the superblock from the bitmap file and initialize some bitmap fields */
  538. static int bitmap_read_sb(struct bitmap *bitmap)
  539. {
  540. char *reason = NULL;
  541. bitmap_super_t *sb;
  542. unsigned long chunksize, daemon_sleep, write_behind;
  543. unsigned long long events;
  544. int err = -EINVAL;
  545. /* page 0 is the superblock, read it... */
  546. if (bitmap->file) {
  547. loff_t isize = i_size_read(bitmap->file->f_mapping->host);
  548. int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
  549. bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
  550. } else {
  551. bitmap->sb_page = read_sb_page(bitmap->mddev,
  552. bitmap->mddev->bitmap_info.offset,
  553. NULL,
  554. 0, sizeof(bitmap_super_t));
  555. }
  556. if (IS_ERR(bitmap->sb_page)) {
  557. err = PTR_ERR(bitmap->sb_page);
  558. bitmap->sb_page = NULL;
  559. return err;
  560. }
  561. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  562. chunksize = le32_to_cpu(sb->chunksize);
  563. daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
  564. write_behind = le32_to_cpu(sb->write_behind);
  565. /* verify that the bitmap-specific fields are valid */
  566. if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
  567. reason = "bad magic";
  568. else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
  569. le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
  570. reason = "unrecognized superblock version";
  571. else if (chunksize < 512)
  572. reason = "bitmap chunksize too small";
  573. else if (!is_power_of_2(chunksize))
  574. reason = "bitmap chunksize not a power of 2";
  575. else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
  576. reason = "daemon sleep period out of range";
  577. else if (write_behind > COUNTER_MAX)
  578. reason = "write-behind limit out of range (0 - 16383)";
  579. if (reason) {
  580. printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
  581. bmname(bitmap), reason);
  582. goto out;
  583. }
  584. /* keep the array size field of the bitmap superblock up to date */
  585. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  586. if (!bitmap->mddev->persistent)
  587. goto success;
  588. /*
  589. * if we have a persistent array superblock, compare the
  590. * bitmap's UUID and event counter to the mddev's
  591. */
  592. if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
  593. printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
  594. bmname(bitmap));
  595. goto out;
  596. }
  597. events = le64_to_cpu(sb->events);
  598. if (events < bitmap->mddev->events) {
  599. printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
  600. "-- forcing full recovery\n", bmname(bitmap), events,
  601. (unsigned long long) bitmap->mddev->events);
  602. sb->state |= cpu_to_le32(BITMAP_STALE);
  603. }
  604. success:
  605. /* assign fields using values from superblock */
  606. bitmap->mddev->bitmap_info.chunksize = chunksize;
  607. bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
  608. bitmap->mddev->bitmap_info.max_write_behind = write_behind;
  609. bitmap->flags |= le32_to_cpu(sb->state);
  610. if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
  611. bitmap->flags |= BITMAP_HOSTENDIAN;
  612. bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
  613. if (bitmap->flags & BITMAP_STALE)
  614. bitmap->events_cleared = bitmap->mddev->events;
  615. err = 0;
  616. out:
  617. kunmap_atomic(sb, KM_USER0);
  618. if (err)
  619. bitmap_print_sb(bitmap);
  620. return err;
  621. }
  622. enum bitmap_mask_op {
  623. MASK_SET,
  624. MASK_UNSET
  625. };
  626. /* record the state of the bitmap in the superblock. Return the old value */
  627. static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
  628. enum bitmap_mask_op op)
  629. {
  630. bitmap_super_t *sb;
  631. unsigned long flags;
  632. int old;
  633. spin_lock_irqsave(&bitmap->lock, flags);
  634. if (!bitmap->sb_page) { /* can't set the state */
  635. spin_unlock_irqrestore(&bitmap->lock, flags);
  636. return 0;
  637. }
  638. spin_unlock_irqrestore(&bitmap->lock, flags);
  639. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  640. old = le32_to_cpu(sb->state) & bits;
  641. switch (op) {
  642. case MASK_SET:
  643. sb->state |= cpu_to_le32(bits);
  644. bitmap->flags |= bits;
  645. break;
  646. case MASK_UNSET:
  647. sb->state &= cpu_to_le32(~bits);
  648. bitmap->flags &= ~bits;
  649. break;
  650. default:
  651. BUG();
  652. }
  653. kunmap_atomic(sb, KM_USER0);
  654. return old;
  655. }
  656. /*
  657. * general bitmap file operations
  658. */
  659. /*
  660. * on-disk bitmap:
  661. *
  662. * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
  663. * file a page at a time. There's a superblock at the start of the file.
  664. */
  665. /* calculate the index of the page that contains this bit */
  666. static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
  667. {
  668. if (!bitmap->mddev->bitmap_info.external)
  669. chunk += sizeof(bitmap_super_t) << 3;
  670. return chunk >> PAGE_BIT_SHIFT;
  671. }
  672. /* calculate the (bit) offset of this bit within a page */
  673. static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
  674. {
  675. if (!bitmap->mddev->bitmap_info.external)
  676. chunk += sizeof(bitmap_super_t) << 3;
  677. return chunk & (PAGE_BITS - 1);
  678. }
  679. /*
  680. * return a pointer to the page in the filemap that contains the given bit
  681. *
  682. * this lookup is complicated by the fact that the bitmap sb might be exactly
  683. * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
  684. * 0 or page 1
  685. */
  686. static inline struct page *filemap_get_page(struct bitmap *bitmap,
  687. unsigned long chunk)
  688. {
  689. if (bitmap->filemap == NULL)
  690. return NULL;
  691. if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
  692. return NULL;
  693. return bitmap->filemap[file_page_index(bitmap, chunk)
  694. - file_page_index(bitmap, 0)];
  695. }
  696. static void bitmap_file_unmap(struct bitmap *bitmap)
  697. {
  698. struct page **map, *sb_page;
  699. unsigned long *attr;
  700. int pages;
  701. unsigned long flags;
  702. spin_lock_irqsave(&bitmap->lock, flags);
  703. map = bitmap->filemap;
  704. bitmap->filemap = NULL;
  705. attr = bitmap->filemap_attr;
  706. bitmap->filemap_attr = NULL;
  707. pages = bitmap->file_pages;
  708. bitmap->file_pages = 0;
  709. sb_page = bitmap->sb_page;
  710. bitmap->sb_page = NULL;
  711. spin_unlock_irqrestore(&bitmap->lock, flags);
  712. while (pages--)
  713. if (map[pages] != sb_page) /* 0 is sb_page, release it below */
  714. free_buffers(map[pages]);
  715. kfree(map);
  716. kfree(attr);
  717. if (sb_page)
  718. free_buffers(sb_page);
  719. }
  720. static void bitmap_file_put(struct bitmap *bitmap)
  721. {
  722. struct file *file;
  723. unsigned long flags;
  724. spin_lock_irqsave(&bitmap->lock, flags);
  725. file = bitmap->file;
  726. bitmap->file = NULL;
  727. spin_unlock_irqrestore(&bitmap->lock, flags);
  728. if (file)
  729. wait_event(bitmap->write_wait,
  730. atomic_read(&bitmap->pending_writes)==0);
  731. bitmap_file_unmap(bitmap);
  732. if (file) {
  733. struct inode *inode = file->f_path.dentry->d_inode;
  734. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  735. fput(file);
  736. }
  737. }
  738. /*
  739. * bitmap_file_kick - if an error occurs while manipulating the bitmap file
  740. * then it is no longer reliable, so we stop using it and we mark the file
  741. * as failed in the superblock
  742. */
  743. static void bitmap_file_kick(struct bitmap *bitmap)
  744. {
  745. char *path, *ptr = NULL;
  746. if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
  747. bitmap_update_sb(bitmap);
  748. if (bitmap->file) {
  749. path = kmalloc(PAGE_SIZE, GFP_KERNEL);
  750. if (path)
  751. ptr = d_path(&bitmap->file->f_path, path,
  752. PAGE_SIZE);
  753. printk(KERN_ALERT
  754. "%s: kicking failed bitmap file %s from array!\n",
  755. bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
  756. kfree(path);
  757. } else
  758. printk(KERN_ALERT
  759. "%s: disabling internal bitmap due to errors\n",
  760. bmname(bitmap));
  761. }
  762. bitmap_file_put(bitmap);
  763. return;
  764. }
  765. enum bitmap_page_attr {
  766. BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
  767. BITMAP_PAGE_CLEAN = 1, /* there are bits that might need to be cleared */
  768. BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
  769. };
  770. static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
  771. enum bitmap_page_attr attr)
  772. {
  773. if (page)
  774. __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
  775. else
  776. __set_bit(attr, &bitmap->logattrs);
  777. }
  778. static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
  779. enum bitmap_page_attr attr)
  780. {
  781. if (page)
  782. __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
  783. else
  784. __clear_bit(attr, &bitmap->logattrs);
  785. }
  786. static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
  787. enum bitmap_page_attr attr)
  788. {
  789. if (page)
  790. return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
  791. else
  792. return test_bit(attr, &bitmap->logattrs);
  793. }
  794. /*
  795. * bitmap_file_set_bit -- called before performing a write to the md device
  796. * to set (and eventually sync) a particular bit in the bitmap file
  797. *
  798. * we set the bit immediately, then we record the page number so that
  799. * when an unplug occurs, we can flush the dirty pages out to disk
  800. */
  801. static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
  802. {
  803. unsigned long bit;
  804. struct page *page = NULL;
  805. void *kaddr;
  806. unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
  807. if (!bitmap->filemap) {
  808. struct dm_dirty_log *log = bitmap->mddev->bitmap_info.log;
  809. if (log)
  810. log->type->mark_region(log, chunk);
  811. } else {
  812. page = filemap_get_page(bitmap, chunk);
  813. if (!page)
  814. return;
  815. bit = file_page_offset(bitmap, chunk);
  816. /* set the bit */
  817. kaddr = kmap_atomic(page, KM_USER0);
  818. if (bitmap->flags & BITMAP_HOSTENDIAN)
  819. set_bit(bit, kaddr);
  820. else
  821. __test_and_set_bit_le(bit, kaddr);
  822. kunmap_atomic(kaddr, KM_USER0);
  823. PRINTK("set file bit %lu page %lu\n", bit, page->index);
  824. }
  825. /* record page number so it gets flushed to disk when unplug occurs */
  826. set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  827. }
  828. /* this gets called when the md device is ready to unplug its underlying
  829. * (slave) device queues -- before we let any writes go down, we need to
  830. * sync the dirty pages of the bitmap file to disk */
  831. void bitmap_unplug(struct bitmap *bitmap)
  832. {
  833. unsigned long i, flags;
  834. int dirty, need_write;
  835. struct page *page;
  836. int wait = 0;
  837. if (!bitmap)
  838. return;
  839. if (!bitmap->filemap) {
  840. /* Must be using a dirty_log */
  841. struct dm_dirty_log *log = bitmap->mddev->bitmap_info.log;
  842. dirty = test_and_clear_bit(BITMAP_PAGE_DIRTY, &bitmap->logattrs);
  843. need_write = test_and_clear_bit(BITMAP_PAGE_NEEDWRITE, &bitmap->logattrs);
  844. if (dirty || need_write)
  845. if (log->type->flush(log))
  846. bitmap->flags |= BITMAP_WRITE_ERROR;
  847. goto out;
  848. }
  849. /* look at each page to see if there are any set bits that need to be
  850. * flushed out to disk */
  851. for (i = 0; i < bitmap->file_pages; i++) {
  852. spin_lock_irqsave(&bitmap->lock, flags);
  853. if (!bitmap->filemap) {
  854. spin_unlock_irqrestore(&bitmap->lock, flags);
  855. return;
  856. }
  857. page = bitmap->filemap[i];
  858. dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  859. need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  860. clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  861. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  862. if (dirty)
  863. wait = 1;
  864. spin_unlock_irqrestore(&bitmap->lock, flags);
  865. if (dirty || need_write)
  866. write_page(bitmap, page, 0);
  867. }
  868. if (wait) { /* if any writes were performed, we need to wait on them */
  869. if (bitmap->file)
  870. wait_event(bitmap->write_wait,
  871. atomic_read(&bitmap->pending_writes)==0);
  872. else
  873. md_super_wait(bitmap->mddev);
  874. }
  875. out:
  876. if (bitmap->flags & BITMAP_WRITE_ERROR)
  877. bitmap_file_kick(bitmap);
  878. }
  879. EXPORT_SYMBOL(bitmap_unplug);
  880. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
  881. /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
  882. * the in-memory bitmap from the on-disk bitmap -- also, sets up the
  883. * memory mapping of the bitmap file
  884. * Special cases:
  885. * if there's no bitmap file, or if the bitmap file had been
  886. * previously kicked from the array, we mark all the bits as
  887. * 1's in order to cause a full resync.
  888. *
  889. * We ignore all bits for sectors that end earlier than 'start'.
  890. * This is used when reading an out-of-date bitmap...
  891. */
  892. static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
  893. {
  894. unsigned long i, chunks, index, oldindex, bit;
  895. struct page *page = NULL, *oldpage = NULL;
  896. unsigned long num_pages, bit_cnt = 0;
  897. struct file *file;
  898. unsigned long bytes, offset;
  899. int outofdate;
  900. int ret = -ENOSPC;
  901. void *paddr;
  902. chunks = bitmap->chunks;
  903. file = bitmap->file;
  904. BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
  905. #ifdef INJECT_FAULTS_3
  906. outofdate = 1;
  907. #else
  908. outofdate = bitmap->flags & BITMAP_STALE;
  909. #endif
  910. if (outofdate)
  911. printk(KERN_INFO "%s: bitmap file is out of date, doing full "
  912. "recovery\n", bmname(bitmap));
  913. bytes = DIV_ROUND_UP(bitmap->chunks, 8);
  914. if (!bitmap->mddev->bitmap_info.external)
  915. bytes += sizeof(bitmap_super_t);
  916. num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
  917. if (file && i_size_read(file->f_mapping->host) < bytes) {
  918. printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
  919. bmname(bitmap),
  920. (unsigned long) i_size_read(file->f_mapping->host),
  921. bytes);
  922. goto err;
  923. }
  924. ret = -ENOMEM;
  925. bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  926. if (!bitmap->filemap)
  927. goto err;
  928. /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
  929. bitmap->filemap_attr = kzalloc(
  930. roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
  931. GFP_KERNEL);
  932. if (!bitmap->filemap_attr)
  933. goto err;
  934. oldindex = ~0L;
  935. for (i = 0; i < chunks; i++) {
  936. int b;
  937. index = file_page_index(bitmap, i);
  938. bit = file_page_offset(bitmap, i);
  939. if (index != oldindex) { /* this is a new page, read it in */
  940. int count;
  941. /* unmap the old page, we're done with it */
  942. if (index == num_pages-1)
  943. count = bytes - index * PAGE_SIZE;
  944. else
  945. count = PAGE_SIZE;
  946. if (index == 0 && bitmap->sb_page) {
  947. /*
  948. * if we're here then the superblock page
  949. * contains some bits (PAGE_SIZE != sizeof sb)
  950. * we've already read it in, so just use it
  951. */
  952. page = bitmap->sb_page;
  953. offset = sizeof(bitmap_super_t);
  954. if (!file)
  955. page = read_sb_page(
  956. bitmap->mddev,
  957. bitmap->mddev->bitmap_info.offset,
  958. page,
  959. index, count);
  960. } else if (file) {
  961. page = read_page(file, index, bitmap, count);
  962. offset = 0;
  963. } else {
  964. page = read_sb_page(bitmap->mddev,
  965. bitmap->mddev->bitmap_info.offset,
  966. NULL,
  967. index, count);
  968. offset = 0;
  969. }
  970. if (IS_ERR(page)) { /* read error */
  971. ret = PTR_ERR(page);
  972. goto err;
  973. }
  974. oldindex = index;
  975. oldpage = page;
  976. bitmap->filemap[bitmap->file_pages++] = page;
  977. bitmap->last_page_size = count;
  978. if (outofdate) {
  979. /*
  980. * if bitmap is out of date, dirty the
  981. * whole page and write it out
  982. */
  983. paddr = kmap_atomic(page, KM_USER0);
  984. memset(paddr + offset, 0xff,
  985. PAGE_SIZE - offset);
  986. kunmap_atomic(paddr, KM_USER0);
  987. write_page(bitmap, page, 1);
  988. ret = -EIO;
  989. if (bitmap->flags & BITMAP_WRITE_ERROR)
  990. goto err;
  991. }
  992. }
  993. paddr = kmap_atomic(page, KM_USER0);
  994. if (bitmap->flags & BITMAP_HOSTENDIAN)
  995. b = test_bit(bit, paddr);
  996. else
  997. b = test_bit_le(bit, paddr);
  998. kunmap_atomic(paddr, KM_USER0);
  999. if (b) {
  1000. /* if the disk bit is set, set the memory bit */
  1001. int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
  1002. >= start);
  1003. bitmap_set_memory_bits(bitmap,
  1004. (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
  1005. needed);
  1006. bit_cnt++;
  1007. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1008. }
  1009. }
  1010. /* everything went OK */
  1011. ret = 0;
  1012. bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
  1013. if (bit_cnt) { /* Kick recovery if any bits were set */
  1014. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  1015. md_wakeup_thread(bitmap->mddev->thread);
  1016. }
  1017. printk(KERN_INFO "%s: bitmap initialized from disk: "
  1018. "read %lu/%lu pages, set %lu of %lu bits\n",
  1019. bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, chunks);
  1020. return 0;
  1021. err:
  1022. printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
  1023. bmname(bitmap), ret);
  1024. return ret;
  1025. }
  1026. void bitmap_write_all(struct bitmap *bitmap)
  1027. {
  1028. /* We don't actually write all bitmap blocks here,
  1029. * just flag them as needing to be written
  1030. */
  1031. int i;
  1032. for (i = 0; i < bitmap->file_pages; i++)
  1033. set_page_attr(bitmap, bitmap->filemap[i],
  1034. BITMAP_PAGE_NEEDWRITE);
  1035. }
  1036. static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
  1037. {
  1038. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  1039. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1040. bitmap->bp[page].count += inc;
  1041. bitmap_checkfree(bitmap, page);
  1042. }
  1043. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1044. sector_t offset, sector_t *blocks,
  1045. int create);
  1046. /*
  1047. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  1048. * out to disk
  1049. */
  1050. void bitmap_daemon_work(mddev_t *mddev)
  1051. {
  1052. struct bitmap *bitmap;
  1053. unsigned long j;
  1054. unsigned long flags;
  1055. struct page *page = NULL, *lastpage = NULL;
  1056. sector_t blocks;
  1057. void *paddr;
  1058. struct dm_dirty_log *log = mddev->bitmap_info.log;
  1059. /* Use a mutex to guard daemon_work against
  1060. * bitmap_destroy.
  1061. */
  1062. mutex_lock(&mddev->bitmap_info.mutex);
  1063. bitmap = mddev->bitmap;
  1064. if (bitmap == NULL) {
  1065. mutex_unlock(&mddev->bitmap_info.mutex);
  1066. return;
  1067. }
  1068. if (time_before(jiffies, bitmap->daemon_lastrun
  1069. + bitmap->mddev->bitmap_info.daemon_sleep))
  1070. goto done;
  1071. bitmap->daemon_lastrun = jiffies;
  1072. if (bitmap->allclean) {
  1073. bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1074. goto done;
  1075. }
  1076. bitmap->allclean = 1;
  1077. spin_lock_irqsave(&bitmap->lock, flags);
  1078. for (j = 0; j < bitmap->chunks; j++) {
  1079. bitmap_counter_t *bmc;
  1080. if (!bitmap->filemap) {
  1081. if (!log)
  1082. /* error or shutdown */
  1083. break;
  1084. } else
  1085. page = filemap_get_page(bitmap, j);
  1086. if (page != lastpage) {
  1087. /* skip this page unless it's marked as needing cleaning */
  1088. if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
  1089. int need_write = test_page_attr(bitmap, page,
  1090. BITMAP_PAGE_NEEDWRITE);
  1091. if (need_write)
  1092. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  1093. spin_unlock_irqrestore(&bitmap->lock, flags);
  1094. if (need_write) {
  1095. write_page(bitmap, page, 0);
  1096. bitmap->allclean = 0;
  1097. }
  1098. spin_lock_irqsave(&bitmap->lock, flags);
  1099. j |= (PAGE_BITS - 1);
  1100. continue;
  1101. }
  1102. /* grab the new page, sync and release the old */
  1103. if (lastpage != NULL) {
  1104. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  1105. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1106. spin_unlock_irqrestore(&bitmap->lock, flags);
  1107. write_page(bitmap, lastpage, 0);
  1108. } else {
  1109. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1110. spin_unlock_irqrestore(&bitmap->lock, flags);
  1111. }
  1112. } else
  1113. spin_unlock_irqrestore(&bitmap->lock, flags);
  1114. lastpage = page;
  1115. /* We are possibly going to clear some bits, so make
  1116. * sure that events_cleared is up-to-date.
  1117. */
  1118. if (bitmap->need_sync &&
  1119. bitmap->mddev->bitmap_info.external == 0) {
  1120. bitmap_super_t *sb;
  1121. bitmap->need_sync = 0;
  1122. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  1123. sb->events_cleared =
  1124. cpu_to_le64(bitmap->events_cleared);
  1125. kunmap_atomic(sb, KM_USER0);
  1126. write_page(bitmap, bitmap->sb_page, 1);
  1127. }
  1128. spin_lock_irqsave(&bitmap->lock, flags);
  1129. if (!bitmap->need_sync)
  1130. clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1131. }
  1132. bmc = bitmap_get_counter(bitmap,
  1133. (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
  1134. &blocks, 0);
  1135. if (bmc) {
  1136. if (*bmc)
  1137. bitmap->allclean = 0;
  1138. if (*bmc == 2) {
  1139. *bmc = 1; /* maybe clear the bit next time */
  1140. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1141. } else if (*bmc == 1 && !bitmap->need_sync) {
  1142. /* we can clear the bit */
  1143. *bmc = 0;
  1144. bitmap_count_page(bitmap,
  1145. (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
  1146. -1);
  1147. /* clear the bit */
  1148. if (page) {
  1149. paddr = kmap_atomic(page, KM_USER0);
  1150. if (bitmap->flags & BITMAP_HOSTENDIAN)
  1151. clear_bit(file_page_offset(bitmap, j),
  1152. paddr);
  1153. else
  1154. __test_and_clear_bit_le(file_page_offset(bitmap, j),
  1155. paddr);
  1156. kunmap_atomic(paddr, KM_USER0);
  1157. } else
  1158. log->type->clear_region(log, j);
  1159. }
  1160. } else
  1161. j |= PAGE_COUNTER_MASK;
  1162. }
  1163. spin_unlock_irqrestore(&bitmap->lock, flags);
  1164. /* now sync the final page */
  1165. if (lastpage != NULL || log != NULL) {
  1166. spin_lock_irqsave(&bitmap->lock, flags);
  1167. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  1168. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1169. spin_unlock_irqrestore(&bitmap->lock, flags);
  1170. if (lastpage)
  1171. write_page(bitmap, lastpage, 0);
  1172. else
  1173. if (log->type->flush(log))
  1174. bitmap->flags |= BITMAP_WRITE_ERROR;
  1175. } else {
  1176. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1177. spin_unlock_irqrestore(&bitmap->lock, flags);
  1178. }
  1179. }
  1180. done:
  1181. if (bitmap->allclean == 0)
  1182. bitmap->mddev->thread->timeout =
  1183. bitmap->mddev->bitmap_info.daemon_sleep;
  1184. mutex_unlock(&mddev->bitmap_info.mutex);
  1185. }
  1186. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1187. sector_t offset, sector_t *blocks,
  1188. int create)
  1189. __releases(bitmap->lock)
  1190. __acquires(bitmap->lock)
  1191. {
  1192. /* If 'create', we might release the lock and reclaim it.
  1193. * The lock must have been taken with interrupts enabled.
  1194. * If !create, we don't release the lock.
  1195. */
  1196. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  1197. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1198. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  1199. sector_t csize;
  1200. int err;
  1201. err = bitmap_checkpage(bitmap, page, create);
  1202. if (bitmap->bp[page].hijacked ||
  1203. bitmap->bp[page].map == NULL)
  1204. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
  1205. PAGE_COUNTER_SHIFT - 1);
  1206. else
  1207. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1208. *blocks = csize - (offset & (csize - 1));
  1209. if (err < 0)
  1210. return NULL;
  1211. /* now locked ... */
  1212. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  1213. /* should we use the first or second counter field
  1214. * of the hijacked pointer? */
  1215. int hi = (pageoff > PAGE_COUNTER_MASK);
  1216. return &((bitmap_counter_t *)
  1217. &bitmap->bp[page].map)[hi];
  1218. } else /* page is allocated */
  1219. return (bitmap_counter_t *)
  1220. &(bitmap->bp[page].map[pageoff]);
  1221. }
  1222. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
  1223. {
  1224. if (!bitmap)
  1225. return 0;
  1226. if (behind) {
  1227. int bw;
  1228. atomic_inc(&bitmap->behind_writes);
  1229. bw = atomic_read(&bitmap->behind_writes);
  1230. if (bw > bitmap->behind_writes_used)
  1231. bitmap->behind_writes_used = bw;
  1232. PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
  1233. bw, bitmap->max_write_behind);
  1234. }
  1235. while (sectors) {
  1236. sector_t blocks;
  1237. bitmap_counter_t *bmc;
  1238. spin_lock_irq(&bitmap->lock);
  1239. bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
  1240. if (!bmc) {
  1241. spin_unlock_irq(&bitmap->lock);
  1242. return 0;
  1243. }
  1244. if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
  1245. DEFINE_WAIT(__wait);
  1246. /* note that it is safe to do the prepare_to_wait
  1247. * after the test as long as we do it before dropping
  1248. * the spinlock.
  1249. */
  1250. prepare_to_wait(&bitmap->overflow_wait, &__wait,
  1251. TASK_UNINTERRUPTIBLE);
  1252. spin_unlock_irq(&bitmap->lock);
  1253. io_schedule();
  1254. finish_wait(&bitmap->overflow_wait, &__wait);
  1255. continue;
  1256. }
  1257. switch (*bmc) {
  1258. case 0:
  1259. bitmap_file_set_bit(bitmap, offset);
  1260. bitmap_count_page(bitmap, offset, 1);
  1261. /* fall through */
  1262. case 1:
  1263. *bmc = 2;
  1264. }
  1265. (*bmc)++;
  1266. spin_unlock_irq(&bitmap->lock);
  1267. offset += blocks;
  1268. if (sectors > blocks)
  1269. sectors -= blocks;
  1270. else
  1271. sectors = 0;
  1272. }
  1273. bitmap->allclean = 0;
  1274. return 0;
  1275. }
  1276. EXPORT_SYMBOL(bitmap_startwrite);
  1277. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1278. int success, int behind)
  1279. {
  1280. if (!bitmap)
  1281. return;
  1282. if (behind) {
  1283. if (atomic_dec_and_test(&bitmap->behind_writes))
  1284. wake_up(&bitmap->behind_wait);
  1285. PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
  1286. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1287. }
  1288. if (bitmap->mddev->degraded)
  1289. /* Never clear bits or update events_cleared when degraded */
  1290. success = 0;
  1291. while (sectors) {
  1292. sector_t blocks;
  1293. unsigned long flags;
  1294. bitmap_counter_t *bmc;
  1295. spin_lock_irqsave(&bitmap->lock, flags);
  1296. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1297. if (!bmc) {
  1298. spin_unlock_irqrestore(&bitmap->lock, flags);
  1299. return;
  1300. }
  1301. if (success &&
  1302. bitmap->events_cleared < bitmap->mddev->events) {
  1303. bitmap->events_cleared = bitmap->mddev->events;
  1304. bitmap->need_sync = 1;
  1305. sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
  1306. }
  1307. if (!success && !NEEDED(*bmc))
  1308. *bmc |= NEEDED_MASK;
  1309. if (COUNTER(*bmc) == COUNTER_MAX)
  1310. wake_up(&bitmap->overflow_wait);
  1311. (*bmc)--;
  1312. if (*bmc <= 2)
  1313. set_page_attr(bitmap,
  1314. filemap_get_page(
  1315. bitmap,
  1316. offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1317. BITMAP_PAGE_CLEAN);
  1318. spin_unlock_irqrestore(&bitmap->lock, flags);
  1319. offset += blocks;
  1320. if (sectors > blocks)
  1321. sectors -= blocks;
  1322. else
  1323. sectors = 0;
  1324. }
  1325. }
  1326. EXPORT_SYMBOL(bitmap_endwrite);
  1327. static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1328. int degraded)
  1329. {
  1330. bitmap_counter_t *bmc;
  1331. int rv;
  1332. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1333. *blocks = 1024;
  1334. return 1; /* always resync if no bitmap */
  1335. }
  1336. spin_lock_irq(&bitmap->lock);
  1337. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1338. rv = 0;
  1339. if (bmc) {
  1340. /* locked */
  1341. if (RESYNC(*bmc))
  1342. rv = 1;
  1343. else if (NEEDED(*bmc)) {
  1344. rv = 1;
  1345. if (!degraded) { /* don't set/clear bits if degraded */
  1346. *bmc |= RESYNC_MASK;
  1347. *bmc &= ~NEEDED_MASK;
  1348. }
  1349. }
  1350. }
  1351. spin_unlock_irq(&bitmap->lock);
  1352. bitmap->allclean = 0;
  1353. return rv;
  1354. }
  1355. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1356. int degraded)
  1357. {
  1358. /* bitmap_start_sync must always report on multiples of whole
  1359. * pages, otherwise resync (which is very PAGE_SIZE based) will
  1360. * get confused.
  1361. * So call __bitmap_start_sync repeatedly (if needed) until
  1362. * At least PAGE_SIZE>>9 blocks are covered.
  1363. * Return the 'or' of the result.
  1364. */
  1365. int rv = 0;
  1366. sector_t blocks1;
  1367. *blocks = 0;
  1368. while (*blocks < (PAGE_SIZE>>9)) {
  1369. rv |= __bitmap_start_sync(bitmap, offset,
  1370. &blocks1, degraded);
  1371. offset += blocks1;
  1372. *blocks += blocks1;
  1373. }
  1374. return rv;
  1375. }
  1376. EXPORT_SYMBOL(bitmap_start_sync);
  1377. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
  1378. {
  1379. bitmap_counter_t *bmc;
  1380. unsigned long flags;
  1381. if (bitmap == NULL) {
  1382. *blocks = 1024;
  1383. return;
  1384. }
  1385. spin_lock_irqsave(&bitmap->lock, flags);
  1386. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1387. if (bmc == NULL)
  1388. goto unlock;
  1389. /* locked */
  1390. if (RESYNC(*bmc)) {
  1391. *bmc &= ~RESYNC_MASK;
  1392. if (!NEEDED(*bmc) && aborted)
  1393. *bmc |= NEEDED_MASK;
  1394. else {
  1395. if (*bmc <= 2)
  1396. set_page_attr(bitmap,
  1397. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1398. BITMAP_PAGE_CLEAN);
  1399. }
  1400. }
  1401. unlock:
  1402. spin_unlock_irqrestore(&bitmap->lock, flags);
  1403. bitmap->allclean = 0;
  1404. }
  1405. EXPORT_SYMBOL(bitmap_end_sync);
  1406. void bitmap_close_sync(struct bitmap *bitmap)
  1407. {
  1408. /* Sync has finished, and any bitmap chunks that weren't synced
  1409. * properly have been aborted. It remains to us to clear the
  1410. * RESYNC bit wherever it is still on
  1411. */
  1412. sector_t sector = 0;
  1413. sector_t blocks;
  1414. if (!bitmap)
  1415. return;
  1416. while (sector < bitmap->mddev->resync_max_sectors) {
  1417. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1418. sector += blocks;
  1419. }
  1420. }
  1421. EXPORT_SYMBOL(bitmap_close_sync);
  1422. void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
  1423. {
  1424. sector_t s = 0;
  1425. sector_t blocks;
  1426. if (!bitmap)
  1427. return;
  1428. if (sector == 0) {
  1429. bitmap->last_end_sync = jiffies;
  1430. return;
  1431. }
  1432. if (time_before(jiffies, (bitmap->last_end_sync
  1433. + bitmap->mddev->bitmap_info.daemon_sleep)))
  1434. return;
  1435. wait_event(bitmap->mddev->recovery_wait,
  1436. atomic_read(&bitmap->mddev->recovery_active) == 0);
  1437. bitmap->mddev->curr_resync_completed = sector;
  1438. set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
  1439. sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
  1440. s = 0;
  1441. while (s < sector && s < bitmap->mddev->resync_max_sectors) {
  1442. bitmap_end_sync(bitmap, s, &blocks, 0);
  1443. s += blocks;
  1444. }
  1445. bitmap->last_end_sync = jiffies;
  1446. sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
  1447. }
  1448. EXPORT_SYMBOL(bitmap_cond_end_sync);
  1449. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1450. {
  1451. /* For each chunk covered by any of these sectors, set the
  1452. * counter to 1 and set resync_needed. They should all
  1453. * be 0 at this point
  1454. */
  1455. sector_t secs;
  1456. bitmap_counter_t *bmc;
  1457. spin_lock_irq(&bitmap->lock);
  1458. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1459. if (!bmc) {
  1460. spin_unlock_irq(&bitmap->lock);
  1461. return;
  1462. }
  1463. if (!*bmc) {
  1464. struct page *page;
  1465. *bmc = 1 | (needed ? NEEDED_MASK : 0);
  1466. bitmap_count_page(bitmap, offset, 1);
  1467. page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
  1468. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1469. }
  1470. spin_unlock_irq(&bitmap->lock);
  1471. bitmap->allclean = 0;
  1472. }
  1473. /* dirty the memory and file bits for bitmap chunks "s" to "e" */
  1474. void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
  1475. {
  1476. unsigned long chunk;
  1477. for (chunk = s; chunk <= e; chunk++) {
  1478. sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
  1479. bitmap_set_memory_bits(bitmap, sec, 1);
  1480. bitmap_file_set_bit(bitmap, sec);
  1481. if (sec < bitmap->mddev->recovery_cp)
  1482. /* We are asserting that the array is dirty,
  1483. * so move the recovery_cp address back so
  1484. * that it is obvious that it is dirty
  1485. */
  1486. bitmap->mddev->recovery_cp = sec;
  1487. }
  1488. }
  1489. /*
  1490. * flush out any pending updates
  1491. */
  1492. void bitmap_flush(mddev_t *mddev)
  1493. {
  1494. struct bitmap *bitmap = mddev->bitmap;
  1495. long sleep;
  1496. if (!bitmap) /* there was no bitmap */
  1497. return;
  1498. /* run the daemon_work three time to ensure everything is flushed
  1499. * that can be
  1500. */
  1501. sleep = mddev->bitmap_info.daemon_sleep * 2;
  1502. bitmap->daemon_lastrun -= sleep;
  1503. bitmap_daemon_work(mddev);
  1504. bitmap->daemon_lastrun -= sleep;
  1505. bitmap_daemon_work(mddev);
  1506. bitmap->daemon_lastrun -= sleep;
  1507. bitmap_daemon_work(mddev);
  1508. bitmap_update_sb(bitmap);
  1509. }
  1510. /*
  1511. * free memory that was allocated
  1512. */
  1513. static void bitmap_free(struct bitmap *bitmap)
  1514. {
  1515. unsigned long k, pages;
  1516. struct bitmap_page *bp;
  1517. if (!bitmap) /* there was no bitmap */
  1518. return;
  1519. /* release the bitmap file and kill the daemon */
  1520. bitmap_file_put(bitmap);
  1521. bp = bitmap->bp;
  1522. pages = bitmap->pages;
  1523. /* free all allocated memory */
  1524. if (bp) /* deallocate the page memory */
  1525. for (k = 0; k < pages; k++)
  1526. if (bp[k].map && !bp[k].hijacked)
  1527. kfree(bp[k].map);
  1528. kfree(bp);
  1529. kfree(bitmap);
  1530. }
  1531. void bitmap_destroy(mddev_t *mddev)
  1532. {
  1533. struct bitmap *bitmap = mddev->bitmap;
  1534. if (!bitmap) /* there was no bitmap */
  1535. return;
  1536. mutex_lock(&mddev->bitmap_info.mutex);
  1537. mddev->bitmap = NULL; /* disconnect from the md device */
  1538. mutex_unlock(&mddev->bitmap_info.mutex);
  1539. if (mddev->thread)
  1540. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1541. if (bitmap->sysfs_can_clear)
  1542. sysfs_put(bitmap->sysfs_can_clear);
  1543. bitmap_free(bitmap);
  1544. }
  1545. /*
  1546. * initialize the bitmap structure
  1547. * if this returns an error, bitmap_destroy must be called to do clean up
  1548. */
  1549. int bitmap_create(mddev_t *mddev)
  1550. {
  1551. struct bitmap *bitmap;
  1552. sector_t blocks = mddev->resync_max_sectors;
  1553. unsigned long chunks;
  1554. unsigned long pages;
  1555. struct file *file = mddev->bitmap_info.file;
  1556. int err;
  1557. struct sysfs_dirent *bm = NULL;
  1558. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1559. if (!file
  1560. && !mddev->bitmap_info.offset
  1561. && !mddev->bitmap_info.log) /* bitmap disabled, nothing to do */
  1562. return 0;
  1563. BUG_ON(file && mddev->bitmap_info.offset);
  1564. BUG_ON(mddev->bitmap_info.offset && mddev->bitmap_info.log);
  1565. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1566. if (!bitmap)
  1567. return -ENOMEM;
  1568. spin_lock_init(&bitmap->lock);
  1569. atomic_set(&bitmap->pending_writes, 0);
  1570. init_waitqueue_head(&bitmap->write_wait);
  1571. init_waitqueue_head(&bitmap->overflow_wait);
  1572. init_waitqueue_head(&bitmap->behind_wait);
  1573. bitmap->mddev = mddev;
  1574. if (mddev->kobj.sd)
  1575. bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
  1576. if (bm) {
  1577. bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
  1578. sysfs_put(bm);
  1579. } else
  1580. bitmap->sysfs_can_clear = NULL;
  1581. bitmap->file = file;
  1582. if (file) {
  1583. get_file(file);
  1584. /* As future accesses to this file will use bmap,
  1585. * and bypass the page cache, we must sync the file
  1586. * first.
  1587. */
  1588. vfs_fsync(file, 1);
  1589. }
  1590. /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
  1591. if (!mddev->bitmap_info.external) {
  1592. /*
  1593. * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
  1594. * instructing us to create a new on-disk bitmap instance.
  1595. */
  1596. if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
  1597. err = bitmap_new_disk_sb(bitmap);
  1598. else
  1599. err = bitmap_read_sb(bitmap);
  1600. } else {
  1601. err = 0;
  1602. if (mddev->bitmap_info.chunksize == 0 ||
  1603. mddev->bitmap_info.daemon_sleep == 0)
  1604. /* chunksize and time_base need to be
  1605. * set first. */
  1606. err = -EINVAL;
  1607. }
  1608. if (err)
  1609. goto error;
  1610. bitmap->daemon_lastrun = jiffies;
  1611. bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
  1612. /* now that chunksize and chunkshift are set, we can use these macros */
  1613. chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
  1614. CHUNK_BLOCK_SHIFT(bitmap);
  1615. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1616. BUG_ON(!pages);
  1617. bitmap->chunks = chunks;
  1618. bitmap->pages = pages;
  1619. bitmap->missing_pages = pages;
  1620. #ifdef INJECT_FATAL_FAULT_1
  1621. bitmap->bp = NULL;
  1622. #else
  1623. bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1624. #endif
  1625. err = -ENOMEM;
  1626. if (!bitmap->bp)
  1627. goto error;
  1628. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1629. pages, bmname(bitmap));
  1630. mddev->bitmap = bitmap;
  1631. return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
  1632. error:
  1633. bitmap_free(bitmap);
  1634. return err;
  1635. }
  1636. int bitmap_load(mddev_t *mddev)
  1637. {
  1638. int err = 0;
  1639. sector_t sector = 0;
  1640. struct bitmap *bitmap = mddev->bitmap;
  1641. if (!bitmap)
  1642. goto out;
  1643. /* Clear out old bitmap info first: Either there is none, or we
  1644. * are resuming after someone else has possibly changed things,
  1645. * so we should forget old cached info.
  1646. * All chunks should be clean, but some might need_sync.
  1647. */
  1648. while (sector < mddev->resync_max_sectors) {
  1649. sector_t blocks;
  1650. bitmap_start_sync(bitmap, sector, &blocks, 0);
  1651. sector += blocks;
  1652. }
  1653. bitmap_close_sync(bitmap);
  1654. if (mddev->bitmap_info.log) {
  1655. unsigned long i;
  1656. struct dm_dirty_log *log = mddev->bitmap_info.log;
  1657. for (i = 0; i < bitmap->chunks; i++)
  1658. if (!log->type->in_sync(log, i, 1))
  1659. bitmap_set_memory_bits(bitmap,
  1660. (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
  1661. 1);
  1662. } else {
  1663. sector_t start = 0;
  1664. if (mddev->degraded == 0
  1665. || bitmap->events_cleared == mddev->events)
  1666. /* no need to keep dirty bits to optimise a
  1667. * re-add of a missing device */
  1668. start = mddev->recovery_cp;
  1669. mutex_lock(&mddev->bitmap_info.mutex);
  1670. err = bitmap_init_from_disk(bitmap, start);
  1671. mutex_unlock(&mddev->bitmap_info.mutex);
  1672. }
  1673. if (err)
  1674. goto out;
  1675. mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
  1676. md_wakeup_thread(mddev->thread);
  1677. bitmap_update_sb(bitmap);
  1678. if (bitmap->flags & BITMAP_WRITE_ERROR)
  1679. err = -EIO;
  1680. out:
  1681. return err;
  1682. }
  1683. EXPORT_SYMBOL_GPL(bitmap_load);
  1684. static ssize_t
  1685. location_show(mddev_t *mddev, char *page)
  1686. {
  1687. ssize_t len;
  1688. if (mddev->bitmap_info.file)
  1689. len = sprintf(page, "file");
  1690. else if (mddev->bitmap_info.offset)
  1691. len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
  1692. else
  1693. len = sprintf(page, "none");
  1694. len += sprintf(page+len, "\n");
  1695. return len;
  1696. }
  1697. static ssize_t
  1698. location_store(mddev_t *mddev, const char *buf, size_t len)
  1699. {
  1700. if (mddev->pers) {
  1701. if (!mddev->pers->quiesce)
  1702. return -EBUSY;
  1703. if (mddev->recovery || mddev->sync_thread)
  1704. return -EBUSY;
  1705. }
  1706. if (mddev->bitmap || mddev->bitmap_info.file ||
  1707. mddev->bitmap_info.offset) {
  1708. /* bitmap already configured. Only option is to clear it */
  1709. if (strncmp(buf, "none", 4) != 0)
  1710. return -EBUSY;
  1711. if (mddev->pers) {
  1712. mddev->pers->quiesce(mddev, 1);
  1713. bitmap_destroy(mddev);
  1714. mddev->pers->quiesce(mddev, 0);
  1715. }
  1716. mddev->bitmap_info.offset = 0;
  1717. if (mddev->bitmap_info.file) {
  1718. struct file *f = mddev->bitmap_info.file;
  1719. mddev->bitmap_info.file = NULL;
  1720. restore_bitmap_write_access(f);
  1721. fput(f);
  1722. }
  1723. } else {
  1724. /* No bitmap, OK to set a location */
  1725. long long offset;
  1726. if (strncmp(buf, "none", 4) == 0)
  1727. /* nothing to be done */;
  1728. else if (strncmp(buf, "file:", 5) == 0) {
  1729. /* Not supported yet */
  1730. return -EINVAL;
  1731. } else {
  1732. int rv;
  1733. if (buf[0] == '+')
  1734. rv = strict_strtoll(buf+1, 10, &offset);
  1735. else
  1736. rv = strict_strtoll(buf, 10, &offset);
  1737. if (rv)
  1738. return rv;
  1739. if (offset == 0)
  1740. return -EINVAL;
  1741. if (mddev->bitmap_info.external == 0 &&
  1742. mddev->major_version == 0 &&
  1743. offset != mddev->bitmap_info.default_offset)
  1744. return -EINVAL;
  1745. mddev->bitmap_info.offset = offset;
  1746. if (mddev->pers) {
  1747. mddev->pers->quiesce(mddev, 1);
  1748. rv = bitmap_create(mddev);
  1749. if (!rv)
  1750. rv = bitmap_load(mddev);
  1751. if (rv) {
  1752. bitmap_destroy(mddev);
  1753. mddev->bitmap_info.offset = 0;
  1754. }
  1755. mddev->pers->quiesce(mddev, 0);
  1756. if (rv)
  1757. return rv;
  1758. }
  1759. }
  1760. }
  1761. if (!mddev->external) {
  1762. /* Ensure new bitmap info is stored in
  1763. * metadata promptly.
  1764. */
  1765. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1766. md_wakeup_thread(mddev->thread);
  1767. }
  1768. return len;
  1769. }
  1770. static struct md_sysfs_entry bitmap_location =
  1771. __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
  1772. static ssize_t
  1773. timeout_show(mddev_t *mddev, char *page)
  1774. {
  1775. ssize_t len;
  1776. unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
  1777. unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
  1778. len = sprintf(page, "%lu", secs);
  1779. if (jifs)
  1780. len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
  1781. len += sprintf(page+len, "\n");
  1782. return len;
  1783. }
  1784. static ssize_t
  1785. timeout_store(mddev_t *mddev, const char *buf, size_t len)
  1786. {
  1787. /* timeout can be set at any time */
  1788. unsigned long timeout;
  1789. int rv = strict_strtoul_scaled(buf, &timeout, 4);
  1790. if (rv)
  1791. return rv;
  1792. /* just to make sure we don't overflow... */
  1793. if (timeout >= LONG_MAX / HZ)
  1794. return -EINVAL;
  1795. timeout = timeout * HZ / 10000;
  1796. if (timeout >= MAX_SCHEDULE_TIMEOUT)
  1797. timeout = MAX_SCHEDULE_TIMEOUT-1;
  1798. if (timeout < 1)
  1799. timeout = 1;
  1800. mddev->bitmap_info.daemon_sleep = timeout;
  1801. if (mddev->thread) {
  1802. /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
  1803. * the bitmap is all clean and we don't need to
  1804. * adjust the timeout right now
  1805. */
  1806. if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
  1807. mddev->thread->timeout = timeout;
  1808. md_wakeup_thread(mddev->thread);
  1809. }
  1810. }
  1811. return len;
  1812. }
  1813. static struct md_sysfs_entry bitmap_timeout =
  1814. __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
  1815. static ssize_t
  1816. backlog_show(mddev_t *mddev, char *page)
  1817. {
  1818. return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
  1819. }
  1820. static ssize_t
  1821. backlog_store(mddev_t *mddev, const char *buf, size_t len)
  1822. {
  1823. unsigned long backlog;
  1824. int rv = strict_strtoul(buf, 10, &backlog);
  1825. if (rv)
  1826. return rv;
  1827. if (backlog > COUNTER_MAX)
  1828. return -EINVAL;
  1829. mddev->bitmap_info.max_write_behind = backlog;
  1830. return len;
  1831. }
  1832. static struct md_sysfs_entry bitmap_backlog =
  1833. __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
  1834. static ssize_t
  1835. chunksize_show(mddev_t *mddev, char *page)
  1836. {
  1837. return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
  1838. }
  1839. static ssize_t
  1840. chunksize_store(mddev_t *mddev, const char *buf, size_t len)
  1841. {
  1842. /* Can only be changed when no bitmap is active */
  1843. int rv;
  1844. unsigned long csize;
  1845. if (mddev->bitmap)
  1846. return -EBUSY;
  1847. rv = strict_strtoul(buf, 10, &csize);
  1848. if (rv)
  1849. return rv;
  1850. if (csize < 512 ||
  1851. !is_power_of_2(csize))
  1852. return -EINVAL;
  1853. mddev->bitmap_info.chunksize = csize;
  1854. return len;
  1855. }
  1856. static struct md_sysfs_entry bitmap_chunksize =
  1857. __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
  1858. static ssize_t metadata_show(mddev_t *mddev, char *page)
  1859. {
  1860. return sprintf(page, "%s\n", (mddev->bitmap_info.external
  1861. ? "external" : "internal"));
  1862. }
  1863. static ssize_t metadata_store(mddev_t *mddev, const char *buf, size_t len)
  1864. {
  1865. if (mddev->bitmap ||
  1866. mddev->bitmap_info.file ||
  1867. mddev->bitmap_info.offset)
  1868. return -EBUSY;
  1869. if (strncmp(buf, "external", 8) == 0)
  1870. mddev->bitmap_info.external = 1;
  1871. else if (strncmp(buf, "internal", 8) == 0)
  1872. mddev->bitmap_info.external = 0;
  1873. else
  1874. return -EINVAL;
  1875. return len;
  1876. }
  1877. static struct md_sysfs_entry bitmap_metadata =
  1878. __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
  1879. static ssize_t can_clear_show(mddev_t *mddev, char *page)
  1880. {
  1881. int len;
  1882. if (mddev->bitmap)
  1883. len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
  1884. "false" : "true"));
  1885. else
  1886. len = sprintf(page, "\n");
  1887. return len;
  1888. }
  1889. static ssize_t can_clear_store(mddev_t *mddev, const char *buf, size_t len)
  1890. {
  1891. if (mddev->bitmap == NULL)
  1892. return -ENOENT;
  1893. if (strncmp(buf, "false", 5) == 0)
  1894. mddev->bitmap->need_sync = 1;
  1895. else if (strncmp(buf, "true", 4) == 0) {
  1896. if (mddev->degraded)
  1897. return -EBUSY;
  1898. mddev->bitmap->need_sync = 0;
  1899. } else
  1900. return -EINVAL;
  1901. return len;
  1902. }
  1903. static struct md_sysfs_entry bitmap_can_clear =
  1904. __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
  1905. static ssize_t
  1906. behind_writes_used_show(mddev_t *mddev, char *page)
  1907. {
  1908. if (mddev->bitmap == NULL)
  1909. return sprintf(page, "0\n");
  1910. return sprintf(page, "%lu\n",
  1911. mddev->bitmap->behind_writes_used);
  1912. }
  1913. static ssize_t
  1914. behind_writes_used_reset(mddev_t *mddev, const char *buf, size_t len)
  1915. {
  1916. if (mddev->bitmap)
  1917. mddev->bitmap->behind_writes_used = 0;
  1918. return len;
  1919. }
  1920. static struct md_sysfs_entry max_backlog_used =
  1921. __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
  1922. behind_writes_used_show, behind_writes_used_reset);
  1923. static struct attribute *md_bitmap_attrs[] = {
  1924. &bitmap_location.attr,
  1925. &bitmap_timeout.attr,
  1926. &bitmap_backlog.attr,
  1927. &bitmap_chunksize.attr,
  1928. &bitmap_metadata.attr,
  1929. &bitmap_can_clear.attr,
  1930. &max_backlog_used.attr,
  1931. NULL
  1932. };
  1933. struct attribute_group md_bitmap_group = {
  1934. .name = "bitmap",
  1935. .attrs = md_bitmap_attrs,
  1936. };