bitmap.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113
  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. }
  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. /* read the superblock from the bitmap file and initialize some bitmap fields */
  475. static int bitmap_read_sb(struct bitmap *bitmap)
  476. {
  477. char *reason = NULL;
  478. bitmap_super_t *sb;
  479. unsigned long chunksize, daemon_sleep, write_behind;
  480. unsigned long long events;
  481. int err = -EINVAL;
  482. /* page 0 is the superblock, read it... */
  483. if (bitmap->file) {
  484. loff_t isize = i_size_read(bitmap->file->f_mapping->host);
  485. int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
  486. bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
  487. } else {
  488. bitmap->sb_page = read_sb_page(bitmap->mddev,
  489. bitmap->mddev->bitmap_info.offset,
  490. NULL,
  491. 0, sizeof(bitmap_super_t));
  492. }
  493. if (IS_ERR(bitmap->sb_page)) {
  494. err = PTR_ERR(bitmap->sb_page);
  495. bitmap->sb_page = NULL;
  496. return err;
  497. }
  498. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  499. chunksize = le32_to_cpu(sb->chunksize);
  500. daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
  501. write_behind = le32_to_cpu(sb->write_behind);
  502. /* verify that the bitmap-specific fields are valid */
  503. if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
  504. reason = "bad magic";
  505. else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
  506. le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
  507. reason = "unrecognized superblock version";
  508. else if (chunksize < 512)
  509. reason = "bitmap chunksize too small";
  510. else if ((1 << ffz(~chunksize)) != chunksize)
  511. reason = "bitmap chunksize not a power of 2";
  512. else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
  513. reason = "daemon sleep period out of range";
  514. else if (write_behind > COUNTER_MAX)
  515. reason = "write-behind limit out of range (0 - 16383)";
  516. if (reason) {
  517. printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
  518. bmname(bitmap), reason);
  519. goto out;
  520. }
  521. /* keep the array size field of the bitmap superblock up to date */
  522. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  523. if (!bitmap->mddev->persistent)
  524. goto success;
  525. /*
  526. * if we have a persistent array superblock, compare the
  527. * bitmap's UUID and event counter to the mddev's
  528. */
  529. if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
  530. printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
  531. bmname(bitmap));
  532. goto out;
  533. }
  534. events = le64_to_cpu(sb->events);
  535. if (events < bitmap->mddev->events) {
  536. printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
  537. "-- forcing full recovery\n", bmname(bitmap), events,
  538. (unsigned long long) bitmap->mddev->events);
  539. sb->state |= cpu_to_le32(BITMAP_STALE);
  540. }
  541. success:
  542. /* assign fields using values from superblock */
  543. bitmap->mddev->bitmap_info.chunksize = chunksize;
  544. bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
  545. bitmap->mddev->bitmap_info.max_write_behind = write_behind;
  546. bitmap->flags |= le32_to_cpu(sb->state);
  547. if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
  548. bitmap->flags |= BITMAP_HOSTENDIAN;
  549. bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
  550. if (sb->state & cpu_to_le32(BITMAP_STALE))
  551. bitmap->events_cleared = bitmap->mddev->events;
  552. err = 0;
  553. out:
  554. kunmap_atomic(sb, KM_USER0);
  555. if (err)
  556. bitmap_print_sb(bitmap);
  557. return err;
  558. }
  559. enum bitmap_mask_op {
  560. MASK_SET,
  561. MASK_UNSET
  562. };
  563. /* record the state of the bitmap in the superblock. Return the old value */
  564. static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
  565. enum bitmap_mask_op op)
  566. {
  567. bitmap_super_t *sb;
  568. unsigned long flags;
  569. int old;
  570. spin_lock_irqsave(&bitmap->lock, flags);
  571. if (!bitmap->sb_page) { /* can't set the state */
  572. spin_unlock_irqrestore(&bitmap->lock, flags);
  573. return 0;
  574. }
  575. spin_unlock_irqrestore(&bitmap->lock, flags);
  576. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  577. old = le32_to_cpu(sb->state) & bits;
  578. switch (op) {
  579. case MASK_SET:
  580. sb->state |= cpu_to_le32(bits);
  581. break;
  582. case MASK_UNSET:
  583. sb->state &= cpu_to_le32(~bits);
  584. break;
  585. default:
  586. BUG();
  587. }
  588. kunmap_atomic(sb, KM_USER0);
  589. return old;
  590. }
  591. /*
  592. * general bitmap file operations
  593. */
  594. /*
  595. * on-disk bitmap:
  596. *
  597. * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
  598. * file a page at a time. There's a superblock at the start of the file.
  599. */
  600. /* calculate the index of the page that contains this bit */
  601. static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
  602. {
  603. if (!bitmap->mddev->bitmap_info.external)
  604. chunk += sizeof(bitmap_super_t) << 3;
  605. return chunk >> PAGE_BIT_SHIFT;
  606. }
  607. /* calculate the (bit) offset of this bit within a page */
  608. static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
  609. {
  610. if (!bitmap->mddev->bitmap_info.external)
  611. chunk += sizeof(bitmap_super_t) << 3;
  612. return chunk & (PAGE_BITS - 1);
  613. }
  614. /*
  615. * return a pointer to the page in the filemap that contains the given bit
  616. *
  617. * this lookup is complicated by the fact that the bitmap sb might be exactly
  618. * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
  619. * 0 or page 1
  620. */
  621. static inline struct page *filemap_get_page(struct bitmap *bitmap,
  622. unsigned long chunk)
  623. {
  624. if (bitmap->filemap == NULL)
  625. return NULL;
  626. if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
  627. return NULL;
  628. return bitmap->filemap[file_page_index(bitmap, chunk)
  629. - file_page_index(bitmap, 0)];
  630. }
  631. static void bitmap_file_unmap(struct bitmap *bitmap)
  632. {
  633. struct page **map, *sb_page;
  634. unsigned long *attr;
  635. int pages;
  636. unsigned long flags;
  637. spin_lock_irqsave(&bitmap->lock, flags);
  638. map = bitmap->filemap;
  639. bitmap->filemap = NULL;
  640. attr = bitmap->filemap_attr;
  641. bitmap->filemap_attr = NULL;
  642. pages = bitmap->file_pages;
  643. bitmap->file_pages = 0;
  644. sb_page = bitmap->sb_page;
  645. bitmap->sb_page = NULL;
  646. spin_unlock_irqrestore(&bitmap->lock, flags);
  647. while (pages--)
  648. if (map[pages] != sb_page) /* 0 is sb_page, release it below */
  649. free_buffers(map[pages]);
  650. kfree(map);
  651. kfree(attr);
  652. if (sb_page)
  653. free_buffers(sb_page);
  654. }
  655. static void bitmap_file_put(struct bitmap *bitmap)
  656. {
  657. struct file *file;
  658. unsigned long flags;
  659. spin_lock_irqsave(&bitmap->lock, flags);
  660. file = bitmap->file;
  661. bitmap->file = NULL;
  662. spin_unlock_irqrestore(&bitmap->lock, flags);
  663. if (file)
  664. wait_event(bitmap->write_wait,
  665. atomic_read(&bitmap->pending_writes)==0);
  666. bitmap_file_unmap(bitmap);
  667. if (file) {
  668. struct inode *inode = file->f_path.dentry->d_inode;
  669. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  670. fput(file);
  671. }
  672. }
  673. /*
  674. * bitmap_file_kick - if an error occurs while manipulating the bitmap file
  675. * then it is no longer reliable, so we stop using it and we mark the file
  676. * as failed in the superblock
  677. */
  678. static void bitmap_file_kick(struct bitmap *bitmap)
  679. {
  680. char *path, *ptr = NULL;
  681. if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
  682. bitmap_update_sb(bitmap);
  683. if (bitmap->file) {
  684. path = kmalloc(PAGE_SIZE, GFP_KERNEL);
  685. if (path)
  686. ptr = d_path(&bitmap->file->f_path, path,
  687. PAGE_SIZE);
  688. printk(KERN_ALERT
  689. "%s: kicking failed bitmap file %s from array!\n",
  690. bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
  691. kfree(path);
  692. } else
  693. printk(KERN_ALERT
  694. "%s: disabling internal bitmap due to errors\n",
  695. bmname(bitmap));
  696. }
  697. bitmap_file_put(bitmap);
  698. return;
  699. }
  700. enum bitmap_page_attr {
  701. BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
  702. BITMAP_PAGE_CLEAN = 1, /* there are bits that might need to be cleared */
  703. BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
  704. };
  705. static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
  706. enum bitmap_page_attr attr)
  707. {
  708. if (page)
  709. __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
  710. else
  711. __set_bit(attr, &bitmap->logattrs);
  712. }
  713. static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
  714. enum bitmap_page_attr attr)
  715. {
  716. if (page)
  717. __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
  718. else
  719. __clear_bit(attr, &bitmap->logattrs);
  720. }
  721. static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
  722. enum bitmap_page_attr attr)
  723. {
  724. if (page)
  725. return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
  726. else
  727. return test_bit(attr, &bitmap->logattrs);
  728. }
  729. /*
  730. * bitmap_file_set_bit -- called before performing a write to the md device
  731. * to set (and eventually sync) a particular bit in the bitmap file
  732. *
  733. * we set the bit immediately, then we record the page number so that
  734. * when an unplug occurs, we can flush the dirty pages out to disk
  735. */
  736. static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
  737. {
  738. unsigned long bit;
  739. struct page *page = NULL;
  740. void *kaddr;
  741. unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
  742. if (!bitmap->filemap) {
  743. struct dm_dirty_log *log = bitmap->mddev->bitmap_info.log;
  744. if (log)
  745. log->type->mark_region(log, chunk);
  746. } else {
  747. page = filemap_get_page(bitmap, chunk);
  748. if (!page)
  749. return;
  750. bit = file_page_offset(bitmap, chunk);
  751. /* set the bit */
  752. kaddr = kmap_atomic(page, KM_USER0);
  753. if (bitmap->flags & BITMAP_HOSTENDIAN)
  754. set_bit(bit, kaddr);
  755. else
  756. __test_and_set_bit_le(bit, kaddr);
  757. kunmap_atomic(kaddr, KM_USER0);
  758. PRINTK("set file bit %lu page %lu\n", bit, page->index);
  759. }
  760. /* record page number so it gets flushed to disk when unplug occurs */
  761. set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  762. }
  763. /* this gets called when the md device is ready to unplug its underlying
  764. * (slave) device queues -- before we let any writes go down, we need to
  765. * sync the dirty pages of the bitmap file to disk */
  766. void bitmap_unplug(struct bitmap *bitmap)
  767. {
  768. unsigned long i, flags;
  769. int dirty, need_write;
  770. struct page *page;
  771. int wait = 0;
  772. if (!bitmap)
  773. return;
  774. if (!bitmap->filemap) {
  775. /* Must be using a dirty_log */
  776. struct dm_dirty_log *log = bitmap->mddev->bitmap_info.log;
  777. dirty = test_and_clear_bit(BITMAP_PAGE_DIRTY, &bitmap->logattrs);
  778. need_write = test_and_clear_bit(BITMAP_PAGE_NEEDWRITE, &bitmap->logattrs);
  779. if (dirty || need_write)
  780. if (log->type->flush(log))
  781. bitmap->flags |= BITMAP_WRITE_ERROR;
  782. goto out;
  783. }
  784. /* look at each page to see if there are any set bits that need to be
  785. * flushed out to disk */
  786. for (i = 0; i < bitmap->file_pages; i++) {
  787. spin_lock_irqsave(&bitmap->lock, flags);
  788. if (!bitmap->filemap) {
  789. spin_unlock_irqrestore(&bitmap->lock, flags);
  790. return;
  791. }
  792. page = bitmap->filemap[i];
  793. dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  794. need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  795. clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  796. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  797. if (dirty)
  798. wait = 1;
  799. spin_unlock_irqrestore(&bitmap->lock, flags);
  800. if (dirty || need_write)
  801. write_page(bitmap, page, 0);
  802. }
  803. if (wait) { /* if any writes were performed, we need to wait on them */
  804. if (bitmap->file)
  805. wait_event(bitmap->write_wait,
  806. atomic_read(&bitmap->pending_writes)==0);
  807. else
  808. md_super_wait(bitmap->mddev);
  809. }
  810. out:
  811. if (bitmap->flags & BITMAP_WRITE_ERROR)
  812. bitmap_file_kick(bitmap);
  813. }
  814. EXPORT_SYMBOL(bitmap_unplug);
  815. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
  816. /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
  817. * the in-memory bitmap from the on-disk bitmap -- also, sets up the
  818. * memory mapping of the bitmap file
  819. * Special cases:
  820. * if there's no bitmap file, or if the bitmap file had been
  821. * previously kicked from the array, we mark all the bits as
  822. * 1's in order to cause a full resync.
  823. *
  824. * We ignore all bits for sectors that end earlier than 'start'.
  825. * This is used when reading an out-of-date bitmap...
  826. */
  827. static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
  828. {
  829. unsigned long i, chunks, index, oldindex, bit;
  830. struct page *page = NULL, *oldpage = NULL;
  831. unsigned long num_pages, bit_cnt = 0;
  832. struct file *file;
  833. unsigned long bytes, offset;
  834. int outofdate;
  835. int ret = -ENOSPC;
  836. void *paddr;
  837. chunks = bitmap->chunks;
  838. file = bitmap->file;
  839. BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
  840. #ifdef INJECT_FAULTS_3
  841. outofdate = 1;
  842. #else
  843. outofdate = bitmap->flags & BITMAP_STALE;
  844. #endif
  845. if (outofdate)
  846. printk(KERN_INFO "%s: bitmap file is out of date, doing full "
  847. "recovery\n", bmname(bitmap));
  848. bytes = DIV_ROUND_UP(bitmap->chunks, 8);
  849. if (!bitmap->mddev->bitmap_info.external)
  850. bytes += sizeof(bitmap_super_t);
  851. num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
  852. if (file && i_size_read(file->f_mapping->host) < bytes) {
  853. printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
  854. bmname(bitmap),
  855. (unsigned long) i_size_read(file->f_mapping->host),
  856. bytes);
  857. goto err;
  858. }
  859. ret = -ENOMEM;
  860. bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  861. if (!bitmap->filemap)
  862. goto err;
  863. /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
  864. bitmap->filemap_attr = kzalloc(
  865. roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
  866. GFP_KERNEL);
  867. if (!bitmap->filemap_attr)
  868. goto err;
  869. oldindex = ~0L;
  870. for (i = 0; i < chunks; i++) {
  871. int b;
  872. index = file_page_index(bitmap, i);
  873. bit = file_page_offset(bitmap, i);
  874. if (index != oldindex) { /* this is a new page, read it in */
  875. int count;
  876. /* unmap the old page, we're done with it */
  877. if (index == num_pages-1)
  878. count = bytes - index * PAGE_SIZE;
  879. else
  880. count = PAGE_SIZE;
  881. if (index == 0 && bitmap->sb_page) {
  882. /*
  883. * if we're here then the superblock page
  884. * contains some bits (PAGE_SIZE != sizeof sb)
  885. * we've already read it in, so just use it
  886. */
  887. page = bitmap->sb_page;
  888. offset = sizeof(bitmap_super_t);
  889. if (!file)
  890. page = read_sb_page(
  891. bitmap->mddev,
  892. bitmap->mddev->bitmap_info.offset,
  893. page,
  894. index, count);
  895. } else if (file) {
  896. page = read_page(file, index, bitmap, count);
  897. offset = 0;
  898. } else {
  899. page = read_sb_page(bitmap->mddev,
  900. bitmap->mddev->bitmap_info.offset,
  901. NULL,
  902. index, count);
  903. offset = 0;
  904. }
  905. if (IS_ERR(page)) { /* read error */
  906. ret = PTR_ERR(page);
  907. goto err;
  908. }
  909. oldindex = index;
  910. oldpage = page;
  911. bitmap->filemap[bitmap->file_pages++] = page;
  912. bitmap->last_page_size = count;
  913. if (outofdate) {
  914. /*
  915. * if bitmap is out of date, dirty the
  916. * whole page and write it out
  917. */
  918. paddr = kmap_atomic(page, KM_USER0);
  919. memset(paddr + offset, 0xff,
  920. PAGE_SIZE - offset);
  921. kunmap_atomic(paddr, KM_USER0);
  922. write_page(bitmap, page, 1);
  923. ret = -EIO;
  924. if (bitmap->flags & BITMAP_WRITE_ERROR)
  925. goto err;
  926. }
  927. }
  928. paddr = kmap_atomic(page, KM_USER0);
  929. if (bitmap->flags & BITMAP_HOSTENDIAN)
  930. b = test_bit(bit, paddr);
  931. else
  932. b = test_bit_le(bit, paddr);
  933. kunmap_atomic(paddr, KM_USER0);
  934. if (b) {
  935. /* if the disk bit is set, set the memory bit */
  936. int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
  937. >= start);
  938. bitmap_set_memory_bits(bitmap,
  939. (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
  940. needed);
  941. bit_cnt++;
  942. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  943. }
  944. }
  945. /* everything went OK */
  946. ret = 0;
  947. bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
  948. if (bit_cnt) { /* Kick recovery if any bits were set */
  949. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  950. md_wakeup_thread(bitmap->mddev->thread);
  951. }
  952. printk(KERN_INFO "%s: bitmap initialized from disk: "
  953. "read %lu/%lu pages, set %lu bits\n",
  954. bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
  955. return 0;
  956. err:
  957. printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
  958. bmname(bitmap), ret);
  959. return ret;
  960. }
  961. void bitmap_write_all(struct bitmap *bitmap)
  962. {
  963. /* We don't actually write all bitmap blocks here,
  964. * just flag them as needing to be written
  965. */
  966. int i;
  967. for (i = 0; i < bitmap->file_pages; i++)
  968. set_page_attr(bitmap, bitmap->filemap[i],
  969. BITMAP_PAGE_NEEDWRITE);
  970. }
  971. static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
  972. {
  973. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  974. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  975. bitmap->bp[page].count += inc;
  976. bitmap_checkfree(bitmap, page);
  977. }
  978. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  979. sector_t offset, sector_t *blocks,
  980. int create);
  981. /*
  982. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  983. * out to disk
  984. */
  985. void bitmap_daemon_work(mddev_t *mddev)
  986. {
  987. struct bitmap *bitmap;
  988. unsigned long j;
  989. unsigned long flags;
  990. struct page *page = NULL, *lastpage = NULL;
  991. sector_t blocks;
  992. void *paddr;
  993. struct dm_dirty_log *log = mddev->bitmap_info.log;
  994. /* Use a mutex to guard daemon_work against
  995. * bitmap_destroy.
  996. */
  997. mutex_lock(&mddev->bitmap_info.mutex);
  998. bitmap = mddev->bitmap;
  999. if (bitmap == NULL) {
  1000. mutex_unlock(&mddev->bitmap_info.mutex);
  1001. return;
  1002. }
  1003. if (time_before(jiffies, bitmap->daemon_lastrun
  1004. + bitmap->mddev->bitmap_info.daemon_sleep))
  1005. goto done;
  1006. bitmap->daemon_lastrun = jiffies;
  1007. if (bitmap->allclean) {
  1008. bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1009. goto done;
  1010. }
  1011. bitmap->allclean = 1;
  1012. spin_lock_irqsave(&bitmap->lock, flags);
  1013. for (j = 0; j < bitmap->chunks; j++) {
  1014. bitmap_counter_t *bmc;
  1015. if (!bitmap->filemap) {
  1016. if (!log)
  1017. /* error or shutdown */
  1018. break;
  1019. } else
  1020. page = filemap_get_page(bitmap, j);
  1021. if (page != lastpage) {
  1022. /* skip this page unless it's marked as needing cleaning */
  1023. if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
  1024. int need_write = test_page_attr(bitmap, page,
  1025. BITMAP_PAGE_NEEDWRITE);
  1026. if (need_write)
  1027. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  1028. spin_unlock_irqrestore(&bitmap->lock, flags);
  1029. if (need_write) {
  1030. write_page(bitmap, page, 0);
  1031. bitmap->allclean = 0;
  1032. }
  1033. spin_lock_irqsave(&bitmap->lock, flags);
  1034. j |= (PAGE_BITS - 1);
  1035. continue;
  1036. }
  1037. /* grab the new page, sync and release the old */
  1038. if (lastpage != NULL) {
  1039. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  1040. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1041. spin_unlock_irqrestore(&bitmap->lock, flags);
  1042. write_page(bitmap, lastpage, 0);
  1043. } else {
  1044. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1045. spin_unlock_irqrestore(&bitmap->lock, flags);
  1046. }
  1047. } else
  1048. spin_unlock_irqrestore(&bitmap->lock, flags);
  1049. lastpage = page;
  1050. /* We are possibly going to clear some bits, so make
  1051. * sure that events_cleared is up-to-date.
  1052. */
  1053. if (bitmap->need_sync &&
  1054. bitmap->mddev->bitmap_info.external == 0) {
  1055. bitmap_super_t *sb;
  1056. bitmap->need_sync = 0;
  1057. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  1058. sb->events_cleared =
  1059. cpu_to_le64(bitmap->events_cleared);
  1060. kunmap_atomic(sb, KM_USER0);
  1061. write_page(bitmap, bitmap->sb_page, 1);
  1062. }
  1063. spin_lock_irqsave(&bitmap->lock, flags);
  1064. if (!bitmap->need_sync)
  1065. clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1066. }
  1067. bmc = bitmap_get_counter(bitmap,
  1068. (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
  1069. &blocks, 0);
  1070. if (bmc) {
  1071. if (*bmc)
  1072. bitmap->allclean = 0;
  1073. if (*bmc == 2) {
  1074. *bmc = 1; /* maybe clear the bit next time */
  1075. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1076. } else if (*bmc == 1 && !bitmap->need_sync) {
  1077. /* we can clear the bit */
  1078. *bmc = 0;
  1079. bitmap_count_page(bitmap,
  1080. (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
  1081. -1);
  1082. /* clear the bit */
  1083. if (page) {
  1084. paddr = kmap_atomic(page, KM_USER0);
  1085. if (bitmap->flags & BITMAP_HOSTENDIAN)
  1086. clear_bit(file_page_offset(bitmap, j),
  1087. paddr);
  1088. else
  1089. __test_and_clear_bit_le(file_page_offset(bitmap, j),
  1090. paddr);
  1091. kunmap_atomic(paddr, KM_USER0);
  1092. } else
  1093. log->type->clear_region(log, j);
  1094. }
  1095. } else
  1096. j |= PAGE_COUNTER_MASK;
  1097. }
  1098. spin_unlock_irqrestore(&bitmap->lock, flags);
  1099. /* now sync the final page */
  1100. if (lastpage != NULL || log != NULL) {
  1101. spin_lock_irqsave(&bitmap->lock, flags);
  1102. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  1103. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1104. spin_unlock_irqrestore(&bitmap->lock, flags);
  1105. if (lastpage)
  1106. write_page(bitmap, lastpage, 0);
  1107. else
  1108. if (log->type->flush(log))
  1109. bitmap->flags |= BITMAP_WRITE_ERROR;
  1110. } else {
  1111. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1112. spin_unlock_irqrestore(&bitmap->lock, flags);
  1113. }
  1114. }
  1115. done:
  1116. if (bitmap->allclean == 0)
  1117. bitmap->mddev->thread->timeout =
  1118. bitmap->mddev->bitmap_info.daemon_sleep;
  1119. mutex_unlock(&mddev->bitmap_info.mutex);
  1120. }
  1121. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1122. sector_t offset, sector_t *blocks,
  1123. int create)
  1124. __releases(bitmap->lock)
  1125. __acquires(bitmap->lock)
  1126. {
  1127. /* If 'create', we might release the lock and reclaim it.
  1128. * The lock must have been taken with interrupts enabled.
  1129. * If !create, we don't release the lock.
  1130. */
  1131. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  1132. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1133. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  1134. sector_t csize;
  1135. int err;
  1136. err = bitmap_checkpage(bitmap, page, create);
  1137. if (bitmap->bp[page].hijacked ||
  1138. bitmap->bp[page].map == NULL)
  1139. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
  1140. PAGE_COUNTER_SHIFT - 1);
  1141. else
  1142. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1143. *blocks = csize - (offset & (csize - 1));
  1144. if (err < 0)
  1145. return NULL;
  1146. /* now locked ... */
  1147. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  1148. /* should we use the first or second counter field
  1149. * of the hijacked pointer? */
  1150. int hi = (pageoff > PAGE_COUNTER_MASK);
  1151. return &((bitmap_counter_t *)
  1152. &bitmap->bp[page].map)[hi];
  1153. } else /* page is allocated */
  1154. return (bitmap_counter_t *)
  1155. &(bitmap->bp[page].map[pageoff]);
  1156. }
  1157. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
  1158. {
  1159. if (!bitmap)
  1160. return 0;
  1161. if (behind) {
  1162. int bw;
  1163. atomic_inc(&bitmap->behind_writes);
  1164. bw = atomic_read(&bitmap->behind_writes);
  1165. if (bw > bitmap->behind_writes_used)
  1166. bitmap->behind_writes_used = bw;
  1167. PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
  1168. bw, bitmap->max_write_behind);
  1169. }
  1170. while (sectors) {
  1171. sector_t blocks;
  1172. bitmap_counter_t *bmc;
  1173. spin_lock_irq(&bitmap->lock);
  1174. bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
  1175. if (!bmc) {
  1176. spin_unlock_irq(&bitmap->lock);
  1177. return 0;
  1178. }
  1179. if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
  1180. DEFINE_WAIT(__wait);
  1181. /* note that it is safe to do the prepare_to_wait
  1182. * after the test as long as we do it before dropping
  1183. * the spinlock.
  1184. */
  1185. prepare_to_wait(&bitmap->overflow_wait, &__wait,
  1186. TASK_UNINTERRUPTIBLE);
  1187. spin_unlock_irq(&bitmap->lock);
  1188. io_schedule();
  1189. finish_wait(&bitmap->overflow_wait, &__wait);
  1190. continue;
  1191. }
  1192. switch (*bmc) {
  1193. case 0:
  1194. bitmap_file_set_bit(bitmap, offset);
  1195. bitmap_count_page(bitmap, offset, 1);
  1196. /* fall through */
  1197. case 1:
  1198. *bmc = 2;
  1199. }
  1200. (*bmc)++;
  1201. spin_unlock_irq(&bitmap->lock);
  1202. offset += blocks;
  1203. if (sectors > blocks)
  1204. sectors -= blocks;
  1205. else
  1206. sectors = 0;
  1207. }
  1208. bitmap->allclean = 0;
  1209. return 0;
  1210. }
  1211. EXPORT_SYMBOL(bitmap_startwrite);
  1212. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1213. int success, int behind)
  1214. {
  1215. if (!bitmap)
  1216. return;
  1217. if (behind) {
  1218. if (atomic_dec_and_test(&bitmap->behind_writes))
  1219. wake_up(&bitmap->behind_wait);
  1220. PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
  1221. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1222. }
  1223. if (bitmap->mddev->degraded)
  1224. /* Never clear bits or update events_cleared when degraded */
  1225. success = 0;
  1226. while (sectors) {
  1227. sector_t blocks;
  1228. unsigned long flags;
  1229. bitmap_counter_t *bmc;
  1230. spin_lock_irqsave(&bitmap->lock, flags);
  1231. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1232. if (!bmc) {
  1233. spin_unlock_irqrestore(&bitmap->lock, flags);
  1234. return;
  1235. }
  1236. if (success &&
  1237. bitmap->events_cleared < bitmap->mddev->events) {
  1238. bitmap->events_cleared = bitmap->mddev->events;
  1239. bitmap->need_sync = 1;
  1240. sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
  1241. }
  1242. if (!success && ! (*bmc & NEEDED_MASK))
  1243. *bmc |= NEEDED_MASK;
  1244. if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
  1245. wake_up(&bitmap->overflow_wait);
  1246. (*bmc)--;
  1247. if (*bmc <= 2)
  1248. set_page_attr(bitmap,
  1249. filemap_get_page(
  1250. bitmap,
  1251. offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1252. BITMAP_PAGE_CLEAN);
  1253. spin_unlock_irqrestore(&bitmap->lock, flags);
  1254. offset += blocks;
  1255. if (sectors > blocks)
  1256. sectors -= blocks;
  1257. else
  1258. sectors = 0;
  1259. }
  1260. }
  1261. EXPORT_SYMBOL(bitmap_endwrite);
  1262. static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1263. int degraded)
  1264. {
  1265. bitmap_counter_t *bmc;
  1266. int rv;
  1267. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1268. *blocks = 1024;
  1269. return 1; /* always resync if no bitmap */
  1270. }
  1271. spin_lock_irq(&bitmap->lock);
  1272. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1273. rv = 0;
  1274. if (bmc) {
  1275. /* locked */
  1276. if (RESYNC(*bmc))
  1277. rv = 1;
  1278. else if (NEEDED(*bmc)) {
  1279. rv = 1;
  1280. if (!degraded) { /* don't set/clear bits if degraded */
  1281. *bmc |= RESYNC_MASK;
  1282. *bmc &= ~NEEDED_MASK;
  1283. }
  1284. }
  1285. }
  1286. spin_unlock_irq(&bitmap->lock);
  1287. bitmap->allclean = 0;
  1288. return rv;
  1289. }
  1290. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1291. int degraded)
  1292. {
  1293. /* bitmap_start_sync must always report on multiples of whole
  1294. * pages, otherwise resync (which is very PAGE_SIZE based) will
  1295. * get confused.
  1296. * So call __bitmap_start_sync repeatedly (if needed) until
  1297. * At least PAGE_SIZE>>9 blocks are covered.
  1298. * Return the 'or' of the result.
  1299. */
  1300. int rv = 0;
  1301. sector_t blocks1;
  1302. *blocks = 0;
  1303. while (*blocks < (PAGE_SIZE>>9)) {
  1304. rv |= __bitmap_start_sync(bitmap, offset,
  1305. &blocks1, degraded);
  1306. offset += blocks1;
  1307. *blocks += blocks1;
  1308. }
  1309. return rv;
  1310. }
  1311. EXPORT_SYMBOL(bitmap_start_sync);
  1312. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
  1313. {
  1314. bitmap_counter_t *bmc;
  1315. unsigned long flags;
  1316. if (bitmap == NULL) {
  1317. *blocks = 1024;
  1318. return;
  1319. }
  1320. spin_lock_irqsave(&bitmap->lock, flags);
  1321. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1322. if (bmc == NULL)
  1323. goto unlock;
  1324. /* locked */
  1325. if (RESYNC(*bmc)) {
  1326. *bmc &= ~RESYNC_MASK;
  1327. if (!NEEDED(*bmc) && aborted)
  1328. *bmc |= NEEDED_MASK;
  1329. else {
  1330. if (*bmc <= 2)
  1331. set_page_attr(bitmap,
  1332. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1333. BITMAP_PAGE_CLEAN);
  1334. }
  1335. }
  1336. unlock:
  1337. spin_unlock_irqrestore(&bitmap->lock, flags);
  1338. bitmap->allclean = 0;
  1339. }
  1340. EXPORT_SYMBOL(bitmap_end_sync);
  1341. void bitmap_close_sync(struct bitmap *bitmap)
  1342. {
  1343. /* Sync has finished, and any bitmap chunks that weren't synced
  1344. * properly have been aborted. It remains to us to clear the
  1345. * RESYNC bit wherever it is still on
  1346. */
  1347. sector_t sector = 0;
  1348. sector_t blocks;
  1349. if (!bitmap)
  1350. return;
  1351. while (sector < bitmap->mddev->resync_max_sectors) {
  1352. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1353. sector += blocks;
  1354. }
  1355. }
  1356. EXPORT_SYMBOL(bitmap_close_sync);
  1357. void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
  1358. {
  1359. sector_t s = 0;
  1360. sector_t blocks;
  1361. if (!bitmap)
  1362. return;
  1363. if (sector == 0) {
  1364. bitmap->last_end_sync = jiffies;
  1365. return;
  1366. }
  1367. if (time_before(jiffies, (bitmap->last_end_sync
  1368. + bitmap->mddev->bitmap_info.daemon_sleep)))
  1369. return;
  1370. wait_event(bitmap->mddev->recovery_wait,
  1371. atomic_read(&bitmap->mddev->recovery_active) == 0);
  1372. bitmap->mddev->curr_resync_completed = sector;
  1373. set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
  1374. sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
  1375. s = 0;
  1376. while (s < sector && s < bitmap->mddev->resync_max_sectors) {
  1377. bitmap_end_sync(bitmap, s, &blocks, 0);
  1378. s += blocks;
  1379. }
  1380. bitmap->last_end_sync = jiffies;
  1381. sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
  1382. }
  1383. EXPORT_SYMBOL(bitmap_cond_end_sync);
  1384. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1385. {
  1386. /* For each chunk covered by any of these sectors, set the
  1387. * counter to 1 and set resync_needed. They should all
  1388. * be 0 at this point
  1389. */
  1390. sector_t secs;
  1391. bitmap_counter_t *bmc;
  1392. spin_lock_irq(&bitmap->lock);
  1393. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1394. if (!bmc) {
  1395. spin_unlock_irq(&bitmap->lock);
  1396. return;
  1397. }
  1398. if (!*bmc) {
  1399. struct page *page;
  1400. *bmc = 1 | (needed ? NEEDED_MASK : 0);
  1401. bitmap_count_page(bitmap, offset, 1);
  1402. page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
  1403. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1404. }
  1405. spin_unlock_irq(&bitmap->lock);
  1406. bitmap->allclean = 0;
  1407. }
  1408. /* dirty the memory and file bits for bitmap chunks "s" to "e" */
  1409. void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
  1410. {
  1411. unsigned long chunk;
  1412. for (chunk = s; chunk <= e; chunk++) {
  1413. sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
  1414. bitmap_set_memory_bits(bitmap, sec, 1);
  1415. bitmap_file_set_bit(bitmap, sec);
  1416. if (sec < bitmap->mddev->recovery_cp)
  1417. /* We are asserting that the array is dirty,
  1418. * so move the recovery_cp address back so
  1419. * that it is obvious that it is dirty
  1420. */
  1421. bitmap->mddev->recovery_cp = sec;
  1422. }
  1423. }
  1424. /*
  1425. * flush out any pending updates
  1426. */
  1427. void bitmap_flush(mddev_t *mddev)
  1428. {
  1429. struct bitmap *bitmap = mddev->bitmap;
  1430. long sleep;
  1431. if (!bitmap) /* there was no bitmap */
  1432. return;
  1433. /* run the daemon_work three time to ensure everything is flushed
  1434. * that can be
  1435. */
  1436. sleep = mddev->bitmap_info.daemon_sleep * 2;
  1437. bitmap->daemon_lastrun -= sleep;
  1438. bitmap_daemon_work(mddev);
  1439. bitmap->daemon_lastrun -= sleep;
  1440. bitmap_daemon_work(mddev);
  1441. bitmap->daemon_lastrun -= sleep;
  1442. bitmap_daemon_work(mddev);
  1443. bitmap_update_sb(bitmap);
  1444. }
  1445. /*
  1446. * free memory that was allocated
  1447. */
  1448. static void bitmap_free(struct bitmap *bitmap)
  1449. {
  1450. unsigned long k, pages;
  1451. struct bitmap_page *bp;
  1452. if (!bitmap) /* there was no bitmap */
  1453. return;
  1454. /* release the bitmap file and kill the daemon */
  1455. bitmap_file_put(bitmap);
  1456. bp = bitmap->bp;
  1457. pages = bitmap->pages;
  1458. /* free all allocated memory */
  1459. if (bp) /* deallocate the page memory */
  1460. for (k = 0; k < pages; k++)
  1461. if (bp[k].map && !bp[k].hijacked)
  1462. kfree(bp[k].map);
  1463. kfree(bp);
  1464. kfree(bitmap);
  1465. }
  1466. void bitmap_destroy(mddev_t *mddev)
  1467. {
  1468. struct bitmap *bitmap = mddev->bitmap;
  1469. if (!bitmap) /* there was no bitmap */
  1470. return;
  1471. mutex_lock(&mddev->bitmap_info.mutex);
  1472. mddev->bitmap = NULL; /* disconnect from the md device */
  1473. mutex_unlock(&mddev->bitmap_info.mutex);
  1474. if (mddev->thread)
  1475. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1476. if (bitmap->sysfs_can_clear)
  1477. sysfs_put(bitmap->sysfs_can_clear);
  1478. bitmap_free(bitmap);
  1479. }
  1480. /*
  1481. * initialize the bitmap structure
  1482. * if this returns an error, bitmap_destroy must be called to do clean up
  1483. */
  1484. int bitmap_create(mddev_t *mddev)
  1485. {
  1486. struct bitmap *bitmap;
  1487. sector_t blocks = mddev->resync_max_sectors;
  1488. unsigned long chunks;
  1489. unsigned long pages;
  1490. struct file *file = mddev->bitmap_info.file;
  1491. int err;
  1492. struct sysfs_dirent *bm = NULL;
  1493. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1494. if (!file
  1495. && !mddev->bitmap_info.offset
  1496. && !mddev->bitmap_info.log) /* bitmap disabled, nothing to do */
  1497. return 0;
  1498. BUG_ON(file && mddev->bitmap_info.offset);
  1499. BUG_ON(mddev->bitmap_info.offset && mddev->bitmap_info.log);
  1500. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1501. if (!bitmap)
  1502. return -ENOMEM;
  1503. spin_lock_init(&bitmap->lock);
  1504. atomic_set(&bitmap->pending_writes, 0);
  1505. init_waitqueue_head(&bitmap->write_wait);
  1506. init_waitqueue_head(&bitmap->overflow_wait);
  1507. init_waitqueue_head(&bitmap->behind_wait);
  1508. bitmap->mddev = mddev;
  1509. if (mddev->kobj.sd)
  1510. bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
  1511. if (bm) {
  1512. bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
  1513. sysfs_put(bm);
  1514. } else
  1515. bitmap->sysfs_can_clear = NULL;
  1516. bitmap->file = file;
  1517. if (file) {
  1518. get_file(file);
  1519. /* As future accesses to this file will use bmap,
  1520. * and bypass the page cache, we must sync the file
  1521. * first.
  1522. */
  1523. vfs_fsync(file, 1);
  1524. }
  1525. /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
  1526. if (!mddev->bitmap_info.external)
  1527. err = bitmap_read_sb(bitmap);
  1528. else {
  1529. err = 0;
  1530. if (mddev->bitmap_info.chunksize == 0 ||
  1531. mddev->bitmap_info.daemon_sleep == 0)
  1532. /* chunksize and time_base need to be
  1533. * set first. */
  1534. err = -EINVAL;
  1535. }
  1536. if (err)
  1537. goto error;
  1538. bitmap->daemon_lastrun = jiffies;
  1539. bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
  1540. /* now that chunksize and chunkshift are set, we can use these macros */
  1541. chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
  1542. CHUNK_BLOCK_SHIFT(bitmap);
  1543. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1544. BUG_ON(!pages);
  1545. bitmap->chunks = chunks;
  1546. bitmap->pages = pages;
  1547. bitmap->missing_pages = pages;
  1548. bitmap->counter_bits = COUNTER_BITS;
  1549. bitmap->syncchunk = ~0UL;
  1550. #ifdef INJECT_FATAL_FAULT_1
  1551. bitmap->bp = NULL;
  1552. #else
  1553. bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1554. #endif
  1555. err = -ENOMEM;
  1556. if (!bitmap->bp)
  1557. goto error;
  1558. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1559. pages, bmname(bitmap));
  1560. mddev->bitmap = bitmap;
  1561. return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
  1562. error:
  1563. bitmap_free(bitmap);
  1564. return err;
  1565. }
  1566. int bitmap_load(mddev_t *mddev)
  1567. {
  1568. int err = 0;
  1569. sector_t sector = 0;
  1570. struct bitmap *bitmap = mddev->bitmap;
  1571. if (!bitmap)
  1572. goto out;
  1573. /* Clear out old bitmap info first: Either there is none, or we
  1574. * are resuming after someone else has possibly changed things,
  1575. * so we should forget old cached info.
  1576. * All chunks should be clean, but some might need_sync.
  1577. */
  1578. while (sector < mddev->resync_max_sectors) {
  1579. sector_t blocks;
  1580. bitmap_start_sync(bitmap, sector, &blocks, 0);
  1581. sector += blocks;
  1582. }
  1583. bitmap_close_sync(bitmap);
  1584. if (mddev->bitmap_info.log) {
  1585. unsigned long i;
  1586. struct dm_dirty_log *log = mddev->bitmap_info.log;
  1587. for (i = 0; i < bitmap->chunks; i++)
  1588. if (!log->type->in_sync(log, i, 1))
  1589. bitmap_set_memory_bits(bitmap,
  1590. (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
  1591. 1);
  1592. } else {
  1593. sector_t start = 0;
  1594. if (mddev->degraded == 0
  1595. || bitmap->events_cleared == mddev->events)
  1596. /* no need to keep dirty bits to optimise a
  1597. * re-add of a missing device */
  1598. start = mddev->recovery_cp;
  1599. err = bitmap_init_from_disk(bitmap, start);
  1600. }
  1601. if (err)
  1602. goto out;
  1603. mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
  1604. md_wakeup_thread(mddev->thread);
  1605. bitmap_update_sb(bitmap);
  1606. if (bitmap->flags & BITMAP_WRITE_ERROR)
  1607. err = -EIO;
  1608. out:
  1609. return err;
  1610. }
  1611. EXPORT_SYMBOL_GPL(bitmap_load);
  1612. static ssize_t
  1613. location_show(mddev_t *mddev, char *page)
  1614. {
  1615. ssize_t len;
  1616. if (mddev->bitmap_info.file)
  1617. len = sprintf(page, "file");
  1618. else if (mddev->bitmap_info.offset)
  1619. len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
  1620. else
  1621. len = sprintf(page, "none");
  1622. len += sprintf(page+len, "\n");
  1623. return len;
  1624. }
  1625. static ssize_t
  1626. location_store(mddev_t *mddev, const char *buf, size_t len)
  1627. {
  1628. if (mddev->pers) {
  1629. if (!mddev->pers->quiesce)
  1630. return -EBUSY;
  1631. if (mddev->recovery || mddev->sync_thread)
  1632. return -EBUSY;
  1633. }
  1634. if (mddev->bitmap || mddev->bitmap_info.file ||
  1635. mddev->bitmap_info.offset) {
  1636. /* bitmap already configured. Only option is to clear it */
  1637. if (strncmp(buf, "none", 4) != 0)
  1638. return -EBUSY;
  1639. if (mddev->pers) {
  1640. mddev->pers->quiesce(mddev, 1);
  1641. bitmap_destroy(mddev);
  1642. mddev->pers->quiesce(mddev, 0);
  1643. }
  1644. mddev->bitmap_info.offset = 0;
  1645. if (mddev->bitmap_info.file) {
  1646. struct file *f = mddev->bitmap_info.file;
  1647. mddev->bitmap_info.file = NULL;
  1648. restore_bitmap_write_access(f);
  1649. fput(f);
  1650. }
  1651. } else {
  1652. /* No bitmap, OK to set a location */
  1653. long long offset;
  1654. if (strncmp(buf, "none", 4) == 0)
  1655. /* nothing to be done */;
  1656. else if (strncmp(buf, "file:", 5) == 0) {
  1657. /* Not supported yet */
  1658. return -EINVAL;
  1659. } else {
  1660. int rv;
  1661. if (buf[0] == '+')
  1662. rv = strict_strtoll(buf+1, 10, &offset);
  1663. else
  1664. rv = strict_strtoll(buf, 10, &offset);
  1665. if (rv)
  1666. return rv;
  1667. if (offset == 0)
  1668. return -EINVAL;
  1669. if (mddev->bitmap_info.external == 0 &&
  1670. mddev->major_version == 0 &&
  1671. offset != mddev->bitmap_info.default_offset)
  1672. return -EINVAL;
  1673. mddev->bitmap_info.offset = offset;
  1674. if (mddev->pers) {
  1675. mddev->pers->quiesce(mddev, 1);
  1676. rv = bitmap_create(mddev);
  1677. if (rv) {
  1678. bitmap_destroy(mddev);
  1679. mddev->bitmap_info.offset = 0;
  1680. }
  1681. mddev->pers->quiesce(mddev, 0);
  1682. if (rv)
  1683. return rv;
  1684. }
  1685. }
  1686. }
  1687. if (!mddev->external) {
  1688. /* Ensure new bitmap info is stored in
  1689. * metadata promptly.
  1690. */
  1691. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1692. md_wakeup_thread(mddev->thread);
  1693. }
  1694. return len;
  1695. }
  1696. static struct md_sysfs_entry bitmap_location =
  1697. __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
  1698. static ssize_t
  1699. timeout_show(mddev_t *mddev, char *page)
  1700. {
  1701. ssize_t len;
  1702. unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
  1703. unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
  1704. len = sprintf(page, "%lu", secs);
  1705. if (jifs)
  1706. len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
  1707. len += sprintf(page+len, "\n");
  1708. return len;
  1709. }
  1710. static ssize_t
  1711. timeout_store(mddev_t *mddev, const char *buf, size_t len)
  1712. {
  1713. /* timeout can be set at any time */
  1714. unsigned long timeout;
  1715. int rv = strict_strtoul_scaled(buf, &timeout, 4);
  1716. if (rv)
  1717. return rv;
  1718. /* just to make sure we don't overflow... */
  1719. if (timeout >= LONG_MAX / HZ)
  1720. return -EINVAL;
  1721. timeout = timeout * HZ / 10000;
  1722. if (timeout >= MAX_SCHEDULE_TIMEOUT)
  1723. timeout = MAX_SCHEDULE_TIMEOUT-1;
  1724. if (timeout < 1)
  1725. timeout = 1;
  1726. mddev->bitmap_info.daemon_sleep = timeout;
  1727. if (mddev->thread) {
  1728. /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
  1729. * the bitmap is all clean and we don't need to
  1730. * adjust the timeout right now
  1731. */
  1732. if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
  1733. mddev->thread->timeout = timeout;
  1734. md_wakeup_thread(mddev->thread);
  1735. }
  1736. }
  1737. return len;
  1738. }
  1739. static struct md_sysfs_entry bitmap_timeout =
  1740. __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
  1741. static ssize_t
  1742. backlog_show(mddev_t *mddev, char *page)
  1743. {
  1744. return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
  1745. }
  1746. static ssize_t
  1747. backlog_store(mddev_t *mddev, const char *buf, size_t len)
  1748. {
  1749. unsigned long backlog;
  1750. int rv = strict_strtoul(buf, 10, &backlog);
  1751. if (rv)
  1752. return rv;
  1753. if (backlog > COUNTER_MAX)
  1754. return -EINVAL;
  1755. mddev->bitmap_info.max_write_behind = backlog;
  1756. return len;
  1757. }
  1758. static struct md_sysfs_entry bitmap_backlog =
  1759. __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
  1760. static ssize_t
  1761. chunksize_show(mddev_t *mddev, char *page)
  1762. {
  1763. return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
  1764. }
  1765. static ssize_t
  1766. chunksize_store(mddev_t *mddev, const char *buf, size_t len)
  1767. {
  1768. /* Can only be changed when no bitmap is active */
  1769. int rv;
  1770. unsigned long csize;
  1771. if (mddev->bitmap)
  1772. return -EBUSY;
  1773. rv = strict_strtoul(buf, 10, &csize);
  1774. if (rv)
  1775. return rv;
  1776. if (csize < 512 ||
  1777. !is_power_of_2(csize))
  1778. return -EINVAL;
  1779. mddev->bitmap_info.chunksize = csize;
  1780. return len;
  1781. }
  1782. static struct md_sysfs_entry bitmap_chunksize =
  1783. __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
  1784. static ssize_t metadata_show(mddev_t *mddev, char *page)
  1785. {
  1786. return sprintf(page, "%s\n", (mddev->bitmap_info.external
  1787. ? "external" : "internal"));
  1788. }
  1789. static ssize_t metadata_store(mddev_t *mddev, const char *buf, size_t len)
  1790. {
  1791. if (mddev->bitmap ||
  1792. mddev->bitmap_info.file ||
  1793. mddev->bitmap_info.offset)
  1794. return -EBUSY;
  1795. if (strncmp(buf, "external", 8) == 0)
  1796. mddev->bitmap_info.external = 1;
  1797. else if (strncmp(buf, "internal", 8) == 0)
  1798. mddev->bitmap_info.external = 0;
  1799. else
  1800. return -EINVAL;
  1801. return len;
  1802. }
  1803. static struct md_sysfs_entry bitmap_metadata =
  1804. __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
  1805. static ssize_t can_clear_show(mddev_t *mddev, char *page)
  1806. {
  1807. int len;
  1808. if (mddev->bitmap)
  1809. len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
  1810. "false" : "true"));
  1811. else
  1812. len = sprintf(page, "\n");
  1813. return len;
  1814. }
  1815. static ssize_t can_clear_store(mddev_t *mddev, const char *buf, size_t len)
  1816. {
  1817. if (mddev->bitmap == NULL)
  1818. return -ENOENT;
  1819. if (strncmp(buf, "false", 5) == 0)
  1820. mddev->bitmap->need_sync = 1;
  1821. else if (strncmp(buf, "true", 4) == 0) {
  1822. if (mddev->degraded)
  1823. return -EBUSY;
  1824. mddev->bitmap->need_sync = 0;
  1825. } else
  1826. return -EINVAL;
  1827. return len;
  1828. }
  1829. static struct md_sysfs_entry bitmap_can_clear =
  1830. __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
  1831. static ssize_t
  1832. behind_writes_used_show(mddev_t *mddev, char *page)
  1833. {
  1834. if (mddev->bitmap == NULL)
  1835. return sprintf(page, "0\n");
  1836. return sprintf(page, "%lu\n",
  1837. mddev->bitmap->behind_writes_used);
  1838. }
  1839. static ssize_t
  1840. behind_writes_used_reset(mddev_t *mddev, const char *buf, size_t len)
  1841. {
  1842. if (mddev->bitmap)
  1843. mddev->bitmap->behind_writes_used = 0;
  1844. return len;
  1845. }
  1846. static struct md_sysfs_entry max_backlog_used =
  1847. __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
  1848. behind_writes_used_show, behind_writes_used_reset);
  1849. static struct attribute *md_bitmap_attrs[] = {
  1850. &bitmap_location.attr,
  1851. &bitmap_timeout.attr,
  1852. &bitmap_backlog.attr,
  1853. &bitmap_chunksize.attr,
  1854. &bitmap_metadata.attr,
  1855. &bitmap_can_clear.attr,
  1856. &max_backlog_used.attr,
  1857. NULL
  1858. };
  1859. struct attribute_group md_bitmap_group = {
  1860. .name = "bitmap",
  1861. .attrs = md_bitmap_attrs,
  1862. };