bitmap.c 64 KB

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