bitmap.c 67 KB

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