bitmap.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  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 inline 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. if (wait)
  258. lock_page(page);
  259. else {
  260. if (TestSetPageLocked(page))
  261. return -EAGAIN; /* already locked */
  262. if (PageWriteback(page)) {
  263. unlock_page(page);
  264. return -EAGAIN;
  265. }
  266. }
  267. ret = page->mapping->a_ops->prepare_write(bitmap->file, page, 0, PAGE_SIZE);
  268. if (!ret)
  269. ret = page->mapping->a_ops->commit_write(bitmap->file, page, 0,
  270. PAGE_SIZE);
  271. if (ret) {
  272. unlock_page(page);
  273. return ret;
  274. }
  275. set_page_dirty(page); /* force it to be written out */
  276. if (!wait) {
  277. /* add to list to be waited for by daemon */
  278. struct page_list *item = mempool_alloc(bitmap->write_pool, GFP_NOIO);
  279. item->page = page;
  280. page_cache_get(page);
  281. spin_lock(&bitmap->write_lock);
  282. list_add(&item->list, &bitmap->complete_pages);
  283. spin_unlock(&bitmap->write_lock);
  284. md_wakeup_thread(bitmap->writeback_daemon);
  285. }
  286. return write_one_page(page, wait);
  287. }
  288. /* read a page from a file, pinning it into cache, and return bytes_read */
  289. static struct page *read_page(struct file *file, unsigned long index,
  290. unsigned long *bytes_read)
  291. {
  292. struct inode *inode = file->f_mapping->host;
  293. struct page *page = NULL;
  294. loff_t isize = i_size_read(inode);
  295. unsigned long end_index = isize >> PAGE_CACHE_SHIFT;
  296. PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_CACHE_SIZE,
  297. (unsigned long long)index << PAGE_CACHE_SHIFT);
  298. page = read_cache_page(inode->i_mapping, index,
  299. (filler_t *)inode->i_mapping->a_ops->readpage, file);
  300. if (IS_ERR(page))
  301. goto out;
  302. wait_on_page_locked(page);
  303. if (!PageUptodate(page) || PageError(page)) {
  304. page_cache_release(page);
  305. page = ERR_PTR(-EIO);
  306. goto out;
  307. }
  308. if (index > end_index) /* we have read beyond EOF */
  309. *bytes_read = 0;
  310. else if (index == end_index) /* possible short read */
  311. *bytes_read = isize & ~PAGE_CACHE_MASK;
  312. else
  313. *bytes_read = PAGE_CACHE_SIZE; /* got a full page */
  314. out:
  315. if (IS_ERR(page))
  316. printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
  317. (int)PAGE_CACHE_SIZE,
  318. (unsigned long long)index << PAGE_CACHE_SHIFT,
  319. PTR_ERR(page));
  320. return page;
  321. }
  322. /*
  323. * bitmap file superblock operations
  324. */
  325. /* update the event counter and sync the superblock to disk */
  326. int bitmap_update_sb(struct bitmap *bitmap)
  327. {
  328. bitmap_super_t *sb;
  329. unsigned long flags;
  330. if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
  331. return 0;
  332. spin_lock_irqsave(&bitmap->lock, flags);
  333. if (!bitmap->sb_page) { /* no superblock */
  334. spin_unlock_irqrestore(&bitmap->lock, flags);
  335. return 0;
  336. }
  337. spin_unlock_irqrestore(&bitmap->lock, flags);
  338. sb = (bitmap_super_t *)kmap(bitmap->sb_page);
  339. sb->events = cpu_to_le64(bitmap->mddev->events);
  340. if (!bitmap->mddev->degraded)
  341. sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
  342. kunmap(bitmap->sb_page);
  343. return write_page(bitmap, bitmap->sb_page, 1);
  344. }
  345. /* print out the bitmap file superblock */
  346. void bitmap_print_sb(struct bitmap *bitmap)
  347. {
  348. bitmap_super_t *sb;
  349. if (!bitmap || !bitmap->sb_page)
  350. return;
  351. sb = (bitmap_super_t *)kmap(bitmap->sb_page);
  352. printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
  353. printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
  354. printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
  355. printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
  356. *(__u32 *)(sb->uuid+0),
  357. *(__u32 *)(sb->uuid+4),
  358. *(__u32 *)(sb->uuid+8),
  359. *(__u32 *)(sb->uuid+12));
  360. printk(KERN_DEBUG " events: %llu\n",
  361. (unsigned long long) le64_to_cpu(sb->events));
  362. printk(KERN_DEBUG "events cleared: %llu\n",
  363. (unsigned long long) le64_to_cpu(sb->events_cleared));
  364. printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
  365. printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
  366. printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
  367. printk(KERN_DEBUG " sync size: %llu KB\n",
  368. (unsigned long long)le64_to_cpu(sb->sync_size)/2);
  369. printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
  370. kunmap(bitmap->sb_page);
  371. }
  372. /* read the superblock from the bitmap file and initialize some bitmap fields */
  373. static int bitmap_read_sb(struct bitmap *bitmap)
  374. {
  375. char *reason = NULL;
  376. bitmap_super_t *sb;
  377. unsigned long chunksize, daemon_sleep, write_behind;
  378. unsigned long bytes_read;
  379. unsigned long long events;
  380. int err = -EINVAL;
  381. /* page 0 is the superblock, read it... */
  382. if (bitmap->file)
  383. bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read);
  384. else {
  385. bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
  386. bytes_read = PAGE_SIZE;
  387. }
  388. if (IS_ERR(bitmap->sb_page)) {
  389. err = PTR_ERR(bitmap->sb_page);
  390. bitmap->sb_page = NULL;
  391. return err;
  392. }
  393. sb = (bitmap_super_t *)kmap(bitmap->sb_page);
  394. if (bytes_read < sizeof(*sb)) { /* short read */
  395. printk(KERN_INFO "%s: bitmap file superblock truncated\n",
  396. bmname(bitmap));
  397. err = -ENOSPC;
  398. goto out;
  399. }
  400. chunksize = le32_to_cpu(sb->chunksize);
  401. daemon_sleep = le32_to_cpu(sb->daemon_sleep);
  402. write_behind = le32_to_cpu(sb->write_behind);
  403. /* verify that the bitmap-specific fields are valid */
  404. if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
  405. reason = "bad magic";
  406. else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
  407. le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
  408. reason = "unrecognized superblock version";
  409. else if (chunksize < 512 || chunksize > (1024 * 1024 * 4))
  410. reason = "bitmap chunksize out of range (512B - 4MB)";
  411. else if ((1 << ffz(~chunksize)) != chunksize)
  412. reason = "bitmap chunksize not a power of 2";
  413. else if (daemon_sleep < 1 || daemon_sleep > 15)
  414. reason = "daemon sleep period out of range (1-15s)";
  415. else if (write_behind > COUNTER_MAX)
  416. reason = "write-behind limit out of range (0 - 16383)";
  417. if (reason) {
  418. printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
  419. bmname(bitmap), reason);
  420. goto out;
  421. }
  422. /* keep the array size field of the bitmap superblock up to date */
  423. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  424. if (!bitmap->mddev->persistent)
  425. goto success;
  426. /*
  427. * if we have a persistent array superblock, compare the
  428. * bitmap's UUID and event counter to the mddev's
  429. */
  430. if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
  431. printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
  432. bmname(bitmap));
  433. goto out;
  434. }
  435. events = le64_to_cpu(sb->events);
  436. if (events < bitmap->mddev->events) {
  437. printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
  438. "-- forcing full recovery\n", bmname(bitmap), events,
  439. (unsigned long long) bitmap->mddev->events);
  440. sb->state |= BITMAP_STALE;
  441. }
  442. success:
  443. /* assign fields using values from superblock */
  444. bitmap->chunksize = chunksize;
  445. bitmap->daemon_sleep = daemon_sleep;
  446. bitmap->daemon_lastrun = jiffies;
  447. bitmap->max_write_behind = write_behind;
  448. bitmap->flags |= sb->state;
  449. if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
  450. bitmap->flags |= BITMAP_HOSTENDIAN;
  451. bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
  452. if (sb->state & BITMAP_STALE)
  453. bitmap->events_cleared = bitmap->mddev->events;
  454. err = 0;
  455. out:
  456. kunmap(bitmap->sb_page);
  457. if (err)
  458. bitmap_print_sb(bitmap);
  459. return err;
  460. }
  461. enum bitmap_mask_op {
  462. MASK_SET,
  463. MASK_UNSET
  464. };
  465. /* record the state of the bitmap in the superblock */
  466. static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
  467. enum bitmap_mask_op op)
  468. {
  469. bitmap_super_t *sb;
  470. unsigned long flags;
  471. spin_lock_irqsave(&bitmap->lock, flags);
  472. if (!bitmap || !bitmap->sb_page) { /* can't set the state */
  473. spin_unlock_irqrestore(&bitmap->lock, flags);
  474. return;
  475. }
  476. page_cache_get(bitmap->sb_page);
  477. spin_unlock_irqrestore(&bitmap->lock, flags);
  478. sb = (bitmap_super_t *)kmap(bitmap->sb_page);
  479. switch (op) {
  480. case MASK_SET: sb->state |= bits;
  481. break;
  482. case MASK_UNSET: sb->state &= ~bits;
  483. break;
  484. default: BUG();
  485. }
  486. kunmap(bitmap->sb_page);
  487. page_cache_release(bitmap->sb_page);
  488. }
  489. /*
  490. * general bitmap file operations
  491. */
  492. /* calculate the index of the page that contains this bit */
  493. static inline unsigned long file_page_index(unsigned long chunk)
  494. {
  495. return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
  496. }
  497. /* calculate the (bit) offset of this bit within a page */
  498. static inline unsigned long file_page_offset(unsigned long chunk)
  499. {
  500. return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
  501. }
  502. /*
  503. * return a pointer to the page in the filemap that contains the given bit
  504. *
  505. * this lookup is complicated by the fact that the bitmap sb might be exactly
  506. * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
  507. * 0 or page 1
  508. */
  509. static inline struct page *filemap_get_page(struct bitmap *bitmap,
  510. unsigned long chunk)
  511. {
  512. return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
  513. }
  514. static void bitmap_file_unmap(struct bitmap *bitmap)
  515. {
  516. struct page **map, *sb_page;
  517. unsigned long *attr;
  518. int pages;
  519. unsigned long flags;
  520. spin_lock_irqsave(&bitmap->lock, flags);
  521. map = bitmap->filemap;
  522. bitmap->filemap = NULL;
  523. attr = bitmap->filemap_attr;
  524. bitmap->filemap_attr = NULL;
  525. pages = bitmap->file_pages;
  526. bitmap->file_pages = 0;
  527. sb_page = bitmap->sb_page;
  528. bitmap->sb_page = NULL;
  529. spin_unlock_irqrestore(&bitmap->lock, flags);
  530. while (pages--)
  531. if (map[pages]->index != 0) /* 0 is sb_page, release it below */
  532. page_cache_release(map[pages]);
  533. kfree(map);
  534. kfree(attr);
  535. if (sb_page)
  536. page_cache_release(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. page_cache_release(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. page_cache_get(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. chunks = bitmap->chunks;
  727. file = bitmap->file;
  728. BUG_ON(!file && !bitmap->offset);
  729. #ifdef INJECT_FAULTS_3
  730. outofdate = 1;
  731. #else
  732. outofdate = bitmap->flags & BITMAP_STALE;
  733. #endif
  734. if (outofdate)
  735. printk(KERN_INFO "%s: bitmap file is out of date, doing full "
  736. "recovery\n", bmname(bitmap));
  737. bytes = (chunks + 7) / 8;
  738. num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
  739. if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
  740. printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
  741. bmname(bitmap),
  742. (unsigned long) i_size_read(file->f_mapping->host),
  743. bytes + sizeof(bitmap_super_t));
  744. goto out;
  745. }
  746. ret = -ENOMEM;
  747. bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  748. if (!bitmap->filemap)
  749. goto out;
  750. bitmap->filemap_attr = kmalloc(sizeof(long) * num_pages, GFP_KERNEL);
  751. if (!bitmap->filemap_attr)
  752. goto out;
  753. memset(bitmap->filemap_attr, 0, sizeof(long) * num_pages);
  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 (oldpage != NULL)
  762. kunmap(oldpage);
  763. if (index == 0) {
  764. /*
  765. * if we're here then the superblock page
  766. * contains some bits (PAGE_SIZE != sizeof sb)
  767. * we've already read it in, so just use it
  768. */
  769. page = bitmap->sb_page;
  770. offset = sizeof(bitmap_super_t);
  771. } else if (file) {
  772. page = read_page(file, index, &dummy);
  773. offset = 0;
  774. } else {
  775. page = read_sb_page(bitmap->mddev, bitmap->offset, index);
  776. offset = 0;
  777. }
  778. if (IS_ERR(page)) { /* read error */
  779. ret = PTR_ERR(page);
  780. goto out;
  781. }
  782. oldindex = index;
  783. oldpage = page;
  784. kmap(page);
  785. if (outofdate) {
  786. /*
  787. * if bitmap is out of date, dirty the
  788. * whole page and write it out
  789. */
  790. memset(page_address(page) + offset, 0xff,
  791. PAGE_SIZE - offset);
  792. ret = write_page(bitmap, page, 1);
  793. if (ret) {
  794. kunmap(page);
  795. /* release, page not in filemap yet */
  796. page_cache_release(page);
  797. goto out;
  798. }
  799. }
  800. bitmap->filemap[bitmap->file_pages++] = page;
  801. }
  802. if (bitmap->flags & BITMAP_HOSTENDIAN)
  803. b = test_bit(bit, page_address(page));
  804. else
  805. b = ext2_test_bit(bit, page_address(page));
  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 (page) /* unmap the last page */
  819. kunmap(page);
  820. if (bit_cnt) { /* Kick recovery if any bits were set */
  821. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  822. md_wakeup_thread(bitmap->mddev->thread);
  823. }
  824. out:
  825. printk(KERN_INFO "%s: bitmap initialized from disk: "
  826. "read %lu/%lu pages, set %lu bits, status: %d\n",
  827. bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
  828. return ret;
  829. }
  830. void bitmap_write_all(struct bitmap *bitmap)
  831. {
  832. /* We don't actually write all bitmap blocks here,
  833. * just flag them as needing to be written
  834. */
  835. unsigned long chunks = bitmap->chunks;
  836. unsigned long bytes = (chunks+7)/8 + sizeof(bitmap_super_t);
  837. unsigned long num_pages = (bytes + PAGE_SIZE-1) / PAGE_SIZE;
  838. while (num_pages--)
  839. bitmap->filemap_attr[num_pages] |= BITMAP_PAGE_NEEDWRITE;
  840. }
  841. static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
  842. {
  843. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  844. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  845. bitmap->bp[page].count += inc;
  846. /*
  847. if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
  848. (unsigned long long)offset, inc, bitmap->bp[page].count);
  849. */
  850. bitmap_checkfree(bitmap, page);
  851. }
  852. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  853. sector_t offset, int *blocks,
  854. int create);
  855. /*
  856. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  857. * out to disk
  858. */
  859. int bitmap_daemon_work(struct bitmap *bitmap)
  860. {
  861. unsigned long j;
  862. unsigned long flags;
  863. struct page *page = NULL, *lastpage = NULL;
  864. int err = 0;
  865. int blocks;
  866. int attr;
  867. if (bitmap == NULL)
  868. return 0;
  869. if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
  870. return 0;
  871. bitmap->daemon_lastrun = jiffies;
  872. for (j = 0; j < bitmap->chunks; j++) {
  873. bitmap_counter_t *bmc;
  874. spin_lock_irqsave(&bitmap->lock, flags);
  875. if (!bitmap->filemap) {
  876. /* error or shutdown */
  877. spin_unlock_irqrestore(&bitmap->lock, flags);
  878. break;
  879. }
  880. page = filemap_get_page(bitmap, j);
  881. if (page != lastpage) {
  882. /* skip this page unless it's marked as needing cleaning */
  883. if (!((attr=get_page_attr(bitmap, page)) & BITMAP_PAGE_CLEAN)) {
  884. if (attr & BITMAP_PAGE_NEEDWRITE) {
  885. page_cache_get(page);
  886. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  887. }
  888. spin_unlock_irqrestore(&bitmap->lock, flags);
  889. if (attr & BITMAP_PAGE_NEEDWRITE) {
  890. switch (write_page(bitmap, page, 0)) {
  891. case -EAGAIN:
  892. set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  893. break;
  894. case 0:
  895. break;
  896. default:
  897. bitmap_file_kick(bitmap);
  898. }
  899. page_cache_release(page);
  900. }
  901. continue;
  902. }
  903. /* grab the new page, sync and release the old */
  904. page_cache_get(page);
  905. if (lastpage != NULL) {
  906. if (get_page_attr(bitmap, lastpage) & BITMAP_PAGE_NEEDWRITE) {
  907. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  908. spin_unlock_irqrestore(&bitmap->lock, flags);
  909. err = write_page(bitmap, lastpage, 0);
  910. if (err == -EAGAIN) {
  911. err = 0;
  912. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  913. }
  914. } else {
  915. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  916. spin_unlock_irqrestore(&bitmap->lock, flags);
  917. }
  918. kunmap(lastpage);
  919. page_cache_release(lastpage);
  920. if (err)
  921. bitmap_file_kick(bitmap);
  922. } else
  923. spin_unlock_irqrestore(&bitmap->lock, flags);
  924. lastpage = page;
  925. kmap(page);
  926. /*
  927. printk("bitmap clean at page %lu\n", j);
  928. */
  929. spin_lock_irqsave(&bitmap->lock, flags);
  930. clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  931. }
  932. bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
  933. &blocks, 0);
  934. if (bmc) {
  935. /*
  936. if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
  937. */
  938. if (*bmc == 2) {
  939. *bmc=1; /* maybe clear the bit next time */
  940. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  941. } else if (*bmc == 1) {
  942. /* we can clear the bit */
  943. *bmc = 0;
  944. bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
  945. -1);
  946. /* clear the bit */
  947. if (bitmap->flags & BITMAP_HOSTENDIAN)
  948. clear_bit(file_page_offset(j), page_address(page));
  949. else
  950. ext2_clear_bit(file_page_offset(j), page_address(page));
  951. }
  952. }
  953. spin_unlock_irqrestore(&bitmap->lock, flags);
  954. }
  955. /* now sync the final page */
  956. if (lastpage != NULL) {
  957. kunmap(lastpage);
  958. spin_lock_irqsave(&bitmap->lock, flags);
  959. if (get_page_attr(bitmap, lastpage) &BITMAP_PAGE_NEEDWRITE) {
  960. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  961. spin_unlock_irqrestore(&bitmap->lock, flags);
  962. err = write_page(bitmap, lastpage, 0);
  963. if (err == -EAGAIN) {
  964. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  965. err = 0;
  966. }
  967. } else {
  968. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  969. spin_unlock_irqrestore(&bitmap->lock, flags);
  970. }
  971. page_cache_release(lastpage);
  972. }
  973. return err;
  974. }
  975. static void daemon_exit(struct bitmap *bitmap, mdk_thread_t **daemon)
  976. {
  977. mdk_thread_t *dmn;
  978. unsigned long flags;
  979. /* if no one is waiting on us, we'll free the md thread struct
  980. * and exit, otherwise we let the waiter clean things up */
  981. spin_lock_irqsave(&bitmap->lock, flags);
  982. if ((dmn = *daemon)) { /* no one is waiting, cleanup and exit */
  983. *daemon = NULL;
  984. spin_unlock_irqrestore(&bitmap->lock, flags);
  985. kfree(dmn);
  986. complete_and_exit(NULL, 0); /* do_exit not exported */
  987. }
  988. spin_unlock_irqrestore(&bitmap->lock, flags);
  989. }
  990. static void bitmap_writeback_daemon(mddev_t *mddev)
  991. {
  992. struct bitmap *bitmap = mddev->bitmap;
  993. struct page *page;
  994. struct page_list *item;
  995. int err = 0;
  996. if (signal_pending(current)) {
  997. printk(KERN_INFO
  998. "%s: bitmap writeback daemon got signal, exiting...\n",
  999. bmname(bitmap));
  1000. err = -EINTR;
  1001. goto out;
  1002. }
  1003. if (bitmap == NULL)
  1004. /* about to be stopped. */
  1005. return;
  1006. PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
  1007. /* wait on bitmap page writebacks */
  1008. while ((item = dequeue_page(bitmap))) {
  1009. page = item->page;
  1010. mempool_free(item, bitmap->write_pool);
  1011. PRINTK("wait on page writeback: %p\n", page);
  1012. wait_on_page_writeback(page);
  1013. PRINTK("finished page writeback: %p\n", page);
  1014. err = PageError(page);
  1015. page_cache_release(page);
  1016. if (err) {
  1017. printk(KERN_WARNING "%s: bitmap file writeback "
  1018. "failed (page %lu): %d\n",
  1019. bmname(bitmap), page->index, err);
  1020. bitmap_file_kick(bitmap);
  1021. goto out;
  1022. }
  1023. }
  1024. out:
  1025. wake_up(&bitmap->write_wait);
  1026. if (err) {
  1027. printk(KERN_INFO "%s: bitmap writeback daemon exiting (%d)\n",
  1028. bmname(bitmap), err);
  1029. daemon_exit(bitmap, &bitmap->writeback_daemon);
  1030. }
  1031. }
  1032. static mdk_thread_t *bitmap_start_daemon(struct bitmap *bitmap,
  1033. void (*func)(mddev_t *), char *name)
  1034. {
  1035. mdk_thread_t *daemon;
  1036. char namebuf[32];
  1037. #ifdef INJECT_FATAL_FAULT_2
  1038. daemon = NULL;
  1039. #else
  1040. sprintf(namebuf, "%%s_%s", name);
  1041. daemon = md_register_thread(func, bitmap->mddev, namebuf);
  1042. #endif
  1043. if (!daemon) {
  1044. printk(KERN_ERR "%s: failed to start bitmap daemon\n",
  1045. bmname(bitmap));
  1046. return ERR_PTR(-ECHILD);
  1047. }
  1048. md_wakeup_thread(daemon); /* start it running */
  1049. PRINTK("%s: %s daemon (pid %d) started...\n",
  1050. bmname(bitmap), name, daemon->tsk->pid);
  1051. return daemon;
  1052. }
  1053. static void bitmap_stop_daemon(struct bitmap *bitmap)
  1054. {
  1055. /* the daemon can't stop itself... it'll just exit instead... */
  1056. if (bitmap->writeback_daemon && ! IS_ERR(bitmap->writeback_daemon) &&
  1057. current->pid != bitmap->writeback_daemon->tsk->pid) {
  1058. mdk_thread_t *daemon;
  1059. unsigned long flags;
  1060. spin_lock_irqsave(&bitmap->lock, flags);
  1061. daemon = bitmap->writeback_daemon;
  1062. bitmap->writeback_daemon = NULL;
  1063. spin_unlock_irqrestore(&bitmap->lock, flags);
  1064. if (daemon && ! IS_ERR(daemon))
  1065. md_unregister_thread(daemon); /* destroy the thread */
  1066. }
  1067. }
  1068. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1069. sector_t offset, int *blocks,
  1070. int create)
  1071. {
  1072. /* If 'create', we might release the lock and reclaim it.
  1073. * The lock must have been taken with interrupts enabled.
  1074. * If !create, we don't release the lock.
  1075. */
  1076. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  1077. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1078. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  1079. sector_t csize;
  1080. if (bitmap_checkpage(bitmap, page, create) < 0) {
  1081. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1082. *blocks = csize - (offset & (csize- 1));
  1083. return NULL;
  1084. }
  1085. /* now locked ... */
  1086. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  1087. /* should we use the first or second counter field
  1088. * of the hijacked pointer? */
  1089. int hi = (pageoff > PAGE_COUNTER_MASK);
  1090. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
  1091. PAGE_COUNTER_SHIFT - 1);
  1092. *blocks = csize - (offset & (csize- 1));
  1093. return &((bitmap_counter_t *)
  1094. &bitmap->bp[page].map)[hi];
  1095. } else { /* page is allocated */
  1096. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1097. *blocks = csize - (offset & (csize- 1));
  1098. return (bitmap_counter_t *)
  1099. &(bitmap->bp[page].map[pageoff]);
  1100. }
  1101. }
  1102. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
  1103. {
  1104. if (!bitmap) return 0;
  1105. if (behind) {
  1106. atomic_inc(&bitmap->behind_writes);
  1107. PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
  1108. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1109. }
  1110. while (sectors) {
  1111. int blocks;
  1112. bitmap_counter_t *bmc;
  1113. spin_lock_irq(&bitmap->lock);
  1114. bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
  1115. if (!bmc) {
  1116. spin_unlock_irq(&bitmap->lock);
  1117. return 0;
  1118. }
  1119. switch(*bmc) {
  1120. case 0:
  1121. bitmap_file_set_bit(bitmap, offset);
  1122. bitmap_count_page(bitmap,offset, 1);
  1123. blk_plug_device(bitmap->mddev->queue);
  1124. /* fall through */
  1125. case 1:
  1126. *bmc = 2;
  1127. }
  1128. if ((*bmc & COUNTER_MAX) == COUNTER_MAX) BUG();
  1129. (*bmc)++;
  1130. spin_unlock_irq(&bitmap->lock);
  1131. offset += blocks;
  1132. if (sectors > blocks)
  1133. sectors -= blocks;
  1134. else sectors = 0;
  1135. }
  1136. return 0;
  1137. }
  1138. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1139. int success, int behind)
  1140. {
  1141. if (!bitmap) return;
  1142. if (behind) {
  1143. atomic_dec(&bitmap->behind_writes);
  1144. PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
  1145. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1146. }
  1147. while (sectors) {
  1148. int blocks;
  1149. unsigned long flags;
  1150. bitmap_counter_t *bmc;
  1151. spin_lock_irqsave(&bitmap->lock, flags);
  1152. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1153. if (!bmc) {
  1154. spin_unlock_irqrestore(&bitmap->lock, flags);
  1155. return;
  1156. }
  1157. if (!success && ! (*bmc & NEEDED_MASK))
  1158. *bmc |= NEEDED_MASK;
  1159. (*bmc)--;
  1160. if (*bmc <= 2) {
  1161. set_page_attr(bitmap,
  1162. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1163. BITMAP_PAGE_CLEAN);
  1164. }
  1165. spin_unlock_irqrestore(&bitmap->lock, flags);
  1166. offset += blocks;
  1167. if (sectors > blocks)
  1168. sectors -= blocks;
  1169. else sectors = 0;
  1170. }
  1171. }
  1172. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
  1173. int degraded)
  1174. {
  1175. bitmap_counter_t *bmc;
  1176. int rv;
  1177. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1178. *blocks = 1024;
  1179. return 1; /* always resync if no bitmap */
  1180. }
  1181. spin_lock_irq(&bitmap->lock);
  1182. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1183. rv = 0;
  1184. if (bmc) {
  1185. /* locked */
  1186. if (RESYNC(*bmc))
  1187. rv = 1;
  1188. else if (NEEDED(*bmc)) {
  1189. rv = 1;
  1190. if (!degraded) { /* don't set/clear bits if degraded */
  1191. *bmc |= RESYNC_MASK;
  1192. *bmc &= ~NEEDED_MASK;
  1193. }
  1194. }
  1195. }
  1196. spin_unlock_irq(&bitmap->lock);
  1197. return rv;
  1198. }
  1199. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
  1200. {
  1201. bitmap_counter_t *bmc;
  1202. unsigned long flags;
  1203. /*
  1204. if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
  1205. */ if (bitmap == NULL) {
  1206. *blocks = 1024;
  1207. return;
  1208. }
  1209. spin_lock_irqsave(&bitmap->lock, flags);
  1210. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1211. if (bmc == NULL)
  1212. goto unlock;
  1213. /* locked */
  1214. /*
  1215. if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
  1216. */
  1217. if (RESYNC(*bmc)) {
  1218. *bmc &= ~RESYNC_MASK;
  1219. if (!NEEDED(*bmc) && aborted)
  1220. *bmc |= NEEDED_MASK;
  1221. else {
  1222. if (*bmc <= 2) {
  1223. set_page_attr(bitmap,
  1224. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1225. BITMAP_PAGE_CLEAN);
  1226. }
  1227. }
  1228. }
  1229. unlock:
  1230. spin_unlock_irqrestore(&bitmap->lock, flags);
  1231. }
  1232. void bitmap_close_sync(struct bitmap *bitmap)
  1233. {
  1234. /* Sync has finished, and any bitmap chunks that weren't synced
  1235. * properly have been aborted. It remains to us to clear the
  1236. * RESYNC bit wherever it is still on
  1237. */
  1238. sector_t sector = 0;
  1239. int blocks;
  1240. if (!bitmap) return;
  1241. while (sector < bitmap->mddev->resync_max_sectors) {
  1242. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1243. /*
  1244. if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
  1245. (unsigned long long)sector, blocks);
  1246. */ sector += blocks;
  1247. }
  1248. }
  1249. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1250. {
  1251. /* For each chunk covered by any of these sectors, set the
  1252. * counter to 1 and set resync_needed. They should all
  1253. * be 0 at this point
  1254. */
  1255. int secs;
  1256. bitmap_counter_t *bmc;
  1257. spin_lock_irq(&bitmap->lock);
  1258. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1259. if (!bmc) {
  1260. spin_unlock_irq(&bitmap->lock);
  1261. return;
  1262. }
  1263. if (! *bmc) {
  1264. struct page *page;
  1265. *bmc = 1 | (needed?NEEDED_MASK:0);
  1266. bitmap_count_page(bitmap, offset, 1);
  1267. page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
  1268. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1269. }
  1270. spin_unlock_irq(&bitmap->lock);
  1271. }
  1272. /*
  1273. * flush out any pending updates
  1274. */
  1275. void bitmap_flush(mddev_t *mddev)
  1276. {
  1277. struct bitmap *bitmap = mddev->bitmap;
  1278. int sleep;
  1279. if (!bitmap) /* there was no bitmap */
  1280. return;
  1281. /* run the daemon_work three time to ensure everything is flushed
  1282. * that can be
  1283. */
  1284. sleep = bitmap->daemon_sleep;
  1285. bitmap->daemon_sleep = 0;
  1286. bitmap_daemon_work(bitmap);
  1287. bitmap_daemon_work(bitmap);
  1288. bitmap_daemon_work(bitmap);
  1289. bitmap->daemon_sleep = sleep;
  1290. bitmap_update_sb(bitmap);
  1291. }
  1292. /*
  1293. * free memory that was allocated
  1294. */
  1295. static void bitmap_free(struct bitmap *bitmap)
  1296. {
  1297. unsigned long k, pages;
  1298. struct bitmap_page *bp;
  1299. if (!bitmap) /* there was no bitmap */
  1300. return;
  1301. /* release the bitmap file and kill the daemon */
  1302. bitmap_file_put(bitmap);
  1303. bp = bitmap->bp;
  1304. pages = bitmap->pages;
  1305. /* free all allocated memory */
  1306. mempool_destroy(bitmap->write_pool);
  1307. if (bp) /* deallocate the page memory */
  1308. for (k = 0; k < pages; k++)
  1309. if (bp[k].map && !bp[k].hijacked)
  1310. kfree(bp[k].map);
  1311. kfree(bp);
  1312. kfree(bitmap);
  1313. }
  1314. void bitmap_destroy(mddev_t *mddev)
  1315. {
  1316. struct bitmap *bitmap = mddev->bitmap;
  1317. if (!bitmap) /* there was no bitmap */
  1318. return;
  1319. mddev->bitmap = NULL; /* disconnect from the md device */
  1320. if (mddev->thread)
  1321. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1322. bitmap_free(bitmap);
  1323. }
  1324. /*
  1325. * initialize the bitmap structure
  1326. * if this returns an error, bitmap_destroy must be called to do clean up
  1327. */
  1328. int bitmap_create(mddev_t *mddev)
  1329. {
  1330. struct bitmap *bitmap;
  1331. unsigned long blocks = mddev->resync_max_sectors;
  1332. unsigned long chunks;
  1333. unsigned long pages;
  1334. struct file *file = mddev->bitmap_file;
  1335. int err;
  1336. sector_t start;
  1337. BUG_ON(sizeof(bitmap_super_t) != 256);
  1338. if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
  1339. return 0;
  1340. BUG_ON(file && mddev->bitmap_offset);
  1341. bitmap = kmalloc(sizeof(*bitmap), GFP_KERNEL);
  1342. if (!bitmap)
  1343. return -ENOMEM;
  1344. memset(bitmap, 0, sizeof(*bitmap));
  1345. spin_lock_init(&bitmap->lock);
  1346. bitmap->mddev = mddev;
  1347. spin_lock_init(&bitmap->write_lock);
  1348. INIT_LIST_HEAD(&bitmap->complete_pages);
  1349. init_waitqueue_head(&bitmap->write_wait);
  1350. bitmap->write_pool = mempool_create(WRITE_POOL_SIZE, write_pool_alloc,
  1351. write_pool_free, NULL);
  1352. err = -ENOMEM;
  1353. if (!bitmap->write_pool)
  1354. goto error;
  1355. bitmap->file = file;
  1356. bitmap->offset = mddev->bitmap_offset;
  1357. if (file) get_file(file);
  1358. /* read superblock from bitmap file (this sets bitmap->chunksize) */
  1359. err = bitmap_read_sb(bitmap);
  1360. if (err)
  1361. goto error;
  1362. bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
  1363. sizeof(bitmap->chunksize));
  1364. /* now that chunksize and chunkshift are set, we can use these macros */
  1365. chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
  1366. CHUNK_BLOCK_RATIO(bitmap);
  1367. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1368. BUG_ON(!pages);
  1369. bitmap->chunks = chunks;
  1370. bitmap->pages = pages;
  1371. bitmap->missing_pages = pages;
  1372. bitmap->counter_bits = COUNTER_BITS;
  1373. bitmap->syncchunk = ~0UL;
  1374. #ifdef INJECT_FATAL_FAULT_1
  1375. bitmap->bp = NULL;
  1376. #else
  1377. bitmap->bp = kmalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1378. #endif
  1379. err = -ENOMEM;
  1380. if (!bitmap->bp)
  1381. goto error;
  1382. memset(bitmap->bp, 0, pages * sizeof(*bitmap->bp));
  1383. bitmap->flags |= BITMAP_ACTIVE;
  1384. /* now that we have some pages available, initialize the in-memory
  1385. * bitmap from the on-disk bitmap */
  1386. start = 0;
  1387. if (mddev->degraded == 0
  1388. || bitmap->events_cleared == mddev->events)
  1389. /* no need to keep dirty bits to optimise a re-add of a missing device */
  1390. start = mddev->recovery_cp;
  1391. err = bitmap_init_from_disk(bitmap, start);
  1392. if (err)
  1393. goto error;
  1394. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1395. pages, bmname(bitmap));
  1396. mddev->bitmap = bitmap;
  1397. if (file)
  1398. /* kick off the bitmap writeback daemon */
  1399. bitmap->writeback_daemon =
  1400. bitmap_start_daemon(bitmap,
  1401. bitmap_writeback_daemon,
  1402. "bitmap_wb");
  1403. if (IS_ERR(bitmap->writeback_daemon))
  1404. return PTR_ERR(bitmap->writeback_daemon);
  1405. mddev->thread->timeout = bitmap->daemon_sleep * HZ;
  1406. return bitmap_update_sb(bitmap);
  1407. error:
  1408. bitmap_free(bitmap);
  1409. return err;
  1410. }
  1411. /* the bitmap API -- for raid personalities */
  1412. EXPORT_SYMBOL(bitmap_startwrite);
  1413. EXPORT_SYMBOL(bitmap_endwrite);
  1414. EXPORT_SYMBOL(bitmap_start_sync);
  1415. EXPORT_SYMBOL(bitmap_end_sync);
  1416. EXPORT_SYMBOL(bitmap_unplug);
  1417. EXPORT_SYMBOL(bitmap_close_sync);
  1418. EXPORT_SYMBOL(bitmap_daemon_work);