bitmap.c 43 KB

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