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