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