md-bitmap.c 69 KB

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