bitmap.c 41 KB

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