blob: ee4a3424a8a316b2f1888507b5a469fae0fd6f14 [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
92/* mempool for queueing pending writes on the bitmap file */
Al Virob4e3ca12005-10-21 03:22:34 -040093static void *write_pool_alloc(gfp_t gfp_flags, void *data)
NeilBrown32a76272005-06-21 17:17:14 -070094{
95 return kmalloc(sizeof(struct page_list), gfp_flags);
96}
97
98static void write_pool_free(void *ptr, void *data)
99{
100 kfree(ptr);
101}
102
103/*
104 * just a placeholder - calls kmalloc for bitmap pages
105 */
106static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
107{
108 unsigned char *page;
109
Olaf Hering44456d32005-07-27 11:45:17 -0700110#ifdef INJECT_FAULTS_1
NeilBrown32a76272005-06-21 17:17:14 -0700111 page = NULL;
112#else
113 page = kmalloc(PAGE_SIZE, GFP_NOIO);
114#endif
115 if (!page)
116 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
117 else
NeilBrowna654b9d82005-06-21 17:17:27 -0700118 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
NeilBrown32a76272005-06-21 17:17:14 -0700119 bmname(bitmap), page);
120 return page;
121}
122
123/*
124 * for now just a placeholder -- just calls kfree for bitmap pages
125 */
126static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
127{
128 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
129 kfree(page);
130}
131
132/*
133 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
134 *
135 * 1) check to see if this page is allocated, if it's not then try to alloc
136 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
137 * page pointer directly as a counter
138 *
139 * if we find our page, we increment the page's refcount so that it stays
140 * allocated while we're using it
141 */
142static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
143{
144 unsigned char *mappage;
145
146 if (page >= bitmap->pages) {
147 printk(KERN_ALERT
148 "%s: invalid bitmap page request: %lu (> %lu)\n",
149 bmname(bitmap), page, bitmap->pages-1);
150 return -EINVAL;
151 }
152
153
154 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
155 return 0;
156
157 if (bitmap->bp[page].map) /* page is already allocated, just return */
158 return 0;
159
160 if (!create)
161 return -ENOENT;
162
163 spin_unlock_irq(&bitmap->lock);
164
165 /* this page has not been allocated yet */
166
167 if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
168 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
169 bmname(bitmap));
170 /* failed - set the hijacked flag so that we can use the
171 * pointer as a counter */
172 spin_lock_irq(&bitmap->lock);
173 if (!bitmap->bp[page].map)
174 bitmap->bp[page].hijacked = 1;
175 goto out;
176 }
177
178 /* got a page */
179
180 spin_lock_irq(&bitmap->lock);
181
182 /* recheck the page */
183
184 if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
185 /* somebody beat us to getting the page */
186 bitmap_free_page(bitmap, mappage);
187 return 0;
188 }
189
190 /* no page was in place and we have one, so install it */
191
192 memset(mappage, 0, PAGE_SIZE);
193 bitmap->bp[page].map = mappage;
194 bitmap->missing_pages--;
195out:
196 return 0;
197}
198
199
200/* if page is completely empty, put it back on the free list, or dealloc it */
201/* if page was hijacked, unmark the flag so it might get alloced next time */
202/* Note: lock should be held when calling this */
203static inline void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
204{
205 char *ptr;
206
207 if (bitmap->bp[page].count) /* page is still busy */
208 return;
209
210 /* page is no longer in use, it can be released */
211
212 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
213 bitmap->bp[page].hijacked = 0;
214 bitmap->bp[page].map = NULL;
215 return;
216 }
217
218 /* normal case, free the page */
219
220#if 0
221/* actually ... let's not. We will probably need the page again exactly when
222 * memory is tight and we are flusing to disk
223 */
224 return;
225#else
226 ptr = bitmap->bp[page].map;
227 bitmap->bp[page].map = NULL;
228 bitmap->missing_pages++;
229 bitmap_free_page(bitmap, ptr);
230 return;
231#endif
232}
233
234
235/*
236 * bitmap file handling - read and write the bitmap file and its superblock
237 */
238
239/* copy the pathname of a file to a buffer */
240char *file_path(struct file *file, char *buf, int count)
241{
242 struct dentry *d;
243 struct vfsmount *v;
244
245 if (!buf)
246 return NULL;
247
248 d = file->f_dentry;
249 v = file->f_vfsmnt;
250
251 buf = d_path(d, v, buf, count);
252
253 return IS_ERR(buf) ? NULL : buf;
254}
255
256/*
257 * basic page I/O operations
258 */
259
NeilBrowna654b9d82005-06-21 17:17:27 -0700260/* IO operations when bitmap is stored near all superblocks */
261static struct page *read_sb_page(mddev_t *mddev, long offset, unsigned long index)
262{
263 /* choose a good rdev and read the page from there */
264
265 mdk_rdev_t *rdev;
266 struct list_head *tmp;
267 struct page *page = alloc_page(GFP_KERNEL);
268 sector_t target;
269
270 if (!page)
271 return ERR_PTR(-ENOMEM);
NeilBrowna654b9d82005-06-21 17:17:27 -0700272
NeilBrownab904d62005-09-09 16:23:52 -0700273 ITERATE_RDEV(mddev, rdev, tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800274 if (! test_bit(In_sync, &rdev->flags)
275 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700276 continue;
277
NeilBrowna654b9d82005-06-21 17:17:27 -0700278 target = (rdev->sb_offset << 1) + offset + index * (PAGE_SIZE/512);
279
NeilBrownab904d62005-09-09 16:23:52 -0700280 if (sync_page_io(rdev->bdev, target, PAGE_SIZE, page, READ)) {
281 page->index = index;
282 return page;
283 }
284 }
285 return ERR_PTR(-EIO);
NeilBrowna654b9d82005-06-21 17:17:27 -0700286
NeilBrowna654b9d82005-06-21 17:17:27 -0700287}
288
289static int write_sb_page(mddev_t *mddev, long offset, struct page *page, int wait)
290{
291 mdk_rdev_t *rdev;
292 struct list_head *tmp;
293
294 ITERATE_RDEV(mddev, rdev, tmp)
NeilBrownb2d444d2005-11-08 21:39:31 -0800295 if (test_bit(In_sync, &rdev->flags)
296 && !test_bit(Faulty, &rdev->flags))
NeilBrowna654b9d82005-06-21 17:17:27 -0700297 md_super_write(mddev, rdev,
298 (rdev->sb_offset<<1) + offset
299 + page->index * (PAGE_SIZE/512),
300 PAGE_SIZE,
301 page);
302
303 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800304 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700305 return 0;
306}
307
NeilBrown32a76272005-06-21 17:17:14 -0700308/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700309 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700310 */
NeilBrown77ad4bc2005-06-21 17:17:21 -0700311static int write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700312{
313 int ret = -ENOMEM;
314
NeilBrowna654b9d82005-06-21 17:17:27 -0700315 if (bitmap->file == NULL)
316 return write_sb_page(bitmap->mddev, bitmap->offset, page, wait);
317
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700318 if (wait)
319 lock_page(page);
320 else {
321 if (TestSetPageLocked(page))
322 return -EAGAIN; /* already locked */
323 if (PageWriteback(page)) {
324 unlock_page(page);
325 return -EAGAIN;
326 }
327 }
NeilBrown32a76272005-06-21 17:17:14 -0700328
Neil Brown34ef75f2005-11-18 01:10:59 -0800329 ret = page->mapping->a_ops->prepare_write(bitmap->file, page, 0, PAGE_SIZE);
NeilBrown32a76272005-06-21 17:17:14 -0700330 if (!ret)
Neil Brown34ef75f2005-11-18 01:10:59 -0800331 ret = page->mapping->a_ops->commit_write(bitmap->file, page, 0,
NeilBrown32a76272005-06-21 17:17:14 -0700332 PAGE_SIZE);
333 if (ret) {
NeilBrown32a76272005-06-21 17:17:14 -0700334 unlock_page(page);
335 return ret;
336 }
337
338 set_page_dirty(page); /* force it to be written out */
NeilBrown77ad4bc2005-06-21 17:17:21 -0700339
340 if (!wait) {
341 /* add to list to be waited for by daemon */
342 struct page_list *item = mempool_alloc(bitmap->write_pool, GFP_NOIO);
343 item->page = page;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800344 get_page(page);
NeilBrown77ad4bc2005-06-21 17:17:21 -0700345 spin_lock(&bitmap->write_lock);
346 list_add(&item->list, &bitmap->complete_pages);
347 spin_unlock(&bitmap->write_lock);
348 md_wakeup_thread(bitmap->writeback_daemon);
349 }
NeilBrown32a76272005-06-21 17:17:14 -0700350 return write_one_page(page, wait);
351}
352
353/* read a page from a file, pinning it into cache, and return bytes_read */
354static struct page *read_page(struct file *file, unsigned long index,
355 unsigned long *bytes_read)
356{
357 struct inode *inode = file->f_mapping->host;
358 struct page *page = NULL;
359 loff_t isize = i_size_read(inode);
NeilBrown2d1f3b52006-01-06 00:20:31 -0800360 unsigned long end_index = isize >> PAGE_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700361
NeilBrown2d1f3b52006-01-06 00:20:31 -0800362 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
363 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700364
365 page = read_cache_page(inode->i_mapping, index,
366 (filler_t *)inode->i_mapping->a_ops->readpage, file);
367 if (IS_ERR(page))
368 goto out;
369 wait_on_page_locked(page);
370 if (!PageUptodate(page) || PageError(page)) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800371 put_page(page);
NeilBrown32a76272005-06-21 17:17:14 -0700372 page = ERR_PTR(-EIO);
373 goto out;
374 }
375
376 if (index > end_index) /* we have read beyond EOF */
377 *bytes_read = 0;
378 else if (index == end_index) /* possible short read */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800379 *bytes_read = isize & ~PAGE_MASK;
NeilBrown32a76272005-06-21 17:17:14 -0700380 else
NeilBrown2d1f3b52006-01-06 00:20:31 -0800381 *bytes_read = PAGE_SIZE; /* got a full page */
NeilBrown32a76272005-06-21 17:17:14 -0700382out:
383 if (IS_ERR(page))
384 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800385 (int)PAGE_SIZE,
386 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700387 PTR_ERR(page));
388 return page;
389}
390
391/*
392 * bitmap file superblock operations
393 */
394
395/* update the event counter and sync the superblock to disk */
396int bitmap_update_sb(struct bitmap *bitmap)
397{
398 bitmap_super_t *sb;
399 unsigned long flags;
400
401 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
402 return 0;
403 spin_lock_irqsave(&bitmap->lock, flags);
404 if (!bitmap->sb_page) { /* no superblock */
405 spin_unlock_irqrestore(&bitmap->lock, flags);
406 return 0;
407 }
NeilBrown32a76272005-06-21 17:17:14 -0700408 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800409 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700410 sb->events = cpu_to_le64(bitmap->mddev->events);
411 if (!bitmap->mddev->degraded)
412 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
NeilBrownea03aff2006-01-06 00:20:34 -0800413 kunmap_atomic(sb, KM_USER0);
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700414 return write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700415}
416
417/* print out the bitmap file superblock */
418void bitmap_print_sb(struct bitmap *bitmap)
419{
420 bitmap_super_t *sb;
421
422 if (!bitmap || !bitmap->sb_page)
423 return;
NeilBrownea03aff2006-01-06 00:20:34 -0800424 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700425 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700426 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
427 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
428 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700429 *(__u32 *)(sb->uuid+0),
430 *(__u32 *)(sb->uuid+4),
431 *(__u32 *)(sb->uuid+8),
432 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700433 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700434 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700435 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700436 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700437 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
438 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
439 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
440 printk(KERN_DEBUG " sync size: %llu KB\n",
441 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700442 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800443 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700444}
445
446/* read the superblock from the bitmap file and initialize some bitmap fields */
447static int bitmap_read_sb(struct bitmap *bitmap)
448{
449 char *reason = NULL;
450 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700451 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700452 unsigned long bytes_read;
453 unsigned long long events;
454 int err = -EINVAL;
455
456 /* page 0 is the superblock, read it... */
NeilBrowna654b9d82005-06-21 17:17:27 -0700457 if (bitmap->file)
458 bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read);
459 else {
460 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
461 bytes_read = PAGE_SIZE;
462 }
NeilBrown32a76272005-06-21 17:17:14 -0700463 if (IS_ERR(bitmap->sb_page)) {
464 err = PTR_ERR(bitmap->sb_page);
465 bitmap->sb_page = NULL;
466 return err;
467 }
468
NeilBrownea03aff2006-01-06 00:20:34 -0800469 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700470
471 if (bytes_read < sizeof(*sb)) { /* short read */
472 printk(KERN_INFO "%s: bitmap file superblock truncated\n",
473 bmname(bitmap));
474 err = -ENOSPC;
475 goto out;
476 }
477
478 chunksize = le32_to_cpu(sb->chunksize);
479 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
NeilBrown4b6d2872005-09-09 16:23:47 -0700480 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700481
482 /* verify that the bitmap-specific fields are valid */
483 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
484 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800485 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
486 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700487 reason = "unrecognized superblock version";
NeilBrown7dd5d342006-01-06 00:20:39 -0800488 else if (chunksize < PAGE_SIZE)
489 reason = "bitmap chunksize too small";
NeilBrown32a76272005-06-21 17:17:14 -0700490 else if ((1 << ffz(~chunksize)) != chunksize)
491 reason = "bitmap chunksize not a power of 2";
NeilBrown7dd5d342006-01-06 00:20:39 -0800492 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT / HZ)
493 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700494 else if (write_behind > COUNTER_MAX)
495 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700496 if (reason) {
497 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
498 bmname(bitmap), reason);
499 goto out;
500 }
501
502 /* keep the array size field of the bitmap superblock up to date */
503 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
504
505 if (!bitmap->mddev->persistent)
506 goto success;
507
508 /*
509 * if we have a persistent array superblock, compare the
510 * bitmap's UUID and event counter to the mddev's
511 */
512 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
513 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
514 bmname(bitmap));
515 goto out;
516 }
517 events = le64_to_cpu(sb->events);
518 if (events < bitmap->mddev->events) {
519 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
520 "-- forcing full recovery\n", bmname(bitmap), events,
521 (unsigned long long) bitmap->mddev->events);
522 sb->state |= BITMAP_STALE;
523 }
524success:
525 /* assign fields using values from superblock */
526 bitmap->chunksize = chunksize;
527 bitmap->daemon_sleep = daemon_sleep;
NeilBrown585f0dd2005-09-09 16:23:49 -0700528 bitmap->daemon_lastrun = jiffies;
NeilBrown4b6d2872005-09-09 16:23:47 -0700529 bitmap->max_write_behind = write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700530 bitmap->flags |= sb->state;
NeilBrownbd926c62005-11-08 21:39:32 -0800531 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
532 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700533 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown6a079972005-09-09 16:23:44 -0700534 if (sb->state & BITMAP_STALE)
535 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700536 err = 0;
537out:
NeilBrownea03aff2006-01-06 00:20:34 -0800538 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700539 if (err)
540 bitmap_print_sb(bitmap);
541 return err;
542}
543
544enum bitmap_mask_op {
545 MASK_SET,
546 MASK_UNSET
547};
548
549/* record the state of the bitmap in the superblock */
550static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
551 enum bitmap_mask_op op)
552{
553 bitmap_super_t *sb;
554 unsigned long flags;
555
556 spin_lock_irqsave(&bitmap->lock, flags);
557 if (!bitmap || !bitmap->sb_page) { /* can't set the state */
558 spin_unlock_irqrestore(&bitmap->lock, flags);
559 return;
560 }
NeilBrown2d1f3b52006-01-06 00:20:31 -0800561 get_page(bitmap->sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700562 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800563 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700564 switch (op) {
565 case MASK_SET: sb->state |= bits;
566 break;
567 case MASK_UNSET: sb->state &= ~bits;
568 break;
569 default: BUG();
570 }
NeilBrownea03aff2006-01-06 00:20:34 -0800571 kunmap_atomic(sb, KM_USER0);
NeilBrown2d1f3b52006-01-06 00:20:31 -0800572 put_page(bitmap->sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700573}
574
575/*
576 * general bitmap file operations
577 */
578
579/* calculate the index of the page that contains this bit */
580static inline unsigned long file_page_index(unsigned long chunk)
581{
582 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
583}
584
585/* calculate the (bit) offset of this bit within a page */
586static inline unsigned long file_page_offset(unsigned long chunk)
587{
588 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
589}
590
591/*
592 * return a pointer to the page in the filemap that contains the given bit
593 *
594 * this lookup is complicated by the fact that the bitmap sb might be exactly
595 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
596 * 0 or page 1
597 */
598static inline struct page *filemap_get_page(struct bitmap *bitmap,
599 unsigned long chunk)
600{
601 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
602}
603
604
605static void bitmap_file_unmap(struct bitmap *bitmap)
606{
607 struct page **map, *sb_page;
608 unsigned long *attr;
609 int pages;
610 unsigned long flags;
611
612 spin_lock_irqsave(&bitmap->lock, flags);
613 map = bitmap->filemap;
614 bitmap->filemap = NULL;
615 attr = bitmap->filemap_attr;
616 bitmap->filemap_attr = NULL;
617 pages = bitmap->file_pages;
618 bitmap->file_pages = 0;
619 sb_page = bitmap->sb_page;
620 bitmap->sb_page = NULL;
621 spin_unlock_irqrestore(&bitmap->lock, flags);
622
623 while (pages--)
624 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800625 put_page(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700626 kfree(map);
627 kfree(attr);
628
NeilBrown1345b1d2006-01-06 00:20:40 -0800629 safe_put_page(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700630}
631
NeilBrown500af872005-09-09 16:23:58 -0700632static void bitmap_stop_daemon(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700633
634/* dequeue the next item in a page list -- don't call from irq context */
NeilBrown77ad4bc2005-06-21 17:17:21 -0700635static struct page_list *dequeue_page(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700636{
637 struct page_list *item = NULL;
NeilBrown77ad4bc2005-06-21 17:17:21 -0700638 struct list_head *head = &bitmap->complete_pages;
NeilBrown32a76272005-06-21 17:17:14 -0700639
640 spin_lock(&bitmap->write_lock);
641 if (list_empty(head))
642 goto out;
643 item = list_entry(head->prev, struct page_list, list);
644 list_del(head->prev);
645out:
646 spin_unlock(&bitmap->write_lock);
647 return item;
648}
649
650static void drain_write_queues(struct bitmap *bitmap)
651{
NeilBrown32a76272005-06-21 17:17:14 -0700652 struct page_list *item;
NeilBrown32a76272005-06-21 17:17:14 -0700653
NeilBrown77ad4bc2005-06-21 17:17:21 -0700654 while ((item = dequeue_page(bitmap))) {
655 /* don't bother to wait */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800656 put_page(item->page);
NeilBrown77ad4bc2005-06-21 17:17:21 -0700657 mempool_free(item, bitmap->write_pool);
NeilBrown32a76272005-06-21 17:17:14 -0700658 }
659
NeilBrown32a76272005-06-21 17:17:14 -0700660 wake_up(&bitmap->write_wait);
NeilBrown32a76272005-06-21 17:17:14 -0700661}
662
663static void bitmap_file_put(struct bitmap *bitmap)
664{
665 struct file *file;
666 struct inode *inode;
667 unsigned long flags;
668
669 spin_lock_irqsave(&bitmap->lock, flags);
670 file = bitmap->file;
671 bitmap->file = NULL;
672 spin_unlock_irqrestore(&bitmap->lock, flags);
673
NeilBrown500af872005-09-09 16:23:58 -0700674 bitmap_stop_daemon(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700675
676 drain_write_queues(bitmap);
677
678 bitmap_file_unmap(bitmap);
679
680 if (file) {
681 inode = file->f_mapping->host;
682 spin_lock(&inode->i_lock);
683 atomic_set(&inode->i_writecount, 1); /* allow writes again */
684 spin_unlock(&inode->i_lock);
685 fput(file);
686 }
687}
688
689
690/*
691 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
692 * then it is no longer reliable, so we stop using it and we mark the file
693 * as failed in the superblock
694 */
695static void bitmap_file_kick(struct bitmap *bitmap)
696{
697 char *path, *ptr = NULL;
698
699 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
700 bitmap_update_sb(bitmap);
701
NeilBrowna654b9d82005-06-21 17:17:27 -0700702 if (bitmap->file) {
703 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
704 if (path)
705 ptr = file_path(bitmap->file, path, PAGE_SIZE);
NeilBrown32a76272005-06-21 17:17:14 -0700706
NeilBrowna654b9d82005-06-21 17:17:27 -0700707 printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
708 bmname(bitmap), ptr ? ptr : "");
NeilBrown32a76272005-06-21 17:17:14 -0700709
NeilBrowna654b9d82005-06-21 17:17:27 -0700710 kfree(path);
711 }
NeilBrown32a76272005-06-21 17:17:14 -0700712
713 bitmap_file_put(bitmap);
714
715 return;
716}
717
718enum bitmap_page_attr {
719 BITMAP_PAGE_DIRTY = 1, // there are set bits that need to be synced
720 BITMAP_PAGE_CLEAN = 2, // there are bits that might need to be cleared
721 BITMAP_PAGE_NEEDWRITE=4, // there are cleared bits that need to be synced
722};
723
724static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
725 enum bitmap_page_attr attr)
726{
727 bitmap->filemap_attr[page->index] |= attr;
728}
729
730static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
731 enum bitmap_page_attr attr)
732{
733 bitmap->filemap_attr[page->index] &= ~attr;
734}
735
736static inline unsigned long get_page_attr(struct bitmap *bitmap, struct page *page)
737{
738 return bitmap->filemap_attr[page->index];
739}
740
741/*
742 * bitmap_file_set_bit -- called before performing a write to the md device
743 * to set (and eventually sync) a particular bit in the bitmap file
744 *
745 * we set the bit immediately, then we record the page number so that
746 * when an unplug occurs, we can flush the dirty pages out to disk
747 */
748static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
749{
750 unsigned long bit;
751 struct page *page;
752 void *kaddr;
753 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
754
NeilBrowna654b9d82005-06-21 17:17:27 -0700755 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700756 return;
757 }
758
759 page = filemap_get_page(bitmap, chunk);
760 bit = file_page_offset(chunk);
761
762
763 /* make sure the page stays cached until it gets written out */
764 if (! (get_page_attr(bitmap, page) & BITMAP_PAGE_DIRTY))
NeilBrown2d1f3b52006-01-06 00:20:31 -0800765 get_page(page);
NeilBrown32a76272005-06-21 17:17:14 -0700766
767 /* set the bit */
768 kaddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800769 if (bitmap->flags & BITMAP_HOSTENDIAN)
770 set_bit(bit, kaddr);
771 else
772 ext2_set_bit(bit, kaddr);
NeilBrown32a76272005-06-21 17:17:14 -0700773 kunmap_atomic(kaddr, KM_USER0);
774 PRINTK("set file bit %lu page %lu\n", bit, page->index);
775
776 /* record page number so it gets flushed to disk when unplug occurs */
777 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
778
779}
780
781/* this gets called when the md device is ready to unplug its underlying
782 * (slave) device queues -- before we let any writes go down, we need to
783 * sync the dirty pages of the bitmap file to disk */
784int bitmap_unplug(struct bitmap *bitmap)
785{
786 unsigned long i, attr, flags;
787 struct page *page;
788 int wait = 0;
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700789 int err;
NeilBrown32a76272005-06-21 17:17:14 -0700790
791 if (!bitmap)
792 return 0;
793
794 /* look at each page to see if there are any set bits that need to be
795 * flushed out to disk */
796 for (i = 0; i < bitmap->file_pages; i++) {
797 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700798 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700799 spin_unlock_irqrestore(&bitmap->lock, flags);
800 return 0;
801 }
802 page = bitmap->filemap[i];
803 attr = get_page_attr(bitmap, page);
804 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
805 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
806 if ((attr & BITMAP_PAGE_DIRTY))
807 wait = 1;
808 spin_unlock_irqrestore(&bitmap->lock, flags);
809
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700810 if (attr & (BITMAP_PAGE_DIRTY | BITMAP_PAGE_NEEDWRITE)) {
811 err = write_page(bitmap, page, 0);
812 if (err == -EAGAIN) {
813 if (attr & BITMAP_PAGE_DIRTY)
814 err = write_page(bitmap, page, 1);
815 else
816 err = 0;
817 }
818 if (err)
NeilBrownbfb39fb2005-06-21 17:17:20 -0700819 return 1;
NeilBrown8a5e9cf2005-06-21 17:17:29 -0700820 }
NeilBrown32a76272005-06-21 17:17:14 -0700821 }
822 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrowna654b9d82005-06-21 17:17:27 -0700823 if (bitmap->file) {
824 spin_lock_irq(&bitmap->write_lock);
825 wait_event_lock_irq(bitmap->write_wait,
826 list_empty(&bitmap->complete_pages), bitmap->write_lock,
827 wake_up_process(bitmap->writeback_daemon->tsk));
828 spin_unlock_irq(&bitmap->write_lock);
829 } else
NeilBrowna9701a32005-11-08 21:39:34 -0800830 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700831 }
832 return 0;
833}
834
NeilBrown6a079972005-09-09 16:23:44 -0700835static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700836/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
837 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
838 * memory mapping of the bitmap file
839 * Special cases:
840 * if there's no bitmap file, or if the bitmap file had been
841 * previously kicked from the array, we mark all the bits as
842 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700843 *
844 * We ignore all bits for sectors that end earlier than 'start'.
845 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700846 */
NeilBrown6a079972005-09-09 16:23:44 -0700847static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700848{
849 unsigned long i, chunks, index, oldindex, bit;
850 struct page *page = NULL, *oldpage = NULL;
851 unsigned long num_pages, bit_cnt = 0;
852 struct file *file;
853 unsigned long bytes, offset, dummy;
854 int outofdate;
855 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800856 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700857
858 chunks = bitmap->chunks;
859 file = bitmap->file;
860
NeilBrowna654b9d82005-06-21 17:17:27 -0700861 BUG_ON(!file && !bitmap->offset);
NeilBrown32a76272005-06-21 17:17:14 -0700862
Olaf Hering44456d32005-07-27 11:45:17 -0700863#ifdef INJECT_FAULTS_3
NeilBrown32a76272005-06-21 17:17:14 -0700864 outofdate = 1;
865#else
866 outofdate = bitmap->flags & BITMAP_STALE;
867#endif
868 if (outofdate)
869 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
870 "recovery\n", bmname(bitmap));
871
872 bytes = (chunks + 7) / 8;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700873
NeilBrowncdbb4cc2005-06-21 17:17:18 -0700874 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700875
NeilBrowna654b9d82005-06-21 17:17:27 -0700876 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
NeilBrown32a76272005-06-21 17:17:14 -0700877 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
878 bmname(bitmap),
879 (unsigned long) i_size_read(file->f_mapping->host),
880 bytes + sizeof(bitmap_super_t));
881 goto out;
882 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700883
884 ret = -ENOMEM;
885
NeilBrown32a76272005-06-21 17:17:14 -0700886 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700887 if (!bitmap->filemap)
NeilBrown32a76272005-06-21 17:17:14 -0700888 goto out;
NeilBrown32a76272005-06-21 17:17:14 -0700889
NeilBrown9ffae0c2006-01-06 00:20:32 -0800890 bitmap->filemap_attr = kzalloc(sizeof(long) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700891 if (!bitmap->filemap_attr)
NeilBrown32a76272005-06-21 17:17:14 -0700892 goto out;
NeilBrown32a76272005-06-21 17:17:14 -0700893
NeilBrown32a76272005-06-21 17:17:14 -0700894 oldindex = ~0L;
895
896 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -0800897 int b;
NeilBrown32a76272005-06-21 17:17:14 -0700898 index = file_page_index(i);
899 bit = file_page_offset(i);
900 if (index != oldindex) { /* this is a new page, read it in */
901 /* unmap the old page, we're done with it */
NeilBrown32a76272005-06-21 17:17:14 -0700902 if (index == 0) {
903 /*
904 * if we're here then the superblock page
905 * contains some bits (PAGE_SIZE != sizeof sb)
906 * we've already read it in, so just use it
907 */
908 page = bitmap->sb_page;
909 offset = sizeof(bitmap_super_t);
NeilBrowna654b9d82005-06-21 17:17:27 -0700910 } else if (file) {
NeilBrown32a76272005-06-21 17:17:14 -0700911 page = read_page(file, index, &dummy);
NeilBrowna654b9d82005-06-21 17:17:27 -0700912 offset = 0;
913 } else {
914 page = read_sb_page(bitmap->mddev, bitmap->offset, index);
NeilBrown32a76272005-06-21 17:17:14 -0700915 offset = 0;
916 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700917 if (IS_ERR(page)) { /* read error */
918 ret = PTR_ERR(page);
919 goto out;
920 }
921
NeilBrown32a76272005-06-21 17:17:14 -0700922 oldindex = index;
923 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -0700924
925 if (outofdate) {
926 /*
927 * if bitmap is out of date, dirty the
928 * whole page and write it out
929 */
NeilBrownea03aff2006-01-06 00:20:34 -0800930 paddr = kmap_atomic(page, KM_USER0);
931 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -0700932 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -0800933 kunmap_atomic(paddr, KM_USER0);
NeilBrown77ad4bc2005-06-21 17:17:21 -0700934 ret = write_page(bitmap, page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700935 if (ret) {
NeilBrown32a76272005-06-21 17:17:14 -0700936 /* release, page not in filemap yet */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800937 put_page(page);
NeilBrown32a76272005-06-21 17:17:14 -0700938 goto out;
939 }
940 }
941
942 bitmap->filemap[bitmap->file_pages++] = page;
943 }
NeilBrownea03aff2006-01-06 00:20:34 -0800944 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800945 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -0800946 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -0800947 else
NeilBrownea03aff2006-01-06 00:20:34 -0800948 b = ext2_test_bit(bit, paddr);
949 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800950 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -0700951 /* if the disk bit is set, set the memory bit */
NeilBrown6a079972005-09-09 16:23:44 -0700952 bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
953 ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
954 );
NeilBrown32a76272005-06-21 17:17:14 -0700955 bit_cnt++;
NeilBrown6a079972005-09-09 16:23:44 -0700956 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -0700957 }
NeilBrown32a76272005-06-21 17:17:14 -0700958 }
959
960 /* everything went OK */
961 ret = 0;
962 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
963
NeilBrown32a76272005-06-21 17:17:14 -0700964 if (bit_cnt) { /* Kick recovery if any bits were set */
965 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
966 md_wakeup_thread(bitmap->mddev->thread);
967 }
968
969out:
970 printk(KERN_INFO "%s: bitmap initialized from disk: "
971 "read %lu/%lu pages, set %lu bits, status: %d\n",
972 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
973
974 return ret;
975}
976
NeilBrowna654b9d82005-06-21 17:17:27 -0700977void bitmap_write_all(struct bitmap *bitmap)
978{
979 /* We don't actually write all bitmap blocks here,
980 * just flag them as needing to be written
981 */
982
983 unsigned long chunks = bitmap->chunks;
984 unsigned long bytes = (chunks+7)/8 + sizeof(bitmap_super_t);
985 unsigned long num_pages = (bytes + PAGE_SIZE-1) / PAGE_SIZE;
986 while (num_pages--)
987 bitmap->filemap_attr[num_pages] |= BITMAP_PAGE_NEEDWRITE;
988}
989
NeilBrown32a76272005-06-21 17:17:14 -0700990
991static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
992{
993 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
994 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
995 bitmap->bp[page].count += inc;
996/*
997 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
998 (unsigned long long)offset, inc, bitmap->bp[page].count);
999*/
1000 bitmap_checkfree(bitmap, page);
1001}
1002static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1003 sector_t offset, int *blocks,
1004 int create);
1005
1006/*
1007 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1008 * out to disk
1009 */
1010
1011int bitmap_daemon_work(struct bitmap *bitmap)
1012{
NeilBrownaa3163f2005-06-21 17:17:22 -07001013 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001014 unsigned long flags;
1015 struct page *page = NULL, *lastpage = NULL;
1016 int err = 0;
1017 int blocks;
1018 int attr;
NeilBrownea03aff2006-01-06 00:20:34 -08001019 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001020
1021 if (bitmap == NULL)
1022 return 0;
1023 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
1024 return 0;
1025 bitmap->daemon_lastrun = jiffies;
1026
1027 for (j = 0; j < bitmap->chunks; j++) {
1028 bitmap_counter_t *bmc;
1029 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -07001030 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -07001031 /* error or shutdown */
1032 spin_unlock_irqrestore(&bitmap->lock, flags);
1033 break;
1034 }
1035
1036 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001037
1038 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001039 /* skip this page unless it's marked as needing cleaning */
1040 if (!((attr=get_page_attr(bitmap, page)) & BITMAP_PAGE_CLEAN)) {
1041 if (attr & BITMAP_PAGE_NEEDWRITE) {
NeilBrown2d1f3b52006-01-06 00:20:31 -08001042 get_page(page);
NeilBrownaa3163f2005-06-21 17:17:22 -07001043 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1044 }
1045 spin_unlock_irqrestore(&bitmap->lock, flags);
1046 if (attr & BITMAP_PAGE_NEEDWRITE) {
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001047 switch (write_page(bitmap, page, 0)) {
1048 case -EAGAIN:
1049 set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1050 break;
1051 case 0:
1052 break;
1053 default:
NeilBrownaa3163f2005-06-21 17:17:22 -07001054 bitmap_file_kick(bitmap);
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001055 }
NeilBrown2d1f3b52006-01-06 00:20:31 -08001056 put_page(page);
NeilBrownaa3163f2005-06-21 17:17:22 -07001057 }
1058 continue;
1059 }
1060
NeilBrown32a76272005-06-21 17:17:14 -07001061 /* grab the new page, sync and release the old */
NeilBrown2d1f3b52006-01-06 00:20:31 -08001062 get_page(page);
NeilBrown32a76272005-06-21 17:17:14 -07001063 if (lastpage != NULL) {
1064 if (get_page_attr(bitmap, lastpage) & BITMAP_PAGE_NEEDWRITE) {
1065 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1066 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown77ad4bc2005-06-21 17:17:21 -07001067 err = write_page(bitmap, lastpage, 0);
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001068 if (err == -EAGAIN) {
1069 err = 0;
1070 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1071 }
NeilBrown32a76272005-06-21 17:17:14 -07001072 } else {
1073 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1074 spin_unlock_irqrestore(&bitmap->lock, flags);
1075 }
NeilBrown2d1f3b52006-01-06 00:20:31 -08001076 put_page(lastpage);
NeilBrown32a76272005-06-21 17:17:14 -07001077 if (err)
1078 bitmap_file_kick(bitmap);
1079 } else
1080 spin_unlock_irqrestore(&bitmap->lock, flags);
1081 lastpage = page;
NeilBrown32a76272005-06-21 17:17:14 -07001082/*
1083 printk("bitmap clean at page %lu\n", j);
1084*/
1085 spin_lock_irqsave(&bitmap->lock, flags);
1086 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1087 }
1088 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1089 &blocks, 0);
1090 if (bmc) {
1091/*
1092 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1093*/
1094 if (*bmc == 2) {
1095 *bmc=1; /* maybe clear the bit next time */
1096 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1097 } else if (*bmc == 1) {
1098 /* we can clear the bit */
1099 *bmc = 0;
1100 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1101 -1);
1102
1103 /* clear the bit */
NeilBrownea03aff2006-01-06 00:20:34 -08001104 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001105 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001106 clear_bit(file_page_offset(j), paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001107 else
NeilBrownea03aff2006-01-06 00:20:34 -08001108 ext2_clear_bit(file_page_offset(j), paddr);
1109 kunmap_atomic(paddr, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -07001110 }
1111 }
1112 spin_unlock_irqrestore(&bitmap->lock, flags);
1113 }
1114
1115 /* now sync the final page */
1116 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001117 spin_lock_irqsave(&bitmap->lock, flags);
1118 if (get_page_attr(bitmap, lastpage) &BITMAP_PAGE_NEEDWRITE) {
1119 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1120 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown77ad4bc2005-06-21 17:17:21 -07001121 err = write_page(bitmap, lastpage, 0);
NeilBrown8a5e9cf2005-06-21 17:17:29 -07001122 if (err == -EAGAIN) {
1123 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1124 err = 0;
1125 }
NeilBrown32a76272005-06-21 17:17:14 -07001126 } else {
1127 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1128 spin_unlock_irqrestore(&bitmap->lock, flags);
1129 }
1130
NeilBrown2d1f3b52006-01-06 00:20:31 -08001131 put_page(lastpage);
NeilBrown32a76272005-06-21 17:17:14 -07001132 }
1133
1134 return err;
1135}
1136
1137static void daemon_exit(struct bitmap *bitmap, mdk_thread_t **daemon)
1138{
1139 mdk_thread_t *dmn;
1140 unsigned long flags;
1141
1142 /* if no one is waiting on us, we'll free the md thread struct
1143 * and exit, otherwise we let the waiter clean things up */
1144 spin_lock_irqsave(&bitmap->lock, flags);
1145 if ((dmn = *daemon)) { /* no one is waiting, cleanup and exit */
1146 *daemon = NULL;
1147 spin_unlock_irqrestore(&bitmap->lock, flags);
1148 kfree(dmn);
1149 complete_and_exit(NULL, 0); /* do_exit not exported */
1150 }
1151 spin_unlock_irqrestore(&bitmap->lock, flags);
1152}
1153
1154static void bitmap_writeback_daemon(mddev_t *mddev)
1155{
1156 struct bitmap *bitmap = mddev->bitmap;
1157 struct page *page;
1158 struct page_list *item;
1159 int err = 0;
1160
NeilBrown77ad4bc2005-06-21 17:17:21 -07001161 if (signal_pending(current)) {
1162 printk(KERN_INFO
1163 "%s: bitmap writeback daemon got signal, exiting...\n",
1164 bmname(bitmap));
1165 err = -EINTR;
1166 goto out;
1167 }
NeilBrown9ba00532005-09-09 16:23:57 -07001168 if (bitmap == NULL)
1169 /* about to be stopped. */
1170 return;
NeilBrown32a76272005-06-21 17:17:14 -07001171
NeilBrown77ad4bc2005-06-21 17:17:21 -07001172 PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
1173 /* wait on bitmap page writebacks */
1174 while ((item = dequeue_page(bitmap))) {
1175 page = item->page;
1176 mempool_free(item, bitmap->write_pool);
1177 PRINTK("wait on page writeback: %p\n", page);
1178 wait_on_page_writeback(page);
1179 PRINTK("finished page writeback: %p\n", page);
1180
1181 err = PageError(page);
NeilBrown2d1f3b52006-01-06 00:20:31 -08001182 put_page(page);
NeilBrown77ad4bc2005-06-21 17:17:21 -07001183 if (err) {
1184 printk(KERN_WARNING "%s: bitmap file writeback "
1185 "failed (page %lu): %d\n",
1186 bmname(bitmap), page->index, err);
1187 bitmap_file_kick(bitmap);
1188 goto out;
NeilBrown32a76272005-06-21 17:17:14 -07001189 }
1190 }
NeilBrown77ad4bc2005-06-21 17:17:21 -07001191 out:
1192 wake_up(&bitmap->write_wait);
NeilBrown32a76272005-06-21 17:17:14 -07001193 if (err) {
1194 printk(KERN_INFO "%s: bitmap writeback daemon exiting (%d)\n",
NeilBrown77ad4bc2005-06-21 17:17:21 -07001195 bmname(bitmap), err);
NeilBrown32a76272005-06-21 17:17:14 -07001196 daemon_exit(bitmap, &bitmap->writeback_daemon);
1197 }
NeilBrown32a76272005-06-21 17:17:14 -07001198}
1199
NeilBrown500af872005-09-09 16:23:58 -07001200static mdk_thread_t *bitmap_start_daemon(struct bitmap *bitmap,
NeilBrown32a76272005-06-21 17:17:14 -07001201 void (*func)(mddev_t *), char *name)
1202{
1203 mdk_thread_t *daemon;
NeilBrown32a76272005-06-21 17:17:14 -07001204 char namebuf[32];
1205
Olaf Hering44456d32005-07-27 11:45:17 -07001206#ifdef INJECT_FATAL_FAULT_2
NeilBrown32a76272005-06-21 17:17:14 -07001207 daemon = NULL;
1208#else
1209 sprintf(namebuf, "%%s_%s", name);
1210 daemon = md_register_thread(func, bitmap->mddev, namebuf);
1211#endif
1212 if (!daemon) {
1213 printk(KERN_ERR "%s: failed to start bitmap daemon\n",
1214 bmname(bitmap));
NeilBrown500af872005-09-09 16:23:58 -07001215 return ERR_PTR(-ECHILD);
NeilBrown32a76272005-06-21 17:17:14 -07001216 }
1217
NeilBrown32a76272005-06-21 17:17:14 -07001218 md_wakeup_thread(daemon); /* start it running */
1219
1220 PRINTK("%s: %s daemon (pid %d) started...\n",
NeilBrownd80a1382005-06-21 17:17:17 -07001221 bmname(bitmap), name, daemon->tsk->pid);
NeilBrown500af872005-09-09 16:23:58 -07001222
1223 return daemon;
NeilBrown32a76272005-06-21 17:17:14 -07001224}
1225
NeilBrown500af872005-09-09 16:23:58 -07001226static void bitmap_stop_daemon(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001227{
NeilBrown500af872005-09-09 16:23:58 -07001228 /* the daemon can't stop itself... it'll just exit instead... */
1229 if (bitmap->writeback_daemon && ! IS_ERR(bitmap->writeback_daemon) &&
1230 current->pid != bitmap->writeback_daemon->tsk->pid) {
1231 mdk_thread_t *daemon;
1232 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07001233
NeilBrown500af872005-09-09 16:23:58 -07001234 spin_lock_irqsave(&bitmap->lock, flags);
1235 daemon = bitmap->writeback_daemon;
1236 bitmap->writeback_daemon = NULL;
1237 spin_unlock_irqrestore(&bitmap->lock, flags);
1238 if (daemon && ! IS_ERR(daemon))
1239 md_unregister_thread(daemon); /* destroy the thread */
1240 }
NeilBrown32a76272005-06-21 17:17:14 -07001241}
1242
1243static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1244 sector_t offset, int *blocks,
1245 int create)
1246{
1247 /* If 'create', we might release the lock and reclaim it.
1248 * The lock must have been taken with interrupts enabled.
1249 * If !create, we don't release the lock.
1250 */
1251 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1252 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1253 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1254 sector_t csize;
1255
1256 if (bitmap_checkpage(bitmap, page, create) < 0) {
1257 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1258 *blocks = csize - (offset & (csize- 1));
1259 return NULL;
1260 }
1261 /* now locked ... */
1262
1263 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1264 /* should we use the first or second counter field
1265 * of the hijacked pointer? */
1266 int hi = (pageoff > PAGE_COUNTER_MASK);
1267 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1268 PAGE_COUNTER_SHIFT - 1);
1269 *blocks = csize - (offset & (csize- 1));
1270 return &((bitmap_counter_t *)
1271 &bitmap->bp[page].map)[hi];
1272 } else { /* page is allocated */
1273 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1274 *blocks = csize - (offset & (csize- 1));
1275 return (bitmap_counter_t *)
1276 &(bitmap->bp[page].map[pageoff]);
1277 }
1278}
1279
NeilBrown4b6d2872005-09-09 16:23:47 -07001280int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001281{
1282 if (!bitmap) return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001283
1284 if (behind) {
1285 atomic_inc(&bitmap->behind_writes);
1286 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1287 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1288 }
1289
NeilBrown32a76272005-06-21 17:17:14 -07001290 while (sectors) {
1291 int blocks;
1292 bitmap_counter_t *bmc;
1293
1294 spin_lock_irq(&bitmap->lock);
1295 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1296 if (!bmc) {
1297 spin_unlock_irq(&bitmap->lock);
1298 return 0;
1299 }
1300
1301 switch(*bmc) {
1302 case 0:
1303 bitmap_file_set_bit(bitmap, offset);
1304 bitmap_count_page(bitmap,offset, 1);
1305 blk_plug_device(bitmap->mddev->queue);
1306 /* fall through */
1307 case 1:
1308 *bmc = 2;
1309 }
1310 if ((*bmc & COUNTER_MAX) == COUNTER_MAX) BUG();
1311 (*bmc)++;
1312
1313 spin_unlock_irq(&bitmap->lock);
1314
1315 offset += blocks;
1316 if (sectors > blocks)
1317 sectors -= blocks;
1318 else sectors = 0;
1319 }
1320 return 0;
1321}
1322
1323void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001324 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001325{
1326 if (!bitmap) return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001327 if (behind) {
1328 atomic_dec(&bitmap->behind_writes);
1329 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1330 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1331 }
1332
NeilBrown32a76272005-06-21 17:17:14 -07001333 while (sectors) {
1334 int blocks;
1335 unsigned long flags;
1336 bitmap_counter_t *bmc;
1337
1338 spin_lock_irqsave(&bitmap->lock, flags);
1339 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1340 if (!bmc) {
1341 spin_unlock_irqrestore(&bitmap->lock, flags);
1342 return;
1343 }
1344
1345 if (!success && ! (*bmc & NEEDED_MASK))
1346 *bmc |= NEEDED_MASK;
1347
1348 (*bmc)--;
1349 if (*bmc <= 2) {
1350 set_page_attr(bitmap,
1351 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1352 BITMAP_PAGE_CLEAN);
1353 }
1354 spin_unlock_irqrestore(&bitmap->lock, flags);
1355 offset += blocks;
1356 if (sectors > blocks)
1357 sectors -= blocks;
1358 else sectors = 0;
1359 }
1360}
1361
NeilBrown6a806c52005-07-15 03:56:35 -07001362int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1363 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001364{
1365 bitmap_counter_t *bmc;
1366 int rv;
1367 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1368 *blocks = 1024;
1369 return 1; /* always resync if no bitmap */
1370 }
1371 spin_lock_irq(&bitmap->lock);
1372 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1373 rv = 0;
1374 if (bmc) {
1375 /* locked */
1376 if (RESYNC(*bmc))
1377 rv = 1;
1378 else if (NEEDED(*bmc)) {
1379 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001380 if (!degraded) { /* don't set/clear bits if degraded */
1381 *bmc |= RESYNC_MASK;
1382 *bmc &= ~NEEDED_MASK;
1383 }
NeilBrown32a76272005-06-21 17:17:14 -07001384 }
1385 }
1386 spin_unlock_irq(&bitmap->lock);
1387 return rv;
1388}
1389
1390void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1391{
1392 bitmap_counter_t *bmc;
1393 unsigned long flags;
1394/*
1395 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1396*/ if (bitmap == NULL) {
1397 *blocks = 1024;
1398 return;
1399 }
1400 spin_lock_irqsave(&bitmap->lock, flags);
1401 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1402 if (bmc == NULL)
1403 goto unlock;
1404 /* locked */
1405/*
1406 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1407*/
1408 if (RESYNC(*bmc)) {
1409 *bmc &= ~RESYNC_MASK;
1410
1411 if (!NEEDED(*bmc) && aborted)
1412 *bmc |= NEEDED_MASK;
1413 else {
1414 if (*bmc <= 2) {
1415 set_page_attr(bitmap,
1416 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1417 BITMAP_PAGE_CLEAN);
1418 }
1419 }
1420 }
1421 unlock:
1422 spin_unlock_irqrestore(&bitmap->lock, flags);
1423}
1424
1425void bitmap_close_sync(struct bitmap *bitmap)
1426{
1427 /* Sync has finished, and any bitmap chunks that weren't synced
1428 * properly have been aborted. It remains to us to clear the
1429 * RESYNC bit wherever it is still on
1430 */
1431 sector_t sector = 0;
1432 int blocks;
1433 if (!bitmap) return;
1434 while (sector < bitmap->mddev->resync_max_sectors) {
1435 bitmap_end_sync(bitmap, sector, &blocks, 0);
1436/*
1437 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1438 (unsigned long long)sector, blocks);
1439*/ sector += blocks;
1440 }
1441}
1442
NeilBrown6a079972005-09-09 16:23:44 -07001443static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001444{
1445 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001446 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001447 * be 0 at this point
1448 */
NeilBrown193f1c92005-08-04 12:53:33 -07001449
1450 int secs;
1451 bitmap_counter_t *bmc;
1452 spin_lock_irq(&bitmap->lock);
1453 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1454 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001455 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001456 return;
NeilBrown32a76272005-06-21 17:17:14 -07001457 }
NeilBrown193f1c92005-08-04 12:53:33 -07001458 if (! *bmc) {
1459 struct page *page;
NeilBrown6a079972005-09-09 16:23:44 -07001460 *bmc = 1 | (needed?NEEDED_MASK:0);
NeilBrown193f1c92005-08-04 12:53:33 -07001461 bitmap_count_page(bitmap, offset, 1);
1462 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1463 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1464 }
1465 spin_unlock_irq(&bitmap->lock);
1466
NeilBrown32a76272005-06-21 17:17:14 -07001467}
1468
NeilBrown32a76272005-06-21 17:17:14 -07001469/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001470 * flush out any pending updates
1471 */
1472void bitmap_flush(mddev_t *mddev)
1473{
1474 struct bitmap *bitmap = mddev->bitmap;
1475 int sleep;
1476
1477 if (!bitmap) /* there was no bitmap */
1478 return;
1479
1480 /* run the daemon_work three time to ensure everything is flushed
1481 * that can be
1482 */
1483 sleep = bitmap->daemon_sleep;
1484 bitmap->daemon_sleep = 0;
1485 bitmap_daemon_work(bitmap);
1486 bitmap_daemon_work(bitmap);
1487 bitmap_daemon_work(bitmap);
1488 bitmap->daemon_sleep = sleep;
1489 bitmap_update_sb(bitmap);
1490}
1491
1492/*
NeilBrown32a76272005-06-21 17:17:14 -07001493 * free memory that was allocated
1494 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001495static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001496{
1497 unsigned long k, pages;
1498 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001499
1500 if (!bitmap) /* there was no bitmap */
1501 return;
1502
NeilBrown32a76272005-06-21 17:17:14 -07001503 /* release the bitmap file and kill the daemon */
1504 bitmap_file_put(bitmap);
1505
1506 bp = bitmap->bp;
1507 pages = bitmap->pages;
1508
1509 /* free all allocated memory */
1510
1511 mempool_destroy(bitmap->write_pool);
1512
1513 if (bp) /* deallocate the page memory */
1514 for (k = 0; k < pages; k++)
1515 if (bp[k].map && !bp[k].hijacked)
1516 kfree(bp[k].map);
1517 kfree(bp);
1518 kfree(bitmap);
1519}
NeilBrown3178b0d2005-09-09 16:23:50 -07001520void bitmap_destroy(mddev_t *mddev)
1521{
1522 struct bitmap *bitmap = mddev->bitmap;
1523
1524 if (!bitmap) /* there was no bitmap */
1525 return;
1526
1527 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownb15c2e52006-01-06 00:20:16 -08001528 if (mddev->thread)
1529 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001530
1531 bitmap_free(bitmap);
1532}
NeilBrown32a76272005-06-21 17:17:14 -07001533
1534/*
1535 * initialize the bitmap structure
1536 * if this returns an error, bitmap_destroy must be called to do clean up
1537 */
1538int bitmap_create(mddev_t *mddev)
1539{
1540 struct bitmap *bitmap;
1541 unsigned long blocks = mddev->resync_max_sectors;
1542 unsigned long chunks;
1543 unsigned long pages;
1544 struct file *file = mddev->bitmap_file;
1545 int err;
NeilBrown6a079972005-09-09 16:23:44 -07001546 sector_t start;
NeilBrown32a76272005-06-21 17:17:14 -07001547
1548 BUG_ON(sizeof(bitmap_super_t) != 256);
1549
NeilBrowna654b9d82005-06-21 17:17:27 -07001550 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001551 return 0;
1552
NeilBrowna654b9d82005-06-21 17:17:27 -07001553 BUG_ON(file && mddev->bitmap_offset);
1554
NeilBrown9ffae0c2006-01-06 00:20:32 -08001555 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001556 if (!bitmap)
1557 return -ENOMEM;
1558
NeilBrown32a76272005-06-21 17:17:14 -07001559 spin_lock_init(&bitmap->lock);
1560 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001561
1562 spin_lock_init(&bitmap->write_lock);
NeilBrown32a76272005-06-21 17:17:14 -07001563 INIT_LIST_HEAD(&bitmap->complete_pages);
1564 init_waitqueue_head(&bitmap->write_wait);
1565 bitmap->write_pool = mempool_create(WRITE_POOL_SIZE, write_pool_alloc,
1566 write_pool_free, NULL);
NeilBrown3178b0d2005-09-09 16:23:50 -07001567 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001568 if (!bitmap->write_pool)
NeilBrown3178b0d2005-09-09 16:23:50 -07001569 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001570
1571 bitmap->file = file;
NeilBrowna654b9d82005-06-21 17:17:27 -07001572 bitmap->offset = mddev->bitmap_offset;
1573 if (file) get_file(file);
NeilBrown32a76272005-06-21 17:17:14 -07001574 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1575 err = bitmap_read_sb(bitmap);
1576 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001577 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001578
1579 bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
1580 sizeof(bitmap->chunksize));
1581
1582 /* now that chunksize and chunkshift are set, we can use these macros */
1583 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1584 CHUNK_BLOCK_RATIO(bitmap);
1585 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1586
1587 BUG_ON(!pages);
1588
1589 bitmap->chunks = chunks;
1590 bitmap->pages = pages;
1591 bitmap->missing_pages = pages;
1592 bitmap->counter_bits = COUNTER_BITS;
1593
1594 bitmap->syncchunk = ~0UL;
1595
Olaf Hering44456d32005-07-27 11:45:17 -07001596#ifdef INJECT_FATAL_FAULT_1
NeilBrown32a76272005-06-21 17:17:14 -07001597 bitmap->bp = NULL;
1598#else
NeilBrown9ffae0c2006-01-06 00:20:32 -08001599 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001600#endif
NeilBrown3178b0d2005-09-09 16:23:50 -07001601 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001602 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001603 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001604
1605 bitmap->flags |= BITMAP_ACTIVE;
1606
1607 /* now that we have some pages available, initialize the in-memory
1608 * bitmap from the on-disk bitmap */
NeilBrown6a079972005-09-09 16:23:44 -07001609 start = 0;
1610 if (mddev->degraded == 0
1611 || bitmap->events_cleared == mddev->events)
1612 /* no need to keep dirty bits to optimise a re-add of a missing device */
1613 start = mddev->recovery_cp;
1614 err = bitmap_init_from_disk(bitmap, start);
NeilBrown193f1c92005-08-04 12:53:33 -07001615
NeilBrown32a76272005-06-21 17:17:14 -07001616 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001617 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001618
1619 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1620 pages, bmname(bitmap));
1621
NeilBrown3178b0d2005-09-09 16:23:50 -07001622 mddev->bitmap = bitmap;
1623
NeilBrown500af872005-09-09 16:23:58 -07001624 if (file)
1625 /* kick off the bitmap writeback daemon */
1626 bitmap->writeback_daemon =
1627 bitmap_start_daemon(bitmap,
1628 bitmap_writeback_daemon,
1629 "bitmap_wb");
1630
1631 if (IS_ERR(bitmap->writeback_daemon))
1632 return PTR_ERR(bitmap->writeback_daemon);
NeilBrownb15c2e52006-01-06 00:20:16 -08001633 mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1634
NeilBrown32a76272005-06-21 17:17:14 -07001635 return bitmap_update_sb(bitmap);
NeilBrown3178b0d2005-09-09 16:23:50 -07001636
1637 error:
1638 bitmap_free(bitmap);
1639 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001640}
1641
1642/* the bitmap API -- for raid personalities */
1643EXPORT_SYMBOL(bitmap_startwrite);
1644EXPORT_SYMBOL(bitmap_endwrite);
1645EXPORT_SYMBOL(bitmap_start_sync);
1646EXPORT_SYMBOL(bitmap_end_sync);
1647EXPORT_SYMBOL(bitmap_unplug);
1648EXPORT_SYMBOL(bitmap_close_sync);
1649EXPORT_SYMBOL(bitmap_daemon_work);