bitmap.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/slab.h>
  20. #include <linux/init.h>
  21. #include <linux/timer.h>
  22. #include <linux/sched.h>
  23. #include <linux/list.h>
  24. #include <linux/file.h>
  25. #include <linux/mount.h>
  26. #include <linux/buffer_head.h>
  27. #include <linux/raid/md.h>
  28. #include <linux/raid/bitmap.h>
  29. /* debug macros */
  30. #define DEBUG 0
  31. #if DEBUG
  32. /* these are for debugging purposes only! */
  33. /* define one and only one of these */
  34. #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
  35. #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
  36. #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
  37. #define INJECT_FAULTS_4 0 /* undef */
  38. #define INJECT_FAULTS_5 0 /* undef */
  39. #define INJECT_FAULTS_6 0
  40. /* if these are defined, the driver will fail! debug only */
  41. #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
  42. #define INJECT_FATAL_FAULT_2 0 /* undef */
  43. #define INJECT_FATAL_FAULT_3 0 /* undef */
  44. #endif
  45. //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
  46. #define DPRINTK(x...) do { } while(0)
  47. #ifndef PRINTK
  48. # if DEBUG > 0
  49. # define PRINTK(x...) printk(KERN_DEBUG x)
  50. # else
  51. # define PRINTK(x...)
  52. # endif
  53. #endif
  54. static inline char * bmname(struct bitmap *bitmap)
  55. {
  56. return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
  57. }
  58. /*
  59. * just a placeholder - calls kmalloc for bitmap pages
  60. */
  61. static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
  62. {
  63. unsigned char *page;
  64. #ifdef INJECT_FAULTS_1
  65. page = NULL;
  66. #else
  67. page = kmalloc(PAGE_SIZE, GFP_NOIO);
  68. #endif
  69. if (!page)
  70. printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
  71. else
  72. PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
  73. bmname(bitmap), page);
  74. return page;
  75. }
  76. /*
  77. * for now just a placeholder -- just calls kfree for bitmap pages
  78. */
  79. static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
  80. {
  81. PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
  82. kfree(page);
  83. }
  84. /*
  85. * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
  86. *
  87. * 1) check to see if this page is allocated, if it's not then try to alloc
  88. * 2) if the alloc fails, set the page's hijacked flag so we'll use the
  89. * page pointer directly as a counter
  90. *
  91. * if we find our page, we increment the page's refcount so that it stays
  92. * allocated while we're using it
  93. */
  94. static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
  95. {
  96. unsigned char *mappage;
  97. if (page >= bitmap->pages) {
  98. printk(KERN_ALERT
  99. "%s: invalid bitmap page request: %lu (> %lu)\n",
  100. bmname(bitmap), page, bitmap->pages-1);
  101. return -EINVAL;
  102. }
  103. if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
  104. return 0;
  105. if (bitmap->bp[page].map) /* page is already allocated, just return */
  106. return 0;
  107. if (!create)
  108. return -ENOENT;
  109. spin_unlock_irq(&bitmap->lock);
  110. /* this page has not been allocated yet */
  111. if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
  112. PRINTK("%s: bitmap map page allocation failed, hijacking\n",
  113. bmname(bitmap));
  114. /* failed - set the hijacked flag so that we can use the
  115. * pointer as a counter */
  116. spin_lock_irq(&bitmap->lock);
  117. if (!bitmap->bp[page].map)
  118. bitmap->bp[page].hijacked = 1;
  119. goto out;
  120. }
  121. /* got a page */
  122. spin_lock_irq(&bitmap->lock);
  123. /* recheck the page */
  124. if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
  125. /* somebody beat us to getting the page */
  126. bitmap_free_page(bitmap, mappage);
  127. return 0;
  128. }
  129. /* no page was in place and we have one, so install it */
  130. memset(mappage, 0, PAGE_SIZE);
  131. bitmap->bp[page].map = mappage;
  132. bitmap->missing_pages--;
  133. out:
  134. return 0;
  135. }
  136. /* if page is completely empty, put it back on the free list, or dealloc it */
  137. /* if page was hijacked, unmark the flag so it might get alloced next time */
  138. /* Note: lock should be held when calling this */
  139. static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
  140. {
  141. char *ptr;
  142. if (bitmap->bp[page].count) /* page is still busy */
  143. return;
  144. /* page is no longer in use, it can be released */
  145. if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
  146. bitmap->bp[page].hijacked = 0;
  147. bitmap->bp[page].map = NULL;
  148. return;
  149. }
  150. /* normal case, free the page */
  151. #if 0
  152. /* actually ... let's not. We will probably need the page again exactly when
  153. * memory is tight and we are flusing to disk
  154. */
  155. return;
  156. #else
  157. ptr = bitmap->bp[page].map;
  158. bitmap->bp[page].map = NULL;
  159. bitmap->missing_pages++;
  160. bitmap_free_page(bitmap, ptr);
  161. return;
  162. #endif
  163. }
  164. /*
  165. * bitmap file handling - read and write the bitmap file and its superblock
  166. */
  167. /*
  168. * basic page I/O operations
  169. */
  170. /* IO operations when bitmap is stored near all superblocks */
  171. static struct page *read_sb_page(mddev_t *mddev, long offset,
  172. struct page *page,
  173. unsigned long index, int size)
  174. {
  175. /* choose a good rdev and read the page from there */
  176. mdk_rdev_t *rdev;
  177. struct list_head *tmp;
  178. sector_t target;
  179. if (!page)
  180. page = alloc_page(GFP_KERNEL);
  181. if (!page)
  182. return ERR_PTR(-ENOMEM);
  183. rdev_for_each(rdev, tmp, mddev) {
  184. if (! test_bit(In_sync, &rdev->flags)
  185. || test_bit(Faulty, &rdev->flags))
  186. continue;
  187. target = rdev->sb_start + offset + index * (PAGE_SIZE/512);
  188. if (sync_page_io(rdev->bdev, target,
  189. roundup(size, bdev_hardsect_size(rdev->bdev)),
  190. page, READ)) {
  191. page->index = index;
  192. attach_page_buffers(page, NULL); /* so that free_buffer will
  193. * quietly no-op */
  194. return page;
  195. }
  196. }
  197. return ERR_PTR(-EIO);
  198. }
  199. static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
  200. {
  201. /* Iterate the disks of an mddev, using rcu to protect access to the
  202. * linked list, and raising the refcount of devices we return to ensure
  203. * they don't disappear while in use.
  204. * As devices are only added or removed when raid_disk is < 0 and
  205. * nr_pending is 0 and In_sync is clear, the entries we return will
  206. * still be in the same position on the list when we re-enter
  207. * list_for_each_continue_rcu.
  208. */
  209. struct list_head *pos;
  210. rcu_read_lock();
  211. if (rdev == NULL)
  212. /* start at the beginning */
  213. pos = &mddev->disks;
  214. else {
  215. /* release the previous rdev and start from there. */
  216. rdev_dec_pending(rdev, mddev);
  217. pos = &rdev->same_set;
  218. }
  219. list_for_each_continue_rcu(pos, &mddev->disks) {
  220. rdev = list_entry(pos, mdk_rdev_t, same_set);
  221. if (rdev->raid_disk >= 0 &&
  222. test_bit(In_sync, &rdev->flags) &&
  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_hardsect_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->size*2
  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 < PAGE_SIZE)
  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. read_sb_page(bitmap->mddev, bitmap->offset,
  837. page,
  838. index, count);
  839. } else if (file) {
  840. page = read_page(file, index, bitmap, count);
  841. offset = 0;
  842. } else {
  843. page = read_sb_page(bitmap->mddev, bitmap->offset,
  844. NULL,
  845. index, count);
  846. offset = 0;
  847. }
  848. if (IS_ERR(page)) { /* read error */
  849. ret = PTR_ERR(page);
  850. goto err;
  851. }
  852. oldindex = index;
  853. oldpage = page;
  854. if (outofdate) {
  855. /*
  856. * if bitmap is out of date, dirty the
  857. * whole page and write it out
  858. */
  859. paddr = kmap_atomic(page, KM_USER0);
  860. memset(paddr + offset, 0xff,
  861. PAGE_SIZE - offset);
  862. kunmap_atomic(paddr, KM_USER0);
  863. write_page(bitmap, page, 1);
  864. ret = -EIO;
  865. if (bitmap->flags & BITMAP_WRITE_ERROR) {
  866. /* release, page not in filemap yet */
  867. put_page(page);
  868. goto err;
  869. }
  870. }
  871. bitmap->filemap[bitmap->file_pages++] = page;
  872. bitmap->last_page_size = count;
  873. }
  874. paddr = kmap_atomic(page, KM_USER0);
  875. if (bitmap->flags & BITMAP_HOSTENDIAN)
  876. b = test_bit(bit, paddr);
  877. else
  878. b = ext2_test_bit(bit, paddr);
  879. kunmap_atomic(paddr, KM_USER0);
  880. if (b) {
  881. /* if the disk bit is set, set the memory bit */
  882. bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
  883. ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
  884. );
  885. bit_cnt++;
  886. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  887. }
  888. }
  889. /* everything went OK */
  890. ret = 0;
  891. bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
  892. if (bit_cnt) { /* Kick recovery if any bits were set */
  893. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  894. md_wakeup_thread(bitmap->mddev->thread);
  895. }
  896. printk(KERN_INFO "%s: bitmap initialized from disk: "
  897. "read %lu/%lu pages, set %lu bits\n",
  898. bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
  899. return 0;
  900. err:
  901. printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
  902. bmname(bitmap), ret);
  903. return ret;
  904. }
  905. void bitmap_write_all(struct bitmap *bitmap)
  906. {
  907. /* We don't actually write all bitmap blocks here,
  908. * just flag them as needing to be written
  909. */
  910. int i;
  911. for (i=0; i < bitmap->file_pages; i++)
  912. set_page_attr(bitmap, bitmap->filemap[i],
  913. BITMAP_PAGE_NEEDWRITE);
  914. }
  915. static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
  916. {
  917. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  918. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  919. bitmap->bp[page].count += inc;
  920. /*
  921. if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
  922. (unsigned long long)offset, inc, bitmap->bp[page].count);
  923. */
  924. bitmap_checkfree(bitmap, page);
  925. }
  926. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  927. sector_t offset, int *blocks,
  928. int create);
  929. /*
  930. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  931. * out to disk
  932. */
  933. void bitmap_daemon_work(struct bitmap *bitmap)
  934. {
  935. unsigned long j;
  936. unsigned long flags;
  937. struct page *page = NULL, *lastpage = NULL;
  938. int blocks;
  939. void *paddr;
  940. if (bitmap == NULL)
  941. return;
  942. if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
  943. goto done;
  944. bitmap->daemon_lastrun = jiffies;
  945. if (bitmap->allclean) {
  946. bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  947. return;
  948. }
  949. bitmap->allclean = 1;
  950. for (j = 0; j < bitmap->chunks; j++) {
  951. bitmap_counter_t *bmc;
  952. spin_lock_irqsave(&bitmap->lock, flags);
  953. if (!bitmap->filemap) {
  954. /* error or shutdown */
  955. spin_unlock_irqrestore(&bitmap->lock, flags);
  956. break;
  957. }
  958. page = filemap_get_page(bitmap, j);
  959. if (page != lastpage) {
  960. /* skip this page unless it's marked as needing cleaning */
  961. if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
  962. int need_write = test_page_attr(bitmap, page,
  963. BITMAP_PAGE_NEEDWRITE);
  964. if (need_write)
  965. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  966. spin_unlock_irqrestore(&bitmap->lock, flags);
  967. if (need_write) {
  968. write_page(bitmap, page, 0);
  969. bitmap->allclean = 0;
  970. }
  971. continue;
  972. }
  973. /* grab the new page, sync and release the old */
  974. if (lastpage != NULL) {
  975. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  976. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  977. spin_unlock_irqrestore(&bitmap->lock, flags);
  978. write_page(bitmap, lastpage, 0);
  979. } else {
  980. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  981. spin_unlock_irqrestore(&bitmap->lock, flags);
  982. }
  983. } else
  984. spin_unlock_irqrestore(&bitmap->lock, flags);
  985. lastpage = page;
  986. /* We are possibly going to clear some bits, so make
  987. * sure that events_cleared is up-to-date.
  988. */
  989. if (bitmap->need_sync) {
  990. bitmap_super_t *sb;
  991. bitmap->need_sync = 0;
  992. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  993. sb->events_cleared =
  994. cpu_to_le64(bitmap->events_cleared);
  995. kunmap_atomic(sb, KM_USER0);
  996. write_page(bitmap, bitmap->sb_page, 1);
  997. }
  998. spin_lock_irqsave(&bitmap->lock, flags);
  999. clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1000. }
  1001. bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
  1002. &blocks, 0);
  1003. if (bmc) {
  1004. /*
  1005. if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
  1006. */
  1007. if (*bmc)
  1008. bitmap->allclean = 0;
  1009. if (*bmc == 2) {
  1010. *bmc=1; /* maybe clear the bit next time */
  1011. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1012. } else if (*bmc == 1) {
  1013. /* we can clear the bit */
  1014. *bmc = 0;
  1015. bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
  1016. -1);
  1017. /* clear the bit */
  1018. paddr = kmap_atomic(page, KM_USER0);
  1019. if (bitmap->flags & BITMAP_HOSTENDIAN)
  1020. clear_bit(file_page_offset(j), paddr);
  1021. else
  1022. ext2_clear_bit(file_page_offset(j), paddr);
  1023. kunmap_atomic(paddr, KM_USER0);
  1024. }
  1025. }
  1026. spin_unlock_irqrestore(&bitmap->lock, flags);
  1027. }
  1028. /* now sync the final page */
  1029. if (lastpage != NULL) {
  1030. spin_lock_irqsave(&bitmap->lock, flags);
  1031. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  1032. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1033. spin_unlock_irqrestore(&bitmap->lock, flags);
  1034. write_page(bitmap, lastpage, 0);
  1035. } else {
  1036. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1037. spin_unlock_irqrestore(&bitmap->lock, flags);
  1038. }
  1039. }
  1040. done:
  1041. if (bitmap->allclean == 0)
  1042. bitmap->mddev->thread->timeout = bitmap->daemon_sleep * HZ;
  1043. }
  1044. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1045. sector_t offset, int *blocks,
  1046. int create)
  1047. {
  1048. /* If 'create', we might release the lock and reclaim it.
  1049. * The lock must have been taken with interrupts enabled.
  1050. * If !create, we don't release the lock.
  1051. */
  1052. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  1053. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1054. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  1055. sector_t csize;
  1056. if (bitmap_checkpage(bitmap, page, create) < 0) {
  1057. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1058. *blocks = csize - (offset & (csize- 1));
  1059. return NULL;
  1060. }
  1061. /* now locked ... */
  1062. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  1063. /* should we use the first or second counter field
  1064. * of the hijacked pointer? */
  1065. int hi = (pageoff > PAGE_COUNTER_MASK);
  1066. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
  1067. PAGE_COUNTER_SHIFT - 1);
  1068. *blocks = csize - (offset & (csize- 1));
  1069. return &((bitmap_counter_t *)
  1070. &bitmap->bp[page].map)[hi];
  1071. } else { /* page is allocated */
  1072. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1073. *blocks = csize - (offset & (csize- 1));
  1074. return (bitmap_counter_t *)
  1075. &(bitmap->bp[page].map[pageoff]);
  1076. }
  1077. }
  1078. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
  1079. {
  1080. if (!bitmap) return 0;
  1081. if (behind) {
  1082. atomic_inc(&bitmap->behind_writes);
  1083. PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
  1084. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1085. }
  1086. while (sectors) {
  1087. int blocks;
  1088. bitmap_counter_t *bmc;
  1089. spin_lock_irq(&bitmap->lock);
  1090. bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
  1091. if (!bmc) {
  1092. spin_unlock_irq(&bitmap->lock);
  1093. return 0;
  1094. }
  1095. if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
  1096. DEFINE_WAIT(__wait);
  1097. /* note that it is safe to do the prepare_to_wait
  1098. * after the test as long as we do it before dropping
  1099. * the spinlock.
  1100. */
  1101. prepare_to_wait(&bitmap->overflow_wait, &__wait,
  1102. TASK_UNINTERRUPTIBLE);
  1103. spin_unlock_irq(&bitmap->lock);
  1104. blk_unplug(bitmap->mddev->queue);
  1105. schedule();
  1106. finish_wait(&bitmap->overflow_wait, &__wait);
  1107. continue;
  1108. }
  1109. switch(*bmc) {
  1110. case 0:
  1111. bitmap_file_set_bit(bitmap, offset);
  1112. bitmap_count_page(bitmap,offset, 1);
  1113. blk_plug_device_unlocked(bitmap->mddev->queue);
  1114. /* fall through */
  1115. case 1:
  1116. *bmc = 2;
  1117. }
  1118. (*bmc)++;
  1119. spin_unlock_irq(&bitmap->lock);
  1120. offset += blocks;
  1121. if (sectors > blocks)
  1122. sectors -= blocks;
  1123. else sectors = 0;
  1124. }
  1125. bitmap->allclean = 0;
  1126. return 0;
  1127. }
  1128. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1129. int success, int behind)
  1130. {
  1131. if (!bitmap) return;
  1132. if (behind) {
  1133. atomic_dec(&bitmap->behind_writes);
  1134. PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
  1135. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1136. }
  1137. while (sectors) {
  1138. int blocks;
  1139. unsigned long flags;
  1140. bitmap_counter_t *bmc;
  1141. spin_lock_irqsave(&bitmap->lock, flags);
  1142. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1143. if (!bmc) {
  1144. spin_unlock_irqrestore(&bitmap->lock, flags);
  1145. return;
  1146. }
  1147. if (success &&
  1148. bitmap->events_cleared < bitmap->mddev->events) {
  1149. bitmap->events_cleared = bitmap->mddev->events;
  1150. bitmap->need_sync = 1;
  1151. }
  1152. if (!success && ! (*bmc & NEEDED_MASK))
  1153. *bmc |= NEEDED_MASK;
  1154. if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
  1155. wake_up(&bitmap->overflow_wait);
  1156. (*bmc)--;
  1157. if (*bmc <= 2) {
  1158. set_page_attr(bitmap,
  1159. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1160. BITMAP_PAGE_CLEAN);
  1161. }
  1162. spin_unlock_irqrestore(&bitmap->lock, flags);
  1163. offset += blocks;
  1164. if (sectors > blocks)
  1165. sectors -= blocks;
  1166. else sectors = 0;
  1167. }
  1168. }
  1169. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
  1170. int degraded)
  1171. {
  1172. bitmap_counter_t *bmc;
  1173. int rv;
  1174. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1175. *blocks = 1024;
  1176. return 1; /* always resync if no bitmap */
  1177. }
  1178. spin_lock_irq(&bitmap->lock);
  1179. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1180. rv = 0;
  1181. if (bmc) {
  1182. /* locked */
  1183. if (RESYNC(*bmc))
  1184. rv = 1;
  1185. else if (NEEDED(*bmc)) {
  1186. rv = 1;
  1187. if (!degraded) { /* don't set/clear bits if degraded */
  1188. *bmc |= RESYNC_MASK;
  1189. *bmc &= ~NEEDED_MASK;
  1190. }
  1191. }
  1192. }
  1193. spin_unlock_irq(&bitmap->lock);
  1194. bitmap->allclean = 0;
  1195. return rv;
  1196. }
  1197. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
  1198. {
  1199. bitmap_counter_t *bmc;
  1200. unsigned long flags;
  1201. /*
  1202. if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
  1203. */ if (bitmap == NULL) {
  1204. *blocks = 1024;
  1205. return;
  1206. }
  1207. spin_lock_irqsave(&bitmap->lock, flags);
  1208. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1209. if (bmc == NULL)
  1210. goto unlock;
  1211. /* locked */
  1212. /*
  1213. if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
  1214. */
  1215. if (RESYNC(*bmc)) {
  1216. *bmc &= ~RESYNC_MASK;
  1217. if (!NEEDED(*bmc) && aborted)
  1218. *bmc |= NEEDED_MASK;
  1219. else {
  1220. if (*bmc <= 2) {
  1221. set_page_attr(bitmap,
  1222. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1223. BITMAP_PAGE_CLEAN);
  1224. }
  1225. }
  1226. }
  1227. unlock:
  1228. spin_unlock_irqrestore(&bitmap->lock, flags);
  1229. bitmap->allclean = 0;
  1230. }
  1231. void bitmap_close_sync(struct bitmap *bitmap)
  1232. {
  1233. /* Sync has finished, and any bitmap chunks that weren't synced
  1234. * properly have been aborted. It remains to us to clear the
  1235. * RESYNC bit wherever it is still on
  1236. */
  1237. sector_t sector = 0;
  1238. int blocks;
  1239. if (!bitmap)
  1240. return;
  1241. while (sector < bitmap->mddev->resync_max_sectors) {
  1242. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1243. sector += blocks;
  1244. }
  1245. }
  1246. void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
  1247. {
  1248. sector_t s = 0;
  1249. int blocks;
  1250. if (!bitmap)
  1251. return;
  1252. if (sector == 0) {
  1253. bitmap->last_end_sync = jiffies;
  1254. return;
  1255. }
  1256. if (time_before(jiffies, (bitmap->last_end_sync
  1257. + bitmap->daemon_sleep * HZ)))
  1258. return;
  1259. wait_event(bitmap->mddev->recovery_wait,
  1260. atomic_read(&bitmap->mddev->recovery_active) == 0);
  1261. sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
  1262. s = 0;
  1263. while (s < sector && s < bitmap->mddev->resync_max_sectors) {
  1264. bitmap_end_sync(bitmap, s, &blocks, 0);
  1265. s += blocks;
  1266. }
  1267. bitmap->last_end_sync = jiffies;
  1268. }
  1269. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1270. {
  1271. /* For each chunk covered by any of these sectors, set the
  1272. * counter to 1 and set resync_needed. They should all
  1273. * be 0 at this point
  1274. */
  1275. int secs;
  1276. bitmap_counter_t *bmc;
  1277. spin_lock_irq(&bitmap->lock);
  1278. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1279. if (!bmc) {
  1280. spin_unlock_irq(&bitmap->lock);
  1281. return;
  1282. }
  1283. if (! *bmc) {
  1284. struct page *page;
  1285. *bmc = 1 | (needed?NEEDED_MASK:0);
  1286. bitmap_count_page(bitmap, offset, 1);
  1287. page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
  1288. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1289. }
  1290. spin_unlock_irq(&bitmap->lock);
  1291. bitmap->allclean = 0;
  1292. }
  1293. /* dirty the memory and file bits for bitmap chunks "s" to "e" */
  1294. void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
  1295. {
  1296. unsigned long chunk;
  1297. for (chunk = s; chunk <= e; chunk++) {
  1298. sector_t sec = chunk << CHUNK_BLOCK_SHIFT(bitmap);
  1299. bitmap_set_memory_bits(bitmap, sec, 1);
  1300. bitmap_file_set_bit(bitmap, sec);
  1301. }
  1302. }
  1303. /*
  1304. * flush out any pending updates
  1305. */
  1306. void bitmap_flush(mddev_t *mddev)
  1307. {
  1308. struct bitmap *bitmap = mddev->bitmap;
  1309. int sleep;
  1310. if (!bitmap) /* there was no bitmap */
  1311. return;
  1312. /* run the daemon_work three time to ensure everything is flushed
  1313. * that can be
  1314. */
  1315. sleep = bitmap->daemon_sleep;
  1316. bitmap->daemon_sleep = 0;
  1317. bitmap_daemon_work(bitmap);
  1318. bitmap_daemon_work(bitmap);
  1319. bitmap_daemon_work(bitmap);
  1320. bitmap->daemon_sleep = sleep;
  1321. bitmap_update_sb(bitmap);
  1322. }
  1323. /*
  1324. * free memory that was allocated
  1325. */
  1326. static void bitmap_free(struct bitmap *bitmap)
  1327. {
  1328. unsigned long k, pages;
  1329. struct bitmap_page *bp;
  1330. if (!bitmap) /* there was no bitmap */
  1331. return;
  1332. /* release the bitmap file and kill the daemon */
  1333. bitmap_file_put(bitmap);
  1334. bp = bitmap->bp;
  1335. pages = bitmap->pages;
  1336. /* free all allocated memory */
  1337. if (bp) /* deallocate the page memory */
  1338. for (k = 0; k < pages; k++)
  1339. if (bp[k].map && !bp[k].hijacked)
  1340. kfree(bp[k].map);
  1341. kfree(bp);
  1342. kfree(bitmap);
  1343. }
  1344. void bitmap_destroy(mddev_t *mddev)
  1345. {
  1346. struct bitmap *bitmap = mddev->bitmap;
  1347. if (!bitmap) /* there was no bitmap */
  1348. return;
  1349. mddev->bitmap = NULL; /* disconnect from the md device */
  1350. if (mddev->thread)
  1351. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1352. bitmap_free(bitmap);
  1353. }
  1354. /*
  1355. * initialize the bitmap structure
  1356. * if this returns an error, bitmap_destroy must be called to do clean up
  1357. */
  1358. int bitmap_create(mddev_t *mddev)
  1359. {
  1360. struct bitmap *bitmap;
  1361. unsigned long blocks = mddev->resync_max_sectors;
  1362. unsigned long chunks;
  1363. unsigned long pages;
  1364. struct file *file = mddev->bitmap_file;
  1365. int err;
  1366. sector_t start;
  1367. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1368. if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
  1369. return 0;
  1370. BUG_ON(file && mddev->bitmap_offset);
  1371. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1372. if (!bitmap)
  1373. return -ENOMEM;
  1374. spin_lock_init(&bitmap->lock);
  1375. atomic_set(&bitmap->pending_writes, 0);
  1376. init_waitqueue_head(&bitmap->write_wait);
  1377. init_waitqueue_head(&bitmap->overflow_wait);
  1378. bitmap->mddev = mddev;
  1379. bitmap->file = file;
  1380. bitmap->offset = mddev->bitmap_offset;
  1381. if (file) {
  1382. get_file(file);
  1383. do_sync_mapping_range(file->f_mapping, 0, LLONG_MAX,
  1384. SYNC_FILE_RANGE_WAIT_BEFORE |
  1385. SYNC_FILE_RANGE_WRITE |
  1386. SYNC_FILE_RANGE_WAIT_AFTER);
  1387. }
  1388. /* read superblock from bitmap file (this sets bitmap->chunksize) */
  1389. err = bitmap_read_sb(bitmap);
  1390. if (err)
  1391. goto error;
  1392. bitmap->chunkshift = ffz(~bitmap->chunksize);
  1393. /* now that chunksize and chunkshift are set, we can use these macros */
  1394. chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
  1395. CHUNK_BLOCK_RATIO(bitmap);
  1396. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1397. BUG_ON(!pages);
  1398. bitmap->chunks = chunks;
  1399. bitmap->pages = pages;
  1400. bitmap->missing_pages = pages;
  1401. bitmap->counter_bits = COUNTER_BITS;
  1402. bitmap->syncchunk = ~0UL;
  1403. #ifdef INJECT_FATAL_FAULT_1
  1404. bitmap->bp = NULL;
  1405. #else
  1406. bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1407. #endif
  1408. err = -ENOMEM;
  1409. if (!bitmap->bp)
  1410. goto error;
  1411. /* now that we have some pages available, initialize the in-memory
  1412. * bitmap from the on-disk bitmap */
  1413. start = 0;
  1414. if (mddev->degraded == 0
  1415. || bitmap->events_cleared == mddev->events)
  1416. /* no need to keep dirty bits to optimise a re-add of a missing device */
  1417. start = mddev->recovery_cp;
  1418. err = bitmap_init_from_disk(bitmap, start);
  1419. if (err)
  1420. goto error;
  1421. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1422. pages, bmname(bitmap));
  1423. mddev->bitmap = bitmap;
  1424. mddev->thread->timeout = bitmap->daemon_sleep * HZ;
  1425. bitmap_update_sb(bitmap);
  1426. return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
  1427. error:
  1428. bitmap_free(bitmap);
  1429. return err;
  1430. }
  1431. /* the bitmap API -- for raid personalities */
  1432. EXPORT_SYMBOL(bitmap_startwrite);
  1433. EXPORT_SYMBOL(bitmap_endwrite);
  1434. EXPORT_SYMBOL(bitmap_start_sync);
  1435. EXPORT_SYMBOL(bitmap_end_sync);
  1436. EXPORT_SYMBOL(bitmap_unplug);
  1437. EXPORT_SYMBOL(bitmap_close_sync);
  1438. EXPORT_SYMBOL(bitmap_cond_end_sync);