Return MD_SB_CLUSTERED if mddev is clustered
[linux-2.6-block.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).
32a7627c
N
16 */
17
bff61975 18#include <linux/blkdev.h>
32a7627c 19#include <linux/module.h>
32a7627c
N
20#include <linux/errno.h>
21#include <linux/slab.h>
22#include <linux/init.h>
32a7627c
N
23#include <linux/timer.h>
24#include <linux/sched.h>
25#include <linux/list.h>
26#include <linux/file.h>
27#include <linux/mount.h>
28#include <linux/buffer_head.h>
57148964 29#include <linux/seq_file.h>
43b2e5d8 30#include "md.h"
ef740c37 31#include "bitmap.h"
32a7627c 32
ac2f40be 33static inline char *bmname(struct bitmap *bitmap)
32a7627c
N
34{
35 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
36}
37
32a7627c
N
38/*
39 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
40 *
41 * 1) check to see if this page is allocated, if it's not then try to alloc
42 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
43 * page pointer directly as a counter
44 *
45 * if we find our page, we increment the page's refcount so that it stays
46 * allocated while we're using it
47 */
40cffcc0 48static int bitmap_checkpage(struct bitmap_counts *bitmap,
ac2f40be 49 unsigned long page, int create)
ee305ace
N
50__releases(bitmap->lock)
51__acquires(bitmap->lock)
32a7627c
N
52{
53 unsigned char *mappage;
54
55 if (page >= bitmap->pages) {
1187cf0a
N
56 /* This can happen if bitmap_start_sync goes beyond
57 * End-of-device while looking for a whole page.
58 * It is harmless.
59 */
32a7627c
N
60 return -EINVAL;
61 }
62
32a7627c
N
63 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
64 return 0;
65
66 if (bitmap->bp[page].map) /* page is already allocated, just return */
67 return 0;
68
69 if (!create)
70 return -ENOENT;
71
32a7627c
N
72 /* this page has not been allocated yet */
73
ac2f40be 74 spin_unlock_irq(&bitmap->lock);
d9590143
N
75 /* It is possible that this is being called inside a
76 * prepare_to_wait/finish_wait loop from raid5c:make_request().
77 * In general it is not permitted to sleep in that context as it
78 * can cause the loop to spin freely.
79 * That doesn't apply here as we can only reach this point
80 * once with any loop.
81 * When this function completes, either bp[page].map or
82 * bp[page].hijacked. In either case, this function will
83 * abort before getting to this point again. So there is
84 * no risk of a free-spin, and so it is safe to assert
85 * that sleeping here is allowed.
86 */
87 sched_annotate_sleep();
792a1d4b 88 mappage = kzalloc(PAGE_SIZE, GFP_NOIO);
ac2f40be
N
89 spin_lock_irq(&bitmap->lock);
90
91 if (mappage == NULL) {
40cffcc0 92 pr_debug("md/bitmap: map page allocation failed, hijacking\n");
32a7627c
N
93 /* failed - set the hijacked flag so that we can use the
94 * pointer as a counter */
32a7627c
N
95 if (!bitmap->bp[page].map)
96 bitmap->bp[page].hijacked = 1;
ac2f40be
N
97 } else if (bitmap->bp[page].map ||
98 bitmap->bp[page].hijacked) {
32a7627c 99 /* somebody beat us to getting the page */
792a1d4b 100 kfree(mappage);
32a7627c 101 return 0;
ac2f40be 102 } else {
32a7627c 103
ac2f40be 104 /* no page was in place and we have one, so install it */
32a7627c 105
ac2f40be
N
106 bitmap->bp[page].map = mappage;
107 bitmap->missing_pages--;
108 }
32a7627c
N
109 return 0;
110}
111
32a7627c
N
112/* if page is completely empty, put it back on the free list, or dealloc it */
113/* if page was hijacked, unmark the flag so it might get alloced next time */
114/* Note: lock should be held when calling this */
40cffcc0 115static void bitmap_checkfree(struct bitmap_counts *bitmap, unsigned long page)
32a7627c
N
116{
117 char *ptr;
118
119 if (bitmap->bp[page].count) /* page is still busy */
120 return;
121
122 /* page is no longer in use, it can be released */
123
124 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
125 bitmap->bp[page].hijacked = 0;
126 bitmap->bp[page].map = NULL;
ac2f40be
N
127 } else {
128 /* normal case, free the page */
129 ptr = bitmap->bp[page].map;
130 bitmap->bp[page].map = NULL;
131 bitmap->missing_pages++;
792a1d4b 132 kfree(ptr);
32a7627c 133 }
32a7627c
N
134}
135
32a7627c
N
136/*
137 * bitmap file handling - read and write the bitmap file and its superblock
138 */
139
32a7627c
N
140/*
141 * basic page I/O operations
142 */
143
a654b9d8 144/* IO operations when bitmap is stored near all superblocks */
27581e5a
N
145static int read_sb_page(struct mddev *mddev, loff_t offset,
146 struct page *page,
147 unsigned long index, int size)
a654b9d8
N
148{
149 /* choose a good rdev and read the page from there */
150
3cb03002 151 struct md_rdev *rdev;
a654b9d8 152 sector_t target;
a654b9d8 153
dafb20fa 154 rdev_for_each(rdev, mddev) {
b2d444d7
N
155 if (! test_bit(In_sync, &rdev->flags)
156 || test_bit(Faulty, &rdev->flags))
ab904d63
N
157 continue;
158
ccebd4c4 159 target = offset + index * (PAGE_SIZE/512);
a654b9d8 160
2b193363 161 if (sync_page_io(rdev, target,
e1defc4f 162 roundup(size, bdev_logical_block_size(rdev->bdev)),
ccebd4c4 163 page, READ, true)) {
ab904d63 164 page->index = index;
27581e5a 165 return 0;
ab904d63
N
166 }
167 }
27581e5a 168 return -EIO;
a654b9d8
N
169}
170
fd01b88c 171static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev)
b2d2c4ce
N
172{
173 /* Iterate the disks of an mddev, using rcu to protect access to the
174 * linked list, and raising the refcount of devices we return to ensure
175 * they don't disappear while in use.
176 * As devices are only added or removed when raid_disk is < 0 and
177 * nr_pending is 0 and In_sync is clear, the entries we return will
178 * still be in the same position on the list when we re-enter
fd177481 179 * list_for_each_entry_continue_rcu.
b2d2c4ce 180 */
b2d2c4ce
N
181 rcu_read_lock();
182 if (rdev == NULL)
183 /* start at the beginning */
fd177481 184 rdev = list_entry_rcu(&mddev->disks, struct md_rdev, same_set);
b2d2c4ce
N
185 else {
186 /* release the previous rdev and start from there. */
187 rdev_dec_pending(rdev, mddev);
b2d2c4ce 188 }
fd177481 189 list_for_each_entry_continue_rcu(rdev, &mddev->disks, same_set) {
b2d2c4ce 190 if (rdev->raid_disk >= 0 &&
b2d2c4ce
N
191 !test_bit(Faulty, &rdev->flags)) {
192 /* this is a usable devices */
193 atomic_inc(&rdev->nr_pending);
194 rcu_read_unlock();
195 return rdev;
196 }
197 }
198 rcu_read_unlock();
199 return NULL;
200}
201
ab6085c7 202static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
a654b9d8 203{
3cb03002 204 struct md_rdev *rdev = NULL;
a6ff7e08 205 struct block_device *bdev;
fd01b88c 206 struct mddev *mddev = bitmap->mddev;
1ec885cd 207 struct bitmap_storage *store = &bitmap->storage;
a654b9d8 208
b2d2c4ce 209 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
ac2f40be
N
210 int size = PAGE_SIZE;
211 loff_t offset = mddev->bitmap_info.offset;
a6ff7e08
JB
212
213 bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
214
9b1215c1
N
215 if (page->index == store->file_pages-1) {
216 int last_page_size = store->bytes & (PAGE_SIZE-1);
217 if (last_page_size == 0)
218 last_page_size = PAGE_SIZE;
219 size = roundup(last_page_size,
a6ff7e08 220 bdev_logical_block_size(bdev));
9b1215c1 221 }
ac2f40be
N
222 /* Just make sure we aren't corrupting data or
223 * metadata
224 */
225 if (mddev->external) {
226 /* Bitmap could be anywhere. */
227 if (rdev->sb_start + offset + (page->index
228 * (PAGE_SIZE/512))
229 > rdev->data_offset
230 &&
231 rdev->sb_start + offset
232 < (rdev->data_offset + mddev->dev_sectors
233 + (PAGE_SIZE/512)))
234 goto bad_alignment;
235 } else if (offset < 0) {
236 /* DATA BITMAP METADATA */
237 if (offset
238 + (long)(page->index * (PAGE_SIZE/512))
239 + size/512 > 0)
240 /* bitmap runs in to metadata */
241 goto bad_alignment;
242 if (rdev->data_offset + mddev->dev_sectors
243 > rdev->sb_start + offset)
244 /* data runs in to bitmap */
245 goto bad_alignment;
246 } else if (rdev->sb_start < rdev->data_offset) {
247 /* METADATA BITMAP DATA */
248 if (rdev->sb_start
249 + offset
250 + page->index*(PAGE_SIZE/512) + size/512
251 > rdev->data_offset)
252 /* bitmap runs in to data */
253 goto bad_alignment;
254 } else {
255 /* DATA METADATA BITMAP - no problems */
256 }
257 md_super_write(mddev, rdev,
258 rdev->sb_start + offset
259 + page->index * (PAGE_SIZE/512),
260 size,
261 page);
b2d2c4ce 262 }
a654b9d8
N
263
264 if (wait)
a9701a30 265 md_super_wait(mddev);
a654b9d8 266 return 0;
4b80991c
N
267
268 bad_alignment:
4b80991c 269 return -EINVAL;
a654b9d8
N
270}
271
4ad13663 272static void bitmap_file_kick(struct bitmap *bitmap);
32a7627c 273/*
a654b9d8 274 * write out a page to a file
32a7627c 275 */
4ad13663 276static void write_page(struct bitmap *bitmap, struct page *page, int wait)
32a7627c 277{
d785a06a 278 struct buffer_head *bh;
32a7627c 279
1ec885cd 280 if (bitmap->storage.file == NULL) {
f0d76d70
N
281 switch (write_sb_page(bitmap, page, wait)) {
282 case -EINVAL:
b405fe91 283 set_bit(BITMAP_WRITE_ERROR, &bitmap->flags);
f0d76d70 284 }
4ad13663 285 } else {
a654b9d8 286
4ad13663 287 bh = page_buffers(page);
c708443c 288
4ad13663
N
289 while (bh && bh->b_blocknr) {
290 atomic_inc(&bitmap->pending_writes);
291 set_buffer_locked(bh);
292 set_buffer_mapped(bh);
721a9602 293 submit_bh(WRITE | REQ_SYNC, bh);
4ad13663
N
294 bh = bh->b_this_page;
295 }
d785a06a 296
ac2f40be 297 if (wait)
4ad13663
N
298 wait_event(bitmap->write_wait,
299 atomic_read(&bitmap->pending_writes)==0);
8a5e9cf1 300 }
b405fe91 301 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
4ad13663 302 bitmap_file_kick(bitmap);
d785a06a
N
303}
304
305static void end_bitmap_write(struct buffer_head *bh, int uptodate)
306{
307 struct bitmap *bitmap = bh->b_private;
32a7627c 308
b405fe91
N
309 if (!uptodate)
310 set_bit(BITMAP_WRITE_ERROR, &bitmap->flags);
d785a06a
N
311 if (atomic_dec_and_test(&bitmap->pending_writes))
312 wake_up(&bitmap->write_wait);
313}
32a7627c 314
d785a06a
N
315/* copied from buffer.c */
316static void
317__clear_page_buffers(struct page *page)
318{
319 ClearPagePrivate(page);
320 set_page_private(page, 0);
321 page_cache_release(page);
322}
323static void free_buffers(struct page *page)
324{
27581e5a 325 struct buffer_head *bh;
77ad4bc7 326
27581e5a
N
327 if (!PagePrivate(page))
328 return;
329
330 bh = page_buffers(page);
d785a06a
N
331 while (bh) {
332 struct buffer_head *next = bh->b_this_page;
333 free_buffer_head(bh);
334 bh = next;
77ad4bc7 335 }
d785a06a
N
336 __clear_page_buffers(page);
337 put_page(page);
32a7627c
N
338}
339
d785a06a
N
340/* read a page from a file.
341 * We both read the page, and attach buffers to the page to record the
342 * address of each block (using bmap). These addresses will be used
343 * to write the block later, completely bypassing the filesystem.
344 * This usage is similar to how swap files are handled, and allows us
345 * to write to a file with no concerns of memory allocation failing.
346 */
27581e5a
N
347static int read_page(struct file *file, unsigned long index,
348 struct bitmap *bitmap,
349 unsigned long count,
350 struct page *page)
32a7627c 351{
27581e5a 352 int ret = 0;
496ad9aa 353 struct inode *inode = file_inode(file);
d785a06a
N
354 struct buffer_head *bh;
355 sector_t block;
32a7627c 356
36a4e1fe
N
357 pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
358 (unsigned long long)index << PAGE_SHIFT);
32a7627c 359
d785a06a
N
360 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
361 if (!bh) {
27581e5a 362 ret = -ENOMEM;
32a7627c
N
363 goto out;
364 }
d785a06a
N
365 attach_page_buffers(page, bh);
366 block = index << (PAGE_SHIFT - inode->i_blkbits);
367 while (bh) {
368 if (count == 0)
369 bh->b_blocknr = 0;
370 else {
371 bh->b_blocknr = bmap(inode, block);
372 if (bh->b_blocknr == 0) {
373 /* Cannot use this file! */
27581e5a 374 ret = -EINVAL;
d785a06a
N
375 goto out;
376 }
377 bh->b_bdev = inode->i_sb->s_bdev;
378 if (count < (1<<inode->i_blkbits))
379 count = 0;
380 else
381 count -= (1<<inode->i_blkbits);
382
383 bh->b_end_io = end_bitmap_write;
384 bh->b_private = bitmap;
ce25c31b
N
385 atomic_inc(&bitmap->pending_writes);
386 set_buffer_locked(bh);
387 set_buffer_mapped(bh);
388 submit_bh(READ, bh);
d785a06a
N
389 }
390 block++;
391 bh = bh->b_this_page;
392 }
d785a06a 393 page->index = index;
ce25c31b
N
394
395 wait_event(bitmap->write_wait,
396 atomic_read(&bitmap->pending_writes)==0);
b405fe91 397 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
27581e5a 398 ret = -EIO;
32a7627c 399out:
27581e5a
N
400 if (ret)
401 printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %d\n",
2d1f3b5d
N
402 (int)PAGE_SIZE,
403 (unsigned long long)index << PAGE_SHIFT,
27581e5a
N
404 ret);
405 return ret;
32a7627c
N
406}
407
408/*
409 * bitmap file superblock operations
410 */
411
412/* update the event counter and sync the superblock to disk */
4ad13663 413void bitmap_update_sb(struct bitmap *bitmap)
32a7627c
N
414{
415 bitmap_super_t *sb;
32a7627c
N
416
417 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
4ad13663 418 return;
ece5cff0
N
419 if (bitmap->mddev->bitmap_info.external)
420 return;
1ec885cd 421 if (!bitmap->storage.sb_page) /* no superblock */
4ad13663 422 return;
1ec885cd 423 sb = kmap_atomic(bitmap->storage.sb_page);
32a7627c 424 sb->events = cpu_to_le64(bitmap->mddev->events);
8258c532 425 if (bitmap->mddev->events < bitmap->events_cleared)
a0da84f3
NB
426 /* rocking back to read-only */
427 bitmap->events_cleared = bitmap->mddev->events;
8258c532
N
428 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
429 sb->state = cpu_to_le32(bitmap->flags);
43a70507
N
430 /* Just in case these have been changed via sysfs: */
431 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
432 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
b81a0404
N
433 /* This might have been changed by a reshape */
434 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
435 sb->chunksize = cpu_to_le32(bitmap->mddev->bitmap_info.chunksize);
c4ce867f 436 sb->nodes = cpu_to_le32(bitmap->mddev->bitmap_info.nodes);
1dff2b87
N
437 sb->sectors_reserved = cpu_to_le32(bitmap->mddev->
438 bitmap_info.space);
b2f46e68 439 kunmap_atomic(sb);
1ec885cd 440 write_page(bitmap, bitmap->storage.sb_page, 1);
32a7627c
N
441}
442
443/* print out the bitmap file superblock */
444void bitmap_print_sb(struct bitmap *bitmap)
445{
446 bitmap_super_t *sb;
447
1ec885cd 448 if (!bitmap || !bitmap->storage.sb_page)
32a7627c 449 return;
1ec885cd 450 sb = kmap_atomic(bitmap->storage.sb_page);
32a7627c 451 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
a2cff26a
N
452 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
453 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
454 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
32a7627c
N
455 *(__u32 *)(sb->uuid+0),
456 *(__u32 *)(sb->uuid+4),
457 *(__u32 *)(sb->uuid+8),
458 *(__u32 *)(sb->uuid+12));
a2cff26a 459 printk(KERN_DEBUG " events: %llu\n",
32a7627c 460 (unsigned long long) le64_to_cpu(sb->events));
a2cff26a 461 printk(KERN_DEBUG "events cleared: %llu\n",
32a7627c 462 (unsigned long long) le64_to_cpu(sb->events_cleared));
a2cff26a
N
463 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
464 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
465 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
466 printk(KERN_DEBUG " sync size: %llu KB\n",
467 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
4b6d287f 468 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
b2f46e68 469 kunmap_atomic(sb);
32a7627c
N
470}
471
9c81075f
JB
472/*
473 * bitmap_new_disk_sb
474 * @bitmap
475 *
476 * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
477 * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
478 * This function verifies 'bitmap_info' and populates the on-disk bitmap
479 * structure, which is to be written to disk.
480 *
481 * Returns: 0 on success, -Exxx on error
482 */
483static int bitmap_new_disk_sb(struct bitmap *bitmap)
484{
485 bitmap_super_t *sb;
486 unsigned long chunksize, daemon_sleep, write_behind;
9c81075f 487
1ec885cd 488 bitmap->storage.sb_page = alloc_page(GFP_KERNEL);
582e2e05
JM
489 if (bitmap->storage.sb_page == NULL)
490 return -ENOMEM;
1ec885cd 491 bitmap->storage.sb_page->index = 0;
9c81075f 492
1ec885cd 493 sb = kmap_atomic(bitmap->storage.sb_page);
9c81075f
JB
494
495 sb->magic = cpu_to_le32(BITMAP_MAGIC);
496 sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
497
498 chunksize = bitmap->mddev->bitmap_info.chunksize;
499 BUG_ON(!chunksize);
500 if (!is_power_of_2(chunksize)) {
b2f46e68 501 kunmap_atomic(sb);
9c81075f
JB
502 printk(KERN_ERR "bitmap chunksize not a power of 2\n");
503 return -EINVAL;
504 }
505 sb->chunksize = cpu_to_le32(chunksize);
506
507 daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
508 if (!daemon_sleep ||
509 (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
510 printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n");
511 daemon_sleep = 5 * HZ;
512 }
513 sb->daemon_sleep = cpu_to_le32(daemon_sleep);
514 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
515
516 /*
517 * FIXME: write_behind for RAID1. If not specified, what
518 * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
519 */
520 write_behind = bitmap->mddev->bitmap_info.max_write_behind;
521 if (write_behind > COUNTER_MAX)
522 write_behind = COUNTER_MAX / 2;
523 sb->write_behind = cpu_to_le32(write_behind);
524 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
525
526 /* keep the array size field of the bitmap superblock up to date */
527 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
528
529 memcpy(sb->uuid, bitmap->mddev->uuid, 16);
530
b405fe91 531 set_bit(BITMAP_STALE, &bitmap->flags);
84e92345 532 sb->state = cpu_to_le32(bitmap->flags);
9c81075f
JB
533 bitmap->events_cleared = bitmap->mddev->events;
534 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
535
b2f46e68 536 kunmap_atomic(sb);
9c81075f
JB
537
538 return 0;
539}
540
32a7627c
N
541/* read the superblock from the bitmap file and initialize some bitmap fields */
542static int bitmap_read_sb(struct bitmap *bitmap)
543{
544 char *reason = NULL;
545 bitmap_super_t *sb;
4b6d287f 546 unsigned long chunksize, daemon_sleep, write_behind;
32a7627c 547 unsigned long long events;
c4ce867f 548 int nodes = 0;
1dff2b87 549 unsigned long sectors_reserved = 0;
32a7627c 550 int err = -EINVAL;
27581e5a 551 struct page *sb_page;
32a7627c 552
1ec885cd 553 if (!bitmap->storage.file && !bitmap->mddev->bitmap_info.offset) {
ef99bf48
N
554 chunksize = 128 * 1024 * 1024;
555 daemon_sleep = 5 * HZ;
556 write_behind = 0;
b405fe91 557 set_bit(BITMAP_STALE, &bitmap->flags);
ef99bf48
N
558 err = 0;
559 goto out_no_sb;
560 }
32a7627c 561 /* page 0 is the superblock, read it... */
27581e5a
N
562 sb_page = alloc_page(GFP_KERNEL);
563 if (!sb_page)
564 return -ENOMEM;
1ec885cd 565 bitmap->storage.sb_page = sb_page;
27581e5a 566
1ec885cd
N
567 if (bitmap->storage.file) {
568 loff_t isize = i_size_read(bitmap->storage.file->f_mapping->host);
f49d5e62
N
569 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
570
1ec885cd 571 err = read_page(bitmap->storage.file, 0,
27581e5a 572 bitmap, bytes, sb_page);
f49d5e62 573 } else {
27581e5a
N
574 err = read_sb_page(bitmap->mddev,
575 bitmap->mddev->bitmap_info.offset,
576 sb_page,
577 0, sizeof(bitmap_super_t));
a654b9d8 578 }
27581e5a 579 if (err)
32a7627c 580 return err;
32a7627c 581
27581e5a 582 sb = kmap_atomic(sb_page);
32a7627c 583
32a7627c 584 chunksize = le32_to_cpu(sb->chunksize);
1b04be96 585 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
4b6d287f 586 write_behind = le32_to_cpu(sb->write_behind);
1dff2b87 587 sectors_reserved = le32_to_cpu(sb->sectors_reserved);
c4ce867f 588 nodes = le32_to_cpu(sb->nodes);
32a7627c
N
589
590 /* verify that the bitmap-specific fields are valid */
591 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
592 reason = "bad magic";
bd926c63
N
593 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
594 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
32a7627c 595 reason = "unrecognized superblock version";
1187cf0a 596 else if (chunksize < 512)
7dd5d34c 597 reason = "bitmap chunksize too small";
d744540c 598 else if (!is_power_of_2(chunksize))
32a7627c 599 reason = "bitmap chunksize not a power of 2";
1b04be96 600 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
7dd5d34c 601 reason = "daemon sleep period out of range";
4b6d287f
N
602 else if (write_behind > COUNTER_MAX)
603 reason = "write-behind limit out of range (0 - 16383)";
32a7627c
N
604 if (reason) {
605 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
606 bmname(bitmap), reason);
607 goto out;
608 }
609
610 /* keep the array size field of the bitmap superblock up to date */
611 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
612
278c1ca2
N
613 if (bitmap->mddev->persistent) {
614 /*
615 * We have a persistent array superblock, so compare the
616 * bitmap's UUID and event counter to the mddev's
617 */
618 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
619 printk(KERN_INFO
620 "%s: bitmap superblock UUID mismatch\n",
621 bmname(bitmap));
622 goto out;
623 }
624 events = le64_to_cpu(sb->events);
625 if (events < bitmap->mddev->events) {
626 printk(KERN_INFO
627 "%s: bitmap file is out of date (%llu < %llu) "
628 "-- forcing full recovery\n",
629 bmname(bitmap), events,
630 (unsigned long long) bitmap->mddev->events);
b405fe91 631 set_bit(BITMAP_STALE, &bitmap->flags);
278c1ca2 632 }
32a7627c 633 }
278c1ca2 634
32a7627c 635 /* assign fields using values from superblock */
4f2e639a 636 bitmap->flags |= le32_to_cpu(sb->state);
bd926c63 637 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
b405fe91 638 set_bit(BITMAP_HOSTENDIAN, &bitmap->flags);
32a7627c
N
639 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
640 err = 0;
641out:
b2f46e68 642 kunmap_atomic(sb);
ef99bf48 643out_no_sb:
b405fe91 644 if (test_bit(BITMAP_STALE, &bitmap->flags))
ef99bf48
N
645 bitmap->events_cleared = bitmap->mddev->events;
646 bitmap->mddev->bitmap_info.chunksize = chunksize;
647 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
648 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
c4ce867f 649 bitmap->mddev->bitmap_info.nodes = nodes;
1dff2b87
N
650 if (bitmap->mddev->bitmap_info.space == 0 ||
651 bitmap->mddev->bitmap_info.space > sectors_reserved)
652 bitmap->mddev->bitmap_info.space = sectors_reserved;
32a7627c
N
653 if (err)
654 bitmap_print_sb(bitmap);
655 return err;
656}
657
32a7627c
N
658/*
659 * general bitmap file operations
660 */
661
ece5cff0
N
662/*
663 * on-disk bitmap:
664 *
665 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
666 * file a page at a time. There's a superblock at the start of the file.
667 */
32a7627c 668/* calculate the index of the page that contains this bit */
1ec885cd
N
669static inline unsigned long file_page_index(struct bitmap_storage *store,
670 unsigned long chunk)
32a7627c 671{
1ec885cd 672 if (store->sb_page)
ece5cff0
N
673 chunk += sizeof(bitmap_super_t) << 3;
674 return chunk >> PAGE_BIT_SHIFT;
32a7627c
N
675}
676
677/* calculate the (bit) offset of this bit within a page */
1ec885cd
N
678static inline unsigned long file_page_offset(struct bitmap_storage *store,
679 unsigned long chunk)
32a7627c 680{
1ec885cd 681 if (store->sb_page)
ece5cff0
N
682 chunk += sizeof(bitmap_super_t) << 3;
683 return chunk & (PAGE_BITS - 1);
32a7627c
N
684}
685
686/*
687 * return a pointer to the page in the filemap that contains the given bit
688 *
32a7627c 689 */
1ec885cd 690static inline struct page *filemap_get_page(struct bitmap_storage *store,
3520fa4d 691 unsigned long chunk)
32a7627c 692{
1ec885cd 693 if (file_page_index(store, chunk) >= store->file_pages)
ac2f40be 694 return NULL;
f2e06c58 695 return store->filemap[file_page_index(store, chunk)];
32a7627c
N
696}
697
d1244cb0
N
698static int bitmap_storage_alloc(struct bitmap_storage *store,
699 unsigned long chunks, int with_super)
700{
701 int pnum;
702 unsigned long num_pages;
703 unsigned long bytes;
704
705 bytes = DIV_ROUND_UP(chunks, 8);
706 if (with_super)
707 bytes += sizeof(bitmap_super_t);
708
709 num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
710
711 store->filemap = kmalloc(sizeof(struct page *)
712 * num_pages, GFP_KERNEL);
713 if (!store->filemap)
714 return -ENOMEM;
715
716 if (with_super && !store->sb_page) {
d60b479d 717 store->sb_page = alloc_page(GFP_KERNEL|__GFP_ZERO);
d1244cb0
N
718 if (store->sb_page == NULL)
719 return -ENOMEM;
720 store->sb_page->index = 0;
721 }
722 pnum = 0;
723 if (store->sb_page) {
724 store->filemap[0] = store->sb_page;
725 pnum = 1;
726 }
727 for ( ; pnum < num_pages; pnum++) {
d60b479d 728 store->filemap[pnum] = alloc_page(GFP_KERNEL|__GFP_ZERO);
d1244cb0
N
729 if (!store->filemap[pnum]) {
730 store->file_pages = pnum;
731 return -ENOMEM;
732 }
733 store->filemap[pnum]->index = pnum;
734 }
735 store->file_pages = pnum;
736
737 /* We need 4 bits per page, rounded up to a multiple
738 * of sizeof(unsigned long) */
739 store->filemap_attr = kzalloc(
740 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
741 GFP_KERNEL);
742 if (!store->filemap_attr)
743 return -ENOMEM;
744
745 store->bytes = bytes;
746
747 return 0;
748}
749
fae7d326 750static void bitmap_file_unmap(struct bitmap_storage *store)
32a7627c
N
751{
752 struct page **map, *sb_page;
32a7627c 753 int pages;
fae7d326 754 struct file *file;
32a7627c 755
fae7d326 756 file = store->file;
1ec885cd 757 map = store->filemap;
1ec885cd 758 pages = store->file_pages;
1ec885cd 759 sb_page = store->sb_page;
32a7627c
N
760
761 while (pages--)
ece5cff0 762 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
d785a06a 763 free_buffers(map[pages]);
32a7627c 764 kfree(map);
fae7d326 765 kfree(store->filemap_attr);
32a7627c 766
d785a06a
N
767 if (sb_page)
768 free_buffers(sb_page);
32a7627c 769
d785a06a 770 if (file) {
496ad9aa 771 struct inode *inode = file_inode(file);
fc0ecff6 772 invalidate_mapping_pages(inode->i_mapping, 0, -1);
32a7627c 773 fput(file);
d785a06a 774 }
32a7627c
N
775}
776
32a7627c
N
777/*
778 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
779 * then it is no longer reliable, so we stop using it and we mark the file
780 * as failed in the superblock
781 */
782static void bitmap_file_kick(struct bitmap *bitmap)
783{
784 char *path, *ptr = NULL;
785
b405fe91 786 if (!test_and_set_bit(BITMAP_STALE, &bitmap->flags)) {
4ad13663 787 bitmap_update_sb(bitmap);
32a7627c 788
1ec885cd 789 if (bitmap->storage.file) {
4ad13663
N
790 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
791 if (path)
1ec885cd
N
792 ptr = d_path(&bitmap->storage.file->f_path,
793 path, PAGE_SIZE);
6bcfd601 794
4ad13663
N
795 printk(KERN_ALERT
796 "%s: kicking failed bitmap file %s from array!\n",
6bcfd601 797 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
32a7627c 798
4ad13663
N
799 kfree(path);
800 } else
801 printk(KERN_ALERT
802 "%s: disabling internal bitmap due to errors\n",
803 bmname(bitmap));
a654b9d8 804 }
32a7627c
N
805}
806
807enum bitmap_page_attr {
ac2f40be 808 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
5a537df4
N
809 BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
810 * i.e. counter is 1 or 2. */
ac2f40be 811 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
32a7627c
N
812};
813
d189122d
N
814static inline void set_page_attr(struct bitmap *bitmap, int pnum,
815 enum bitmap_page_attr attr)
32a7627c 816{
bdfd1140 817 set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
32a7627c
N
818}
819
d189122d
N
820static inline void clear_page_attr(struct bitmap *bitmap, int pnum,
821 enum bitmap_page_attr attr)
32a7627c 822{
bdfd1140 823 clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
32a7627c
N
824}
825
bdfd1140
N
826static inline int test_page_attr(struct bitmap *bitmap, int pnum,
827 enum bitmap_page_attr attr)
32a7627c 828{
1ec885cd 829 return test_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
32a7627c
N
830}
831
bdfd1140
N
832static inline int test_and_clear_page_attr(struct bitmap *bitmap, int pnum,
833 enum bitmap_page_attr attr)
834{
835 return test_and_clear_bit((pnum<<2) + attr,
836 bitmap->storage.filemap_attr);
837}
32a7627c
N
838/*
839 * bitmap_file_set_bit -- called before performing a write to the md device
840 * to set (and eventually sync) a particular bit in the bitmap file
841 *
842 * we set the bit immediately, then we record the page number so that
843 * when an unplug occurs, we can flush the dirty pages out to disk
844 */
845static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
846{
847 unsigned long bit;
3520fa4d 848 struct page *page;
32a7627c 849 void *kaddr;
40cffcc0 850 unsigned long chunk = block >> bitmap->counts.chunkshift;
32a7627c 851
1ec885cd 852 page = filemap_get_page(&bitmap->storage, chunk);
3520fa4d
JB
853 if (!page)
854 return;
1ec885cd 855 bit = file_page_offset(&bitmap->storage, chunk);
32a7627c 856
3520fa4d 857 /* set the bit */
b2f46e68 858 kaddr = kmap_atomic(page);
b405fe91 859 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
3520fa4d
JB
860 set_bit(bit, kaddr);
861 else
3f810b6c 862 set_bit_le(bit, kaddr);
b2f46e68 863 kunmap_atomic(kaddr);
36a4e1fe 864 pr_debug("set file bit %lu page %lu\n", bit, page->index);
32a7627c 865 /* record page number so it gets flushed to disk when unplug occurs */
d189122d 866 set_page_attr(bitmap, page->index, BITMAP_PAGE_DIRTY);
32a7627c
N
867}
868
ef99bf48
N
869static void bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block)
870{
871 unsigned long bit;
872 struct page *page;
873 void *paddr;
40cffcc0 874 unsigned long chunk = block >> bitmap->counts.chunkshift;
ef99bf48 875
1ec885cd 876 page = filemap_get_page(&bitmap->storage, chunk);
ef99bf48
N
877 if (!page)
878 return;
1ec885cd 879 bit = file_page_offset(&bitmap->storage, chunk);
ef99bf48 880 paddr = kmap_atomic(page);
b405fe91 881 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
ef99bf48
N
882 clear_bit(bit, paddr);
883 else
3f810b6c 884 clear_bit_le(bit, paddr);
ef99bf48 885 kunmap_atomic(paddr);
d189122d
N
886 if (!test_page_attr(bitmap, page->index, BITMAP_PAGE_NEEDWRITE)) {
887 set_page_attr(bitmap, page->index, BITMAP_PAGE_PENDING);
ef99bf48
N
888 bitmap->allclean = 0;
889 }
890}
891
32a7627c
N
892/* this gets called when the md device is ready to unplug its underlying
893 * (slave) device queues -- before we let any writes go down, we need to
894 * sync the dirty pages of the bitmap file to disk */
4ad13663 895void bitmap_unplug(struct bitmap *bitmap)
32a7627c 896{
74667123 897 unsigned long i;
ec7a3197 898 int dirty, need_write;
32a7627c 899
62f82faa
N
900 if (!bitmap || !bitmap->storage.filemap ||
901 test_bit(BITMAP_STALE, &bitmap->flags))
4ad13663 902 return;
32a7627c
N
903
904 /* look at each page to see if there are any set bits that need to be
905 * flushed out to disk */
1ec885cd 906 for (i = 0; i < bitmap->storage.file_pages; i++) {
bdfd1140 907 if (!bitmap->storage.filemap)
4ad13663 908 return;
bdfd1140
N
909 dirty = test_and_clear_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
910 need_write = test_and_clear_page_attr(bitmap, i,
911 BITMAP_PAGE_NEEDWRITE);
912 if (dirty || need_write) {
d189122d 913 clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING);
bdfd1140
N
914 write_page(bitmap, bitmap->storage.filemap[i], 0);
915 }
32a7627c 916 }
4b5060dd
N
917 if (bitmap->storage.file)
918 wait_event(bitmap->write_wait,
919 atomic_read(&bitmap->pending_writes)==0);
920 else
921 md_super_wait(bitmap->mddev);
922
b405fe91 923 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
d785a06a 924 bitmap_file_kick(bitmap);
32a7627c 925}
ac2f40be 926EXPORT_SYMBOL(bitmap_unplug);
32a7627c 927
6a07997f 928static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
32a7627c
N
929/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
930 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
931 * memory mapping of the bitmap file
932 * Special cases:
933 * if there's no bitmap file, or if the bitmap file had been
934 * previously kicked from the array, we mark all the bits as
935 * 1's in order to cause a full resync.
6a07997f
N
936 *
937 * We ignore all bits for sectors that end earlier than 'start'.
938 * This is used when reading an out-of-date bitmap...
32a7627c 939 */
6a07997f 940static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
32a7627c
N
941{
942 unsigned long i, chunks, index, oldindex, bit;
27581e5a 943 struct page *page = NULL;
d1244cb0 944 unsigned long bit_cnt = 0;
32a7627c 945 struct file *file;
d1244cb0 946 unsigned long offset;
32a7627c
N
947 int outofdate;
948 int ret = -ENOSPC;
ea03aff9 949 void *paddr;
1ec885cd 950 struct bitmap_storage *store = &bitmap->storage;
32a7627c 951
40cffcc0 952 chunks = bitmap->counts.chunks;
1ec885cd 953 file = store->file;
32a7627c 954
ef99bf48
N
955 if (!file && !bitmap->mddev->bitmap_info.offset) {
956 /* No permanent bitmap - fill with '1s'. */
1ec885cd
N
957 store->filemap = NULL;
958 store->file_pages = 0;
ef99bf48
N
959 for (i = 0; i < chunks ; i++) {
960 /* if the disk bit is set, set the memory bit */
40cffcc0 961 int needed = ((sector_t)(i+1) << (bitmap->counts.chunkshift)
ef99bf48
N
962 >= start);
963 bitmap_set_memory_bits(bitmap,
40cffcc0 964 (sector_t)i << bitmap->counts.chunkshift,
ef99bf48
N
965 needed);
966 }
967 return 0;
968 }
32a7627c 969
b405fe91 970 outofdate = test_bit(BITMAP_STALE, &bitmap->flags);
32a7627c
N
971 if (outofdate)
972 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
973 "recovery\n", bmname(bitmap));
974
d1244cb0 975 if (file && i_size_read(file->f_mapping->host) < store->bytes) {
32a7627c 976 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
d1244cb0
N
977 bmname(bitmap),
978 (unsigned long) i_size_read(file->f_mapping->host),
979 store->bytes);
4ad13663 980 goto err;
32a7627c 981 }
bc7f77de 982
d1244cb0 983 oldindex = ~0L;
27581e5a 984 offset = 0;
d1244cb0 985 if (!bitmap->mddev->bitmap_info.external)
27581e5a 986 offset = sizeof(bitmap_super_t);
32a7627c
N
987
988 for (i = 0; i < chunks; i++) {
bd926c63 989 int b;
1ec885cd
N
990 index = file_page_index(&bitmap->storage, i);
991 bit = file_page_offset(&bitmap->storage, i);
32a7627c 992 if (index != oldindex) { /* this is a new page, read it in */
d785a06a 993 int count;
32a7627c 994 /* unmap the old page, we're done with it */
d1244cb0
N
995 if (index == store->file_pages-1)
996 count = store->bytes - index * PAGE_SIZE;
d785a06a
N
997 else
998 count = PAGE_SIZE;
1ec885cd 999 page = store->filemap[index];
27581e5a
N
1000 if (file)
1001 ret = read_page(file, index, bitmap,
1002 count, page);
1003 else
1004 ret = read_sb_page(
1005 bitmap->mddev,
1006 bitmap->mddev->bitmap_info.offset,
1007 page,
1008 index, count);
1009
1010 if (ret)
4ad13663 1011 goto err;
a654b9d8 1012
32a7627c 1013 oldindex = index;
32a7627c
N
1014
1015 if (outofdate) {
1016 /*
1017 * if bitmap is out of date, dirty the
ac2f40be 1018 * whole page and write it out
32a7627c 1019 */
b2f46e68 1020 paddr = kmap_atomic(page);
ea03aff9 1021 memset(paddr + offset, 0xff,
6a07997f 1022 PAGE_SIZE - offset);
b2f46e68 1023 kunmap_atomic(paddr);
4ad13663
N
1024 write_page(bitmap, page, 1);
1025
1026 ret = -EIO;
b405fe91
N
1027 if (test_bit(BITMAP_WRITE_ERROR,
1028 &bitmap->flags))
4ad13663 1029 goto err;
32a7627c 1030 }
32a7627c 1031 }
b2f46e68 1032 paddr = kmap_atomic(page);
b405fe91 1033 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
ea03aff9 1034 b = test_bit(bit, paddr);
bd926c63 1035 else
6b33aff3 1036 b = test_bit_le(bit, paddr);
b2f46e68 1037 kunmap_atomic(paddr);
bd926c63 1038 if (b) {
32a7627c 1039 /* if the disk bit is set, set the memory bit */
40cffcc0 1040 int needed = ((sector_t)(i+1) << bitmap->counts.chunkshift
db305e50
N
1041 >= start);
1042 bitmap_set_memory_bits(bitmap,
40cffcc0 1043 (sector_t)i << bitmap->counts.chunkshift,
db305e50 1044 needed);
32a7627c
N
1045 bit_cnt++;
1046 }
27581e5a 1047 offset = 0;
32a7627c
N
1048 }
1049
32a7627c 1050 printk(KERN_INFO "%s: bitmap initialized from disk: "
d1244cb0 1051 "read %lu pages, set %lu of %lu bits\n",
1ec885cd 1052 bmname(bitmap), store->file_pages,
d1244cb0 1053 bit_cnt, chunks);
4ad13663
N
1054
1055 return 0;
32a7627c 1056
4ad13663
N
1057 err:
1058 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1059 bmname(bitmap), ret);
32a7627c
N
1060 return ret;
1061}
1062
a654b9d8
N
1063void bitmap_write_all(struct bitmap *bitmap)
1064{
1065 /* We don't actually write all bitmap blocks here,
1066 * just flag them as needing to be written
1067 */
ec7a3197 1068 int i;
a654b9d8 1069
1ec885cd 1070 if (!bitmap || !bitmap->storage.filemap)
ef99bf48 1071 return;
1ec885cd 1072 if (bitmap->storage.file)
ef99bf48
N
1073 /* Only one copy, so nothing needed */
1074 return;
1075
1ec885cd 1076 for (i = 0; i < bitmap->storage.file_pages; i++)
d189122d 1077 set_page_attr(bitmap, i,
ec7a3197 1078 BITMAP_PAGE_NEEDWRITE);
2585f3ef 1079 bitmap->allclean = 0;
a654b9d8
N
1080}
1081
40cffcc0
N
1082static void bitmap_count_page(struct bitmap_counts *bitmap,
1083 sector_t offset, int inc)
32a7627c 1084{
61a0d80c 1085 sector_t chunk = offset >> bitmap->chunkshift;
32a7627c
N
1086 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1087 bitmap->bp[page].count += inc;
32a7627c
N
1088 bitmap_checkfree(bitmap, page);
1089}
bf07bb7d 1090
40cffcc0 1091static void bitmap_set_pending(struct bitmap_counts *bitmap, sector_t offset)
bf07bb7d
N
1092{
1093 sector_t chunk = offset >> bitmap->chunkshift;
1094 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1095 struct bitmap_page *bp = &bitmap->bp[page];
1096
1097 if (!bp->pending)
1098 bp->pending = 1;
1099}
1100
40cffcc0 1101static bitmap_counter_t *bitmap_get_counter(struct bitmap_counts *bitmap,
57dab0bd 1102 sector_t offset, sector_t *blocks,
32a7627c
N
1103 int create);
1104
1105/*
1106 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1107 * out to disk
1108 */
1109
fd01b88c 1110void bitmap_daemon_work(struct mddev *mddev)
32a7627c 1111{
aa5cbd10 1112 struct bitmap *bitmap;
aa3163f8 1113 unsigned long j;
bf07bb7d 1114 unsigned long nextpage;
57dab0bd 1115 sector_t blocks;
40cffcc0 1116 struct bitmap_counts *counts;
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
2e61ebbc 1128 + mddev->bitmap_info.daemon_sleep))
7be3dfec
N
1129 goto done;
1130
32a7627c 1131 bitmap->daemon_lastrun = jiffies;
8311c29d 1132 if (bitmap->allclean) {
2e61ebbc 1133 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
aa5cbd10 1134 goto done;
8311c29d
N
1135 }
1136 bitmap->allclean = 1;
32a7627c 1137
bf07bb7d
N
1138 /* Any file-page which is PENDING now needs to be written.
1139 * So set NEEDWRITE now, then after we make any last-minute changes
1140 * we will write it.
1141 */
1ec885cd 1142 for (j = 0; j < bitmap->storage.file_pages; j++)
bdfd1140
N
1143 if (test_and_clear_page_attr(bitmap, j,
1144 BITMAP_PAGE_PENDING))
d189122d 1145 set_page_attr(bitmap, j,
bf07bb7d 1146 BITMAP_PAGE_NEEDWRITE);
bf07bb7d
N
1147
1148 if (bitmap->need_sync &&
1149 mddev->bitmap_info.external == 0) {
1150 /* Arrange for superblock update as well as
1151 * other changes */
1152 bitmap_super_t *sb;
1153 bitmap->need_sync = 0;
1ec885cd
N
1154 if (bitmap->storage.filemap) {
1155 sb = kmap_atomic(bitmap->storage.sb_page);
ef99bf48
N
1156 sb->events_cleared =
1157 cpu_to_le64(bitmap->events_cleared);
1158 kunmap_atomic(sb);
d189122d 1159 set_page_attr(bitmap, 0,
ef99bf48
N
1160 BITMAP_PAGE_NEEDWRITE);
1161 }
bf07bb7d
N
1162 }
1163 /* Now look at the bitmap counters and if any are '2' or '1',
1164 * decrement and handle accordingly.
1165 */
40cffcc0
N
1166 counts = &bitmap->counts;
1167 spin_lock_irq(&counts->lock);
bf07bb7d 1168 nextpage = 0;
40cffcc0 1169 for (j = 0; j < counts->chunks; j++) {
32a7627c 1170 bitmap_counter_t *bmc;
40cffcc0 1171 sector_t block = (sector_t)j << counts->chunkshift;
3520fa4d 1172
bf07bb7d
N
1173 if (j == nextpage) {
1174 nextpage += PAGE_COUNTER_RATIO;
40cffcc0 1175 if (!counts->bp[j >> PAGE_COUNTER_SHIFT].pending) {
bf07bb7d 1176 j |= PAGE_COUNTER_MASK;
aa3163f8
N
1177 continue;
1178 }
40cffcc0 1179 counts->bp[j >> PAGE_COUNTER_SHIFT].pending = 0;
32a7627c 1180 }
40cffcc0 1181 bmc = bitmap_get_counter(counts,
ef99bf48 1182 block,
db305e50 1183 &blocks, 0);
bf07bb7d
N
1184
1185 if (!bmc) {
5a537df4 1186 j |= PAGE_COUNTER_MASK;
bf07bb7d
N
1187 continue;
1188 }
1189 if (*bmc == 1 && !bitmap->need_sync) {
1190 /* We can clear the bit */
bf07bb7d 1191 *bmc = 0;
40cffcc0 1192 bitmap_count_page(counts, block, -1);
ef99bf48 1193 bitmap_file_clear_bit(bitmap, block);
bf07bb7d
N
1194 } else if (*bmc && *bmc <= 2) {
1195 *bmc = 1;
40cffcc0 1196 bitmap_set_pending(counts, block);
bf07bb7d 1197 bitmap->allclean = 0;
5a537df4 1198 }
32a7627c 1199 }
40cffcc0 1200 spin_unlock_irq(&counts->lock);
32a7627c 1201
bf07bb7d
N
1202 /* Now start writeout on any page in NEEDWRITE that isn't DIRTY.
1203 * DIRTY pages need to be written by bitmap_unplug so it can wait
1204 * for them.
1205 * If we find any DIRTY page we stop there and let bitmap_unplug
1206 * handle all the rest. This is important in the case where
1207 * the first blocking holds the superblock and it has been updated.
1208 * We mustn't write any other blocks before the superblock.
1209 */
62f82faa
N
1210 for (j = 0;
1211 j < bitmap->storage.file_pages
1212 && !test_bit(BITMAP_STALE, &bitmap->flags);
1213 j++) {
bf07bb7d 1214
d189122d 1215 if (test_page_attr(bitmap, j,
bf07bb7d
N
1216 BITMAP_PAGE_DIRTY))
1217 /* bitmap_unplug will handle the rest */
1218 break;
bdfd1140
N
1219 if (test_and_clear_page_attr(bitmap, j,
1220 BITMAP_PAGE_NEEDWRITE)) {
1ec885cd 1221 write_page(bitmap, bitmap->storage.filemap[j], 0);
32a7627c 1222 }
32a7627c
N
1223 }
1224
7be3dfec 1225 done:
8311c29d 1226 if (bitmap->allclean == 0)
2e61ebbc
N
1227 mddev->thread->timeout =
1228 mddev->bitmap_info.daemon_sleep;
c3d9714e 1229 mutex_unlock(&mddev->bitmap_info.mutex);
32a7627c
N
1230}
1231
40cffcc0 1232static bitmap_counter_t *bitmap_get_counter(struct bitmap_counts *bitmap,
57dab0bd 1233 sector_t offset, sector_t *blocks,
32a7627c 1234 int create)
ee305ace
N
1235__releases(bitmap->lock)
1236__acquires(bitmap->lock)
32a7627c
N
1237{
1238 /* If 'create', we might release the lock and reclaim it.
1239 * The lock must have been taken with interrupts enabled.
1240 * If !create, we don't release the lock.
1241 */
61a0d80c 1242 sector_t chunk = offset >> bitmap->chunkshift;
32a7627c
N
1243 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1244 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1245 sector_t csize;
ef425673 1246 int err;
32a7627c 1247
ef425673
N
1248 err = bitmap_checkpage(bitmap, page, create);
1249
1250 if (bitmap->bp[page].hijacked ||
1251 bitmap->bp[page].map == NULL)
61a0d80c 1252 csize = ((sector_t)1) << (bitmap->chunkshift +
ef425673
N
1253 PAGE_COUNTER_SHIFT - 1);
1254 else
61a0d80c 1255 csize = ((sector_t)1) << bitmap->chunkshift;
ef425673
N
1256 *blocks = csize - (offset & (csize - 1));
1257
1258 if (err < 0)
32a7627c 1259 return NULL;
ef425673 1260
32a7627c
N
1261 /* now locked ... */
1262
1263 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1264 /* should we use the first or second counter field
1265 * of the hijacked pointer? */
1266 int hi = (pageoff > PAGE_COUNTER_MASK);
32a7627c
N
1267 return &((bitmap_counter_t *)
1268 &bitmap->bp[page].map)[hi];
ef425673 1269 } else /* page is allocated */
32a7627c
N
1270 return (bitmap_counter_t *)
1271 &(bitmap->bp[page].map[pageoff]);
32a7627c
N
1272}
1273
4b6d287f 1274int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
32a7627c 1275{
ac2f40be
N
1276 if (!bitmap)
1277 return 0;
4b6d287f
N
1278
1279 if (behind) {
696fcd53 1280 int bw;
4b6d287f 1281 atomic_inc(&bitmap->behind_writes);
696fcd53
PC
1282 bw = atomic_read(&bitmap->behind_writes);
1283 if (bw > bitmap->behind_writes_used)
1284 bitmap->behind_writes_used = bw;
1285
36a4e1fe
N
1286 pr_debug("inc write-behind count %d/%lu\n",
1287 bw, bitmap->mddev->bitmap_info.max_write_behind);
4b6d287f
N
1288 }
1289
32a7627c 1290 while (sectors) {
57dab0bd 1291 sector_t blocks;
32a7627c
N
1292 bitmap_counter_t *bmc;
1293
40cffcc0
N
1294 spin_lock_irq(&bitmap->counts.lock);
1295 bmc = bitmap_get_counter(&bitmap->counts, offset, &blocks, 1);
32a7627c 1296 if (!bmc) {
40cffcc0 1297 spin_unlock_irq(&bitmap->counts.lock);
32a7627c
N
1298 return 0;
1299 }
1300
27d5ea04 1301 if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
da6e1a32
NB
1302 DEFINE_WAIT(__wait);
1303 /* note that it is safe to do the prepare_to_wait
1304 * after the test as long as we do it before dropping
1305 * the spinlock.
1306 */
1307 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1308 TASK_UNINTERRUPTIBLE);
40cffcc0 1309 spin_unlock_irq(&bitmap->counts.lock);
f54a9d0e 1310 schedule();
da6e1a32
NB
1311 finish_wait(&bitmap->overflow_wait, &__wait);
1312 continue;
1313 }
1314
ac2f40be 1315 switch (*bmc) {
32a7627c
N
1316 case 0:
1317 bitmap_file_set_bit(bitmap, offset);
40cffcc0 1318 bitmap_count_page(&bitmap->counts, offset, 1);
32a7627c
N
1319 /* fall through */
1320 case 1:
1321 *bmc = 2;
1322 }
da6e1a32 1323
32a7627c
N
1324 (*bmc)++;
1325
40cffcc0 1326 spin_unlock_irq(&bitmap->counts.lock);
32a7627c
N
1327
1328 offset += blocks;
1329 if (sectors > blocks)
1330 sectors -= blocks;
ac2f40be
N
1331 else
1332 sectors = 0;
32a7627c
N
1333 }
1334 return 0;
1335}
ac2f40be 1336EXPORT_SYMBOL(bitmap_startwrite);
32a7627c
N
1337
1338void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
4b6d287f 1339 int success, int behind)
32a7627c 1340{
ac2f40be
N
1341 if (!bitmap)
1342 return;
4b6d287f 1343 if (behind) {
e555190d
N
1344 if (atomic_dec_and_test(&bitmap->behind_writes))
1345 wake_up(&bitmap->behind_wait);
36a4e1fe
N
1346 pr_debug("dec write-behind count %d/%lu\n",
1347 atomic_read(&bitmap->behind_writes),
1348 bitmap->mddev->bitmap_info.max_write_behind);
4b6d287f
N
1349 }
1350
32a7627c 1351 while (sectors) {
57dab0bd 1352 sector_t blocks;
32a7627c
N
1353 unsigned long flags;
1354 bitmap_counter_t *bmc;
1355
40cffcc0
N
1356 spin_lock_irqsave(&bitmap->counts.lock, flags);
1357 bmc = bitmap_get_counter(&bitmap->counts, offset, &blocks, 0);
32a7627c 1358 if (!bmc) {
40cffcc0 1359 spin_unlock_irqrestore(&bitmap->counts.lock, flags);
32a7627c
N
1360 return;
1361 }
1362
961902c0 1363 if (success && !bitmap->mddev->degraded &&
a0da84f3
NB
1364 bitmap->events_cleared < bitmap->mddev->events) {
1365 bitmap->events_cleared = bitmap->mddev->events;
1366 bitmap->need_sync = 1;
5ff5afff 1367 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
a0da84f3
NB
1368 }
1369
27d5ea04 1370 if (!success && !NEEDED(*bmc))
32a7627c
N
1371 *bmc |= NEEDED_MASK;
1372
27d5ea04 1373 if (COUNTER(*bmc) == COUNTER_MAX)
da6e1a32
NB
1374 wake_up(&bitmap->overflow_wait);
1375
32a7627c 1376 (*bmc)--;
2585f3ef 1377 if (*bmc <= 2) {
40cffcc0 1378 bitmap_set_pending(&bitmap->counts, offset);
2585f3ef
N
1379 bitmap->allclean = 0;
1380 }
40cffcc0 1381 spin_unlock_irqrestore(&bitmap->counts.lock, flags);
32a7627c
N
1382 offset += blocks;
1383 if (sectors > blocks)
1384 sectors -= blocks;
ac2f40be
N
1385 else
1386 sectors = 0;
32a7627c
N
1387 }
1388}
ac2f40be 1389EXPORT_SYMBOL(bitmap_endwrite);
32a7627c 1390
57dab0bd 1391static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
1187cf0a 1392 int degraded)
32a7627c
N
1393{
1394 bitmap_counter_t *bmc;
1395 int rv;
1396 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1397 *blocks = 1024;
1398 return 1; /* always resync if no bitmap */
1399 }
40cffcc0
N
1400 spin_lock_irq(&bitmap->counts.lock);
1401 bmc = bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
32a7627c
N
1402 rv = 0;
1403 if (bmc) {
1404 /* locked */
1405 if (RESYNC(*bmc))
1406 rv = 1;
1407 else if (NEEDED(*bmc)) {
1408 rv = 1;
6a806c51
N
1409 if (!degraded) { /* don't set/clear bits if degraded */
1410 *bmc |= RESYNC_MASK;
1411 *bmc &= ~NEEDED_MASK;
1412 }
32a7627c
N
1413 }
1414 }
40cffcc0 1415 spin_unlock_irq(&bitmap->counts.lock);
32a7627c
N
1416 return rv;
1417}
1418
57dab0bd 1419int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
1187cf0a
N
1420 int degraded)
1421{
1422 /* bitmap_start_sync must always report on multiples of whole
1423 * pages, otherwise resync (which is very PAGE_SIZE based) will
1424 * get confused.
1425 * So call __bitmap_start_sync repeatedly (if needed) until
1426 * At least PAGE_SIZE>>9 blocks are covered.
1427 * Return the 'or' of the result.
1428 */
1429 int rv = 0;
57dab0bd 1430 sector_t blocks1;
1187cf0a
N
1431
1432 *blocks = 0;
1433 while (*blocks < (PAGE_SIZE>>9)) {
1434 rv |= __bitmap_start_sync(bitmap, offset,
1435 &blocks1, degraded);
1436 offset += blocks1;
1437 *blocks += blocks1;
1438 }
1439 return rv;
1440}
ac2f40be 1441EXPORT_SYMBOL(bitmap_start_sync);
1187cf0a 1442
57dab0bd 1443void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
32a7627c
N
1444{
1445 bitmap_counter_t *bmc;
1446 unsigned long flags;
ac2f40be
N
1447
1448 if (bitmap == NULL) {
32a7627c
N
1449 *blocks = 1024;
1450 return;
1451 }
40cffcc0
N
1452 spin_lock_irqsave(&bitmap->counts.lock, flags);
1453 bmc = bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
32a7627c
N
1454 if (bmc == NULL)
1455 goto unlock;
1456 /* locked */
32a7627c
N
1457 if (RESYNC(*bmc)) {
1458 *bmc &= ~RESYNC_MASK;
1459
1460 if (!NEEDED(*bmc) && aborted)
1461 *bmc |= NEEDED_MASK;
1462 else {
2585f3ef 1463 if (*bmc <= 2) {
40cffcc0 1464 bitmap_set_pending(&bitmap->counts, offset);
2585f3ef
N
1465 bitmap->allclean = 0;
1466 }
32a7627c
N
1467 }
1468 }
1469 unlock:
40cffcc0 1470 spin_unlock_irqrestore(&bitmap->counts.lock, flags);
32a7627c 1471}
ac2f40be 1472EXPORT_SYMBOL(bitmap_end_sync);
32a7627c
N
1473
1474void bitmap_close_sync(struct bitmap *bitmap)
1475{
1476 /* Sync has finished, and any bitmap chunks that weren't synced
1477 * properly have been aborted. It remains to us to clear the
1478 * RESYNC bit wherever it is still on
1479 */
1480 sector_t sector = 0;
57dab0bd 1481 sector_t blocks;
b47490c9
N
1482 if (!bitmap)
1483 return;
32a7627c
N
1484 while (sector < bitmap->mddev->resync_max_sectors) {
1485 bitmap_end_sync(bitmap, sector, &blocks, 0);
b47490c9
N
1486 sector += blocks;
1487 }
1488}
ac2f40be 1489EXPORT_SYMBOL(bitmap_close_sync);
b47490c9
N
1490
1491void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1492{
1493 sector_t s = 0;
57dab0bd 1494 sector_t blocks;
b47490c9
N
1495
1496 if (!bitmap)
1497 return;
1498 if (sector == 0) {
1499 bitmap->last_end_sync = jiffies;
1500 return;
1501 }
1502 if (time_before(jiffies, (bitmap->last_end_sync
1b04be96 1503 + bitmap->mddev->bitmap_info.daemon_sleep)))
b47490c9
N
1504 return;
1505 wait_event(bitmap->mddev->recovery_wait,
1506 atomic_read(&bitmap->mddev->recovery_active) == 0);
1507
75d3da43 1508 bitmap->mddev->curr_resync_completed = sector;
070dc6dd 1509 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
40cffcc0 1510 sector &= ~((1ULL << bitmap->counts.chunkshift) - 1);
b47490c9
N
1511 s = 0;
1512 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1513 bitmap_end_sync(bitmap, s, &blocks, 0);
1514 s += blocks;
32a7627c 1515 }
b47490c9 1516 bitmap->last_end_sync = jiffies;
acb180b0 1517 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
32a7627c 1518}
ac2f40be 1519EXPORT_SYMBOL(bitmap_cond_end_sync);
32a7627c 1520
6a07997f 1521static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
32a7627c
N
1522{
1523 /* For each chunk covered by any of these sectors, set the
ef99bf48 1524 * counter to 2 and possibly set resync_needed. They should all
32a7627c
N
1525 * be 0 at this point
1526 */
193f1c93 1527
57dab0bd 1528 sector_t secs;
193f1c93 1529 bitmap_counter_t *bmc;
40cffcc0
N
1530 spin_lock_irq(&bitmap->counts.lock);
1531 bmc = bitmap_get_counter(&bitmap->counts, offset, &secs, 1);
193f1c93 1532 if (!bmc) {
40cffcc0 1533 spin_unlock_irq(&bitmap->counts.lock);
193f1c93 1534 return;
32a7627c 1535 }
ac2f40be 1536 if (!*bmc) {
915c420d 1537 *bmc = 2 | (needed ? NEEDED_MASK : 0);
40cffcc0
N
1538 bitmap_count_page(&bitmap->counts, offset, 1);
1539 bitmap_set_pending(&bitmap->counts, offset);
2585f3ef 1540 bitmap->allclean = 0;
193f1c93 1541 }
40cffcc0 1542 spin_unlock_irq(&bitmap->counts.lock);
32a7627c
N
1543}
1544
9b1d1dac
PC
1545/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1546void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1547{
1548 unsigned long chunk;
1549
1550 for (chunk = s; chunk <= e; chunk++) {
40cffcc0 1551 sector_t sec = (sector_t)chunk << bitmap->counts.chunkshift;
9b1d1dac
PC
1552 bitmap_set_memory_bits(bitmap, sec, 1);
1553 bitmap_file_set_bit(bitmap, sec);
ffa23322
N
1554 if (sec < bitmap->mddev->recovery_cp)
1555 /* We are asserting that the array is dirty,
1556 * so move the recovery_cp address back so
1557 * that it is obvious that it is dirty
1558 */
1559 bitmap->mddev->recovery_cp = sec;
9b1d1dac
PC
1560 }
1561}
1562
6b8b3e8a
N
1563/*
1564 * flush out any pending updates
1565 */
fd01b88c 1566void bitmap_flush(struct mddev *mddev)
6b8b3e8a
N
1567{
1568 struct bitmap *bitmap = mddev->bitmap;
42a04b50 1569 long sleep;
6b8b3e8a
N
1570
1571 if (!bitmap) /* there was no bitmap */
1572 return;
1573
1574 /* run the daemon_work three time to ensure everything is flushed
1575 * that can be
1576 */
1b04be96 1577 sleep = mddev->bitmap_info.daemon_sleep * 2;
42a04b50 1578 bitmap->daemon_lastrun -= sleep;
aa5cbd10 1579 bitmap_daemon_work(mddev);
42a04b50 1580 bitmap->daemon_lastrun -= sleep;
aa5cbd10 1581 bitmap_daemon_work(mddev);
42a04b50 1582 bitmap->daemon_lastrun -= sleep;
aa5cbd10 1583 bitmap_daemon_work(mddev);
6b8b3e8a
N
1584 bitmap_update_sb(bitmap);
1585}
1586
32a7627c
N
1587/*
1588 * free memory that was allocated
1589 */
3178b0db 1590static void bitmap_free(struct bitmap *bitmap)
32a7627c
N
1591{
1592 unsigned long k, pages;
1593 struct bitmap_page *bp;
32a7627c
N
1594
1595 if (!bitmap) /* there was no bitmap */
1596 return;
1597
fae7d326
N
1598 /* Shouldn't be needed - but just in case.... */
1599 wait_event(bitmap->write_wait,
1600 atomic_read(&bitmap->pending_writes) == 0);
1601
1602 /* release the bitmap file */
1603 bitmap_file_unmap(&bitmap->storage);
32a7627c 1604
40cffcc0
N
1605 bp = bitmap->counts.bp;
1606 pages = bitmap->counts.pages;
32a7627c
N
1607
1608 /* free all allocated memory */
1609
32a7627c
N
1610 if (bp) /* deallocate the page memory */
1611 for (k = 0; k < pages; k++)
1612 if (bp[k].map && !bp[k].hijacked)
1613 kfree(bp[k].map);
1614 kfree(bp);
1615 kfree(bitmap);
1616}
aa5cbd10 1617
fd01b88c 1618void bitmap_destroy(struct mddev *mddev)
3178b0db
N
1619{
1620 struct bitmap *bitmap = mddev->bitmap;
1621
1622 if (!bitmap) /* there was no bitmap */
1623 return;
1624
c3d9714e 1625 mutex_lock(&mddev->bitmap_info.mutex);
978a7a47 1626 spin_lock(&mddev->lock);
3178b0db 1627 mddev->bitmap = NULL; /* disconnect from the md device */
978a7a47 1628 spin_unlock(&mddev->lock);
c3d9714e 1629 mutex_unlock(&mddev->bitmap_info.mutex);
b15c2e57
N
1630 if (mddev->thread)
1631 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
3178b0db 1632
ece5cff0
N
1633 if (bitmap->sysfs_can_clear)
1634 sysfs_put(bitmap->sysfs_can_clear);
1635
3178b0db
N
1636 bitmap_free(bitmap);
1637}
32a7627c
N
1638
1639/*
1640 * initialize the bitmap structure
1641 * if this returns an error, bitmap_destroy must be called to do clean up
1642 */
fd01b88c 1643int bitmap_create(struct mddev *mddev)
32a7627c
N
1644{
1645 struct bitmap *bitmap;
1f593903 1646 sector_t blocks = mddev->resync_max_sectors;
c3d9714e 1647 struct file *file = mddev->bitmap_info.file;
32a7627c 1648 int err;
324a56e1 1649 struct kernfs_node *bm = NULL;
32a7627c 1650
5f6e3c83 1651 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
32a7627c 1652
c3d9714e 1653 BUG_ON(file && mddev->bitmap_info.offset);
a654b9d8 1654
9ffae0cf 1655 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
32a7627c
N
1656 if (!bitmap)
1657 return -ENOMEM;
1658
40cffcc0 1659 spin_lock_init(&bitmap->counts.lock);
ce25c31b
N
1660 atomic_set(&bitmap->pending_writes, 0);
1661 init_waitqueue_head(&bitmap->write_wait);
da6e1a32 1662 init_waitqueue_head(&bitmap->overflow_wait);
e555190d 1663 init_waitqueue_head(&bitmap->behind_wait);
ce25c31b 1664
32a7627c 1665 bitmap->mddev = mddev;
32a7627c 1666
5ff5afff 1667 if (mddev->kobj.sd)
388975cc 1668 bm = sysfs_get_dirent(mddev->kobj.sd, "bitmap");
ece5cff0 1669 if (bm) {
388975cc 1670 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, "can_clear");
ece5cff0
N
1671 sysfs_put(bm);
1672 } else
1673 bitmap->sysfs_can_clear = NULL;
1674
1ec885cd 1675 bitmap->storage.file = file;
ce25c31b
N
1676 if (file) {
1677 get_file(file);
ae8fa283
N
1678 /* As future accesses to this file will use bmap,
1679 * and bypass the page cache, we must sync the file
1680 * first.
1681 */
8018ab05 1682 vfs_fsync(file, 1);
ce25c31b 1683 }
42a04b50 1684 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
9c81075f
JB
1685 if (!mddev->bitmap_info.external) {
1686 /*
1687 * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
1688 * instructing us to create a new on-disk bitmap instance.
1689 */
1690 if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
1691 err = bitmap_new_disk_sb(bitmap);
1692 else
1693 err = bitmap_read_sb(bitmap);
1694 } else {
ece5cff0
N
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;
d60b479d
N
1706 err = bitmap_resize(bitmap, blocks, mddev->bitmap_info.chunksize, 1);
1707 if (err)
3178b0db 1708 goto error;
32a7627c 1709
69e51b44 1710 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
d60b479d 1711 bitmap->counts.pages, bmname(bitmap));
69e51b44
N
1712
1713 mddev->bitmap = bitmap;
b405fe91 1714 return test_bit(BITMAP_WRITE_ERROR, &bitmap->flags) ? -EIO : 0;
69e51b44
N
1715
1716 error:
1717 bitmap_free(bitmap);
1718 return err;
1719}
1720
fd01b88c 1721int bitmap_load(struct mddev *mddev)
69e51b44
N
1722{
1723 int err = 0;
3520fa4d 1724 sector_t start = 0;
69e51b44
N
1725 sector_t sector = 0;
1726 struct bitmap *bitmap = mddev->bitmap;
1727
1728 if (!bitmap)
1729 goto out;
1730
1731 /* Clear out old bitmap info first: Either there is none, or we
1732 * are resuming after someone else has possibly changed things,
1733 * so we should forget old cached info.
1734 * All chunks should be clean, but some might need_sync.
1735 */
1736 while (sector < mddev->resync_max_sectors) {
57dab0bd 1737 sector_t blocks;
69e51b44
N
1738 bitmap_start_sync(bitmap, sector, &blocks, 0);
1739 sector += blocks;
1740 }
1741 bitmap_close_sync(bitmap);
1742
3520fa4d
JB
1743 if (mddev->degraded == 0
1744 || bitmap->events_cleared == mddev->events)
1745 /* no need to keep dirty bits to optimise a
1746 * re-add of a missing device */
1747 start = mddev->recovery_cp;
1748
afbaa90b 1749 mutex_lock(&mddev->bitmap_info.mutex);
3520fa4d 1750 err = bitmap_init_from_disk(bitmap, start);
afbaa90b 1751 mutex_unlock(&mddev->bitmap_info.mutex);
3520fa4d 1752
32a7627c 1753 if (err)
69e51b44 1754 goto out;
b405fe91 1755 clear_bit(BITMAP_STALE, &bitmap->flags);
ef99bf48
N
1756
1757 /* Kick recovery in case any bits were set */
1758 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
3178b0db 1759
1b04be96 1760 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
9cd30fdc 1761 md_wakeup_thread(mddev->thread);
b15c2e57 1762
4ad13663
N
1763 bitmap_update_sb(bitmap);
1764
b405fe91 1765 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
69e51b44
N
1766 err = -EIO;
1767out:
3178b0db 1768 return err;
32a7627c 1769}
69e51b44 1770EXPORT_SYMBOL_GPL(bitmap_load);
32a7627c 1771
57148964
N
1772void bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
1773{
1774 unsigned long chunk_kb;
40cffcc0 1775 struct bitmap_counts *counts;
57148964
N
1776
1777 if (!bitmap)
1778 return;
1779
40cffcc0
N
1780 counts = &bitmap->counts;
1781
57148964
N
1782 chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
1783 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
1784 "%lu%s chunk",
40cffcc0
N
1785 counts->pages - counts->missing_pages,
1786 counts->pages,
1787 (counts->pages - counts->missing_pages)
57148964
N
1788 << (PAGE_SHIFT - 10),
1789 chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
1790 chunk_kb ? "KB" : "B");
1ec885cd 1791 if (bitmap->storage.file) {
57148964 1792 seq_printf(seq, ", file: ");
1ec885cd 1793 seq_path(seq, &bitmap->storage.file->f_path, " \t\n");
57148964
N
1794 }
1795
1796 seq_printf(seq, "\n");
57148964
N
1797}
1798
d60b479d
N
1799int bitmap_resize(struct bitmap *bitmap, sector_t blocks,
1800 int chunksize, int init)
1801{
1802 /* If chunk_size is 0, choose an appropriate chunk size.
1803 * Then possibly allocate new storage space.
1804 * Then quiesce, copy bits, replace bitmap, and re-start
1805 *
1806 * This function is called both to set up the initial bitmap
1807 * and to resize the bitmap while the array is active.
1808 * If this happens as a result of the array being resized,
1809 * chunksize will be zero, and we need to choose a suitable
1810 * chunksize, otherwise we use what we are given.
1811 */
1812 struct bitmap_storage store;
1813 struct bitmap_counts old_counts;
1814 unsigned long chunks;
1815 sector_t block;
1816 sector_t old_blocks, new_blocks;
1817 int chunkshift;
1818 int ret = 0;
1819 long pages;
1820 struct bitmap_page *new_bp;
1821
1822 if (chunksize == 0) {
1823 /* If there is enough space, leave the chunk size unchanged,
1824 * else increase by factor of two until there is enough space.
1825 */
1826 long bytes;
1827 long space = bitmap->mddev->bitmap_info.space;
1828
1829 if (space == 0) {
1830 /* We don't know how much space there is, so limit
1831 * to current size - in sectors.
1832 */
1833 bytes = DIV_ROUND_UP(bitmap->counts.chunks, 8);
1834 if (!bitmap->mddev->bitmap_info.external)
1835 bytes += sizeof(bitmap_super_t);
1836 space = DIV_ROUND_UP(bytes, 512);
1837 bitmap->mddev->bitmap_info.space = space;
1838 }
1839 chunkshift = bitmap->counts.chunkshift;
1840 chunkshift--;
1841 do {
1842 /* 'chunkshift' is shift from block size to chunk size */
1843 chunkshift++;
1844 chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
1845 bytes = DIV_ROUND_UP(chunks, 8);
1846 if (!bitmap->mddev->bitmap_info.external)
1847 bytes += sizeof(bitmap_super_t);
1848 } while (bytes > (space << 9));
1849 } else
1850 chunkshift = ffz(~chunksize) - BITMAP_BLOCK_SHIFT;
1851
1852 chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
1853 memset(&store, 0, sizeof(store));
1854 if (bitmap->mddev->bitmap_info.offset || bitmap->mddev->bitmap_info.file)
1855 ret = bitmap_storage_alloc(&store, chunks,
1856 !bitmap->mddev->bitmap_info.external);
1857 if (ret)
1858 goto err;
1859
1860 pages = DIV_ROUND_UP(chunks, PAGE_COUNTER_RATIO);
1861
1862 new_bp = kzalloc(pages * sizeof(*new_bp), GFP_KERNEL);
1863 ret = -ENOMEM;
1864 if (!new_bp) {
1865 bitmap_file_unmap(&store);
1866 goto err;
1867 }
1868
1869 if (!init)
1870 bitmap->mddev->pers->quiesce(bitmap->mddev, 1);
1871
1872 store.file = bitmap->storage.file;
1873 bitmap->storage.file = NULL;
1874
1875 if (store.sb_page && bitmap->storage.sb_page)
1876 memcpy(page_address(store.sb_page),
1877 page_address(bitmap->storage.sb_page),
1878 sizeof(bitmap_super_t));
1879 bitmap_file_unmap(&bitmap->storage);
1880 bitmap->storage = store;
1881
1882 old_counts = bitmap->counts;
1883 bitmap->counts.bp = new_bp;
1884 bitmap->counts.pages = pages;
1885 bitmap->counts.missing_pages = pages;
1886 bitmap->counts.chunkshift = chunkshift;
1887 bitmap->counts.chunks = chunks;
1888 bitmap->mddev->bitmap_info.chunksize = 1 << (chunkshift +
1889 BITMAP_BLOCK_SHIFT);
1890
1891 blocks = min(old_counts.chunks << old_counts.chunkshift,
1892 chunks << chunkshift);
1893
1894 spin_lock_irq(&bitmap->counts.lock);
1895 for (block = 0; block < blocks; ) {
1896 bitmap_counter_t *bmc_old, *bmc_new;
1897 int set;
1898
1899 bmc_old = bitmap_get_counter(&old_counts, block,
1900 &old_blocks, 0);
1901 set = bmc_old && NEEDED(*bmc_old);
1902
1903 if (set) {
1904 bmc_new = bitmap_get_counter(&bitmap->counts, block,
1905 &new_blocks, 1);
1906 if (*bmc_new == 0) {
1907 /* need to set on-disk bits too. */
1908 sector_t end = block + new_blocks;
1909 sector_t start = block >> chunkshift;
1910 start <<= chunkshift;
1911 while (start < end) {
1912 bitmap_file_set_bit(bitmap, block);
1913 start += 1 << chunkshift;
1914 }
1915 *bmc_new = 2;
1916 bitmap_count_page(&bitmap->counts,
1917 block, 1);
1918 bitmap_set_pending(&bitmap->counts,
1919 block);
1920 }
1921 *bmc_new |= NEEDED_MASK;
1922 if (new_blocks < old_blocks)
1923 old_blocks = new_blocks;
1924 }
1925 block += old_blocks;
1926 }
1927
1928 if (!init) {
1929 int i;
1930 while (block < (chunks << chunkshift)) {
1931 bitmap_counter_t *bmc;
1932 bmc = bitmap_get_counter(&bitmap->counts, block,
1933 &new_blocks, 1);
1934 if (bmc) {
1935 /* new space. It needs to be resynced, so
1936 * we set NEEDED_MASK.
1937 */
1938 if (*bmc == 0) {
1939 *bmc = NEEDED_MASK | 2;
1940 bitmap_count_page(&bitmap->counts,
1941 block, 1);
1942 bitmap_set_pending(&bitmap->counts,
1943 block);
1944 }
1945 }
1946 block += new_blocks;
1947 }
1948 for (i = 0; i < bitmap->storage.file_pages; i++)
1949 set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
1950 }
1951 spin_unlock_irq(&bitmap->counts.lock);
1952
1953 if (!init) {
1954 bitmap_unplug(bitmap);
1955 bitmap->mddev->pers->quiesce(bitmap->mddev, 0);
1956 }
1957 ret = 0;
1958err:
1959 return ret;
1960}
1961EXPORT_SYMBOL_GPL(bitmap_resize);
1962
43a70507 1963static ssize_t
fd01b88c 1964location_show(struct mddev *mddev, char *page)
43a70507
N
1965{
1966 ssize_t len;
ac2f40be 1967 if (mddev->bitmap_info.file)
43a70507 1968 len = sprintf(page, "file");
ac2f40be 1969 else if (mddev->bitmap_info.offset)
43a70507 1970 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
ac2f40be 1971 else
43a70507
N
1972 len = sprintf(page, "none");
1973 len += sprintf(page+len, "\n");
1974 return len;
1975}
1976
1977static ssize_t
fd01b88c 1978location_store(struct mddev *mddev, const char *buf, size_t len)
43a70507
N
1979{
1980
1981 if (mddev->pers) {
1982 if (!mddev->pers->quiesce)
1983 return -EBUSY;
1984 if (mddev->recovery || mddev->sync_thread)
1985 return -EBUSY;
1986 }
1987
1988 if (mddev->bitmap || mddev->bitmap_info.file ||
1989 mddev->bitmap_info.offset) {
1990 /* bitmap already configured. Only option is to clear it */
1991 if (strncmp(buf, "none", 4) != 0)
1992 return -EBUSY;
1993 if (mddev->pers) {
1994 mddev->pers->quiesce(mddev, 1);
1995 bitmap_destroy(mddev);
1996 mddev->pers->quiesce(mddev, 0);
1997 }
1998 mddev->bitmap_info.offset = 0;
1999 if (mddev->bitmap_info.file) {
2000 struct file *f = mddev->bitmap_info.file;
2001 mddev->bitmap_info.file = NULL;
43a70507
N
2002 fput(f);
2003 }
2004 } else {
2005 /* No bitmap, OK to set a location */
2006 long long offset;
2007 if (strncmp(buf, "none", 4) == 0)
2008 /* nothing to be done */;
2009 else if (strncmp(buf, "file:", 5) == 0) {
2010 /* Not supported yet */
2011 return -EINVAL;
2012 } else {
2013 int rv;
2014 if (buf[0] == '+')
b29bebd6 2015 rv = kstrtoll(buf+1, 10, &offset);
43a70507 2016 else
b29bebd6 2017 rv = kstrtoll(buf, 10, &offset);
43a70507
N
2018 if (rv)
2019 return rv;
2020 if (offset == 0)
2021 return -EINVAL;
ece5cff0
N
2022 if (mddev->bitmap_info.external == 0 &&
2023 mddev->major_version == 0 &&
43a70507
N
2024 offset != mddev->bitmap_info.default_offset)
2025 return -EINVAL;
2026 mddev->bitmap_info.offset = offset;
2027 if (mddev->pers) {
2028 mddev->pers->quiesce(mddev, 1);
2029 rv = bitmap_create(mddev);
4474ca42
N
2030 if (!rv)
2031 rv = bitmap_load(mddev);
43a70507
N
2032 if (rv) {
2033 bitmap_destroy(mddev);
2034 mddev->bitmap_info.offset = 0;
2035 }
2036 mddev->pers->quiesce(mddev, 0);
2037 if (rv)
2038 return rv;
2039 }
2040 }
2041 }
2042 if (!mddev->external) {
2043 /* Ensure new bitmap info is stored in
2044 * metadata promptly.
2045 */
2046 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2047 md_wakeup_thread(mddev->thread);
2048 }
2049 return len;
2050}
2051
2052static struct md_sysfs_entry bitmap_location =
2053__ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
2054
6409bb05
N
2055/* 'bitmap/space' is the space available at 'location' for the
2056 * bitmap. This allows the kernel to know when it is safe to
2057 * resize the bitmap to match a resized array.
2058 */
2059static ssize_t
2060space_show(struct mddev *mddev, char *page)
2061{
2062 return sprintf(page, "%lu\n", mddev->bitmap_info.space);
2063}
2064
2065static ssize_t
2066space_store(struct mddev *mddev, const char *buf, size_t len)
2067{
2068 unsigned long sectors;
2069 int rv;
2070
2071 rv = kstrtoul(buf, 10, &sectors);
2072 if (rv)
2073 return rv;
2074
2075 if (sectors == 0)
2076 return -EINVAL;
2077
2078 if (mddev->bitmap &&
9b1215c1 2079 sectors < (mddev->bitmap->storage.bytes + 511) >> 9)
6409bb05
N
2080 return -EFBIG; /* Bitmap is too big for this small space */
2081
2082 /* could make sure it isn't too big, but that isn't really
2083 * needed - user-space should be careful.
2084 */
2085 mddev->bitmap_info.space = sectors;
2086 return len;
2087}
2088
2089static struct md_sysfs_entry bitmap_space =
2090__ATTR(space, S_IRUGO|S_IWUSR, space_show, space_store);
2091
43a70507 2092static ssize_t
fd01b88c 2093timeout_show(struct mddev *mddev, char *page)
43a70507
N
2094{
2095 ssize_t len;
2096 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
2097 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
ac2f40be 2098
43a70507
N
2099 len = sprintf(page, "%lu", secs);
2100 if (jifs)
2101 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
2102 len += sprintf(page+len, "\n");
2103 return len;
2104}
2105
2106static ssize_t
fd01b88c 2107timeout_store(struct mddev *mddev, const char *buf, size_t len)
43a70507
N
2108{
2109 /* timeout can be set at any time */
2110 unsigned long timeout;
2111 int rv = strict_strtoul_scaled(buf, &timeout, 4);
2112 if (rv)
2113 return rv;
2114
2115 /* just to make sure we don't overflow... */
2116 if (timeout >= LONG_MAX / HZ)
2117 return -EINVAL;
2118
2119 timeout = timeout * HZ / 10000;
2120
2121 if (timeout >= MAX_SCHEDULE_TIMEOUT)
2122 timeout = MAX_SCHEDULE_TIMEOUT-1;
2123 if (timeout < 1)
2124 timeout = 1;
2125 mddev->bitmap_info.daemon_sleep = timeout;
2126 if (mddev->thread) {
2127 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
2128 * the bitmap is all clean and we don't need to
2129 * adjust the timeout right now
2130 */
2131 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
2132 mddev->thread->timeout = timeout;
2133 md_wakeup_thread(mddev->thread);
2134 }
2135 }
2136 return len;
2137}
2138
2139static struct md_sysfs_entry bitmap_timeout =
2140__ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
2141
2142static ssize_t
fd01b88c 2143backlog_show(struct mddev *mddev, char *page)
43a70507
N
2144{
2145 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
2146}
2147
2148static ssize_t
fd01b88c 2149backlog_store(struct mddev *mddev, const char *buf, size_t len)
43a70507
N
2150{
2151 unsigned long backlog;
b29bebd6 2152 int rv = kstrtoul(buf, 10, &backlog);
43a70507
N
2153 if (rv)
2154 return rv;
2155 if (backlog > COUNTER_MAX)
2156 return -EINVAL;
2157 mddev->bitmap_info.max_write_behind = backlog;
2158 return len;
2159}
2160
2161static struct md_sysfs_entry bitmap_backlog =
2162__ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
2163
2164static ssize_t
fd01b88c 2165chunksize_show(struct mddev *mddev, char *page)
43a70507
N
2166{
2167 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
2168}
2169
2170static ssize_t
fd01b88c 2171chunksize_store(struct mddev *mddev, const char *buf, size_t len)
43a70507
N
2172{
2173 /* Can only be changed when no bitmap is active */
2174 int rv;
2175 unsigned long csize;
2176 if (mddev->bitmap)
2177 return -EBUSY;
b29bebd6 2178 rv = kstrtoul(buf, 10, &csize);
43a70507
N
2179 if (rv)
2180 return rv;
2181 if (csize < 512 ||
2182 !is_power_of_2(csize))
2183 return -EINVAL;
2184 mddev->bitmap_info.chunksize = csize;
2185 return len;
2186}
2187
2188static struct md_sysfs_entry bitmap_chunksize =
2189__ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
2190
fd01b88c 2191static ssize_t metadata_show(struct mddev *mddev, char *page)
ece5cff0 2192{
c4ce867f
GR
2193 if (mddev_is_clustered(mddev))
2194 return sprintf(page, "clustered\n");
ece5cff0
N
2195 return sprintf(page, "%s\n", (mddev->bitmap_info.external
2196 ? "external" : "internal"));
2197}
2198
fd01b88c 2199static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
ece5cff0
N
2200{
2201 if (mddev->bitmap ||
2202 mddev->bitmap_info.file ||
2203 mddev->bitmap_info.offset)
2204 return -EBUSY;
2205 if (strncmp(buf, "external", 8) == 0)
2206 mddev->bitmap_info.external = 1;
c4ce867f
GR
2207 else if ((strncmp(buf, "internal", 8) == 0) ||
2208 (strncmp(buf, "clustered", 9) == 0))
ece5cff0
N
2209 mddev->bitmap_info.external = 0;
2210 else
2211 return -EINVAL;
2212 return len;
2213}
2214
2215static struct md_sysfs_entry bitmap_metadata =
2216__ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2217
fd01b88c 2218static ssize_t can_clear_show(struct mddev *mddev, char *page)
ece5cff0
N
2219{
2220 int len;
b7b17c9b 2221 spin_lock(&mddev->lock);
ece5cff0
N
2222 if (mddev->bitmap)
2223 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
2224 "false" : "true"));
2225 else
2226 len = sprintf(page, "\n");
b7b17c9b 2227 spin_unlock(&mddev->lock);
ece5cff0
N
2228 return len;
2229}
2230
fd01b88c 2231static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
ece5cff0
N
2232{
2233 if (mddev->bitmap == NULL)
2234 return -ENOENT;
2235 if (strncmp(buf, "false", 5) == 0)
2236 mddev->bitmap->need_sync = 1;
2237 else if (strncmp(buf, "true", 4) == 0) {
2238 if (mddev->degraded)
2239 return -EBUSY;
2240 mddev->bitmap->need_sync = 0;
2241 } else
2242 return -EINVAL;
2243 return len;
2244}
2245
2246static struct md_sysfs_entry bitmap_can_clear =
2247__ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
2248
696fcd53 2249static ssize_t
fd01b88c 2250behind_writes_used_show(struct mddev *mddev, char *page)
696fcd53 2251{
b7b17c9b
N
2252 ssize_t ret;
2253 spin_lock(&mddev->lock);
696fcd53 2254 if (mddev->bitmap == NULL)
b7b17c9b
N
2255 ret = sprintf(page, "0\n");
2256 else
2257 ret = sprintf(page, "%lu\n",
2258 mddev->bitmap->behind_writes_used);
2259 spin_unlock(&mddev->lock);
2260 return ret;
696fcd53
PC
2261}
2262
2263static ssize_t
fd01b88c 2264behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
696fcd53
PC
2265{
2266 if (mddev->bitmap)
2267 mddev->bitmap->behind_writes_used = 0;
2268 return len;
2269}
2270
2271static struct md_sysfs_entry max_backlog_used =
2272__ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2273 behind_writes_used_show, behind_writes_used_reset);
2274
43a70507
N
2275static struct attribute *md_bitmap_attrs[] = {
2276 &bitmap_location.attr,
6409bb05 2277 &bitmap_space.attr,
43a70507
N
2278 &bitmap_timeout.attr,
2279 &bitmap_backlog.attr,
2280 &bitmap_chunksize.attr,
ece5cff0
N
2281 &bitmap_metadata.attr,
2282 &bitmap_can_clear.attr,
696fcd53 2283 &max_backlog_used.attr,
43a70507
N
2284 NULL
2285};
2286struct attribute_group md_bitmap_group = {
2287 .name = "bitmap",
2288 .attrs = md_bitmap_attrs,
2289};
2290