bitmap.c 44 KB

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