bitmap.c 44 KB

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