bitmap.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724
  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. * wait if count gets too high, wake when it drops to half.
  16. */
  17. #include <linux/blkdev.h>
  18. #include <linux/module.h>
  19. #include <linux/errno.h>
  20. #include <linux/slab.h>
  21. #include <linux/init.h>
  22. #include <linux/timer.h>
  23. #include <linux/sched.h>
  24. #include <linux/list.h>
  25. #include <linux/file.h>
  26. #include <linux/mount.h>
  27. #include <linux/buffer_head.h>
  28. #include "md.h"
  29. #include "bitmap.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. //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
  47. #define DPRINTK(x...) do { } while(0)
  48. #ifndef PRINTK
  49. # if DEBUG > 0
  50. # define PRINTK(x...) printk(KERN_DEBUG x)
  51. # else
  52. # define PRINTK(x...)
  53. # endif
  54. #endif
  55. static inline char * bmname(struct bitmap *bitmap)
  56. {
  57. return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
  58. }
  59. /*
  60. * just a placeholder - calls kmalloc for bitmap pages
  61. */
  62. static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
  63. {
  64. unsigned char *page;
  65. #ifdef INJECT_FAULTS_1
  66. page = NULL;
  67. #else
  68. page = kmalloc(PAGE_SIZE, GFP_NOIO);
  69. #endif
  70. if (!page)
  71. printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
  72. else
  73. PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
  74. bmname(bitmap), page);
  75. return page;
  76. }
  77. /*
  78. * for now just a placeholder -- just calls kfree for bitmap pages
  79. */
  80. static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
  81. {
  82. PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
  83. kfree(page);
  84. }
  85. /*
  86. * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
  87. *
  88. * 1) check to see if this page is allocated, if it's not then try to alloc
  89. * 2) if the alloc fails, set the page's hijacked flag so we'll use the
  90. * page pointer directly as a counter
  91. *
  92. * if we find our page, we increment the page's refcount so that it stays
  93. * allocated while we're using it
  94. */
  95. static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
  96. __releases(bitmap->lock)
  97. __acquires(bitmap->lock)
  98. {
  99. unsigned char *mappage;
  100. if (page >= bitmap->pages) {
  101. /* This can happen if bitmap_start_sync goes beyond
  102. * End-of-device while looking for a whole page.
  103. * It is harmless.
  104. */
  105. return -EINVAL;
  106. }
  107. if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
  108. return 0;
  109. if (bitmap->bp[page].map) /* page is already allocated, just return */
  110. return 0;
  111. if (!create)
  112. return -ENOENT;
  113. spin_unlock_irq(&bitmap->lock);
  114. /* this page has not been allocated yet */
  115. if ((mappage = bitmap_alloc_page(bitmap)) == 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. spin_lock_irq(&bitmap->lock);
  121. if (!bitmap->bp[page].map)
  122. bitmap->bp[page].hijacked = 1;
  123. goto out;
  124. }
  125. /* got a page */
  126. spin_lock_irq(&bitmap->lock);
  127. /* recheck the page */
  128. if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
  129. /* somebody beat us to getting the page */
  130. bitmap_free_page(bitmap, mappage);
  131. return 0;
  132. }
  133. /* no page was in place and we have one, so install it */
  134. memset(mappage, 0, PAGE_SIZE);
  135. bitmap->bp[page].map = mappage;
  136. bitmap->missing_pages--;
  137. out:
  138. return 0;
  139. }
  140. /* if page is completely empty, put it back on the free list, or dealloc it */
  141. /* if page was hijacked, unmark the flag so it might get alloced next time */
  142. /* Note: lock should be held when calling this */
  143. static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
  144. {
  145. char *ptr;
  146. if (bitmap->bp[page].count) /* page is still busy */
  147. return;
  148. /* page is no longer in use, it can be released */
  149. if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
  150. bitmap->bp[page].hijacked = 0;
  151. bitmap->bp[page].map = NULL;
  152. return;
  153. }
  154. /* normal case, free the page */
  155. #if 0
  156. /* actually ... let's not. We will probably need the page again exactly when
  157. * memory is tight and we are flusing to disk
  158. */
  159. return;
  160. #else
  161. ptr = bitmap->bp[page].map;
  162. bitmap->bp[page].map = NULL;
  163. bitmap->missing_pages++;
  164. bitmap_free_page(bitmap, ptr);
  165. return;
  166. #endif
  167. }
  168. /*
  169. * bitmap file handling - read and write the bitmap file and its superblock
  170. */
  171. /*
  172. * basic page I/O operations
  173. */
  174. /* IO operations when bitmap is stored near all superblocks */
  175. static struct page *read_sb_page(mddev_t *mddev, loff_t offset,
  176. struct page *page,
  177. unsigned long index, int size)
  178. {
  179. /* choose a good rdev and read the page from there */
  180. mdk_rdev_t *rdev;
  181. sector_t target;
  182. if (!page)
  183. page = alloc_page(GFP_KERNEL);
  184. if (!page)
  185. return ERR_PTR(-ENOMEM);
  186. list_for_each_entry(rdev, &mddev->disks, same_set) {
  187. if (! test_bit(In_sync, &rdev->flags)
  188. || test_bit(Faulty, &rdev->flags))
  189. continue;
  190. target = rdev->sb_start + offset + index * (PAGE_SIZE/512);
  191. if (sync_page_io(rdev->bdev, target,
  192. roundup(size, bdev_logical_block_size(rdev->bdev)),
  193. page, READ)) {
  194. page->index = index;
  195. attach_page_buffers(page, NULL); /* so that free_buffer will
  196. * quietly no-op */
  197. return page;
  198. }
  199. }
  200. return ERR_PTR(-EIO);
  201. }
  202. static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
  203. {
  204. /* Iterate the disks of an mddev, using rcu to protect access to the
  205. * linked list, and raising the refcount of devices we return to ensure
  206. * they don't disappear while in use.
  207. * As devices are only added or removed when raid_disk is < 0 and
  208. * nr_pending is 0 and In_sync is clear, the entries we return will
  209. * still be in the same position on the list when we re-enter
  210. * list_for_each_continue_rcu.
  211. */
  212. struct list_head *pos;
  213. rcu_read_lock();
  214. if (rdev == NULL)
  215. /* start at the beginning */
  216. pos = &mddev->disks;
  217. else {
  218. /* release the previous rdev and start from there. */
  219. rdev_dec_pending(rdev, mddev);
  220. pos = &rdev->same_set;
  221. }
  222. list_for_each_continue_rcu(pos, &mddev->disks) {
  223. rdev = list_entry(pos, mdk_rdev_t, same_set);
  224. if (rdev->raid_disk >= 0 &&
  225. !test_bit(Faulty, &rdev->flags)) {
  226. /* this is a usable devices */
  227. atomic_inc(&rdev->nr_pending);
  228. rcu_read_unlock();
  229. return rdev;
  230. }
  231. }
  232. rcu_read_unlock();
  233. return NULL;
  234. }
  235. static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
  236. {
  237. mdk_rdev_t *rdev = NULL;
  238. mddev_t *mddev = bitmap->mddev;
  239. while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
  240. int size = PAGE_SIZE;
  241. loff_t offset = mddev->bitmap_info.offset;
  242. if (page->index == bitmap->file_pages-1)
  243. size = roundup(bitmap->last_page_size,
  244. bdev_logical_block_size(rdev->bdev));
  245. /* Just make sure we aren't corrupting data or
  246. * metadata
  247. */
  248. if (mddev->external) {
  249. /* Bitmap could be anywhere. */
  250. if (rdev->sb_start + offset + (page->index *(PAGE_SIZE/512)) >
  251. rdev->data_offset &&
  252. rdev->sb_start + offset <
  253. rdev->data_offset + mddev->dev_sectors +
  254. (PAGE_SIZE/512))
  255. goto bad_alignment;
  256. } else if (offset < 0) {
  257. /* DATA BITMAP METADATA */
  258. if (offset
  259. + (long)(page->index * (PAGE_SIZE/512))
  260. + size/512 > 0)
  261. /* bitmap runs in to metadata */
  262. goto bad_alignment;
  263. if (rdev->data_offset + mddev->dev_sectors
  264. > rdev->sb_start + offset)
  265. /* data runs in to bitmap */
  266. goto bad_alignment;
  267. } else if (rdev->sb_start < rdev->data_offset) {
  268. /* METADATA BITMAP DATA */
  269. if (rdev->sb_start
  270. + offset
  271. + page->index*(PAGE_SIZE/512) + size/512
  272. > rdev->data_offset)
  273. /* bitmap runs in to data */
  274. goto bad_alignment;
  275. } else {
  276. /* DATA METADATA BITMAP - no problems */
  277. }
  278. md_super_write(mddev, rdev,
  279. rdev->sb_start + offset
  280. + page->index * (PAGE_SIZE/512),
  281. size,
  282. page);
  283. }
  284. if (wait)
  285. md_super_wait(mddev);
  286. return 0;
  287. bad_alignment:
  288. return -EINVAL;
  289. }
  290. static void bitmap_file_kick(struct bitmap *bitmap);
  291. /*
  292. * write out a page to a file
  293. */
  294. static void write_page(struct bitmap *bitmap, struct page *page, int wait)
  295. {
  296. struct buffer_head *bh;
  297. if (bitmap->file == NULL) {
  298. switch (write_sb_page(bitmap, page, wait)) {
  299. case -EINVAL:
  300. bitmap->flags |= BITMAP_WRITE_ERROR;
  301. }
  302. } else {
  303. bh = page_buffers(page);
  304. while (bh && bh->b_blocknr) {
  305. atomic_inc(&bitmap->pending_writes);
  306. set_buffer_locked(bh);
  307. set_buffer_mapped(bh);
  308. submit_bh(WRITE, bh);
  309. bh = bh->b_this_page;
  310. }
  311. if (wait) {
  312. wait_event(bitmap->write_wait,
  313. atomic_read(&bitmap->pending_writes)==0);
  314. }
  315. }
  316. if (bitmap->flags & BITMAP_WRITE_ERROR)
  317. bitmap_file_kick(bitmap);
  318. }
  319. static void end_bitmap_write(struct buffer_head *bh, int uptodate)
  320. {
  321. struct bitmap *bitmap = bh->b_private;
  322. unsigned long flags;
  323. if (!uptodate) {
  324. spin_lock_irqsave(&bitmap->lock, flags);
  325. bitmap->flags |= BITMAP_WRITE_ERROR;
  326. spin_unlock_irqrestore(&bitmap->lock, flags);
  327. }
  328. if (atomic_dec_and_test(&bitmap->pending_writes))
  329. wake_up(&bitmap->write_wait);
  330. }
  331. /* copied from buffer.c */
  332. static void
  333. __clear_page_buffers(struct page *page)
  334. {
  335. ClearPagePrivate(page);
  336. set_page_private(page, 0);
  337. page_cache_release(page);
  338. }
  339. static void free_buffers(struct page *page)
  340. {
  341. struct buffer_head *bh = page_buffers(page);
  342. while (bh) {
  343. struct buffer_head *next = bh->b_this_page;
  344. free_buffer_head(bh);
  345. bh = next;
  346. }
  347. __clear_page_buffers(page);
  348. put_page(page);
  349. }
  350. /* read a page from a file.
  351. * We both read the page, and attach buffers to the page to record the
  352. * address of each block (using bmap). These addresses will be used
  353. * to write the block later, completely bypassing the filesystem.
  354. * This usage is similar to how swap files are handled, and allows us
  355. * to write to a file with no concerns of memory allocation failing.
  356. */
  357. static struct page *read_page(struct file *file, unsigned long index,
  358. struct bitmap *bitmap,
  359. unsigned long count)
  360. {
  361. struct page *page = NULL;
  362. struct inode *inode = file->f_path.dentry->d_inode;
  363. struct buffer_head *bh;
  364. sector_t block;
  365. PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
  366. (unsigned long long)index << PAGE_SHIFT);
  367. page = alloc_page(GFP_KERNEL);
  368. if (!page)
  369. page = ERR_PTR(-ENOMEM);
  370. if (IS_ERR(page))
  371. goto out;
  372. bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
  373. if (!bh) {
  374. put_page(page);
  375. page = ERR_PTR(-ENOMEM);
  376. goto out;
  377. }
  378. attach_page_buffers(page, bh);
  379. block = index << (PAGE_SHIFT - inode->i_blkbits);
  380. while (bh) {
  381. if (count == 0)
  382. bh->b_blocknr = 0;
  383. else {
  384. bh->b_blocknr = bmap(inode, block);
  385. if (bh->b_blocknr == 0) {
  386. /* Cannot use this file! */
  387. free_buffers(page);
  388. page = ERR_PTR(-EINVAL);
  389. goto out;
  390. }
  391. bh->b_bdev = inode->i_sb->s_bdev;
  392. if (count < (1<<inode->i_blkbits))
  393. count = 0;
  394. else
  395. count -= (1<<inode->i_blkbits);
  396. bh->b_end_io = end_bitmap_write;
  397. bh->b_private = bitmap;
  398. atomic_inc(&bitmap->pending_writes);
  399. set_buffer_locked(bh);
  400. set_buffer_mapped(bh);
  401. submit_bh(READ, bh);
  402. }
  403. block++;
  404. bh = bh->b_this_page;
  405. }
  406. page->index = index;
  407. wait_event(bitmap->write_wait,
  408. atomic_read(&bitmap->pending_writes)==0);
  409. if (bitmap->flags & BITMAP_WRITE_ERROR) {
  410. free_buffers(page);
  411. page = ERR_PTR(-EIO);
  412. }
  413. out:
  414. if (IS_ERR(page))
  415. printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
  416. (int)PAGE_SIZE,
  417. (unsigned long long)index << PAGE_SHIFT,
  418. PTR_ERR(page));
  419. return page;
  420. }
  421. /*
  422. * bitmap file superblock operations
  423. */
  424. /* update the event counter and sync the superblock to disk */
  425. void bitmap_update_sb(struct bitmap *bitmap)
  426. {
  427. bitmap_super_t *sb;
  428. unsigned long flags;
  429. if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
  430. return;
  431. spin_lock_irqsave(&bitmap->lock, flags);
  432. if (!bitmap->sb_page) { /* no superblock */
  433. spin_unlock_irqrestore(&bitmap->lock, flags);
  434. return;
  435. }
  436. spin_unlock_irqrestore(&bitmap->lock, flags);
  437. sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
  438. sb->events = cpu_to_le64(bitmap->mddev->events);
  439. if (bitmap->mddev->events < bitmap->events_cleared) {
  440. /* rocking back to read-only */
  441. bitmap->events_cleared = bitmap->mddev->events;
  442. sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
  443. }
  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 = (bitmap_super_t *)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 = (bitmap_super_t *)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->daemon_lastrun = jiffies;
  546. bitmap->mddev->bitmap_info.max_write_behind = write_behind;
  547. bitmap->flags |= le32_to_cpu(sb->state);
  548. if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
  549. bitmap->flags |= BITMAP_HOSTENDIAN;
  550. bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
  551. if (sb->state & cpu_to_le32(BITMAP_STALE))
  552. bitmap->events_cleared = bitmap->mddev->events;
  553. err = 0;
  554. out:
  555. kunmap_atomic(sb, KM_USER0);
  556. if (err)
  557. bitmap_print_sb(bitmap);
  558. return err;
  559. }
  560. enum bitmap_mask_op {
  561. MASK_SET,
  562. MASK_UNSET
  563. };
  564. /* record the state of the bitmap in the superblock. Return the old value */
  565. static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
  566. enum bitmap_mask_op op)
  567. {
  568. bitmap_super_t *sb;
  569. unsigned long flags;
  570. int old;
  571. spin_lock_irqsave(&bitmap->lock, flags);
  572. if (!bitmap->sb_page) { /* can't set the state */
  573. spin_unlock_irqrestore(&bitmap->lock, flags);
  574. return 0;
  575. }
  576. spin_unlock_irqrestore(&bitmap->lock, flags);
  577. sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
  578. old = le32_to_cpu(sb->state) & bits;
  579. switch (op) {
  580. case MASK_SET: sb->state |= cpu_to_le32(bits);
  581. break;
  582. case MASK_UNSET: sb->state &= cpu_to_le32(~bits);
  583. break;
  584. default: BUG();
  585. }
  586. kunmap_atomic(sb, KM_USER0);
  587. return old;
  588. }
  589. /*
  590. * general bitmap file operations
  591. */
  592. /* calculate the index of the page that contains this bit */
  593. static inline unsigned long file_page_index(unsigned long chunk)
  594. {
  595. return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
  596. }
  597. /* calculate the (bit) offset of this bit within a page */
  598. static inline unsigned long file_page_offset(unsigned long chunk)
  599. {
  600. return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
  601. }
  602. /*
  603. * return a pointer to the page in the filemap that contains the given bit
  604. *
  605. * this lookup is complicated by the fact that the bitmap sb might be exactly
  606. * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
  607. * 0 or page 1
  608. */
  609. static inline struct page *filemap_get_page(struct bitmap *bitmap,
  610. unsigned long chunk)
  611. {
  612. if (file_page_index(chunk) >= bitmap->file_pages) return NULL;
  613. return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
  614. }
  615. static void bitmap_file_unmap(struct bitmap *bitmap)
  616. {
  617. struct page **map, *sb_page;
  618. unsigned long *attr;
  619. int pages;
  620. unsigned long flags;
  621. spin_lock_irqsave(&bitmap->lock, flags);
  622. map = bitmap->filemap;
  623. bitmap->filemap = NULL;
  624. attr = bitmap->filemap_attr;
  625. bitmap->filemap_attr = NULL;
  626. pages = bitmap->file_pages;
  627. bitmap->file_pages = 0;
  628. sb_page = bitmap->sb_page;
  629. bitmap->sb_page = NULL;
  630. spin_unlock_irqrestore(&bitmap->lock, flags);
  631. while (pages--)
  632. if (map[pages]->index != 0) /* 0 is sb_page, release it below */
  633. free_buffers(map[pages]);
  634. kfree(map);
  635. kfree(attr);
  636. if (sb_page)
  637. free_buffers(sb_page);
  638. }
  639. static void bitmap_file_put(struct bitmap *bitmap)
  640. {
  641. struct file *file;
  642. unsigned long flags;
  643. spin_lock_irqsave(&bitmap->lock, flags);
  644. file = bitmap->file;
  645. bitmap->file = NULL;
  646. spin_unlock_irqrestore(&bitmap->lock, flags);
  647. if (file)
  648. wait_event(bitmap->write_wait,
  649. atomic_read(&bitmap->pending_writes)==0);
  650. bitmap_file_unmap(bitmap);
  651. if (file) {
  652. struct inode *inode = file->f_path.dentry->d_inode;
  653. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  654. fput(file);
  655. }
  656. }
  657. /*
  658. * bitmap_file_kick - if an error occurs while manipulating the bitmap file
  659. * then it is no longer reliable, so we stop using it and we mark the file
  660. * as failed in the superblock
  661. */
  662. static void bitmap_file_kick(struct bitmap *bitmap)
  663. {
  664. char *path, *ptr = NULL;
  665. if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
  666. bitmap_update_sb(bitmap);
  667. if (bitmap->file) {
  668. path = kmalloc(PAGE_SIZE, GFP_KERNEL);
  669. if (path)
  670. ptr = d_path(&bitmap->file->f_path, path,
  671. PAGE_SIZE);
  672. printk(KERN_ALERT
  673. "%s: kicking failed bitmap file %s from array!\n",
  674. bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
  675. kfree(path);
  676. } else
  677. printk(KERN_ALERT
  678. "%s: disabling internal bitmap due to errors\n",
  679. bmname(bitmap));
  680. }
  681. bitmap_file_put(bitmap);
  682. return;
  683. }
  684. enum bitmap_page_attr {
  685. BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
  686. BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
  687. BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
  688. };
  689. static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
  690. enum bitmap_page_attr attr)
  691. {
  692. __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
  693. }
  694. static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
  695. enum bitmap_page_attr attr)
  696. {
  697. __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
  698. }
  699. static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
  700. enum bitmap_page_attr attr)
  701. {
  702. return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
  703. }
  704. /*
  705. * bitmap_file_set_bit -- called before performing a write to the md device
  706. * to set (and eventually sync) a particular bit in the bitmap file
  707. *
  708. * we set the bit immediately, then we record the page number so that
  709. * when an unplug occurs, we can flush the dirty pages out to disk
  710. */
  711. static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
  712. {
  713. unsigned long bit;
  714. struct page *page;
  715. void *kaddr;
  716. unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
  717. if (!bitmap->filemap) {
  718. return;
  719. }
  720. page = filemap_get_page(bitmap, chunk);
  721. if (!page) return;
  722. bit = file_page_offset(chunk);
  723. /* set the bit */
  724. kaddr = kmap_atomic(page, KM_USER0);
  725. if (bitmap->flags & BITMAP_HOSTENDIAN)
  726. set_bit(bit, kaddr);
  727. else
  728. ext2_set_bit(bit, kaddr);
  729. kunmap_atomic(kaddr, KM_USER0);
  730. PRINTK("set file bit %lu page %lu\n", bit, page->index);
  731. /* record page number so it gets flushed to disk when unplug occurs */
  732. set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  733. }
  734. /* this gets called when the md device is ready to unplug its underlying
  735. * (slave) device queues -- before we let any writes go down, we need to
  736. * sync the dirty pages of the bitmap file to disk */
  737. void bitmap_unplug(struct bitmap *bitmap)
  738. {
  739. unsigned long i, flags;
  740. int dirty, need_write;
  741. struct page *page;
  742. int wait = 0;
  743. if (!bitmap)
  744. return;
  745. /* look at each page to see if there are any set bits that need to be
  746. * flushed out to disk */
  747. for (i = 0; i < bitmap->file_pages; i++) {
  748. spin_lock_irqsave(&bitmap->lock, flags);
  749. if (!bitmap->filemap) {
  750. spin_unlock_irqrestore(&bitmap->lock, flags);
  751. return;
  752. }
  753. page = bitmap->filemap[i];
  754. dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  755. need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  756. clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  757. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  758. if (dirty)
  759. wait = 1;
  760. spin_unlock_irqrestore(&bitmap->lock, flags);
  761. if (dirty | need_write)
  762. write_page(bitmap, page, 0);
  763. }
  764. if (wait) { /* if any writes were performed, we need to wait on them */
  765. if (bitmap->file)
  766. wait_event(bitmap->write_wait,
  767. atomic_read(&bitmap->pending_writes)==0);
  768. else
  769. md_super_wait(bitmap->mddev);
  770. }
  771. if (bitmap->flags & BITMAP_WRITE_ERROR)
  772. bitmap_file_kick(bitmap);
  773. }
  774. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
  775. /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
  776. * the in-memory bitmap from the on-disk bitmap -- also, sets up the
  777. * memory mapping of the bitmap file
  778. * Special cases:
  779. * if there's no bitmap file, or if the bitmap file had been
  780. * previously kicked from the array, we mark all the bits as
  781. * 1's in order to cause a full resync.
  782. *
  783. * We ignore all bits for sectors that end earlier than 'start'.
  784. * This is used when reading an out-of-date bitmap...
  785. */
  786. static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
  787. {
  788. unsigned long i, chunks, index, oldindex, bit;
  789. struct page *page = NULL, *oldpage = NULL;
  790. unsigned long num_pages, bit_cnt = 0;
  791. struct file *file;
  792. unsigned long bytes, offset;
  793. int outofdate;
  794. int ret = -ENOSPC;
  795. void *paddr;
  796. chunks = bitmap->chunks;
  797. file = bitmap->file;
  798. BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
  799. #ifdef INJECT_FAULTS_3
  800. outofdate = 1;
  801. #else
  802. outofdate = bitmap->flags & BITMAP_STALE;
  803. #endif
  804. if (outofdate)
  805. printk(KERN_INFO "%s: bitmap file is out of date, doing full "
  806. "recovery\n", bmname(bitmap));
  807. bytes = (chunks + 7) / 8;
  808. num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
  809. if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
  810. printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
  811. bmname(bitmap),
  812. (unsigned long) i_size_read(file->f_mapping->host),
  813. bytes + sizeof(bitmap_super_t));
  814. goto err;
  815. }
  816. ret = -ENOMEM;
  817. bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  818. if (!bitmap->filemap)
  819. goto err;
  820. /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
  821. bitmap->filemap_attr = kzalloc(
  822. roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
  823. GFP_KERNEL);
  824. if (!bitmap->filemap_attr)
  825. goto err;
  826. oldindex = ~0L;
  827. for (i = 0; i < chunks; i++) {
  828. int b;
  829. index = file_page_index(i);
  830. bit = file_page_offset(i);
  831. if (index != oldindex) { /* this is a new page, read it in */
  832. int count;
  833. /* unmap the old page, we're done with it */
  834. if (index == num_pages-1)
  835. count = bytes + sizeof(bitmap_super_t)
  836. - index * PAGE_SIZE;
  837. else
  838. count = PAGE_SIZE;
  839. if (index == 0) {
  840. /*
  841. * if we're here then the superblock page
  842. * contains some bits (PAGE_SIZE != sizeof sb)
  843. * we've already read it in, so just use it
  844. */
  845. page = bitmap->sb_page;
  846. offset = sizeof(bitmap_super_t);
  847. if (!file)
  848. read_sb_page(bitmap->mddev,
  849. bitmap->mddev->bitmap_info.offset,
  850. page,
  851. index, count);
  852. } else if (file) {
  853. page = read_page(file, index, bitmap, count);
  854. offset = 0;
  855. } else {
  856. page = read_sb_page(bitmap->mddev,
  857. bitmap->mddev->bitmap_info.offset,
  858. NULL,
  859. index, count);
  860. offset = 0;
  861. }
  862. if (IS_ERR(page)) { /* read error */
  863. ret = PTR_ERR(page);
  864. goto err;
  865. }
  866. oldindex = index;
  867. oldpage = page;
  868. bitmap->filemap[bitmap->file_pages++] = page;
  869. bitmap->last_page_size = count;
  870. if (outofdate) {
  871. /*
  872. * if bitmap is out of date, dirty the
  873. * whole page and write it out
  874. */
  875. paddr = kmap_atomic(page, KM_USER0);
  876. memset(paddr + offset, 0xff,
  877. PAGE_SIZE - offset);
  878. kunmap_atomic(paddr, KM_USER0);
  879. write_page(bitmap, page, 1);
  880. ret = -EIO;
  881. if (bitmap->flags & BITMAP_WRITE_ERROR)
  882. goto err;
  883. }
  884. }
  885. paddr = kmap_atomic(page, KM_USER0);
  886. if (bitmap->flags & BITMAP_HOSTENDIAN)
  887. b = test_bit(bit, paddr);
  888. else
  889. b = ext2_test_bit(bit, paddr);
  890. kunmap_atomic(paddr, KM_USER0);
  891. if (b) {
  892. /* if the disk bit is set, set the memory bit */
  893. int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
  894. >= start);
  895. bitmap_set_memory_bits(bitmap,
  896. (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
  897. needed);
  898. bit_cnt++;
  899. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  900. }
  901. }
  902. /* everything went OK */
  903. ret = 0;
  904. bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
  905. if (bit_cnt) { /* Kick recovery if any bits were set */
  906. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  907. md_wakeup_thread(bitmap->mddev->thread);
  908. }
  909. printk(KERN_INFO "%s: bitmap initialized from disk: "
  910. "read %lu/%lu pages, set %lu bits\n",
  911. bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
  912. return 0;
  913. err:
  914. printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
  915. bmname(bitmap), ret);
  916. return ret;
  917. }
  918. void bitmap_write_all(struct bitmap *bitmap)
  919. {
  920. /* We don't actually write all bitmap blocks here,
  921. * just flag them as needing to be written
  922. */
  923. int i;
  924. for (i=0; i < bitmap->file_pages; i++)
  925. set_page_attr(bitmap, bitmap->filemap[i],
  926. BITMAP_PAGE_NEEDWRITE);
  927. }
  928. static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
  929. {
  930. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  931. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  932. bitmap->bp[page].count += inc;
  933. /*
  934. if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
  935. (unsigned long long)offset, inc, bitmap->bp[page].count);
  936. */
  937. bitmap_checkfree(bitmap, page);
  938. }
  939. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  940. sector_t offset, int *blocks,
  941. int create);
  942. /*
  943. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  944. * out to disk
  945. */
  946. void bitmap_daemon_work(mddev_t *mddev)
  947. {
  948. struct bitmap *bitmap;
  949. unsigned long j;
  950. unsigned long flags;
  951. struct page *page = NULL, *lastpage = NULL;
  952. int blocks;
  953. void *paddr;
  954. /* Use a mutex to guard daemon_work against
  955. * bitmap_destroy.
  956. */
  957. mutex_lock(&mddev->bitmap_info.mutex);
  958. bitmap = mddev->bitmap;
  959. if (bitmap == NULL) {
  960. mutex_unlock(&mddev->bitmap_info.mutex);
  961. return;
  962. }
  963. if (time_before(jiffies, bitmap->daemon_lastrun
  964. + bitmap->mddev->bitmap_info.daemon_sleep))
  965. goto done;
  966. bitmap->daemon_lastrun = jiffies;
  967. if (bitmap->allclean) {
  968. bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  969. goto done;
  970. }
  971. bitmap->allclean = 1;
  972. spin_lock_irqsave(&bitmap->lock, flags);
  973. for (j = 0; j < bitmap->chunks; j++) {
  974. bitmap_counter_t *bmc;
  975. if (!bitmap->filemap)
  976. /* error or shutdown */
  977. break;
  978. page = filemap_get_page(bitmap, j);
  979. if (page != lastpage) {
  980. /* skip this page unless it's marked as needing cleaning */
  981. if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
  982. int need_write = test_page_attr(bitmap, page,
  983. BITMAP_PAGE_NEEDWRITE);
  984. if (need_write)
  985. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  986. spin_unlock_irqrestore(&bitmap->lock, flags);
  987. if (need_write) {
  988. write_page(bitmap, page, 0);
  989. bitmap->allclean = 0;
  990. }
  991. spin_lock_irqsave(&bitmap->lock, flags);
  992. j |= (PAGE_BITS - 1);
  993. continue;
  994. }
  995. /* grab the new page, sync and release the old */
  996. if (lastpage != NULL) {
  997. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  998. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  999. spin_unlock_irqrestore(&bitmap->lock, flags);
  1000. write_page(bitmap, lastpage, 0);
  1001. } else {
  1002. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1003. spin_unlock_irqrestore(&bitmap->lock, flags);
  1004. }
  1005. } else
  1006. spin_unlock_irqrestore(&bitmap->lock, flags);
  1007. lastpage = page;
  1008. /* We are possibly going to clear some bits, so make
  1009. * sure that events_cleared is up-to-date.
  1010. */
  1011. if (bitmap->need_sync) {
  1012. bitmap_super_t *sb;
  1013. bitmap->need_sync = 0;
  1014. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  1015. sb->events_cleared =
  1016. cpu_to_le64(bitmap->events_cleared);
  1017. kunmap_atomic(sb, KM_USER0);
  1018. write_page(bitmap, bitmap->sb_page, 1);
  1019. }
  1020. spin_lock_irqsave(&bitmap->lock, flags);
  1021. clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1022. }
  1023. bmc = bitmap_get_counter(bitmap,
  1024. (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
  1025. &blocks, 0);
  1026. if (bmc) {
  1027. /*
  1028. if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
  1029. */
  1030. if (*bmc)
  1031. bitmap->allclean = 0;
  1032. if (*bmc == 2) {
  1033. *bmc=1; /* maybe clear the bit next time */
  1034. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1035. } else if (*bmc == 1) {
  1036. /* we can clear the bit */
  1037. *bmc = 0;
  1038. bitmap_count_page(bitmap,
  1039. (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
  1040. -1);
  1041. /* clear the bit */
  1042. paddr = kmap_atomic(page, KM_USER0);
  1043. if (bitmap->flags & BITMAP_HOSTENDIAN)
  1044. clear_bit(file_page_offset(j), paddr);
  1045. else
  1046. ext2_clear_bit(file_page_offset(j), paddr);
  1047. kunmap_atomic(paddr, KM_USER0);
  1048. }
  1049. } else
  1050. j |= PAGE_COUNTER_MASK;
  1051. }
  1052. spin_unlock_irqrestore(&bitmap->lock, flags);
  1053. /* now sync the final page */
  1054. if (lastpage != NULL) {
  1055. spin_lock_irqsave(&bitmap->lock, flags);
  1056. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  1057. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1058. spin_unlock_irqrestore(&bitmap->lock, flags);
  1059. write_page(bitmap, lastpage, 0);
  1060. } else {
  1061. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1062. spin_unlock_irqrestore(&bitmap->lock, flags);
  1063. }
  1064. }
  1065. done:
  1066. if (bitmap->allclean == 0)
  1067. bitmap->mddev->thread->timeout =
  1068. bitmap->mddev->bitmap_info.daemon_sleep;
  1069. mutex_unlock(&mddev->bitmap_info.mutex);
  1070. }
  1071. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1072. sector_t offset, int *blocks,
  1073. int create)
  1074. __releases(bitmap->lock)
  1075. __acquires(bitmap->lock)
  1076. {
  1077. /* If 'create', we might release the lock and reclaim it.
  1078. * The lock must have been taken with interrupts enabled.
  1079. * If !create, we don't release the lock.
  1080. */
  1081. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  1082. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1083. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  1084. sector_t csize;
  1085. if (bitmap_checkpage(bitmap, page, create) < 0) {
  1086. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1087. *blocks = csize - (offset & (csize- 1));
  1088. return NULL;
  1089. }
  1090. /* now locked ... */
  1091. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  1092. /* should we use the first or second counter field
  1093. * of the hijacked pointer? */
  1094. int hi = (pageoff > PAGE_COUNTER_MASK);
  1095. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
  1096. PAGE_COUNTER_SHIFT - 1);
  1097. *blocks = csize - (offset & (csize- 1));
  1098. return &((bitmap_counter_t *)
  1099. &bitmap->bp[page].map)[hi];
  1100. } else { /* page is allocated */
  1101. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1102. *blocks = csize - (offset & (csize- 1));
  1103. return (bitmap_counter_t *)
  1104. &(bitmap->bp[page].map[pageoff]);
  1105. }
  1106. }
  1107. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
  1108. {
  1109. if (!bitmap) return 0;
  1110. if (behind) {
  1111. atomic_inc(&bitmap->behind_writes);
  1112. PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
  1113. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1114. }
  1115. while (sectors) {
  1116. int blocks;
  1117. bitmap_counter_t *bmc;
  1118. spin_lock_irq(&bitmap->lock);
  1119. bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
  1120. if (!bmc) {
  1121. spin_unlock_irq(&bitmap->lock);
  1122. return 0;
  1123. }
  1124. if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
  1125. DEFINE_WAIT(__wait);
  1126. /* note that it is safe to do the prepare_to_wait
  1127. * after the test as long as we do it before dropping
  1128. * the spinlock.
  1129. */
  1130. prepare_to_wait(&bitmap->overflow_wait, &__wait,
  1131. TASK_UNINTERRUPTIBLE);
  1132. spin_unlock_irq(&bitmap->lock);
  1133. blk_unplug(bitmap->mddev->queue);
  1134. schedule();
  1135. finish_wait(&bitmap->overflow_wait, &__wait);
  1136. continue;
  1137. }
  1138. switch(*bmc) {
  1139. case 0:
  1140. bitmap_file_set_bit(bitmap, offset);
  1141. bitmap_count_page(bitmap,offset, 1);
  1142. blk_plug_device_unlocked(bitmap->mddev->queue);
  1143. /* fall through */
  1144. case 1:
  1145. *bmc = 2;
  1146. }
  1147. (*bmc)++;
  1148. spin_unlock_irq(&bitmap->lock);
  1149. offset += blocks;
  1150. if (sectors > blocks)
  1151. sectors -= blocks;
  1152. else sectors = 0;
  1153. }
  1154. bitmap->allclean = 0;
  1155. return 0;
  1156. }
  1157. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1158. int success, int behind)
  1159. {
  1160. if (!bitmap) return;
  1161. if (behind) {
  1162. atomic_dec(&bitmap->behind_writes);
  1163. PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
  1164. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1165. }
  1166. if (bitmap->mddev->degraded)
  1167. /* Never clear bits or update events_cleared when degraded */
  1168. success = 0;
  1169. while (sectors) {
  1170. int blocks;
  1171. unsigned long flags;
  1172. bitmap_counter_t *bmc;
  1173. spin_lock_irqsave(&bitmap->lock, flags);
  1174. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1175. if (!bmc) {
  1176. spin_unlock_irqrestore(&bitmap->lock, flags);
  1177. return;
  1178. }
  1179. if (success &&
  1180. bitmap->events_cleared < bitmap->mddev->events) {
  1181. bitmap->events_cleared = bitmap->mddev->events;
  1182. bitmap->need_sync = 1;
  1183. }
  1184. if (!success && ! (*bmc & NEEDED_MASK))
  1185. *bmc |= NEEDED_MASK;
  1186. if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
  1187. wake_up(&bitmap->overflow_wait);
  1188. (*bmc)--;
  1189. if (*bmc <= 2) {
  1190. set_page_attr(bitmap,
  1191. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1192. BITMAP_PAGE_CLEAN);
  1193. }
  1194. spin_unlock_irqrestore(&bitmap->lock, flags);
  1195. offset += blocks;
  1196. if (sectors > blocks)
  1197. sectors -= blocks;
  1198. else sectors = 0;
  1199. }
  1200. }
  1201. static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
  1202. int degraded)
  1203. {
  1204. bitmap_counter_t *bmc;
  1205. int rv;
  1206. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1207. *blocks = 1024;
  1208. return 1; /* always resync if no bitmap */
  1209. }
  1210. spin_lock_irq(&bitmap->lock);
  1211. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1212. rv = 0;
  1213. if (bmc) {
  1214. /* locked */
  1215. if (RESYNC(*bmc))
  1216. rv = 1;
  1217. else if (NEEDED(*bmc)) {
  1218. rv = 1;
  1219. if (!degraded) { /* don't set/clear bits if degraded */
  1220. *bmc |= RESYNC_MASK;
  1221. *bmc &= ~NEEDED_MASK;
  1222. }
  1223. }
  1224. }
  1225. spin_unlock_irq(&bitmap->lock);
  1226. bitmap->allclean = 0;
  1227. return rv;
  1228. }
  1229. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
  1230. int degraded)
  1231. {
  1232. /* bitmap_start_sync must always report on multiples of whole
  1233. * pages, otherwise resync (which is very PAGE_SIZE based) will
  1234. * get confused.
  1235. * So call __bitmap_start_sync repeatedly (if needed) until
  1236. * At least PAGE_SIZE>>9 blocks are covered.
  1237. * Return the 'or' of the result.
  1238. */
  1239. int rv = 0;
  1240. int blocks1;
  1241. *blocks = 0;
  1242. while (*blocks < (PAGE_SIZE>>9)) {
  1243. rv |= __bitmap_start_sync(bitmap, offset,
  1244. &blocks1, degraded);
  1245. offset += blocks1;
  1246. *blocks += blocks1;
  1247. }
  1248. return rv;
  1249. }
  1250. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
  1251. {
  1252. bitmap_counter_t *bmc;
  1253. unsigned long flags;
  1254. /*
  1255. if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
  1256. */ if (bitmap == NULL) {
  1257. *blocks = 1024;
  1258. return;
  1259. }
  1260. spin_lock_irqsave(&bitmap->lock, flags);
  1261. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1262. if (bmc == NULL)
  1263. goto unlock;
  1264. /* locked */
  1265. /*
  1266. if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
  1267. */
  1268. if (RESYNC(*bmc)) {
  1269. *bmc &= ~RESYNC_MASK;
  1270. if (!NEEDED(*bmc) && aborted)
  1271. *bmc |= NEEDED_MASK;
  1272. else {
  1273. if (*bmc <= 2) {
  1274. set_page_attr(bitmap,
  1275. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1276. BITMAP_PAGE_CLEAN);
  1277. }
  1278. }
  1279. }
  1280. unlock:
  1281. spin_unlock_irqrestore(&bitmap->lock, flags);
  1282. bitmap->allclean = 0;
  1283. }
  1284. void bitmap_close_sync(struct bitmap *bitmap)
  1285. {
  1286. /* Sync has finished, and any bitmap chunks that weren't synced
  1287. * properly have been aborted. It remains to us to clear the
  1288. * RESYNC bit wherever it is still on
  1289. */
  1290. sector_t sector = 0;
  1291. int blocks;
  1292. if (!bitmap)
  1293. return;
  1294. while (sector < bitmap->mddev->resync_max_sectors) {
  1295. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1296. sector += blocks;
  1297. }
  1298. }
  1299. void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
  1300. {
  1301. sector_t s = 0;
  1302. int blocks;
  1303. if (!bitmap)
  1304. return;
  1305. if (sector == 0) {
  1306. bitmap->last_end_sync = jiffies;
  1307. return;
  1308. }
  1309. if (time_before(jiffies, (bitmap->last_end_sync
  1310. + bitmap->mddev->bitmap_info.daemon_sleep)))
  1311. return;
  1312. wait_event(bitmap->mddev->recovery_wait,
  1313. atomic_read(&bitmap->mddev->recovery_active) == 0);
  1314. bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync;
  1315. set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
  1316. sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
  1317. s = 0;
  1318. while (s < sector && s < bitmap->mddev->resync_max_sectors) {
  1319. bitmap_end_sync(bitmap, s, &blocks, 0);
  1320. s += blocks;
  1321. }
  1322. bitmap->last_end_sync = jiffies;
  1323. sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
  1324. }
  1325. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1326. {
  1327. /* For each chunk covered by any of these sectors, set the
  1328. * counter to 1 and set resync_needed. They should all
  1329. * be 0 at this point
  1330. */
  1331. int secs;
  1332. bitmap_counter_t *bmc;
  1333. spin_lock_irq(&bitmap->lock);
  1334. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1335. if (!bmc) {
  1336. spin_unlock_irq(&bitmap->lock);
  1337. return;
  1338. }
  1339. if (! *bmc) {
  1340. struct page *page;
  1341. *bmc = 1 | (needed?NEEDED_MASK:0);
  1342. bitmap_count_page(bitmap, offset, 1);
  1343. page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
  1344. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1345. }
  1346. spin_unlock_irq(&bitmap->lock);
  1347. bitmap->allclean = 0;
  1348. }
  1349. /* dirty the memory and file bits for bitmap chunks "s" to "e" */
  1350. void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
  1351. {
  1352. unsigned long chunk;
  1353. for (chunk = s; chunk <= e; chunk++) {
  1354. sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
  1355. bitmap_set_memory_bits(bitmap, sec, 1);
  1356. bitmap_file_set_bit(bitmap, sec);
  1357. }
  1358. }
  1359. /*
  1360. * flush out any pending updates
  1361. */
  1362. void bitmap_flush(mddev_t *mddev)
  1363. {
  1364. struct bitmap *bitmap = mddev->bitmap;
  1365. long sleep;
  1366. if (!bitmap) /* there was no bitmap */
  1367. return;
  1368. /* run the daemon_work three time to ensure everything is flushed
  1369. * that can be
  1370. */
  1371. sleep = mddev->bitmap_info.daemon_sleep * 2;
  1372. bitmap->daemon_lastrun -= sleep;
  1373. bitmap_daemon_work(mddev);
  1374. bitmap->daemon_lastrun -= sleep;
  1375. bitmap_daemon_work(mddev);
  1376. bitmap->daemon_lastrun -= sleep;
  1377. bitmap_daemon_work(mddev);
  1378. bitmap_update_sb(bitmap);
  1379. }
  1380. /*
  1381. * free memory that was allocated
  1382. */
  1383. static void bitmap_free(struct bitmap *bitmap)
  1384. {
  1385. unsigned long k, pages;
  1386. struct bitmap_page *bp;
  1387. if (!bitmap) /* there was no bitmap */
  1388. return;
  1389. /* release the bitmap file and kill the daemon */
  1390. bitmap_file_put(bitmap);
  1391. bp = bitmap->bp;
  1392. pages = bitmap->pages;
  1393. /* free all allocated memory */
  1394. if (bp) /* deallocate the page memory */
  1395. for (k = 0; k < pages; k++)
  1396. if (bp[k].map && !bp[k].hijacked)
  1397. kfree(bp[k].map);
  1398. kfree(bp);
  1399. kfree(bitmap);
  1400. }
  1401. void bitmap_destroy(mddev_t *mddev)
  1402. {
  1403. struct bitmap *bitmap = mddev->bitmap;
  1404. if (!bitmap) /* there was no bitmap */
  1405. return;
  1406. mutex_lock(&mddev->bitmap_info.mutex);
  1407. mddev->bitmap = NULL; /* disconnect from the md device */
  1408. mutex_unlock(&mddev->bitmap_info.mutex);
  1409. if (mddev->thread)
  1410. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1411. bitmap_free(bitmap);
  1412. }
  1413. /*
  1414. * initialize the bitmap structure
  1415. * if this returns an error, bitmap_destroy must be called to do clean up
  1416. */
  1417. int bitmap_create(mddev_t *mddev)
  1418. {
  1419. struct bitmap *bitmap;
  1420. sector_t blocks = mddev->resync_max_sectors;
  1421. unsigned long chunks;
  1422. unsigned long pages;
  1423. struct file *file = mddev->bitmap_info.file;
  1424. int err;
  1425. sector_t start;
  1426. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1427. if (!file && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
  1428. return 0;
  1429. BUG_ON(file && mddev->bitmap_info.offset);
  1430. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1431. if (!bitmap)
  1432. return -ENOMEM;
  1433. spin_lock_init(&bitmap->lock);
  1434. atomic_set(&bitmap->pending_writes, 0);
  1435. init_waitqueue_head(&bitmap->write_wait);
  1436. init_waitqueue_head(&bitmap->overflow_wait);
  1437. bitmap->mddev = mddev;
  1438. bitmap->file = file;
  1439. if (file) {
  1440. get_file(file);
  1441. /* As future accesses to this file will use bmap,
  1442. * and bypass the page cache, we must sync the file
  1443. * first.
  1444. */
  1445. vfs_fsync(file, file->f_dentry, 1);
  1446. }
  1447. /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
  1448. err = bitmap_read_sb(bitmap);
  1449. if (err)
  1450. goto error;
  1451. bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
  1452. /* now that chunksize and chunkshift are set, we can use these macros */
  1453. chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
  1454. CHUNK_BLOCK_SHIFT(bitmap);
  1455. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1456. BUG_ON(!pages);
  1457. bitmap->chunks = chunks;
  1458. bitmap->pages = pages;
  1459. bitmap->missing_pages = pages;
  1460. bitmap->counter_bits = COUNTER_BITS;
  1461. bitmap->syncchunk = ~0UL;
  1462. #ifdef INJECT_FATAL_FAULT_1
  1463. bitmap->bp = NULL;
  1464. #else
  1465. bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1466. #endif
  1467. err = -ENOMEM;
  1468. if (!bitmap->bp)
  1469. goto error;
  1470. /* now that we have some pages available, initialize the in-memory
  1471. * bitmap from the on-disk bitmap */
  1472. start = 0;
  1473. if (mddev->degraded == 0
  1474. || bitmap->events_cleared == mddev->events)
  1475. /* no need to keep dirty bits to optimise a re-add of a missing device */
  1476. start = mddev->recovery_cp;
  1477. err = bitmap_init_from_disk(bitmap, start);
  1478. if (err)
  1479. goto error;
  1480. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1481. pages, bmname(bitmap));
  1482. mddev->bitmap = bitmap;
  1483. mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
  1484. md_wakeup_thread(mddev->thread);
  1485. bitmap_update_sb(bitmap);
  1486. return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
  1487. error:
  1488. bitmap_free(bitmap);
  1489. return err;
  1490. }
  1491. /* the bitmap API -- for raid personalities */
  1492. EXPORT_SYMBOL(bitmap_startwrite);
  1493. EXPORT_SYMBOL(bitmap_endwrite);
  1494. EXPORT_SYMBOL(bitmap_start_sync);
  1495. EXPORT_SYMBOL(bitmap_end_sync);
  1496. EXPORT_SYMBOL(bitmap_unplug);
  1497. EXPORT_SYMBOL(bitmap_close_sync);
  1498. EXPORT_SYMBOL(bitmap_cond_end_sync);