bitmap.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  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_UNPLUG | 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. ext2_set_bit(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 = ext2_test_bit(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. ext2_clear_bit(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. md_unplug(bitmap->mddev);
  1189. schedule();
  1190. finish_wait(&bitmap->overflow_wait, &__wait);
  1191. continue;
  1192. }
  1193. switch (*bmc) {
  1194. case 0:
  1195. bitmap_file_set_bit(bitmap, offset);
  1196. bitmap_count_page(bitmap, offset, 1);
  1197. /* fall through */
  1198. case 1:
  1199. *bmc = 2;
  1200. }
  1201. (*bmc)++;
  1202. spin_unlock_irq(&bitmap->lock);
  1203. offset += blocks;
  1204. if (sectors > blocks)
  1205. sectors -= blocks;
  1206. else
  1207. sectors = 0;
  1208. }
  1209. bitmap->allclean = 0;
  1210. return 0;
  1211. }
  1212. EXPORT_SYMBOL(bitmap_startwrite);
  1213. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1214. int success, int behind)
  1215. {
  1216. if (!bitmap)
  1217. return;
  1218. if (behind) {
  1219. if (atomic_dec_and_test(&bitmap->behind_writes))
  1220. wake_up(&bitmap->behind_wait);
  1221. PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
  1222. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1223. }
  1224. if (bitmap->mddev->degraded)
  1225. /* Never clear bits or update events_cleared when degraded */
  1226. success = 0;
  1227. while (sectors) {
  1228. sector_t blocks;
  1229. unsigned long flags;
  1230. bitmap_counter_t *bmc;
  1231. spin_lock_irqsave(&bitmap->lock, flags);
  1232. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1233. if (!bmc) {
  1234. spin_unlock_irqrestore(&bitmap->lock, flags);
  1235. return;
  1236. }
  1237. if (success &&
  1238. bitmap->events_cleared < bitmap->mddev->events) {
  1239. bitmap->events_cleared = bitmap->mddev->events;
  1240. bitmap->need_sync = 1;
  1241. sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
  1242. }
  1243. if (!success && ! (*bmc & NEEDED_MASK))
  1244. *bmc |= NEEDED_MASK;
  1245. if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
  1246. wake_up(&bitmap->overflow_wait);
  1247. (*bmc)--;
  1248. if (*bmc <= 2)
  1249. set_page_attr(bitmap,
  1250. filemap_get_page(
  1251. bitmap,
  1252. offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1253. BITMAP_PAGE_CLEAN);
  1254. spin_unlock_irqrestore(&bitmap->lock, flags);
  1255. offset += blocks;
  1256. if (sectors > blocks)
  1257. sectors -= blocks;
  1258. else
  1259. sectors = 0;
  1260. }
  1261. }
  1262. EXPORT_SYMBOL(bitmap_endwrite);
  1263. static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1264. int degraded)
  1265. {
  1266. bitmap_counter_t *bmc;
  1267. int rv;
  1268. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1269. *blocks = 1024;
  1270. return 1; /* always resync if no bitmap */
  1271. }
  1272. spin_lock_irq(&bitmap->lock);
  1273. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1274. rv = 0;
  1275. if (bmc) {
  1276. /* locked */
  1277. if (RESYNC(*bmc))
  1278. rv = 1;
  1279. else if (NEEDED(*bmc)) {
  1280. rv = 1;
  1281. if (!degraded) { /* don't set/clear bits if degraded */
  1282. *bmc |= RESYNC_MASK;
  1283. *bmc &= ~NEEDED_MASK;
  1284. }
  1285. }
  1286. }
  1287. spin_unlock_irq(&bitmap->lock);
  1288. bitmap->allclean = 0;
  1289. return rv;
  1290. }
  1291. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1292. int degraded)
  1293. {
  1294. /* bitmap_start_sync must always report on multiples of whole
  1295. * pages, otherwise resync (which is very PAGE_SIZE based) will
  1296. * get confused.
  1297. * So call __bitmap_start_sync repeatedly (if needed) until
  1298. * At least PAGE_SIZE>>9 blocks are covered.
  1299. * Return the 'or' of the result.
  1300. */
  1301. int rv = 0;
  1302. sector_t blocks1;
  1303. *blocks = 0;
  1304. while (*blocks < (PAGE_SIZE>>9)) {
  1305. rv |= __bitmap_start_sync(bitmap, offset,
  1306. &blocks1, degraded);
  1307. offset += blocks1;
  1308. *blocks += blocks1;
  1309. }
  1310. return rv;
  1311. }
  1312. EXPORT_SYMBOL(bitmap_start_sync);
  1313. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
  1314. {
  1315. bitmap_counter_t *bmc;
  1316. unsigned long flags;
  1317. if (bitmap == NULL) {
  1318. *blocks = 1024;
  1319. return;
  1320. }
  1321. spin_lock_irqsave(&bitmap->lock, flags);
  1322. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1323. if (bmc == NULL)
  1324. goto unlock;
  1325. /* locked */
  1326. if (RESYNC(*bmc)) {
  1327. *bmc &= ~RESYNC_MASK;
  1328. if (!NEEDED(*bmc) && aborted)
  1329. *bmc |= NEEDED_MASK;
  1330. else {
  1331. if (*bmc <= 2)
  1332. set_page_attr(bitmap,
  1333. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1334. BITMAP_PAGE_CLEAN);
  1335. }
  1336. }
  1337. unlock:
  1338. spin_unlock_irqrestore(&bitmap->lock, flags);
  1339. bitmap->allclean = 0;
  1340. }
  1341. EXPORT_SYMBOL(bitmap_end_sync);
  1342. void bitmap_close_sync(struct bitmap *bitmap)
  1343. {
  1344. /* Sync has finished, and any bitmap chunks that weren't synced
  1345. * properly have been aborted. It remains to us to clear the
  1346. * RESYNC bit wherever it is still on
  1347. */
  1348. sector_t sector = 0;
  1349. sector_t blocks;
  1350. if (!bitmap)
  1351. return;
  1352. while (sector < bitmap->mddev->resync_max_sectors) {
  1353. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1354. sector += blocks;
  1355. }
  1356. }
  1357. EXPORT_SYMBOL(bitmap_close_sync);
  1358. void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
  1359. {
  1360. sector_t s = 0;
  1361. sector_t blocks;
  1362. if (!bitmap)
  1363. return;
  1364. if (sector == 0) {
  1365. bitmap->last_end_sync = jiffies;
  1366. return;
  1367. }
  1368. if (time_before(jiffies, (bitmap->last_end_sync
  1369. + bitmap->mddev->bitmap_info.daemon_sleep)))
  1370. return;
  1371. wait_event(bitmap->mddev->recovery_wait,
  1372. atomic_read(&bitmap->mddev->recovery_active) == 0);
  1373. bitmap->mddev->curr_resync_completed = sector;
  1374. set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
  1375. sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
  1376. s = 0;
  1377. while (s < sector && s < bitmap->mddev->resync_max_sectors) {
  1378. bitmap_end_sync(bitmap, s, &blocks, 0);
  1379. s += blocks;
  1380. }
  1381. bitmap->last_end_sync = jiffies;
  1382. sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
  1383. }
  1384. EXPORT_SYMBOL(bitmap_cond_end_sync);
  1385. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1386. {
  1387. /* For each chunk covered by any of these sectors, set the
  1388. * counter to 1 and set resync_needed. They should all
  1389. * be 0 at this point
  1390. */
  1391. sector_t secs;
  1392. bitmap_counter_t *bmc;
  1393. spin_lock_irq(&bitmap->lock);
  1394. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1395. if (!bmc) {
  1396. spin_unlock_irq(&bitmap->lock);
  1397. return;
  1398. }
  1399. if (!*bmc) {
  1400. struct page *page;
  1401. *bmc = 1 | (needed ? NEEDED_MASK : 0);
  1402. bitmap_count_page(bitmap, offset, 1);
  1403. page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
  1404. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1405. }
  1406. spin_unlock_irq(&bitmap->lock);
  1407. bitmap->allclean = 0;
  1408. }
  1409. /* dirty the memory and file bits for bitmap chunks "s" to "e" */
  1410. void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
  1411. {
  1412. unsigned long chunk;
  1413. for (chunk = s; chunk <= e; chunk++) {
  1414. sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
  1415. bitmap_set_memory_bits(bitmap, sec, 1);
  1416. bitmap_file_set_bit(bitmap, sec);
  1417. if (sec < bitmap->mddev->recovery_cp)
  1418. /* We are asserting that the array is dirty,
  1419. * so move the recovery_cp address back so
  1420. * that it is obvious that it is dirty
  1421. */
  1422. bitmap->mddev->recovery_cp = sec;
  1423. }
  1424. }
  1425. /*
  1426. * flush out any pending updates
  1427. */
  1428. void bitmap_flush(mddev_t *mddev)
  1429. {
  1430. struct bitmap *bitmap = mddev->bitmap;
  1431. long sleep;
  1432. if (!bitmap) /* there was no bitmap */
  1433. return;
  1434. /* run the daemon_work three time to ensure everything is flushed
  1435. * that can be
  1436. */
  1437. sleep = mddev->bitmap_info.daemon_sleep * 2;
  1438. bitmap->daemon_lastrun -= sleep;
  1439. bitmap_daemon_work(mddev);
  1440. bitmap->daemon_lastrun -= sleep;
  1441. bitmap_daemon_work(mddev);
  1442. bitmap->daemon_lastrun -= sleep;
  1443. bitmap_daemon_work(mddev);
  1444. bitmap_update_sb(bitmap);
  1445. }
  1446. /*
  1447. * free memory that was allocated
  1448. */
  1449. static void bitmap_free(struct bitmap *bitmap)
  1450. {
  1451. unsigned long k, pages;
  1452. struct bitmap_page *bp;
  1453. if (!bitmap) /* there was no bitmap */
  1454. return;
  1455. /* release the bitmap file and kill the daemon */
  1456. bitmap_file_put(bitmap);
  1457. bp = bitmap->bp;
  1458. pages = bitmap->pages;
  1459. /* free all allocated memory */
  1460. if (bp) /* deallocate the page memory */
  1461. for (k = 0; k < pages; k++)
  1462. if (bp[k].map && !bp[k].hijacked)
  1463. kfree(bp[k].map);
  1464. kfree(bp);
  1465. kfree(bitmap);
  1466. }
  1467. void bitmap_destroy(mddev_t *mddev)
  1468. {
  1469. struct bitmap *bitmap = mddev->bitmap;
  1470. if (!bitmap) /* there was no bitmap */
  1471. return;
  1472. mutex_lock(&mddev->bitmap_info.mutex);
  1473. mddev->bitmap = NULL; /* disconnect from the md device */
  1474. mutex_unlock(&mddev->bitmap_info.mutex);
  1475. if (mddev->thread)
  1476. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1477. if (bitmap->sysfs_can_clear)
  1478. sysfs_put(bitmap->sysfs_can_clear);
  1479. bitmap_free(bitmap);
  1480. }
  1481. /*
  1482. * initialize the bitmap structure
  1483. * if this returns an error, bitmap_destroy must be called to do clean up
  1484. */
  1485. int bitmap_create(mddev_t *mddev)
  1486. {
  1487. struct bitmap *bitmap;
  1488. sector_t blocks = mddev->resync_max_sectors;
  1489. unsigned long chunks;
  1490. unsigned long pages;
  1491. struct file *file = mddev->bitmap_info.file;
  1492. int err;
  1493. struct sysfs_dirent *bm = NULL;
  1494. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1495. if (!file
  1496. && !mddev->bitmap_info.offset
  1497. && !mddev->bitmap_info.log) /* bitmap disabled, nothing to do */
  1498. return 0;
  1499. BUG_ON(file && mddev->bitmap_info.offset);
  1500. BUG_ON(mddev->bitmap_info.offset && mddev->bitmap_info.log);
  1501. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1502. if (!bitmap)
  1503. return -ENOMEM;
  1504. spin_lock_init(&bitmap->lock);
  1505. atomic_set(&bitmap->pending_writes, 0);
  1506. init_waitqueue_head(&bitmap->write_wait);
  1507. init_waitqueue_head(&bitmap->overflow_wait);
  1508. init_waitqueue_head(&bitmap->behind_wait);
  1509. bitmap->mddev = mddev;
  1510. if (mddev->kobj.sd)
  1511. bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
  1512. if (bm) {
  1513. bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
  1514. sysfs_put(bm);
  1515. } else
  1516. bitmap->sysfs_can_clear = NULL;
  1517. bitmap->file = file;
  1518. if (file) {
  1519. get_file(file);
  1520. /* As future accesses to this file will use bmap,
  1521. * and bypass the page cache, we must sync the file
  1522. * first.
  1523. */
  1524. vfs_fsync(file, 1);
  1525. }
  1526. /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
  1527. if (!mddev->bitmap_info.external)
  1528. err = bitmap_read_sb(bitmap);
  1529. else {
  1530. err = 0;
  1531. if (mddev->bitmap_info.chunksize == 0 ||
  1532. mddev->bitmap_info.daemon_sleep == 0)
  1533. /* chunksize and time_base need to be
  1534. * set first. */
  1535. err = -EINVAL;
  1536. }
  1537. if (err)
  1538. goto error;
  1539. bitmap->daemon_lastrun = jiffies;
  1540. bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
  1541. /* now that chunksize and chunkshift are set, we can use these macros */
  1542. chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
  1543. CHUNK_BLOCK_SHIFT(bitmap);
  1544. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1545. BUG_ON(!pages);
  1546. bitmap->chunks = chunks;
  1547. bitmap->pages = pages;
  1548. bitmap->missing_pages = pages;
  1549. bitmap->counter_bits = COUNTER_BITS;
  1550. bitmap->syncchunk = ~0UL;
  1551. #ifdef INJECT_FATAL_FAULT_1
  1552. bitmap->bp = NULL;
  1553. #else
  1554. bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1555. #endif
  1556. err = -ENOMEM;
  1557. if (!bitmap->bp)
  1558. goto error;
  1559. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1560. pages, bmname(bitmap));
  1561. mddev->bitmap = bitmap;
  1562. return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
  1563. error:
  1564. bitmap_free(bitmap);
  1565. return err;
  1566. }
  1567. int bitmap_load(mddev_t *mddev)
  1568. {
  1569. int err = 0;
  1570. sector_t sector = 0;
  1571. struct bitmap *bitmap = mddev->bitmap;
  1572. if (!bitmap)
  1573. goto out;
  1574. /* Clear out old bitmap info first: Either there is none, or we
  1575. * are resuming after someone else has possibly changed things,
  1576. * so we should forget old cached info.
  1577. * All chunks should be clean, but some might need_sync.
  1578. */
  1579. while (sector < mddev->resync_max_sectors) {
  1580. sector_t blocks;
  1581. bitmap_start_sync(bitmap, sector, &blocks, 0);
  1582. sector += blocks;
  1583. }
  1584. bitmap_close_sync(bitmap);
  1585. if (mddev->bitmap_info.log) {
  1586. unsigned long i;
  1587. struct dm_dirty_log *log = mddev->bitmap_info.log;
  1588. for (i = 0; i < bitmap->chunks; i++)
  1589. if (!log->type->in_sync(log, i, 1))
  1590. bitmap_set_memory_bits(bitmap,
  1591. (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
  1592. 1);
  1593. } else {
  1594. sector_t start = 0;
  1595. if (mddev->degraded == 0
  1596. || bitmap->events_cleared == mddev->events)
  1597. /* no need to keep dirty bits to optimise a
  1598. * re-add of a missing device */
  1599. start = mddev->recovery_cp;
  1600. err = bitmap_init_from_disk(bitmap, start);
  1601. }
  1602. if (err)
  1603. goto out;
  1604. mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
  1605. md_wakeup_thread(mddev->thread);
  1606. bitmap_update_sb(bitmap);
  1607. if (bitmap->flags & BITMAP_WRITE_ERROR)
  1608. err = -EIO;
  1609. out:
  1610. return err;
  1611. }
  1612. EXPORT_SYMBOL_GPL(bitmap_load);
  1613. static ssize_t
  1614. location_show(mddev_t *mddev, char *page)
  1615. {
  1616. ssize_t len;
  1617. if (mddev->bitmap_info.file)
  1618. len = sprintf(page, "file");
  1619. else if (mddev->bitmap_info.offset)
  1620. len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
  1621. else
  1622. len = sprintf(page, "none");
  1623. len += sprintf(page+len, "\n");
  1624. return len;
  1625. }
  1626. static ssize_t
  1627. location_store(mddev_t *mddev, const char *buf, size_t len)
  1628. {
  1629. if (mddev->pers) {
  1630. if (!mddev->pers->quiesce)
  1631. return -EBUSY;
  1632. if (mddev->recovery || mddev->sync_thread)
  1633. return -EBUSY;
  1634. }
  1635. if (mddev->bitmap || mddev->bitmap_info.file ||
  1636. mddev->bitmap_info.offset) {
  1637. /* bitmap already configured. Only option is to clear it */
  1638. if (strncmp(buf, "none", 4) != 0)
  1639. return -EBUSY;
  1640. if (mddev->pers) {
  1641. mddev->pers->quiesce(mddev, 1);
  1642. bitmap_destroy(mddev);
  1643. mddev->pers->quiesce(mddev, 0);
  1644. }
  1645. mddev->bitmap_info.offset = 0;
  1646. if (mddev->bitmap_info.file) {
  1647. struct file *f = mddev->bitmap_info.file;
  1648. mddev->bitmap_info.file = NULL;
  1649. restore_bitmap_write_access(f);
  1650. fput(f);
  1651. }
  1652. } else {
  1653. /* No bitmap, OK to set a location */
  1654. long long offset;
  1655. if (strncmp(buf, "none", 4) == 0)
  1656. /* nothing to be done */;
  1657. else if (strncmp(buf, "file:", 5) == 0) {
  1658. /* Not supported yet */
  1659. return -EINVAL;
  1660. } else {
  1661. int rv;
  1662. if (buf[0] == '+')
  1663. rv = strict_strtoll(buf+1, 10, &offset);
  1664. else
  1665. rv = strict_strtoll(buf, 10, &offset);
  1666. if (rv)
  1667. return rv;
  1668. if (offset == 0)
  1669. return -EINVAL;
  1670. if (mddev->bitmap_info.external == 0 &&
  1671. mddev->major_version == 0 &&
  1672. offset != mddev->bitmap_info.default_offset)
  1673. return -EINVAL;
  1674. mddev->bitmap_info.offset = offset;
  1675. if (mddev->pers) {
  1676. mddev->pers->quiesce(mddev, 1);
  1677. rv = bitmap_create(mddev);
  1678. if (rv) {
  1679. bitmap_destroy(mddev);
  1680. mddev->bitmap_info.offset = 0;
  1681. }
  1682. mddev->pers->quiesce(mddev, 0);
  1683. if (rv)
  1684. return rv;
  1685. }
  1686. }
  1687. }
  1688. if (!mddev->external) {
  1689. /* Ensure new bitmap info is stored in
  1690. * metadata promptly.
  1691. */
  1692. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1693. md_wakeup_thread(mddev->thread);
  1694. }
  1695. return len;
  1696. }
  1697. static struct md_sysfs_entry bitmap_location =
  1698. __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
  1699. static ssize_t
  1700. timeout_show(mddev_t *mddev, char *page)
  1701. {
  1702. ssize_t len;
  1703. unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
  1704. unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
  1705. len = sprintf(page, "%lu", secs);
  1706. if (jifs)
  1707. len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
  1708. len += sprintf(page+len, "\n");
  1709. return len;
  1710. }
  1711. static ssize_t
  1712. timeout_store(mddev_t *mddev, const char *buf, size_t len)
  1713. {
  1714. /* timeout can be set at any time */
  1715. unsigned long timeout;
  1716. int rv = strict_strtoul_scaled(buf, &timeout, 4);
  1717. if (rv)
  1718. return rv;
  1719. /* just to make sure we don't overflow... */
  1720. if (timeout >= LONG_MAX / HZ)
  1721. return -EINVAL;
  1722. timeout = timeout * HZ / 10000;
  1723. if (timeout >= MAX_SCHEDULE_TIMEOUT)
  1724. timeout = MAX_SCHEDULE_TIMEOUT-1;
  1725. if (timeout < 1)
  1726. timeout = 1;
  1727. mddev->bitmap_info.daemon_sleep = timeout;
  1728. if (mddev->thread) {
  1729. /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
  1730. * the bitmap is all clean and we don't need to
  1731. * adjust the timeout right now
  1732. */
  1733. if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
  1734. mddev->thread->timeout = timeout;
  1735. md_wakeup_thread(mddev->thread);
  1736. }
  1737. }
  1738. return len;
  1739. }
  1740. static struct md_sysfs_entry bitmap_timeout =
  1741. __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
  1742. static ssize_t
  1743. backlog_show(mddev_t *mddev, char *page)
  1744. {
  1745. return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
  1746. }
  1747. static ssize_t
  1748. backlog_store(mddev_t *mddev, const char *buf, size_t len)
  1749. {
  1750. unsigned long backlog;
  1751. int rv = strict_strtoul(buf, 10, &backlog);
  1752. if (rv)
  1753. return rv;
  1754. if (backlog > COUNTER_MAX)
  1755. return -EINVAL;
  1756. mddev->bitmap_info.max_write_behind = backlog;
  1757. return len;
  1758. }
  1759. static struct md_sysfs_entry bitmap_backlog =
  1760. __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
  1761. static ssize_t
  1762. chunksize_show(mddev_t *mddev, char *page)
  1763. {
  1764. return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
  1765. }
  1766. static ssize_t
  1767. chunksize_store(mddev_t *mddev, const char *buf, size_t len)
  1768. {
  1769. /* Can only be changed when no bitmap is active */
  1770. int rv;
  1771. unsigned long csize;
  1772. if (mddev->bitmap)
  1773. return -EBUSY;
  1774. rv = strict_strtoul(buf, 10, &csize);
  1775. if (rv)
  1776. return rv;
  1777. if (csize < 512 ||
  1778. !is_power_of_2(csize))
  1779. return -EINVAL;
  1780. mddev->bitmap_info.chunksize = csize;
  1781. return len;
  1782. }
  1783. static struct md_sysfs_entry bitmap_chunksize =
  1784. __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
  1785. static ssize_t metadata_show(mddev_t *mddev, char *page)
  1786. {
  1787. return sprintf(page, "%s\n", (mddev->bitmap_info.external
  1788. ? "external" : "internal"));
  1789. }
  1790. static ssize_t metadata_store(mddev_t *mddev, const char *buf, size_t len)
  1791. {
  1792. if (mddev->bitmap ||
  1793. mddev->bitmap_info.file ||
  1794. mddev->bitmap_info.offset)
  1795. return -EBUSY;
  1796. if (strncmp(buf, "external", 8) == 0)
  1797. mddev->bitmap_info.external = 1;
  1798. else if (strncmp(buf, "internal", 8) == 0)
  1799. mddev->bitmap_info.external = 0;
  1800. else
  1801. return -EINVAL;
  1802. return len;
  1803. }
  1804. static struct md_sysfs_entry bitmap_metadata =
  1805. __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
  1806. static ssize_t can_clear_show(mddev_t *mddev, char *page)
  1807. {
  1808. int len;
  1809. if (mddev->bitmap)
  1810. len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
  1811. "false" : "true"));
  1812. else
  1813. len = sprintf(page, "\n");
  1814. return len;
  1815. }
  1816. static ssize_t can_clear_store(mddev_t *mddev, const char *buf, size_t len)
  1817. {
  1818. if (mddev->bitmap == NULL)
  1819. return -ENOENT;
  1820. if (strncmp(buf, "false", 5) == 0)
  1821. mddev->bitmap->need_sync = 1;
  1822. else if (strncmp(buf, "true", 4) == 0) {
  1823. if (mddev->degraded)
  1824. return -EBUSY;
  1825. mddev->bitmap->need_sync = 0;
  1826. } else
  1827. return -EINVAL;
  1828. return len;
  1829. }
  1830. static struct md_sysfs_entry bitmap_can_clear =
  1831. __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
  1832. static ssize_t
  1833. behind_writes_used_show(mddev_t *mddev, char *page)
  1834. {
  1835. if (mddev->bitmap == NULL)
  1836. return sprintf(page, "0\n");
  1837. return sprintf(page, "%lu\n",
  1838. mddev->bitmap->behind_writes_used);
  1839. }
  1840. static ssize_t
  1841. behind_writes_used_reset(mddev_t *mddev, const char *buf, size_t len)
  1842. {
  1843. if (mddev->bitmap)
  1844. mddev->bitmap->behind_writes_used = 0;
  1845. return len;
  1846. }
  1847. static struct md_sysfs_entry max_backlog_used =
  1848. __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
  1849. behind_writes_used_show, behind_writes_used_reset);
  1850. static struct attribute *md_bitmap_attrs[] = {
  1851. &bitmap_location.attr,
  1852. &bitmap_timeout.attr,
  1853. &bitmap_backlog.attr,
  1854. &bitmap_chunksize.attr,
  1855. &bitmap_metadata.attr,
  1856. &bitmap_can_clear.attr,
  1857. &max_backlog_used.attr,
  1858. NULL
  1859. };
  1860. struct attribute_group md_bitmap_group = {
  1861. .name = "bitmap",
  1862. .attrs = md_bitmap_attrs,
  1863. };