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