md-bitmap.c 70 KB

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