bitmap.c 43 KB

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