bitmap.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030
  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 "md.h"
  28. #include "bitmap.h"
  29. /* debug macros */
  30. #define DEBUG 0
  31. #if DEBUG
  32. /* these are for debugging purposes only! */
  33. /* define one and only one of these */
  34. #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
  35. #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
  36. #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
  37. #define INJECT_FAULTS_4 0 /* undef */
  38. #define INJECT_FAULTS_5 0 /* undef */
  39. #define INJECT_FAULTS_6 0
  40. /* if these are defined, the driver will fail! debug only */
  41. #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
  42. #define INJECT_FATAL_FAULT_2 0 /* undef */
  43. #define INJECT_FATAL_FAULT_3 0 /* undef */
  44. #endif
  45. #ifndef PRINTK
  46. # if DEBUG > 0
  47. # define PRINTK(x...) printk(KERN_DEBUG x)
  48. # else
  49. # define PRINTK(x...)
  50. # endif
  51. #endif
  52. static inline char *bmname(struct bitmap *bitmap)
  53. {
  54. return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
  55. }
  56. /*
  57. * just a placeholder - calls kmalloc for bitmap pages
  58. */
  59. static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
  60. {
  61. unsigned char *page;
  62. #ifdef INJECT_FAULTS_1
  63. page = NULL;
  64. #else
  65. page = kzalloc(PAGE_SIZE, GFP_NOIO);
  66. #endif
  67. if (!page)
  68. printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
  69. else
  70. PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
  71. bmname(bitmap), page);
  72. return page;
  73. }
  74. /*
  75. * for now just a placeholder -- just calls kfree for bitmap pages
  76. */
  77. static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
  78. {
  79. PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
  80. kfree(page);
  81. }
  82. /*
  83. * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
  84. *
  85. * 1) check to see if this page is allocated, if it's not then try to alloc
  86. * 2) if the alloc fails, set the page's hijacked flag so we'll use the
  87. * page pointer directly as a counter
  88. *
  89. * if we find our page, we increment the page's refcount so that it stays
  90. * allocated while we're using it
  91. */
  92. static int bitmap_checkpage(struct bitmap *bitmap,
  93. unsigned long page, int create)
  94. __releases(bitmap->lock)
  95. __acquires(bitmap->lock)
  96. {
  97. unsigned char *mappage;
  98. if (page >= bitmap->pages) {
  99. /* This can happen if bitmap_start_sync goes beyond
  100. * End-of-device while looking for a whole page.
  101. * It is harmless.
  102. */
  103. return -EINVAL;
  104. }
  105. if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
  106. return 0;
  107. if (bitmap->bp[page].map) /* page is already allocated, just return */
  108. return 0;
  109. if (!create)
  110. return -ENOENT;
  111. /* this page has not been allocated yet */
  112. spin_unlock_irq(&bitmap->lock);
  113. mappage = bitmap_alloc_page(bitmap);
  114. spin_lock_irq(&bitmap->lock);
  115. if (mappage == NULL) {
  116. PRINTK("%s: bitmap map page allocation failed, hijacking\n",
  117. bmname(bitmap));
  118. /* failed - set the hijacked flag so that we can use the
  119. * pointer as a counter */
  120. if (!bitmap->bp[page].map)
  121. bitmap->bp[page].hijacked = 1;
  122. } else if (bitmap->bp[page].map ||
  123. bitmap->bp[page].hijacked) {
  124. /* somebody beat us to getting the page */
  125. bitmap_free_page(bitmap, mappage);
  126. return 0;
  127. } else {
  128. /* no page was in place and we have one, so install it */
  129. bitmap->bp[page].map = mappage;
  130. bitmap->missing_pages--;
  131. }
  132. return 0;
  133. }
  134. /* if page is completely empty, put it back on the free list, or dealloc it */
  135. /* if page was hijacked, unmark the flag so it might get alloced next time */
  136. /* Note: lock should be held when calling this */
  137. static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
  138. {
  139. char *ptr;
  140. if (bitmap->bp[page].count) /* page is still busy */
  141. return;
  142. /* page is no longer in use, it can be released */
  143. if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
  144. bitmap->bp[page].hijacked = 0;
  145. bitmap->bp[page].map = NULL;
  146. } else {
  147. /* normal case, free the page */
  148. ptr = bitmap->bp[page].map;
  149. bitmap->bp[page].map = NULL;
  150. bitmap->missing_pages++;
  151. bitmap_free_page(bitmap, ptr);
  152. }
  153. }
  154. /*
  155. * bitmap file handling - read and write the bitmap file and its superblock
  156. */
  157. /*
  158. * basic page I/O operations
  159. */
  160. /* IO operations when bitmap is stored near all superblocks */
  161. static struct page *read_sb_page(mddev_t *mddev, loff_t offset,
  162. struct page *page,
  163. unsigned long index, int size)
  164. {
  165. /* choose a good rdev and read the page from there */
  166. mdk_rdev_t *rdev;
  167. sector_t target;
  168. int did_alloc = 0;
  169. if (!page) {
  170. page = alloc_page(GFP_KERNEL);
  171. if (!page)
  172. return ERR_PTR(-ENOMEM);
  173. did_alloc = 1;
  174. }
  175. list_for_each_entry(rdev, &mddev->disks, same_set) {
  176. if (! test_bit(In_sync, &rdev->flags)
  177. || test_bit(Faulty, &rdev->flags))
  178. continue;
  179. target = rdev->sb_start + offset + index * (PAGE_SIZE/512);
  180. if (sync_page_io(rdev->bdev, target,
  181. roundup(size, bdev_logical_block_size(rdev->bdev)),
  182. page, READ)) {
  183. page->index = index;
  184. attach_page_buffers(page, NULL); /* so that free_buffer will
  185. * quietly no-op */
  186. return page;
  187. }
  188. }
  189. if (did_alloc)
  190. put_page(page);
  191. return ERR_PTR(-EIO);
  192. }
  193. static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
  194. {
  195. /* Iterate the disks of an mddev, using rcu to protect access to the
  196. * linked list, and raising the refcount of devices we return to ensure
  197. * they don't disappear while in use.
  198. * As devices are only added or removed when raid_disk is < 0 and
  199. * nr_pending is 0 and In_sync is clear, the entries we return will
  200. * still be in the same position on the list when we re-enter
  201. * list_for_each_continue_rcu.
  202. */
  203. struct list_head *pos;
  204. rcu_read_lock();
  205. if (rdev == NULL)
  206. /* start at the beginning */
  207. pos = &mddev->disks;
  208. else {
  209. /* release the previous rdev and start from there. */
  210. rdev_dec_pending(rdev, mddev);
  211. pos = &rdev->same_set;
  212. }
  213. list_for_each_continue_rcu(pos, &mddev->disks) {
  214. rdev = list_entry(pos, mdk_rdev_t, same_set);
  215. if (rdev->raid_disk >= 0 &&
  216. !test_bit(Faulty, &rdev->flags)) {
  217. /* this is a usable devices */
  218. atomic_inc(&rdev->nr_pending);
  219. rcu_read_unlock();
  220. return rdev;
  221. }
  222. }
  223. rcu_read_unlock();
  224. return NULL;
  225. }
  226. static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
  227. {
  228. mdk_rdev_t *rdev = NULL;
  229. mddev_t *mddev = bitmap->mddev;
  230. while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
  231. int size = PAGE_SIZE;
  232. loff_t offset = mddev->bitmap_info.offset;
  233. if (page->index == bitmap->file_pages-1)
  234. size = roundup(bitmap->last_page_size,
  235. bdev_logical_block_size(rdev->bdev));
  236. /* Just make sure we aren't corrupting data or
  237. * metadata
  238. */
  239. if (mddev->external) {
  240. /* Bitmap could be anywhere. */
  241. if (rdev->sb_start + offset + (page->index
  242. * (PAGE_SIZE/512))
  243. > rdev->data_offset
  244. &&
  245. rdev->sb_start + offset
  246. < (rdev->data_offset + mddev->dev_sectors
  247. + (PAGE_SIZE/512)))
  248. goto bad_alignment;
  249. } else if (offset < 0) {
  250. /* DATA BITMAP METADATA */
  251. if (offset
  252. + (long)(page->index * (PAGE_SIZE/512))
  253. + size/512 > 0)
  254. /* bitmap runs in to metadata */
  255. goto bad_alignment;
  256. if (rdev->data_offset + mddev->dev_sectors
  257. > rdev->sb_start + offset)
  258. /* data runs in to bitmap */
  259. goto bad_alignment;
  260. } else if (rdev->sb_start < rdev->data_offset) {
  261. /* METADATA BITMAP DATA */
  262. if (rdev->sb_start
  263. + offset
  264. + page->index*(PAGE_SIZE/512) + size/512
  265. > rdev->data_offset)
  266. /* bitmap runs in to data */
  267. goto bad_alignment;
  268. } else {
  269. /* DATA METADATA BITMAP - no problems */
  270. }
  271. md_super_write(mddev, rdev,
  272. rdev->sb_start + offset
  273. + page->index * (PAGE_SIZE/512),
  274. size,
  275. page);
  276. }
  277. if (wait)
  278. md_super_wait(mddev);
  279. return 0;
  280. bad_alignment:
  281. return -EINVAL;
  282. }
  283. static void bitmap_file_kick(struct bitmap *bitmap);
  284. /*
  285. * write out a page to a file
  286. */
  287. static void write_page(struct bitmap *bitmap, struct page *page, int wait)
  288. {
  289. struct buffer_head *bh;
  290. if (bitmap->file == NULL) {
  291. switch (write_sb_page(bitmap, page, wait)) {
  292. case -EINVAL:
  293. bitmap->flags |= BITMAP_WRITE_ERROR;
  294. }
  295. } else {
  296. bh = page_buffers(page);
  297. while (bh && bh->b_blocknr) {
  298. atomic_inc(&bitmap->pending_writes);
  299. set_buffer_locked(bh);
  300. set_buffer_mapped(bh);
  301. submit_bh(WRITE, bh);
  302. bh = bh->b_this_page;
  303. }
  304. if (wait)
  305. wait_event(bitmap->write_wait,
  306. atomic_read(&bitmap->pending_writes)==0);
  307. }
  308. if (bitmap->flags & BITMAP_WRITE_ERROR)
  309. bitmap_file_kick(bitmap);
  310. }
  311. static void end_bitmap_write(struct buffer_head *bh, int uptodate)
  312. {
  313. struct bitmap *bitmap = bh->b_private;
  314. unsigned long flags;
  315. if (!uptodate) {
  316. spin_lock_irqsave(&bitmap->lock, flags);
  317. bitmap->flags |= BITMAP_WRITE_ERROR;
  318. spin_unlock_irqrestore(&bitmap->lock, flags);
  319. }
  320. if (atomic_dec_and_test(&bitmap->pending_writes))
  321. wake_up(&bitmap->write_wait);
  322. }
  323. /* copied from buffer.c */
  324. static void
  325. __clear_page_buffers(struct page *page)
  326. {
  327. ClearPagePrivate(page);
  328. set_page_private(page, 0);
  329. page_cache_release(page);
  330. }
  331. static void free_buffers(struct page *page)
  332. {
  333. struct buffer_head *bh = page_buffers(page);
  334. while (bh) {
  335. struct buffer_head *next = bh->b_this_page;
  336. free_buffer_head(bh);
  337. bh = next;
  338. }
  339. __clear_page_buffers(page);
  340. put_page(page);
  341. }
  342. /* read a page from a file.
  343. * We both read the page, and attach buffers to the page to record the
  344. * address of each block (using bmap). These addresses will be used
  345. * to write the block later, completely bypassing the filesystem.
  346. * This usage is similar to how swap files are handled, and allows us
  347. * to write to a file with no concerns of memory allocation failing.
  348. */
  349. static struct page *read_page(struct file *file, unsigned long index,
  350. struct bitmap *bitmap,
  351. unsigned long count)
  352. {
  353. struct page *page = NULL;
  354. struct inode *inode = file->f_path.dentry->d_inode;
  355. struct buffer_head *bh;
  356. sector_t block;
  357. PRINTK("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
  358. (unsigned long long)index << PAGE_SHIFT);
  359. page = alloc_page(GFP_KERNEL);
  360. if (!page)
  361. page = ERR_PTR(-ENOMEM);
  362. if (IS_ERR(page))
  363. goto out;
  364. bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
  365. if (!bh) {
  366. put_page(page);
  367. page = ERR_PTR(-ENOMEM);
  368. goto out;
  369. }
  370. attach_page_buffers(page, bh);
  371. block = index << (PAGE_SHIFT - inode->i_blkbits);
  372. while (bh) {
  373. if (count == 0)
  374. bh->b_blocknr = 0;
  375. else {
  376. bh->b_blocknr = bmap(inode, block);
  377. if (bh->b_blocknr == 0) {
  378. /* Cannot use this file! */
  379. free_buffers(page);
  380. page = ERR_PTR(-EINVAL);
  381. goto out;
  382. }
  383. bh->b_bdev = inode->i_sb->s_bdev;
  384. if (count < (1<<inode->i_blkbits))
  385. count = 0;
  386. else
  387. count -= (1<<inode->i_blkbits);
  388. bh->b_end_io = end_bitmap_write;
  389. bh->b_private = bitmap;
  390. atomic_inc(&bitmap->pending_writes);
  391. set_buffer_locked(bh);
  392. set_buffer_mapped(bh);
  393. submit_bh(READ, bh);
  394. }
  395. block++;
  396. bh = bh->b_this_page;
  397. }
  398. page->index = index;
  399. wait_event(bitmap->write_wait,
  400. atomic_read(&bitmap->pending_writes)==0);
  401. if (bitmap->flags & BITMAP_WRITE_ERROR) {
  402. free_buffers(page);
  403. page = ERR_PTR(-EIO);
  404. }
  405. out:
  406. if (IS_ERR(page))
  407. printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n",
  408. (int)PAGE_SIZE,
  409. (unsigned long long)index << PAGE_SHIFT,
  410. PTR_ERR(page));
  411. return page;
  412. }
  413. /*
  414. * bitmap file superblock operations
  415. */
  416. /* update the event counter and sync the superblock to disk */
  417. void bitmap_update_sb(struct bitmap *bitmap)
  418. {
  419. bitmap_super_t *sb;
  420. unsigned long flags;
  421. if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
  422. return;
  423. if (bitmap->mddev->bitmap_info.external)
  424. return;
  425. spin_lock_irqsave(&bitmap->lock, flags);
  426. if (!bitmap->sb_page) { /* no superblock */
  427. spin_unlock_irqrestore(&bitmap->lock, flags);
  428. return;
  429. }
  430. spin_unlock_irqrestore(&bitmap->lock, flags);
  431. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  432. sb->events = cpu_to_le64(bitmap->mddev->events);
  433. if (bitmap->mddev->events < bitmap->events_cleared) {
  434. /* rocking back to read-only */
  435. bitmap->events_cleared = bitmap->mddev->events;
  436. sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
  437. }
  438. /* Just in case these have been changed via sysfs: */
  439. sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
  440. sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
  441. kunmap_atomic(sb, KM_USER0);
  442. write_page(bitmap, bitmap->sb_page, 1);
  443. }
  444. /* print out the bitmap file superblock */
  445. void bitmap_print_sb(struct bitmap *bitmap)
  446. {
  447. bitmap_super_t *sb;
  448. if (!bitmap || !bitmap->sb_page)
  449. return;
  450. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  451. printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
  452. printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
  453. printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
  454. printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
  455. *(__u32 *)(sb->uuid+0),
  456. *(__u32 *)(sb->uuid+4),
  457. *(__u32 *)(sb->uuid+8),
  458. *(__u32 *)(sb->uuid+12));
  459. printk(KERN_DEBUG " events: %llu\n",
  460. (unsigned long long) le64_to_cpu(sb->events));
  461. printk(KERN_DEBUG "events cleared: %llu\n",
  462. (unsigned long long) le64_to_cpu(sb->events_cleared));
  463. printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
  464. printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
  465. printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
  466. printk(KERN_DEBUG " sync size: %llu KB\n",
  467. (unsigned long long)le64_to_cpu(sb->sync_size)/2);
  468. printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
  469. kunmap_atomic(sb, KM_USER0);
  470. }
  471. /* read the superblock from the bitmap file and initialize some bitmap fields */
  472. static int bitmap_read_sb(struct bitmap *bitmap)
  473. {
  474. char *reason = NULL;
  475. bitmap_super_t *sb;
  476. unsigned long chunksize, daemon_sleep, write_behind;
  477. unsigned long long events;
  478. int err = -EINVAL;
  479. /* page 0 is the superblock, read it... */
  480. if (bitmap->file) {
  481. loff_t isize = i_size_read(bitmap->file->f_mapping->host);
  482. int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
  483. bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
  484. } else {
  485. bitmap->sb_page = read_sb_page(bitmap->mddev,
  486. bitmap->mddev->bitmap_info.offset,
  487. NULL,
  488. 0, sizeof(bitmap_super_t));
  489. }
  490. if (IS_ERR(bitmap->sb_page)) {
  491. err = PTR_ERR(bitmap->sb_page);
  492. bitmap->sb_page = NULL;
  493. return err;
  494. }
  495. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  496. chunksize = le32_to_cpu(sb->chunksize);
  497. daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
  498. write_behind = le32_to_cpu(sb->write_behind);
  499. /* verify that the bitmap-specific fields are valid */
  500. if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
  501. reason = "bad magic";
  502. else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
  503. le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
  504. reason = "unrecognized superblock version";
  505. else if (chunksize < 512)
  506. reason = "bitmap chunksize too small";
  507. else if ((1 << ffz(~chunksize)) != chunksize)
  508. reason = "bitmap chunksize not a power of 2";
  509. else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
  510. reason = "daemon sleep period out of range";
  511. else if (write_behind > COUNTER_MAX)
  512. reason = "write-behind limit out of range (0 - 16383)";
  513. if (reason) {
  514. printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
  515. bmname(bitmap), reason);
  516. goto out;
  517. }
  518. /* keep the array size field of the bitmap superblock up to date */
  519. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  520. if (!bitmap->mddev->persistent)
  521. goto success;
  522. /*
  523. * if we have a persistent array superblock, compare the
  524. * bitmap's UUID and event counter to the mddev's
  525. */
  526. if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
  527. printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
  528. bmname(bitmap));
  529. goto out;
  530. }
  531. events = le64_to_cpu(sb->events);
  532. if (events < bitmap->mddev->events) {
  533. printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
  534. "-- forcing full recovery\n", bmname(bitmap), events,
  535. (unsigned long long) bitmap->mddev->events);
  536. sb->state |= cpu_to_le32(BITMAP_STALE);
  537. }
  538. success:
  539. /* assign fields using values from superblock */
  540. bitmap->mddev->bitmap_info.chunksize = chunksize;
  541. bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
  542. bitmap->mddev->bitmap_info.max_write_behind = write_behind;
  543. bitmap->flags |= le32_to_cpu(sb->state);
  544. if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
  545. bitmap->flags |= BITMAP_HOSTENDIAN;
  546. bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
  547. if (sb->state & cpu_to_le32(BITMAP_STALE))
  548. bitmap->events_cleared = bitmap->mddev->events;
  549. err = 0;
  550. out:
  551. kunmap_atomic(sb, KM_USER0);
  552. if (err)
  553. bitmap_print_sb(bitmap);
  554. return err;
  555. }
  556. enum bitmap_mask_op {
  557. MASK_SET,
  558. MASK_UNSET
  559. };
  560. /* record the state of the bitmap in the superblock. Return the old value */
  561. static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
  562. enum bitmap_mask_op op)
  563. {
  564. bitmap_super_t *sb;
  565. unsigned long flags;
  566. int old;
  567. spin_lock_irqsave(&bitmap->lock, flags);
  568. if (!bitmap->sb_page) { /* can't set the state */
  569. spin_unlock_irqrestore(&bitmap->lock, flags);
  570. return 0;
  571. }
  572. spin_unlock_irqrestore(&bitmap->lock, flags);
  573. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  574. old = le32_to_cpu(sb->state) & bits;
  575. switch (op) {
  576. case MASK_SET:
  577. sb->state |= cpu_to_le32(bits);
  578. break;
  579. case MASK_UNSET:
  580. sb->state &= cpu_to_le32(~bits);
  581. break;
  582. default:
  583. BUG();
  584. }
  585. kunmap_atomic(sb, KM_USER0);
  586. return old;
  587. }
  588. /*
  589. * general bitmap file operations
  590. */
  591. /*
  592. * on-disk bitmap:
  593. *
  594. * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
  595. * file a page at a time. There's a superblock at the start of the file.
  596. */
  597. /* calculate the index of the page that contains this bit */
  598. static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
  599. {
  600. if (!bitmap->mddev->bitmap_info.external)
  601. chunk += sizeof(bitmap_super_t) << 3;
  602. return chunk >> PAGE_BIT_SHIFT;
  603. }
  604. /* calculate the (bit) offset of this bit within a page */
  605. static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
  606. {
  607. if (!bitmap->mddev->bitmap_info.external)
  608. chunk += sizeof(bitmap_super_t) << 3;
  609. return chunk & (PAGE_BITS - 1);
  610. }
  611. /*
  612. * return a pointer to the page in the filemap that contains the given bit
  613. *
  614. * this lookup is complicated by the fact that the bitmap sb might be exactly
  615. * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
  616. * 0 or page 1
  617. */
  618. static inline struct page *filemap_get_page(struct bitmap *bitmap,
  619. unsigned long chunk)
  620. {
  621. if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
  622. return NULL;
  623. return bitmap->filemap[file_page_index(bitmap, chunk)
  624. - file_page_index(bitmap, 0)];
  625. }
  626. static void bitmap_file_unmap(struct bitmap *bitmap)
  627. {
  628. struct page **map, *sb_page;
  629. unsigned long *attr;
  630. int pages;
  631. unsigned long flags;
  632. spin_lock_irqsave(&bitmap->lock, flags);
  633. map = bitmap->filemap;
  634. bitmap->filemap = NULL;
  635. attr = bitmap->filemap_attr;
  636. bitmap->filemap_attr = NULL;
  637. pages = bitmap->file_pages;
  638. bitmap->file_pages = 0;
  639. sb_page = bitmap->sb_page;
  640. bitmap->sb_page = NULL;
  641. spin_unlock_irqrestore(&bitmap->lock, flags);
  642. while (pages--)
  643. if (map[pages] != sb_page) /* 0 is sb_page, release it below */
  644. free_buffers(map[pages]);
  645. kfree(map);
  646. kfree(attr);
  647. if (sb_page)
  648. free_buffers(sb_page);
  649. }
  650. static void bitmap_file_put(struct bitmap *bitmap)
  651. {
  652. struct file *file;
  653. unsigned long flags;
  654. spin_lock_irqsave(&bitmap->lock, flags);
  655. file = bitmap->file;
  656. bitmap->file = NULL;
  657. spin_unlock_irqrestore(&bitmap->lock, flags);
  658. if (file)
  659. wait_event(bitmap->write_wait,
  660. atomic_read(&bitmap->pending_writes)==0);
  661. bitmap_file_unmap(bitmap);
  662. if (file) {
  663. struct inode *inode = file->f_path.dentry->d_inode;
  664. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  665. fput(file);
  666. }
  667. }
  668. /*
  669. * bitmap_file_kick - if an error occurs while manipulating the bitmap file
  670. * then it is no longer reliable, so we stop using it and we mark the file
  671. * as failed in the superblock
  672. */
  673. static void bitmap_file_kick(struct bitmap *bitmap)
  674. {
  675. char *path, *ptr = NULL;
  676. if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
  677. bitmap_update_sb(bitmap);
  678. if (bitmap->file) {
  679. path = kmalloc(PAGE_SIZE, GFP_KERNEL);
  680. if (path)
  681. ptr = d_path(&bitmap->file->f_path, path,
  682. PAGE_SIZE);
  683. printk(KERN_ALERT
  684. "%s: kicking failed bitmap file %s from array!\n",
  685. bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
  686. kfree(path);
  687. } else
  688. printk(KERN_ALERT
  689. "%s: disabling internal bitmap due to errors\n",
  690. bmname(bitmap));
  691. }
  692. bitmap_file_put(bitmap);
  693. return;
  694. }
  695. enum bitmap_page_attr {
  696. BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
  697. BITMAP_PAGE_CLEAN = 1, /* there are bits that might need to be cleared */
  698. BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
  699. };
  700. static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
  701. enum bitmap_page_attr attr)
  702. {
  703. __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
  704. }
  705. static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
  706. enum bitmap_page_attr attr)
  707. {
  708. __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
  709. }
  710. static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
  711. enum bitmap_page_attr attr)
  712. {
  713. return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
  714. }
  715. /*
  716. * bitmap_file_set_bit -- called before performing a write to the md device
  717. * to set (and eventually sync) a particular bit in the bitmap file
  718. *
  719. * we set the bit immediately, then we record the page number so that
  720. * when an unplug occurs, we can flush the dirty pages out to disk
  721. */
  722. static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
  723. {
  724. unsigned long bit;
  725. struct page *page;
  726. void *kaddr;
  727. unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
  728. if (!bitmap->filemap)
  729. return;
  730. page = filemap_get_page(bitmap, chunk);
  731. if (!page)
  732. return;
  733. bit = file_page_offset(bitmap, chunk);
  734. /* set the bit */
  735. kaddr = kmap_atomic(page, KM_USER0);
  736. if (bitmap->flags & BITMAP_HOSTENDIAN)
  737. set_bit(bit, kaddr);
  738. else
  739. ext2_set_bit(bit, kaddr);
  740. kunmap_atomic(kaddr, KM_USER0);
  741. PRINTK("set file bit %lu page %lu\n", bit, page->index);
  742. /* record page number so it gets flushed to disk when unplug occurs */
  743. set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  744. }
  745. /* this gets called when the md device is ready to unplug its underlying
  746. * (slave) device queues -- before we let any writes go down, we need to
  747. * sync the dirty pages of the bitmap file to disk */
  748. void bitmap_unplug(struct bitmap *bitmap)
  749. {
  750. unsigned long i, flags;
  751. int dirty, need_write;
  752. struct page *page;
  753. int wait = 0;
  754. if (!bitmap)
  755. return;
  756. /* look at each page to see if there are any set bits that need to be
  757. * flushed out to disk */
  758. for (i = 0; i < bitmap->file_pages; i++) {
  759. spin_lock_irqsave(&bitmap->lock, flags);
  760. if (!bitmap->filemap) {
  761. spin_unlock_irqrestore(&bitmap->lock, flags);
  762. return;
  763. }
  764. page = bitmap->filemap[i];
  765. dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  766. need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  767. clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  768. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  769. if (dirty)
  770. wait = 1;
  771. spin_unlock_irqrestore(&bitmap->lock, flags);
  772. if (dirty || need_write)
  773. write_page(bitmap, page, 0);
  774. }
  775. if (wait) { /* if any writes were performed, we need to wait on them */
  776. if (bitmap->file)
  777. wait_event(bitmap->write_wait,
  778. atomic_read(&bitmap->pending_writes)==0);
  779. else
  780. md_super_wait(bitmap->mddev);
  781. }
  782. if (bitmap->flags & BITMAP_WRITE_ERROR)
  783. bitmap_file_kick(bitmap);
  784. }
  785. EXPORT_SYMBOL(bitmap_unplug);
  786. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
  787. /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
  788. * the in-memory bitmap from the on-disk bitmap -- also, sets up the
  789. * memory mapping of the bitmap file
  790. * Special cases:
  791. * if there's no bitmap file, or if the bitmap file had been
  792. * previously kicked from the array, we mark all the bits as
  793. * 1's in order to cause a full resync.
  794. *
  795. * We ignore all bits for sectors that end earlier than 'start'.
  796. * This is used when reading an out-of-date bitmap...
  797. */
  798. static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
  799. {
  800. unsigned long i, chunks, index, oldindex, bit;
  801. struct page *page = NULL, *oldpage = NULL;
  802. unsigned long num_pages, bit_cnt = 0;
  803. struct file *file;
  804. unsigned long bytes, offset;
  805. int outofdate;
  806. int ret = -ENOSPC;
  807. void *paddr;
  808. chunks = bitmap->chunks;
  809. file = bitmap->file;
  810. BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
  811. #ifdef INJECT_FAULTS_3
  812. outofdate = 1;
  813. #else
  814. outofdate = bitmap->flags & BITMAP_STALE;
  815. #endif
  816. if (outofdate)
  817. printk(KERN_INFO "%s: bitmap file is out of date, doing full "
  818. "recovery\n", bmname(bitmap));
  819. bytes = (chunks + 7) / 8;
  820. if (!bitmap->mddev->bitmap_info.external)
  821. bytes += sizeof(bitmap_super_t);
  822. num_pages = (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  823. if (file && i_size_read(file->f_mapping->host) < bytes) {
  824. printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
  825. bmname(bitmap),
  826. (unsigned long) i_size_read(file->f_mapping->host),
  827. bytes);
  828. goto err;
  829. }
  830. ret = -ENOMEM;
  831. bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  832. if (!bitmap->filemap)
  833. goto err;
  834. /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
  835. bitmap->filemap_attr = kzalloc(
  836. roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
  837. GFP_KERNEL);
  838. if (!bitmap->filemap_attr)
  839. goto err;
  840. oldindex = ~0L;
  841. for (i = 0; i < chunks; i++) {
  842. int b;
  843. index = file_page_index(bitmap, i);
  844. bit = file_page_offset(bitmap, i);
  845. if (index != oldindex) { /* this is a new page, read it in */
  846. int count;
  847. /* unmap the old page, we're done with it */
  848. if (index == num_pages-1)
  849. count = bytes - index * PAGE_SIZE;
  850. else
  851. count = PAGE_SIZE;
  852. if (index == 0 && bitmap->sb_page) {
  853. /*
  854. * if we're here then the superblock page
  855. * contains some bits (PAGE_SIZE != sizeof sb)
  856. * we've already read it in, so just use it
  857. */
  858. page = bitmap->sb_page;
  859. offset = sizeof(bitmap_super_t);
  860. if (!file)
  861. read_sb_page(bitmap->mddev,
  862. bitmap->mddev->bitmap_info.offset,
  863. page,
  864. index, count);
  865. } else if (file) {
  866. page = read_page(file, index, bitmap, count);
  867. offset = 0;
  868. } else {
  869. page = read_sb_page(bitmap->mddev,
  870. bitmap->mddev->bitmap_info.offset,
  871. NULL,
  872. index, count);
  873. offset = 0;
  874. }
  875. if (IS_ERR(page)) { /* read error */
  876. ret = PTR_ERR(page);
  877. goto err;
  878. }
  879. oldindex = index;
  880. oldpage = page;
  881. bitmap->filemap[bitmap->file_pages++] = page;
  882. bitmap->last_page_size = count;
  883. if (outofdate) {
  884. /*
  885. * if bitmap is out of date, dirty the
  886. * whole page and write it out
  887. */
  888. paddr = kmap_atomic(page, KM_USER0);
  889. memset(paddr + offset, 0xff,
  890. PAGE_SIZE - offset);
  891. kunmap_atomic(paddr, KM_USER0);
  892. write_page(bitmap, page, 1);
  893. ret = -EIO;
  894. if (bitmap->flags & BITMAP_WRITE_ERROR)
  895. goto err;
  896. }
  897. }
  898. paddr = kmap_atomic(page, KM_USER0);
  899. if (bitmap->flags & BITMAP_HOSTENDIAN)
  900. b = test_bit(bit, paddr);
  901. else
  902. b = ext2_test_bit(bit, paddr);
  903. kunmap_atomic(paddr, KM_USER0);
  904. if (b) {
  905. /* if the disk bit is set, set the memory bit */
  906. int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
  907. >= start);
  908. bitmap_set_memory_bits(bitmap,
  909. (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
  910. needed);
  911. bit_cnt++;
  912. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  913. }
  914. }
  915. /* everything went OK */
  916. ret = 0;
  917. bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
  918. if (bit_cnt) { /* Kick recovery if any bits were set */
  919. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  920. md_wakeup_thread(bitmap->mddev->thread);
  921. }
  922. printk(KERN_INFO "%s: bitmap initialized from disk: "
  923. "read %lu/%lu pages, set %lu bits\n",
  924. bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
  925. return 0;
  926. err:
  927. printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
  928. bmname(bitmap), ret);
  929. return ret;
  930. }
  931. void bitmap_write_all(struct bitmap *bitmap)
  932. {
  933. /* We don't actually write all bitmap blocks here,
  934. * just flag them as needing to be written
  935. */
  936. int i;
  937. for (i = 0; i < bitmap->file_pages; i++)
  938. set_page_attr(bitmap, bitmap->filemap[i],
  939. BITMAP_PAGE_NEEDWRITE);
  940. }
  941. static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
  942. {
  943. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  944. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  945. bitmap->bp[page].count += inc;
  946. bitmap_checkfree(bitmap, page);
  947. }
  948. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  949. sector_t offset, int *blocks,
  950. int create);
  951. /*
  952. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  953. * out to disk
  954. */
  955. void bitmap_daemon_work(mddev_t *mddev)
  956. {
  957. struct bitmap *bitmap;
  958. unsigned long j;
  959. unsigned long flags;
  960. struct page *page = NULL, *lastpage = NULL;
  961. int blocks;
  962. void *paddr;
  963. /* Use a mutex to guard daemon_work against
  964. * bitmap_destroy.
  965. */
  966. mutex_lock(&mddev->bitmap_info.mutex);
  967. bitmap = mddev->bitmap;
  968. if (bitmap == NULL) {
  969. mutex_unlock(&mddev->bitmap_info.mutex);
  970. return;
  971. }
  972. if (time_before(jiffies, bitmap->daemon_lastrun
  973. + bitmap->mddev->bitmap_info.daemon_sleep))
  974. goto done;
  975. bitmap->daemon_lastrun = jiffies;
  976. if (bitmap->allclean) {
  977. bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  978. goto done;
  979. }
  980. bitmap->allclean = 1;
  981. spin_lock_irqsave(&bitmap->lock, flags);
  982. for (j = 0; j < bitmap->chunks; j++) {
  983. bitmap_counter_t *bmc;
  984. if (!bitmap->filemap)
  985. /* error or shutdown */
  986. break;
  987. page = filemap_get_page(bitmap, j);
  988. if (page != lastpage) {
  989. /* skip this page unless it's marked as needing cleaning */
  990. if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
  991. int need_write = test_page_attr(bitmap, page,
  992. BITMAP_PAGE_NEEDWRITE);
  993. if (need_write)
  994. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  995. spin_unlock_irqrestore(&bitmap->lock, flags);
  996. if (need_write) {
  997. write_page(bitmap, page, 0);
  998. bitmap->allclean = 0;
  999. }
  1000. spin_lock_irqsave(&bitmap->lock, flags);
  1001. j |= (PAGE_BITS - 1);
  1002. continue;
  1003. }
  1004. /* grab the new page, sync and release the old */
  1005. if (lastpage != NULL) {
  1006. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  1007. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1008. spin_unlock_irqrestore(&bitmap->lock, flags);
  1009. write_page(bitmap, lastpage, 0);
  1010. } else {
  1011. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1012. spin_unlock_irqrestore(&bitmap->lock, flags);
  1013. }
  1014. } else
  1015. spin_unlock_irqrestore(&bitmap->lock, flags);
  1016. lastpage = page;
  1017. /* We are possibly going to clear some bits, so make
  1018. * sure that events_cleared is up-to-date.
  1019. */
  1020. if (bitmap->need_sync &&
  1021. bitmap->mddev->bitmap_info.external == 0) {
  1022. bitmap_super_t *sb;
  1023. bitmap->need_sync = 0;
  1024. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  1025. sb->events_cleared =
  1026. cpu_to_le64(bitmap->events_cleared);
  1027. kunmap_atomic(sb, KM_USER0);
  1028. write_page(bitmap, bitmap->sb_page, 1);
  1029. }
  1030. spin_lock_irqsave(&bitmap->lock, flags);
  1031. if (!bitmap->need_sync)
  1032. clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1033. }
  1034. bmc = bitmap_get_counter(bitmap,
  1035. (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
  1036. &blocks, 0);
  1037. if (bmc) {
  1038. if (*bmc)
  1039. bitmap->allclean = 0;
  1040. if (*bmc == 2) {
  1041. *bmc = 1; /* maybe clear the bit next time */
  1042. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1043. } else if (*bmc == 1 && !bitmap->need_sync) {
  1044. /* we can clear the bit */
  1045. *bmc = 0;
  1046. bitmap_count_page(bitmap,
  1047. (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
  1048. -1);
  1049. /* clear the bit */
  1050. paddr = kmap_atomic(page, KM_USER0);
  1051. if (bitmap->flags & BITMAP_HOSTENDIAN)
  1052. clear_bit(file_page_offset(bitmap, j),
  1053. paddr);
  1054. else
  1055. ext2_clear_bit(file_page_offset(bitmap, j),
  1056. paddr);
  1057. kunmap_atomic(paddr, KM_USER0);
  1058. }
  1059. } else
  1060. j |= PAGE_COUNTER_MASK;
  1061. }
  1062. spin_unlock_irqrestore(&bitmap->lock, flags);
  1063. /* now sync the final page */
  1064. if (lastpage != NULL) {
  1065. spin_lock_irqsave(&bitmap->lock, flags);
  1066. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  1067. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1068. spin_unlock_irqrestore(&bitmap->lock, flags);
  1069. write_page(bitmap, lastpage, 0);
  1070. } else {
  1071. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1072. spin_unlock_irqrestore(&bitmap->lock, flags);
  1073. }
  1074. }
  1075. done:
  1076. if (bitmap->allclean == 0)
  1077. bitmap->mddev->thread->timeout =
  1078. bitmap->mddev->bitmap_info.daemon_sleep;
  1079. mutex_unlock(&mddev->bitmap_info.mutex);
  1080. }
  1081. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1082. sector_t offset, int *blocks,
  1083. int create)
  1084. __releases(bitmap->lock)
  1085. __acquires(bitmap->lock)
  1086. {
  1087. /* If 'create', we might release the lock and reclaim it.
  1088. * The lock must have been taken with interrupts enabled.
  1089. * If !create, we don't release the lock.
  1090. */
  1091. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  1092. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1093. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  1094. sector_t csize;
  1095. if (bitmap_checkpage(bitmap, page, create) < 0) {
  1096. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1097. *blocks = csize - (offset & (csize - 1));
  1098. return NULL;
  1099. }
  1100. /* now locked ... */
  1101. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  1102. /* should we use the first or second counter field
  1103. * of the hijacked pointer? */
  1104. int hi = (pageoff > PAGE_COUNTER_MASK);
  1105. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
  1106. PAGE_COUNTER_SHIFT - 1);
  1107. *blocks = csize - (offset & (csize - 1));
  1108. return &((bitmap_counter_t *)
  1109. &bitmap->bp[page].map)[hi];
  1110. } else { /* page is allocated */
  1111. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1112. *blocks = csize - (offset & (csize - 1));
  1113. return (bitmap_counter_t *)
  1114. &(bitmap->bp[page].map[pageoff]);
  1115. }
  1116. }
  1117. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
  1118. {
  1119. if (!bitmap)
  1120. return 0;
  1121. if (behind) {
  1122. int bw;
  1123. atomic_inc(&bitmap->behind_writes);
  1124. bw = atomic_read(&bitmap->behind_writes);
  1125. if (bw > bitmap->behind_writes_used)
  1126. bitmap->behind_writes_used = bw;
  1127. PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
  1128. bw, bitmap->max_write_behind);
  1129. }
  1130. while (sectors) {
  1131. int blocks;
  1132. bitmap_counter_t *bmc;
  1133. spin_lock_irq(&bitmap->lock);
  1134. bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
  1135. if (!bmc) {
  1136. spin_unlock_irq(&bitmap->lock);
  1137. return 0;
  1138. }
  1139. if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
  1140. DEFINE_WAIT(__wait);
  1141. /* note that it is safe to do the prepare_to_wait
  1142. * after the test as long as we do it before dropping
  1143. * the spinlock.
  1144. */
  1145. prepare_to_wait(&bitmap->overflow_wait, &__wait,
  1146. TASK_UNINTERRUPTIBLE);
  1147. spin_unlock_irq(&bitmap->lock);
  1148. md_unplug(bitmap->mddev);
  1149. schedule();
  1150. finish_wait(&bitmap->overflow_wait, &__wait);
  1151. continue;
  1152. }
  1153. switch (*bmc) {
  1154. case 0:
  1155. bitmap_file_set_bit(bitmap, offset);
  1156. bitmap_count_page(bitmap, offset, 1);
  1157. /* fall through */
  1158. case 1:
  1159. *bmc = 2;
  1160. }
  1161. (*bmc)++;
  1162. spin_unlock_irq(&bitmap->lock);
  1163. offset += blocks;
  1164. if (sectors > blocks)
  1165. sectors -= blocks;
  1166. else
  1167. sectors = 0;
  1168. }
  1169. bitmap->allclean = 0;
  1170. return 0;
  1171. }
  1172. EXPORT_SYMBOL(bitmap_startwrite);
  1173. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1174. int success, int behind)
  1175. {
  1176. if (!bitmap)
  1177. return;
  1178. if (behind) {
  1179. if (atomic_dec_and_test(&bitmap->behind_writes))
  1180. wake_up(&bitmap->behind_wait);
  1181. PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
  1182. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1183. }
  1184. if (bitmap->mddev->degraded)
  1185. /* Never clear bits or update events_cleared when degraded */
  1186. success = 0;
  1187. while (sectors) {
  1188. int blocks;
  1189. unsigned long flags;
  1190. bitmap_counter_t *bmc;
  1191. spin_lock_irqsave(&bitmap->lock, flags);
  1192. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1193. if (!bmc) {
  1194. spin_unlock_irqrestore(&bitmap->lock, flags);
  1195. return;
  1196. }
  1197. if (success &&
  1198. bitmap->events_cleared < bitmap->mddev->events) {
  1199. bitmap->events_cleared = bitmap->mddev->events;
  1200. bitmap->need_sync = 1;
  1201. sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
  1202. }
  1203. if (!success && ! (*bmc & NEEDED_MASK))
  1204. *bmc |= NEEDED_MASK;
  1205. if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
  1206. wake_up(&bitmap->overflow_wait);
  1207. (*bmc)--;
  1208. if (*bmc <= 2)
  1209. set_page_attr(bitmap,
  1210. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1211. BITMAP_PAGE_CLEAN);
  1212. spin_unlock_irqrestore(&bitmap->lock, flags);
  1213. offset += blocks;
  1214. if (sectors > blocks)
  1215. sectors -= blocks;
  1216. else
  1217. sectors = 0;
  1218. }
  1219. }
  1220. EXPORT_SYMBOL(bitmap_endwrite);
  1221. static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
  1222. int degraded)
  1223. {
  1224. bitmap_counter_t *bmc;
  1225. int rv;
  1226. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1227. *blocks = 1024;
  1228. return 1; /* always resync if no bitmap */
  1229. }
  1230. spin_lock_irq(&bitmap->lock);
  1231. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1232. rv = 0;
  1233. if (bmc) {
  1234. /* locked */
  1235. if (RESYNC(*bmc))
  1236. rv = 1;
  1237. else if (NEEDED(*bmc)) {
  1238. rv = 1;
  1239. if (!degraded) { /* don't set/clear bits if degraded */
  1240. *bmc |= RESYNC_MASK;
  1241. *bmc &= ~NEEDED_MASK;
  1242. }
  1243. }
  1244. }
  1245. spin_unlock_irq(&bitmap->lock);
  1246. bitmap->allclean = 0;
  1247. return rv;
  1248. }
  1249. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
  1250. int degraded)
  1251. {
  1252. /* bitmap_start_sync must always report on multiples of whole
  1253. * pages, otherwise resync (which is very PAGE_SIZE based) will
  1254. * get confused.
  1255. * So call __bitmap_start_sync repeatedly (if needed) until
  1256. * At least PAGE_SIZE>>9 blocks are covered.
  1257. * Return the 'or' of the result.
  1258. */
  1259. int rv = 0;
  1260. int blocks1;
  1261. *blocks = 0;
  1262. while (*blocks < (PAGE_SIZE>>9)) {
  1263. rv |= __bitmap_start_sync(bitmap, offset,
  1264. &blocks1, degraded);
  1265. offset += blocks1;
  1266. *blocks += blocks1;
  1267. }
  1268. return rv;
  1269. }
  1270. EXPORT_SYMBOL(bitmap_start_sync);
  1271. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
  1272. {
  1273. bitmap_counter_t *bmc;
  1274. unsigned long flags;
  1275. if (bitmap == NULL) {
  1276. *blocks = 1024;
  1277. return;
  1278. }
  1279. spin_lock_irqsave(&bitmap->lock, flags);
  1280. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1281. if (bmc == NULL)
  1282. goto unlock;
  1283. /* locked */
  1284. if (RESYNC(*bmc)) {
  1285. *bmc &= ~RESYNC_MASK;
  1286. if (!NEEDED(*bmc) && aborted)
  1287. *bmc |= NEEDED_MASK;
  1288. else {
  1289. if (*bmc <= 2)
  1290. set_page_attr(bitmap,
  1291. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1292. BITMAP_PAGE_CLEAN);
  1293. }
  1294. }
  1295. unlock:
  1296. spin_unlock_irqrestore(&bitmap->lock, flags);
  1297. bitmap->allclean = 0;
  1298. }
  1299. EXPORT_SYMBOL(bitmap_end_sync);
  1300. void bitmap_close_sync(struct bitmap *bitmap)
  1301. {
  1302. /* Sync has finished, and any bitmap chunks that weren't synced
  1303. * properly have been aborted. It remains to us to clear the
  1304. * RESYNC bit wherever it is still on
  1305. */
  1306. sector_t sector = 0;
  1307. int blocks;
  1308. if (!bitmap)
  1309. return;
  1310. while (sector < bitmap->mddev->resync_max_sectors) {
  1311. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1312. sector += blocks;
  1313. }
  1314. }
  1315. EXPORT_SYMBOL(bitmap_close_sync);
  1316. void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
  1317. {
  1318. sector_t s = 0;
  1319. int blocks;
  1320. if (!bitmap)
  1321. return;
  1322. if (sector == 0) {
  1323. bitmap->last_end_sync = jiffies;
  1324. return;
  1325. }
  1326. if (time_before(jiffies, (bitmap->last_end_sync
  1327. + bitmap->mddev->bitmap_info.daemon_sleep)))
  1328. return;
  1329. wait_event(bitmap->mddev->recovery_wait,
  1330. atomic_read(&bitmap->mddev->recovery_active) == 0);
  1331. bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync;
  1332. if (bitmap->mddev->persistent)
  1333. set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
  1334. sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
  1335. s = 0;
  1336. while (s < sector && s < bitmap->mddev->resync_max_sectors) {
  1337. bitmap_end_sync(bitmap, s, &blocks, 0);
  1338. s += blocks;
  1339. }
  1340. bitmap->last_end_sync = jiffies;
  1341. sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
  1342. }
  1343. EXPORT_SYMBOL(bitmap_cond_end_sync);
  1344. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1345. {
  1346. /* For each chunk covered by any of these sectors, set the
  1347. * counter to 1 and set resync_needed. They should all
  1348. * be 0 at this point
  1349. */
  1350. int secs;
  1351. bitmap_counter_t *bmc;
  1352. spin_lock_irq(&bitmap->lock);
  1353. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1354. if (!bmc) {
  1355. spin_unlock_irq(&bitmap->lock);
  1356. return;
  1357. }
  1358. if (!*bmc) {
  1359. struct page *page;
  1360. *bmc = 1 | (needed ? NEEDED_MASK : 0);
  1361. bitmap_count_page(bitmap, offset, 1);
  1362. page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
  1363. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1364. }
  1365. spin_unlock_irq(&bitmap->lock);
  1366. bitmap->allclean = 0;
  1367. }
  1368. /* dirty the memory and file bits for bitmap chunks "s" to "e" */
  1369. void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
  1370. {
  1371. unsigned long chunk;
  1372. for (chunk = s; chunk <= e; chunk++) {
  1373. sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
  1374. bitmap_set_memory_bits(bitmap, sec, 1);
  1375. bitmap_file_set_bit(bitmap, sec);
  1376. if (sec < bitmap->mddev->recovery_cp)
  1377. /* We are asserting that the array is dirty,
  1378. * so move the recovery_cp address back so
  1379. * that it is obvious that it is dirty
  1380. */
  1381. bitmap->mddev->recovery_cp = sec;
  1382. }
  1383. }
  1384. /*
  1385. * flush out any pending updates
  1386. */
  1387. void bitmap_flush(mddev_t *mddev)
  1388. {
  1389. struct bitmap *bitmap = mddev->bitmap;
  1390. long sleep;
  1391. if (!bitmap) /* there was no bitmap */
  1392. return;
  1393. /* run the daemon_work three time to ensure everything is flushed
  1394. * that can be
  1395. */
  1396. sleep = mddev->bitmap_info.daemon_sleep * 2;
  1397. bitmap->daemon_lastrun -= sleep;
  1398. bitmap_daemon_work(mddev);
  1399. bitmap->daemon_lastrun -= sleep;
  1400. bitmap_daemon_work(mddev);
  1401. bitmap->daemon_lastrun -= sleep;
  1402. bitmap_daemon_work(mddev);
  1403. bitmap_update_sb(bitmap);
  1404. }
  1405. /*
  1406. * free memory that was allocated
  1407. */
  1408. static void bitmap_free(struct bitmap *bitmap)
  1409. {
  1410. unsigned long k, pages;
  1411. struct bitmap_page *bp;
  1412. if (!bitmap) /* there was no bitmap */
  1413. return;
  1414. /* release the bitmap file and kill the daemon */
  1415. bitmap_file_put(bitmap);
  1416. bp = bitmap->bp;
  1417. pages = bitmap->pages;
  1418. /* free all allocated memory */
  1419. if (bp) /* deallocate the page memory */
  1420. for (k = 0; k < pages; k++)
  1421. if (bp[k].map && !bp[k].hijacked)
  1422. kfree(bp[k].map);
  1423. kfree(bp);
  1424. kfree(bitmap);
  1425. }
  1426. void bitmap_destroy(mddev_t *mddev)
  1427. {
  1428. struct bitmap *bitmap = mddev->bitmap;
  1429. if (!bitmap) /* there was no bitmap */
  1430. return;
  1431. mutex_lock(&mddev->bitmap_info.mutex);
  1432. mddev->bitmap = NULL; /* disconnect from the md device */
  1433. mutex_unlock(&mddev->bitmap_info.mutex);
  1434. if (mddev->thread)
  1435. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1436. if (bitmap->sysfs_can_clear)
  1437. sysfs_put(bitmap->sysfs_can_clear);
  1438. bitmap_free(bitmap);
  1439. }
  1440. /*
  1441. * initialize the bitmap structure
  1442. * if this returns an error, bitmap_destroy must be called to do clean up
  1443. */
  1444. int bitmap_create(mddev_t *mddev)
  1445. {
  1446. struct bitmap *bitmap;
  1447. sector_t blocks = mddev->resync_max_sectors;
  1448. unsigned long chunks;
  1449. unsigned long pages;
  1450. struct file *file = mddev->bitmap_info.file;
  1451. int err;
  1452. sector_t start;
  1453. struct sysfs_dirent *bm = NULL;
  1454. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1455. if (!file && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
  1456. return 0;
  1457. BUG_ON(file && mddev->bitmap_info.offset);
  1458. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1459. if (!bitmap)
  1460. return -ENOMEM;
  1461. spin_lock_init(&bitmap->lock);
  1462. atomic_set(&bitmap->pending_writes, 0);
  1463. init_waitqueue_head(&bitmap->write_wait);
  1464. init_waitqueue_head(&bitmap->overflow_wait);
  1465. init_waitqueue_head(&bitmap->behind_wait);
  1466. bitmap->mddev = mddev;
  1467. if (mddev->kobj.sd)
  1468. bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
  1469. if (bm) {
  1470. bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
  1471. sysfs_put(bm);
  1472. } else
  1473. bitmap->sysfs_can_clear = NULL;
  1474. bitmap->file = file;
  1475. if (file) {
  1476. get_file(file);
  1477. /* As future accesses to this file will use bmap,
  1478. * and bypass the page cache, we must sync the file
  1479. * first.
  1480. */
  1481. vfs_fsync(file, 1);
  1482. }
  1483. /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
  1484. if (!mddev->bitmap_info.external)
  1485. err = bitmap_read_sb(bitmap);
  1486. else {
  1487. err = 0;
  1488. if (mddev->bitmap_info.chunksize == 0 ||
  1489. mddev->bitmap_info.daemon_sleep == 0)
  1490. /* chunksize and time_base need to be
  1491. * set first. */
  1492. err = -EINVAL;
  1493. }
  1494. if (err)
  1495. goto error;
  1496. bitmap->daemon_lastrun = jiffies;
  1497. bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
  1498. /* now that chunksize and chunkshift are set, we can use these macros */
  1499. chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
  1500. CHUNK_BLOCK_SHIFT(bitmap);
  1501. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1502. BUG_ON(!pages);
  1503. bitmap->chunks = chunks;
  1504. bitmap->pages = pages;
  1505. bitmap->missing_pages = pages;
  1506. bitmap->counter_bits = COUNTER_BITS;
  1507. bitmap->syncchunk = ~0UL;
  1508. #ifdef INJECT_FATAL_FAULT_1
  1509. bitmap->bp = NULL;
  1510. #else
  1511. bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1512. #endif
  1513. err = -ENOMEM;
  1514. if (!bitmap->bp)
  1515. goto error;
  1516. /* now that we have some pages available, initialize the in-memory
  1517. * bitmap from the on-disk bitmap */
  1518. start = 0;
  1519. if (mddev->degraded == 0
  1520. || bitmap->events_cleared == mddev->events)
  1521. /* no need to keep dirty bits to optimise a re-add of a missing device */
  1522. start = mddev->recovery_cp;
  1523. err = bitmap_init_from_disk(bitmap, start);
  1524. if (err)
  1525. goto error;
  1526. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1527. pages, bmname(bitmap));
  1528. mddev->bitmap = bitmap;
  1529. mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
  1530. md_wakeup_thread(mddev->thread);
  1531. bitmap_update_sb(bitmap);
  1532. return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
  1533. error:
  1534. bitmap_free(bitmap);
  1535. return err;
  1536. }
  1537. static ssize_t
  1538. location_show(mddev_t *mddev, char *page)
  1539. {
  1540. ssize_t len;
  1541. if (mddev->bitmap_info.file)
  1542. len = sprintf(page, "file");
  1543. else if (mddev->bitmap_info.offset)
  1544. len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
  1545. else
  1546. len = sprintf(page, "none");
  1547. len += sprintf(page+len, "\n");
  1548. return len;
  1549. }
  1550. static ssize_t
  1551. location_store(mddev_t *mddev, const char *buf, size_t len)
  1552. {
  1553. if (mddev->pers) {
  1554. if (!mddev->pers->quiesce)
  1555. return -EBUSY;
  1556. if (mddev->recovery || mddev->sync_thread)
  1557. return -EBUSY;
  1558. }
  1559. if (mddev->bitmap || mddev->bitmap_info.file ||
  1560. mddev->bitmap_info.offset) {
  1561. /* bitmap already configured. Only option is to clear it */
  1562. if (strncmp(buf, "none", 4) != 0)
  1563. return -EBUSY;
  1564. if (mddev->pers) {
  1565. mddev->pers->quiesce(mddev, 1);
  1566. bitmap_destroy(mddev);
  1567. mddev->pers->quiesce(mddev, 0);
  1568. }
  1569. mddev->bitmap_info.offset = 0;
  1570. if (mddev->bitmap_info.file) {
  1571. struct file *f = mddev->bitmap_info.file;
  1572. mddev->bitmap_info.file = NULL;
  1573. restore_bitmap_write_access(f);
  1574. fput(f);
  1575. }
  1576. } else {
  1577. /* No bitmap, OK to set a location */
  1578. long long offset;
  1579. if (strncmp(buf, "none", 4) == 0)
  1580. /* nothing to be done */;
  1581. else if (strncmp(buf, "file:", 5) == 0) {
  1582. /* Not supported yet */
  1583. return -EINVAL;
  1584. } else {
  1585. int rv;
  1586. if (buf[0] == '+')
  1587. rv = strict_strtoll(buf+1, 10, &offset);
  1588. else
  1589. rv = strict_strtoll(buf, 10, &offset);
  1590. if (rv)
  1591. return rv;
  1592. if (offset == 0)
  1593. return -EINVAL;
  1594. if (mddev->bitmap_info.external == 0 &&
  1595. mddev->major_version == 0 &&
  1596. offset != mddev->bitmap_info.default_offset)
  1597. return -EINVAL;
  1598. mddev->bitmap_info.offset = offset;
  1599. if (mddev->pers) {
  1600. mddev->pers->quiesce(mddev, 1);
  1601. rv = bitmap_create(mddev);
  1602. if (rv) {
  1603. bitmap_destroy(mddev);
  1604. mddev->bitmap_info.offset = 0;
  1605. }
  1606. mddev->pers->quiesce(mddev, 0);
  1607. if (rv)
  1608. return rv;
  1609. }
  1610. }
  1611. }
  1612. if (!mddev->external) {
  1613. /* Ensure new bitmap info is stored in
  1614. * metadata promptly.
  1615. */
  1616. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1617. md_wakeup_thread(mddev->thread);
  1618. }
  1619. return len;
  1620. }
  1621. static struct md_sysfs_entry bitmap_location =
  1622. __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
  1623. static ssize_t
  1624. timeout_show(mddev_t *mddev, char *page)
  1625. {
  1626. ssize_t len;
  1627. unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
  1628. unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
  1629. len = sprintf(page, "%lu", secs);
  1630. if (jifs)
  1631. len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
  1632. len += sprintf(page+len, "\n");
  1633. return len;
  1634. }
  1635. static ssize_t
  1636. timeout_store(mddev_t *mddev, const char *buf, size_t len)
  1637. {
  1638. /* timeout can be set at any time */
  1639. unsigned long timeout;
  1640. int rv = strict_strtoul_scaled(buf, &timeout, 4);
  1641. if (rv)
  1642. return rv;
  1643. /* just to make sure we don't overflow... */
  1644. if (timeout >= LONG_MAX / HZ)
  1645. return -EINVAL;
  1646. timeout = timeout * HZ / 10000;
  1647. if (timeout >= MAX_SCHEDULE_TIMEOUT)
  1648. timeout = MAX_SCHEDULE_TIMEOUT-1;
  1649. if (timeout < 1)
  1650. timeout = 1;
  1651. mddev->bitmap_info.daemon_sleep = timeout;
  1652. if (mddev->thread) {
  1653. /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
  1654. * the bitmap is all clean and we don't need to
  1655. * adjust the timeout right now
  1656. */
  1657. if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
  1658. mddev->thread->timeout = timeout;
  1659. md_wakeup_thread(mddev->thread);
  1660. }
  1661. }
  1662. return len;
  1663. }
  1664. static struct md_sysfs_entry bitmap_timeout =
  1665. __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
  1666. static ssize_t
  1667. backlog_show(mddev_t *mddev, char *page)
  1668. {
  1669. return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
  1670. }
  1671. static ssize_t
  1672. backlog_store(mddev_t *mddev, const char *buf, size_t len)
  1673. {
  1674. unsigned long backlog;
  1675. int rv = strict_strtoul(buf, 10, &backlog);
  1676. if (rv)
  1677. return rv;
  1678. if (backlog > COUNTER_MAX)
  1679. return -EINVAL;
  1680. mddev->bitmap_info.max_write_behind = backlog;
  1681. return len;
  1682. }
  1683. static struct md_sysfs_entry bitmap_backlog =
  1684. __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
  1685. static ssize_t
  1686. chunksize_show(mddev_t *mddev, char *page)
  1687. {
  1688. return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
  1689. }
  1690. static ssize_t
  1691. chunksize_store(mddev_t *mddev, const char *buf, size_t len)
  1692. {
  1693. /* Can only be changed when no bitmap is active */
  1694. int rv;
  1695. unsigned long csize;
  1696. if (mddev->bitmap)
  1697. return -EBUSY;
  1698. rv = strict_strtoul(buf, 10, &csize);
  1699. if (rv)
  1700. return rv;
  1701. if (csize < 512 ||
  1702. !is_power_of_2(csize))
  1703. return -EINVAL;
  1704. mddev->bitmap_info.chunksize = csize;
  1705. return len;
  1706. }
  1707. static struct md_sysfs_entry bitmap_chunksize =
  1708. __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
  1709. static ssize_t metadata_show(mddev_t *mddev, char *page)
  1710. {
  1711. return sprintf(page, "%s\n", (mddev->bitmap_info.external
  1712. ? "external" : "internal"));
  1713. }
  1714. static ssize_t metadata_store(mddev_t *mddev, const char *buf, size_t len)
  1715. {
  1716. if (mddev->bitmap ||
  1717. mddev->bitmap_info.file ||
  1718. mddev->bitmap_info.offset)
  1719. return -EBUSY;
  1720. if (strncmp(buf, "external", 8) == 0)
  1721. mddev->bitmap_info.external = 1;
  1722. else if (strncmp(buf, "internal", 8) == 0)
  1723. mddev->bitmap_info.external = 0;
  1724. else
  1725. return -EINVAL;
  1726. return len;
  1727. }
  1728. static struct md_sysfs_entry bitmap_metadata =
  1729. __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
  1730. static ssize_t can_clear_show(mddev_t *mddev, char *page)
  1731. {
  1732. int len;
  1733. if (mddev->bitmap)
  1734. len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
  1735. "false" : "true"));
  1736. else
  1737. len = sprintf(page, "\n");
  1738. return len;
  1739. }
  1740. static ssize_t can_clear_store(mddev_t *mddev, const char *buf, size_t len)
  1741. {
  1742. if (mddev->bitmap == NULL)
  1743. return -ENOENT;
  1744. if (strncmp(buf, "false", 5) == 0)
  1745. mddev->bitmap->need_sync = 1;
  1746. else if (strncmp(buf, "true", 4) == 0) {
  1747. if (mddev->degraded)
  1748. return -EBUSY;
  1749. mddev->bitmap->need_sync = 0;
  1750. } else
  1751. return -EINVAL;
  1752. return len;
  1753. }
  1754. static struct md_sysfs_entry bitmap_can_clear =
  1755. __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
  1756. static ssize_t
  1757. behind_writes_used_show(mddev_t *mddev, char *page)
  1758. {
  1759. if (mddev->bitmap == NULL)
  1760. return sprintf(page, "0\n");
  1761. return sprintf(page, "%lu\n",
  1762. mddev->bitmap->behind_writes_used);
  1763. }
  1764. static ssize_t
  1765. behind_writes_used_reset(mddev_t *mddev, const char *buf, size_t len)
  1766. {
  1767. if (mddev->bitmap)
  1768. mddev->bitmap->behind_writes_used = 0;
  1769. return len;
  1770. }
  1771. static struct md_sysfs_entry max_backlog_used =
  1772. __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
  1773. behind_writes_used_show, behind_writes_used_reset);
  1774. static struct attribute *md_bitmap_attrs[] = {
  1775. &bitmap_location.attr,
  1776. &bitmap_timeout.attr,
  1777. &bitmap_backlog.attr,
  1778. &bitmap_chunksize.attr,
  1779. &bitmap_metadata.attr,
  1780. &bitmap_can_clear.attr,
  1781. &max_backlog_used.attr,
  1782. NULL
  1783. };
  1784. struct attribute_group md_bitmap_group = {
  1785. .name = "bitmap",
  1786. .attrs = md_bitmap_attrs,
  1787. };