]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/md/bitmap.c
md: support updating bitmap parameters via sysfs.
[net-next-2.6.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;
32a7627c
N
500 spin_lock_irqsave(&bitmap->lock, flags);
501 if (!bitmap->sb_page) { /* no superblock */
502 spin_unlock_irqrestore(&bitmap->lock, flags);
4ad13663 503 return;
32a7627c 504 }
32a7627c 505 spin_unlock_irqrestore(&bitmap->lock, flags);
ea03aff9 506 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
32a7627c 507 sb->events = cpu_to_le64(bitmap->mddev->events);
a0da84f3
NB
508 if (bitmap->mddev->events < bitmap->events_cleared) {
509 /* rocking back to read-only */
510 bitmap->events_cleared = bitmap->mddev->events;
511 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
512 }
43a70507
N
513 /* Just in case these have been changed via sysfs: */
514 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
515 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
ea03aff9 516 kunmap_atomic(sb, KM_USER0);
4ad13663 517 write_page(bitmap, bitmap->sb_page, 1);
32a7627c
N
518}
519
520/* print out the bitmap file superblock */
521void bitmap_print_sb(struct bitmap *bitmap)
522{
523 bitmap_super_t *sb;
524
525 if (!bitmap || !bitmap->sb_page)
526 return;
ea03aff9 527 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
32a7627c 528 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
a2cff26a
N
529 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
530 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
531 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
32a7627c
N
532 *(__u32 *)(sb->uuid+0),
533 *(__u32 *)(sb->uuid+4),
534 *(__u32 *)(sb->uuid+8),
535 *(__u32 *)(sb->uuid+12));
a2cff26a 536 printk(KERN_DEBUG " events: %llu\n",
32a7627c 537 (unsigned long long) le64_to_cpu(sb->events));
a2cff26a 538 printk(KERN_DEBUG "events cleared: %llu\n",
32a7627c 539 (unsigned long long) le64_to_cpu(sb->events_cleared));
a2cff26a
N
540 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
541 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
542 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
543 printk(KERN_DEBUG " sync size: %llu KB\n",
544 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
4b6d287f 545 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
ea03aff9 546 kunmap_atomic(sb, KM_USER0);
32a7627c
N
547}
548
549/* read the superblock from the bitmap file and initialize some bitmap fields */
550static int bitmap_read_sb(struct bitmap *bitmap)
551{
552 char *reason = NULL;
553 bitmap_super_t *sb;
4b6d287f 554 unsigned long chunksize, daemon_sleep, write_behind;
32a7627c
N
555 unsigned long long events;
556 int err = -EINVAL;
557
558 /* page 0 is the superblock, read it... */
f49d5e62
N
559 if (bitmap->file) {
560 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
561 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
562
563 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
564 } else {
42a04b50
N
565 bitmap->sb_page = read_sb_page(bitmap->mddev,
566 bitmap->mddev->bitmap_info.offset,
a2ed9615
N
567 NULL,
568 0, sizeof(bitmap_super_t));
a654b9d8 569 }
32a7627c
N
570 if (IS_ERR(bitmap->sb_page)) {
571 err = PTR_ERR(bitmap->sb_page);
572 bitmap->sb_page = NULL;
573 return err;
574 }
575
ea03aff9 576 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
32a7627c 577
32a7627c 578 chunksize = le32_to_cpu(sb->chunksize);
1b04be96 579 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
4b6d287f 580 write_behind = le32_to_cpu(sb->write_behind);
32a7627c
N
581
582 /* verify that the bitmap-specific fields are valid */
583 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
584 reason = "bad magic";
bd926c63
N
585 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
586 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
32a7627c 587 reason = "unrecognized superblock version";
1187cf0a 588 else if (chunksize < 512)
7dd5d34c 589 reason = "bitmap chunksize too small";
32a7627c
N
590 else if ((1 << ffz(~chunksize)) != chunksize)
591 reason = "bitmap chunksize not a power of 2";
1b04be96 592 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
7dd5d34c 593 reason = "daemon sleep period out of range";
4b6d287f
N
594 else if (write_behind > COUNTER_MAX)
595 reason = "write-behind limit out of range (0 - 16383)";
32a7627c
N
596 if (reason) {
597 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
598 bmname(bitmap), reason);
599 goto out;
600 }
601
602 /* keep the array size field of the bitmap superblock up to date */
603 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
604
605 if (!bitmap->mddev->persistent)
606 goto success;
607
608 /*
609 * if we have a persistent array superblock, compare the
610 * bitmap's UUID and event counter to the mddev's
611 */
612 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
613 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
614 bmname(bitmap));
615 goto out;
616 }
617 events = le64_to_cpu(sb->events);
618 if (events < bitmap->mddev->events) {
619 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
620 "-- forcing full recovery\n", bmname(bitmap), events,
621 (unsigned long long) bitmap->mddev->events);
4f2e639a 622 sb->state |= cpu_to_le32(BITMAP_STALE);
32a7627c
N
623 }
624success:
625 /* assign fields using values from superblock */
42a04b50
N
626 bitmap->mddev->bitmap_info.chunksize = chunksize;
627 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
585f0dd5 628 bitmap->daemon_lastrun = jiffies;
42a04b50 629 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
4f2e639a 630 bitmap->flags |= le32_to_cpu(sb->state);
bd926c63
N
631 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
632 bitmap->flags |= BITMAP_HOSTENDIAN;
32a7627c 633 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
4f2e639a 634 if (sb->state & cpu_to_le32(BITMAP_STALE))
6a07997f 635 bitmap->events_cleared = bitmap->mddev->events;
32a7627c
N
636 err = 0;
637out:
ea03aff9 638 kunmap_atomic(sb, KM_USER0);
32a7627c
N
639 if (err)
640 bitmap_print_sb(bitmap);
641 return err;
642}
643
644enum bitmap_mask_op {
645 MASK_SET,
646 MASK_UNSET
647};
648
4ad13663
N
649/* record the state of the bitmap in the superblock. Return the old value */
650static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
651 enum bitmap_mask_op op)
32a7627c
N
652{
653 bitmap_super_t *sb;
654 unsigned long flags;
4ad13663 655 int old;
32a7627c
N
656
657 spin_lock_irqsave(&bitmap->lock, flags);
7e317655 658 if (!bitmap->sb_page) { /* can't set the state */
32a7627c 659 spin_unlock_irqrestore(&bitmap->lock, flags);
4ad13663 660 return 0;
32a7627c 661 }
32a7627c 662 spin_unlock_irqrestore(&bitmap->lock, flags);
ea03aff9 663 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
4ad13663 664 old = le32_to_cpu(sb->state) & bits;
32a7627c 665 switch (op) {
4f2e639a 666 case MASK_SET: sb->state |= cpu_to_le32(bits);
32a7627c 667 break;
4f2e639a 668 case MASK_UNSET: sb->state &= cpu_to_le32(~bits);
32a7627c
N
669 break;
670 default: BUG();
671 }
ea03aff9 672 kunmap_atomic(sb, KM_USER0);
4ad13663 673 return old;
32a7627c
N
674}
675
676/*
677 * general bitmap file operations
678 */
679
680/* calculate the index of the page that contains this bit */
681static inline unsigned long file_page_index(unsigned long chunk)
682{
683 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
684}
685
686/* calculate the (bit) offset of this bit within a page */
687static inline unsigned long file_page_offset(unsigned long chunk)
688{
689 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
690}
691
692/*
693 * return a pointer to the page in the filemap that contains the given bit
694 *
695 * this lookup is complicated by the fact that the bitmap sb might be exactly
696 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
697 * 0 or page 1
698 */
699static inline struct page *filemap_get_page(struct bitmap *bitmap,
700 unsigned long chunk)
701{
9b1d1dac 702 if (file_page_index(chunk) >= bitmap->file_pages) return NULL;
32a7627c
N
703 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
704}
705
706
707static void bitmap_file_unmap(struct bitmap *bitmap)
708{
709 struct page **map, *sb_page;
710 unsigned long *attr;
711 int pages;
712 unsigned long flags;
713
714 spin_lock_irqsave(&bitmap->lock, flags);
715 map = bitmap->filemap;
716 bitmap->filemap = NULL;
717 attr = bitmap->filemap_attr;
718 bitmap->filemap_attr = NULL;
719 pages = bitmap->file_pages;
720 bitmap->file_pages = 0;
721 sb_page = bitmap->sb_page;
722 bitmap->sb_page = NULL;
723 spin_unlock_irqrestore(&bitmap->lock, flags);
724
725 while (pages--)
726 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
d785a06a 727 free_buffers(map[pages]);
32a7627c
N
728 kfree(map);
729 kfree(attr);
730
d785a06a
N
731 if (sb_page)
732 free_buffers(sb_page);
32a7627c
N
733}
734
735static void bitmap_file_put(struct bitmap *bitmap)
736{
737 struct file *file;
32a7627c
N
738 unsigned long flags;
739
740 spin_lock_irqsave(&bitmap->lock, flags);
741 file = bitmap->file;
742 bitmap->file = NULL;
743 spin_unlock_irqrestore(&bitmap->lock, flags);
744
d785a06a
N
745 if (file)
746 wait_event(bitmap->write_wait,
747 atomic_read(&bitmap->pending_writes)==0);
32a7627c
N
748 bitmap_file_unmap(bitmap);
749
d785a06a 750 if (file) {
c649bb9c 751 struct inode *inode = file->f_path.dentry->d_inode;
fc0ecff6 752 invalidate_mapping_pages(inode->i_mapping, 0, -1);
32a7627c 753 fput(file);
d785a06a 754 }
32a7627c
N
755}
756
757
758/*
759 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
760 * then it is no longer reliable, so we stop using it and we mark the file
761 * as failed in the superblock
762 */
763static void bitmap_file_kick(struct bitmap *bitmap)
764{
765 char *path, *ptr = NULL;
766
4ad13663
N
767 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
768 bitmap_update_sb(bitmap);
32a7627c 769
4ad13663
N
770 if (bitmap->file) {
771 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
772 if (path)
6bcfd601
CH
773 ptr = d_path(&bitmap->file->f_path, path,
774 PAGE_SIZE);
775
32a7627c 776
4ad13663
N
777 printk(KERN_ALERT
778 "%s: kicking failed bitmap file %s from array!\n",
6bcfd601 779 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
32a7627c 780
4ad13663
N
781 kfree(path);
782 } else
783 printk(KERN_ALERT
784 "%s: disabling internal bitmap due to errors\n",
785 bmname(bitmap));
a654b9d8 786 }
32a7627c
N
787
788 bitmap_file_put(bitmap);
789
790 return;
791}
792
793enum bitmap_page_attr {
e16b68b6
N
794 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
795 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
796 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
32a7627c
N
797};
798
799static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
800 enum bitmap_page_attr attr)
801{
e16b68b6 802 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
32a7627c
N
803}
804
805static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
806 enum bitmap_page_attr attr)
807{
e16b68b6 808 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
32a7627c
N
809}
810
ec7a3197
N
811static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
812 enum bitmap_page_attr attr)
32a7627c 813{
e16b68b6 814 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
32a7627c
N
815}
816
817/*
818 * bitmap_file_set_bit -- called before performing a write to the md device
819 * to set (and eventually sync) a particular bit in the bitmap file
820 *
821 * we set the bit immediately, then we record the page number so that
822 * when an unplug occurs, we can flush the dirty pages out to disk
823 */
824static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
825{
826 unsigned long bit;
827 struct page *page;
828 void *kaddr;
829 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
830
a654b9d8 831 if (!bitmap->filemap) {
32a7627c
N
832 return;
833 }
834
835 page = filemap_get_page(bitmap, chunk);
9b1d1dac 836 if (!page) return;
32a7627c
N
837 bit = file_page_offset(chunk);
838
32a7627c
N
839 /* set the bit */
840 kaddr = kmap_atomic(page, KM_USER0);
bd926c63
N
841 if (bitmap->flags & BITMAP_HOSTENDIAN)
842 set_bit(bit, kaddr);
843 else
844 ext2_set_bit(bit, kaddr);
32a7627c
N
845 kunmap_atomic(kaddr, KM_USER0);
846 PRINTK("set file bit %lu page %lu\n", bit, page->index);
847
848 /* record page number so it gets flushed to disk when unplug occurs */
849 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
850
851}
852
853/* this gets called when the md device is ready to unplug its underlying
854 * (slave) device queues -- before we let any writes go down, we need to
855 * sync the dirty pages of the bitmap file to disk */
4ad13663 856void bitmap_unplug(struct bitmap *bitmap)
32a7627c 857{
ec7a3197
N
858 unsigned long i, flags;
859 int dirty, need_write;
32a7627c
N
860 struct page *page;
861 int wait = 0;
862
863 if (!bitmap)
4ad13663 864 return;
32a7627c
N
865
866 /* look at each page to see if there are any set bits that need to be
867 * flushed out to disk */
868 for (i = 0; i < bitmap->file_pages; i++) {
869 spin_lock_irqsave(&bitmap->lock, flags);
a654b9d8 870 if (!bitmap->filemap) {
32a7627c 871 spin_unlock_irqrestore(&bitmap->lock, flags);
4ad13663 872 return;
32a7627c
N
873 }
874 page = bitmap->filemap[i];
ec7a3197
N
875 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
876 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
32a7627c
N
877 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
878 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
ec7a3197 879 if (dirty)
32a7627c
N
880 wait = 1;
881 spin_unlock_irqrestore(&bitmap->lock, flags);
882
d785a06a 883 if (dirty | need_write)
4ad13663 884 write_page(bitmap, page, 0);
32a7627c
N
885 }
886 if (wait) { /* if any writes were performed, we need to wait on them */
0b79ccf0 887 if (bitmap->file)
d785a06a
N
888 wait_event(bitmap->write_wait,
889 atomic_read(&bitmap->pending_writes)==0);
0b79ccf0 890 else
a9701a30 891 md_super_wait(bitmap->mddev);
32a7627c 892 }
d785a06a
N
893 if (bitmap->flags & BITMAP_WRITE_ERROR)
894 bitmap_file_kick(bitmap);
32a7627c
N
895}
896
6a07997f 897static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
32a7627c
N
898/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
899 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
900 * memory mapping of the bitmap file
901 * Special cases:
902 * if there's no bitmap file, or if the bitmap file had been
903 * previously kicked from the array, we mark all the bits as
904 * 1's in order to cause a full resync.
6a07997f
N
905 *
906 * We ignore all bits for sectors that end earlier than 'start'.
907 * This is used when reading an out-of-date bitmap...
32a7627c 908 */
6a07997f 909static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
32a7627c
N
910{
911 unsigned long i, chunks, index, oldindex, bit;
912 struct page *page = NULL, *oldpage = NULL;
913 unsigned long num_pages, bit_cnt = 0;
914 struct file *file;
d785a06a 915 unsigned long bytes, offset;
32a7627c
N
916 int outofdate;
917 int ret = -ENOSPC;
ea03aff9 918 void *paddr;
32a7627c
N
919
920 chunks = bitmap->chunks;
921 file = bitmap->file;
922
42a04b50 923 BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
32a7627c 924
44456d37 925#ifdef INJECT_FAULTS_3
32a7627c
N
926 outofdate = 1;
927#else
928 outofdate = bitmap->flags & BITMAP_STALE;
929#endif
930 if (outofdate)
931 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
932 "recovery\n", bmname(bitmap));
933
934 bytes = (chunks + 7) / 8;
bc7f77de 935
cdbb4cc2 936 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
bc7f77de 937
a654b9d8 938 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
32a7627c
N
939 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
940 bmname(bitmap),
941 (unsigned long) i_size_read(file->f_mapping->host),
942 bytes + sizeof(bitmap_super_t));
4ad13663 943 goto err;
32a7627c 944 }
bc7f77de
N
945
946 ret = -ENOMEM;
947
32a7627c 948 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
bc7f77de 949 if (!bitmap->filemap)
4ad13663 950 goto err;
32a7627c 951
e16b68b6
N
952 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
953 bitmap->filemap_attr = kzalloc(
505fa2c4 954 roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
e16b68b6 955 GFP_KERNEL);
bc7f77de 956 if (!bitmap->filemap_attr)
4ad13663 957 goto err;
32a7627c 958
32a7627c
N
959 oldindex = ~0L;
960
961 for (i = 0; i < chunks; i++) {
bd926c63 962 int b;
32a7627c
N
963 index = file_page_index(i);
964 bit = file_page_offset(i);
965 if (index != oldindex) { /* this is a new page, read it in */
d785a06a 966 int count;
32a7627c 967 /* unmap the old page, we're done with it */
d785a06a 968 if (index == num_pages-1)
f49d5e62
N
969 count = bytes + sizeof(bitmap_super_t)
970 - index * PAGE_SIZE;
d785a06a
N
971 else
972 count = PAGE_SIZE;
32a7627c
N
973 if (index == 0) {
974 /*
975 * if we're here then the superblock page
976 * contains some bits (PAGE_SIZE != sizeof sb)
977 * we've already read it in, so just use it
978 */
979 page = bitmap->sb_page;
980 offset = sizeof(bitmap_super_t);
53845270
N
981 if (!file)
982 read_sb_page(bitmap->mddev,
42a04b50 983 bitmap->mddev->bitmap_info.offset,
53845270
N
984 page,
985 index, count);
a654b9d8 986 } else if (file) {
d785a06a 987 page = read_page(file, index, bitmap, count);
a654b9d8
N
988 offset = 0;
989 } else {
42a04b50
N
990 page = read_sb_page(bitmap->mddev,
991 bitmap->mddev->bitmap_info.offset,
a2ed9615
N
992 NULL,
993 index, count);
32a7627c
N
994 offset = 0;
995 }
a654b9d8
N
996 if (IS_ERR(page)) { /* read error */
997 ret = PTR_ERR(page);
4ad13663 998 goto err;
a654b9d8
N
999 }
1000
32a7627c
N
1001 oldindex = index;
1002 oldpage = page;
32a7627c 1003
b74fd282
N
1004 bitmap->filemap[bitmap->file_pages++] = page;
1005 bitmap->last_page_size = count;
1006
32a7627c
N
1007 if (outofdate) {
1008 /*
1009 * if bitmap is out of date, dirty the
1010 * whole page and write it out
1011 */
ea03aff9
N
1012 paddr = kmap_atomic(page, KM_USER0);
1013 memset(paddr + offset, 0xff,
6a07997f 1014 PAGE_SIZE - offset);
ea03aff9 1015 kunmap_atomic(paddr, KM_USER0);
4ad13663
N
1016 write_page(bitmap, page, 1);
1017
1018 ret = -EIO;
b74fd282 1019 if (bitmap->flags & BITMAP_WRITE_ERROR)
4ad13663 1020 goto err;
32a7627c 1021 }
32a7627c 1022 }
ea03aff9 1023 paddr = kmap_atomic(page, KM_USER0);
bd926c63 1024 if (bitmap->flags & BITMAP_HOSTENDIAN)
ea03aff9 1025 b = test_bit(bit, paddr);
bd926c63 1026 else
ea03aff9
N
1027 b = ext2_test_bit(bit, paddr);
1028 kunmap_atomic(paddr, KM_USER0);
bd926c63 1029 if (b) {
32a7627c 1030 /* if the disk bit is set, set the memory bit */
db305e50
N
1031 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1032 >= start);
1033 bitmap_set_memory_bits(bitmap,
1034 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1035 needed);
32a7627c 1036 bit_cnt++;
6a07997f 1037 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
32a7627c 1038 }
32a7627c
N
1039 }
1040
1041 /* everything went OK */
1042 ret = 0;
1043 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1044
32a7627c
N
1045 if (bit_cnt) { /* Kick recovery if any bits were set */
1046 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1047 md_wakeup_thread(bitmap->mddev->thread);
1048 }
1049
32a7627c 1050 printk(KERN_INFO "%s: bitmap initialized from disk: "
4ad13663
N
1051 "read %lu/%lu pages, set %lu bits\n",
1052 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
1053
1054 return 0;
32a7627c 1055
4ad13663
N
1056 err:
1057 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1058 bmname(bitmap), ret);
32a7627c
N
1059 return ret;
1060}
1061
a654b9d8
N
1062void bitmap_write_all(struct bitmap *bitmap)
1063{
1064 /* We don't actually write all bitmap blocks here,
1065 * just flag them as needing to be written
1066 */
ec7a3197 1067 int i;
a654b9d8 1068
ec7a3197
N
1069 for (i=0; i < bitmap->file_pages; i++)
1070 set_page_attr(bitmap, bitmap->filemap[i],
1071 BITMAP_PAGE_NEEDWRITE);
a654b9d8
N
1072}
1073
32a7627c
N
1074
1075static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1076{
1077 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1078 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1079 bitmap->bp[page].count += inc;
1080/*
1081 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1082 (unsigned long long)offset, inc, bitmap->bp[page].count);
1083*/
1084 bitmap_checkfree(bitmap, page);
1085}
1086static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1087 sector_t offset, int *blocks,
1088 int create);
1089
1090/*
1091 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1092 * out to disk
1093 */
1094
aa5cbd10 1095void bitmap_daemon_work(mddev_t *mddev)
32a7627c 1096{
aa5cbd10 1097 struct bitmap *bitmap;
aa3163f8 1098 unsigned long j;
32a7627c
N
1099 unsigned long flags;
1100 struct page *page = NULL, *lastpage = NULL;
32a7627c 1101 int blocks;
ea03aff9 1102 void *paddr;
32a7627c 1103
aa5cbd10
N
1104 /* Use a mutex to guard daemon_work against
1105 * bitmap_destroy.
1106 */
c3d9714e 1107 mutex_lock(&mddev->bitmap_info.mutex);
aa5cbd10
N
1108 bitmap = mddev->bitmap;
1109 if (bitmap == NULL) {
c3d9714e 1110 mutex_unlock(&mddev->bitmap_info.mutex);
4ad13663 1111 return;
aa5cbd10 1112 }
42a04b50 1113 if (time_before(jiffies, bitmap->daemon_lastrun
1b04be96 1114 + bitmap->mddev->bitmap_info.daemon_sleep))
7be3dfec
N
1115 goto done;
1116
32a7627c 1117 bitmap->daemon_lastrun = jiffies;
8311c29d
N
1118 if (bitmap->allclean) {
1119 bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
aa5cbd10 1120 goto done;
8311c29d
N
1121 }
1122 bitmap->allclean = 1;
32a7627c 1123
be512691 1124 spin_lock_irqsave(&bitmap->lock, flags);
32a7627c
N
1125 for (j = 0; j < bitmap->chunks; j++) {
1126 bitmap_counter_t *bmc;
be512691 1127 if (!bitmap->filemap)
32a7627c 1128 /* error or shutdown */
32a7627c 1129 break;
32a7627c
N
1130
1131 page = filemap_get_page(bitmap, j);
32a7627c
N
1132
1133 if (page != lastpage) {
aa3163f8 1134 /* skip this page unless it's marked as needing cleaning */
ec7a3197
N
1135 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1136 int need_write = test_page_attr(bitmap, page,
1137 BITMAP_PAGE_NEEDWRITE);
a647e4bc 1138 if (need_write)
aa3163f8 1139 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
a647e4bc 1140
aa3163f8 1141 spin_unlock_irqrestore(&bitmap->lock, flags);
8311c29d 1142 if (need_write) {
4ad13663 1143 write_page(bitmap, page, 0);
8311c29d
N
1144 bitmap->allclean = 0;
1145 }
be512691
N
1146 spin_lock_irqsave(&bitmap->lock, flags);
1147 j |= (PAGE_BITS - 1);
aa3163f8
N
1148 continue;
1149 }
1150
32a7627c 1151 /* grab the new page, sync and release the old */
32a7627c 1152 if (lastpage != NULL) {
ec7a3197 1153 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
32a7627c
N
1154 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1155 spin_unlock_irqrestore(&bitmap->lock, flags);
4ad13663 1156 write_page(bitmap, lastpage, 0);
32a7627c
N
1157 } else {
1158 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1159 spin_unlock_irqrestore(&bitmap->lock, flags);
1160 }
32a7627c
N
1161 } else
1162 spin_unlock_irqrestore(&bitmap->lock, flags);
1163 lastpage = page;
a0da84f3
NB
1164
1165 /* We are possibly going to clear some bits, so make
1166 * sure that events_cleared is up-to-date.
1167 */
1168 if (bitmap->need_sync) {
1169 bitmap_super_t *sb;
1170 bitmap->need_sync = 0;
1171 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1172 sb->events_cleared =
1173 cpu_to_le64(bitmap->events_cleared);
1174 kunmap_atomic(sb, KM_USER0);
1175 write_page(bitmap, bitmap->sb_page, 1);
1176 }
32a7627c
N
1177 spin_lock_irqsave(&bitmap->lock, flags);
1178 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1179 }
db305e50
N
1180 bmc = bitmap_get_counter(bitmap,
1181 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1182 &blocks, 0);
32a7627c
N
1183 if (bmc) {
1184/*
1185 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1186*/
8311c29d
N
1187 if (*bmc)
1188 bitmap->allclean = 0;
1189
32a7627c
N
1190 if (*bmc == 2) {
1191 *bmc=1; /* maybe clear the bit next time */
1192 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1193 } else if (*bmc == 1) {
1194 /* we can clear the bit */
1195 *bmc = 0;
db305e50
N
1196 bitmap_count_page(bitmap,
1197 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
32a7627c
N
1198 -1);
1199
1200 /* clear the bit */
ea03aff9 1201 paddr = kmap_atomic(page, KM_USER0);
bd926c63 1202 if (bitmap->flags & BITMAP_HOSTENDIAN)
ea03aff9 1203 clear_bit(file_page_offset(j), paddr);
bd926c63 1204 else
ea03aff9
N
1205 ext2_clear_bit(file_page_offset(j), paddr);
1206 kunmap_atomic(paddr, KM_USER0);
32a7627c 1207 }
be512691
N
1208 } else
1209 j |= PAGE_COUNTER_MASK;
32a7627c 1210 }
be512691 1211 spin_unlock_irqrestore(&bitmap->lock, flags);
32a7627c
N
1212
1213 /* now sync the final page */
1214 if (lastpage != NULL) {
32a7627c 1215 spin_lock_irqsave(&bitmap->lock, flags);
ec7a3197 1216 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
32a7627c
N
1217 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1218 spin_unlock_irqrestore(&bitmap->lock, flags);
4ad13663 1219 write_page(bitmap, lastpage, 0);
32a7627c
N
1220 } else {
1221 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1222 spin_unlock_irqrestore(&bitmap->lock, flags);
1223 }
32a7627c
N
1224 }
1225
7be3dfec 1226 done:
8311c29d 1227 if (bitmap->allclean == 0)
42a04b50 1228 bitmap->mddev->thread->timeout =
1b04be96 1229 bitmap->mddev->bitmap_info.daemon_sleep;
c3d9714e 1230 mutex_unlock(&mddev->bitmap_info.mutex);
32a7627c
N
1231}
1232
32a7627c
N
1233static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1234 sector_t offset, int *blocks,
1235 int create)
ee305ace
N
1236__releases(bitmap->lock)
1237__acquires(bitmap->lock)
32a7627c
N
1238{
1239 /* If 'create', we might release the lock and reclaim it.
1240 * The lock must have been taken with interrupts enabled.
1241 * If !create, we don't release the lock.
1242 */
1243 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1244 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1245 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1246 sector_t csize;
1247
1248 if (bitmap_checkpage(bitmap, page, create) < 0) {
1249 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1250 *blocks = csize - (offset & (csize- 1));
1251 return NULL;
1252 }
1253 /* now locked ... */
1254
1255 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1256 /* should we use the first or second counter field
1257 * of the hijacked pointer? */
1258 int hi = (pageoff > PAGE_COUNTER_MASK);
1259 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1260 PAGE_COUNTER_SHIFT - 1);
1261 *blocks = csize - (offset & (csize- 1));
1262 return &((bitmap_counter_t *)
1263 &bitmap->bp[page].map)[hi];
1264 } else { /* page is allocated */
1265 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1266 *blocks = csize - (offset & (csize- 1));
1267 return (bitmap_counter_t *)
1268 &(bitmap->bp[page].map[pageoff]);
1269 }
1270}
1271
4b6d287f 1272int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
32a7627c
N
1273{
1274 if (!bitmap) return 0;
4b6d287f
N
1275
1276 if (behind) {
1277 atomic_inc(&bitmap->behind_writes);
1278 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1279 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1280 }
1281
32a7627c
N
1282 while (sectors) {
1283 int blocks;
1284 bitmap_counter_t *bmc;
1285
1286 spin_lock_irq(&bitmap->lock);
1287 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1288 if (!bmc) {
1289 spin_unlock_irq(&bitmap->lock);
1290 return 0;
1291 }
1292
da6e1a32
NB
1293 if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
1294 DEFINE_WAIT(__wait);
1295 /* note that it is safe to do the prepare_to_wait
1296 * after the test as long as we do it before dropping
1297 * the spinlock.
1298 */
1299 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1300 TASK_UNINTERRUPTIBLE);
1301 spin_unlock_irq(&bitmap->lock);
2ad8b1ef 1302 blk_unplug(bitmap->mddev->queue);
da6e1a32
NB
1303 schedule();
1304 finish_wait(&bitmap->overflow_wait, &__wait);
1305 continue;
1306 }
1307
32a7627c
N
1308 switch(*bmc) {
1309 case 0:
1310 bitmap_file_set_bit(bitmap, offset);
1311 bitmap_count_page(bitmap,offset, 1);
93769f58 1312 blk_plug_device_unlocked(bitmap->mddev->queue);
32a7627c
N
1313 /* fall through */
1314 case 1:
1315 *bmc = 2;
1316 }
da6e1a32 1317
32a7627c
N
1318 (*bmc)++;
1319
1320 spin_unlock_irq(&bitmap->lock);
1321
1322 offset += blocks;
1323 if (sectors > blocks)
1324 sectors -= blocks;
1325 else sectors = 0;
1326 }
8311c29d 1327 bitmap->allclean = 0;
32a7627c
N
1328 return 0;
1329}
1330
1331void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
4b6d287f 1332 int success, int behind)
32a7627c
N
1333{
1334 if (!bitmap) return;
4b6d287f
N
1335 if (behind) {
1336 atomic_dec(&bitmap->behind_writes);
1337 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1338 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1339 }
d0a4bb49
N
1340 if (bitmap->mddev->degraded)
1341 /* Never clear bits or update events_cleared when degraded */
1342 success = 0;
4b6d287f 1343
32a7627c
N
1344 while (sectors) {
1345 int blocks;
1346 unsigned long flags;
1347 bitmap_counter_t *bmc;
1348
1349 spin_lock_irqsave(&bitmap->lock, flags);
1350 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1351 if (!bmc) {
1352 spin_unlock_irqrestore(&bitmap->lock, flags);
1353 return;
1354 }
1355
a0da84f3
NB
1356 if (success &&
1357 bitmap->events_cleared < bitmap->mddev->events) {
1358 bitmap->events_cleared = bitmap->mddev->events;
1359 bitmap->need_sync = 1;
1360 }
1361
32a7627c
N
1362 if (!success && ! (*bmc & NEEDED_MASK))
1363 *bmc |= NEEDED_MASK;
1364
da6e1a32
NB
1365 if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
1366 wake_up(&bitmap->overflow_wait);
1367
32a7627c
N
1368 (*bmc)--;
1369 if (*bmc <= 2) {
1370 set_page_attr(bitmap,
1371 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1372 BITMAP_PAGE_CLEAN);
1373 }
1374 spin_unlock_irqrestore(&bitmap->lock, flags);
1375 offset += blocks;
1376 if (sectors > blocks)
1377 sectors -= blocks;
1378 else sectors = 0;
1379 }
1380}
1381
1187cf0a
N
1382static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1383 int degraded)
32a7627c
N
1384{
1385 bitmap_counter_t *bmc;
1386 int rv;
1387 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1388 *blocks = 1024;
1389 return 1; /* always resync if no bitmap */
1390 }
1391 spin_lock_irq(&bitmap->lock);
1392 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1393 rv = 0;
1394 if (bmc) {
1395 /* locked */
1396 if (RESYNC(*bmc))
1397 rv = 1;
1398 else if (NEEDED(*bmc)) {
1399 rv = 1;
6a806c51
N
1400 if (!degraded) { /* don't set/clear bits if degraded */
1401 *bmc |= RESYNC_MASK;
1402 *bmc &= ~NEEDED_MASK;
1403 }
32a7627c
N
1404 }
1405 }
1406 spin_unlock_irq(&bitmap->lock);
8311c29d 1407 bitmap->allclean = 0;
32a7627c
N
1408 return rv;
1409}
1410
1187cf0a
N
1411int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1412 int degraded)
1413{
1414 /* bitmap_start_sync must always report on multiples of whole
1415 * pages, otherwise resync (which is very PAGE_SIZE based) will
1416 * get confused.
1417 * So call __bitmap_start_sync repeatedly (if needed) until
1418 * At least PAGE_SIZE>>9 blocks are covered.
1419 * Return the 'or' of the result.
1420 */
1421 int rv = 0;
1422 int blocks1;
1423
1424 *blocks = 0;
1425 while (*blocks < (PAGE_SIZE>>9)) {
1426 rv |= __bitmap_start_sync(bitmap, offset,
1427 &blocks1, degraded);
1428 offset += blocks1;
1429 *blocks += blocks1;
1430 }
1431 return rv;
1432}
1433
32a7627c
N
1434void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1435{
1436 bitmap_counter_t *bmc;
1437 unsigned long flags;
1438/*
1439 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1440*/ if (bitmap == NULL) {
1441 *blocks = 1024;
1442 return;
1443 }
1444 spin_lock_irqsave(&bitmap->lock, flags);
1445 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1446 if (bmc == NULL)
1447 goto unlock;
1448 /* locked */
1449/*
1450 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1451*/
1452 if (RESYNC(*bmc)) {
1453 *bmc &= ~RESYNC_MASK;
1454
1455 if (!NEEDED(*bmc) && aborted)
1456 *bmc |= NEEDED_MASK;
1457 else {
1458 if (*bmc <= 2) {
1459 set_page_attr(bitmap,
1460 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1461 BITMAP_PAGE_CLEAN);
1462 }
1463 }
1464 }
1465 unlock:
1466 spin_unlock_irqrestore(&bitmap->lock, flags);
8311c29d 1467 bitmap->allclean = 0;
32a7627c
N
1468}
1469
1470void bitmap_close_sync(struct bitmap *bitmap)
1471{
1472 /* Sync has finished, and any bitmap chunks that weren't synced
1473 * properly have been aborted. It remains to us to clear the
1474 * RESYNC bit wherever it is still on
1475 */
1476 sector_t sector = 0;
1477 int blocks;
b47490c9
N
1478 if (!bitmap)
1479 return;
32a7627c
N
1480 while (sector < bitmap->mddev->resync_max_sectors) {
1481 bitmap_end_sync(bitmap, sector, &blocks, 0);
b47490c9
N
1482 sector += blocks;
1483 }
1484}
1485
1486void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1487{
1488 sector_t s = 0;
1489 int blocks;
1490
1491 if (!bitmap)
1492 return;
1493 if (sector == 0) {
1494 bitmap->last_end_sync = jiffies;
1495 return;
1496 }
1497 if (time_before(jiffies, (bitmap->last_end_sync
1b04be96 1498 + bitmap->mddev->bitmap_info.daemon_sleep)))
b47490c9
N
1499 return;
1500 wait_event(bitmap->mddev->recovery_wait,
1501 atomic_read(&bitmap->mddev->recovery_active) == 0);
1502
97e4f42d
N
1503 bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync;
1504 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
b47490c9
N
1505 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1506 s = 0;
1507 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1508 bitmap_end_sync(bitmap, s, &blocks, 0);
1509 s += blocks;
32a7627c 1510 }
b47490c9 1511 bitmap->last_end_sync = jiffies;
acb180b0 1512 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
32a7627c
N
1513}
1514
6a07997f 1515static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
32a7627c
N
1516{
1517 /* For each chunk covered by any of these sectors, set the
193f1c93 1518 * counter to 1 and set resync_needed. They should all
32a7627c
N
1519 * be 0 at this point
1520 */
193f1c93
N
1521
1522 int secs;
1523 bitmap_counter_t *bmc;
1524 spin_lock_irq(&bitmap->lock);
1525 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1526 if (!bmc) {
32a7627c 1527 spin_unlock_irq(&bitmap->lock);
193f1c93 1528 return;
32a7627c 1529 }
193f1c93
N
1530 if (! *bmc) {
1531 struct page *page;
6a07997f 1532 *bmc = 1 | (needed?NEEDED_MASK:0);
193f1c93
N
1533 bitmap_count_page(bitmap, offset, 1);
1534 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1535 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1536 }
1537 spin_unlock_irq(&bitmap->lock);
8311c29d 1538 bitmap->allclean = 0;
32a7627c
N
1539}
1540
9b1d1dac
PC
1541/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1542void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1543{
1544 unsigned long chunk;
1545
1546 for (chunk = s; chunk <= e; chunk++) {
db305e50 1547 sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
9b1d1dac
PC
1548 bitmap_set_memory_bits(bitmap, sec, 1);
1549 bitmap_file_set_bit(bitmap, sec);
1550 }
1551}
1552
6b8b3e8a
N
1553/*
1554 * flush out any pending updates
1555 */
1556void bitmap_flush(mddev_t *mddev)
1557{
1558 struct bitmap *bitmap = mddev->bitmap;
42a04b50 1559 long sleep;
6b8b3e8a
N
1560
1561 if (!bitmap) /* there was no bitmap */
1562 return;
1563
1564 /* run the daemon_work three time to ensure everything is flushed
1565 * that can be
1566 */
1b04be96 1567 sleep = mddev->bitmap_info.daemon_sleep * 2;
42a04b50 1568 bitmap->daemon_lastrun -= sleep;
aa5cbd10 1569 bitmap_daemon_work(mddev);
42a04b50 1570 bitmap->daemon_lastrun -= sleep;
aa5cbd10 1571 bitmap_daemon_work(mddev);
42a04b50 1572 bitmap->daemon_lastrun -= sleep;
aa5cbd10 1573 bitmap_daemon_work(mddev);
6b8b3e8a
N
1574 bitmap_update_sb(bitmap);
1575}
1576
32a7627c
N
1577/*
1578 * free memory that was allocated
1579 */
3178b0db 1580static void bitmap_free(struct bitmap *bitmap)
32a7627c
N
1581{
1582 unsigned long k, pages;
1583 struct bitmap_page *bp;
32a7627c
N
1584
1585 if (!bitmap) /* there was no bitmap */
1586 return;
1587
32a7627c
N
1588 /* release the bitmap file and kill the daemon */
1589 bitmap_file_put(bitmap);
1590
1591 bp = bitmap->bp;
1592 pages = bitmap->pages;
1593
1594 /* free all allocated memory */
1595
32a7627c
N
1596 if (bp) /* deallocate the page memory */
1597 for (k = 0; k < pages; k++)
1598 if (bp[k].map && !bp[k].hijacked)
1599 kfree(bp[k].map);
1600 kfree(bp);
1601 kfree(bitmap);
1602}
aa5cbd10 1603
3178b0db
N
1604void bitmap_destroy(mddev_t *mddev)
1605{
1606 struct bitmap *bitmap = mddev->bitmap;
1607
1608 if (!bitmap) /* there was no bitmap */
1609 return;
1610
c3d9714e 1611 mutex_lock(&mddev->bitmap_info.mutex);
3178b0db 1612 mddev->bitmap = NULL; /* disconnect from the md device */
c3d9714e 1613 mutex_unlock(&mddev->bitmap_info.mutex);
b15c2e57
N
1614 if (mddev->thread)
1615 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
3178b0db
N
1616
1617 bitmap_free(bitmap);
1618}
32a7627c
N
1619
1620/*
1621 * initialize the bitmap structure
1622 * if this returns an error, bitmap_destroy must be called to do clean up
1623 */
1624int bitmap_create(mddev_t *mddev)
1625{
1626 struct bitmap *bitmap;
1f593903 1627 sector_t blocks = mddev->resync_max_sectors;
32a7627c
N
1628 unsigned long chunks;
1629 unsigned long pages;
c3d9714e 1630 struct file *file = mddev->bitmap_info.file;
32a7627c 1631 int err;
6a07997f 1632 sector_t start;
32a7627c 1633
5f6e3c83 1634 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
32a7627c 1635
c3d9714e 1636 if (!file && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
32a7627c
N
1637 return 0;
1638
c3d9714e 1639 BUG_ON(file && mddev->bitmap_info.offset);
a654b9d8 1640
9ffae0cf 1641 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
32a7627c
N
1642 if (!bitmap)
1643 return -ENOMEM;
1644
32a7627c 1645 spin_lock_init(&bitmap->lock);
ce25c31b
N
1646 atomic_set(&bitmap->pending_writes, 0);
1647 init_waitqueue_head(&bitmap->write_wait);
da6e1a32 1648 init_waitqueue_head(&bitmap->overflow_wait);
ce25c31b 1649
32a7627c 1650 bitmap->mddev = mddev;
32a7627c 1651
32a7627c 1652 bitmap->file = file;
ce25c31b
N
1653 if (file) {
1654 get_file(file);
ae8fa283
N
1655 /* As future accesses to this file will use bmap,
1656 * and bypass the page cache, we must sync the file
1657 * first.
1658 */
1659 vfs_fsync(file, file->f_dentry, 1);
ce25c31b 1660 }
42a04b50 1661 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
32a7627c
N
1662 err = bitmap_read_sb(bitmap);
1663 if (err)
3178b0db 1664 goto error;
32a7627c 1665
42a04b50 1666 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
32a7627c
N
1667
1668 /* now that chunksize and chunkshift are set, we can use these macros */
1f593903
N
1669 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
1670 CHUNK_BLOCK_SHIFT(bitmap);
32a7627c
N
1671 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1672
1673 BUG_ON(!pages);
1674
1675 bitmap->chunks = chunks;
1676 bitmap->pages = pages;
1677 bitmap->missing_pages = pages;
1678 bitmap->counter_bits = COUNTER_BITS;
1679
1680 bitmap->syncchunk = ~0UL;
1681
44456d37 1682#ifdef INJECT_FATAL_FAULT_1
32a7627c
N
1683 bitmap->bp = NULL;
1684#else
9ffae0cf 1685 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
32a7627c 1686#endif
3178b0db 1687 err = -ENOMEM;
32a7627c 1688 if (!bitmap->bp)
3178b0db 1689 goto error;
32a7627c 1690
32a7627c
N
1691 /* now that we have some pages available, initialize the in-memory
1692 * bitmap from the on-disk bitmap */
6a07997f
N
1693 start = 0;
1694 if (mddev->degraded == 0
1695 || bitmap->events_cleared == mddev->events)
1696 /* no need to keep dirty bits to optimise a re-add of a missing device */
1697 start = mddev->recovery_cp;
1698 err = bitmap_init_from_disk(bitmap, start);
193f1c93 1699
32a7627c 1700 if (err)
3178b0db 1701 goto error;
32a7627c
N
1702
1703 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1704 pages, bmname(bitmap));
1705
3178b0db
N
1706 mddev->bitmap = bitmap;
1707
1b04be96 1708 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
9cd30fdc 1709 md_wakeup_thread(mddev->thread);
b15c2e57 1710
4ad13663
N
1711 bitmap_update_sb(bitmap);
1712
1713 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
3178b0db
N
1714
1715 error:
1716 bitmap_free(bitmap);
1717 return err;
32a7627c
N
1718}
1719
43a70507
N
1720static ssize_t
1721location_show(mddev_t *mddev, char *page)
1722{
1723 ssize_t len;
1724 if (mddev->bitmap_info.file) {
1725 len = sprintf(page, "file");
1726 } else if (mddev->bitmap_info.offset) {
1727 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
1728 } else
1729 len = sprintf(page, "none");
1730 len += sprintf(page+len, "\n");
1731 return len;
1732}
1733
1734static ssize_t
1735location_store(mddev_t *mddev, const char *buf, size_t len)
1736{
1737
1738 if (mddev->pers) {
1739 if (!mddev->pers->quiesce)
1740 return -EBUSY;
1741 if (mddev->recovery || mddev->sync_thread)
1742 return -EBUSY;
1743 }
1744
1745 if (mddev->bitmap || mddev->bitmap_info.file ||
1746 mddev->bitmap_info.offset) {
1747 /* bitmap already configured. Only option is to clear it */
1748 if (strncmp(buf, "none", 4) != 0)
1749 return -EBUSY;
1750 if (mddev->pers) {
1751 mddev->pers->quiesce(mddev, 1);
1752 bitmap_destroy(mddev);
1753 mddev->pers->quiesce(mddev, 0);
1754 }
1755 mddev->bitmap_info.offset = 0;
1756 if (mddev->bitmap_info.file) {
1757 struct file *f = mddev->bitmap_info.file;
1758 mddev->bitmap_info.file = NULL;
1759 restore_bitmap_write_access(f);
1760 fput(f);
1761 }
1762 } else {
1763 /* No bitmap, OK to set a location */
1764 long long offset;
1765 if (strncmp(buf, "none", 4) == 0)
1766 /* nothing to be done */;
1767 else if (strncmp(buf, "file:", 5) == 0) {
1768 /* Not supported yet */
1769 return -EINVAL;
1770 } else {
1771 int rv;
1772 if (buf[0] == '+')
1773 rv = strict_strtoll(buf+1, 10, &offset);
1774 else
1775 rv = strict_strtoll(buf, 10, &offset);
1776 if (rv)
1777 return rv;
1778 if (offset == 0)
1779 return -EINVAL;
1780 if (mddev->major_version == 0 &&
1781 offset != mddev->bitmap_info.default_offset)
1782 return -EINVAL;
1783 mddev->bitmap_info.offset = offset;
1784 if (mddev->pers) {
1785 mddev->pers->quiesce(mddev, 1);
1786 rv = bitmap_create(mddev);
1787 if (rv) {
1788 bitmap_destroy(mddev);
1789 mddev->bitmap_info.offset = 0;
1790 }
1791 mddev->pers->quiesce(mddev, 0);
1792 if (rv)
1793 return rv;
1794 }
1795 }
1796 }
1797 if (!mddev->external) {
1798 /* Ensure new bitmap info is stored in
1799 * metadata promptly.
1800 */
1801 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1802 md_wakeup_thread(mddev->thread);
1803 }
1804 return len;
1805}
1806
1807static struct md_sysfs_entry bitmap_location =
1808__ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
1809
1810static ssize_t
1811timeout_show(mddev_t *mddev, char *page)
1812{
1813 ssize_t len;
1814 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
1815 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
1816
1817 len = sprintf(page, "%lu", secs);
1818 if (jifs)
1819 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
1820 len += sprintf(page+len, "\n");
1821 return len;
1822}
1823
1824static ssize_t
1825timeout_store(mddev_t *mddev, const char *buf, size_t len)
1826{
1827 /* timeout can be set at any time */
1828 unsigned long timeout;
1829 int rv = strict_strtoul_scaled(buf, &timeout, 4);
1830 if (rv)
1831 return rv;
1832
1833 /* just to make sure we don't overflow... */
1834 if (timeout >= LONG_MAX / HZ)
1835 return -EINVAL;
1836
1837 timeout = timeout * HZ / 10000;
1838
1839 if (timeout >= MAX_SCHEDULE_TIMEOUT)
1840 timeout = MAX_SCHEDULE_TIMEOUT-1;
1841 if (timeout < 1)
1842 timeout = 1;
1843 mddev->bitmap_info.daemon_sleep = timeout;
1844 if (mddev->thread) {
1845 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1846 * the bitmap is all clean and we don't need to
1847 * adjust the timeout right now
1848 */
1849 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
1850 mddev->thread->timeout = timeout;
1851 md_wakeup_thread(mddev->thread);
1852 }
1853 }
1854 return len;
1855}
1856
1857static struct md_sysfs_entry bitmap_timeout =
1858__ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
1859
1860static ssize_t
1861backlog_show(mddev_t *mddev, char *page)
1862{
1863 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
1864}
1865
1866static ssize_t
1867backlog_store(mddev_t *mddev, const char *buf, size_t len)
1868{
1869 unsigned long backlog;
1870 int rv = strict_strtoul(buf, 10, &backlog);
1871 if (rv)
1872 return rv;
1873 if (backlog > COUNTER_MAX)
1874 return -EINVAL;
1875 mddev->bitmap_info.max_write_behind = backlog;
1876 return len;
1877}
1878
1879static struct md_sysfs_entry bitmap_backlog =
1880__ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
1881
1882static ssize_t
1883chunksize_show(mddev_t *mddev, char *page)
1884{
1885 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
1886}
1887
1888static ssize_t
1889chunksize_store(mddev_t *mddev, const char *buf, size_t len)
1890{
1891 /* Can only be changed when no bitmap is active */
1892 int rv;
1893 unsigned long csize;
1894 if (mddev->bitmap)
1895 return -EBUSY;
1896 rv = strict_strtoul(buf, 10, &csize);
1897 if (rv)
1898 return rv;
1899 if (csize < 512 ||
1900 !is_power_of_2(csize))
1901 return -EINVAL;
1902 mddev->bitmap_info.chunksize = csize;
1903 return len;
1904}
1905
1906static struct md_sysfs_entry bitmap_chunksize =
1907__ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
1908
1909static struct attribute *md_bitmap_attrs[] = {
1910 &bitmap_location.attr,
1911 &bitmap_timeout.attr,
1912 &bitmap_backlog.attr,
1913 &bitmap_chunksize.attr,
1914 NULL
1915};
1916struct attribute_group md_bitmap_group = {
1917 .name = "bitmap",
1918 .attrs = md_bitmap_attrs,
1919};
1920
1921
32a7627c
N
1922/* the bitmap API -- for raid personalities */
1923EXPORT_SYMBOL(bitmap_startwrite);
1924EXPORT_SYMBOL(bitmap_endwrite);
1925EXPORT_SYMBOL(bitmap_start_sync);
1926EXPORT_SYMBOL(bitmap_end_sync);
1927EXPORT_SYMBOL(bitmap_unplug);
1928EXPORT_SYMBOL(bitmap_close_sync);
b47490c9 1929EXPORT_SYMBOL(bitmap_cond_end_sync);