blob: f8ffaee20ff8cdea3e34c50b158452a2935598fd [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
10 * - added bitmap daemon (to asynchronously clear bitmap bits from disk)
11 */
12
13/*
14 * Still to do:
15 *
16 * flush after percent set rather than just time based. (maybe both).
17 * wait if count gets too high, wake when it drops to half.
18 * allow bitmap to be mirrored with superblock (before or after...)
19 * allow hot-add to re-instate a current device.
20 * allow hot-add of bitmap after quiessing device
21 */
22
23#include <linux/module.h>
NeilBrown32a76272005-06-21 17:17:14 -070024#include <linux/errno.h>
25#include <linux/slab.h>
26#include <linux/init.h>
27#include <linux/config.h>
28#include <linux/timer.h>
29#include <linux/sched.h>
30#include <linux/list.h>
31#include <linux/file.h>
32#include <linux/mount.h>
33#include <linux/buffer_head.h>
34#include <linux/raid/md.h>
35#include <linux/raid/bitmap.h>
36
37/* debug macros */
38
39#define DEBUG 0
40
41#if DEBUG
42/* these are for debugging purposes only! */
43
44/* define one and only one of these */
45#define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
46#define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
47#define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
48#define INJECT_FAULTS_4 0 /* undef */
49#define INJECT_FAULTS_5 0 /* undef */
50#define INJECT_FAULTS_6 0
51
52/* if these are defined, the driver will fail! debug only */
53#define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
54#define INJECT_FATAL_FAULT_2 0 /* undef */
55#define INJECT_FATAL_FAULT_3 0 /* undef */
56#endif
57
58//#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
59#define DPRINTK(x...) do { } while(0)
60
61#ifndef PRINTK
62# if DEBUG > 0
63# define PRINTK(x...) printk(KERN_DEBUG x)
64# else
65# define PRINTK(x...)
66# endif
67#endif
68
69static inline char * bmname(struct bitmap *bitmap)
70{
71 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
72}
73
74
75/*
76 * test if the bitmap is active
77 */
78int bitmap_active(struct bitmap *bitmap)
79{
80 unsigned long flags;
81 int res = 0;
82
83 if (!bitmap)
84 return res;
85 spin_lock_irqsave(&bitmap->lock, flags);
86 res = bitmap->flags & BITMAP_ACTIVE;
87 spin_unlock_irqrestore(&bitmap->lock, flags);
88 return res;
89}
90
91#define WRITE_POOL_SIZE 256
NeilBrown32a76272005-06-21 17:17:14 -070092
93/*
94 * just a placeholder - calls kmalloc for bitmap pages
95 */
96static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
97{
98 unsigned char *page;
99
Olaf Hering44456d32005-07-27 11:45:17 -0700100#ifdef INJECT_FAULTS_1
NeilBrown32a76272005-06-21 17:17:14 -0700101 page = NULL;
102#else
103 page = kmalloc(PAGE_SIZE, GFP_NOIO);
104#endif
105 if (!page)
106 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
107 else
NeilBrowna654b9d82005-06-21 17:17:27 -0700108 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
NeilBrown32a76272005-06-21 17:17:14 -0700109 bmname(bitmap), page);
110 return page;
111}
112
113/*
114 * for now just a placeholder -- just calls kfree for bitmap pages
115 */
116static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
117{
118 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
119 kfree(page);
120}
121
122/*
123 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
124 *
125 * 1) check to see if this page is allocated, if it's not then try to alloc
126 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
127 * page pointer directly as a counter
128 *
129 * if we find our page, we increment the page's refcount so that it stays
130 * allocated while we're using it
131 */
132static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
133{
134 unsigned char *mappage;
135
136 if (page >= bitmap->pages) {
137 printk(KERN_ALERT
138 "%s: invalid bitmap page request: %lu (> %lu)\n",
139 bmname(bitmap), page, bitmap->pages-1);
140 return -EINVAL;
141 }
142
143
144 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
145 return 0;
146
147 if (bitmap->bp[page].map) /* page is already allocated, just return */
148 return 0;
149
150 if (!create)
151 return -ENOENT;
152
153 spin_unlock_irq(&bitmap->lock);
154
155 /* this page has not been allocated yet */
156
157 if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
158 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
159 bmname(bitmap));
160 /* failed - set the hijacked flag so that we can use the
161 * pointer as a counter */
162 spin_lock_irq(&bitmap->lock);
163 if (!bitmap->bp[page].map)
164 bitmap->bp[page].hijacked = 1;
165 goto out;
166 }
167
168 /* got a page */
169
170 spin_lock_irq(&bitmap->lock);
171
172 /* recheck the page */
173
174 if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
175 /* somebody beat us to getting the page */
176 bitmap_free_page(bitmap, mappage);
177 return 0;
178 }
179
180 /* no page was in place and we have one, so install it */
181
182 memset(mappage, 0, PAGE_SIZE);
183 bitmap->bp[page].map = mappage;
184 bitmap->missing_pages--;
185out:
186 return 0;
187}
188
189
190/* if page is completely empty, put it back on the free list, or dealloc it */
191/* if page was hijacked, unmark the flag so it might get alloced next time */
192/* Note: lock should be held when calling this */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800193static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
NeilBrown32a76272005-06-21 17:17:14 -0700194{
195 char *ptr;
196
197 if (bitmap->bp[page].count) /* page is still busy */
198 return;
199
200 /* page is no longer in use, it can be released */
201
202 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
203 bitmap->bp[page].hijacked = 0;
204 bitmap->bp[page].map = NULL;
205 return;
206 }
207
208 /* normal case, free the page */
209
210#if 0
211/* actually ... let's not. We will probably need the page again exactly when
212 * memory is tight and we are flusing to disk
213 */
214 return;
215#else
216 ptr = bitmap->bp[page].map;
217 bitmap->bp[page].map = NULL;
218 bitmap->missing_pages++;
219 bitmap_free_page(bitmap, ptr);
220 return;
221#endif
222}
223
224
225/*
226 * bitmap file handling - read and write the bitmap file and its superblock
227 */
228
229/* copy the pathname of a file to a buffer */
230char *file_path(struct file *file, char *buf, int count)
231{
232 struct dentry *d;
233 struct vfsmount *v;
234
235 if (!buf)
236 return NULL;
237
238 d = file->f_dentry;
239 v = file->f_vfsmnt;
240
241 buf = d_path(d, v, buf, count);
242
243 return IS_ERR(buf) ? NULL : buf;
244}
245
246/*
247 * basic page I/O operations
248 */
249
NeilBrowna654b9d82005-06-21 17:17:27 -0700250/* IO operations when bitmap is stored near all superblocks */
251static struct page *read_sb_page(mddev_t *mddev, long offset, unsigned long index)
252{
253 /* choose a good rdev and read the page from there */
254
255 mdk_rdev_t *rdev;
256 struct list_head *tmp;
257 struct page *page = alloc_page(GFP_KERNEL);
258 sector_t target;
259
260 if (!page)
261 return ERR_PTR(-ENOMEM);
NeilBrowna654b9d82005-06-21 17:17:27 -0700262
NeilBrownab904d62005-09-09 16:23:52 -0700263 ITERATE_RDEV(mddev, rdev, tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800264 if (! test_bit(In_sync, &rdev->flags)
265 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700266 continue;
267
NeilBrowna654b9d82005-06-21 17:17:27 -0700268 target = (rdev->sb_offset << 1) + offset + index * (PAGE_SIZE/512);
269
NeilBrownab904d62005-09-09 16:23:52 -0700270 if (sync_page_io(rdev->bdev, target, PAGE_SIZE, page, READ)) {
271 page->index = index;
272 return page;
273 }
274 }
275 return ERR_PTR(-EIO);
NeilBrowna654b9d82005-06-21 17:17:27 -0700276
NeilBrowna654b9d82005-06-21 17:17:27 -0700277}
278
279static int write_sb_page(mddev_t *mddev, long offset, struct page *page, int wait)
280{
281 mdk_rdev_t *rdev;
282 struct list_head *tmp;
283
284 ITERATE_RDEV(mddev, rdev, tmp)
NeilBrownb2d444d2005-11-08 21:39:31 -0800285 if (test_bit(In_sync, &rdev->flags)
286 && !test_bit(Faulty, &rdev->flags))
NeilBrowna654b9d82005-06-21 17:17:27 -0700287 md_super_write(mddev, rdev,
288 (rdev->sb_offset<<1) + offset
289 + page->index * (PAGE_SIZE/512),
290 PAGE_SIZE,
291 page);
292
293 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800294 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700295 return 0;
296}
297
NeilBrown32a76272005-06-21 17:17:14 -0700298/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700299 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700300 */
NeilBrown77ad4bc2005-06-21 17:17:21 -0700301static int write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700302{
303 int ret = -ENOMEM;
304
NeilBrowna654b9d82005-06-21 17:17:27 -0700305 if (bitmap->file == NULL)
306 return write_sb_page(bitmap->mddev, bitmap->offset, page, wait);
307
NeilBrownc7084432006-01-06 00:20:45 -0800308 flush_dcache_page(page); /* make sure visible to anyone reading the file */
309
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700310 if (wait)
311 lock_page(page);
312 else {
313 if (TestSetPageLocked(page))
314 return -EAGAIN; /* already locked */
315 if (PageWriteback(page)) {
316 unlock_page(page);
317 return -EAGAIN;
318 }
319 }
NeilBrown32a76272005-06-21 17:17:14 -0700320
Neil Brown34ef75f2005-11-18 01:10:59 -0800321 ret = page->mapping->a_ops->prepare_write(bitmap->file, page, 0, PAGE_SIZE);
NeilBrown32a76272005-06-21 17:17:14 -0700322 if (!ret)
Neil Brown34ef75f2005-11-18 01:10:59 -0800323 ret = page->mapping->a_ops->commit_write(bitmap->file, page, 0,
NeilBrown32a76272005-06-21 17:17:14 -0700324 PAGE_SIZE);
325 if (ret) {
NeilBrown32a76272005-06-21 17:17:14 -0700326 unlock_page(page);
327 return ret;
328 }
329
330 set_page_dirty(page); /* force it to be written out */
NeilBrown77ad4bc2005-06-21 17:17:21 -0700331
332 if (!wait) {
333 /* add to list to be waited for by daemon */
334 struct page_list *item = mempool_alloc(bitmap->write_pool, GFP_NOIO);
335 item->page = page;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800336 get_page(page);
NeilBrown77ad4bc2005-06-21 17:17:21 -0700337 spin_lock(&bitmap->write_lock);
338 list_add(&item->list, &bitmap->complete_pages);
339 spin_unlock(&bitmap->write_lock);
340 md_wakeup_thread(bitmap->writeback_daemon);
341 }
NeilBrown32a76272005-06-21 17:17:14 -0700342 return write_one_page(page, wait);
343}
344
345/* read a page from a file, pinning it into cache, and return bytes_read */
346static struct page *read_page(struct file *file, unsigned long index,
347 unsigned long *bytes_read)
348{
349 struct inode *inode = file->f_mapping->host;
350 struct page *page = NULL;
351 loff_t isize = i_size_read(inode);
NeilBrown2d1f3b52006-01-06 00:20:31 -0800352 unsigned long end_index = isize >> PAGE_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700353
NeilBrown2d1f3b52006-01-06 00:20:31 -0800354 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
355 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700356
357 page = read_cache_page(inode->i_mapping, index,
358 (filler_t *)inode->i_mapping->a_ops->readpage, file);
359 if (IS_ERR(page))
360 goto out;
361 wait_on_page_locked(page);
362 if (!PageUptodate(page) || PageError(page)) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800363 put_page(page);
NeilBrown32a76272005-06-21 17:17:14 -0700364 page = ERR_PTR(-EIO);
365 goto out;
366 }
367
368 if (index > end_index) /* we have read beyond EOF */
369 *bytes_read = 0;
370 else if (index == end_index) /* possible short read */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800371 *bytes_read = isize & ~PAGE_MASK;
NeilBrown32a76272005-06-21 17:17:14 -0700372 else
NeilBrown2d1f3b52006-01-06 00:20:31 -0800373 *bytes_read = PAGE_SIZE; /* got a full page */
NeilBrown32a76272005-06-21 17:17:14 -0700374out:
375 if (IS_ERR(page))
376 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800377 (int)PAGE_SIZE,
378 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700379 PTR_ERR(page));
380 return page;
381}
382
383/*
384 * bitmap file superblock operations
385 */
386
387/* update the event counter and sync the superblock to disk */
388int bitmap_update_sb(struct bitmap *bitmap)
389{
390 bitmap_super_t *sb;
391 unsigned long flags;
392
393 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
394 return 0;
395 spin_lock_irqsave(&bitmap->lock, flags);
396 if (!bitmap->sb_page) { /* no superblock */
397 spin_unlock_irqrestore(&bitmap->lock, flags);
398 return 0;
399 }
NeilBrown32a76272005-06-21 17:17:14 -0700400 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800401 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700402 sb->events = cpu_to_le64(bitmap->mddev->events);
403 if (!bitmap->mddev->degraded)
404 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
NeilBrownea03aff2006-01-06 00:20:34 -0800405 kunmap_atomic(sb, KM_USER0);
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700406 return write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700407}
408
409/* print out the bitmap file superblock */
410void bitmap_print_sb(struct bitmap *bitmap)
411{
412 bitmap_super_t *sb;
413
414 if (!bitmap || !bitmap->sb_page)
415 return;
NeilBrownea03aff2006-01-06 00:20:34 -0800416 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700417 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700418 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
419 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
420 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700421 *(__u32 *)(sb->uuid+0),
422 *(__u32 *)(sb->uuid+4),
423 *(__u32 *)(sb->uuid+8),
424 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700425 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700426 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700427 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700428 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700429 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
430 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
431 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
432 printk(KERN_DEBUG " sync size: %llu KB\n",
433 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700434 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800435 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700436}
437
438/* read the superblock from the bitmap file and initialize some bitmap fields */
439static int bitmap_read_sb(struct bitmap *bitmap)
440{
441 char *reason = NULL;
442 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700443 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700444 unsigned long bytes_read;
445 unsigned long long events;
446 int err = -EINVAL;
447
448 /* page 0 is the superblock, read it... */
NeilBrowna654b9d82005-06-21 17:17:27 -0700449 if (bitmap->file)
450 bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read);
451 else {
452 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
453 bytes_read = PAGE_SIZE;
454 }
NeilBrown32a76272005-06-21 17:17:14 -0700455 if (IS_ERR(bitmap->sb_page)) {
456 err = PTR_ERR(bitmap->sb_page);
457 bitmap->sb_page = NULL;
458 return err;
459 }
460
NeilBrownea03aff2006-01-06 00:20:34 -0800461 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700462
463 if (bytes_read < sizeof(*sb)) { /* short read */
464 printk(KERN_INFO "%s: bitmap file superblock truncated\n",
465 bmname(bitmap));
466 err = -ENOSPC;
467 goto out;
468 }
469
470 chunksize = le32_to_cpu(sb->chunksize);
471 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
NeilBrown4b6d2872005-09-09 16:23:47 -0700472 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700473
474 /* verify that the bitmap-specific fields are valid */
475 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
476 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800477 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
478 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700479 reason = "unrecognized superblock version";
NeilBrown7dd5d342006-01-06 00:20:39 -0800480 else if (chunksize < PAGE_SIZE)
481 reason = "bitmap chunksize too small";
NeilBrown32a76272005-06-21 17:17:14 -0700482 else if ((1 << ffz(~chunksize)) != chunksize)
483 reason = "bitmap chunksize not a power of 2";
NeilBrown7dd5d342006-01-06 00:20:39 -0800484 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT / HZ)
485 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700486 else if (write_behind > COUNTER_MAX)
487 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700488 if (reason) {
489 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
490 bmname(bitmap), reason);
491 goto out;
492 }
493
494 /* keep the array size field of the bitmap superblock up to date */
495 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
496
497 if (!bitmap->mddev->persistent)
498 goto success;
499
500 /*
501 * if we have a persistent array superblock, compare the
502 * bitmap's UUID and event counter to the mddev's
503 */
504 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
505 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
506 bmname(bitmap));
507 goto out;
508 }
509 events = le64_to_cpu(sb->events);
510 if (events < bitmap->mddev->events) {
511 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
512 "-- forcing full recovery\n", bmname(bitmap), events,
513 (unsigned long long) bitmap->mddev->events);
514 sb->state |= BITMAP_STALE;
515 }
516success:
517 /* assign fields using values from superblock */
518 bitmap->chunksize = chunksize;
519 bitmap->daemon_sleep = daemon_sleep;
NeilBrown585f0dd2005-09-09 16:23:49 -0700520 bitmap->daemon_lastrun = jiffies;
NeilBrown4b6d2872005-09-09 16:23:47 -0700521 bitmap->max_write_behind = write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700522 bitmap->flags |= sb->state;
NeilBrownbd926c62005-11-08 21:39:32 -0800523 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
524 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700525 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown6a079972005-09-09 16:23:44 -0700526 if (sb->state & BITMAP_STALE)
527 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700528 err = 0;
529out:
NeilBrownea03aff2006-01-06 00:20:34 -0800530 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700531 if (err)
532 bitmap_print_sb(bitmap);
533 return err;
534}
535
536enum bitmap_mask_op {
537 MASK_SET,
538 MASK_UNSET
539};
540
541/* record the state of the bitmap in the superblock */
542static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
543 enum bitmap_mask_op op)
544{
545 bitmap_super_t *sb;
546 unsigned long flags;
547
548 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800549 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700550 spin_unlock_irqrestore(&bitmap->lock, flags);
551 return;
552 }
NeilBrown2d1f3b52006-01-06 00:20:31 -0800553 get_page(bitmap->sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700554 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800555 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700556 switch (op) {
557 case MASK_SET: sb->state |= bits;
558 break;
559 case MASK_UNSET: sb->state &= ~bits;
560 break;
561 default: BUG();
562 }
NeilBrownea03aff2006-01-06 00:20:34 -0800563 kunmap_atomic(sb, KM_USER0);
NeilBrown2d1f3b52006-01-06 00:20:31 -0800564 put_page(bitmap->sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700565}
566
567/*
568 * general bitmap file operations
569 */
570
571/* calculate the index of the page that contains this bit */
572static inline unsigned long file_page_index(unsigned long chunk)
573{
574 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
575}
576
577/* calculate the (bit) offset of this bit within a page */
578static inline unsigned long file_page_offset(unsigned long chunk)
579{
580 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
581}
582
583/*
584 * return a pointer to the page in the filemap that contains the given bit
585 *
586 * this lookup is complicated by the fact that the bitmap sb might be exactly
587 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
588 * 0 or page 1
589 */
590static inline struct page *filemap_get_page(struct bitmap *bitmap,
591 unsigned long chunk)
592{
593 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
594}
595
596
597static void bitmap_file_unmap(struct bitmap *bitmap)
598{
599 struct page **map, *sb_page;
600 unsigned long *attr;
601 int pages;
602 unsigned long flags;
603
604 spin_lock_irqsave(&bitmap->lock, flags);
605 map = bitmap->filemap;
606 bitmap->filemap = NULL;
607 attr = bitmap->filemap_attr;
608 bitmap->filemap_attr = NULL;
609 pages = bitmap->file_pages;
610 bitmap->file_pages = 0;
611 sb_page = bitmap->sb_page;
612 bitmap->sb_page = NULL;
613 spin_unlock_irqrestore(&bitmap->lock, flags);
614
615 while (pages--)
616 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800617 put_page(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700618 kfree(map);
619 kfree(attr);
620
NeilBrown1345b1d2006-01-06 00:20:40 -0800621 safe_put_page(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700622}
623
NeilBrown500af872005-09-09 16:23:58 -0700624static void bitmap_stop_daemon(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700625
626/* dequeue the next item in a page list -- don't call from irq context */
NeilBrown77ad4bc2005-06-21 17:17:21 -0700627static struct page_list *dequeue_page(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700628{
629 struct page_list *item = NULL;
NeilBrown77ad4bc2005-06-21 17:17:21 -0700630 struct list_head *head = &bitmap->complete_pages;
NeilBrown32a76272005-06-21 17:17:14 -0700631
632 spin_lock(&bitmap->write_lock);
633 if (list_empty(head))
634 goto out;
635 item = list_entry(head->prev, struct page_list, list);
636 list_del(head->prev);
637out:
638 spin_unlock(&bitmap->write_lock);
639 return item;
640}
641
642static void drain_write_queues(struct bitmap *bitmap)
643{
NeilBrown32a76272005-06-21 17:17:14 -0700644 struct page_list *item;
NeilBrown32a76272005-06-21 17:17:14 -0700645
NeilBrown77ad4bc2005-06-21 17:17:21 -0700646 while ((item = dequeue_page(bitmap))) {
647 /* don't bother to wait */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800648 put_page(item->page);
NeilBrown77ad4bc2005-06-21 17:17:21 -0700649 mempool_free(item, bitmap->write_pool);
NeilBrown32a76272005-06-21 17:17:14 -0700650 }
651
NeilBrown32a76272005-06-21 17:17:14 -0700652 wake_up(&bitmap->write_wait);
NeilBrown32a76272005-06-21 17:17:14 -0700653}
654
655static void bitmap_file_put(struct bitmap *bitmap)
656{
657 struct file *file;
658 struct inode *inode;
659 unsigned long flags;
660
661 spin_lock_irqsave(&bitmap->lock, flags);
662 file = bitmap->file;
663 bitmap->file = NULL;
664 spin_unlock_irqrestore(&bitmap->lock, flags);
665
NeilBrown500af872005-09-09 16:23:58 -0700666 bitmap_stop_daemon(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700667
668 drain_write_queues(bitmap);
669
670 bitmap_file_unmap(bitmap);
671
672 if (file) {
673 inode = file->f_mapping->host;
674 spin_lock(&inode->i_lock);
675 atomic_set(&inode->i_writecount, 1); /* allow writes again */
676 spin_unlock(&inode->i_lock);
677 fput(file);
678 }
679}
680
681
682/*
683 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
684 * then it is no longer reliable, so we stop using it and we mark the file
685 * as failed in the superblock
686 */
687static void bitmap_file_kick(struct bitmap *bitmap)
688{
689 char *path, *ptr = NULL;
690
691 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
692 bitmap_update_sb(bitmap);
693
NeilBrowna654b9d82005-06-21 17:17:27 -0700694 if (bitmap->file) {
695 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
696 if (path)
697 ptr = file_path(bitmap->file, path, PAGE_SIZE);
NeilBrown32a76272005-06-21 17:17:14 -0700698
NeilBrowna654b9d82005-06-21 17:17:27 -0700699 printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
700 bmname(bitmap), ptr ? ptr : "");
NeilBrown32a76272005-06-21 17:17:14 -0700701
NeilBrowna654b9d82005-06-21 17:17:27 -0700702 kfree(path);
703 }
NeilBrown32a76272005-06-21 17:17:14 -0700704
705 bitmap_file_put(bitmap);
706
707 return;
708}
709
710enum bitmap_page_attr {
711 BITMAP_PAGE_DIRTY = 1, // there are set bits that need to be synced
712 BITMAP_PAGE_CLEAN = 2, // there are bits that might need to be cleared
713 BITMAP_PAGE_NEEDWRITE=4, // there are cleared bits that need to be synced
714};
715
716static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
717 enum bitmap_page_attr attr)
718{
719 bitmap->filemap_attr[page->index] |= attr;
720}
721
722static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
723 enum bitmap_page_attr attr)
724{
725 bitmap->filemap_attr[page->index] &= ~attr;
726}
727
728static inline unsigned long get_page_attr(struct bitmap *bitmap, struct page *page)
729{
730 return bitmap->filemap_attr[page->index];
731}
732
733/*
734 * bitmap_file_set_bit -- called before performing a write to the md device
735 * to set (and eventually sync) a particular bit in the bitmap file
736 *
737 * we set the bit immediately, then we record the page number so that
738 * when an unplug occurs, we can flush the dirty pages out to disk
739 */
740static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
741{
742 unsigned long bit;
743 struct page *page;
744 void *kaddr;
745 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
746
NeilBrowna654b9d82005-06-21 17:17:27 -0700747 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700748 return;
749 }
750
751 page = filemap_get_page(bitmap, chunk);
752 bit = file_page_offset(chunk);
753
754
755 /* make sure the page stays cached until it gets written out */
756 if (! (get_page_attr(bitmap, page) & BITMAP_PAGE_DIRTY))
NeilBrown2d1f3b52006-01-06 00:20:31 -0800757 get_page(page);
NeilBrown32a76272005-06-21 17:17:14 -0700758
759 /* set the bit */
760 kaddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800761 if (bitmap->flags & BITMAP_HOSTENDIAN)
762 set_bit(bit, kaddr);
763 else
764 ext2_set_bit(bit, kaddr);
NeilBrown32a76272005-06-21 17:17:14 -0700765 kunmap_atomic(kaddr, KM_USER0);
766 PRINTK("set file bit %lu page %lu\n", bit, page->index);
767
768 /* record page number so it gets flushed to disk when unplug occurs */
769 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
770
771}
772
773/* this gets called when the md device is ready to unplug its underlying
774 * (slave) device queues -- before we let any writes go down, we need to
775 * sync the dirty pages of the bitmap file to disk */
776int bitmap_unplug(struct bitmap *bitmap)
777{
778 unsigned long i, attr, flags;
779 struct page *page;
780 int wait = 0;
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700781 int err;
NeilBrown32a76272005-06-21 17:17:14 -0700782
783 if (!bitmap)
784 return 0;
785
786 /* look at each page to see if there are any set bits that need to be
787 * flushed out to disk */
788 for (i = 0; i < bitmap->file_pages; i++) {
789 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700790 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700791 spin_unlock_irqrestore(&bitmap->lock, flags);
792 return 0;
793 }
794 page = bitmap->filemap[i];
795 attr = get_page_attr(bitmap, page);
796 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
797 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
798 if ((attr & BITMAP_PAGE_DIRTY))
799 wait = 1;
800 spin_unlock_irqrestore(&bitmap->lock, flags);
801
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700802 if (attr & (BITMAP_PAGE_DIRTY | BITMAP_PAGE_NEEDWRITE)) {
803 err = write_page(bitmap, page, 0);
804 if (err == -EAGAIN) {
805 if (attr & BITMAP_PAGE_DIRTY)
806 err = write_page(bitmap, page, 1);
807 else
808 err = 0;
809 }
810 if (err)
NeilBrownbfb39fb2005-06-21 17:17:20 -0700811 return 1;
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700812 }
NeilBrown32a76272005-06-21 17:17:14 -0700813 }
814 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrowna654b9d82005-06-21 17:17:27 -0700815 if (bitmap->file) {
816 spin_lock_irq(&bitmap->write_lock);
817 wait_event_lock_irq(bitmap->write_wait,
818 list_empty(&bitmap->complete_pages), bitmap->write_lock,
819 wake_up_process(bitmap->writeback_daemon->tsk));
820 spin_unlock_irq(&bitmap->write_lock);
821 } else
NeilBrowna9701a32005-11-08 21:39:34 -0800822 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700823 }
824 return 0;
825}
826
NeilBrown6a079972005-09-09 16:23:44 -0700827static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700828/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
829 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
830 * memory mapping of the bitmap file
831 * Special cases:
832 * if there's no bitmap file, or if the bitmap file had been
833 * previously kicked from the array, we mark all the bits as
834 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700835 *
836 * We ignore all bits for sectors that end earlier than 'start'.
837 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700838 */
NeilBrown6a079972005-09-09 16:23:44 -0700839static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700840{
841 unsigned long i, chunks, index, oldindex, bit;
842 struct page *page = NULL, *oldpage = NULL;
843 unsigned long num_pages, bit_cnt = 0;
844 struct file *file;
845 unsigned long bytes, offset, dummy;
846 int outofdate;
847 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800848 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700849
850 chunks = bitmap->chunks;
851 file = bitmap->file;
852
NeilBrowna654b9d82005-06-21 17:17:27 -0700853 BUG_ON(!file && !bitmap->offset);
NeilBrown32a76272005-06-21 17:17:14 -0700854
Olaf Hering44456d32005-07-27 11:45:17 -0700855#ifdef INJECT_FAULTS_3
NeilBrown32a76272005-06-21 17:17:14 -0700856 outofdate = 1;
857#else
858 outofdate = bitmap->flags & BITMAP_STALE;
859#endif
860 if (outofdate)
861 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
862 "recovery\n", bmname(bitmap));
863
864 bytes = (chunks + 7) / 8;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700865
NeilBrowncdbb4cc2005-06-21 17:17:18 -0700866 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700867
NeilBrowna654b9d82005-06-21 17:17:27 -0700868 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
NeilBrown32a76272005-06-21 17:17:14 -0700869 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
870 bmname(bitmap),
871 (unsigned long) i_size_read(file->f_mapping->host),
872 bytes + sizeof(bitmap_super_t));
873 goto out;
874 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700875
876 ret = -ENOMEM;
877
NeilBrown32a76272005-06-21 17:17:14 -0700878 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700879 if (!bitmap->filemap)
NeilBrown32a76272005-06-21 17:17:14 -0700880 goto out;
NeilBrown32a76272005-06-21 17:17:14 -0700881
NeilBrown9ffae0c2006-01-06 00:20:32 -0800882 bitmap->filemap_attr = kzalloc(sizeof(long) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700883 if (!bitmap->filemap_attr)
NeilBrown32a76272005-06-21 17:17:14 -0700884 goto out;
NeilBrown32a76272005-06-21 17:17:14 -0700885
NeilBrown32a76272005-06-21 17:17:14 -0700886 oldindex = ~0L;
887
888 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -0800889 int b;
NeilBrown32a76272005-06-21 17:17:14 -0700890 index = file_page_index(i);
891 bit = file_page_offset(i);
892 if (index != oldindex) { /* this is a new page, read it in */
893 /* unmap the old page, we're done with it */
NeilBrown32a76272005-06-21 17:17:14 -0700894 if (index == 0) {
895 /*
896 * if we're here then the superblock page
897 * contains some bits (PAGE_SIZE != sizeof sb)
898 * we've already read it in, so just use it
899 */
900 page = bitmap->sb_page;
901 offset = sizeof(bitmap_super_t);
NeilBrowna654b9d82005-06-21 17:17:27 -0700902 } else if (file) {
NeilBrown32a76272005-06-21 17:17:14 -0700903 page = read_page(file, index, &dummy);
NeilBrowna654b9d82005-06-21 17:17:27 -0700904 offset = 0;
905 } else {
906 page = read_sb_page(bitmap->mddev, bitmap->offset, index);
NeilBrown32a76272005-06-21 17:17:14 -0700907 offset = 0;
908 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700909 if (IS_ERR(page)) { /* read error */
910 ret = PTR_ERR(page);
911 goto out;
912 }
913
NeilBrown32a76272005-06-21 17:17:14 -0700914 oldindex = index;
915 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -0700916
917 if (outofdate) {
918 /*
919 * if bitmap is out of date, dirty the
920 * whole page and write it out
921 */
NeilBrownea03aff2006-01-06 00:20:34 -0800922 paddr = kmap_atomic(page, KM_USER0);
923 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -0700924 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -0800925 kunmap_atomic(paddr, KM_USER0);
NeilBrown77ad4bc2005-06-21 17:17:21 -0700926 ret = write_page(bitmap, page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700927 if (ret) {
NeilBrown32a76272005-06-21 17:17:14 -0700928 /* release, page not in filemap yet */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800929 put_page(page);
NeilBrown32a76272005-06-21 17:17:14 -0700930 goto out;
931 }
932 }
933
934 bitmap->filemap[bitmap->file_pages++] = page;
935 }
NeilBrownea03aff2006-01-06 00:20:34 -0800936 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800937 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -0800938 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -0800939 else
NeilBrownea03aff2006-01-06 00:20:34 -0800940 b = ext2_test_bit(bit, paddr);
941 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800942 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -0700943 /* if the disk bit is set, set the memory bit */
NeilBrown6a079972005-09-09 16:23:44 -0700944 bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
945 ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
946 );
NeilBrown32a76272005-06-21 17:17:14 -0700947 bit_cnt++;
NeilBrown6a079972005-09-09 16:23:44 -0700948 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -0700949 }
NeilBrown32a76272005-06-21 17:17:14 -0700950 }
951
952 /* everything went OK */
953 ret = 0;
954 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
955
NeilBrown32a76272005-06-21 17:17:14 -0700956 if (bit_cnt) { /* Kick recovery if any bits were set */
957 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
958 md_wakeup_thread(bitmap->mddev->thread);
959 }
960
961out:
962 printk(KERN_INFO "%s: bitmap initialized from disk: "
963 "read %lu/%lu pages, set %lu bits, status: %d\n",
964 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
965
966 return ret;
967}
968
NeilBrowna654b9d82005-06-21 17:17:27 -0700969void bitmap_write_all(struct bitmap *bitmap)
970{
971 /* We don't actually write all bitmap blocks here,
972 * just flag them as needing to be written
973 */
974
975 unsigned long chunks = bitmap->chunks;
976 unsigned long bytes = (chunks+7)/8 + sizeof(bitmap_super_t);
977 unsigned long num_pages = (bytes + PAGE_SIZE-1) / PAGE_SIZE;
978 while (num_pages--)
979 bitmap->filemap_attr[num_pages] |= BITMAP_PAGE_NEEDWRITE;
980}
981
NeilBrown32a76272005-06-21 17:17:14 -0700982
983static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
984{
985 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
986 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
987 bitmap->bp[page].count += inc;
988/*
989 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
990 (unsigned long long)offset, inc, bitmap->bp[page].count);
991*/
992 bitmap_checkfree(bitmap, page);
993}
994static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
995 sector_t offset, int *blocks,
996 int create);
997
998/*
999 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1000 * out to disk
1001 */
1002
1003int bitmap_daemon_work(struct bitmap *bitmap)
1004{
NeilBrownaa3163f2005-06-21 17:17:22 -07001005 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001006 unsigned long flags;
1007 struct page *page = NULL, *lastpage = NULL;
1008 int err = 0;
1009 int blocks;
1010 int attr;
NeilBrownea03aff2006-01-06 00:20:34 -08001011 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001012
1013 if (bitmap == NULL)
1014 return 0;
1015 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
1016 return 0;
1017 bitmap->daemon_lastrun = jiffies;
1018
1019 for (j = 0; j < bitmap->chunks; j++) {
1020 bitmap_counter_t *bmc;
1021 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -07001022 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -07001023 /* error or shutdown */
1024 spin_unlock_irqrestore(&bitmap->lock, flags);
1025 break;
1026 }
1027
1028 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001029
1030 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001031 /* skip this page unless it's marked as needing cleaning */
1032 if (!((attr=get_page_attr(bitmap, page)) & BITMAP_PAGE_CLEAN)) {
1033 if (attr & BITMAP_PAGE_NEEDWRITE) {
NeilBrown2d1f3b52006-01-06 00:20:31 -08001034 get_page(page);
NeilBrownaa3163f2005-06-21 17:17:22 -07001035 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1036 }
1037 spin_unlock_irqrestore(&bitmap->lock, flags);
1038 if (attr & BITMAP_PAGE_NEEDWRITE) {
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001039 switch (write_page(bitmap, page, 0)) {
1040 case -EAGAIN:
1041 set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1042 break;
1043 case 0:
1044 break;
1045 default:
NeilBrownaa3163f2005-06-21 17:17:22 -07001046 bitmap_file_kick(bitmap);
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001047 }
NeilBrown2d1f3b52006-01-06 00:20:31 -08001048 put_page(page);
NeilBrownaa3163f2005-06-21 17:17:22 -07001049 }
1050 continue;
1051 }
1052
NeilBrown32a76272005-06-21 17:17:14 -07001053 /* grab the new page, sync and release the old */
NeilBrown2d1f3b52006-01-06 00:20:31 -08001054 get_page(page);
NeilBrown32a76272005-06-21 17:17:14 -07001055 if (lastpage != NULL) {
1056 if (get_page_attr(bitmap, lastpage) & BITMAP_PAGE_NEEDWRITE) {
1057 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1058 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown77ad4bc2005-06-21 17:17:21 -07001059 err = write_page(bitmap, lastpage, 0);
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001060 if (err == -EAGAIN) {
1061 err = 0;
1062 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1063 }
NeilBrown32a76272005-06-21 17:17:14 -07001064 } else {
1065 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1066 spin_unlock_irqrestore(&bitmap->lock, flags);
1067 }
NeilBrown2d1f3b52006-01-06 00:20:31 -08001068 put_page(lastpage);
NeilBrown32a76272005-06-21 17:17:14 -07001069 if (err)
1070 bitmap_file_kick(bitmap);
1071 } else
1072 spin_unlock_irqrestore(&bitmap->lock, flags);
1073 lastpage = page;
NeilBrown32a76272005-06-21 17:17:14 -07001074/*
1075 printk("bitmap clean at page %lu\n", j);
1076*/
1077 spin_lock_irqsave(&bitmap->lock, flags);
1078 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1079 }
1080 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1081 &blocks, 0);
1082 if (bmc) {
1083/*
1084 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1085*/
1086 if (*bmc == 2) {
1087 *bmc=1; /* maybe clear the bit next time */
1088 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1089 } else if (*bmc == 1) {
1090 /* we can clear the bit */
1091 *bmc = 0;
1092 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1093 -1);
1094
1095 /* clear the bit */
NeilBrownea03aff2006-01-06 00:20:34 -08001096 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001097 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001098 clear_bit(file_page_offset(j), paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001099 else
NeilBrownea03aff2006-01-06 00:20:34 -08001100 ext2_clear_bit(file_page_offset(j), paddr);
1101 kunmap_atomic(paddr, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -07001102 }
1103 }
1104 spin_unlock_irqrestore(&bitmap->lock, flags);
1105 }
1106
1107 /* now sync the final page */
1108 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001109 spin_lock_irqsave(&bitmap->lock, flags);
1110 if (get_page_attr(bitmap, lastpage) &BITMAP_PAGE_NEEDWRITE) {
1111 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1112 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown77ad4bc2005-06-21 17:17:21 -07001113 err = write_page(bitmap, lastpage, 0);
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001114 if (err == -EAGAIN) {
1115 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1116 err = 0;
1117 }
NeilBrown32a76272005-06-21 17:17:14 -07001118 } else {
1119 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1120 spin_unlock_irqrestore(&bitmap->lock, flags);
1121 }
1122
NeilBrown2d1f3b52006-01-06 00:20:31 -08001123 put_page(lastpage);
NeilBrown32a76272005-06-21 17:17:14 -07001124 }
1125
1126 return err;
1127}
1128
1129static void daemon_exit(struct bitmap *bitmap, mdk_thread_t **daemon)
1130{
1131 mdk_thread_t *dmn;
1132 unsigned long flags;
1133
1134 /* if no one is waiting on us, we'll free the md thread struct
1135 * and exit, otherwise we let the waiter clean things up */
1136 spin_lock_irqsave(&bitmap->lock, flags);
1137 if ((dmn = *daemon)) { /* no one is waiting, cleanup and exit */
1138 *daemon = NULL;
1139 spin_unlock_irqrestore(&bitmap->lock, flags);
1140 kfree(dmn);
1141 complete_and_exit(NULL, 0); /* do_exit not exported */
1142 }
1143 spin_unlock_irqrestore(&bitmap->lock, flags);
1144}
1145
1146static void bitmap_writeback_daemon(mddev_t *mddev)
1147{
1148 struct bitmap *bitmap = mddev->bitmap;
1149 struct page *page;
1150 struct page_list *item;
1151 int err = 0;
1152
NeilBrown77ad4bc2005-06-21 17:17:21 -07001153 if (signal_pending(current)) {
1154 printk(KERN_INFO
1155 "%s: bitmap writeback daemon got signal, exiting...\n",
1156 bmname(bitmap));
1157 err = -EINTR;
1158 goto out;
1159 }
NeilBrown9ba00532005-09-09 16:23:57 -07001160 if (bitmap == NULL)
1161 /* about to be stopped. */
1162 return;
NeilBrown32a76272005-06-21 17:17:14 -07001163
NeilBrown77ad4bc2005-06-21 17:17:21 -07001164 PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
1165 /* wait on bitmap page writebacks */
1166 while ((item = dequeue_page(bitmap))) {
1167 page = item->page;
1168 mempool_free(item, bitmap->write_pool);
1169 PRINTK("wait on page writeback: %p\n", page);
1170 wait_on_page_writeback(page);
1171 PRINTK("finished page writeback: %p\n", page);
1172
1173 err = PageError(page);
NeilBrown2d1f3b52006-01-06 00:20:31 -08001174 put_page(page);
NeilBrown77ad4bc2005-06-21 17:17:21 -07001175 if (err) {
1176 printk(KERN_WARNING "%s: bitmap file writeback "
1177 "failed (page %lu): %d\n",
1178 bmname(bitmap), page->index, err);
1179 bitmap_file_kick(bitmap);
1180 goto out;
NeilBrown32a76272005-06-21 17:17:14 -07001181 }
1182 }
NeilBrown77ad4bc2005-06-21 17:17:21 -07001183 out:
1184 wake_up(&bitmap->write_wait);
NeilBrown32a76272005-06-21 17:17:14 -07001185 if (err) {
1186 printk(KERN_INFO "%s: bitmap writeback daemon exiting (%d)\n",
NeilBrown77ad4bc2005-06-21 17:17:21 -07001187 bmname(bitmap), err);
NeilBrown32a76272005-06-21 17:17:14 -07001188 daemon_exit(bitmap, &bitmap->writeback_daemon);
1189 }
NeilBrown32a76272005-06-21 17:17:14 -07001190}
1191
NeilBrown500af872005-09-09 16:23:58 -07001192static mdk_thread_t *bitmap_start_daemon(struct bitmap *bitmap,
NeilBrown32a76272005-06-21 17:17:14 -07001193 void (*func)(mddev_t *), char *name)
1194{
1195 mdk_thread_t *daemon;
NeilBrown32a76272005-06-21 17:17:14 -07001196 char namebuf[32];
1197
Olaf Hering44456d32005-07-27 11:45:17 -07001198#ifdef INJECT_FATAL_FAULT_2
NeilBrown32a76272005-06-21 17:17:14 -07001199 daemon = NULL;
1200#else
1201 sprintf(namebuf, "%%s_%s", name);
1202 daemon = md_register_thread(func, bitmap->mddev, namebuf);
1203#endif
1204 if (!daemon) {
1205 printk(KERN_ERR "%s: failed to start bitmap daemon\n",
1206 bmname(bitmap));
NeilBrown500af872005-09-09 16:23:58 -07001207 return ERR_PTR(-ECHILD);
NeilBrown32a76272005-06-21 17:17:14 -07001208 }
1209
NeilBrown32a76272005-06-21 17:17:14 -07001210 md_wakeup_thread(daemon); /* start it running */
1211
1212 PRINTK("%s: %s daemon (pid %d) started...\n",
NeilBrownd80a1382005-06-21 17:17:17 -07001213 bmname(bitmap), name, daemon->tsk->pid);
NeilBrown500af872005-09-09 16:23:58 -07001214
1215 return daemon;
NeilBrown32a76272005-06-21 17:17:14 -07001216}
1217
NeilBrown500af872005-09-09 16:23:58 -07001218static void bitmap_stop_daemon(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001219{
NeilBrown500af872005-09-09 16:23:58 -07001220 /* the daemon can't stop itself... it'll just exit instead... */
1221 if (bitmap->writeback_daemon && ! IS_ERR(bitmap->writeback_daemon) &&
1222 current->pid != bitmap->writeback_daemon->tsk->pid) {
1223 mdk_thread_t *daemon;
1224 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07001225
NeilBrown500af872005-09-09 16:23:58 -07001226 spin_lock_irqsave(&bitmap->lock, flags);
1227 daemon = bitmap->writeback_daemon;
1228 bitmap->writeback_daemon = NULL;
1229 spin_unlock_irqrestore(&bitmap->lock, flags);
1230 if (daemon && ! IS_ERR(daemon))
1231 md_unregister_thread(daemon); /* destroy the thread */
1232 }
NeilBrown32a76272005-06-21 17:17:14 -07001233}
1234
1235static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1236 sector_t offset, int *blocks,
1237 int create)
1238{
1239 /* If 'create', we might release the lock and reclaim it.
1240 * The lock must have been taken with interrupts enabled.
1241 * If !create, we don't release the lock.
1242 */
1243 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1244 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1245 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1246 sector_t csize;
1247
1248 if (bitmap_checkpage(bitmap, page, create) < 0) {
1249 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1250 *blocks = csize - (offset & (csize- 1));
1251 return NULL;
1252 }
1253 /* now locked ... */
1254
1255 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1256 /* should we use the first or second counter field
1257 * of the hijacked pointer? */
1258 int hi = (pageoff > PAGE_COUNTER_MASK);
1259 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1260 PAGE_COUNTER_SHIFT - 1);
1261 *blocks = csize - (offset & (csize- 1));
1262 return &((bitmap_counter_t *)
1263 &bitmap->bp[page].map)[hi];
1264 } else { /* page is allocated */
1265 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1266 *blocks = csize - (offset & (csize- 1));
1267 return (bitmap_counter_t *)
1268 &(bitmap->bp[page].map[pageoff]);
1269 }
1270}
1271
NeilBrown4b6d2872005-09-09 16:23:47 -07001272int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001273{
1274 if (!bitmap) return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001275
1276 if (behind) {
1277 atomic_inc(&bitmap->behind_writes);
1278 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1279 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1280 }
1281
NeilBrown32a76272005-06-21 17:17:14 -07001282 while (sectors) {
1283 int blocks;
1284 bitmap_counter_t *bmc;
1285
1286 spin_lock_irq(&bitmap->lock);
1287 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1288 if (!bmc) {
1289 spin_unlock_irq(&bitmap->lock);
1290 return 0;
1291 }
1292
1293 switch(*bmc) {
1294 case 0:
1295 bitmap_file_set_bit(bitmap, offset);
1296 bitmap_count_page(bitmap,offset, 1);
1297 blk_plug_device(bitmap->mddev->queue);
1298 /* fall through */
1299 case 1:
1300 *bmc = 2;
1301 }
Eric Sesterhenn5daf2cf2006-03-24 18:35:26 +01001302 BUG_ON((*bmc & COUNTER_MAX) == COUNTER_MAX);
NeilBrown32a76272005-06-21 17:17:14 -07001303 (*bmc)++;
1304
1305 spin_unlock_irq(&bitmap->lock);
1306
1307 offset += blocks;
1308 if (sectors > blocks)
1309 sectors -= blocks;
1310 else sectors = 0;
1311 }
1312 return 0;
1313}
1314
1315void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001316 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001317{
1318 if (!bitmap) return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001319 if (behind) {
1320 atomic_dec(&bitmap->behind_writes);
1321 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1322 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1323 }
1324
NeilBrown32a76272005-06-21 17:17:14 -07001325 while (sectors) {
1326 int blocks;
1327 unsigned long flags;
1328 bitmap_counter_t *bmc;
1329
1330 spin_lock_irqsave(&bitmap->lock, flags);
1331 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1332 if (!bmc) {
1333 spin_unlock_irqrestore(&bitmap->lock, flags);
1334 return;
1335 }
1336
1337 if (!success && ! (*bmc & NEEDED_MASK))
1338 *bmc |= NEEDED_MASK;
1339
1340 (*bmc)--;
1341 if (*bmc <= 2) {
1342 set_page_attr(bitmap,
1343 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1344 BITMAP_PAGE_CLEAN);
1345 }
1346 spin_unlock_irqrestore(&bitmap->lock, flags);
1347 offset += blocks;
1348 if (sectors > blocks)
1349 sectors -= blocks;
1350 else sectors = 0;
1351 }
1352}
1353
NeilBrown6a806c52005-07-15 03:56:35 -07001354int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1355 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001356{
1357 bitmap_counter_t *bmc;
1358 int rv;
1359 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1360 *blocks = 1024;
1361 return 1; /* always resync if no bitmap */
1362 }
1363 spin_lock_irq(&bitmap->lock);
1364 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1365 rv = 0;
1366 if (bmc) {
1367 /* locked */
1368 if (RESYNC(*bmc))
1369 rv = 1;
1370 else if (NEEDED(*bmc)) {
1371 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001372 if (!degraded) { /* don't set/clear bits if degraded */
1373 *bmc |= RESYNC_MASK;
1374 *bmc &= ~NEEDED_MASK;
1375 }
NeilBrown32a76272005-06-21 17:17:14 -07001376 }
1377 }
1378 spin_unlock_irq(&bitmap->lock);
1379 return rv;
1380}
1381
1382void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1383{
1384 bitmap_counter_t *bmc;
1385 unsigned long flags;
1386/*
1387 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1388*/ if (bitmap == NULL) {
1389 *blocks = 1024;
1390 return;
1391 }
1392 spin_lock_irqsave(&bitmap->lock, flags);
1393 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1394 if (bmc == NULL)
1395 goto unlock;
1396 /* locked */
1397/*
1398 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1399*/
1400 if (RESYNC(*bmc)) {
1401 *bmc &= ~RESYNC_MASK;
1402
1403 if (!NEEDED(*bmc) && aborted)
1404 *bmc |= NEEDED_MASK;
1405 else {
1406 if (*bmc <= 2) {
1407 set_page_attr(bitmap,
1408 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1409 BITMAP_PAGE_CLEAN);
1410 }
1411 }
1412 }
1413 unlock:
1414 spin_unlock_irqrestore(&bitmap->lock, flags);
1415}
1416
1417void bitmap_close_sync(struct bitmap *bitmap)
1418{
1419 /* Sync has finished, and any bitmap chunks that weren't synced
1420 * properly have been aborted. It remains to us to clear the
1421 * RESYNC bit wherever it is still on
1422 */
1423 sector_t sector = 0;
1424 int blocks;
1425 if (!bitmap) return;
1426 while (sector < bitmap->mddev->resync_max_sectors) {
1427 bitmap_end_sync(bitmap, sector, &blocks, 0);
1428/*
1429 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1430 (unsigned long long)sector, blocks);
1431*/ sector += blocks;
1432 }
1433}
1434
NeilBrown6a079972005-09-09 16:23:44 -07001435static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001436{
1437 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001438 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001439 * be 0 at this point
1440 */
NeilBrown193f1c92005-08-04 12:53:33 -07001441
1442 int secs;
1443 bitmap_counter_t *bmc;
1444 spin_lock_irq(&bitmap->lock);
1445 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1446 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001447 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001448 return;
NeilBrown32a76272005-06-21 17:17:14 -07001449 }
NeilBrown193f1c92005-08-04 12:53:33 -07001450 if (! *bmc) {
1451 struct page *page;
NeilBrown6a079972005-09-09 16:23:44 -07001452 *bmc = 1 | (needed?NEEDED_MASK:0);
NeilBrown193f1c92005-08-04 12:53:33 -07001453 bitmap_count_page(bitmap, offset, 1);
1454 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1455 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1456 }
1457 spin_unlock_irq(&bitmap->lock);
1458
NeilBrown32a76272005-06-21 17:17:14 -07001459}
1460
NeilBrown32a76272005-06-21 17:17:14 -07001461/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001462 * flush out any pending updates
1463 */
1464void bitmap_flush(mddev_t *mddev)
1465{
1466 struct bitmap *bitmap = mddev->bitmap;
1467 int sleep;
1468
1469 if (!bitmap) /* there was no bitmap */
1470 return;
1471
1472 /* run the daemon_work three time to ensure everything is flushed
1473 * that can be
1474 */
1475 sleep = bitmap->daemon_sleep;
1476 bitmap->daemon_sleep = 0;
1477 bitmap_daemon_work(bitmap);
1478 bitmap_daemon_work(bitmap);
1479 bitmap_daemon_work(bitmap);
1480 bitmap->daemon_sleep = sleep;
1481 bitmap_update_sb(bitmap);
1482}
1483
1484/*
NeilBrown32a76272005-06-21 17:17:14 -07001485 * free memory that was allocated
1486 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001487static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001488{
1489 unsigned long k, pages;
1490 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001491
1492 if (!bitmap) /* there was no bitmap */
1493 return;
1494
NeilBrown32a76272005-06-21 17:17:14 -07001495 /* release the bitmap file and kill the daemon */
1496 bitmap_file_put(bitmap);
1497
1498 bp = bitmap->bp;
1499 pages = bitmap->pages;
1500
1501 /* free all allocated memory */
1502
1503 mempool_destroy(bitmap->write_pool);
1504
1505 if (bp) /* deallocate the page memory */
1506 for (k = 0; k < pages; k++)
1507 if (bp[k].map && !bp[k].hijacked)
1508 kfree(bp[k].map);
1509 kfree(bp);
1510 kfree(bitmap);
1511}
NeilBrown3178b0d2005-09-09 16:23:50 -07001512void bitmap_destroy(mddev_t *mddev)
1513{
1514 struct bitmap *bitmap = mddev->bitmap;
1515
1516 if (!bitmap) /* there was no bitmap */
1517 return;
1518
1519 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownb15c2e52006-01-06 00:20:16 -08001520 if (mddev->thread)
1521 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001522
1523 bitmap_free(bitmap);
1524}
NeilBrown32a76272005-06-21 17:17:14 -07001525
1526/*
1527 * initialize the bitmap structure
1528 * if this returns an error, bitmap_destroy must be called to do clean up
1529 */
1530int bitmap_create(mddev_t *mddev)
1531{
1532 struct bitmap *bitmap;
1533 unsigned long blocks = mddev->resync_max_sectors;
1534 unsigned long chunks;
1535 unsigned long pages;
1536 struct file *file = mddev->bitmap_file;
1537 int err;
NeilBrown6a079972005-09-09 16:23:44 -07001538 sector_t start;
NeilBrown32a76272005-06-21 17:17:14 -07001539
1540 BUG_ON(sizeof(bitmap_super_t) != 256);
1541
NeilBrowna654b9d82005-06-21 17:17:27 -07001542 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001543 return 0;
1544
NeilBrowna654b9d82005-06-21 17:17:27 -07001545 BUG_ON(file && mddev->bitmap_offset);
1546
NeilBrown9ffae0c2006-01-06 00:20:32 -08001547 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001548 if (!bitmap)
1549 return -ENOMEM;
1550
NeilBrown32a76272005-06-21 17:17:14 -07001551 spin_lock_init(&bitmap->lock);
1552 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001553
1554 spin_lock_init(&bitmap->write_lock);
NeilBrown32a76272005-06-21 17:17:14 -07001555 INIT_LIST_HEAD(&bitmap->complete_pages);
1556 init_waitqueue_head(&bitmap->write_wait);
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08001557 bitmap->write_pool = mempool_create_kmalloc_pool(WRITE_POOL_SIZE,
1558 sizeof(struct page_list));
NeilBrown3178b0d2005-09-09 16:23:50 -07001559 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001560 if (!bitmap->write_pool)
NeilBrown3178b0d2005-09-09 16:23:50 -07001561 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001562
1563 bitmap->file = file;
NeilBrowna654b9d82005-06-21 17:17:27 -07001564 bitmap->offset = mddev->bitmap_offset;
1565 if (file) get_file(file);
NeilBrown32a76272005-06-21 17:17:14 -07001566 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1567 err = bitmap_read_sb(bitmap);
1568 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001569 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001570
1571 bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
1572 sizeof(bitmap->chunksize));
1573
1574 /* now that chunksize and chunkshift are set, we can use these macros */
1575 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1576 CHUNK_BLOCK_RATIO(bitmap);
1577 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1578
1579 BUG_ON(!pages);
1580
1581 bitmap->chunks = chunks;
1582 bitmap->pages = pages;
1583 bitmap->missing_pages = pages;
1584 bitmap->counter_bits = COUNTER_BITS;
1585
1586 bitmap->syncchunk = ~0UL;
1587
Olaf Hering44456d32005-07-27 11:45:17 -07001588#ifdef INJECT_FATAL_FAULT_1
NeilBrown32a76272005-06-21 17:17:14 -07001589 bitmap->bp = NULL;
1590#else
NeilBrown9ffae0c2006-01-06 00:20:32 -08001591 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001592#endif
NeilBrown3178b0d2005-09-09 16:23:50 -07001593 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001594 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001595 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001596
1597 bitmap->flags |= BITMAP_ACTIVE;
1598
1599 /* now that we have some pages available, initialize the in-memory
1600 * bitmap from the on-disk bitmap */
NeilBrown6a079972005-09-09 16:23:44 -07001601 start = 0;
1602 if (mddev->degraded == 0
1603 || bitmap->events_cleared == mddev->events)
1604 /* no need to keep dirty bits to optimise a re-add of a missing device */
1605 start = mddev->recovery_cp;
1606 err = bitmap_init_from_disk(bitmap, start);
NeilBrown193f1c92005-08-04 12:53:33 -07001607
NeilBrown32a76272005-06-21 17:17:14 -07001608 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001609 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001610
1611 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1612 pages, bmname(bitmap));
1613
NeilBrown3178b0d2005-09-09 16:23:50 -07001614 mddev->bitmap = bitmap;
1615
NeilBrown500af872005-09-09 16:23:58 -07001616 if (file)
1617 /* kick off the bitmap writeback daemon */
1618 bitmap->writeback_daemon =
1619 bitmap_start_daemon(bitmap,
1620 bitmap_writeback_daemon,
1621 "bitmap_wb");
1622
1623 if (IS_ERR(bitmap->writeback_daemon))
1624 return PTR_ERR(bitmap->writeback_daemon);
NeilBrownb15c2e52006-01-06 00:20:16 -08001625 mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1626
NeilBrown32a76272005-06-21 17:17:14 -07001627 return bitmap_update_sb(bitmap);
NeilBrown3178b0d2005-09-09 16:23:50 -07001628
1629 error:
1630 bitmap_free(bitmap);
1631 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001632}
1633
1634/* the bitmap API -- for raid personalities */
1635EXPORT_SYMBOL(bitmap_startwrite);
1636EXPORT_SYMBOL(bitmap_endwrite);
1637EXPORT_SYMBOL(bitmap_start_sync);
1638EXPORT_SYMBOL(bitmap_end_sync);
1639EXPORT_SYMBOL(bitmap_unplug);
1640EXPORT_SYMBOL(bitmap_close_sync);
1641EXPORT_SYMBOL(bitmap_daemon_work);