bitmap.c 56 KB

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