bitmap.c 55 KB

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