bitmap.c 55 KB

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