blob: 62958491f329cdedc988d17d38997c0dc69c37d7 [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 */
NeilBrownf6af9492009-12-14 12:49:54 +1100215static struct page *read_sb_page(mddev_t *mddev, loff_t offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100216 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;
NeilBrownf6af9492009-12-14 12:49:54 +1100290 loff_t offset = mddev->bitmap_info.offset;
NeilBrownab6085c2007-05-23 13:58:10 -0700291 if (page->index == bitmap->file_pages-1)
292 size = roundup(bitmap->last_page_size,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400293 bdev_logical_block_size(rdev->bdev));
NeilBrownf0d76d72007-07-17 04:06:12 -0700294 /* Just make sure we aren't corrupting data or
295 * metadata
296 */
NeilBrownf6af9492009-12-14 12:49:54 +1100297 if (mddev->external) {
298 /* Bitmap could be anywhere. */
299 if (rdev->sb_start + offset + (page->index *(PAGE_SIZE/512)) >
300 rdev->data_offset &&
301 rdev->sb_start + offset <
302 rdev->data_offset + mddev->dev_sectors +
303 (PAGE_SIZE/512))
304 goto bad_alignment;
305 } else if (offset < 0) {
NeilBrownf0d76d72007-07-17 04:06:12 -0700306 /* DATA BITMAP METADATA */
NeilBrown42a04b52009-12-14 12:49:53 +1100307 if (offset
NeilBrown85bfb4d2007-10-22 20:45:11 -0700308 + (long)(page->index * (PAGE_SIZE/512))
NeilBrownf0d76d72007-07-17 04:06:12 -0700309 + size/512 > 0)
310 /* bitmap runs in to metadata */
NeilBrown4b809912008-07-21 17:05:25 +1000311 goto bad_alignment;
Andre Noll58c0fed2009-03-31 14:33:13 +1100312 if (rdev->data_offset + mddev->dev_sectors
NeilBrown42a04b52009-12-14 12:49:53 +1100313 > rdev->sb_start + offset)
NeilBrownf0d76d72007-07-17 04:06:12 -0700314 /* data runs in to bitmap */
NeilBrown4b809912008-07-21 17:05:25 +1000315 goto bad_alignment;
Andre Noll0f420352008-07-11 22:02:23 +1000316 } else if (rdev->sb_start < rdev->data_offset) {
NeilBrownf0d76d72007-07-17 04:06:12 -0700317 /* METADATA BITMAP DATA */
Andre Noll0f420352008-07-11 22:02:23 +1000318 if (rdev->sb_start
NeilBrown42a04b52009-12-14 12:49:53 +1100319 + offset
NeilBrownf0d76d72007-07-17 04:06:12 -0700320 + page->index*(PAGE_SIZE/512) + size/512
321 > rdev->data_offset)
322 /* bitmap runs in to data */
NeilBrown4b809912008-07-21 17:05:25 +1000323 goto bad_alignment;
NeilBrownf0d76d72007-07-17 04:06:12 -0700324 } else {
325 /* DATA METADATA BITMAP - no problems */
326 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700327 md_super_write(mddev, rdev,
NeilBrown42a04b52009-12-14 12:49:53 +1100328 rdev->sb_start + offset
NeilBrowna654b9d82005-06-21 17:17:27 -0700329 + page->index * (PAGE_SIZE/512),
NeilBrownab6085c2007-05-23 13:58:10 -0700330 size,
NeilBrowna654b9d82005-06-21 17:17:27 -0700331 page);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000332 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700333
334 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800335 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700336 return 0;
NeilBrown4b809912008-07-21 17:05:25 +1000337
338 bad_alignment:
NeilBrown4b809912008-07-21 17:05:25 +1000339 return -EINVAL;
NeilBrowna654b9d82005-06-21 17:17:27 -0700340}
341
NeilBrown4ad13662007-07-17 04:06:13 -0700342static void bitmap_file_kick(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700343/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700344 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700345 */
NeilBrown4ad13662007-07-17 04:06:13 -0700346static void write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700347{
NeilBrownd785a062006-06-26 00:27:48 -0700348 struct buffer_head *bh;
NeilBrown32a76272005-06-21 17:17:14 -0700349
NeilBrownf0d76d72007-07-17 04:06:12 -0700350 if (bitmap->file == NULL) {
351 switch (write_sb_page(bitmap, page, wait)) {
352 case -EINVAL:
353 bitmap->flags |= BITMAP_WRITE_ERROR;
NeilBrownf0d76d72007-07-17 04:06:12 -0700354 }
NeilBrown4ad13662007-07-17 04:06:13 -0700355 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700356
NeilBrown4ad13662007-07-17 04:06:13 -0700357 bh = page_buffers(page);
NeilBrownc7084432006-01-06 00:20:45 -0800358
NeilBrown4ad13662007-07-17 04:06:13 -0700359 while (bh && bh->b_blocknr) {
360 atomic_inc(&bitmap->pending_writes);
361 set_buffer_locked(bh);
362 set_buffer_mapped(bh);
363 submit_bh(WRITE, bh);
364 bh = bh->b_this_page;
365 }
NeilBrown32a76272005-06-21 17:17:14 -0700366
NeilBrown4ad13662007-07-17 04:06:13 -0700367 if (wait) {
368 wait_event(bitmap->write_wait,
369 atomic_read(&bitmap->pending_writes)==0);
370 }
NeilBrown32a76272005-06-21 17:17:14 -0700371 }
NeilBrown4ad13662007-07-17 04:06:13 -0700372 if (bitmap->flags & BITMAP_WRITE_ERROR)
373 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700374}
375
NeilBrownd785a062006-06-26 00:27:48 -0700376static void end_bitmap_write(struct buffer_head *bh, int uptodate)
NeilBrown32a76272005-06-21 17:17:14 -0700377{
NeilBrownd785a062006-06-26 00:27:48 -0700378 struct bitmap *bitmap = bh->b_private;
379 unsigned long flags;
380
381 if (!uptodate) {
382 spin_lock_irqsave(&bitmap->lock, flags);
383 bitmap->flags |= BITMAP_WRITE_ERROR;
384 spin_unlock_irqrestore(&bitmap->lock, flags);
385 }
386 if (atomic_dec_and_test(&bitmap->pending_writes))
387 wake_up(&bitmap->write_wait);
388}
389
390/* copied from buffer.c */
391static void
392__clear_page_buffers(struct page *page)
393{
394 ClearPagePrivate(page);
395 set_page_private(page, 0);
396 page_cache_release(page);
397}
398static void free_buffers(struct page *page)
399{
400 struct buffer_head *bh = page_buffers(page);
401
402 while (bh) {
403 struct buffer_head *next = bh->b_this_page;
404 free_buffer_head(bh);
405 bh = next;
406 }
407 __clear_page_buffers(page);
408 put_page(page);
409}
410
411/* read a page from a file.
412 * We both read the page, and attach buffers to the page to record the
413 * address of each block (using bmap). These addresses will be used
414 * to write the block later, completely bypassing the filesystem.
415 * This usage is similar to how swap files are handled, and allows us
416 * to write to a file with no concerns of memory allocation failing.
417 */
418static struct page *read_page(struct file *file, unsigned long index,
419 struct bitmap *bitmap,
420 unsigned long count)
421{
NeilBrown32a76272005-06-21 17:17:14 -0700422 struct page *page = NULL;
Josef Sipekc649bb92006-12-08 02:37:19 -0800423 struct inode *inode = file->f_path.dentry->d_inode;
NeilBrownd785a062006-06-26 00:27:48 -0700424 struct buffer_head *bh;
425 sector_t block;
NeilBrown32a76272005-06-21 17:17:14 -0700426
NeilBrown2d1f3b52006-01-06 00:20:31 -0800427 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
428 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700429
NeilBrownd785a062006-06-26 00:27:48 -0700430 page = alloc_page(GFP_KERNEL);
431 if (!page)
432 page = ERR_PTR(-ENOMEM);
NeilBrown32a76272005-06-21 17:17:14 -0700433 if (IS_ERR(page))
434 goto out;
NeilBrownd785a062006-06-26 00:27:48 -0700435
NeilBrownd785a062006-06-26 00:27:48 -0700436 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
437 if (!bh) {
438 put_page(page);
439 page = ERR_PTR(-ENOMEM);
440 goto out;
441 }
442 attach_page_buffers(page, bh);
443 block = index << (PAGE_SHIFT - inode->i_blkbits);
444 while (bh) {
445 if (count == 0)
446 bh->b_blocknr = 0;
447 else {
448 bh->b_blocknr = bmap(inode, block);
449 if (bh->b_blocknr == 0) {
450 /* Cannot use this file! */
451 free_buffers(page);
452 page = ERR_PTR(-EINVAL);
453 goto out;
454 }
455 bh->b_bdev = inode->i_sb->s_bdev;
456 if (count < (1<<inode->i_blkbits))
457 count = 0;
458 else
459 count -= (1<<inode->i_blkbits);
NeilBrown32a76272005-06-21 17:17:14 -0700460
NeilBrownd785a062006-06-26 00:27:48 -0700461 bh->b_end_io = end_bitmap_write;
462 bh->b_private = bitmap;
NeilBrownce25c312006-06-26 00:27:49 -0700463 atomic_inc(&bitmap->pending_writes);
464 set_buffer_locked(bh);
465 set_buffer_mapped(bh);
466 submit_bh(READ, bh);
NeilBrownd785a062006-06-26 00:27:48 -0700467 }
468 block++;
469 bh = bh->b_this_page;
470 }
NeilBrownd785a062006-06-26 00:27:48 -0700471 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700472
473 wait_event(bitmap->write_wait,
474 atomic_read(&bitmap->pending_writes)==0);
475 if (bitmap->flags & BITMAP_WRITE_ERROR) {
476 free_buffers(page);
477 page = ERR_PTR(-EIO);
478 }
NeilBrown32a76272005-06-21 17:17:14 -0700479out:
480 if (IS_ERR(page))
481 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800482 (int)PAGE_SIZE,
483 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700484 PTR_ERR(page));
485 return page;
486}
487
488/*
489 * bitmap file superblock operations
490 */
491
492/* update the event counter and sync the superblock to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700493void bitmap_update_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700494{
495 bitmap_super_t *sb;
496 unsigned long flags;
497
498 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
NeilBrown4ad13662007-07-17 04:06:13 -0700499 return;
NeilBrown32a76272005-06-21 17:17:14 -0700500 spin_lock_irqsave(&bitmap->lock, flags);
501 if (!bitmap->sb_page) { /* no superblock */
502 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700503 return;
NeilBrown32a76272005-06-21 17:17:14 -0700504 }
NeilBrown32a76272005-06-21 17:17:14 -0700505 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800506 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700507 sb->events = cpu_to_le64(bitmap->mddev->events);
Neil Browna0da84f2008-06-28 08:31:22 +1000508 if (bitmap->mddev->events < bitmap->events_cleared) {
509 /* rocking back to read-only */
510 bitmap->events_cleared = bitmap->mddev->events;
511 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
512 }
NeilBrown43a70502009-12-14 12:49:55 +1100513 /* Just in case these have been changed via sysfs: */
514 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
515 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
NeilBrownea03aff2006-01-06 00:20:34 -0800516 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700517 write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700518}
519
520/* print out the bitmap file superblock */
521void bitmap_print_sb(struct bitmap *bitmap)
522{
523 bitmap_super_t *sb;
524
525 if (!bitmap || !bitmap->sb_page)
526 return;
NeilBrownea03aff2006-01-06 00:20:34 -0800527 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700528 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700529 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
530 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
531 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700532 *(__u32 *)(sb->uuid+0),
533 *(__u32 *)(sb->uuid+4),
534 *(__u32 *)(sb->uuid+8),
535 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700536 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700537 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700538 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700539 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700540 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
541 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
542 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
543 printk(KERN_DEBUG " sync size: %llu KB\n",
544 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700545 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800546 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700547}
548
549/* read the superblock from the bitmap file and initialize some bitmap fields */
550static int bitmap_read_sb(struct bitmap *bitmap)
551{
552 char *reason = NULL;
553 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700554 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700555 unsigned long long events;
556 int err = -EINVAL;
557
558 /* page 0 is the superblock, read it... */
NeilBrownf49d5e62007-01-26 00:57:03 -0800559 if (bitmap->file) {
560 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
561 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
562
563 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
564 } else {
NeilBrown42a04b52009-12-14 12:49:53 +1100565 bitmap->sb_page = read_sb_page(bitmap->mddev,
566 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100567 NULL,
568 0, sizeof(bitmap_super_t));
NeilBrowna654b9d82005-06-21 17:17:27 -0700569 }
NeilBrown32a76272005-06-21 17:17:14 -0700570 if (IS_ERR(bitmap->sb_page)) {
571 err = PTR_ERR(bitmap->sb_page);
572 bitmap->sb_page = NULL;
573 return err;
574 }
575
NeilBrownea03aff2006-01-06 00:20:34 -0800576 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700577
NeilBrown32a76272005-06-21 17:17:14 -0700578 chunksize = le32_to_cpu(sb->chunksize);
NeilBrown1b04be92009-12-14 12:49:53 +1100579 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
NeilBrown4b6d2872005-09-09 16:23:47 -0700580 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700581
582 /* verify that the bitmap-specific fields are valid */
583 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
584 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800585 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
586 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700587 reason = "unrecognized superblock version";
NeilBrown1187cf02009-03-31 14:27:02 +1100588 else if (chunksize < 512)
NeilBrown7dd5d342006-01-06 00:20:39 -0800589 reason = "bitmap chunksize too small";
NeilBrown32a76272005-06-21 17:17:14 -0700590 else if ((1 << ffz(~chunksize)) != chunksize)
591 reason = "bitmap chunksize not a power of 2";
NeilBrown1b04be92009-12-14 12:49:53 +1100592 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
NeilBrown7dd5d342006-01-06 00:20:39 -0800593 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700594 else if (write_behind > COUNTER_MAX)
595 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700596 if (reason) {
597 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
598 bmname(bitmap), reason);
599 goto out;
600 }
601
602 /* keep the array size field of the bitmap superblock up to date */
603 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
604
605 if (!bitmap->mddev->persistent)
606 goto success;
607
608 /*
609 * if we have a persistent array superblock, compare the
610 * bitmap's UUID and event counter to the mddev's
611 */
612 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
613 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
614 bmname(bitmap));
615 goto out;
616 }
617 events = le64_to_cpu(sb->events);
618 if (events < bitmap->mddev->events) {
619 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
620 "-- forcing full recovery\n", bmname(bitmap), events,
621 (unsigned long long) bitmap->mddev->events);
NeilBrown4f2e6392006-10-21 10:24:09 -0700622 sb->state |= cpu_to_le32(BITMAP_STALE);
NeilBrown32a76272005-06-21 17:17:14 -0700623 }
624success:
625 /* assign fields using values from superblock */
NeilBrown42a04b52009-12-14 12:49:53 +1100626 bitmap->mddev->bitmap_info.chunksize = chunksize;
627 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
NeilBrown42a04b52009-12-14 12:49:53 +1100628 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
NeilBrown4f2e6392006-10-21 10:24:09 -0700629 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800630 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
631 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700632 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown4f2e6392006-10-21 10:24:09 -0700633 if (sb->state & cpu_to_le32(BITMAP_STALE))
NeilBrown6a079972005-09-09 16:23:44 -0700634 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700635 err = 0;
636out:
NeilBrownea03aff2006-01-06 00:20:34 -0800637 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700638 if (err)
639 bitmap_print_sb(bitmap);
640 return err;
641}
642
643enum bitmap_mask_op {
644 MASK_SET,
645 MASK_UNSET
646};
647
NeilBrown4ad13662007-07-17 04:06:13 -0700648/* record the state of the bitmap in the superblock. Return the old value */
649static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
650 enum bitmap_mask_op op)
NeilBrown32a76272005-06-21 17:17:14 -0700651{
652 bitmap_super_t *sb;
653 unsigned long flags;
NeilBrown4ad13662007-07-17 04:06:13 -0700654 int old;
NeilBrown32a76272005-06-21 17:17:14 -0700655
656 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800657 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700658 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700659 return 0;
NeilBrown32a76272005-06-21 17:17:14 -0700660 }
NeilBrown32a76272005-06-21 17:17:14 -0700661 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrownea03aff2006-01-06 00:20:34 -0800662 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700663 old = le32_to_cpu(sb->state) & bits;
NeilBrown32a76272005-06-21 17:17:14 -0700664 switch (op) {
NeilBrown4f2e6392006-10-21 10:24:09 -0700665 case MASK_SET: sb->state |= cpu_to_le32(bits);
NeilBrown32a76272005-06-21 17:17:14 -0700666 break;
NeilBrown4f2e6392006-10-21 10:24:09 -0700667 case MASK_UNSET: sb->state &= cpu_to_le32(~bits);
NeilBrown32a76272005-06-21 17:17:14 -0700668 break;
669 default: BUG();
670 }
NeilBrownea03aff2006-01-06 00:20:34 -0800671 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700672 return old;
NeilBrown32a76272005-06-21 17:17:14 -0700673}
674
675/*
676 * general bitmap file operations
677 */
678
679/* calculate the index of the page that contains this bit */
680static inline unsigned long file_page_index(unsigned long chunk)
681{
682 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
683}
684
685/* calculate the (bit) offset of this bit within a page */
686static inline unsigned long file_page_offset(unsigned long chunk)
687{
688 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
689}
690
691/*
692 * return a pointer to the page in the filemap that contains the given bit
693 *
694 * this lookup is complicated by the fact that the bitmap sb might be exactly
695 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
696 * 0 or page 1
697 */
698static inline struct page *filemap_get_page(struct bitmap *bitmap,
699 unsigned long chunk)
700{
Paul Clements9b1d1da2006-10-03 01:15:49 -0700701 if (file_page_index(chunk) >= bitmap->file_pages) return NULL;
NeilBrown32a76272005-06-21 17:17:14 -0700702 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
703}
704
705
706static void bitmap_file_unmap(struct bitmap *bitmap)
707{
708 struct page **map, *sb_page;
709 unsigned long *attr;
710 int pages;
711 unsigned long flags;
712
713 spin_lock_irqsave(&bitmap->lock, flags);
714 map = bitmap->filemap;
715 bitmap->filemap = NULL;
716 attr = bitmap->filemap_attr;
717 bitmap->filemap_attr = NULL;
718 pages = bitmap->file_pages;
719 bitmap->file_pages = 0;
720 sb_page = bitmap->sb_page;
721 bitmap->sb_page = NULL;
722 spin_unlock_irqrestore(&bitmap->lock, flags);
723
724 while (pages--)
725 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700726 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700727 kfree(map);
728 kfree(attr);
729
NeilBrownd785a062006-06-26 00:27:48 -0700730 if (sb_page)
731 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700732}
733
734static void bitmap_file_put(struct bitmap *bitmap)
735{
736 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700737 unsigned long flags;
738
739 spin_lock_irqsave(&bitmap->lock, flags);
740 file = bitmap->file;
741 bitmap->file = NULL;
742 spin_unlock_irqrestore(&bitmap->lock, flags);
743
NeilBrownd785a062006-06-26 00:27:48 -0700744 if (file)
745 wait_event(bitmap->write_wait,
746 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700747 bitmap_file_unmap(bitmap);
748
NeilBrownd785a062006-06-26 00:27:48 -0700749 if (file) {
Josef Sipekc649bb92006-12-08 02:37:19 -0800750 struct inode *inode = file->f_path.dentry->d_inode;
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800751 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700752 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700753 }
NeilBrown32a76272005-06-21 17:17:14 -0700754}
755
756
757/*
758 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
759 * then it is no longer reliable, so we stop using it and we mark the file
760 * as failed in the superblock
761 */
762static void bitmap_file_kick(struct bitmap *bitmap)
763{
764 char *path, *ptr = NULL;
765
NeilBrown4ad13662007-07-17 04:06:13 -0700766 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
767 bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700768
NeilBrown4ad13662007-07-17 04:06:13 -0700769 if (bitmap->file) {
770 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
771 if (path)
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700772 ptr = d_path(&bitmap->file->f_path, path,
773 PAGE_SIZE);
774
NeilBrown32a76272005-06-21 17:17:14 -0700775
NeilBrown4ad13662007-07-17 04:06:13 -0700776 printk(KERN_ALERT
777 "%s: kicking failed bitmap file %s from array!\n",
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700778 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700779
NeilBrown4ad13662007-07-17 04:06:13 -0700780 kfree(path);
781 } else
782 printk(KERN_ALERT
783 "%s: disabling internal bitmap due to errors\n",
784 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700785 }
NeilBrown32a76272005-06-21 17:17:14 -0700786
787 bitmap_file_put(bitmap);
788
789 return;
790}
791
792enum bitmap_page_attr {
NeilBrowne16b68b2006-06-26 00:27:45 -0700793 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
794 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
795 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
NeilBrown32a76272005-06-21 17:17:14 -0700796};
797
798static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
799 enum bitmap_page_attr attr)
800{
NeilBrowne16b68b2006-06-26 00:27:45 -0700801 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700802}
803
804static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
805 enum bitmap_page_attr attr)
806{
NeilBrowne16b68b2006-06-26 00:27:45 -0700807 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700808}
809
NeilBrownec7a3192006-06-26 00:27:45 -0700810static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
811 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700812{
NeilBrowne16b68b2006-06-26 00:27:45 -0700813 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700814}
815
816/*
817 * bitmap_file_set_bit -- called before performing a write to the md device
818 * to set (and eventually sync) a particular bit in the bitmap file
819 *
820 * we set the bit immediately, then we record the page number so that
821 * when an unplug occurs, we can flush the dirty pages out to disk
822 */
823static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
824{
825 unsigned long bit;
826 struct page *page;
827 void *kaddr;
828 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
829
NeilBrowna654b9d82005-06-21 17:17:27 -0700830 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700831 return;
832 }
833
834 page = filemap_get_page(bitmap, chunk);
Paul Clements9b1d1da2006-10-03 01:15:49 -0700835 if (!page) return;
NeilBrown32a76272005-06-21 17:17:14 -0700836 bit = file_page_offset(chunk);
837
NeilBrown32a76272005-06-21 17:17:14 -0700838 /* set the bit */
839 kaddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800840 if (bitmap->flags & BITMAP_HOSTENDIAN)
841 set_bit(bit, kaddr);
842 else
843 ext2_set_bit(bit, kaddr);
NeilBrown32a76272005-06-21 17:17:14 -0700844 kunmap_atomic(kaddr, KM_USER0);
845 PRINTK("set file bit %lu page %lu\n", bit, page->index);
846
847 /* record page number so it gets flushed to disk when unplug occurs */
848 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
849
850}
851
852/* this gets called when the md device is ready to unplug its underlying
853 * (slave) device queues -- before we let any writes go down, we need to
854 * sync the dirty pages of the bitmap file to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700855void bitmap_unplug(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700856{
NeilBrownec7a3192006-06-26 00:27:45 -0700857 unsigned long i, flags;
858 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700859 struct page *page;
860 int wait = 0;
861
862 if (!bitmap)
NeilBrown4ad13662007-07-17 04:06:13 -0700863 return;
NeilBrown32a76272005-06-21 17:17:14 -0700864
865 /* look at each page to see if there are any set bits that need to be
866 * flushed out to disk */
867 for (i = 0; i < bitmap->file_pages; i++) {
868 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700869 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700870 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700871 return;
NeilBrown32a76272005-06-21 17:17:14 -0700872 }
873 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700874 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
875 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700876 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
877 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700878 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700879 wait = 1;
880 spin_unlock_irqrestore(&bitmap->lock, flags);
881
NeilBrownd785a062006-06-26 00:27:48 -0700882 if (dirty | need_write)
NeilBrown4ad13662007-07-17 04:06:13 -0700883 write_page(bitmap, page, 0);
NeilBrown32a76272005-06-21 17:17:14 -0700884 }
885 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700886 if (bitmap->file)
NeilBrownd785a062006-06-26 00:27:48 -0700887 wait_event(bitmap->write_wait,
888 atomic_read(&bitmap->pending_writes)==0);
NeilBrown0b79ccf2006-06-26 00:27:44 -0700889 else
NeilBrowna9701a32005-11-08 21:39:34 -0800890 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700891 }
NeilBrownd785a062006-06-26 00:27:48 -0700892 if (bitmap->flags & BITMAP_WRITE_ERROR)
893 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700894}
895
NeilBrown6a079972005-09-09 16:23:44 -0700896static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700897/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
898 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
899 * memory mapping of the bitmap file
900 * Special cases:
901 * if there's no bitmap file, or if the bitmap file had been
902 * previously kicked from the array, we mark all the bits as
903 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700904 *
905 * We ignore all bits for sectors that end earlier than 'start'.
906 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700907 */
NeilBrown6a079972005-09-09 16:23:44 -0700908static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700909{
910 unsigned long i, chunks, index, oldindex, bit;
911 struct page *page = NULL, *oldpage = NULL;
912 unsigned long num_pages, bit_cnt = 0;
913 struct file *file;
NeilBrownd785a062006-06-26 00:27:48 -0700914 unsigned long bytes, offset;
NeilBrown32a76272005-06-21 17:17:14 -0700915 int outofdate;
916 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800917 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700918
919 chunks = bitmap->chunks;
920 file = bitmap->file;
921
NeilBrown42a04b52009-12-14 12:49:53 +1100922 BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
NeilBrown32a76272005-06-21 17:17:14 -0700923
Olaf Hering44456d32005-07-27 11:45:17 -0700924#ifdef INJECT_FAULTS_3
NeilBrown32a76272005-06-21 17:17:14 -0700925 outofdate = 1;
926#else
927 outofdate = bitmap->flags & BITMAP_STALE;
928#endif
929 if (outofdate)
930 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
931 "recovery\n", bmname(bitmap));
932
933 bytes = (chunks + 7) / 8;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700934
NeilBrowncdbb4cc2005-06-21 17:17:18 -0700935 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700936
NeilBrowna654b9d82005-06-21 17:17:27 -0700937 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
NeilBrown32a76272005-06-21 17:17:14 -0700938 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
939 bmname(bitmap),
940 (unsigned long) i_size_read(file->f_mapping->host),
941 bytes + sizeof(bitmap_super_t));
NeilBrown4ad13662007-07-17 04:06:13 -0700942 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700943 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700944
945 ret = -ENOMEM;
946
NeilBrown32a76272005-06-21 17:17:14 -0700947 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700948 if (!bitmap->filemap)
NeilBrown4ad13662007-07-17 04:06:13 -0700949 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700950
NeilBrowne16b68b2006-06-26 00:27:45 -0700951 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
952 bitmap->filemap_attr = kzalloc(
Neil Brown505fa2c2007-04-11 23:28:44 -0700953 roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
NeilBrowne16b68b2006-06-26 00:27:45 -0700954 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700955 if (!bitmap->filemap_attr)
NeilBrown4ad13662007-07-17 04:06:13 -0700956 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700957
NeilBrown32a76272005-06-21 17:17:14 -0700958 oldindex = ~0L;
959
960 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -0800961 int b;
NeilBrown32a76272005-06-21 17:17:14 -0700962 index = file_page_index(i);
963 bit = file_page_offset(i);
964 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -0700965 int count;
NeilBrown32a76272005-06-21 17:17:14 -0700966 /* unmap the old page, we're done with it */
NeilBrownd785a062006-06-26 00:27:48 -0700967 if (index == num_pages-1)
NeilBrownf49d5e62007-01-26 00:57:03 -0800968 count = bytes + sizeof(bitmap_super_t)
969 - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -0700970 else
971 count = PAGE_SIZE;
NeilBrown32a76272005-06-21 17:17:14 -0700972 if (index == 0) {
973 /*
974 * if we're here then the superblock page
975 * contains some bits (PAGE_SIZE != sizeof sb)
976 * we've already read it in, so just use it
977 */
978 page = bitmap->sb_page;
979 offset = sizeof(bitmap_super_t);
NeilBrown53845272009-01-09 08:31:05 +1100980 if (!file)
981 read_sb_page(bitmap->mddev,
NeilBrown42a04b52009-12-14 12:49:53 +1100982 bitmap->mddev->bitmap_info.offset,
NeilBrown53845272009-01-09 08:31:05 +1100983 page,
984 index, count);
NeilBrowna654b9d82005-06-21 17:17:27 -0700985 } else if (file) {
NeilBrownd785a062006-06-26 00:27:48 -0700986 page = read_page(file, index, bitmap, count);
NeilBrowna654b9d82005-06-21 17:17:27 -0700987 offset = 0;
988 } else {
NeilBrown42a04b52009-12-14 12:49:53 +1100989 page = read_sb_page(bitmap->mddev,
990 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100991 NULL,
992 index, count);
NeilBrown32a76272005-06-21 17:17:14 -0700993 offset = 0;
994 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700995 if (IS_ERR(page)) { /* read error */
996 ret = PTR_ERR(page);
NeilBrown4ad13662007-07-17 04:06:13 -0700997 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -0700998 }
999
NeilBrown32a76272005-06-21 17:17:14 -07001000 oldindex = index;
1001 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -07001002
NeilBrownb74fd282009-05-07 12:47:19 +10001003 bitmap->filemap[bitmap->file_pages++] = page;
1004 bitmap->last_page_size = count;
1005
NeilBrown32a76272005-06-21 17:17:14 -07001006 if (outofdate) {
1007 /*
1008 * if bitmap is out of date, dirty the
1009 * whole page and write it out
1010 */
NeilBrownea03aff2006-01-06 00:20:34 -08001011 paddr = kmap_atomic(page, KM_USER0);
1012 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -07001013 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -08001014 kunmap_atomic(paddr, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -07001015 write_page(bitmap, page, 1);
1016
1017 ret = -EIO;
NeilBrownb74fd282009-05-07 12:47:19 +10001018 if (bitmap->flags & BITMAP_WRITE_ERROR)
NeilBrown4ad13662007-07-17 04:06:13 -07001019 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001020 }
NeilBrown32a76272005-06-21 17:17:14 -07001021 }
NeilBrownea03aff2006-01-06 00:20:34 -08001022 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001023 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001024 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001025 else
NeilBrownea03aff2006-01-06 00:20:34 -08001026 b = ext2_test_bit(bit, paddr);
1027 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001028 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -07001029 /* if the disk bit is set, set the memory bit */
NeilBrowndb305e52009-05-07 12:49:06 +10001030 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1031 >= start);
1032 bitmap_set_memory_bits(bitmap,
1033 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1034 needed);
NeilBrown32a76272005-06-21 17:17:14 -07001035 bit_cnt++;
NeilBrown6a079972005-09-09 16:23:44 -07001036 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -07001037 }
NeilBrown32a76272005-06-21 17:17:14 -07001038 }
1039
1040 /* everything went OK */
1041 ret = 0;
1042 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1043
NeilBrown32a76272005-06-21 17:17:14 -07001044 if (bit_cnt) { /* Kick recovery if any bits were set */
1045 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1046 md_wakeup_thread(bitmap->mddev->thread);
1047 }
1048
NeilBrown32a76272005-06-21 17:17:14 -07001049 printk(KERN_INFO "%s: bitmap initialized from disk: "
NeilBrown4ad13662007-07-17 04:06:13 -07001050 "read %lu/%lu pages, set %lu bits\n",
1051 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
NeilBrown32a76272005-06-21 17:17:14 -07001052
NeilBrown4ad13662007-07-17 04:06:13 -07001053 return 0;
1054
1055 err:
1056 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1057 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001058 return ret;
1059}
1060
NeilBrowna654b9d82005-06-21 17:17:27 -07001061void bitmap_write_all(struct bitmap *bitmap)
1062{
1063 /* We don't actually write all bitmap blocks here,
1064 * just flag them as needing to be written
1065 */
NeilBrownec7a3192006-06-26 00:27:45 -07001066 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001067
NeilBrownec7a3192006-06-26 00:27:45 -07001068 for (i=0; i < bitmap->file_pages; i++)
1069 set_page_attr(bitmap, bitmap->filemap[i],
1070 BITMAP_PAGE_NEEDWRITE);
NeilBrowna654b9d82005-06-21 17:17:27 -07001071}
1072
NeilBrown32a76272005-06-21 17:17:14 -07001073
1074static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1075{
1076 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1077 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1078 bitmap->bp[page].count += inc;
1079/*
1080 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1081 (unsigned long long)offset, inc, bitmap->bp[page].count);
1082*/
1083 bitmap_checkfree(bitmap, page);
1084}
1085static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1086 sector_t offset, int *blocks,
1087 int create);
1088
1089/*
1090 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1091 * out to disk
1092 */
1093
NeilBrownaa5cbd12009-12-14 12:49:46 +11001094void bitmap_daemon_work(mddev_t *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001095{
NeilBrownaa5cbd12009-12-14 12:49:46 +11001096 struct bitmap *bitmap;
NeilBrownaa3163f2005-06-21 17:17:22 -07001097 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001098 unsigned long flags;
1099 struct page *page = NULL, *lastpage = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001100 int blocks;
NeilBrownea03aff2006-01-06 00:20:34 -08001101 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001102
NeilBrownaa5cbd12009-12-14 12:49:46 +11001103 /* Use a mutex to guard daemon_work against
1104 * bitmap_destroy.
1105 */
NeilBrownc3d97142009-12-14 12:49:52 +11001106 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrownaa5cbd12009-12-14 12:49:46 +11001107 bitmap = mddev->bitmap;
1108 if (bitmap == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001109 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown4ad13662007-07-17 04:06:13 -07001110 return;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001111 }
NeilBrown42a04b52009-12-14 12:49:53 +11001112 if (time_before(jiffies, bitmap->daemon_lastrun
NeilBrown1b04be92009-12-14 12:49:53 +11001113 + bitmap->mddev->bitmap_info.daemon_sleep))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001114 goto done;
1115
NeilBrown32a76272005-06-21 17:17:14 -07001116 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001117 if (bitmap->allclean) {
1118 bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001119 goto done;
NeilBrown8311c292008-03-04 14:29:30 -08001120 }
1121 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001122
NeilBrownbe512692009-05-26 09:41:17 +10001123 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001124 for (j = 0; j < bitmap->chunks; j++) {
1125 bitmap_counter_t *bmc;
NeilBrownbe512692009-05-26 09:41:17 +10001126 if (!bitmap->filemap)
NeilBrown32a76272005-06-21 17:17:14 -07001127 /* error or shutdown */
NeilBrown32a76272005-06-21 17:17:14 -07001128 break;
NeilBrown32a76272005-06-21 17:17:14 -07001129
1130 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001131
1132 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001133 /* skip this page unless it's marked as needing cleaning */
NeilBrownec7a3192006-06-26 00:27:45 -07001134 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1135 int need_write = test_page_attr(bitmap, page,
1136 BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001137 if (need_write)
NeilBrownaa3163f2005-06-21 17:17:22 -07001138 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001139
NeilBrownaa3163f2005-06-21 17:17:22 -07001140 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown8311c292008-03-04 14:29:30 -08001141 if (need_write) {
NeilBrown4ad13662007-07-17 04:06:13 -07001142 write_page(bitmap, page, 0);
NeilBrown8311c292008-03-04 14:29:30 -08001143 bitmap->allclean = 0;
1144 }
NeilBrownbe512692009-05-26 09:41:17 +10001145 spin_lock_irqsave(&bitmap->lock, flags);
1146 j |= (PAGE_BITS - 1);
NeilBrownaa3163f2005-06-21 17:17:22 -07001147 continue;
1148 }
1149
NeilBrown32a76272005-06-21 17:17:14 -07001150 /* grab the new page, sync and release the old */
NeilBrown32a76272005-06-21 17:17:14 -07001151 if (lastpage != NULL) {
NeilBrownec7a3192006-06-26 00:27:45 -07001152 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001153 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1154 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001155 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001156 } else {
1157 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1158 spin_unlock_irqrestore(&bitmap->lock, flags);
1159 }
NeilBrown32a76272005-06-21 17:17:14 -07001160 } else
1161 spin_unlock_irqrestore(&bitmap->lock, flags);
1162 lastpage = page;
Neil Browna0da84f2008-06-28 08:31:22 +10001163
1164 /* We are possibly going to clear some bits, so make
1165 * sure that events_cleared is up-to-date.
1166 */
1167 if (bitmap->need_sync) {
1168 bitmap_super_t *sb;
1169 bitmap->need_sync = 0;
1170 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1171 sb->events_cleared =
1172 cpu_to_le64(bitmap->events_cleared);
1173 kunmap_atomic(sb, KM_USER0);
1174 write_page(bitmap, bitmap->sb_page, 1);
1175 }
NeilBrown32a76272005-06-21 17:17:14 -07001176 spin_lock_irqsave(&bitmap->lock, flags);
1177 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1178 }
NeilBrowndb305e52009-05-07 12:49:06 +10001179 bmc = bitmap_get_counter(bitmap,
1180 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1181 &blocks, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001182 if (bmc) {
1183/*
1184 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1185*/
NeilBrown8311c292008-03-04 14:29:30 -08001186 if (*bmc)
1187 bitmap->allclean = 0;
1188
NeilBrown32a76272005-06-21 17:17:14 -07001189 if (*bmc == 2) {
1190 *bmc=1; /* maybe clear the bit next time */
1191 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1192 } else if (*bmc == 1) {
1193 /* we can clear the bit */
1194 *bmc = 0;
NeilBrowndb305e52009-05-07 12:49:06 +10001195 bitmap_count_page(bitmap,
1196 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
NeilBrown32a76272005-06-21 17:17:14 -07001197 -1);
1198
1199 /* clear the bit */
NeilBrownea03aff2006-01-06 00:20:34 -08001200 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001201 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001202 clear_bit(file_page_offset(j), paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001203 else
NeilBrownea03aff2006-01-06 00:20:34 -08001204 ext2_clear_bit(file_page_offset(j), paddr);
1205 kunmap_atomic(paddr, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -07001206 }
NeilBrownbe512692009-05-26 09:41:17 +10001207 } else
1208 j |= PAGE_COUNTER_MASK;
NeilBrown32a76272005-06-21 17:17:14 -07001209 }
NeilBrownbe512692009-05-26 09:41:17 +10001210 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001211
1212 /* now sync the final page */
1213 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001214 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001215 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001216 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1217 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001218 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001219 } else {
1220 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1221 spin_unlock_irqrestore(&bitmap->lock, flags);
1222 }
NeilBrown32a76272005-06-21 17:17:14 -07001223 }
1224
NeilBrown7be3dfe2008-03-10 11:43:48 -07001225 done:
NeilBrown8311c292008-03-04 14:29:30 -08001226 if (bitmap->allclean == 0)
NeilBrown42a04b52009-12-14 12:49:53 +11001227 bitmap->mddev->thread->timeout =
NeilBrown1b04be92009-12-14 12:49:53 +11001228 bitmap->mddev->bitmap_info.daemon_sleep;
NeilBrownc3d97142009-12-14 12:49:52 +11001229 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown32a76272005-06-21 17:17:14 -07001230}
1231
NeilBrown32a76272005-06-21 17:17:14 -07001232static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1233 sector_t offset, int *blocks,
1234 int create)
NeilBrownee305ac2009-09-23 18:06:44 +10001235__releases(bitmap->lock)
1236__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -07001237{
1238 /* If 'create', we might release the lock and reclaim it.
1239 * The lock must have been taken with interrupts enabled.
1240 * If !create, we don't release the lock.
1241 */
1242 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1243 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1244 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1245 sector_t csize;
1246
1247 if (bitmap_checkpage(bitmap, page, create) < 0) {
1248 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1249 *blocks = csize - (offset & (csize- 1));
1250 return NULL;
1251 }
1252 /* now locked ... */
1253
1254 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1255 /* should we use the first or second counter field
1256 * of the hijacked pointer? */
1257 int hi = (pageoff > PAGE_COUNTER_MASK);
1258 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1259 PAGE_COUNTER_SHIFT - 1);
1260 *blocks = csize - (offset & (csize- 1));
1261 return &((bitmap_counter_t *)
1262 &bitmap->bp[page].map)[hi];
1263 } else { /* page is allocated */
1264 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1265 *blocks = csize - (offset & (csize- 1));
1266 return (bitmap_counter_t *)
1267 &(bitmap->bp[page].map[pageoff]);
1268 }
1269}
1270
NeilBrown4b6d2872005-09-09 16:23:47 -07001271int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001272{
1273 if (!bitmap) return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001274
1275 if (behind) {
1276 atomic_inc(&bitmap->behind_writes);
1277 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1278 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1279 }
1280
NeilBrown32a76272005-06-21 17:17:14 -07001281 while (sectors) {
1282 int blocks;
1283 bitmap_counter_t *bmc;
1284
1285 spin_lock_irq(&bitmap->lock);
1286 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1287 if (!bmc) {
1288 spin_unlock_irq(&bitmap->lock);
1289 return 0;
1290 }
1291
Neil Brownda6e1a32007-02-08 14:20:37 -08001292 if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
1293 DEFINE_WAIT(__wait);
1294 /* note that it is safe to do the prepare_to_wait
1295 * after the test as long as we do it before dropping
1296 * the spinlock.
1297 */
1298 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1299 TASK_UNINTERRUPTIBLE);
1300 spin_unlock_irq(&bitmap->lock);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05001301 blk_unplug(bitmap->mddev->queue);
Neil Brownda6e1a32007-02-08 14:20:37 -08001302 schedule();
1303 finish_wait(&bitmap->overflow_wait, &__wait);
1304 continue;
1305 }
1306
NeilBrown32a76272005-06-21 17:17:14 -07001307 switch(*bmc) {
1308 case 0:
1309 bitmap_file_set_bit(bitmap, offset);
1310 bitmap_count_page(bitmap,offset, 1);
Jens Axboe93769f52008-08-01 20:32:31 +02001311 blk_plug_device_unlocked(bitmap->mddev->queue);
NeilBrown32a76272005-06-21 17:17:14 -07001312 /* fall through */
1313 case 1:
1314 *bmc = 2;
1315 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001316
NeilBrown32a76272005-06-21 17:17:14 -07001317 (*bmc)++;
1318
1319 spin_unlock_irq(&bitmap->lock);
1320
1321 offset += blocks;
1322 if (sectors > blocks)
1323 sectors -= blocks;
1324 else sectors = 0;
1325 }
NeilBrown8311c292008-03-04 14:29:30 -08001326 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001327 return 0;
1328}
1329
1330void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001331 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001332{
1333 if (!bitmap) return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001334 if (behind) {
1335 atomic_dec(&bitmap->behind_writes);
1336 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1337 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1338 }
NeilBrownd0a4bb42009-03-31 14:27:02 +11001339 if (bitmap->mddev->degraded)
1340 /* Never clear bits or update events_cleared when degraded */
1341 success = 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001342
NeilBrown32a76272005-06-21 17:17:14 -07001343 while (sectors) {
1344 int blocks;
1345 unsigned long flags;
1346 bitmap_counter_t *bmc;
1347
1348 spin_lock_irqsave(&bitmap->lock, flags);
1349 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1350 if (!bmc) {
1351 spin_unlock_irqrestore(&bitmap->lock, flags);
1352 return;
1353 }
1354
Neil Browna0da84f2008-06-28 08:31:22 +10001355 if (success &&
1356 bitmap->events_cleared < bitmap->mddev->events) {
1357 bitmap->events_cleared = bitmap->mddev->events;
1358 bitmap->need_sync = 1;
1359 }
1360
NeilBrown32a76272005-06-21 17:17:14 -07001361 if (!success && ! (*bmc & NEEDED_MASK))
1362 *bmc |= NEEDED_MASK;
1363
Neil Brownda6e1a32007-02-08 14:20:37 -08001364 if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
1365 wake_up(&bitmap->overflow_wait);
1366
NeilBrown32a76272005-06-21 17:17:14 -07001367 (*bmc)--;
1368 if (*bmc <= 2) {
1369 set_page_attr(bitmap,
1370 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1371 BITMAP_PAGE_CLEAN);
1372 }
1373 spin_unlock_irqrestore(&bitmap->lock, flags);
1374 offset += blocks;
1375 if (sectors > blocks)
1376 sectors -= blocks;
1377 else sectors = 0;
1378 }
1379}
1380
NeilBrown1187cf02009-03-31 14:27:02 +11001381static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1382 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001383{
1384 bitmap_counter_t *bmc;
1385 int rv;
1386 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1387 *blocks = 1024;
1388 return 1; /* always resync if no bitmap */
1389 }
1390 spin_lock_irq(&bitmap->lock);
1391 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1392 rv = 0;
1393 if (bmc) {
1394 /* locked */
1395 if (RESYNC(*bmc))
1396 rv = 1;
1397 else if (NEEDED(*bmc)) {
1398 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001399 if (!degraded) { /* don't set/clear bits if degraded */
1400 *bmc |= RESYNC_MASK;
1401 *bmc &= ~NEEDED_MASK;
1402 }
NeilBrown32a76272005-06-21 17:17:14 -07001403 }
1404 }
1405 spin_unlock_irq(&bitmap->lock);
NeilBrown8311c292008-03-04 14:29:30 -08001406 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001407 return rv;
1408}
1409
NeilBrown1187cf02009-03-31 14:27:02 +11001410int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1411 int degraded)
1412{
1413 /* bitmap_start_sync must always report on multiples of whole
1414 * pages, otherwise resync (which is very PAGE_SIZE based) will
1415 * get confused.
1416 * So call __bitmap_start_sync repeatedly (if needed) until
1417 * At least PAGE_SIZE>>9 blocks are covered.
1418 * Return the 'or' of the result.
1419 */
1420 int rv = 0;
1421 int blocks1;
1422
1423 *blocks = 0;
1424 while (*blocks < (PAGE_SIZE>>9)) {
1425 rv |= __bitmap_start_sync(bitmap, offset,
1426 &blocks1, degraded);
1427 offset += blocks1;
1428 *blocks += blocks1;
1429 }
1430 return rv;
1431}
1432
NeilBrown32a76272005-06-21 17:17:14 -07001433void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1434{
1435 bitmap_counter_t *bmc;
1436 unsigned long flags;
1437/*
1438 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1439*/ if (bitmap == NULL) {
1440 *blocks = 1024;
1441 return;
1442 }
1443 spin_lock_irqsave(&bitmap->lock, flags);
1444 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1445 if (bmc == NULL)
1446 goto unlock;
1447 /* locked */
1448/*
1449 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1450*/
1451 if (RESYNC(*bmc)) {
1452 *bmc &= ~RESYNC_MASK;
1453
1454 if (!NEEDED(*bmc) && aborted)
1455 *bmc |= NEEDED_MASK;
1456 else {
1457 if (*bmc <= 2) {
1458 set_page_attr(bitmap,
1459 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1460 BITMAP_PAGE_CLEAN);
1461 }
1462 }
1463 }
1464 unlock:
1465 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown8311c292008-03-04 14:29:30 -08001466 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001467}
1468
1469void bitmap_close_sync(struct bitmap *bitmap)
1470{
1471 /* Sync has finished, and any bitmap chunks that weren't synced
1472 * properly have been aborted. It remains to us to clear the
1473 * RESYNC bit wherever it is still on
1474 */
1475 sector_t sector = 0;
1476 int blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001477 if (!bitmap)
1478 return;
NeilBrown32a76272005-06-21 17:17:14 -07001479 while (sector < bitmap->mddev->resync_max_sectors) {
1480 bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001481 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001482 }
1483}
1484
NeilBrownb47490c2008-02-06 01:39:50 -08001485void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1486{
1487 sector_t s = 0;
1488 int blocks;
1489
1490 if (!bitmap)
1491 return;
1492 if (sector == 0) {
1493 bitmap->last_end_sync = jiffies;
1494 return;
1495 }
1496 if (time_before(jiffies, (bitmap->last_end_sync
NeilBrown1b04be92009-12-14 12:49:53 +11001497 + bitmap->mddev->bitmap_info.daemon_sleep)))
NeilBrownb47490c2008-02-06 01:39:50 -08001498 return;
1499 wait_event(bitmap->mddev->recovery_wait,
1500 atomic_read(&bitmap->mddev->recovery_active) == 0);
1501
NeilBrown97e4f422009-03-31 14:33:13 +11001502 bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync;
1503 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
NeilBrownb47490c2008-02-06 01:39:50 -08001504 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1505 s = 0;
1506 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1507 bitmap_end_sync(bitmap, s, &blocks, 0);
1508 s += blocks;
1509 }
1510 bitmap->last_end_sync = jiffies;
NeilBrownacb180b2009-04-14 16:28:34 +10001511 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
NeilBrownb47490c2008-02-06 01:39:50 -08001512}
1513
NeilBrown6a079972005-09-09 16:23:44 -07001514static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001515{
1516 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001517 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001518 * be 0 at this point
1519 */
NeilBrown193f1c92005-08-04 12:53:33 -07001520
1521 int secs;
1522 bitmap_counter_t *bmc;
1523 spin_lock_irq(&bitmap->lock);
1524 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1525 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001526 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001527 return;
NeilBrown32a76272005-06-21 17:17:14 -07001528 }
NeilBrown193f1c92005-08-04 12:53:33 -07001529 if (! *bmc) {
1530 struct page *page;
NeilBrown6a079972005-09-09 16:23:44 -07001531 *bmc = 1 | (needed?NEEDED_MASK:0);
NeilBrown193f1c92005-08-04 12:53:33 -07001532 bitmap_count_page(bitmap, offset, 1);
1533 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1534 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1535 }
1536 spin_unlock_irq(&bitmap->lock);
NeilBrown8311c292008-03-04 14:29:30 -08001537 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001538}
1539
Paul Clements9b1d1da2006-10-03 01:15:49 -07001540/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1541void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1542{
1543 unsigned long chunk;
1544
1545 for (chunk = s; chunk <= e; chunk++) {
NeilBrowndb305e52009-05-07 12:49:06 +10001546 sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001547 bitmap_set_memory_bits(bitmap, sec, 1);
1548 bitmap_file_set_bit(bitmap, sec);
1549 }
1550}
1551
NeilBrown32a76272005-06-21 17:17:14 -07001552/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001553 * flush out any pending updates
1554 */
1555void bitmap_flush(mddev_t *mddev)
1556{
1557 struct bitmap *bitmap = mddev->bitmap;
NeilBrown42a04b52009-12-14 12:49:53 +11001558 long sleep;
NeilBrown6b8b3e82005-08-04 12:53:35 -07001559
1560 if (!bitmap) /* there was no bitmap */
1561 return;
1562
1563 /* run the daemon_work three time to ensure everything is flushed
1564 * that can be
1565 */
NeilBrown1b04be92009-12-14 12:49:53 +11001566 sleep = mddev->bitmap_info.daemon_sleep * 2;
NeilBrown42a04b52009-12-14 12:49:53 +11001567 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001568 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001569 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001570 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001571 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001572 bitmap_daemon_work(mddev);
NeilBrown6b8b3e82005-08-04 12:53:35 -07001573 bitmap_update_sb(bitmap);
1574}
1575
1576/*
NeilBrown32a76272005-06-21 17:17:14 -07001577 * free memory that was allocated
1578 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001579static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001580{
1581 unsigned long k, pages;
1582 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001583
1584 if (!bitmap) /* there was no bitmap */
1585 return;
1586
NeilBrown32a76272005-06-21 17:17:14 -07001587 /* release the bitmap file and kill the daemon */
1588 bitmap_file_put(bitmap);
1589
1590 bp = bitmap->bp;
1591 pages = bitmap->pages;
1592
1593 /* free all allocated memory */
1594
NeilBrown32a76272005-06-21 17:17:14 -07001595 if (bp) /* deallocate the page memory */
1596 for (k = 0; k < pages; k++)
1597 if (bp[k].map && !bp[k].hijacked)
1598 kfree(bp[k].map);
1599 kfree(bp);
1600 kfree(bitmap);
1601}
NeilBrownaa5cbd12009-12-14 12:49:46 +11001602
NeilBrown3178b0d2005-09-09 16:23:50 -07001603void bitmap_destroy(mddev_t *mddev)
1604{
1605 struct bitmap *bitmap = mddev->bitmap;
1606
1607 if (!bitmap) /* there was no bitmap */
1608 return;
1609
NeilBrownc3d97142009-12-14 12:49:52 +11001610 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrown3178b0d2005-09-09 16:23:50 -07001611 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownc3d97142009-12-14 12:49:52 +11001612 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrownb15c2e52006-01-06 00:20:16 -08001613 if (mddev->thread)
1614 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001615
1616 bitmap_free(bitmap);
1617}
NeilBrown32a76272005-06-21 17:17:14 -07001618
1619/*
1620 * initialize the bitmap structure
1621 * if this returns an error, bitmap_destroy must be called to do clean up
1622 */
1623int bitmap_create(mddev_t *mddev)
1624{
1625 struct bitmap *bitmap;
NeilBrown1f593902009-04-20 11:50:24 +10001626 sector_t blocks = mddev->resync_max_sectors;
NeilBrown32a76272005-06-21 17:17:14 -07001627 unsigned long chunks;
1628 unsigned long pages;
NeilBrownc3d97142009-12-14 12:49:52 +11001629 struct file *file = mddev->bitmap_info.file;
NeilBrown32a76272005-06-21 17:17:14 -07001630 int err;
NeilBrown6a079972005-09-09 16:23:44 -07001631 sector_t start;
NeilBrown32a76272005-06-21 17:17:14 -07001632
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001633 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001634
NeilBrownc3d97142009-12-14 12:49:52 +11001635 if (!file && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001636 return 0;
1637
NeilBrownc3d97142009-12-14 12:49:52 +11001638 BUG_ON(file && mddev->bitmap_info.offset);
NeilBrowna654b9d82005-06-21 17:17:27 -07001639
NeilBrown9ffae0c2006-01-06 00:20:32 -08001640 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001641 if (!bitmap)
1642 return -ENOMEM;
1643
NeilBrown32a76272005-06-21 17:17:14 -07001644 spin_lock_init(&bitmap->lock);
NeilBrownce25c312006-06-26 00:27:49 -07001645 atomic_set(&bitmap->pending_writes, 0);
1646 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001647 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001648
NeilBrown32a76272005-06-21 17:17:14 -07001649 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001650
NeilBrown32a76272005-06-21 17:17:14 -07001651 bitmap->file = file;
NeilBrownce25c312006-06-26 00:27:49 -07001652 if (file) {
1653 get_file(file);
NeilBrownae8fa282009-10-16 15:56:01 +11001654 /* As future accesses to this file will use bmap,
1655 * and bypass the page cache, we must sync the file
1656 * first.
1657 */
1658 vfs_fsync(file, file->f_dentry, 1);
NeilBrownce25c312006-06-26 00:27:49 -07001659 }
NeilBrown42a04b52009-12-14 12:49:53 +11001660 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
NeilBrown32a76272005-06-21 17:17:14 -07001661 err = bitmap_read_sb(bitmap);
1662 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001663 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001664
NeilBrown624ce4f2009-12-14 12:49:56 +11001665 bitmap->daemon_lastrun = jiffies;
NeilBrown42a04b52009-12-14 12:49:53 +11001666 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
NeilBrown32a76272005-06-21 17:17:14 -07001667
1668 /* now that chunksize and chunkshift are set, we can use these macros */
NeilBrown1f593902009-04-20 11:50:24 +10001669 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
1670 CHUNK_BLOCK_SHIFT(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -07001671 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1672
1673 BUG_ON(!pages);
1674
1675 bitmap->chunks = chunks;
1676 bitmap->pages = pages;
1677 bitmap->missing_pages = pages;
1678 bitmap->counter_bits = COUNTER_BITS;
1679
1680 bitmap->syncchunk = ~0UL;
1681
Olaf Hering44456d32005-07-27 11:45:17 -07001682#ifdef INJECT_FATAL_FAULT_1
NeilBrown32a76272005-06-21 17:17:14 -07001683 bitmap->bp = NULL;
1684#else
NeilBrown9ffae0c2006-01-06 00:20:32 -08001685 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001686#endif
NeilBrown3178b0d2005-09-09 16:23:50 -07001687 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001688 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001689 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001690
NeilBrown32a76272005-06-21 17:17:14 -07001691 /* now that we have some pages available, initialize the in-memory
1692 * bitmap from the on-disk bitmap */
NeilBrown6a079972005-09-09 16:23:44 -07001693 start = 0;
1694 if (mddev->degraded == 0
1695 || bitmap->events_cleared == mddev->events)
1696 /* no need to keep dirty bits to optimise a re-add of a missing device */
1697 start = mddev->recovery_cp;
1698 err = bitmap_init_from_disk(bitmap, start);
NeilBrown193f1c92005-08-04 12:53:33 -07001699
NeilBrown32a76272005-06-21 17:17:14 -07001700 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001701 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001702
1703 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1704 pages, bmname(bitmap));
1705
NeilBrown3178b0d2005-09-09 16:23:50 -07001706 mddev->bitmap = bitmap;
1707
NeilBrown1b04be92009-12-14 12:49:53 +11001708 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
NeilBrown9cd30fd2009-12-14 12:49:54 +11001709 md_wakeup_thread(mddev->thread);
NeilBrownb15c2e52006-01-06 00:20:16 -08001710
NeilBrown4ad13662007-07-17 04:06:13 -07001711 bitmap_update_sb(bitmap);
1712
1713 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
NeilBrown3178b0d2005-09-09 16:23:50 -07001714
1715 error:
1716 bitmap_free(bitmap);
1717 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001718}
1719
NeilBrown43a70502009-12-14 12:49:55 +11001720static ssize_t
1721location_show(mddev_t *mddev, char *page)
1722{
1723 ssize_t len;
1724 if (mddev->bitmap_info.file) {
1725 len = sprintf(page, "file");
1726 } else if (mddev->bitmap_info.offset) {
1727 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
1728 } else
1729 len = sprintf(page, "none");
1730 len += sprintf(page+len, "\n");
1731 return len;
1732}
1733
1734static ssize_t
1735location_store(mddev_t *mddev, const char *buf, size_t len)
1736{
1737
1738 if (mddev->pers) {
1739 if (!mddev->pers->quiesce)
1740 return -EBUSY;
1741 if (mddev->recovery || mddev->sync_thread)
1742 return -EBUSY;
1743 }
1744
1745 if (mddev->bitmap || mddev->bitmap_info.file ||
1746 mddev->bitmap_info.offset) {
1747 /* bitmap already configured. Only option is to clear it */
1748 if (strncmp(buf, "none", 4) != 0)
1749 return -EBUSY;
1750 if (mddev->pers) {
1751 mddev->pers->quiesce(mddev, 1);
1752 bitmap_destroy(mddev);
1753 mddev->pers->quiesce(mddev, 0);
1754 }
1755 mddev->bitmap_info.offset = 0;
1756 if (mddev->bitmap_info.file) {
1757 struct file *f = mddev->bitmap_info.file;
1758 mddev->bitmap_info.file = NULL;
1759 restore_bitmap_write_access(f);
1760 fput(f);
1761 }
1762 } else {
1763 /* No bitmap, OK to set a location */
1764 long long offset;
1765 if (strncmp(buf, "none", 4) == 0)
1766 /* nothing to be done */;
1767 else if (strncmp(buf, "file:", 5) == 0) {
1768 /* Not supported yet */
1769 return -EINVAL;
1770 } else {
1771 int rv;
1772 if (buf[0] == '+')
1773 rv = strict_strtoll(buf+1, 10, &offset);
1774 else
1775 rv = strict_strtoll(buf, 10, &offset);
1776 if (rv)
1777 return rv;
1778 if (offset == 0)
1779 return -EINVAL;
1780 if (mddev->major_version == 0 &&
1781 offset != mddev->bitmap_info.default_offset)
1782 return -EINVAL;
1783 mddev->bitmap_info.offset = offset;
1784 if (mddev->pers) {
1785 mddev->pers->quiesce(mddev, 1);
1786 rv = bitmap_create(mddev);
1787 if (rv) {
1788 bitmap_destroy(mddev);
1789 mddev->bitmap_info.offset = 0;
1790 }
1791 mddev->pers->quiesce(mddev, 0);
1792 if (rv)
1793 return rv;
1794 }
1795 }
1796 }
1797 if (!mddev->external) {
1798 /* Ensure new bitmap info is stored in
1799 * metadata promptly.
1800 */
1801 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1802 md_wakeup_thread(mddev->thread);
1803 }
1804 return len;
1805}
1806
1807static struct md_sysfs_entry bitmap_location =
1808__ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
1809
1810static ssize_t
1811timeout_show(mddev_t *mddev, char *page)
1812{
1813 ssize_t len;
1814 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
1815 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
1816
1817 len = sprintf(page, "%lu", secs);
1818 if (jifs)
1819 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
1820 len += sprintf(page+len, "\n");
1821 return len;
1822}
1823
1824static ssize_t
1825timeout_store(mddev_t *mddev, const char *buf, size_t len)
1826{
1827 /* timeout can be set at any time */
1828 unsigned long timeout;
1829 int rv = strict_strtoul_scaled(buf, &timeout, 4);
1830 if (rv)
1831 return rv;
1832
1833 /* just to make sure we don't overflow... */
1834 if (timeout >= LONG_MAX / HZ)
1835 return -EINVAL;
1836
1837 timeout = timeout * HZ / 10000;
1838
1839 if (timeout >= MAX_SCHEDULE_TIMEOUT)
1840 timeout = MAX_SCHEDULE_TIMEOUT-1;
1841 if (timeout < 1)
1842 timeout = 1;
1843 mddev->bitmap_info.daemon_sleep = timeout;
1844 if (mddev->thread) {
1845 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1846 * the bitmap is all clean and we don't need to
1847 * adjust the timeout right now
1848 */
1849 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
1850 mddev->thread->timeout = timeout;
1851 md_wakeup_thread(mddev->thread);
1852 }
1853 }
1854 return len;
1855}
1856
1857static struct md_sysfs_entry bitmap_timeout =
1858__ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
1859
1860static ssize_t
1861backlog_show(mddev_t *mddev, char *page)
1862{
1863 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
1864}
1865
1866static ssize_t
1867backlog_store(mddev_t *mddev, const char *buf, size_t len)
1868{
1869 unsigned long backlog;
1870 int rv = strict_strtoul(buf, 10, &backlog);
1871 if (rv)
1872 return rv;
1873 if (backlog > COUNTER_MAX)
1874 return -EINVAL;
1875 mddev->bitmap_info.max_write_behind = backlog;
1876 return len;
1877}
1878
1879static struct md_sysfs_entry bitmap_backlog =
1880__ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
1881
1882static ssize_t
1883chunksize_show(mddev_t *mddev, char *page)
1884{
1885 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
1886}
1887
1888static ssize_t
1889chunksize_store(mddev_t *mddev, const char *buf, size_t len)
1890{
1891 /* Can only be changed when no bitmap is active */
1892 int rv;
1893 unsigned long csize;
1894 if (mddev->bitmap)
1895 return -EBUSY;
1896 rv = strict_strtoul(buf, 10, &csize);
1897 if (rv)
1898 return rv;
1899 if (csize < 512 ||
1900 !is_power_of_2(csize))
1901 return -EINVAL;
1902 mddev->bitmap_info.chunksize = csize;
1903 return len;
1904}
1905
1906static struct md_sysfs_entry bitmap_chunksize =
1907__ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
1908
1909static struct attribute *md_bitmap_attrs[] = {
1910 &bitmap_location.attr,
1911 &bitmap_timeout.attr,
1912 &bitmap_backlog.attr,
1913 &bitmap_chunksize.attr,
1914 NULL
1915};
1916struct attribute_group md_bitmap_group = {
1917 .name = "bitmap",
1918 .attrs = md_bitmap_attrs,
1919};
1920
1921
NeilBrown32a76272005-06-21 17:17:14 -07001922/* the bitmap API -- for raid personalities */
1923EXPORT_SYMBOL(bitmap_startwrite);
1924EXPORT_SYMBOL(bitmap_endwrite);
1925EXPORT_SYMBOL(bitmap_start_sync);
1926EXPORT_SYMBOL(bitmap_end_sync);
1927EXPORT_SYMBOL(bitmap_unplug);
1928EXPORT_SYMBOL(bitmap_close_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08001929EXPORT_SYMBOL(bitmap_cond_end_sync);