bitmap.c 38 KB

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