bitmap.c 42 KB

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