bitmap.c 56 KB

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