md/raid10: don't clear bitmap during recovery if array will still be degraded.
[linux-2.6-block.git] / drivers / md / bitmap.c
CommitLineData
32a7627c
N
1/*
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
32a7627c
N
10 */
11
12/*
13 * Still to do:
14 *
15 * flush after percent set rather than just time based. (maybe both).
16 * wait if count gets too high, wake when it drops to half.
32a7627c
N
17 */
18
bff61975 19#include <linux/blkdev.h>
32a7627c 20#include <linux/module.h>
32a7627c
N
21#include <linux/errno.h>
22#include <linux/slab.h>
23#include <linux/init.h>
32a7627c
N
24#include <linux/timer.h>
25#include <linux/sched.h>
26#include <linux/list.h>
27#include <linux/file.h>
28#include <linux/mount.h>
29#include <linux/buffer_head.h>
43b2e5d8 30#include "md.h"
ef740c37 31#include "bitmap.h"
32a7627c
N
32
33/* debug macros */
34
35#define DEBUG 0
36
37#if DEBUG
38/* these are for debugging purposes only! */
39
40/* define one and only one of these */
41#define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
42#define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
43#define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
44#define INJECT_FAULTS_4 0 /* undef */
45#define INJECT_FAULTS_5 0 /* undef */
46#define INJECT_FAULTS_6 0
47
48/* if these are defined, the driver will fail! debug only */
49#define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
50#define INJECT_FATAL_FAULT_2 0 /* undef */
51#define INJECT_FATAL_FAULT_3 0 /* undef */
52#endif
53
54//#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
55#define DPRINTK(x...) do { } while(0)
56
57#ifndef PRINTK
58# if DEBUG > 0
59# define PRINTK(x...) printk(KERN_DEBUG x)
60# else
61# define PRINTK(x...)
62# endif
63#endif
64
65static inline char * bmname(struct bitmap *bitmap)
66{
67 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
68}
69
32a7627c
N
70
71/*
72 * just a placeholder - calls kmalloc for bitmap pages
73 */
74static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
75{
76 unsigned char *page;
77
44456d37 78#ifdef INJECT_FAULTS_1
32a7627c
N
79 page = NULL;
80#else
81 page = kmalloc(PAGE_SIZE, GFP_NOIO);
82#endif
83 if (!page)
84 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
85 else
a654b9d8 86 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
32a7627c
N
87 bmname(bitmap), page);
88 return page;
89}
90
91/*
92 * for now just a placeholder -- just calls kfree for bitmap pages
93 */
94static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
95{
96 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
97 kfree(page);
98}
99
100/*
101 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
102 *
103 * 1) check to see if this page is allocated, if it's not then try to alloc
104 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
105 * page pointer directly as a counter
106 *
107 * if we find our page, we increment the page's refcount so that it stays
108 * allocated while we're using it
109 */
110static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
111{
112 unsigned char *mappage;
113
114 if (page >= bitmap->pages) {
1187cf0a
N
115 /* This can happen if bitmap_start_sync goes beyond
116 * End-of-device while looking for a whole page.
117 * It is harmless.
118 */
32a7627c
N
119 return -EINVAL;
120 }
121
122
123 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
124 return 0;
125
126 if (bitmap->bp[page].map) /* page is already allocated, just return */
127 return 0;
128
129 if (!create)
130 return -ENOENT;
131
132 spin_unlock_irq(&bitmap->lock);
133
134 /* this page has not been allocated yet */
135
136 if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
137 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
138 bmname(bitmap));
139 /* failed - set the hijacked flag so that we can use the
140 * pointer as a counter */
141 spin_lock_irq(&bitmap->lock);
142 if (!bitmap->bp[page].map)
143 bitmap->bp[page].hijacked = 1;
144 goto out;
145 }
146
147 /* got a page */
148
149 spin_lock_irq(&bitmap->lock);
150
151 /* recheck the page */
152
153 if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
154 /* somebody beat us to getting the page */
155 bitmap_free_page(bitmap, mappage);
156 return 0;
157 }
158
159 /* no page was in place and we have one, so install it */
160
161 memset(mappage, 0, PAGE_SIZE);
162 bitmap->bp[page].map = mappage;
163 bitmap->missing_pages--;
164out:
165 return 0;
166}
167
168
169/* if page is completely empty, put it back on the free list, or dealloc it */
170/* if page was hijacked, unmark the flag so it might get alloced next time */
171/* Note: lock should be held when calling this */
858119e1 172static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
32a7627c
N
173{
174 char *ptr;
175
176 if (bitmap->bp[page].count) /* page is still busy */
177 return;
178
179 /* page is no longer in use, it can be released */
180
181 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
182 bitmap->bp[page].hijacked = 0;
183 bitmap->bp[page].map = NULL;
184 return;
185 }
186
187 /* normal case, free the page */
188
189#if 0
190/* actually ... let's not. We will probably need the page again exactly when
191 * memory is tight and we are flusing to disk
192 */
193 return;
194#else
195 ptr = bitmap->bp[page].map;
196 bitmap->bp[page].map = NULL;
197 bitmap->missing_pages++;
198 bitmap_free_page(bitmap, ptr);
199 return;
200#endif
201}
202
203
204/*
205 * bitmap file handling - read and write the bitmap file and its superblock
206 */
207
32a7627c
N
208/*
209 * basic page I/O operations
210 */
211
a654b9d8 212/* IO operations when bitmap is stored near all superblocks */
a2ed9615
N
213static struct page *read_sb_page(mddev_t *mddev, long offset,
214 struct page *page,
215 unsigned long index, int size)
a654b9d8
N
216{
217 /* choose a good rdev and read the page from there */
218
219 mdk_rdev_t *rdev;
a654b9d8
N
220 sector_t target;
221
a2ed9615
N
222 if (!page)
223 page = alloc_page(GFP_KERNEL);
a654b9d8
N
224 if (!page)
225 return ERR_PTR(-ENOMEM);
a654b9d8 226
159ec1fc 227 list_for_each_entry(rdev, &mddev->disks, same_set) {
b2d444d7
N
228 if (! test_bit(In_sync, &rdev->flags)
229 || test_bit(Faulty, &rdev->flags))
ab904d63
N
230 continue;
231
0f420358 232 target = rdev->sb_start + offset + index * (PAGE_SIZE/512);
a654b9d8 233
a2ed9615
N
234 if (sync_page_io(rdev->bdev, target,
235 roundup(size, bdev_hardsect_size(rdev->bdev)),
236 page, READ)) {
ab904d63 237 page->index = index;
ce25c31b
N
238 attach_page_buffers(page, NULL); /* so that free_buffer will
239 * quietly no-op */
ab904d63
N
240 return page;
241 }
242 }
243 return ERR_PTR(-EIO);
a654b9d8 244
a654b9d8
N
245}
246
b2d2c4ce
N
247static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
248{
249 /* Iterate the disks of an mddev, using rcu to protect access to the
250 * linked list, and raising the refcount of devices we return to ensure
251 * they don't disappear while in use.
252 * As devices are only added or removed when raid_disk is < 0 and
253 * nr_pending is 0 and In_sync is clear, the entries we return will
254 * still be in the same position on the list when we re-enter
255 * list_for_each_continue_rcu.
256 */
257 struct list_head *pos;
258 rcu_read_lock();
259 if (rdev == NULL)
260 /* start at the beginning */
261 pos = &mddev->disks;
262 else {
263 /* release the previous rdev and start from there. */
264 rdev_dec_pending(rdev, mddev);
265 pos = &rdev->same_set;
266 }
267 list_for_each_continue_rcu(pos, &mddev->disks) {
268 rdev = list_entry(pos, mdk_rdev_t, same_set);
269 if (rdev->raid_disk >= 0 &&
b2d2c4ce
N
270 !test_bit(Faulty, &rdev->flags)) {
271 /* this is a usable devices */
272 atomic_inc(&rdev->nr_pending);
273 rcu_read_unlock();
274 return rdev;
275 }
276 }
277 rcu_read_unlock();
278 return NULL;
279}
280
ab6085c7 281static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
a654b9d8 282{
b2d2c4ce 283 mdk_rdev_t *rdev = NULL;
ab6085c7 284 mddev_t *mddev = bitmap->mddev;
a654b9d8 285
b2d2c4ce 286 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
ab6085c7
N
287 int size = PAGE_SIZE;
288 if (page->index == bitmap->file_pages-1)
289 size = roundup(bitmap->last_page_size,
290 bdev_hardsect_size(rdev->bdev));
f0d76d70
N
291 /* Just make sure we aren't corrupting data or
292 * metadata
293 */
294 if (bitmap->offset < 0) {
295 /* DATA BITMAP METADATA */
296 if (bitmap->offset
85bfb4da 297 + (long)(page->index * (PAGE_SIZE/512))
f0d76d70
N
298 + size/512 > 0)
299 /* bitmap runs in to metadata */
4b80991c 300 goto bad_alignment;
58c0fed4 301 if (rdev->data_offset + mddev->dev_sectors
0f420358 302 > rdev->sb_start + bitmap->offset)
f0d76d70 303 /* data runs in to bitmap */
4b80991c 304 goto bad_alignment;
0f420358 305 } else if (rdev->sb_start < rdev->data_offset) {
f0d76d70 306 /* METADATA BITMAP DATA */
0f420358 307 if (rdev->sb_start
f0d76d70
N
308 + bitmap->offset
309 + page->index*(PAGE_SIZE/512) + size/512
310 > rdev->data_offset)
311 /* bitmap runs in to data */
4b80991c 312 goto bad_alignment;
f0d76d70
N
313 } else {
314 /* DATA METADATA BITMAP - no problems */
315 }
a654b9d8 316 md_super_write(mddev, rdev,
0f420358 317 rdev->sb_start + bitmap->offset
a654b9d8 318 + page->index * (PAGE_SIZE/512),
ab6085c7 319 size,
a654b9d8 320 page);
b2d2c4ce 321 }
a654b9d8
N
322
323 if (wait)
a9701a30 324 md_super_wait(mddev);
a654b9d8 325 return 0;
4b80991c
N
326
327 bad_alignment:
328 rcu_read_unlock();
329 return -EINVAL;
a654b9d8
N
330}
331
4ad13663 332static void bitmap_file_kick(struct bitmap *bitmap);
32a7627c 333/*
a654b9d8 334 * write out a page to a file
32a7627c 335 */
4ad13663 336static void write_page(struct bitmap *bitmap, struct page *page, int wait)
32a7627c 337{
d785a06a 338 struct buffer_head *bh;
32a7627c 339
f0d76d70
N
340 if (bitmap->file == NULL) {
341 switch (write_sb_page(bitmap, page, wait)) {
342 case -EINVAL:
343 bitmap->flags |= BITMAP_WRITE_ERROR;
f0d76d70 344 }
4ad13663 345 } else {
a654b9d8 346
4ad13663 347 bh = page_buffers(page);
c708443c 348
4ad13663
N
349 while (bh && bh->b_blocknr) {
350 atomic_inc(&bitmap->pending_writes);
351 set_buffer_locked(bh);
352 set_buffer_mapped(bh);
353 submit_bh(WRITE, bh);
354 bh = bh->b_this_page;
355 }
d785a06a 356
4ad13663
N
357 if (wait) {
358 wait_event(bitmap->write_wait,
359 atomic_read(&bitmap->pending_writes)==0);
360 }
8a5e9cf1 361 }
4ad13663
N
362 if (bitmap->flags & BITMAP_WRITE_ERROR)
363 bitmap_file_kick(bitmap);
d785a06a
N
364}
365
366static void end_bitmap_write(struct buffer_head *bh, int uptodate)
367{
368 struct bitmap *bitmap = bh->b_private;
369 unsigned long flags;
32a7627c 370
d785a06a
N
371 if (!uptodate) {
372 spin_lock_irqsave(&bitmap->lock, flags);
373 bitmap->flags |= BITMAP_WRITE_ERROR;
374 spin_unlock_irqrestore(&bitmap->lock, flags);
32a7627c 375 }
d785a06a
N
376 if (atomic_dec_and_test(&bitmap->pending_writes))
377 wake_up(&bitmap->write_wait);
378}
32a7627c 379
d785a06a
N
380/* copied from buffer.c */
381static void
382__clear_page_buffers(struct page *page)
383{
384 ClearPagePrivate(page);
385 set_page_private(page, 0);
386 page_cache_release(page);
387}
388static void free_buffers(struct page *page)
389{
390 struct buffer_head *bh = page_buffers(page);
77ad4bc7 391
d785a06a
N
392 while (bh) {
393 struct buffer_head *next = bh->b_this_page;
394 free_buffer_head(bh);
395 bh = next;
77ad4bc7 396 }
d785a06a
N
397 __clear_page_buffers(page);
398 put_page(page);
32a7627c
N
399}
400
d785a06a
N
401/* read a page from a file.
402 * We both read the page, and attach buffers to the page to record the
403 * address of each block (using bmap). These addresses will be used
404 * to write the block later, completely bypassing the filesystem.
405 * This usage is similar to how swap files are handled, and allows us
406 * to write to a file with no concerns of memory allocation failing.
407 */
32a7627c 408static struct page *read_page(struct file *file, unsigned long index,
d785a06a
N
409 struct bitmap *bitmap,
410 unsigned long count)
32a7627c 411{
32a7627c 412 struct page *page = NULL;
c649bb9c 413 struct inode *inode = file->f_path.dentry->d_inode;
d785a06a
N
414 struct buffer_head *bh;
415 sector_t block;
32a7627c 416
2d1f3b5d
N
417 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
418 (unsigned long long)index << PAGE_SHIFT);
32a7627c 419
d785a06a
N
420 page = alloc_page(GFP_KERNEL);
421 if (!page)
422 page = ERR_PTR(-ENOMEM);
32a7627c
N
423 if (IS_ERR(page))
424 goto out;
d785a06a 425
d785a06a
N
426 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
427 if (!bh) {
2d1f3b5d 428 put_page(page);
d785a06a 429 page = ERR_PTR(-ENOMEM);
32a7627c
N
430 goto out;
431 }
d785a06a
N
432 attach_page_buffers(page, bh);
433 block = index << (PAGE_SHIFT - inode->i_blkbits);
434 while (bh) {
435 if (count == 0)
436 bh->b_blocknr = 0;
437 else {
438 bh->b_blocknr = bmap(inode, block);
439 if (bh->b_blocknr == 0) {
440 /* Cannot use this file! */
441 free_buffers(page);
442 page = ERR_PTR(-EINVAL);
443 goto out;
444 }
445 bh->b_bdev = inode->i_sb->s_bdev;
446 if (count < (1<<inode->i_blkbits))
447 count = 0;
448 else
449 count -= (1<<inode->i_blkbits);
450
451 bh->b_end_io = end_bitmap_write;
452 bh->b_private = bitmap;
ce25c31b
N
453 atomic_inc(&bitmap->pending_writes);
454 set_buffer_locked(bh);
455 set_buffer_mapped(bh);
456 submit_bh(READ, bh);
d785a06a
N
457 }
458 block++;
459 bh = bh->b_this_page;
460 }
d785a06a 461 page->index = index;
ce25c31b
N
462
463 wait_event(bitmap->write_wait,
464 atomic_read(&bitmap->pending_writes)==0);
465 if (bitmap->flags & BITMAP_WRITE_ERROR) {
466 free_buffers(page);
467 page = ERR_PTR(-EIO);
468 }
32a7627c
N
469out:
470 if (IS_ERR(page))
471 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
2d1f3b5d
N
472 (int)PAGE_SIZE,
473 (unsigned long long)index << PAGE_SHIFT,
32a7627c
N
474 PTR_ERR(page));
475 return page;
476}
477
478/*
479 * bitmap file superblock operations
480 */
481
482/* update the event counter and sync the superblock to disk */
4ad13663 483void bitmap_update_sb(struct bitmap *bitmap)
32a7627c
N
484{
485 bitmap_super_t *sb;
486 unsigned long flags;
487
488 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
4ad13663 489 return;
32a7627c
N
490 spin_lock_irqsave(&bitmap->lock, flags);
491 if (!bitmap->sb_page) { /* no superblock */
492 spin_unlock_irqrestore(&bitmap->lock, flags);
4ad13663 493 return;
32a7627c 494 }
32a7627c 495 spin_unlock_irqrestore(&bitmap->lock, flags);
ea03aff9 496 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
32a7627c 497 sb->events = cpu_to_le64(bitmap->mddev->events);
a0da84f3
NB
498 if (bitmap->mddev->events < bitmap->events_cleared) {
499 /* rocking back to read-only */
500 bitmap->events_cleared = bitmap->mddev->events;
501 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
502 }
ea03aff9 503 kunmap_atomic(sb, KM_USER0);
4ad13663 504 write_page(bitmap, bitmap->sb_page, 1);
32a7627c
N
505}
506
507/* print out the bitmap file superblock */
508void bitmap_print_sb(struct bitmap *bitmap)
509{
510 bitmap_super_t *sb;
511
512 if (!bitmap || !bitmap->sb_page)
513 return;
ea03aff9 514 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
32a7627c 515 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
a2cff26a
N
516 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
517 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
518 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
32a7627c
N
519 *(__u32 *)(sb->uuid+0),
520 *(__u32 *)(sb->uuid+4),
521 *(__u32 *)(sb->uuid+8),
522 *(__u32 *)(sb->uuid+12));
a2cff26a 523 printk(KERN_DEBUG " events: %llu\n",
32a7627c 524 (unsigned long long) le64_to_cpu(sb->events));
a2cff26a 525 printk(KERN_DEBUG "events cleared: %llu\n",
32a7627c 526 (unsigned long long) le64_to_cpu(sb->events_cleared));
a2cff26a
N
527 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
528 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
529 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
530 printk(KERN_DEBUG " sync size: %llu KB\n",
531 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
4b6d287f 532 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
ea03aff9 533 kunmap_atomic(sb, KM_USER0);
32a7627c
N
534}
535
536/* read the superblock from the bitmap file and initialize some bitmap fields */
537static int bitmap_read_sb(struct bitmap *bitmap)
538{
539 char *reason = NULL;
540 bitmap_super_t *sb;
4b6d287f 541 unsigned long chunksize, daemon_sleep, write_behind;
32a7627c
N
542 unsigned long long events;
543 int err = -EINVAL;
544
545 /* page 0 is the superblock, read it... */
f49d5e62
N
546 if (bitmap->file) {
547 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
548 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
549
550 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
551 } else {
a2ed9615
N
552 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset,
553 NULL,
554 0, sizeof(bitmap_super_t));
a654b9d8 555 }
32a7627c
N
556 if (IS_ERR(bitmap->sb_page)) {
557 err = PTR_ERR(bitmap->sb_page);
558 bitmap->sb_page = NULL;
559 return err;
560 }
561
ea03aff9 562 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
32a7627c 563
32a7627c
N
564 chunksize = le32_to_cpu(sb->chunksize);
565 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
4b6d287f 566 write_behind = le32_to_cpu(sb->write_behind);
32a7627c
N
567
568 /* verify that the bitmap-specific fields are valid */
569 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
570 reason = "bad magic";
bd926c63
N
571 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
572 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
32a7627c 573 reason = "unrecognized superblock version";
1187cf0a 574 else if (chunksize < 512)
7dd5d34c 575 reason = "bitmap chunksize too small";
32a7627c
N
576 else if ((1 << ffz(~chunksize)) != chunksize)
577 reason = "bitmap chunksize not a power of 2";
7dd5d34c
N
578 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT / HZ)
579 reason = "daemon sleep period out of range";
4b6d287f
N
580 else if (write_behind > COUNTER_MAX)
581 reason = "write-behind limit out of range (0 - 16383)";
32a7627c
N
582 if (reason) {
583 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
584 bmname(bitmap), reason);
585 goto out;
586 }
587
588 /* keep the array size field of the bitmap superblock up to date */
589 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
590
591 if (!bitmap->mddev->persistent)
592 goto success;
593
594 /*
595 * if we have a persistent array superblock, compare the
596 * bitmap's UUID and event counter to the mddev's
597 */
598 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
599 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
600 bmname(bitmap));
601 goto out;
602 }
603 events = le64_to_cpu(sb->events);
604 if (events < bitmap->mddev->events) {
605 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
606 "-- forcing full recovery\n", bmname(bitmap), events,
607 (unsigned long long) bitmap->mddev->events);
4f2e639a 608 sb->state |= cpu_to_le32(BITMAP_STALE);
32a7627c
N
609 }
610success:
611 /* assign fields using values from superblock */
612 bitmap->chunksize = chunksize;
613 bitmap->daemon_sleep = daemon_sleep;
585f0dd5 614 bitmap->daemon_lastrun = jiffies;
4b6d287f 615 bitmap->max_write_behind = write_behind;
4f2e639a 616 bitmap->flags |= le32_to_cpu(sb->state);
bd926c63
N
617 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
618 bitmap->flags |= BITMAP_HOSTENDIAN;
32a7627c 619 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
4f2e639a 620 if (sb->state & cpu_to_le32(BITMAP_STALE))
6a07997f 621 bitmap->events_cleared = bitmap->mddev->events;
32a7627c
N
622 err = 0;
623out:
ea03aff9 624 kunmap_atomic(sb, KM_USER0);
32a7627c
N
625 if (err)
626 bitmap_print_sb(bitmap);
627 return err;
628}
629
630enum bitmap_mask_op {
631 MASK_SET,
632 MASK_UNSET
633};
634
4ad13663
N
635/* record the state of the bitmap in the superblock. Return the old value */
636static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
637 enum bitmap_mask_op op)
32a7627c
N
638{
639 bitmap_super_t *sb;
640 unsigned long flags;
4ad13663 641 int old;
32a7627c
N
642
643 spin_lock_irqsave(&bitmap->lock, flags);
7e317655 644 if (!bitmap->sb_page) { /* can't set the state */
32a7627c 645 spin_unlock_irqrestore(&bitmap->lock, flags);
4ad13663 646 return 0;
32a7627c 647 }
32a7627c 648 spin_unlock_irqrestore(&bitmap->lock, flags);
ea03aff9 649 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
4ad13663 650 old = le32_to_cpu(sb->state) & bits;
32a7627c 651 switch (op) {
4f2e639a 652 case MASK_SET: sb->state |= cpu_to_le32(bits);
32a7627c 653 break;
4f2e639a 654 case MASK_UNSET: sb->state &= cpu_to_le32(~bits);
32a7627c
N
655 break;
656 default: BUG();
657 }
ea03aff9 658 kunmap_atomic(sb, KM_USER0);
4ad13663 659 return old;
32a7627c
N
660}
661
662/*
663 * general bitmap file operations
664 */
665
666/* calculate the index of the page that contains this bit */
667static inline unsigned long file_page_index(unsigned long chunk)
668{
669 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
670}
671
672/* calculate the (bit) offset of this bit within a page */
673static inline unsigned long file_page_offset(unsigned long chunk)
674{
675 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
676}
677
678/*
679 * return a pointer to the page in the filemap that contains the given bit
680 *
681 * this lookup is complicated by the fact that the bitmap sb might be exactly
682 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
683 * 0 or page 1
684 */
685static inline struct page *filemap_get_page(struct bitmap *bitmap,
686 unsigned long chunk)
687{
9b1d1dac 688 if (file_page_index(chunk) >= bitmap->file_pages) return NULL;
32a7627c
N
689 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
690}
691
692
693static void bitmap_file_unmap(struct bitmap *bitmap)
694{
695 struct page **map, *sb_page;
696 unsigned long *attr;
697 int pages;
698 unsigned long flags;
699
700 spin_lock_irqsave(&bitmap->lock, flags);
701 map = bitmap->filemap;
702 bitmap->filemap = NULL;
703 attr = bitmap->filemap_attr;
704 bitmap->filemap_attr = NULL;
705 pages = bitmap->file_pages;
706 bitmap->file_pages = 0;
707 sb_page = bitmap->sb_page;
708 bitmap->sb_page = NULL;
709 spin_unlock_irqrestore(&bitmap->lock, flags);
710
711 while (pages--)
712 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
d785a06a 713 free_buffers(map[pages]);
32a7627c
N
714 kfree(map);
715 kfree(attr);
716
d785a06a
N
717 if (sb_page)
718 free_buffers(sb_page);
32a7627c
N
719}
720
721static void bitmap_file_put(struct bitmap *bitmap)
722{
723 struct file *file;
32a7627c
N
724 unsigned long flags;
725
726 spin_lock_irqsave(&bitmap->lock, flags);
727 file = bitmap->file;
728 bitmap->file = NULL;
729 spin_unlock_irqrestore(&bitmap->lock, flags);
730
d785a06a
N
731 if (file)
732 wait_event(bitmap->write_wait,
733 atomic_read(&bitmap->pending_writes)==0);
32a7627c
N
734 bitmap_file_unmap(bitmap);
735
d785a06a 736 if (file) {
c649bb9c 737 struct inode *inode = file->f_path.dentry->d_inode;
fc0ecff6 738 invalidate_mapping_pages(inode->i_mapping, 0, -1);
32a7627c 739 fput(file);
d785a06a 740 }
32a7627c
N
741}
742
743
744/*
745 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
746 * then it is no longer reliable, so we stop using it and we mark the file
747 * as failed in the superblock
748 */
749static void bitmap_file_kick(struct bitmap *bitmap)
750{
751 char *path, *ptr = NULL;
752
4ad13663
N
753 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
754 bitmap_update_sb(bitmap);
32a7627c 755
4ad13663
N
756 if (bitmap->file) {
757 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
758 if (path)
6bcfd601
CH
759 ptr = d_path(&bitmap->file->f_path, path,
760 PAGE_SIZE);
761
32a7627c 762
4ad13663
N
763 printk(KERN_ALERT
764 "%s: kicking failed bitmap file %s from array!\n",
6bcfd601 765 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
32a7627c 766
4ad13663
N
767 kfree(path);
768 } else
769 printk(KERN_ALERT
770 "%s: disabling internal bitmap due to errors\n",
771 bmname(bitmap));
a654b9d8 772 }
32a7627c
N
773
774 bitmap_file_put(bitmap);
775
776 return;
777}
778
779enum bitmap_page_attr {
e16b68b6
N
780 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
781 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
782 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
32a7627c
N
783};
784
785static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
786 enum bitmap_page_attr attr)
787{
e16b68b6 788 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
32a7627c
N
789}
790
791static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
792 enum bitmap_page_attr attr)
793{
e16b68b6 794 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
32a7627c
N
795}
796
ec7a3197
N
797static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
798 enum bitmap_page_attr attr)
32a7627c 799{
e16b68b6 800 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
32a7627c
N
801}
802
803/*
804 * bitmap_file_set_bit -- called before performing a write to the md device
805 * to set (and eventually sync) a particular bit in the bitmap file
806 *
807 * we set the bit immediately, then we record the page number so that
808 * when an unplug occurs, we can flush the dirty pages out to disk
809 */
810static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
811{
812 unsigned long bit;
813 struct page *page;
814 void *kaddr;
815 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
816
a654b9d8 817 if (!bitmap->filemap) {
32a7627c
N
818 return;
819 }
820
821 page = filemap_get_page(bitmap, chunk);
9b1d1dac 822 if (!page) return;
32a7627c
N
823 bit = file_page_offset(chunk);
824
32a7627c
N
825 /* set the bit */
826 kaddr = kmap_atomic(page, KM_USER0);
bd926c63
N
827 if (bitmap->flags & BITMAP_HOSTENDIAN)
828 set_bit(bit, kaddr);
829 else
830 ext2_set_bit(bit, kaddr);
32a7627c
N
831 kunmap_atomic(kaddr, KM_USER0);
832 PRINTK("set file bit %lu page %lu\n", bit, page->index);
833
834 /* record page number so it gets flushed to disk when unplug occurs */
835 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
836
837}
838
839/* this gets called when the md device is ready to unplug its underlying
840 * (slave) device queues -- before we let any writes go down, we need to
841 * sync the dirty pages of the bitmap file to disk */
4ad13663 842void bitmap_unplug(struct bitmap *bitmap)
32a7627c 843{
ec7a3197
N
844 unsigned long i, flags;
845 int dirty, need_write;
32a7627c
N
846 struct page *page;
847 int wait = 0;
848
849 if (!bitmap)
4ad13663 850 return;
32a7627c
N
851
852 /* look at each page to see if there are any set bits that need to be
853 * flushed out to disk */
854 for (i = 0; i < bitmap->file_pages; i++) {
855 spin_lock_irqsave(&bitmap->lock, flags);
a654b9d8 856 if (!bitmap->filemap) {
32a7627c 857 spin_unlock_irqrestore(&bitmap->lock, flags);
4ad13663 858 return;
32a7627c
N
859 }
860 page = bitmap->filemap[i];
ec7a3197
N
861 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
862 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
32a7627c
N
863 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
864 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
ec7a3197 865 if (dirty)
32a7627c
N
866 wait = 1;
867 spin_unlock_irqrestore(&bitmap->lock, flags);
868
d785a06a 869 if (dirty | need_write)
4ad13663 870 write_page(bitmap, page, 0);
32a7627c
N
871 }
872 if (wait) { /* if any writes were performed, we need to wait on them */
0b79ccf0 873 if (bitmap->file)
d785a06a
N
874 wait_event(bitmap->write_wait,
875 atomic_read(&bitmap->pending_writes)==0);
0b79ccf0 876 else
a9701a30 877 md_super_wait(bitmap->mddev);
32a7627c 878 }
d785a06a
N
879 if (bitmap->flags & BITMAP_WRITE_ERROR)
880 bitmap_file_kick(bitmap);
32a7627c
N
881}
882
6a07997f 883static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
32a7627c
N
884/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
885 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
886 * memory mapping of the bitmap file
887 * Special cases:
888 * if there's no bitmap file, or if the bitmap file had been
889 * previously kicked from the array, we mark all the bits as
890 * 1's in order to cause a full resync.
6a07997f
N
891 *
892 * We ignore all bits for sectors that end earlier than 'start'.
893 * This is used when reading an out-of-date bitmap...
32a7627c 894 */
6a07997f 895static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
32a7627c
N
896{
897 unsigned long i, chunks, index, oldindex, bit;
898 struct page *page = NULL, *oldpage = NULL;
899 unsigned long num_pages, bit_cnt = 0;
900 struct file *file;
d785a06a 901 unsigned long bytes, offset;
32a7627c
N
902 int outofdate;
903 int ret = -ENOSPC;
ea03aff9 904 void *paddr;
32a7627c
N
905
906 chunks = bitmap->chunks;
907 file = bitmap->file;
908
a654b9d8 909 BUG_ON(!file && !bitmap->offset);
32a7627c 910
44456d37 911#ifdef INJECT_FAULTS_3
32a7627c
N
912 outofdate = 1;
913#else
914 outofdate = bitmap->flags & BITMAP_STALE;
915#endif
916 if (outofdate)
917 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
918 "recovery\n", bmname(bitmap));
919
920 bytes = (chunks + 7) / 8;
bc7f77de 921
cdbb4cc2 922 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
bc7f77de 923
a654b9d8 924 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
32a7627c
N
925 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
926 bmname(bitmap),
927 (unsigned long) i_size_read(file->f_mapping->host),
928 bytes + sizeof(bitmap_super_t));
4ad13663 929 goto err;
32a7627c 930 }
bc7f77de
N
931
932 ret = -ENOMEM;
933
32a7627c 934 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
bc7f77de 935 if (!bitmap->filemap)
4ad13663 936 goto err;
32a7627c 937
e16b68b6
N
938 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
939 bitmap->filemap_attr = kzalloc(
505fa2c4 940 roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
e16b68b6 941 GFP_KERNEL);
bc7f77de 942 if (!bitmap->filemap_attr)
4ad13663 943 goto err;
32a7627c 944
32a7627c
N
945 oldindex = ~0L;
946
947 for (i = 0; i < chunks; i++) {
bd926c63 948 int b;
32a7627c
N
949 index = file_page_index(i);
950 bit = file_page_offset(i);
951 if (index != oldindex) { /* this is a new page, read it in */
d785a06a 952 int count;
32a7627c 953 /* unmap the old page, we're done with it */
d785a06a 954 if (index == num_pages-1)
f49d5e62
N
955 count = bytes + sizeof(bitmap_super_t)
956 - index * PAGE_SIZE;
d785a06a
N
957 else
958 count = PAGE_SIZE;
32a7627c
N
959 if (index == 0) {
960 /*
961 * if we're here then the superblock page
962 * contains some bits (PAGE_SIZE != sizeof sb)
963 * we've already read it in, so just use it
964 */
965 page = bitmap->sb_page;
966 offset = sizeof(bitmap_super_t);
53845270
N
967 if (!file)
968 read_sb_page(bitmap->mddev,
969 bitmap->offset,
970 page,
971 index, count);
a654b9d8 972 } else if (file) {
d785a06a 973 page = read_page(file, index, bitmap, count);
a654b9d8
N
974 offset = 0;
975 } else {
a2ed9615
N
976 page = read_sb_page(bitmap->mddev, bitmap->offset,
977 NULL,
978 index, count);
32a7627c
N
979 offset = 0;
980 }
a654b9d8
N
981 if (IS_ERR(page)) { /* read error */
982 ret = PTR_ERR(page);
4ad13663 983 goto err;
a654b9d8
N
984 }
985
32a7627c
N
986 oldindex = index;
987 oldpage = page;
32a7627c 988
b74fd282
N
989 bitmap->filemap[bitmap->file_pages++] = page;
990 bitmap->last_page_size = count;
991
32a7627c
N
992 if (outofdate) {
993 /*
994 * if bitmap is out of date, dirty the
995 * whole page and write it out
996 */
ea03aff9
N
997 paddr = kmap_atomic(page, KM_USER0);
998 memset(paddr + offset, 0xff,
6a07997f 999 PAGE_SIZE - offset);
ea03aff9 1000 kunmap_atomic(paddr, KM_USER0);
4ad13663
N
1001 write_page(bitmap, page, 1);
1002
1003 ret = -EIO;
b74fd282 1004 if (bitmap->flags & BITMAP_WRITE_ERROR)
4ad13663 1005 goto err;
32a7627c 1006 }
32a7627c 1007 }
ea03aff9 1008 paddr = kmap_atomic(page, KM_USER0);
bd926c63 1009 if (bitmap->flags & BITMAP_HOSTENDIAN)
ea03aff9 1010 b = test_bit(bit, paddr);
bd926c63 1011 else
ea03aff9
N
1012 b = ext2_test_bit(bit, paddr);
1013 kunmap_atomic(paddr, KM_USER0);
bd926c63 1014 if (b) {
32a7627c 1015 /* if the disk bit is set, set the memory bit */
6a07997f
N
1016 bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
1017 ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
1018 );
32a7627c 1019 bit_cnt++;
6a07997f 1020 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
32a7627c 1021 }
32a7627c
N
1022 }
1023
1024 /* everything went OK */
1025 ret = 0;
1026 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1027
32a7627c
N
1028 if (bit_cnt) { /* Kick recovery if any bits were set */
1029 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1030 md_wakeup_thread(bitmap->mddev->thread);
1031 }
1032
32a7627c 1033 printk(KERN_INFO "%s: bitmap initialized from disk: "
4ad13663
N
1034 "read %lu/%lu pages, set %lu bits\n",
1035 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
1036
1037 return 0;
32a7627c 1038
4ad13663
N
1039 err:
1040 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1041 bmname(bitmap), ret);
32a7627c
N
1042 return ret;
1043}
1044
a654b9d8
N
1045void bitmap_write_all(struct bitmap *bitmap)
1046{
1047 /* We don't actually write all bitmap blocks here,
1048 * just flag them as needing to be written
1049 */
ec7a3197 1050 int i;
a654b9d8 1051
ec7a3197
N
1052 for (i=0; i < bitmap->file_pages; i++)
1053 set_page_attr(bitmap, bitmap->filemap[i],
1054 BITMAP_PAGE_NEEDWRITE);
a654b9d8
N
1055}
1056
32a7627c
N
1057
1058static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1059{
1060 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1061 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1062 bitmap->bp[page].count += inc;
1063/*
1064 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1065 (unsigned long long)offset, inc, bitmap->bp[page].count);
1066*/
1067 bitmap_checkfree(bitmap, page);
1068}
1069static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1070 sector_t offset, int *blocks,
1071 int create);
1072
1073/*
1074 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1075 * out to disk
1076 */
1077
4ad13663 1078void bitmap_daemon_work(struct bitmap *bitmap)
32a7627c 1079{
aa3163f8 1080 unsigned long j;
32a7627c
N
1081 unsigned long flags;
1082 struct page *page = NULL, *lastpage = NULL;
32a7627c 1083 int blocks;
ea03aff9 1084 void *paddr;
32a7627c
N
1085
1086 if (bitmap == NULL)
4ad13663 1087 return;
32a7627c 1088 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
7be3dfec
N
1089 goto done;
1090
32a7627c 1091 bitmap->daemon_lastrun = jiffies;
8311c29d
N
1092 if (bitmap->allclean) {
1093 bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1094 return;
1095 }
1096 bitmap->allclean = 1;
32a7627c
N
1097
1098 for (j = 0; j < bitmap->chunks; j++) {
1099 bitmap_counter_t *bmc;
1100 spin_lock_irqsave(&bitmap->lock, flags);
a654b9d8 1101 if (!bitmap->filemap) {
32a7627c
N
1102 /* error or shutdown */
1103 spin_unlock_irqrestore(&bitmap->lock, flags);
1104 break;
1105 }
1106
1107 page = filemap_get_page(bitmap, j);
32a7627c
N
1108
1109 if (page != lastpage) {
aa3163f8 1110 /* skip this page unless it's marked as needing cleaning */
ec7a3197
N
1111 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1112 int need_write = test_page_attr(bitmap, page,
1113 BITMAP_PAGE_NEEDWRITE);
a647e4bc 1114 if (need_write)
aa3163f8 1115 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
a647e4bc 1116
aa3163f8 1117 spin_unlock_irqrestore(&bitmap->lock, flags);
8311c29d 1118 if (need_write) {
4ad13663 1119 write_page(bitmap, page, 0);
8311c29d
N
1120 bitmap->allclean = 0;
1121 }
aa3163f8
N
1122 continue;
1123 }
1124
32a7627c 1125 /* grab the new page, sync and release the old */
32a7627c 1126 if (lastpage != NULL) {
ec7a3197 1127 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
32a7627c
N
1128 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1129 spin_unlock_irqrestore(&bitmap->lock, flags);
4ad13663 1130 write_page(bitmap, lastpage, 0);
32a7627c
N
1131 } else {
1132 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1133 spin_unlock_irqrestore(&bitmap->lock, flags);
1134 }
32a7627c
N
1135 } else
1136 spin_unlock_irqrestore(&bitmap->lock, flags);
1137 lastpage = page;
a0da84f3
NB
1138
1139 /* We are possibly going to clear some bits, so make
1140 * sure that events_cleared is up-to-date.
1141 */
1142 if (bitmap->need_sync) {
1143 bitmap_super_t *sb;
1144 bitmap->need_sync = 0;
1145 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1146 sb->events_cleared =
1147 cpu_to_le64(bitmap->events_cleared);
1148 kunmap_atomic(sb, KM_USER0);
1149 write_page(bitmap, bitmap->sb_page, 1);
1150 }
32a7627c
N
1151 spin_lock_irqsave(&bitmap->lock, flags);
1152 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1153 }
1154 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1155 &blocks, 0);
1156 if (bmc) {
1157/*
1158 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1159*/
8311c29d
N
1160 if (*bmc)
1161 bitmap->allclean = 0;
1162
32a7627c
N
1163 if (*bmc == 2) {
1164 *bmc=1; /* maybe clear the bit next time */
1165 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1166 } else if (*bmc == 1) {
1167 /* we can clear the bit */
1168 *bmc = 0;
1169 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1170 -1);
1171
1172 /* clear the bit */
ea03aff9 1173 paddr = kmap_atomic(page, KM_USER0);
bd926c63 1174 if (bitmap->flags & BITMAP_HOSTENDIAN)
ea03aff9 1175 clear_bit(file_page_offset(j), paddr);
bd926c63 1176 else
ea03aff9
N
1177 ext2_clear_bit(file_page_offset(j), paddr);
1178 kunmap_atomic(paddr, KM_USER0);
32a7627c
N
1179 }
1180 }
1181 spin_unlock_irqrestore(&bitmap->lock, flags);
1182 }
1183
1184 /* now sync the final page */
1185 if (lastpage != NULL) {
32a7627c 1186 spin_lock_irqsave(&bitmap->lock, flags);
ec7a3197 1187 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
32a7627c
N
1188 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1189 spin_unlock_irqrestore(&bitmap->lock, flags);
4ad13663 1190 write_page(bitmap, lastpage, 0);
32a7627c
N
1191 } else {
1192 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1193 spin_unlock_irqrestore(&bitmap->lock, flags);
1194 }
32a7627c
N
1195 }
1196
7be3dfec 1197 done:
8311c29d
N
1198 if (bitmap->allclean == 0)
1199 bitmap->mddev->thread->timeout = bitmap->daemon_sleep * HZ;
32a7627c
N
1200}
1201
32a7627c
N
1202static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1203 sector_t offset, int *blocks,
1204 int create)
1205{
1206 /* If 'create', we might release the lock and reclaim it.
1207 * The lock must have been taken with interrupts enabled.
1208 * If !create, we don't release the lock.
1209 */
1210 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1211 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1212 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1213 sector_t csize;
1214
1215 if (bitmap_checkpage(bitmap, page, create) < 0) {
1216 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1217 *blocks = csize - (offset & (csize- 1));
1218 return NULL;
1219 }
1220 /* now locked ... */
1221
1222 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1223 /* should we use the first or second counter field
1224 * of the hijacked pointer? */
1225 int hi = (pageoff > PAGE_COUNTER_MASK);
1226 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1227 PAGE_COUNTER_SHIFT - 1);
1228 *blocks = csize - (offset & (csize- 1));
1229 return &((bitmap_counter_t *)
1230 &bitmap->bp[page].map)[hi];
1231 } else { /* page is allocated */
1232 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1233 *blocks = csize - (offset & (csize- 1));
1234 return (bitmap_counter_t *)
1235 &(bitmap->bp[page].map[pageoff]);
1236 }
1237}
1238
4b6d287f 1239int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
32a7627c
N
1240{
1241 if (!bitmap) return 0;
4b6d287f
N
1242
1243 if (behind) {
1244 atomic_inc(&bitmap->behind_writes);
1245 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1246 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1247 }
1248
32a7627c
N
1249 while (sectors) {
1250 int blocks;
1251 bitmap_counter_t *bmc;
1252
1253 spin_lock_irq(&bitmap->lock);
1254 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1255 if (!bmc) {
1256 spin_unlock_irq(&bitmap->lock);
1257 return 0;
1258 }
1259
da6e1a32
NB
1260 if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
1261 DEFINE_WAIT(__wait);
1262 /* note that it is safe to do the prepare_to_wait
1263 * after the test as long as we do it before dropping
1264 * the spinlock.
1265 */
1266 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1267 TASK_UNINTERRUPTIBLE);
1268 spin_unlock_irq(&bitmap->lock);
2ad8b1ef 1269 blk_unplug(bitmap->mddev->queue);
da6e1a32
NB
1270 schedule();
1271 finish_wait(&bitmap->overflow_wait, &__wait);
1272 continue;
1273 }
1274
32a7627c
N
1275 switch(*bmc) {
1276 case 0:
1277 bitmap_file_set_bit(bitmap, offset);
1278 bitmap_count_page(bitmap,offset, 1);
93769f58 1279 blk_plug_device_unlocked(bitmap->mddev->queue);
32a7627c
N
1280 /* fall through */
1281 case 1:
1282 *bmc = 2;
1283 }
da6e1a32 1284
32a7627c
N
1285 (*bmc)++;
1286
1287 spin_unlock_irq(&bitmap->lock);
1288
1289 offset += blocks;
1290 if (sectors > blocks)
1291 sectors -= blocks;
1292 else sectors = 0;
1293 }
8311c29d 1294 bitmap->allclean = 0;
32a7627c
N
1295 return 0;
1296}
1297
1298void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
4b6d287f 1299 int success, int behind)
32a7627c
N
1300{
1301 if (!bitmap) return;
4b6d287f
N
1302 if (behind) {
1303 atomic_dec(&bitmap->behind_writes);
1304 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1305 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1306 }
d0a4bb49
N
1307 if (bitmap->mddev->degraded)
1308 /* Never clear bits or update events_cleared when degraded */
1309 success = 0;
4b6d287f 1310
32a7627c
N
1311 while (sectors) {
1312 int blocks;
1313 unsigned long flags;
1314 bitmap_counter_t *bmc;
1315
1316 spin_lock_irqsave(&bitmap->lock, flags);
1317 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1318 if (!bmc) {
1319 spin_unlock_irqrestore(&bitmap->lock, flags);
1320 return;
1321 }
1322
a0da84f3
NB
1323 if (success &&
1324 bitmap->events_cleared < bitmap->mddev->events) {
1325 bitmap->events_cleared = bitmap->mddev->events;
1326 bitmap->need_sync = 1;
1327 }
1328
32a7627c
N
1329 if (!success && ! (*bmc & NEEDED_MASK))
1330 *bmc |= NEEDED_MASK;
1331
da6e1a32
NB
1332 if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
1333 wake_up(&bitmap->overflow_wait);
1334
32a7627c
N
1335 (*bmc)--;
1336 if (*bmc <= 2) {
1337 set_page_attr(bitmap,
1338 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1339 BITMAP_PAGE_CLEAN);
1340 }
1341 spin_unlock_irqrestore(&bitmap->lock, flags);
1342 offset += blocks;
1343 if (sectors > blocks)
1344 sectors -= blocks;
1345 else sectors = 0;
1346 }
1347}
1348
1187cf0a
N
1349static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1350 int degraded)
32a7627c
N
1351{
1352 bitmap_counter_t *bmc;
1353 int rv;
1354 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1355 *blocks = 1024;
1356 return 1; /* always resync if no bitmap */
1357 }
1358 spin_lock_irq(&bitmap->lock);
1359 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1360 rv = 0;
1361 if (bmc) {
1362 /* locked */
1363 if (RESYNC(*bmc))
1364 rv = 1;
1365 else if (NEEDED(*bmc)) {
1366 rv = 1;
6a806c51
N
1367 if (!degraded) { /* don't set/clear bits if degraded */
1368 *bmc |= RESYNC_MASK;
1369 *bmc &= ~NEEDED_MASK;
1370 }
32a7627c
N
1371 }
1372 }
1373 spin_unlock_irq(&bitmap->lock);
8311c29d 1374 bitmap->allclean = 0;
32a7627c
N
1375 return rv;
1376}
1377
1187cf0a
N
1378int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1379 int degraded)
1380{
1381 /* bitmap_start_sync must always report on multiples of whole
1382 * pages, otherwise resync (which is very PAGE_SIZE based) will
1383 * get confused.
1384 * So call __bitmap_start_sync repeatedly (if needed) until
1385 * At least PAGE_SIZE>>9 blocks are covered.
1386 * Return the 'or' of the result.
1387 */
1388 int rv = 0;
1389 int blocks1;
1390
1391 *blocks = 0;
1392 while (*blocks < (PAGE_SIZE>>9)) {
1393 rv |= __bitmap_start_sync(bitmap, offset,
1394 &blocks1, degraded);
1395 offset += blocks1;
1396 *blocks += blocks1;
1397 }
1398 return rv;
1399}
1400
32a7627c
N
1401void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1402{
1403 bitmap_counter_t *bmc;
1404 unsigned long flags;
1405/*
1406 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1407*/ if (bitmap == NULL) {
1408 *blocks = 1024;
1409 return;
1410 }
1411 spin_lock_irqsave(&bitmap->lock, flags);
1412 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1413 if (bmc == NULL)
1414 goto unlock;
1415 /* locked */
1416/*
1417 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1418*/
1419 if (RESYNC(*bmc)) {
1420 *bmc &= ~RESYNC_MASK;
1421
1422 if (!NEEDED(*bmc) && aborted)
1423 *bmc |= NEEDED_MASK;
1424 else {
1425 if (*bmc <= 2) {
1426 set_page_attr(bitmap,
1427 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1428 BITMAP_PAGE_CLEAN);
1429 }
1430 }
1431 }
1432 unlock:
1433 spin_unlock_irqrestore(&bitmap->lock, flags);
8311c29d 1434 bitmap->allclean = 0;
32a7627c
N
1435}
1436
1437void bitmap_close_sync(struct bitmap *bitmap)
1438{
1439 /* Sync has finished, and any bitmap chunks that weren't synced
1440 * properly have been aborted. It remains to us to clear the
1441 * RESYNC bit wherever it is still on
1442 */
1443 sector_t sector = 0;
1444 int blocks;
b47490c9
N
1445 if (!bitmap)
1446 return;
32a7627c
N
1447 while (sector < bitmap->mddev->resync_max_sectors) {
1448 bitmap_end_sync(bitmap, sector, &blocks, 0);
b47490c9
N
1449 sector += blocks;
1450 }
1451}
1452
1453void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1454{
1455 sector_t s = 0;
1456 int blocks;
1457
1458 if (!bitmap)
1459 return;
1460 if (sector == 0) {
1461 bitmap->last_end_sync = jiffies;
1462 return;
1463 }
1464 if (time_before(jiffies, (bitmap->last_end_sync
1465 + bitmap->daemon_sleep * HZ)))
1466 return;
1467 wait_event(bitmap->mddev->recovery_wait,
1468 atomic_read(&bitmap->mddev->recovery_active) == 0);
1469
97e4f42d
N
1470 bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync;
1471 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
b47490c9
N
1472 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1473 s = 0;
1474 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1475 bitmap_end_sync(bitmap, s, &blocks, 0);
1476 s += blocks;
32a7627c 1477 }
b47490c9 1478 bitmap->last_end_sync = jiffies;
acb180b0 1479 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
32a7627c
N
1480}
1481
6a07997f 1482static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
32a7627c
N
1483{
1484 /* For each chunk covered by any of these sectors, set the
193f1c93 1485 * counter to 1 and set resync_needed. They should all
32a7627c
N
1486 * be 0 at this point
1487 */
193f1c93
N
1488
1489 int secs;
1490 bitmap_counter_t *bmc;
1491 spin_lock_irq(&bitmap->lock);
1492 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1493 if (!bmc) {
32a7627c 1494 spin_unlock_irq(&bitmap->lock);
193f1c93 1495 return;
32a7627c 1496 }
193f1c93
N
1497 if (! *bmc) {
1498 struct page *page;
6a07997f 1499 *bmc = 1 | (needed?NEEDED_MASK:0);
193f1c93
N
1500 bitmap_count_page(bitmap, offset, 1);
1501 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1502 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1503 }
1504 spin_unlock_irq(&bitmap->lock);
8311c29d 1505 bitmap->allclean = 0;
32a7627c
N
1506}
1507
9b1d1dac
PC
1508/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1509void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1510{
1511 unsigned long chunk;
1512
1513 for (chunk = s; chunk <= e; chunk++) {
1514 sector_t sec = chunk << CHUNK_BLOCK_SHIFT(bitmap);
1515 bitmap_set_memory_bits(bitmap, sec, 1);
1516 bitmap_file_set_bit(bitmap, sec);
1517 }
1518}
1519
6b8b3e8a
N
1520/*
1521 * flush out any pending updates
1522 */
1523void bitmap_flush(mddev_t *mddev)
1524{
1525 struct bitmap *bitmap = mddev->bitmap;
1526 int sleep;
1527
1528 if (!bitmap) /* there was no bitmap */
1529 return;
1530
1531 /* run the daemon_work three time to ensure everything is flushed
1532 * that can be
1533 */
1534 sleep = bitmap->daemon_sleep;
1535 bitmap->daemon_sleep = 0;
1536 bitmap_daemon_work(bitmap);
1537 bitmap_daemon_work(bitmap);
1538 bitmap_daemon_work(bitmap);
1539 bitmap->daemon_sleep = sleep;
1540 bitmap_update_sb(bitmap);
1541}
1542
32a7627c
N
1543/*
1544 * free memory that was allocated
1545 */
3178b0db 1546static void bitmap_free(struct bitmap *bitmap)
32a7627c
N
1547{
1548 unsigned long k, pages;
1549 struct bitmap_page *bp;
32a7627c
N
1550
1551 if (!bitmap) /* there was no bitmap */
1552 return;
1553
32a7627c
N
1554 /* release the bitmap file and kill the daemon */
1555 bitmap_file_put(bitmap);
1556
1557 bp = bitmap->bp;
1558 pages = bitmap->pages;
1559
1560 /* free all allocated memory */
1561
32a7627c
N
1562 if (bp) /* deallocate the page memory */
1563 for (k = 0; k < pages; k++)
1564 if (bp[k].map && !bp[k].hijacked)
1565 kfree(bp[k].map);
1566 kfree(bp);
1567 kfree(bitmap);
1568}
3178b0db
N
1569void bitmap_destroy(mddev_t *mddev)
1570{
1571 struct bitmap *bitmap = mddev->bitmap;
1572
1573 if (!bitmap) /* there was no bitmap */
1574 return;
1575
1576 mddev->bitmap = NULL; /* disconnect from the md device */
b15c2e57
N
1577 if (mddev->thread)
1578 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
3178b0db
N
1579
1580 bitmap_free(bitmap);
1581}
32a7627c
N
1582
1583/*
1584 * initialize the bitmap structure
1585 * if this returns an error, bitmap_destroy must be called to do clean up
1586 */
1587int bitmap_create(mddev_t *mddev)
1588{
1589 struct bitmap *bitmap;
1f593903 1590 sector_t blocks = mddev->resync_max_sectors;
32a7627c
N
1591 unsigned long chunks;
1592 unsigned long pages;
1593 struct file *file = mddev->bitmap_file;
1594 int err;
6a07997f 1595 sector_t start;
32a7627c 1596
5f6e3c83 1597 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
32a7627c 1598
a654b9d8 1599 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
32a7627c
N
1600 return 0;
1601
a654b9d8
N
1602 BUG_ON(file && mddev->bitmap_offset);
1603
9ffae0cf 1604 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
32a7627c
N
1605 if (!bitmap)
1606 return -ENOMEM;
1607
32a7627c 1608 spin_lock_init(&bitmap->lock);
ce25c31b
N
1609 atomic_set(&bitmap->pending_writes, 0);
1610 init_waitqueue_head(&bitmap->write_wait);
da6e1a32 1611 init_waitqueue_head(&bitmap->overflow_wait);
ce25c31b 1612
32a7627c 1613 bitmap->mddev = mddev;
32a7627c 1614
32a7627c 1615 bitmap->file = file;
a654b9d8 1616 bitmap->offset = mddev->bitmap_offset;
ce25c31b
N
1617 if (file) {
1618 get_file(file);
ef51c976
MF
1619 do_sync_mapping_range(file->f_mapping, 0, LLONG_MAX,
1620 SYNC_FILE_RANGE_WAIT_BEFORE |
1621 SYNC_FILE_RANGE_WRITE |
1622 SYNC_FILE_RANGE_WAIT_AFTER);
ce25c31b 1623 }
32a7627c
N
1624 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1625 err = bitmap_read_sb(bitmap);
1626 if (err)
3178b0db 1627 goto error;
32a7627c 1628
a638b2dc 1629 bitmap->chunkshift = ffz(~bitmap->chunksize);
32a7627c
N
1630
1631 /* now that chunksize and chunkshift are set, we can use these macros */
1f593903
N
1632 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
1633 CHUNK_BLOCK_SHIFT(bitmap);
32a7627c
N
1634 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1635
1636 BUG_ON(!pages);
1637
1638 bitmap->chunks = chunks;
1639 bitmap->pages = pages;
1640 bitmap->missing_pages = pages;
1641 bitmap->counter_bits = COUNTER_BITS;
1642
1643 bitmap->syncchunk = ~0UL;
1644
44456d37 1645#ifdef INJECT_FATAL_FAULT_1
32a7627c
N
1646 bitmap->bp = NULL;
1647#else
9ffae0cf 1648 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
32a7627c 1649#endif
3178b0db 1650 err = -ENOMEM;
32a7627c 1651 if (!bitmap->bp)
3178b0db 1652 goto error;
32a7627c 1653
32a7627c
N
1654 /* now that we have some pages available, initialize the in-memory
1655 * bitmap from the on-disk bitmap */
6a07997f
N
1656 start = 0;
1657 if (mddev->degraded == 0
1658 || bitmap->events_cleared == mddev->events)
1659 /* no need to keep dirty bits to optimise a re-add of a missing device */
1660 start = mddev->recovery_cp;
1661 err = bitmap_init_from_disk(bitmap, start);
193f1c93 1662
32a7627c 1663 if (err)
3178b0db 1664 goto error;
32a7627c
N
1665
1666 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1667 pages, bmname(bitmap));
1668
3178b0db
N
1669 mddev->bitmap = bitmap;
1670
b15c2e57
N
1671 mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1672
4ad13663
N
1673 bitmap_update_sb(bitmap);
1674
1675 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
3178b0db
N
1676
1677 error:
1678 bitmap_free(bitmap);
1679 return err;
32a7627c
N
1680}
1681
1682/* the bitmap API -- for raid personalities */
1683EXPORT_SYMBOL(bitmap_startwrite);
1684EXPORT_SYMBOL(bitmap_endwrite);
1685EXPORT_SYMBOL(bitmap_start_sync);
1686EXPORT_SYMBOL(bitmap_end_sync);
1687EXPORT_SYMBOL(bitmap_unplug);
1688EXPORT_SYMBOL(bitmap_close_sync);
b47490c9 1689EXPORT_SYMBOL(bitmap_cond_end_sync);