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