bitmap.c 68 KB

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