bitmap.c 41 KB

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