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