bitmap.c 53 KB

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