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