blob: c9c6a345e17bbb674239a8d39ce7748fb0fc2d49 [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;
NeilBrownece5cff2009-12-14 12:49:56 +1100500 if (bitmap->mddev->bitmap_info.external)
501 return;
NeilBrown32a76272005-06-21 17:17:14 -0700502 spin_lock_irqsave(&bitmap->lock, flags);
503 if (!bitmap->sb_page) { /* no superblock */
504 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700505 return;
NeilBrown32a76272005-06-21 17:17:14 -0700506 }
NeilBrown32a76272005-06-21 17:17:14 -0700507 spin_unlock_irqrestore(&bitmap->lock, flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100508 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700509 sb->events = cpu_to_le64(bitmap->mddev->events);
Neil Browna0da84f2008-06-28 08:31:22 +1000510 if (bitmap->mddev->events < bitmap->events_cleared) {
511 /* rocking back to read-only */
512 bitmap->events_cleared = bitmap->mddev->events;
513 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
514 }
NeilBrown43a70502009-12-14 12:49:55 +1100515 /* Just in case these have been changed via sysfs: */
516 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
517 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
NeilBrownea03aff2006-01-06 00:20:34 -0800518 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700519 write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700520}
521
522/* print out the bitmap file superblock */
523void bitmap_print_sb(struct bitmap *bitmap)
524{
525 bitmap_super_t *sb;
526
527 if (!bitmap || !bitmap->sb_page)
528 return;
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100529 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700530 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700531 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
532 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
533 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700534 *(__u32 *)(sb->uuid+0),
535 *(__u32 *)(sb->uuid+4),
536 *(__u32 *)(sb->uuid+8),
537 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700538 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700539 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700540 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700541 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700542 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
543 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
544 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
545 printk(KERN_DEBUG " sync size: %llu KB\n",
546 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700547 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800548 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700549}
550
551/* read the superblock from the bitmap file and initialize some bitmap fields */
552static int bitmap_read_sb(struct bitmap *bitmap)
553{
554 char *reason = NULL;
555 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700556 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700557 unsigned long long events;
558 int err = -EINVAL;
559
560 /* page 0 is the superblock, read it... */
NeilBrownf49d5e62007-01-26 00:57:03 -0800561 if (bitmap->file) {
562 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
563 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
564
565 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
566 } else {
NeilBrown42a04b52009-12-14 12:49:53 +1100567 bitmap->sb_page = read_sb_page(bitmap->mddev,
568 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100569 NULL,
570 0, sizeof(bitmap_super_t));
NeilBrowna654b9d82005-06-21 17:17:27 -0700571 }
NeilBrown32a76272005-06-21 17:17:14 -0700572 if (IS_ERR(bitmap->sb_page)) {
573 err = PTR_ERR(bitmap->sb_page);
574 bitmap->sb_page = NULL;
575 return err;
576 }
577
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100578 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700579
NeilBrown32a76272005-06-21 17:17:14 -0700580 chunksize = le32_to_cpu(sb->chunksize);
NeilBrown1b04be92009-12-14 12:49:53 +1100581 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
NeilBrown4b6d2872005-09-09 16:23:47 -0700582 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700583
584 /* verify that the bitmap-specific fields are valid */
585 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
586 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800587 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
588 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700589 reason = "unrecognized superblock version";
NeilBrown1187cf02009-03-31 14:27:02 +1100590 else if (chunksize < 512)
NeilBrown7dd5d342006-01-06 00:20:39 -0800591 reason = "bitmap chunksize too small";
NeilBrown32a76272005-06-21 17:17:14 -0700592 else if ((1 << ffz(~chunksize)) != chunksize)
593 reason = "bitmap chunksize not a power of 2";
NeilBrown1b04be92009-12-14 12:49:53 +1100594 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
NeilBrown7dd5d342006-01-06 00:20:39 -0800595 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700596 else if (write_behind > COUNTER_MAX)
597 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700598 if (reason) {
599 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
600 bmname(bitmap), reason);
601 goto out;
602 }
603
604 /* keep the array size field of the bitmap superblock up to date */
605 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
606
607 if (!bitmap->mddev->persistent)
608 goto success;
609
610 /*
611 * if we have a persistent array superblock, compare the
612 * bitmap's UUID and event counter to the mddev's
613 */
614 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
615 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
616 bmname(bitmap));
617 goto out;
618 }
619 events = le64_to_cpu(sb->events);
620 if (events < bitmap->mddev->events) {
621 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
622 "-- forcing full recovery\n", bmname(bitmap), events,
623 (unsigned long long) bitmap->mddev->events);
NeilBrown4f2e6392006-10-21 10:24:09 -0700624 sb->state |= cpu_to_le32(BITMAP_STALE);
NeilBrown32a76272005-06-21 17:17:14 -0700625 }
626success:
627 /* assign fields using values from superblock */
NeilBrown42a04b52009-12-14 12:49:53 +1100628 bitmap->mddev->bitmap_info.chunksize = chunksize;
629 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
NeilBrown42a04b52009-12-14 12:49:53 +1100630 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
NeilBrown4f2e6392006-10-21 10:24:09 -0700631 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800632 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
633 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700634 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown4f2e6392006-10-21 10:24:09 -0700635 if (sb->state & cpu_to_le32(BITMAP_STALE))
NeilBrown6a079972005-09-09 16:23:44 -0700636 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700637 err = 0;
638out:
NeilBrownea03aff2006-01-06 00:20:34 -0800639 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700640 if (err)
641 bitmap_print_sb(bitmap);
642 return err;
643}
644
645enum bitmap_mask_op {
646 MASK_SET,
647 MASK_UNSET
648};
649
NeilBrown4ad13662007-07-17 04:06:13 -0700650/* record the state of the bitmap in the superblock. Return the old value */
651static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
652 enum bitmap_mask_op op)
NeilBrown32a76272005-06-21 17:17:14 -0700653{
654 bitmap_super_t *sb;
655 unsigned long flags;
NeilBrown4ad13662007-07-17 04:06:13 -0700656 int old;
NeilBrown32a76272005-06-21 17:17:14 -0700657
658 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800659 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700660 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700661 return 0;
NeilBrown32a76272005-06-21 17:17:14 -0700662 }
NeilBrown32a76272005-06-21 17:17:14 -0700663 spin_unlock_irqrestore(&bitmap->lock, flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100664 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700665 old = le32_to_cpu(sb->state) & bits;
NeilBrown32a76272005-06-21 17:17:14 -0700666 switch (op) {
NeilBrown4f2e6392006-10-21 10:24:09 -0700667 case MASK_SET: sb->state |= cpu_to_le32(bits);
NeilBrown32a76272005-06-21 17:17:14 -0700668 break;
NeilBrown4f2e6392006-10-21 10:24:09 -0700669 case MASK_UNSET: sb->state &= cpu_to_le32(~bits);
NeilBrown32a76272005-06-21 17:17:14 -0700670 break;
671 default: BUG();
672 }
NeilBrownea03aff2006-01-06 00:20:34 -0800673 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700674 return old;
NeilBrown32a76272005-06-21 17:17:14 -0700675}
676
677/*
678 * general bitmap file operations
679 */
680
NeilBrownece5cff2009-12-14 12:49:56 +1100681/*
682 * on-disk bitmap:
683 *
684 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
685 * file a page at a time. There's a superblock at the start of the file.
686 */
NeilBrown32a76272005-06-21 17:17:14 -0700687/* calculate the index of the page that contains this bit */
NeilBrownece5cff2009-12-14 12:49:56 +1100688static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700689{
NeilBrownece5cff2009-12-14 12:49:56 +1100690 if (!bitmap->mddev->bitmap_info.external)
691 chunk += sizeof(bitmap_super_t) << 3;
692 return chunk >> PAGE_BIT_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700693}
694
695/* calculate the (bit) offset of this bit within a page */
NeilBrownece5cff2009-12-14 12:49:56 +1100696static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700697{
NeilBrownece5cff2009-12-14 12:49:56 +1100698 if (!bitmap->mddev->bitmap_info.external)
699 chunk += sizeof(bitmap_super_t) << 3;
700 return chunk & (PAGE_BITS - 1);
NeilBrown32a76272005-06-21 17:17:14 -0700701}
702
703/*
704 * return a pointer to the page in the filemap that contains the given bit
705 *
706 * this lookup is complicated by the fact that the bitmap sb might be exactly
707 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
708 * 0 or page 1
709 */
710static inline struct page *filemap_get_page(struct bitmap *bitmap,
711 unsigned long chunk)
712{
NeilBrownece5cff2009-12-14 12:49:56 +1100713 if (file_page_index(bitmap, chunk) >= bitmap->file_pages) return NULL;
714 return bitmap->filemap[file_page_index(bitmap, chunk)
715 - file_page_index(bitmap, 0)];
NeilBrown32a76272005-06-21 17:17:14 -0700716}
717
718
719static void bitmap_file_unmap(struct bitmap *bitmap)
720{
721 struct page **map, *sb_page;
722 unsigned long *attr;
723 int pages;
724 unsigned long flags;
725
726 spin_lock_irqsave(&bitmap->lock, flags);
727 map = bitmap->filemap;
728 bitmap->filemap = NULL;
729 attr = bitmap->filemap_attr;
730 bitmap->filemap_attr = NULL;
731 pages = bitmap->file_pages;
732 bitmap->file_pages = 0;
733 sb_page = bitmap->sb_page;
734 bitmap->sb_page = NULL;
735 spin_unlock_irqrestore(&bitmap->lock, flags);
736
737 while (pages--)
NeilBrownece5cff2009-12-14 12:49:56 +1100738 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700739 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700740 kfree(map);
741 kfree(attr);
742
NeilBrownd785a062006-06-26 00:27:48 -0700743 if (sb_page)
744 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700745}
746
747static void bitmap_file_put(struct bitmap *bitmap)
748{
749 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700750 unsigned long flags;
751
752 spin_lock_irqsave(&bitmap->lock, flags);
753 file = bitmap->file;
754 bitmap->file = NULL;
755 spin_unlock_irqrestore(&bitmap->lock, flags);
756
NeilBrownd785a062006-06-26 00:27:48 -0700757 if (file)
758 wait_event(bitmap->write_wait,
759 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700760 bitmap_file_unmap(bitmap);
761
NeilBrownd785a062006-06-26 00:27:48 -0700762 if (file) {
Josef Sipekc649bb92006-12-08 02:37:19 -0800763 struct inode *inode = file->f_path.dentry->d_inode;
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800764 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700765 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700766 }
NeilBrown32a76272005-06-21 17:17:14 -0700767}
768
769
770/*
771 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
772 * then it is no longer reliable, so we stop using it and we mark the file
773 * as failed in the superblock
774 */
775static void bitmap_file_kick(struct bitmap *bitmap)
776{
777 char *path, *ptr = NULL;
778
NeilBrown4ad13662007-07-17 04:06:13 -0700779 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
780 bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700781
NeilBrown4ad13662007-07-17 04:06:13 -0700782 if (bitmap->file) {
783 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
784 if (path)
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700785 ptr = d_path(&bitmap->file->f_path, path,
786 PAGE_SIZE);
787
NeilBrown32a76272005-06-21 17:17:14 -0700788
NeilBrown4ad13662007-07-17 04:06:13 -0700789 printk(KERN_ALERT
790 "%s: kicking failed bitmap file %s from array!\n",
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700791 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700792
NeilBrown4ad13662007-07-17 04:06:13 -0700793 kfree(path);
794 } else
795 printk(KERN_ALERT
796 "%s: disabling internal bitmap due to errors\n",
797 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700798 }
NeilBrown32a76272005-06-21 17:17:14 -0700799
800 bitmap_file_put(bitmap);
801
802 return;
803}
804
805enum bitmap_page_attr {
NeilBrowne16b68b2006-06-26 00:27:45 -0700806 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
807 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
808 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
NeilBrown32a76272005-06-21 17:17:14 -0700809};
810
811static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
812 enum bitmap_page_attr attr)
813{
NeilBrowne16b68b2006-06-26 00:27:45 -0700814 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700815}
816
817static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
818 enum bitmap_page_attr attr)
819{
NeilBrowne16b68b2006-06-26 00:27:45 -0700820 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700821}
822
NeilBrownec7a3192006-06-26 00:27:45 -0700823static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
824 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700825{
NeilBrowne16b68b2006-06-26 00:27:45 -0700826 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700827}
828
829/*
830 * bitmap_file_set_bit -- called before performing a write to the md device
831 * to set (and eventually sync) a particular bit in the bitmap file
832 *
833 * we set the bit immediately, then we record the page number so that
834 * when an unplug occurs, we can flush the dirty pages out to disk
835 */
836static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
837{
838 unsigned long bit;
839 struct page *page;
840 void *kaddr;
841 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
842
NeilBrowna654b9d82005-06-21 17:17:27 -0700843 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700844 return;
845 }
846
847 page = filemap_get_page(bitmap, chunk);
Paul Clements9b1d1da2006-10-03 01:15:49 -0700848 if (!page) return;
NeilBrownece5cff2009-12-14 12:49:56 +1100849 bit = file_page_offset(bitmap, chunk);
NeilBrown32a76272005-06-21 17:17:14 -0700850
NeilBrown32a76272005-06-21 17:17:14 -0700851 /* set the bit */
852 kaddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800853 if (bitmap->flags & BITMAP_HOSTENDIAN)
854 set_bit(bit, kaddr);
855 else
856 ext2_set_bit(bit, kaddr);
NeilBrown32a76272005-06-21 17:17:14 -0700857 kunmap_atomic(kaddr, KM_USER0);
858 PRINTK("set file bit %lu page %lu\n", bit, page->index);
859
860 /* record page number so it gets flushed to disk when unplug occurs */
861 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
862
863}
864
865/* this gets called when the md device is ready to unplug its underlying
866 * (slave) device queues -- before we let any writes go down, we need to
867 * sync the dirty pages of the bitmap file to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700868void bitmap_unplug(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700869{
NeilBrownec7a3192006-06-26 00:27:45 -0700870 unsigned long i, flags;
871 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700872 struct page *page;
873 int wait = 0;
874
875 if (!bitmap)
NeilBrown4ad13662007-07-17 04:06:13 -0700876 return;
NeilBrown32a76272005-06-21 17:17:14 -0700877
878 /* look at each page to see if there are any set bits that need to be
879 * flushed out to disk */
880 for (i = 0; i < bitmap->file_pages; i++) {
881 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700882 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700883 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700884 return;
NeilBrown32a76272005-06-21 17:17:14 -0700885 }
886 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700887 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
888 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700889 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
890 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700891 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700892 wait = 1;
893 spin_unlock_irqrestore(&bitmap->lock, flags);
894
NeilBrownd785a062006-06-26 00:27:48 -0700895 if (dirty | need_write)
NeilBrown4ad13662007-07-17 04:06:13 -0700896 write_page(bitmap, page, 0);
NeilBrown32a76272005-06-21 17:17:14 -0700897 }
898 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700899 if (bitmap->file)
NeilBrownd785a062006-06-26 00:27:48 -0700900 wait_event(bitmap->write_wait,
901 atomic_read(&bitmap->pending_writes)==0);
NeilBrown0b79ccf2006-06-26 00:27:44 -0700902 else
NeilBrowna9701a32005-11-08 21:39:34 -0800903 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700904 }
NeilBrownd785a062006-06-26 00:27:48 -0700905 if (bitmap->flags & BITMAP_WRITE_ERROR)
906 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700907}
908
NeilBrown6a079972005-09-09 16:23:44 -0700909static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700910/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
911 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
912 * memory mapping of the bitmap file
913 * Special cases:
914 * if there's no bitmap file, or if the bitmap file had been
915 * previously kicked from the array, we mark all the bits as
916 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700917 *
918 * We ignore all bits for sectors that end earlier than 'start'.
919 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700920 */
NeilBrown6a079972005-09-09 16:23:44 -0700921static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700922{
923 unsigned long i, chunks, index, oldindex, bit;
924 struct page *page = NULL, *oldpage = NULL;
925 unsigned long num_pages, bit_cnt = 0;
926 struct file *file;
NeilBrownd785a062006-06-26 00:27:48 -0700927 unsigned long bytes, offset;
NeilBrown32a76272005-06-21 17:17:14 -0700928 int outofdate;
929 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800930 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700931
932 chunks = bitmap->chunks;
933 file = bitmap->file;
934
NeilBrown42a04b52009-12-14 12:49:53 +1100935 BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
NeilBrown32a76272005-06-21 17:17:14 -0700936
Olaf Hering44456d32005-07-27 11:45:17 -0700937#ifdef INJECT_FAULTS_3
NeilBrown32a76272005-06-21 17:17:14 -0700938 outofdate = 1;
939#else
940 outofdate = bitmap->flags & BITMAP_STALE;
941#endif
942 if (outofdate)
943 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
944 "recovery\n", bmname(bitmap));
945
946 bytes = (chunks + 7) / 8;
NeilBrownece5cff2009-12-14 12:49:56 +1100947 if (!bitmap->mddev->bitmap_info.external)
948 bytes += sizeof(bitmap_super_t);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700949
NeilBrownece5cff2009-12-14 12:49:56 +1100950
951 num_pages = (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700952
NeilBrownece5cff2009-12-14 12:49:56 +1100953 if (file && i_size_read(file->f_mapping->host) < bytes) {
NeilBrown32a76272005-06-21 17:17:14 -0700954 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
955 bmname(bitmap),
956 (unsigned long) i_size_read(file->f_mapping->host),
NeilBrownece5cff2009-12-14 12:49:56 +1100957 bytes);
NeilBrown4ad13662007-07-17 04:06:13 -0700958 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700959 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700960
961 ret = -ENOMEM;
962
NeilBrown32a76272005-06-21 17:17:14 -0700963 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700964 if (!bitmap->filemap)
NeilBrown4ad13662007-07-17 04:06:13 -0700965 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700966
NeilBrowne16b68b2006-06-26 00:27:45 -0700967 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
968 bitmap->filemap_attr = kzalloc(
Neil Brown505fa2c2007-04-11 23:28:44 -0700969 roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
NeilBrowne16b68b2006-06-26 00:27:45 -0700970 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700971 if (!bitmap->filemap_attr)
NeilBrown4ad13662007-07-17 04:06:13 -0700972 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700973
NeilBrown32a76272005-06-21 17:17:14 -0700974 oldindex = ~0L;
975
976 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -0800977 int b;
NeilBrownece5cff2009-12-14 12:49:56 +1100978 index = file_page_index(bitmap, i);
979 bit = file_page_offset(bitmap, i);
NeilBrown32a76272005-06-21 17:17:14 -0700980 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -0700981 int count;
NeilBrown32a76272005-06-21 17:17:14 -0700982 /* unmap the old page, we're done with it */
NeilBrownd785a062006-06-26 00:27:48 -0700983 if (index == num_pages-1)
NeilBrownece5cff2009-12-14 12:49:56 +1100984 count = bytes - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -0700985 else
986 count = PAGE_SIZE;
NeilBrownece5cff2009-12-14 12:49:56 +1100987 if (index == 0 && bitmap->sb_page) {
NeilBrown32a76272005-06-21 17:17:14 -0700988 /*
989 * if we're here then the superblock page
990 * contains some bits (PAGE_SIZE != sizeof sb)
991 * we've already read it in, so just use it
992 */
993 page = bitmap->sb_page;
994 offset = sizeof(bitmap_super_t);
NeilBrown53845272009-01-09 08:31:05 +1100995 if (!file)
996 read_sb_page(bitmap->mddev,
NeilBrown42a04b52009-12-14 12:49:53 +1100997 bitmap->mddev->bitmap_info.offset,
NeilBrown53845272009-01-09 08:31:05 +1100998 page,
999 index, count);
NeilBrowna654b9d82005-06-21 17:17:27 -07001000 } else if (file) {
NeilBrownd785a062006-06-26 00:27:48 -07001001 page = read_page(file, index, bitmap, count);
NeilBrowna654b9d82005-06-21 17:17:27 -07001002 offset = 0;
1003 } else {
NeilBrown42a04b52009-12-14 12:49:53 +11001004 page = read_sb_page(bitmap->mddev,
1005 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +11001006 NULL,
1007 index, count);
NeilBrown32a76272005-06-21 17:17:14 -07001008 offset = 0;
1009 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001010 if (IS_ERR(page)) { /* read error */
1011 ret = PTR_ERR(page);
NeilBrown4ad13662007-07-17 04:06:13 -07001012 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -07001013 }
1014
NeilBrown32a76272005-06-21 17:17:14 -07001015 oldindex = index;
1016 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -07001017
NeilBrownb74fd282009-05-07 12:47:19 +10001018 bitmap->filemap[bitmap->file_pages++] = page;
1019 bitmap->last_page_size = count;
1020
NeilBrown32a76272005-06-21 17:17:14 -07001021 if (outofdate) {
1022 /*
1023 * if bitmap is out of date, dirty the
1024 * whole page and write it out
1025 */
NeilBrownea03aff2006-01-06 00:20:34 -08001026 paddr = kmap_atomic(page, KM_USER0);
1027 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -07001028 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -08001029 kunmap_atomic(paddr, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -07001030 write_page(bitmap, page, 1);
1031
1032 ret = -EIO;
NeilBrownb74fd282009-05-07 12:47:19 +10001033 if (bitmap->flags & BITMAP_WRITE_ERROR)
NeilBrown4ad13662007-07-17 04:06:13 -07001034 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001035 }
NeilBrown32a76272005-06-21 17:17:14 -07001036 }
NeilBrownea03aff2006-01-06 00:20:34 -08001037 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001038 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001039 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001040 else
NeilBrownea03aff2006-01-06 00:20:34 -08001041 b = ext2_test_bit(bit, paddr);
1042 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001043 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -07001044 /* if the disk bit is set, set the memory bit */
NeilBrowndb305e52009-05-07 12:49:06 +10001045 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1046 >= start);
1047 bitmap_set_memory_bits(bitmap,
1048 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1049 needed);
NeilBrown32a76272005-06-21 17:17:14 -07001050 bit_cnt++;
NeilBrown6a079972005-09-09 16:23:44 -07001051 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -07001052 }
NeilBrown32a76272005-06-21 17:17:14 -07001053 }
1054
1055 /* everything went OK */
1056 ret = 0;
1057 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1058
NeilBrown32a76272005-06-21 17:17:14 -07001059 if (bit_cnt) { /* Kick recovery if any bits were set */
1060 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1061 md_wakeup_thread(bitmap->mddev->thread);
1062 }
1063
NeilBrown32a76272005-06-21 17:17:14 -07001064 printk(KERN_INFO "%s: bitmap initialized from disk: "
NeilBrown4ad13662007-07-17 04:06:13 -07001065 "read %lu/%lu pages, set %lu bits\n",
1066 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
NeilBrown32a76272005-06-21 17:17:14 -07001067
NeilBrown4ad13662007-07-17 04:06:13 -07001068 return 0;
1069
1070 err:
1071 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1072 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001073 return ret;
1074}
1075
NeilBrowna654b9d82005-06-21 17:17:27 -07001076void bitmap_write_all(struct bitmap *bitmap)
1077{
1078 /* We don't actually write all bitmap blocks here,
1079 * just flag them as needing to be written
1080 */
NeilBrownec7a3192006-06-26 00:27:45 -07001081 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001082
NeilBrownec7a3192006-06-26 00:27:45 -07001083 for (i=0; i < bitmap->file_pages; i++)
1084 set_page_attr(bitmap, bitmap->filemap[i],
1085 BITMAP_PAGE_NEEDWRITE);
NeilBrowna654b9d82005-06-21 17:17:27 -07001086}
1087
NeilBrown32a76272005-06-21 17:17:14 -07001088
1089static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1090{
1091 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1092 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1093 bitmap->bp[page].count += inc;
1094/*
1095 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1096 (unsigned long long)offset, inc, bitmap->bp[page].count);
1097*/
1098 bitmap_checkfree(bitmap, page);
1099}
1100static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1101 sector_t offset, int *blocks,
1102 int create);
1103
1104/*
1105 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1106 * out to disk
1107 */
1108
NeilBrownaa5cbd12009-12-14 12:49:46 +11001109void bitmap_daemon_work(mddev_t *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001110{
NeilBrownaa5cbd12009-12-14 12:49:46 +11001111 struct bitmap *bitmap;
NeilBrownaa3163f2005-06-21 17:17:22 -07001112 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001113 unsigned long flags;
1114 struct page *page = NULL, *lastpage = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001115 int blocks;
NeilBrownea03aff2006-01-06 00:20:34 -08001116 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001117
NeilBrownaa5cbd12009-12-14 12:49:46 +11001118 /* Use a mutex to guard daemon_work against
1119 * bitmap_destroy.
1120 */
NeilBrownc3d97142009-12-14 12:49:52 +11001121 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrownaa5cbd12009-12-14 12:49:46 +11001122 bitmap = mddev->bitmap;
1123 if (bitmap == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001124 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown4ad13662007-07-17 04:06:13 -07001125 return;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001126 }
NeilBrown42a04b52009-12-14 12:49:53 +11001127 if (time_before(jiffies, bitmap->daemon_lastrun
NeilBrown1b04be92009-12-14 12:49:53 +11001128 + bitmap->mddev->bitmap_info.daemon_sleep))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001129 goto done;
1130
NeilBrown32a76272005-06-21 17:17:14 -07001131 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001132 if (bitmap->allclean) {
1133 bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001134 goto done;
NeilBrown8311c292008-03-04 14:29:30 -08001135 }
1136 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001137
NeilBrownbe512692009-05-26 09:41:17 +10001138 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001139 for (j = 0; j < bitmap->chunks; j++) {
1140 bitmap_counter_t *bmc;
NeilBrownbe512692009-05-26 09:41:17 +10001141 if (!bitmap->filemap)
NeilBrown32a76272005-06-21 17:17:14 -07001142 /* error or shutdown */
NeilBrown32a76272005-06-21 17:17:14 -07001143 break;
NeilBrown32a76272005-06-21 17:17:14 -07001144
1145 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001146
1147 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001148 /* skip this page unless it's marked as needing cleaning */
NeilBrownec7a3192006-06-26 00:27:45 -07001149 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1150 int need_write = test_page_attr(bitmap, page,
1151 BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001152 if (need_write)
NeilBrownaa3163f2005-06-21 17:17:22 -07001153 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001154
NeilBrownaa3163f2005-06-21 17:17:22 -07001155 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown8311c292008-03-04 14:29:30 -08001156 if (need_write) {
NeilBrown4ad13662007-07-17 04:06:13 -07001157 write_page(bitmap, page, 0);
NeilBrown8311c292008-03-04 14:29:30 -08001158 bitmap->allclean = 0;
1159 }
NeilBrownbe512692009-05-26 09:41:17 +10001160 spin_lock_irqsave(&bitmap->lock, flags);
1161 j |= (PAGE_BITS - 1);
NeilBrownaa3163f2005-06-21 17:17:22 -07001162 continue;
1163 }
1164
NeilBrown32a76272005-06-21 17:17:14 -07001165 /* grab the new page, sync and release the old */
NeilBrown32a76272005-06-21 17:17:14 -07001166 if (lastpage != NULL) {
NeilBrownec7a3192006-06-26 00:27:45 -07001167 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001168 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1169 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001170 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001171 } else {
1172 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1173 spin_unlock_irqrestore(&bitmap->lock, flags);
1174 }
NeilBrown32a76272005-06-21 17:17:14 -07001175 } else
1176 spin_unlock_irqrestore(&bitmap->lock, flags);
1177 lastpage = page;
Neil Browna0da84f2008-06-28 08:31:22 +10001178
1179 /* We are possibly going to clear some bits, so make
1180 * sure that events_cleared is up-to-date.
1181 */
NeilBrownece5cff2009-12-14 12:49:56 +11001182 if (bitmap->need_sync &&
1183 bitmap->mddev->bitmap_info.external == 0) {
Neil Browna0da84f2008-06-28 08:31:22 +10001184 bitmap_super_t *sb;
1185 bitmap->need_sync = 0;
1186 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1187 sb->events_cleared =
1188 cpu_to_le64(bitmap->events_cleared);
1189 kunmap_atomic(sb, KM_USER0);
1190 write_page(bitmap, bitmap->sb_page, 1);
1191 }
NeilBrown32a76272005-06-21 17:17:14 -07001192 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownece5cff2009-12-14 12:49:56 +11001193 if (!bitmap->need_sync)
1194 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -07001195 }
NeilBrowndb305e52009-05-07 12:49:06 +10001196 bmc = bitmap_get_counter(bitmap,
1197 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1198 &blocks, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001199 if (bmc) {
1200/*
1201 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1202*/
NeilBrown8311c292008-03-04 14:29:30 -08001203 if (*bmc)
1204 bitmap->allclean = 0;
1205
NeilBrown32a76272005-06-21 17:17:14 -07001206 if (*bmc == 2) {
1207 *bmc=1; /* maybe clear the bit next time */
1208 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrownece5cff2009-12-14 12:49:56 +11001209 } else if (*bmc == 1 && !bitmap->need_sync) {
NeilBrown32a76272005-06-21 17:17:14 -07001210 /* we can clear the bit */
1211 *bmc = 0;
NeilBrowndb305e52009-05-07 12:49:06 +10001212 bitmap_count_page(bitmap,
1213 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
NeilBrown32a76272005-06-21 17:17:14 -07001214 -1);
1215
1216 /* clear the bit */
NeilBrownea03aff2006-01-06 00:20:34 -08001217 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001218 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownece5cff2009-12-14 12:49:56 +11001219 clear_bit(file_page_offset(bitmap, j),
1220 paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001221 else
NeilBrownece5cff2009-12-14 12:49:56 +11001222 ext2_clear_bit(file_page_offset(bitmap, j),
1223 paddr);
NeilBrownea03aff2006-01-06 00:20:34 -08001224 kunmap_atomic(paddr, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -07001225 }
NeilBrownbe512692009-05-26 09:41:17 +10001226 } else
1227 j |= PAGE_COUNTER_MASK;
NeilBrown32a76272005-06-21 17:17:14 -07001228 }
NeilBrownbe512692009-05-26 09:41:17 +10001229 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001230
1231 /* now sync the final page */
1232 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001233 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001234 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001235 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1236 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001237 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001238 } else {
1239 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1240 spin_unlock_irqrestore(&bitmap->lock, flags);
1241 }
NeilBrown32a76272005-06-21 17:17:14 -07001242 }
1243
NeilBrown7be3dfe2008-03-10 11:43:48 -07001244 done:
NeilBrown8311c292008-03-04 14:29:30 -08001245 if (bitmap->allclean == 0)
NeilBrown42a04b52009-12-14 12:49:53 +11001246 bitmap->mddev->thread->timeout =
NeilBrown1b04be92009-12-14 12:49:53 +11001247 bitmap->mddev->bitmap_info.daemon_sleep;
NeilBrownc3d97142009-12-14 12:49:52 +11001248 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown32a76272005-06-21 17:17:14 -07001249}
1250
NeilBrown32a76272005-06-21 17:17:14 -07001251static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1252 sector_t offset, int *blocks,
1253 int create)
NeilBrownee305ac2009-09-23 18:06:44 +10001254__releases(bitmap->lock)
1255__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -07001256{
1257 /* If 'create', we might release the lock and reclaim it.
1258 * The lock must have been taken with interrupts enabled.
1259 * If !create, we don't release the lock.
1260 */
1261 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1262 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1263 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1264 sector_t csize;
1265
1266 if (bitmap_checkpage(bitmap, page, create) < 0) {
1267 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1268 *blocks = csize - (offset & (csize- 1));
1269 return NULL;
1270 }
1271 /* now locked ... */
1272
1273 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1274 /* should we use the first or second counter field
1275 * of the hijacked pointer? */
1276 int hi = (pageoff > PAGE_COUNTER_MASK);
1277 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1278 PAGE_COUNTER_SHIFT - 1);
1279 *blocks = csize - (offset & (csize- 1));
1280 return &((bitmap_counter_t *)
1281 &bitmap->bp[page].map)[hi];
1282 } else { /* page is allocated */
1283 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1284 *blocks = csize - (offset & (csize- 1));
1285 return (bitmap_counter_t *)
1286 &(bitmap->bp[page].map[pageoff]);
1287 }
1288}
1289
NeilBrown4b6d2872005-09-09 16:23:47 -07001290int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001291{
1292 if (!bitmap) return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001293
1294 if (behind) {
Paul Clements696fcd52010-03-08 16:02:37 +11001295 int bw;
NeilBrown4b6d2872005-09-09 16:23:47 -07001296 atomic_inc(&bitmap->behind_writes);
Paul Clements696fcd52010-03-08 16:02:37 +11001297 bw = atomic_read(&bitmap->behind_writes);
1298 if (bw > bitmap->behind_writes_used)
1299 bitmap->behind_writes_used = bw;
1300
NeilBrown4b6d2872005-09-09 16:23:47 -07001301 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
Paul Clements696fcd52010-03-08 16:02:37 +11001302 bw, bitmap->max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001303 }
1304
NeilBrown32a76272005-06-21 17:17:14 -07001305 while (sectors) {
1306 int blocks;
1307 bitmap_counter_t *bmc;
1308
1309 spin_lock_irq(&bitmap->lock);
1310 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1311 if (!bmc) {
1312 spin_unlock_irq(&bitmap->lock);
1313 return 0;
1314 }
1315
Neil Brownda6e1a32007-02-08 14:20:37 -08001316 if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
1317 DEFINE_WAIT(__wait);
1318 /* note that it is safe to do the prepare_to_wait
1319 * after the test as long as we do it before dropping
1320 * the spinlock.
1321 */
1322 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1323 TASK_UNINTERRUPTIBLE);
1324 spin_unlock_irq(&bitmap->lock);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05001325 blk_unplug(bitmap->mddev->queue);
Neil Brownda6e1a32007-02-08 14:20:37 -08001326 schedule();
1327 finish_wait(&bitmap->overflow_wait, &__wait);
1328 continue;
1329 }
1330
NeilBrown32a76272005-06-21 17:17:14 -07001331 switch(*bmc) {
1332 case 0:
1333 bitmap_file_set_bit(bitmap, offset);
1334 bitmap_count_page(bitmap,offset, 1);
Jens Axboe93769f52008-08-01 20:32:31 +02001335 blk_plug_device_unlocked(bitmap->mddev->queue);
NeilBrown32a76272005-06-21 17:17:14 -07001336 /* fall through */
1337 case 1:
1338 *bmc = 2;
1339 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001340
NeilBrown32a76272005-06-21 17:17:14 -07001341 (*bmc)++;
1342
1343 spin_unlock_irq(&bitmap->lock);
1344
1345 offset += blocks;
1346 if (sectors > blocks)
1347 sectors -= blocks;
1348 else sectors = 0;
1349 }
NeilBrown8311c292008-03-04 14:29:30 -08001350 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001351 return 0;
1352}
1353
1354void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001355 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001356{
1357 if (!bitmap) return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001358 if (behind) {
NeilBrowne5551902010-03-31 11:21:44 +11001359 if (atomic_dec_and_test(&bitmap->behind_writes))
1360 wake_up(&bitmap->behind_wait);
NeilBrown4b6d2872005-09-09 16:23:47 -07001361 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1362 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1363 }
NeilBrownd0a4bb42009-03-31 14:27:02 +11001364 if (bitmap->mddev->degraded)
1365 /* Never clear bits or update events_cleared when degraded */
1366 success = 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001367
NeilBrown32a76272005-06-21 17:17:14 -07001368 while (sectors) {
1369 int blocks;
1370 unsigned long flags;
1371 bitmap_counter_t *bmc;
1372
1373 spin_lock_irqsave(&bitmap->lock, flags);
1374 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1375 if (!bmc) {
1376 spin_unlock_irqrestore(&bitmap->lock, flags);
1377 return;
1378 }
1379
Neil Browna0da84f2008-06-28 08:31:22 +10001380 if (success &&
1381 bitmap->events_cleared < bitmap->mddev->events) {
1382 bitmap->events_cleared = bitmap->mddev->events;
1383 bitmap->need_sync = 1;
NeilBrownece5cff2009-12-14 12:49:56 +11001384 sysfs_notify_dirent(bitmap->sysfs_can_clear);
Neil Browna0da84f2008-06-28 08:31:22 +10001385 }
1386
NeilBrown32a76272005-06-21 17:17:14 -07001387 if (!success && ! (*bmc & NEEDED_MASK))
1388 *bmc |= NEEDED_MASK;
1389
Neil Brownda6e1a32007-02-08 14:20:37 -08001390 if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
1391 wake_up(&bitmap->overflow_wait);
1392
NeilBrown32a76272005-06-21 17:17:14 -07001393 (*bmc)--;
1394 if (*bmc <= 2) {
1395 set_page_attr(bitmap,
1396 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1397 BITMAP_PAGE_CLEAN);
1398 }
1399 spin_unlock_irqrestore(&bitmap->lock, flags);
1400 offset += blocks;
1401 if (sectors > blocks)
1402 sectors -= blocks;
1403 else sectors = 0;
1404 }
1405}
1406
NeilBrown1187cf02009-03-31 14:27:02 +11001407static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1408 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001409{
1410 bitmap_counter_t *bmc;
1411 int rv;
1412 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1413 *blocks = 1024;
1414 return 1; /* always resync if no bitmap */
1415 }
1416 spin_lock_irq(&bitmap->lock);
1417 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1418 rv = 0;
1419 if (bmc) {
1420 /* locked */
1421 if (RESYNC(*bmc))
1422 rv = 1;
1423 else if (NEEDED(*bmc)) {
1424 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001425 if (!degraded) { /* don't set/clear bits if degraded */
1426 *bmc |= RESYNC_MASK;
1427 *bmc &= ~NEEDED_MASK;
1428 }
NeilBrown32a76272005-06-21 17:17:14 -07001429 }
1430 }
1431 spin_unlock_irq(&bitmap->lock);
NeilBrown8311c292008-03-04 14:29:30 -08001432 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001433 return rv;
1434}
1435
NeilBrown1187cf02009-03-31 14:27:02 +11001436int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1437 int degraded)
1438{
1439 /* bitmap_start_sync must always report on multiples of whole
1440 * pages, otherwise resync (which is very PAGE_SIZE based) will
1441 * get confused.
1442 * So call __bitmap_start_sync repeatedly (if needed) until
1443 * At least PAGE_SIZE>>9 blocks are covered.
1444 * Return the 'or' of the result.
1445 */
1446 int rv = 0;
1447 int blocks1;
1448
1449 *blocks = 0;
1450 while (*blocks < (PAGE_SIZE>>9)) {
1451 rv |= __bitmap_start_sync(bitmap, offset,
1452 &blocks1, degraded);
1453 offset += blocks1;
1454 *blocks += blocks1;
1455 }
1456 return rv;
1457}
1458
NeilBrown32a76272005-06-21 17:17:14 -07001459void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1460{
1461 bitmap_counter_t *bmc;
1462 unsigned long flags;
1463/*
1464 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1465*/ if (bitmap == NULL) {
1466 *blocks = 1024;
1467 return;
1468 }
1469 spin_lock_irqsave(&bitmap->lock, flags);
1470 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1471 if (bmc == NULL)
1472 goto unlock;
1473 /* locked */
1474/*
1475 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1476*/
1477 if (RESYNC(*bmc)) {
1478 *bmc &= ~RESYNC_MASK;
1479
1480 if (!NEEDED(*bmc) && aborted)
1481 *bmc |= NEEDED_MASK;
1482 else {
1483 if (*bmc <= 2) {
1484 set_page_attr(bitmap,
1485 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1486 BITMAP_PAGE_CLEAN);
1487 }
1488 }
1489 }
1490 unlock:
1491 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown8311c292008-03-04 14:29:30 -08001492 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001493}
1494
1495void bitmap_close_sync(struct bitmap *bitmap)
1496{
1497 /* Sync has finished, and any bitmap chunks that weren't synced
1498 * properly have been aborted. It remains to us to clear the
1499 * RESYNC bit wherever it is still on
1500 */
1501 sector_t sector = 0;
1502 int blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001503 if (!bitmap)
1504 return;
NeilBrown32a76272005-06-21 17:17:14 -07001505 while (sector < bitmap->mddev->resync_max_sectors) {
1506 bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001507 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001508 }
1509}
1510
NeilBrownb47490c2008-02-06 01:39:50 -08001511void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1512{
1513 sector_t s = 0;
1514 int blocks;
1515
1516 if (!bitmap)
1517 return;
1518 if (sector == 0) {
1519 bitmap->last_end_sync = jiffies;
1520 return;
1521 }
1522 if (time_before(jiffies, (bitmap->last_end_sync
NeilBrown1b04be92009-12-14 12:49:53 +11001523 + bitmap->mddev->bitmap_info.daemon_sleep)))
NeilBrownb47490c2008-02-06 01:39:50 -08001524 return;
1525 wait_event(bitmap->mddev->recovery_wait,
1526 atomic_read(&bitmap->mddev->recovery_active) == 0);
1527
NeilBrown97e4f422009-03-31 14:33:13 +11001528 bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync;
1529 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
NeilBrownb47490c2008-02-06 01:39:50 -08001530 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1531 s = 0;
1532 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1533 bitmap_end_sync(bitmap, s, &blocks, 0);
1534 s += blocks;
1535 }
1536 bitmap->last_end_sync = jiffies;
NeilBrownacb180b2009-04-14 16:28:34 +10001537 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
NeilBrownb47490c2008-02-06 01:39:50 -08001538}
1539
NeilBrown6a079972005-09-09 16:23:44 -07001540static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001541{
1542 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001543 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001544 * be 0 at this point
1545 */
NeilBrown193f1c92005-08-04 12:53:33 -07001546
1547 int secs;
1548 bitmap_counter_t *bmc;
1549 spin_lock_irq(&bitmap->lock);
1550 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1551 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001552 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001553 return;
NeilBrown32a76272005-06-21 17:17:14 -07001554 }
NeilBrown193f1c92005-08-04 12:53:33 -07001555 if (! *bmc) {
1556 struct page *page;
NeilBrown6a079972005-09-09 16:23:44 -07001557 *bmc = 1 | (needed?NEEDED_MASK:0);
NeilBrown193f1c92005-08-04 12:53:33 -07001558 bitmap_count_page(bitmap, offset, 1);
1559 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1560 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1561 }
1562 spin_unlock_irq(&bitmap->lock);
NeilBrown8311c292008-03-04 14:29:30 -08001563 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001564}
1565
Paul Clements9b1d1da2006-10-03 01:15:49 -07001566/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1567void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1568{
1569 unsigned long chunk;
1570
1571 for (chunk = s; chunk <= e; chunk++) {
NeilBrowndb305e52009-05-07 12:49:06 +10001572 sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001573 bitmap_set_memory_bits(bitmap, sec, 1);
1574 bitmap_file_set_bit(bitmap, sec);
NeilBrownffa23322009-12-14 12:49:56 +11001575 if (sec < bitmap->mddev->recovery_cp)
1576 /* We are asserting that the array is dirty,
1577 * so move the recovery_cp address back so
1578 * that it is obvious that it is dirty
1579 */
1580 bitmap->mddev->recovery_cp = sec;
Paul Clements9b1d1da2006-10-03 01:15:49 -07001581 }
1582}
1583
NeilBrown32a76272005-06-21 17:17:14 -07001584/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001585 * flush out any pending updates
1586 */
1587void bitmap_flush(mddev_t *mddev)
1588{
1589 struct bitmap *bitmap = mddev->bitmap;
NeilBrown42a04b52009-12-14 12:49:53 +11001590 long sleep;
NeilBrown6b8b3e82005-08-04 12:53:35 -07001591
1592 if (!bitmap) /* there was no bitmap */
1593 return;
1594
1595 /* run the daemon_work three time to ensure everything is flushed
1596 * that can be
1597 */
NeilBrown1b04be92009-12-14 12:49:53 +11001598 sleep = mddev->bitmap_info.daemon_sleep * 2;
NeilBrown42a04b52009-12-14 12:49:53 +11001599 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001600 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001601 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001602 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001603 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001604 bitmap_daemon_work(mddev);
NeilBrown6b8b3e82005-08-04 12:53:35 -07001605 bitmap_update_sb(bitmap);
1606}
1607
1608/*
NeilBrown32a76272005-06-21 17:17:14 -07001609 * free memory that was allocated
1610 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001611static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001612{
1613 unsigned long k, pages;
1614 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001615
1616 if (!bitmap) /* there was no bitmap */
1617 return;
1618
NeilBrown32a76272005-06-21 17:17:14 -07001619 /* release the bitmap file and kill the daemon */
1620 bitmap_file_put(bitmap);
1621
1622 bp = bitmap->bp;
1623 pages = bitmap->pages;
1624
1625 /* free all allocated memory */
1626
NeilBrown32a76272005-06-21 17:17:14 -07001627 if (bp) /* deallocate the page memory */
1628 for (k = 0; k < pages; k++)
1629 if (bp[k].map && !bp[k].hijacked)
1630 kfree(bp[k].map);
1631 kfree(bp);
1632 kfree(bitmap);
1633}
NeilBrownaa5cbd12009-12-14 12:49:46 +11001634
NeilBrown3178b0d2005-09-09 16:23:50 -07001635void bitmap_destroy(mddev_t *mddev)
1636{
1637 struct bitmap *bitmap = mddev->bitmap;
1638
1639 if (!bitmap) /* there was no bitmap */
1640 return;
1641
NeilBrownc3d97142009-12-14 12:49:52 +11001642 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrown3178b0d2005-09-09 16:23:50 -07001643 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownc3d97142009-12-14 12:49:52 +11001644 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrownb15c2e52006-01-06 00:20:16 -08001645 if (mddev->thread)
1646 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001647
NeilBrownece5cff2009-12-14 12:49:56 +11001648 if (bitmap->sysfs_can_clear)
1649 sysfs_put(bitmap->sysfs_can_clear);
1650
NeilBrown3178b0d2005-09-09 16:23:50 -07001651 bitmap_free(bitmap);
1652}
NeilBrown32a76272005-06-21 17:17:14 -07001653
1654/*
1655 * initialize the bitmap structure
1656 * if this returns an error, bitmap_destroy must be called to do clean up
1657 */
1658int bitmap_create(mddev_t *mddev)
1659{
1660 struct bitmap *bitmap;
NeilBrown1f593902009-04-20 11:50:24 +10001661 sector_t blocks = mddev->resync_max_sectors;
NeilBrown32a76272005-06-21 17:17:14 -07001662 unsigned long chunks;
1663 unsigned long pages;
NeilBrownc3d97142009-12-14 12:49:52 +11001664 struct file *file = mddev->bitmap_info.file;
NeilBrown32a76272005-06-21 17:17:14 -07001665 int err;
NeilBrown6a079972005-09-09 16:23:44 -07001666 sector_t start;
NeilBrownece5cff2009-12-14 12:49:56 +11001667 struct sysfs_dirent *bm;
NeilBrown32a76272005-06-21 17:17:14 -07001668
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001669 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001670
NeilBrownc3d97142009-12-14 12:49:52 +11001671 if (!file && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001672 return 0;
1673
NeilBrownc3d97142009-12-14 12:49:52 +11001674 BUG_ON(file && mddev->bitmap_info.offset);
NeilBrowna654b9d82005-06-21 17:17:27 -07001675
NeilBrown9ffae0c2006-01-06 00:20:32 -08001676 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001677 if (!bitmap)
1678 return -ENOMEM;
1679
NeilBrown32a76272005-06-21 17:17:14 -07001680 spin_lock_init(&bitmap->lock);
NeilBrownce25c312006-06-26 00:27:49 -07001681 atomic_set(&bitmap->pending_writes, 0);
1682 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001683 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrowne5551902010-03-31 11:21:44 +11001684 init_waitqueue_head(&bitmap->behind_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001685
NeilBrown32a76272005-06-21 17:17:14 -07001686 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001687
NeilBrownece5cff2009-12-14 12:49:56 +11001688 bm = sysfs_get_dirent(mddev->kobj.sd, "bitmap");
1689 if (bm) {
1690 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, "can_clear");
1691 sysfs_put(bm);
1692 } else
1693 bitmap->sysfs_can_clear = NULL;
1694
NeilBrown32a76272005-06-21 17:17:14 -07001695 bitmap->file = file;
NeilBrownce25c312006-06-26 00:27:49 -07001696 if (file) {
1697 get_file(file);
NeilBrownae8fa282009-10-16 15:56:01 +11001698 /* As future accesses to this file will use bmap,
1699 * and bypass the page cache, we must sync the file
1700 * first.
1701 */
1702 vfs_fsync(file, file->f_dentry, 1);
NeilBrownce25c312006-06-26 00:27:49 -07001703 }
NeilBrown42a04b52009-12-14 12:49:53 +11001704 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
NeilBrownece5cff2009-12-14 12:49:56 +11001705 if (!mddev->bitmap_info.external)
1706 err = bitmap_read_sb(bitmap);
1707 else {
1708 err = 0;
1709 if (mddev->bitmap_info.chunksize == 0 ||
1710 mddev->bitmap_info.daemon_sleep == 0)
1711 /* chunksize and time_base need to be
1712 * set first. */
1713 err = -EINVAL;
1714 }
NeilBrown32a76272005-06-21 17:17:14 -07001715 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001716 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001717
NeilBrown624ce4f2009-12-14 12:49:56 +11001718 bitmap->daemon_lastrun = jiffies;
NeilBrown42a04b52009-12-14 12:49:53 +11001719 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
NeilBrown32a76272005-06-21 17:17:14 -07001720
1721 /* now that chunksize and chunkshift are set, we can use these macros */
NeilBrown1f593902009-04-20 11:50:24 +10001722 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
1723 CHUNK_BLOCK_SHIFT(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -07001724 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1725
1726 BUG_ON(!pages);
1727
1728 bitmap->chunks = chunks;
1729 bitmap->pages = pages;
1730 bitmap->missing_pages = pages;
1731 bitmap->counter_bits = COUNTER_BITS;
1732
1733 bitmap->syncchunk = ~0UL;
1734
Olaf Hering44456d32005-07-27 11:45:17 -07001735#ifdef INJECT_FATAL_FAULT_1
NeilBrown32a76272005-06-21 17:17:14 -07001736 bitmap->bp = NULL;
1737#else
NeilBrown9ffae0c2006-01-06 00:20:32 -08001738 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001739#endif
NeilBrown3178b0d2005-09-09 16:23:50 -07001740 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001741 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001742 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001743
NeilBrown32a76272005-06-21 17:17:14 -07001744 /* now that we have some pages available, initialize the in-memory
1745 * bitmap from the on-disk bitmap */
NeilBrown6a079972005-09-09 16:23:44 -07001746 start = 0;
1747 if (mddev->degraded == 0
1748 || bitmap->events_cleared == mddev->events)
1749 /* no need to keep dirty bits to optimise a re-add of a missing device */
1750 start = mddev->recovery_cp;
1751 err = bitmap_init_from_disk(bitmap, start);
NeilBrown193f1c92005-08-04 12:53:33 -07001752
NeilBrown32a76272005-06-21 17:17:14 -07001753 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001754 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001755
1756 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1757 pages, bmname(bitmap));
1758
NeilBrown3178b0d2005-09-09 16:23:50 -07001759 mddev->bitmap = bitmap;
1760
NeilBrown1b04be92009-12-14 12:49:53 +11001761 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
NeilBrown9cd30fd2009-12-14 12:49:54 +11001762 md_wakeup_thread(mddev->thread);
NeilBrownb15c2e52006-01-06 00:20:16 -08001763
NeilBrown4ad13662007-07-17 04:06:13 -07001764 bitmap_update_sb(bitmap);
1765
1766 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
NeilBrown3178b0d2005-09-09 16:23:50 -07001767
1768 error:
1769 bitmap_free(bitmap);
1770 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001771}
1772
NeilBrown43a70502009-12-14 12:49:55 +11001773static ssize_t
1774location_show(mddev_t *mddev, char *page)
1775{
1776 ssize_t len;
1777 if (mddev->bitmap_info.file) {
1778 len = sprintf(page, "file");
1779 } else if (mddev->bitmap_info.offset) {
1780 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
1781 } else
1782 len = sprintf(page, "none");
1783 len += sprintf(page+len, "\n");
1784 return len;
1785}
1786
1787static ssize_t
1788location_store(mddev_t *mddev, const char *buf, size_t len)
1789{
1790
1791 if (mddev->pers) {
1792 if (!mddev->pers->quiesce)
1793 return -EBUSY;
1794 if (mddev->recovery || mddev->sync_thread)
1795 return -EBUSY;
1796 }
1797
1798 if (mddev->bitmap || mddev->bitmap_info.file ||
1799 mddev->bitmap_info.offset) {
1800 /* bitmap already configured. Only option is to clear it */
1801 if (strncmp(buf, "none", 4) != 0)
1802 return -EBUSY;
1803 if (mddev->pers) {
1804 mddev->pers->quiesce(mddev, 1);
1805 bitmap_destroy(mddev);
1806 mddev->pers->quiesce(mddev, 0);
1807 }
1808 mddev->bitmap_info.offset = 0;
1809 if (mddev->bitmap_info.file) {
1810 struct file *f = mddev->bitmap_info.file;
1811 mddev->bitmap_info.file = NULL;
1812 restore_bitmap_write_access(f);
1813 fput(f);
1814 }
1815 } else {
1816 /* No bitmap, OK to set a location */
1817 long long offset;
1818 if (strncmp(buf, "none", 4) == 0)
1819 /* nothing to be done */;
1820 else if (strncmp(buf, "file:", 5) == 0) {
1821 /* Not supported yet */
1822 return -EINVAL;
1823 } else {
1824 int rv;
1825 if (buf[0] == '+')
1826 rv = strict_strtoll(buf+1, 10, &offset);
1827 else
1828 rv = strict_strtoll(buf, 10, &offset);
1829 if (rv)
1830 return rv;
1831 if (offset == 0)
1832 return -EINVAL;
NeilBrownece5cff2009-12-14 12:49:56 +11001833 if (mddev->bitmap_info.external == 0 &&
1834 mddev->major_version == 0 &&
NeilBrown43a70502009-12-14 12:49:55 +11001835 offset != mddev->bitmap_info.default_offset)
1836 return -EINVAL;
1837 mddev->bitmap_info.offset = offset;
1838 if (mddev->pers) {
1839 mddev->pers->quiesce(mddev, 1);
1840 rv = bitmap_create(mddev);
1841 if (rv) {
1842 bitmap_destroy(mddev);
1843 mddev->bitmap_info.offset = 0;
1844 }
1845 mddev->pers->quiesce(mddev, 0);
1846 if (rv)
1847 return rv;
1848 }
1849 }
1850 }
1851 if (!mddev->external) {
1852 /* Ensure new bitmap info is stored in
1853 * metadata promptly.
1854 */
1855 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1856 md_wakeup_thread(mddev->thread);
1857 }
1858 return len;
1859}
1860
1861static struct md_sysfs_entry bitmap_location =
1862__ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
1863
1864static ssize_t
1865timeout_show(mddev_t *mddev, char *page)
1866{
1867 ssize_t len;
1868 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
1869 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
1870
1871 len = sprintf(page, "%lu", secs);
1872 if (jifs)
1873 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
1874 len += sprintf(page+len, "\n");
1875 return len;
1876}
1877
1878static ssize_t
1879timeout_store(mddev_t *mddev, const char *buf, size_t len)
1880{
1881 /* timeout can be set at any time */
1882 unsigned long timeout;
1883 int rv = strict_strtoul_scaled(buf, &timeout, 4);
1884 if (rv)
1885 return rv;
1886
1887 /* just to make sure we don't overflow... */
1888 if (timeout >= LONG_MAX / HZ)
1889 return -EINVAL;
1890
1891 timeout = timeout * HZ / 10000;
1892
1893 if (timeout >= MAX_SCHEDULE_TIMEOUT)
1894 timeout = MAX_SCHEDULE_TIMEOUT-1;
1895 if (timeout < 1)
1896 timeout = 1;
1897 mddev->bitmap_info.daemon_sleep = timeout;
1898 if (mddev->thread) {
1899 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1900 * the bitmap is all clean and we don't need to
1901 * adjust the timeout right now
1902 */
1903 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
1904 mddev->thread->timeout = timeout;
1905 md_wakeup_thread(mddev->thread);
1906 }
1907 }
1908 return len;
1909}
1910
1911static struct md_sysfs_entry bitmap_timeout =
1912__ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
1913
1914static ssize_t
1915backlog_show(mddev_t *mddev, char *page)
1916{
1917 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
1918}
1919
1920static ssize_t
1921backlog_store(mddev_t *mddev, const char *buf, size_t len)
1922{
1923 unsigned long backlog;
1924 int rv = strict_strtoul(buf, 10, &backlog);
1925 if (rv)
1926 return rv;
1927 if (backlog > COUNTER_MAX)
1928 return -EINVAL;
1929 mddev->bitmap_info.max_write_behind = backlog;
1930 return len;
1931}
1932
1933static struct md_sysfs_entry bitmap_backlog =
1934__ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
1935
1936static ssize_t
1937chunksize_show(mddev_t *mddev, char *page)
1938{
1939 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
1940}
1941
1942static ssize_t
1943chunksize_store(mddev_t *mddev, const char *buf, size_t len)
1944{
1945 /* Can only be changed when no bitmap is active */
1946 int rv;
1947 unsigned long csize;
1948 if (mddev->bitmap)
1949 return -EBUSY;
1950 rv = strict_strtoul(buf, 10, &csize);
1951 if (rv)
1952 return rv;
1953 if (csize < 512 ||
1954 !is_power_of_2(csize))
1955 return -EINVAL;
1956 mddev->bitmap_info.chunksize = csize;
1957 return len;
1958}
1959
1960static struct md_sysfs_entry bitmap_chunksize =
1961__ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
1962
NeilBrownece5cff2009-12-14 12:49:56 +11001963static ssize_t metadata_show(mddev_t *mddev, char *page)
1964{
1965 return sprintf(page, "%s\n", (mddev->bitmap_info.external
1966 ? "external" : "internal"));
1967}
1968
1969static ssize_t metadata_store(mddev_t *mddev, const char *buf, size_t len)
1970{
1971 if (mddev->bitmap ||
1972 mddev->bitmap_info.file ||
1973 mddev->bitmap_info.offset)
1974 return -EBUSY;
1975 if (strncmp(buf, "external", 8) == 0)
1976 mddev->bitmap_info.external = 1;
1977 else if (strncmp(buf, "internal", 8) == 0)
1978 mddev->bitmap_info.external = 0;
1979 else
1980 return -EINVAL;
1981 return len;
1982}
1983
1984static struct md_sysfs_entry bitmap_metadata =
1985__ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
1986
1987static ssize_t can_clear_show(mddev_t *mddev, char *page)
1988{
1989 int len;
1990 if (mddev->bitmap)
1991 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
1992 "false" : "true"));
1993 else
1994 len = sprintf(page, "\n");
1995 return len;
1996}
1997
1998static ssize_t can_clear_store(mddev_t *mddev, const char *buf, size_t len)
1999{
2000 if (mddev->bitmap == NULL)
2001 return -ENOENT;
2002 if (strncmp(buf, "false", 5) == 0)
2003 mddev->bitmap->need_sync = 1;
2004 else if (strncmp(buf, "true", 4) == 0) {
2005 if (mddev->degraded)
2006 return -EBUSY;
2007 mddev->bitmap->need_sync = 0;
2008 } else
2009 return -EINVAL;
2010 return len;
2011}
2012
2013static struct md_sysfs_entry bitmap_can_clear =
2014__ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
2015
Paul Clements696fcd52010-03-08 16:02:37 +11002016static ssize_t
2017behind_writes_used_show(mddev_t *mddev, char *page)
2018{
2019 if (mddev->bitmap == NULL)
2020 return sprintf(page, "0\n");
2021 return sprintf(page, "%lu\n",
2022 mddev->bitmap->behind_writes_used);
2023}
2024
2025static ssize_t
2026behind_writes_used_reset(mddev_t *mddev, const char *buf, size_t len)
2027{
2028 if (mddev->bitmap)
2029 mddev->bitmap->behind_writes_used = 0;
2030 return len;
2031}
2032
2033static struct md_sysfs_entry max_backlog_used =
2034__ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2035 behind_writes_used_show, behind_writes_used_reset);
2036
NeilBrown43a70502009-12-14 12:49:55 +11002037static struct attribute *md_bitmap_attrs[] = {
2038 &bitmap_location.attr,
2039 &bitmap_timeout.attr,
2040 &bitmap_backlog.attr,
2041 &bitmap_chunksize.attr,
NeilBrownece5cff2009-12-14 12:49:56 +11002042 &bitmap_metadata.attr,
2043 &bitmap_can_clear.attr,
Paul Clements696fcd52010-03-08 16:02:37 +11002044 &max_backlog_used.attr,
NeilBrown43a70502009-12-14 12:49:55 +11002045 NULL
2046};
2047struct attribute_group md_bitmap_group = {
2048 .name = "bitmap",
2049 .attrs = md_bitmap_attrs,
2050};
2051
2052
NeilBrown32a76272005-06-21 17:17:14 -07002053/* the bitmap API -- for raid personalities */
2054EXPORT_SYMBOL(bitmap_startwrite);
2055EXPORT_SYMBOL(bitmap_endwrite);
2056EXPORT_SYMBOL(bitmap_start_sync);
2057EXPORT_SYMBOL(bitmap_end_sync);
2058EXPORT_SYMBOL(bitmap_unplug);
2059EXPORT_SYMBOL(bitmap_close_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08002060EXPORT_SYMBOL(bitmap_cond_end_sync);