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