bitmap.c 40 KB

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