bitmap.c 41 KB

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