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