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