bitmap.c 67 KB

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