bitmap.c 40 KB

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