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