blob: ae94f3beb5fc89c1a9e31b2851fd82588c667bb4 [file] [log] [blame]
NeilBrown32a76272005-06-21 17:17:14 -07001/*
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
NeilBrown32a76272005-06-21 17:17:14 -070010 */
11
12/*
13 * Still to do:
14 *
15 * flush after percent set rather than just time based. (maybe both).
16 * wait if count gets too high, wake when it drops to half.
NeilBrown32a76272005-06-21 17:17:14 -070017 */
18
19#include <linux/module.h>
NeilBrown32a76272005-06-21 17:17:14 -070020#include <linux/errno.h>
21#include <linux/slab.h>
22#include <linux/init.h>
NeilBrown32a76272005-06-21 17:17:14 -070023#include <linux/timer.h>
24#include <linux/sched.h>
25#include <linux/list.h>
26#include <linux/file.h>
27#include <linux/mount.h>
28#include <linux/buffer_head.h>
29#include <linux/raid/md.h>
30#include <linux/raid/bitmap.h>
31
32/* debug macros */
33
34#define DEBUG 0
35
36#if DEBUG
37/* these are for debugging purposes only! */
38
39/* define one and only one of these */
40#define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
41#define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
42#define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
43#define INJECT_FAULTS_4 0 /* undef */
44#define INJECT_FAULTS_5 0 /* undef */
45#define INJECT_FAULTS_6 0
46
47/* if these are defined, the driver will fail! debug only */
48#define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
49#define INJECT_FATAL_FAULT_2 0 /* undef */
50#define INJECT_FATAL_FAULT_3 0 /* undef */
51#endif
52
53//#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
54#define DPRINTK(x...) do { } while(0)
55
56#ifndef PRINTK
57# if DEBUG > 0
58# define PRINTK(x...) printk(KERN_DEBUG x)
59# else
60# define PRINTK(x...)
61# endif
62#endif
63
64static inline char * bmname(struct bitmap *bitmap)
65{
66 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
67}
68
NeilBrown32a76272005-06-21 17:17:14 -070069
70/*
71 * just a placeholder - calls kmalloc for bitmap pages
72 */
73static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
74{
75 unsigned char *page;
76
Olaf Hering44456d32005-07-27 11:45:17 -070077#ifdef INJECT_FAULTS_1
NeilBrown32a76272005-06-21 17:17:14 -070078 page = NULL;
79#else
80 page = kmalloc(PAGE_SIZE, GFP_NOIO);
81#endif
82 if (!page)
83 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
84 else
NeilBrowna654b9d82005-06-21 17:17:27 -070085 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
NeilBrown32a76272005-06-21 17:17:14 -070086 bmname(bitmap), page);
87 return page;
88}
89
90/*
91 * for now just a placeholder -- just calls kfree for bitmap pages
92 */
93static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
94{
95 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
96 kfree(page);
97}
98
99/*
100 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
101 *
102 * 1) check to see if this page is allocated, if it's not then try to alloc
103 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
104 * page pointer directly as a counter
105 *
106 * if we find our page, we increment the page's refcount so that it stays
107 * allocated while we're using it
108 */
109static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
110{
111 unsigned char *mappage;
112
113 if (page >= bitmap->pages) {
114 printk(KERN_ALERT
115 "%s: invalid bitmap page request: %lu (> %lu)\n",
116 bmname(bitmap), page, bitmap->pages-1);
117 return -EINVAL;
118 }
119
120
121 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
122 return 0;
123
124 if (bitmap->bp[page].map) /* page is already allocated, just return */
125 return 0;
126
127 if (!create)
128 return -ENOENT;
129
130 spin_unlock_irq(&bitmap->lock);
131
132 /* this page has not been allocated yet */
133
134 if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
135 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
136 bmname(bitmap));
137 /* failed - set the hijacked flag so that we can use the
138 * pointer as a counter */
139 spin_lock_irq(&bitmap->lock);
140 if (!bitmap->bp[page].map)
141 bitmap->bp[page].hijacked = 1;
142 goto out;
143 }
144
145 /* got a page */
146
147 spin_lock_irq(&bitmap->lock);
148
149 /* recheck the page */
150
151 if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
152 /* somebody beat us to getting the page */
153 bitmap_free_page(bitmap, mappage);
154 return 0;
155 }
156
157 /* no page was in place and we have one, so install it */
158
159 memset(mappage, 0, PAGE_SIZE);
160 bitmap->bp[page].map = mappage;
161 bitmap->missing_pages--;
162out:
163 return 0;
164}
165
166
167/* if page is completely empty, put it back on the free list, or dealloc it */
168/* if page was hijacked, unmark the flag so it might get alloced next time */
169/* Note: lock should be held when calling this */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800170static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
NeilBrown32a76272005-06-21 17:17:14 -0700171{
172 char *ptr;
173
174 if (bitmap->bp[page].count) /* page is still busy */
175 return;
176
177 /* page is no longer in use, it can be released */
178
179 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
180 bitmap->bp[page].hijacked = 0;
181 bitmap->bp[page].map = NULL;
182 return;
183 }
184
185 /* normal case, free the page */
186
187#if 0
188/* actually ... let's not. We will probably need the page again exactly when
189 * memory is tight and we are flusing to disk
190 */
191 return;
192#else
193 ptr = bitmap->bp[page].map;
194 bitmap->bp[page].map = NULL;
195 bitmap->missing_pages++;
196 bitmap_free_page(bitmap, ptr);
197 return;
198#endif
199}
200
201
202/*
203 * bitmap file handling - read and write the bitmap file and its superblock
204 */
205
206/* copy the pathname of a file to a buffer */
207char *file_path(struct file *file, char *buf, int count)
208{
209 struct dentry *d;
210 struct vfsmount *v;
211
212 if (!buf)
213 return NULL;
214
Josef Sipekc649bb92006-12-08 02:37:19 -0800215 d = file->f_path.dentry;
216 v = file->f_path.mnt;
NeilBrown32a76272005-06-21 17:17:14 -0700217
218 buf = d_path(d, v, buf, count);
219
220 return IS_ERR(buf) ? NULL : buf;
221}
222
223/*
224 * basic page I/O operations
225 */
226
NeilBrowna654b9d82005-06-21 17:17:27 -0700227/* IO operations when bitmap is stored near all superblocks */
228static struct page *read_sb_page(mddev_t *mddev, long offset, unsigned long index)
229{
230 /* choose a good rdev and read the page from there */
231
232 mdk_rdev_t *rdev;
233 struct list_head *tmp;
234 struct page *page = alloc_page(GFP_KERNEL);
235 sector_t target;
236
237 if (!page)
238 return ERR_PTR(-ENOMEM);
NeilBrowna654b9d82005-06-21 17:17:27 -0700239
NeilBrownab904d62005-09-09 16:23:52 -0700240 ITERATE_RDEV(mddev, rdev, tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800241 if (! test_bit(In_sync, &rdev->flags)
242 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700243 continue;
244
NeilBrowna654b9d82005-06-21 17:17:27 -0700245 target = (rdev->sb_offset << 1) + offset + index * (PAGE_SIZE/512);
246
NeilBrownab904d62005-09-09 16:23:52 -0700247 if (sync_page_io(rdev->bdev, target, PAGE_SIZE, page, READ)) {
248 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700249 attach_page_buffers(page, NULL); /* so that free_buffer will
250 * quietly no-op */
NeilBrownab904d62005-09-09 16:23:52 -0700251 return page;
252 }
253 }
254 return ERR_PTR(-EIO);
NeilBrowna654b9d82005-06-21 17:17:27 -0700255
NeilBrowna654b9d82005-06-21 17:17:27 -0700256}
257
NeilBrownab6085c2007-05-23 13:58:10 -0700258static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrowna654b9d82005-06-21 17:17:27 -0700259{
260 mdk_rdev_t *rdev;
261 struct list_head *tmp;
NeilBrownab6085c2007-05-23 13:58:10 -0700262 mddev_t *mddev = bitmap->mddev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700263
264 ITERATE_RDEV(mddev, rdev, tmp)
NeilBrownb2d444d2005-11-08 21:39:31 -0800265 if (test_bit(In_sync, &rdev->flags)
NeilBrownab6085c2007-05-23 13:58:10 -0700266 && !test_bit(Faulty, &rdev->flags)) {
267 int size = PAGE_SIZE;
268 if (page->index == bitmap->file_pages-1)
269 size = roundup(bitmap->last_page_size,
270 bdev_hardsect_size(rdev->bdev));
NeilBrownf0d76d72007-07-17 04:06:12 -0700271 /* Just make sure we aren't corrupting data or
272 * metadata
273 */
274 if (bitmap->offset < 0) {
275 /* DATA BITMAP METADATA */
276 if (bitmap->offset
277 + page->index * (PAGE_SIZE/512)
278 + size/512 > 0)
279 /* bitmap runs in to metadata */
280 return -EINVAL;
281 if (rdev->data_offset + mddev->size*2
282 > rdev->sb_offset*2 + bitmap->offset)
283 /* data runs in to bitmap */
284 return -EINVAL;
285 } else if (rdev->sb_offset*2 < rdev->data_offset) {
286 /* METADATA BITMAP DATA */
287 if (rdev->sb_offset*2
288 + bitmap->offset
289 + page->index*(PAGE_SIZE/512) + size/512
290 > rdev->data_offset)
291 /* bitmap runs in to data */
292 return -EINVAL;
293 } else {
294 /* DATA METADATA BITMAP - no problems */
295 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700296 md_super_write(mddev, rdev,
NeilBrownab6085c2007-05-23 13:58:10 -0700297 (rdev->sb_offset<<1) + bitmap->offset
NeilBrowna654b9d82005-06-21 17:17:27 -0700298 + page->index * (PAGE_SIZE/512),
NeilBrownab6085c2007-05-23 13:58:10 -0700299 size,
NeilBrowna654b9d82005-06-21 17:17:27 -0700300 page);
NeilBrownab6085c2007-05-23 13:58:10 -0700301 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700302
303 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800304 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700305 return 0;
306}
307
NeilBrown32a76272005-06-21 17:17:14 -0700308/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700309 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700310 */
NeilBrown77ad4bc2005-06-21 17:17:21 -0700311static int write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700312{
NeilBrownd785a062006-06-26 00:27:48 -0700313 struct buffer_head *bh;
NeilBrown32a76272005-06-21 17:17:14 -0700314
NeilBrownf0d76d72007-07-17 04:06:12 -0700315 if (bitmap->file == NULL) {
316 switch (write_sb_page(bitmap, page, wait)) {
317 case -EINVAL:
318 bitmap->flags |= BITMAP_WRITE_ERROR;
319 return -EIO;
320 }
321 return 0;
322 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700323
NeilBrownd785a062006-06-26 00:27:48 -0700324 bh = page_buffers(page);
NeilBrownc7084432006-01-06 00:20:45 -0800325
NeilBrownd785a062006-06-26 00:27:48 -0700326 while (bh && bh->b_blocknr) {
327 atomic_inc(&bitmap->pending_writes);
328 set_buffer_locked(bh);
329 set_buffer_mapped(bh);
330 submit_bh(WRITE, bh);
331 bh = bh->b_this_page;
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700332 }
NeilBrown32a76272005-06-21 17:17:14 -0700333
NeilBrownd785a062006-06-26 00:27:48 -0700334 if (wait) {
335 wait_event(bitmap->write_wait,
336 atomic_read(&bitmap->pending_writes)==0);
337 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
NeilBrown32a76272005-06-21 17:17:14 -0700338 }
NeilBrownd785a062006-06-26 00:27:48 -0700339 return 0;
NeilBrown32a76272005-06-21 17:17:14 -0700340}
341
NeilBrownd785a062006-06-26 00:27:48 -0700342static void end_bitmap_write(struct buffer_head *bh, int uptodate)
NeilBrown32a76272005-06-21 17:17:14 -0700343{
NeilBrownd785a062006-06-26 00:27:48 -0700344 struct bitmap *bitmap = bh->b_private;
345 unsigned long flags;
346
347 if (!uptodate) {
348 spin_lock_irqsave(&bitmap->lock, flags);
349 bitmap->flags |= BITMAP_WRITE_ERROR;
350 spin_unlock_irqrestore(&bitmap->lock, flags);
351 }
352 if (atomic_dec_and_test(&bitmap->pending_writes))
353 wake_up(&bitmap->write_wait);
354}
355
356/* copied from buffer.c */
357static void
358__clear_page_buffers(struct page *page)
359{
360 ClearPagePrivate(page);
361 set_page_private(page, 0);
362 page_cache_release(page);
363}
364static void free_buffers(struct page *page)
365{
366 struct buffer_head *bh = page_buffers(page);
367
368 while (bh) {
369 struct buffer_head *next = bh->b_this_page;
370 free_buffer_head(bh);
371 bh = next;
372 }
373 __clear_page_buffers(page);
374 put_page(page);
375}
376
377/* read a page from a file.
378 * We both read the page, and attach buffers to the page to record the
379 * address of each block (using bmap). These addresses will be used
380 * to write the block later, completely bypassing the filesystem.
381 * This usage is similar to how swap files are handled, and allows us
382 * to write to a file with no concerns of memory allocation failing.
383 */
384static struct page *read_page(struct file *file, unsigned long index,
385 struct bitmap *bitmap,
386 unsigned long count)
387{
NeilBrown32a76272005-06-21 17:17:14 -0700388 struct page *page = NULL;
Josef Sipekc649bb92006-12-08 02:37:19 -0800389 struct inode *inode = file->f_path.dentry->d_inode;
NeilBrownd785a062006-06-26 00:27:48 -0700390 struct buffer_head *bh;
391 sector_t block;
NeilBrown32a76272005-06-21 17:17:14 -0700392
NeilBrown2d1f3b52006-01-06 00:20:31 -0800393 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
394 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700395
NeilBrownd785a062006-06-26 00:27:48 -0700396 page = alloc_page(GFP_KERNEL);
397 if (!page)
398 page = ERR_PTR(-ENOMEM);
NeilBrown32a76272005-06-21 17:17:14 -0700399 if (IS_ERR(page))
400 goto out;
NeilBrownd785a062006-06-26 00:27:48 -0700401
NeilBrownd785a062006-06-26 00:27:48 -0700402 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
403 if (!bh) {
404 put_page(page);
405 page = ERR_PTR(-ENOMEM);
406 goto out;
407 }
408 attach_page_buffers(page, bh);
409 block = index << (PAGE_SHIFT - inode->i_blkbits);
410 while (bh) {
411 if (count == 0)
412 bh->b_blocknr = 0;
413 else {
414 bh->b_blocknr = bmap(inode, block);
415 if (bh->b_blocknr == 0) {
416 /* Cannot use this file! */
417 free_buffers(page);
418 page = ERR_PTR(-EINVAL);
419 goto out;
420 }
421 bh->b_bdev = inode->i_sb->s_bdev;
422 if (count < (1<<inode->i_blkbits))
423 count = 0;
424 else
425 count -= (1<<inode->i_blkbits);
NeilBrown32a76272005-06-21 17:17:14 -0700426
NeilBrownd785a062006-06-26 00:27:48 -0700427 bh->b_end_io = end_bitmap_write;
428 bh->b_private = bitmap;
NeilBrownce25c312006-06-26 00:27:49 -0700429 atomic_inc(&bitmap->pending_writes);
430 set_buffer_locked(bh);
431 set_buffer_mapped(bh);
432 submit_bh(READ, bh);
NeilBrownd785a062006-06-26 00:27:48 -0700433 }
434 block++;
435 bh = bh->b_this_page;
436 }
NeilBrownd785a062006-06-26 00:27:48 -0700437 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700438
439 wait_event(bitmap->write_wait,
440 atomic_read(&bitmap->pending_writes)==0);
441 if (bitmap->flags & BITMAP_WRITE_ERROR) {
442 free_buffers(page);
443 page = ERR_PTR(-EIO);
444 }
NeilBrown32a76272005-06-21 17:17:14 -0700445out:
446 if (IS_ERR(page))
447 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800448 (int)PAGE_SIZE,
449 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700450 PTR_ERR(page));
451 return page;
452}
453
454/*
455 * bitmap file superblock operations
456 */
457
458/* update the event counter and sync the superblock to disk */
459int bitmap_update_sb(struct bitmap *bitmap)
460{
461 bitmap_super_t *sb;
462 unsigned long flags;
463
464 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
465 return 0;
466 spin_lock_irqsave(&bitmap->lock, flags);
467 if (!bitmap->sb_page) { /* no superblock */
468 spin_unlock_irqrestore(&bitmap->lock, flags);
469 return 0;
470 }
NeilBrown32a76272005-06-21 17:17:14 -0700471 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800472 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700473 sb->events = cpu_to_le64(bitmap->mddev->events);
474 if (!bitmap->mddev->degraded)
475 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
NeilBrownea03aff2006-01-06 00:20:34 -0800476 kunmap_atomic(sb, KM_USER0);
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700477 return write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700478}
479
480/* print out the bitmap file superblock */
481void bitmap_print_sb(struct bitmap *bitmap)
482{
483 bitmap_super_t *sb;
484
485 if (!bitmap || !bitmap->sb_page)
486 return;
NeilBrownea03aff2006-01-06 00:20:34 -0800487 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700488 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700489 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
490 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
491 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700492 *(__u32 *)(sb->uuid+0),
493 *(__u32 *)(sb->uuid+4),
494 *(__u32 *)(sb->uuid+8),
495 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700496 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700497 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700498 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700499 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700500 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
501 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
502 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
503 printk(KERN_DEBUG " sync size: %llu KB\n",
504 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700505 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800506 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700507}
508
509/* read the superblock from the bitmap file and initialize some bitmap fields */
510static int bitmap_read_sb(struct bitmap *bitmap)
511{
512 char *reason = NULL;
513 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700514 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700515 unsigned long long events;
516 int err = -EINVAL;
517
518 /* page 0 is the superblock, read it... */
NeilBrownf49d5e62007-01-26 00:57:03 -0800519 if (bitmap->file) {
520 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
521 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
522
523 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
524 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700525 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
NeilBrowna654b9d82005-06-21 17:17:27 -0700526 }
NeilBrown32a76272005-06-21 17:17:14 -0700527 if (IS_ERR(bitmap->sb_page)) {
528 err = PTR_ERR(bitmap->sb_page);
529 bitmap->sb_page = NULL;
530 return err;
531 }
532
NeilBrownea03aff2006-01-06 00:20:34 -0800533 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700534
NeilBrown32a76272005-06-21 17:17:14 -0700535 chunksize = le32_to_cpu(sb->chunksize);
536 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
NeilBrown4b6d2872005-09-09 16:23:47 -0700537 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700538
539 /* verify that the bitmap-specific fields are valid */
540 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
541 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800542 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
543 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700544 reason = "unrecognized superblock version";
NeilBrown7dd5d342006-01-06 00:20:39 -0800545 else if (chunksize < PAGE_SIZE)
546 reason = "bitmap chunksize too small";
NeilBrown32a76272005-06-21 17:17:14 -0700547 else if ((1 << ffz(~chunksize)) != chunksize)
548 reason = "bitmap chunksize not a power of 2";
NeilBrown7dd5d342006-01-06 00:20:39 -0800549 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT / HZ)
550 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700551 else if (write_behind > COUNTER_MAX)
552 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700553 if (reason) {
554 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
555 bmname(bitmap), reason);
556 goto out;
557 }
558
559 /* keep the array size field of the bitmap superblock up to date */
560 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
561
562 if (!bitmap->mddev->persistent)
563 goto success;
564
565 /*
566 * if we have a persistent array superblock, compare the
567 * bitmap's UUID and event counter to the mddev's
568 */
569 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
570 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
571 bmname(bitmap));
572 goto out;
573 }
574 events = le64_to_cpu(sb->events);
575 if (events < bitmap->mddev->events) {
576 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
577 "-- forcing full recovery\n", bmname(bitmap), events,
578 (unsigned long long) bitmap->mddev->events);
NeilBrown4f2e6392006-10-21 10:24:09 -0700579 sb->state |= cpu_to_le32(BITMAP_STALE);
NeilBrown32a76272005-06-21 17:17:14 -0700580 }
581success:
582 /* assign fields using values from superblock */
583 bitmap->chunksize = chunksize;
584 bitmap->daemon_sleep = daemon_sleep;
NeilBrown585f0dd2005-09-09 16:23:49 -0700585 bitmap->daemon_lastrun = jiffies;
NeilBrown4b6d2872005-09-09 16:23:47 -0700586 bitmap->max_write_behind = write_behind;
NeilBrown4f2e6392006-10-21 10:24:09 -0700587 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800588 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
589 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700590 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown4f2e6392006-10-21 10:24:09 -0700591 if (sb->state & cpu_to_le32(BITMAP_STALE))
NeilBrown6a079972005-09-09 16:23:44 -0700592 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700593 err = 0;
594out:
NeilBrownea03aff2006-01-06 00:20:34 -0800595 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700596 if (err)
597 bitmap_print_sb(bitmap);
598 return err;
599}
600
601enum bitmap_mask_op {
602 MASK_SET,
603 MASK_UNSET
604};
605
606/* record the state of the bitmap in the superblock */
607static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
608 enum bitmap_mask_op op)
609{
610 bitmap_super_t *sb;
611 unsigned long flags;
612
613 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800614 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700615 spin_unlock_irqrestore(&bitmap->lock, flags);
616 return;
617 }
NeilBrown32a76272005-06-21 17:17:14 -0700618 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800619 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700620 switch (op) {
NeilBrown4f2e6392006-10-21 10:24:09 -0700621 case MASK_SET: sb->state |= cpu_to_le32(bits);
NeilBrown32a76272005-06-21 17:17:14 -0700622 break;
NeilBrown4f2e6392006-10-21 10:24:09 -0700623 case MASK_UNSET: sb->state &= cpu_to_le32(~bits);
NeilBrown32a76272005-06-21 17:17:14 -0700624 break;
625 default: BUG();
626 }
NeilBrownea03aff2006-01-06 00:20:34 -0800627 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700628}
629
630/*
631 * general bitmap file operations
632 */
633
634/* calculate the index of the page that contains this bit */
635static inline unsigned long file_page_index(unsigned long chunk)
636{
637 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
638}
639
640/* calculate the (bit) offset of this bit within a page */
641static inline unsigned long file_page_offset(unsigned long chunk)
642{
643 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
644}
645
646/*
647 * return a pointer to the page in the filemap that contains the given bit
648 *
649 * this lookup is complicated by the fact that the bitmap sb might be exactly
650 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
651 * 0 or page 1
652 */
653static inline struct page *filemap_get_page(struct bitmap *bitmap,
654 unsigned long chunk)
655{
Paul Clements9b1d1da2006-10-03 01:15:49 -0700656 if (file_page_index(chunk) >= bitmap->file_pages) return NULL;
NeilBrown32a76272005-06-21 17:17:14 -0700657 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
658}
659
660
661static void bitmap_file_unmap(struct bitmap *bitmap)
662{
663 struct page **map, *sb_page;
664 unsigned long *attr;
665 int pages;
666 unsigned long flags;
667
668 spin_lock_irqsave(&bitmap->lock, flags);
669 map = bitmap->filemap;
670 bitmap->filemap = NULL;
671 attr = bitmap->filemap_attr;
672 bitmap->filemap_attr = NULL;
673 pages = bitmap->file_pages;
674 bitmap->file_pages = 0;
675 sb_page = bitmap->sb_page;
676 bitmap->sb_page = NULL;
677 spin_unlock_irqrestore(&bitmap->lock, flags);
678
679 while (pages--)
680 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700681 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700682 kfree(map);
683 kfree(attr);
684
NeilBrownd785a062006-06-26 00:27:48 -0700685 if (sb_page)
686 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700687}
688
689static void bitmap_file_put(struct bitmap *bitmap)
690{
691 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700692 unsigned long flags;
693
694 spin_lock_irqsave(&bitmap->lock, flags);
695 file = bitmap->file;
696 bitmap->file = NULL;
697 spin_unlock_irqrestore(&bitmap->lock, flags);
698
NeilBrownd785a062006-06-26 00:27:48 -0700699 if (file)
700 wait_event(bitmap->write_wait,
701 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700702 bitmap_file_unmap(bitmap);
703
NeilBrownd785a062006-06-26 00:27:48 -0700704 if (file) {
Josef Sipekc649bb92006-12-08 02:37:19 -0800705 struct inode *inode = file->f_path.dentry->d_inode;
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800706 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700707 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700708 }
NeilBrown32a76272005-06-21 17:17:14 -0700709}
710
711
712/*
713 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
714 * then it is no longer reliable, so we stop using it and we mark the file
715 * as failed in the superblock
716 */
717static void bitmap_file_kick(struct bitmap *bitmap)
718{
719 char *path, *ptr = NULL;
720
721 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
722 bitmap_update_sb(bitmap);
723
NeilBrowna654b9d82005-06-21 17:17:27 -0700724 if (bitmap->file) {
725 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
726 if (path)
727 ptr = file_path(bitmap->file, path, PAGE_SIZE);
NeilBrown32a76272005-06-21 17:17:14 -0700728
NeilBrowna654b9d82005-06-21 17:17:27 -0700729 printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
730 bmname(bitmap), ptr ? ptr : "");
NeilBrown32a76272005-06-21 17:17:14 -0700731
NeilBrowna654b9d82005-06-21 17:17:27 -0700732 kfree(path);
733 }
NeilBrown32a76272005-06-21 17:17:14 -0700734
735 bitmap_file_put(bitmap);
736
737 return;
738}
739
740enum bitmap_page_attr {
NeilBrowne16b68b2006-06-26 00:27:45 -0700741 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
742 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
743 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
NeilBrown32a76272005-06-21 17:17:14 -0700744};
745
746static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
747 enum bitmap_page_attr attr)
748{
NeilBrowne16b68b2006-06-26 00:27:45 -0700749 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700750}
751
752static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
753 enum bitmap_page_attr attr)
754{
NeilBrowne16b68b2006-06-26 00:27:45 -0700755 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700756}
757
NeilBrownec7a3192006-06-26 00:27:45 -0700758static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
759 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700760{
NeilBrowne16b68b2006-06-26 00:27:45 -0700761 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700762}
763
764/*
765 * bitmap_file_set_bit -- called before performing a write to the md device
766 * to set (and eventually sync) a particular bit in the bitmap file
767 *
768 * we set the bit immediately, then we record the page number so that
769 * when an unplug occurs, we can flush the dirty pages out to disk
770 */
771static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
772{
773 unsigned long bit;
774 struct page *page;
775 void *kaddr;
776 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
777
NeilBrowna654b9d82005-06-21 17:17:27 -0700778 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700779 return;
780 }
781
782 page = filemap_get_page(bitmap, chunk);
Paul Clements9b1d1da2006-10-03 01:15:49 -0700783 if (!page) return;
NeilBrown32a76272005-06-21 17:17:14 -0700784 bit = file_page_offset(chunk);
785
NeilBrown32a76272005-06-21 17:17:14 -0700786 /* set the bit */
787 kaddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800788 if (bitmap->flags & BITMAP_HOSTENDIAN)
789 set_bit(bit, kaddr);
790 else
791 ext2_set_bit(bit, kaddr);
NeilBrown32a76272005-06-21 17:17:14 -0700792 kunmap_atomic(kaddr, KM_USER0);
793 PRINTK("set file bit %lu page %lu\n", bit, page->index);
794
795 /* record page number so it gets flushed to disk when unplug occurs */
796 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
797
798}
799
800/* this gets called when the md device is ready to unplug its underlying
801 * (slave) device queues -- before we let any writes go down, we need to
802 * sync the dirty pages of the bitmap file to disk */
803int bitmap_unplug(struct bitmap *bitmap)
804{
NeilBrownec7a3192006-06-26 00:27:45 -0700805 unsigned long i, flags;
806 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700807 struct page *page;
808 int wait = 0;
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700809 int err;
NeilBrown32a76272005-06-21 17:17:14 -0700810
811 if (!bitmap)
812 return 0;
813
814 /* look at each page to see if there are any set bits that need to be
815 * flushed out to disk */
816 for (i = 0; i < bitmap->file_pages; i++) {
817 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700818 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700819 spin_unlock_irqrestore(&bitmap->lock, flags);
820 return 0;
821 }
822 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700823 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
824 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700825 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
826 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700827 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700828 wait = 1;
829 spin_unlock_irqrestore(&bitmap->lock, flags);
830
NeilBrownd785a062006-06-26 00:27:48 -0700831 if (dirty | need_write)
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700832 err = write_page(bitmap, page, 0);
NeilBrown32a76272005-06-21 17:17:14 -0700833 }
834 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700835 if (bitmap->file)
NeilBrownd785a062006-06-26 00:27:48 -0700836 wait_event(bitmap->write_wait,
837 atomic_read(&bitmap->pending_writes)==0);
NeilBrown0b79ccf2006-06-26 00:27:44 -0700838 else
NeilBrowna9701a32005-11-08 21:39:34 -0800839 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700840 }
NeilBrownd785a062006-06-26 00:27:48 -0700841 if (bitmap->flags & BITMAP_WRITE_ERROR)
842 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700843 return 0;
844}
845
NeilBrown6a079972005-09-09 16:23:44 -0700846static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700847/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
848 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
849 * memory mapping of the bitmap file
850 * Special cases:
851 * if there's no bitmap file, or if the bitmap file had been
852 * previously kicked from the array, we mark all the bits as
853 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700854 *
855 * We ignore all bits for sectors that end earlier than 'start'.
856 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700857 */
NeilBrown6a079972005-09-09 16:23:44 -0700858static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700859{
860 unsigned long i, chunks, index, oldindex, bit;
861 struct page *page = NULL, *oldpage = NULL;
862 unsigned long num_pages, bit_cnt = 0;
863 struct file *file;
NeilBrownd785a062006-06-26 00:27:48 -0700864 unsigned long bytes, offset;
NeilBrown32a76272005-06-21 17:17:14 -0700865 int outofdate;
866 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800867 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700868
869 chunks = bitmap->chunks;
870 file = bitmap->file;
871
NeilBrowna654b9d82005-06-21 17:17:27 -0700872 BUG_ON(!file && !bitmap->offset);
NeilBrown32a76272005-06-21 17:17:14 -0700873
Olaf Hering44456d32005-07-27 11:45:17 -0700874#ifdef INJECT_FAULTS_3
NeilBrown32a76272005-06-21 17:17:14 -0700875 outofdate = 1;
876#else
877 outofdate = bitmap->flags & BITMAP_STALE;
878#endif
879 if (outofdate)
880 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
881 "recovery\n", bmname(bitmap));
882
883 bytes = (chunks + 7) / 8;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700884
NeilBrowncdbb4cc2005-06-21 17:17:18 -0700885 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700886
NeilBrowna654b9d82005-06-21 17:17:27 -0700887 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
NeilBrown32a76272005-06-21 17:17:14 -0700888 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
889 bmname(bitmap),
890 (unsigned long) i_size_read(file->f_mapping->host),
891 bytes + sizeof(bitmap_super_t));
892 goto out;
893 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700894
895 ret = -ENOMEM;
896
NeilBrown32a76272005-06-21 17:17:14 -0700897 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700898 if (!bitmap->filemap)
NeilBrown32a76272005-06-21 17:17:14 -0700899 goto out;
NeilBrown32a76272005-06-21 17:17:14 -0700900
NeilBrowne16b68b2006-06-26 00:27:45 -0700901 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
902 bitmap->filemap_attr = kzalloc(
Neil Brown505fa2c2007-04-11 23:28:44 -0700903 roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
NeilBrowne16b68b2006-06-26 00:27:45 -0700904 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700905 if (!bitmap->filemap_attr)
NeilBrown32a76272005-06-21 17:17:14 -0700906 goto out;
NeilBrown32a76272005-06-21 17:17:14 -0700907
NeilBrown32a76272005-06-21 17:17:14 -0700908 oldindex = ~0L;
909
910 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -0800911 int b;
NeilBrown32a76272005-06-21 17:17:14 -0700912 index = file_page_index(i);
913 bit = file_page_offset(i);
914 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -0700915 int count;
NeilBrown32a76272005-06-21 17:17:14 -0700916 /* unmap the old page, we're done with it */
NeilBrownd785a062006-06-26 00:27:48 -0700917 if (index == num_pages-1)
NeilBrownf49d5e62007-01-26 00:57:03 -0800918 count = bytes + sizeof(bitmap_super_t)
919 - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -0700920 else
921 count = PAGE_SIZE;
NeilBrown32a76272005-06-21 17:17:14 -0700922 if (index == 0) {
923 /*
924 * if we're here then the superblock page
925 * contains some bits (PAGE_SIZE != sizeof sb)
926 * we've already read it in, so just use it
927 */
928 page = bitmap->sb_page;
929 offset = sizeof(bitmap_super_t);
NeilBrowna654b9d82005-06-21 17:17:27 -0700930 } else if (file) {
NeilBrownd785a062006-06-26 00:27:48 -0700931 page = read_page(file, index, bitmap, count);
NeilBrowna654b9d82005-06-21 17:17:27 -0700932 offset = 0;
933 } else {
934 page = read_sb_page(bitmap->mddev, bitmap->offset, index);
NeilBrown32a76272005-06-21 17:17:14 -0700935 offset = 0;
936 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700937 if (IS_ERR(page)) { /* read error */
938 ret = PTR_ERR(page);
939 goto out;
940 }
941
NeilBrown32a76272005-06-21 17:17:14 -0700942 oldindex = index;
943 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -0700944
945 if (outofdate) {
946 /*
947 * if bitmap is out of date, dirty the
948 * whole page and write it out
949 */
NeilBrownea03aff2006-01-06 00:20:34 -0800950 paddr = kmap_atomic(page, KM_USER0);
951 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -0700952 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -0800953 kunmap_atomic(paddr, KM_USER0);
NeilBrown77ad4bc2005-06-21 17:17:21 -0700954 ret = write_page(bitmap, page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700955 if (ret) {
NeilBrown32a76272005-06-21 17:17:14 -0700956 /* release, page not in filemap yet */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800957 put_page(page);
NeilBrown32a76272005-06-21 17:17:14 -0700958 goto out;
959 }
960 }
961
962 bitmap->filemap[bitmap->file_pages++] = page;
NeilBrownab6085c2007-05-23 13:58:10 -0700963 bitmap->last_page_size = count;
NeilBrown32a76272005-06-21 17:17:14 -0700964 }
NeilBrownea03aff2006-01-06 00:20:34 -0800965 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800966 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -0800967 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -0800968 else
NeilBrownea03aff2006-01-06 00:20:34 -0800969 b = ext2_test_bit(bit, paddr);
970 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800971 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -0700972 /* if the disk bit is set, set the memory bit */
NeilBrown6a079972005-09-09 16:23:44 -0700973 bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
974 ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
975 );
NeilBrown32a76272005-06-21 17:17:14 -0700976 bit_cnt++;
NeilBrown6a079972005-09-09 16:23:44 -0700977 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -0700978 }
NeilBrown32a76272005-06-21 17:17:14 -0700979 }
980
981 /* everything went OK */
982 ret = 0;
983 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
984
NeilBrown32a76272005-06-21 17:17:14 -0700985 if (bit_cnt) { /* Kick recovery if any bits were set */
986 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
987 md_wakeup_thread(bitmap->mddev->thread);
988 }
989
990out:
991 printk(KERN_INFO "%s: bitmap initialized from disk: "
992 "read %lu/%lu pages, set %lu bits, status: %d\n",
993 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
994
995 return ret;
996}
997
NeilBrowna654b9d82005-06-21 17:17:27 -0700998void bitmap_write_all(struct bitmap *bitmap)
999{
1000 /* We don't actually write all bitmap blocks here,
1001 * just flag them as needing to be written
1002 */
NeilBrownec7a3192006-06-26 00:27:45 -07001003 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001004
NeilBrownec7a3192006-06-26 00:27:45 -07001005 for (i=0; i < bitmap->file_pages; i++)
1006 set_page_attr(bitmap, bitmap->filemap[i],
1007 BITMAP_PAGE_NEEDWRITE);
NeilBrowna654b9d82005-06-21 17:17:27 -07001008}
1009
NeilBrown32a76272005-06-21 17:17:14 -07001010
1011static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1012{
1013 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1014 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1015 bitmap->bp[page].count += inc;
1016/*
1017 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1018 (unsigned long long)offset, inc, bitmap->bp[page].count);
1019*/
1020 bitmap_checkfree(bitmap, page);
1021}
1022static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1023 sector_t offset, int *blocks,
1024 int create);
1025
1026/*
1027 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1028 * out to disk
1029 */
1030
1031int bitmap_daemon_work(struct bitmap *bitmap)
1032{
NeilBrownaa3163f2005-06-21 17:17:22 -07001033 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001034 unsigned long flags;
1035 struct page *page = NULL, *lastpage = NULL;
1036 int err = 0;
1037 int blocks;
NeilBrownea03aff2006-01-06 00:20:34 -08001038 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001039
1040 if (bitmap == NULL)
1041 return 0;
1042 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
1043 return 0;
1044 bitmap->daemon_lastrun = jiffies;
1045
1046 for (j = 0; j < bitmap->chunks; j++) {
1047 bitmap_counter_t *bmc;
1048 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -07001049 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -07001050 /* error or shutdown */
1051 spin_unlock_irqrestore(&bitmap->lock, flags);
1052 break;
1053 }
1054
1055 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001056
1057 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001058 /* skip this page unless it's marked as needing cleaning */
NeilBrownec7a3192006-06-26 00:27:45 -07001059 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1060 int need_write = test_page_attr(bitmap, page,
1061 BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001062 if (need_write)
NeilBrownaa3163f2005-06-21 17:17:22 -07001063 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001064
NeilBrownaa3163f2005-06-21 17:17:22 -07001065 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001066 if (need_write) {
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001067 switch (write_page(bitmap, page, 0)) {
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001068 case 0:
1069 break;
1070 default:
NeilBrownaa3163f2005-06-21 17:17:22 -07001071 bitmap_file_kick(bitmap);
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001072 }
NeilBrownaa3163f2005-06-21 17:17:22 -07001073 }
1074 continue;
1075 }
1076
NeilBrown32a76272005-06-21 17:17:14 -07001077 /* grab the new page, sync and release the old */
NeilBrown32a76272005-06-21 17:17:14 -07001078 if (lastpage != NULL) {
NeilBrownec7a3192006-06-26 00:27:45 -07001079 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001080 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1081 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown77ad4bc2005-06-21 17:17:21 -07001082 err = write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001083 } else {
1084 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1085 spin_unlock_irqrestore(&bitmap->lock, flags);
1086 }
NeilBrown32a76272005-06-21 17:17:14 -07001087 if (err)
1088 bitmap_file_kick(bitmap);
1089 } else
1090 spin_unlock_irqrestore(&bitmap->lock, flags);
1091 lastpage = page;
NeilBrown32a76272005-06-21 17:17:14 -07001092/*
1093 printk("bitmap clean at page %lu\n", j);
1094*/
1095 spin_lock_irqsave(&bitmap->lock, flags);
1096 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1097 }
1098 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1099 &blocks, 0);
1100 if (bmc) {
1101/*
1102 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1103*/
1104 if (*bmc == 2) {
1105 *bmc=1; /* maybe clear the bit next time */
1106 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1107 } else if (*bmc == 1) {
1108 /* we can clear the bit */
1109 *bmc = 0;
1110 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1111 -1);
1112
1113 /* clear the bit */
NeilBrownea03aff2006-01-06 00:20:34 -08001114 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001115 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001116 clear_bit(file_page_offset(j), paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001117 else
NeilBrownea03aff2006-01-06 00:20:34 -08001118 ext2_clear_bit(file_page_offset(j), paddr);
1119 kunmap_atomic(paddr, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -07001120 }
1121 }
1122 spin_unlock_irqrestore(&bitmap->lock, flags);
1123 }
1124
1125 /* now sync the final page */
1126 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001127 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001128 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001129 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1130 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown77ad4bc2005-06-21 17:17:21 -07001131 err = write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001132 } else {
1133 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1134 spin_unlock_irqrestore(&bitmap->lock, flags);
1135 }
NeilBrown32a76272005-06-21 17:17:14 -07001136 }
1137
1138 return err;
1139}
1140
NeilBrown32a76272005-06-21 17:17:14 -07001141static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1142 sector_t offset, int *blocks,
1143 int create)
1144{
1145 /* If 'create', we might release the lock and reclaim it.
1146 * The lock must have been taken with interrupts enabled.
1147 * If !create, we don't release the lock.
1148 */
1149 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1150 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1151 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1152 sector_t csize;
1153
1154 if (bitmap_checkpage(bitmap, page, create) < 0) {
1155 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1156 *blocks = csize - (offset & (csize- 1));
1157 return NULL;
1158 }
1159 /* now locked ... */
1160
1161 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1162 /* should we use the first or second counter field
1163 * of the hijacked pointer? */
1164 int hi = (pageoff > PAGE_COUNTER_MASK);
1165 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1166 PAGE_COUNTER_SHIFT - 1);
1167 *blocks = csize - (offset & (csize- 1));
1168 return &((bitmap_counter_t *)
1169 &bitmap->bp[page].map)[hi];
1170 } else { /* page is allocated */
1171 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1172 *blocks = csize - (offset & (csize- 1));
1173 return (bitmap_counter_t *)
1174 &(bitmap->bp[page].map[pageoff]);
1175 }
1176}
1177
NeilBrown4b6d2872005-09-09 16:23:47 -07001178int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001179{
1180 if (!bitmap) return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001181
1182 if (behind) {
1183 atomic_inc(&bitmap->behind_writes);
1184 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1185 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1186 }
1187
NeilBrown32a76272005-06-21 17:17:14 -07001188 while (sectors) {
1189 int blocks;
1190 bitmap_counter_t *bmc;
1191
1192 spin_lock_irq(&bitmap->lock);
1193 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1194 if (!bmc) {
1195 spin_unlock_irq(&bitmap->lock);
1196 return 0;
1197 }
1198
Neil Brownda6e1a32007-02-08 14:20:37 -08001199 if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
1200 DEFINE_WAIT(__wait);
1201 /* note that it is safe to do the prepare_to_wait
1202 * after the test as long as we do it before dropping
1203 * the spinlock.
1204 */
1205 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1206 TASK_UNINTERRUPTIBLE);
1207 spin_unlock_irq(&bitmap->lock);
1208 bitmap->mddev->queue
1209 ->unplug_fn(bitmap->mddev->queue);
1210 schedule();
1211 finish_wait(&bitmap->overflow_wait, &__wait);
1212 continue;
1213 }
1214
NeilBrown32a76272005-06-21 17:17:14 -07001215 switch(*bmc) {
1216 case 0:
1217 bitmap_file_set_bit(bitmap, offset);
1218 bitmap_count_page(bitmap,offset, 1);
1219 blk_plug_device(bitmap->mddev->queue);
1220 /* fall through */
1221 case 1:
1222 *bmc = 2;
1223 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001224
NeilBrown32a76272005-06-21 17:17:14 -07001225 (*bmc)++;
1226
1227 spin_unlock_irq(&bitmap->lock);
1228
1229 offset += blocks;
1230 if (sectors > blocks)
1231 sectors -= blocks;
1232 else sectors = 0;
1233 }
1234 return 0;
1235}
1236
1237void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001238 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001239{
1240 if (!bitmap) return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001241 if (behind) {
1242 atomic_dec(&bitmap->behind_writes);
1243 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1244 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1245 }
1246
NeilBrown32a76272005-06-21 17:17:14 -07001247 while (sectors) {
1248 int blocks;
1249 unsigned long flags;
1250 bitmap_counter_t *bmc;
1251
1252 spin_lock_irqsave(&bitmap->lock, flags);
1253 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1254 if (!bmc) {
1255 spin_unlock_irqrestore(&bitmap->lock, flags);
1256 return;
1257 }
1258
1259 if (!success && ! (*bmc & NEEDED_MASK))
1260 *bmc |= NEEDED_MASK;
1261
Neil Brownda6e1a32007-02-08 14:20:37 -08001262 if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
1263 wake_up(&bitmap->overflow_wait);
1264
NeilBrown32a76272005-06-21 17:17:14 -07001265 (*bmc)--;
1266 if (*bmc <= 2) {
1267 set_page_attr(bitmap,
1268 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1269 BITMAP_PAGE_CLEAN);
1270 }
1271 spin_unlock_irqrestore(&bitmap->lock, flags);
1272 offset += blocks;
1273 if (sectors > blocks)
1274 sectors -= blocks;
1275 else sectors = 0;
1276 }
1277}
1278
NeilBrown6a806c52005-07-15 03:56:35 -07001279int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1280 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001281{
1282 bitmap_counter_t *bmc;
1283 int rv;
1284 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1285 *blocks = 1024;
1286 return 1; /* always resync if no bitmap */
1287 }
1288 spin_lock_irq(&bitmap->lock);
1289 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1290 rv = 0;
1291 if (bmc) {
1292 /* locked */
1293 if (RESYNC(*bmc))
1294 rv = 1;
1295 else if (NEEDED(*bmc)) {
1296 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001297 if (!degraded) { /* don't set/clear bits if degraded */
1298 *bmc |= RESYNC_MASK;
1299 *bmc &= ~NEEDED_MASK;
1300 }
NeilBrown32a76272005-06-21 17:17:14 -07001301 }
1302 }
1303 spin_unlock_irq(&bitmap->lock);
1304 return rv;
1305}
1306
1307void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1308{
1309 bitmap_counter_t *bmc;
1310 unsigned long flags;
1311/*
1312 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1313*/ if (bitmap == NULL) {
1314 *blocks = 1024;
1315 return;
1316 }
1317 spin_lock_irqsave(&bitmap->lock, flags);
1318 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1319 if (bmc == NULL)
1320 goto unlock;
1321 /* locked */
1322/*
1323 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1324*/
1325 if (RESYNC(*bmc)) {
1326 *bmc &= ~RESYNC_MASK;
1327
1328 if (!NEEDED(*bmc) && aborted)
1329 *bmc |= NEEDED_MASK;
1330 else {
1331 if (*bmc <= 2) {
1332 set_page_attr(bitmap,
1333 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1334 BITMAP_PAGE_CLEAN);
1335 }
1336 }
1337 }
1338 unlock:
1339 spin_unlock_irqrestore(&bitmap->lock, flags);
1340}
1341
1342void bitmap_close_sync(struct bitmap *bitmap)
1343{
1344 /* Sync has finished, and any bitmap chunks that weren't synced
1345 * properly have been aborted. It remains to us to clear the
1346 * RESYNC bit wherever it is still on
1347 */
1348 sector_t sector = 0;
1349 int blocks;
1350 if (!bitmap) return;
1351 while (sector < bitmap->mddev->resync_max_sectors) {
1352 bitmap_end_sync(bitmap, sector, &blocks, 0);
1353/*
1354 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1355 (unsigned long long)sector, blocks);
1356*/ sector += blocks;
1357 }
1358}
1359
NeilBrown6a079972005-09-09 16:23:44 -07001360static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001361{
1362 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001363 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001364 * be 0 at this point
1365 */
NeilBrown193f1c92005-08-04 12:53:33 -07001366
1367 int secs;
1368 bitmap_counter_t *bmc;
1369 spin_lock_irq(&bitmap->lock);
1370 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1371 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001372 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001373 return;
NeilBrown32a76272005-06-21 17:17:14 -07001374 }
NeilBrown193f1c92005-08-04 12:53:33 -07001375 if (! *bmc) {
1376 struct page *page;
NeilBrown6a079972005-09-09 16:23:44 -07001377 *bmc = 1 | (needed?NEEDED_MASK:0);
NeilBrown193f1c92005-08-04 12:53:33 -07001378 bitmap_count_page(bitmap, offset, 1);
1379 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1380 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1381 }
1382 spin_unlock_irq(&bitmap->lock);
1383
NeilBrown32a76272005-06-21 17:17:14 -07001384}
1385
Paul Clements9b1d1da2006-10-03 01:15:49 -07001386/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1387void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1388{
1389 unsigned long chunk;
1390
1391 for (chunk = s; chunk <= e; chunk++) {
1392 sector_t sec = chunk << CHUNK_BLOCK_SHIFT(bitmap);
1393 bitmap_set_memory_bits(bitmap, sec, 1);
1394 bitmap_file_set_bit(bitmap, sec);
1395 }
1396}
1397
NeilBrown32a76272005-06-21 17:17:14 -07001398/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001399 * flush out any pending updates
1400 */
1401void bitmap_flush(mddev_t *mddev)
1402{
1403 struct bitmap *bitmap = mddev->bitmap;
1404 int sleep;
1405
1406 if (!bitmap) /* there was no bitmap */
1407 return;
1408
1409 /* run the daemon_work three time to ensure everything is flushed
1410 * that can be
1411 */
1412 sleep = bitmap->daemon_sleep;
1413 bitmap->daemon_sleep = 0;
1414 bitmap_daemon_work(bitmap);
1415 bitmap_daemon_work(bitmap);
1416 bitmap_daemon_work(bitmap);
1417 bitmap->daemon_sleep = sleep;
1418 bitmap_update_sb(bitmap);
1419}
1420
1421/*
NeilBrown32a76272005-06-21 17:17:14 -07001422 * free memory that was allocated
1423 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001424static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001425{
1426 unsigned long k, pages;
1427 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001428
1429 if (!bitmap) /* there was no bitmap */
1430 return;
1431
NeilBrown32a76272005-06-21 17:17:14 -07001432 /* release the bitmap file and kill the daemon */
1433 bitmap_file_put(bitmap);
1434
1435 bp = bitmap->bp;
1436 pages = bitmap->pages;
1437
1438 /* free all allocated memory */
1439
NeilBrown32a76272005-06-21 17:17:14 -07001440 if (bp) /* deallocate the page memory */
1441 for (k = 0; k < pages; k++)
1442 if (bp[k].map && !bp[k].hijacked)
1443 kfree(bp[k].map);
1444 kfree(bp);
1445 kfree(bitmap);
1446}
NeilBrown3178b0d2005-09-09 16:23:50 -07001447void bitmap_destroy(mddev_t *mddev)
1448{
1449 struct bitmap *bitmap = mddev->bitmap;
1450
1451 if (!bitmap) /* there was no bitmap */
1452 return;
1453
1454 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownb15c2e52006-01-06 00:20:16 -08001455 if (mddev->thread)
1456 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001457
1458 bitmap_free(bitmap);
1459}
NeilBrown32a76272005-06-21 17:17:14 -07001460
1461/*
1462 * initialize the bitmap structure
1463 * if this returns an error, bitmap_destroy must be called to do clean up
1464 */
1465int bitmap_create(mddev_t *mddev)
1466{
1467 struct bitmap *bitmap;
1468 unsigned long blocks = mddev->resync_max_sectors;
1469 unsigned long chunks;
1470 unsigned long pages;
1471 struct file *file = mddev->bitmap_file;
1472 int err;
NeilBrown6a079972005-09-09 16:23:44 -07001473 sector_t start;
NeilBrown32a76272005-06-21 17:17:14 -07001474
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001475 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001476
NeilBrowna654b9d82005-06-21 17:17:27 -07001477 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001478 return 0;
1479
NeilBrowna654b9d82005-06-21 17:17:27 -07001480 BUG_ON(file && mddev->bitmap_offset);
1481
NeilBrown9ffae0c2006-01-06 00:20:32 -08001482 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001483 if (!bitmap)
1484 return -ENOMEM;
1485
NeilBrown32a76272005-06-21 17:17:14 -07001486 spin_lock_init(&bitmap->lock);
NeilBrownce25c312006-06-26 00:27:49 -07001487 atomic_set(&bitmap->pending_writes, 0);
1488 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001489 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001490
NeilBrown32a76272005-06-21 17:17:14 -07001491 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001492
NeilBrown32a76272005-06-21 17:17:14 -07001493 bitmap->file = file;
NeilBrowna654b9d82005-06-21 17:17:27 -07001494 bitmap->offset = mddev->bitmap_offset;
NeilBrownce25c312006-06-26 00:27:49 -07001495 if (file) {
1496 get_file(file);
Mark Fashehef51c972007-05-08 00:27:10 -07001497 do_sync_mapping_range(file->f_mapping, 0, LLONG_MAX,
1498 SYNC_FILE_RANGE_WAIT_BEFORE |
1499 SYNC_FILE_RANGE_WRITE |
1500 SYNC_FILE_RANGE_WAIT_AFTER);
NeilBrownce25c312006-06-26 00:27:49 -07001501 }
NeilBrown32a76272005-06-21 17:17:14 -07001502 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1503 err = bitmap_read_sb(bitmap);
1504 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001505 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001506
Paul Clementsa638b2d2006-10-03 01:16:01 -07001507 bitmap->chunkshift = ffz(~bitmap->chunksize);
NeilBrown32a76272005-06-21 17:17:14 -07001508
1509 /* now that chunksize and chunkshift are set, we can use these macros */
1510 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1511 CHUNK_BLOCK_RATIO(bitmap);
1512 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1513
1514 BUG_ON(!pages);
1515
1516 bitmap->chunks = chunks;
1517 bitmap->pages = pages;
1518 bitmap->missing_pages = pages;
1519 bitmap->counter_bits = COUNTER_BITS;
1520
1521 bitmap->syncchunk = ~0UL;
1522
Olaf Hering44456d32005-07-27 11:45:17 -07001523#ifdef INJECT_FATAL_FAULT_1
NeilBrown32a76272005-06-21 17:17:14 -07001524 bitmap->bp = NULL;
1525#else
NeilBrown9ffae0c2006-01-06 00:20:32 -08001526 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001527#endif
NeilBrown3178b0d2005-09-09 16:23:50 -07001528 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001529 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001530 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001531
NeilBrown32a76272005-06-21 17:17:14 -07001532 /* now that we have some pages available, initialize the in-memory
1533 * bitmap from the on-disk bitmap */
NeilBrown6a079972005-09-09 16:23:44 -07001534 start = 0;
1535 if (mddev->degraded == 0
1536 || bitmap->events_cleared == mddev->events)
1537 /* no need to keep dirty bits to optimise a re-add of a missing device */
1538 start = mddev->recovery_cp;
1539 err = bitmap_init_from_disk(bitmap, start);
NeilBrown193f1c92005-08-04 12:53:33 -07001540
NeilBrown32a76272005-06-21 17:17:14 -07001541 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001542 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001543
1544 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1545 pages, bmname(bitmap));
1546
NeilBrown3178b0d2005-09-09 16:23:50 -07001547 mddev->bitmap = bitmap;
1548
NeilBrownb15c2e52006-01-06 00:20:16 -08001549 mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1550
NeilBrown32a76272005-06-21 17:17:14 -07001551 return bitmap_update_sb(bitmap);
NeilBrown3178b0d2005-09-09 16:23:50 -07001552
1553 error:
1554 bitmap_free(bitmap);
1555 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001556}
1557
1558/* the bitmap API -- for raid personalities */
1559EXPORT_SYMBOL(bitmap_startwrite);
1560EXPORT_SYMBOL(bitmap_endwrite);
1561EXPORT_SYMBOL(bitmap_start_sync);
1562EXPORT_SYMBOL(bitmap_end_sync);
1563EXPORT_SYMBOL(bitmap_unplug);
1564EXPORT_SYMBOL(bitmap_close_sync);