blob: ab7c8e4a61f943c516ae5a3534e8775c33180f6b [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 */
NeilBrowna2ed9612008-12-19 16:25:01 +1100211static struct page *read_sb_page(mddev_t *mddev, long offset,
212 struct page *page,
213 unsigned long index, int size)
NeilBrowna654b9d82005-06-21 17:17:27 -0700214{
215 /* choose a good rdev and read the page from there */
216
217 mdk_rdev_t *rdev;
218 struct list_head *tmp;
NeilBrowna654b9d82005-06-21 17:17:27 -0700219 sector_t target;
220
221 if (!page)
NeilBrowna2ed9612008-12-19 16:25:01 +1100222 page = alloc_page(GFP_KERNEL);
223 if (!page)
NeilBrowna654b9d82005-06-21 17:17:27 -0700224 return ERR_PTR(-ENOMEM);
NeilBrowna654b9d82005-06-21 17:17:27 -0700225
NeilBrownd089c6a2008-02-06 01:39:59 -0800226 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800227 if (! test_bit(In_sync, &rdev->flags)
228 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700229 continue;
230
Andre Noll0f420352008-07-11 22:02:23 +1000231 target = rdev->sb_start + offset + index * (PAGE_SIZE/512);
NeilBrowna654b9d82005-06-21 17:17:27 -0700232
NeilBrowna2ed9612008-12-19 16:25:01 +1100233 if (sync_page_io(rdev->bdev, target,
234 roundup(size, bdev_hardsect_size(rdev->bdev)),
235 page, READ)) {
NeilBrownab904d62005-09-09 16:23:52 -0700236 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700237 attach_page_buffers(page, NULL); /* so that free_buffer will
238 * quietly no-op */
NeilBrownab904d62005-09-09 16:23:52 -0700239 return page;
240 }
241 }
242 return ERR_PTR(-EIO);
NeilBrowna654b9d82005-06-21 17:17:27 -0700243
NeilBrowna654b9d82005-06-21 17:17:27 -0700244}
245
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000246static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
247{
248 /* Iterate the disks of an mddev, using rcu to protect access to the
249 * linked list, and raising the refcount of devices we return to ensure
250 * they don't disappear while in use.
251 * As devices are only added or removed when raid_disk is < 0 and
252 * nr_pending is 0 and In_sync is clear, the entries we return will
253 * still be in the same position on the list when we re-enter
254 * list_for_each_continue_rcu.
255 */
256 struct list_head *pos;
257 rcu_read_lock();
258 if (rdev == NULL)
259 /* start at the beginning */
260 pos = &mddev->disks;
261 else {
262 /* release the previous rdev and start from there. */
263 rdev_dec_pending(rdev, mddev);
264 pos = &rdev->same_set;
265 }
266 list_for_each_continue_rcu(pos, &mddev->disks) {
267 rdev = list_entry(pos, mdk_rdev_t, same_set);
268 if (rdev->raid_disk >= 0 &&
269 test_bit(In_sync, &rdev->flags) &&
270 !test_bit(Faulty, &rdev->flags)) {
271 /* this is a usable devices */
272 atomic_inc(&rdev->nr_pending);
273 rcu_read_unlock();
274 return rdev;
275 }
276 }
277 rcu_read_unlock();
278 return NULL;
279}
280
NeilBrownab6085c2007-05-23 13:58:10 -0700281static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrowna654b9d82005-06-21 17:17:27 -0700282{
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000283 mdk_rdev_t *rdev = NULL;
NeilBrownab6085c2007-05-23 13:58:10 -0700284 mddev_t *mddev = bitmap->mddev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700285
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000286 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
NeilBrownab6085c2007-05-23 13:58:10 -0700287 int size = PAGE_SIZE;
288 if (page->index == bitmap->file_pages-1)
289 size = roundup(bitmap->last_page_size,
290 bdev_hardsect_size(rdev->bdev));
NeilBrownf0d76d72007-07-17 04:06:12 -0700291 /* Just make sure we aren't corrupting data or
292 * metadata
293 */
294 if (bitmap->offset < 0) {
295 /* DATA BITMAP METADATA */
296 if (bitmap->offset
NeilBrown85bfb4d2007-10-22 20:45:11 -0700297 + (long)(page->index * (PAGE_SIZE/512))
NeilBrownf0d76d72007-07-17 04:06:12 -0700298 + size/512 > 0)
299 /* bitmap runs in to metadata */
NeilBrown4b809912008-07-21 17:05:25 +1000300 goto bad_alignment;
NeilBrownf0d76d72007-07-17 04:06:12 -0700301 if (rdev->data_offset + mddev->size*2
Andre Noll0f420352008-07-11 22:02:23 +1000302 > rdev->sb_start + bitmap->offset)
NeilBrownf0d76d72007-07-17 04:06:12 -0700303 /* data runs in to bitmap */
NeilBrown4b809912008-07-21 17:05:25 +1000304 goto bad_alignment;
Andre Noll0f420352008-07-11 22:02:23 +1000305 } else if (rdev->sb_start < rdev->data_offset) {
NeilBrownf0d76d72007-07-17 04:06:12 -0700306 /* METADATA BITMAP DATA */
Andre Noll0f420352008-07-11 22:02:23 +1000307 if (rdev->sb_start
NeilBrownf0d76d72007-07-17 04:06:12 -0700308 + bitmap->offset
309 + page->index*(PAGE_SIZE/512) + size/512
310 > rdev->data_offset)
311 /* bitmap runs in to data */
NeilBrown4b809912008-07-21 17:05:25 +1000312 goto bad_alignment;
NeilBrownf0d76d72007-07-17 04:06:12 -0700313 } else {
314 /* DATA METADATA BITMAP - no problems */
315 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700316 md_super_write(mddev, rdev,
Andre Noll0f420352008-07-11 22:02:23 +1000317 rdev->sb_start + bitmap->offset
NeilBrowna654b9d82005-06-21 17:17:27 -0700318 + page->index * (PAGE_SIZE/512),
NeilBrownab6085c2007-05-23 13:58:10 -0700319 size,
NeilBrowna654b9d82005-06-21 17:17:27 -0700320 page);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000321 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700322
323 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800324 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700325 return 0;
NeilBrown4b809912008-07-21 17:05:25 +1000326
327 bad_alignment:
328 rcu_read_unlock();
329 return -EINVAL;
NeilBrowna654b9d82005-06-21 17:17:27 -0700330}
331
NeilBrown4ad13662007-07-17 04:06:13 -0700332static void bitmap_file_kick(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700333/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700334 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700335 */
NeilBrown4ad13662007-07-17 04:06:13 -0700336static void write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700337{
NeilBrownd785a062006-06-26 00:27:48 -0700338 struct buffer_head *bh;
NeilBrown32a76272005-06-21 17:17:14 -0700339
NeilBrownf0d76d72007-07-17 04:06:12 -0700340 if (bitmap->file == NULL) {
341 switch (write_sb_page(bitmap, page, wait)) {
342 case -EINVAL:
343 bitmap->flags |= BITMAP_WRITE_ERROR;
NeilBrownf0d76d72007-07-17 04:06:12 -0700344 }
NeilBrown4ad13662007-07-17 04:06:13 -0700345 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700346
NeilBrown4ad13662007-07-17 04:06:13 -0700347 bh = page_buffers(page);
NeilBrownc7084432006-01-06 00:20:45 -0800348
NeilBrown4ad13662007-07-17 04:06:13 -0700349 while (bh && bh->b_blocknr) {
350 atomic_inc(&bitmap->pending_writes);
351 set_buffer_locked(bh);
352 set_buffer_mapped(bh);
353 submit_bh(WRITE, bh);
354 bh = bh->b_this_page;
355 }
NeilBrown32a76272005-06-21 17:17:14 -0700356
NeilBrown4ad13662007-07-17 04:06:13 -0700357 if (wait) {
358 wait_event(bitmap->write_wait,
359 atomic_read(&bitmap->pending_writes)==0);
360 }
NeilBrown32a76272005-06-21 17:17:14 -0700361 }
NeilBrown4ad13662007-07-17 04:06:13 -0700362 if (bitmap->flags & BITMAP_WRITE_ERROR)
363 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700364}
365
NeilBrownd785a062006-06-26 00:27:48 -0700366static void end_bitmap_write(struct buffer_head *bh, int uptodate)
NeilBrown32a76272005-06-21 17:17:14 -0700367{
NeilBrownd785a062006-06-26 00:27:48 -0700368 struct bitmap *bitmap = bh->b_private;
369 unsigned long flags;
370
371 if (!uptodate) {
372 spin_lock_irqsave(&bitmap->lock, flags);
373 bitmap->flags |= BITMAP_WRITE_ERROR;
374 spin_unlock_irqrestore(&bitmap->lock, flags);
375 }
376 if (atomic_dec_and_test(&bitmap->pending_writes))
377 wake_up(&bitmap->write_wait);
378}
379
380/* copied from buffer.c */
381static void
382__clear_page_buffers(struct page *page)
383{
384 ClearPagePrivate(page);
385 set_page_private(page, 0);
386 page_cache_release(page);
387}
388static void free_buffers(struct page *page)
389{
390 struct buffer_head *bh = page_buffers(page);
391
392 while (bh) {
393 struct buffer_head *next = bh->b_this_page;
394 free_buffer_head(bh);
395 bh = next;
396 }
397 __clear_page_buffers(page);
398 put_page(page);
399}
400
401/* read a page from a file.
402 * We both read the page, and attach buffers to the page to record the
403 * address of each block (using bmap). These addresses will be used
404 * to write the block later, completely bypassing the filesystem.
405 * This usage is similar to how swap files are handled, and allows us
406 * to write to a file with no concerns of memory allocation failing.
407 */
408static struct page *read_page(struct file *file, unsigned long index,
409 struct bitmap *bitmap,
410 unsigned long count)
411{
NeilBrown32a76272005-06-21 17:17:14 -0700412 struct page *page = NULL;
Josef Sipekc649bb92006-12-08 02:37:19 -0800413 struct inode *inode = file->f_path.dentry->d_inode;
NeilBrownd785a062006-06-26 00:27:48 -0700414 struct buffer_head *bh;
415 sector_t block;
NeilBrown32a76272005-06-21 17:17:14 -0700416
NeilBrown2d1f3b52006-01-06 00:20:31 -0800417 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
418 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700419
NeilBrownd785a062006-06-26 00:27:48 -0700420 page = alloc_page(GFP_KERNEL);
421 if (!page)
422 page = ERR_PTR(-ENOMEM);
NeilBrown32a76272005-06-21 17:17:14 -0700423 if (IS_ERR(page))
424 goto out;
NeilBrownd785a062006-06-26 00:27:48 -0700425
NeilBrownd785a062006-06-26 00:27:48 -0700426 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
427 if (!bh) {
428 put_page(page);
429 page = ERR_PTR(-ENOMEM);
430 goto out;
431 }
432 attach_page_buffers(page, bh);
433 block = index << (PAGE_SHIFT - inode->i_blkbits);
434 while (bh) {
435 if (count == 0)
436 bh->b_blocknr = 0;
437 else {
438 bh->b_blocknr = bmap(inode, block);
439 if (bh->b_blocknr == 0) {
440 /* Cannot use this file! */
441 free_buffers(page);
442 page = ERR_PTR(-EINVAL);
443 goto out;
444 }
445 bh->b_bdev = inode->i_sb->s_bdev;
446 if (count < (1<<inode->i_blkbits))
447 count = 0;
448 else
449 count -= (1<<inode->i_blkbits);
NeilBrown32a76272005-06-21 17:17:14 -0700450
NeilBrownd785a062006-06-26 00:27:48 -0700451 bh->b_end_io = end_bitmap_write;
452 bh->b_private = bitmap;
NeilBrownce25c312006-06-26 00:27:49 -0700453 atomic_inc(&bitmap->pending_writes);
454 set_buffer_locked(bh);
455 set_buffer_mapped(bh);
456 submit_bh(READ, bh);
NeilBrownd785a062006-06-26 00:27:48 -0700457 }
458 block++;
459 bh = bh->b_this_page;
460 }
NeilBrownd785a062006-06-26 00:27:48 -0700461 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700462
463 wait_event(bitmap->write_wait,
464 atomic_read(&bitmap->pending_writes)==0);
465 if (bitmap->flags & BITMAP_WRITE_ERROR) {
466 free_buffers(page);
467 page = ERR_PTR(-EIO);
468 }
NeilBrown32a76272005-06-21 17:17:14 -0700469out:
470 if (IS_ERR(page))
471 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800472 (int)PAGE_SIZE,
473 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700474 PTR_ERR(page));
475 return page;
476}
477
478/*
479 * bitmap file superblock operations
480 */
481
482/* update the event counter and sync the superblock to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700483void bitmap_update_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700484{
485 bitmap_super_t *sb;
486 unsigned long flags;
487
488 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
NeilBrown4ad13662007-07-17 04:06:13 -0700489 return;
NeilBrown32a76272005-06-21 17:17:14 -0700490 spin_lock_irqsave(&bitmap->lock, flags);
491 if (!bitmap->sb_page) { /* no superblock */
492 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700493 return;
NeilBrown32a76272005-06-21 17:17:14 -0700494 }
NeilBrown32a76272005-06-21 17:17:14 -0700495 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800496 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700497 sb->events = cpu_to_le64(bitmap->mddev->events);
Neil Browna0da84f2008-06-28 08:31:22 +1000498 if (bitmap->mddev->events < bitmap->events_cleared) {
499 /* rocking back to read-only */
500 bitmap->events_cleared = bitmap->mddev->events;
501 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
502 }
NeilBrownea03aff2006-01-06 00:20:34 -0800503 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700504 write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700505}
506
507/* print out the bitmap file superblock */
508void bitmap_print_sb(struct bitmap *bitmap)
509{
510 bitmap_super_t *sb;
511
512 if (!bitmap || !bitmap->sb_page)
513 return;
NeilBrownea03aff2006-01-06 00:20:34 -0800514 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700515 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700516 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
517 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
518 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700519 *(__u32 *)(sb->uuid+0),
520 *(__u32 *)(sb->uuid+4),
521 *(__u32 *)(sb->uuid+8),
522 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700523 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700524 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700525 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700526 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700527 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
528 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
529 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
530 printk(KERN_DEBUG " sync size: %llu KB\n",
531 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700532 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800533 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700534}
535
536/* read the superblock from the bitmap file and initialize some bitmap fields */
537static int bitmap_read_sb(struct bitmap *bitmap)
538{
539 char *reason = NULL;
540 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700541 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700542 unsigned long long events;
543 int err = -EINVAL;
544
545 /* page 0 is the superblock, read it... */
NeilBrownf49d5e62007-01-26 00:57:03 -0800546 if (bitmap->file) {
547 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
548 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
549
550 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
551 } else {
NeilBrowna2ed9612008-12-19 16:25:01 +1100552 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset,
553 NULL,
554 0, sizeof(bitmap_super_t));
NeilBrowna654b9d82005-06-21 17:17:27 -0700555 }
NeilBrown32a76272005-06-21 17:17:14 -0700556 if (IS_ERR(bitmap->sb_page)) {
557 err = PTR_ERR(bitmap->sb_page);
558 bitmap->sb_page = NULL;
559 return err;
560 }
561
NeilBrownea03aff2006-01-06 00:20:34 -0800562 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700563
NeilBrown32a76272005-06-21 17:17:14 -0700564 chunksize = le32_to_cpu(sb->chunksize);
565 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
NeilBrown4b6d2872005-09-09 16:23:47 -0700566 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700567
568 /* verify that the bitmap-specific fields are valid */
569 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
570 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800571 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
572 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700573 reason = "unrecognized superblock version";
NeilBrown7dd5d342006-01-06 00:20:39 -0800574 else if (chunksize < PAGE_SIZE)
575 reason = "bitmap chunksize too small";
NeilBrown32a76272005-06-21 17:17:14 -0700576 else if ((1 << ffz(~chunksize)) != chunksize)
577 reason = "bitmap chunksize not a power of 2";
NeilBrown7dd5d342006-01-06 00:20:39 -0800578 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT / HZ)
579 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700580 else if (write_behind > COUNTER_MAX)
581 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700582 if (reason) {
583 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
584 bmname(bitmap), reason);
585 goto out;
586 }
587
588 /* keep the array size field of the bitmap superblock up to date */
589 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
590
591 if (!bitmap->mddev->persistent)
592 goto success;
593
594 /*
595 * if we have a persistent array superblock, compare the
596 * bitmap's UUID and event counter to the mddev's
597 */
598 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
599 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
600 bmname(bitmap));
601 goto out;
602 }
603 events = le64_to_cpu(sb->events);
604 if (events < bitmap->mddev->events) {
605 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
606 "-- forcing full recovery\n", bmname(bitmap), events,
607 (unsigned long long) bitmap->mddev->events);
NeilBrown4f2e6392006-10-21 10:24:09 -0700608 sb->state |= cpu_to_le32(BITMAP_STALE);
NeilBrown32a76272005-06-21 17:17:14 -0700609 }
610success:
611 /* assign fields using values from superblock */
612 bitmap->chunksize = chunksize;
613 bitmap->daemon_sleep = daemon_sleep;
NeilBrown585f0dd2005-09-09 16:23:49 -0700614 bitmap->daemon_lastrun = jiffies;
NeilBrown4b6d2872005-09-09 16:23:47 -0700615 bitmap->max_write_behind = write_behind;
NeilBrown4f2e6392006-10-21 10:24:09 -0700616 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800617 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
618 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700619 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown4f2e6392006-10-21 10:24:09 -0700620 if (sb->state & cpu_to_le32(BITMAP_STALE))
NeilBrown6a079972005-09-09 16:23:44 -0700621 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700622 err = 0;
623out:
NeilBrownea03aff2006-01-06 00:20:34 -0800624 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700625 if (err)
626 bitmap_print_sb(bitmap);
627 return err;
628}
629
630enum bitmap_mask_op {
631 MASK_SET,
632 MASK_UNSET
633};
634
NeilBrown4ad13662007-07-17 04:06:13 -0700635/* record the state of the bitmap in the superblock. Return the old value */
636static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
637 enum bitmap_mask_op op)
NeilBrown32a76272005-06-21 17:17:14 -0700638{
639 bitmap_super_t *sb;
640 unsigned long flags;
NeilBrown4ad13662007-07-17 04:06:13 -0700641 int old;
NeilBrown32a76272005-06-21 17:17:14 -0700642
643 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800644 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700645 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700646 return 0;
NeilBrown32a76272005-06-21 17:17:14 -0700647 }
NeilBrown32a76272005-06-21 17:17:14 -0700648 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800649 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700650 old = le32_to_cpu(sb->state) & bits;
NeilBrown32a76272005-06-21 17:17:14 -0700651 switch (op) {
NeilBrown4f2e6392006-10-21 10:24:09 -0700652 case MASK_SET: sb->state |= cpu_to_le32(bits);
NeilBrown32a76272005-06-21 17:17:14 -0700653 break;
NeilBrown4f2e6392006-10-21 10:24:09 -0700654 case MASK_UNSET: sb->state &= cpu_to_le32(~bits);
NeilBrown32a76272005-06-21 17:17:14 -0700655 break;
656 default: BUG();
657 }
NeilBrownea03aff2006-01-06 00:20:34 -0800658 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700659 return old;
NeilBrown32a76272005-06-21 17:17:14 -0700660}
661
662/*
663 * general bitmap file operations
664 */
665
666/* calculate the index of the page that contains this bit */
667static inline unsigned long file_page_index(unsigned long chunk)
668{
669 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
670}
671
672/* calculate the (bit) offset of this bit within a page */
673static inline unsigned long file_page_offset(unsigned long chunk)
674{
675 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
676}
677
678/*
679 * return a pointer to the page in the filemap that contains the given bit
680 *
681 * this lookup is complicated by the fact that the bitmap sb might be exactly
682 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
683 * 0 or page 1
684 */
685static inline struct page *filemap_get_page(struct bitmap *bitmap,
686 unsigned long chunk)
687{
Paul Clements9b1d1da2006-10-03 01:15:49 -0700688 if (file_page_index(chunk) >= bitmap->file_pages) return NULL;
NeilBrown32a76272005-06-21 17:17:14 -0700689 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
690}
691
692
693static void bitmap_file_unmap(struct bitmap *bitmap)
694{
695 struct page **map, *sb_page;
696 unsigned long *attr;
697 int pages;
698 unsigned long flags;
699
700 spin_lock_irqsave(&bitmap->lock, flags);
701 map = bitmap->filemap;
702 bitmap->filemap = NULL;
703 attr = bitmap->filemap_attr;
704 bitmap->filemap_attr = NULL;
705 pages = bitmap->file_pages;
706 bitmap->file_pages = 0;
707 sb_page = bitmap->sb_page;
708 bitmap->sb_page = NULL;
709 spin_unlock_irqrestore(&bitmap->lock, flags);
710
711 while (pages--)
712 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700713 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700714 kfree(map);
715 kfree(attr);
716
NeilBrownd785a062006-06-26 00:27:48 -0700717 if (sb_page)
718 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700719}
720
721static void bitmap_file_put(struct bitmap *bitmap)
722{
723 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700724 unsigned long flags;
725
726 spin_lock_irqsave(&bitmap->lock, flags);
727 file = bitmap->file;
728 bitmap->file = NULL;
729 spin_unlock_irqrestore(&bitmap->lock, flags);
730
NeilBrownd785a062006-06-26 00:27:48 -0700731 if (file)
732 wait_event(bitmap->write_wait,
733 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700734 bitmap_file_unmap(bitmap);
735
NeilBrownd785a062006-06-26 00:27:48 -0700736 if (file) {
Josef Sipekc649bb92006-12-08 02:37:19 -0800737 struct inode *inode = file->f_path.dentry->d_inode;
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800738 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700739 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700740 }
NeilBrown32a76272005-06-21 17:17:14 -0700741}
742
743
744/*
745 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
746 * then it is no longer reliable, so we stop using it and we mark the file
747 * as failed in the superblock
748 */
749static void bitmap_file_kick(struct bitmap *bitmap)
750{
751 char *path, *ptr = NULL;
752
NeilBrown4ad13662007-07-17 04:06:13 -0700753 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
754 bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700755
NeilBrown4ad13662007-07-17 04:06:13 -0700756 if (bitmap->file) {
757 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
758 if (path)
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700759 ptr = d_path(&bitmap->file->f_path, path,
760 PAGE_SIZE);
761
NeilBrown32a76272005-06-21 17:17:14 -0700762
NeilBrown4ad13662007-07-17 04:06:13 -0700763 printk(KERN_ALERT
764 "%s: kicking failed bitmap file %s from array!\n",
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700765 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700766
NeilBrown4ad13662007-07-17 04:06:13 -0700767 kfree(path);
768 } else
769 printk(KERN_ALERT
770 "%s: disabling internal bitmap due to errors\n",
771 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700772 }
NeilBrown32a76272005-06-21 17:17:14 -0700773
774 bitmap_file_put(bitmap);
775
776 return;
777}
778
779enum bitmap_page_attr {
NeilBrowne16b68b2006-06-26 00:27:45 -0700780 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
781 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
782 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
NeilBrown32a76272005-06-21 17:17:14 -0700783};
784
785static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
786 enum bitmap_page_attr attr)
787{
NeilBrowne16b68b2006-06-26 00:27:45 -0700788 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700789}
790
791static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
792 enum bitmap_page_attr attr)
793{
NeilBrowne16b68b2006-06-26 00:27:45 -0700794 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700795}
796
NeilBrownec7a3192006-06-26 00:27:45 -0700797static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
798 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700799{
NeilBrowne16b68b2006-06-26 00:27:45 -0700800 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700801}
802
803/*
804 * bitmap_file_set_bit -- called before performing a write to the md device
805 * to set (and eventually sync) a particular bit in the bitmap file
806 *
807 * we set the bit immediately, then we record the page number so that
808 * when an unplug occurs, we can flush the dirty pages out to disk
809 */
810static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
811{
812 unsigned long bit;
813 struct page *page;
814 void *kaddr;
815 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
816
NeilBrowna654b9d82005-06-21 17:17:27 -0700817 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700818 return;
819 }
820
821 page = filemap_get_page(bitmap, chunk);
Paul Clements9b1d1da2006-10-03 01:15:49 -0700822 if (!page) return;
NeilBrown32a76272005-06-21 17:17:14 -0700823 bit = file_page_offset(chunk);
824
NeilBrown32a76272005-06-21 17:17:14 -0700825 /* set the bit */
826 kaddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800827 if (bitmap->flags & BITMAP_HOSTENDIAN)
828 set_bit(bit, kaddr);
829 else
830 ext2_set_bit(bit, kaddr);
NeilBrown32a76272005-06-21 17:17:14 -0700831 kunmap_atomic(kaddr, KM_USER0);
832 PRINTK("set file bit %lu page %lu\n", bit, page->index);
833
834 /* record page number so it gets flushed to disk when unplug occurs */
835 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
836
837}
838
839/* this gets called when the md device is ready to unplug its underlying
840 * (slave) device queues -- before we let any writes go down, we need to
841 * sync the dirty pages of the bitmap file to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700842void bitmap_unplug(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700843{
NeilBrownec7a3192006-06-26 00:27:45 -0700844 unsigned long i, flags;
845 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700846 struct page *page;
847 int wait = 0;
848
849 if (!bitmap)
NeilBrown4ad13662007-07-17 04:06:13 -0700850 return;
NeilBrown32a76272005-06-21 17:17:14 -0700851
852 /* look at each page to see if there are any set bits that need to be
853 * flushed out to disk */
854 for (i = 0; i < bitmap->file_pages; i++) {
855 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700856 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700857 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700858 return;
NeilBrown32a76272005-06-21 17:17:14 -0700859 }
860 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700861 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
862 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700863 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
864 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700865 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700866 wait = 1;
867 spin_unlock_irqrestore(&bitmap->lock, flags);
868
NeilBrownd785a062006-06-26 00:27:48 -0700869 if (dirty | need_write)
NeilBrown4ad13662007-07-17 04:06:13 -0700870 write_page(bitmap, page, 0);
NeilBrown32a76272005-06-21 17:17:14 -0700871 }
872 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700873 if (bitmap->file)
NeilBrownd785a062006-06-26 00:27:48 -0700874 wait_event(bitmap->write_wait,
875 atomic_read(&bitmap->pending_writes)==0);
NeilBrown0b79ccf2006-06-26 00:27:44 -0700876 else
NeilBrowna9701a32005-11-08 21:39:34 -0800877 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700878 }
NeilBrownd785a062006-06-26 00:27:48 -0700879 if (bitmap->flags & BITMAP_WRITE_ERROR)
880 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700881}
882
NeilBrown6a079972005-09-09 16:23:44 -0700883static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700884/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
885 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
886 * memory mapping of the bitmap file
887 * Special cases:
888 * if there's no bitmap file, or if the bitmap file had been
889 * previously kicked from the array, we mark all the bits as
890 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700891 *
892 * We ignore all bits for sectors that end earlier than 'start'.
893 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700894 */
NeilBrown6a079972005-09-09 16:23:44 -0700895static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700896{
897 unsigned long i, chunks, index, oldindex, bit;
898 struct page *page = NULL, *oldpage = NULL;
899 unsigned long num_pages, bit_cnt = 0;
900 struct file *file;
NeilBrownd785a062006-06-26 00:27:48 -0700901 unsigned long bytes, offset;
NeilBrown32a76272005-06-21 17:17:14 -0700902 int outofdate;
903 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800904 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700905
906 chunks = bitmap->chunks;
907 file = bitmap->file;
908
NeilBrowna654b9d82005-06-21 17:17:27 -0700909 BUG_ON(!file && !bitmap->offset);
NeilBrown32a76272005-06-21 17:17:14 -0700910
Olaf Hering44456d32005-07-27 11:45:17 -0700911#ifdef INJECT_FAULTS_3
NeilBrown32a76272005-06-21 17:17:14 -0700912 outofdate = 1;
913#else
914 outofdate = bitmap->flags & BITMAP_STALE;
915#endif
916 if (outofdate)
917 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
918 "recovery\n", bmname(bitmap));
919
920 bytes = (chunks + 7) / 8;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700921
NeilBrowncdbb4cc2005-06-21 17:17:18 -0700922 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700923
NeilBrowna654b9d82005-06-21 17:17:27 -0700924 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
NeilBrown32a76272005-06-21 17:17:14 -0700925 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
926 bmname(bitmap),
927 (unsigned long) i_size_read(file->f_mapping->host),
928 bytes + sizeof(bitmap_super_t));
NeilBrown4ad13662007-07-17 04:06:13 -0700929 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700930 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700931
932 ret = -ENOMEM;
933
NeilBrown32a76272005-06-21 17:17:14 -0700934 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700935 if (!bitmap->filemap)
NeilBrown4ad13662007-07-17 04:06:13 -0700936 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700937
NeilBrowne16b68b2006-06-26 00:27:45 -0700938 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
939 bitmap->filemap_attr = kzalloc(
Neil Brown505fa2c2007-04-11 23:28:44 -0700940 roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
NeilBrowne16b68b2006-06-26 00:27:45 -0700941 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700942 if (!bitmap->filemap_attr)
NeilBrown4ad13662007-07-17 04:06:13 -0700943 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700944
NeilBrown32a76272005-06-21 17:17:14 -0700945 oldindex = ~0L;
946
947 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -0800948 int b;
NeilBrown32a76272005-06-21 17:17:14 -0700949 index = file_page_index(i);
950 bit = file_page_offset(i);
951 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -0700952 int count;
NeilBrown32a76272005-06-21 17:17:14 -0700953 /* unmap the old page, we're done with it */
NeilBrownd785a062006-06-26 00:27:48 -0700954 if (index == num_pages-1)
NeilBrownf49d5e62007-01-26 00:57:03 -0800955 count = bytes + sizeof(bitmap_super_t)
956 - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -0700957 else
958 count = PAGE_SIZE;
NeilBrown32a76272005-06-21 17:17:14 -0700959 if (index == 0) {
960 /*
961 * if we're here then the superblock page
962 * contains some bits (PAGE_SIZE != sizeof sb)
963 * we've already read it in, so just use it
964 */
965 page = bitmap->sb_page;
966 offset = sizeof(bitmap_super_t);
NeilBrowna2ed9612008-12-19 16:25:01 +1100967 read_sb_page(bitmap->mddev, bitmap->offset,
968 page,
969 index, count);
NeilBrowna654b9d82005-06-21 17:17:27 -0700970 } else if (file) {
NeilBrownd785a062006-06-26 00:27:48 -0700971 page = read_page(file, index, bitmap, count);
NeilBrowna654b9d82005-06-21 17:17:27 -0700972 offset = 0;
973 } else {
NeilBrowna2ed9612008-12-19 16:25:01 +1100974 page = read_sb_page(bitmap->mddev, bitmap->offset,
975 NULL,
976 index, count);
NeilBrown32a76272005-06-21 17:17:14 -0700977 offset = 0;
978 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700979 if (IS_ERR(page)) { /* read error */
980 ret = PTR_ERR(page);
NeilBrown4ad13662007-07-17 04:06:13 -0700981 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -0700982 }
983
NeilBrown32a76272005-06-21 17:17:14 -0700984 oldindex = index;
985 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -0700986
987 if (outofdate) {
988 /*
989 * if bitmap is out of date, dirty the
990 * whole page and write it out
991 */
NeilBrownea03aff2006-01-06 00:20:34 -0800992 paddr = kmap_atomic(page, KM_USER0);
993 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -0700994 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -0800995 kunmap_atomic(paddr, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700996 write_page(bitmap, page, 1);
997
998 ret = -EIO;
999 if (bitmap->flags & BITMAP_WRITE_ERROR) {
NeilBrown32a76272005-06-21 17:17:14 -07001000 /* release, page not in filemap yet */
NeilBrown2d1f3b52006-01-06 00:20:31 -08001001 put_page(page);
NeilBrown4ad13662007-07-17 04:06:13 -07001002 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001003 }
1004 }
1005
1006 bitmap->filemap[bitmap->file_pages++] = page;
NeilBrownab6085c2007-05-23 13:58:10 -07001007 bitmap->last_page_size = count;
NeilBrown32a76272005-06-21 17:17:14 -07001008 }
NeilBrownea03aff2006-01-06 00:20:34 -08001009 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001010 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001011 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001012 else
NeilBrownea03aff2006-01-06 00:20:34 -08001013 b = ext2_test_bit(bit, paddr);
1014 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001015 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -07001016 /* if the disk bit is set, set the memory bit */
NeilBrown6a079972005-09-09 16:23:44 -07001017 bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
1018 ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
1019 );
NeilBrown32a76272005-06-21 17:17:14 -07001020 bit_cnt++;
NeilBrown6a079972005-09-09 16:23:44 -07001021 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -07001022 }
NeilBrown32a76272005-06-21 17:17:14 -07001023 }
1024
1025 /* everything went OK */
1026 ret = 0;
1027 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1028
NeilBrown32a76272005-06-21 17:17:14 -07001029 if (bit_cnt) { /* Kick recovery if any bits were set */
1030 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1031 md_wakeup_thread(bitmap->mddev->thread);
1032 }
1033
NeilBrown32a76272005-06-21 17:17:14 -07001034 printk(KERN_INFO "%s: bitmap initialized from disk: "
NeilBrown4ad13662007-07-17 04:06:13 -07001035 "read %lu/%lu pages, set %lu bits\n",
1036 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
NeilBrown32a76272005-06-21 17:17:14 -07001037
NeilBrown4ad13662007-07-17 04:06:13 -07001038 return 0;
1039
1040 err:
1041 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1042 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001043 return ret;
1044}
1045
NeilBrowna654b9d82005-06-21 17:17:27 -07001046void bitmap_write_all(struct bitmap *bitmap)
1047{
1048 /* We don't actually write all bitmap blocks here,
1049 * just flag them as needing to be written
1050 */
NeilBrownec7a3192006-06-26 00:27:45 -07001051 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001052
NeilBrownec7a3192006-06-26 00:27:45 -07001053 for (i=0; i < bitmap->file_pages; i++)
1054 set_page_attr(bitmap, bitmap->filemap[i],
1055 BITMAP_PAGE_NEEDWRITE);
NeilBrowna654b9d82005-06-21 17:17:27 -07001056}
1057
NeilBrown32a76272005-06-21 17:17:14 -07001058
1059static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1060{
1061 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1062 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1063 bitmap->bp[page].count += inc;
1064/*
1065 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1066 (unsigned long long)offset, inc, bitmap->bp[page].count);
1067*/
1068 bitmap_checkfree(bitmap, page);
1069}
1070static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1071 sector_t offset, int *blocks,
1072 int create);
1073
1074/*
1075 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1076 * out to disk
1077 */
1078
NeilBrown4ad13662007-07-17 04:06:13 -07001079void bitmap_daemon_work(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001080{
NeilBrownaa3163f2005-06-21 17:17:22 -07001081 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001082 unsigned long flags;
1083 struct page *page = NULL, *lastpage = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001084 int blocks;
NeilBrownea03aff2006-01-06 00:20:34 -08001085 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001086
1087 if (bitmap == NULL)
NeilBrown4ad13662007-07-17 04:06:13 -07001088 return;
NeilBrown32a76272005-06-21 17:17:14 -07001089 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001090 goto done;
1091
NeilBrown32a76272005-06-21 17:17:14 -07001092 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001093 if (bitmap->allclean) {
1094 bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1095 return;
1096 }
1097 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001098
1099 for (j = 0; j < bitmap->chunks; j++) {
1100 bitmap_counter_t *bmc;
1101 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -07001102 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -07001103 /* error or shutdown */
1104 spin_unlock_irqrestore(&bitmap->lock, flags);
1105 break;
1106 }
1107
1108 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001109
1110 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001111 /* skip this page unless it's marked as needing cleaning */
NeilBrownec7a3192006-06-26 00:27:45 -07001112 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1113 int need_write = test_page_attr(bitmap, page,
1114 BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001115 if (need_write)
NeilBrownaa3163f2005-06-21 17:17:22 -07001116 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001117
NeilBrownaa3163f2005-06-21 17:17:22 -07001118 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown8311c292008-03-04 14:29:30 -08001119 if (need_write) {
NeilBrown4ad13662007-07-17 04:06:13 -07001120 write_page(bitmap, page, 0);
NeilBrown8311c292008-03-04 14:29:30 -08001121 bitmap->allclean = 0;
1122 }
NeilBrownaa3163f2005-06-21 17:17:22 -07001123 continue;
1124 }
1125
NeilBrown32a76272005-06-21 17:17:14 -07001126 /* grab the new page, sync and release the old */
NeilBrown32a76272005-06-21 17:17:14 -07001127 if (lastpage != NULL) {
NeilBrownec7a3192006-06-26 00:27:45 -07001128 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001129 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1130 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001131 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001132 } else {
1133 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1134 spin_unlock_irqrestore(&bitmap->lock, flags);
1135 }
NeilBrown32a76272005-06-21 17:17:14 -07001136 } else
1137 spin_unlock_irqrestore(&bitmap->lock, flags);
1138 lastpage = page;
Neil Browna0da84f2008-06-28 08:31:22 +10001139
1140 /* We are possibly going to clear some bits, so make
1141 * sure that events_cleared is up-to-date.
1142 */
1143 if (bitmap->need_sync) {
1144 bitmap_super_t *sb;
1145 bitmap->need_sync = 0;
1146 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1147 sb->events_cleared =
1148 cpu_to_le64(bitmap->events_cleared);
1149 kunmap_atomic(sb, KM_USER0);
1150 write_page(bitmap, bitmap->sb_page, 1);
1151 }
NeilBrown32a76272005-06-21 17:17:14 -07001152 spin_lock_irqsave(&bitmap->lock, flags);
1153 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1154 }
1155 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1156 &blocks, 0);
1157 if (bmc) {
1158/*
1159 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1160*/
NeilBrown8311c292008-03-04 14:29:30 -08001161 if (*bmc)
1162 bitmap->allclean = 0;
1163
NeilBrown32a76272005-06-21 17:17:14 -07001164 if (*bmc == 2) {
1165 *bmc=1; /* maybe clear the bit next time */
1166 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1167 } else if (*bmc == 1) {
1168 /* we can clear the bit */
1169 *bmc = 0;
1170 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1171 -1);
1172
1173 /* clear the bit */
NeilBrownea03aff2006-01-06 00:20:34 -08001174 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001175 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001176 clear_bit(file_page_offset(j), paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001177 else
NeilBrownea03aff2006-01-06 00:20:34 -08001178 ext2_clear_bit(file_page_offset(j), paddr);
1179 kunmap_atomic(paddr, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -07001180 }
1181 }
1182 spin_unlock_irqrestore(&bitmap->lock, flags);
1183 }
1184
1185 /* now sync the final page */
1186 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001187 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001188 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001189 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1190 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001191 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001192 } else {
1193 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1194 spin_unlock_irqrestore(&bitmap->lock, flags);
1195 }
NeilBrown32a76272005-06-21 17:17:14 -07001196 }
1197
NeilBrown7be3dfe2008-03-10 11:43:48 -07001198 done:
NeilBrown8311c292008-03-04 14:29:30 -08001199 if (bitmap->allclean == 0)
1200 bitmap->mddev->thread->timeout = bitmap->daemon_sleep * HZ;
NeilBrown32a76272005-06-21 17:17:14 -07001201}
1202
NeilBrown32a76272005-06-21 17:17:14 -07001203static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1204 sector_t offset, int *blocks,
1205 int create)
1206{
1207 /* If 'create', we might release the lock and reclaim it.
1208 * The lock must have been taken with interrupts enabled.
1209 * If !create, we don't release the lock.
1210 */
1211 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1212 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1213 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1214 sector_t csize;
1215
1216 if (bitmap_checkpage(bitmap, page, create) < 0) {
1217 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1218 *blocks = csize - (offset & (csize- 1));
1219 return NULL;
1220 }
1221 /* now locked ... */
1222
1223 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1224 /* should we use the first or second counter field
1225 * of the hijacked pointer? */
1226 int hi = (pageoff > PAGE_COUNTER_MASK);
1227 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1228 PAGE_COUNTER_SHIFT - 1);
1229 *blocks = csize - (offset & (csize- 1));
1230 return &((bitmap_counter_t *)
1231 &bitmap->bp[page].map)[hi];
1232 } else { /* page is allocated */
1233 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1234 *blocks = csize - (offset & (csize- 1));
1235 return (bitmap_counter_t *)
1236 &(bitmap->bp[page].map[pageoff]);
1237 }
1238}
1239
NeilBrown4b6d2872005-09-09 16:23:47 -07001240int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001241{
1242 if (!bitmap) return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001243
1244 if (behind) {
1245 atomic_inc(&bitmap->behind_writes);
1246 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1247 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1248 }
1249
NeilBrown32a76272005-06-21 17:17:14 -07001250 while (sectors) {
1251 int blocks;
1252 bitmap_counter_t *bmc;
1253
1254 spin_lock_irq(&bitmap->lock);
1255 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1256 if (!bmc) {
1257 spin_unlock_irq(&bitmap->lock);
1258 return 0;
1259 }
1260
Neil Brownda6e1a32007-02-08 14:20:37 -08001261 if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
1262 DEFINE_WAIT(__wait);
1263 /* note that it is safe to do the prepare_to_wait
1264 * after the test as long as we do it before dropping
1265 * the spinlock.
1266 */
1267 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1268 TASK_UNINTERRUPTIBLE);
1269 spin_unlock_irq(&bitmap->lock);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05001270 blk_unplug(bitmap->mddev->queue);
Neil Brownda6e1a32007-02-08 14:20:37 -08001271 schedule();
1272 finish_wait(&bitmap->overflow_wait, &__wait);
1273 continue;
1274 }
1275
NeilBrown32a76272005-06-21 17:17:14 -07001276 switch(*bmc) {
1277 case 0:
1278 bitmap_file_set_bit(bitmap, offset);
1279 bitmap_count_page(bitmap,offset, 1);
Jens Axboe93769f52008-08-01 20:32:31 +02001280 blk_plug_device_unlocked(bitmap->mddev->queue);
NeilBrown32a76272005-06-21 17:17:14 -07001281 /* fall through */
1282 case 1:
1283 *bmc = 2;
1284 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001285
NeilBrown32a76272005-06-21 17:17:14 -07001286 (*bmc)++;
1287
1288 spin_unlock_irq(&bitmap->lock);
1289
1290 offset += blocks;
1291 if (sectors > blocks)
1292 sectors -= blocks;
1293 else sectors = 0;
1294 }
NeilBrown8311c292008-03-04 14:29:30 -08001295 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001296 return 0;
1297}
1298
1299void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001300 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001301{
1302 if (!bitmap) return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001303 if (behind) {
1304 atomic_dec(&bitmap->behind_writes);
1305 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1306 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1307 }
1308
NeilBrown32a76272005-06-21 17:17:14 -07001309 while (sectors) {
1310 int blocks;
1311 unsigned long flags;
1312 bitmap_counter_t *bmc;
1313
1314 spin_lock_irqsave(&bitmap->lock, flags);
1315 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1316 if (!bmc) {
1317 spin_unlock_irqrestore(&bitmap->lock, flags);
1318 return;
1319 }
1320
Neil Browna0da84f2008-06-28 08:31:22 +10001321 if (success &&
1322 bitmap->events_cleared < bitmap->mddev->events) {
1323 bitmap->events_cleared = bitmap->mddev->events;
1324 bitmap->need_sync = 1;
1325 }
1326
NeilBrown32a76272005-06-21 17:17:14 -07001327 if (!success && ! (*bmc & NEEDED_MASK))
1328 *bmc |= NEEDED_MASK;
1329
Neil Brownda6e1a32007-02-08 14:20:37 -08001330 if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
1331 wake_up(&bitmap->overflow_wait);
1332
NeilBrown32a76272005-06-21 17:17:14 -07001333 (*bmc)--;
1334 if (*bmc <= 2) {
1335 set_page_attr(bitmap,
1336 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1337 BITMAP_PAGE_CLEAN);
1338 }
1339 spin_unlock_irqrestore(&bitmap->lock, flags);
1340 offset += blocks;
1341 if (sectors > blocks)
1342 sectors -= blocks;
1343 else sectors = 0;
1344 }
1345}
1346
NeilBrown6a806c52005-07-15 03:56:35 -07001347int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1348 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001349{
1350 bitmap_counter_t *bmc;
1351 int rv;
1352 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1353 *blocks = 1024;
1354 return 1; /* always resync if no bitmap */
1355 }
1356 spin_lock_irq(&bitmap->lock);
1357 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1358 rv = 0;
1359 if (bmc) {
1360 /* locked */
1361 if (RESYNC(*bmc))
1362 rv = 1;
1363 else if (NEEDED(*bmc)) {
1364 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001365 if (!degraded) { /* don't set/clear bits if degraded */
1366 *bmc |= RESYNC_MASK;
1367 *bmc &= ~NEEDED_MASK;
1368 }
NeilBrown32a76272005-06-21 17:17:14 -07001369 }
1370 }
1371 spin_unlock_irq(&bitmap->lock);
NeilBrown8311c292008-03-04 14:29:30 -08001372 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001373 return rv;
1374}
1375
1376void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1377{
1378 bitmap_counter_t *bmc;
1379 unsigned long flags;
1380/*
1381 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1382*/ if (bitmap == NULL) {
1383 *blocks = 1024;
1384 return;
1385 }
1386 spin_lock_irqsave(&bitmap->lock, flags);
1387 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1388 if (bmc == NULL)
1389 goto unlock;
1390 /* locked */
1391/*
1392 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1393*/
1394 if (RESYNC(*bmc)) {
1395 *bmc &= ~RESYNC_MASK;
1396
1397 if (!NEEDED(*bmc) && aborted)
1398 *bmc |= NEEDED_MASK;
1399 else {
1400 if (*bmc <= 2) {
1401 set_page_attr(bitmap,
1402 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1403 BITMAP_PAGE_CLEAN);
1404 }
1405 }
1406 }
1407 unlock:
1408 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown8311c292008-03-04 14:29:30 -08001409 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001410}
1411
1412void bitmap_close_sync(struct bitmap *bitmap)
1413{
1414 /* Sync has finished, and any bitmap chunks that weren't synced
1415 * properly have been aborted. It remains to us to clear the
1416 * RESYNC bit wherever it is still on
1417 */
1418 sector_t sector = 0;
1419 int blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001420 if (!bitmap)
1421 return;
NeilBrown32a76272005-06-21 17:17:14 -07001422 while (sector < bitmap->mddev->resync_max_sectors) {
1423 bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001424 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001425 }
1426}
1427
NeilBrownb47490c2008-02-06 01:39:50 -08001428void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1429{
1430 sector_t s = 0;
1431 int blocks;
1432
1433 if (!bitmap)
1434 return;
1435 if (sector == 0) {
1436 bitmap->last_end_sync = jiffies;
1437 return;
1438 }
1439 if (time_before(jiffies, (bitmap->last_end_sync
1440 + bitmap->daemon_sleep * HZ)))
1441 return;
1442 wait_event(bitmap->mddev->recovery_wait,
1443 atomic_read(&bitmap->mddev->recovery_active) == 0);
1444
1445 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1446 s = 0;
1447 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1448 bitmap_end_sync(bitmap, s, &blocks, 0);
1449 s += blocks;
1450 }
1451 bitmap->last_end_sync = jiffies;
1452}
1453
NeilBrown6a079972005-09-09 16:23:44 -07001454static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001455{
1456 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001457 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001458 * be 0 at this point
1459 */
NeilBrown193f1c92005-08-04 12:53:33 -07001460
1461 int secs;
1462 bitmap_counter_t *bmc;
1463 spin_lock_irq(&bitmap->lock);
1464 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1465 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001466 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001467 return;
NeilBrown32a76272005-06-21 17:17:14 -07001468 }
NeilBrown193f1c92005-08-04 12:53:33 -07001469 if (! *bmc) {
1470 struct page *page;
NeilBrown6a079972005-09-09 16:23:44 -07001471 *bmc = 1 | (needed?NEEDED_MASK:0);
NeilBrown193f1c92005-08-04 12:53:33 -07001472 bitmap_count_page(bitmap, offset, 1);
1473 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1474 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1475 }
1476 spin_unlock_irq(&bitmap->lock);
NeilBrown8311c292008-03-04 14:29:30 -08001477 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001478}
1479
Paul Clements9b1d1da2006-10-03 01:15:49 -07001480/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1481void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1482{
1483 unsigned long chunk;
1484
1485 for (chunk = s; chunk <= e; chunk++) {
1486 sector_t sec = chunk << CHUNK_BLOCK_SHIFT(bitmap);
1487 bitmap_set_memory_bits(bitmap, sec, 1);
1488 bitmap_file_set_bit(bitmap, sec);
1489 }
1490}
1491
NeilBrown32a76272005-06-21 17:17:14 -07001492/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001493 * flush out any pending updates
1494 */
1495void bitmap_flush(mddev_t *mddev)
1496{
1497 struct bitmap *bitmap = mddev->bitmap;
1498 int sleep;
1499
1500 if (!bitmap) /* there was no bitmap */
1501 return;
1502
1503 /* run the daemon_work three time to ensure everything is flushed
1504 * that can be
1505 */
1506 sleep = bitmap->daemon_sleep;
1507 bitmap->daemon_sleep = 0;
1508 bitmap_daemon_work(bitmap);
1509 bitmap_daemon_work(bitmap);
1510 bitmap_daemon_work(bitmap);
1511 bitmap->daemon_sleep = sleep;
1512 bitmap_update_sb(bitmap);
1513}
1514
1515/*
NeilBrown32a76272005-06-21 17:17:14 -07001516 * free memory that was allocated
1517 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001518static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001519{
1520 unsigned long k, pages;
1521 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001522
1523 if (!bitmap) /* there was no bitmap */
1524 return;
1525
NeilBrown32a76272005-06-21 17:17:14 -07001526 /* release the bitmap file and kill the daemon */
1527 bitmap_file_put(bitmap);
1528
1529 bp = bitmap->bp;
1530 pages = bitmap->pages;
1531
1532 /* free all allocated memory */
1533
NeilBrown32a76272005-06-21 17:17:14 -07001534 if (bp) /* deallocate the page memory */
1535 for (k = 0; k < pages; k++)
1536 if (bp[k].map && !bp[k].hijacked)
1537 kfree(bp[k].map);
1538 kfree(bp);
1539 kfree(bitmap);
1540}
NeilBrown3178b0d2005-09-09 16:23:50 -07001541void bitmap_destroy(mddev_t *mddev)
1542{
1543 struct bitmap *bitmap = mddev->bitmap;
1544
1545 if (!bitmap) /* there was no bitmap */
1546 return;
1547
1548 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownb15c2e52006-01-06 00:20:16 -08001549 if (mddev->thread)
1550 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001551
1552 bitmap_free(bitmap);
1553}
NeilBrown32a76272005-06-21 17:17:14 -07001554
1555/*
1556 * initialize the bitmap structure
1557 * if this returns an error, bitmap_destroy must be called to do clean up
1558 */
1559int bitmap_create(mddev_t *mddev)
1560{
1561 struct bitmap *bitmap;
1562 unsigned long blocks = mddev->resync_max_sectors;
1563 unsigned long chunks;
1564 unsigned long pages;
1565 struct file *file = mddev->bitmap_file;
1566 int err;
NeilBrown6a079972005-09-09 16:23:44 -07001567 sector_t start;
NeilBrown32a76272005-06-21 17:17:14 -07001568
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001569 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001570
NeilBrowna654b9d82005-06-21 17:17:27 -07001571 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001572 return 0;
1573
NeilBrowna654b9d82005-06-21 17:17:27 -07001574 BUG_ON(file && mddev->bitmap_offset);
1575
NeilBrown9ffae0c2006-01-06 00:20:32 -08001576 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001577 if (!bitmap)
1578 return -ENOMEM;
1579
NeilBrown32a76272005-06-21 17:17:14 -07001580 spin_lock_init(&bitmap->lock);
NeilBrownce25c312006-06-26 00:27:49 -07001581 atomic_set(&bitmap->pending_writes, 0);
1582 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001583 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001584
NeilBrown32a76272005-06-21 17:17:14 -07001585 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001586
NeilBrown32a76272005-06-21 17:17:14 -07001587 bitmap->file = file;
NeilBrowna654b9d82005-06-21 17:17:27 -07001588 bitmap->offset = mddev->bitmap_offset;
NeilBrownce25c312006-06-26 00:27:49 -07001589 if (file) {
1590 get_file(file);
Mark Fashehef51c972007-05-08 00:27:10 -07001591 do_sync_mapping_range(file->f_mapping, 0, LLONG_MAX,
1592 SYNC_FILE_RANGE_WAIT_BEFORE |
1593 SYNC_FILE_RANGE_WRITE |
1594 SYNC_FILE_RANGE_WAIT_AFTER);
NeilBrownce25c312006-06-26 00:27:49 -07001595 }
NeilBrown32a76272005-06-21 17:17:14 -07001596 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1597 err = bitmap_read_sb(bitmap);
1598 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001599 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001600
Paul Clementsa638b2d2006-10-03 01:16:01 -07001601 bitmap->chunkshift = ffz(~bitmap->chunksize);
NeilBrown32a76272005-06-21 17:17:14 -07001602
1603 /* now that chunksize and chunkshift are set, we can use these macros */
1604 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1605 CHUNK_BLOCK_RATIO(bitmap);
1606 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1607
1608 BUG_ON(!pages);
1609
1610 bitmap->chunks = chunks;
1611 bitmap->pages = pages;
1612 bitmap->missing_pages = pages;
1613 bitmap->counter_bits = COUNTER_BITS;
1614
1615 bitmap->syncchunk = ~0UL;
1616
Olaf Hering44456d32005-07-27 11:45:17 -07001617#ifdef INJECT_FATAL_FAULT_1
NeilBrown32a76272005-06-21 17:17:14 -07001618 bitmap->bp = NULL;
1619#else
NeilBrown9ffae0c2006-01-06 00:20:32 -08001620 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001621#endif
NeilBrown3178b0d2005-09-09 16:23:50 -07001622 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001623 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001624 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001625
NeilBrown32a76272005-06-21 17:17:14 -07001626 /* now that we have some pages available, initialize the in-memory
1627 * bitmap from the on-disk bitmap */
NeilBrown6a079972005-09-09 16:23:44 -07001628 start = 0;
1629 if (mddev->degraded == 0
1630 || bitmap->events_cleared == mddev->events)
1631 /* no need to keep dirty bits to optimise a re-add of a missing device */
1632 start = mddev->recovery_cp;
1633 err = bitmap_init_from_disk(bitmap, start);
NeilBrown193f1c92005-08-04 12:53:33 -07001634
NeilBrown32a76272005-06-21 17:17:14 -07001635 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001636 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001637
1638 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1639 pages, bmname(bitmap));
1640
NeilBrown3178b0d2005-09-09 16:23:50 -07001641 mddev->bitmap = bitmap;
1642
NeilBrownb15c2e52006-01-06 00:20:16 -08001643 mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1644
NeilBrown4ad13662007-07-17 04:06:13 -07001645 bitmap_update_sb(bitmap);
1646
1647 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
NeilBrown3178b0d2005-09-09 16:23:50 -07001648
1649 error:
1650 bitmap_free(bitmap);
1651 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001652}
1653
1654/* the bitmap API -- for raid personalities */
1655EXPORT_SYMBOL(bitmap_startwrite);
1656EXPORT_SYMBOL(bitmap_endwrite);
1657EXPORT_SYMBOL(bitmap_start_sync);
1658EXPORT_SYMBOL(bitmap_end_sync);
1659EXPORT_SYMBOL(bitmap_unplug);
1660EXPORT_SYMBOL(bitmap_close_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08001661EXPORT_SYMBOL(bitmap_cond_end_sync);