bitmap.c 42 KB

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