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