bitmap.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117
  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. */
  16. #include <linux/blkdev.h>
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/slab.h>
  20. #include <linux/init.h>
  21. #include <linux/timer.h>
  22. #include <linux/sched.h>
  23. #include <linux/list.h>
  24. #include <linux/file.h>
  25. #include <linux/mount.h>
  26. #include <linux/buffer_head.h>
  27. #include "md.h"
  28. #include "bitmap.h"
  29. static inline char *bmname(struct bitmap *bitmap)
  30. {
  31. return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
  32. }
  33. /*
  34. * just a placeholder - calls kmalloc for bitmap pages
  35. */
  36. static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
  37. {
  38. unsigned char *page;
  39. page = kzalloc(PAGE_SIZE, GFP_NOIO);
  40. if (!page)
  41. printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
  42. else
  43. pr_debug("%s: bitmap_alloc_page: allocated page at %p\n",
  44. bmname(bitmap), page);
  45. return page;
  46. }
  47. /*
  48. * for now just a placeholder -- just calls kfree for bitmap pages
  49. */
  50. static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
  51. {
  52. pr_debug("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
  53. kfree(page);
  54. }
  55. /*
  56. * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
  57. *
  58. * 1) check to see if this page is allocated, if it's not then try to alloc
  59. * 2) if the alloc fails, set the page's hijacked flag so we'll use the
  60. * page pointer directly as a counter
  61. *
  62. * if we find our page, we increment the page's refcount so that it stays
  63. * allocated while we're using it
  64. */
  65. static int bitmap_checkpage(struct bitmap *bitmap,
  66. unsigned long page, int create)
  67. __releases(bitmap->lock)
  68. __acquires(bitmap->lock)
  69. {
  70. unsigned char *mappage;
  71. if (page >= bitmap->pages) {
  72. /* This can happen if bitmap_start_sync goes beyond
  73. * End-of-device while looking for a whole page.
  74. * It is harmless.
  75. */
  76. return -EINVAL;
  77. }
  78. if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
  79. return 0;
  80. if (bitmap->bp[page].map) /* page is already allocated, just return */
  81. return 0;
  82. if (!create)
  83. return -ENOENT;
  84. /* this page has not been allocated yet */
  85. spin_unlock_irq(&bitmap->lock);
  86. mappage = bitmap_alloc_page(bitmap);
  87. spin_lock_irq(&bitmap->lock);
  88. if (mappage == NULL) {
  89. pr_debug("%s: bitmap map page allocation failed, hijacking\n",
  90. bmname(bitmap));
  91. /* failed - set the hijacked flag so that we can use the
  92. * pointer as a counter */
  93. if (!bitmap->bp[page].map)
  94. bitmap->bp[page].hijacked = 1;
  95. } else if (bitmap->bp[page].map ||
  96. bitmap->bp[page].hijacked) {
  97. /* somebody beat us to getting the page */
  98. bitmap_free_page(bitmap, mappage);
  99. return 0;
  100. } else {
  101. /* no page was in place and we have one, so install it */
  102. bitmap->bp[page].map = mappage;
  103. bitmap->missing_pages--;
  104. }
  105. return 0;
  106. }
  107. /* if page is completely empty, put it back on the free list, or dealloc it */
  108. /* if page was hijacked, unmark the flag so it might get alloced next time */
  109. /* Note: lock should be held when calling this */
  110. static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
  111. {
  112. char *ptr;
  113. if (bitmap->bp[page].count) /* page is still busy */
  114. return;
  115. /* page is no longer in use, it can be released */
  116. if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
  117. bitmap->bp[page].hijacked = 0;
  118. bitmap->bp[page].map = NULL;
  119. } else {
  120. /* normal case, free the page */
  121. ptr = bitmap->bp[page].map;
  122. bitmap->bp[page].map = NULL;
  123. bitmap->missing_pages++;
  124. bitmap_free_page(bitmap, ptr);
  125. }
  126. }
  127. /*
  128. * bitmap file handling - read and write the bitmap file and its superblock
  129. */
  130. /*
  131. * basic page I/O operations
  132. */
  133. /* IO operations when bitmap is stored near all superblocks */
  134. static struct page *read_sb_page(struct mddev *mddev, loff_t offset,
  135. struct page *page,
  136. unsigned long index, int size)
  137. {
  138. /* choose a good rdev and read the page from there */
  139. struct md_rdev *rdev;
  140. sector_t target;
  141. int did_alloc = 0;
  142. if (!page) {
  143. page = alloc_page(GFP_KERNEL);
  144. if (!page)
  145. return ERR_PTR(-ENOMEM);
  146. did_alloc = 1;
  147. }
  148. list_for_each_entry(rdev, &mddev->disks, same_set) {
  149. if (! test_bit(In_sync, &rdev->flags)
  150. || test_bit(Faulty, &rdev->flags))
  151. continue;
  152. target = offset + index * (PAGE_SIZE/512);
  153. if (sync_page_io(rdev, target,
  154. roundup(size, bdev_logical_block_size(rdev->bdev)),
  155. page, READ, true)) {
  156. page->index = index;
  157. attach_page_buffers(page, NULL); /* so that free_buffer will
  158. * quietly no-op */
  159. return page;
  160. }
  161. }
  162. if (did_alloc)
  163. put_page(page);
  164. return ERR_PTR(-EIO);
  165. }
  166. static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev)
  167. {
  168. /* Iterate the disks of an mddev, using rcu to protect access to the
  169. * linked list, and raising the refcount of devices we return to ensure
  170. * they don't disappear while in use.
  171. * As devices are only added or removed when raid_disk is < 0 and
  172. * nr_pending is 0 and In_sync is clear, the entries we return will
  173. * still be in the same position on the list when we re-enter
  174. * list_for_each_continue_rcu.
  175. */
  176. struct list_head *pos;
  177. rcu_read_lock();
  178. if (rdev == NULL)
  179. /* start at the beginning */
  180. pos = &mddev->disks;
  181. else {
  182. /* release the previous rdev and start from there. */
  183. rdev_dec_pending(rdev, mddev);
  184. pos = &rdev->same_set;
  185. }
  186. list_for_each_continue_rcu(pos, &mddev->disks) {
  187. rdev = list_entry(pos, struct md_rdev, same_set);
  188. if (rdev->raid_disk >= 0 &&
  189. !test_bit(Faulty, &rdev->flags)) {
  190. /* this is a usable devices */
  191. atomic_inc(&rdev->nr_pending);
  192. rcu_read_unlock();
  193. return rdev;
  194. }
  195. }
  196. rcu_read_unlock();
  197. return NULL;
  198. }
  199. static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
  200. {
  201. struct md_rdev *rdev = NULL;
  202. struct block_device *bdev;
  203. struct mddev *mddev = bitmap->mddev;
  204. while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
  205. int size = PAGE_SIZE;
  206. loff_t offset = mddev->bitmap_info.offset;
  207. bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
  208. if (page->index == bitmap->file_pages-1)
  209. size = roundup(bitmap->last_page_size,
  210. bdev_logical_block_size(bdev));
  211. /* Just make sure we aren't corrupting data or
  212. * metadata
  213. */
  214. if (mddev->external) {
  215. /* Bitmap could be anywhere. */
  216. if (rdev->sb_start + offset + (page->index
  217. * (PAGE_SIZE/512))
  218. > rdev->data_offset
  219. &&
  220. rdev->sb_start + offset
  221. < (rdev->data_offset + mddev->dev_sectors
  222. + (PAGE_SIZE/512)))
  223. goto bad_alignment;
  224. } else if (offset < 0) {
  225. /* DATA BITMAP METADATA */
  226. if (offset
  227. + (long)(page->index * (PAGE_SIZE/512))
  228. + size/512 > 0)
  229. /* bitmap runs in to metadata */
  230. goto bad_alignment;
  231. if (rdev->data_offset + mddev->dev_sectors
  232. > rdev->sb_start + offset)
  233. /* data runs in to bitmap */
  234. goto bad_alignment;
  235. } else if (rdev->sb_start < rdev->data_offset) {
  236. /* METADATA BITMAP DATA */
  237. if (rdev->sb_start
  238. + offset
  239. + page->index*(PAGE_SIZE/512) + size/512
  240. > rdev->data_offset)
  241. /* bitmap runs in to data */
  242. goto bad_alignment;
  243. } else {
  244. /* DATA METADATA BITMAP - no problems */
  245. }
  246. md_super_write(mddev, rdev,
  247. rdev->sb_start + offset
  248. + page->index * (PAGE_SIZE/512),
  249. size,
  250. page);
  251. }
  252. if (wait)
  253. md_super_wait(mddev);
  254. return 0;
  255. bad_alignment:
  256. return -EINVAL;
  257. }
  258. static void bitmap_file_kick(struct bitmap *bitmap);
  259. /*
  260. * write out a page to a file
  261. */
  262. static void write_page(struct bitmap *bitmap, struct page *page, int wait)
  263. {
  264. struct buffer_head *bh;
  265. if (bitmap->file == NULL) {
  266. switch (write_sb_page(bitmap, page, wait)) {
  267. case -EINVAL:
  268. bitmap->flags |= BITMAP_WRITE_ERROR;
  269. }
  270. } else {
  271. bh = page_buffers(page);
  272. while (bh && bh->b_blocknr) {
  273. atomic_inc(&bitmap->pending_writes);
  274. set_buffer_locked(bh);
  275. set_buffer_mapped(bh);
  276. submit_bh(WRITE | REQ_SYNC, bh);
  277. bh = bh->b_this_page;
  278. }
  279. if (wait)
  280. wait_event(bitmap->write_wait,
  281. atomic_read(&bitmap->pending_writes)==0);
  282. }
  283. if (bitmap->flags & BITMAP_WRITE_ERROR)
  284. bitmap_file_kick(bitmap);
  285. }
  286. static void end_bitmap_write(struct buffer_head *bh, int uptodate)
  287. {
  288. struct bitmap *bitmap = bh->b_private;
  289. unsigned long flags;
  290. if (!uptodate) {
  291. spin_lock_irqsave(&bitmap->lock, flags);
  292. bitmap->flags |= BITMAP_WRITE_ERROR;
  293. spin_unlock_irqrestore(&bitmap->lock, flags);
  294. }
  295. if (atomic_dec_and_test(&bitmap->pending_writes))
  296. wake_up(&bitmap->write_wait);
  297. }
  298. /* copied from buffer.c */
  299. static void
  300. __clear_page_buffers(struct page *page)
  301. {
  302. ClearPagePrivate(page);
  303. set_page_private(page, 0);
  304. page_cache_release(page);
  305. }
  306. static void free_buffers(struct page *page)
  307. {
  308. struct buffer_head *bh = page_buffers(page);
  309. while (bh) {
  310. struct buffer_head *next = bh->b_this_page;
  311. free_buffer_head(bh);
  312. bh = next;
  313. }
  314. __clear_page_buffers(page);
  315. put_page(page);
  316. }
  317. /* read a page from a file.
  318. * We both read the page, and attach buffers to the page to record the
  319. * address of each block (using bmap). These addresses will be used
  320. * to write the block later, completely bypassing the filesystem.
  321. * This usage is similar to how swap files are handled, and allows us
  322. * to write to a file with no concerns of memory allocation failing.
  323. */
  324. static struct page *read_page(struct file *file, unsigned long index,
  325. struct bitmap *bitmap,
  326. unsigned long count)
  327. {
  328. struct page *page = NULL;
  329. struct inode *inode = file->f_path.dentry->d_inode;
  330. struct buffer_head *bh;
  331. sector_t block;
  332. pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
  333. (unsigned long long)index << PAGE_SHIFT);
  334. page = alloc_page(GFP_KERNEL);
  335. if (!page)
  336. page = ERR_PTR(-ENOMEM);
  337. if (IS_ERR(page))
  338. goto out;
  339. bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
  340. if (!bh) {
  341. put_page(page);
  342. page = ERR_PTR(-ENOMEM);
  343. goto out;
  344. }
  345. attach_page_buffers(page, bh);
  346. block = index << (PAGE_SHIFT - inode->i_blkbits);
  347. while (bh) {
  348. if (count == 0)
  349. bh->b_blocknr = 0;
  350. else {
  351. bh->b_blocknr = bmap(inode, block);
  352. if (bh->b_blocknr == 0) {
  353. /* Cannot use this file! */
  354. free_buffers(page);
  355. page = ERR_PTR(-EINVAL);
  356. goto out;
  357. }
  358. bh->b_bdev = inode->i_sb->s_bdev;
  359. if (count < (1<<inode->i_blkbits))
  360. count = 0;
  361. else
  362. count -= (1<<inode->i_blkbits);
  363. bh->b_end_io = end_bitmap_write;
  364. bh->b_private = bitmap;
  365. atomic_inc(&bitmap->pending_writes);
  366. set_buffer_locked(bh);
  367. set_buffer_mapped(bh);
  368. submit_bh(READ, bh);
  369. }
  370. block++;
  371. bh = bh->b_this_page;
  372. }
  373. page->index = index;
  374. wait_event(bitmap->write_wait,
  375. atomic_read(&bitmap->pending_writes)==0);
  376. if (bitmap->flags & BITMAP_WRITE_ERROR) {
  377. free_buffers(page);
  378. page = ERR_PTR(-EIO);
  379. }
  380. out:
  381. if (IS_ERR(page))
  382. printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n",
  383. (int)PAGE_SIZE,
  384. (unsigned long long)index << PAGE_SHIFT,
  385. PTR_ERR(page));
  386. return page;
  387. }
  388. /*
  389. * bitmap file superblock operations
  390. */
  391. /* update the event counter and sync the superblock to disk */
  392. void bitmap_update_sb(struct bitmap *bitmap)
  393. {
  394. bitmap_super_t *sb;
  395. unsigned long flags;
  396. if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
  397. return;
  398. if (bitmap->mddev->bitmap_info.external)
  399. return;
  400. spin_lock_irqsave(&bitmap->lock, flags);
  401. if (!bitmap->sb_page) { /* no superblock */
  402. spin_unlock_irqrestore(&bitmap->lock, flags);
  403. return;
  404. }
  405. spin_unlock_irqrestore(&bitmap->lock, flags);
  406. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  407. sb->events = cpu_to_le64(bitmap->mddev->events);
  408. if (bitmap->mddev->events < bitmap->events_cleared)
  409. /* rocking back to read-only */
  410. bitmap->events_cleared = bitmap->mddev->events;
  411. sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
  412. sb->state = cpu_to_le32(bitmap->flags);
  413. /* Just in case these have been changed via sysfs: */
  414. sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
  415. sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
  416. kunmap_atomic(sb, KM_USER0);
  417. write_page(bitmap, bitmap->sb_page, 1);
  418. }
  419. /* print out the bitmap file superblock */
  420. void bitmap_print_sb(struct bitmap *bitmap)
  421. {
  422. bitmap_super_t *sb;
  423. if (!bitmap || !bitmap->sb_page)
  424. return;
  425. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  426. printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
  427. printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
  428. printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
  429. printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
  430. *(__u32 *)(sb->uuid+0),
  431. *(__u32 *)(sb->uuid+4),
  432. *(__u32 *)(sb->uuid+8),
  433. *(__u32 *)(sb->uuid+12));
  434. printk(KERN_DEBUG " events: %llu\n",
  435. (unsigned long long) le64_to_cpu(sb->events));
  436. printk(KERN_DEBUG "events cleared: %llu\n",
  437. (unsigned long long) le64_to_cpu(sb->events_cleared));
  438. printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
  439. printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
  440. printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
  441. printk(KERN_DEBUG " sync size: %llu KB\n",
  442. (unsigned long long)le64_to_cpu(sb->sync_size)/2);
  443. printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
  444. kunmap_atomic(sb, KM_USER0);
  445. }
  446. /*
  447. * bitmap_new_disk_sb
  448. * @bitmap
  449. *
  450. * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
  451. * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
  452. * This function verifies 'bitmap_info' and populates the on-disk bitmap
  453. * structure, which is to be written to disk.
  454. *
  455. * Returns: 0 on success, -Exxx on error
  456. */
  457. static int bitmap_new_disk_sb(struct bitmap *bitmap)
  458. {
  459. bitmap_super_t *sb;
  460. unsigned long chunksize, daemon_sleep, write_behind;
  461. int err = -EINVAL;
  462. bitmap->sb_page = alloc_page(GFP_KERNEL);
  463. if (IS_ERR(bitmap->sb_page)) {
  464. err = PTR_ERR(bitmap->sb_page);
  465. bitmap->sb_page = NULL;
  466. return err;
  467. }
  468. bitmap->sb_page->index = 0;
  469. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  470. sb->magic = cpu_to_le32(BITMAP_MAGIC);
  471. sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
  472. chunksize = bitmap->mddev->bitmap_info.chunksize;
  473. BUG_ON(!chunksize);
  474. if (!is_power_of_2(chunksize)) {
  475. kunmap_atomic(sb, KM_USER0);
  476. printk(KERN_ERR "bitmap chunksize not a power of 2\n");
  477. return -EINVAL;
  478. }
  479. sb->chunksize = cpu_to_le32(chunksize);
  480. daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
  481. if (!daemon_sleep ||
  482. (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
  483. printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n");
  484. daemon_sleep = 5 * HZ;
  485. }
  486. sb->daemon_sleep = cpu_to_le32(daemon_sleep);
  487. bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
  488. /*
  489. * FIXME: write_behind for RAID1. If not specified, what
  490. * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
  491. */
  492. write_behind = bitmap->mddev->bitmap_info.max_write_behind;
  493. if (write_behind > COUNTER_MAX)
  494. write_behind = COUNTER_MAX / 2;
  495. sb->write_behind = cpu_to_le32(write_behind);
  496. bitmap->mddev->bitmap_info.max_write_behind = write_behind;
  497. /* keep the array size field of the bitmap superblock up to date */
  498. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  499. memcpy(sb->uuid, bitmap->mddev->uuid, 16);
  500. bitmap->flags |= BITMAP_STALE;
  501. sb->state |= cpu_to_le32(BITMAP_STALE);
  502. bitmap->events_cleared = bitmap->mddev->events;
  503. sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
  504. bitmap->flags |= BITMAP_HOSTENDIAN;
  505. sb->version = cpu_to_le32(BITMAP_MAJOR_HOSTENDIAN);
  506. kunmap_atomic(sb, KM_USER0);
  507. return 0;
  508. }
  509. /* read the superblock from the bitmap file and initialize some bitmap fields */
  510. static int bitmap_read_sb(struct bitmap *bitmap)
  511. {
  512. char *reason = NULL;
  513. bitmap_super_t *sb;
  514. unsigned long chunksize, daemon_sleep, write_behind;
  515. unsigned long long events;
  516. int err = -EINVAL;
  517. /* page 0 is the superblock, read it... */
  518. if (bitmap->file) {
  519. loff_t isize = i_size_read(bitmap->file->f_mapping->host);
  520. int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
  521. bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
  522. } else {
  523. bitmap->sb_page = read_sb_page(bitmap->mddev,
  524. bitmap->mddev->bitmap_info.offset,
  525. NULL,
  526. 0, sizeof(bitmap_super_t));
  527. }
  528. if (IS_ERR(bitmap->sb_page)) {
  529. err = PTR_ERR(bitmap->sb_page);
  530. bitmap->sb_page = NULL;
  531. return err;
  532. }
  533. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  534. chunksize = le32_to_cpu(sb->chunksize);
  535. daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
  536. write_behind = le32_to_cpu(sb->write_behind);
  537. /* verify that the bitmap-specific fields are valid */
  538. if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
  539. reason = "bad magic";
  540. else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
  541. le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
  542. reason = "unrecognized superblock version";
  543. else if (chunksize < 512)
  544. reason = "bitmap chunksize too small";
  545. else if (!is_power_of_2(chunksize))
  546. reason = "bitmap chunksize not a power of 2";
  547. else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
  548. reason = "daemon sleep period out of range";
  549. else if (write_behind > COUNTER_MAX)
  550. reason = "write-behind limit out of range (0 - 16383)";
  551. if (reason) {
  552. printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
  553. bmname(bitmap), reason);
  554. goto out;
  555. }
  556. /* keep the array size field of the bitmap superblock up to date */
  557. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  558. if (!bitmap->mddev->persistent)
  559. goto success;
  560. /*
  561. * if we have a persistent array superblock, compare the
  562. * bitmap's UUID and event counter to the mddev's
  563. */
  564. if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
  565. printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
  566. bmname(bitmap));
  567. goto out;
  568. }
  569. events = le64_to_cpu(sb->events);
  570. if (events < bitmap->mddev->events) {
  571. printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
  572. "-- forcing full recovery\n", bmname(bitmap), events,
  573. (unsigned long long) bitmap->mddev->events);
  574. sb->state |= cpu_to_le32(BITMAP_STALE);
  575. }
  576. success:
  577. /* assign fields using values from superblock */
  578. bitmap->mddev->bitmap_info.chunksize = chunksize;
  579. bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
  580. bitmap->mddev->bitmap_info.max_write_behind = write_behind;
  581. bitmap->flags |= le32_to_cpu(sb->state);
  582. if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
  583. bitmap->flags |= BITMAP_HOSTENDIAN;
  584. bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
  585. if (bitmap->flags & BITMAP_STALE)
  586. bitmap->events_cleared = bitmap->mddev->events;
  587. err = 0;
  588. out:
  589. kunmap_atomic(sb, KM_USER0);
  590. if (err)
  591. bitmap_print_sb(bitmap);
  592. return err;
  593. }
  594. enum bitmap_mask_op {
  595. MASK_SET,
  596. MASK_UNSET
  597. };
  598. /* record the state of the bitmap in the superblock. Return the old value */
  599. static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
  600. enum bitmap_mask_op op)
  601. {
  602. bitmap_super_t *sb;
  603. unsigned long flags;
  604. int old;
  605. spin_lock_irqsave(&bitmap->lock, flags);
  606. if (!bitmap->sb_page) { /* can't set the state */
  607. spin_unlock_irqrestore(&bitmap->lock, flags);
  608. return 0;
  609. }
  610. spin_unlock_irqrestore(&bitmap->lock, flags);
  611. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  612. old = le32_to_cpu(sb->state) & bits;
  613. switch (op) {
  614. case MASK_SET:
  615. sb->state |= cpu_to_le32(bits);
  616. bitmap->flags |= bits;
  617. break;
  618. case MASK_UNSET:
  619. sb->state &= cpu_to_le32(~bits);
  620. bitmap->flags &= ~bits;
  621. break;
  622. default:
  623. BUG();
  624. }
  625. kunmap_atomic(sb, KM_USER0);
  626. return old;
  627. }
  628. /*
  629. * general bitmap file operations
  630. */
  631. /*
  632. * on-disk bitmap:
  633. *
  634. * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
  635. * file a page at a time. There's a superblock at the start of the file.
  636. */
  637. /* calculate the index of the page that contains this bit */
  638. static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
  639. {
  640. if (!bitmap->mddev->bitmap_info.external)
  641. chunk += sizeof(bitmap_super_t) << 3;
  642. return chunk >> PAGE_BIT_SHIFT;
  643. }
  644. /* calculate the (bit) offset of this bit within a page */
  645. static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
  646. {
  647. if (!bitmap->mddev->bitmap_info.external)
  648. chunk += sizeof(bitmap_super_t) << 3;
  649. return chunk & (PAGE_BITS - 1);
  650. }
  651. /*
  652. * return a pointer to the page in the filemap that contains the given bit
  653. *
  654. * this lookup is complicated by the fact that the bitmap sb might be exactly
  655. * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
  656. * 0 or page 1
  657. */
  658. static inline struct page *filemap_get_page(struct bitmap *bitmap,
  659. unsigned long chunk)
  660. {
  661. if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
  662. return NULL;
  663. return bitmap->filemap[file_page_index(bitmap, chunk)
  664. - file_page_index(bitmap, 0)];
  665. }
  666. static void bitmap_file_unmap(struct bitmap *bitmap)
  667. {
  668. struct page **map, *sb_page;
  669. unsigned long *attr;
  670. int pages;
  671. unsigned long flags;
  672. spin_lock_irqsave(&bitmap->lock, flags);
  673. map = bitmap->filemap;
  674. bitmap->filemap = NULL;
  675. attr = bitmap->filemap_attr;
  676. bitmap->filemap_attr = NULL;
  677. pages = bitmap->file_pages;
  678. bitmap->file_pages = 0;
  679. sb_page = bitmap->sb_page;
  680. bitmap->sb_page = NULL;
  681. spin_unlock_irqrestore(&bitmap->lock, flags);
  682. while (pages--)
  683. if (map[pages] != sb_page) /* 0 is sb_page, release it below */
  684. free_buffers(map[pages]);
  685. kfree(map);
  686. kfree(attr);
  687. if (sb_page)
  688. free_buffers(sb_page);
  689. }
  690. static void bitmap_file_put(struct bitmap *bitmap)
  691. {
  692. struct file *file;
  693. unsigned long flags;
  694. spin_lock_irqsave(&bitmap->lock, flags);
  695. file = bitmap->file;
  696. bitmap->file = NULL;
  697. spin_unlock_irqrestore(&bitmap->lock, flags);
  698. if (file)
  699. wait_event(bitmap->write_wait,
  700. atomic_read(&bitmap->pending_writes)==0);
  701. bitmap_file_unmap(bitmap);
  702. if (file) {
  703. struct inode *inode = file->f_path.dentry->d_inode;
  704. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  705. fput(file);
  706. }
  707. }
  708. /*
  709. * bitmap_file_kick - if an error occurs while manipulating the bitmap file
  710. * then it is no longer reliable, so we stop using it and we mark the file
  711. * as failed in the superblock
  712. */
  713. static void bitmap_file_kick(struct bitmap *bitmap)
  714. {
  715. char *path, *ptr = NULL;
  716. if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
  717. bitmap_update_sb(bitmap);
  718. if (bitmap->file) {
  719. path = kmalloc(PAGE_SIZE, GFP_KERNEL);
  720. if (path)
  721. ptr = d_path(&bitmap->file->f_path, path,
  722. PAGE_SIZE);
  723. printk(KERN_ALERT
  724. "%s: kicking failed bitmap file %s from array!\n",
  725. bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
  726. kfree(path);
  727. } else
  728. printk(KERN_ALERT
  729. "%s: disabling internal bitmap due to errors\n",
  730. bmname(bitmap));
  731. }
  732. bitmap_file_put(bitmap);
  733. return;
  734. }
  735. enum bitmap_page_attr {
  736. BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
  737. BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
  738. * i.e. counter is 1 or 2. */
  739. BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
  740. };
  741. static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
  742. enum bitmap_page_attr attr)
  743. {
  744. __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
  745. }
  746. static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
  747. enum bitmap_page_attr attr)
  748. {
  749. __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
  750. }
  751. static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
  752. enum bitmap_page_attr attr)
  753. {
  754. return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
  755. }
  756. /*
  757. * bitmap_file_set_bit -- called before performing a write to the md device
  758. * to set (and eventually sync) a particular bit in the bitmap file
  759. *
  760. * we set the bit immediately, then we record the page number so that
  761. * when an unplug occurs, we can flush the dirty pages out to disk
  762. */
  763. static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
  764. {
  765. unsigned long bit;
  766. struct page *page;
  767. void *kaddr;
  768. unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
  769. if (!bitmap->filemap)
  770. return;
  771. page = filemap_get_page(bitmap, chunk);
  772. if (!page)
  773. return;
  774. bit = file_page_offset(bitmap, chunk);
  775. /* set the bit */
  776. kaddr = kmap_atomic(page, KM_USER0);
  777. if (bitmap->flags & BITMAP_HOSTENDIAN)
  778. set_bit(bit, kaddr);
  779. else
  780. __set_bit_le(bit, kaddr);
  781. kunmap_atomic(kaddr, KM_USER0);
  782. pr_debug("set file bit %lu page %lu\n", bit, page->index);
  783. /* record page number so it gets flushed to disk when unplug occurs */
  784. set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  785. }
  786. /* this gets called when the md device is ready to unplug its underlying
  787. * (slave) device queues -- before we let any writes go down, we need to
  788. * sync the dirty pages of the bitmap file to disk */
  789. void bitmap_unplug(struct bitmap *bitmap)
  790. {
  791. unsigned long i, flags;
  792. int dirty, need_write;
  793. struct page *page;
  794. int wait = 0;
  795. if (!bitmap)
  796. return;
  797. /* look at each page to see if there are any set bits that need to be
  798. * flushed out to disk */
  799. for (i = 0; i < bitmap->file_pages; i++) {
  800. spin_lock_irqsave(&bitmap->lock, flags);
  801. if (!bitmap->filemap) {
  802. spin_unlock_irqrestore(&bitmap->lock, flags);
  803. return;
  804. }
  805. page = bitmap->filemap[i];
  806. dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  807. need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  808. clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  809. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  810. if (dirty)
  811. wait = 1;
  812. spin_unlock_irqrestore(&bitmap->lock, flags);
  813. if (dirty || need_write)
  814. write_page(bitmap, page, 0);
  815. }
  816. if (wait) { /* if any writes were performed, we need to wait on them */
  817. if (bitmap->file)
  818. wait_event(bitmap->write_wait,
  819. atomic_read(&bitmap->pending_writes)==0);
  820. else
  821. md_super_wait(bitmap->mddev);
  822. }
  823. if (bitmap->flags & BITMAP_WRITE_ERROR)
  824. bitmap_file_kick(bitmap);
  825. }
  826. EXPORT_SYMBOL(bitmap_unplug);
  827. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
  828. /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
  829. * the in-memory bitmap from the on-disk bitmap -- also, sets up the
  830. * memory mapping of the bitmap file
  831. * Special cases:
  832. * if there's no bitmap file, or if the bitmap file had been
  833. * previously kicked from the array, we mark all the bits as
  834. * 1's in order to cause a full resync.
  835. *
  836. * We ignore all bits for sectors that end earlier than 'start'.
  837. * This is used when reading an out-of-date bitmap...
  838. */
  839. static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
  840. {
  841. unsigned long i, chunks, index, oldindex, bit;
  842. struct page *page = NULL, *oldpage = NULL;
  843. unsigned long num_pages, bit_cnt = 0;
  844. struct file *file;
  845. unsigned long bytes, offset;
  846. int outofdate;
  847. int ret = -ENOSPC;
  848. void *paddr;
  849. chunks = bitmap->chunks;
  850. file = bitmap->file;
  851. BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
  852. outofdate = bitmap->flags & BITMAP_STALE;
  853. if (outofdate)
  854. printk(KERN_INFO "%s: bitmap file is out of date, doing full "
  855. "recovery\n", bmname(bitmap));
  856. bytes = DIV_ROUND_UP(bitmap->chunks, 8);
  857. if (!bitmap->mddev->bitmap_info.external)
  858. bytes += sizeof(bitmap_super_t);
  859. num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
  860. if (file && i_size_read(file->f_mapping->host) < bytes) {
  861. printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
  862. bmname(bitmap),
  863. (unsigned long) i_size_read(file->f_mapping->host),
  864. bytes);
  865. goto err;
  866. }
  867. ret = -ENOMEM;
  868. bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  869. if (!bitmap->filemap)
  870. goto err;
  871. /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
  872. bitmap->filemap_attr = kzalloc(
  873. roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
  874. GFP_KERNEL);
  875. if (!bitmap->filemap_attr)
  876. goto err;
  877. oldindex = ~0L;
  878. for (i = 0; i < chunks; i++) {
  879. int b;
  880. index = file_page_index(bitmap, i);
  881. bit = file_page_offset(bitmap, i);
  882. if (index != oldindex) { /* this is a new page, read it in */
  883. int count;
  884. /* unmap the old page, we're done with it */
  885. if (index == num_pages-1)
  886. count = bytes - index * PAGE_SIZE;
  887. else
  888. count = PAGE_SIZE;
  889. if (index == 0 && bitmap->sb_page) {
  890. /*
  891. * if we're here then the superblock page
  892. * contains some bits (PAGE_SIZE != sizeof sb)
  893. * we've already read it in, so just use it
  894. */
  895. page = bitmap->sb_page;
  896. offset = sizeof(bitmap_super_t);
  897. if (!file)
  898. page = read_sb_page(
  899. bitmap->mddev,
  900. bitmap->mddev->bitmap_info.offset,
  901. page,
  902. index, count);
  903. } else if (file) {
  904. page = read_page(file, index, bitmap, count);
  905. offset = 0;
  906. } else {
  907. page = read_sb_page(bitmap->mddev,
  908. bitmap->mddev->bitmap_info.offset,
  909. NULL,
  910. index, count);
  911. offset = 0;
  912. }
  913. if (IS_ERR(page)) { /* read error */
  914. ret = PTR_ERR(page);
  915. goto err;
  916. }
  917. oldindex = index;
  918. oldpage = page;
  919. bitmap->filemap[bitmap->file_pages++] = page;
  920. bitmap->last_page_size = count;
  921. if (outofdate) {
  922. /*
  923. * if bitmap is out of date, dirty the
  924. * whole page and write it out
  925. */
  926. paddr = kmap_atomic(page, KM_USER0);
  927. memset(paddr + offset, 0xff,
  928. PAGE_SIZE - offset);
  929. kunmap_atomic(paddr, KM_USER0);
  930. write_page(bitmap, page, 1);
  931. ret = -EIO;
  932. if (bitmap->flags & BITMAP_WRITE_ERROR)
  933. goto err;
  934. }
  935. }
  936. paddr = kmap_atomic(page, KM_USER0);
  937. if (bitmap->flags & BITMAP_HOSTENDIAN)
  938. b = test_bit(bit, paddr);
  939. else
  940. b = test_bit_le(bit, paddr);
  941. kunmap_atomic(paddr, KM_USER0);
  942. if (b) {
  943. /* if the disk bit is set, set the memory bit */
  944. int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
  945. >= start);
  946. bitmap_set_memory_bits(bitmap,
  947. (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
  948. needed);
  949. bit_cnt++;
  950. }
  951. }
  952. /* everything went OK */
  953. ret = 0;
  954. bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
  955. if (bit_cnt) { /* Kick recovery if any bits were set */
  956. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  957. md_wakeup_thread(bitmap->mddev->thread);
  958. }
  959. printk(KERN_INFO "%s: bitmap initialized from disk: "
  960. "read %lu/%lu pages, set %lu of %lu bits\n",
  961. bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, chunks);
  962. return 0;
  963. err:
  964. printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
  965. bmname(bitmap), ret);
  966. return ret;
  967. }
  968. void bitmap_write_all(struct bitmap *bitmap)
  969. {
  970. /* We don't actually write all bitmap blocks here,
  971. * just flag them as needing to be written
  972. */
  973. int i;
  974. spin_lock_irq(&bitmap->lock);
  975. for (i = 0; i < bitmap->file_pages; i++)
  976. set_page_attr(bitmap, bitmap->filemap[i],
  977. BITMAP_PAGE_NEEDWRITE);
  978. bitmap->allclean = 0;
  979. spin_unlock_irq(&bitmap->lock);
  980. }
  981. static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
  982. {
  983. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  984. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  985. bitmap->bp[page].count += inc;
  986. bitmap_checkfree(bitmap, page);
  987. }
  988. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  989. sector_t offset, sector_t *blocks,
  990. int create);
  991. /*
  992. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  993. * out to disk
  994. */
  995. void bitmap_daemon_work(struct mddev *mddev)
  996. {
  997. struct bitmap *bitmap;
  998. unsigned long j;
  999. unsigned long flags;
  1000. struct page *page = NULL, *lastpage = NULL;
  1001. sector_t blocks;
  1002. void *paddr;
  1003. /* Use a mutex to guard daemon_work against
  1004. * bitmap_destroy.
  1005. */
  1006. mutex_lock(&mddev->bitmap_info.mutex);
  1007. bitmap = mddev->bitmap;
  1008. if (bitmap == NULL) {
  1009. mutex_unlock(&mddev->bitmap_info.mutex);
  1010. return;
  1011. }
  1012. if (time_before(jiffies, bitmap->daemon_lastrun
  1013. + mddev->bitmap_info.daemon_sleep))
  1014. goto done;
  1015. bitmap->daemon_lastrun = jiffies;
  1016. if (bitmap->allclean) {
  1017. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1018. goto done;
  1019. }
  1020. bitmap->allclean = 1;
  1021. spin_lock_irqsave(&bitmap->lock, flags);
  1022. for (j = 0; j < bitmap->chunks; j++) {
  1023. bitmap_counter_t *bmc;
  1024. if (!bitmap->filemap)
  1025. /* error or shutdown */
  1026. break;
  1027. page = filemap_get_page(bitmap, j);
  1028. if (page != lastpage) {
  1029. /* skip this page unless it's marked as needing cleaning */
  1030. if (!test_page_attr(bitmap, page, BITMAP_PAGE_PENDING)) {
  1031. int need_write = test_page_attr(bitmap, page,
  1032. BITMAP_PAGE_NEEDWRITE);
  1033. if (need_write)
  1034. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  1035. spin_unlock_irqrestore(&bitmap->lock, flags);
  1036. if (need_write)
  1037. write_page(bitmap, page, 0);
  1038. spin_lock_irqsave(&bitmap->lock, flags);
  1039. j |= (PAGE_BITS - 1);
  1040. continue;
  1041. }
  1042. /* grab the new page, sync and release the old */
  1043. if (lastpage != NULL) {
  1044. if (test_page_attr(bitmap, lastpage,
  1045. BITMAP_PAGE_NEEDWRITE)) {
  1046. clear_page_attr(bitmap, lastpage,
  1047. BITMAP_PAGE_NEEDWRITE);
  1048. spin_unlock_irqrestore(&bitmap->lock, flags);
  1049. write_page(bitmap, lastpage, 0);
  1050. } else {
  1051. set_page_attr(bitmap, lastpage,
  1052. BITMAP_PAGE_NEEDWRITE);
  1053. bitmap->allclean = 0;
  1054. spin_unlock_irqrestore(&bitmap->lock, flags);
  1055. }
  1056. } else
  1057. spin_unlock_irqrestore(&bitmap->lock, flags);
  1058. lastpage = page;
  1059. /* We are possibly going to clear some bits, so make
  1060. * sure that events_cleared is up-to-date.
  1061. */
  1062. if (bitmap->need_sync &&
  1063. mddev->bitmap_info.external == 0) {
  1064. bitmap_super_t *sb;
  1065. bitmap->need_sync = 0;
  1066. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  1067. sb->events_cleared =
  1068. cpu_to_le64(bitmap->events_cleared);
  1069. kunmap_atomic(sb, KM_USER0);
  1070. write_page(bitmap, bitmap->sb_page, 1);
  1071. }
  1072. spin_lock_irqsave(&bitmap->lock, flags);
  1073. if (!bitmap->need_sync)
  1074. clear_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
  1075. else
  1076. bitmap->allclean = 0;
  1077. }
  1078. bmc = bitmap_get_counter(bitmap,
  1079. (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
  1080. &blocks, 0);
  1081. if (!bmc)
  1082. j |= PAGE_COUNTER_MASK;
  1083. else if (*bmc) {
  1084. if (*bmc == 1 && !bitmap->need_sync) {
  1085. /* we can clear the bit */
  1086. *bmc = 0;
  1087. bitmap_count_page(bitmap,
  1088. (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
  1089. -1);
  1090. /* clear the bit */
  1091. paddr = kmap_atomic(page, KM_USER0);
  1092. if (bitmap->flags & BITMAP_HOSTENDIAN)
  1093. clear_bit(file_page_offset(bitmap, j),
  1094. paddr);
  1095. else
  1096. __clear_bit_le(
  1097. file_page_offset(bitmap,
  1098. j),
  1099. paddr);
  1100. kunmap_atomic(paddr, KM_USER0);
  1101. } else if (*bmc <= 2) {
  1102. *bmc = 1; /* maybe clear the bit next time */
  1103. set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
  1104. bitmap->allclean = 0;
  1105. }
  1106. }
  1107. }
  1108. spin_unlock_irqrestore(&bitmap->lock, flags);
  1109. /* now sync the final page */
  1110. if (lastpage != NULL) {
  1111. spin_lock_irqsave(&bitmap->lock, flags);
  1112. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  1113. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1114. spin_unlock_irqrestore(&bitmap->lock, flags);
  1115. write_page(bitmap, lastpage, 0);
  1116. } else {
  1117. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1118. bitmap->allclean = 0;
  1119. spin_unlock_irqrestore(&bitmap->lock, flags);
  1120. }
  1121. }
  1122. done:
  1123. if (bitmap->allclean == 0)
  1124. mddev->thread->timeout =
  1125. mddev->bitmap_info.daemon_sleep;
  1126. mutex_unlock(&mddev->bitmap_info.mutex);
  1127. }
  1128. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1129. sector_t offset, sector_t *blocks,
  1130. int create)
  1131. __releases(bitmap->lock)
  1132. __acquires(bitmap->lock)
  1133. {
  1134. /* If 'create', we might release the lock and reclaim it.
  1135. * The lock must have been taken with interrupts enabled.
  1136. * If !create, we don't release the lock.
  1137. */
  1138. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  1139. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1140. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  1141. sector_t csize;
  1142. int err;
  1143. err = bitmap_checkpage(bitmap, page, create);
  1144. if (bitmap->bp[page].hijacked ||
  1145. bitmap->bp[page].map == NULL)
  1146. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
  1147. PAGE_COUNTER_SHIFT - 1);
  1148. else
  1149. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1150. *blocks = csize - (offset & (csize - 1));
  1151. if (err < 0)
  1152. return NULL;
  1153. /* now locked ... */
  1154. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  1155. /* should we use the first or second counter field
  1156. * of the hijacked pointer? */
  1157. int hi = (pageoff > PAGE_COUNTER_MASK);
  1158. return &((bitmap_counter_t *)
  1159. &bitmap->bp[page].map)[hi];
  1160. } else /* page is allocated */
  1161. return (bitmap_counter_t *)
  1162. &(bitmap->bp[page].map[pageoff]);
  1163. }
  1164. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
  1165. {
  1166. if (!bitmap)
  1167. return 0;
  1168. if (behind) {
  1169. int bw;
  1170. atomic_inc(&bitmap->behind_writes);
  1171. bw = atomic_read(&bitmap->behind_writes);
  1172. if (bw > bitmap->behind_writes_used)
  1173. bitmap->behind_writes_used = bw;
  1174. pr_debug("inc write-behind count %d/%lu\n",
  1175. bw, bitmap->mddev->bitmap_info.max_write_behind);
  1176. }
  1177. while (sectors) {
  1178. sector_t blocks;
  1179. bitmap_counter_t *bmc;
  1180. spin_lock_irq(&bitmap->lock);
  1181. bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
  1182. if (!bmc) {
  1183. spin_unlock_irq(&bitmap->lock);
  1184. return 0;
  1185. }
  1186. if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
  1187. DEFINE_WAIT(__wait);
  1188. /* note that it is safe to do the prepare_to_wait
  1189. * after the test as long as we do it before dropping
  1190. * the spinlock.
  1191. */
  1192. prepare_to_wait(&bitmap->overflow_wait, &__wait,
  1193. TASK_UNINTERRUPTIBLE);
  1194. spin_unlock_irq(&bitmap->lock);
  1195. io_schedule();
  1196. finish_wait(&bitmap->overflow_wait, &__wait);
  1197. continue;
  1198. }
  1199. switch (*bmc) {
  1200. case 0:
  1201. bitmap_file_set_bit(bitmap, offset);
  1202. bitmap_count_page(bitmap, offset, 1);
  1203. /* fall through */
  1204. case 1:
  1205. *bmc = 2;
  1206. }
  1207. (*bmc)++;
  1208. spin_unlock_irq(&bitmap->lock);
  1209. offset += blocks;
  1210. if (sectors > blocks)
  1211. sectors -= blocks;
  1212. else
  1213. sectors = 0;
  1214. }
  1215. return 0;
  1216. }
  1217. EXPORT_SYMBOL(bitmap_startwrite);
  1218. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1219. int success, int behind)
  1220. {
  1221. if (!bitmap)
  1222. return;
  1223. if (behind) {
  1224. if (atomic_dec_and_test(&bitmap->behind_writes))
  1225. wake_up(&bitmap->behind_wait);
  1226. pr_debug("dec write-behind count %d/%lu\n",
  1227. atomic_read(&bitmap->behind_writes),
  1228. bitmap->mddev->bitmap_info.max_write_behind);
  1229. }
  1230. while (sectors) {
  1231. sector_t blocks;
  1232. unsigned long flags;
  1233. bitmap_counter_t *bmc;
  1234. spin_lock_irqsave(&bitmap->lock, flags);
  1235. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1236. if (!bmc) {
  1237. spin_unlock_irqrestore(&bitmap->lock, flags);
  1238. return;
  1239. }
  1240. if (success && !bitmap->mddev->degraded &&
  1241. bitmap->events_cleared < bitmap->mddev->events) {
  1242. bitmap->events_cleared = bitmap->mddev->events;
  1243. bitmap->need_sync = 1;
  1244. sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
  1245. }
  1246. if (!success && !NEEDED(*bmc))
  1247. *bmc |= NEEDED_MASK;
  1248. if (COUNTER(*bmc) == COUNTER_MAX)
  1249. wake_up(&bitmap->overflow_wait);
  1250. (*bmc)--;
  1251. if (*bmc <= 2) {
  1252. set_page_attr(bitmap,
  1253. filemap_get_page(
  1254. bitmap,
  1255. offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1256. BITMAP_PAGE_PENDING);
  1257. bitmap->allclean = 0;
  1258. }
  1259. spin_unlock_irqrestore(&bitmap->lock, flags);
  1260. offset += blocks;
  1261. if (sectors > blocks)
  1262. sectors -= blocks;
  1263. else
  1264. sectors = 0;
  1265. }
  1266. }
  1267. EXPORT_SYMBOL(bitmap_endwrite);
  1268. static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1269. int degraded)
  1270. {
  1271. bitmap_counter_t *bmc;
  1272. int rv;
  1273. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1274. *blocks = 1024;
  1275. return 1; /* always resync if no bitmap */
  1276. }
  1277. spin_lock_irq(&bitmap->lock);
  1278. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1279. rv = 0;
  1280. if (bmc) {
  1281. /* locked */
  1282. if (RESYNC(*bmc))
  1283. rv = 1;
  1284. else if (NEEDED(*bmc)) {
  1285. rv = 1;
  1286. if (!degraded) { /* don't set/clear bits if degraded */
  1287. *bmc |= RESYNC_MASK;
  1288. *bmc &= ~NEEDED_MASK;
  1289. }
  1290. }
  1291. }
  1292. spin_unlock_irq(&bitmap->lock);
  1293. return rv;
  1294. }
  1295. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1296. int degraded)
  1297. {
  1298. /* bitmap_start_sync must always report on multiples of whole
  1299. * pages, otherwise resync (which is very PAGE_SIZE based) will
  1300. * get confused.
  1301. * So call __bitmap_start_sync repeatedly (if needed) until
  1302. * At least PAGE_SIZE>>9 blocks are covered.
  1303. * Return the 'or' of the result.
  1304. */
  1305. int rv = 0;
  1306. sector_t blocks1;
  1307. *blocks = 0;
  1308. while (*blocks < (PAGE_SIZE>>9)) {
  1309. rv |= __bitmap_start_sync(bitmap, offset,
  1310. &blocks1, degraded);
  1311. offset += blocks1;
  1312. *blocks += blocks1;
  1313. }
  1314. return rv;
  1315. }
  1316. EXPORT_SYMBOL(bitmap_start_sync);
  1317. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
  1318. {
  1319. bitmap_counter_t *bmc;
  1320. unsigned long flags;
  1321. if (bitmap == NULL) {
  1322. *blocks = 1024;
  1323. return;
  1324. }
  1325. spin_lock_irqsave(&bitmap->lock, flags);
  1326. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1327. if (bmc == NULL)
  1328. goto unlock;
  1329. /* locked */
  1330. if (RESYNC(*bmc)) {
  1331. *bmc &= ~RESYNC_MASK;
  1332. if (!NEEDED(*bmc) && aborted)
  1333. *bmc |= NEEDED_MASK;
  1334. else {
  1335. if (*bmc <= 2) {
  1336. set_page_attr(bitmap,
  1337. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1338. BITMAP_PAGE_PENDING);
  1339. bitmap->allclean = 0;
  1340. }
  1341. }
  1342. }
  1343. unlock:
  1344. spin_unlock_irqrestore(&bitmap->lock, flags);
  1345. }
  1346. EXPORT_SYMBOL(bitmap_end_sync);
  1347. void bitmap_close_sync(struct bitmap *bitmap)
  1348. {
  1349. /* Sync has finished, and any bitmap chunks that weren't synced
  1350. * properly have been aborted. It remains to us to clear the
  1351. * RESYNC bit wherever it is still on
  1352. */
  1353. sector_t sector = 0;
  1354. sector_t blocks;
  1355. if (!bitmap)
  1356. return;
  1357. while (sector < bitmap->mddev->resync_max_sectors) {
  1358. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1359. sector += blocks;
  1360. }
  1361. }
  1362. EXPORT_SYMBOL(bitmap_close_sync);
  1363. void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
  1364. {
  1365. sector_t s = 0;
  1366. sector_t blocks;
  1367. if (!bitmap)
  1368. return;
  1369. if (sector == 0) {
  1370. bitmap->last_end_sync = jiffies;
  1371. return;
  1372. }
  1373. if (time_before(jiffies, (bitmap->last_end_sync
  1374. + bitmap->mddev->bitmap_info.daemon_sleep)))
  1375. return;
  1376. wait_event(bitmap->mddev->recovery_wait,
  1377. atomic_read(&bitmap->mddev->recovery_active) == 0);
  1378. bitmap->mddev->curr_resync_completed = sector;
  1379. set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
  1380. sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
  1381. s = 0;
  1382. while (s < sector && s < bitmap->mddev->resync_max_sectors) {
  1383. bitmap_end_sync(bitmap, s, &blocks, 0);
  1384. s += blocks;
  1385. }
  1386. bitmap->last_end_sync = jiffies;
  1387. sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
  1388. }
  1389. EXPORT_SYMBOL(bitmap_cond_end_sync);
  1390. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1391. {
  1392. /* For each chunk covered by any of these sectors, set the
  1393. * counter to 1 and set resync_needed. They should all
  1394. * be 0 at this point
  1395. */
  1396. sector_t secs;
  1397. bitmap_counter_t *bmc;
  1398. spin_lock_irq(&bitmap->lock);
  1399. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1400. if (!bmc) {
  1401. spin_unlock_irq(&bitmap->lock);
  1402. return;
  1403. }
  1404. if (!*bmc) {
  1405. struct page *page;
  1406. *bmc = 2 | (needed ? NEEDED_MASK : 0);
  1407. bitmap_count_page(bitmap, offset, 1);
  1408. page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
  1409. set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
  1410. bitmap->allclean = 0;
  1411. }
  1412. spin_unlock_irq(&bitmap->lock);
  1413. }
  1414. /* dirty the memory and file bits for bitmap chunks "s" to "e" */
  1415. void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
  1416. {
  1417. unsigned long chunk;
  1418. for (chunk = s; chunk <= e; chunk++) {
  1419. sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
  1420. bitmap_set_memory_bits(bitmap, sec, 1);
  1421. spin_lock_irq(&bitmap->lock);
  1422. bitmap_file_set_bit(bitmap, sec);
  1423. spin_unlock_irq(&bitmap->lock);
  1424. if (sec < bitmap->mddev->recovery_cp)
  1425. /* We are asserting that the array is dirty,
  1426. * so move the recovery_cp address back so
  1427. * that it is obvious that it is dirty
  1428. */
  1429. bitmap->mddev->recovery_cp = sec;
  1430. }
  1431. }
  1432. /*
  1433. * flush out any pending updates
  1434. */
  1435. void bitmap_flush(struct mddev *mddev)
  1436. {
  1437. struct bitmap *bitmap = mddev->bitmap;
  1438. long sleep;
  1439. if (!bitmap) /* there was no bitmap */
  1440. return;
  1441. /* run the daemon_work three time to ensure everything is flushed
  1442. * that can be
  1443. */
  1444. sleep = mddev->bitmap_info.daemon_sleep * 2;
  1445. bitmap->daemon_lastrun -= sleep;
  1446. bitmap_daemon_work(mddev);
  1447. bitmap->daemon_lastrun -= sleep;
  1448. bitmap_daemon_work(mddev);
  1449. bitmap->daemon_lastrun -= sleep;
  1450. bitmap_daemon_work(mddev);
  1451. bitmap_update_sb(bitmap);
  1452. }
  1453. /*
  1454. * free memory that was allocated
  1455. */
  1456. static void bitmap_free(struct bitmap *bitmap)
  1457. {
  1458. unsigned long k, pages;
  1459. struct bitmap_page *bp;
  1460. if (!bitmap) /* there was no bitmap */
  1461. return;
  1462. /* release the bitmap file and kill the daemon */
  1463. bitmap_file_put(bitmap);
  1464. bp = bitmap->bp;
  1465. pages = bitmap->pages;
  1466. /* free all allocated memory */
  1467. if (bp) /* deallocate the page memory */
  1468. for (k = 0; k < pages; k++)
  1469. if (bp[k].map && !bp[k].hijacked)
  1470. kfree(bp[k].map);
  1471. kfree(bp);
  1472. kfree(bitmap);
  1473. }
  1474. void bitmap_destroy(struct mddev *mddev)
  1475. {
  1476. struct bitmap *bitmap = mddev->bitmap;
  1477. if (!bitmap) /* there was no bitmap */
  1478. return;
  1479. mutex_lock(&mddev->bitmap_info.mutex);
  1480. mddev->bitmap = NULL; /* disconnect from the md device */
  1481. mutex_unlock(&mddev->bitmap_info.mutex);
  1482. if (mddev->thread)
  1483. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1484. if (bitmap->sysfs_can_clear)
  1485. sysfs_put(bitmap->sysfs_can_clear);
  1486. bitmap_free(bitmap);
  1487. }
  1488. /*
  1489. * initialize the bitmap structure
  1490. * if this returns an error, bitmap_destroy must be called to do clean up
  1491. */
  1492. int bitmap_create(struct mddev *mddev)
  1493. {
  1494. struct bitmap *bitmap;
  1495. sector_t blocks = mddev->resync_max_sectors;
  1496. unsigned long chunks;
  1497. unsigned long pages;
  1498. struct file *file = mddev->bitmap_info.file;
  1499. int err;
  1500. struct sysfs_dirent *bm = NULL;
  1501. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1502. if (!file
  1503. && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
  1504. return 0;
  1505. BUG_ON(file && mddev->bitmap_info.offset);
  1506. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1507. if (!bitmap)
  1508. return -ENOMEM;
  1509. spin_lock_init(&bitmap->lock);
  1510. atomic_set(&bitmap->pending_writes, 0);
  1511. init_waitqueue_head(&bitmap->write_wait);
  1512. init_waitqueue_head(&bitmap->overflow_wait);
  1513. init_waitqueue_head(&bitmap->behind_wait);
  1514. bitmap->mddev = mddev;
  1515. if (mddev->kobj.sd)
  1516. bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
  1517. if (bm) {
  1518. bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
  1519. sysfs_put(bm);
  1520. } else
  1521. bitmap->sysfs_can_clear = NULL;
  1522. bitmap->file = file;
  1523. if (file) {
  1524. get_file(file);
  1525. /* As future accesses to this file will use bmap,
  1526. * and bypass the page cache, we must sync the file
  1527. * first.
  1528. */
  1529. vfs_fsync(file, 1);
  1530. }
  1531. /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
  1532. if (!mddev->bitmap_info.external) {
  1533. /*
  1534. * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
  1535. * instructing us to create a new on-disk bitmap instance.
  1536. */
  1537. if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
  1538. err = bitmap_new_disk_sb(bitmap);
  1539. else
  1540. err = bitmap_read_sb(bitmap);
  1541. } else {
  1542. err = 0;
  1543. if (mddev->bitmap_info.chunksize == 0 ||
  1544. mddev->bitmap_info.daemon_sleep == 0)
  1545. /* chunksize and time_base need to be
  1546. * set first. */
  1547. err = -EINVAL;
  1548. }
  1549. if (err)
  1550. goto error;
  1551. bitmap->daemon_lastrun = jiffies;
  1552. bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
  1553. /* now that chunksize and chunkshift are set, we can use these macros */
  1554. chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
  1555. CHUNK_BLOCK_SHIFT(bitmap);
  1556. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1557. BUG_ON(!pages);
  1558. bitmap->chunks = chunks;
  1559. bitmap->pages = pages;
  1560. bitmap->missing_pages = pages;
  1561. bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1562. err = -ENOMEM;
  1563. if (!bitmap->bp)
  1564. goto error;
  1565. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1566. pages, bmname(bitmap));
  1567. mddev->bitmap = bitmap;
  1568. return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
  1569. error:
  1570. bitmap_free(bitmap);
  1571. return err;
  1572. }
  1573. int bitmap_load(struct mddev *mddev)
  1574. {
  1575. int err = 0;
  1576. sector_t start = 0;
  1577. sector_t sector = 0;
  1578. struct bitmap *bitmap = mddev->bitmap;
  1579. if (!bitmap)
  1580. goto out;
  1581. /* Clear out old bitmap info first: Either there is none, or we
  1582. * are resuming after someone else has possibly changed things,
  1583. * so we should forget old cached info.
  1584. * All chunks should be clean, but some might need_sync.
  1585. */
  1586. while (sector < mddev->resync_max_sectors) {
  1587. sector_t blocks;
  1588. bitmap_start_sync(bitmap, sector, &blocks, 0);
  1589. sector += blocks;
  1590. }
  1591. bitmap_close_sync(bitmap);
  1592. if (mddev->degraded == 0
  1593. || bitmap->events_cleared == mddev->events)
  1594. /* no need to keep dirty bits to optimise a
  1595. * re-add of a missing device */
  1596. start = mddev->recovery_cp;
  1597. err = bitmap_init_from_disk(bitmap, start);
  1598. if (err)
  1599. goto out;
  1600. mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
  1601. md_wakeup_thread(mddev->thread);
  1602. bitmap_update_sb(bitmap);
  1603. if (bitmap->flags & BITMAP_WRITE_ERROR)
  1604. err = -EIO;
  1605. out:
  1606. return err;
  1607. }
  1608. EXPORT_SYMBOL_GPL(bitmap_load);
  1609. static ssize_t
  1610. location_show(struct mddev *mddev, char *page)
  1611. {
  1612. ssize_t len;
  1613. if (mddev->bitmap_info.file)
  1614. len = sprintf(page, "file");
  1615. else if (mddev->bitmap_info.offset)
  1616. len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
  1617. else
  1618. len = sprintf(page, "none");
  1619. len += sprintf(page+len, "\n");
  1620. return len;
  1621. }
  1622. static ssize_t
  1623. location_store(struct mddev *mddev, const char *buf, size_t len)
  1624. {
  1625. if (mddev->pers) {
  1626. if (!mddev->pers->quiesce)
  1627. return -EBUSY;
  1628. if (mddev->recovery || mddev->sync_thread)
  1629. return -EBUSY;
  1630. }
  1631. if (mddev->bitmap || mddev->bitmap_info.file ||
  1632. mddev->bitmap_info.offset) {
  1633. /* bitmap already configured. Only option is to clear it */
  1634. if (strncmp(buf, "none", 4) != 0)
  1635. return -EBUSY;
  1636. if (mddev->pers) {
  1637. mddev->pers->quiesce(mddev, 1);
  1638. bitmap_destroy(mddev);
  1639. mddev->pers->quiesce(mddev, 0);
  1640. }
  1641. mddev->bitmap_info.offset = 0;
  1642. if (mddev->bitmap_info.file) {
  1643. struct file *f = mddev->bitmap_info.file;
  1644. mddev->bitmap_info.file = NULL;
  1645. restore_bitmap_write_access(f);
  1646. fput(f);
  1647. }
  1648. } else {
  1649. /* No bitmap, OK to set a location */
  1650. long long offset;
  1651. if (strncmp(buf, "none", 4) == 0)
  1652. /* nothing to be done */;
  1653. else if (strncmp(buf, "file:", 5) == 0) {
  1654. /* Not supported yet */
  1655. return -EINVAL;
  1656. } else {
  1657. int rv;
  1658. if (buf[0] == '+')
  1659. rv = strict_strtoll(buf+1, 10, &offset);
  1660. else
  1661. rv = strict_strtoll(buf, 10, &offset);
  1662. if (rv)
  1663. return rv;
  1664. if (offset == 0)
  1665. return -EINVAL;
  1666. if (mddev->bitmap_info.external == 0 &&
  1667. mddev->major_version == 0 &&
  1668. offset != mddev->bitmap_info.default_offset)
  1669. return -EINVAL;
  1670. mddev->bitmap_info.offset = offset;
  1671. if (mddev->pers) {
  1672. mddev->pers->quiesce(mddev, 1);
  1673. rv = bitmap_create(mddev);
  1674. if (rv) {
  1675. bitmap_destroy(mddev);
  1676. mddev->bitmap_info.offset = 0;
  1677. }
  1678. mddev->pers->quiesce(mddev, 0);
  1679. if (rv)
  1680. return rv;
  1681. }
  1682. }
  1683. }
  1684. if (!mddev->external) {
  1685. /* Ensure new bitmap info is stored in
  1686. * metadata promptly.
  1687. */
  1688. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1689. md_wakeup_thread(mddev->thread);
  1690. }
  1691. return len;
  1692. }
  1693. static struct md_sysfs_entry bitmap_location =
  1694. __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
  1695. static ssize_t
  1696. timeout_show(struct mddev *mddev, char *page)
  1697. {
  1698. ssize_t len;
  1699. unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
  1700. unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
  1701. len = sprintf(page, "%lu", secs);
  1702. if (jifs)
  1703. len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
  1704. len += sprintf(page+len, "\n");
  1705. return len;
  1706. }
  1707. static ssize_t
  1708. timeout_store(struct mddev *mddev, const char *buf, size_t len)
  1709. {
  1710. /* timeout can be set at any time */
  1711. unsigned long timeout;
  1712. int rv = strict_strtoul_scaled(buf, &timeout, 4);
  1713. if (rv)
  1714. return rv;
  1715. /* just to make sure we don't overflow... */
  1716. if (timeout >= LONG_MAX / HZ)
  1717. return -EINVAL;
  1718. timeout = timeout * HZ / 10000;
  1719. if (timeout >= MAX_SCHEDULE_TIMEOUT)
  1720. timeout = MAX_SCHEDULE_TIMEOUT-1;
  1721. if (timeout < 1)
  1722. timeout = 1;
  1723. mddev->bitmap_info.daemon_sleep = timeout;
  1724. if (mddev->thread) {
  1725. /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
  1726. * the bitmap is all clean and we don't need to
  1727. * adjust the timeout right now
  1728. */
  1729. if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
  1730. mddev->thread->timeout = timeout;
  1731. md_wakeup_thread(mddev->thread);
  1732. }
  1733. }
  1734. return len;
  1735. }
  1736. static struct md_sysfs_entry bitmap_timeout =
  1737. __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
  1738. static ssize_t
  1739. backlog_show(struct mddev *mddev, char *page)
  1740. {
  1741. return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
  1742. }
  1743. static ssize_t
  1744. backlog_store(struct mddev *mddev, const char *buf, size_t len)
  1745. {
  1746. unsigned long backlog;
  1747. int rv = strict_strtoul(buf, 10, &backlog);
  1748. if (rv)
  1749. return rv;
  1750. if (backlog > COUNTER_MAX)
  1751. return -EINVAL;
  1752. mddev->bitmap_info.max_write_behind = backlog;
  1753. return len;
  1754. }
  1755. static struct md_sysfs_entry bitmap_backlog =
  1756. __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
  1757. static ssize_t
  1758. chunksize_show(struct mddev *mddev, char *page)
  1759. {
  1760. return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
  1761. }
  1762. static ssize_t
  1763. chunksize_store(struct mddev *mddev, const char *buf, size_t len)
  1764. {
  1765. /* Can only be changed when no bitmap is active */
  1766. int rv;
  1767. unsigned long csize;
  1768. if (mddev->bitmap)
  1769. return -EBUSY;
  1770. rv = strict_strtoul(buf, 10, &csize);
  1771. if (rv)
  1772. return rv;
  1773. if (csize < 512 ||
  1774. !is_power_of_2(csize))
  1775. return -EINVAL;
  1776. mddev->bitmap_info.chunksize = csize;
  1777. return len;
  1778. }
  1779. static struct md_sysfs_entry bitmap_chunksize =
  1780. __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
  1781. static ssize_t metadata_show(struct mddev *mddev, char *page)
  1782. {
  1783. return sprintf(page, "%s\n", (mddev->bitmap_info.external
  1784. ? "external" : "internal"));
  1785. }
  1786. static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
  1787. {
  1788. if (mddev->bitmap ||
  1789. mddev->bitmap_info.file ||
  1790. mddev->bitmap_info.offset)
  1791. return -EBUSY;
  1792. if (strncmp(buf, "external", 8) == 0)
  1793. mddev->bitmap_info.external = 1;
  1794. else if (strncmp(buf, "internal", 8) == 0)
  1795. mddev->bitmap_info.external = 0;
  1796. else
  1797. return -EINVAL;
  1798. return len;
  1799. }
  1800. static struct md_sysfs_entry bitmap_metadata =
  1801. __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
  1802. static ssize_t can_clear_show(struct mddev *mddev, char *page)
  1803. {
  1804. int len;
  1805. if (mddev->bitmap)
  1806. len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
  1807. "false" : "true"));
  1808. else
  1809. len = sprintf(page, "\n");
  1810. return len;
  1811. }
  1812. static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
  1813. {
  1814. if (mddev->bitmap == NULL)
  1815. return -ENOENT;
  1816. if (strncmp(buf, "false", 5) == 0)
  1817. mddev->bitmap->need_sync = 1;
  1818. else if (strncmp(buf, "true", 4) == 0) {
  1819. if (mddev->degraded)
  1820. return -EBUSY;
  1821. mddev->bitmap->need_sync = 0;
  1822. } else
  1823. return -EINVAL;
  1824. return len;
  1825. }
  1826. static struct md_sysfs_entry bitmap_can_clear =
  1827. __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
  1828. static ssize_t
  1829. behind_writes_used_show(struct mddev *mddev, char *page)
  1830. {
  1831. if (mddev->bitmap == NULL)
  1832. return sprintf(page, "0\n");
  1833. return sprintf(page, "%lu\n",
  1834. mddev->bitmap->behind_writes_used);
  1835. }
  1836. static ssize_t
  1837. behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
  1838. {
  1839. if (mddev->bitmap)
  1840. mddev->bitmap->behind_writes_used = 0;
  1841. return len;
  1842. }
  1843. static struct md_sysfs_entry max_backlog_used =
  1844. __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
  1845. behind_writes_used_show, behind_writes_used_reset);
  1846. static struct attribute *md_bitmap_attrs[] = {
  1847. &bitmap_location.attr,
  1848. &bitmap_timeout.attr,
  1849. &bitmap_backlog.attr,
  1850. &bitmap_chunksize.attr,
  1851. &bitmap_metadata.attr,
  1852. &bitmap_can_clear.attr,
  1853. &max_backlog_used.attr,
  1854. NULL
  1855. };
  1856. struct attribute_group md_bitmap_group = {
  1857. .name = "bitmap",
  1858. .attrs = md_bitmap_attrs,
  1859. };