bitmap.c 56 KB

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