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