bitmap.c 39 KB

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