blob: 609e68d3e1bc69dacba818ebbed154f651858d6d [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;
NeilBrown77ad4bc2005-06-21 17:17:21 -0700335 spin_lock(&bitmap->write_lock);
336 list_add(&item->list, &bitmap->complete_pages);
337 spin_unlock(&bitmap->write_lock);
NeilBrown77ad4bc2005-06-21 17:17:21 -0700338 }
NeilBrown32a76272005-06-21 17:17:14 -0700339 return write_one_page(page, wait);
340}
341
342/* read a page from a file, pinning it into cache, and return bytes_read */
343static struct page *read_page(struct file *file, unsigned long index,
344 unsigned long *bytes_read)
345{
346 struct inode *inode = file->f_mapping->host;
347 struct page *page = NULL;
348 loff_t isize = i_size_read(inode);
NeilBrown2d1f3b52006-01-06 00:20:31 -0800349 unsigned long end_index = isize >> PAGE_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700350
NeilBrown2d1f3b52006-01-06 00:20:31 -0800351 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
352 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700353
354 page = read_cache_page(inode->i_mapping, index,
355 (filler_t *)inode->i_mapping->a_ops->readpage, file);
356 if (IS_ERR(page))
357 goto out;
358 wait_on_page_locked(page);
359 if (!PageUptodate(page) || PageError(page)) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800360 put_page(page);
NeilBrown32a76272005-06-21 17:17:14 -0700361 page = ERR_PTR(-EIO);
362 goto out;
363 }
364
365 if (index > end_index) /* we have read beyond EOF */
366 *bytes_read = 0;
367 else if (index == end_index) /* possible short read */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800368 *bytes_read = isize & ~PAGE_MASK;
NeilBrown32a76272005-06-21 17:17:14 -0700369 else
NeilBrown2d1f3b52006-01-06 00:20:31 -0800370 *bytes_read = PAGE_SIZE; /* got a full page */
NeilBrown32a76272005-06-21 17:17:14 -0700371out:
372 if (IS_ERR(page))
373 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800374 (int)PAGE_SIZE,
375 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700376 PTR_ERR(page));
377 return page;
378}
379
380/*
381 * bitmap file superblock operations
382 */
383
384/* update the event counter and sync the superblock to disk */
385int bitmap_update_sb(struct bitmap *bitmap)
386{
387 bitmap_super_t *sb;
388 unsigned long flags;
389
390 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
391 return 0;
392 spin_lock_irqsave(&bitmap->lock, flags);
393 if (!bitmap->sb_page) { /* no superblock */
394 spin_unlock_irqrestore(&bitmap->lock, flags);
395 return 0;
396 }
NeilBrown32a76272005-06-21 17:17:14 -0700397 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800398 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700399 sb->events = cpu_to_le64(bitmap->mddev->events);
400 if (!bitmap->mddev->degraded)
401 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
NeilBrownea03aff2006-01-06 00:20:34 -0800402 kunmap_atomic(sb, KM_USER0);
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700403 return write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700404}
405
406/* print out the bitmap file superblock */
407void bitmap_print_sb(struct bitmap *bitmap)
408{
409 bitmap_super_t *sb;
410
411 if (!bitmap || !bitmap->sb_page)
412 return;
NeilBrownea03aff2006-01-06 00:20:34 -0800413 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700414 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700415 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
416 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
417 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700418 *(__u32 *)(sb->uuid+0),
419 *(__u32 *)(sb->uuid+4),
420 *(__u32 *)(sb->uuid+8),
421 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700422 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700423 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700424 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700425 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700426 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
427 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
428 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
429 printk(KERN_DEBUG " sync size: %llu KB\n",
430 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700431 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800432 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700433}
434
435/* read the superblock from the bitmap file and initialize some bitmap fields */
436static int bitmap_read_sb(struct bitmap *bitmap)
437{
438 char *reason = NULL;
439 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700440 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700441 unsigned long bytes_read;
442 unsigned long long events;
443 int err = -EINVAL;
444
445 /* page 0 is the superblock, read it... */
NeilBrowna654b9d82005-06-21 17:17:27 -0700446 if (bitmap->file)
447 bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read);
448 else {
449 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
450 bytes_read = PAGE_SIZE;
451 }
NeilBrown32a76272005-06-21 17:17:14 -0700452 if (IS_ERR(bitmap->sb_page)) {
453 err = PTR_ERR(bitmap->sb_page);
454 bitmap->sb_page = NULL;
455 return err;
456 }
457
NeilBrownea03aff2006-01-06 00:20:34 -0800458 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700459
460 if (bytes_read < sizeof(*sb)) { /* short read */
461 printk(KERN_INFO "%s: bitmap file superblock truncated\n",
462 bmname(bitmap));
463 err = -ENOSPC;
464 goto out;
465 }
466
467 chunksize = le32_to_cpu(sb->chunksize);
468 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
NeilBrown4b6d2872005-09-09 16:23:47 -0700469 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700470
471 /* verify that the bitmap-specific fields are valid */
472 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
473 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800474 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
475 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700476 reason = "unrecognized superblock version";
NeilBrown7dd5d342006-01-06 00:20:39 -0800477 else if (chunksize < PAGE_SIZE)
478 reason = "bitmap chunksize too small";
NeilBrown32a76272005-06-21 17:17:14 -0700479 else if ((1 << ffz(~chunksize)) != chunksize)
480 reason = "bitmap chunksize not a power of 2";
NeilBrown7dd5d342006-01-06 00:20:39 -0800481 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT / HZ)
482 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700483 else if (write_behind > COUNTER_MAX)
484 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700485 if (reason) {
486 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
487 bmname(bitmap), reason);
488 goto out;
489 }
490
491 /* keep the array size field of the bitmap superblock up to date */
492 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
493
494 if (!bitmap->mddev->persistent)
495 goto success;
496
497 /*
498 * if we have a persistent array superblock, compare the
499 * bitmap's UUID and event counter to the mddev's
500 */
501 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
502 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
503 bmname(bitmap));
504 goto out;
505 }
506 events = le64_to_cpu(sb->events);
507 if (events < bitmap->mddev->events) {
508 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
509 "-- forcing full recovery\n", bmname(bitmap), events,
510 (unsigned long long) bitmap->mddev->events);
511 sb->state |= BITMAP_STALE;
512 }
513success:
514 /* assign fields using values from superblock */
515 bitmap->chunksize = chunksize;
516 bitmap->daemon_sleep = daemon_sleep;
NeilBrown585f0dd2005-09-09 16:23:49 -0700517 bitmap->daemon_lastrun = jiffies;
NeilBrown4b6d2872005-09-09 16:23:47 -0700518 bitmap->max_write_behind = write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700519 bitmap->flags |= sb->state;
NeilBrownbd926c62005-11-08 21:39:32 -0800520 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
521 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700522 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown6a079972005-09-09 16:23:44 -0700523 if (sb->state & BITMAP_STALE)
524 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700525 err = 0;
526out:
NeilBrownea03aff2006-01-06 00:20:34 -0800527 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700528 if (err)
529 bitmap_print_sb(bitmap);
530 return err;
531}
532
533enum bitmap_mask_op {
534 MASK_SET,
535 MASK_UNSET
536};
537
538/* record the state of the bitmap in the superblock */
539static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
540 enum bitmap_mask_op op)
541{
542 bitmap_super_t *sb;
543 unsigned long flags;
544
545 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800546 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700547 spin_unlock_irqrestore(&bitmap->lock, flags);
548 return;
549 }
NeilBrown32a76272005-06-21 17:17:14 -0700550 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800551 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700552 switch (op) {
553 case MASK_SET: sb->state |= bits;
554 break;
555 case MASK_UNSET: sb->state &= ~bits;
556 break;
557 default: BUG();
558 }
NeilBrownea03aff2006-01-06 00:20:34 -0800559 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700560}
561
562/*
563 * general bitmap file operations
564 */
565
566/* calculate the index of the page that contains this bit */
567static inline unsigned long file_page_index(unsigned long chunk)
568{
569 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
570}
571
572/* calculate the (bit) offset of this bit within a page */
573static inline unsigned long file_page_offset(unsigned long chunk)
574{
575 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
576}
577
578/*
579 * return a pointer to the page in the filemap that contains the given bit
580 *
581 * this lookup is complicated by the fact that the bitmap sb might be exactly
582 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
583 * 0 or page 1
584 */
585static inline struct page *filemap_get_page(struct bitmap *bitmap,
586 unsigned long chunk)
587{
588 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
589}
590
591
592static void bitmap_file_unmap(struct bitmap *bitmap)
593{
594 struct page **map, *sb_page;
595 unsigned long *attr;
596 int pages;
597 unsigned long flags;
598
599 spin_lock_irqsave(&bitmap->lock, flags);
600 map = bitmap->filemap;
601 bitmap->filemap = NULL;
602 attr = bitmap->filemap_attr;
603 bitmap->filemap_attr = NULL;
604 pages = bitmap->file_pages;
605 bitmap->file_pages = 0;
606 sb_page = bitmap->sb_page;
607 bitmap->sb_page = NULL;
608 spin_unlock_irqrestore(&bitmap->lock, flags);
609
610 while (pages--)
611 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800612 put_page(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700613 kfree(map);
614 kfree(attr);
615
NeilBrown1345b1d2006-01-06 00:20:40 -0800616 safe_put_page(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700617}
618
NeilBrown32a76272005-06-21 17:17:14 -0700619/* dequeue the next item in a page list -- don't call from irq context */
NeilBrown77ad4bc2005-06-21 17:17:21 -0700620static struct page_list *dequeue_page(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700621{
622 struct page_list *item = NULL;
NeilBrown77ad4bc2005-06-21 17:17:21 -0700623 struct list_head *head = &bitmap->complete_pages;
NeilBrown32a76272005-06-21 17:17:14 -0700624
625 spin_lock(&bitmap->write_lock);
626 if (list_empty(head))
627 goto out;
628 item = list_entry(head->prev, struct page_list, list);
629 list_del(head->prev);
630out:
631 spin_unlock(&bitmap->write_lock);
632 return item;
633}
634
635static void drain_write_queues(struct bitmap *bitmap)
636{
NeilBrown32a76272005-06-21 17:17:14 -0700637 struct page_list *item;
NeilBrown32a76272005-06-21 17:17:14 -0700638
NeilBrown77ad4bc2005-06-21 17:17:21 -0700639 while ((item = dequeue_page(bitmap))) {
640 /* don't bother to wait */
NeilBrown77ad4bc2005-06-21 17:17:21 -0700641 mempool_free(item, bitmap->write_pool);
NeilBrown32a76272005-06-21 17:17:14 -0700642 }
NeilBrown32a76272005-06-21 17:17:14 -0700643}
644
645static void bitmap_file_put(struct bitmap *bitmap)
646{
647 struct file *file;
648 struct inode *inode;
649 unsigned long flags;
650
651 spin_lock_irqsave(&bitmap->lock, flags);
652 file = bitmap->file;
653 bitmap->file = NULL;
654 spin_unlock_irqrestore(&bitmap->lock, flags);
655
NeilBrown32a76272005-06-21 17:17:14 -0700656 drain_write_queues(bitmap);
657
658 bitmap_file_unmap(bitmap);
659
660 if (file) {
661 inode = file->f_mapping->host;
662 spin_lock(&inode->i_lock);
663 atomic_set(&inode->i_writecount, 1); /* allow writes again */
664 spin_unlock(&inode->i_lock);
665 fput(file);
666 }
667}
668
669
670/*
671 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
672 * then it is no longer reliable, so we stop using it and we mark the file
673 * as failed in the superblock
674 */
675static void bitmap_file_kick(struct bitmap *bitmap)
676{
677 char *path, *ptr = NULL;
678
679 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
680 bitmap_update_sb(bitmap);
681
NeilBrowna654b9d82005-06-21 17:17:27 -0700682 if (bitmap->file) {
683 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
684 if (path)
685 ptr = file_path(bitmap->file, path, PAGE_SIZE);
NeilBrown32a76272005-06-21 17:17:14 -0700686
NeilBrowna654b9d82005-06-21 17:17:27 -0700687 printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
688 bmname(bitmap), ptr ? ptr : "");
NeilBrown32a76272005-06-21 17:17:14 -0700689
NeilBrowna654b9d82005-06-21 17:17:27 -0700690 kfree(path);
691 }
NeilBrown32a76272005-06-21 17:17:14 -0700692
693 bitmap_file_put(bitmap);
694
695 return;
696}
697
698enum bitmap_page_attr {
NeilBrowne16b68b2006-06-26 00:27:45 -0700699 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
700 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
701 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
NeilBrown32a76272005-06-21 17:17:14 -0700702};
703
704static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
705 enum bitmap_page_attr attr)
706{
NeilBrowne16b68b2006-06-26 00:27:45 -0700707 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700708}
709
710static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
711 enum bitmap_page_attr attr)
712{
NeilBrowne16b68b2006-06-26 00:27:45 -0700713 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700714}
715
NeilBrownec7a3192006-06-26 00:27:45 -0700716static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
717 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700718{
NeilBrowne16b68b2006-06-26 00:27:45 -0700719 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700720}
721
722/*
723 * bitmap_file_set_bit -- called before performing a write to the md device
724 * to set (and eventually sync) a particular bit in the bitmap file
725 *
726 * we set the bit immediately, then we record the page number so that
727 * when an unplug occurs, we can flush the dirty pages out to disk
728 */
729static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
730{
731 unsigned long bit;
732 struct page *page;
733 void *kaddr;
734 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
735
NeilBrowna654b9d82005-06-21 17:17:27 -0700736 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700737 return;
738 }
739
740 page = filemap_get_page(bitmap, chunk);
741 bit = file_page_offset(chunk);
742
NeilBrown32a76272005-06-21 17:17:14 -0700743 /* set the bit */
744 kaddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800745 if (bitmap->flags & BITMAP_HOSTENDIAN)
746 set_bit(bit, kaddr);
747 else
748 ext2_set_bit(bit, kaddr);
NeilBrown32a76272005-06-21 17:17:14 -0700749 kunmap_atomic(kaddr, KM_USER0);
750 PRINTK("set file bit %lu page %lu\n", bit, page->index);
751
752 /* record page number so it gets flushed to disk when unplug occurs */
753 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
754
755}
756
NeilBrown0b79ccf2006-06-26 00:27:44 -0700757static void bitmap_writeback(struct bitmap *bitmap);
758
NeilBrown32a76272005-06-21 17:17:14 -0700759/* this gets called when the md device is ready to unplug its underlying
760 * (slave) device queues -- before we let any writes go down, we need to
761 * sync the dirty pages of the bitmap file to disk */
762int bitmap_unplug(struct bitmap *bitmap)
763{
NeilBrownec7a3192006-06-26 00:27:45 -0700764 unsigned long i, flags;
765 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700766 struct page *page;
767 int wait = 0;
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700768 int err;
NeilBrown32a76272005-06-21 17:17:14 -0700769
770 if (!bitmap)
771 return 0;
772
773 /* look at each page to see if there are any set bits that need to be
774 * flushed out to disk */
775 for (i = 0; i < bitmap->file_pages; i++) {
776 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700777 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700778 spin_unlock_irqrestore(&bitmap->lock, flags);
779 return 0;
780 }
781 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700782 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
783 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700784 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
785 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700786 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700787 wait = 1;
788 spin_unlock_irqrestore(&bitmap->lock, flags);
789
NeilBrownec7a3192006-06-26 00:27:45 -0700790 if (dirty | need_write) {
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700791 err = write_page(bitmap, page, 0);
792 if (err == -EAGAIN) {
NeilBrownec7a3192006-06-26 00:27:45 -0700793 if (dirty)
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700794 err = write_page(bitmap, page, 1);
795 else
796 err = 0;
797 }
798 if (err)
NeilBrownbfb39fb2005-06-21 17:17:20 -0700799 return 1;
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700800 }
NeilBrown32a76272005-06-21 17:17:14 -0700801 }
802 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700803 if (bitmap->file)
804 bitmap_writeback(bitmap);
805 else
NeilBrowna9701a32005-11-08 21:39:34 -0800806 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700807 }
808 return 0;
809}
810
NeilBrown6a079972005-09-09 16:23:44 -0700811static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700812/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
813 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
814 * memory mapping of the bitmap file
815 * Special cases:
816 * if there's no bitmap file, or if the bitmap file had been
817 * previously kicked from the array, we mark all the bits as
818 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700819 *
820 * We ignore all bits for sectors that end earlier than 'start'.
821 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700822 */
NeilBrown6a079972005-09-09 16:23:44 -0700823static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700824{
825 unsigned long i, chunks, index, oldindex, bit;
826 struct page *page = NULL, *oldpage = NULL;
827 unsigned long num_pages, bit_cnt = 0;
828 struct file *file;
829 unsigned long bytes, offset, dummy;
830 int outofdate;
831 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800832 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700833
834 chunks = bitmap->chunks;
835 file = bitmap->file;
836
NeilBrowna654b9d82005-06-21 17:17:27 -0700837 BUG_ON(!file && !bitmap->offset);
NeilBrown32a76272005-06-21 17:17:14 -0700838
Olaf Hering44456d32005-07-27 11:45:17 -0700839#ifdef INJECT_FAULTS_3
NeilBrown32a76272005-06-21 17:17:14 -0700840 outofdate = 1;
841#else
842 outofdate = bitmap->flags & BITMAP_STALE;
843#endif
844 if (outofdate)
845 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
846 "recovery\n", bmname(bitmap));
847
848 bytes = (chunks + 7) / 8;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700849
NeilBrowncdbb4cc2005-06-21 17:17:18 -0700850 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700851
NeilBrowna654b9d82005-06-21 17:17:27 -0700852 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
NeilBrown32a76272005-06-21 17:17:14 -0700853 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
854 bmname(bitmap),
855 (unsigned long) i_size_read(file->f_mapping->host),
856 bytes + sizeof(bitmap_super_t));
857 goto out;
858 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700859
860 ret = -ENOMEM;
861
NeilBrown32a76272005-06-21 17:17:14 -0700862 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700863 if (!bitmap->filemap)
NeilBrown32a76272005-06-21 17:17:14 -0700864 goto out;
NeilBrown32a76272005-06-21 17:17:14 -0700865
NeilBrowne16b68b2006-06-26 00:27:45 -0700866 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
867 bitmap->filemap_attr = kzalloc(
868 (((num_pages*4/8)+sizeof(unsigned long)-1)
869 /sizeof(unsigned long))
870 *sizeof(unsigned long),
871 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700872 if (!bitmap->filemap_attr)
NeilBrown32a76272005-06-21 17:17:14 -0700873 goto out;
NeilBrown32a76272005-06-21 17:17:14 -0700874
NeilBrown32a76272005-06-21 17:17:14 -0700875 oldindex = ~0L;
876
877 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -0800878 int b;
NeilBrown32a76272005-06-21 17:17:14 -0700879 index = file_page_index(i);
880 bit = file_page_offset(i);
881 if (index != oldindex) { /* this is a new page, read it in */
882 /* unmap the old page, we're done with it */
NeilBrown32a76272005-06-21 17:17:14 -0700883 if (index == 0) {
884 /*
885 * if we're here then the superblock page
886 * contains some bits (PAGE_SIZE != sizeof sb)
887 * we've already read it in, so just use it
888 */
889 page = bitmap->sb_page;
890 offset = sizeof(bitmap_super_t);
NeilBrowna654b9d82005-06-21 17:17:27 -0700891 } else if (file) {
NeilBrown32a76272005-06-21 17:17:14 -0700892 page = read_page(file, index, &dummy);
NeilBrowna654b9d82005-06-21 17:17:27 -0700893 offset = 0;
894 } else {
895 page = read_sb_page(bitmap->mddev, bitmap->offset, index);
NeilBrown32a76272005-06-21 17:17:14 -0700896 offset = 0;
897 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700898 if (IS_ERR(page)) { /* read error */
899 ret = PTR_ERR(page);
900 goto out;
901 }
902
NeilBrown32a76272005-06-21 17:17:14 -0700903 oldindex = index;
904 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -0700905
906 if (outofdate) {
907 /*
908 * if bitmap is out of date, dirty the
909 * whole page and write it out
910 */
NeilBrownea03aff2006-01-06 00:20:34 -0800911 paddr = kmap_atomic(page, KM_USER0);
912 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -0700913 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -0800914 kunmap_atomic(paddr, KM_USER0);
NeilBrown77ad4bc2005-06-21 17:17:21 -0700915 ret = write_page(bitmap, page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700916 if (ret) {
NeilBrown32a76272005-06-21 17:17:14 -0700917 /* release, page not in filemap yet */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800918 put_page(page);
NeilBrown32a76272005-06-21 17:17:14 -0700919 goto out;
920 }
921 }
922
923 bitmap->filemap[bitmap->file_pages++] = page;
924 }
NeilBrownea03aff2006-01-06 00:20:34 -0800925 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800926 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -0800927 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -0800928 else
NeilBrownea03aff2006-01-06 00:20:34 -0800929 b = ext2_test_bit(bit, paddr);
930 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800931 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -0700932 /* if the disk bit is set, set the memory bit */
NeilBrown6a079972005-09-09 16:23:44 -0700933 bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
934 ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
935 );
NeilBrown32a76272005-06-21 17:17:14 -0700936 bit_cnt++;
NeilBrown6a079972005-09-09 16:23:44 -0700937 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -0700938 }
NeilBrown32a76272005-06-21 17:17:14 -0700939 }
940
941 /* everything went OK */
942 ret = 0;
943 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
944
NeilBrown32a76272005-06-21 17:17:14 -0700945 if (bit_cnt) { /* Kick recovery if any bits were set */
946 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
947 md_wakeup_thread(bitmap->mddev->thread);
948 }
949
950out:
951 printk(KERN_INFO "%s: bitmap initialized from disk: "
952 "read %lu/%lu pages, set %lu bits, status: %d\n",
953 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
954
955 return ret;
956}
957
NeilBrowna654b9d82005-06-21 17:17:27 -0700958void bitmap_write_all(struct bitmap *bitmap)
959{
960 /* We don't actually write all bitmap blocks here,
961 * just flag them as needing to be written
962 */
NeilBrownec7a3192006-06-26 00:27:45 -0700963 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -0700964
NeilBrownec7a3192006-06-26 00:27:45 -0700965 for (i=0; i < bitmap->file_pages; i++)
966 set_page_attr(bitmap, bitmap->filemap[i],
967 BITMAP_PAGE_NEEDWRITE);
NeilBrowna654b9d82005-06-21 17:17:27 -0700968}
969
NeilBrown32a76272005-06-21 17:17:14 -0700970
971static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
972{
973 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
974 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
975 bitmap->bp[page].count += inc;
976/*
977 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
978 (unsigned long long)offset, inc, bitmap->bp[page].count);
979*/
980 bitmap_checkfree(bitmap, page);
981}
982static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
983 sector_t offset, int *blocks,
984 int create);
985
986/*
987 * bitmap daemon -- periodically wakes up to clean bits and flush pages
988 * out to disk
989 */
990
991int bitmap_daemon_work(struct bitmap *bitmap)
992{
NeilBrownaa3163f2005-06-21 17:17:22 -0700993 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -0700994 unsigned long flags;
995 struct page *page = NULL, *lastpage = NULL;
996 int err = 0;
997 int blocks;
NeilBrownea03aff2006-01-06 00:20:34 -0800998 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700999
1000 if (bitmap == NULL)
1001 return 0;
1002 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
1003 return 0;
1004 bitmap->daemon_lastrun = jiffies;
1005
1006 for (j = 0; j < bitmap->chunks; j++) {
1007 bitmap_counter_t *bmc;
1008 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -07001009 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -07001010 /* error or shutdown */
1011 spin_unlock_irqrestore(&bitmap->lock, flags);
1012 break;
1013 }
1014
1015 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001016
1017 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001018 /* skip this page unless it's marked as needing cleaning */
NeilBrownec7a3192006-06-26 00:27:45 -07001019 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1020 int need_write = test_page_attr(bitmap, page,
1021 BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001022 if (need_write)
NeilBrownaa3163f2005-06-21 17:17:22 -07001023 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001024
NeilBrownaa3163f2005-06-21 17:17:22 -07001025 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001026 if (need_write) {
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001027 switch (write_page(bitmap, page, 0)) {
1028 case -EAGAIN:
1029 set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1030 break;
1031 case 0:
1032 break;
1033 default:
NeilBrownaa3163f2005-06-21 17:17:22 -07001034 bitmap_file_kick(bitmap);
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001035 }
NeilBrownaa3163f2005-06-21 17:17:22 -07001036 }
1037 continue;
1038 }
1039
NeilBrown32a76272005-06-21 17:17:14 -07001040 /* grab the new page, sync and release the old */
NeilBrown32a76272005-06-21 17:17:14 -07001041 if (lastpage != NULL) {
NeilBrownec7a3192006-06-26 00:27:45 -07001042 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001043 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1044 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown77ad4bc2005-06-21 17:17:21 -07001045 err = write_page(bitmap, lastpage, 0);
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001046 if (err == -EAGAIN) {
1047 err = 0;
1048 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1049 }
NeilBrown32a76272005-06-21 17:17:14 -07001050 } else {
1051 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1052 spin_unlock_irqrestore(&bitmap->lock, flags);
1053 }
NeilBrown32a76272005-06-21 17:17:14 -07001054 if (err)
1055 bitmap_file_kick(bitmap);
1056 } else
1057 spin_unlock_irqrestore(&bitmap->lock, flags);
1058 lastpage = page;
NeilBrown32a76272005-06-21 17:17:14 -07001059/*
1060 printk("bitmap clean at page %lu\n", j);
1061*/
1062 spin_lock_irqsave(&bitmap->lock, flags);
1063 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1064 }
1065 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1066 &blocks, 0);
1067 if (bmc) {
1068/*
1069 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1070*/
1071 if (*bmc == 2) {
1072 *bmc=1; /* maybe clear the bit next time */
1073 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1074 } else if (*bmc == 1) {
1075 /* we can clear the bit */
1076 *bmc = 0;
1077 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1078 -1);
1079
1080 /* clear the bit */
NeilBrownea03aff2006-01-06 00:20:34 -08001081 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001082 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001083 clear_bit(file_page_offset(j), paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001084 else
NeilBrownea03aff2006-01-06 00:20:34 -08001085 ext2_clear_bit(file_page_offset(j), paddr);
1086 kunmap_atomic(paddr, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -07001087 }
1088 }
1089 spin_unlock_irqrestore(&bitmap->lock, flags);
1090 }
1091
1092 /* now sync the final page */
1093 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001094 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001095 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001096 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1097 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown77ad4bc2005-06-21 17:17:21 -07001098 err = write_page(bitmap, lastpage, 0);
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001099 if (err == -EAGAIN) {
1100 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1101 err = 0;
1102 }
NeilBrown32a76272005-06-21 17:17:14 -07001103 } else {
1104 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1105 spin_unlock_irqrestore(&bitmap->lock, flags);
1106 }
NeilBrown32a76272005-06-21 17:17:14 -07001107 }
1108
1109 return err;
1110}
1111
NeilBrown0b79ccf2006-06-26 00:27:44 -07001112static void bitmap_writeback(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001113{
NeilBrown32a76272005-06-21 17:17:14 -07001114 struct page *page;
1115 struct page_list *item;
1116 int err = 0;
1117
NeilBrown77ad4bc2005-06-21 17:17:21 -07001118 PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
1119 /* wait on bitmap page writebacks */
1120 while ((item = dequeue_page(bitmap))) {
1121 page = item->page;
1122 mempool_free(item, bitmap->write_pool);
1123 PRINTK("wait on page writeback: %p\n", page);
1124 wait_on_page_writeback(page);
1125 PRINTK("finished page writeback: %p\n", page);
1126
1127 err = PageError(page);
NeilBrown77ad4bc2005-06-21 17:17:21 -07001128 if (err) {
1129 printk(KERN_WARNING "%s: bitmap file writeback "
1130 "failed (page %lu): %d\n",
1131 bmname(bitmap), page->index, err);
1132 bitmap_file_kick(bitmap);
NeilBrown0b79ccf2006-06-26 00:27:44 -07001133 break;
NeilBrown32a76272005-06-21 17:17:14 -07001134 }
1135 }
NeilBrown32a76272005-06-21 17:17:14 -07001136}
1137
1138static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1139 sector_t offset, int *blocks,
1140 int create)
1141{
1142 /* If 'create', we might release the lock and reclaim it.
1143 * The lock must have been taken with interrupts enabled.
1144 * If !create, we don't release the lock.
1145 */
1146 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1147 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1148 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1149 sector_t csize;
1150
1151 if (bitmap_checkpage(bitmap, page, create) < 0) {
1152 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1153 *blocks = csize - (offset & (csize- 1));
1154 return NULL;
1155 }
1156 /* now locked ... */
1157
1158 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1159 /* should we use the first or second counter field
1160 * of the hijacked pointer? */
1161 int hi = (pageoff > PAGE_COUNTER_MASK);
1162 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1163 PAGE_COUNTER_SHIFT - 1);
1164 *blocks = csize - (offset & (csize- 1));
1165 return &((bitmap_counter_t *)
1166 &bitmap->bp[page].map)[hi];
1167 } else { /* page is allocated */
1168 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1169 *blocks = csize - (offset & (csize- 1));
1170 return (bitmap_counter_t *)
1171 &(bitmap->bp[page].map[pageoff]);
1172 }
1173}
1174
NeilBrown4b6d2872005-09-09 16:23:47 -07001175int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001176{
1177 if (!bitmap) return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001178
1179 if (behind) {
1180 atomic_inc(&bitmap->behind_writes);
1181 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1182 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1183 }
1184
NeilBrown32a76272005-06-21 17:17:14 -07001185 while (sectors) {
1186 int blocks;
1187 bitmap_counter_t *bmc;
1188
1189 spin_lock_irq(&bitmap->lock);
1190 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1191 if (!bmc) {
1192 spin_unlock_irq(&bitmap->lock);
1193 return 0;
1194 }
1195
1196 switch(*bmc) {
1197 case 0:
1198 bitmap_file_set_bit(bitmap, offset);
1199 bitmap_count_page(bitmap,offset, 1);
1200 blk_plug_device(bitmap->mddev->queue);
1201 /* fall through */
1202 case 1:
1203 *bmc = 2;
1204 }
Eric Sesterhenn5daf2cf2006-03-24 18:35:26 +01001205 BUG_ON((*bmc & COUNTER_MAX) == COUNTER_MAX);
NeilBrown32a76272005-06-21 17:17:14 -07001206 (*bmc)++;
1207
1208 spin_unlock_irq(&bitmap->lock);
1209
1210 offset += blocks;
1211 if (sectors > blocks)
1212 sectors -= blocks;
1213 else sectors = 0;
1214 }
1215 return 0;
1216}
1217
1218void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001219 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001220{
1221 if (!bitmap) return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001222 if (behind) {
1223 atomic_dec(&bitmap->behind_writes);
1224 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1225 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1226 }
1227
NeilBrown32a76272005-06-21 17:17:14 -07001228 while (sectors) {
1229 int blocks;
1230 unsigned long flags;
1231 bitmap_counter_t *bmc;
1232
1233 spin_lock_irqsave(&bitmap->lock, flags);
1234 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1235 if (!bmc) {
1236 spin_unlock_irqrestore(&bitmap->lock, flags);
1237 return;
1238 }
1239
1240 if (!success && ! (*bmc & NEEDED_MASK))
1241 *bmc |= NEEDED_MASK;
1242
1243 (*bmc)--;
1244 if (*bmc <= 2) {
1245 set_page_attr(bitmap,
1246 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1247 BITMAP_PAGE_CLEAN);
1248 }
1249 spin_unlock_irqrestore(&bitmap->lock, flags);
1250 offset += blocks;
1251 if (sectors > blocks)
1252 sectors -= blocks;
1253 else sectors = 0;
1254 }
1255}
1256
NeilBrown6a806c52005-07-15 03:56:35 -07001257int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1258 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001259{
1260 bitmap_counter_t *bmc;
1261 int rv;
1262 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1263 *blocks = 1024;
1264 return 1; /* always resync if no bitmap */
1265 }
1266 spin_lock_irq(&bitmap->lock);
1267 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1268 rv = 0;
1269 if (bmc) {
1270 /* locked */
1271 if (RESYNC(*bmc))
1272 rv = 1;
1273 else if (NEEDED(*bmc)) {
1274 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001275 if (!degraded) { /* don't set/clear bits if degraded */
1276 *bmc |= RESYNC_MASK;
1277 *bmc &= ~NEEDED_MASK;
1278 }
NeilBrown32a76272005-06-21 17:17:14 -07001279 }
1280 }
1281 spin_unlock_irq(&bitmap->lock);
1282 return rv;
1283}
1284
1285void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1286{
1287 bitmap_counter_t *bmc;
1288 unsigned long flags;
1289/*
1290 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1291*/ if (bitmap == NULL) {
1292 *blocks = 1024;
1293 return;
1294 }
1295 spin_lock_irqsave(&bitmap->lock, flags);
1296 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1297 if (bmc == NULL)
1298 goto unlock;
1299 /* locked */
1300/*
1301 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1302*/
1303 if (RESYNC(*bmc)) {
1304 *bmc &= ~RESYNC_MASK;
1305
1306 if (!NEEDED(*bmc) && aborted)
1307 *bmc |= NEEDED_MASK;
1308 else {
1309 if (*bmc <= 2) {
1310 set_page_attr(bitmap,
1311 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1312 BITMAP_PAGE_CLEAN);
1313 }
1314 }
1315 }
1316 unlock:
1317 spin_unlock_irqrestore(&bitmap->lock, flags);
1318}
1319
1320void bitmap_close_sync(struct bitmap *bitmap)
1321{
1322 /* Sync has finished, and any bitmap chunks that weren't synced
1323 * properly have been aborted. It remains to us to clear the
1324 * RESYNC bit wherever it is still on
1325 */
1326 sector_t sector = 0;
1327 int blocks;
1328 if (!bitmap) return;
1329 while (sector < bitmap->mddev->resync_max_sectors) {
1330 bitmap_end_sync(bitmap, sector, &blocks, 0);
1331/*
1332 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1333 (unsigned long long)sector, blocks);
1334*/ sector += blocks;
1335 }
1336}
1337
NeilBrown6a079972005-09-09 16:23:44 -07001338static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001339{
1340 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001341 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001342 * be 0 at this point
1343 */
NeilBrown193f1c92005-08-04 12:53:33 -07001344
1345 int secs;
1346 bitmap_counter_t *bmc;
1347 spin_lock_irq(&bitmap->lock);
1348 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1349 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001350 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001351 return;
NeilBrown32a76272005-06-21 17:17:14 -07001352 }
NeilBrown193f1c92005-08-04 12:53:33 -07001353 if (! *bmc) {
1354 struct page *page;
NeilBrown6a079972005-09-09 16:23:44 -07001355 *bmc = 1 | (needed?NEEDED_MASK:0);
NeilBrown193f1c92005-08-04 12:53:33 -07001356 bitmap_count_page(bitmap, offset, 1);
1357 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1358 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1359 }
1360 spin_unlock_irq(&bitmap->lock);
1361
NeilBrown32a76272005-06-21 17:17:14 -07001362}
1363
NeilBrown32a76272005-06-21 17:17:14 -07001364/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001365 * flush out any pending updates
1366 */
1367void bitmap_flush(mddev_t *mddev)
1368{
1369 struct bitmap *bitmap = mddev->bitmap;
1370 int sleep;
1371
1372 if (!bitmap) /* there was no bitmap */
1373 return;
1374
1375 /* run the daemon_work three time to ensure everything is flushed
1376 * that can be
1377 */
1378 sleep = bitmap->daemon_sleep;
1379 bitmap->daemon_sleep = 0;
1380 bitmap_daemon_work(bitmap);
1381 bitmap_daemon_work(bitmap);
1382 bitmap_daemon_work(bitmap);
1383 bitmap->daemon_sleep = sleep;
1384 bitmap_update_sb(bitmap);
1385}
1386
1387/*
NeilBrown32a76272005-06-21 17:17:14 -07001388 * free memory that was allocated
1389 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001390static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001391{
1392 unsigned long k, pages;
1393 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001394
1395 if (!bitmap) /* there was no bitmap */
1396 return;
1397
NeilBrown32a76272005-06-21 17:17:14 -07001398 /* release the bitmap file and kill the daemon */
1399 bitmap_file_put(bitmap);
1400
1401 bp = bitmap->bp;
1402 pages = bitmap->pages;
1403
1404 /* free all allocated memory */
1405
1406 mempool_destroy(bitmap->write_pool);
1407
1408 if (bp) /* deallocate the page memory */
1409 for (k = 0; k < pages; k++)
1410 if (bp[k].map && !bp[k].hijacked)
1411 kfree(bp[k].map);
1412 kfree(bp);
1413 kfree(bitmap);
1414}
NeilBrown3178b0d2005-09-09 16:23:50 -07001415void bitmap_destroy(mddev_t *mddev)
1416{
1417 struct bitmap *bitmap = mddev->bitmap;
1418
1419 if (!bitmap) /* there was no bitmap */
1420 return;
1421
1422 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownb15c2e52006-01-06 00:20:16 -08001423 if (mddev->thread)
1424 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001425
1426 bitmap_free(bitmap);
1427}
NeilBrown32a76272005-06-21 17:17:14 -07001428
1429/*
1430 * initialize the bitmap structure
1431 * if this returns an error, bitmap_destroy must be called to do clean up
1432 */
1433int bitmap_create(mddev_t *mddev)
1434{
1435 struct bitmap *bitmap;
1436 unsigned long blocks = mddev->resync_max_sectors;
1437 unsigned long chunks;
1438 unsigned long pages;
1439 struct file *file = mddev->bitmap_file;
1440 int err;
NeilBrown6a079972005-09-09 16:23:44 -07001441 sector_t start;
NeilBrown32a76272005-06-21 17:17:14 -07001442
1443 BUG_ON(sizeof(bitmap_super_t) != 256);
1444
NeilBrowna654b9d82005-06-21 17:17:27 -07001445 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001446 return 0;
1447
NeilBrowna654b9d82005-06-21 17:17:27 -07001448 BUG_ON(file && mddev->bitmap_offset);
1449
NeilBrown9ffae0c2006-01-06 00:20:32 -08001450 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001451 if (!bitmap)
1452 return -ENOMEM;
1453
NeilBrown32a76272005-06-21 17:17:14 -07001454 spin_lock_init(&bitmap->lock);
1455 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001456
1457 spin_lock_init(&bitmap->write_lock);
NeilBrown32a76272005-06-21 17:17:14 -07001458 INIT_LIST_HEAD(&bitmap->complete_pages);
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08001459 bitmap->write_pool = mempool_create_kmalloc_pool(WRITE_POOL_SIZE,
1460 sizeof(struct page_list));
NeilBrown3178b0d2005-09-09 16:23:50 -07001461 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001462 if (!bitmap->write_pool)
NeilBrown3178b0d2005-09-09 16:23:50 -07001463 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001464
1465 bitmap->file = file;
NeilBrowna654b9d82005-06-21 17:17:27 -07001466 bitmap->offset = mddev->bitmap_offset;
1467 if (file) get_file(file);
NeilBrown32a76272005-06-21 17:17:14 -07001468 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1469 err = bitmap_read_sb(bitmap);
1470 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001471 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001472
1473 bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
1474 sizeof(bitmap->chunksize));
1475
1476 /* now that chunksize and chunkshift are set, we can use these macros */
1477 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1478 CHUNK_BLOCK_RATIO(bitmap);
1479 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1480
1481 BUG_ON(!pages);
1482
1483 bitmap->chunks = chunks;
1484 bitmap->pages = pages;
1485 bitmap->missing_pages = pages;
1486 bitmap->counter_bits = COUNTER_BITS;
1487
1488 bitmap->syncchunk = ~0UL;
1489
Olaf Hering44456d32005-07-27 11:45:17 -07001490#ifdef INJECT_FATAL_FAULT_1
NeilBrown32a76272005-06-21 17:17:14 -07001491 bitmap->bp = NULL;
1492#else
NeilBrown9ffae0c2006-01-06 00:20:32 -08001493 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001494#endif
NeilBrown3178b0d2005-09-09 16:23:50 -07001495 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001496 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001497 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001498
1499 bitmap->flags |= BITMAP_ACTIVE;
1500
1501 /* now that we have some pages available, initialize the in-memory
1502 * bitmap from the on-disk bitmap */
NeilBrown6a079972005-09-09 16:23:44 -07001503 start = 0;
1504 if (mddev->degraded == 0
1505 || bitmap->events_cleared == mddev->events)
1506 /* no need to keep dirty bits to optimise a re-add of a missing device */
1507 start = mddev->recovery_cp;
1508 err = bitmap_init_from_disk(bitmap, start);
NeilBrown193f1c92005-08-04 12:53:33 -07001509
NeilBrown32a76272005-06-21 17:17:14 -07001510 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001511 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001512
1513 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1514 pages, bmname(bitmap));
1515
NeilBrown3178b0d2005-09-09 16:23:50 -07001516 mddev->bitmap = bitmap;
1517
NeilBrownb15c2e52006-01-06 00:20:16 -08001518 mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1519
NeilBrown32a76272005-06-21 17:17:14 -07001520 return bitmap_update_sb(bitmap);
NeilBrown3178b0d2005-09-09 16:23:50 -07001521
1522 error:
1523 bitmap_free(bitmap);
1524 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001525}
1526
1527/* the bitmap API -- for raid personalities */
1528EXPORT_SYMBOL(bitmap_startwrite);
1529EXPORT_SYMBOL(bitmap_endwrite);
1530EXPORT_SYMBOL(bitmap_start_sync);
1531EXPORT_SYMBOL(bitmap_end_sync);
1532EXPORT_SYMBOL(bitmap_unplug);
1533EXPORT_SYMBOL(bitmap_close_sync);
1534EXPORT_SYMBOL(bitmap_daemon_work);