bitmap.c 41 KB

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