bitmap.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261
  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. */
  596. static inline struct page *filemap_get_page(struct bitmap_storage *store,
  597. unsigned long chunk)
  598. {
  599. if (file_page_index(store, chunk) >= store->file_pages)
  600. return NULL;
  601. return store->filemap[file_page_index(store, chunk)];
  602. }
  603. static int bitmap_storage_alloc(struct bitmap_storage *store,
  604. unsigned long chunks, int with_super)
  605. {
  606. int pnum;
  607. unsigned long num_pages;
  608. unsigned long bytes;
  609. bytes = DIV_ROUND_UP(chunks, 8);
  610. if (with_super)
  611. bytes += sizeof(bitmap_super_t);
  612. num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
  613. store->filemap = kmalloc(sizeof(struct page *)
  614. * num_pages, GFP_KERNEL);
  615. if (!store->filemap)
  616. return -ENOMEM;
  617. if (with_super && !store->sb_page) {
  618. store->sb_page = alloc_page(GFP_KERNEL|__GFP_ZERO);
  619. if (store->sb_page == NULL)
  620. return -ENOMEM;
  621. store->sb_page->index = 0;
  622. }
  623. pnum = 0;
  624. if (store->sb_page) {
  625. store->filemap[0] = store->sb_page;
  626. pnum = 1;
  627. }
  628. for ( ; pnum < num_pages; pnum++) {
  629. store->filemap[pnum] = alloc_page(GFP_KERNEL|__GFP_ZERO);
  630. if (!store->filemap[pnum]) {
  631. store->file_pages = pnum;
  632. return -ENOMEM;
  633. }
  634. store->filemap[pnum]->index = pnum;
  635. }
  636. store->file_pages = pnum;
  637. /* We need 4 bits per page, rounded up to a multiple
  638. * of sizeof(unsigned long) */
  639. store->filemap_attr = kzalloc(
  640. roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
  641. GFP_KERNEL);
  642. if (!store->filemap_attr)
  643. return -ENOMEM;
  644. store->bytes = bytes;
  645. return 0;
  646. }
  647. static void bitmap_file_unmap(struct bitmap_storage *store)
  648. {
  649. struct page **map, *sb_page;
  650. int pages;
  651. struct file *file;
  652. file = store->file;
  653. map = store->filemap;
  654. pages = store->file_pages;
  655. sb_page = store->sb_page;
  656. while (pages--)
  657. if (map[pages] != sb_page) /* 0 is sb_page, release it below */
  658. free_buffers(map[pages]);
  659. kfree(map);
  660. kfree(store->filemap_attr);
  661. if (sb_page)
  662. free_buffers(sb_page);
  663. if (file) {
  664. struct inode *inode = file_inode(file);
  665. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  666. fput(file);
  667. }
  668. }
  669. /*
  670. * bitmap_file_kick - if an error occurs while manipulating the bitmap file
  671. * then it is no longer reliable, so we stop using it and we mark the file
  672. * as failed in the superblock
  673. */
  674. static void bitmap_file_kick(struct bitmap *bitmap)
  675. {
  676. char *path, *ptr = NULL;
  677. if (!test_and_set_bit(BITMAP_STALE, &bitmap->flags)) {
  678. bitmap_update_sb(bitmap);
  679. if (bitmap->storage.file) {
  680. path = kmalloc(PAGE_SIZE, GFP_KERNEL);
  681. if (path)
  682. ptr = d_path(&bitmap->storage.file->f_path,
  683. path, PAGE_SIZE);
  684. printk(KERN_ALERT
  685. "%s: kicking failed bitmap file %s from array!\n",
  686. bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
  687. kfree(path);
  688. } else
  689. printk(KERN_ALERT
  690. "%s: disabling internal bitmap due to errors\n",
  691. bmname(bitmap));
  692. }
  693. }
  694. enum bitmap_page_attr {
  695. BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
  696. BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
  697. * i.e. counter is 1 or 2. */
  698. BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
  699. };
  700. static inline void set_page_attr(struct bitmap *bitmap, int pnum,
  701. enum bitmap_page_attr attr)
  702. {
  703. set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
  704. }
  705. static inline void clear_page_attr(struct bitmap *bitmap, int pnum,
  706. enum bitmap_page_attr attr)
  707. {
  708. clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
  709. }
  710. static inline int test_page_attr(struct bitmap *bitmap, int pnum,
  711. enum bitmap_page_attr attr)
  712. {
  713. return test_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
  714. }
  715. static inline int test_and_clear_page_attr(struct bitmap *bitmap, int pnum,
  716. enum bitmap_page_attr attr)
  717. {
  718. return test_and_clear_bit((pnum<<2) + attr,
  719. bitmap->storage.filemap_attr);
  720. }
  721. /*
  722. * bitmap_file_set_bit -- called before performing a write to the md device
  723. * to set (and eventually sync) a particular bit in the bitmap file
  724. *
  725. * we set the bit immediately, then we record the page number so that
  726. * when an unplug occurs, we can flush the dirty pages out to disk
  727. */
  728. static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
  729. {
  730. unsigned long bit;
  731. struct page *page;
  732. void *kaddr;
  733. unsigned long chunk = block >> bitmap->counts.chunkshift;
  734. page = filemap_get_page(&bitmap->storage, chunk);
  735. if (!page)
  736. return;
  737. bit = file_page_offset(&bitmap->storage, chunk);
  738. /* set the bit */
  739. kaddr = kmap_atomic(page);
  740. if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
  741. set_bit(bit, kaddr);
  742. else
  743. set_bit_le(bit, kaddr);
  744. kunmap_atomic(kaddr);
  745. pr_debug("set file bit %lu page %lu\n", bit, page->index);
  746. /* record page number so it gets flushed to disk when unplug occurs */
  747. set_page_attr(bitmap, page->index, BITMAP_PAGE_DIRTY);
  748. }
  749. static void bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block)
  750. {
  751. unsigned long bit;
  752. struct page *page;
  753. void *paddr;
  754. unsigned long chunk = block >> bitmap->counts.chunkshift;
  755. page = filemap_get_page(&bitmap->storage, chunk);
  756. if (!page)
  757. return;
  758. bit = file_page_offset(&bitmap->storage, chunk);
  759. paddr = kmap_atomic(page);
  760. if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
  761. clear_bit(bit, paddr);
  762. else
  763. clear_bit_le(bit, paddr);
  764. kunmap_atomic(paddr);
  765. if (!test_page_attr(bitmap, page->index, BITMAP_PAGE_NEEDWRITE)) {
  766. set_page_attr(bitmap, page->index, BITMAP_PAGE_PENDING);
  767. bitmap->allclean = 0;
  768. }
  769. }
  770. /* this gets called when the md device is ready to unplug its underlying
  771. * (slave) device queues -- before we let any writes go down, we need to
  772. * sync the dirty pages of the bitmap file to disk */
  773. void bitmap_unplug(struct bitmap *bitmap)
  774. {
  775. unsigned long i;
  776. int dirty, need_write;
  777. if (!bitmap || !bitmap->storage.filemap ||
  778. test_bit(BITMAP_STALE, &bitmap->flags))
  779. return;
  780. /* look at each page to see if there are any set bits that need to be
  781. * flushed out to disk */
  782. for (i = 0; i < bitmap->storage.file_pages; i++) {
  783. if (!bitmap->storage.filemap)
  784. return;
  785. dirty = test_and_clear_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
  786. need_write = test_and_clear_page_attr(bitmap, i,
  787. BITMAP_PAGE_NEEDWRITE);
  788. if (dirty || need_write) {
  789. clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING);
  790. write_page(bitmap, bitmap->storage.filemap[i], 0);
  791. }
  792. }
  793. if (bitmap->storage.file)
  794. wait_event(bitmap->write_wait,
  795. atomic_read(&bitmap->pending_writes)==0);
  796. else
  797. md_super_wait(bitmap->mddev);
  798. if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
  799. bitmap_file_kick(bitmap);
  800. }
  801. EXPORT_SYMBOL(bitmap_unplug);
  802. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
  803. /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
  804. * the in-memory bitmap from the on-disk bitmap -- also, sets up the
  805. * memory mapping of the bitmap file
  806. * Special cases:
  807. * if there's no bitmap file, or if the bitmap file had been
  808. * previously kicked from the array, we mark all the bits as
  809. * 1's in order to cause a full resync.
  810. *
  811. * We ignore all bits for sectors that end earlier than 'start'.
  812. * This is used when reading an out-of-date bitmap...
  813. */
  814. static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
  815. {
  816. unsigned long i, chunks, index, oldindex, bit;
  817. struct page *page = NULL;
  818. unsigned long bit_cnt = 0;
  819. struct file *file;
  820. unsigned long offset;
  821. int outofdate;
  822. int ret = -ENOSPC;
  823. void *paddr;
  824. struct bitmap_storage *store = &bitmap->storage;
  825. chunks = bitmap->counts.chunks;
  826. file = store->file;
  827. if (!file && !bitmap->mddev->bitmap_info.offset) {
  828. /* No permanent bitmap - fill with '1s'. */
  829. store->filemap = NULL;
  830. store->file_pages = 0;
  831. for (i = 0; i < chunks ; i++) {
  832. /* if the disk bit is set, set the memory bit */
  833. int needed = ((sector_t)(i+1) << (bitmap->counts.chunkshift)
  834. >= start);
  835. bitmap_set_memory_bits(bitmap,
  836. (sector_t)i << bitmap->counts.chunkshift,
  837. needed);
  838. }
  839. return 0;
  840. }
  841. outofdate = test_bit(BITMAP_STALE, &bitmap->flags);
  842. if (outofdate)
  843. printk(KERN_INFO "%s: bitmap file is out of date, doing full "
  844. "recovery\n", bmname(bitmap));
  845. if (file && i_size_read(file->f_mapping->host) < store->bytes) {
  846. printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
  847. bmname(bitmap),
  848. (unsigned long) i_size_read(file->f_mapping->host),
  849. store->bytes);
  850. goto err;
  851. }
  852. oldindex = ~0L;
  853. offset = 0;
  854. if (!bitmap->mddev->bitmap_info.external)
  855. offset = sizeof(bitmap_super_t);
  856. for (i = 0; i < chunks; i++) {
  857. int b;
  858. index = file_page_index(&bitmap->storage, i);
  859. bit = file_page_offset(&bitmap->storage, i);
  860. if (index != oldindex) { /* this is a new page, read it in */
  861. int count;
  862. /* unmap the old page, we're done with it */
  863. if (index == store->file_pages-1)
  864. count = store->bytes - index * PAGE_SIZE;
  865. else
  866. count = PAGE_SIZE;
  867. page = store->filemap[index];
  868. if (file)
  869. ret = read_page(file, index, bitmap,
  870. count, page);
  871. else
  872. ret = read_sb_page(
  873. bitmap->mddev,
  874. bitmap->mddev->bitmap_info.offset,
  875. page,
  876. index, count);
  877. if (ret)
  878. goto err;
  879. oldindex = index;
  880. if (outofdate) {
  881. /*
  882. * if bitmap is out of date, dirty the
  883. * whole page and write it out
  884. */
  885. paddr = kmap_atomic(page);
  886. memset(paddr + offset, 0xff,
  887. PAGE_SIZE - offset);
  888. kunmap_atomic(paddr);
  889. write_page(bitmap, page, 1);
  890. ret = -EIO;
  891. if (test_bit(BITMAP_WRITE_ERROR,
  892. &bitmap->flags))
  893. goto err;
  894. }
  895. }
  896. paddr = kmap_atomic(page);
  897. if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
  898. b = test_bit(bit, paddr);
  899. else
  900. b = test_bit_le(bit, paddr);
  901. kunmap_atomic(paddr);
  902. if (b) {
  903. /* if the disk bit is set, set the memory bit */
  904. int needed = ((sector_t)(i+1) << bitmap->counts.chunkshift
  905. >= start);
  906. bitmap_set_memory_bits(bitmap,
  907. (sector_t)i << bitmap->counts.chunkshift,
  908. needed);
  909. bit_cnt++;
  910. }
  911. offset = 0;
  912. }
  913. printk(KERN_INFO "%s: bitmap initialized from disk: "
  914. "read %lu pages, set %lu of %lu bits\n",
  915. bmname(bitmap), store->file_pages,
  916. bit_cnt, chunks);
  917. return 0;
  918. err:
  919. printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
  920. bmname(bitmap), ret);
  921. return ret;
  922. }
  923. void bitmap_write_all(struct bitmap *bitmap)
  924. {
  925. /* We don't actually write all bitmap blocks here,
  926. * just flag them as needing to be written
  927. */
  928. int i;
  929. if (!bitmap || !bitmap->storage.filemap)
  930. return;
  931. if (bitmap->storage.file)
  932. /* Only one copy, so nothing needed */
  933. return;
  934. for (i = 0; i < bitmap->storage.file_pages; i++)
  935. set_page_attr(bitmap, i,
  936. BITMAP_PAGE_NEEDWRITE);
  937. bitmap->allclean = 0;
  938. }
  939. static void bitmap_count_page(struct bitmap_counts *bitmap,
  940. sector_t offset, int inc)
  941. {
  942. sector_t chunk = offset >> bitmap->chunkshift;
  943. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  944. bitmap->bp[page].count += inc;
  945. bitmap_checkfree(bitmap, page);
  946. }
  947. static void bitmap_set_pending(struct bitmap_counts *bitmap, sector_t offset)
  948. {
  949. sector_t chunk = offset >> bitmap->chunkshift;
  950. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  951. struct bitmap_page *bp = &bitmap->bp[page];
  952. if (!bp->pending)
  953. bp->pending = 1;
  954. }
  955. static bitmap_counter_t *bitmap_get_counter(struct bitmap_counts *bitmap,
  956. sector_t offset, sector_t *blocks,
  957. int create);
  958. /*
  959. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  960. * out to disk
  961. */
  962. void bitmap_daemon_work(struct mddev *mddev)
  963. {
  964. struct bitmap *bitmap;
  965. unsigned long j;
  966. unsigned long nextpage;
  967. sector_t blocks;
  968. struct bitmap_counts *counts;
  969. /* Use a mutex to guard daemon_work against
  970. * bitmap_destroy.
  971. */
  972. mutex_lock(&mddev->bitmap_info.mutex);
  973. bitmap = mddev->bitmap;
  974. if (bitmap == NULL) {
  975. mutex_unlock(&mddev->bitmap_info.mutex);
  976. return;
  977. }
  978. if (time_before(jiffies, bitmap->daemon_lastrun
  979. + mddev->bitmap_info.daemon_sleep))
  980. goto done;
  981. bitmap->daemon_lastrun = jiffies;
  982. if (bitmap->allclean) {
  983. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  984. goto done;
  985. }
  986. bitmap->allclean = 1;
  987. /* Any file-page which is PENDING now needs to be written.
  988. * So set NEEDWRITE now, then after we make any last-minute changes
  989. * we will write it.
  990. */
  991. for (j = 0; j < bitmap->storage.file_pages; j++)
  992. if (test_and_clear_page_attr(bitmap, j,
  993. BITMAP_PAGE_PENDING))
  994. set_page_attr(bitmap, j,
  995. BITMAP_PAGE_NEEDWRITE);
  996. if (bitmap->need_sync &&
  997. mddev->bitmap_info.external == 0) {
  998. /* Arrange for superblock update as well as
  999. * other changes */
  1000. bitmap_super_t *sb;
  1001. bitmap->need_sync = 0;
  1002. if (bitmap->storage.filemap) {
  1003. sb = kmap_atomic(bitmap->storage.sb_page);
  1004. sb->events_cleared =
  1005. cpu_to_le64(bitmap->events_cleared);
  1006. kunmap_atomic(sb);
  1007. set_page_attr(bitmap, 0,
  1008. BITMAP_PAGE_NEEDWRITE);
  1009. }
  1010. }
  1011. /* Now look at the bitmap counters and if any are '2' or '1',
  1012. * decrement and handle accordingly.
  1013. */
  1014. counts = &bitmap->counts;
  1015. spin_lock_irq(&counts->lock);
  1016. nextpage = 0;
  1017. for (j = 0; j < counts->chunks; j++) {
  1018. bitmap_counter_t *bmc;
  1019. sector_t block = (sector_t)j << counts->chunkshift;
  1020. if (j == nextpage) {
  1021. nextpage += PAGE_COUNTER_RATIO;
  1022. if (!counts->bp[j >> PAGE_COUNTER_SHIFT].pending) {
  1023. j |= PAGE_COUNTER_MASK;
  1024. continue;
  1025. }
  1026. counts->bp[j >> PAGE_COUNTER_SHIFT].pending = 0;
  1027. }
  1028. bmc = bitmap_get_counter(counts,
  1029. block,
  1030. &blocks, 0);
  1031. if (!bmc) {
  1032. j |= PAGE_COUNTER_MASK;
  1033. continue;
  1034. }
  1035. if (*bmc == 1 && !bitmap->need_sync) {
  1036. /* We can clear the bit */
  1037. *bmc = 0;
  1038. bitmap_count_page(counts, block, -1);
  1039. bitmap_file_clear_bit(bitmap, block);
  1040. } else if (*bmc && *bmc <= 2) {
  1041. *bmc = 1;
  1042. bitmap_set_pending(counts, block);
  1043. bitmap->allclean = 0;
  1044. }
  1045. }
  1046. spin_unlock_irq(&counts->lock);
  1047. /* Now start writeout on any page in NEEDWRITE that isn't DIRTY.
  1048. * DIRTY pages need to be written by bitmap_unplug so it can wait
  1049. * for them.
  1050. * If we find any DIRTY page we stop there and let bitmap_unplug
  1051. * handle all the rest. This is important in the case where
  1052. * the first blocking holds the superblock and it has been updated.
  1053. * We mustn't write any other blocks before the superblock.
  1054. */
  1055. for (j = 0;
  1056. j < bitmap->storage.file_pages
  1057. && !test_bit(BITMAP_STALE, &bitmap->flags);
  1058. j++) {
  1059. if (test_page_attr(bitmap, j,
  1060. BITMAP_PAGE_DIRTY))
  1061. /* bitmap_unplug will handle the rest */
  1062. break;
  1063. if (test_and_clear_page_attr(bitmap, j,
  1064. BITMAP_PAGE_NEEDWRITE)) {
  1065. write_page(bitmap, bitmap->storage.filemap[j], 0);
  1066. }
  1067. }
  1068. done:
  1069. if (bitmap->allclean == 0)
  1070. mddev->thread->timeout =
  1071. mddev->bitmap_info.daemon_sleep;
  1072. mutex_unlock(&mddev->bitmap_info.mutex);
  1073. }
  1074. static bitmap_counter_t *bitmap_get_counter(struct bitmap_counts *bitmap,
  1075. sector_t offset, sector_t *blocks,
  1076. int create)
  1077. __releases(bitmap->lock)
  1078. __acquires(bitmap->lock)
  1079. {
  1080. /* If 'create', we might release the lock and reclaim it.
  1081. * The lock must have been taken with interrupts enabled.
  1082. * If !create, we don't release the lock.
  1083. */
  1084. sector_t chunk = offset >> bitmap->chunkshift;
  1085. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1086. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  1087. sector_t csize;
  1088. int err;
  1089. err = bitmap_checkpage(bitmap, page, create);
  1090. if (bitmap->bp[page].hijacked ||
  1091. bitmap->bp[page].map == NULL)
  1092. csize = ((sector_t)1) << (bitmap->chunkshift +
  1093. PAGE_COUNTER_SHIFT - 1);
  1094. else
  1095. csize = ((sector_t)1) << bitmap->chunkshift;
  1096. *blocks = csize - (offset & (csize - 1));
  1097. if (err < 0)
  1098. return NULL;
  1099. /* now locked ... */
  1100. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  1101. /* should we use the first or second counter field
  1102. * of the hijacked pointer? */
  1103. int hi = (pageoff > PAGE_COUNTER_MASK);
  1104. return &((bitmap_counter_t *)
  1105. &bitmap->bp[page].map)[hi];
  1106. } else /* page is allocated */
  1107. return (bitmap_counter_t *)
  1108. &(bitmap->bp[page].map[pageoff]);
  1109. }
  1110. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
  1111. {
  1112. if (!bitmap)
  1113. return 0;
  1114. if (behind) {
  1115. int bw;
  1116. atomic_inc(&bitmap->behind_writes);
  1117. bw = atomic_read(&bitmap->behind_writes);
  1118. if (bw > bitmap->behind_writes_used)
  1119. bitmap->behind_writes_used = bw;
  1120. pr_debug("inc write-behind count %d/%lu\n",
  1121. bw, bitmap->mddev->bitmap_info.max_write_behind);
  1122. }
  1123. while (sectors) {
  1124. sector_t blocks;
  1125. bitmap_counter_t *bmc;
  1126. spin_lock_irq(&bitmap->counts.lock);
  1127. bmc = bitmap_get_counter(&bitmap->counts, offset, &blocks, 1);
  1128. if (!bmc) {
  1129. spin_unlock_irq(&bitmap->counts.lock);
  1130. return 0;
  1131. }
  1132. if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
  1133. DEFINE_WAIT(__wait);
  1134. /* note that it is safe to do the prepare_to_wait
  1135. * after the test as long as we do it before dropping
  1136. * the spinlock.
  1137. */
  1138. prepare_to_wait(&bitmap->overflow_wait, &__wait,
  1139. TASK_UNINTERRUPTIBLE);
  1140. spin_unlock_irq(&bitmap->counts.lock);
  1141. schedule();
  1142. finish_wait(&bitmap->overflow_wait, &__wait);
  1143. continue;
  1144. }
  1145. switch (*bmc) {
  1146. case 0:
  1147. bitmap_file_set_bit(bitmap, offset);
  1148. bitmap_count_page(&bitmap->counts, offset, 1);
  1149. /* fall through */
  1150. case 1:
  1151. *bmc = 2;
  1152. }
  1153. (*bmc)++;
  1154. spin_unlock_irq(&bitmap->counts.lock);
  1155. offset += blocks;
  1156. if (sectors > blocks)
  1157. sectors -= blocks;
  1158. else
  1159. sectors = 0;
  1160. }
  1161. return 0;
  1162. }
  1163. EXPORT_SYMBOL(bitmap_startwrite);
  1164. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1165. int success, int behind)
  1166. {
  1167. if (!bitmap)
  1168. return;
  1169. if (behind) {
  1170. if (atomic_dec_and_test(&bitmap->behind_writes))
  1171. wake_up(&bitmap->behind_wait);
  1172. pr_debug("dec write-behind count %d/%lu\n",
  1173. atomic_read(&bitmap->behind_writes),
  1174. bitmap->mddev->bitmap_info.max_write_behind);
  1175. }
  1176. while (sectors) {
  1177. sector_t blocks;
  1178. unsigned long flags;
  1179. bitmap_counter_t *bmc;
  1180. spin_lock_irqsave(&bitmap->counts.lock, flags);
  1181. bmc = bitmap_get_counter(&bitmap->counts, offset, &blocks, 0);
  1182. if (!bmc) {
  1183. spin_unlock_irqrestore(&bitmap->counts.lock, flags);
  1184. return;
  1185. }
  1186. if (success && !bitmap->mddev->degraded &&
  1187. bitmap->events_cleared < bitmap->mddev->events) {
  1188. bitmap->events_cleared = bitmap->mddev->events;
  1189. bitmap->need_sync = 1;
  1190. sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
  1191. }
  1192. if (!success && !NEEDED(*bmc))
  1193. *bmc |= NEEDED_MASK;
  1194. if (COUNTER(*bmc) == COUNTER_MAX)
  1195. wake_up(&bitmap->overflow_wait);
  1196. (*bmc)--;
  1197. if (*bmc <= 2) {
  1198. bitmap_set_pending(&bitmap->counts, offset);
  1199. bitmap->allclean = 0;
  1200. }
  1201. spin_unlock_irqrestore(&bitmap->counts.lock, flags);
  1202. offset += blocks;
  1203. if (sectors > blocks)
  1204. sectors -= blocks;
  1205. else
  1206. sectors = 0;
  1207. }
  1208. }
  1209. EXPORT_SYMBOL(bitmap_endwrite);
  1210. static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1211. int degraded)
  1212. {
  1213. bitmap_counter_t *bmc;
  1214. int rv;
  1215. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1216. *blocks = 1024;
  1217. return 1; /* always resync if no bitmap */
  1218. }
  1219. spin_lock_irq(&bitmap->counts.lock);
  1220. bmc = bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
  1221. rv = 0;
  1222. if (bmc) {
  1223. /* locked */
  1224. if (RESYNC(*bmc))
  1225. rv = 1;
  1226. else if (NEEDED(*bmc)) {
  1227. rv = 1;
  1228. if (!degraded) { /* don't set/clear bits if degraded */
  1229. *bmc |= RESYNC_MASK;
  1230. *bmc &= ~NEEDED_MASK;
  1231. }
  1232. }
  1233. }
  1234. spin_unlock_irq(&bitmap->counts.lock);
  1235. return rv;
  1236. }
  1237. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1238. int degraded)
  1239. {
  1240. /* bitmap_start_sync must always report on multiples of whole
  1241. * pages, otherwise resync (which is very PAGE_SIZE based) will
  1242. * get confused.
  1243. * So call __bitmap_start_sync repeatedly (if needed) until
  1244. * At least PAGE_SIZE>>9 blocks are covered.
  1245. * Return the 'or' of the result.
  1246. */
  1247. int rv = 0;
  1248. sector_t blocks1;
  1249. *blocks = 0;
  1250. while (*blocks < (PAGE_SIZE>>9)) {
  1251. rv |= __bitmap_start_sync(bitmap, offset,
  1252. &blocks1, degraded);
  1253. offset += blocks1;
  1254. *blocks += blocks1;
  1255. }
  1256. return rv;
  1257. }
  1258. EXPORT_SYMBOL(bitmap_start_sync);
  1259. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
  1260. {
  1261. bitmap_counter_t *bmc;
  1262. unsigned long flags;
  1263. if (bitmap == NULL) {
  1264. *blocks = 1024;
  1265. return;
  1266. }
  1267. spin_lock_irqsave(&bitmap->counts.lock, flags);
  1268. bmc = bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
  1269. if (bmc == NULL)
  1270. goto unlock;
  1271. /* locked */
  1272. if (RESYNC(*bmc)) {
  1273. *bmc &= ~RESYNC_MASK;
  1274. if (!NEEDED(*bmc) && aborted)
  1275. *bmc |= NEEDED_MASK;
  1276. else {
  1277. if (*bmc <= 2) {
  1278. bitmap_set_pending(&bitmap->counts, offset);
  1279. bitmap->allclean = 0;
  1280. }
  1281. }
  1282. }
  1283. unlock:
  1284. spin_unlock_irqrestore(&bitmap->counts.lock, flags);
  1285. }
  1286. EXPORT_SYMBOL(bitmap_end_sync);
  1287. void bitmap_close_sync(struct bitmap *bitmap)
  1288. {
  1289. /* Sync has finished, and any bitmap chunks that weren't synced
  1290. * properly have been aborted. It remains to us to clear the
  1291. * RESYNC bit wherever it is still on
  1292. */
  1293. sector_t sector = 0;
  1294. sector_t blocks;
  1295. if (!bitmap)
  1296. return;
  1297. while (sector < bitmap->mddev->resync_max_sectors) {
  1298. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1299. sector += blocks;
  1300. }
  1301. }
  1302. EXPORT_SYMBOL(bitmap_close_sync);
  1303. void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
  1304. {
  1305. sector_t s = 0;
  1306. sector_t blocks;
  1307. if (!bitmap)
  1308. return;
  1309. if (sector == 0) {
  1310. bitmap->last_end_sync = jiffies;
  1311. return;
  1312. }
  1313. if (time_before(jiffies, (bitmap->last_end_sync
  1314. + bitmap->mddev->bitmap_info.daemon_sleep)))
  1315. return;
  1316. wait_event(bitmap->mddev->recovery_wait,
  1317. atomic_read(&bitmap->mddev->recovery_active) == 0);
  1318. bitmap->mddev->curr_resync_completed = sector;
  1319. set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
  1320. sector &= ~((1ULL << bitmap->counts.chunkshift) - 1);
  1321. s = 0;
  1322. while (s < sector && s < bitmap->mddev->resync_max_sectors) {
  1323. bitmap_end_sync(bitmap, s, &blocks, 0);
  1324. s += blocks;
  1325. }
  1326. bitmap->last_end_sync = jiffies;
  1327. sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
  1328. }
  1329. EXPORT_SYMBOL(bitmap_cond_end_sync);
  1330. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1331. {
  1332. /* For each chunk covered by any of these sectors, set the
  1333. * counter to 2 and possibly set resync_needed. They should all
  1334. * be 0 at this point
  1335. */
  1336. sector_t secs;
  1337. bitmap_counter_t *bmc;
  1338. spin_lock_irq(&bitmap->counts.lock);
  1339. bmc = bitmap_get_counter(&bitmap->counts, offset, &secs, 1);
  1340. if (!bmc) {
  1341. spin_unlock_irq(&bitmap->counts.lock);
  1342. return;
  1343. }
  1344. if (!*bmc) {
  1345. *bmc = 2 | (needed ? NEEDED_MASK : 0);
  1346. bitmap_count_page(&bitmap->counts, offset, 1);
  1347. bitmap_set_pending(&bitmap->counts, offset);
  1348. bitmap->allclean = 0;
  1349. }
  1350. spin_unlock_irq(&bitmap->counts.lock);
  1351. }
  1352. /* dirty the memory and file bits for bitmap chunks "s" to "e" */
  1353. void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
  1354. {
  1355. unsigned long chunk;
  1356. for (chunk = s; chunk <= e; chunk++) {
  1357. sector_t sec = (sector_t)chunk << bitmap->counts.chunkshift;
  1358. bitmap_set_memory_bits(bitmap, sec, 1);
  1359. bitmap_file_set_bit(bitmap, sec);
  1360. if (sec < bitmap->mddev->recovery_cp)
  1361. /* We are asserting that the array is dirty,
  1362. * so move the recovery_cp address back so
  1363. * that it is obvious that it is dirty
  1364. */
  1365. bitmap->mddev->recovery_cp = sec;
  1366. }
  1367. }
  1368. /*
  1369. * flush out any pending updates
  1370. */
  1371. void bitmap_flush(struct mddev *mddev)
  1372. {
  1373. struct bitmap *bitmap = mddev->bitmap;
  1374. long sleep;
  1375. if (!bitmap) /* there was no bitmap */
  1376. return;
  1377. /* run the daemon_work three time to ensure everything is flushed
  1378. * that can be
  1379. */
  1380. sleep = mddev->bitmap_info.daemon_sleep * 2;
  1381. bitmap->daemon_lastrun -= sleep;
  1382. bitmap_daemon_work(mddev);
  1383. bitmap->daemon_lastrun -= sleep;
  1384. bitmap_daemon_work(mddev);
  1385. bitmap->daemon_lastrun -= sleep;
  1386. bitmap_daemon_work(mddev);
  1387. bitmap_update_sb(bitmap);
  1388. }
  1389. /*
  1390. * free memory that was allocated
  1391. */
  1392. static void bitmap_free(struct bitmap *bitmap)
  1393. {
  1394. unsigned long k, pages;
  1395. struct bitmap_page *bp;
  1396. if (!bitmap) /* there was no bitmap */
  1397. return;
  1398. /* Shouldn't be needed - but just in case.... */
  1399. wait_event(bitmap->write_wait,
  1400. atomic_read(&bitmap->pending_writes) == 0);
  1401. /* release the bitmap file */
  1402. bitmap_file_unmap(&bitmap->storage);
  1403. bp = bitmap->counts.bp;
  1404. pages = bitmap->counts.pages;
  1405. /* free all allocated memory */
  1406. if (bp) /* deallocate the page memory */
  1407. for (k = 0; k < pages; k++)
  1408. if (bp[k].map && !bp[k].hijacked)
  1409. kfree(bp[k].map);
  1410. kfree(bp);
  1411. kfree(bitmap);
  1412. }
  1413. void bitmap_destroy(struct mddev *mddev)
  1414. {
  1415. struct bitmap *bitmap = mddev->bitmap;
  1416. if (!bitmap) /* there was no bitmap */
  1417. return;
  1418. mutex_lock(&mddev->bitmap_info.mutex);
  1419. mddev->bitmap = NULL; /* disconnect from the md device */
  1420. mutex_unlock(&mddev->bitmap_info.mutex);
  1421. if (mddev->thread)
  1422. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1423. if (bitmap->sysfs_can_clear)
  1424. sysfs_put(bitmap->sysfs_can_clear);
  1425. bitmap_free(bitmap);
  1426. }
  1427. /*
  1428. * initialize the bitmap structure
  1429. * if this returns an error, bitmap_destroy must be called to do clean up
  1430. */
  1431. int bitmap_create(struct mddev *mddev)
  1432. {
  1433. struct bitmap *bitmap;
  1434. sector_t blocks = mddev->resync_max_sectors;
  1435. struct file *file = mddev->bitmap_info.file;
  1436. int err;
  1437. struct kernfs_node *bm = NULL;
  1438. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1439. BUG_ON(file && mddev->bitmap_info.offset);
  1440. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1441. if (!bitmap)
  1442. return -ENOMEM;
  1443. spin_lock_init(&bitmap->counts.lock);
  1444. atomic_set(&bitmap->pending_writes, 0);
  1445. init_waitqueue_head(&bitmap->write_wait);
  1446. init_waitqueue_head(&bitmap->overflow_wait);
  1447. init_waitqueue_head(&bitmap->behind_wait);
  1448. bitmap->mddev = mddev;
  1449. if (mddev->kobj.sd)
  1450. bm = sysfs_get_dirent(mddev->kobj.sd, "bitmap");
  1451. if (bm) {
  1452. bitmap->sysfs_can_clear = sysfs_get_dirent(bm, "can_clear");
  1453. sysfs_put(bm);
  1454. } else
  1455. bitmap->sysfs_can_clear = NULL;
  1456. bitmap->storage.file = file;
  1457. if (file) {
  1458. get_file(file);
  1459. /* As future accesses to this file will use bmap,
  1460. * and bypass the page cache, we must sync the file
  1461. * first.
  1462. */
  1463. vfs_fsync(file, 1);
  1464. }
  1465. /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
  1466. if (!mddev->bitmap_info.external) {
  1467. /*
  1468. * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
  1469. * instructing us to create a new on-disk bitmap instance.
  1470. */
  1471. if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
  1472. err = bitmap_new_disk_sb(bitmap);
  1473. else
  1474. err = bitmap_read_sb(bitmap);
  1475. } else {
  1476. err = 0;
  1477. if (mddev->bitmap_info.chunksize == 0 ||
  1478. mddev->bitmap_info.daemon_sleep == 0)
  1479. /* chunksize and time_base need to be
  1480. * set first. */
  1481. err = -EINVAL;
  1482. }
  1483. if (err)
  1484. goto error;
  1485. bitmap->daemon_lastrun = jiffies;
  1486. err = bitmap_resize(bitmap, blocks, mddev->bitmap_info.chunksize, 1);
  1487. if (err)
  1488. goto error;
  1489. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1490. bitmap->counts.pages, bmname(bitmap));
  1491. mddev->bitmap = bitmap;
  1492. return test_bit(BITMAP_WRITE_ERROR, &bitmap->flags) ? -EIO : 0;
  1493. error:
  1494. bitmap_free(bitmap);
  1495. return err;
  1496. }
  1497. int bitmap_load(struct mddev *mddev)
  1498. {
  1499. int err = 0;
  1500. sector_t start = 0;
  1501. sector_t sector = 0;
  1502. struct bitmap *bitmap = mddev->bitmap;
  1503. if (!bitmap)
  1504. goto out;
  1505. /* Clear out old bitmap info first: Either there is none, or we
  1506. * are resuming after someone else has possibly changed things,
  1507. * so we should forget old cached info.
  1508. * All chunks should be clean, but some might need_sync.
  1509. */
  1510. while (sector < mddev->resync_max_sectors) {
  1511. sector_t blocks;
  1512. bitmap_start_sync(bitmap, sector, &blocks, 0);
  1513. sector += blocks;
  1514. }
  1515. bitmap_close_sync(bitmap);
  1516. if (mddev->degraded == 0
  1517. || bitmap->events_cleared == mddev->events)
  1518. /* no need to keep dirty bits to optimise a
  1519. * re-add of a missing device */
  1520. start = mddev->recovery_cp;
  1521. mutex_lock(&mddev->bitmap_info.mutex);
  1522. err = bitmap_init_from_disk(bitmap, start);
  1523. mutex_unlock(&mddev->bitmap_info.mutex);
  1524. if (err)
  1525. goto out;
  1526. clear_bit(BITMAP_STALE, &bitmap->flags);
  1527. /* Kick recovery in case any bits were set */
  1528. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  1529. mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
  1530. md_wakeup_thread(mddev->thread);
  1531. bitmap_update_sb(bitmap);
  1532. if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
  1533. err = -EIO;
  1534. out:
  1535. return err;
  1536. }
  1537. EXPORT_SYMBOL_GPL(bitmap_load);
  1538. void bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
  1539. {
  1540. unsigned long chunk_kb;
  1541. struct bitmap_counts *counts;
  1542. if (!bitmap)
  1543. return;
  1544. counts = &bitmap->counts;
  1545. chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
  1546. seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
  1547. "%lu%s chunk",
  1548. counts->pages - counts->missing_pages,
  1549. counts->pages,
  1550. (counts->pages - counts->missing_pages)
  1551. << (PAGE_SHIFT - 10),
  1552. chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
  1553. chunk_kb ? "KB" : "B");
  1554. if (bitmap->storage.file) {
  1555. seq_printf(seq, ", file: ");
  1556. seq_path(seq, &bitmap->storage.file->f_path, " \t\n");
  1557. }
  1558. seq_printf(seq, "\n");
  1559. }
  1560. int bitmap_resize(struct bitmap *bitmap, sector_t blocks,
  1561. int chunksize, int init)
  1562. {
  1563. /* If chunk_size is 0, choose an appropriate chunk size.
  1564. * Then possibly allocate new storage space.
  1565. * Then quiesce, copy bits, replace bitmap, and re-start
  1566. *
  1567. * This function is called both to set up the initial bitmap
  1568. * and to resize the bitmap while the array is active.
  1569. * If this happens as a result of the array being resized,
  1570. * chunksize will be zero, and we need to choose a suitable
  1571. * chunksize, otherwise we use what we are given.
  1572. */
  1573. struct bitmap_storage store;
  1574. struct bitmap_counts old_counts;
  1575. unsigned long chunks;
  1576. sector_t block;
  1577. sector_t old_blocks, new_blocks;
  1578. int chunkshift;
  1579. int ret = 0;
  1580. long pages;
  1581. struct bitmap_page *new_bp;
  1582. if (chunksize == 0) {
  1583. /* If there is enough space, leave the chunk size unchanged,
  1584. * else increase by factor of two until there is enough space.
  1585. */
  1586. long bytes;
  1587. long space = bitmap->mddev->bitmap_info.space;
  1588. if (space == 0) {
  1589. /* We don't know how much space there is, so limit
  1590. * to current size - in sectors.
  1591. */
  1592. bytes = DIV_ROUND_UP(bitmap->counts.chunks, 8);
  1593. if (!bitmap->mddev->bitmap_info.external)
  1594. bytes += sizeof(bitmap_super_t);
  1595. space = DIV_ROUND_UP(bytes, 512);
  1596. bitmap->mddev->bitmap_info.space = space;
  1597. }
  1598. chunkshift = bitmap->counts.chunkshift;
  1599. chunkshift--;
  1600. do {
  1601. /* 'chunkshift' is shift from block size to chunk size */
  1602. chunkshift++;
  1603. chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
  1604. bytes = DIV_ROUND_UP(chunks, 8);
  1605. if (!bitmap->mddev->bitmap_info.external)
  1606. bytes += sizeof(bitmap_super_t);
  1607. } while (bytes > (space << 9));
  1608. } else
  1609. chunkshift = ffz(~chunksize) - BITMAP_BLOCK_SHIFT;
  1610. chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
  1611. memset(&store, 0, sizeof(store));
  1612. if (bitmap->mddev->bitmap_info.offset || bitmap->mddev->bitmap_info.file)
  1613. ret = bitmap_storage_alloc(&store, chunks,
  1614. !bitmap->mddev->bitmap_info.external);
  1615. if (ret)
  1616. goto err;
  1617. pages = DIV_ROUND_UP(chunks, PAGE_COUNTER_RATIO);
  1618. new_bp = kzalloc(pages * sizeof(*new_bp), GFP_KERNEL);
  1619. ret = -ENOMEM;
  1620. if (!new_bp) {
  1621. bitmap_file_unmap(&store);
  1622. goto err;
  1623. }
  1624. if (!init)
  1625. bitmap->mddev->pers->quiesce(bitmap->mddev, 1);
  1626. store.file = bitmap->storage.file;
  1627. bitmap->storage.file = NULL;
  1628. if (store.sb_page && bitmap->storage.sb_page)
  1629. memcpy(page_address(store.sb_page),
  1630. page_address(bitmap->storage.sb_page),
  1631. sizeof(bitmap_super_t));
  1632. bitmap_file_unmap(&bitmap->storage);
  1633. bitmap->storage = store;
  1634. old_counts = bitmap->counts;
  1635. bitmap->counts.bp = new_bp;
  1636. bitmap->counts.pages = pages;
  1637. bitmap->counts.missing_pages = pages;
  1638. bitmap->counts.chunkshift = chunkshift;
  1639. bitmap->counts.chunks = chunks;
  1640. bitmap->mddev->bitmap_info.chunksize = 1 << (chunkshift +
  1641. BITMAP_BLOCK_SHIFT);
  1642. blocks = min(old_counts.chunks << old_counts.chunkshift,
  1643. chunks << chunkshift);
  1644. spin_lock_irq(&bitmap->counts.lock);
  1645. for (block = 0; block < blocks; ) {
  1646. bitmap_counter_t *bmc_old, *bmc_new;
  1647. int set;
  1648. bmc_old = bitmap_get_counter(&old_counts, block,
  1649. &old_blocks, 0);
  1650. set = bmc_old && NEEDED(*bmc_old);
  1651. if (set) {
  1652. bmc_new = bitmap_get_counter(&bitmap->counts, block,
  1653. &new_blocks, 1);
  1654. if (*bmc_new == 0) {
  1655. /* need to set on-disk bits too. */
  1656. sector_t end = block + new_blocks;
  1657. sector_t start = block >> chunkshift;
  1658. start <<= chunkshift;
  1659. while (start < end) {
  1660. bitmap_file_set_bit(bitmap, block);
  1661. start += 1 << chunkshift;
  1662. }
  1663. *bmc_new = 2;
  1664. bitmap_count_page(&bitmap->counts,
  1665. block, 1);
  1666. bitmap_set_pending(&bitmap->counts,
  1667. block);
  1668. }
  1669. *bmc_new |= NEEDED_MASK;
  1670. if (new_blocks < old_blocks)
  1671. old_blocks = new_blocks;
  1672. }
  1673. block += old_blocks;
  1674. }
  1675. if (!init) {
  1676. int i;
  1677. while (block < (chunks << chunkshift)) {
  1678. bitmap_counter_t *bmc;
  1679. bmc = bitmap_get_counter(&bitmap->counts, block,
  1680. &new_blocks, 1);
  1681. if (bmc) {
  1682. /* new space. It needs to be resynced, so
  1683. * we set NEEDED_MASK.
  1684. */
  1685. if (*bmc == 0) {
  1686. *bmc = NEEDED_MASK | 2;
  1687. bitmap_count_page(&bitmap->counts,
  1688. block, 1);
  1689. bitmap_set_pending(&bitmap->counts,
  1690. block);
  1691. }
  1692. }
  1693. block += new_blocks;
  1694. }
  1695. for (i = 0; i < bitmap->storage.file_pages; i++)
  1696. set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
  1697. }
  1698. spin_unlock_irq(&bitmap->counts.lock);
  1699. if (!init) {
  1700. bitmap_unplug(bitmap);
  1701. bitmap->mddev->pers->quiesce(bitmap->mddev, 0);
  1702. }
  1703. ret = 0;
  1704. err:
  1705. return ret;
  1706. }
  1707. EXPORT_SYMBOL_GPL(bitmap_resize);
  1708. static ssize_t
  1709. location_show(struct mddev *mddev, char *page)
  1710. {
  1711. ssize_t len;
  1712. if (mddev->bitmap_info.file)
  1713. len = sprintf(page, "file");
  1714. else if (mddev->bitmap_info.offset)
  1715. len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
  1716. else
  1717. len = sprintf(page, "none");
  1718. len += sprintf(page+len, "\n");
  1719. return len;
  1720. }
  1721. static ssize_t
  1722. location_store(struct mddev *mddev, const char *buf, size_t len)
  1723. {
  1724. if (mddev->pers) {
  1725. if (!mddev->pers->quiesce)
  1726. return -EBUSY;
  1727. if (mddev->recovery || mddev->sync_thread)
  1728. return -EBUSY;
  1729. }
  1730. if (mddev->bitmap || mddev->bitmap_info.file ||
  1731. mddev->bitmap_info.offset) {
  1732. /* bitmap already configured. Only option is to clear it */
  1733. if (strncmp(buf, "none", 4) != 0)
  1734. return -EBUSY;
  1735. if (mddev->pers) {
  1736. mddev->pers->quiesce(mddev, 1);
  1737. bitmap_destroy(mddev);
  1738. mddev->pers->quiesce(mddev, 0);
  1739. }
  1740. mddev->bitmap_info.offset = 0;
  1741. if (mddev->bitmap_info.file) {
  1742. struct file *f = mddev->bitmap_info.file;
  1743. mddev->bitmap_info.file = NULL;
  1744. fput(f);
  1745. }
  1746. } else {
  1747. /* No bitmap, OK to set a location */
  1748. long long offset;
  1749. if (strncmp(buf, "none", 4) == 0)
  1750. /* nothing to be done */;
  1751. else if (strncmp(buf, "file:", 5) == 0) {
  1752. /* Not supported yet */
  1753. return -EINVAL;
  1754. } else {
  1755. int rv;
  1756. if (buf[0] == '+')
  1757. rv = kstrtoll(buf+1, 10, &offset);
  1758. else
  1759. rv = kstrtoll(buf, 10, &offset);
  1760. if (rv)
  1761. return rv;
  1762. if (offset == 0)
  1763. return -EINVAL;
  1764. if (mddev->bitmap_info.external == 0 &&
  1765. mddev->major_version == 0 &&
  1766. offset != mddev->bitmap_info.default_offset)
  1767. return -EINVAL;
  1768. mddev->bitmap_info.offset = offset;
  1769. if (mddev->pers) {
  1770. mddev->pers->quiesce(mddev, 1);
  1771. rv = bitmap_create(mddev);
  1772. if (!rv)
  1773. rv = bitmap_load(mddev);
  1774. if (rv) {
  1775. bitmap_destroy(mddev);
  1776. mddev->bitmap_info.offset = 0;
  1777. }
  1778. mddev->pers->quiesce(mddev, 0);
  1779. if (rv)
  1780. return rv;
  1781. }
  1782. }
  1783. }
  1784. if (!mddev->external) {
  1785. /* Ensure new bitmap info is stored in
  1786. * metadata promptly.
  1787. */
  1788. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1789. md_wakeup_thread(mddev->thread);
  1790. }
  1791. return len;
  1792. }
  1793. static struct md_sysfs_entry bitmap_location =
  1794. __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
  1795. /* 'bitmap/space' is the space available at 'location' for the
  1796. * bitmap. This allows the kernel to know when it is safe to
  1797. * resize the bitmap to match a resized array.
  1798. */
  1799. static ssize_t
  1800. space_show(struct mddev *mddev, char *page)
  1801. {
  1802. return sprintf(page, "%lu\n", mddev->bitmap_info.space);
  1803. }
  1804. static ssize_t
  1805. space_store(struct mddev *mddev, const char *buf, size_t len)
  1806. {
  1807. unsigned long sectors;
  1808. int rv;
  1809. rv = kstrtoul(buf, 10, &sectors);
  1810. if (rv)
  1811. return rv;
  1812. if (sectors == 0)
  1813. return -EINVAL;
  1814. if (mddev->bitmap &&
  1815. sectors < (mddev->bitmap->storage.bytes + 511) >> 9)
  1816. return -EFBIG; /* Bitmap is too big for this small space */
  1817. /* could make sure it isn't too big, but that isn't really
  1818. * needed - user-space should be careful.
  1819. */
  1820. mddev->bitmap_info.space = sectors;
  1821. return len;
  1822. }
  1823. static struct md_sysfs_entry bitmap_space =
  1824. __ATTR(space, S_IRUGO|S_IWUSR, space_show, space_store);
  1825. static ssize_t
  1826. timeout_show(struct mddev *mddev, char *page)
  1827. {
  1828. ssize_t len;
  1829. unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
  1830. unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
  1831. len = sprintf(page, "%lu", secs);
  1832. if (jifs)
  1833. len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
  1834. len += sprintf(page+len, "\n");
  1835. return len;
  1836. }
  1837. static ssize_t
  1838. timeout_store(struct mddev *mddev, const char *buf, size_t len)
  1839. {
  1840. /* timeout can be set at any time */
  1841. unsigned long timeout;
  1842. int rv = strict_strtoul_scaled(buf, &timeout, 4);
  1843. if (rv)
  1844. return rv;
  1845. /* just to make sure we don't overflow... */
  1846. if (timeout >= LONG_MAX / HZ)
  1847. return -EINVAL;
  1848. timeout = timeout * HZ / 10000;
  1849. if (timeout >= MAX_SCHEDULE_TIMEOUT)
  1850. timeout = MAX_SCHEDULE_TIMEOUT-1;
  1851. if (timeout < 1)
  1852. timeout = 1;
  1853. mddev->bitmap_info.daemon_sleep = timeout;
  1854. if (mddev->thread) {
  1855. /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
  1856. * the bitmap is all clean and we don't need to
  1857. * adjust the timeout right now
  1858. */
  1859. if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
  1860. mddev->thread->timeout = timeout;
  1861. md_wakeup_thread(mddev->thread);
  1862. }
  1863. }
  1864. return len;
  1865. }
  1866. static struct md_sysfs_entry bitmap_timeout =
  1867. __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
  1868. static ssize_t
  1869. backlog_show(struct mddev *mddev, char *page)
  1870. {
  1871. return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
  1872. }
  1873. static ssize_t
  1874. backlog_store(struct mddev *mddev, const char *buf, size_t len)
  1875. {
  1876. unsigned long backlog;
  1877. int rv = kstrtoul(buf, 10, &backlog);
  1878. if (rv)
  1879. return rv;
  1880. if (backlog > COUNTER_MAX)
  1881. return -EINVAL;
  1882. mddev->bitmap_info.max_write_behind = backlog;
  1883. return len;
  1884. }
  1885. static struct md_sysfs_entry bitmap_backlog =
  1886. __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
  1887. static ssize_t
  1888. chunksize_show(struct mddev *mddev, char *page)
  1889. {
  1890. return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
  1891. }
  1892. static ssize_t
  1893. chunksize_store(struct mddev *mddev, const char *buf, size_t len)
  1894. {
  1895. /* Can only be changed when no bitmap is active */
  1896. int rv;
  1897. unsigned long csize;
  1898. if (mddev->bitmap)
  1899. return -EBUSY;
  1900. rv = kstrtoul(buf, 10, &csize);
  1901. if (rv)
  1902. return rv;
  1903. if (csize < 512 ||
  1904. !is_power_of_2(csize))
  1905. return -EINVAL;
  1906. mddev->bitmap_info.chunksize = csize;
  1907. return len;
  1908. }
  1909. static struct md_sysfs_entry bitmap_chunksize =
  1910. __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
  1911. static ssize_t metadata_show(struct mddev *mddev, char *page)
  1912. {
  1913. return sprintf(page, "%s\n", (mddev->bitmap_info.external
  1914. ? "external" : "internal"));
  1915. }
  1916. static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
  1917. {
  1918. if (mddev->bitmap ||
  1919. mddev->bitmap_info.file ||
  1920. mddev->bitmap_info.offset)
  1921. return -EBUSY;
  1922. if (strncmp(buf, "external", 8) == 0)
  1923. mddev->bitmap_info.external = 1;
  1924. else if (strncmp(buf, "internal", 8) == 0)
  1925. mddev->bitmap_info.external = 0;
  1926. else
  1927. return -EINVAL;
  1928. return len;
  1929. }
  1930. static struct md_sysfs_entry bitmap_metadata =
  1931. __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
  1932. static ssize_t can_clear_show(struct mddev *mddev, char *page)
  1933. {
  1934. int len;
  1935. if (mddev->bitmap)
  1936. len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
  1937. "false" : "true"));
  1938. else
  1939. len = sprintf(page, "\n");
  1940. return len;
  1941. }
  1942. static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
  1943. {
  1944. if (mddev->bitmap == NULL)
  1945. return -ENOENT;
  1946. if (strncmp(buf, "false", 5) == 0)
  1947. mddev->bitmap->need_sync = 1;
  1948. else if (strncmp(buf, "true", 4) == 0) {
  1949. if (mddev->degraded)
  1950. return -EBUSY;
  1951. mddev->bitmap->need_sync = 0;
  1952. } else
  1953. return -EINVAL;
  1954. return len;
  1955. }
  1956. static struct md_sysfs_entry bitmap_can_clear =
  1957. __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
  1958. static ssize_t
  1959. behind_writes_used_show(struct mddev *mddev, char *page)
  1960. {
  1961. if (mddev->bitmap == NULL)
  1962. return sprintf(page, "0\n");
  1963. return sprintf(page, "%lu\n",
  1964. mddev->bitmap->behind_writes_used);
  1965. }
  1966. static ssize_t
  1967. behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
  1968. {
  1969. if (mddev->bitmap)
  1970. mddev->bitmap->behind_writes_used = 0;
  1971. return len;
  1972. }
  1973. static struct md_sysfs_entry max_backlog_used =
  1974. __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
  1975. behind_writes_used_show, behind_writes_used_reset);
  1976. static struct attribute *md_bitmap_attrs[] = {
  1977. &bitmap_location.attr,
  1978. &bitmap_space.attr,
  1979. &bitmap_timeout.attr,
  1980. &bitmap_backlog.attr,
  1981. &bitmap_chunksize.attr,
  1982. &bitmap_metadata.attr,
  1983. &bitmap_can_clear.attr,
  1984. &max_backlog_used.attr,
  1985. NULL
  1986. };
  1987. struct attribute_group md_bitmap_group = {
  1988. .name = "bitmap",
  1989. .attrs = md_bitmap_attrs,
  1990. };