bitmap.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120
  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. + bitmap->mddev->bitmap_info.daemon_sleep))
  1014. goto done;
  1015. bitmap->daemon_lastrun = jiffies;
  1016. if (bitmap->allclean) {
  1017. bitmap->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. bitmap->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. bitmap->mddev->thread->timeout =
  1125. bitmap->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. if (bitmap->mddev->degraded)
  1231. /* Never clear bits or update events_cleared when degraded */
  1232. success = 0;
  1233. while (sectors) {
  1234. sector_t blocks;
  1235. unsigned long flags;
  1236. bitmap_counter_t *bmc;
  1237. spin_lock_irqsave(&bitmap->lock, flags);
  1238. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1239. if (!bmc) {
  1240. spin_unlock_irqrestore(&bitmap->lock, flags);
  1241. return;
  1242. }
  1243. if (success &&
  1244. bitmap->events_cleared < bitmap->mddev->events) {
  1245. bitmap->events_cleared = bitmap->mddev->events;
  1246. bitmap->need_sync = 1;
  1247. sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
  1248. }
  1249. if (!success && !NEEDED(*bmc))
  1250. *bmc |= NEEDED_MASK;
  1251. if (COUNTER(*bmc) == COUNTER_MAX)
  1252. wake_up(&bitmap->overflow_wait);
  1253. (*bmc)--;
  1254. if (*bmc <= 2) {
  1255. set_page_attr(bitmap,
  1256. filemap_get_page(
  1257. bitmap,
  1258. offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1259. BITMAP_PAGE_PENDING);
  1260. bitmap->allclean = 0;
  1261. }
  1262. spin_unlock_irqrestore(&bitmap->lock, flags);
  1263. offset += blocks;
  1264. if (sectors > blocks)
  1265. sectors -= blocks;
  1266. else
  1267. sectors = 0;
  1268. }
  1269. }
  1270. EXPORT_SYMBOL(bitmap_endwrite);
  1271. static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1272. int degraded)
  1273. {
  1274. bitmap_counter_t *bmc;
  1275. int rv;
  1276. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1277. *blocks = 1024;
  1278. return 1; /* always resync if no bitmap */
  1279. }
  1280. spin_lock_irq(&bitmap->lock);
  1281. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1282. rv = 0;
  1283. if (bmc) {
  1284. /* locked */
  1285. if (RESYNC(*bmc))
  1286. rv = 1;
  1287. else if (NEEDED(*bmc)) {
  1288. rv = 1;
  1289. if (!degraded) { /* don't set/clear bits if degraded */
  1290. *bmc |= RESYNC_MASK;
  1291. *bmc &= ~NEEDED_MASK;
  1292. }
  1293. }
  1294. }
  1295. spin_unlock_irq(&bitmap->lock);
  1296. return rv;
  1297. }
  1298. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1299. int degraded)
  1300. {
  1301. /* bitmap_start_sync must always report on multiples of whole
  1302. * pages, otherwise resync (which is very PAGE_SIZE based) will
  1303. * get confused.
  1304. * So call __bitmap_start_sync repeatedly (if needed) until
  1305. * At least PAGE_SIZE>>9 blocks are covered.
  1306. * Return the 'or' of the result.
  1307. */
  1308. int rv = 0;
  1309. sector_t blocks1;
  1310. *blocks = 0;
  1311. while (*blocks < (PAGE_SIZE>>9)) {
  1312. rv |= __bitmap_start_sync(bitmap, offset,
  1313. &blocks1, degraded);
  1314. offset += blocks1;
  1315. *blocks += blocks1;
  1316. }
  1317. return rv;
  1318. }
  1319. EXPORT_SYMBOL(bitmap_start_sync);
  1320. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
  1321. {
  1322. bitmap_counter_t *bmc;
  1323. unsigned long flags;
  1324. if (bitmap == NULL) {
  1325. *blocks = 1024;
  1326. return;
  1327. }
  1328. spin_lock_irqsave(&bitmap->lock, flags);
  1329. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1330. if (bmc == NULL)
  1331. goto unlock;
  1332. /* locked */
  1333. if (RESYNC(*bmc)) {
  1334. *bmc &= ~RESYNC_MASK;
  1335. if (!NEEDED(*bmc) && aborted)
  1336. *bmc |= NEEDED_MASK;
  1337. else {
  1338. if (*bmc <= 2) {
  1339. set_page_attr(bitmap,
  1340. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1341. BITMAP_PAGE_PENDING);
  1342. bitmap->allclean = 0;
  1343. }
  1344. }
  1345. }
  1346. unlock:
  1347. spin_unlock_irqrestore(&bitmap->lock, flags);
  1348. }
  1349. EXPORT_SYMBOL(bitmap_end_sync);
  1350. void bitmap_close_sync(struct bitmap *bitmap)
  1351. {
  1352. /* Sync has finished, and any bitmap chunks that weren't synced
  1353. * properly have been aborted. It remains to us to clear the
  1354. * RESYNC bit wherever it is still on
  1355. */
  1356. sector_t sector = 0;
  1357. sector_t blocks;
  1358. if (!bitmap)
  1359. return;
  1360. while (sector < bitmap->mddev->resync_max_sectors) {
  1361. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1362. sector += blocks;
  1363. }
  1364. }
  1365. EXPORT_SYMBOL(bitmap_close_sync);
  1366. void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
  1367. {
  1368. sector_t s = 0;
  1369. sector_t blocks;
  1370. if (!bitmap)
  1371. return;
  1372. if (sector == 0) {
  1373. bitmap->last_end_sync = jiffies;
  1374. return;
  1375. }
  1376. if (time_before(jiffies, (bitmap->last_end_sync
  1377. + bitmap->mddev->bitmap_info.daemon_sleep)))
  1378. return;
  1379. wait_event(bitmap->mddev->recovery_wait,
  1380. atomic_read(&bitmap->mddev->recovery_active) == 0);
  1381. bitmap->mddev->curr_resync_completed = sector;
  1382. set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
  1383. sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
  1384. s = 0;
  1385. while (s < sector && s < bitmap->mddev->resync_max_sectors) {
  1386. bitmap_end_sync(bitmap, s, &blocks, 0);
  1387. s += blocks;
  1388. }
  1389. bitmap->last_end_sync = jiffies;
  1390. sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
  1391. }
  1392. EXPORT_SYMBOL(bitmap_cond_end_sync);
  1393. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1394. {
  1395. /* For each chunk covered by any of these sectors, set the
  1396. * counter to 1 and set resync_needed. They should all
  1397. * be 0 at this point
  1398. */
  1399. sector_t secs;
  1400. bitmap_counter_t *bmc;
  1401. spin_lock_irq(&bitmap->lock);
  1402. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1403. if (!bmc) {
  1404. spin_unlock_irq(&bitmap->lock);
  1405. return;
  1406. }
  1407. if (!*bmc) {
  1408. struct page *page;
  1409. *bmc = 1 | (needed ? NEEDED_MASK : 0);
  1410. bitmap_count_page(bitmap, offset, 1);
  1411. page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
  1412. set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
  1413. bitmap->allclean = 0;
  1414. }
  1415. spin_unlock_irq(&bitmap->lock);
  1416. }
  1417. /* dirty the memory and file bits for bitmap chunks "s" to "e" */
  1418. void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
  1419. {
  1420. unsigned long chunk;
  1421. for (chunk = s; chunk <= e; chunk++) {
  1422. sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
  1423. bitmap_set_memory_bits(bitmap, sec, 1);
  1424. spin_lock_irq(&bitmap->lock);
  1425. bitmap_file_set_bit(bitmap, sec);
  1426. spin_unlock_irq(&bitmap->lock);
  1427. if (sec < bitmap->mddev->recovery_cp)
  1428. /* We are asserting that the array is dirty,
  1429. * so move the recovery_cp address back so
  1430. * that it is obvious that it is dirty
  1431. */
  1432. bitmap->mddev->recovery_cp = sec;
  1433. }
  1434. }
  1435. /*
  1436. * flush out any pending updates
  1437. */
  1438. void bitmap_flush(struct mddev *mddev)
  1439. {
  1440. struct bitmap *bitmap = mddev->bitmap;
  1441. long sleep;
  1442. if (!bitmap) /* there was no bitmap */
  1443. return;
  1444. /* run the daemon_work three time to ensure everything is flushed
  1445. * that can be
  1446. */
  1447. sleep = mddev->bitmap_info.daemon_sleep * 2;
  1448. bitmap->daemon_lastrun -= sleep;
  1449. bitmap_daemon_work(mddev);
  1450. bitmap->daemon_lastrun -= sleep;
  1451. bitmap_daemon_work(mddev);
  1452. bitmap->daemon_lastrun -= sleep;
  1453. bitmap_daemon_work(mddev);
  1454. bitmap_update_sb(bitmap);
  1455. }
  1456. /*
  1457. * free memory that was allocated
  1458. */
  1459. static void bitmap_free(struct bitmap *bitmap)
  1460. {
  1461. unsigned long k, pages;
  1462. struct bitmap_page *bp;
  1463. if (!bitmap) /* there was no bitmap */
  1464. return;
  1465. /* release the bitmap file and kill the daemon */
  1466. bitmap_file_put(bitmap);
  1467. bp = bitmap->bp;
  1468. pages = bitmap->pages;
  1469. /* free all allocated memory */
  1470. if (bp) /* deallocate the page memory */
  1471. for (k = 0; k < pages; k++)
  1472. if (bp[k].map && !bp[k].hijacked)
  1473. kfree(bp[k].map);
  1474. kfree(bp);
  1475. kfree(bitmap);
  1476. }
  1477. void bitmap_destroy(struct mddev *mddev)
  1478. {
  1479. struct bitmap *bitmap = mddev->bitmap;
  1480. if (!bitmap) /* there was no bitmap */
  1481. return;
  1482. mutex_lock(&mddev->bitmap_info.mutex);
  1483. mddev->bitmap = NULL; /* disconnect from the md device */
  1484. mutex_unlock(&mddev->bitmap_info.mutex);
  1485. if (mddev->thread)
  1486. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1487. if (bitmap->sysfs_can_clear)
  1488. sysfs_put(bitmap->sysfs_can_clear);
  1489. bitmap_free(bitmap);
  1490. }
  1491. /*
  1492. * initialize the bitmap structure
  1493. * if this returns an error, bitmap_destroy must be called to do clean up
  1494. */
  1495. int bitmap_create(struct mddev *mddev)
  1496. {
  1497. struct bitmap *bitmap;
  1498. sector_t blocks = mddev->resync_max_sectors;
  1499. unsigned long chunks;
  1500. unsigned long pages;
  1501. struct file *file = mddev->bitmap_info.file;
  1502. int err;
  1503. struct sysfs_dirent *bm = NULL;
  1504. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1505. if (!file
  1506. && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
  1507. return 0;
  1508. BUG_ON(file && mddev->bitmap_info.offset);
  1509. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1510. if (!bitmap)
  1511. return -ENOMEM;
  1512. spin_lock_init(&bitmap->lock);
  1513. atomic_set(&bitmap->pending_writes, 0);
  1514. init_waitqueue_head(&bitmap->write_wait);
  1515. init_waitqueue_head(&bitmap->overflow_wait);
  1516. init_waitqueue_head(&bitmap->behind_wait);
  1517. bitmap->mddev = mddev;
  1518. if (mddev->kobj.sd)
  1519. bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
  1520. if (bm) {
  1521. bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
  1522. sysfs_put(bm);
  1523. } else
  1524. bitmap->sysfs_can_clear = NULL;
  1525. bitmap->file = file;
  1526. if (file) {
  1527. get_file(file);
  1528. /* As future accesses to this file will use bmap,
  1529. * and bypass the page cache, we must sync the file
  1530. * first.
  1531. */
  1532. vfs_fsync(file, 1);
  1533. }
  1534. /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
  1535. if (!mddev->bitmap_info.external) {
  1536. /*
  1537. * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
  1538. * instructing us to create a new on-disk bitmap instance.
  1539. */
  1540. if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
  1541. err = bitmap_new_disk_sb(bitmap);
  1542. else
  1543. err = bitmap_read_sb(bitmap);
  1544. } else {
  1545. err = 0;
  1546. if (mddev->bitmap_info.chunksize == 0 ||
  1547. mddev->bitmap_info.daemon_sleep == 0)
  1548. /* chunksize and time_base need to be
  1549. * set first. */
  1550. err = -EINVAL;
  1551. }
  1552. if (err)
  1553. goto error;
  1554. bitmap->daemon_lastrun = jiffies;
  1555. bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
  1556. /* now that chunksize and chunkshift are set, we can use these macros */
  1557. chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
  1558. CHUNK_BLOCK_SHIFT(bitmap);
  1559. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1560. BUG_ON(!pages);
  1561. bitmap->chunks = chunks;
  1562. bitmap->pages = pages;
  1563. bitmap->missing_pages = pages;
  1564. bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1565. err = -ENOMEM;
  1566. if (!bitmap->bp)
  1567. goto error;
  1568. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1569. pages, bmname(bitmap));
  1570. mddev->bitmap = bitmap;
  1571. return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
  1572. error:
  1573. bitmap_free(bitmap);
  1574. return err;
  1575. }
  1576. int bitmap_load(struct mddev *mddev)
  1577. {
  1578. int err = 0;
  1579. sector_t start = 0;
  1580. sector_t sector = 0;
  1581. struct bitmap *bitmap = mddev->bitmap;
  1582. if (!bitmap)
  1583. goto out;
  1584. /* Clear out old bitmap info first: Either there is none, or we
  1585. * are resuming after someone else has possibly changed things,
  1586. * so we should forget old cached info.
  1587. * All chunks should be clean, but some might need_sync.
  1588. */
  1589. while (sector < mddev->resync_max_sectors) {
  1590. sector_t blocks;
  1591. bitmap_start_sync(bitmap, sector, &blocks, 0);
  1592. sector += blocks;
  1593. }
  1594. bitmap_close_sync(bitmap);
  1595. if (mddev->degraded == 0
  1596. || bitmap->events_cleared == mddev->events)
  1597. /* no need to keep dirty bits to optimise a
  1598. * re-add of a missing device */
  1599. start = mddev->recovery_cp;
  1600. err = bitmap_init_from_disk(bitmap, start);
  1601. if (err)
  1602. goto out;
  1603. mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
  1604. md_wakeup_thread(mddev->thread);
  1605. bitmap_update_sb(bitmap);
  1606. if (bitmap->flags & BITMAP_WRITE_ERROR)
  1607. err = -EIO;
  1608. out:
  1609. return err;
  1610. }
  1611. EXPORT_SYMBOL_GPL(bitmap_load);
  1612. static ssize_t
  1613. location_show(struct mddev *mddev, char *page)
  1614. {
  1615. ssize_t len;
  1616. if (mddev->bitmap_info.file)
  1617. len = sprintf(page, "file");
  1618. else if (mddev->bitmap_info.offset)
  1619. len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
  1620. else
  1621. len = sprintf(page, "none");
  1622. len += sprintf(page+len, "\n");
  1623. return len;
  1624. }
  1625. static ssize_t
  1626. location_store(struct mddev *mddev, const char *buf, size_t len)
  1627. {
  1628. if (mddev->pers) {
  1629. if (!mddev->pers->quiesce)
  1630. return -EBUSY;
  1631. if (mddev->recovery || mddev->sync_thread)
  1632. return -EBUSY;
  1633. }
  1634. if (mddev->bitmap || mddev->bitmap_info.file ||
  1635. mddev->bitmap_info.offset) {
  1636. /* bitmap already configured. Only option is to clear it */
  1637. if (strncmp(buf, "none", 4) != 0)
  1638. return -EBUSY;
  1639. if (mddev->pers) {
  1640. mddev->pers->quiesce(mddev, 1);
  1641. bitmap_destroy(mddev);
  1642. mddev->pers->quiesce(mddev, 0);
  1643. }
  1644. mddev->bitmap_info.offset = 0;
  1645. if (mddev->bitmap_info.file) {
  1646. struct file *f = mddev->bitmap_info.file;
  1647. mddev->bitmap_info.file = NULL;
  1648. restore_bitmap_write_access(f);
  1649. fput(f);
  1650. }
  1651. } else {
  1652. /* No bitmap, OK to set a location */
  1653. long long offset;
  1654. if (strncmp(buf, "none", 4) == 0)
  1655. /* nothing to be done */;
  1656. else if (strncmp(buf, "file:", 5) == 0) {
  1657. /* Not supported yet */
  1658. return -EINVAL;
  1659. } else {
  1660. int rv;
  1661. if (buf[0] == '+')
  1662. rv = strict_strtoll(buf+1, 10, &offset);
  1663. else
  1664. rv = strict_strtoll(buf, 10, &offset);
  1665. if (rv)
  1666. return rv;
  1667. if (offset == 0)
  1668. return -EINVAL;
  1669. if (mddev->bitmap_info.external == 0 &&
  1670. mddev->major_version == 0 &&
  1671. offset != mddev->bitmap_info.default_offset)
  1672. return -EINVAL;
  1673. mddev->bitmap_info.offset = offset;
  1674. if (mddev->pers) {
  1675. mddev->pers->quiesce(mddev, 1);
  1676. rv = bitmap_create(mddev);
  1677. if (rv) {
  1678. bitmap_destroy(mddev);
  1679. mddev->bitmap_info.offset = 0;
  1680. }
  1681. mddev->pers->quiesce(mddev, 0);
  1682. if (rv)
  1683. return rv;
  1684. }
  1685. }
  1686. }
  1687. if (!mddev->external) {
  1688. /* Ensure new bitmap info is stored in
  1689. * metadata promptly.
  1690. */
  1691. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1692. md_wakeup_thread(mddev->thread);
  1693. }
  1694. return len;
  1695. }
  1696. static struct md_sysfs_entry bitmap_location =
  1697. __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
  1698. static ssize_t
  1699. timeout_show(struct mddev *mddev, char *page)
  1700. {
  1701. ssize_t len;
  1702. unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
  1703. unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
  1704. len = sprintf(page, "%lu", secs);
  1705. if (jifs)
  1706. len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
  1707. len += sprintf(page+len, "\n");
  1708. return len;
  1709. }
  1710. static ssize_t
  1711. timeout_store(struct mddev *mddev, const char *buf, size_t len)
  1712. {
  1713. /* timeout can be set at any time */
  1714. unsigned long timeout;
  1715. int rv = strict_strtoul_scaled(buf, &timeout, 4);
  1716. if (rv)
  1717. return rv;
  1718. /* just to make sure we don't overflow... */
  1719. if (timeout >= LONG_MAX / HZ)
  1720. return -EINVAL;
  1721. timeout = timeout * HZ / 10000;
  1722. if (timeout >= MAX_SCHEDULE_TIMEOUT)
  1723. timeout = MAX_SCHEDULE_TIMEOUT-1;
  1724. if (timeout < 1)
  1725. timeout = 1;
  1726. mddev->bitmap_info.daemon_sleep = timeout;
  1727. if (mddev->thread) {
  1728. /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
  1729. * the bitmap is all clean and we don't need to
  1730. * adjust the timeout right now
  1731. */
  1732. if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
  1733. mddev->thread->timeout = timeout;
  1734. md_wakeup_thread(mddev->thread);
  1735. }
  1736. }
  1737. return len;
  1738. }
  1739. static struct md_sysfs_entry bitmap_timeout =
  1740. __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
  1741. static ssize_t
  1742. backlog_show(struct mddev *mddev, char *page)
  1743. {
  1744. return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
  1745. }
  1746. static ssize_t
  1747. backlog_store(struct mddev *mddev, const char *buf, size_t len)
  1748. {
  1749. unsigned long backlog;
  1750. int rv = strict_strtoul(buf, 10, &backlog);
  1751. if (rv)
  1752. return rv;
  1753. if (backlog > COUNTER_MAX)
  1754. return -EINVAL;
  1755. mddev->bitmap_info.max_write_behind = backlog;
  1756. return len;
  1757. }
  1758. static struct md_sysfs_entry bitmap_backlog =
  1759. __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
  1760. static ssize_t
  1761. chunksize_show(struct mddev *mddev, char *page)
  1762. {
  1763. return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
  1764. }
  1765. static ssize_t
  1766. chunksize_store(struct mddev *mddev, const char *buf, size_t len)
  1767. {
  1768. /* Can only be changed when no bitmap is active */
  1769. int rv;
  1770. unsigned long csize;
  1771. if (mddev->bitmap)
  1772. return -EBUSY;
  1773. rv = strict_strtoul(buf, 10, &csize);
  1774. if (rv)
  1775. return rv;
  1776. if (csize < 512 ||
  1777. !is_power_of_2(csize))
  1778. return -EINVAL;
  1779. mddev->bitmap_info.chunksize = csize;
  1780. return len;
  1781. }
  1782. static struct md_sysfs_entry bitmap_chunksize =
  1783. __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
  1784. static ssize_t metadata_show(struct mddev *mddev, char *page)
  1785. {
  1786. return sprintf(page, "%s\n", (mddev->bitmap_info.external
  1787. ? "external" : "internal"));
  1788. }
  1789. static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
  1790. {
  1791. if (mddev->bitmap ||
  1792. mddev->bitmap_info.file ||
  1793. mddev->bitmap_info.offset)
  1794. return -EBUSY;
  1795. if (strncmp(buf, "external", 8) == 0)
  1796. mddev->bitmap_info.external = 1;
  1797. else if (strncmp(buf, "internal", 8) == 0)
  1798. mddev->bitmap_info.external = 0;
  1799. else
  1800. return -EINVAL;
  1801. return len;
  1802. }
  1803. static struct md_sysfs_entry bitmap_metadata =
  1804. __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
  1805. static ssize_t can_clear_show(struct mddev *mddev, char *page)
  1806. {
  1807. int len;
  1808. if (mddev->bitmap)
  1809. len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
  1810. "false" : "true"));
  1811. else
  1812. len = sprintf(page, "\n");
  1813. return len;
  1814. }
  1815. static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
  1816. {
  1817. if (mddev->bitmap == NULL)
  1818. return -ENOENT;
  1819. if (strncmp(buf, "false", 5) == 0)
  1820. mddev->bitmap->need_sync = 1;
  1821. else if (strncmp(buf, "true", 4) == 0) {
  1822. if (mddev->degraded)
  1823. return -EBUSY;
  1824. mddev->bitmap->need_sync = 0;
  1825. } else
  1826. return -EINVAL;
  1827. return len;
  1828. }
  1829. static struct md_sysfs_entry bitmap_can_clear =
  1830. __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
  1831. static ssize_t
  1832. behind_writes_used_show(struct mddev *mddev, char *page)
  1833. {
  1834. if (mddev->bitmap == NULL)
  1835. return sprintf(page, "0\n");
  1836. return sprintf(page, "%lu\n",
  1837. mddev->bitmap->behind_writes_used);
  1838. }
  1839. static ssize_t
  1840. behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
  1841. {
  1842. if (mddev->bitmap)
  1843. mddev->bitmap->behind_writes_used = 0;
  1844. return len;
  1845. }
  1846. static struct md_sysfs_entry max_backlog_used =
  1847. __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
  1848. behind_writes_used_show, behind_writes_used_reset);
  1849. static struct attribute *md_bitmap_attrs[] = {
  1850. &bitmap_location.attr,
  1851. &bitmap_timeout.attr,
  1852. &bitmap_backlog.attr,
  1853. &bitmap_chunksize.attr,
  1854. &bitmap_metadata.attr,
  1855. &bitmap_can_clear.attr,
  1856. &max_backlog_used.attr,
  1857. NULL
  1858. };
  1859. struct attribute_group md_bitmap_group = {
  1860. .name = "bitmap",
  1861. .attrs = md_bitmap_attrs,
  1862. };