bitmap.c 56 KB

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