]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/md/bitmap.c
[PATCH] md: convert 'faulty' and 'in_sync' fields to bits in 'flags' field
[linux-2.6-omap-h63xx.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                 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
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(NULL, page, 0, PAGE_SIZE);
330         if (!ret)
331                 ret = page->mapping->a_ops->commit_write(NULL, 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                 page_cache_get(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_CACHE_SHIFT;
361
362         PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_CACHE_SIZE,
363                         (unsigned long long)index << PAGE_CACHE_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                 page_cache_release(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_CACHE_MASK;
380         else
381                 *bytes_read = PAGE_CACHE_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_CACHE_SIZE,
386                         (unsigned long long)index << PAGE_CACHE_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 (sb->version != cpu_to_le32(BITMAP_MAJOR))
486                 reason = "unrecognized superblock version";
487         else if (chunksize < 512 || chunksize > (1024 * 1024 * 4))
488                 reason = "bitmap chunksize out of range (512B - 4MB)";
489         else if ((1 << ffz(~chunksize)) != chunksize)
490                 reason = "bitmap chunksize not a power of 2";
491         else if (daemon_sleep < 1 || daemon_sleep > 15)
492                 reason = "daemon sleep period out of range (1-15s)";
493         else if (write_behind > COUNTER_MAX)
494                 reason = "write-behind limit out of range (0 - 16383)";
495         if (reason) {
496                 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
497                         bmname(bitmap), reason);
498                 goto out;
499         }
500
501         /* keep the array size field of the bitmap superblock up to date */
502         sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
503
504         if (!bitmap->mddev->persistent)
505                 goto success;
506
507         /*
508          * if we have a persistent array superblock, compare the
509          * bitmap's UUID and event counter to the mddev's
510          */
511         if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
512                 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
513                         bmname(bitmap));
514                 goto out;
515         }
516         events = le64_to_cpu(sb->events);
517         if (events < bitmap->mddev->events) {
518                 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
519                         "-- forcing full recovery\n", bmname(bitmap), events,
520                         (unsigned long long) bitmap->mddev->events);
521                 sb->state |= BITMAP_STALE;
522         }
523 success:
524         /* assign fields using values from superblock */
525         bitmap->chunksize = chunksize;
526         bitmap->daemon_sleep = daemon_sleep;
527         bitmap->daemon_lastrun = jiffies;
528         bitmap->max_write_behind = write_behind;
529         bitmap->flags |= sb->state;
530         bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
531         if (sb->state & BITMAP_STALE)
532                 bitmap->events_cleared = bitmap->mddev->events;
533         err = 0;
534 out:
535         kunmap(bitmap->sb_page);
536         if (err)
537                 bitmap_print_sb(bitmap);
538         return err;
539 }
540
541 enum bitmap_mask_op {
542         MASK_SET,
543         MASK_UNSET
544 };
545
546 /* record the state of the bitmap in the superblock */
547 static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
548                                 enum bitmap_mask_op op)
549 {
550         bitmap_super_t *sb;
551         unsigned long flags;
552
553         spin_lock_irqsave(&bitmap->lock, flags);
554         if (!bitmap || !bitmap->sb_page) { /* can't set the state */
555                 spin_unlock_irqrestore(&bitmap->lock, flags);
556                 return;
557         }
558         page_cache_get(bitmap->sb_page);
559         spin_unlock_irqrestore(&bitmap->lock, flags);
560         sb = (bitmap_super_t *)kmap(bitmap->sb_page);
561         switch (op) {
562                 case MASK_SET: sb->state |= bits;
563                                 break;
564                 case MASK_UNSET: sb->state &= ~bits;
565                                 break;
566                 default: BUG();
567         }
568         kunmap(bitmap->sb_page);
569         page_cache_release(bitmap->sb_page);
570 }
571
572 /*
573  * general bitmap file operations
574  */
575
576 /* calculate the index of the page that contains this bit */
577 static inline unsigned long file_page_index(unsigned long chunk)
578 {
579         return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
580 }
581
582 /* calculate the (bit) offset of this bit within a page */
583 static inline unsigned long file_page_offset(unsigned long chunk)
584 {
585         return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
586 }
587
588 /*
589  * return a pointer to the page in the filemap that contains the given bit
590  *
591  * this lookup is complicated by the fact that the bitmap sb might be exactly
592  * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
593  * 0 or page 1
594  */
595 static inline struct page *filemap_get_page(struct bitmap *bitmap,
596                                         unsigned long chunk)
597 {
598         return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
599 }
600
601
602 static void bitmap_file_unmap(struct bitmap *bitmap)
603 {
604         struct page **map, *sb_page;
605         unsigned long *attr;
606         int pages;
607         unsigned long flags;
608
609         spin_lock_irqsave(&bitmap->lock, flags);
610         map = bitmap->filemap;
611         bitmap->filemap = NULL;
612         attr = bitmap->filemap_attr;
613         bitmap->filemap_attr = NULL;
614         pages = bitmap->file_pages;
615         bitmap->file_pages = 0;
616         sb_page = bitmap->sb_page;
617         bitmap->sb_page = NULL;
618         spin_unlock_irqrestore(&bitmap->lock, flags);
619
620         while (pages--)
621                 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
622                         page_cache_release(map[pages]);
623         kfree(map);
624         kfree(attr);
625
626         if (sb_page)
627                 page_cache_release(sb_page);
628 }
629
630 static void bitmap_stop_daemon(struct bitmap *bitmap);
631
632 /* dequeue the next item in a page list -- don't call from irq context */
633 static struct page_list *dequeue_page(struct bitmap *bitmap)
634 {
635         struct page_list *item = NULL;
636         struct list_head *head = &bitmap->complete_pages;
637
638         spin_lock(&bitmap->write_lock);
639         if (list_empty(head))
640                 goto out;
641         item = list_entry(head->prev, struct page_list, list);
642         list_del(head->prev);
643 out:
644         spin_unlock(&bitmap->write_lock);
645         return item;
646 }
647
648 static void drain_write_queues(struct bitmap *bitmap)
649 {
650         struct page_list *item;
651
652         while ((item = dequeue_page(bitmap))) {
653                 /* don't bother to wait */
654                 page_cache_release(item->page);
655                 mempool_free(item, bitmap->write_pool);
656         }
657
658         wake_up(&bitmap->write_wait);
659 }
660
661 static void bitmap_file_put(struct bitmap *bitmap)
662 {
663         struct file *file;
664         struct inode *inode;
665         unsigned long flags;
666
667         spin_lock_irqsave(&bitmap->lock, flags);
668         file = bitmap->file;
669         bitmap->file = NULL;
670         spin_unlock_irqrestore(&bitmap->lock, flags);
671
672         bitmap_stop_daemon(bitmap);
673
674         drain_write_queues(bitmap);
675
676         bitmap_file_unmap(bitmap);
677
678         if (file) {
679                 inode = file->f_mapping->host;
680                 spin_lock(&inode->i_lock);
681                 atomic_set(&inode->i_writecount, 1); /* allow writes again */
682                 spin_unlock(&inode->i_lock);
683                 fput(file);
684         }
685 }
686
687
688 /*
689  * bitmap_file_kick - if an error occurs while manipulating the bitmap file
690  * then it is no longer reliable, so we stop using it and we mark the file
691  * as failed in the superblock
692  */
693 static void bitmap_file_kick(struct bitmap *bitmap)
694 {
695         char *path, *ptr = NULL;
696
697         bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
698         bitmap_update_sb(bitmap);
699
700         if (bitmap->file) {
701                 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
702                 if (path)
703                         ptr = file_path(bitmap->file, path, PAGE_SIZE);
704
705                 printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
706                        bmname(bitmap), ptr ? ptr : "");
707
708                 kfree(path);
709         }
710
711         bitmap_file_put(bitmap);
712
713         return;
714 }
715
716 enum bitmap_page_attr {
717         BITMAP_PAGE_DIRTY = 1, // there are set bits that need to be synced
718         BITMAP_PAGE_CLEAN = 2, // there are bits that might need to be cleared
719         BITMAP_PAGE_NEEDWRITE=4, // there are cleared bits that need to be synced
720 };
721
722 static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
723                                 enum bitmap_page_attr attr)
724 {
725         bitmap->filemap_attr[page->index] |= attr;
726 }
727
728 static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
729                                 enum bitmap_page_attr attr)
730 {
731         bitmap->filemap_attr[page->index] &= ~attr;
732 }
733
734 static inline unsigned long get_page_attr(struct bitmap *bitmap, struct page *page)
735 {
736         return bitmap->filemap_attr[page->index];
737 }
738
739 /*
740  * bitmap_file_set_bit -- called before performing a write to the md device
741  * to set (and eventually sync) a particular bit in the bitmap file
742  *
743  * we set the bit immediately, then we record the page number so that
744  * when an unplug occurs, we can flush the dirty pages out to disk
745  */
746 static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
747 {
748         unsigned long bit;
749         struct page *page;
750         void *kaddr;
751         unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
752
753         if (!bitmap->filemap) {
754                 return;
755         }
756
757         page = filemap_get_page(bitmap, chunk);
758         bit = file_page_offset(chunk);
759
760
761         /* make sure the page stays cached until it gets written out */
762         if (! (get_page_attr(bitmap, page) & BITMAP_PAGE_DIRTY))
763                 page_cache_get(page);
764
765         /* set the bit */
766         kaddr = kmap_atomic(page, KM_USER0);
767         set_bit(bit, kaddr);
768         kunmap_atomic(kaddr, KM_USER0);
769         PRINTK("set file bit %lu page %lu\n", bit, page->index);
770
771         /* record page number so it gets flushed to disk when unplug occurs */
772         set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
773
774 }
775
776 /* this gets called when the md device is ready to unplug its underlying
777  * (slave) device queues -- before we let any writes go down, we need to
778  * sync the dirty pages of the bitmap file to disk */
779 int bitmap_unplug(struct bitmap *bitmap)
780 {
781         unsigned long i, attr, flags;
782         struct page *page;
783         int wait = 0;
784         int err;
785
786         if (!bitmap)
787                 return 0;
788
789         /* look at each page to see if there are any set bits that need to be
790          * flushed out to disk */
791         for (i = 0; i < bitmap->file_pages; i++) {
792                 spin_lock_irqsave(&bitmap->lock, flags);
793                 if (!bitmap->filemap) {
794                         spin_unlock_irqrestore(&bitmap->lock, flags);
795                         return 0;
796                 }
797                 page = bitmap->filemap[i];
798                 attr = get_page_attr(bitmap, page);
799                 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
800                 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
801                 if ((attr & BITMAP_PAGE_DIRTY))
802                         wait = 1;
803                 spin_unlock_irqrestore(&bitmap->lock, flags);
804
805                 if (attr & (BITMAP_PAGE_DIRTY | BITMAP_PAGE_NEEDWRITE)) {
806                         err = write_page(bitmap, page, 0);
807                         if (err == -EAGAIN) {
808                                 if (attr & BITMAP_PAGE_DIRTY)
809                                         err = write_page(bitmap, page, 1);
810                                 else
811                                         err = 0;
812                         }
813                         if (err)
814                                 return 1;
815                 }
816         }
817         if (wait) { /* if any writes were performed, we need to wait on them */
818                 if (bitmap->file) {
819                         spin_lock_irq(&bitmap->write_lock);
820                         wait_event_lock_irq(bitmap->write_wait,
821                                             list_empty(&bitmap->complete_pages), bitmap->write_lock,
822                                             wake_up_process(bitmap->writeback_daemon->tsk));
823                         spin_unlock_irq(&bitmap->write_lock);
824                 } else
825                         wait_event(bitmap->mddev->sb_wait,
826                                    atomic_read(&bitmap->mddev->pending_writes)==0);
827         }
828         return 0;
829 }
830
831 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
832 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
833  * the in-memory bitmap from the on-disk bitmap -- also, sets up the
834  * memory mapping of the bitmap file
835  * Special cases:
836  *   if there's no bitmap file, or if the bitmap file had been
837  *   previously kicked from the array, we mark all the bits as
838  *   1's in order to cause a full resync.
839  *
840  * We ignore all bits for sectors that end earlier than 'start'.
841  * This is used when reading an out-of-date bitmap...
842  */
843 static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
844 {
845         unsigned long i, chunks, index, oldindex, bit;
846         struct page *page = NULL, *oldpage = NULL;
847         unsigned long num_pages, bit_cnt = 0;
848         struct file *file;
849         unsigned long bytes, offset, dummy;
850         int outofdate;
851         int ret = -ENOSPC;
852
853         chunks = bitmap->chunks;
854         file = bitmap->file;
855
856         BUG_ON(!file && !bitmap->offset);
857
858 #ifdef INJECT_FAULTS_3
859         outofdate = 1;
860 #else
861         outofdate = bitmap->flags & BITMAP_STALE;
862 #endif
863         if (outofdate)
864                 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
865                         "recovery\n", bmname(bitmap));
866
867         bytes = (chunks + 7) / 8;
868
869         num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
870
871         if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
872                 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
873                         bmname(bitmap),
874                         (unsigned long) i_size_read(file->f_mapping->host),
875                         bytes + sizeof(bitmap_super_t));
876                 goto out;
877         }
878
879         ret = -ENOMEM;
880
881         bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
882         if (!bitmap->filemap)
883                 goto out;
884
885         bitmap->filemap_attr = kmalloc(sizeof(long) * num_pages, GFP_KERNEL);
886         if (!bitmap->filemap_attr)
887                 goto out;
888
889         memset(bitmap->filemap_attr, 0, sizeof(long) * num_pages);
890
891         oldindex = ~0L;
892
893         for (i = 0; i < chunks; i++) {
894                 index = file_page_index(i);
895                 bit = file_page_offset(i);
896                 if (index != oldindex) { /* this is a new page, read it in */
897                         /* unmap the old page, we're done with it */
898                         if (oldpage != NULL)
899                                 kunmap(oldpage);
900                         if (index == 0) {
901                                 /*
902                                  * if we're here then the superblock page
903                                  * contains some bits (PAGE_SIZE != sizeof sb)
904                                  * we've already read it in, so just use it
905                                  */
906                                 page = bitmap->sb_page;
907                                 offset = sizeof(bitmap_super_t);
908                         } else if (file) {
909                                 page = read_page(file, index, &dummy);
910                                 offset = 0;
911                         } else {
912                                 page = read_sb_page(bitmap->mddev, bitmap->offset, index);
913                                 offset = 0;
914                         }
915                         if (IS_ERR(page)) { /* read error */
916                                 ret = PTR_ERR(page);
917                                 goto out;
918                         }
919
920                         oldindex = index;
921                         oldpage = page;
922                         kmap(page);
923
924                         if (outofdate) {
925                                 /*
926                                  * if bitmap is out of date, dirty the
927                                  * whole page and write it out
928                                  */
929                                 memset(page_address(page) + offset, 0xff,
930                                        PAGE_SIZE - offset);
931                                 ret = write_page(bitmap, page, 1);
932                                 if (ret) {
933                                         kunmap(page);
934                                         /* release, page not in filemap yet */
935                                         page_cache_release(page);
936                                         goto out;
937                                 }
938                         }
939
940                         bitmap->filemap[bitmap->file_pages++] = page;
941                 }
942                 if (test_bit(bit, page_address(page))) {
943                         /* if the disk bit is set, set the memory bit */
944                         bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
945                                                ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
946                                 );
947                         bit_cnt++;
948                         set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
949                 }
950         }
951
952         /* everything went OK */
953         ret = 0;
954         bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
955
956         if (page) /* unmap the last page */
957                 kunmap(page);
958
959         if (bit_cnt) { /* Kick recovery if any bits were set */
960                 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
961                 md_wakeup_thread(bitmap->mddev->thread);
962         }
963
964 out:
965         printk(KERN_INFO "%s: bitmap initialized from disk: "
966                 "read %lu/%lu pages, set %lu bits, status: %d\n",
967                 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
968
969         return ret;
970 }
971
972 void bitmap_write_all(struct bitmap *bitmap)
973 {
974         /* We don't actually write all bitmap blocks here,
975          * just flag them as needing to be written
976          */
977
978         unsigned long chunks = bitmap->chunks;
979         unsigned long bytes = (chunks+7)/8 + sizeof(bitmap_super_t);
980         unsigned long num_pages = (bytes + PAGE_SIZE-1) / PAGE_SIZE;
981         while (num_pages--)
982                 bitmap->filemap_attr[num_pages] |= BITMAP_PAGE_NEEDWRITE;
983 }
984
985
986 static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
987 {
988         sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
989         unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
990         bitmap->bp[page].count += inc;
991 /*
992         if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
993                               (unsigned long long)offset, inc, bitmap->bp[page].count);
994 */
995         bitmap_checkfree(bitmap, page);
996 }
997 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
998                                             sector_t offset, int *blocks,
999                                             int create);
1000
1001 /*
1002  * bitmap daemon -- periodically wakes up to clean bits and flush pages
1003  *                      out to disk
1004  */
1005
1006 int bitmap_daemon_work(struct bitmap *bitmap)
1007 {
1008         unsigned long j;
1009         unsigned long flags;
1010         struct page *page = NULL, *lastpage = NULL;
1011         int err = 0;
1012         int blocks;
1013         int attr;
1014
1015         if (bitmap == NULL)
1016                 return 0;
1017         if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
1018                 return 0;
1019         bitmap->daemon_lastrun = jiffies;
1020
1021         for (j = 0; j < bitmap->chunks; j++) {
1022                 bitmap_counter_t *bmc;
1023                 spin_lock_irqsave(&bitmap->lock, flags);
1024                 if (!bitmap->filemap) {
1025                         /* error or shutdown */
1026                         spin_unlock_irqrestore(&bitmap->lock, flags);
1027                         break;
1028                 }
1029
1030                 page = filemap_get_page(bitmap, j);
1031
1032                 if (page != lastpage) {
1033                         /* skip this page unless it's marked as needing cleaning */
1034                         if (!((attr=get_page_attr(bitmap, page)) & BITMAP_PAGE_CLEAN)) {
1035                                 if (attr & BITMAP_PAGE_NEEDWRITE) {
1036                                         page_cache_get(page);
1037                                         clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1038                                 }
1039                                 spin_unlock_irqrestore(&bitmap->lock, flags);
1040                                 if (attr & BITMAP_PAGE_NEEDWRITE) {
1041                                         switch (write_page(bitmap, page, 0)) {
1042                                         case -EAGAIN:
1043                                                 set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1044                                                 break;
1045                                         case 0:
1046                                                 break;
1047                                         default:
1048                                                 bitmap_file_kick(bitmap);
1049                                         }
1050                                         page_cache_release(page);
1051                                 }
1052                                 continue;
1053                         }
1054
1055                         /* grab the new page, sync and release the old */
1056                         page_cache_get(page);
1057                         if (lastpage != NULL) {
1058                                 if (get_page_attr(bitmap, lastpage) & BITMAP_PAGE_NEEDWRITE) {
1059                                         clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1060                                         spin_unlock_irqrestore(&bitmap->lock, flags);
1061                                         err = write_page(bitmap, lastpage, 0);
1062                                         if (err == -EAGAIN) {
1063                                                 err = 0;
1064                                                 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1065                                         }
1066                                 } else {
1067                                         set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1068                                         spin_unlock_irqrestore(&bitmap->lock, flags);
1069                                 }
1070                                 kunmap(lastpage);
1071                                 page_cache_release(lastpage);
1072                                 if (err)
1073                                         bitmap_file_kick(bitmap);
1074                         } else
1075                                 spin_unlock_irqrestore(&bitmap->lock, flags);
1076                         lastpage = page;
1077                         kmap(page);
1078 /*
1079                         printk("bitmap clean at page %lu\n", j);
1080 */
1081                         spin_lock_irqsave(&bitmap->lock, flags);
1082                         clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1083                 }
1084                 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1085                                         &blocks, 0);
1086                 if (bmc) {
1087 /*
1088   if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1089 */
1090                         if (*bmc == 2) {
1091                                 *bmc=1; /* maybe clear the bit next time */
1092                                 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1093                         } else if (*bmc == 1) {
1094                                 /* we can clear the bit */
1095                                 *bmc = 0;
1096                                 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1097                                                   -1);
1098
1099                                 /* clear the bit */
1100                                 clear_bit(file_page_offset(j), page_address(page));
1101                         }
1102                 }
1103                 spin_unlock_irqrestore(&bitmap->lock, flags);
1104         }
1105
1106         /* now sync the final page */
1107         if (lastpage != NULL) {
1108                 kunmap(lastpage);
1109                 spin_lock_irqsave(&bitmap->lock, flags);
1110                 if (get_page_attr(bitmap, lastpage) &BITMAP_PAGE_NEEDWRITE) {
1111                         clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1112                         spin_unlock_irqrestore(&bitmap->lock, flags);
1113                         err = write_page(bitmap, lastpage, 0);
1114                         if (err == -EAGAIN) {
1115                                 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1116                                 err = 0;
1117                         }
1118                 } else {
1119                         set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1120                         spin_unlock_irqrestore(&bitmap->lock, flags);
1121                 }
1122
1123                 page_cache_release(lastpage);
1124         }
1125
1126         return err;
1127 }
1128
1129 static void daemon_exit(struct bitmap *bitmap, mdk_thread_t **daemon)
1130 {
1131         mdk_thread_t *dmn;
1132         unsigned long flags;
1133
1134         /* if no one is waiting on us, we'll free the md thread struct
1135          * and exit, otherwise we let the waiter clean things up */
1136         spin_lock_irqsave(&bitmap->lock, flags);
1137         if ((dmn = *daemon)) { /* no one is waiting, cleanup and exit */
1138                 *daemon = NULL;
1139                 spin_unlock_irqrestore(&bitmap->lock, flags);
1140                 kfree(dmn);
1141                 complete_and_exit(NULL, 0); /* do_exit not exported */
1142         }
1143         spin_unlock_irqrestore(&bitmap->lock, flags);
1144 }
1145
1146 static void bitmap_writeback_daemon(mddev_t *mddev)
1147 {
1148         struct bitmap *bitmap = mddev->bitmap;
1149         struct page *page;
1150         struct page_list *item;
1151         int err = 0;
1152
1153         if (signal_pending(current)) {
1154                 printk(KERN_INFO
1155                        "%s: bitmap writeback daemon got signal, exiting...\n",
1156                        bmname(bitmap));
1157                 err = -EINTR;
1158                 goto out;
1159         }
1160         if (bitmap == NULL)
1161                 /* about to be stopped. */
1162                 return;
1163
1164         PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
1165         /* wait on bitmap page writebacks */
1166         while ((item = dequeue_page(bitmap))) {
1167                 page = item->page;
1168                 mempool_free(item, bitmap->write_pool);
1169                 PRINTK("wait on page writeback: %p\n", page);
1170                 wait_on_page_writeback(page);
1171                 PRINTK("finished page writeback: %p\n", page);
1172
1173                 err = PageError(page);
1174                 page_cache_release(page);
1175                 if (err) {
1176                         printk(KERN_WARNING "%s: bitmap file writeback "
1177                                "failed (page %lu): %d\n",
1178                                bmname(bitmap), page->index, err);
1179                         bitmap_file_kick(bitmap);
1180                         goto out;
1181                 }
1182         }
1183  out:
1184         wake_up(&bitmap->write_wait);
1185         if (err) {
1186                 printk(KERN_INFO "%s: bitmap writeback daemon exiting (%d)\n",
1187                        bmname(bitmap), err);
1188                 daemon_exit(bitmap, &bitmap->writeback_daemon);
1189         }
1190 }
1191
1192 static mdk_thread_t *bitmap_start_daemon(struct bitmap *bitmap,
1193                                 void (*func)(mddev_t *), char *name)
1194 {
1195         mdk_thread_t *daemon;
1196         char namebuf[32];
1197
1198 #ifdef INJECT_FATAL_FAULT_2
1199         daemon = NULL;
1200 #else
1201         sprintf(namebuf, "%%s_%s", name);
1202         daemon = md_register_thread(func, bitmap->mddev, namebuf);
1203 #endif
1204         if (!daemon) {
1205                 printk(KERN_ERR "%s: failed to start bitmap daemon\n",
1206                         bmname(bitmap));
1207                 return ERR_PTR(-ECHILD);
1208         }
1209
1210         md_wakeup_thread(daemon); /* start it running */
1211
1212         PRINTK("%s: %s daemon (pid %d) started...\n",
1213                 bmname(bitmap), name, daemon->tsk->pid);
1214
1215         return daemon;
1216 }
1217
1218 static void bitmap_stop_daemon(struct bitmap *bitmap)
1219 {
1220         /* the daemon can't stop itself... it'll just exit instead... */
1221         if (bitmap->writeback_daemon && ! IS_ERR(bitmap->writeback_daemon) &&
1222             current->pid != bitmap->writeback_daemon->tsk->pid) {
1223                 mdk_thread_t *daemon;
1224                 unsigned long flags;
1225
1226                 spin_lock_irqsave(&bitmap->lock, flags);
1227                 daemon = bitmap->writeback_daemon;
1228                 bitmap->writeback_daemon = NULL;
1229                 spin_unlock_irqrestore(&bitmap->lock, flags);
1230                 if (daemon && ! IS_ERR(daemon))
1231                         md_unregister_thread(daemon); /* destroy the thread */
1232         }
1233 }
1234
1235 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1236                                             sector_t offset, int *blocks,
1237                                             int create)
1238 {
1239         /* If 'create', we might release the lock and reclaim it.
1240          * The lock must have been taken with interrupts enabled.
1241          * If !create, we don't release the lock.
1242          */
1243         sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1244         unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1245         unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1246         sector_t csize;
1247
1248         if (bitmap_checkpage(bitmap, page, create) < 0) {
1249                 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1250                 *blocks = csize - (offset & (csize- 1));
1251                 return NULL;
1252         }
1253         /* now locked ... */
1254
1255         if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1256                 /* should we use the first or second counter field
1257                  * of the hijacked pointer? */
1258                 int hi = (pageoff > PAGE_COUNTER_MASK);
1259                 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1260                                           PAGE_COUNTER_SHIFT - 1);
1261                 *blocks = csize - (offset & (csize- 1));
1262                 return  &((bitmap_counter_t *)
1263                           &bitmap->bp[page].map)[hi];
1264         } else { /* page is allocated */
1265                 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1266                 *blocks = csize - (offset & (csize- 1));
1267                 return (bitmap_counter_t *)
1268                         &(bitmap->bp[page].map[pageoff]);
1269         }
1270 }
1271
1272 int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
1273 {
1274         if (!bitmap) return 0;
1275
1276         if (behind) {
1277                 atomic_inc(&bitmap->behind_writes);
1278                 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1279                   atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1280         }
1281
1282         while (sectors) {
1283                 int blocks;
1284                 bitmap_counter_t *bmc;
1285
1286                 spin_lock_irq(&bitmap->lock);
1287                 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1288                 if (!bmc) {
1289                         spin_unlock_irq(&bitmap->lock);
1290                         return 0;
1291                 }
1292
1293                 switch(*bmc) {
1294                 case 0:
1295                         bitmap_file_set_bit(bitmap, offset);
1296                         bitmap_count_page(bitmap,offset, 1);
1297                         blk_plug_device(bitmap->mddev->queue);
1298                         /* fall through */
1299                 case 1:
1300                         *bmc = 2;
1301                 }
1302                 if ((*bmc & COUNTER_MAX) == COUNTER_MAX) BUG();
1303                 (*bmc)++;
1304
1305                 spin_unlock_irq(&bitmap->lock);
1306
1307                 offset += blocks;
1308                 if (sectors > blocks)
1309                         sectors -= blocks;
1310                 else sectors = 0;
1311         }
1312         return 0;
1313 }
1314
1315 void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
1316                      int success, int behind)
1317 {
1318         if (!bitmap) return;
1319         if (behind) {
1320                 atomic_dec(&bitmap->behind_writes);
1321                 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1322                   atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1323         }
1324
1325         while (sectors) {
1326                 int blocks;
1327                 unsigned long flags;
1328                 bitmap_counter_t *bmc;
1329
1330                 spin_lock_irqsave(&bitmap->lock, flags);
1331                 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1332                 if (!bmc) {
1333                         spin_unlock_irqrestore(&bitmap->lock, flags);
1334                         return;
1335                 }
1336
1337                 if (!success && ! (*bmc & NEEDED_MASK))
1338                         *bmc |= NEEDED_MASK;
1339
1340                 (*bmc)--;
1341                 if (*bmc <= 2) {
1342                         set_page_attr(bitmap,
1343                                       filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1344                                       BITMAP_PAGE_CLEAN);
1345                 }
1346                 spin_unlock_irqrestore(&bitmap->lock, flags);
1347                 offset += blocks;
1348                 if (sectors > blocks)
1349                         sectors -= blocks;
1350                 else sectors = 0;
1351         }
1352 }
1353
1354 int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1355                         int degraded)
1356 {
1357         bitmap_counter_t *bmc;
1358         int rv;
1359         if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1360                 *blocks = 1024;
1361                 return 1; /* always resync if no bitmap */
1362         }
1363         spin_lock_irq(&bitmap->lock);
1364         bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1365         rv = 0;
1366         if (bmc) {
1367                 /* locked */
1368                 if (RESYNC(*bmc))
1369                         rv = 1;
1370                 else if (NEEDED(*bmc)) {
1371                         rv = 1;
1372                         if (!degraded) { /* don't set/clear bits if degraded */
1373                                 *bmc |= RESYNC_MASK;
1374                                 *bmc &= ~NEEDED_MASK;
1375                         }
1376                 }
1377         }
1378         spin_unlock_irq(&bitmap->lock);
1379         return rv;
1380 }
1381
1382 void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1383 {
1384         bitmap_counter_t *bmc;
1385         unsigned long flags;
1386 /*
1387         if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1388 */      if (bitmap == NULL) {
1389                 *blocks = 1024;
1390                 return;
1391         }
1392         spin_lock_irqsave(&bitmap->lock, flags);
1393         bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1394         if (bmc == NULL)
1395                 goto unlock;
1396         /* locked */
1397 /*
1398         if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1399 */
1400         if (RESYNC(*bmc)) {
1401                 *bmc &= ~RESYNC_MASK;
1402
1403                 if (!NEEDED(*bmc) && aborted)
1404                         *bmc |= NEEDED_MASK;
1405                 else {
1406                         if (*bmc <= 2) {
1407                                 set_page_attr(bitmap,
1408                                               filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1409                                               BITMAP_PAGE_CLEAN);
1410                         }
1411                 }
1412         }
1413  unlock:
1414         spin_unlock_irqrestore(&bitmap->lock, flags);
1415 }
1416
1417 void bitmap_close_sync(struct bitmap *bitmap)
1418 {
1419         /* Sync has finished, and any bitmap chunks that weren't synced
1420          * properly have been aborted.  It remains to us to clear the
1421          * RESYNC bit wherever it is still on
1422          */
1423         sector_t sector = 0;
1424         int blocks;
1425         if (!bitmap) return;
1426         while (sector < bitmap->mddev->resync_max_sectors) {
1427                 bitmap_end_sync(bitmap, sector, &blocks, 0);
1428 /*
1429                 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1430                                          (unsigned long long)sector, blocks);
1431 */              sector += blocks;
1432         }
1433 }
1434
1435 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
1436 {
1437         /* For each chunk covered by any of these sectors, set the
1438          * counter to 1 and set resync_needed.  They should all
1439          * be 0 at this point
1440          */
1441
1442         int secs;
1443         bitmap_counter_t *bmc;
1444         spin_lock_irq(&bitmap->lock);
1445         bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1446         if (!bmc) {
1447                 spin_unlock_irq(&bitmap->lock);
1448                 return;
1449         }
1450         if (! *bmc) {
1451                 struct page *page;
1452                 *bmc = 1 | (needed?NEEDED_MASK:0);
1453                 bitmap_count_page(bitmap, offset, 1);
1454                 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1455                 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1456         }
1457         spin_unlock_irq(&bitmap->lock);
1458
1459 }
1460
1461 /*
1462  * flush out any pending updates
1463  */
1464 void bitmap_flush(mddev_t *mddev)
1465 {
1466         struct bitmap *bitmap = mddev->bitmap;
1467         int sleep;
1468
1469         if (!bitmap) /* there was no bitmap */
1470                 return;
1471
1472         /* run the daemon_work three time to ensure everything is flushed
1473          * that can be
1474          */
1475         sleep = bitmap->daemon_sleep;
1476         bitmap->daemon_sleep = 0;
1477         bitmap_daemon_work(bitmap);
1478         bitmap_daemon_work(bitmap);
1479         bitmap_daemon_work(bitmap);
1480         bitmap->daemon_sleep = sleep;
1481         bitmap_update_sb(bitmap);
1482 }
1483
1484 /*
1485  * free memory that was allocated
1486  */
1487 static void bitmap_free(struct bitmap *bitmap)
1488 {
1489         unsigned long k, pages;
1490         struct bitmap_page *bp;
1491
1492         if (!bitmap) /* there was no bitmap */
1493                 return;
1494
1495         /* release the bitmap file and kill the daemon */
1496         bitmap_file_put(bitmap);
1497
1498         bp = bitmap->bp;
1499         pages = bitmap->pages;
1500
1501         /* free all allocated memory */
1502
1503         mempool_destroy(bitmap->write_pool);
1504
1505         if (bp) /* deallocate the page memory */
1506                 for (k = 0; k < pages; k++)
1507                         if (bp[k].map && !bp[k].hijacked)
1508                                 kfree(bp[k].map);
1509         kfree(bp);
1510         kfree(bitmap);
1511 }
1512 void bitmap_destroy(mddev_t *mddev)
1513 {
1514         struct bitmap *bitmap = mddev->bitmap;
1515
1516         if (!bitmap) /* there was no bitmap */
1517                 return;
1518
1519         mddev->bitmap = NULL; /* disconnect from the md device */
1520
1521         bitmap_free(bitmap);
1522 }
1523
1524 /*
1525  * initialize the bitmap structure
1526  * if this returns an error, bitmap_destroy must be called to do clean up
1527  */
1528 int bitmap_create(mddev_t *mddev)
1529 {
1530         struct bitmap *bitmap;
1531         unsigned long blocks = mddev->resync_max_sectors;
1532         unsigned long chunks;
1533         unsigned long pages;
1534         struct file *file = mddev->bitmap_file;
1535         int err;
1536         sector_t start;
1537
1538         BUG_ON(sizeof(bitmap_super_t) != 256);
1539
1540         if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
1541                 return 0;
1542
1543         BUG_ON(file && mddev->bitmap_offset);
1544
1545         bitmap = kmalloc(sizeof(*bitmap), GFP_KERNEL);
1546         if (!bitmap)
1547                 return -ENOMEM;
1548
1549         memset(bitmap, 0, sizeof(*bitmap));
1550
1551         spin_lock_init(&bitmap->lock);
1552         bitmap->mddev = mddev;
1553
1554         spin_lock_init(&bitmap->write_lock);
1555         INIT_LIST_HEAD(&bitmap->complete_pages);
1556         init_waitqueue_head(&bitmap->write_wait);
1557         bitmap->write_pool = mempool_create(WRITE_POOL_SIZE, write_pool_alloc,
1558                                 write_pool_free, NULL);
1559         err = -ENOMEM;
1560         if (!bitmap->write_pool)
1561                 goto error;
1562
1563         bitmap->file = file;
1564         bitmap->offset = mddev->bitmap_offset;
1565         if (file) get_file(file);
1566         /* read superblock from bitmap file (this sets bitmap->chunksize) */
1567         err = bitmap_read_sb(bitmap);
1568         if (err)
1569                 goto error;
1570
1571         bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
1572                                         sizeof(bitmap->chunksize));
1573
1574         /* now that chunksize and chunkshift are set, we can use these macros */
1575         chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1576                         CHUNK_BLOCK_RATIO(bitmap);
1577         pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1578
1579         BUG_ON(!pages);
1580
1581         bitmap->chunks = chunks;
1582         bitmap->pages = pages;
1583         bitmap->missing_pages = pages;
1584         bitmap->counter_bits = COUNTER_BITS;
1585
1586         bitmap->syncchunk = ~0UL;
1587
1588 #ifdef INJECT_FATAL_FAULT_1
1589         bitmap->bp = NULL;
1590 #else
1591         bitmap->bp = kmalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
1592 #endif
1593         err = -ENOMEM;
1594         if (!bitmap->bp)
1595                 goto error;
1596         memset(bitmap->bp, 0, pages * sizeof(*bitmap->bp));
1597
1598         bitmap->flags |= BITMAP_ACTIVE;
1599
1600         /* now that we have some pages available, initialize the in-memory
1601          * bitmap from the on-disk bitmap */
1602         start = 0;
1603         if (mddev->degraded == 0
1604             || bitmap->events_cleared == mddev->events)
1605                 /* no need to keep dirty bits to optimise a re-add of a missing device */
1606                 start = mddev->recovery_cp;
1607         err = bitmap_init_from_disk(bitmap, start);
1608
1609         if (err)
1610                 goto error;
1611
1612         printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1613                 pages, bmname(bitmap));
1614
1615         mddev->bitmap = bitmap;
1616
1617         if (file)
1618                 /* kick off the bitmap writeback daemon */
1619                 bitmap->writeback_daemon =
1620                         bitmap_start_daemon(bitmap,
1621                                             bitmap_writeback_daemon,
1622                                             "bitmap_wb");
1623
1624         if (IS_ERR(bitmap->writeback_daemon))
1625                 return PTR_ERR(bitmap->writeback_daemon);
1626         return bitmap_update_sb(bitmap);
1627
1628  error:
1629         bitmap_free(bitmap);
1630         return err;
1631 }
1632
1633 /* the bitmap API -- for raid personalities */
1634 EXPORT_SYMBOL(bitmap_startwrite);
1635 EXPORT_SYMBOL(bitmap_endwrite);
1636 EXPORT_SYMBOL(bitmap_start_sync);
1637 EXPORT_SYMBOL(bitmap_end_sync);
1638 EXPORT_SYMBOL(bitmap_unplug);
1639 EXPORT_SYMBOL(bitmap_close_sync);
1640 EXPORT_SYMBOL(bitmap_daemon_work);