bitmap.c 60 KB

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