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