blob: ac89a5deaca2e12fa62ccb78f1d62b088039675c [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
NeilBrown32a76272005-06-21 17:17:14 -070010 */
11
12/*
13 * Still to do:
14 *
15 * flush after percent set rather than just time based. (maybe both).
16 * wait if count gets too high, wake when it drops to half.
NeilBrown32a76272005-06-21 17:17:14 -070017 */
18
19#include <linux/module.h>
NeilBrown32a76272005-06-21 17:17:14 -070020#include <linux/errno.h>
21#include <linux/slab.h>
22#include <linux/init.h>
NeilBrown32a76272005-06-21 17:17:14 -070023#include <linux/timer.h>
24#include <linux/sched.h>
25#include <linux/list.h>
26#include <linux/file.h>
27#include <linux/mount.h>
28#include <linux/buffer_head.h>
29#include <linux/raid/md.h>
30#include <linux/raid/bitmap.h>
31
32/* debug macros */
33
34#define DEBUG 0
35
36#if DEBUG
37/* these are for debugging purposes only! */
38
39/* define one and only one of these */
40#define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
41#define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
42#define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
43#define INJECT_FAULTS_4 0 /* undef */
44#define INJECT_FAULTS_5 0 /* undef */
45#define INJECT_FAULTS_6 0
46
47/* if these are defined, the driver will fail! debug only */
48#define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
49#define INJECT_FATAL_FAULT_2 0 /* undef */
50#define INJECT_FATAL_FAULT_3 0 /* undef */
51#endif
52
53//#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
54#define DPRINTK(x...) do { } while(0)
55
56#ifndef PRINTK
57# if DEBUG > 0
58# define PRINTK(x...) printk(KERN_DEBUG x)
59# else
60# define PRINTK(x...)
61# endif
62#endif
63
64static inline char * bmname(struct bitmap *bitmap)
65{
66 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
67}
68
NeilBrown32a76272005-06-21 17:17:14 -070069
70/*
71 * just a placeholder - calls kmalloc for bitmap pages
72 */
73static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
74{
75 unsigned char *page;
76
Olaf Hering44456d32005-07-27 11:45:17 -070077#ifdef INJECT_FAULTS_1
NeilBrown32a76272005-06-21 17:17:14 -070078 page = NULL;
79#else
80 page = kmalloc(PAGE_SIZE, GFP_NOIO);
81#endif
82 if (!page)
83 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
84 else
NeilBrowna654b9d82005-06-21 17:17:27 -070085 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
NeilBrown32a76272005-06-21 17:17:14 -070086 bmname(bitmap), page);
87 return page;
88}
89
90/*
91 * for now just a placeholder -- just calls kfree for bitmap pages
92 */
93static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
94{
95 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
96 kfree(page);
97}
98
99/*
100 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
101 *
102 * 1) check to see if this page is allocated, if it's not then try to alloc
103 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
104 * page pointer directly as a counter
105 *
106 * if we find our page, we increment the page's refcount so that it stays
107 * allocated while we're using it
108 */
109static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
110{
111 unsigned char *mappage;
112
113 if (page >= bitmap->pages) {
114 printk(KERN_ALERT
115 "%s: invalid bitmap page request: %lu (> %lu)\n",
116 bmname(bitmap), page, bitmap->pages-1);
117 return -EINVAL;
118 }
119
120
121 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
122 return 0;
123
124 if (bitmap->bp[page].map) /* page is already allocated, just return */
125 return 0;
126
127 if (!create)
128 return -ENOENT;
129
130 spin_unlock_irq(&bitmap->lock);
131
132 /* this page has not been allocated yet */
133
134 if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
135 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
136 bmname(bitmap));
137 /* failed - set the hijacked flag so that we can use the
138 * pointer as a counter */
139 spin_lock_irq(&bitmap->lock);
140 if (!bitmap->bp[page].map)
141 bitmap->bp[page].hijacked = 1;
142 goto out;
143 }
144
145 /* got a page */
146
147 spin_lock_irq(&bitmap->lock);
148
149 /* recheck the page */
150
151 if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
152 /* somebody beat us to getting the page */
153 bitmap_free_page(bitmap, mappage);
154 return 0;
155 }
156
157 /* no page was in place and we have one, so install it */
158
159 memset(mappage, 0, PAGE_SIZE);
160 bitmap->bp[page].map = mappage;
161 bitmap->missing_pages--;
162out:
163 return 0;
164}
165
166
167/* if page is completely empty, put it back on the free list, or dealloc it */
168/* if page was hijacked, unmark the flag so it might get alloced next time */
169/* Note: lock should be held when calling this */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800170static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
NeilBrown32a76272005-06-21 17:17:14 -0700171{
172 char *ptr;
173
174 if (bitmap->bp[page].count) /* page is still busy */
175 return;
176
177 /* page is no longer in use, it can be released */
178
179 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
180 bitmap->bp[page].hijacked = 0;
181 bitmap->bp[page].map = NULL;
182 return;
183 }
184
185 /* normal case, free the page */
186
187#if 0
188/* actually ... let's not. We will probably need the page again exactly when
189 * memory is tight and we are flusing to disk
190 */
191 return;
192#else
193 ptr = bitmap->bp[page].map;
194 bitmap->bp[page].map = NULL;
195 bitmap->missing_pages++;
196 bitmap_free_page(bitmap, ptr);
197 return;
198#endif
199}
200
201
202/*
203 * bitmap file handling - read and write the bitmap file and its superblock
204 */
205
NeilBrown32a76272005-06-21 17:17:14 -0700206/*
207 * basic page I/O operations
208 */
209
NeilBrowna654b9d82005-06-21 17:17:27 -0700210/* IO operations when bitmap is stored near all superblocks */
211static struct page *read_sb_page(mddev_t *mddev, long offset, unsigned long index)
212{
213 /* choose a good rdev and read the page from there */
214
215 mdk_rdev_t *rdev;
216 struct list_head *tmp;
217 struct page *page = alloc_page(GFP_KERNEL);
218 sector_t target;
219
220 if (!page)
221 return ERR_PTR(-ENOMEM);
NeilBrowna654b9d82005-06-21 17:17:27 -0700222
NeilBrownd089c6a2008-02-06 01:39:59 -0800223 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800224 if (! test_bit(In_sync, &rdev->flags)
225 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700226 continue;
227
Andre Noll0f420352008-07-11 22:02:23 +1000228 target = rdev->sb_start + offset + index * (PAGE_SIZE/512);
NeilBrowna654b9d82005-06-21 17:17:27 -0700229
NeilBrownab904d62005-09-09 16:23:52 -0700230 if (sync_page_io(rdev->bdev, target, PAGE_SIZE, page, READ)) {
231 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700232 attach_page_buffers(page, NULL); /* so that free_buffer will
233 * quietly no-op */
NeilBrownab904d62005-09-09 16:23:52 -0700234 return page;
235 }
236 }
237 return ERR_PTR(-EIO);
NeilBrowna654b9d82005-06-21 17:17:27 -0700238
NeilBrowna654b9d82005-06-21 17:17:27 -0700239}
240
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000241static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
242{
243 /* Iterate the disks of an mddev, using rcu to protect access to the
244 * linked list, and raising the refcount of devices we return to ensure
245 * they don't disappear while in use.
246 * As devices are only added or removed when raid_disk is < 0 and
247 * nr_pending is 0 and In_sync is clear, the entries we return will
248 * still be in the same position on the list when we re-enter
249 * list_for_each_continue_rcu.
250 */
251 struct list_head *pos;
252 rcu_read_lock();
253 if (rdev == NULL)
254 /* start at the beginning */
255 pos = &mddev->disks;
256 else {
257 /* release the previous rdev and start from there. */
258 rdev_dec_pending(rdev, mddev);
259 pos = &rdev->same_set;
260 }
261 list_for_each_continue_rcu(pos, &mddev->disks) {
262 rdev = list_entry(pos, mdk_rdev_t, same_set);
263 if (rdev->raid_disk >= 0 &&
264 test_bit(In_sync, &rdev->flags) &&
265 !test_bit(Faulty, &rdev->flags)) {
266 /* this is a usable devices */
267 atomic_inc(&rdev->nr_pending);
268 rcu_read_unlock();
269 return rdev;
270 }
271 }
272 rcu_read_unlock();
273 return NULL;
274}
275
NeilBrownab6085c2007-05-23 13:58:10 -0700276static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrowna654b9d82005-06-21 17:17:27 -0700277{
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000278 mdk_rdev_t *rdev = NULL;
NeilBrownab6085c2007-05-23 13:58:10 -0700279 mddev_t *mddev = bitmap->mddev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700280
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000281 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
NeilBrownab6085c2007-05-23 13:58:10 -0700282 int size = PAGE_SIZE;
283 if (page->index == bitmap->file_pages-1)
284 size = roundup(bitmap->last_page_size,
285 bdev_hardsect_size(rdev->bdev));
NeilBrownf0d76d72007-07-17 04:06:12 -0700286 /* Just make sure we aren't corrupting data or
287 * metadata
288 */
289 if (bitmap->offset < 0) {
290 /* DATA BITMAP METADATA */
291 if (bitmap->offset
NeilBrown85bfb4d2007-10-22 20:45:11 -0700292 + (long)(page->index * (PAGE_SIZE/512))
NeilBrownf0d76d72007-07-17 04:06:12 -0700293 + size/512 > 0)
294 /* bitmap runs in to metadata */
NeilBrown4b809912008-07-21 17:05:25 +1000295 goto bad_alignment;
NeilBrownf0d76d72007-07-17 04:06:12 -0700296 if (rdev->data_offset + mddev->size*2
Andre Noll0f420352008-07-11 22:02:23 +1000297 > rdev->sb_start + bitmap->offset)
NeilBrownf0d76d72007-07-17 04:06:12 -0700298 /* data runs in to bitmap */
NeilBrown4b809912008-07-21 17:05:25 +1000299 goto bad_alignment;
Andre Noll0f420352008-07-11 22:02:23 +1000300 } else if (rdev->sb_start < rdev->data_offset) {
NeilBrownf0d76d72007-07-17 04:06:12 -0700301 /* METADATA BITMAP DATA */
Andre Noll0f420352008-07-11 22:02:23 +1000302 if (rdev->sb_start
NeilBrownf0d76d72007-07-17 04:06:12 -0700303 + bitmap->offset
304 + page->index*(PAGE_SIZE/512) + size/512
305 > rdev->data_offset)
306 /* bitmap runs in to data */
NeilBrown4b809912008-07-21 17:05:25 +1000307 goto bad_alignment;
NeilBrownf0d76d72007-07-17 04:06:12 -0700308 } else {
309 /* DATA METADATA BITMAP - no problems */
310 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700311 md_super_write(mddev, rdev,
Andre Noll0f420352008-07-11 22:02:23 +1000312 rdev->sb_start + bitmap->offset
NeilBrowna654b9d82005-06-21 17:17:27 -0700313 + page->index * (PAGE_SIZE/512),
NeilBrownab6085c2007-05-23 13:58:10 -0700314 size,
NeilBrowna654b9d82005-06-21 17:17:27 -0700315 page);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000316 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700317
318 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800319 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700320 return 0;
NeilBrown4b809912008-07-21 17:05:25 +1000321
322 bad_alignment:
323 rcu_read_unlock();
324 return -EINVAL;
NeilBrowna654b9d82005-06-21 17:17:27 -0700325}
326
NeilBrown4ad13662007-07-17 04:06:13 -0700327static void bitmap_file_kick(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700328/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700329 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700330 */
NeilBrown4ad13662007-07-17 04:06:13 -0700331static void write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700332{
NeilBrownd785a062006-06-26 00:27:48 -0700333 struct buffer_head *bh;
NeilBrown32a76272005-06-21 17:17:14 -0700334
NeilBrownf0d76d72007-07-17 04:06:12 -0700335 if (bitmap->file == NULL) {
336 switch (write_sb_page(bitmap, page, wait)) {
337 case -EINVAL:
338 bitmap->flags |= BITMAP_WRITE_ERROR;
NeilBrownf0d76d72007-07-17 04:06:12 -0700339 }
NeilBrown4ad13662007-07-17 04:06:13 -0700340 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700341
NeilBrown4ad13662007-07-17 04:06:13 -0700342 bh = page_buffers(page);
NeilBrownc7084432006-01-06 00:20:45 -0800343
NeilBrown4ad13662007-07-17 04:06:13 -0700344 while (bh && bh->b_blocknr) {
345 atomic_inc(&bitmap->pending_writes);
346 set_buffer_locked(bh);
347 set_buffer_mapped(bh);
348 submit_bh(WRITE, bh);
349 bh = bh->b_this_page;
350 }
NeilBrown32a76272005-06-21 17:17:14 -0700351
NeilBrown4ad13662007-07-17 04:06:13 -0700352 if (wait) {
353 wait_event(bitmap->write_wait,
354 atomic_read(&bitmap->pending_writes)==0);
355 }
NeilBrown32a76272005-06-21 17:17:14 -0700356 }
NeilBrown4ad13662007-07-17 04:06:13 -0700357 if (bitmap->flags & BITMAP_WRITE_ERROR)
358 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700359}
360
NeilBrownd785a062006-06-26 00:27:48 -0700361static void end_bitmap_write(struct buffer_head *bh, int uptodate)
NeilBrown32a76272005-06-21 17:17:14 -0700362{
NeilBrownd785a062006-06-26 00:27:48 -0700363 struct bitmap *bitmap = bh->b_private;
364 unsigned long flags;
365
366 if (!uptodate) {
367 spin_lock_irqsave(&bitmap->lock, flags);
368 bitmap->flags |= BITMAP_WRITE_ERROR;
369 spin_unlock_irqrestore(&bitmap->lock, flags);
370 }
371 if (atomic_dec_and_test(&bitmap->pending_writes))
372 wake_up(&bitmap->write_wait);
373}
374
375/* copied from buffer.c */
376static void
377__clear_page_buffers(struct page *page)
378{
379 ClearPagePrivate(page);
380 set_page_private(page, 0);
381 page_cache_release(page);
382}
383static void free_buffers(struct page *page)
384{
385 struct buffer_head *bh = page_buffers(page);
386
387 while (bh) {
388 struct buffer_head *next = bh->b_this_page;
389 free_buffer_head(bh);
390 bh = next;
391 }
392 __clear_page_buffers(page);
393 put_page(page);
394}
395
396/* read a page from a file.
397 * We both read the page, and attach buffers to the page to record the
398 * address of each block (using bmap). These addresses will be used
399 * to write the block later, completely bypassing the filesystem.
400 * This usage is similar to how swap files are handled, and allows us
401 * to write to a file with no concerns of memory allocation failing.
402 */
403static struct page *read_page(struct file *file, unsigned long index,
404 struct bitmap *bitmap,
405 unsigned long count)
406{
NeilBrown32a76272005-06-21 17:17:14 -0700407 struct page *page = NULL;
Josef Sipekc649bb92006-12-08 02:37:19 -0800408 struct inode *inode = file->f_path.dentry->d_inode;
NeilBrownd785a062006-06-26 00:27:48 -0700409 struct buffer_head *bh;
410 sector_t block;
NeilBrown32a76272005-06-21 17:17:14 -0700411
NeilBrown2d1f3b52006-01-06 00:20:31 -0800412 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
413 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700414
NeilBrownd785a062006-06-26 00:27:48 -0700415 page = alloc_page(GFP_KERNEL);
416 if (!page)
417 page = ERR_PTR(-ENOMEM);
NeilBrown32a76272005-06-21 17:17:14 -0700418 if (IS_ERR(page))
419 goto out;
NeilBrownd785a062006-06-26 00:27:48 -0700420
NeilBrownd785a062006-06-26 00:27:48 -0700421 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
422 if (!bh) {
423 put_page(page);
424 page = ERR_PTR(-ENOMEM);
425 goto out;
426 }
427 attach_page_buffers(page, bh);
428 block = index << (PAGE_SHIFT - inode->i_blkbits);
429 while (bh) {
430 if (count == 0)
431 bh->b_blocknr = 0;
432 else {
433 bh->b_blocknr = bmap(inode, block);
434 if (bh->b_blocknr == 0) {
435 /* Cannot use this file! */
436 free_buffers(page);
437 page = ERR_PTR(-EINVAL);
438 goto out;
439 }
440 bh->b_bdev = inode->i_sb->s_bdev;
441 if (count < (1<<inode->i_blkbits))
442 count = 0;
443 else
444 count -= (1<<inode->i_blkbits);
NeilBrown32a76272005-06-21 17:17:14 -0700445
NeilBrownd785a062006-06-26 00:27:48 -0700446 bh->b_end_io = end_bitmap_write;
447 bh->b_private = bitmap;
NeilBrownce25c312006-06-26 00:27:49 -0700448 atomic_inc(&bitmap->pending_writes);
449 set_buffer_locked(bh);
450 set_buffer_mapped(bh);
451 submit_bh(READ, bh);
NeilBrownd785a062006-06-26 00:27:48 -0700452 }
453 block++;
454 bh = bh->b_this_page;
455 }
NeilBrownd785a062006-06-26 00:27:48 -0700456 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700457
458 wait_event(bitmap->write_wait,
459 atomic_read(&bitmap->pending_writes)==0);
460 if (bitmap->flags & BITMAP_WRITE_ERROR) {
461 free_buffers(page);
462 page = ERR_PTR(-EIO);
463 }
NeilBrown32a76272005-06-21 17:17:14 -0700464out:
465 if (IS_ERR(page))
466 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800467 (int)PAGE_SIZE,
468 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700469 PTR_ERR(page));
470 return page;
471}
472
473/*
474 * bitmap file superblock operations
475 */
476
477/* update the event counter and sync the superblock to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700478void bitmap_update_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700479{
480 bitmap_super_t *sb;
481 unsigned long flags;
482
483 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
NeilBrown4ad13662007-07-17 04:06:13 -0700484 return;
NeilBrown32a76272005-06-21 17:17:14 -0700485 spin_lock_irqsave(&bitmap->lock, flags);
486 if (!bitmap->sb_page) { /* no superblock */
487 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700488 return;
NeilBrown32a76272005-06-21 17:17:14 -0700489 }
NeilBrown32a76272005-06-21 17:17:14 -0700490 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800491 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700492 sb->events = cpu_to_le64(bitmap->mddev->events);
Neil Browna0da84f2008-06-28 08:31:22 +1000493 if (bitmap->mddev->events < bitmap->events_cleared) {
494 /* rocking back to read-only */
495 bitmap->events_cleared = bitmap->mddev->events;
496 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
497 }
NeilBrownea03aff2006-01-06 00:20:34 -0800498 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700499 write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700500}
501
502/* print out the bitmap file superblock */
503void bitmap_print_sb(struct bitmap *bitmap)
504{
505 bitmap_super_t *sb;
506
507 if (!bitmap || !bitmap->sb_page)
508 return;
NeilBrownea03aff2006-01-06 00:20:34 -0800509 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700510 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700511 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
512 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
513 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700514 *(__u32 *)(sb->uuid+0),
515 *(__u32 *)(sb->uuid+4),
516 *(__u32 *)(sb->uuid+8),
517 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700518 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700519 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700520 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700521 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700522 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
523 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
524 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
525 printk(KERN_DEBUG " sync size: %llu KB\n",
526 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700527 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800528 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700529}
530
531/* read the superblock from the bitmap file and initialize some bitmap fields */
532static int bitmap_read_sb(struct bitmap *bitmap)
533{
534 char *reason = NULL;
535 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700536 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700537 unsigned long long events;
538 int err = -EINVAL;
539
540 /* page 0 is the superblock, read it... */
NeilBrownf49d5e62007-01-26 00:57:03 -0800541 if (bitmap->file) {
542 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
543 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
544
545 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
546 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700547 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
NeilBrowna654b9d82005-06-21 17:17:27 -0700548 }
NeilBrown32a76272005-06-21 17:17:14 -0700549 if (IS_ERR(bitmap->sb_page)) {
550 err = PTR_ERR(bitmap->sb_page);
551 bitmap->sb_page = NULL;
552 return err;
553 }
554
NeilBrownea03aff2006-01-06 00:20:34 -0800555 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700556
NeilBrown32a76272005-06-21 17:17:14 -0700557 chunksize = le32_to_cpu(sb->chunksize);
558 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
NeilBrown4b6d2872005-09-09 16:23:47 -0700559 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700560
561 /* verify that the bitmap-specific fields are valid */
562 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
563 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800564 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
565 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700566 reason = "unrecognized superblock version";
NeilBrown7dd5d342006-01-06 00:20:39 -0800567 else if (chunksize < PAGE_SIZE)
568 reason = "bitmap chunksize too small";
NeilBrown32a76272005-06-21 17:17:14 -0700569 else if ((1 << ffz(~chunksize)) != chunksize)
570 reason = "bitmap chunksize not a power of 2";
NeilBrown7dd5d342006-01-06 00:20:39 -0800571 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT / HZ)
572 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700573 else if (write_behind > COUNTER_MAX)
574 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700575 if (reason) {
576 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
577 bmname(bitmap), reason);
578 goto out;
579 }
580
581 /* keep the array size field of the bitmap superblock up to date */
582 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
583
584 if (!bitmap->mddev->persistent)
585 goto success;
586
587 /*
588 * if we have a persistent array superblock, compare the
589 * bitmap's UUID and event counter to the mddev's
590 */
591 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
592 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
593 bmname(bitmap));
594 goto out;
595 }
596 events = le64_to_cpu(sb->events);
597 if (events < bitmap->mddev->events) {
598 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
599 "-- forcing full recovery\n", bmname(bitmap), events,
600 (unsigned long long) bitmap->mddev->events);
NeilBrown4f2e6392006-10-21 10:24:09 -0700601 sb->state |= cpu_to_le32(BITMAP_STALE);
NeilBrown32a76272005-06-21 17:17:14 -0700602 }
603success:
604 /* assign fields using values from superblock */
605 bitmap->chunksize = chunksize;
606 bitmap->daemon_sleep = daemon_sleep;
NeilBrown585f0dd2005-09-09 16:23:49 -0700607 bitmap->daemon_lastrun = jiffies;
NeilBrown4b6d2872005-09-09 16:23:47 -0700608 bitmap->max_write_behind = write_behind;
NeilBrown4f2e6392006-10-21 10:24:09 -0700609 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800610 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
611 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700612 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown4f2e6392006-10-21 10:24:09 -0700613 if (sb->state & cpu_to_le32(BITMAP_STALE))
NeilBrown6a079972005-09-09 16:23:44 -0700614 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700615 err = 0;
616out:
NeilBrownea03aff2006-01-06 00:20:34 -0800617 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700618 if (err)
619 bitmap_print_sb(bitmap);
620 return err;
621}
622
623enum bitmap_mask_op {
624 MASK_SET,
625 MASK_UNSET
626};
627
NeilBrown4ad13662007-07-17 04:06:13 -0700628/* record the state of the bitmap in the superblock. Return the old value */
629static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
630 enum bitmap_mask_op op)
NeilBrown32a76272005-06-21 17:17:14 -0700631{
632 bitmap_super_t *sb;
633 unsigned long flags;
NeilBrown4ad13662007-07-17 04:06:13 -0700634 int old;
NeilBrown32a76272005-06-21 17:17:14 -0700635
636 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800637 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700638 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700639 return 0;
NeilBrown32a76272005-06-21 17:17:14 -0700640 }
NeilBrown32a76272005-06-21 17:17:14 -0700641 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800642 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700643 old = le32_to_cpu(sb->state) & bits;
NeilBrown32a76272005-06-21 17:17:14 -0700644 switch (op) {
NeilBrown4f2e6392006-10-21 10:24:09 -0700645 case MASK_SET: sb->state |= cpu_to_le32(bits);
NeilBrown32a76272005-06-21 17:17:14 -0700646 break;
NeilBrown4f2e6392006-10-21 10:24:09 -0700647 case MASK_UNSET: sb->state &= cpu_to_le32(~bits);
NeilBrown32a76272005-06-21 17:17:14 -0700648 break;
649 default: BUG();
650 }
NeilBrownea03aff2006-01-06 00:20:34 -0800651 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700652 return old;
NeilBrown32a76272005-06-21 17:17:14 -0700653}
654
655/*
656 * general bitmap file operations
657 */
658
659/* calculate the index of the page that contains this bit */
660static inline unsigned long file_page_index(unsigned long chunk)
661{
662 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
663}
664
665/* calculate the (bit) offset of this bit within a page */
666static inline unsigned long file_page_offset(unsigned long chunk)
667{
668 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
669}
670
671/*
672 * return a pointer to the page in the filemap that contains the given bit
673 *
674 * this lookup is complicated by the fact that the bitmap sb might be exactly
675 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
676 * 0 or page 1
677 */
678static inline struct page *filemap_get_page(struct bitmap *bitmap,
679 unsigned long chunk)
680{
Paul Clements9b1d1da2006-10-03 01:15:49 -0700681 if (file_page_index(chunk) >= bitmap->file_pages) return NULL;
NeilBrown32a76272005-06-21 17:17:14 -0700682 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
683}
684
685
686static void bitmap_file_unmap(struct bitmap *bitmap)
687{
688 struct page **map, *sb_page;
689 unsigned long *attr;
690 int pages;
691 unsigned long flags;
692
693 spin_lock_irqsave(&bitmap->lock, flags);
694 map = bitmap->filemap;
695 bitmap->filemap = NULL;
696 attr = bitmap->filemap_attr;
697 bitmap->filemap_attr = NULL;
698 pages = bitmap->file_pages;
699 bitmap->file_pages = 0;
700 sb_page = bitmap->sb_page;
701 bitmap->sb_page = NULL;
702 spin_unlock_irqrestore(&bitmap->lock, flags);
703
704 while (pages--)
705 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700706 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700707 kfree(map);
708 kfree(attr);
709
NeilBrownd785a062006-06-26 00:27:48 -0700710 if (sb_page)
711 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700712}
713
714static void bitmap_file_put(struct bitmap *bitmap)
715{
716 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700717 unsigned long flags;
718
719 spin_lock_irqsave(&bitmap->lock, flags);
720 file = bitmap->file;
721 bitmap->file = NULL;
722 spin_unlock_irqrestore(&bitmap->lock, flags);
723
NeilBrownd785a062006-06-26 00:27:48 -0700724 if (file)
725 wait_event(bitmap->write_wait,
726 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700727 bitmap_file_unmap(bitmap);
728
NeilBrownd785a062006-06-26 00:27:48 -0700729 if (file) {
Josef Sipekc649bb92006-12-08 02:37:19 -0800730 struct inode *inode = file->f_path.dentry->d_inode;
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800731 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700732 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700733 }
NeilBrown32a76272005-06-21 17:17:14 -0700734}
735
736
737/*
738 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
739 * then it is no longer reliable, so we stop using it and we mark the file
740 * as failed in the superblock
741 */
742static void bitmap_file_kick(struct bitmap *bitmap)
743{
744 char *path, *ptr = NULL;
745
NeilBrown4ad13662007-07-17 04:06:13 -0700746 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
747 bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700748
NeilBrown4ad13662007-07-17 04:06:13 -0700749 if (bitmap->file) {
750 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
751 if (path)
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700752 ptr = d_path(&bitmap->file->f_path, path,
753 PAGE_SIZE);
754
NeilBrown32a76272005-06-21 17:17:14 -0700755
NeilBrown4ad13662007-07-17 04:06:13 -0700756 printk(KERN_ALERT
757 "%s: kicking failed bitmap file %s from array!\n",
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700758 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700759
NeilBrown4ad13662007-07-17 04:06:13 -0700760 kfree(path);
761 } else
762 printk(KERN_ALERT
763 "%s: disabling internal bitmap due to errors\n",
764 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700765 }
NeilBrown32a76272005-06-21 17:17:14 -0700766
767 bitmap_file_put(bitmap);
768
769 return;
770}
771
772enum bitmap_page_attr {
NeilBrowne16b68b2006-06-26 00:27:45 -0700773 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
774 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
775 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
NeilBrown32a76272005-06-21 17:17:14 -0700776};
777
778static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
779 enum bitmap_page_attr attr)
780{
NeilBrowne16b68b2006-06-26 00:27:45 -0700781 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700782}
783
784static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
785 enum bitmap_page_attr attr)
786{
NeilBrowne16b68b2006-06-26 00:27:45 -0700787 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700788}
789
NeilBrownec7a3192006-06-26 00:27:45 -0700790static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
791 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700792{
NeilBrowne16b68b2006-06-26 00:27:45 -0700793 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700794}
795
796/*
797 * bitmap_file_set_bit -- called before performing a write to the md device
798 * to set (and eventually sync) a particular bit in the bitmap file
799 *
800 * we set the bit immediately, then we record the page number so that
801 * when an unplug occurs, we can flush the dirty pages out to disk
802 */
803static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
804{
805 unsigned long bit;
806 struct page *page;
807 void *kaddr;
808 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
809
NeilBrowna654b9d82005-06-21 17:17:27 -0700810 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700811 return;
812 }
813
814 page = filemap_get_page(bitmap, chunk);
Paul Clements9b1d1da2006-10-03 01:15:49 -0700815 if (!page) return;
NeilBrown32a76272005-06-21 17:17:14 -0700816 bit = file_page_offset(chunk);
817
NeilBrown32a76272005-06-21 17:17:14 -0700818 /* set the bit */
819 kaddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800820 if (bitmap->flags & BITMAP_HOSTENDIAN)
821 set_bit(bit, kaddr);
822 else
823 ext2_set_bit(bit, kaddr);
NeilBrown32a76272005-06-21 17:17:14 -0700824 kunmap_atomic(kaddr, KM_USER0);
825 PRINTK("set file bit %lu page %lu\n", bit, page->index);
826
827 /* record page number so it gets flushed to disk when unplug occurs */
828 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
829
830}
831
832/* this gets called when the md device is ready to unplug its underlying
833 * (slave) device queues -- before we let any writes go down, we need to
834 * sync the dirty pages of the bitmap file to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700835void bitmap_unplug(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700836{
NeilBrownec7a3192006-06-26 00:27:45 -0700837 unsigned long i, flags;
838 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700839 struct page *page;
840 int wait = 0;
841
842 if (!bitmap)
NeilBrown4ad13662007-07-17 04:06:13 -0700843 return;
NeilBrown32a76272005-06-21 17:17:14 -0700844
845 /* look at each page to see if there are any set bits that need to be
846 * flushed out to disk */
847 for (i = 0; i < bitmap->file_pages; i++) {
848 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700849 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700850 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700851 return;
NeilBrown32a76272005-06-21 17:17:14 -0700852 }
853 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700854 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
855 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700856 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
857 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700858 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700859 wait = 1;
860 spin_unlock_irqrestore(&bitmap->lock, flags);
861
NeilBrownd785a062006-06-26 00:27:48 -0700862 if (dirty | need_write)
NeilBrown4ad13662007-07-17 04:06:13 -0700863 write_page(bitmap, page, 0);
NeilBrown32a76272005-06-21 17:17:14 -0700864 }
865 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700866 if (bitmap->file)
NeilBrownd785a062006-06-26 00:27:48 -0700867 wait_event(bitmap->write_wait,
868 atomic_read(&bitmap->pending_writes)==0);
NeilBrown0b79ccf2006-06-26 00:27:44 -0700869 else
NeilBrowna9701a32005-11-08 21:39:34 -0800870 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700871 }
NeilBrownd785a062006-06-26 00:27:48 -0700872 if (bitmap->flags & BITMAP_WRITE_ERROR)
873 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700874}
875
NeilBrown6a079972005-09-09 16:23:44 -0700876static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700877/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
878 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
879 * memory mapping of the bitmap file
880 * Special cases:
881 * if there's no bitmap file, or if the bitmap file had been
882 * previously kicked from the array, we mark all the bits as
883 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700884 *
885 * We ignore all bits for sectors that end earlier than 'start'.
886 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700887 */
NeilBrown6a079972005-09-09 16:23:44 -0700888static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700889{
890 unsigned long i, chunks, index, oldindex, bit;
891 struct page *page = NULL, *oldpage = NULL;
892 unsigned long num_pages, bit_cnt = 0;
893 struct file *file;
NeilBrownd785a062006-06-26 00:27:48 -0700894 unsigned long bytes, offset;
NeilBrown32a76272005-06-21 17:17:14 -0700895 int outofdate;
896 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800897 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700898
899 chunks = bitmap->chunks;
900 file = bitmap->file;
901
NeilBrowna654b9d82005-06-21 17:17:27 -0700902 BUG_ON(!file && !bitmap->offset);
NeilBrown32a76272005-06-21 17:17:14 -0700903
Olaf Hering44456d32005-07-27 11:45:17 -0700904#ifdef INJECT_FAULTS_3
NeilBrown32a76272005-06-21 17:17:14 -0700905 outofdate = 1;
906#else
907 outofdate = bitmap->flags & BITMAP_STALE;
908#endif
909 if (outofdate)
910 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
911 "recovery\n", bmname(bitmap));
912
913 bytes = (chunks + 7) / 8;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700914
NeilBrowncdbb4cc2005-06-21 17:17:18 -0700915 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700916
NeilBrowna654b9d82005-06-21 17:17:27 -0700917 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
NeilBrown32a76272005-06-21 17:17:14 -0700918 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
919 bmname(bitmap),
920 (unsigned long) i_size_read(file->f_mapping->host),
921 bytes + sizeof(bitmap_super_t));
NeilBrown4ad13662007-07-17 04:06:13 -0700922 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700923 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700924
925 ret = -ENOMEM;
926
NeilBrown32a76272005-06-21 17:17:14 -0700927 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700928 if (!bitmap->filemap)
NeilBrown4ad13662007-07-17 04:06:13 -0700929 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700930
NeilBrowne16b68b2006-06-26 00:27:45 -0700931 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
932 bitmap->filemap_attr = kzalloc(
Neil Brown505fa2c2007-04-11 23:28:44 -0700933 roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
NeilBrowne16b68b2006-06-26 00:27:45 -0700934 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700935 if (!bitmap->filemap_attr)
NeilBrown4ad13662007-07-17 04:06:13 -0700936 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700937
NeilBrown32a76272005-06-21 17:17:14 -0700938 oldindex = ~0L;
939
940 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -0800941 int b;
NeilBrown32a76272005-06-21 17:17:14 -0700942 index = file_page_index(i);
943 bit = file_page_offset(i);
944 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -0700945 int count;
NeilBrown32a76272005-06-21 17:17:14 -0700946 /* unmap the old page, we're done with it */
NeilBrownd785a062006-06-26 00:27:48 -0700947 if (index == num_pages-1)
NeilBrownf49d5e62007-01-26 00:57:03 -0800948 count = bytes + sizeof(bitmap_super_t)
949 - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -0700950 else
951 count = PAGE_SIZE;
NeilBrown32a76272005-06-21 17:17:14 -0700952 if (index == 0) {
953 /*
954 * if we're here then the superblock page
955 * contains some bits (PAGE_SIZE != sizeof sb)
956 * we've already read it in, so just use it
957 */
958 page = bitmap->sb_page;
959 offset = sizeof(bitmap_super_t);
NeilBrowna654b9d82005-06-21 17:17:27 -0700960 } else if (file) {
NeilBrownd785a062006-06-26 00:27:48 -0700961 page = read_page(file, index, bitmap, count);
NeilBrowna654b9d82005-06-21 17:17:27 -0700962 offset = 0;
963 } else {
964 page = read_sb_page(bitmap->mddev, bitmap->offset, index);
NeilBrown32a76272005-06-21 17:17:14 -0700965 offset = 0;
966 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700967 if (IS_ERR(page)) { /* read error */
968 ret = PTR_ERR(page);
NeilBrown4ad13662007-07-17 04:06:13 -0700969 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -0700970 }
971
NeilBrown32a76272005-06-21 17:17:14 -0700972 oldindex = index;
973 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -0700974
975 if (outofdate) {
976 /*
977 * if bitmap is out of date, dirty the
978 * whole page and write it out
979 */
NeilBrownea03aff2006-01-06 00:20:34 -0800980 paddr = kmap_atomic(page, KM_USER0);
981 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -0700982 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -0800983 kunmap_atomic(paddr, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700984 write_page(bitmap, page, 1);
985
986 ret = -EIO;
987 if (bitmap->flags & BITMAP_WRITE_ERROR) {
NeilBrown32a76272005-06-21 17:17:14 -0700988 /* release, page not in filemap yet */
NeilBrown2d1f3b52006-01-06 00:20:31 -0800989 put_page(page);
NeilBrown4ad13662007-07-17 04:06:13 -0700990 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700991 }
992 }
993
994 bitmap->filemap[bitmap->file_pages++] = page;
NeilBrownab6085c2007-05-23 13:58:10 -0700995 bitmap->last_page_size = count;
NeilBrown32a76272005-06-21 17:17:14 -0700996 }
NeilBrownea03aff2006-01-06 00:20:34 -0800997 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800998 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -0800999 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001000 else
NeilBrownea03aff2006-01-06 00:20:34 -08001001 b = ext2_test_bit(bit, paddr);
1002 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001003 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -07001004 /* if the disk bit is set, set the memory bit */
NeilBrown6a079972005-09-09 16:23:44 -07001005 bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
1006 ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
1007 );
NeilBrown32a76272005-06-21 17:17:14 -07001008 bit_cnt++;
NeilBrown6a079972005-09-09 16:23:44 -07001009 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -07001010 }
NeilBrown32a76272005-06-21 17:17:14 -07001011 }
1012
1013 /* everything went OK */
1014 ret = 0;
1015 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1016
NeilBrown32a76272005-06-21 17:17:14 -07001017 if (bit_cnt) { /* Kick recovery if any bits were set */
1018 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1019 md_wakeup_thread(bitmap->mddev->thread);
1020 }
1021
NeilBrown32a76272005-06-21 17:17:14 -07001022 printk(KERN_INFO "%s: bitmap initialized from disk: "
NeilBrown4ad13662007-07-17 04:06:13 -07001023 "read %lu/%lu pages, set %lu bits\n",
1024 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
NeilBrown32a76272005-06-21 17:17:14 -07001025
NeilBrown4ad13662007-07-17 04:06:13 -07001026 return 0;
1027
1028 err:
1029 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1030 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001031 return ret;
1032}
1033
NeilBrowna654b9d82005-06-21 17:17:27 -07001034void bitmap_write_all(struct bitmap *bitmap)
1035{
1036 /* We don't actually write all bitmap blocks here,
1037 * just flag them as needing to be written
1038 */
NeilBrownec7a3192006-06-26 00:27:45 -07001039 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001040
NeilBrownec7a3192006-06-26 00:27:45 -07001041 for (i=0; i < bitmap->file_pages; i++)
1042 set_page_attr(bitmap, bitmap->filemap[i],
1043 BITMAP_PAGE_NEEDWRITE);
NeilBrowna654b9d82005-06-21 17:17:27 -07001044}
1045
NeilBrown32a76272005-06-21 17:17:14 -07001046
1047static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1048{
1049 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1050 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1051 bitmap->bp[page].count += inc;
1052/*
1053 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1054 (unsigned long long)offset, inc, bitmap->bp[page].count);
1055*/
1056 bitmap_checkfree(bitmap, page);
1057}
1058static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1059 sector_t offset, int *blocks,
1060 int create);
1061
1062/*
1063 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1064 * out to disk
1065 */
1066
NeilBrown4ad13662007-07-17 04:06:13 -07001067void bitmap_daemon_work(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001068{
NeilBrownaa3163f2005-06-21 17:17:22 -07001069 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001070 unsigned long flags;
1071 struct page *page = NULL, *lastpage = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001072 int blocks;
NeilBrownea03aff2006-01-06 00:20:34 -08001073 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001074
1075 if (bitmap == NULL)
NeilBrown4ad13662007-07-17 04:06:13 -07001076 return;
NeilBrown32a76272005-06-21 17:17:14 -07001077 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001078 goto done;
1079
NeilBrown32a76272005-06-21 17:17:14 -07001080 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001081 if (bitmap->allclean) {
1082 bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1083 return;
1084 }
1085 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001086
1087 for (j = 0; j < bitmap->chunks; j++) {
1088 bitmap_counter_t *bmc;
1089 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -07001090 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -07001091 /* error or shutdown */
1092 spin_unlock_irqrestore(&bitmap->lock, flags);
1093 break;
1094 }
1095
1096 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001097
1098 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001099 /* skip this page unless it's marked as needing cleaning */
NeilBrownec7a3192006-06-26 00:27:45 -07001100 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1101 int need_write = test_page_attr(bitmap, page,
1102 BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001103 if (need_write)
NeilBrownaa3163f2005-06-21 17:17:22 -07001104 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001105
NeilBrownaa3163f2005-06-21 17:17:22 -07001106 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown8311c292008-03-04 14:29:30 -08001107 if (need_write) {
NeilBrown4ad13662007-07-17 04:06:13 -07001108 write_page(bitmap, page, 0);
NeilBrown8311c292008-03-04 14:29:30 -08001109 bitmap->allclean = 0;
1110 }
NeilBrownaa3163f2005-06-21 17:17:22 -07001111 continue;
1112 }
1113
NeilBrown32a76272005-06-21 17:17:14 -07001114 /* grab the new page, sync and release the old */
NeilBrown32a76272005-06-21 17:17:14 -07001115 if (lastpage != NULL) {
NeilBrownec7a3192006-06-26 00:27:45 -07001116 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001117 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1118 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001119 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001120 } else {
1121 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1122 spin_unlock_irqrestore(&bitmap->lock, flags);
1123 }
NeilBrown32a76272005-06-21 17:17:14 -07001124 } else
1125 spin_unlock_irqrestore(&bitmap->lock, flags);
1126 lastpage = page;
Neil Browna0da84f2008-06-28 08:31:22 +10001127
1128 /* We are possibly going to clear some bits, so make
1129 * sure that events_cleared is up-to-date.
1130 */
1131 if (bitmap->need_sync) {
1132 bitmap_super_t *sb;
1133 bitmap->need_sync = 0;
1134 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1135 sb->events_cleared =
1136 cpu_to_le64(bitmap->events_cleared);
1137 kunmap_atomic(sb, KM_USER0);
1138 write_page(bitmap, bitmap->sb_page, 1);
1139 }
NeilBrown32a76272005-06-21 17:17:14 -07001140 spin_lock_irqsave(&bitmap->lock, flags);
1141 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1142 }
1143 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1144 &blocks, 0);
1145 if (bmc) {
1146/*
1147 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1148*/
NeilBrown8311c292008-03-04 14:29:30 -08001149 if (*bmc)
1150 bitmap->allclean = 0;
1151
NeilBrown32a76272005-06-21 17:17:14 -07001152 if (*bmc == 2) {
1153 *bmc=1; /* maybe clear the bit next time */
1154 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1155 } else if (*bmc == 1) {
1156 /* we can clear the bit */
1157 *bmc = 0;
1158 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1159 -1);
1160
1161 /* clear the bit */
NeilBrownea03aff2006-01-06 00:20:34 -08001162 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001163 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001164 clear_bit(file_page_offset(j), paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001165 else
NeilBrownea03aff2006-01-06 00:20:34 -08001166 ext2_clear_bit(file_page_offset(j), paddr);
1167 kunmap_atomic(paddr, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -07001168 }
1169 }
1170 spin_unlock_irqrestore(&bitmap->lock, flags);
1171 }
1172
1173 /* now sync the final page */
1174 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001175 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001176 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001177 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1178 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001179 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001180 } else {
1181 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1182 spin_unlock_irqrestore(&bitmap->lock, flags);
1183 }
NeilBrown32a76272005-06-21 17:17:14 -07001184 }
1185
NeilBrown7be3dfe2008-03-10 11:43:48 -07001186 done:
NeilBrown8311c292008-03-04 14:29:30 -08001187 if (bitmap->allclean == 0)
1188 bitmap->mddev->thread->timeout = bitmap->daemon_sleep * HZ;
NeilBrown32a76272005-06-21 17:17:14 -07001189}
1190
NeilBrown32a76272005-06-21 17:17:14 -07001191static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1192 sector_t offset, int *blocks,
1193 int create)
1194{
1195 /* If 'create', we might release the lock and reclaim it.
1196 * The lock must have been taken with interrupts enabled.
1197 * If !create, we don't release the lock.
1198 */
1199 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1200 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1201 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1202 sector_t csize;
1203
1204 if (bitmap_checkpage(bitmap, page, create) < 0) {
1205 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1206 *blocks = csize - (offset & (csize- 1));
1207 return NULL;
1208 }
1209 /* now locked ... */
1210
1211 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1212 /* should we use the first or second counter field
1213 * of the hijacked pointer? */
1214 int hi = (pageoff > PAGE_COUNTER_MASK);
1215 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1216 PAGE_COUNTER_SHIFT - 1);
1217 *blocks = csize - (offset & (csize- 1));
1218 return &((bitmap_counter_t *)
1219 &bitmap->bp[page].map)[hi];
1220 } else { /* page is allocated */
1221 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1222 *blocks = csize - (offset & (csize- 1));
1223 return (bitmap_counter_t *)
1224 &(bitmap->bp[page].map[pageoff]);
1225 }
1226}
1227
NeilBrown4b6d2872005-09-09 16:23:47 -07001228int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001229{
1230 if (!bitmap) return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001231
1232 if (behind) {
1233 atomic_inc(&bitmap->behind_writes);
1234 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1235 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1236 }
1237
NeilBrown32a76272005-06-21 17:17:14 -07001238 while (sectors) {
1239 int blocks;
1240 bitmap_counter_t *bmc;
1241
1242 spin_lock_irq(&bitmap->lock);
1243 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1244 if (!bmc) {
1245 spin_unlock_irq(&bitmap->lock);
1246 return 0;
1247 }
1248
Neil Brownda6e1a32007-02-08 14:20:37 -08001249 if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
1250 DEFINE_WAIT(__wait);
1251 /* note that it is safe to do the prepare_to_wait
1252 * after the test as long as we do it before dropping
1253 * the spinlock.
1254 */
1255 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1256 TASK_UNINTERRUPTIBLE);
1257 spin_unlock_irq(&bitmap->lock);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05001258 blk_unplug(bitmap->mddev->queue);
Neil Brownda6e1a32007-02-08 14:20:37 -08001259 schedule();
1260 finish_wait(&bitmap->overflow_wait, &__wait);
1261 continue;
1262 }
1263
NeilBrown32a76272005-06-21 17:17:14 -07001264 switch(*bmc) {
1265 case 0:
1266 bitmap_file_set_bit(bitmap, offset);
1267 bitmap_count_page(bitmap,offset, 1);
Jens Axboe93769f52008-08-01 20:32:31 +02001268 blk_plug_device_unlocked(bitmap->mddev->queue);
NeilBrown32a76272005-06-21 17:17:14 -07001269 /* fall through */
1270 case 1:
1271 *bmc = 2;
1272 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001273
NeilBrown32a76272005-06-21 17:17:14 -07001274 (*bmc)++;
1275
1276 spin_unlock_irq(&bitmap->lock);
1277
1278 offset += blocks;
1279 if (sectors > blocks)
1280 sectors -= blocks;
1281 else sectors = 0;
1282 }
NeilBrown8311c292008-03-04 14:29:30 -08001283 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001284 return 0;
1285}
1286
1287void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001288 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001289{
1290 if (!bitmap) return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001291 if (behind) {
1292 atomic_dec(&bitmap->behind_writes);
1293 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1294 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1295 }
1296
NeilBrown32a76272005-06-21 17:17:14 -07001297 while (sectors) {
1298 int blocks;
1299 unsigned long flags;
1300 bitmap_counter_t *bmc;
1301
1302 spin_lock_irqsave(&bitmap->lock, flags);
1303 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1304 if (!bmc) {
1305 spin_unlock_irqrestore(&bitmap->lock, flags);
1306 return;
1307 }
1308
Neil Browna0da84f2008-06-28 08:31:22 +10001309 if (success &&
1310 bitmap->events_cleared < bitmap->mddev->events) {
1311 bitmap->events_cleared = bitmap->mddev->events;
1312 bitmap->need_sync = 1;
1313 }
1314
NeilBrown32a76272005-06-21 17:17:14 -07001315 if (!success && ! (*bmc & NEEDED_MASK))
1316 *bmc |= NEEDED_MASK;
1317
Neil Brownda6e1a32007-02-08 14:20:37 -08001318 if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
1319 wake_up(&bitmap->overflow_wait);
1320
NeilBrown32a76272005-06-21 17:17:14 -07001321 (*bmc)--;
1322 if (*bmc <= 2) {
1323 set_page_attr(bitmap,
1324 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1325 BITMAP_PAGE_CLEAN);
1326 }
1327 spin_unlock_irqrestore(&bitmap->lock, flags);
1328 offset += blocks;
1329 if (sectors > blocks)
1330 sectors -= blocks;
1331 else sectors = 0;
1332 }
1333}
1334
NeilBrown6a806c52005-07-15 03:56:35 -07001335int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1336 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001337{
1338 bitmap_counter_t *bmc;
1339 int rv;
1340 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1341 *blocks = 1024;
1342 return 1; /* always resync if no bitmap */
1343 }
1344 spin_lock_irq(&bitmap->lock);
1345 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1346 rv = 0;
1347 if (bmc) {
1348 /* locked */
1349 if (RESYNC(*bmc))
1350 rv = 1;
1351 else if (NEEDED(*bmc)) {
1352 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001353 if (!degraded) { /* don't set/clear bits if degraded */
1354 *bmc |= RESYNC_MASK;
1355 *bmc &= ~NEEDED_MASK;
1356 }
NeilBrown32a76272005-06-21 17:17:14 -07001357 }
1358 }
1359 spin_unlock_irq(&bitmap->lock);
NeilBrown8311c292008-03-04 14:29:30 -08001360 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001361 return rv;
1362}
1363
1364void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1365{
1366 bitmap_counter_t *bmc;
1367 unsigned long flags;
1368/*
1369 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1370*/ if (bitmap == NULL) {
1371 *blocks = 1024;
1372 return;
1373 }
1374 spin_lock_irqsave(&bitmap->lock, flags);
1375 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1376 if (bmc == NULL)
1377 goto unlock;
1378 /* locked */
1379/*
1380 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1381*/
1382 if (RESYNC(*bmc)) {
1383 *bmc &= ~RESYNC_MASK;
1384
1385 if (!NEEDED(*bmc) && aborted)
1386 *bmc |= NEEDED_MASK;
1387 else {
1388 if (*bmc <= 2) {
1389 set_page_attr(bitmap,
1390 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1391 BITMAP_PAGE_CLEAN);
1392 }
1393 }
1394 }
1395 unlock:
1396 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown8311c292008-03-04 14:29:30 -08001397 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001398}
1399
1400void bitmap_close_sync(struct bitmap *bitmap)
1401{
1402 /* Sync has finished, and any bitmap chunks that weren't synced
1403 * properly have been aborted. It remains to us to clear the
1404 * RESYNC bit wherever it is still on
1405 */
1406 sector_t sector = 0;
1407 int blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001408 if (!bitmap)
1409 return;
NeilBrown32a76272005-06-21 17:17:14 -07001410 while (sector < bitmap->mddev->resync_max_sectors) {
1411 bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001412 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001413 }
1414}
1415
NeilBrownb47490c2008-02-06 01:39:50 -08001416void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1417{
1418 sector_t s = 0;
1419 int blocks;
1420
1421 if (!bitmap)
1422 return;
1423 if (sector == 0) {
1424 bitmap->last_end_sync = jiffies;
1425 return;
1426 }
1427 if (time_before(jiffies, (bitmap->last_end_sync
1428 + bitmap->daemon_sleep * HZ)))
1429 return;
1430 wait_event(bitmap->mddev->recovery_wait,
1431 atomic_read(&bitmap->mddev->recovery_active) == 0);
1432
1433 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1434 s = 0;
1435 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1436 bitmap_end_sync(bitmap, s, &blocks, 0);
1437 s += blocks;
1438 }
1439 bitmap->last_end_sync = jiffies;
1440}
1441
NeilBrown6a079972005-09-09 16:23:44 -07001442static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001443{
1444 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001445 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001446 * be 0 at this point
1447 */
NeilBrown193f1c92005-08-04 12:53:33 -07001448
1449 int secs;
1450 bitmap_counter_t *bmc;
1451 spin_lock_irq(&bitmap->lock);
1452 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1453 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001454 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001455 return;
NeilBrown32a76272005-06-21 17:17:14 -07001456 }
NeilBrown193f1c92005-08-04 12:53:33 -07001457 if (! *bmc) {
1458 struct page *page;
NeilBrown6a079972005-09-09 16:23:44 -07001459 *bmc = 1 | (needed?NEEDED_MASK:0);
NeilBrown193f1c92005-08-04 12:53:33 -07001460 bitmap_count_page(bitmap, offset, 1);
1461 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1462 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1463 }
1464 spin_unlock_irq(&bitmap->lock);
NeilBrown8311c292008-03-04 14:29:30 -08001465 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001466}
1467
Paul Clements9b1d1da2006-10-03 01:15:49 -07001468/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1469void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1470{
1471 unsigned long chunk;
1472
1473 for (chunk = s; chunk <= e; chunk++) {
1474 sector_t sec = chunk << CHUNK_BLOCK_SHIFT(bitmap);
1475 bitmap_set_memory_bits(bitmap, sec, 1);
1476 bitmap_file_set_bit(bitmap, sec);
1477 }
1478}
1479
NeilBrown32a76272005-06-21 17:17:14 -07001480/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001481 * flush out any pending updates
1482 */
1483void bitmap_flush(mddev_t *mddev)
1484{
1485 struct bitmap *bitmap = mddev->bitmap;
1486 int sleep;
1487
1488 if (!bitmap) /* there was no bitmap */
1489 return;
1490
1491 /* run the daemon_work three time to ensure everything is flushed
1492 * that can be
1493 */
1494 sleep = bitmap->daemon_sleep;
1495 bitmap->daemon_sleep = 0;
1496 bitmap_daemon_work(bitmap);
1497 bitmap_daemon_work(bitmap);
1498 bitmap_daemon_work(bitmap);
1499 bitmap->daemon_sleep = sleep;
1500 bitmap_update_sb(bitmap);
1501}
1502
1503/*
NeilBrown32a76272005-06-21 17:17:14 -07001504 * free memory that was allocated
1505 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001506static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001507{
1508 unsigned long k, pages;
1509 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001510
1511 if (!bitmap) /* there was no bitmap */
1512 return;
1513
NeilBrown32a76272005-06-21 17:17:14 -07001514 /* release the bitmap file and kill the daemon */
1515 bitmap_file_put(bitmap);
1516
1517 bp = bitmap->bp;
1518 pages = bitmap->pages;
1519
1520 /* free all allocated memory */
1521
NeilBrown32a76272005-06-21 17:17:14 -07001522 if (bp) /* deallocate the page memory */
1523 for (k = 0; k < pages; k++)
1524 if (bp[k].map && !bp[k].hijacked)
1525 kfree(bp[k].map);
1526 kfree(bp);
1527 kfree(bitmap);
1528}
NeilBrown3178b0d2005-09-09 16:23:50 -07001529void bitmap_destroy(mddev_t *mddev)
1530{
1531 struct bitmap *bitmap = mddev->bitmap;
1532
1533 if (!bitmap) /* there was no bitmap */
1534 return;
1535
1536 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownb15c2e52006-01-06 00:20:16 -08001537 if (mddev->thread)
1538 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001539
1540 bitmap_free(bitmap);
1541}
NeilBrown32a76272005-06-21 17:17:14 -07001542
1543/*
1544 * initialize the bitmap structure
1545 * if this returns an error, bitmap_destroy must be called to do clean up
1546 */
1547int bitmap_create(mddev_t *mddev)
1548{
1549 struct bitmap *bitmap;
1550 unsigned long blocks = mddev->resync_max_sectors;
1551 unsigned long chunks;
1552 unsigned long pages;
1553 struct file *file = mddev->bitmap_file;
1554 int err;
NeilBrown6a079972005-09-09 16:23:44 -07001555 sector_t start;
NeilBrown32a76272005-06-21 17:17:14 -07001556
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001557 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001558
NeilBrowna654b9d82005-06-21 17:17:27 -07001559 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001560 return 0;
1561
NeilBrowna654b9d82005-06-21 17:17:27 -07001562 BUG_ON(file && mddev->bitmap_offset);
1563
NeilBrown9ffae0c2006-01-06 00:20:32 -08001564 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001565 if (!bitmap)
1566 return -ENOMEM;
1567
NeilBrown32a76272005-06-21 17:17:14 -07001568 spin_lock_init(&bitmap->lock);
NeilBrownce25c312006-06-26 00:27:49 -07001569 atomic_set(&bitmap->pending_writes, 0);
1570 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001571 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001572
NeilBrown32a76272005-06-21 17:17:14 -07001573 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001574
NeilBrown32a76272005-06-21 17:17:14 -07001575 bitmap->file = file;
NeilBrowna654b9d82005-06-21 17:17:27 -07001576 bitmap->offset = mddev->bitmap_offset;
NeilBrownce25c312006-06-26 00:27:49 -07001577 if (file) {
1578 get_file(file);
Mark Fashehef51c972007-05-08 00:27:10 -07001579 do_sync_mapping_range(file->f_mapping, 0, LLONG_MAX,
1580 SYNC_FILE_RANGE_WAIT_BEFORE |
1581 SYNC_FILE_RANGE_WRITE |
1582 SYNC_FILE_RANGE_WAIT_AFTER);
NeilBrownce25c312006-06-26 00:27:49 -07001583 }
NeilBrown32a76272005-06-21 17:17:14 -07001584 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1585 err = bitmap_read_sb(bitmap);
1586 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001587 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001588
Paul Clementsa638b2d2006-10-03 01:16:01 -07001589 bitmap->chunkshift = ffz(~bitmap->chunksize);
NeilBrown32a76272005-06-21 17:17:14 -07001590
1591 /* now that chunksize and chunkshift are set, we can use these macros */
1592 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1593 CHUNK_BLOCK_RATIO(bitmap);
1594 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1595
1596 BUG_ON(!pages);
1597
1598 bitmap->chunks = chunks;
1599 bitmap->pages = pages;
1600 bitmap->missing_pages = pages;
1601 bitmap->counter_bits = COUNTER_BITS;
1602
1603 bitmap->syncchunk = ~0UL;
1604
Olaf Hering44456d32005-07-27 11:45:17 -07001605#ifdef INJECT_FATAL_FAULT_1
NeilBrown32a76272005-06-21 17:17:14 -07001606 bitmap->bp = NULL;
1607#else
NeilBrown9ffae0c2006-01-06 00:20:32 -08001608 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001609#endif
NeilBrown3178b0d2005-09-09 16:23:50 -07001610 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001611 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001612 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001613
NeilBrown32a76272005-06-21 17:17:14 -07001614 /* now that we have some pages available, initialize the in-memory
1615 * bitmap from the on-disk bitmap */
NeilBrown6a079972005-09-09 16:23:44 -07001616 start = 0;
1617 if (mddev->degraded == 0
1618 || bitmap->events_cleared == mddev->events)
1619 /* no need to keep dirty bits to optimise a re-add of a missing device */
1620 start = mddev->recovery_cp;
1621 err = bitmap_init_from_disk(bitmap, start);
NeilBrown193f1c92005-08-04 12:53:33 -07001622
NeilBrown32a76272005-06-21 17:17:14 -07001623 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001624 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001625
1626 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1627 pages, bmname(bitmap));
1628
NeilBrown3178b0d2005-09-09 16:23:50 -07001629 mddev->bitmap = bitmap;
1630
NeilBrownb15c2e52006-01-06 00:20:16 -08001631 mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1632
NeilBrown4ad13662007-07-17 04:06:13 -07001633 bitmap_update_sb(bitmap);
1634
1635 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
NeilBrown3178b0d2005-09-09 16:23:50 -07001636
1637 error:
1638 bitmap_free(bitmap);
1639 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001640}
1641
1642/* the bitmap API -- for raid personalities */
1643EXPORT_SYMBOL(bitmap_startwrite);
1644EXPORT_SYMBOL(bitmap_endwrite);
1645EXPORT_SYMBOL(bitmap_start_sync);
1646EXPORT_SYMBOL(bitmap_end_sync);
1647EXPORT_SYMBOL(bitmap_unplug);
1648EXPORT_SYMBOL(bitmap_close_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08001649EXPORT_SYMBOL(bitmap_cond_end_sync);