bitmap.c 68 KB

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