bitmap.c 59 KB

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