bitmap.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  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/blkdev.h>
  18. #include <linux/module.h>
  19. #include <linux/errno.h>
  20. #include <linux/slab.h>
  21. #include <linux/init.h>
  22. #include <linux/timer.h>
  23. #include <linux/sched.h>
  24. #include <linux/list.h>
  25. #include <linux/file.h>
  26. #include <linux/mount.h>
  27. #include <linux/buffer_head.h>
  28. #include "md.h"
  29. #include "bitmap.h"
  30. /* debug macros */
  31. #define DEBUG 0
  32. #if DEBUG
  33. /* these are for debugging purposes only! */
  34. /* define one and only one of these */
  35. #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
  36. #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
  37. #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
  38. #define INJECT_FAULTS_4 0 /* undef */
  39. #define INJECT_FAULTS_5 0 /* undef */
  40. #define INJECT_FAULTS_6 0
  41. /* if these are defined, the driver will fail! debug only */
  42. #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
  43. #define INJECT_FATAL_FAULT_2 0 /* undef */
  44. #define INJECT_FATAL_FAULT_3 0 /* undef */
  45. #endif
  46. //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
  47. #define DPRINTK(x...) do { } while(0)
  48. #ifndef PRINTK
  49. # if DEBUG > 0
  50. # define PRINTK(x...) printk(KERN_DEBUG x)
  51. # else
  52. # define PRINTK(x...)
  53. # endif
  54. #endif
  55. static inline char * bmname(struct bitmap *bitmap)
  56. {
  57. return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
  58. }
  59. /*
  60. * just a placeholder - calls kmalloc for bitmap pages
  61. */
  62. static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
  63. {
  64. unsigned char *page;
  65. #ifdef INJECT_FAULTS_1
  66. page = NULL;
  67. #else
  68. page = kmalloc(PAGE_SIZE, GFP_NOIO);
  69. #endif
  70. if (!page)
  71. printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
  72. else
  73. PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
  74. bmname(bitmap), page);
  75. return page;
  76. }
  77. /*
  78. * for now just a placeholder -- just calls kfree for bitmap pages
  79. */
  80. static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
  81. {
  82. PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
  83. kfree(page);
  84. }
  85. /*
  86. * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
  87. *
  88. * 1) check to see if this page is allocated, if it's not then try to alloc
  89. * 2) if the alloc fails, set the page's hijacked flag so we'll use the
  90. * page pointer directly as a counter
  91. *
  92. * if we find our page, we increment the page's refcount so that it stays
  93. * allocated while we're using it
  94. */
  95. static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
  96. {
  97. unsigned char *mappage;
  98. if (page >= bitmap->pages) {
  99. /* This can happen if bitmap_start_sync goes beyond
  100. * End-of-device while looking for a whole page.
  101. * It is harmless.
  102. */
  103. return -EINVAL;
  104. }
  105. if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
  106. return 0;
  107. if (bitmap->bp[page].map) /* page is already allocated, just return */
  108. return 0;
  109. if (!create)
  110. return -ENOENT;
  111. spin_unlock_irq(&bitmap->lock);
  112. /* this page has not been allocated yet */
  113. if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
  114. PRINTK("%s: bitmap map page allocation failed, hijacking\n",
  115. bmname(bitmap));
  116. /* failed - set the hijacked flag so that we can use the
  117. * pointer as a counter */
  118. spin_lock_irq(&bitmap->lock);
  119. if (!bitmap->bp[page].map)
  120. bitmap->bp[page].hijacked = 1;
  121. goto out;
  122. }
  123. /* got a page */
  124. spin_lock_irq(&bitmap->lock);
  125. /* recheck the page */
  126. if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
  127. /* somebody beat us to getting the page */
  128. bitmap_free_page(bitmap, mappage);
  129. return 0;
  130. }
  131. /* no page was in place and we have one, so install it */
  132. memset(mappage, 0, PAGE_SIZE);
  133. bitmap->bp[page].map = mappage;
  134. bitmap->missing_pages--;
  135. out:
  136. return 0;
  137. }
  138. /* if page is completely empty, put it back on the free list, or dealloc it */
  139. /* if page was hijacked, unmark the flag so it might get alloced next time */
  140. /* Note: lock should be held when calling this */
  141. static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
  142. {
  143. char *ptr;
  144. if (bitmap->bp[page].count) /* page is still busy */
  145. return;
  146. /* page is no longer in use, it can be released */
  147. if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
  148. bitmap->bp[page].hijacked = 0;
  149. bitmap->bp[page].map = NULL;
  150. return;
  151. }
  152. /* normal case, free the page */
  153. #if 0
  154. /* actually ... let's not. We will probably need the page again exactly when
  155. * memory is tight and we are flusing to disk
  156. */
  157. return;
  158. #else
  159. ptr = bitmap->bp[page].map;
  160. bitmap->bp[page].map = NULL;
  161. bitmap->missing_pages++;
  162. bitmap_free_page(bitmap, ptr);
  163. return;
  164. #endif
  165. }
  166. /*
  167. * bitmap file handling - read and write the bitmap file and its superblock
  168. */
  169. /*
  170. * basic page I/O operations
  171. */
  172. /* IO operations when bitmap is stored near all superblocks */
  173. static struct page *read_sb_page(mddev_t *mddev, long offset,
  174. struct page *page,
  175. unsigned long index, int size)
  176. {
  177. /* choose a good rdev and read the page from there */
  178. mdk_rdev_t *rdev;
  179. sector_t target;
  180. if (!page)
  181. page = alloc_page(GFP_KERNEL);
  182. if (!page)
  183. return ERR_PTR(-ENOMEM);
  184. list_for_each_entry(rdev, &mddev->disks, same_set) {
  185. if (! test_bit(In_sync, &rdev->flags)
  186. || test_bit(Faulty, &rdev->flags))
  187. continue;
  188. target = rdev->sb_start + offset + index * (PAGE_SIZE/512);
  189. if (sync_page_io(rdev->bdev, target,
  190. roundup(size, bdev_hardsect_size(rdev->bdev)),
  191. page, READ)) {
  192. page->index = index;
  193. attach_page_buffers(page, NULL); /* so that free_buffer will
  194. * quietly no-op */
  195. return page;
  196. }
  197. }
  198. return ERR_PTR(-EIO);
  199. }
  200. static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
  201. {
  202. /* Iterate the disks of an mddev, using rcu to protect access to the
  203. * linked list, and raising the refcount of devices we return to ensure
  204. * they don't disappear while in use.
  205. * As devices are only added or removed when raid_disk is < 0 and
  206. * nr_pending is 0 and In_sync is clear, the entries we return will
  207. * still be in the same position on the list when we re-enter
  208. * list_for_each_continue_rcu.
  209. */
  210. struct list_head *pos;
  211. rcu_read_lock();
  212. if (rdev == NULL)
  213. /* start at the beginning */
  214. pos = &mddev->disks;
  215. else {
  216. /* release the previous rdev and start from there. */
  217. rdev_dec_pending(rdev, mddev);
  218. pos = &rdev->same_set;
  219. }
  220. list_for_each_continue_rcu(pos, &mddev->disks) {
  221. rdev = list_entry(pos, mdk_rdev_t, same_set);
  222. if (rdev->raid_disk >= 0 &&
  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->dev_sectors
  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 < 512)
  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. bitmap->filemap[bitmap->file_pages++] = page;
  857. bitmap->last_page_size = count;
  858. if (outofdate) {
  859. /*
  860. * if bitmap is out of date, dirty the
  861. * whole page and write it out
  862. */
  863. paddr = kmap_atomic(page, KM_USER0);
  864. memset(paddr + offset, 0xff,
  865. PAGE_SIZE - offset);
  866. kunmap_atomic(paddr, KM_USER0);
  867. write_page(bitmap, page, 1);
  868. ret = -EIO;
  869. if (bitmap->flags & BITMAP_WRITE_ERROR)
  870. goto err;
  871. }
  872. }
  873. paddr = kmap_atomic(page, KM_USER0);
  874. if (bitmap->flags & BITMAP_HOSTENDIAN)
  875. b = test_bit(bit, paddr);
  876. else
  877. b = ext2_test_bit(bit, paddr);
  878. kunmap_atomic(paddr, KM_USER0);
  879. if (b) {
  880. /* if the disk bit is set, set the memory bit */
  881. int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
  882. >= start);
  883. bitmap_set_memory_bits(bitmap,
  884. (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
  885. needed);
  886. bit_cnt++;
  887. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  888. }
  889. }
  890. /* everything went OK */
  891. ret = 0;
  892. bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
  893. if (bit_cnt) { /* Kick recovery if any bits were set */
  894. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  895. md_wakeup_thread(bitmap->mddev->thread);
  896. }
  897. printk(KERN_INFO "%s: bitmap initialized from disk: "
  898. "read %lu/%lu pages, set %lu bits\n",
  899. bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
  900. return 0;
  901. err:
  902. printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
  903. bmname(bitmap), ret);
  904. return ret;
  905. }
  906. void bitmap_write_all(struct bitmap *bitmap)
  907. {
  908. /* We don't actually write all bitmap blocks here,
  909. * just flag them as needing to be written
  910. */
  911. int i;
  912. for (i=0; i < bitmap->file_pages; i++)
  913. set_page_attr(bitmap, bitmap->filemap[i],
  914. BITMAP_PAGE_NEEDWRITE);
  915. }
  916. static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
  917. {
  918. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  919. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  920. bitmap->bp[page].count += inc;
  921. /*
  922. if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
  923. (unsigned long long)offset, inc, bitmap->bp[page].count);
  924. */
  925. bitmap_checkfree(bitmap, page);
  926. }
  927. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  928. sector_t offset, int *blocks,
  929. int create);
  930. /*
  931. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  932. * out to disk
  933. */
  934. void bitmap_daemon_work(struct bitmap *bitmap)
  935. {
  936. unsigned long j;
  937. unsigned long flags;
  938. struct page *page = NULL, *lastpage = NULL;
  939. int blocks;
  940. void *paddr;
  941. if (bitmap == NULL)
  942. return;
  943. if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
  944. goto done;
  945. bitmap->daemon_lastrun = jiffies;
  946. if (bitmap->allclean) {
  947. bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  948. return;
  949. }
  950. bitmap->allclean = 1;
  951. for (j = 0; j < bitmap->chunks; j++) {
  952. bitmap_counter_t *bmc;
  953. spin_lock_irqsave(&bitmap->lock, flags);
  954. if (!bitmap->filemap) {
  955. /* error or shutdown */
  956. spin_unlock_irqrestore(&bitmap->lock, flags);
  957. break;
  958. }
  959. page = filemap_get_page(bitmap, j);
  960. if (page != lastpage) {
  961. /* skip this page unless it's marked as needing cleaning */
  962. if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
  963. int need_write = test_page_attr(bitmap, page,
  964. BITMAP_PAGE_NEEDWRITE);
  965. if (need_write)
  966. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  967. spin_unlock_irqrestore(&bitmap->lock, flags);
  968. if (need_write) {
  969. write_page(bitmap, page, 0);
  970. bitmap->allclean = 0;
  971. }
  972. continue;
  973. }
  974. /* grab the new page, sync and release the old */
  975. if (lastpage != NULL) {
  976. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  977. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  978. spin_unlock_irqrestore(&bitmap->lock, flags);
  979. write_page(bitmap, lastpage, 0);
  980. } else {
  981. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  982. spin_unlock_irqrestore(&bitmap->lock, flags);
  983. }
  984. } else
  985. spin_unlock_irqrestore(&bitmap->lock, flags);
  986. lastpage = page;
  987. /* We are possibly going to clear some bits, so make
  988. * sure that events_cleared is up-to-date.
  989. */
  990. if (bitmap->need_sync) {
  991. bitmap_super_t *sb;
  992. bitmap->need_sync = 0;
  993. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  994. sb->events_cleared =
  995. cpu_to_le64(bitmap->events_cleared);
  996. kunmap_atomic(sb, KM_USER0);
  997. write_page(bitmap, bitmap->sb_page, 1);
  998. }
  999. spin_lock_irqsave(&bitmap->lock, flags);
  1000. clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1001. }
  1002. bmc = bitmap_get_counter(bitmap,
  1003. (sector_t)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,
  1018. (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
  1019. -1);
  1020. /* clear the bit */
  1021. paddr = kmap_atomic(page, KM_USER0);
  1022. if (bitmap->flags & BITMAP_HOSTENDIAN)
  1023. clear_bit(file_page_offset(j), paddr);
  1024. else
  1025. ext2_clear_bit(file_page_offset(j), paddr);
  1026. kunmap_atomic(paddr, KM_USER0);
  1027. }
  1028. }
  1029. spin_unlock_irqrestore(&bitmap->lock, flags);
  1030. }
  1031. /* now sync the final page */
  1032. if (lastpage != NULL) {
  1033. spin_lock_irqsave(&bitmap->lock, flags);
  1034. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  1035. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1036. spin_unlock_irqrestore(&bitmap->lock, flags);
  1037. write_page(bitmap, lastpage, 0);
  1038. } else {
  1039. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1040. spin_unlock_irqrestore(&bitmap->lock, flags);
  1041. }
  1042. }
  1043. done:
  1044. if (bitmap->allclean == 0)
  1045. bitmap->mddev->thread->timeout = bitmap->daemon_sleep * HZ;
  1046. }
  1047. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1048. sector_t offset, int *blocks,
  1049. int create)
  1050. {
  1051. /* If 'create', we might release the lock and reclaim it.
  1052. * The lock must have been taken with interrupts enabled.
  1053. * If !create, we don't release the lock.
  1054. */
  1055. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  1056. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1057. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  1058. sector_t csize;
  1059. if (bitmap_checkpage(bitmap, page, create) < 0) {
  1060. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1061. *blocks = csize - (offset & (csize- 1));
  1062. return NULL;
  1063. }
  1064. /* now locked ... */
  1065. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  1066. /* should we use the first or second counter field
  1067. * of the hijacked pointer? */
  1068. int hi = (pageoff > PAGE_COUNTER_MASK);
  1069. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
  1070. PAGE_COUNTER_SHIFT - 1);
  1071. *blocks = csize - (offset & (csize- 1));
  1072. return &((bitmap_counter_t *)
  1073. &bitmap->bp[page].map)[hi];
  1074. } else { /* page is allocated */
  1075. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1076. *blocks = csize - (offset & (csize- 1));
  1077. return (bitmap_counter_t *)
  1078. &(bitmap->bp[page].map[pageoff]);
  1079. }
  1080. }
  1081. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
  1082. {
  1083. if (!bitmap) return 0;
  1084. if (behind) {
  1085. atomic_inc(&bitmap->behind_writes);
  1086. PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
  1087. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1088. }
  1089. while (sectors) {
  1090. int blocks;
  1091. bitmap_counter_t *bmc;
  1092. spin_lock_irq(&bitmap->lock);
  1093. bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
  1094. if (!bmc) {
  1095. spin_unlock_irq(&bitmap->lock);
  1096. return 0;
  1097. }
  1098. if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
  1099. DEFINE_WAIT(__wait);
  1100. /* note that it is safe to do the prepare_to_wait
  1101. * after the test as long as we do it before dropping
  1102. * the spinlock.
  1103. */
  1104. prepare_to_wait(&bitmap->overflow_wait, &__wait,
  1105. TASK_UNINTERRUPTIBLE);
  1106. spin_unlock_irq(&bitmap->lock);
  1107. blk_unplug(bitmap->mddev->queue);
  1108. schedule();
  1109. finish_wait(&bitmap->overflow_wait, &__wait);
  1110. continue;
  1111. }
  1112. switch(*bmc) {
  1113. case 0:
  1114. bitmap_file_set_bit(bitmap, offset);
  1115. bitmap_count_page(bitmap,offset, 1);
  1116. blk_plug_device_unlocked(bitmap->mddev->queue);
  1117. /* fall through */
  1118. case 1:
  1119. *bmc = 2;
  1120. }
  1121. (*bmc)++;
  1122. spin_unlock_irq(&bitmap->lock);
  1123. offset += blocks;
  1124. if (sectors > blocks)
  1125. sectors -= blocks;
  1126. else sectors = 0;
  1127. }
  1128. bitmap->allclean = 0;
  1129. return 0;
  1130. }
  1131. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1132. int success, int behind)
  1133. {
  1134. if (!bitmap) return;
  1135. if (behind) {
  1136. atomic_dec(&bitmap->behind_writes);
  1137. PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
  1138. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1139. }
  1140. if (bitmap->mddev->degraded)
  1141. /* Never clear bits or update events_cleared when degraded */
  1142. success = 0;
  1143. while (sectors) {
  1144. int blocks;
  1145. unsigned long flags;
  1146. bitmap_counter_t *bmc;
  1147. spin_lock_irqsave(&bitmap->lock, flags);
  1148. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1149. if (!bmc) {
  1150. spin_unlock_irqrestore(&bitmap->lock, flags);
  1151. return;
  1152. }
  1153. if (success &&
  1154. bitmap->events_cleared < bitmap->mddev->events) {
  1155. bitmap->events_cleared = bitmap->mddev->events;
  1156. bitmap->need_sync = 1;
  1157. }
  1158. if (!success && ! (*bmc & NEEDED_MASK))
  1159. *bmc |= NEEDED_MASK;
  1160. if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
  1161. wake_up(&bitmap->overflow_wait);
  1162. (*bmc)--;
  1163. if (*bmc <= 2) {
  1164. set_page_attr(bitmap,
  1165. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1166. BITMAP_PAGE_CLEAN);
  1167. }
  1168. spin_unlock_irqrestore(&bitmap->lock, flags);
  1169. offset += blocks;
  1170. if (sectors > blocks)
  1171. sectors -= blocks;
  1172. else sectors = 0;
  1173. }
  1174. }
  1175. static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
  1176. int degraded)
  1177. {
  1178. bitmap_counter_t *bmc;
  1179. int rv;
  1180. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1181. *blocks = 1024;
  1182. return 1; /* always resync if no bitmap */
  1183. }
  1184. spin_lock_irq(&bitmap->lock);
  1185. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1186. rv = 0;
  1187. if (bmc) {
  1188. /* locked */
  1189. if (RESYNC(*bmc))
  1190. rv = 1;
  1191. else if (NEEDED(*bmc)) {
  1192. rv = 1;
  1193. if (!degraded) { /* don't set/clear bits if degraded */
  1194. *bmc |= RESYNC_MASK;
  1195. *bmc &= ~NEEDED_MASK;
  1196. }
  1197. }
  1198. }
  1199. spin_unlock_irq(&bitmap->lock);
  1200. bitmap->allclean = 0;
  1201. return rv;
  1202. }
  1203. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
  1204. int degraded)
  1205. {
  1206. /* bitmap_start_sync must always report on multiples of whole
  1207. * pages, otherwise resync (which is very PAGE_SIZE based) will
  1208. * get confused.
  1209. * So call __bitmap_start_sync repeatedly (if needed) until
  1210. * At least PAGE_SIZE>>9 blocks are covered.
  1211. * Return the 'or' of the result.
  1212. */
  1213. int rv = 0;
  1214. int blocks1;
  1215. *blocks = 0;
  1216. while (*blocks < (PAGE_SIZE>>9)) {
  1217. rv |= __bitmap_start_sync(bitmap, offset,
  1218. &blocks1, degraded);
  1219. offset += blocks1;
  1220. *blocks += blocks1;
  1221. }
  1222. return rv;
  1223. }
  1224. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
  1225. {
  1226. bitmap_counter_t *bmc;
  1227. unsigned long flags;
  1228. /*
  1229. if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
  1230. */ if (bitmap == NULL) {
  1231. *blocks = 1024;
  1232. return;
  1233. }
  1234. spin_lock_irqsave(&bitmap->lock, flags);
  1235. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1236. if (bmc == NULL)
  1237. goto unlock;
  1238. /* locked */
  1239. /*
  1240. if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
  1241. */
  1242. if (RESYNC(*bmc)) {
  1243. *bmc &= ~RESYNC_MASK;
  1244. if (!NEEDED(*bmc) && aborted)
  1245. *bmc |= NEEDED_MASK;
  1246. else {
  1247. if (*bmc <= 2) {
  1248. set_page_attr(bitmap,
  1249. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1250. BITMAP_PAGE_CLEAN);
  1251. }
  1252. }
  1253. }
  1254. unlock:
  1255. spin_unlock_irqrestore(&bitmap->lock, flags);
  1256. bitmap->allclean = 0;
  1257. }
  1258. void bitmap_close_sync(struct bitmap *bitmap)
  1259. {
  1260. /* Sync has finished, and any bitmap chunks that weren't synced
  1261. * properly have been aborted. It remains to us to clear the
  1262. * RESYNC bit wherever it is still on
  1263. */
  1264. sector_t sector = 0;
  1265. int blocks;
  1266. if (!bitmap)
  1267. return;
  1268. while (sector < bitmap->mddev->resync_max_sectors) {
  1269. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1270. sector += blocks;
  1271. }
  1272. }
  1273. void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
  1274. {
  1275. sector_t s = 0;
  1276. int blocks;
  1277. if (!bitmap)
  1278. return;
  1279. if (sector == 0) {
  1280. bitmap->last_end_sync = jiffies;
  1281. return;
  1282. }
  1283. if (time_before(jiffies, (bitmap->last_end_sync
  1284. + bitmap->daemon_sleep * HZ)))
  1285. return;
  1286. wait_event(bitmap->mddev->recovery_wait,
  1287. atomic_read(&bitmap->mddev->recovery_active) == 0);
  1288. bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync;
  1289. set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
  1290. sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
  1291. s = 0;
  1292. while (s < sector && s < bitmap->mddev->resync_max_sectors) {
  1293. bitmap_end_sync(bitmap, s, &blocks, 0);
  1294. s += blocks;
  1295. }
  1296. bitmap->last_end_sync = jiffies;
  1297. sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
  1298. }
  1299. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1300. {
  1301. /* For each chunk covered by any of these sectors, set the
  1302. * counter to 1 and set resync_needed. They should all
  1303. * be 0 at this point
  1304. */
  1305. int secs;
  1306. bitmap_counter_t *bmc;
  1307. spin_lock_irq(&bitmap->lock);
  1308. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1309. if (!bmc) {
  1310. spin_unlock_irq(&bitmap->lock);
  1311. return;
  1312. }
  1313. if (! *bmc) {
  1314. struct page *page;
  1315. *bmc = 1 | (needed?NEEDED_MASK:0);
  1316. bitmap_count_page(bitmap, offset, 1);
  1317. page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
  1318. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1319. }
  1320. spin_unlock_irq(&bitmap->lock);
  1321. bitmap->allclean = 0;
  1322. }
  1323. /* dirty the memory and file bits for bitmap chunks "s" to "e" */
  1324. void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
  1325. {
  1326. unsigned long chunk;
  1327. for (chunk = s; chunk <= e; chunk++) {
  1328. sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
  1329. bitmap_set_memory_bits(bitmap, sec, 1);
  1330. bitmap_file_set_bit(bitmap, sec);
  1331. }
  1332. }
  1333. /*
  1334. * flush out any pending updates
  1335. */
  1336. void bitmap_flush(mddev_t *mddev)
  1337. {
  1338. struct bitmap *bitmap = mddev->bitmap;
  1339. int sleep;
  1340. if (!bitmap) /* there was no bitmap */
  1341. return;
  1342. /* run the daemon_work three time to ensure everything is flushed
  1343. * that can be
  1344. */
  1345. sleep = bitmap->daemon_sleep;
  1346. bitmap->daemon_sleep = 0;
  1347. bitmap_daemon_work(bitmap);
  1348. bitmap_daemon_work(bitmap);
  1349. bitmap_daemon_work(bitmap);
  1350. bitmap->daemon_sleep = sleep;
  1351. bitmap_update_sb(bitmap);
  1352. }
  1353. /*
  1354. * free memory that was allocated
  1355. */
  1356. static void bitmap_free(struct bitmap *bitmap)
  1357. {
  1358. unsigned long k, pages;
  1359. struct bitmap_page *bp;
  1360. if (!bitmap) /* there was no bitmap */
  1361. return;
  1362. /* release the bitmap file and kill the daemon */
  1363. bitmap_file_put(bitmap);
  1364. bp = bitmap->bp;
  1365. pages = bitmap->pages;
  1366. /* free all allocated memory */
  1367. if (bp) /* deallocate the page memory */
  1368. for (k = 0; k < pages; k++)
  1369. if (bp[k].map && !bp[k].hijacked)
  1370. kfree(bp[k].map);
  1371. kfree(bp);
  1372. kfree(bitmap);
  1373. }
  1374. void bitmap_destroy(mddev_t *mddev)
  1375. {
  1376. struct bitmap *bitmap = mddev->bitmap;
  1377. if (!bitmap) /* there was no bitmap */
  1378. return;
  1379. mddev->bitmap = NULL; /* disconnect from the md device */
  1380. if (mddev->thread)
  1381. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1382. bitmap_free(bitmap);
  1383. }
  1384. /*
  1385. * initialize the bitmap structure
  1386. * if this returns an error, bitmap_destroy must be called to do clean up
  1387. */
  1388. int bitmap_create(mddev_t *mddev)
  1389. {
  1390. struct bitmap *bitmap;
  1391. sector_t blocks = mddev->resync_max_sectors;
  1392. unsigned long chunks;
  1393. unsigned long pages;
  1394. struct file *file = mddev->bitmap_file;
  1395. int err;
  1396. sector_t start;
  1397. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1398. if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
  1399. return 0;
  1400. BUG_ON(file && mddev->bitmap_offset);
  1401. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1402. if (!bitmap)
  1403. return -ENOMEM;
  1404. spin_lock_init(&bitmap->lock);
  1405. atomic_set(&bitmap->pending_writes, 0);
  1406. init_waitqueue_head(&bitmap->write_wait);
  1407. init_waitqueue_head(&bitmap->overflow_wait);
  1408. bitmap->mddev = mddev;
  1409. bitmap->file = file;
  1410. bitmap->offset = mddev->bitmap_offset;
  1411. if (file) {
  1412. get_file(file);
  1413. do_sync_mapping_range(file->f_mapping, 0, LLONG_MAX,
  1414. SYNC_FILE_RANGE_WAIT_BEFORE |
  1415. SYNC_FILE_RANGE_WRITE |
  1416. SYNC_FILE_RANGE_WAIT_AFTER);
  1417. }
  1418. /* read superblock from bitmap file (this sets bitmap->chunksize) */
  1419. err = bitmap_read_sb(bitmap);
  1420. if (err)
  1421. goto error;
  1422. bitmap->chunkshift = ffz(~bitmap->chunksize);
  1423. /* now that chunksize and chunkshift are set, we can use these macros */
  1424. chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
  1425. CHUNK_BLOCK_SHIFT(bitmap);
  1426. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1427. BUG_ON(!pages);
  1428. bitmap->chunks = chunks;
  1429. bitmap->pages = pages;
  1430. bitmap->missing_pages = pages;
  1431. bitmap->counter_bits = COUNTER_BITS;
  1432. bitmap->syncchunk = ~0UL;
  1433. #ifdef INJECT_FATAL_FAULT_1
  1434. bitmap->bp = NULL;
  1435. #else
  1436. bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1437. #endif
  1438. err = -ENOMEM;
  1439. if (!bitmap->bp)
  1440. goto error;
  1441. /* now that we have some pages available, initialize the in-memory
  1442. * bitmap from the on-disk bitmap */
  1443. start = 0;
  1444. if (mddev->degraded == 0
  1445. || bitmap->events_cleared == mddev->events)
  1446. /* no need to keep dirty bits to optimise a re-add of a missing device */
  1447. start = mddev->recovery_cp;
  1448. err = bitmap_init_from_disk(bitmap, start);
  1449. if (err)
  1450. goto error;
  1451. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1452. pages, bmname(bitmap));
  1453. mddev->bitmap = bitmap;
  1454. mddev->thread->timeout = bitmap->daemon_sleep * HZ;
  1455. bitmap_update_sb(bitmap);
  1456. return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
  1457. error:
  1458. bitmap_free(bitmap);
  1459. return err;
  1460. }
  1461. /* the bitmap API -- for raid personalities */
  1462. EXPORT_SYMBOL(bitmap_startwrite);
  1463. EXPORT_SYMBOL(bitmap_endwrite);
  1464. EXPORT_SYMBOL(bitmap_start_sync);
  1465. EXPORT_SYMBOL(bitmap_end_sync);
  1466. EXPORT_SYMBOL(bitmap_unplug);
  1467. EXPORT_SYMBOL(bitmap_close_sync);
  1468. EXPORT_SYMBOL(bitmap_cond_end_sync);