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