blob: a5e5f2fbf96351baca4fb443a472f1f58692d379 [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
NeilBrownbff61972009-03-31 14:33:13 +110019#include <linux/blkdev.h>
NeilBrown32a76272005-06-21 17:17:14 -070020#include <linux/module.h>
NeilBrown32a76272005-06-21 17:17:14 -070021#include <linux/errno.h>
22#include <linux/slab.h>
23#include <linux/init.h>
NeilBrown32a76272005-06-21 17:17:14 -070024#include <linux/timer.h>
25#include <linux/sched.h>
26#include <linux/list.h>
27#include <linux/file.h>
28#include <linux/mount.h>
29#include <linux/buffer_head.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110030#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110031#include "bitmap.h"
NeilBrown32a76272005-06-21 17:17:14 -070032
33/* debug macros */
34
35#define DEBUG 0
36
37#if DEBUG
38/* these are for debugging purposes only! */
39
40/* define one and only one of these */
41#define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
42#define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
43#define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
44#define INJECT_FAULTS_4 0 /* undef */
45#define INJECT_FAULTS_5 0 /* undef */
46#define INJECT_FAULTS_6 0
47
48/* if these are defined, the driver will fail! debug only */
49#define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
50#define INJECT_FATAL_FAULT_2 0 /* undef */
51#define INJECT_FATAL_FAULT_3 0 /* undef */
52#endif
53
54//#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
55#define DPRINTK(x...) do { } while(0)
56
57#ifndef PRINTK
58# if DEBUG > 0
59# define PRINTK(x...) printk(KERN_DEBUG x)
60# else
61# define PRINTK(x...)
62# endif
63#endif
64
65static inline char * bmname(struct bitmap *bitmap)
66{
67 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
68}
69
NeilBrown32a76272005-06-21 17:17:14 -070070
71/*
72 * just a placeholder - calls kmalloc for bitmap pages
73 */
74static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
75{
76 unsigned char *page;
77
Olaf Hering44456d32005-07-27 11:45:17 -070078#ifdef INJECT_FAULTS_1
NeilBrown32a76272005-06-21 17:17:14 -070079 page = NULL;
80#else
81 page = kmalloc(PAGE_SIZE, GFP_NOIO);
82#endif
83 if (!page)
84 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
85 else
NeilBrowna654b9d82005-06-21 17:17:27 -070086 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
NeilBrown32a76272005-06-21 17:17:14 -070087 bmname(bitmap), page);
88 return page;
89}
90
91/*
92 * for now just a placeholder -- just calls kfree for bitmap pages
93 */
94static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
95{
96 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
97 kfree(page);
98}
99
100/*
101 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
102 *
103 * 1) check to see if this page is allocated, if it's not then try to alloc
104 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
105 * page pointer directly as a counter
106 *
107 * if we find our page, we increment the page's refcount so that it stays
108 * allocated while we're using it
109 */
110static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
NeilBrownee305ac2009-09-23 18:06:44 +1000111__releases(bitmap->lock)
112__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -0700113{
114 unsigned char *mappage;
115
116 if (page >= bitmap->pages) {
NeilBrown1187cf02009-03-31 14:27:02 +1100117 /* This can happen if bitmap_start_sync goes beyond
118 * End-of-device while looking for a whole page.
119 * It is harmless.
120 */
NeilBrown32a76272005-06-21 17:17:14 -0700121 return -EINVAL;
122 }
123
124
125 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
126 return 0;
127
128 if (bitmap->bp[page].map) /* page is already allocated, just return */
129 return 0;
130
131 if (!create)
132 return -ENOENT;
133
134 spin_unlock_irq(&bitmap->lock);
135
136 /* this page has not been allocated yet */
137
138 if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
139 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
140 bmname(bitmap));
141 /* failed - set the hijacked flag so that we can use the
142 * pointer as a counter */
143 spin_lock_irq(&bitmap->lock);
144 if (!bitmap->bp[page].map)
145 bitmap->bp[page].hijacked = 1;
146 goto out;
147 }
148
149 /* got a page */
150
151 spin_lock_irq(&bitmap->lock);
152
153 /* recheck the page */
154
155 if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
156 /* somebody beat us to getting the page */
157 bitmap_free_page(bitmap, mappage);
158 return 0;
159 }
160
161 /* no page was in place and we have one, so install it */
162
163 memset(mappage, 0, PAGE_SIZE);
164 bitmap->bp[page].map = mappage;
165 bitmap->missing_pages--;
166out:
167 return 0;
168}
169
170
171/* if page is completely empty, put it back on the free list, or dealloc it */
172/* if page was hijacked, unmark the flag so it might get alloced next time */
173/* Note: lock should be held when calling this */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800174static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
NeilBrown32a76272005-06-21 17:17:14 -0700175{
176 char *ptr;
177
178 if (bitmap->bp[page].count) /* page is still busy */
179 return;
180
181 /* page is no longer in use, it can be released */
182
183 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
184 bitmap->bp[page].hijacked = 0;
185 bitmap->bp[page].map = NULL;
186 return;
187 }
188
189 /* normal case, free the page */
190
191#if 0
192/* actually ... let's not. We will probably need the page again exactly when
193 * memory is tight and we are flusing to disk
194 */
195 return;
196#else
197 ptr = bitmap->bp[page].map;
198 bitmap->bp[page].map = NULL;
199 bitmap->missing_pages++;
200 bitmap_free_page(bitmap, ptr);
201 return;
202#endif
203}
204
205
206/*
207 * bitmap file handling - read and write the bitmap file and its superblock
208 */
209
NeilBrown32a76272005-06-21 17:17:14 -0700210/*
211 * basic page I/O operations
212 */
213
NeilBrowna654b9d82005-06-21 17:17:27 -0700214/* IO operations when bitmap is stored near all superblocks */
NeilBrowna2ed9612008-12-19 16:25:01 +1100215static struct page *read_sb_page(mddev_t *mddev, long offset,
216 struct page *page,
217 unsigned long index, int size)
NeilBrowna654b9d82005-06-21 17:17:27 -0700218{
219 /* choose a good rdev and read the page from there */
220
221 mdk_rdev_t *rdev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700222 sector_t target;
223
224 if (!page)
NeilBrowna2ed9612008-12-19 16:25:01 +1100225 page = alloc_page(GFP_KERNEL);
226 if (!page)
NeilBrowna654b9d82005-06-21 17:17:27 -0700227 return ERR_PTR(-ENOMEM);
NeilBrowna654b9d82005-06-21 17:17:27 -0700228
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100229 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800230 if (! test_bit(In_sync, &rdev->flags)
231 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700232 continue;
233
Andre Noll0f420352008-07-11 22:02:23 +1000234 target = rdev->sb_start + offset + index * (PAGE_SIZE/512);
NeilBrowna654b9d82005-06-21 17:17:27 -0700235
NeilBrowna2ed9612008-12-19 16:25:01 +1100236 if (sync_page_io(rdev->bdev, target,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400237 roundup(size, bdev_logical_block_size(rdev->bdev)),
NeilBrowna2ed9612008-12-19 16:25:01 +1100238 page, READ)) {
NeilBrownab904d62005-09-09 16:23:52 -0700239 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700240 attach_page_buffers(page, NULL); /* so that free_buffer will
241 * quietly no-op */
NeilBrownab904d62005-09-09 16:23:52 -0700242 return page;
243 }
244 }
245 return ERR_PTR(-EIO);
NeilBrowna654b9d82005-06-21 17:17:27 -0700246
NeilBrowna654b9d82005-06-21 17:17:27 -0700247}
248
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000249static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
250{
251 /* Iterate the disks of an mddev, using rcu to protect access to the
252 * linked list, and raising the refcount of devices we return to ensure
253 * they don't disappear while in use.
254 * As devices are only added or removed when raid_disk is < 0 and
255 * nr_pending is 0 and In_sync is clear, the entries we return will
256 * still be in the same position on the list when we re-enter
257 * list_for_each_continue_rcu.
258 */
259 struct list_head *pos;
260 rcu_read_lock();
261 if (rdev == NULL)
262 /* start at the beginning */
263 pos = &mddev->disks;
264 else {
265 /* release the previous rdev and start from there. */
266 rdev_dec_pending(rdev, mddev);
267 pos = &rdev->same_set;
268 }
269 list_for_each_continue_rcu(pos, &mddev->disks) {
270 rdev = list_entry(pos, mdk_rdev_t, same_set);
271 if (rdev->raid_disk >= 0 &&
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000272 !test_bit(Faulty, &rdev->flags)) {
273 /* this is a usable devices */
274 atomic_inc(&rdev->nr_pending);
275 rcu_read_unlock();
276 return rdev;
277 }
278 }
279 rcu_read_unlock();
280 return NULL;
281}
282
NeilBrownab6085c2007-05-23 13:58:10 -0700283static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrowna654b9d82005-06-21 17:17:27 -0700284{
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000285 mdk_rdev_t *rdev = NULL;
NeilBrownab6085c2007-05-23 13:58:10 -0700286 mddev_t *mddev = bitmap->mddev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700287
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000288 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
NeilBrownab6085c2007-05-23 13:58:10 -0700289 int size = PAGE_SIZE;
290 if (page->index == bitmap->file_pages-1)
291 size = roundup(bitmap->last_page_size,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400292 bdev_logical_block_size(rdev->bdev));
NeilBrownf0d76d72007-07-17 04:06:12 -0700293 /* Just make sure we aren't corrupting data or
294 * metadata
295 */
296 if (bitmap->offset < 0) {
297 /* DATA BITMAP METADATA */
298 if (bitmap->offset
NeilBrown85bfb4d2007-10-22 20:45:11 -0700299 + (long)(page->index * (PAGE_SIZE/512))
NeilBrownf0d76d72007-07-17 04:06:12 -0700300 + size/512 > 0)
301 /* bitmap runs in to metadata */
NeilBrown4b809912008-07-21 17:05:25 +1000302 goto bad_alignment;
Andre Noll58c0fed2009-03-31 14:33:13 +1100303 if (rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +1000304 > rdev->sb_start + bitmap->offset)
NeilBrownf0d76d72007-07-17 04:06:12 -0700305 /* data runs in to bitmap */
NeilBrown4b809912008-07-21 17:05:25 +1000306 goto bad_alignment;
Andre Noll0f420352008-07-11 22:02:23 +1000307 } else if (rdev->sb_start < rdev->data_offset) {
NeilBrownf0d76d72007-07-17 04:06:12 -0700308 /* METADATA BITMAP DATA */
Andre Noll0f420352008-07-11 22:02:23 +1000309 if (rdev->sb_start
NeilBrownf0d76d72007-07-17 04:06:12 -0700310 + bitmap->offset
311 + page->index*(PAGE_SIZE/512) + size/512
312 > rdev->data_offset)
313 /* bitmap runs in to data */
NeilBrown4b809912008-07-21 17:05:25 +1000314 goto bad_alignment;
NeilBrownf0d76d72007-07-17 04:06:12 -0700315 } else {
316 /* DATA METADATA BITMAP - no problems */
317 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700318 md_super_write(mddev, rdev,
Andre Noll0f420352008-07-11 22:02:23 +1000319 rdev->sb_start + bitmap->offset
NeilBrowna654b9d82005-06-21 17:17:27 -0700320 + page->index * (PAGE_SIZE/512),
NeilBrownab6085c2007-05-23 13:58:10 -0700321 size,
NeilBrowna654b9d82005-06-21 17:17:27 -0700322 page);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000323 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700324
325 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800326 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700327 return 0;
NeilBrown4b809912008-07-21 17:05:25 +1000328
329 bad_alignment:
NeilBrown4b809912008-07-21 17:05:25 +1000330 return -EINVAL;
NeilBrowna654b9d82005-06-21 17:17:27 -0700331}
332
NeilBrown4ad13662007-07-17 04:06:13 -0700333static void bitmap_file_kick(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700334/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700335 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700336 */
NeilBrown4ad13662007-07-17 04:06:13 -0700337static void write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700338{
NeilBrownd785a062006-06-26 00:27:48 -0700339 struct buffer_head *bh;
NeilBrown32a76272005-06-21 17:17:14 -0700340
NeilBrownf0d76d72007-07-17 04:06:12 -0700341 if (bitmap->file == NULL) {
342 switch (write_sb_page(bitmap, page, wait)) {
343 case -EINVAL:
344 bitmap->flags |= BITMAP_WRITE_ERROR;
NeilBrownf0d76d72007-07-17 04:06:12 -0700345 }
NeilBrown4ad13662007-07-17 04:06:13 -0700346 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700347
NeilBrown4ad13662007-07-17 04:06:13 -0700348 bh = page_buffers(page);
NeilBrownc7084432006-01-06 00:20:45 -0800349
NeilBrown4ad13662007-07-17 04:06:13 -0700350 while (bh && bh->b_blocknr) {
351 atomic_inc(&bitmap->pending_writes);
352 set_buffer_locked(bh);
353 set_buffer_mapped(bh);
354 submit_bh(WRITE, bh);
355 bh = bh->b_this_page;
356 }
NeilBrown32a76272005-06-21 17:17:14 -0700357
NeilBrown4ad13662007-07-17 04:06:13 -0700358 if (wait) {
359 wait_event(bitmap->write_wait,
360 atomic_read(&bitmap->pending_writes)==0);
361 }
NeilBrown32a76272005-06-21 17:17:14 -0700362 }
NeilBrown4ad13662007-07-17 04:06:13 -0700363 if (bitmap->flags & BITMAP_WRITE_ERROR)
364 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700365}
366
NeilBrownd785a062006-06-26 00:27:48 -0700367static void end_bitmap_write(struct buffer_head *bh, int uptodate)
NeilBrown32a76272005-06-21 17:17:14 -0700368{
NeilBrownd785a062006-06-26 00:27:48 -0700369 struct bitmap *bitmap = bh->b_private;
370 unsigned long flags;
371
372 if (!uptodate) {
373 spin_lock_irqsave(&bitmap->lock, flags);
374 bitmap->flags |= BITMAP_WRITE_ERROR;
375 spin_unlock_irqrestore(&bitmap->lock, flags);
376 }
377 if (atomic_dec_and_test(&bitmap->pending_writes))
378 wake_up(&bitmap->write_wait);
379}
380
381/* copied from buffer.c */
382static void
383__clear_page_buffers(struct page *page)
384{
385 ClearPagePrivate(page);
386 set_page_private(page, 0);
387 page_cache_release(page);
388}
389static void free_buffers(struct page *page)
390{
391 struct buffer_head *bh = page_buffers(page);
392
393 while (bh) {
394 struct buffer_head *next = bh->b_this_page;
395 free_buffer_head(bh);
396 bh = next;
397 }
398 __clear_page_buffers(page);
399 put_page(page);
400}
401
402/* read a page from a file.
403 * We both read the page, and attach buffers to the page to record the
404 * address of each block (using bmap). These addresses will be used
405 * to write the block later, completely bypassing the filesystem.
406 * This usage is similar to how swap files are handled, and allows us
407 * to write to a file with no concerns of memory allocation failing.
408 */
409static struct page *read_page(struct file *file, unsigned long index,
410 struct bitmap *bitmap,
411 unsigned long count)
412{
NeilBrown32a76272005-06-21 17:17:14 -0700413 struct page *page = NULL;
Josef Sipekc649bb92006-12-08 02:37:19 -0800414 struct inode *inode = file->f_path.dentry->d_inode;
NeilBrownd785a062006-06-26 00:27:48 -0700415 struct buffer_head *bh;
416 sector_t block;
NeilBrown32a76272005-06-21 17:17:14 -0700417
NeilBrown2d1f3b52006-01-06 00:20:31 -0800418 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
419 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700420
NeilBrownd785a062006-06-26 00:27:48 -0700421 page = alloc_page(GFP_KERNEL);
422 if (!page)
423 page = ERR_PTR(-ENOMEM);
NeilBrown32a76272005-06-21 17:17:14 -0700424 if (IS_ERR(page))
425 goto out;
NeilBrownd785a062006-06-26 00:27:48 -0700426
NeilBrownd785a062006-06-26 00:27:48 -0700427 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
428 if (!bh) {
429 put_page(page);
430 page = ERR_PTR(-ENOMEM);
431 goto out;
432 }
433 attach_page_buffers(page, bh);
434 block = index << (PAGE_SHIFT - inode->i_blkbits);
435 while (bh) {
436 if (count == 0)
437 bh->b_blocknr = 0;
438 else {
439 bh->b_blocknr = bmap(inode, block);
440 if (bh->b_blocknr == 0) {
441 /* Cannot use this file! */
442 free_buffers(page);
443 page = ERR_PTR(-EINVAL);
444 goto out;
445 }
446 bh->b_bdev = inode->i_sb->s_bdev;
447 if (count < (1<<inode->i_blkbits))
448 count = 0;
449 else
450 count -= (1<<inode->i_blkbits);
NeilBrown32a76272005-06-21 17:17:14 -0700451
NeilBrownd785a062006-06-26 00:27:48 -0700452 bh->b_end_io = end_bitmap_write;
453 bh->b_private = bitmap;
NeilBrownce25c312006-06-26 00:27:49 -0700454 atomic_inc(&bitmap->pending_writes);
455 set_buffer_locked(bh);
456 set_buffer_mapped(bh);
457 submit_bh(READ, bh);
NeilBrownd785a062006-06-26 00:27:48 -0700458 }
459 block++;
460 bh = bh->b_this_page;
461 }
NeilBrownd785a062006-06-26 00:27:48 -0700462 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700463
464 wait_event(bitmap->write_wait,
465 atomic_read(&bitmap->pending_writes)==0);
466 if (bitmap->flags & BITMAP_WRITE_ERROR) {
467 free_buffers(page);
468 page = ERR_PTR(-EIO);
469 }
NeilBrown32a76272005-06-21 17:17:14 -0700470out:
471 if (IS_ERR(page))
472 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800473 (int)PAGE_SIZE,
474 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700475 PTR_ERR(page));
476 return page;
477}
478
479/*
480 * bitmap file superblock operations
481 */
482
483/* update the event counter and sync the superblock to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700484void bitmap_update_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700485{
486 bitmap_super_t *sb;
487 unsigned long flags;
488
489 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
NeilBrown4ad13662007-07-17 04:06:13 -0700490 return;
NeilBrown32a76272005-06-21 17:17:14 -0700491 spin_lock_irqsave(&bitmap->lock, flags);
492 if (!bitmap->sb_page) { /* no superblock */
493 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700494 return;
NeilBrown32a76272005-06-21 17:17:14 -0700495 }
NeilBrown32a76272005-06-21 17:17:14 -0700496 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800497 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700498 sb->events = cpu_to_le64(bitmap->mddev->events);
Neil Browna0da84f2008-06-28 08:31:22 +1000499 if (bitmap->mddev->events < bitmap->events_cleared) {
500 /* rocking back to read-only */
501 bitmap->events_cleared = bitmap->mddev->events;
502 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
503 }
NeilBrownea03aff2006-01-06 00:20:34 -0800504 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700505 write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700506}
507
508/* print out the bitmap file superblock */
509void bitmap_print_sb(struct bitmap *bitmap)
510{
511 bitmap_super_t *sb;
512
513 if (!bitmap || !bitmap->sb_page)
514 return;
NeilBrownea03aff2006-01-06 00:20:34 -0800515 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700516 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700517 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
518 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
519 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700520 *(__u32 *)(sb->uuid+0),
521 *(__u32 *)(sb->uuid+4),
522 *(__u32 *)(sb->uuid+8),
523 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700524 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700525 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700526 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700527 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700528 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
529 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
530 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
531 printk(KERN_DEBUG " sync size: %llu KB\n",
532 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700533 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800534 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700535}
536
537/* read the superblock from the bitmap file and initialize some bitmap fields */
538static int bitmap_read_sb(struct bitmap *bitmap)
539{
540 char *reason = NULL;
541 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700542 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700543 unsigned long long events;
544 int err = -EINVAL;
545
546 /* page 0 is the superblock, read it... */
NeilBrownf49d5e62007-01-26 00:57:03 -0800547 if (bitmap->file) {
548 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
549 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
550
551 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
552 } else {
NeilBrowna2ed9612008-12-19 16:25:01 +1100553 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset,
554 NULL,
555 0, sizeof(bitmap_super_t));
NeilBrowna654b9d82005-06-21 17:17:27 -0700556 }
NeilBrown32a76272005-06-21 17:17:14 -0700557 if (IS_ERR(bitmap->sb_page)) {
558 err = PTR_ERR(bitmap->sb_page);
559 bitmap->sb_page = NULL;
560 return err;
561 }
562
NeilBrownea03aff2006-01-06 00:20:34 -0800563 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700564
NeilBrown32a76272005-06-21 17:17:14 -0700565 chunksize = le32_to_cpu(sb->chunksize);
566 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
NeilBrown4b6d2872005-09-09 16:23:47 -0700567 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700568
569 /* verify that the bitmap-specific fields are valid */
570 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
571 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800572 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
573 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700574 reason = "unrecognized superblock version";
NeilBrown1187cf02009-03-31 14:27:02 +1100575 else if (chunksize < 512)
NeilBrown7dd5d342006-01-06 00:20:39 -0800576 reason = "bitmap chunksize too small";
NeilBrown32a76272005-06-21 17:17:14 -0700577 else if ((1 << ffz(~chunksize)) != chunksize)
578 reason = "bitmap chunksize not a power of 2";
NeilBrown7dd5d342006-01-06 00:20:39 -0800579 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT / HZ)
580 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700581 else if (write_behind > COUNTER_MAX)
582 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700583 if (reason) {
584 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
585 bmname(bitmap), reason);
586 goto out;
587 }
588
589 /* keep the array size field of the bitmap superblock up to date */
590 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
591
592 if (!bitmap->mddev->persistent)
593 goto success;
594
595 /*
596 * if we have a persistent array superblock, compare the
597 * bitmap's UUID and event counter to the mddev's
598 */
599 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
600 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
601 bmname(bitmap));
602 goto out;
603 }
604 events = le64_to_cpu(sb->events);
605 if (events < bitmap->mddev->events) {
606 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
607 "-- forcing full recovery\n", bmname(bitmap), events,
608 (unsigned long long) bitmap->mddev->events);
NeilBrown4f2e6392006-10-21 10:24:09 -0700609 sb->state |= cpu_to_le32(BITMAP_STALE);
NeilBrown32a76272005-06-21 17:17:14 -0700610 }
611success:
612 /* assign fields using values from superblock */
613 bitmap->chunksize = chunksize;
614 bitmap->daemon_sleep = daemon_sleep;
NeilBrown585f0dd2005-09-09 16:23:49 -0700615 bitmap->daemon_lastrun = jiffies;
NeilBrown4b6d2872005-09-09 16:23:47 -0700616 bitmap->max_write_behind = write_behind;
NeilBrown4f2e6392006-10-21 10:24:09 -0700617 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800618 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
619 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700620 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown4f2e6392006-10-21 10:24:09 -0700621 if (sb->state & cpu_to_le32(BITMAP_STALE))
NeilBrown6a079972005-09-09 16:23:44 -0700622 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700623 err = 0;
624out:
NeilBrownea03aff2006-01-06 00:20:34 -0800625 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700626 if (err)
627 bitmap_print_sb(bitmap);
628 return err;
629}
630
631enum bitmap_mask_op {
632 MASK_SET,
633 MASK_UNSET
634};
635
NeilBrown4ad13662007-07-17 04:06:13 -0700636/* record the state of the bitmap in the superblock. Return the old value */
637static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
638 enum bitmap_mask_op op)
NeilBrown32a76272005-06-21 17:17:14 -0700639{
640 bitmap_super_t *sb;
641 unsigned long flags;
NeilBrown4ad13662007-07-17 04:06:13 -0700642 int old;
NeilBrown32a76272005-06-21 17:17:14 -0700643
644 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800645 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700646 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700647 return 0;
NeilBrown32a76272005-06-21 17:17:14 -0700648 }
NeilBrown32a76272005-06-21 17:17:14 -0700649 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800650 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700651 old = le32_to_cpu(sb->state) & bits;
NeilBrown32a76272005-06-21 17:17:14 -0700652 switch (op) {
NeilBrown4f2e6392006-10-21 10:24:09 -0700653 case MASK_SET: sb->state |= cpu_to_le32(bits);
NeilBrown32a76272005-06-21 17:17:14 -0700654 break;
NeilBrown4f2e6392006-10-21 10:24:09 -0700655 case MASK_UNSET: sb->state &= cpu_to_le32(~bits);
NeilBrown32a76272005-06-21 17:17:14 -0700656 break;
657 default: BUG();
658 }
NeilBrownea03aff2006-01-06 00:20:34 -0800659 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700660 return old;
NeilBrown32a76272005-06-21 17:17:14 -0700661}
662
663/*
664 * general bitmap file operations
665 */
666
667/* calculate the index of the page that contains this bit */
668static inline unsigned long file_page_index(unsigned long chunk)
669{
670 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
671}
672
673/* calculate the (bit) offset of this bit within a page */
674static inline unsigned long file_page_offset(unsigned long chunk)
675{
676 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
677}
678
679/*
680 * return a pointer to the page in the filemap that contains the given bit
681 *
682 * this lookup is complicated by the fact that the bitmap sb might be exactly
683 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
684 * 0 or page 1
685 */
686static inline struct page *filemap_get_page(struct bitmap *bitmap,
687 unsigned long chunk)
688{
Paul Clements9b1d1da2006-10-03 01:15:49 -0700689 if (file_page_index(chunk) >= bitmap->file_pages) return NULL;
NeilBrown32a76272005-06-21 17:17:14 -0700690 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
691}
692
693
694static void bitmap_file_unmap(struct bitmap *bitmap)
695{
696 struct page **map, *sb_page;
697 unsigned long *attr;
698 int pages;
699 unsigned long flags;
700
701 spin_lock_irqsave(&bitmap->lock, flags);
702 map = bitmap->filemap;
703 bitmap->filemap = NULL;
704 attr = bitmap->filemap_attr;
705 bitmap->filemap_attr = NULL;
706 pages = bitmap->file_pages;
707 bitmap->file_pages = 0;
708 sb_page = bitmap->sb_page;
709 bitmap->sb_page = NULL;
710 spin_unlock_irqrestore(&bitmap->lock, flags);
711
712 while (pages--)
713 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700714 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700715 kfree(map);
716 kfree(attr);
717
NeilBrownd785a062006-06-26 00:27:48 -0700718 if (sb_page)
719 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700720}
721
722static void bitmap_file_put(struct bitmap *bitmap)
723{
724 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700725 unsigned long flags;
726
727 spin_lock_irqsave(&bitmap->lock, flags);
728 file = bitmap->file;
729 bitmap->file = NULL;
730 spin_unlock_irqrestore(&bitmap->lock, flags);
731
NeilBrownd785a062006-06-26 00:27:48 -0700732 if (file)
733 wait_event(bitmap->write_wait,
734 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700735 bitmap_file_unmap(bitmap);
736
NeilBrownd785a062006-06-26 00:27:48 -0700737 if (file) {
Josef Sipekc649bb92006-12-08 02:37:19 -0800738 struct inode *inode = file->f_path.dentry->d_inode;
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800739 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700740 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700741 }
NeilBrown32a76272005-06-21 17:17:14 -0700742}
743
744
745/*
746 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
747 * then it is no longer reliable, so we stop using it and we mark the file
748 * as failed in the superblock
749 */
750static void bitmap_file_kick(struct bitmap *bitmap)
751{
752 char *path, *ptr = NULL;
753
NeilBrown4ad13662007-07-17 04:06:13 -0700754 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
755 bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700756
NeilBrown4ad13662007-07-17 04:06:13 -0700757 if (bitmap->file) {
758 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
759 if (path)
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700760 ptr = d_path(&bitmap->file->f_path, path,
761 PAGE_SIZE);
762
NeilBrown32a76272005-06-21 17:17:14 -0700763
NeilBrown4ad13662007-07-17 04:06:13 -0700764 printk(KERN_ALERT
765 "%s: kicking failed bitmap file %s from array!\n",
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700766 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700767
NeilBrown4ad13662007-07-17 04:06:13 -0700768 kfree(path);
769 } else
770 printk(KERN_ALERT
771 "%s: disabling internal bitmap due to errors\n",
772 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700773 }
NeilBrown32a76272005-06-21 17:17:14 -0700774
775 bitmap_file_put(bitmap);
776
777 return;
778}
779
780enum bitmap_page_attr {
NeilBrowne16b68b2006-06-26 00:27:45 -0700781 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
782 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
783 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
NeilBrown32a76272005-06-21 17:17:14 -0700784};
785
786static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
787 enum bitmap_page_attr attr)
788{
NeilBrowne16b68b2006-06-26 00:27:45 -0700789 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700790}
791
792static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
793 enum bitmap_page_attr attr)
794{
NeilBrowne16b68b2006-06-26 00:27:45 -0700795 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700796}
797
NeilBrownec7a3192006-06-26 00:27:45 -0700798static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
799 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700800{
NeilBrowne16b68b2006-06-26 00:27:45 -0700801 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700802}
803
804/*
805 * bitmap_file_set_bit -- called before performing a write to the md device
806 * to set (and eventually sync) a particular bit in the bitmap file
807 *
808 * we set the bit immediately, then we record the page number so that
809 * when an unplug occurs, we can flush the dirty pages out to disk
810 */
811static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
812{
813 unsigned long bit;
814 struct page *page;
815 void *kaddr;
816 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
817
NeilBrowna654b9d82005-06-21 17:17:27 -0700818 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700819 return;
820 }
821
822 page = filemap_get_page(bitmap, chunk);
Paul Clements9b1d1da2006-10-03 01:15:49 -0700823 if (!page) return;
NeilBrown32a76272005-06-21 17:17:14 -0700824 bit = file_page_offset(chunk);
825
NeilBrown32a76272005-06-21 17:17:14 -0700826 /* set the bit */
827 kaddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800828 if (bitmap->flags & BITMAP_HOSTENDIAN)
829 set_bit(bit, kaddr);
830 else
831 ext2_set_bit(bit, kaddr);
NeilBrown32a76272005-06-21 17:17:14 -0700832 kunmap_atomic(kaddr, KM_USER0);
833 PRINTK("set file bit %lu page %lu\n", bit, page->index);
834
835 /* record page number so it gets flushed to disk when unplug occurs */
836 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
837
838}
839
840/* this gets called when the md device is ready to unplug its underlying
841 * (slave) device queues -- before we let any writes go down, we need to
842 * sync the dirty pages of the bitmap file to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700843void bitmap_unplug(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700844{
NeilBrownec7a3192006-06-26 00:27:45 -0700845 unsigned long i, flags;
846 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700847 struct page *page;
848 int wait = 0;
849
850 if (!bitmap)
NeilBrown4ad13662007-07-17 04:06:13 -0700851 return;
NeilBrown32a76272005-06-21 17:17:14 -0700852
853 /* look at each page to see if there are any set bits that need to be
854 * flushed out to disk */
855 for (i = 0; i < bitmap->file_pages; i++) {
856 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700857 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700858 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700859 return;
NeilBrown32a76272005-06-21 17:17:14 -0700860 }
861 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700862 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
863 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700864 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
865 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700866 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700867 wait = 1;
868 spin_unlock_irqrestore(&bitmap->lock, flags);
869
NeilBrownd785a062006-06-26 00:27:48 -0700870 if (dirty | need_write)
NeilBrown4ad13662007-07-17 04:06:13 -0700871 write_page(bitmap, page, 0);
NeilBrown32a76272005-06-21 17:17:14 -0700872 }
873 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700874 if (bitmap->file)
NeilBrownd785a062006-06-26 00:27:48 -0700875 wait_event(bitmap->write_wait,
876 atomic_read(&bitmap->pending_writes)==0);
NeilBrown0b79ccf2006-06-26 00:27:44 -0700877 else
NeilBrowna9701a32005-11-08 21:39:34 -0800878 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700879 }
NeilBrownd785a062006-06-26 00:27:48 -0700880 if (bitmap->flags & BITMAP_WRITE_ERROR)
881 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700882}
883
NeilBrown6a079972005-09-09 16:23:44 -0700884static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700885/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
886 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
887 * memory mapping of the bitmap file
888 * Special cases:
889 * if there's no bitmap file, or if the bitmap file had been
890 * previously kicked from the array, we mark all the bits as
891 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700892 *
893 * We ignore all bits for sectors that end earlier than 'start'.
894 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700895 */
NeilBrown6a079972005-09-09 16:23:44 -0700896static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700897{
898 unsigned long i, chunks, index, oldindex, bit;
899 struct page *page = NULL, *oldpage = NULL;
900 unsigned long num_pages, bit_cnt = 0;
901 struct file *file;
NeilBrownd785a062006-06-26 00:27:48 -0700902 unsigned long bytes, offset;
NeilBrown32a76272005-06-21 17:17:14 -0700903 int outofdate;
904 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800905 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700906
907 chunks = bitmap->chunks;
908 file = bitmap->file;
909
NeilBrowna654b9d82005-06-21 17:17:27 -0700910 BUG_ON(!file && !bitmap->offset);
NeilBrown32a76272005-06-21 17:17:14 -0700911
Olaf Hering44456d32005-07-27 11:45:17 -0700912#ifdef INJECT_FAULTS_3
NeilBrown32a76272005-06-21 17:17:14 -0700913 outofdate = 1;
914#else
915 outofdate = bitmap->flags & BITMAP_STALE;
916#endif
917 if (outofdate)
918 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
919 "recovery\n", bmname(bitmap));
920
921 bytes = (chunks + 7) / 8;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700922
NeilBrowncdbb4cc2005-06-21 17:17:18 -0700923 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700924
NeilBrowna654b9d82005-06-21 17:17:27 -0700925 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
NeilBrown32a76272005-06-21 17:17:14 -0700926 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
927 bmname(bitmap),
928 (unsigned long) i_size_read(file->f_mapping->host),
929 bytes + sizeof(bitmap_super_t));
NeilBrown4ad13662007-07-17 04:06:13 -0700930 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700931 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700932
933 ret = -ENOMEM;
934
NeilBrown32a76272005-06-21 17:17:14 -0700935 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700936 if (!bitmap->filemap)
NeilBrown4ad13662007-07-17 04:06:13 -0700937 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700938
NeilBrowne16b68b2006-06-26 00:27:45 -0700939 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
940 bitmap->filemap_attr = kzalloc(
Neil Brown505fa2c2007-04-11 23:28:44 -0700941 roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
NeilBrowne16b68b2006-06-26 00:27:45 -0700942 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700943 if (!bitmap->filemap_attr)
NeilBrown4ad13662007-07-17 04:06:13 -0700944 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700945
NeilBrown32a76272005-06-21 17:17:14 -0700946 oldindex = ~0L;
947
948 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -0800949 int b;
NeilBrown32a76272005-06-21 17:17:14 -0700950 index = file_page_index(i);
951 bit = file_page_offset(i);
952 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -0700953 int count;
NeilBrown32a76272005-06-21 17:17:14 -0700954 /* unmap the old page, we're done with it */
NeilBrownd785a062006-06-26 00:27:48 -0700955 if (index == num_pages-1)
NeilBrownf49d5e62007-01-26 00:57:03 -0800956 count = bytes + sizeof(bitmap_super_t)
957 - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -0700958 else
959 count = PAGE_SIZE;
NeilBrown32a76272005-06-21 17:17:14 -0700960 if (index == 0) {
961 /*
962 * if we're here then the superblock page
963 * contains some bits (PAGE_SIZE != sizeof sb)
964 * we've already read it in, so just use it
965 */
966 page = bitmap->sb_page;
967 offset = sizeof(bitmap_super_t);
NeilBrown53845272009-01-09 08:31:05 +1100968 if (!file)
969 read_sb_page(bitmap->mddev,
970 bitmap->offset,
971 page,
972 index, count);
NeilBrowna654b9d82005-06-21 17:17:27 -0700973 } else if (file) {
NeilBrownd785a062006-06-26 00:27:48 -0700974 page = read_page(file, index, bitmap, count);
NeilBrowna654b9d82005-06-21 17:17:27 -0700975 offset = 0;
976 } else {
NeilBrowna2ed9612008-12-19 16:25:01 +1100977 page = read_sb_page(bitmap->mddev, bitmap->offset,
978 NULL,
979 index, count);
NeilBrown32a76272005-06-21 17:17:14 -0700980 offset = 0;
981 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700982 if (IS_ERR(page)) { /* read error */
983 ret = PTR_ERR(page);
NeilBrown4ad13662007-07-17 04:06:13 -0700984 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -0700985 }
986
NeilBrown32a76272005-06-21 17:17:14 -0700987 oldindex = index;
988 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -0700989
NeilBrownb74fd282009-05-07 12:47:19 +1000990 bitmap->filemap[bitmap->file_pages++] = page;
991 bitmap->last_page_size = count;
992
NeilBrown32a76272005-06-21 17:17:14 -0700993 if (outofdate) {
994 /*
995 * if bitmap is out of date, dirty the
996 * whole page and write it out
997 */
NeilBrownea03aff2006-01-06 00:20:34 -0800998 paddr = kmap_atomic(page, KM_USER0);
999 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -07001000 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -08001001 kunmap_atomic(paddr, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -07001002 write_page(bitmap, page, 1);
1003
1004 ret = -EIO;
NeilBrownb74fd282009-05-07 12:47:19 +10001005 if (bitmap->flags & BITMAP_WRITE_ERROR)
NeilBrown4ad13662007-07-17 04:06:13 -07001006 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001007 }
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 */
NeilBrowndb305e52009-05-07 12:49:06 +10001017 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1018 >= start);
1019 bitmap_set_memory_bits(bitmap,
1020 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1021 needed);
NeilBrown32a76272005-06-21 17:17:14 -07001022 bit_cnt++;
NeilBrown6a079972005-09-09 16:23:44 -07001023 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -07001024 }
NeilBrown32a76272005-06-21 17:17:14 -07001025 }
1026
1027 /* everything went OK */
1028 ret = 0;
1029 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1030
NeilBrown32a76272005-06-21 17:17:14 -07001031 if (bit_cnt) { /* Kick recovery if any bits were set */
1032 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1033 md_wakeup_thread(bitmap->mddev->thread);
1034 }
1035
NeilBrown32a76272005-06-21 17:17:14 -07001036 printk(KERN_INFO "%s: bitmap initialized from disk: "
NeilBrown4ad13662007-07-17 04:06:13 -07001037 "read %lu/%lu pages, set %lu bits\n",
1038 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
NeilBrown32a76272005-06-21 17:17:14 -07001039
NeilBrown4ad13662007-07-17 04:06:13 -07001040 return 0;
1041
1042 err:
1043 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1044 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001045 return ret;
1046}
1047
NeilBrowna654b9d82005-06-21 17:17:27 -07001048void bitmap_write_all(struct bitmap *bitmap)
1049{
1050 /* We don't actually write all bitmap blocks here,
1051 * just flag them as needing to be written
1052 */
NeilBrownec7a3192006-06-26 00:27:45 -07001053 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001054
NeilBrownec7a3192006-06-26 00:27:45 -07001055 for (i=0; i < bitmap->file_pages; i++)
1056 set_page_attr(bitmap, bitmap->filemap[i],
1057 BITMAP_PAGE_NEEDWRITE);
NeilBrowna654b9d82005-06-21 17:17:27 -07001058}
1059
NeilBrown32a76272005-06-21 17:17:14 -07001060
1061static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1062{
1063 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1064 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1065 bitmap->bp[page].count += inc;
1066/*
1067 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1068 (unsigned long long)offset, inc, bitmap->bp[page].count);
1069*/
1070 bitmap_checkfree(bitmap, page);
1071}
1072static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1073 sector_t offset, int *blocks,
1074 int create);
1075
1076/*
1077 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1078 * out to disk
1079 */
1080
NeilBrownaa5cbd12009-12-14 12:49:46 +11001081void bitmap_daemon_work(mddev_t *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001082{
NeilBrownaa5cbd12009-12-14 12:49:46 +11001083 struct bitmap *bitmap;
NeilBrownaa3163f2005-06-21 17:17:22 -07001084 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001085 unsigned long flags;
1086 struct page *page = NULL, *lastpage = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001087 int blocks;
NeilBrownea03aff2006-01-06 00:20:34 -08001088 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001089
NeilBrownaa5cbd12009-12-14 12:49:46 +11001090 /* Use a mutex to guard daemon_work against
1091 * bitmap_destroy.
1092 */
1093 mutex_lock(&mddev->bitmap_mutex);
1094 bitmap = mddev->bitmap;
1095 if (bitmap == NULL) {
1096 mutex_unlock(&mddev->bitmap_mutex);
NeilBrown4ad13662007-07-17 04:06:13 -07001097 return;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001098 }
NeilBrown32a76272005-06-21 17:17:14 -07001099 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001100 goto done;
1101
NeilBrown32a76272005-06-21 17:17:14 -07001102 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001103 if (bitmap->allclean) {
1104 bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001105 goto done;
NeilBrown8311c292008-03-04 14:29:30 -08001106 }
1107 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001108
NeilBrownbe512692009-05-26 09:41:17 +10001109 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001110 for (j = 0; j < bitmap->chunks; j++) {
1111 bitmap_counter_t *bmc;
NeilBrownbe512692009-05-26 09:41:17 +10001112 if (!bitmap->filemap)
NeilBrown32a76272005-06-21 17:17:14 -07001113 /* error or shutdown */
NeilBrown32a76272005-06-21 17:17:14 -07001114 break;
NeilBrown32a76272005-06-21 17:17:14 -07001115
1116 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001117
1118 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001119 /* skip this page unless it's marked as needing cleaning */
NeilBrownec7a3192006-06-26 00:27:45 -07001120 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1121 int need_write = test_page_attr(bitmap, page,
1122 BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001123 if (need_write)
NeilBrownaa3163f2005-06-21 17:17:22 -07001124 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001125
NeilBrownaa3163f2005-06-21 17:17:22 -07001126 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown8311c292008-03-04 14:29:30 -08001127 if (need_write) {
NeilBrown4ad13662007-07-17 04:06:13 -07001128 write_page(bitmap, page, 0);
NeilBrown8311c292008-03-04 14:29:30 -08001129 bitmap->allclean = 0;
1130 }
NeilBrownbe512692009-05-26 09:41:17 +10001131 spin_lock_irqsave(&bitmap->lock, flags);
1132 j |= (PAGE_BITS - 1);
NeilBrownaa3163f2005-06-21 17:17:22 -07001133 continue;
1134 }
1135
NeilBrown32a76272005-06-21 17:17:14 -07001136 /* grab the new page, sync and release the old */
NeilBrown32a76272005-06-21 17:17:14 -07001137 if (lastpage != NULL) {
NeilBrownec7a3192006-06-26 00:27:45 -07001138 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001139 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1140 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001141 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001142 } else {
1143 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1144 spin_unlock_irqrestore(&bitmap->lock, flags);
1145 }
NeilBrown32a76272005-06-21 17:17:14 -07001146 } else
1147 spin_unlock_irqrestore(&bitmap->lock, flags);
1148 lastpage = page;
Neil Browna0da84f2008-06-28 08:31:22 +10001149
1150 /* We are possibly going to clear some bits, so make
1151 * sure that events_cleared is up-to-date.
1152 */
1153 if (bitmap->need_sync) {
1154 bitmap_super_t *sb;
1155 bitmap->need_sync = 0;
1156 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1157 sb->events_cleared =
1158 cpu_to_le64(bitmap->events_cleared);
1159 kunmap_atomic(sb, KM_USER0);
1160 write_page(bitmap, bitmap->sb_page, 1);
1161 }
NeilBrown32a76272005-06-21 17:17:14 -07001162 spin_lock_irqsave(&bitmap->lock, flags);
1163 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1164 }
NeilBrowndb305e52009-05-07 12:49:06 +10001165 bmc = bitmap_get_counter(bitmap,
1166 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1167 &blocks, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001168 if (bmc) {
1169/*
1170 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1171*/
NeilBrown8311c292008-03-04 14:29:30 -08001172 if (*bmc)
1173 bitmap->allclean = 0;
1174
NeilBrown32a76272005-06-21 17:17:14 -07001175 if (*bmc == 2) {
1176 *bmc=1; /* maybe clear the bit next time */
1177 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1178 } else if (*bmc == 1) {
1179 /* we can clear the bit */
1180 *bmc = 0;
NeilBrowndb305e52009-05-07 12:49:06 +10001181 bitmap_count_page(bitmap,
1182 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
NeilBrown32a76272005-06-21 17:17:14 -07001183 -1);
1184
1185 /* clear the bit */
NeilBrownea03aff2006-01-06 00:20:34 -08001186 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001187 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001188 clear_bit(file_page_offset(j), paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001189 else
NeilBrownea03aff2006-01-06 00:20:34 -08001190 ext2_clear_bit(file_page_offset(j), paddr);
1191 kunmap_atomic(paddr, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -07001192 }
NeilBrownbe512692009-05-26 09:41:17 +10001193 } else
1194 j |= PAGE_COUNTER_MASK;
NeilBrown32a76272005-06-21 17:17:14 -07001195 }
NeilBrownbe512692009-05-26 09:41:17 +10001196 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001197
1198 /* now sync the final page */
1199 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001200 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001201 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001202 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1203 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001204 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001205 } else {
1206 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1207 spin_unlock_irqrestore(&bitmap->lock, flags);
1208 }
NeilBrown32a76272005-06-21 17:17:14 -07001209 }
1210
NeilBrown7be3dfe2008-03-10 11:43:48 -07001211 done:
NeilBrown8311c292008-03-04 14:29:30 -08001212 if (bitmap->allclean == 0)
1213 bitmap->mddev->thread->timeout = bitmap->daemon_sleep * HZ;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001214 mutex_unlock(&mddev->bitmap_mutex);
NeilBrown32a76272005-06-21 17:17:14 -07001215}
1216
NeilBrown32a76272005-06-21 17:17:14 -07001217static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1218 sector_t offset, int *blocks,
1219 int create)
NeilBrownee305ac2009-09-23 18:06:44 +10001220__releases(bitmap->lock)
1221__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -07001222{
1223 /* If 'create', we might release the lock and reclaim it.
1224 * The lock must have been taken with interrupts enabled.
1225 * If !create, we don't release the lock.
1226 */
1227 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1228 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1229 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1230 sector_t csize;
1231
1232 if (bitmap_checkpage(bitmap, page, create) < 0) {
1233 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1234 *blocks = csize - (offset & (csize- 1));
1235 return NULL;
1236 }
1237 /* now locked ... */
1238
1239 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1240 /* should we use the first or second counter field
1241 * of the hijacked pointer? */
1242 int hi = (pageoff > PAGE_COUNTER_MASK);
1243 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1244 PAGE_COUNTER_SHIFT - 1);
1245 *blocks = csize - (offset & (csize- 1));
1246 return &((bitmap_counter_t *)
1247 &bitmap->bp[page].map)[hi];
1248 } else { /* page is allocated */
1249 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1250 *blocks = csize - (offset & (csize- 1));
1251 return (bitmap_counter_t *)
1252 &(bitmap->bp[page].map[pageoff]);
1253 }
1254}
1255
NeilBrown4b6d2872005-09-09 16:23:47 -07001256int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001257{
1258 if (!bitmap) return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001259
1260 if (behind) {
1261 atomic_inc(&bitmap->behind_writes);
1262 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1263 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1264 }
1265
NeilBrown32a76272005-06-21 17:17:14 -07001266 while (sectors) {
1267 int blocks;
1268 bitmap_counter_t *bmc;
1269
1270 spin_lock_irq(&bitmap->lock);
1271 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1272 if (!bmc) {
1273 spin_unlock_irq(&bitmap->lock);
1274 return 0;
1275 }
1276
Neil Brownda6e1a32007-02-08 14:20:37 -08001277 if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
1278 DEFINE_WAIT(__wait);
1279 /* note that it is safe to do the prepare_to_wait
1280 * after the test as long as we do it before dropping
1281 * the spinlock.
1282 */
1283 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1284 TASK_UNINTERRUPTIBLE);
1285 spin_unlock_irq(&bitmap->lock);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05001286 blk_unplug(bitmap->mddev->queue);
Neil Brownda6e1a32007-02-08 14:20:37 -08001287 schedule();
1288 finish_wait(&bitmap->overflow_wait, &__wait);
1289 continue;
1290 }
1291
NeilBrown32a76272005-06-21 17:17:14 -07001292 switch(*bmc) {
1293 case 0:
1294 bitmap_file_set_bit(bitmap, offset);
1295 bitmap_count_page(bitmap,offset, 1);
Jens Axboe93769f52008-08-01 20:32:31 +02001296 blk_plug_device_unlocked(bitmap->mddev->queue);
NeilBrown32a76272005-06-21 17:17:14 -07001297 /* fall through */
1298 case 1:
1299 *bmc = 2;
1300 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001301
NeilBrown32a76272005-06-21 17:17:14 -07001302 (*bmc)++;
1303
1304 spin_unlock_irq(&bitmap->lock);
1305
1306 offset += blocks;
1307 if (sectors > blocks)
1308 sectors -= blocks;
1309 else sectors = 0;
1310 }
NeilBrown8311c292008-03-04 14:29:30 -08001311 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001312 return 0;
1313}
1314
1315void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001316 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001317{
1318 if (!bitmap) return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001319 if (behind) {
1320 atomic_dec(&bitmap->behind_writes);
1321 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1322 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1323 }
NeilBrownd0a4bb42009-03-31 14:27:02 +11001324 if (bitmap->mddev->degraded)
1325 /* Never clear bits or update events_cleared when degraded */
1326 success = 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001327
NeilBrown32a76272005-06-21 17:17:14 -07001328 while (sectors) {
1329 int blocks;
1330 unsigned long flags;
1331 bitmap_counter_t *bmc;
1332
1333 spin_lock_irqsave(&bitmap->lock, flags);
1334 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1335 if (!bmc) {
1336 spin_unlock_irqrestore(&bitmap->lock, flags);
1337 return;
1338 }
1339
Neil Browna0da84f2008-06-28 08:31:22 +10001340 if (success &&
1341 bitmap->events_cleared < bitmap->mddev->events) {
1342 bitmap->events_cleared = bitmap->mddev->events;
1343 bitmap->need_sync = 1;
1344 }
1345
NeilBrown32a76272005-06-21 17:17:14 -07001346 if (!success && ! (*bmc & NEEDED_MASK))
1347 *bmc |= NEEDED_MASK;
1348
Neil Brownda6e1a32007-02-08 14:20:37 -08001349 if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
1350 wake_up(&bitmap->overflow_wait);
1351
NeilBrown32a76272005-06-21 17:17:14 -07001352 (*bmc)--;
1353 if (*bmc <= 2) {
1354 set_page_attr(bitmap,
1355 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1356 BITMAP_PAGE_CLEAN);
1357 }
1358 spin_unlock_irqrestore(&bitmap->lock, flags);
1359 offset += blocks;
1360 if (sectors > blocks)
1361 sectors -= blocks;
1362 else sectors = 0;
1363 }
1364}
1365
NeilBrown1187cf02009-03-31 14:27:02 +11001366static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1367 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001368{
1369 bitmap_counter_t *bmc;
1370 int rv;
1371 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1372 *blocks = 1024;
1373 return 1; /* always resync if no bitmap */
1374 }
1375 spin_lock_irq(&bitmap->lock);
1376 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1377 rv = 0;
1378 if (bmc) {
1379 /* locked */
1380 if (RESYNC(*bmc))
1381 rv = 1;
1382 else if (NEEDED(*bmc)) {
1383 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001384 if (!degraded) { /* don't set/clear bits if degraded */
1385 *bmc |= RESYNC_MASK;
1386 *bmc &= ~NEEDED_MASK;
1387 }
NeilBrown32a76272005-06-21 17:17:14 -07001388 }
1389 }
1390 spin_unlock_irq(&bitmap->lock);
NeilBrown8311c292008-03-04 14:29:30 -08001391 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001392 return rv;
1393}
1394
NeilBrown1187cf02009-03-31 14:27:02 +11001395int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1396 int degraded)
1397{
1398 /* bitmap_start_sync must always report on multiples of whole
1399 * pages, otherwise resync (which is very PAGE_SIZE based) will
1400 * get confused.
1401 * So call __bitmap_start_sync repeatedly (if needed) until
1402 * At least PAGE_SIZE>>9 blocks are covered.
1403 * Return the 'or' of the result.
1404 */
1405 int rv = 0;
1406 int blocks1;
1407
1408 *blocks = 0;
1409 while (*blocks < (PAGE_SIZE>>9)) {
1410 rv |= __bitmap_start_sync(bitmap, offset,
1411 &blocks1, degraded);
1412 offset += blocks1;
1413 *blocks += blocks1;
1414 }
1415 return rv;
1416}
1417
NeilBrown32a76272005-06-21 17:17:14 -07001418void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1419{
1420 bitmap_counter_t *bmc;
1421 unsigned long flags;
1422/*
1423 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1424*/ if (bitmap == NULL) {
1425 *blocks = 1024;
1426 return;
1427 }
1428 spin_lock_irqsave(&bitmap->lock, flags);
1429 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1430 if (bmc == NULL)
1431 goto unlock;
1432 /* locked */
1433/*
1434 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1435*/
1436 if (RESYNC(*bmc)) {
1437 *bmc &= ~RESYNC_MASK;
1438
1439 if (!NEEDED(*bmc) && aborted)
1440 *bmc |= NEEDED_MASK;
1441 else {
1442 if (*bmc <= 2) {
1443 set_page_attr(bitmap,
1444 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1445 BITMAP_PAGE_CLEAN);
1446 }
1447 }
1448 }
1449 unlock:
1450 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown8311c292008-03-04 14:29:30 -08001451 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001452}
1453
1454void bitmap_close_sync(struct bitmap *bitmap)
1455{
1456 /* Sync has finished, and any bitmap chunks that weren't synced
1457 * properly have been aborted. It remains to us to clear the
1458 * RESYNC bit wherever it is still on
1459 */
1460 sector_t sector = 0;
1461 int blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001462 if (!bitmap)
1463 return;
NeilBrown32a76272005-06-21 17:17:14 -07001464 while (sector < bitmap->mddev->resync_max_sectors) {
1465 bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001466 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001467 }
1468}
1469
NeilBrownb47490c2008-02-06 01:39:50 -08001470void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1471{
1472 sector_t s = 0;
1473 int blocks;
1474
1475 if (!bitmap)
1476 return;
1477 if (sector == 0) {
1478 bitmap->last_end_sync = jiffies;
1479 return;
1480 }
1481 if (time_before(jiffies, (bitmap->last_end_sync
1482 + bitmap->daemon_sleep * HZ)))
1483 return;
1484 wait_event(bitmap->mddev->recovery_wait,
1485 atomic_read(&bitmap->mddev->recovery_active) == 0);
1486
NeilBrown97e4f422009-03-31 14:33:13 +11001487 bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync;
1488 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
NeilBrownb47490c2008-02-06 01:39:50 -08001489 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1490 s = 0;
1491 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1492 bitmap_end_sync(bitmap, s, &blocks, 0);
1493 s += blocks;
1494 }
1495 bitmap->last_end_sync = jiffies;
NeilBrownacb180b2009-04-14 16:28:34 +10001496 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
NeilBrownb47490c2008-02-06 01:39:50 -08001497}
1498
NeilBrown6a079972005-09-09 16:23:44 -07001499static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001500{
1501 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001502 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001503 * be 0 at this point
1504 */
NeilBrown193f1c92005-08-04 12:53:33 -07001505
1506 int secs;
1507 bitmap_counter_t *bmc;
1508 spin_lock_irq(&bitmap->lock);
1509 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1510 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001511 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001512 return;
NeilBrown32a76272005-06-21 17:17:14 -07001513 }
NeilBrown193f1c92005-08-04 12:53:33 -07001514 if (! *bmc) {
1515 struct page *page;
NeilBrown6a079972005-09-09 16:23:44 -07001516 *bmc = 1 | (needed?NEEDED_MASK:0);
NeilBrown193f1c92005-08-04 12:53:33 -07001517 bitmap_count_page(bitmap, offset, 1);
1518 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1519 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1520 }
1521 spin_unlock_irq(&bitmap->lock);
NeilBrown8311c292008-03-04 14:29:30 -08001522 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001523}
1524
Paul Clements9b1d1da2006-10-03 01:15:49 -07001525/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1526void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1527{
1528 unsigned long chunk;
1529
1530 for (chunk = s; chunk <= e; chunk++) {
NeilBrowndb305e52009-05-07 12:49:06 +10001531 sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001532 bitmap_set_memory_bits(bitmap, sec, 1);
1533 bitmap_file_set_bit(bitmap, sec);
1534 }
1535}
1536
NeilBrown32a76272005-06-21 17:17:14 -07001537/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001538 * flush out any pending updates
1539 */
1540void bitmap_flush(mddev_t *mddev)
1541{
1542 struct bitmap *bitmap = mddev->bitmap;
1543 int sleep;
1544
1545 if (!bitmap) /* there was no bitmap */
1546 return;
1547
1548 /* run the daemon_work three time to ensure everything is flushed
1549 * that can be
1550 */
1551 sleep = bitmap->daemon_sleep;
1552 bitmap->daemon_sleep = 0;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001553 bitmap_daemon_work(mddev);
1554 bitmap_daemon_work(mddev);
1555 bitmap_daemon_work(mddev);
NeilBrown6b8b3e82005-08-04 12:53:35 -07001556 bitmap->daemon_sleep = sleep;
1557 bitmap_update_sb(bitmap);
1558}
1559
1560/*
NeilBrown32a76272005-06-21 17:17:14 -07001561 * free memory that was allocated
1562 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001563static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001564{
1565 unsigned long k, pages;
1566 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001567
1568 if (!bitmap) /* there was no bitmap */
1569 return;
1570
NeilBrown32a76272005-06-21 17:17:14 -07001571 /* release the bitmap file and kill the daemon */
1572 bitmap_file_put(bitmap);
1573
1574 bp = bitmap->bp;
1575 pages = bitmap->pages;
1576
1577 /* free all allocated memory */
1578
NeilBrown32a76272005-06-21 17:17:14 -07001579 if (bp) /* deallocate the page memory */
1580 for (k = 0; k < pages; k++)
1581 if (bp[k].map && !bp[k].hijacked)
1582 kfree(bp[k].map);
1583 kfree(bp);
1584 kfree(bitmap);
1585}
NeilBrownaa5cbd12009-12-14 12:49:46 +11001586
NeilBrown3178b0d2005-09-09 16:23:50 -07001587void bitmap_destroy(mddev_t *mddev)
1588{
1589 struct bitmap *bitmap = mddev->bitmap;
1590
1591 if (!bitmap) /* there was no bitmap */
1592 return;
1593
NeilBrownaa5cbd12009-12-14 12:49:46 +11001594 mutex_lock(&mddev->bitmap_mutex);
NeilBrown3178b0d2005-09-09 16:23:50 -07001595 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownaa5cbd12009-12-14 12:49:46 +11001596 mutex_unlock(&mddev->bitmap_mutex);
NeilBrownb15c2e52006-01-06 00:20:16 -08001597 if (mddev->thread)
1598 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001599
1600 bitmap_free(bitmap);
1601}
NeilBrown32a76272005-06-21 17:17:14 -07001602
1603/*
1604 * initialize the bitmap structure
1605 * if this returns an error, bitmap_destroy must be called to do clean up
1606 */
1607int bitmap_create(mddev_t *mddev)
1608{
1609 struct bitmap *bitmap;
NeilBrown1f593902009-04-20 11:50:24 +10001610 sector_t blocks = mddev->resync_max_sectors;
NeilBrown32a76272005-06-21 17:17:14 -07001611 unsigned long chunks;
1612 unsigned long pages;
1613 struct file *file = mddev->bitmap_file;
1614 int err;
NeilBrown6a079972005-09-09 16:23:44 -07001615 sector_t start;
NeilBrown32a76272005-06-21 17:17:14 -07001616
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001617 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001618
NeilBrowna654b9d82005-06-21 17:17:27 -07001619 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001620 return 0;
1621
NeilBrowna654b9d82005-06-21 17:17:27 -07001622 BUG_ON(file && mddev->bitmap_offset);
1623
NeilBrown9ffae0c2006-01-06 00:20:32 -08001624 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001625 if (!bitmap)
1626 return -ENOMEM;
1627
NeilBrown32a76272005-06-21 17:17:14 -07001628 spin_lock_init(&bitmap->lock);
NeilBrownce25c312006-06-26 00:27:49 -07001629 atomic_set(&bitmap->pending_writes, 0);
1630 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001631 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001632
NeilBrown32a76272005-06-21 17:17:14 -07001633 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001634
NeilBrown32a76272005-06-21 17:17:14 -07001635 bitmap->file = file;
NeilBrowna654b9d82005-06-21 17:17:27 -07001636 bitmap->offset = mddev->bitmap_offset;
NeilBrownce25c312006-06-26 00:27:49 -07001637 if (file) {
1638 get_file(file);
NeilBrownae8fa282009-10-16 15:56:01 +11001639 /* As future accesses to this file will use bmap,
1640 * and bypass the page cache, we must sync the file
1641 * first.
1642 */
1643 vfs_fsync(file, file->f_dentry, 1);
NeilBrownce25c312006-06-26 00:27:49 -07001644 }
NeilBrown32a76272005-06-21 17:17:14 -07001645 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1646 err = bitmap_read_sb(bitmap);
1647 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001648 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001649
Paul Clementsa638b2d2006-10-03 01:16:01 -07001650 bitmap->chunkshift = ffz(~bitmap->chunksize);
NeilBrown32a76272005-06-21 17:17:14 -07001651
1652 /* now that chunksize and chunkshift are set, we can use these macros */
NeilBrown1f593902009-04-20 11:50:24 +10001653 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
1654 CHUNK_BLOCK_SHIFT(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -07001655 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1656
1657 BUG_ON(!pages);
1658
1659 bitmap->chunks = chunks;
1660 bitmap->pages = pages;
1661 bitmap->missing_pages = pages;
1662 bitmap->counter_bits = COUNTER_BITS;
1663
1664 bitmap->syncchunk = ~0UL;
1665
Olaf Hering44456d32005-07-27 11:45:17 -07001666#ifdef INJECT_FATAL_FAULT_1
NeilBrown32a76272005-06-21 17:17:14 -07001667 bitmap->bp = NULL;
1668#else
NeilBrown9ffae0c2006-01-06 00:20:32 -08001669 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001670#endif
NeilBrown3178b0d2005-09-09 16:23:50 -07001671 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001672 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001673 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001674
NeilBrown32a76272005-06-21 17:17:14 -07001675 /* now that we have some pages available, initialize the in-memory
1676 * bitmap from the on-disk bitmap */
NeilBrown6a079972005-09-09 16:23:44 -07001677 start = 0;
1678 if (mddev->degraded == 0
1679 || bitmap->events_cleared == mddev->events)
1680 /* no need to keep dirty bits to optimise a re-add of a missing device */
1681 start = mddev->recovery_cp;
1682 err = bitmap_init_from_disk(bitmap, start);
NeilBrown193f1c92005-08-04 12:53:33 -07001683
NeilBrown32a76272005-06-21 17:17:14 -07001684 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001685 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001686
1687 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1688 pages, bmname(bitmap));
1689
NeilBrown3178b0d2005-09-09 16:23:50 -07001690 mddev->bitmap = bitmap;
1691
NeilBrownb15c2e52006-01-06 00:20:16 -08001692 mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1693
NeilBrown4ad13662007-07-17 04:06:13 -07001694 bitmap_update_sb(bitmap);
1695
1696 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
NeilBrown3178b0d2005-09-09 16:23:50 -07001697
1698 error:
1699 bitmap_free(bitmap);
1700 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001701}
1702
1703/* the bitmap API -- for raid personalities */
1704EXPORT_SYMBOL(bitmap_startwrite);
1705EXPORT_SYMBOL(bitmap_endwrite);
1706EXPORT_SYMBOL(bitmap_start_sync);
1707EXPORT_SYMBOL(bitmap_end_sync);
1708EXPORT_SYMBOL(bitmap_unplug);
1709EXPORT_SYMBOL(bitmap_close_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08001710EXPORT_SYMBOL(bitmap_cond_end_sync);