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