blob: 0f1732570770cd3bda658da3577be02e3e3f2517 [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.
17 * allow bitmap to be mirrored with superblock (before or after...)
18 * allow hot-add to re-instate a current device.
19 * allow hot-add of bitmap after quiessing device
20 */
21
22#include <linux/module.h>
NeilBrown32a76272005-06-21 17:17:14 -070023#include <linux/errno.h>
24#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/config.h>
27#include <linux/timer.h>
28#include <linux/sched.h>
29#include <linux/list.h>
30#include <linux/file.h>
31#include <linux/mount.h>
32#include <linux/buffer_head.h>
33#include <linux/raid/md.h>
34#include <linux/raid/bitmap.h>
35
36/* debug macros */
37
38#define DEBUG 0
39
40#if DEBUG
41/* these are for debugging purposes only! */
42
43/* define one and only one of these */
44#define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
45#define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
46#define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
47#define INJECT_FAULTS_4 0 /* undef */
48#define INJECT_FAULTS_5 0 /* undef */
49#define INJECT_FAULTS_6 0
50
51/* if these are defined, the driver will fail! debug only */
52#define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
53#define INJECT_FATAL_FAULT_2 0 /* undef */
54#define INJECT_FATAL_FAULT_3 0 /* undef */
55#endif
56
57//#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
58#define DPRINTK(x...) do { } while(0)
59
60#ifndef PRINTK
61# if DEBUG > 0
62# define PRINTK(x...) printk(KERN_DEBUG x)
63# else
64# define PRINTK(x...)
65# endif
66#endif
67
68static inline char * bmname(struct bitmap *bitmap)
69{
70 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
71}
72
73
74/*
75 * test if the bitmap is active
76 */
77int bitmap_active(struct bitmap *bitmap)
78{
79 unsigned long flags;
80 int res = 0;
81
82 if (!bitmap)
83 return res;
84 spin_lock_irqsave(&bitmap->lock, flags);
85 res = bitmap->flags & BITMAP_ACTIVE;
86 spin_unlock_irqrestore(&bitmap->lock, flags);
87 return res;
88}
89
90#define WRITE_POOL_SIZE 256
NeilBrown32a76272005-06-21 17:17:14 -070091
92/*
93 * just a placeholder - calls kmalloc for bitmap pages
94 */
95static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
96{
97 unsigned char *page;
98
Olaf Hering44456d32005-07-27 11:45:17 -070099#ifdef INJECT_FAULTS_1
NeilBrown32a76272005-06-21 17:17:14 -0700100 page = NULL;
101#else
102 page = kmalloc(PAGE_SIZE, GFP_NOIO);
103#endif
104 if (!page)
105 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
106 else
NeilBrowna654b9d82005-06-21 17:17:27 -0700107 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
NeilBrown32a76272005-06-21 17:17:14 -0700108 bmname(bitmap), page);
109 return page;
110}
111
112/*
113 * for now just a placeholder -- just calls kfree for bitmap pages
114 */
115static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
116{
117 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
118 kfree(page);
119}
120
121/*
122 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
123 *
124 * 1) check to see if this page is allocated, if it's not then try to alloc
125 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
126 * page pointer directly as a counter
127 *
128 * if we find our page, we increment the page's refcount so that it stays
129 * allocated while we're using it
130 */
131static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
132{
133 unsigned char *mappage;
134
135 if (page >= bitmap->pages) {
136 printk(KERN_ALERT
137 "%s: invalid bitmap page request: %lu (> %lu)\n",
138 bmname(bitmap), page, bitmap->pages-1);
139 return -EINVAL;
140 }
141
142
143 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
144 return 0;
145
146 if (bitmap->bp[page].map) /* page is already allocated, just return */
147 return 0;
148
149 if (!create)
150 return -ENOENT;
151
152 spin_unlock_irq(&bitmap->lock);
153
154 /* this page has not been allocated yet */
155
156 if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
157 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
158 bmname(bitmap));
159 /* failed - set the hijacked flag so that we can use the
160 * pointer as a counter */
161 spin_lock_irq(&bitmap->lock);
162 if (!bitmap->bp[page].map)
163 bitmap->bp[page].hijacked = 1;
164 goto out;
165 }
166
167 /* got a page */
168
169 spin_lock_irq(&bitmap->lock);
170
171 /* recheck the page */
172
173 if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
174 /* somebody beat us to getting the page */
175 bitmap_free_page(bitmap, mappage);
176 return 0;
177 }
178
179 /* no page was in place and we have one, so install it */
180
181 memset(mappage, 0, PAGE_SIZE);
182 bitmap->bp[page].map = mappage;
183 bitmap->missing_pages--;
184out:
185 return 0;
186}
187
188
189/* if page is completely empty, put it back on the free list, or dealloc it */
190/* if page was hijacked, unmark the flag so it might get alloced next time */
191/* Note: lock should be held when calling this */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800192static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
NeilBrown32a76272005-06-21 17:17:14 -0700193{
194 char *ptr;
195
196 if (bitmap->bp[page].count) /* page is still busy */
197 return;
198
199 /* page is no longer in use, it can be released */
200
201 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
202 bitmap->bp[page].hijacked = 0;
203 bitmap->bp[page].map = NULL;
204 return;
205 }
206
207 /* normal case, free the page */
208
209#if 0
210/* actually ... let's not. We will probably need the page again exactly when
211 * memory is tight and we are flusing to disk
212 */
213 return;
214#else
215 ptr = bitmap->bp[page].map;
216 bitmap->bp[page].map = NULL;
217 bitmap->missing_pages++;
218 bitmap_free_page(bitmap, ptr);
219 return;
220#endif
221}
222
223
224/*
225 * bitmap file handling - read and write the bitmap file and its superblock
226 */
227
228/* copy the pathname of a file to a buffer */
229char *file_path(struct file *file, char *buf, int count)
230{
231 struct dentry *d;
232 struct vfsmount *v;
233
234 if (!buf)
235 return NULL;
236
237 d = file->f_dentry;
238 v = file->f_vfsmnt;
239
240 buf = d_path(d, v, buf, count);
241
242 return IS_ERR(buf) ? NULL : buf;
243}
244
245/*
246 * basic page I/O operations
247 */
248
NeilBrowna654b9d82005-06-21 17:17:27 -0700249/* IO operations when bitmap is stored near all superblocks */
250static struct page *read_sb_page(mddev_t *mddev, long offset, unsigned long index)
251{
252 /* choose a good rdev and read the page from there */
253
254 mdk_rdev_t *rdev;
255 struct list_head *tmp;
256 struct page *page = alloc_page(GFP_KERNEL);
257 sector_t target;
258
259 if (!page)
260 return ERR_PTR(-ENOMEM);
NeilBrowna654b9d82005-06-21 17:17:27 -0700261
NeilBrownab904d62005-09-09 16:23:52 -0700262 ITERATE_RDEV(mddev, rdev, tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800263 if (! test_bit(In_sync, &rdev->flags)
264 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700265 continue;
266
NeilBrowna654b9d82005-06-21 17:17:27 -0700267 target = (rdev->sb_offset << 1) + offset + index * (PAGE_SIZE/512);
268
NeilBrownab904d62005-09-09 16:23:52 -0700269 if (sync_page_io(rdev->bdev, target, PAGE_SIZE, page, READ)) {
270 page->index = index;
271 return page;
272 }
273 }
274 return ERR_PTR(-EIO);
NeilBrowna654b9d82005-06-21 17:17:27 -0700275
NeilBrowna654b9d82005-06-21 17:17:27 -0700276}
277
278static int write_sb_page(mddev_t *mddev, long offset, struct page *page, int wait)
279{
280 mdk_rdev_t *rdev;
281 struct list_head *tmp;
282
283 ITERATE_RDEV(mddev, rdev, tmp)
NeilBrownb2d444d2005-11-08 21:39:31 -0800284 if (test_bit(In_sync, &rdev->flags)
285 && !test_bit(Faulty, &rdev->flags))
NeilBrowna654b9d82005-06-21 17:17:27 -0700286 md_super_write(mddev, rdev,
287 (rdev->sb_offset<<1) + offset
288 + page->index * (PAGE_SIZE/512),
289 PAGE_SIZE,
290 page);
291
292 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800293 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700294 return 0;
295}
296
NeilBrown32a76272005-06-21 17:17:14 -0700297/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700298 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700299 */
NeilBrown77ad4bc2005-06-21 17:17:21 -0700300static int write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700301{
302 int ret = -ENOMEM;
303
NeilBrowna654b9d82005-06-21 17:17:27 -0700304 if (bitmap->file == NULL)
305 return write_sb_page(bitmap->mddev, bitmap->offset, page, wait);
306
NeilBrownc7084432006-01-06 00:20:45 -0800307 flush_dcache_page(page); /* make sure visible to anyone reading the file */
308
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700309 if (wait)
310 lock_page(page);
311 else {
312 if (TestSetPageLocked(page))
313 return -EAGAIN; /* already locked */
314 if (PageWriteback(page)) {
315 unlock_page(page);
316 return -EAGAIN;
317 }
318 }
NeilBrown32a76272005-06-21 17:17:14 -0700319
Neil Brown34ef75f2005-11-18 01:10:59 -0800320 ret = page->mapping->a_ops->prepare_write(bitmap->file, page, 0, PAGE_SIZE);
NeilBrown32a76272005-06-21 17:17:14 -0700321 if (!ret)
Neil Brown34ef75f2005-11-18 01:10:59 -0800322 ret = page->mapping->a_ops->commit_write(bitmap->file, page, 0,
NeilBrown32a76272005-06-21 17:17:14 -0700323 PAGE_SIZE);
324 if (ret) {
NeilBrown32a76272005-06-21 17:17:14 -0700325 unlock_page(page);
326 return ret;
327 }
328
329 set_page_dirty(page); /* force it to be written out */
NeilBrown77ad4bc2005-06-21 17:17:21 -0700330
331 if (!wait) {
NeilBrown0b79ccf2006-06-26 00:27:44 -0700332 /* add to list to be waited for */
NeilBrown77ad4bc2005-06-21 17:17:21 -0700333 struct page_list *item = mempool_alloc(bitmap->write_pool, GFP_NOIO);
334 item->page = page;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800335 get_page(page);
NeilBrown77ad4bc2005-06-21 17:17:21 -0700336 spin_lock(&bitmap->write_lock);
337 list_add(&item->list, &bitmap->complete_pages);
338 spin_unlock(&bitmap->write_lock);
NeilBrown77ad4bc2005-06-21 17:17:21 -0700339 }
NeilBrown32a76272005-06-21 17:17:14 -0700340 return write_one_page(page, wait);
341}
342
343/* read a page from a file, pinning it into cache, and return bytes_read */
344static struct page *read_page(struct file *file, unsigned long index,
345 unsigned long *bytes_read)
346{
347 struct inode *inode = file->f_mapping->host;
348 struct page *page = NULL;
349 loff_t isize = i_size_read(inode);
NeilBrown2d1f3b52006-01-06 00:20:31 -0800350 unsigned long end_index = isize >> PAGE_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700351
NeilBrown2d1f3b52006-01-06 00:20:31 -0800352 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
353 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700354
355 page = read_cache_page(inode->i_mapping, index,
356 (filler_t *)inode->i_mapping->a_ops->readpage, file);
357 if (IS_ERR(page))
358 goto out;
359 wait_on_page_locked(page);
360 if (!PageUptodate(page) || PageError(page)) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800361 put_page(page);
NeilBrown32a76272005-06-21 17:17:14 -0700362 page = ERR_PTR(-EIO);
363 goto out;
364 }
365
366 if (index > end_index) /* we have read beyond EOF */
367 *bytes_read = 0;
368 else if (index == end_index) /* possible short read */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800369 *bytes_read = isize & ~PAGE_MASK;
NeilBrown32a76272005-06-21 17:17:14 -0700370 else
NeilBrown2d1f3b52006-01-06 00:20:31 -0800371 *bytes_read = PAGE_SIZE; /* got a full page */
NeilBrown32a76272005-06-21 17:17:14 -0700372out:
373 if (IS_ERR(page))
374 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800375 (int)PAGE_SIZE,
376 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700377 PTR_ERR(page));
378 return page;
379}
380
381/*
382 * bitmap file superblock operations
383 */
384
385/* update the event counter and sync the superblock to disk */
386int bitmap_update_sb(struct bitmap *bitmap)
387{
388 bitmap_super_t *sb;
389 unsigned long flags;
390
391 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
392 return 0;
393 spin_lock_irqsave(&bitmap->lock, flags);
394 if (!bitmap->sb_page) { /* no superblock */
395 spin_unlock_irqrestore(&bitmap->lock, flags);
396 return 0;
397 }
NeilBrown32a76272005-06-21 17:17:14 -0700398 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800399 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700400 sb->events = cpu_to_le64(bitmap->mddev->events);
401 if (!bitmap->mddev->degraded)
402 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
NeilBrownea03aff2006-01-06 00:20:34 -0800403 kunmap_atomic(sb, KM_USER0);
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700404 return write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700405}
406
407/* print out the bitmap file superblock */
408void bitmap_print_sb(struct bitmap *bitmap)
409{
410 bitmap_super_t *sb;
411
412 if (!bitmap || !bitmap->sb_page)
413 return;
NeilBrownea03aff2006-01-06 00:20:34 -0800414 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700415 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700416 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
417 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
418 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700419 *(__u32 *)(sb->uuid+0),
420 *(__u32 *)(sb->uuid+4),
421 *(__u32 *)(sb->uuid+8),
422 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700423 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700424 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700425 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700426 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700427 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
428 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
429 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
430 printk(KERN_DEBUG " sync size: %llu KB\n",
431 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700432 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800433 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700434}
435
436/* read the superblock from the bitmap file and initialize some bitmap fields */
437static int bitmap_read_sb(struct bitmap *bitmap)
438{
439 char *reason = NULL;
440 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700441 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700442 unsigned long bytes_read;
443 unsigned long long events;
444 int err = -EINVAL;
445
446 /* page 0 is the superblock, read it... */
NeilBrowna654b9d82005-06-21 17:17:27 -0700447 if (bitmap->file)
448 bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read);
449 else {
450 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
451 bytes_read = PAGE_SIZE;
452 }
NeilBrown32a76272005-06-21 17:17:14 -0700453 if (IS_ERR(bitmap->sb_page)) {
454 err = PTR_ERR(bitmap->sb_page);
455 bitmap->sb_page = NULL;
456 return err;
457 }
458
NeilBrownea03aff2006-01-06 00:20:34 -0800459 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700460
461 if (bytes_read < sizeof(*sb)) { /* short read */
462 printk(KERN_INFO "%s: bitmap file superblock truncated\n",
463 bmname(bitmap));
464 err = -ENOSPC;
465 goto out;
466 }
467
468 chunksize = le32_to_cpu(sb->chunksize);
469 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
NeilBrown4b6d2872005-09-09 16:23:47 -0700470 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700471
472 /* verify that the bitmap-specific fields are valid */
473 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
474 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800475 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
476 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700477 reason = "unrecognized superblock version";
NeilBrown7dd5d342006-01-06 00:20:39 -0800478 else if (chunksize < PAGE_SIZE)
479 reason = "bitmap chunksize too small";
NeilBrown32a76272005-06-21 17:17:14 -0700480 else if ((1 << ffz(~chunksize)) != chunksize)
481 reason = "bitmap chunksize not a power of 2";
NeilBrown7dd5d342006-01-06 00:20:39 -0800482 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT / HZ)
483 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700484 else if (write_behind > COUNTER_MAX)
485 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700486 if (reason) {
487 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
488 bmname(bitmap), reason);
489 goto out;
490 }
491
492 /* keep the array size field of the bitmap superblock up to date */
493 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
494
495 if (!bitmap->mddev->persistent)
496 goto success;
497
498 /*
499 * if we have a persistent array superblock, compare the
500 * bitmap's UUID and event counter to the mddev's
501 */
502 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
503 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
504 bmname(bitmap));
505 goto out;
506 }
507 events = le64_to_cpu(sb->events);
508 if (events < bitmap->mddev->events) {
509 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
510 "-- forcing full recovery\n", bmname(bitmap), events,
511 (unsigned long long) bitmap->mddev->events);
512 sb->state |= BITMAP_STALE;
513 }
514success:
515 /* assign fields using values from superblock */
516 bitmap->chunksize = chunksize;
517 bitmap->daemon_sleep = daemon_sleep;
NeilBrown585f0dd2005-09-09 16:23:49 -0700518 bitmap->daemon_lastrun = jiffies;
NeilBrown4b6d2872005-09-09 16:23:47 -0700519 bitmap->max_write_behind = write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700520 bitmap->flags |= sb->state;
NeilBrownbd926c62005-11-08 21:39:32 -0800521 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
522 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700523 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown6a079972005-09-09 16:23:44 -0700524 if (sb->state & BITMAP_STALE)
525 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700526 err = 0;
527out:
NeilBrownea03aff2006-01-06 00:20:34 -0800528 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700529 if (err)
530 bitmap_print_sb(bitmap);
531 return err;
532}
533
534enum bitmap_mask_op {
535 MASK_SET,
536 MASK_UNSET
537};
538
539/* record the state of the bitmap in the superblock */
540static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
541 enum bitmap_mask_op op)
542{
543 bitmap_super_t *sb;
544 unsigned long flags;
545
546 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800547 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700548 spin_unlock_irqrestore(&bitmap->lock, flags);
549 return;
550 }
NeilBrown2d1f3b52006-01-06 00:20:31 -0800551 get_page(bitmap->sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700552 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800553 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700554 switch (op) {
555 case MASK_SET: sb->state |= bits;
556 break;
557 case MASK_UNSET: sb->state &= ~bits;
558 break;
559 default: BUG();
560 }
NeilBrownea03aff2006-01-06 00:20:34 -0800561 kunmap_atomic(sb, KM_USER0);
NeilBrown2d1f3b52006-01-06 00:20:31 -0800562 put_page(bitmap->sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700563}
564
565/*
566 * general bitmap file operations
567 */
568
569/* calculate the index of the page that contains this bit */
570static inline unsigned long file_page_index(unsigned long chunk)
571{
572 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
573}
574
575/* calculate the (bit) offset of this bit within a page */
576static inline unsigned long file_page_offset(unsigned long chunk)
577{
578 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
579}
580
581/*
582 * return a pointer to the page in the filemap that contains the given bit
583 *
584 * this lookup is complicated by the fact that the bitmap sb might be exactly
585 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
586 * 0 or page 1
587 */
588static inline struct page *filemap_get_page(struct bitmap *bitmap,
589 unsigned long chunk)
590{
591 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
592}
593
594
595static void bitmap_file_unmap(struct bitmap *bitmap)
596{
597 struct page **map, *sb_page;
598 unsigned long *attr;
599 int pages;
600 unsigned long flags;
601
602 spin_lock_irqsave(&bitmap->lock, flags);
603 map = bitmap->filemap;
604 bitmap->filemap = NULL;
605 attr = bitmap->filemap_attr;
606 bitmap->filemap_attr = NULL;
607 pages = bitmap->file_pages;
608 bitmap->file_pages = 0;
609 sb_page = bitmap->sb_page;
610 bitmap->sb_page = NULL;
611 spin_unlock_irqrestore(&bitmap->lock, flags);
612
613 while (pages--)
614 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800615 put_page(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700616 kfree(map);
617 kfree(attr);
618
NeilBrown1345b1d2006-01-06 00:20:40 -0800619 safe_put_page(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700620}
621
NeilBrown32a76272005-06-21 17:17:14 -0700622/* dequeue the next item in a page list -- don't call from irq context */
NeilBrown77ad4bc2005-06-21 17:17:21 -0700623static struct page_list *dequeue_page(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700624{
625 struct page_list *item = NULL;
NeilBrown77ad4bc2005-06-21 17:17:21 -0700626 struct list_head *head = &bitmap->complete_pages;
NeilBrown32a76272005-06-21 17:17:14 -0700627
628 spin_lock(&bitmap->write_lock);
629 if (list_empty(head))
630 goto out;
631 item = list_entry(head->prev, struct page_list, list);
632 list_del(head->prev);
633out:
634 spin_unlock(&bitmap->write_lock);
635 return item;
636}
637
638static void drain_write_queues(struct bitmap *bitmap)
639{
NeilBrown32a76272005-06-21 17:17:14 -0700640 struct page_list *item;
NeilBrown32a76272005-06-21 17:17:14 -0700641
NeilBrown77ad4bc2005-06-21 17:17:21 -0700642 while ((item = dequeue_page(bitmap))) {
643 /* don't bother to wait */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800644 put_page(item->page);
NeilBrown77ad4bc2005-06-21 17:17:21 -0700645 mempool_free(item, bitmap->write_pool);
NeilBrown32a76272005-06-21 17:17:14 -0700646 }
NeilBrown32a76272005-06-21 17:17:14 -0700647}
648
649static void bitmap_file_put(struct bitmap *bitmap)
650{
651 struct file *file;
652 struct inode *inode;
653 unsigned long flags;
654
655 spin_lock_irqsave(&bitmap->lock, flags);
656 file = bitmap->file;
657 bitmap->file = NULL;
658 spin_unlock_irqrestore(&bitmap->lock, flags);
659
NeilBrown32a76272005-06-21 17:17:14 -0700660 drain_write_queues(bitmap);
661
662 bitmap_file_unmap(bitmap);
663
664 if (file) {
665 inode = file->f_mapping->host;
666 spin_lock(&inode->i_lock);
667 atomic_set(&inode->i_writecount, 1); /* allow writes again */
668 spin_unlock(&inode->i_lock);
669 fput(file);
670 }
671}
672
673
674/*
675 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
676 * then it is no longer reliable, so we stop using it and we mark the file
677 * as failed in the superblock
678 */
679static void bitmap_file_kick(struct bitmap *bitmap)
680{
681 char *path, *ptr = NULL;
682
683 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
684 bitmap_update_sb(bitmap);
685
NeilBrowna654b9d82005-06-21 17:17:27 -0700686 if (bitmap->file) {
687 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
688 if (path)
689 ptr = file_path(bitmap->file, path, PAGE_SIZE);
NeilBrown32a76272005-06-21 17:17:14 -0700690
NeilBrowna654b9d82005-06-21 17:17:27 -0700691 printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
692 bmname(bitmap), ptr ? ptr : "");
NeilBrown32a76272005-06-21 17:17:14 -0700693
NeilBrowna654b9d82005-06-21 17:17:27 -0700694 kfree(path);
695 }
NeilBrown32a76272005-06-21 17:17:14 -0700696
697 bitmap_file_put(bitmap);
698
699 return;
700}
701
702enum bitmap_page_attr {
703 BITMAP_PAGE_DIRTY = 1, // there are set bits that need to be synced
704 BITMAP_PAGE_CLEAN = 2, // there are bits that might need to be cleared
705 BITMAP_PAGE_NEEDWRITE=4, // there are cleared bits that need to be synced
706};
707
708static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
709 enum bitmap_page_attr attr)
710{
711 bitmap->filemap_attr[page->index] |= attr;
712}
713
714static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
715 enum bitmap_page_attr attr)
716{
717 bitmap->filemap_attr[page->index] &= ~attr;
718}
719
NeilBrownec7a3192006-06-26 00:27:45 -0700720static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
721 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700722{
NeilBrownec7a3192006-06-26 00:27:45 -0700723 return bitmap->filemap_attr[page->index] & attr;
NeilBrown32a76272005-06-21 17:17:14 -0700724}
725
726/*
727 * bitmap_file_set_bit -- called before performing a write to the md device
728 * to set (and eventually sync) a particular bit in the bitmap file
729 *
730 * we set the bit immediately, then we record the page number so that
731 * when an unplug occurs, we can flush the dirty pages out to disk
732 */
733static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
734{
735 unsigned long bit;
736 struct page *page;
737 void *kaddr;
738 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
739
NeilBrowna654b9d82005-06-21 17:17:27 -0700740 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700741 return;
742 }
743
744 page = filemap_get_page(bitmap, chunk);
745 bit = file_page_offset(chunk);
746
747
748 /* make sure the page stays cached until it gets written out */
NeilBrownec7a3192006-06-26 00:27:45 -0700749 if (! test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY))
NeilBrown2d1f3b52006-01-06 00:20:31 -0800750 get_page(page);
NeilBrown32a76272005-06-21 17:17:14 -0700751
752 /* set the bit */
753 kaddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800754 if (bitmap->flags & BITMAP_HOSTENDIAN)
755 set_bit(bit, kaddr);
756 else
757 ext2_set_bit(bit, kaddr);
NeilBrown32a76272005-06-21 17:17:14 -0700758 kunmap_atomic(kaddr, KM_USER0);
759 PRINTK("set file bit %lu page %lu\n", bit, page->index);
760
761 /* record page number so it gets flushed to disk when unplug occurs */
762 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
763
764}
765
NeilBrown0b79ccf2006-06-26 00:27:44 -0700766static void bitmap_writeback(struct bitmap *bitmap);
767
NeilBrown32a76272005-06-21 17:17:14 -0700768/* this gets called when the md device is ready to unplug its underlying
769 * (slave) device queues -- before we let any writes go down, we need to
770 * sync the dirty pages of the bitmap file to disk */
771int bitmap_unplug(struct bitmap *bitmap)
772{
NeilBrownec7a3192006-06-26 00:27:45 -0700773 unsigned long i, flags;
774 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700775 struct page *page;
776 int wait = 0;
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700777 int err;
NeilBrown32a76272005-06-21 17:17:14 -0700778
779 if (!bitmap)
780 return 0;
781
782 /* look at each page to see if there are any set bits that need to be
783 * flushed out to disk */
784 for (i = 0; i < bitmap->file_pages; i++) {
785 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700786 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700787 spin_unlock_irqrestore(&bitmap->lock, flags);
788 return 0;
789 }
790 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700791 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
792 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700793 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
794 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700795 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700796 wait = 1;
797 spin_unlock_irqrestore(&bitmap->lock, flags);
798
NeilBrownec7a3192006-06-26 00:27:45 -0700799 if (dirty | need_write) {
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700800 err = write_page(bitmap, page, 0);
801 if (err == -EAGAIN) {
NeilBrownec7a3192006-06-26 00:27:45 -0700802 if (dirty)
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700803 err = write_page(bitmap, page, 1);
804 else
805 err = 0;
806 }
807 if (err)
NeilBrownbfb39fb2005-06-21 17:17:20 -0700808 return 1;
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700809 }
NeilBrown32a76272005-06-21 17:17:14 -0700810 }
811 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700812 if (bitmap->file)
813 bitmap_writeback(bitmap);
814 else
NeilBrowna9701a32005-11-08 21:39:34 -0800815 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700816 }
817 return 0;
818}
819
NeilBrown6a079972005-09-09 16:23:44 -0700820static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700821/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
822 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
823 * memory mapping of the bitmap file
824 * Special cases:
825 * if there's no bitmap file, or if the bitmap file had been
826 * previously kicked from the array, we mark all the bits as
827 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700828 *
829 * We ignore all bits for sectors that end earlier than 'start'.
830 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700831 */
NeilBrown6a079972005-09-09 16:23:44 -0700832static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700833{
834 unsigned long i, chunks, index, oldindex, bit;
835 struct page *page = NULL, *oldpage = NULL;
836 unsigned long num_pages, bit_cnt = 0;
837 struct file *file;
838 unsigned long bytes, offset, dummy;
839 int outofdate;
840 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800841 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700842
843 chunks = bitmap->chunks;
844 file = bitmap->file;
845
NeilBrowna654b9d82005-06-21 17:17:27 -0700846 BUG_ON(!file && !bitmap->offset);
NeilBrown32a76272005-06-21 17:17:14 -0700847
Olaf Hering44456d32005-07-27 11:45:17 -0700848#ifdef INJECT_FAULTS_3
NeilBrown32a76272005-06-21 17:17:14 -0700849 outofdate = 1;
850#else
851 outofdate = bitmap->flags & BITMAP_STALE;
852#endif
853 if (outofdate)
854 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
855 "recovery\n", bmname(bitmap));
856
857 bytes = (chunks + 7) / 8;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700858
NeilBrowncdbb4cc2005-06-21 17:17:18 -0700859 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700860
NeilBrowna654b9d82005-06-21 17:17:27 -0700861 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
NeilBrown32a76272005-06-21 17:17:14 -0700862 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
863 bmname(bitmap),
864 (unsigned long) i_size_read(file->f_mapping->host),
865 bytes + sizeof(bitmap_super_t));
866 goto out;
867 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700868
869 ret = -ENOMEM;
870
NeilBrown32a76272005-06-21 17:17:14 -0700871 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700872 if (!bitmap->filemap)
NeilBrown32a76272005-06-21 17:17:14 -0700873 goto out;
NeilBrown32a76272005-06-21 17:17:14 -0700874
NeilBrown9ffae0c2006-01-06 00:20:32 -0800875 bitmap->filemap_attr = kzalloc(sizeof(long) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700876 if (!bitmap->filemap_attr)
NeilBrown32a76272005-06-21 17:17:14 -0700877 goto out;
NeilBrown32a76272005-06-21 17:17:14 -0700878
NeilBrown32a76272005-06-21 17:17:14 -0700879 oldindex = ~0L;
880
881 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -0800882 int b;
NeilBrown32a76272005-06-21 17:17:14 -0700883 index = file_page_index(i);
884 bit = file_page_offset(i);
885 if (index != oldindex) { /* this is a new page, read it in */
886 /* unmap the old page, we're done with it */
NeilBrown32a76272005-06-21 17:17:14 -0700887 if (index == 0) {
888 /*
889 * if we're here then the superblock page
890 * contains some bits (PAGE_SIZE != sizeof sb)
891 * we've already read it in, so just use it
892 */
893 page = bitmap->sb_page;
894 offset = sizeof(bitmap_super_t);
NeilBrowna654b9d82005-06-21 17:17:27 -0700895 } else if (file) {
NeilBrown32a76272005-06-21 17:17:14 -0700896 page = read_page(file, index, &dummy);
NeilBrowna654b9d82005-06-21 17:17:27 -0700897 offset = 0;
898 } else {
899 page = read_sb_page(bitmap->mddev, bitmap->offset, index);
NeilBrown32a76272005-06-21 17:17:14 -0700900 offset = 0;
901 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700902 if (IS_ERR(page)) { /* read error */
903 ret = PTR_ERR(page);
904 goto out;
905 }
906
NeilBrown32a76272005-06-21 17:17:14 -0700907 oldindex = index;
908 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -0700909
910 if (outofdate) {
911 /*
912 * if bitmap is out of date, dirty the
913 * whole page and write it out
914 */
NeilBrownea03aff2006-01-06 00:20:34 -0800915 paddr = kmap_atomic(page, KM_USER0);
916 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -0700917 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -0800918 kunmap_atomic(paddr, KM_USER0);
NeilBrown77ad4bc2005-06-21 17:17:21 -0700919 ret = write_page(bitmap, page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700920 if (ret) {
NeilBrown32a76272005-06-21 17:17:14 -0700921 /* release, page not in filemap yet */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800922 put_page(page);
NeilBrown32a76272005-06-21 17:17:14 -0700923 goto out;
924 }
925 }
926
927 bitmap->filemap[bitmap->file_pages++] = page;
928 }
NeilBrownea03aff2006-01-06 00:20:34 -0800929 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800930 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -0800931 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -0800932 else
NeilBrownea03aff2006-01-06 00:20:34 -0800933 b = ext2_test_bit(bit, paddr);
934 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800935 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -0700936 /* if the disk bit is set, set the memory bit */
NeilBrown6a079972005-09-09 16:23:44 -0700937 bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
938 ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
939 );
NeilBrown32a76272005-06-21 17:17:14 -0700940 bit_cnt++;
NeilBrown6a079972005-09-09 16:23:44 -0700941 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -0700942 }
NeilBrown32a76272005-06-21 17:17:14 -0700943 }
944
945 /* everything went OK */
946 ret = 0;
947 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
948
NeilBrown32a76272005-06-21 17:17:14 -0700949 if (bit_cnt) { /* Kick recovery if any bits were set */
950 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
951 md_wakeup_thread(bitmap->mddev->thread);
952 }
953
954out:
955 printk(KERN_INFO "%s: bitmap initialized from disk: "
956 "read %lu/%lu pages, set %lu bits, status: %d\n",
957 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
958
959 return ret;
960}
961
NeilBrowna654b9d82005-06-21 17:17:27 -0700962void bitmap_write_all(struct bitmap *bitmap)
963{
964 /* We don't actually write all bitmap blocks here,
965 * just flag them as needing to be written
966 */
NeilBrownec7a3192006-06-26 00:27:45 -0700967 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -0700968
NeilBrownec7a3192006-06-26 00:27:45 -0700969 for (i=0; i < bitmap->file_pages; i++)
970 set_page_attr(bitmap, bitmap->filemap[i],
971 BITMAP_PAGE_NEEDWRITE);
NeilBrowna654b9d82005-06-21 17:17:27 -0700972}
973
NeilBrown32a76272005-06-21 17:17:14 -0700974
975static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
976{
977 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
978 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
979 bitmap->bp[page].count += inc;
980/*
981 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
982 (unsigned long long)offset, inc, bitmap->bp[page].count);
983*/
984 bitmap_checkfree(bitmap, page);
985}
986static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
987 sector_t offset, int *blocks,
988 int create);
989
990/*
991 * bitmap daemon -- periodically wakes up to clean bits and flush pages
992 * out to disk
993 */
994
995int bitmap_daemon_work(struct bitmap *bitmap)
996{
NeilBrownaa3163f2005-06-21 17:17:22 -0700997 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -0700998 unsigned long flags;
999 struct page *page = NULL, *lastpage = NULL;
1000 int err = 0;
1001 int blocks;
NeilBrownea03aff2006-01-06 00:20:34 -08001002 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001003
1004 if (bitmap == NULL)
1005 return 0;
1006 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
1007 return 0;
1008 bitmap->daemon_lastrun = jiffies;
1009
1010 for (j = 0; j < bitmap->chunks; j++) {
1011 bitmap_counter_t *bmc;
1012 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -07001013 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -07001014 /* error or shutdown */
1015 spin_unlock_irqrestore(&bitmap->lock, flags);
1016 break;
1017 }
1018
1019 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001020
1021 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001022 /* skip this page unless it's marked as needing cleaning */
NeilBrownec7a3192006-06-26 00:27:45 -07001023 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1024 int need_write = test_page_attr(bitmap, page,
1025 BITMAP_PAGE_NEEDWRITE);
1026 if (need_write) {
NeilBrown2d1f3b52006-01-06 00:20:31 -08001027 get_page(page);
NeilBrownaa3163f2005-06-21 17:17:22 -07001028 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1029 }
1030 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001031 if (need_write) {
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001032 switch (write_page(bitmap, page, 0)) {
1033 case -EAGAIN:
1034 set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1035 break;
1036 case 0:
1037 break;
1038 default:
NeilBrownaa3163f2005-06-21 17:17:22 -07001039 bitmap_file_kick(bitmap);
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001040 }
NeilBrown2d1f3b52006-01-06 00:20:31 -08001041 put_page(page);
NeilBrownaa3163f2005-06-21 17:17:22 -07001042 }
1043 continue;
1044 }
1045
NeilBrown32a76272005-06-21 17:17:14 -07001046 /* grab the new page, sync and release the old */
NeilBrown2d1f3b52006-01-06 00:20:31 -08001047 get_page(page);
NeilBrown32a76272005-06-21 17:17:14 -07001048 if (lastpage != NULL) {
NeilBrownec7a3192006-06-26 00:27:45 -07001049 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001050 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1051 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown77ad4bc2005-06-21 17:17:21 -07001052 err = write_page(bitmap, lastpage, 0);
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001053 if (err == -EAGAIN) {
1054 err = 0;
1055 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1056 }
NeilBrown32a76272005-06-21 17:17:14 -07001057 } else {
1058 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1059 spin_unlock_irqrestore(&bitmap->lock, flags);
1060 }
NeilBrown2d1f3b52006-01-06 00:20:31 -08001061 put_page(lastpage);
NeilBrown32a76272005-06-21 17:17:14 -07001062 if (err)
1063 bitmap_file_kick(bitmap);
1064 } else
1065 spin_unlock_irqrestore(&bitmap->lock, flags);
1066 lastpage = page;
NeilBrown32a76272005-06-21 17:17:14 -07001067/*
1068 printk("bitmap clean at page %lu\n", j);
1069*/
1070 spin_lock_irqsave(&bitmap->lock, flags);
1071 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1072 }
1073 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1074 &blocks, 0);
1075 if (bmc) {
1076/*
1077 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1078*/
1079 if (*bmc == 2) {
1080 *bmc=1; /* maybe clear the bit next time */
1081 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1082 } else if (*bmc == 1) {
1083 /* we can clear the bit */
1084 *bmc = 0;
1085 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1086 -1);
1087
1088 /* clear the bit */
NeilBrownea03aff2006-01-06 00:20:34 -08001089 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001090 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001091 clear_bit(file_page_offset(j), paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001092 else
NeilBrownea03aff2006-01-06 00:20:34 -08001093 ext2_clear_bit(file_page_offset(j), paddr);
1094 kunmap_atomic(paddr, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -07001095 }
1096 }
1097 spin_unlock_irqrestore(&bitmap->lock, flags);
1098 }
1099
1100 /* now sync the final page */
1101 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001102 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001103 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001104 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1105 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown77ad4bc2005-06-21 17:17:21 -07001106 err = write_page(bitmap, lastpage, 0);
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001107 if (err == -EAGAIN) {
1108 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1109 err = 0;
1110 }
NeilBrown32a76272005-06-21 17:17:14 -07001111 } else {
1112 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1113 spin_unlock_irqrestore(&bitmap->lock, flags);
1114 }
1115
NeilBrown2d1f3b52006-01-06 00:20:31 -08001116 put_page(lastpage);
NeilBrown32a76272005-06-21 17:17:14 -07001117 }
1118
1119 return err;
1120}
1121
NeilBrown0b79ccf2006-06-26 00:27:44 -07001122static void bitmap_writeback(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001123{
NeilBrown32a76272005-06-21 17:17:14 -07001124 struct page *page;
1125 struct page_list *item;
1126 int err = 0;
1127
NeilBrown77ad4bc2005-06-21 17:17:21 -07001128 PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
1129 /* wait on bitmap page writebacks */
1130 while ((item = dequeue_page(bitmap))) {
1131 page = item->page;
1132 mempool_free(item, bitmap->write_pool);
1133 PRINTK("wait on page writeback: %p\n", page);
1134 wait_on_page_writeback(page);
1135 PRINTK("finished page writeback: %p\n", page);
1136
1137 err = PageError(page);
NeilBrown2d1f3b52006-01-06 00:20:31 -08001138 put_page(page);
NeilBrown77ad4bc2005-06-21 17:17:21 -07001139 if (err) {
1140 printk(KERN_WARNING "%s: bitmap file writeback "
1141 "failed (page %lu): %d\n",
1142 bmname(bitmap), page->index, err);
1143 bitmap_file_kick(bitmap);
NeilBrown0b79ccf2006-06-26 00:27:44 -07001144 break;
NeilBrown32a76272005-06-21 17:17:14 -07001145 }
1146 }
NeilBrown32a76272005-06-21 17:17:14 -07001147}
1148
1149static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1150 sector_t offset, int *blocks,
1151 int create)
1152{
1153 /* If 'create', we might release the lock and reclaim it.
1154 * The lock must have been taken with interrupts enabled.
1155 * If !create, we don't release the lock.
1156 */
1157 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1158 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1159 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1160 sector_t csize;
1161
1162 if (bitmap_checkpage(bitmap, page, create) < 0) {
1163 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1164 *blocks = csize - (offset & (csize- 1));
1165 return NULL;
1166 }
1167 /* now locked ... */
1168
1169 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1170 /* should we use the first or second counter field
1171 * of the hijacked pointer? */
1172 int hi = (pageoff > PAGE_COUNTER_MASK);
1173 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1174 PAGE_COUNTER_SHIFT - 1);
1175 *blocks = csize - (offset & (csize- 1));
1176 return &((bitmap_counter_t *)
1177 &bitmap->bp[page].map)[hi];
1178 } else { /* page is allocated */
1179 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1180 *blocks = csize - (offset & (csize- 1));
1181 return (bitmap_counter_t *)
1182 &(bitmap->bp[page].map[pageoff]);
1183 }
1184}
1185
NeilBrown4b6d2872005-09-09 16:23:47 -07001186int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001187{
1188 if (!bitmap) return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001189
1190 if (behind) {
1191 atomic_inc(&bitmap->behind_writes);
1192 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1193 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1194 }
1195
NeilBrown32a76272005-06-21 17:17:14 -07001196 while (sectors) {
1197 int blocks;
1198 bitmap_counter_t *bmc;
1199
1200 spin_lock_irq(&bitmap->lock);
1201 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1202 if (!bmc) {
1203 spin_unlock_irq(&bitmap->lock);
1204 return 0;
1205 }
1206
1207 switch(*bmc) {
1208 case 0:
1209 bitmap_file_set_bit(bitmap, offset);
1210 bitmap_count_page(bitmap,offset, 1);
1211 blk_plug_device(bitmap->mddev->queue);
1212 /* fall through */
1213 case 1:
1214 *bmc = 2;
1215 }
Eric Sesterhenn5daf2cf2006-03-24 18:35:26 +01001216 BUG_ON((*bmc & COUNTER_MAX) == COUNTER_MAX);
NeilBrown32a76272005-06-21 17:17:14 -07001217 (*bmc)++;
1218
1219 spin_unlock_irq(&bitmap->lock);
1220
1221 offset += blocks;
1222 if (sectors > blocks)
1223 sectors -= blocks;
1224 else sectors = 0;
1225 }
1226 return 0;
1227}
1228
1229void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001230 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001231{
1232 if (!bitmap) return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001233 if (behind) {
1234 atomic_dec(&bitmap->behind_writes);
1235 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1236 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1237 }
1238
NeilBrown32a76272005-06-21 17:17:14 -07001239 while (sectors) {
1240 int blocks;
1241 unsigned long flags;
1242 bitmap_counter_t *bmc;
1243
1244 spin_lock_irqsave(&bitmap->lock, flags);
1245 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1246 if (!bmc) {
1247 spin_unlock_irqrestore(&bitmap->lock, flags);
1248 return;
1249 }
1250
1251 if (!success && ! (*bmc & NEEDED_MASK))
1252 *bmc |= NEEDED_MASK;
1253
1254 (*bmc)--;
1255 if (*bmc <= 2) {
1256 set_page_attr(bitmap,
1257 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1258 BITMAP_PAGE_CLEAN);
1259 }
1260 spin_unlock_irqrestore(&bitmap->lock, flags);
1261 offset += blocks;
1262 if (sectors > blocks)
1263 sectors -= blocks;
1264 else sectors = 0;
1265 }
1266}
1267
NeilBrown6a806c52005-07-15 03:56:35 -07001268int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1269 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001270{
1271 bitmap_counter_t *bmc;
1272 int rv;
1273 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1274 *blocks = 1024;
1275 return 1; /* always resync if no bitmap */
1276 }
1277 spin_lock_irq(&bitmap->lock);
1278 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1279 rv = 0;
1280 if (bmc) {
1281 /* locked */
1282 if (RESYNC(*bmc))
1283 rv = 1;
1284 else if (NEEDED(*bmc)) {
1285 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001286 if (!degraded) { /* don't set/clear bits if degraded */
1287 *bmc |= RESYNC_MASK;
1288 *bmc &= ~NEEDED_MASK;
1289 }
NeilBrown32a76272005-06-21 17:17:14 -07001290 }
1291 }
1292 spin_unlock_irq(&bitmap->lock);
1293 return rv;
1294}
1295
1296void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1297{
1298 bitmap_counter_t *bmc;
1299 unsigned long flags;
1300/*
1301 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1302*/ if (bitmap == NULL) {
1303 *blocks = 1024;
1304 return;
1305 }
1306 spin_lock_irqsave(&bitmap->lock, flags);
1307 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1308 if (bmc == NULL)
1309 goto unlock;
1310 /* locked */
1311/*
1312 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1313*/
1314 if (RESYNC(*bmc)) {
1315 *bmc &= ~RESYNC_MASK;
1316
1317 if (!NEEDED(*bmc) && aborted)
1318 *bmc |= NEEDED_MASK;
1319 else {
1320 if (*bmc <= 2) {
1321 set_page_attr(bitmap,
1322 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1323 BITMAP_PAGE_CLEAN);
1324 }
1325 }
1326 }
1327 unlock:
1328 spin_unlock_irqrestore(&bitmap->lock, flags);
1329}
1330
1331void bitmap_close_sync(struct bitmap *bitmap)
1332{
1333 /* Sync has finished, and any bitmap chunks that weren't synced
1334 * properly have been aborted. It remains to us to clear the
1335 * RESYNC bit wherever it is still on
1336 */
1337 sector_t sector = 0;
1338 int blocks;
1339 if (!bitmap) return;
1340 while (sector < bitmap->mddev->resync_max_sectors) {
1341 bitmap_end_sync(bitmap, sector, &blocks, 0);
1342/*
1343 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1344 (unsigned long long)sector, blocks);
1345*/ sector += blocks;
1346 }
1347}
1348
NeilBrown6a079972005-09-09 16:23:44 -07001349static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001350{
1351 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001352 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001353 * be 0 at this point
1354 */
NeilBrown193f1c92005-08-04 12:53:33 -07001355
1356 int secs;
1357 bitmap_counter_t *bmc;
1358 spin_lock_irq(&bitmap->lock);
1359 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1360 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001361 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001362 return;
NeilBrown32a76272005-06-21 17:17:14 -07001363 }
NeilBrown193f1c92005-08-04 12:53:33 -07001364 if (! *bmc) {
1365 struct page *page;
NeilBrown6a079972005-09-09 16:23:44 -07001366 *bmc = 1 | (needed?NEEDED_MASK:0);
NeilBrown193f1c92005-08-04 12:53:33 -07001367 bitmap_count_page(bitmap, offset, 1);
1368 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1369 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1370 }
1371 spin_unlock_irq(&bitmap->lock);
1372
NeilBrown32a76272005-06-21 17:17:14 -07001373}
1374
NeilBrown32a76272005-06-21 17:17:14 -07001375/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001376 * flush out any pending updates
1377 */
1378void bitmap_flush(mddev_t *mddev)
1379{
1380 struct bitmap *bitmap = mddev->bitmap;
1381 int sleep;
1382
1383 if (!bitmap) /* there was no bitmap */
1384 return;
1385
1386 /* run the daemon_work three time to ensure everything is flushed
1387 * that can be
1388 */
1389 sleep = bitmap->daemon_sleep;
1390 bitmap->daemon_sleep = 0;
1391 bitmap_daemon_work(bitmap);
1392 bitmap_daemon_work(bitmap);
1393 bitmap_daemon_work(bitmap);
1394 bitmap->daemon_sleep = sleep;
1395 bitmap_update_sb(bitmap);
1396}
1397
1398/*
NeilBrown32a76272005-06-21 17:17:14 -07001399 * free memory that was allocated
1400 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001401static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001402{
1403 unsigned long k, pages;
1404 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001405
1406 if (!bitmap) /* there was no bitmap */
1407 return;
1408
NeilBrown32a76272005-06-21 17:17:14 -07001409 /* release the bitmap file and kill the daemon */
1410 bitmap_file_put(bitmap);
1411
1412 bp = bitmap->bp;
1413 pages = bitmap->pages;
1414
1415 /* free all allocated memory */
1416
1417 mempool_destroy(bitmap->write_pool);
1418
1419 if (bp) /* deallocate the page memory */
1420 for (k = 0; k < pages; k++)
1421 if (bp[k].map && !bp[k].hijacked)
1422 kfree(bp[k].map);
1423 kfree(bp);
1424 kfree(bitmap);
1425}
NeilBrown3178b0d2005-09-09 16:23:50 -07001426void bitmap_destroy(mddev_t *mddev)
1427{
1428 struct bitmap *bitmap = mddev->bitmap;
1429
1430 if (!bitmap) /* there was no bitmap */
1431 return;
1432
1433 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownb15c2e52006-01-06 00:20:16 -08001434 if (mddev->thread)
1435 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001436
1437 bitmap_free(bitmap);
1438}
NeilBrown32a76272005-06-21 17:17:14 -07001439
1440/*
1441 * initialize the bitmap structure
1442 * if this returns an error, bitmap_destroy must be called to do clean up
1443 */
1444int bitmap_create(mddev_t *mddev)
1445{
1446 struct bitmap *bitmap;
1447 unsigned long blocks = mddev->resync_max_sectors;
1448 unsigned long chunks;
1449 unsigned long pages;
1450 struct file *file = mddev->bitmap_file;
1451 int err;
NeilBrown6a079972005-09-09 16:23:44 -07001452 sector_t start;
NeilBrown32a76272005-06-21 17:17:14 -07001453
1454 BUG_ON(sizeof(bitmap_super_t) != 256);
1455
NeilBrowna654b9d82005-06-21 17:17:27 -07001456 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001457 return 0;
1458
NeilBrowna654b9d82005-06-21 17:17:27 -07001459 BUG_ON(file && mddev->bitmap_offset);
1460
NeilBrown9ffae0c2006-01-06 00:20:32 -08001461 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001462 if (!bitmap)
1463 return -ENOMEM;
1464
NeilBrown32a76272005-06-21 17:17:14 -07001465 spin_lock_init(&bitmap->lock);
1466 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001467
1468 spin_lock_init(&bitmap->write_lock);
NeilBrown32a76272005-06-21 17:17:14 -07001469 INIT_LIST_HEAD(&bitmap->complete_pages);
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08001470 bitmap->write_pool = mempool_create_kmalloc_pool(WRITE_POOL_SIZE,
1471 sizeof(struct page_list));
NeilBrown3178b0d2005-09-09 16:23:50 -07001472 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001473 if (!bitmap->write_pool)
NeilBrown3178b0d2005-09-09 16:23:50 -07001474 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001475
1476 bitmap->file = file;
NeilBrowna654b9d82005-06-21 17:17:27 -07001477 bitmap->offset = mddev->bitmap_offset;
1478 if (file) get_file(file);
NeilBrown32a76272005-06-21 17:17:14 -07001479 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1480 err = bitmap_read_sb(bitmap);
1481 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001482 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001483
1484 bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
1485 sizeof(bitmap->chunksize));
1486
1487 /* now that chunksize and chunkshift are set, we can use these macros */
1488 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1489 CHUNK_BLOCK_RATIO(bitmap);
1490 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1491
1492 BUG_ON(!pages);
1493
1494 bitmap->chunks = chunks;
1495 bitmap->pages = pages;
1496 bitmap->missing_pages = pages;
1497 bitmap->counter_bits = COUNTER_BITS;
1498
1499 bitmap->syncchunk = ~0UL;
1500
Olaf Hering44456d32005-07-27 11:45:17 -07001501#ifdef INJECT_FATAL_FAULT_1
NeilBrown32a76272005-06-21 17:17:14 -07001502 bitmap->bp = NULL;
1503#else
NeilBrown9ffae0c2006-01-06 00:20:32 -08001504 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001505#endif
NeilBrown3178b0d2005-09-09 16:23:50 -07001506 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001507 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001508 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001509
1510 bitmap->flags |= BITMAP_ACTIVE;
1511
1512 /* now that we have some pages available, initialize the in-memory
1513 * bitmap from the on-disk bitmap */
NeilBrown6a079972005-09-09 16:23:44 -07001514 start = 0;
1515 if (mddev->degraded == 0
1516 || bitmap->events_cleared == mddev->events)
1517 /* no need to keep dirty bits to optimise a re-add of a missing device */
1518 start = mddev->recovery_cp;
1519 err = bitmap_init_from_disk(bitmap, start);
NeilBrown193f1c92005-08-04 12:53:33 -07001520
NeilBrown32a76272005-06-21 17:17:14 -07001521 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001522 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001523
1524 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1525 pages, bmname(bitmap));
1526
NeilBrown3178b0d2005-09-09 16:23:50 -07001527 mddev->bitmap = bitmap;
1528
NeilBrownb15c2e52006-01-06 00:20:16 -08001529 mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1530
NeilBrown32a76272005-06-21 17:17:14 -07001531 return bitmap_update_sb(bitmap);
NeilBrown3178b0d2005-09-09 16:23:50 -07001532
1533 error:
1534 bitmap_free(bitmap);
1535 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001536}
1537
1538/* the bitmap API -- for raid personalities */
1539EXPORT_SYMBOL(bitmap_startwrite);
1540EXPORT_SYMBOL(bitmap_endwrite);
1541EXPORT_SYMBOL(bitmap_start_sync);
1542EXPORT_SYMBOL(bitmap_end_sync);
1543EXPORT_SYMBOL(bitmap_unplug);
1544EXPORT_SYMBOL(bitmap_close_sync);
1545EXPORT_SYMBOL(bitmap_daemon_work);