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