[PATCH] md: support write-mostly device in raid1
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / md / bitmap.c
CommitLineData
32a7627c
N
1/*
2 * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
3 *
4 * bitmap_create - sets up the bitmap structure
5 * bitmap_destroy - destroys the bitmap structure
6 *
7 * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.:
8 * - added disk storage for bitmap
9 * - changes to allow various bitmap chunk sizes
10 * - added bitmap daemon (to asynchronously clear bitmap bits from disk)
11 */
12
13/*
14 * Still to do:
15 *
16 * flush after percent set rather than just time based. (maybe both).
17 * wait if count gets too high, wake when it drops to half.
18 * allow bitmap to be mirrored with superblock (before or after...)
19 * allow hot-add to re-instate a current device.
20 * allow hot-add of bitmap after quiessing device
21 */
22
23#include <linux/module.h>
24#include <linux/version.h>
25#include <linux/errno.h>
26#include <linux/slab.h>
27#include <linux/init.h>
28#include <linux/config.h>
29#include <linux/timer.h>
30#include <linux/sched.h>
31#include <linux/list.h>
32#include <linux/file.h>
33#include <linux/mount.h>
34#include <linux/buffer_head.h>
35#include <linux/raid/md.h>
36#include <linux/raid/bitmap.h>
37
38/* debug macros */
39
40#define DEBUG 0
41
42#if DEBUG
43/* these are for debugging purposes only! */
44
45/* define one and only one of these */
46#define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
47#define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
48#define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
49#define INJECT_FAULTS_4 0 /* undef */
50#define INJECT_FAULTS_5 0 /* undef */
51#define INJECT_FAULTS_6 0
52
53/* if these are defined, the driver will fail! debug only */
54#define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
55#define INJECT_FATAL_FAULT_2 0 /* undef */
56#define INJECT_FATAL_FAULT_3 0 /* undef */
57#endif
58
59//#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
60#define DPRINTK(x...) do { } while(0)
61
62#ifndef PRINTK
63# if DEBUG > 0
64# define PRINTK(x...) printk(KERN_DEBUG x)
65# else
66# define PRINTK(x...)
67# endif
68#endif
69
70static inline char * bmname(struct bitmap *bitmap)
71{
72 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
73}
74
75
76/*
77 * test if the bitmap is active
78 */
79int bitmap_active(struct bitmap *bitmap)
80{
81 unsigned long flags;
82 int res = 0;
83
84 if (!bitmap)
85 return res;
86 spin_lock_irqsave(&bitmap->lock, flags);
87 res = bitmap->flags & BITMAP_ACTIVE;
88 spin_unlock_irqrestore(&bitmap->lock, flags);
89 return res;
90}
91
92#define WRITE_POOL_SIZE 256
93/* mempool for queueing pending writes on the bitmap file */
94static void *write_pool_alloc(unsigned int gfp_flags, void *data)
95{
96 return kmalloc(sizeof(struct page_list), gfp_flags);
97}
98
99static void write_pool_free(void *ptr, void *data)
100{
101 kfree(ptr);
102}
103
104/*
105 * just a placeholder - calls kmalloc for bitmap pages
106 */
107static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
108{
109 unsigned char *page;
110
44456d37 111#ifdef INJECT_FAULTS_1
32a7627c
N
112 page = NULL;
113#else
114 page = kmalloc(PAGE_SIZE, GFP_NOIO);
115#endif
116 if (!page)
117 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
118 else
a654b9d8 119 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
32a7627c
N
120 bmname(bitmap), page);
121 return page;
122}
123
124/*
125 * for now just a placeholder -- just calls kfree for bitmap pages
126 */
127static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
128{
129 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
130 kfree(page);
131}
132
133/*
134 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
135 *
136 * 1) check to see if this page is allocated, if it's not then try to alloc
137 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
138 * page pointer directly as a counter
139 *
140 * if we find our page, we increment the page's refcount so that it stays
141 * allocated while we're using it
142 */
143static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
144{
145 unsigned char *mappage;
146
147 if (page >= bitmap->pages) {
148 printk(KERN_ALERT
149 "%s: invalid bitmap page request: %lu (> %lu)\n",
150 bmname(bitmap), page, bitmap->pages-1);
151 return -EINVAL;
152 }
153
154
155 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
156 return 0;
157
158 if (bitmap->bp[page].map) /* page is already allocated, just return */
159 return 0;
160
161 if (!create)
162 return -ENOENT;
163
164 spin_unlock_irq(&bitmap->lock);
165
166 /* this page has not been allocated yet */
167
168 if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
169 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
170 bmname(bitmap));
171 /* failed - set the hijacked flag so that we can use the
172 * pointer as a counter */
173 spin_lock_irq(&bitmap->lock);
174 if (!bitmap->bp[page].map)
175 bitmap->bp[page].hijacked = 1;
176 goto out;
177 }
178
179 /* got a page */
180
181 spin_lock_irq(&bitmap->lock);
182
183 /* recheck the page */
184
185 if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
186 /* somebody beat us to getting the page */
187 bitmap_free_page(bitmap, mappage);
188 return 0;
189 }
190
191 /* no page was in place and we have one, so install it */
192
193 memset(mappage, 0, PAGE_SIZE);
194 bitmap->bp[page].map = mappage;
195 bitmap->missing_pages--;
196out:
197 return 0;
198}
199
200
201/* if page is completely empty, put it back on the free list, or dealloc it */
202/* if page was hijacked, unmark the flag so it might get alloced next time */
203/* Note: lock should be held when calling this */
204static inline void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
205{
206 char *ptr;
207
208 if (bitmap->bp[page].count) /* page is still busy */
209 return;
210
211 /* page is no longer in use, it can be released */
212
213 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
214 bitmap->bp[page].hijacked = 0;
215 bitmap->bp[page].map = NULL;
216 return;
217 }
218
219 /* normal case, free the page */
220
221#if 0
222/* actually ... let's not. We will probably need the page again exactly when
223 * memory is tight and we are flusing to disk
224 */
225 return;
226#else
227 ptr = bitmap->bp[page].map;
228 bitmap->bp[page].map = NULL;
229 bitmap->missing_pages++;
230 bitmap_free_page(bitmap, ptr);
231 return;
232#endif
233}
234
235
236/*
237 * bitmap file handling - read and write the bitmap file and its superblock
238 */
239
240/* copy the pathname of a file to a buffer */
241char *file_path(struct file *file, char *buf, int count)
242{
243 struct dentry *d;
244 struct vfsmount *v;
245
246 if (!buf)
247 return NULL;
248
249 d = file->f_dentry;
250 v = file->f_vfsmnt;
251
252 buf = d_path(d, v, buf, count);
253
254 return IS_ERR(buf) ? NULL : buf;
255}
256
257/*
258 * basic page I/O operations
259 */
260
a654b9d8
N
261/* IO operations when bitmap is stored near all superblocks */
262static struct page *read_sb_page(mddev_t *mddev, long offset, unsigned long index)
263{
264 /* choose a good rdev and read the page from there */
265
266 mdk_rdev_t *rdev;
267 struct list_head *tmp;
268 struct page *page = alloc_page(GFP_KERNEL);
269 sector_t target;
270
271 if (!page)
272 return ERR_PTR(-ENOMEM);
273 do {
274 ITERATE_RDEV(mddev, rdev, tmp)
275 if (rdev->in_sync && !rdev->faulty)
276 goto found;
277 return ERR_PTR(-EIO);
278
279 found:
280 target = (rdev->sb_offset << 1) + offset + index * (PAGE_SIZE/512);
281
282 } while (!sync_page_io(rdev->bdev, target, PAGE_SIZE, page, READ));
283
284 page->index = index;
285 return page;
286}
287
288static int write_sb_page(mddev_t *mddev, long offset, struct page *page, int wait)
289{
290 mdk_rdev_t *rdev;
291 struct list_head *tmp;
292
293 ITERATE_RDEV(mddev, rdev, tmp)
294 if (rdev->in_sync && !rdev->faulty)
295 md_super_write(mddev, rdev,
296 (rdev->sb_offset<<1) + offset
297 + page->index * (PAGE_SIZE/512),
298 PAGE_SIZE,
299 page);
300
301 if (wait)
302 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
303 return 0;
304}
305
32a7627c 306/*
a654b9d8 307 * write out a page to a file
32a7627c 308 */
77ad4bc7 309static int write_page(struct bitmap *bitmap, struct page *page, int wait)
32a7627c
N
310{
311 int ret = -ENOMEM;
312
a654b9d8
N
313 if (bitmap->file == NULL)
314 return write_sb_page(bitmap->mddev, bitmap->offset, page, wait);
315
8a5e9cf1
N
316 if (wait)
317 lock_page(page);
318 else {
319 if (TestSetPageLocked(page))
320 return -EAGAIN; /* already locked */
321 if (PageWriteback(page)) {
322 unlock_page(page);
323 return -EAGAIN;
324 }
325 }
32a7627c 326
32a7627c
N
327 ret = page->mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
328 if (!ret)
329 ret = page->mapping->a_ops->commit_write(NULL, page, 0,
330 PAGE_SIZE);
331 if (ret) {
32a7627c
N
332 unlock_page(page);
333 return ret;
334 }
335
336 set_page_dirty(page); /* force it to be written out */
77ad4bc7
N
337
338 if (!wait) {
339 /* add to list to be waited for by daemon */
340 struct page_list *item = mempool_alloc(bitmap->write_pool, GFP_NOIO);
341 item->page = page;
342 page_cache_get(page);
343 spin_lock(&bitmap->write_lock);
344 list_add(&item->list, &bitmap->complete_pages);
345 spin_unlock(&bitmap->write_lock);
346 md_wakeup_thread(bitmap->writeback_daemon);
347 }
32a7627c
N
348 return write_one_page(page, wait);
349}
350
351/* read a page from a file, pinning it into cache, and return bytes_read */
352static struct page *read_page(struct file *file, unsigned long index,
353 unsigned long *bytes_read)
354{
355 struct inode *inode = file->f_mapping->host;
356 struct page *page = NULL;
357 loff_t isize = i_size_read(inode);
358 unsigned long end_index = isize >> PAGE_CACHE_SHIFT;
359
360 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_CACHE_SIZE,
361 (unsigned long long)index << PAGE_CACHE_SHIFT);
362
363 page = read_cache_page(inode->i_mapping, index,
364 (filler_t *)inode->i_mapping->a_ops->readpage, file);
365 if (IS_ERR(page))
366 goto out;
367 wait_on_page_locked(page);
368 if (!PageUptodate(page) || PageError(page)) {
369 page_cache_release(page);
370 page = ERR_PTR(-EIO);
371 goto out;
372 }
373
374 if (index > end_index) /* we have read beyond EOF */
375 *bytes_read = 0;
376 else if (index == end_index) /* possible short read */
377 *bytes_read = isize & ~PAGE_CACHE_MASK;
378 else
379 *bytes_read = PAGE_CACHE_SIZE; /* got a full page */
380out:
381 if (IS_ERR(page))
382 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
383 (int)PAGE_CACHE_SIZE,
384 (unsigned long long)index << PAGE_CACHE_SHIFT,
385 PTR_ERR(page));
386 return page;
387}
388
389/*
390 * bitmap file superblock operations
391 */
392
393/* update the event counter and sync the superblock to disk */
394int bitmap_update_sb(struct bitmap *bitmap)
395{
396 bitmap_super_t *sb;
397 unsigned long flags;
398
399 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
400 return 0;
401 spin_lock_irqsave(&bitmap->lock, flags);
402 if (!bitmap->sb_page) { /* no superblock */
403 spin_unlock_irqrestore(&bitmap->lock, flags);
404 return 0;
405 }
32a7627c
N
406 spin_unlock_irqrestore(&bitmap->lock, flags);
407 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
408 sb->events = cpu_to_le64(bitmap->mddev->events);
409 if (!bitmap->mddev->degraded)
410 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
411 kunmap(bitmap->sb_page);
8a5e9cf1 412 return write_page(bitmap, bitmap->sb_page, 1);
32a7627c
N
413}
414
415/* print out the bitmap file superblock */
416void bitmap_print_sb(struct bitmap *bitmap)
417{
418 bitmap_super_t *sb;
419
420 if (!bitmap || !bitmap->sb_page)
421 return;
422 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
423 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
a2cff26a
N
424 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
425 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
426 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
32a7627c
N
427 *(__u32 *)(sb->uuid+0),
428 *(__u32 *)(sb->uuid+4),
429 *(__u32 *)(sb->uuid+8),
430 *(__u32 *)(sb->uuid+12));
a2cff26a 431 printk(KERN_DEBUG " events: %llu\n",
32a7627c 432 (unsigned long long) le64_to_cpu(sb->events));
a2cff26a 433 printk(KERN_DEBUG "events cleared: %llu\n",
32a7627c 434 (unsigned long long) le64_to_cpu(sb->events_cleared));
a2cff26a
N
435 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
436 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
437 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
438 printk(KERN_DEBUG " sync size: %llu KB\n",
439 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
32a7627c
N
440 kunmap(bitmap->sb_page);
441}
442
443/* read the superblock from the bitmap file and initialize some bitmap fields */
444static int bitmap_read_sb(struct bitmap *bitmap)
445{
446 char *reason = NULL;
447 bitmap_super_t *sb;
448 unsigned long chunksize, daemon_sleep;
449 unsigned long bytes_read;
450 unsigned long long events;
451 int err = -EINVAL;
452
453 /* page 0 is the superblock, read it... */
a654b9d8
N
454 if (bitmap->file)
455 bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read);
456 else {
457 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
458 bytes_read = PAGE_SIZE;
459 }
32a7627c
N
460 if (IS_ERR(bitmap->sb_page)) {
461 err = PTR_ERR(bitmap->sb_page);
462 bitmap->sb_page = NULL;
463 return err;
464 }
465
466 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
467
468 if (bytes_read < sizeof(*sb)) { /* short read */
469 printk(KERN_INFO "%s: bitmap file superblock truncated\n",
470 bmname(bitmap));
471 err = -ENOSPC;
472 goto out;
473 }
474
475 chunksize = le32_to_cpu(sb->chunksize);
476 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
477
478 /* verify that the bitmap-specific fields are valid */
479 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
480 reason = "bad magic";
481 else if (sb->version != cpu_to_le32(BITMAP_MAJOR))
482 reason = "unrecognized superblock version";
483 else if (chunksize < 512 || chunksize > (1024 * 1024 * 4))
484 reason = "bitmap chunksize out of range (512B - 4MB)";
485 else if ((1 << ffz(~chunksize)) != chunksize)
486 reason = "bitmap chunksize not a power of 2";
487 else if (daemon_sleep < 1 || daemon_sleep > 15)
488 reason = "daemon sleep period out of range";
489 if (reason) {
490 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
491 bmname(bitmap), reason);
492 goto out;
493 }
494
495 /* keep the array size field of the bitmap superblock up to date */
496 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
497
498 if (!bitmap->mddev->persistent)
499 goto success;
500
501 /*
502 * if we have a persistent array superblock, compare the
503 * bitmap's UUID and event counter to the mddev's
504 */
505 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
506 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
507 bmname(bitmap));
508 goto out;
509 }
510 events = le64_to_cpu(sb->events);
511 if (events < bitmap->mddev->events) {
512 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
513 "-- forcing full recovery\n", bmname(bitmap), events,
514 (unsigned long long) bitmap->mddev->events);
515 sb->state |= BITMAP_STALE;
516 }
517success:
518 /* assign fields using values from superblock */
519 bitmap->chunksize = chunksize;
520 bitmap->daemon_sleep = daemon_sleep;
521 bitmap->flags |= sb->state;
522 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
6a07997f
N
523 if (sb->state & BITMAP_STALE)
524 bitmap->events_cleared = bitmap->mddev->events;
32a7627c
N
525 err = 0;
526out:
527 kunmap(bitmap->sb_page);
528 if (err)
529 bitmap_print_sb(bitmap);
530 return err;
531}
532
533enum bitmap_mask_op {
534 MASK_SET,
535 MASK_UNSET
536};
537
538/* record the state of the bitmap in the superblock */
539static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
540 enum bitmap_mask_op op)
541{
542 bitmap_super_t *sb;
543 unsigned long flags;
544
545 spin_lock_irqsave(&bitmap->lock, flags);
546 if (!bitmap || !bitmap->sb_page) { /* can't set the state */
547 spin_unlock_irqrestore(&bitmap->lock, flags);
548 return;
549 }
550 page_cache_get(bitmap->sb_page);
551 spin_unlock_irqrestore(&bitmap->lock, flags);
552 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
553 switch (op) {
554 case MASK_SET: sb->state |= bits;
555 break;
556 case MASK_UNSET: sb->state &= ~bits;
557 break;
558 default: BUG();
559 }
560 kunmap(bitmap->sb_page);
561 page_cache_release(bitmap->sb_page);
562}
563
564/*
565 * general bitmap file operations
566 */
567
568/* calculate the index of the page that contains this bit */
569static inline unsigned long file_page_index(unsigned long chunk)
570{
571 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
572}
573
574/* calculate the (bit) offset of this bit within a page */
575static inline unsigned long file_page_offset(unsigned long chunk)
576{
577 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
578}
579
580/*
581 * return a pointer to the page in the filemap that contains the given bit
582 *
583 * this lookup is complicated by the fact that the bitmap sb might be exactly
584 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
585 * 0 or page 1
586 */
587static inline struct page *filemap_get_page(struct bitmap *bitmap,
588 unsigned long chunk)
589{
590 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
591}
592
593
594static void bitmap_file_unmap(struct bitmap *bitmap)
595{
596 struct page **map, *sb_page;
597 unsigned long *attr;
598 int pages;
599 unsigned long flags;
600
601 spin_lock_irqsave(&bitmap->lock, flags);
602 map = bitmap->filemap;
603 bitmap->filemap = NULL;
604 attr = bitmap->filemap_attr;
605 bitmap->filemap_attr = NULL;
606 pages = bitmap->file_pages;
607 bitmap->file_pages = 0;
608 sb_page = bitmap->sb_page;
609 bitmap->sb_page = NULL;
610 spin_unlock_irqrestore(&bitmap->lock, flags);
611
612 while (pages--)
613 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
614 page_cache_release(map[pages]);
615 kfree(map);
616 kfree(attr);
617
618 if (sb_page)
619 page_cache_release(sb_page);
620}
621
622static void bitmap_stop_daemons(struct bitmap *bitmap);
623
624/* dequeue the next item in a page list -- don't call from irq context */
77ad4bc7 625static struct page_list *dequeue_page(struct bitmap *bitmap)
32a7627c
N
626{
627 struct page_list *item = NULL;
77ad4bc7 628 struct list_head *head = &bitmap->complete_pages;
32a7627c
N
629
630 spin_lock(&bitmap->write_lock);
631 if (list_empty(head))
632 goto out;
633 item = list_entry(head->prev, struct page_list, list);
634 list_del(head->prev);
635out:
636 spin_unlock(&bitmap->write_lock);
637 return item;
638}
639
640static void drain_write_queues(struct bitmap *bitmap)
641{
32a7627c 642 struct page_list *item;
32a7627c 643
77ad4bc7
N
644 while ((item = dequeue_page(bitmap))) {
645 /* don't bother to wait */
646 page_cache_release(item->page);
647 mempool_free(item, bitmap->write_pool);
32a7627c
N
648 }
649
32a7627c 650 wake_up(&bitmap->write_wait);
32a7627c
N
651}
652
653static void bitmap_file_put(struct bitmap *bitmap)
654{
655 struct file *file;
656 struct inode *inode;
657 unsigned long flags;
658
659 spin_lock_irqsave(&bitmap->lock, flags);
660 file = bitmap->file;
661 bitmap->file = NULL;
662 spin_unlock_irqrestore(&bitmap->lock, flags);
663
664 bitmap_stop_daemons(bitmap);
665
666 drain_write_queues(bitmap);
667
668 bitmap_file_unmap(bitmap);
669
670 if (file) {
671 inode = file->f_mapping->host;
672 spin_lock(&inode->i_lock);
673 atomic_set(&inode->i_writecount, 1); /* allow writes again */
674 spin_unlock(&inode->i_lock);
675 fput(file);
676 }
677}
678
679
680/*
681 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
682 * then it is no longer reliable, so we stop using it and we mark the file
683 * as failed in the superblock
684 */
685static void bitmap_file_kick(struct bitmap *bitmap)
686{
687 char *path, *ptr = NULL;
688
689 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
690 bitmap_update_sb(bitmap);
691
a654b9d8
N
692 if (bitmap->file) {
693 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
694 if (path)
695 ptr = file_path(bitmap->file, path, PAGE_SIZE);
32a7627c 696
a654b9d8
N
697 printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
698 bmname(bitmap), ptr ? ptr : "");
32a7627c 699
a654b9d8
N
700 kfree(path);
701 }
32a7627c
N
702
703 bitmap_file_put(bitmap);
704
705 return;
706}
707
708enum bitmap_page_attr {
709 BITMAP_PAGE_DIRTY = 1, // there are set bits that need to be synced
710 BITMAP_PAGE_CLEAN = 2, // there are bits that might need to be cleared
711 BITMAP_PAGE_NEEDWRITE=4, // there are cleared bits that need to be synced
712};
713
714static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
715 enum bitmap_page_attr attr)
716{
717 bitmap->filemap_attr[page->index] |= attr;
718}
719
720static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
721 enum bitmap_page_attr attr)
722{
723 bitmap->filemap_attr[page->index] &= ~attr;
724}
725
726static inline unsigned long get_page_attr(struct bitmap *bitmap, struct page *page)
727{
728 return bitmap->filemap_attr[page->index];
729}
730
731/*
732 * bitmap_file_set_bit -- called before performing a write to the md device
733 * to set (and eventually sync) a particular bit in the bitmap file
734 *
735 * we set the bit immediately, then we record the page number so that
736 * when an unplug occurs, we can flush the dirty pages out to disk
737 */
738static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
739{
740 unsigned long bit;
741 struct page *page;
742 void *kaddr;
743 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
744
a654b9d8 745 if (!bitmap->filemap) {
32a7627c
N
746 return;
747 }
748
749 page = filemap_get_page(bitmap, chunk);
750 bit = file_page_offset(chunk);
751
752
753 /* make sure the page stays cached until it gets written out */
754 if (! (get_page_attr(bitmap, page) & BITMAP_PAGE_DIRTY))
755 page_cache_get(page);
756
757 /* set the bit */
758 kaddr = kmap_atomic(page, KM_USER0);
759 set_bit(bit, kaddr);
760 kunmap_atomic(kaddr, KM_USER0);
761 PRINTK("set file bit %lu page %lu\n", bit, page->index);
762
763 /* record page number so it gets flushed to disk when unplug occurs */
764 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
765
766}
767
768/* this gets called when the md device is ready to unplug its underlying
769 * (slave) device queues -- before we let any writes go down, we need to
770 * sync the dirty pages of the bitmap file to disk */
771int bitmap_unplug(struct bitmap *bitmap)
772{
773 unsigned long i, attr, flags;
774 struct page *page;
775 int wait = 0;
8a5e9cf1 776 int err;
32a7627c
N
777
778 if (!bitmap)
779 return 0;
780
781 /* look at each page to see if there are any set bits that need to be
782 * flushed out to disk */
783 for (i = 0; i < bitmap->file_pages; i++) {
784 spin_lock_irqsave(&bitmap->lock, flags);
a654b9d8 785 if (!bitmap->filemap) {
32a7627c
N
786 spin_unlock_irqrestore(&bitmap->lock, flags);
787 return 0;
788 }
789 page = bitmap->filemap[i];
790 attr = get_page_attr(bitmap, page);
791 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
792 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
793 if ((attr & BITMAP_PAGE_DIRTY))
794 wait = 1;
795 spin_unlock_irqrestore(&bitmap->lock, flags);
796
8a5e9cf1
N
797 if (attr & (BITMAP_PAGE_DIRTY | BITMAP_PAGE_NEEDWRITE)) {
798 err = write_page(bitmap, page, 0);
799 if (err == -EAGAIN) {
800 if (attr & BITMAP_PAGE_DIRTY)
801 err = write_page(bitmap, page, 1);
802 else
803 err = 0;
804 }
805 if (err)
bfb39fba 806 return 1;
8a5e9cf1 807 }
32a7627c
N
808 }
809 if (wait) { /* if any writes were performed, we need to wait on them */
a654b9d8
N
810 if (bitmap->file) {
811 spin_lock_irq(&bitmap->write_lock);
812 wait_event_lock_irq(bitmap->write_wait,
813 list_empty(&bitmap->complete_pages), bitmap->write_lock,
814 wake_up_process(bitmap->writeback_daemon->tsk));
815 spin_unlock_irq(&bitmap->write_lock);
816 } else
817 wait_event(bitmap->mddev->sb_wait,
818 atomic_read(&bitmap->mddev->pending_writes)==0);
32a7627c
N
819 }
820 return 0;
821}
822
6a07997f 823static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
32a7627c
N
824/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
825 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
826 * memory mapping of the bitmap file
827 * Special cases:
828 * if there's no bitmap file, or if the bitmap file had been
829 * previously kicked from the array, we mark all the bits as
830 * 1's in order to cause a full resync.
6a07997f
N
831 *
832 * We ignore all bits for sectors that end earlier than 'start'.
833 * This is used when reading an out-of-date bitmap...
32a7627c 834 */
6a07997f 835static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
32a7627c
N
836{
837 unsigned long i, chunks, index, oldindex, bit;
838 struct page *page = NULL, *oldpage = NULL;
839 unsigned long num_pages, bit_cnt = 0;
840 struct file *file;
841 unsigned long bytes, offset, dummy;
842 int outofdate;
843 int ret = -ENOSPC;
844
845 chunks = bitmap->chunks;
846 file = bitmap->file;
847
a654b9d8 848 BUG_ON(!file && !bitmap->offset);
32a7627c 849
44456d37 850#ifdef INJECT_FAULTS_3
32a7627c
N
851 outofdate = 1;
852#else
853 outofdate = bitmap->flags & BITMAP_STALE;
854#endif
855 if (outofdate)
856 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
857 "recovery\n", bmname(bitmap));
858
859 bytes = (chunks + 7) / 8;
bc7f77de 860
cdbb4cc2 861 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
bc7f77de 862
a654b9d8 863 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
32a7627c
N
864 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
865 bmname(bitmap),
866 (unsigned long) i_size_read(file->f_mapping->host),
867 bytes + sizeof(bitmap_super_t));
868 goto out;
869 }
bc7f77de
N
870
871 ret = -ENOMEM;
872
32a7627c 873 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
bc7f77de 874 if (!bitmap->filemap)
32a7627c 875 goto out;
32a7627c
N
876
877 bitmap->filemap_attr = kmalloc(sizeof(long) * num_pages, GFP_KERNEL);
bc7f77de 878 if (!bitmap->filemap_attr)
32a7627c 879 goto out;
32a7627c
N
880
881 memset(bitmap->filemap_attr, 0, sizeof(long) * num_pages);
882
883 oldindex = ~0L;
884
885 for (i = 0; i < chunks; i++) {
886 index = file_page_index(i);
887 bit = file_page_offset(i);
888 if (index != oldindex) { /* this is a new page, read it in */
889 /* unmap the old page, we're done with it */
890 if (oldpage != NULL)
891 kunmap(oldpage);
892 if (index == 0) {
893 /*
894 * if we're here then the superblock page
895 * contains some bits (PAGE_SIZE != sizeof sb)
896 * we've already read it in, so just use it
897 */
898 page = bitmap->sb_page;
899 offset = sizeof(bitmap_super_t);
a654b9d8 900 } else if (file) {
32a7627c 901 page = read_page(file, index, &dummy);
a654b9d8
N
902 offset = 0;
903 } else {
904 page = read_sb_page(bitmap->mddev, bitmap->offset, index);
32a7627c
N
905 offset = 0;
906 }
a654b9d8
N
907 if (IS_ERR(page)) { /* read error */
908 ret = PTR_ERR(page);
909 goto out;
910 }
911
32a7627c
N
912 oldindex = index;
913 oldpage = page;
914 kmap(page);
915
916 if (outofdate) {
917 /*
918 * if bitmap is out of date, dirty the
919 * whole page and write it out
920 */
921 memset(page_address(page) + offset, 0xff,
6a07997f 922 PAGE_SIZE - offset);
77ad4bc7 923 ret = write_page(bitmap, page, 1);
32a7627c
N
924 if (ret) {
925 kunmap(page);
926 /* release, page not in filemap yet */
927 page_cache_release(page);
928 goto out;
929 }
930 }
931
932 bitmap->filemap[bitmap->file_pages++] = page;
933 }
934 if (test_bit(bit, page_address(page))) {
935 /* if the disk bit is set, set the memory bit */
6a07997f
N
936 bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
937 ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
938 );
32a7627c 939 bit_cnt++;
6a07997f 940 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
32a7627c 941 }
32a7627c
N
942 }
943
944 /* everything went OK */
945 ret = 0;
946 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
947
948 if (page) /* unmap the last page */
949 kunmap(page);
950
951 if (bit_cnt) { /* Kick recovery if any bits were set */
952 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
953 md_wakeup_thread(bitmap->mddev->thread);
954 }
955
956out:
957 printk(KERN_INFO "%s: bitmap initialized from disk: "
958 "read %lu/%lu pages, set %lu bits, status: %d\n",
959 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
960
961 return ret;
962}
963
a654b9d8
N
964void bitmap_write_all(struct bitmap *bitmap)
965{
966 /* We don't actually write all bitmap blocks here,
967 * just flag them as needing to be written
968 */
969
970 unsigned long chunks = bitmap->chunks;
971 unsigned long bytes = (chunks+7)/8 + sizeof(bitmap_super_t);
972 unsigned long num_pages = (bytes + PAGE_SIZE-1) / PAGE_SIZE;
973 while (num_pages--)
974 bitmap->filemap_attr[num_pages] |= BITMAP_PAGE_NEEDWRITE;
975}
976
32a7627c
N
977
978static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
979{
980 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
981 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
982 bitmap->bp[page].count += inc;
983/*
984 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
985 (unsigned long long)offset, inc, bitmap->bp[page].count);
986*/
987 bitmap_checkfree(bitmap, page);
988}
989static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
990 sector_t offset, int *blocks,
991 int create);
992
993/*
994 * bitmap daemon -- periodically wakes up to clean bits and flush pages
995 * out to disk
996 */
997
998int bitmap_daemon_work(struct bitmap *bitmap)
999{
aa3163f8 1000 unsigned long j;
32a7627c
N
1001 unsigned long flags;
1002 struct page *page = NULL, *lastpage = NULL;
1003 int err = 0;
1004 int blocks;
1005 int attr;
1006
1007 if (bitmap == NULL)
1008 return 0;
1009 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
1010 return 0;
1011 bitmap->daemon_lastrun = jiffies;
1012
1013 for (j = 0; j < bitmap->chunks; j++) {
1014 bitmap_counter_t *bmc;
1015 spin_lock_irqsave(&bitmap->lock, flags);
a654b9d8 1016 if (!bitmap->filemap) {
32a7627c
N
1017 /* error or shutdown */
1018 spin_unlock_irqrestore(&bitmap->lock, flags);
1019 break;
1020 }
1021
1022 page = filemap_get_page(bitmap, j);
32a7627c
N
1023
1024 if (page != lastpage) {
aa3163f8
N
1025 /* skip this page unless it's marked as needing cleaning */
1026 if (!((attr=get_page_attr(bitmap, page)) & BITMAP_PAGE_CLEAN)) {
1027 if (attr & BITMAP_PAGE_NEEDWRITE) {
1028 page_cache_get(page);
1029 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1030 }
1031 spin_unlock_irqrestore(&bitmap->lock, flags);
1032 if (attr & BITMAP_PAGE_NEEDWRITE) {
8a5e9cf1
N
1033 switch (write_page(bitmap, page, 0)) {
1034 case -EAGAIN:
1035 set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1036 break;
1037 case 0:
1038 break;
1039 default:
aa3163f8 1040 bitmap_file_kick(bitmap);
8a5e9cf1 1041 }
aa3163f8
N
1042 page_cache_release(page);
1043 }
1044 continue;
1045 }
1046
32a7627c
N
1047 /* grab the new page, sync and release the old */
1048 page_cache_get(page);
1049 if (lastpage != NULL) {
1050 if (get_page_attr(bitmap, lastpage) & BITMAP_PAGE_NEEDWRITE) {
1051 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1052 spin_unlock_irqrestore(&bitmap->lock, flags);
77ad4bc7 1053 err = write_page(bitmap, lastpage, 0);
8a5e9cf1
N
1054 if (err == -EAGAIN) {
1055 err = 0;
1056 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1057 }
32a7627c
N
1058 } else {
1059 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1060 spin_unlock_irqrestore(&bitmap->lock, flags);
1061 }
1062 kunmap(lastpage);
1063 page_cache_release(lastpage);
1064 if (err)
1065 bitmap_file_kick(bitmap);
1066 } else
1067 spin_unlock_irqrestore(&bitmap->lock, flags);
1068 lastpage = page;
1069 kmap(page);
1070/*
1071 printk("bitmap clean at page %lu\n", j);
1072*/
1073 spin_lock_irqsave(&bitmap->lock, flags);
1074 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1075 }
1076 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1077 &blocks, 0);
1078 if (bmc) {
1079/*
1080 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1081*/
1082 if (*bmc == 2) {
1083 *bmc=1; /* maybe clear the bit next time */
1084 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1085 } else if (*bmc == 1) {
1086 /* we can clear the bit */
1087 *bmc = 0;
1088 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1089 -1);
1090
1091 /* clear the bit */
aa3163f8 1092 clear_bit(file_page_offset(j), page_address(page));
32a7627c
N
1093 }
1094 }
1095 spin_unlock_irqrestore(&bitmap->lock, flags);
1096 }
1097
1098 /* now sync the final page */
1099 if (lastpage != NULL) {
1100 kunmap(lastpage);
1101 spin_lock_irqsave(&bitmap->lock, flags);
1102 if (get_page_attr(bitmap, lastpage) &BITMAP_PAGE_NEEDWRITE) {
1103 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1104 spin_unlock_irqrestore(&bitmap->lock, flags);
77ad4bc7 1105 err = write_page(bitmap, lastpage, 0);
8a5e9cf1
N
1106 if (err == -EAGAIN) {
1107 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1108 err = 0;
1109 }
32a7627c
N
1110 } else {
1111 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1112 spin_unlock_irqrestore(&bitmap->lock, flags);
1113 }
1114
1115 page_cache_release(lastpage);
1116 }
1117
1118 return err;
1119}
1120
1121static void daemon_exit(struct bitmap *bitmap, mdk_thread_t **daemon)
1122{
1123 mdk_thread_t *dmn;
1124 unsigned long flags;
1125
1126 /* if no one is waiting on us, we'll free the md thread struct
1127 * and exit, otherwise we let the waiter clean things up */
1128 spin_lock_irqsave(&bitmap->lock, flags);
1129 if ((dmn = *daemon)) { /* no one is waiting, cleanup and exit */
1130 *daemon = NULL;
1131 spin_unlock_irqrestore(&bitmap->lock, flags);
1132 kfree(dmn);
1133 complete_and_exit(NULL, 0); /* do_exit not exported */
1134 }
1135 spin_unlock_irqrestore(&bitmap->lock, flags);
1136}
1137
1138static void bitmap_writeback_daemon(mddev_t *mddev)
1139{
1140 struct bitmap *bitmap = mddev->bitmap;
1141 struct page *page;
1142 struct page_list *item;
1143 int err = 0;
1144
77ad4bc7
N
1145 if (signal_pending(current)) {
1146 printk(KERN_INFO
1147 "%s: bitmap writeback daemon got signal, exiting...\n",
1148 bmname(bitmap));
1149 err = -EINTR;
1150 goto out;
1151 }
32a7627c 1152
77ad4bc7
N
1153 PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
1154 /* wait on bitmap page writebacks */
1155 while ((item = dequeue_page(bitmap))) {
1156 page = item->page;
1157 mempool_free(item, bitmap->write_pool);
1158 PRINTK("wait on page writeback: %p\n", page);
1159 wait_on_page_writeback(page);
1160 PRINTK("finished page writeback: %p\n", page);
1161
1162 err = PageError(page);
1163 page_cache_release(page);
1164 if (err) {
1165 printk(KERN_WARNING "%s: bitmap file writeback "
1166 "failed (page %lu): %d\n",
1167 bmname(bitmap), page->index, err);
1168 bitmap_file_kick(bitmap);
1169 goto out;
32a7627c
N
1170 }
1171 }
77ad4bc7
N
1172 out:
1173 wake_up(&bitmap->write_wait);
32a7627c
N
1174 if (err) {
1175 printk(KERN_INFO "%s: bitmap writeback daemon exiting (%d)\n",
77ad4bc7 1176 bmname(bitmap), err);
32a7627c
N
1177 daemon_exit(bitmap, &bitmap->writeback_daemon);
1178 }
32a7627c
N
1179}
1180
1181static int bitmap_start_daemon(struct bitmap *bitmap, mdk_thread_t **ptr,
1182 void (*func)(mddev_t *), char *name)
1183{
1184 mdk_thread_t *daemon;
1185 unsigned long flags;
1186 char namebuf[32];
1187
1188 spin_lock_irqsave(&bitmap->lock, flags);
1189 *ptr = NULL;
a654b9d8 1190
32a7627c
N
1191 if (!bitmap->file) /* no need for daemon if there's no backing file */
1192 goto out_unlock;
1193
1194 spin_unlock_irqrestore(&bitmap->lock, flags);
1195
44456d37 1196#ifdef INJECT_FATAL_FAULT_2
32a7627c
N
1197 daemon = NULL;
1198#else
1199 sprintf(namebuf, "%%s_%s", name);
1200 daemon = md_register_thread(func, bitmap->mddev, namebuf);
1201#endif
1202 if (!daemon) {
1203 printk(KERN_ERR "%s: failed to start bitmap daemon\n",
1204 bmname(bitmap));
1205 return -ECHILD;
1206 }
1207
1208 spin_lock_irqsave(&bitmap->lock, flags);
1209 *ptr = daemon;
1210
1211 md_wakeup_thread(daemon); /* start it running */
1212
1213 PRINTK("%s: %s daemon (pid %d) started...\n",
d80a138c 1214 bmname(bitmap), name, daemon->tsk->pid);
32a7627c
N
1215out_unlock:
1216 spin_unlock_irqrestore(&bitmap->lock, flags);
1217 return 0;
1218}
1219
1220static int bitmap_start_daemons(struct bitmap *bitmap)
1221{
1222 int err = bitmap_start_daemon(bitmap, &bitmap->writeback_daemon,
1223 bitmap_writeback_daemon, "bitmap_wb");
1224 return err;
1225}
1226
1227static void bitmap_stop_daemon(struct bitmap *bitmap, mdk_thread_t **ptr)
1228{
1229 mdk_thread_t *daemon;
1230 unsigned long flags;
1231
1232 spin_lock_irqsave(&bitmap->lock, flags);
1233 daemon = *ptr;
1234 *ptr = NULL;
1235 spin_unlock_irqrestore(&bitmap->lock, flags);
1236 if (daemon)
1237 md_unregister_thread(daemon); /* destroy the thread */
1238}
1239
1240static void bitmap_stop_daemons(struct bitmap *bitmap)
1241{
1242 /* the daemons can't stop themselves... they'll just exit instead... */
1243 if (bitmap->writeback_daemon &&
1244 current->pid != bitmap->writeback_daemon->tsk->pid)
1245 bitmap_stop_daemon(bitmap, &bitmap->writeback_daemon);
1246}
1247
1248static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1249 sector_t offset, int *blocks,
1250 int create)
1251{
1252 /* If 'create', we might release the lock and reclaim it.
1253 * The lock must have been taken with interrupts enabled.
1254 * If !create, we don't release the lock.
1255 */
1256 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1257 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1258 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1259 sector_t csize;
1260
1261 if (bitmap_checkpage(bitmap, page, create) < 0) {
1262 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1263 *blocks = csize - (offset & (csize- 1));
1264 return NULL;
1265 }
1266 /* now locked ... */
1267
1268 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1269 /* should we use the first or second counter field
1270 * of the hijacked pointer? */
1271 int hi = (pageoff > PAGE_COUNTER_MASK);
1272 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1273 PAGE_COUNTER_SHIFT - 1);
1274 *blocks = csize - (offset & (csize- 1));
1275 return &((bitmap_counter_t *)
1276 &bitmap->bp[page].map)[hi];
1277 } else { /* page is allocated */
1278 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1279 *blocks = csize - (offset & (csize- 1));
1280 return (bitmap_counter_t *)
1281 &(bitmap->bp[page].map[pageoff]);
1282 }
1283}
1284
1285int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors)
1286{
1287 if (!bitmap) return 0;
1288 while (sectors) {
1289 int blocks;
1290 bitmap_counter_t *bmc;
1291
1292 spin_lock_irq(&bitmap->lock);
1293 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1294 if (!bmc) {
1295 spin_unlock_irq(&bitmap->lock);
1296 return 0;
1297 }
1298
1299 switch(*bmc) {
1300 case 0:
1301 bitmap_file_set_bit(bitmap, offset);
1302 bitmap_count_page(bitmap,offset, 1);
1303 blk_plug_device(bitmap->mddev->queue);
1304 /* fall through */
1305 case 1:
1306 *bmc = 2;
1307 }
1308 if ((*bmc & COUNTER_MAX) == COUNTER_MAX) BUG();
1309 (*bmc)++;
1310
1311 spin_unlock_irq(&bitmap->lock);
1312
1313 offset += blocks;
1314 if (sectors > blocks)
1315 sectors -= blocks;
1316 else sectors = 0;
1317 }
1318 return 0;
1319}
1320
1321void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
1322 int success)
1323{
1324 if (!bitmap) return;
1325 while (sectors) {
1326 int blocks;
1327 unsigned long flags;
1328 bitmap_counter_t *bmc;
1329
1330 spin_lock_irqsave(&bitmap->lock, flags);
1331 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1332 if (!bmc) {
1333 spin_unlock_irqrestore(&bitmap->lock, flags);
1334 return;
1335 }
1336
1337 if (!success && ! (*bmc & NEEDED_MASK))
1338 *bmc |= NEEDED_MASK;
1339
1340 (*bmc)--;
1341 if (*bmc <= 2) {
1342 set_page_attr(bitmap,
1343 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1344 BITMAP_PAGE_CLEAN);
1345 }
1346 spin_unlock_irqrestore(&bitmap->lock, flags);
1347 offset += blocks;
1348 if (sectors > blocks)
1349 sectors -= blocks;
1350 else sectors = 0;
1351 }
1352}
1353
6a806c51
N
1354int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1355 int degraded)
32a7627c
N
1356{
1357 bitmap_counter_t *bmc;
1358 int rv;
1359 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1360 *blocks = 1024;
1361 return 1; /* always resync if no bitmap */
1362 }
1363 spin_lock_irq(&bitmap->lock);
1364 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1365 rv = 0;
1366 if (bmc) {
1367 /* locked */
1368 if (RESYNC(*bmc))
1369 rv = 1;
1370 else if (NEEDED(*bmc)) {
1371 rv = 1;
6a806c51
N
1372 if (!degraded) { /* don't set/clear bits if degraded */
1373 *bmc |= RESYNC_MASK;
1374 *bmc &= ~NEEDED_MASK;
1375 }
32a7627c
N
1376 }
1377 }
1378 spin_unlock_irq(&bitmap->lock);
1379 return rv;
1380}
1381
1382void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1383{
1384 bitmap_counter_t *bmc;
1385 unsigned long flags;
1386/*
1387 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1388*/ if (bitmap == NULL) {
1389 *blocks = 1024;
1390 return;
1391 }
1392 spin_lock_irqsave(&bitmap->lock, flags);
1393 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1394 if (bmc == NULL)
1395 goto unlock;
1396 /* locked */
1397/*
1398 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1399*/
1400 if (RESYNC(*bmc)) {
1401 *bmc &= ~RESYNC_MASK;
1402
1403 if (!NEEDED(*bmc) && aborted)
1404 *bmc |= NEEDED_MASK;
1405 else {
1406 if (*bmc <= 2) {
1407 set_page_attr(bitmap,
1408 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1409 BITMAP_PAGE_CLEAN);
1410 }
1411 }
1412 }
1413 unlock:
1414 spin_unlock_irqrestore(&bitmap->lock, flags);
1415}
1416
1417void bitmap_close_sync(struct bitmap *bitmap)
1418{
1419 /* Sync has finished, and any bitmap chunks that weren't synced
1420 * properly have been aborted. It remains to us to clear the
1421 * RESYNC bit wherever it is still on
1422 */
1423 sector_t sector = 0;
1424 int blocks;
1425 if (!bitmap) return;
1426 while (sector < bitmap->mddev->resync_max_sectors) {
1427 bitmap_end_sync(bitmap, sector, &blocks, 0);
1428/*
1429 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1430 (unsigned long long)sector, blocks);
1431*/ sector += blocks;
1432 }
1433}
1434
6a07997f 1435static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
32a7627c
N
1436{
1437 /* For each chunk covered by any of these sectors, set the
193f1c93 1438 * counter to 1 and set resync_needed. They should all
32a7627c
N
1439 * be 0 at this point
1440 */
193f1c93
N
1441
1442 int secs;
1443 bitmap_counter_t *bmc;
1444 spin_lock_irq(&bitmap->lock);
1445 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1446 if (!bmc) {
32a7627c 1447 spin_unlock_irq(&bitmap->lock);
193f1c93 1448 return;
32a7627c 1449 }
193f1c93
N
1450 if (! *bmc) {
1451 struct page *page;
6a07997f 1452 *bmc = 1 | (needed?NEEDED_MASK:0);
193f1c93
N
1453 bitmap_count_page(bitmap, offset, 1);
1454 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1455 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1456 }
1457 spin_unlock_irq(&bitmap->lock);
1458
32a7627c
N
1459}
1460
6b8b3e8a
N
1461/*
1462 * flush out any pending updates
1463 */
1464void bitmap_flush(mddev_t *mddev)
1465{
1466 struct bitmap *bitmap = mddev->bitmap;
1467 int sleep;
1468
1469 if (!bitmap) /* there was no bitmap */
1470 return;
1471
1472 /* run the daemon_work three time to ensure everything is flushed
1473 * that can be
1474 */
1475 sleep = bitmap->daemon_sleep;
1476 bitmap->daemon_sleep = 0;
1477 bitmap_daemon_work(bitmap);
1478 bitmap_daemon_work(bitmap);
1479 bitmap_daemon_work(bitmap);
1480 bitmap->daemon_sleep = sleep;
1481 bitmap_update_sb(bitmap);
1482}
1483
32a7627c
N
1484/*
1485 * free memory that was allocated
1486 */
1487void bitmap_destroy(mddev_t *mddev)
1488{
1489 unsigned long k, pages;
1490 struct bitmap_page *bp;
1491 struct bitmap *bitmap = mddev->bitmap;
1492
1493 if (!bitmap) /* there was no bitmap */
1494 return;
1495
1496 mddev->bitmap = NULL; /* disconnect from the md device */
1497
1498 /* release the bitmap file and kill the daemon */
1499 bitmap_file_put(bitmap);
1500
1501 bp = bitmap->bp;
1502 pages = bitmap->pages;
1503
1504 /* free all allocated memory */
1505
1506 mempool_destroy(bitmap->write_pool);
1507
1508 if (bp) /* deallocate the page memory */
1509 for (k = 0; k < pages; k++)
1510 if (bp[k].map && !bp[k].hijacked)
1511 kfree(bp[k].map);
1512 kfree(bp);
1513 kfree(bitmap);
1514}
1515
1516/*
1517 * initialize the bitmap structure
1518 * if this returns an error, bitmap_destroy must be called to do clean up
1519 */
1520int bitmap_create(mddev_t *mddev)
1521{
1522 struct bitmap *bitmap;
1523 unsigned long blocks = mddev->resync_max_sectors;
1524 unsigned long chunks;
1525 unsigned long pages;
1526 struct file *file = mddev->bitmap_file;
1527 int err;
6a07997f 1528 sector_t start;
32a7627c
N
1529
1530 BUG_ON(sizeof(bitmap_super_t) != 256);
1531
a654b9d8 1532 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
32a7627c
N
1533 return 0;
1534
a654b9d8
N
1535 BUG_ON(file && mddev->bitmap_offset);
1536
32a7627c
N
1537 bitmap = kmalloc(sizeof(*bitmap), GFP_KERNEL);
1538 if (!bitmap)
1539 return -ENOMEM;
1540
1541 memset(bitmap, 0, sizeof(*bitmap));
1542
1543 spin_lock_init(&bitmap->lock);
1544 bitmap->mddev = mddev;
1545 mddev->bitmap = bitmap;
1546
1547 spin_lock_init(&bitmap->write_lock);
32a7627c
N
1548 INIT_LIST_HEAD(&bitmap->complete_pages);
1549 init_waitqueue_head(&bitmap->write_wait);
1550 bitmap->write_pool = mempool_create(WRITE_POOL_SIZE, write_pool_alloc,
1551 write_pool_free, NULL);
1552 if (!bitmap->write_pool)
1553 return -ENOMEM;
1554
1555 bitmap->file = file;
a654b9d8
N
1556 bitmap->offset = mddev->bitmap_offset;
1557 if (file) get_file(file);
32a7627c
N
1558 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1559 err = bitmap_read_sb(bitmap);
1560 if (err)
1561 return err;
1562
1563 bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
1564 sizeof(bitmap->chunksize));
1565
1566 /* now that chunksize and chunkshift are set, we can use these macros */
1567 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1568 CHUNK_BLOCK_RATIO(bitmap);
1569 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1570
1571 BUG_ON(!pages);
1572
1573 bitmap->chunks = chunks;
1574 bitmap->pages = pages;
1575 bitmap->missing_pages = pages;
1576 bitmap->counter_bits = COUNTER_BITS;
1577
1578 bitmap->syncchunk = ~0UL;
1579
44456d37 1580#ifdef INJECT_FATAL_FAULT_1
32a7627c
N
1581 bitmap->bp = NULL;
1582#else
1583 bitmap->bp = kmalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
1584#endif
1585 if (!bitmap->bp)
1586 return -ENOMEM;
1587 memset(bitmap->bp, 0, pages * sizeof(*bitmap->bp));
1588
1589 bitmap->flags |= BITMAP_ACTIVE;
1590
1591 /* now that we have some pages available, initialize the in-memory
1592 * bitmap from the on-disk bitmap */
6a07997f
N
1593 start = 0;
1594 if (mddev->degraded == 0
1595 || bitmap->events_cleared == mddev->events)
1596 /* no need to keep dirty bits to optimise a re-add of a missing device */
1597 start = mddev->recovery_cp;
1598 err = bitmap_init_from_disk(bitmap, start);
193f1c93 1599
32a7627c
N
1600 if (err)
1601 return err;
1602
1603 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1604 pages, bmname(bitmap));
1605
1606 /* kick off the bitmap daemons */
1607 err = bitmap_start_daemons(bitmap);
1608 if (err)
1609 return err;
1610 return bitmap_update_sb(bitmap);
1611}
1612
1613/* the bitmap API -- for raid personalities */
1614EXPORT_SYMBOL(bitmap_startwrite);
1615EXPORT_SYMBOL(bitmap_endwrite);
1616EXPORT_SYMBOL(bitmap_start_sync);
1617EXPORT_SYMBOL(bitmap_end_sync);
1618EXPORT_SYMBOL(bitmap_unplug);
1619EXPORT_SYMBOL(bitmap_close_sync);
1620EXPORT_SYMBOL(bitmap_daemon_work);