bitmap.c 41 KB

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