bitmap.c 38 KB

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