bitmap.c 43 KB

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