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