bitmap.c 42 KB

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