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