bitmap.c 55 KB

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