bitmap.c 38 KB

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