bitmap.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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. ITERATE_RDEV(mddev, rdev, tmp) {
  223. if (! rdev->in_sync || rdev->faulty)
  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 (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. printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
  369. kunmap(bitmap->sb_page);
  370. }
  371. /* read the superblock from the bitmap file and initialize some bitmap fields */
  372. static int bitmap_read_sb(struct bitmap *bitmap)
  373. {
  374. char *reason = NULL;
  375. bitmap_super_t *sb;
  376. unsigned long chunksize, daemon_sleep, write_behind;
  377. unsigned long bytes_read;
  378. unsigned long long events;
  379. int err = -EINVAL;
  380. /* page 0 is the superblock, read it... */
  381. if (bitmap->file)
  382. bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read);
  383. else {
  384. bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
  385. bytes_read = PAGE_SIZE;
  386. }
  387. if (IS_ERR(bitmap->sb_page)) {
  388. err = PTR_ERR(bitmap->sb_page);
  389. bitmap->sb_page = NULL;
  390. return err;
  391. }
  392. sb = (bitmap_super_t *)kmap(bitmap->sb_page);
  393. if (bytes_read < sizeof(*sb)) { /* short read */
  394. printk(KERN_INFO "%s: bitmap file superblock truncated\n",
  395. bmname(bitmap));
  396. err = -ENOSPC;
  397. goto out;
  398. }
  399. chunksize = le32_to_cpu(sb->chunksize);
  400. daemon_sleep = le32_to_cpu(sb->daemon_sleep);
  401. write_behind = le32_to_cpu(sb->write_behind);
  402. /* verify that the bitmap-specific fields are valid */
  403. if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
  404. reason = "bad magic";
  405. else if (sb->version != cpu_to_le32(BITMAP_MAJOR))
  406. reason = "unrecognized superblock version";
  407. else if (chunksize < 512 || chunksize > (1024 * 1024 * 4))
  408. reason = "bitmap chunksize out of range (512B - 4MB)";
  409. else if ((1 << ffz(~chunksize)) != chunksize)
  410. reason = "bitmap chunksize not a power of 2";
  411. else if (daemon_sleep < 1 || daemon_sleep > 15)
  412. reason = "daemon sleep period out of range (1-15s)";
  413. else if (write_behind > COUNTER_MAX)
  414. reason = "write-behind limit out of range (0 - 16383)";
  415. if (reason) {
  416. printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
  417. bmname(bitmap), reason);
  418. goto out;
  419. }
  420. /* keep the array size field of the bitmap superblock up to date */
  421. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  422. if (!bitmap->mddev->persistent)
  423. goto success;
  424. /*
  425. * if we have a persistent array superblock, compare the
  426. * bitmap's UUID and event counter to the mddev's
  427. */
  428. if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
  429. printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
  430. bmname(bitmap));
  431. goto out;
  432. }
  433. events = le64_to_cpu(sb->events);
  434. if (events < bitmap->mddev->events) {
  435. printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
  436. "-- forcing full recovery\n", bmname(bitmap), events,
  437. (unsigned long long) bitmap->mddev->events);
  438. sb->state |= BITMAP_STALE;
  439. }
  440. success:
  441. /* assign fields using values from superblock */
  442. bitmap->chunksize = chunksize;
  443. bitmap->daemon_sleep = daemon_sleep;
  444. bitmap->daemon_lastrun = jiffies;
  445. bitmap->max_write_behind = write_behind;
  446. bitmap->flags |= sb->state;
  447. bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
  448. if (sb->state & BITMAP_STALE)
  449. bitmap->events_cleared = bitmap->mddev->events;
  450. err = 0;
  451. out:
  452. kunmap(bitmap->sb_page);
  453. if (err)
  454. bitmap_print_sb(bitmap);
  455. return err;
  456. }
  457. enum bitmap_mask_op {
  458. MASK_SET,
  459. MASK_UNSET
  460. };
  461. /* record the state of the bitmap in the superblock */
  462. static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
  463. enum bitmap_mask_op op)
  464. {
  465. bitmap_super_t *sb;
  466. unsigned long flags;
  467. spin_lock_irqsave(&bitmap->lock, flags);
  468. if (!bitmap || !bitmap->sb_page) { /* can't set the state */
  469. spin_unlock_irqrestore(&bitmap->lock, flags);
  470. return;
  471. }
  472. page_cache_get(bitmap->sb_page);
  473. spin_unlock_irqrestore(&bitmap->lock, flags);
  474. sb = (bitmap_super_t *)kmap(bitmap->sb_page);
  475. switch (op) {
  476. case MASK_SET: sb->state |= bits;
  477. break;
  478. case MASK_UNSET: sb->state &= ~bits;
  479. break;
  480. default: BUG();
  481. }
  482. kunmap(bitmap->sb_page);
  483. page_cache_release(bitmap->sb_page);
  484. }
  485. /*
  486. * general bitmap file operations
  487. */
  488. /* calculate the index of the page that contains this bit */
  489. static inline unsigned long file_page_index(unsigned long chunk)
  490. {
  491. return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
  492. }
  493. /* calculate the (bit) offset of this bit within a page */
  494. static inline unsigned long file_page_offset(unsigned long chunk)
  495. {
  496. return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
  497. }
  498. /*
  499. * return a pointer to the page in the filemap that contains the given bit
  500. *
  501. * this lookup is complicated by the fact that the bitmap sb might be exactly
  502. * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
  503. * 0 or page 1
  504. */
  505. static inline struct page *filemap_get_page(struct bitmap *bitmap,
  506. unsigned long chunk)
  507. {
  508. return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
  509. }
  510. static void bitmap_file_unmap(struct bitmap *bitmap)
  511. {
  512. struct page **map, *sb_page;
  513. unsigned long *attr;
  514. int pages;
  515. unsigned long flags;
  516. spin_lock_irqsave(&bitmap->lock, flags);
  517. map = bitmap->filemap;
  518. bitmap->filemap = NULL;
  519. attr = bitmap->filemap_attr;
  520. bitmap->filemap_attr = NULL;
  521. pages = bitmap->file_pages;
  522. bitmap->file_pages = 0;
  523. sb_page = bitmap->sb_page;
  524. bitmap->sb_page = NULL;
  525. spin_unlock_irqrestore(&bitmap->lock, flags);
  526. while (pages--)
  527. if (map[pages]->index != 0) /* 0 is sb_page, release it below */
  528. page_cache_release(map[pages]);
  529. kfree(map);
  530. kfree(attr);
  531. if (sb_page)
  532. page_cache_release(sb_page);
  533. }
  534. static void bitmap_stop_daemon(struct bitmap *bitmap);
  535. /* dequeue the next item in a page list -- don't call from irq context */
  536. static struct page_list *dequeue_page(struct bitmap *bitmap)
  537. {
  538. struct page_list *item = NULL;
  539. struct list_head *head = &bitmap->complete_pages;
  540. spin_lock(&bitmap->write_lock);
  541. if (list_empty(head))
  542. goto out;
  543. item = list_entry(head->prev, struct page_list, list);
  544. list_del(head->prev);
  545. out:
  546. spin_unlock(&bitmap->write_lock);
  547. return item;
  548. }
  549. static void drain_write_queues(struct bitmap *bitmap)
  550. {
  551. struct page_list *item;
  552. while ((item = dequeue_page(bitmap))) {
  553. /* don't bother to wait */
  554. page_cache_release(item->page);
  555. mempool_free(item, bitmap->write_pool);
  556. }
  557. wake_up(&bitmap->write_wait);
  558. }
  559. static void bitmap_file_put(struct bitmap *bitmap)
  560. {
  561. struct file *file;
  562. struct inode *inode;
  563. unsigned long flags;
  564. spin_lock_irqsave(&bitmap->lock, flags);
  565. file = bitmap->file;
  566. bitmap->file = NULL;
  567. spin_unlock_irqrestore(&bitmap->lock, flags);
  568. bitmap_stop_daemon(bitmap);
  569. drain_write_queues(bitmap);
  570. bitmap_file_unmap(bitmap);
  571. if (file) {
  572. inode = file->f_mapping->host;
  573. spin_lock(&inode->i_lock);
  574. atomic_set(&inode->i_writecount, 1); /* allow writes again */
  575. spin_unlock(&inode->i_lock);
  576. fput(file);
  577. }
  578. }
  579. /*
  580. * bitmap_file_kick - if an error occurs while manipulating the bitmap file
  581. * then it is no longer reliable, so we stop using it and we mark the file
  582. * as failed in the superblock
  583. */
  584. static void bitmap_file_kick(struct bitmap *bitmap)
  585. {
  586. char *path, *ptr = NULL;
  587. bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
  588. bitmap_update_sb(bitmap);
  589. if (bitmap->file) {
  590. path = kmalloc(PAGE_SIZE, GFP_KERNEL);
  591. if (path)
  592. ptr = file_path(bitmap->file, path, PAGE_SIZE);
  593. printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
  594. bmname(bitmap), ptr ? ptr : "");
  595. kfree(path);
  596. }
  597. bitmap_file_put(bitmap);
  598. return;
  599. }
  600. enum bitmap_page_attr {
  601. BITMAP_PAGE_DIRTY = 1, // there are set bits that need to be synced
  602. BITMAP_PAGE_CLEAN = 2, // there are bits that might need to be cleared
  603. BITMAP_PAGE_NEEDWRITE=4, // there are cleared bits that need to be synced
  604. };
  605. static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
  606. enum bitmap_page_attr attr)
  607. {
  608. bitmap->filemap_attr[page->index] |= attr;
  609. }
  610. static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
  611. enum bitmap_page_attr attr)
  612. {
  613. bitmap->filemap_attr[page->index] &= ~attr;
  614. }
  615. static inline unsigned long get_page_attr(struct bitmap *bitmap, struct page *page)
  616. {
  617. return bitmap->filemap_attr[page->index];
  618. }
  619. /*
  620. * bitmap_file_set_bit -- called before performing a write to the md device
  621. * to set (and eventually sync) a particular bit in the bitmap file
  622. *
  623. * we set the bit immediately, then we record the page number so that
  624. * when an unplug occurs, we can flush the dirty pages out to disk
  625. */
  626. static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
  627. {
  628. unsigned long bit;
  629. struct page *page;
  630. void *kaddr;
  631. unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
  632. if (!bitmap->filemap) {
  633. return;
  634. }
  635. page = filemap_get_page(bitmap, chunk);
  636. bit = file_page_offset(chunk);
  637. /* make sure the page stays cached until it gets written out */
  638. if (! (get_page_attr(bitmap, page) & BITMAP_PAGE_DIRTY))
  639. page_cache_get(page);
  640. /* set the bit */
  641. kaddr = kmap_atomic(page, KM_USER0);
  642. set_bit(bit, kaddr);
  643. kunmap_atomic(kaddr, KM_USER0);
  644. PRINTK("set file bit %lu page %lu\n", bit, page->index);
  645. /* record page number so it gets flushed to disk when unplug occurs */
  646. set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  647. }
  648. /* this gets called when the md device is ready to unplug its underlying
  649. * (slave) device queues -- before we let any writes go down, we need to
  650. * sync the dirty pages of the bitmap file to disk */
  651. int bitmap_unplug(struct bitmap *bitmap)
  652. {
  653. unsigned long i, attr, flags;
  654. struct page *page;
  655. int wait = 0;
  656. int err;
  657. if (!bitmap)
  658. return 0;
  659. /* look at each page to see if there are any set bits that need to be
  660. * flushed out to disk */
  661. for (i = 0; i < bitmap->file_pages; i++) {
  662. spin_lock_irqsave(&bitmap->lock, flags);
  663. if (!bitmap->filemap) {
  664. spin_unlock_irqrestore(&bitmap->lock, flags);
  665. return 0;
  666. }
  667. page = bitmap->filemap[i];
  668. attr = get_page_attr(bitmap, page);
  669. clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  670. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  671. if ((attr & BITMAP_PAGE_DIRTY))
  672. wait = 1;
  673. spin_unlock_irqrestore(&bitmap->lock, flags);
  674. if (attr & (BITMAP_PAGE_DIRTY | BITMAP_PAGE_NEEDWRITE)) {
  675. err = write_page(bitmap, page, 0);
  676. if (err == -EAGAIN) {
  677. if (attr & BITMAP_PAGE_DIRTY)
  678. err = write_page(bitmap, page, 1);
  679. else
  680. err = 0;
  681. }
  682. if (err)
  683. return 1;
  684. }
  685. }
  686. if (wait) { /* if any writes were performed, we need to wait on them */
  687. if (bitmap->file) {
  688. spin_lock_irq(&bitmap->write_lock);
  689. wait_event_lock_irq(bitmap->write_wait,
  690. list_empty(&bitmap->complete_pages), bitmap->write_lock,
  691. wake_up_process(bitmap->writeback_daemon->tsk));
  692. spin_unlock_irq(&bitmap->write_lock);
  693. } else
  694. wait_event(bitmap->mddev->sb_wait,
  695. atomic_read(&bitmap->mddev->pending_writes)==0);
  696. }
  697. return 0;
  698. }
  699. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
  700. /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
  701. * the in-memory bitmap from the on-disk bitmap -- also, sets up the
  702. * memory mapping of the bitmap file
  703. * Special cases:
  704. * if there's no bitmap file, or if the bitmap file had been
  705. * previously kicked from the array, we mark all the bits as
  706. * 1's in order to cause a full resync.
  707. *
  708. * We ignore all bits for sectors that end earlier than 'start'.
  709. * This is used when reading an out-of-date bitmap...
  710. */
  711. static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
  712. {
  713. unsigned long i, chunks, index, oldindex, bit;
  714. struct page *page = NULL, *oldpage = NULL;
  715. unsigned long num_pages, bit_cnt = 0;
  716. struct file *file;
  717. unsigned long bytes, offset, dummy;
  718. int outofdate;
  719. int ret = -ENOSPC;
  720. chunks = bitmap->chunks;
  721. file = bitmap->file;
  722. BUG_ON(!file && !bitmap->offset);
  723. #ifdef INJECT_FAULTS_3
  724. outofdate = 1;
  725. #else
  726. outofdate = bitmap->flags & BITMAP_STALE;
  727. #endif
  728. if (outofdate)
  729. printk(KERN_INFO "%s: bitmap file is out of date, doing full "
  730. "recovery\n", bmname(bitmap));
  731. bytes = (chunks + 7) / 8;
  732. num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
  733. if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
  734. printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
  735. bmname(bitmap),
  736. (unsigned long) i_size_read(file->f_mapping->host),
  737. bytes + sizeof(bitmap_super_t));
  738. goto out;
  739. }
  740. ret = -ENOMEM;
  741. bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  742. if (!bitmap->filemap)
  743. goto out;
  744. bitmap->filemap_attr = kmalloc(sizeof(long) * num_pages, GFP_KERNEL);
  745. if (!bitmap->filemap_attr)
  746. goto out;
  747. memset(bitmap->filemap_attr, 0, sizeof(long) * num_pages);
  748. oldindex = ~0L;
  749. for (i = 0; i < chunks; i++) {
  750. index = file_page_index(i);
  751. bit = file_page_offset(i);
  752. if (index != oldindex) { /* this is a new page, read it in */
  753. /* unmap the old page, we're done with it */
  754. if (oldpage != NULL)
  755. kunmap(oldpage);
  756. if (index == 0) {
  757. /*
  758. * if we're here then the superblock page
  759. * contains some bits (PAGE_SIZE != sizeof sb)
  760. * we've already read it in, so just use it
  761. */
  762. page = bitmap->sb_page;
  763. offset = sizeof(bitmap_super_t);
  764. } else if (file) {
  765. page = read_page(file, index, &dummy);
  766. offset = 0;
  767. } else {
  768. page = read_sb_page(bitmap->mddev, bitmap->offset, index);
  769. offset = 0;
  770. }
  771. if (IS_ERR(page)) { /* read error */
  772. ret = PTR_ERR(page);
  773. goto out;
  774. }
  775. oldindex = index;
  776. oldpage = page;
  777. kmap(page);
  778. if (outofdate) {
  779. /*
  780. * if bitmap is out of date, dirty the
  781. * whole page and write it out
  782. */
  783. memset(page_address(page) + offset, 0xff,
  784. PAGE_SIZE - offset);
  785. ret = write_page(bitmap, page, 1);
  786. if (ret) {
  787. kunmap(page);
  788. /* release, page not in filemap yet */
  789. page_cache_release(page);
  790. goto out;
  791. }
  792. }
  793. bitmap->filemap[bitmap->file_pages++] = page;
  794. }
  795. if (test_bit(bit, page_address(page))) {
  796. /* if the disk bit is set, set the memory bit */
  797. bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
  798. ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
  799. );
  800. bit_cnt++;
  801. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  802. }
  803. }
  804. /* everything went OK */
  805. ret = 0;
  806. bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
  807. if (page) /* unmap the last page */
  808. kunmap(page);
  809. if (bit_cnt) { /* Kick recovery if any bits were set */
  810. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  811. md_wakeup_thread(bitmap->mddev->thread);
  812. }
  813. out:
  814. printk(KERN_INFO "%s: bitmap initialized from disk: "
  815. "read %lu/%lu pages, set %lu bits, status: %d\n",
  816. bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
  817. return ret;
  818. }
  819. void bitmap_write_all(struct bitmap *bitmap)
  820. {
  821. /* We don't actually write all bitmap blocks here,
  822. * just flag them as needing to be written
  823. */
  824. unsigned long chunks = bitmap->chunks;
  825. unsigned long bytes = (chunks+7)/8 + sizeof(bitmap_super_t);
  826. unsigned long num_pages = (bytes + PAGE_SIZE-1) / PAGE_SIZE;
  827. while (num_pages--)
  828. bitmap->filemap_attr[num_pages] |= BITMAP_PAGE_NEEDWRITE;
  829. }
  830. static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
  831. {
  832. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  833. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  834. bitmap->bp[page].count += inc;
  835. /*
  836. if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
  837. (unsigned long long)offset, inc, bitmap->bp[page].count);
  838. */
  839. bitmap_checkfree(bitmap, page);
  840. }
  841. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  842. sector_t offset, int *blocks,
  843. int create);
  844. /*
  845. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  846. * out to disk
  847. */
  848. int bitmap_daemon_work(struct bitmap *bitmap)
  849. {
  850. unsigned long j;
  851. unsigned long flags;
  852. struct page *page = NULL, *lastpage = NULL;
  853. int err = 0;
  854. int blocks;
  855. int attr;
  856. if (bitmap == NULL)
  857. return 0;
  858. if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
  859. return 0;
  860. bitmap->daemon_lastrun = jiffies;
  861. for (j = 0; j < bitmap->chunks; j++) {
  862. bitmap_counter_t *bmc;
  863. spin_lock_irqsave(&bitmap->lock, flags);
  864. if (!bitmap->filemap) {
  865. /* error or shutdown */
  866. spin_unlock_irqrestore(&bitmap->lock, flags);
  867. break;
  868. }
  869. page = filemap_get_page(bitmap, j);
  870. if (page != lastpage) {
  871. /* skip this page unless it's marked as needing cleaning */
  872. if (!((attr=get_page_attr(bitmap, page)) & BITMAP_PAGE_CLEAN)) {
  873. if (attr & BITMAP_PAGE_NEEDWRITE) {
  874. page_cache_get(page);
  875. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  876. }
  877. spin_unlock_irqrestore(&bitmap->lock, flags);
  878. if (attr & BITMAP_PAGE_NEEDWRITE) {
  879. switch (write_page(bitmap, page, 0)) {
  880. case -EAGAIN:
  881. set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  882. break;
  883. case 0:
  884. break;
  885. default:
  886. bitmap_file_kick(bitmap);
  887. }
  888. page_cache_release(page);
  889. }
  890. continue;
  891. }
  892. /* grab the new page, sync and release the old */
  893. page_cache_get(page);
  894. if (lastpage != NULL) {
  895. if (get_page_attr(bitmap, lastpage) & BITMAP_PAGE_NEEDWRITE) {
  896. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  897. spin_unlock_irqrestore(&bitmap->lock, flags);
  898. err = write_page(bitmap, lastpage, 0);
  899. if (err == -EAGAIN) {
  900. err = 0;
  901. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  902. }
  903. } else {
  904. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  905. spin_unlock_irqrestore(&bitmap->lock, flags);
  906. }
  907. kunmap(lastpage);
  908. page_cache_release(lastpage);
  909. if (err)
  910. bitmap_file_kick(bitmap);
  911. } else
  912. spin_unlock_irqrestore(&bitmap->lock, flags);
  913. lastpage = page;
  914. kmap(page);
  915. /*
  916. printk("bitmap clean at page %lu\n", j);
  917. */
  918. spin_lock_irqsave(&bitmap->lock, flags);
  919. clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  920. }
  921. bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
  922. &blocks, 0);
  923. if (bmc) {
  924. /*
  925. if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
  926. */
  927. if (*bmc == 2) {
  928. *bmc=1; /* maybe clear the bit next time */
  929. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  930. } else if (*bmc == 1) {
  931. /* we can clear the bit */
  932. *bmc = 0;
  933. bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
  934. -1);
  935. /* clear the bit */
  936. clear_bit(file_page_offset(j), page_address(page));
  937. }
  938. }
  939. spin_unlock_irqrestore(&bitmap->lock, flags);
  940. }
  941. /* now sync the final page */
  942. if (lastpage != NULL) {
  943. kunmap(lastpage);
  944. spin_lock_irqsave(&bitmap->lock, flags);
  945. if (get_page_attr(bitmap, lastpage) &BITMAP_PAGE_NEEDWRITE) {
  946. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  947. spin_unlock_irqrestore(&bitmap->lock, flags);
  948. err = write_page(bitmap, lastpage, 0);
  949. if (err == -EAGAIN) {
  950. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  951. err = 0;
  952. }
  953. } else {
  954. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  955. spin_unlock_irqrestore(&bitmap->lock, flags);
  956. }
  957. page_cache_release(lastpage);
  958. }
  959. return err;
  960. }
  961. static void daemon_exit(struct bitmap *bitmap, mdk_thread_t **daemon)
  962. {
  963. mdk_thread_t *dmn;
  964. unsigned long flags;
  965. /* if no one is waiting on us, we'll free the md thread struct
  966. * and exit, otherwise we let the waiter clean things up */
  967. spin_lock_irqsave(&bitmap->lock, flags);
  968. if ((dmn = *daemon)) { /* no one is waiting, cleanup and exit */
  969. *daemon = NULL;
  970. spin_unlock_irqrestore(&bitmap->lock, flags);
  971. kfree(dmn);
  972. complete_and_exit(NULL, 0); /* do_exit not exported */
  973. }
  974. spin_unlock_irqrestore(&bitmap->lock, flags);
  975. }
  976. static void bitmap_writeback_daemon(mddev_t *mddev)
  977. {
  978. struct bitmap *bitmap = mddev->bitmap;
  979. struct page *page;
  980. struct page_list *item;
  981. int err = 0;
  982. if (signal_pending(current)) {
  983. printk(KERN_INFO
  984. "%s: bitmap writeback daemon got signal, exiting...\n",
  985. bmname(bitmap));
  986. err = -EINTR;
  987. goto out;
  988. }
  989. if (bitmap == NULL)
  990. /* about to be stopped. */
  991. return;
  992. PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
  993. /* wait on bitmap page writebacks */
  994. while ((item = dequeue_page(bitmap))) {
  995. page = item->page;
  996. mempool_free(item, bitmap->write_pool);
  997. PRINTK("wait on page writeback: %p\n", page);
  998. wait_on_page_writeback(page);
  999. PRINTK("finished page writeback: %p\n", page);
  1000. err = PageError(page);
  1001. page_cache_release(page);
  1002. if (err) {
  1003. printk(KERN_WARNING "%s: bitmap file writeback "
  1004. "failed (page %lu): %d\n",
  1005. bmname(bitmap), page->index, err);
  1006. bitmap_file_kick(bitmap);
  1007. goto out;
  1008. }
  1009. }
  1010. out:
  1011. wake_up(&bitmap->write_wait);
  1012. if (err) {
  1013. printk(KERN_INFO "%s: bitmap writeback daemon exiting (%d)\n",
  1014. bmname(bitmap), err);
  1015. daemon_exit(bitmap, &bitmap->writeback_daemon);
  1016. }
  1017. }
  1018. static mdk_thread_t *bitmap_start_daemon(struct bitmap *bitmap,
  1019. void (*func)(mddev_t *), char *name)
  1020. {
  1021. mdk_thread_t *daemon;
  1022. char namebuf[32];
  1023. #ifdef INJECT_FATAL_FAULT_2
  1024. daemon = NULL;
  1025. #else
  1026. sprintf(namebuf, "%%s_%s", name);
  1027. daemon = md_register_thread(func, bitmap->mddev, namebuf);
  1028. #endif
  1029. if (!daemon) {
  1030. printk(KERN_ERR "%s: failed to start bitmap daemon\n",
  1031. bmname(bitmap));
  1032. return ERR_PTR(-ECHILD);
  1033. }
  1034. md_wakeup_thread(daemon); /* start it running */
  1035. PRINTK("%s: %s daemon (pid %d) started...\n",
  1036. bmname(bitmap), name, daemon->tsk->pid);
  1037. return daemon;
  1038. }
  1039. static void bitmap_stop_daemon(struct bitmap *bitmap)
  1040. {
  1041. /* the daemon can't stop itself... it'll just exit instead... */
  1042. if (bitmap->writeback_daemon && ! IS_ERR(bitmap->writeback_daemon) &&
  1043. current->pid != bitmap->writeback_daemon->tsk->pid) {
  1044. mdk_thread_t *daemon;
  1045. unsigned long flags;
  1046. spin_lock_irqsave(&bitmap->lock, flags);
  1047. daemon = bitmap->writeback_daemon;
  1048. bitmap->writeback_daemon = NULL;
  1049. spin_unlock_irqrestore(&bitmap->lock, flags);
  1050. if (daemon && ! IS_ERR(daemon))
  1051. md_unregister_thread(daemon); /* destroy the thread */
  1052. }
  1053. }
  1054. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1055. sector_t offset, int *blocks,
  1056. int create)
  1057. {
  1058. /* If 'create', we might release the lock and reclaim it.
  1059. * The lock must have been taken with interrupts enabled.
  1060. * If !create, we don't release the lock.
  1061. */
  1062. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  1063. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1064. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  1065. sector_t csize;
  1066. if (bitmap_checkpage(bitmap, page, create) < 0) {
  1067. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1068. *blocks = csize - (offset & (csize- 1));
  1069. return NULL;
  1070. }
  1071. /* now locked ... */
  1072. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  1073. /* should we use the first or second counter field
  1074. * of the hijacked pointer? */
  1075. int hi = (pageoff > PAGE_COUNTER_MASK);
  1076. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
  1077. PAGE_COUNTER_SHIFT - 1);
  1078. *blocks = csize - (offset & (csize- 1));
  1079. return &((bitmap_counter_t *)
  1080. &bitmap->bp[page].map)[hi];
  1081. } else { /* page is allocated */
  1082. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1083. *blocks = csize - (offset & (csize- 1));
  1084. return (bitmap_counter_t *)
  1085. &(bitmap->bp[page].map[pageoff]);
  1086. }
  1087. }
  1088. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
  1089. {
  1090. if (!bitmap) return 0;
  1091. if (behind) {
  1092. atomic_inc(&bitmap->behind_writes);
  1093. PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
  1094. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1095. }
  1096. while (sectors) {
  1097. int blocks;
  1098. bitmap_counter_t *bmc;
  1099. spin_lock_irq(&bitmap->lock);
  1100. bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
  1101. if (!bmc) {
  1102. spin_unlock_irq(&bitmap->lock);
  1103. return 0;
  1104. }
  1105. switch(*bmc) {
  1106. case 0:
  1107. bitmap_file_set_bit(bitmap, offset);
  1108. bitmap_count_page(bitmap,offset, 1);
  1109. blk_plug_device(bitmap->mddev->queue);
  1110. /* fall through */
  1111. case 1:
  1112. *bmc = 2;
  1113. }
  1114. if ((*bmc & COUNTER_MAX) == COUNTER_MAX) BUG();
  1115. (*bmc)++;
  1116. spin_unlock_irq(&bitmap->lock);
  1117. offset += blocks;
  1118. if (sectors > blocks)
  1119. sectors -= blocks;
  1120. else sectors = 0;
  1121. }
  1122. return 0;
  1123. }
  1124. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1125. int success, int behind)
  1126. {
  1127. if (!bitmap) return;
  1128. if (behind) {
  1129. atomic_dec(&bitmap->behind_writes);
  1130. PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
  1131. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1132. }
  1133. while (sectors) {
  1134. int blocks;
  1135. unsigned long flags;
  1136. bitmap_counter_t *bmc;
  1137. spin_lock_irqsave(&bitmap->lock, flags);
  1138. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1139. if (!bmc) {
  1140. spin_unlock_irqrestore(&bitmap->lock, flags);
  1141. return;
  1142. }
  1143. if (!success && ! (*bmc & NEEDED_MASK))
  1144. *bmc |= NEEDED_MASK;
  1145. (*bmc)--;
  1146. if (*bmc <= 2) {
  1147. set_page_attr(bitmap,
  1148. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1149. BITMAP_PAGE_CLEAN);
  1150. }
  1151. spin_unlock_irqrestore(&bitmap->lock, flags);
  1152. offset += blocks;
  1153. if (sectors > blocks)
  1154. sectors -= blocks;
  1155. else sectors = 0;
  1156. }
  1157. }
  1158. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
  1159. int degraded)
  1160. {
  1161. bitmap_counter_t *bmc;
  1162. int rv;
  1163. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1164. *blocks = 1024;
  1165. return 1; /* always resync if no bitmap */
  1166. }
  1167. spin_lock_irq(&bitmap->lock);
  1168. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1169. rv = 0;
  1170. if (bmc) {
  1171. /* locked */
  1172. if (RESYNC(*bmc))
  1173. rv = 1;
  1174. else if (NEEDED(*bmc)) {
  1175. rv = 1;
  1176. if (!degraded) { /* don't set/clear bits if degraded */
  1177. *bmc |= RESYNC_MASK;
  1178. *bmc &= ~NEEDED_MASK;
  1179. }
  1180. }
  1181. }
  1182. spin_unlock_irq(&bitmap->lock);
  1183. return rv;
  1184. }
  1185. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
  1186. {
  1187. bitmap_counter_t *bmc;
  1188. unsigned long flags;
  1189. /*
  1190. if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
  1191. */ if (bitmap == NULL) {
  1192. *blocks = 1024;
  1193. return;
  1194. }
  1195. spin_lock_irqsave(&bitmap->lock, flags);
  1196. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1197. if (bmc == NULL)
  1198. goto unlock;
  1199. /* locked */
  1200. /*
  1201. if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
  1202. */
  1203. if (RESYNC(*bmc)) {
  1204. *bmc &= ~RESYNC_MASK;
  1205. if (!NEEDED(*bmc) && aborted)
  1206. *bmc |= NEEDED_MASK;
  1207. else {
  1208. if (*bmc <= 2) {
  1209. set_page_attr(bitmap,
  1210. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1211. BITMAP_PAGE_CLEAN);
  1212. }
  1213. }
  1214. }
  1215. unlock:
  1216. spin_unlock_irqrestore(&bitmap->lock, flags);
  1217. }
  1218. void bitmap_close_sync(struct bitmap *bitmap)
  1219. {
  1220. /* Sync has finished, and any bitmap chunks that weren't synced
  1221. * properly have been aborted. It remains to us to clear the
  1222. * RESYNC bit wherever it is still on
  1223. */
  1224. sector_t sector = 0;
  1225. int blocks;
  1226. if (!bitmap) return;
  1227. while (sector < bitmap->mddev->resync_max_sectors) {
  1228. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1229. /*
  1230. if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
  1231. (unsigned long long)sector, blocks);
  1232. */ sector += blocks;
  1233. }
  1234. }
  1235. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1236. {
  1237. /* For each chunk covered by any of these sectors, set the
  1238. * counter to 1 and set resync_needed. They should all
  1239. * be 0 at this point
  1240. */
  1241. int secs;
  1242. bitmap_counter_t *bmc;
  1243. spin_lock_irq(&bitmap->lock);
  1244. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1245. if (!bmc) {
  1246. spin_unlock_irq(&bitmap->lock);
  1247. return;
  1248. }
  1249. if (! *bmc) {
  1250. struct page *page;
  1251. *bmc = 1 | (needed?NEEDED_MASK:0);
  1252. bitmap_count_page(bitmap, offset, 1);
  1253. page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
  1254. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1255. }
  1256. spin_unlock_irq(&bitmap->lock);
  1257. }
  1258. /*
  1259. * flush out any pending updates
  1260. */
  1261. void bitmap_flush(mddev_t *mddev)
  1262. {
  1263. struct bitmap *bitmap = mddev->bitmap;
  1264. int sleep;
  1265. if (!bitmap) /* there was no bitmap */
  1266. return;
  1267. /* run the daemon_work three time to ensure everything is flushed
  1268. * that can be
  1269. */
  1270. sleep = bitmap->daemon_sleep;
  1271. bitmap->daemon_sleep = 0;
  1272. bitmap_daemon_work(bitmap);
  1273. bitmap_daemon_work(bitmap);
  1274. bitmap_daemon_work(bitmap);
  1275. bitmap->daemon_sleep = sleep;
  1276. bitmap_update_sb(bitmap);
  1277. }
  1278. /*
  1279. * free memory that was allocated
  1280. */
  1281. static void bitmap_free(struct bitmap *bitmap)
  1282. {
  1283. unsigned long k, pages;
  1284. struct bitmap_page *bp;
  1285. if (!bitmap) /* there was no bitmap */
  1286. return;
  1287. /* release the bitmap file and kill the daemon */
  1288. bitmap_file_put(bitmap);
  1289. bp = bitmap->bp;
  1290. pages = bitmap->pages;
  1291. /* free all allocated memory */
  1292. mempool_destroy(bitmap->write_pool);
  1293. if (bp) /* deallocate the page memory */
  1294. for (k = 0; k < pages; k++)
  1295. if (bp[k].map && !bp[k].hijacked)
  1296. kfree(bp[k].map);
  1297. kfree(bp);
  1298. kfree(bitmap);
  1299. }
  1300. void bitmap_destroy(mddev_t *mddev)
  1301. {
  1302. struct bitmap *bitmap = mddev->bitmap;
  1303. if (!bitmap) /* there was no bitmap */
  1304. return;
  1305. mddev->bitmap = NULL; /* disconnect from the md device */
  1306. bitmap_free(bitmap);
  1307. }
  1308. /*
  1309. * initialize the bitmap structure
  1310. * if this returns an error, bitmap_destroy must be called to do clean up
  1311. */
  1312. int bitmap_create(mddev_t *mddev)
  1313. {
  1314. struct bitmap *bitmap;
  1315. unsigned long blocks = mddev->resync_max_sectors;
  1316. unsigned long chunks;
  1317. unsigned long pages;
  1318. struct file *file = mddev->bitmap_file;
  1319. int err;
  1320. sector_t start;
  1321. BUG_ON(sizeof(bitmap_super_t) != 256);
  1322. if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
  1323. return 0;
  1324. BUG_ON(file && mddev->bitmap_offset);
  1325. bitmap = kmalloc(sizeof(*bitmap), GFP_KERNEL);
  1326. if (!bitmap)
  1327. return -ENOMEM;
  1328. memset(bitmap, 0, sizeof(*bitmap));
  1329. spin_lock_init(&bitmap->lock);
  1330. bitmap->mddev = mddev;
  1331. spin_lock_init(&bitmap->write_lock);
  1332. INIT_LIST_HEAD(&bitmap->complete_pages);
  1333. init_waitqueue_head(&bitmap->write_wait);
  1334. bitmap->write_pool = mempool_create(WRITE_POOL_SIZE, write_pool_alloc,
  1335. write_pool_free, NULL);
  1336. err = -ENOMEM;
  1337. if (!bitmap->write_pool)
  1338. goto error;
  1339. bitmap->file = file;
  1340. bitmap->offset = mddev->bitmap_offset;
  1341. if (file) get_file(file);
  1342. /* read superblock from bitmap file (this sets bitmap->chunksize) */
  1343. err = bitmap_read_sb(bitmap);
  1344. if (err)
  1345. goto error;
  1346. bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
  1347. sizeof(bitmap->chunksize));
  1348. /* now that chunksize and chunkshift are set, we can use these macros */
  1349. chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
  1350. CHUNK_BLOCK_RATIO(bitmap);
  1351. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1352. BUG_ON(!pages);
  1353. bitmap->chunks = chunks;
  1354. bitmap->pages = pages;
  1355. bitmap->missing_pages = pages;
  1356. bitmap->counter_bits = COUNTER_BITS;
  1357. bitmap->syncchunk = ~0UL;
  1358. #ifdef INJECT_FATAL_FAULT_1
  1359. bitmap->bp = NULL;
  1360. #else
  1361. bitmap->bp = kmalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1362. #endif
  1363. err = -ENOMEM;
  1364. if (!bitmap->bp)
  1365. goto error;
  1366. memset(bitmap->bp, 0, pages * sizeof(*bitmap->bp));
  1367. bitmap->flags |= BITMAP_ACTIVE;
  1368. /* now that we have some pages available, initialize the in-memory
  1369. * bitmap from the on-disk bitmap */
  1370. start = 0;
  1371. if (mddev->degraded == 0
  1372. || bitmap->events_cleared == mddev->events)
  1373. /* no need to keep dirty bits to optimise a re-add of a missing device */
  1374. start = mddev->recovery_cp;
  1375. err = bitmap_init_from_disk(bitmap, start);
  1376. if (err)
  1377. goto error;
  1378. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1379. pages, bmname(bitmap));
  1380. mddev->bitmap = bitmap;
  1381. if (file)
  1382. /* kick off the bitmap writeback daemon */
  1383. bitmap->writeback_daemon =
  1384. bitmap_start_daemon(bitmap,
  1385. bitmap_writeback_daemon,
  1386. "bitmap_wb");
  1387. if (IS_ERR(bitmap->writeback_daemon))
  1388. return PTR_ERR(bitmap->writeback_daemon);
  1389. return bitmap_update_sb(bitmap);
  1390. error:
  1391. bitmap_free(bitmap);
  1392. return err;
  1393. }
  1394. /* the bitmap API -- for raid personalities */
  1395. EXPORT_SYMBOL(bitmap_startwrite);
  1396. EXPORT_SYMBOL(bitmap_endwrite);
  1397. EXPORT_SYMBOL(bitmap_start_sync);
  1398. EXPORT_SYMBOL(bitmap_end_sync);
  1399. EXPORT_SYMBOL(bitmap_unplug);
  1400. EXPORT_SYMBOL(bitmap_close_sync);
  1401. EXPORT_SYMBOL(bitmap_daemon_work);