blob: 3f04699725db4de742d55bde8342fb2b6e7523c8 [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).
NeilBrown32a76272005-06-21 17:17:14 -070016 */
17
NeilBrownbff61972009-03-31 14:33:13 +110018#include <linux/blkdev.h>
NeilBrown32a76272005-06-21 17:17:14 -070019#include <linux/module.h>
NeilBrown32a76272005-06-21 17:17:14 -070020#include <linux/errno.h>
21#include <linux/slab.h>
22#include <linux/init.h>
NeilBrown32a76272005-06-21 17:17:14 -070023#include <linux/timer.h>
24#include <linux/sched.h>
25#include <linux/list.h>
26#include <linux/file.h>
27#include <linux/mount.h>
28#include <linux/buffer_head.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110029#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110030#include "bitmap.h"
NeilBrown32a76272005-06-21 17:17:14 -070031
32/* debug macros */
33
34#define DEBUG 0
35
36#if DEBUG
37/* these are for debugging purposes only! */
38
39/* define one and only one of these */
40#define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
41#define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
42#define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
43#define INJECT_FAULTS_4 0 /* undef */
44#define INJECT_FAULTS_5 0 /* undef */
45#define INJECT_FAULTS_6 0
46
47/* if these are defined, the driver will fail! debug only */
48#define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
49#define INJECT_FATAL_FAULT_2 0 /* undef */
50#define INJECT_FATAL_FAULT_3 0 /* undef */
51#endif
52
NeilBrown32a76272005-06-21 17:17:14 -070053#ifndef PRINTK
54# if DEBUG > 0
55# define PRINTK(x...) printk(KERN_DEBUG x)
56# else
57# define PRINTK(x...)
58# endif
59#endif
60
NeilBrownac2f40b2010-06-01 19:37:31 +100061static inline char *bmname(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -070062{
63 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
64}
65
NeilBrown32a76272005-06-21 17:17:14 -070066/*
67 * just a placeholder - calls kmalloc for bitmap pages
68 */
69static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
70{
71 unsigned char *page;
72
Olaf Hering44456d32005-07-27 11:45:17 -070073#ifdef INJECT_FAULTS_1
NeilBrown32a76272005-06-21 17:17:14 -070074 page = NULL;
75#else
NeilBrownac2f40b2010-06-01 19:37:31 +100076 page = kzalloc(PAGE_SIZE, GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -070077#endif
78 if (!page)
79 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
80 else
NeilBrowna654b9d82005-06-21 17:17:27 -070081 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
NeilBrown32a76272005-06-21 17:17:14 -070082 bmname(bitmap), page);
83 return page;
84}
85
86/*
87 * for now just a placeholder -- just calls kfree for bitmap pages
88 */
89static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
90{
91 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
92 kfree(page);
93}
94
95/*
96 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
97 *
98 * 1) check to see if this page is allocated, if it's not then try to alloc
99 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
100 * page pointer directly as a counter
101 *
102 * if we find our page, we increment the page's refcount so that it stays
103 * allocated while we're using it
104 */
NeilBrownac2f40b2010-06-01 19:37:31 +1000105static int bitmap_checkpage(struct bitmap *bitmap,
106 unsigned long page, int create)
NeilBrownee305ac2009-09-23 18:06:44 +1000107__releases(bitmap->lock)
108__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -0700109{
110 unsigned char *mappage;
111
112 if (page >= bitmap->pages) {
NeilBrown1187cf02009-03-31 14:27:02 +1100113 /* This can happen if bitmap_start_sync goes beyond
114 * End-of-device while looking for a whole page.
115 * It is harmless.
116 */
NeilBrown32a76272005-06-21 17:17:14 -0700117 return -EINVAL;
118 }
119
NeilBrown32a76272005-06-21 17:17:14 -0700120 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
121 return 0;
122
123 if (bitmap->bp[page].map) /* page is already allocated, just return */
124 return 0;
125
126 if (!create)
127 return -ENOENT;
128
NeilBrown32a76272005-06-21 17:17:14 -0700129 /* this page has not been allocated yet */
130
NeilBrownac2f40b2010-06-01 19:37:31 +1000131 spin_unlock_irq(&bitmap->lock);
132 mappage = bitmap_alloc_page(bitmap);
133 spin_lock_irq(&bitmap->lock);
134
135 if (mappage == NULL) {
NeilBrown32a76272005-06-21 17:17:14 -0700136 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
137 bmname(bitmap));
138 /* failed - set the hijacked flag so that we can use the
139 * pointer as a counter */
NeilBrown32a76272005-06-21 17:17:14 -0700140 if (!bitmap->bp[page].map)
141 bitmap->bp[page].hijacked = 1;
NeilBrownac2f40b2010-06-01 19:37:31 +1000142 } else if (bitmap->bp[page].map ||
143 bitmap->bp[page].hijacked) {
NeilBrown32a76272005-06-21 17:17:14 -0700144 /* somebody beat us to getting the page */
145 bitmap_free_page(bitmap, mappage);
146 return 0;
NeilBrownac2f40b2010-06-01 19:37:31 +1000147 } else {
148
149 /* no page was in place and we have one, so install it */
150
151 bitmap->bp[page].map = mappage;
152 bitmap->missing_pages--;
NeilBrown32a76272005-06-21 17:17:14 -0700153 }
NeilBrown32a76272005-06-21 17:17:14 -0700154 return 0;
155}
156
NeilBrown32a76272005-06-21 17:17:14 -0700157/* if page is completely empty, put it back on the free list, or dealloc it */
158/* if page was hijacked, unmark the flag so it might get alloced next time */
159/* Note: lock should be held when calling this */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800160static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
NeilBrown32a76272005-06-21 17:17:14 -0700161{
162 char *ptr;
163
164 if (bitmap->bp[page].count) /* page is still busy */
165 return;
166
167 /* page is no longer in use, it can be released */
168
169 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
170 bitmap->bp[page].hijacked = 0;
171 bitmap->bp[page].map = NULL;
NeilBrownac2f40b2010-06-01 19:37:31 +1000172 } else {
173 /* normal case, free the page */
174 ptr = bitmap->bp[page].map;
175 bitmap->bp[page].map = NULL;
176 bitmap->missing_pages++;
177 bitmap_free_page(bitmap, ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700178 }
NeilBrown32a76272005-06-21 17:17:14 -0700179}
180
NeilBrown32a76272005-06-21 17:17:14 -0700181/*
182 * bitmap file handling - read and write the bitmap file and its superblock
183 */
184
NeilBrown32a76272005-06-21 17:17:14 -0700185/*
186 * basic page I/O operations
187 */
188
NeilBrowna654b9d82005-06-21 17:17:27 -0700189/* IO operations when bitmap is stored near all superblocks */
NeilBrownf6af9492009-12-14 12:49:54 +1100190static struct page *read_sb_page(mddev_t *mddev, loff_t offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100191 struct page *page,
192 unsigned long index, int size)
NeilBrowna654b9d82005-06-21 17:17:27 -0700193{
194 /* choose a good rdev and read the page from there */
195
196 mdk_rdev_t *rdev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700197 sector_t target;
NeilBrownac2f40b2010-06-01 19:37:31 +1000198 int did_alloc = 0;
NeilBrowna654b9d82005-06-21 17:17:27 -0700199
NeilBrownac2f40b2010-06-01 19:37:31 +1000200 if (!page) {
NeilBrowna2ed9612008-12-19 16:25:01 +1100201 page = alloc_page(GFP_KERNEL);
NeilBrownac2f40b2010-06-01 19:37:31 +1000202 if (!page)
203 return ERR_PTR(-ENOMEM);
204 did_alloc = 1;
205 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700206
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100207 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800208 if (! test_bit(In_sync, &rdev->flags)
209 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700210 continue;
211
Andre Noll0f420352008-07-11 22:02:23 +1000212 target = rdev->sb_start + offset + index * (PAGE_SIZE/512);
NeilBrowna654b9d82005-06-21 17:17:27 -0700213
NeilBrowna2ed9612008-12-19 16:25:01 +1100214 if (sync_page_io(rdev->bdev, target,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400215 roundup(size, bdev_logical_block_size(rdev->bdev)),
NeilBrowna2ed9612008-12-19 16:25:01 +1100216 page, READ)) {
NeilBrownab904d62005-09-09 16:23:52 -0700217 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700218 attach_page_buffers(page, NULL); /* so that free_buffer will
219 * quietly no-op */
NeilBrownab904d62005-09-09 16:23:52 -0700220 return page;
221 }
222 }
NeilBrownac2f40b2010-06-01 19:37:31 +1000223 if (did_alloc)
224 put_page(page);
NeilBrownab904d62005-09-09 16:23:52 -0700225 return ERR_PTR(-EIO);
NeilBrowna654b9d82005-06-21 17:17:27 -0700226
NeilBrowna654b9d82005-06-21 17:17:27 -0700227}
228
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000229static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
230{
231 /* Iterate the disks of an mddev, using rcu to protect access to the
232 * linked list, and raising the refcount of devices we return to ensure
233 * they don't disappear while in use.
234 * As devices are only added or removed when raid_disk is < 0 and
235 * nr_pending is 0 and In_sync is clear, the entries we return will
236 * still be in the same position on the list when we re-enter
237 * list_for_each_continue_rcu.
238 */
239 struct list_head *pos;
240 rcu_read_lock();
241 if (rdev == NULL)
242 /* start at the beginning */
243 pos = &mddev->disks;
244 else {
245 /* release the previous rdev and start from there. */
246 rdev_dec_pending(rdev, mddev);
247 pos = &rdev->same_set;
248 }
249 list_for_each_continue_rcu(pos, &mddev->disks) {
250 rdev = list_entry(pos, mdk_rdev_t, same_set);
251 if (rdev->raid_disk >= 0 &&
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000252 !test_bit(Faulty, &rdev->flags)) {
253 /* this is a usable devices */
254 atomic_inc(&rdev->nr_pending);
255 rcu_read_unlock();
256 return rdev;
257 }
258 }
259 rcu_read_unlock();
260 return NULL;
261}
262
NeilBrownab6085c2007-05-23 13:58:10 -0700263static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrowna654b9d82005-06-21 17:17:27 -0700264{
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000265 mdk_rdev_t *rdev = NULL;
NeilBrownab6085c2007-05-23 13:58:10 -0700266 mddev_t *mddev = bitmap->mddev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700267
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000268 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000269 int size = PAGE_SIZE;
270 loff_t offset = mddev->bitmap_info.offset;
271 if (page->index == bitmap->file_pages-1)
272 size = roundup(bitmap->last_page_size,
273 bdev_logical_block_size(rdev->bdev));
274 /* Just make sure we aren't corrupting data or
275 * metadata
276 */
277 if (mddev->external) {
278 /* Bitmap could be anywhere. */
279 if (rdev->sb_start + offset + (page->index
280 * (PAGE_SIZE/512))
281 > rdev->data_offset
282 &&
283 rdev->sb_start + offset
284 < (rdev->data_offset + mddev->dev_sectors
285 + (PAGE_SIZE/512)))
286 goto bad_alignment;
287 } else if (offset < 0) {
288 /* DATA BITMAP METADATA */
289 if (offset
290 + (long)(page->index * (PAGE_SIZE/512))
291 + size/512 > 0)
292 /* bitmap runs in to metadata */
293 goto bad_alignment;
294 if (rdev->data_offset + mddev->dev_sectors
295 > rdev->sb_start + offset)
296 /* data runs in to bitmap */
297 goto bad_alignment;
298 } else if (rdev->sb_start < rdev->data_offset) {
299 /* METADATA BITMAP DATA */
300 if (rdev->sb_start
301 + offset
302 + page->index*(PAGE_SIZE/512) + size/512
303 > rdev->data_offset)
304 /* bitmap runs in to data */
305 goto bad_alignment;
306 } else {
307 /* DATA METADATA BITMAP - no problems */
308 }
309 md_super_write(mddev, rdev,
310 rdev->sb_start + offset
311 + page->index * (PAGE_SIZE/512),
312 size,
313 page);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000314 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700315
316 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800317 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700318 return 0;
NeilBrown4b809912008-07-21 17:05:25 +1000319
320 bad_alignment:
NeilBrown4b809912008-07-21 17:05:25 +1000321 return -EINVAL;
NeilBrowna654b9d82005-06-21 17:17:27 -0700322}
323
NeilBrown4ad13662007-07-17 04:06:13 -0700324static void bitmap_file_kick(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700325/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700326 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700327 */
NeilBrown4ad13662007-07-17 04:06:13 -0700328static void write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700329{
NeilBrownd785a062006-06-26 00:27:48 -0700330 struct buffer_head *bh;
NeilBrown32a76272005-06-21 17:17:14 -0700331
NeilBrownf0d76d72007-07-17 04:06:12 -0700332 if (bitmap->file == NULL) {
333 switch (write_sb_page(bitmap, page, wait)) {
334 case -EINVAL:
335 bitmap->flags |= BITMAP_WRITE_ERROR;
NeilBrownf0d76d72007-07-17 04:06:12 -0700336 }
NeilBrown4ad13662007-07-17 04:06:13 -0700337 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700338
NeilBrown4ad13662007-07-17 04:06:13 -0700339 bh = page_buffers(page);
NeilBrownc7084432006-01-06 00:20:45 -0800340
NeilBrown4ad13662007-07-17 04:06:13 -0700341 while (bh && bh->b_blocknr) {
342 atomic_inc(&bitmap->pending_writes);
343 set_buffer_locked(bh);
344 set_buffer_mapped(bh);
345 submit_bh(WRITE, bh);
346 bh = bh->b_this_page;
347 }
NeilBrown32a76272005-06-21 17:17:14 -0700348
NeilBrownac2f40b2010-06-01 19:37:31 +1000349 if (wait)
NeilBrown4ad13662007-07-17 04:06:13 -0700350 wait_event(bitmap->write_wait,
351 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700352 }
NeilBrown4ad13662007-07-17 04:06:13 -0700353 if (bitmap->flags & BITMAP_WRITE_ERROR)
354 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700355}
356
NeilBrownd785a062006-06-26 00:27:48 -0700357static void end_bitmap_write(struct buffer_head *bh, int uptodate)
NeilBrown32a76272005-06-21 17:17:14 -0700358{
NeilBrownd785a062006-06-26 00:27:48 -0700359 struct bitmap *bitmap = bh->b_private;
360 unsigned long flags;
361
362 if (!uptodate) {
363 spin_lock_irqsave(&bitmap->lock, flags);
364 bitmap->flags |= BITMAP_WRITE_ERROR;
365 spin_unlock_irqrestore(&bitmap->lock, flags);
366 }
367 if (atomic_dec_and_test(&bitmap->pending_writes))
368 wake_up(&bitmap->write_wait);
369}
370
371/* copied from buffer.c */
372static void
373__clear_page_buffers(struct page *page)
374{
375 ClearPagePrivate(page);
376 set_page_private(page, 0);
377 page_cache_release(page);
378}
379static void free_buffers(struct page *page)
380{
381 struct buffer_head *bh = page_buffers(page);
382
383 while (bh) {
384 struct buffer_head *next = bh->b_this_page;
385 free_buffer_head(bh);
386 bh = next;
387 }
388 __clear_page_buffers(page);
389 put_page(page);
390}
391
392/* read a page from a file.
393 * We both read the page, and attach buffers to the page to record the
394 * address of each block (using bmap). These addresses will be used
395 * to write the block later, completely bypassing the filesystem.
396 * This usage is similar to how swap files are handled, and allows us
397 * to write to a file with no concerns of memory allocation failing.
398 */
399static struct page *read_page(struct file *file, unsigned long index,
400 struct bitmap *bitmap,
401 unsigned long count)
402{
NeilBrown32a76272005-06-21 17:17:14 -0700403 struct page *page = NULL;
Josef Sipekc649bb92006-12-08 02:37:19 -0800404 struct inode *inode = file->f_path.dentry->d_inode;
NeilBrownd785a062006-06-26 00:27:48 -0700405 struct buffer_head *bh;
406 sector_t block;
NeilBrown32a76272005-06-21 17:17:14 -0700407
NeilBrownac2f40b2010-06-01 19:37:31 +1000408 PRINTK("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
NeilBrown2d1f3b52006-01-06 00:20:31 -0800409 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700410
NeilBrownd785a062006-06-26 00:27:48 -0700411 page = alloc_page(GFP_KERNEL);
412 if (!page)
413 page = ERR_PTR(-ENOMEM);
NeilBrown32a76272005-06-21 17:17:14 -0700414 if (IS_ERR(page))
415 goto out;
NeilBrownd785a062006-06-26 00:27:48 -0700416
NeilBrownd785a062006-06-26 00:27:48 -0700417 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
418 if (!bh) {
419 put_page(page);
420 page = ERR_PTR(-ENOMEM);
421 goto out;
422 }
423 attach_page_buffers(page, bh);
424 block = index << (PAGE_SHIFT - inode->i_blkbits);
425 while (bh) {
426 if (count == 0)
427 bh->b_blocknr = 0;
428 else {
429 bh->b_blocknr = bmap(inode, block);
430 if (bh->b_blocknr == 0) {
431 /* Cannot use this file! */
432 free_buffers(page);
433 page = ERR_PTR(-EINVAL);
434 goto out;
435 }
436 bh->b_bdev = inode->i_sb->s_bdev;
437 if (count < (1<<inode->i_blkbits))
438 count = 0;
439 else
440 count -= (1<<inode->i_blkbits);
NeilBrown32a76272005-06-21 17:17:14 -0700441
NeilBrownd785a062006-06-26 00:27:48 -0700442 bh->b_end_io = end_bitmap_write;
443 bh->b_private = bitmap;
NeilBrownce25c312006-06-26 00:27:49 -0700444 atomic_inc(&bitmap->pending_writes);
445 set_buffer_locked(bh);
446 set_buffer_mapped(bh);
447 submit_bh(READ, bh);
NeilBrownd785a062006-06-26 00:27:48 -0700448 }
449 block++;
450 bh = bh->b_this_page;
451 }
NeilBrownd785a062006-06-26 00:27:48 -0700452 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700453
454 wait_event(bitmap->write_wait,
455 atomic_read(&bitmap->pending_writes)==0);
456 if (bitmap->flags & BITMAP_WRITE_ERROR) {
457 free_buffers(page);
458 page = ERR_PTR(-EIO);
459 }
NeilBrown32a76272005-06-21 17:17:14 -0700460out:
461 if (IS_ERR(page))
NeilBrownac2f40b2010-06-01 19:37:31 +1000462 printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800463 (int)PAGE_SIZE,
464 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700465 PTR_ERR(page));
466 return page;
467}
468
469/*
470 * bitmap file superblock operations
471 */
472
473/* update the event counter and sync the superblock to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700474void bitmap_update_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700475{
476 bitmap_super_t *sb;
477 unsigned long flags;
478
479 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
NeilBrown4ad13662007-07-17 04:06:13 -0700480 return;
NeilBrownece5cff2009-12-14 12:49:56 +1100481 if (bitmap->mddev->bitmap_info.external)
482 return;
NeilBrown32a76272005-06-21 17:17:14 -0700483 spin_lock_irqsave(&bitmap->lock, flags);
484 if (!bitmap->sb_page) { /* no superblock */
485 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700486 return;
NeilBrown32a76272005-06-21 17:17:14 -0700487 }
NeilBrown32a76272005-06-21 17:17:14 -0700488 spin_unlock_irqrestore(&bitmap->lock, flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100489 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700490 sb->events = cpu_to_le64(bitmap->mddev->events);
Neil Browna0da84f2008-06-28 08:31:22 +1000491 if (bitmap->mddev->events < bitmap->events_cleared) {
492 /* rocking back to read-only */
493 bitmap->events_cleared = bitmap->mddev->events;
494 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
495 }
NeilBrown43a70502009-12-14 12:49:55 +1100496 /* Just in case these have been changed via sysfs: */
497 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
498 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
NeilBrownea03aff2006-01-06 00:20:34 -0800499 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700500 write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700501}
502
503/* print out the bitmap file superblock */
504void bitmap_print_sb(struct bitmap *bitmap)
505{
506 bitmap_super_t *sb;
507
508 if (!bitmap || !bitmap->sb_page)
509 return;
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100510 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700511 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700512 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
513 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
514 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700515 *(__u32 *)(sb->uuid+0),
516 *(__u32 *)(sb->uuid+4),
517 *(__u32 *)(sb->uuid+8),
518 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700519 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700520 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700521 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700522 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700523 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
524 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
525 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
526 printk(KERN_DEBUG " sync size: %llu KB\n",
527 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700528 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800529 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700530}
531
532/* read the superblock from the bitmap file and initialize some bitmap fields */
533static int bitmap_read_sb(struct bitmap *bitmap)
534{
535 char *reason = NULL;
536 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700537 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700538 unsigned long long events;
539 int err = -EINVAL;
540
541 /* page 0 is the superblock, read it... */
NeilBrownf49d5e62007-01-26 00:57:03 -0800542 if (bitmap->file) {
543 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
544 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
545
546 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
547 } else {
NeilBrown42a04b52009-12-14 12:49:53 +1100548 bitmap->sb_page = read_sb_page(bitmap->mddev,
549 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100550 NULL,
551 0, sizeof(bitmap_super_t));
NeilBrowna654b9d82005-06-21 17:17:27 -0700552 }
NeilBrown32a76272005-06-21 17:17:14 -0700553 if (IS_ERR(bitmap->sb_page)) {
554 err = PTR_ERR(bitmap->sb_page);
555 bitmap->sb_page = NULL;
556 return err;
557 }
558
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100559 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700560
NeilBrown32a76272005-06-21 17:17:14 -0700561 chunksize = le32_to_cpu(sb->chunksize);
NeilBrown1b04be92009-12-14 12:49:53 +1100562 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
NeilBrown4b6d2872005-09-09 16:23:47 -0700563 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700564
565 /* verify that the bitmap-specific fields are valid */
566 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
567 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800568 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
569 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700570 reason = "unrecognized superblock version";
NeilBrown1187cf02009-03-31 14:27:02 +1100571 else if (chunksize < 512)
NeilBrown7dd5d342006-01-06 00:20:39 -0800572 reason = "bitmap chunksize too small";
NeilBrown32a76272005-06-21 17:17:14 -0700573 else if ((1 << ffz(~chunksize)) != chunksize)
574 reason = "bitmap chunksize not a power of 2";
NeilBrown1b04be92009-12-14 12:49:53 +1100575 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
NeilBrown7dd5d342006-01-06 00:20:39 -0800576 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700577 else if (write_behind > COUNTER_MAX)
578 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700579 if (reason) {
580 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
581 bmname(bitmap), reason);
582 goto out;
583 }
584
585 /* keep the array size field of the bitmap superblock up to date */
586 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
587
588 if (!bitmap->mddev->persistent)
589 goto success;
590
591 /*
592 * if we have a persistent array superblock, compare the
593 * bitmap's UUID and event counter to the mddev's
594 */
595 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
596 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
597 bmname(bitmap));
598 goto out;
599 }
600 events = le64_to_cpu(sb->events);
601 if (events < bitmap->mddev->events) {
602 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
603 "-- forcing full recovery\n", bmname(bitmap), events,
604 (unsigned long long) bitmap->mddev->events);
NeilBrown4f2e6392006-10-21 10:24:09 -0700605 sb->state |= cpu_to_le32(BITMAP_STALE);
NeilBrown32a76272005-06-21 17:17:14 -0700606 }
607success:
608 /* assign fields using values from superblock */
NeilBrown42a04b52009-12-14 12:49:53 +1100609 bitmap->mddev->bitmap_info.chunksize = chunksize;
610 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
NeilBrown42a04b52009-12-14 12:49:53 +1100611 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
NeilBrown4f2e6392006-10-21 10:24:09 -0700612 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800613 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
614 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700615 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown4f2e6392006-10-21 10:24:09 -0700616 if (sb->state & cpu_to_le32(BITMAP_STALE))
NeilBrown6a079972005-09-09 16:23:44 -0700617 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700618 err = 0;
619out:
NeilBrownea03aff2006-01-06 00:20:34 -0800620 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700621 if (err)
622 bitmap_print_sb(bitmap);
623 return err;
624}
625
626enum bitmap_mask_op {
627 MASK_SET,
628 MASK_UNSET
629};
630
NeilBrown4ad13662007-07-17 04:06:13 -0700631/* record the state of the bitmap in the superblock. Return the old value */
632static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
633 enum bitmap_mask_op op)
NeilBrown32a76272005-06-21 17:17:14 -0700634{
635 bitmap_super_t *sb;
636 unsigned long flags;
NeilBrown4ad13662007-07-17 04:06:13 -0700637 int old;
NeilBrown32a76272005-06-21 17:17:14 -0700638
639 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800640 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700641 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700642 return 0;
NeilBrown32a76272005-06-21 17:17:14 -0700643 }
NeilBrown32a76272005-06-21 17:17:14 -0700644 spin_unlock_irqrestore(&bitmap->lock, flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100645 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700646 old = le32_to_cpu(sb->state) & bits;
NeilBrown32a76272005-06-21 17:17:14 -0700647 switch (op) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000648 case MASK_SET:
649 sb->state |= cpu_to_le32(bits);
650 break;
651 case MASK_UNSET:
652 sb->state &= cpu_to_le32(~bits);
653 break;
654 default:
655 BUG();
NeilBrown32a76272005-06-21 17:17:14 -0700656 }
NeilBrownea03aff2006-01-06 00:20:34 -0800657 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700658 return old;
NeilBrown32a76272005-06-21 17:17:14 -0700659}
660
661/*
662 * general bitmap file operations
663 */
664
NeilBrownece5cff2009-12-14 12:49:56 +1100665/*
666 * on-disk bitmap:
667 *
668 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
669 * file a page at a time. There's a superblock at the start of the file.
670 */
NeilBrown32a76272005-06-21 17:17:14 -0700671/* calculate the index of the page that contains this bit */
NeilBrownece5cff2009-12-14 12:49:56 +1100672static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700673{
NeilBrownece5cff2009-12-14 12:49:56 +1100674 if (!bitmap->mddev->bitmap_info.external)
675 chunk += sizeof(bitmap_super_t) << 3;
676 return chunk >> PAGE_BIT_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700677}
678
679/* calculate the (bit) offset of this bit within a page */
NeilBrownece5cff2009-12-14 12:49:56 +1100680static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700681{
NeilBrownece5cff2009-12-14 12:49:56 +1100682 if (!bitmap->mddev->bitmap_info.external)
683 chunk += sizeof(bitmap_super_t) << 3;
684 return chunk & (PAGE_BITS - 1);
NeilBrown32a76272005-06-21 17:17:14 -0700685}
686
687/*
688 * return a pointer to the page in the filemap that contains the given bit
689 *
690 * this lookup is complicated by the fact that the bitmap sb might be exactly
691 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
692 * 0 or page 1
693 */
694static inline struct page *filemap_get_page(struct bitmap *bitmap,
695 unsigned long chunk)
696{
NeilBrownac2f40b2010-06-01 19:37:31 +1000697 if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
698 return NULL;
NeilBrownece5cff2009-12-14 12:49:56 +1100699 return bitmap->filemap[file_page_index(bitmap, chunk)
700 - file_page_index(bitmap, 0)];
NeilBrown32a76272005-06-21 17:17:14 -0700701}
702
NeilBrown32a76272005-06-21 17:17:14 -0700703static void bitmap_file_unmap(struct bitmap *bitmap)
704{
705 struct page **map, *sb_page;
706 unsigned long *attr;
707 int pages;
708 unsigned long flags;
709
710 spin_lock_irqsave(&bitmap->lock, flags);
711 map = bitmap->filemap;
712 bitmap->filemap = NULL;
713 attr = bitmap->filemap_attr;
714 bitmap->filemap_attr = NULL;
715 pages = bitmap->file_pages;
716 bitmap->file_pages = 0;
717 sb_page = bitmap->sb_page;
718 bitmap->sb_page = NULL;
719 spin_unlock_irqrestore(&bitmap->lock, flags);
720
721 while (pages--)
NeilBrownece5cff2009-12-14 12:49:56 +1100722 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700723 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700724 kfree(map);
725 kfree(attr);
726
NeilBrownd785a062006-06-26 00:27:48 -0700727 if (sb_page)
728 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700729}
730
731static void bitmap_file_put(struct bitmap *bitmap)
732{
733 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700734 unsigned long flags;
735
736 spin_lock_irqsave(&bitmap->lock, flags);
737 file = bitmap->file;
738 bitmap->file = NULL;
739 spin_unlock_irqrestore(&bitmap->lock, flags);
740
NeilBrownd785a062006-06-26 00:27:48 -0700741 if (file)
742 wait_event(bitmap->write_wait,
743 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700744 bitmap_file_unmap(bitmap);
745
NeilBrownd785a062006-06-26 00:27:48 -0700746 if (file) {
Josef Sipekc649bb92006-12-08 02:37:19 -0800747 struct inode *inode = file->f_path.dentry->d_inode;
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800748 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700749 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700750 }
NeilBrown32a76272005-06-21 17:17:14 -0700751}
752
NeilBrown32a76272005-06-21 17:17:14 -0700753/*
754 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
755 * then it is no longer reliable, so we stop using it and we mark the file
756 * as failed in the superblock
757 */
758static void bitmap_file_kick(struct bitmap *bitmap)
759{
760 char *path, *ptr = NULL;
761
NeilBrown4ad13662007-07-17 04:06:13 -0700762 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
763 bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700764
NeilBrown4ad13662007-07-17 04:06:13 -0700765 if (bitmap->file) {
766 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
767 if (path)
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700768 ptr = d_path(&bitmap->file->f_path, path,
769 PAGE_SIZE);
770
NeilBrown4ad13662007-07-17 04:06:13 -0700771 printk(KERN_ALERT
772 "%s: kicking failed bitmap file %s from array!\n",
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700773 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700774
NeilBrown4ad13662007-07-17 04:06:13 -0700775 kfree(path);
776 } else
777 printk(KERN_ALERT
778 "%s: disabling internal bitmap due to errors\n",
779 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700780 }
NeilBrown32a76272005-06-21 17:17:14 -0700781
782 bitmap_file_put(bitmap);
783
784 return;
785}
786
787enum bitmap_page_attr {
NeilBrownac2f40b2010-06-01 19:37:31 +1000788 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
789 BITMAP_PAGE_CLEAN = 1, /* there are bits that might need to be cleared */
790 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
NeilBrown32a76272005-06-21 17:17:14 -0700791};
792
793static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
794 enum bitmap_page_attr attr)
795{
NeilBrowne16b68b2006-06-26 00:27:45 -0700796 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700797}
798
799static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
800 enum bitmap_page_attr attr)
801{
NeilBrowne16b68b2006-06-26 00:27:45 -0700802 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700803}
804
NeilBrownec7a3192006-06-26 00:27:45 -0700805static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
806 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700807{
NeilBrowne16b68b2006-06-26 00:27:45 -0700808 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700809}
810
811/*
812 * bitmap_file_set_bit -- called before performing a write to the md device
813 * to set (and eventually sync) a particular bit in the bitmap file
814 *
815 * we set the bit immediately, then we record the page number so that
816 * when an unplug occurs, we can flush the dirty pages out to disk
817 */
818static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
819{
820 unsigned long bit;
821 struct page *page;
822 void *kaddr;
823 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
824
NeilBrownac2f40b2010-06-01 19:37:31 +1000825 if (!bitmap->filemap)
NeilBrown32a76272005-06-21 17:17:14 -0700826 return;
NeilBrown32a76272005-06-21 17:17:14 -0700827
828 page = filemap_get_page(bitmap, chunk);
NeilBrownac2f40b2010-06-01 19:37:31 +1000829 if (!page)
830 return;
NeilBrownece5cff2009-12-14 12:49:56 +1100831 bit = file_page_offset(bitmap, chunk);
NeilBrown32a76272005-06-21 17:17:14 -0700832
NeilBrownac2f40b2010-06-01 19:37:31 +1000833 /* set the bit */
NeilBrown32a76272005-06-21 17:17:14 -0700834 kaddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -0800835 if (bitmap->flags & BITMAP_HOSTENDIAN)
836 set_bit(bit, kaddr);
837 else
838 ext2_set_bit(bit, kaddr);
NeilBrown32a76272005-06-21 17:17:14 -0700839 kunmap_atomic(kaddr, KM_USER0);
840 PRINTK("set file bit %lu page %lu\n", bit, page->index);
841
842 /* record page number so it gets flushed to disk when unplug occurs */
843 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
NeilBrown32a76272005-06-21 17:17:14 -0700844}
845
846/* this gets called when the md device is ready to unplug its underlying
847 * (slave) device queues -- before we let any writes go down, we need to
848 * sync the dirty pages of the bitmap file to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700849void bitmap_unplug(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700850{
NeilBrownec7a3192006-06-26 00:27:45 -0700851 unsigned long i, flags;
852 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700853 struct page *page;
854 int wait = 0;
855
856 if (!bitmap)
NeilBrown4ad13662007-07-17 04:06:13 -0700857 return;
NeilBrown32a76272005-06-21 17:17:14 -0700858
859 /* look at each page to see if there are any set bits that need to be
860 * flushed out to disk */
861 for (i = 0; i < bitmap->file_pages; i++) {
862 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700863 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700864 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700865 return;
NeilBrown32a76272005-06-21 17:17:14 -0700866 }
867 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700868 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
869 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700870 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
871 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700872 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700873 wait = 1;
874 spin_unlock_irqrestore(&bitmap->lock, flags);
875
NeilBrownac2f40b2010-06-01 19:37:31 +1000876 if (dirty || need_write)
NeilBrown4ad13662007-07-17 04:06:13 -0700877 write_page(bitmap, page, 0);
NeilBrown32a76272005-06-21 17:17:14 -0700878 }
879 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700880 if (bitmap->file)
NeilBrownd785a062006-06-26 00:27:48 -0700881 wait_event(bitmap->write_wait,
882 atomic_read(&bitmap->pending_writes)==0);
NeilBrown0b79ccf2006-06-26 00:27:44 -0700883 else
NeilBrowna9701a32005-11-08 21:39:34 -0800884 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700885 }
NeilBrownd785a062006-06-26 00:27:48 -0700886 if (bitmap->flags & BITMAP_WRITE_ERROR)
887 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700888}
NeilBrownac2f40b2010-06-01 19:37:31 +1000889EXPORT_SYMBOL(bitmap_unplug);
NeilBrown32a76272005-06-21 17:17:14 -0700890
NeilBrown6a079972005-09-09 16:23:44 -0700891static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700892/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
893 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
894 * memory mapping of the bitmap file
895 * Special cases:
896 * if there's no bitmap file, or if the bitmap file had been
897 * previously kicked from the array, we mark all the bits as
898 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700899 *
900 * We ignore all bits for sectors that end earlier than 'start'.
901 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700902 */
NeilBrown6a079972005-09-09 16:23:44 -0700903static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700904{
905 unsigned long i, chunks, index, oldindex, bit;
906 struct page *page = NULL, *oldpage = NULL;
907 unsigned long num_pages, bit_cnt = 0;
908 struct file *file;
NeilBrownd785a062006-06-26 00:27:48 -0700909 unsigned long bytes, offset;
NeilBrown32a76272005-06-21 17:17:14 -0700910 int outofdate;
911 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800912 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700913
914 chunks = bitmap->chunks;
915 file = bitmap->file;
916
NeilBrown42a04b52009-12-14 12:49:53 +1100917 BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
NeilBrown32a76272005-06-21 17:17:14 -0700918
Olaf Hering44456d32005-07-27 11:45:17 -0700919#ifdef INJECT_FAULTS_3
NeilBrown32a76272005-06-21 17:17:14 -0700920 outofdate = 1;
921#else
922 outofdate = bitmap->flags & BITMAP_STALE;
923#endif
924 if (outofdate)
925 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
926 "recovery\n", bmname(bitmap));
927
928 bytes = (chunks + 7) / 8;
NeilBrownece5cff2009-12-14 12:49:56 +1100929 if (!bitmap->mddev->bitmap_info.external)
930 bytes += sizeof(bitmap_super_t);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700931
NeilBrownece5cff2009-12-14 12:49:56 +1100932 num_pages = (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
NeilBrownbc7f77d2005-06-21 17:17:17 -0700933
NeilBrownece5cff2009-12-14 12:49:56 +1100934 if (file && i_size_read(file->f_mapping->host) < bytes) {
NeilBrown32a76272005-06-21 17:17:14 -0700935 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
936 bmname(bitmap),
937 (unsigned long) i_size_read(file->f_mapping->host),
NeilBrownece5cff2009-12-14 12:49:56 +1100938 bytes);
NeilBrown4ad13662007-07-17 04:06:13 -0700939 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700940 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700941
942 ret = -ENOMEM;
943
NeilBrown32a76272005-06-21 17:17:14 -0700944 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700945 if (!bitmap->filemap)
NeilBrown4ad13662007-07-17 04:06:13 -0700946 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700947
NeilBrowne16b68b2006-06-26 00:27:45 -0700948 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
949 bitmap->filemap_attr = kzalloc(
NeilBrownac2f40b2010-06-01 19:37:31 +1000950 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
NeilBrowne16b68b2006-06-26 00:27:45 -0700951 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700952 if (!bitmap->filemap_attr)
NeilBrown4ad13662007-07-17 04:06:13 -0700953 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700954
NeilBrown32a76272005-06-21 17:17:14 -0700955 oldindex = ~0L;
956
957 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -0800958 int b;
NeilBrownece5cff2009-12-14 12:49:56 +1100959 index = file_page_index(bitmap, i);
960 bit = file_page_offset(bitmap, i);
NeilBrown32a76272005-06-21 17:17:14 -0700961 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -0700962 int count;
NeilBrown32a76272005-06-21 17:17:14 -0700963 /* unmap the old page, we're done with it */
NeilBrownd785a062006-06-26 00:27:48 -0700964 if (index == num_pages-1)
NeilBrownece5cff2009-12-14 12:49:56 +1100965 count = bytes - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -0700966 else
967 count = PAGE_SIZE;
NeilBrownece5cff2009-12-14 12:49:56 +1100968 if (index == 0 && bitmap->sb_page) {
NeilBrown32a76272005-06-21 17:17:14 -0700969 /*
970 * if we're here then the superblock page
971 * contains some bits (PAGE_SIZE != sizeof sb)
972 * we've already read it in, so just use it
973 */
974 page = bitmap->sb_page;
975 offset = sizeof(bitmap_super_t);
NeilBrown53845272009-01-09 08:31:05 +1100976 if (!file)
977 read_sb_page(bitmap->mddev,
NeilBrown42a04b52009-12-14 12:49:53 +1100978 bitmap->mddev->bitmap_info.offset,
NeilBrown53845272009-01-09 08:31:05 +1100979 page,
980 index, count);
NeilBrowna654b9d82005-06-21 17:17:27 -0700981 } else if (file) {
NeilBrownd785a062006-06-26 00:27:48 -0700982 page = read_page(file, index, bitmap, count);
NeilBrowna654b9d82005-06-21 17:17:27 -0700983 offset = 0;
984 } else {
NeilBrown42a04b52009-12-14 12:49:53 +1100985 page = read_sb_page(bitmap->mddev,
986 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100987 NULL,
988 index, count);
NeilBrown32a76272005-06-21 17:17:14 -0700989 offset = 0;
990 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700991 if (IS_ERR(page)) { /* read error */
992 ret = PTR_ERR(page);
NeilBrown4ad13662007-07-17 04:06:13 -0700993 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -0700994 }
995
NeilBrown32a76272005-06-21 17:17:14 -0700996 oldindex = index;
997 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -0700998
NeilBrownb74fd282009-05-07 12:47:19 +1000999 bitmap->filemap[bitmap->file_pages++] = page;
1000 bitmap->last_page_size = count;
1001
NeilBrown32a76272005-06-21 17:17:14 -07001002 if (outofdate) {
1003 /*
1004 * if bitmap is out of date, dirty the
NeilBrownac2f40b2010-06-01 19:37:31 +10001005 * whole page and write it out
NeilBrown32a76272005-06-21 17:17:14 -07001006 */
NeilBrownea03aff2006-01-06 00:20:34 -08001007 paddr = kmap_atomic(page, KM_USER0);
1008 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -07001009 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -08001010 kunmap_atomic(paddr, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -07001011 write_page(bitmap, page, 1);
1012
1013 ret = -EIO;
NeilBrownb74fd282009-05-07 12:47:19 +10001014 if (bitmap->flags & BITMAP_WRITE_ERROR)
NeilBrown4ad13662007-07-17 04:06:13 -07001015 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001016 }
NeilBrown32a76272005-06-21 17:17:14 -07001017 }
NeilBrownea03aff2006-01-06 00:20:34 -08001018 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001019 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001020 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001021 else
NeilBrownea03aff2006-01-06 00:20:34 -08001022 b = ext2_test_bit(bit, paddr);
1023 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001024 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -07001025 /* if the disk bit is set, set the memory bit */
NeilBrowndb305e52009-05-07 12:49:06 +10001026 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1027 >= start);
1028 bitmap_set_memory_bits(bitmap,
1029 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1030 needed);
NeilBrown32a76272005-06-21 17:17:14 -07001031 bit_cnt++;
NeilBrown6a079972005-09-09 16:23:44 -07001032 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -07001033 }
NeilBrown32a76272005-06-21 17:17:14 -07001034 }
1035
NeilBrownac2f40b2010-06-01 19:37:31 +10001036 /* everything went OK */
NeilBrown32a76272005-06-21 17:17:14 -07001037 ret = 0;
1038 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1039
NeilBrown32a76272005-06-21 17:17:14 -07001040 if (bit_cnt) { /* Kick recovery if any bits were set */
1041 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1042 md_wakeup_thread(bitmap->mddev->thread);
1043 }
1044
NeilBrown32a76272005-06-21 17:17:14 -07001045 printk(KERN_INFO "%s: bitmap initialized from disk: "
NeilBrown4ad13662007-07-17 04:06:13 -07001046 "read %lu/%lu pages, set %lu bits\n",
1047 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
NeilBrown32a76272005-06-21 17:17:14 -07001048
NeilBrown4ad13662007-07-17 04:06:13 -07001049 return 0;
1050
1051 err:
1052 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1053 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001054 return ret;
1055}
1056
NeilBrowna654b9d82005-06-21 17:17:27 -07001057void bitmap_write_all(struct bitmap *bitmap)
1058{
1059 /* We don't actually write all bitmap blocks here,
1060 * just flag them as needing to be written
1061 */
NeilBrownec7a3192006-06-26 00:27:45 -07001062 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001063
NeilBrownac2f40b2010-06-01 19:37:31 +10001064 for (i = 0; i < bitmap->file_pages; i++)
NeilBrownec7a3192006-06-26 00:27:45 -07001065 set_page_attr(bitmap, bitmap->filemap[i],
1066 BITMAP_PAGE_NEEDWRITE);
NeilBrowna654b9d82005-06-21 17:17:27 -07001067}
1068
NeilBrown32a76272005-06-21 17:17:14 -07001069static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1070{
1071 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1072 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1073 bitmap->bp[page].count += inc;
NeilBrown32a76272005-06-21 17:17:14 -07001074 bitmap_checkfree(bitmap, page);
1075}
1076static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1077 sector_t offset, int *blocks,
1078 int create);
1079
1080/*
1081 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1082 * out to disk
1083 */
1084
NeilBrownaa5cbd12009-12-14 12:49:46 +11001085void bitmap_daemon_work(mddev_t *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001086{
NeilBrownaa5cbd12009-12-14 12:49:46 +11001087 struct bitmap *bitmap;
NeilBrownaa3163f2005-06-21 17:17:22 -07001088 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001089 unsigned long flags;
1090 struct page *page = NULL, *lastpage = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001091 int blocks;
NeilBrownea03aff2006-01-06 00:20:34 -08001092 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001093
NeilBrownaa5cbd12009-12-14 12:49:46 +11001094 /* Use a mutex to guard daemon_work against
1095 * bitmap_destroy.
1096 */
NeilBrownc3d97142009-12-14 12:49:52 +11001097 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrownaa5cbd12009-12-14 12:49:46 +11001098 bitmap = mddev->bitmap;
1099 if (bitmap == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001100 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown4ad13662007-07-17 04:06:13 -07001101 return;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001102 }
NeilBrown42a04b52009-12-14 12:49:53 +11001103 if (time_before(jiffies, bitmap->daemon_lastrun
NeilBrown1b04be92009-12-14 12:49:53 +11001104 + bitmap->mddev->bitmap_info.daemon_sleep))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001105 goto done;
1106
NeilBrown32a76272005-06-21 17:17:14 -07001107 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001108 if (bitmap->allclean) {
1109 bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001110 goto done;
NeilBrown8311c292008-03-04 14:29:30 -08001111 }
1112 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001113
NeilBrownbe512692009-05-26 09:41:17 +10001114 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001115 for (j = 0; j < bitmap->chunks; j++) {
1116 bitmap_counter_t *bmc;
NeilBrownbe512692009-05-26 09:41:17 +10001117 if (!bitmap->filemap)
NeilBrown32a76272005-06-21 17:17:14 -07001118 /* error or shutdown */
NeilBrown32a76272005-06-21 17:17:14 -07001119 break;
NeilBrown32a76272005-06-21 17:17:14 -07001120
1121 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001122
1123 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001124 /* skip this page unless it's marked as needing cleaning */
NeilBrownec7a3192006-06-26 00:27:45 -07001125 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1126 int need_write = test_page_attr(bitmap, page,
1127 BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001128 if (need_write)
NeilBrownaa3163f2005-06-21 17:17:22 -07001129 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001130
NeilBrownaa3163f2005-06-21 17:17:22 -07001131 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown8311c292008-03-04 14:29:30 -08001132 if (need_write) {
NeilBrown4ad13662007-07-17 04:06:13 -07001133 write_page(bitmap, page, 0);
NeilBrown8311c292008-03-04 14:29:30 -08001134 bitmap->allclean = 0;
1135 }
NeilBrownbe512692009-05-26 09:41:17 +10001136 spin_lock_irqsave(&bitmap->lock, flags);
1137 j |= (PAGE_BITS - 1);
NeilBrownaa3163f2005-06-21 17:17:22 -07001138 continue;
1139 }
1140
NeilBrown32a76272005-06-21 17:17:14 -07001141 /* grab the new page, sync and release the old */
NeilBrown32a76272005-06-21 17:17:14 -07001142 if (lastpage != NULL) {
NeilBrownec7a3192006-06-26 00:27:45 -07001143 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001144 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1145 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001146 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001147 } else {
1148 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1149 spin_unlock_irqrestore(&bitmap->lock, flags);
1150 }
NeilBrown32a76272005-06-21 17:17:14 -07001151 } else
1152 spin_unlock_irqrestore(&bitmap->lock, flags);
1153 lastpage = page;
Neil Browna0da84f2008-06-28 08:31:22 +10001154
1155 /* We are possibly going to clear some bits, so make
1156 * sure that events_cleared is up-to-date.
1157 */
NeilBrownece5cff2009-12-14 12:49:56 +11001158 if (bitmap->need_sync &&
1159 bitmap->mddev->bitmap_info.external == 0) {
Neil Browna0da84f2008-06-28 08:31:22 +10001160 bitmap_super_t *sb;
1161 bitmap->need_sync = 0;
1162 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1163 sb->events_cleared =
1164 cpu_to_le64(bitmap->events_cleared);
1165 kunmap_atomic(sb, KM_USER0);
1166 write_page(bitmap, bitmap->sb_page, 1);
1167 }
NeilBrown32a76272005-06-21 17:17:14 -07001168 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownece5cff2009-12-14 12:49:56 +11001169 if (!bitmap->need_sync)
1170 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -07001171 }
NeilBrowndb305e52009-05-07 12:49:06 +10001172 bmc = bitmap_get_counter(bitmap,
1173 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1174 &blocks, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001175 if (bmc) {
NeilBrown8311c292008-03-04 14:29:30 -08001176 if (*bmc)
1177 bitmap->allclean = 0;
1178
NeilBrown32a76272005-06-21 17:17:14 -07001179 if (*bmc == 2) {
NeilBrownac2f40b2010-06-01 19:37:31 +10001180 *bmc = 1; /* maybe clear the bit next time */
NeilBrown32a76272005-06-21 17:17:14 -07001181 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrownece5cff2009-12-14 12:49:56 +11001182 } else if (*bmc == 1 && !bitmap->need_sync) {
NeilBrown32a76272005-06-21 17:17:14 -07001183 /* we can clear the bit */
1184 *bmc = 0;
NeilBrowndb305e52009-05-07 12:49:06 +10001185 bitmap_count_page(bitmap,
1186 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
NeilBrown32a76272005-06-21 17:17:14 -07001187 -1);
1188
1189 /* clear the bit */
NeilBrownea03aff2006-01-06 00:20:34 -08001190 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001191 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownece5cff2009-12-14 12:49:56 +11001192 clear_bit(file_page_offset(bitmap, j),
1193 paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001194 else
NeilBrownece5cff2009-12-14 12:49:56 +11001195 ext2_clear_bit(file_page_offset(bitmap, j),
1196 paddr);
NeilBrownea03aff2006-01-06 00:20:34 -08001197 kunmap_atomic(paddr, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -07001198 }
NeilBrownbe512692009-05-26 09:41:17 +10001199 } else
1200 j |= PAGE_COUNTER_MASK;
NeilBrown32a76272005-06-21 17:17:14 -07001201 }
NeilBrownbe512692009-05-26 09:41:17 +10001202 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001203
1204 /* now sync the final page */
1205 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001206 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001207 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001208 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1209 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001210 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001211 } else {
1212 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1213 spin_unlock_irqrestore(&bitmap->lock, flags);
1214 }
NeilBrown32a76272005-06-21 17:17:14 -07001215 }
1216
NeilBrown7be3dfe2008-03-10 11:43:48 -07001217 done:
NeilBrown8311c292008-03-04 14:29:30 -08001218 if (bitmap->allclean == 0)
NeilBrownac2f40b2010-06-01 19:37:31 +10001219 bitmap->mddev->thread->timeout =
NeilBrown1b04be92009-12-14 12:49:53 +11001220 bitmap->mddev->bitmap_info.daemon_sleep;
NeilBrownc3d97142009-12-14 12:49:52 +11001221 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown32a76272005-06-21 17:17:14 -07001222}
1223
NeilBrown32a76272005-06-21 17:17:14 -07001224static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1225 sector_t offset, int *blocks,
1226 int create)
NeilBrownee305ac2009-09-23 18:06:44 +10001227__releases(bitmap->lock)
1228__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -07001229{
1230 /* If 'create', we might release the lock and reclaim it.
1231 * The lock must have been taken with interrupts enabled.
1232 * If !create, we don't release the lock.
1233 */
1234 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1235 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1236 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1237 sector_t csize;
1238
1239 if (bitmap_checkpage(bitmap, page, create) < 0) {
1240 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
NeilBrownac2f40b2010-06-01 19:37:31 +10001241 *blocks = csize - (offset & (csize - 1));
NeilBrown32a76272005-06-21 17:17:14 -07001242 return NULL;
1243 }
1244 /* now locked ... */
1245
1246 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1247 /* should we use the first or second counter field
1248 * of the hijacked pointer? */
1249 int hi = (pageoff > PAGE_COUNTER_MASK);
1250 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1251 PAGE_COUNTER_SHIFT - 1);
NeilBrownac2f40b2010-06-01 19:37:31 +10001252 *blocks = csize - (offset & (csize - 1));
NeilBrown32a76272005-06-21 17:17:14 -07001253 return &((bitmap_counter_t *)
1254 &bitmap->bp[page].map)[hi];
1255 } else { /* page is allocated */
1256 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
NeilBrownac2f40b2010-06-01 19:37:31 +10001257 *blocks = csize - (offset & (csize - 1));
NeilBrown32a76272005-06-21 17:17:14 -07001258 return (bitmap_counter_t *)
1259 &(bitmap->bp[page].map[pageoff]);
1260 }
1261}
1262
NeilBrown4b6d2872005-09-09 16:23:47 -07001263int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001264{
NeilBrownac2f40b2010-06-01 19:37:31 +10001265 if (!bitmap)
1266 return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001267
1268 if (behind) {
Paul Clements696fcd52010-03-08 16:02:37 +11001269 int bw;
NeilBrown4b6d2872005-09-09 16:23:47 -07001270 atomic_inc(&bitmap->behind_writes);
Paul Clements696fcd52010-03-08 16:02:37 +11001271 bw = atomic_read(&bitmap->behind_writes);
1272 if (bw > bitmap->behind_writes_used)
1273 bitmap->behind_writes_used = bw;
1274
NeilBrown4b6d2872005-09-09 16:23:47 -07001275 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
Paul Clements696fcd52010-03-08 16:02:37 +11001276 bw, bitmap->max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001277 }
1278
NeilBrown32a76272005-06-21 17:17:14 -07001279 while (sectors) {
1280 int blocks;
1281 bitmap_counter_t *bmc;
1282
1283 spin_lock_irq(&bitmap->lock);
1284 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1285 if (!bmc) {
1286 spin_unlock_irq(&bitmap->lock);
1287 return 0;
1288 }
1289
Neil Brownda6e1a32007-02-08 14:20:37 -08001290 if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
1291 DEFINE_WAIT(__wait);
1292 /* note that it is safe to do the prepare_to_wait
1293 * after the test as long as we do it before dropping
1294 * the spinlock.
1295 */
1296 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1297 TASK_UNINTERRUPTIBLE);
1298 spin_unlock_irq(&bitmap->lock);
NeilBrownb63d7c22010-06-01 19:37:33 +10001299 md_unplug(bitmap->mddev);
Neil Brownda6e1a32007-02-08 14:20:37 -08001300 schedule();
1301 finish_wait(&bitmap->overflow_wait, &__wait);
1302 continue;
1303 }
1304
NeilBrownac2f40b2010-06-01 19:37:31 +10001305 switch (*bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001306 case 0:
1307 bitmap_file_set_bit(bitmap, offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001308 bitmap_count_page(bitmap, offset, 1);
NeilBrown32a76272005-06-21 17:17:14 -07001309 /* fall through */
1310 case 1:
1311 *bmc = 2;
1312 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001313
NeilBrown32a76272005-06-21 17:17:14 -07001314 (*bmc)++;
1315
1316 spin_unlock_irq(&bitmap->lock);
1317
1318 offset += blocks;
1319 if (sectors > blocks)
1320 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001321 else
1322 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001323 }
NeilBrown8311c292008-03-04 14:29:30 -08001324 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001325 return 0;
1326}
NeilBrownac2f40b2010-06-01 19:37:31 +10001327EXPORT_SYMBOL(bitmap_startwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001328
1329void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001330 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001331{
NeilBrownac2f40b2010-06-01 19:37:31 +10001332 if (!bitmap)
1333 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001334 if (behind) {
NeilBrowne5551902010-03-31 11:21:44 +11001335 if (atomic_dec_and_test(&bitmap->behind_writes))
1336 wake_up(&bitmap->behind_wait);
NeilBrown4b6d2872005-09-09 16:23:47 -07001337 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1338 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1339 }
NeilBrownd0a4bb42009-03-31 14:27:02 +11001340 if (bitmap->mddev->degraded)
1341 /* Never clear bits or update events_cleared when degraded */
1342 success = 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001343
NeilBrown32a76272005-06-21 17:17:14 -07001344 while (sectors) {
1345 int blocks;
1346 unsigned long flags;
1347 bitmap_counter_t *bmc;
1348
1349 spin_lock_irqsave(&bitmap->lock, flags);
1350 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1351 if (!bmc) {
1352 spin_unlock_irqrestore(&bitmap->lock, flags);
1353 return;
1354 }
1355
Neil Browna0da84f2008-06-28 08:31:22 +10001356 if (success &&
1357 bitmap->events_cleared < bitmap->mddev->events) {
1358 bitmap->events_cleared = bitmap->mddev->events;
1359 bitmap->need_sync = 1;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001360 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
Neil Browna0da84f2008-06-28 08:31:22 +10001361 }
1362
NeilBrown32a76272005-06-21 17:17:14 -07001363 if (!success && ! (*bmc & NEEDED_MASK))
1364 *bmc |= NEEDED_MASK;
1365
Neil Brownda6e1a32007-02-08 14:20:37 -08001366 if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
1367 wake_up(&bitmap->overflow_wait);
1368
NeilBrown32a76272005-06-21 17:17:14 -07001369 (*bmc)--;
NeilBrownac2f40b2010-06-01 19:37:31 +10001370 if (*bmc <= 2)
NeilBrown32a76272005-06-21 17:17:14 -07001371 set_page_attr(bitmap,
1372 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1373 BITMAP_PAGE_CLEAN);
NeilBrownac2f40b2010-06-01 19:37:31 +10001374
NeilBrown32a76272005-06-21 17:17:14 -07001375 spin_unlock_irqrestore(&bitmap->lock, flags);
1376 offset += blocks;
1377 if (sectors > blocks)
1378 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001379 else
1380 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001381 }
1382}
NeilBrownac2f40b2010-06-01 19:37:31 +10001383EXPORT_SYMBOL(bitmap_endwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001384
NeilBrown1187cf02009-03-31 14:27:02 +11001385static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1386 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001387{
1388 bitmap_counter_t *bmc;
1389 int rv;
1390 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1391 *blocks = 1024;
1392 return 1; /* always resync if no bitmap */
1393 }
1394 spin_lock_irq(&bitmap->lock);
1395 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1396 rv = 0;
1397 if (bmc) {
1398 /* locked */
1399 if (RESYNC(*bmc))
1400 rv = 1;
1401 else if (NEEDED(*bmc)) {
1402 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001403 if (!degraded) { /* don't set/clear bits if degraded */
1404 *bmc |= RESYNC_MASK;
1405 *bmc &= ~NEEDED_MASK;
1406 }
NeilBrown32a76272005-06-21 17:17:14 -07001407 }
1408 }
1409 spin_unlock_irq(&bitmap->lock);
NeilBrown8311c292008-03-04 14:29:30 -08001410 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001411 return rv;
1412}
1413
NeilBrown1187cf02009-03-31 14:27:02 +11001414int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1415 int degraded)
1416{
1417 /* bitmap_start_sync must always report on multiples of whole
1418 * pages, otherwise resync (which is very PAGE_SIZE based) will
1419 * get confused.
1420 * So call __bitmap_start_sync repeatedly (if needed) until
1421 * At least PAGE_SIZE>>9 blocks are covered.
1422 * Return the 'or' of the result.
1423 */
1424 int rv = 0;
1425 int blocks1;
1426
1427 *blocks = 0;
1428 while (*blocks < (PAGE_SIZE>>9)) {
1429 rv |= __bitmap_start_sync(bitmap, offset,
1430 &blocks1, degraded);
1431 offset += blocks1;
1432 *blocks += blocks1;
1433 }
1434 return rv;
1435}
NeilBrownac2f40b2010-06-01 19:37:31 +10001436EXPORT_SYMBOL(bitmap_start_sync);
NeilBrown1187cf02009-03-31 14:27:02 +11001437
NeilBrown32a76272005-06-21 17:17:14 -07001438void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1439{
1440 bitmap_counter_t *bmc;
1441 unsigned long flags;
NeilBrownac2f40b2010-06-01 19:37:31 +10001442
1443 if (bitmap == NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001444 *blocks = 1024;
1445 return;
1446 }
1447 spin_lock_irqsave(&bitmap->lock, flags);
1448 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1449 if (bmc == NULL)
1450 goto unlock;
1451 /* locked */
NeilBrown32a76272005-06-21 17:17:14 -07001452 if (RESYNC(*bmc)) {
1453 *bmc &= ~RESYNC_MASK;
1454
1455 if (!NEEDED(*bmc) && aborted)
1456 *bmc |= NEEDED_MASK;
1457 else {
NeilBrownac2f40b2010-06-01 19:37:31 +10001458 if (*bmc <= 2)
NeilBrown32a76272005-06-21 17:17:14 -07001459 set_page_attr(bitmap,
1460 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1461 BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -07001462 }
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}
NeilBrownac2f40b2010-06-01 19:37:31 +10001468EXPORT_SYMBOL(bitmap_end_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001469
1470void bitmap_close_sync(struct bitmap *bitmap)
1471{
1472 /* Sync has finished, and any bitmap chunks that weren't synced
1473 * properly have been aborted. It remains to us to clear the
1474 * RESYNC bit wherever it is still on
1475 */
1476 sector_t sector = 0;
1477 int blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001478 if (!bitmap)
1479 return;
NeilBrown32a76272005-06-21 17:17:14 -07001480 while (sector < bitmap->mddev->resync_max_sectors) {
1481 bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001482 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001483 }
1484}
NeilBrownac2f40b2010-06-01 19:37:31 +10001485EXPORT_SYMBOL(bitmap_close_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001486
NeilBrownb47490c2008-02-06 01:39:50 -08001487void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1488{
1489 sector_t s = 0;
1490 int blocks;
1491
1492 if (!bitmap)
1493 return;
1494 if (sector == 0) {
1495 bitmap->last_end_sync = jiffies;
1496 return;
1497 }
1498 if (time_before(jiffies, (bitmap->last_end_sync
NeilBrown1b04be92009-12-14 12:49:53 +11001499 + bitmap->mddev->bitmap_info.daemon_sleep)))
NeilBrownb47490c2008-02-06 01:39:50 -08001500 return;
1501 wait_event(bitmap->mddev->recovery_wait,
1502 atomic_read(&bitmap->mddev->recovery_active) == 0);
1503
NeilBrown97e4f422009-03-31 14:33:13 +11001504 bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync;
NeilBrown676e42d2010-06-01 19:37:26 +10001505 if (bitmap->mddev->persistent)
1506 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
NeilBrownb47490c2008-02-06 01:39:50 -08001507 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1508 s = 0;
1509 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1510 bitmap_end_sync(bitmap, s, &blocks, 0);
1511 s += blocks;
1512 }
1513 bitmap->last_end_sync = jiffies;
NeilBrownacb180b2009-04-14 16:28:34 +10001514 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
NeilBrownb47490c2008-02-06 01:39:50 -08001515}
NeilBrownac2f40b2010-06-01 19:37:31 +10001516EXPORT_SYMBOL(bitmap_cond_end_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08001517
NeilBrown6a079972005-09-09 16:23:44 -07001518static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001519{
1520 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001521 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001522 * be 0 at this point
1523 */
NeilBrown193f1c92005-08-04 12:53:33 -07001524
1525 int secs;
1526 bitmap_counter_t *bmc;
1527 spin_lock_irq(&bitmap->lock);
1528 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1529 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001530 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001531 return;
NeilBrown32a76272005-06-21 17:17:14 -07001532 }
NeilBrownac2f40b2010-06-01 19:37:31 +10001533 if (!*bmc) {
NeilBrown193f1c92005-08-04 12:53:33 -07001534 struct page *page;
NeilBrownac2f40b2010-06-01 19:37:31 +10001535 *bmc = 1 | (needed ? NEEDED_MASK : 0);
NeilBrown193f1c92005-08-04 12:53:33 -07001536 bitmap_count_page(bitmap, offset, 1);
1537 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1538 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1539 }
1540 spin_unlock_irq(&bitmap->lock);
NeilBrown8311c292008-03-04 14:29:30 -08001541 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001542}
1543
Paul Clements9b1d1da2006-10-03 01:15:49 -07001544/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1545void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1546{
1547 unsigned long chunk;
1548
1549 for (chunk = s; chunk <= e; chunk++) {
NeilBrowndb305e52009-05-07 12:49:06 +10001550 sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001551 bitmap_set_memory_bits(bitmap, sec, 1);
1552 bitmap_file_set_bit(bitmap, sec);
NeilBrownffa23322009-12-14 12:49:56 +11001553 if (sec < bitmap->mddev->recovery_cp)
1554 /* We are asserting that the array is dirty,
1555 * so move the recovery_cp address back so
1556 * that it is obvious that it is dirty
1557 */
1558 bitmap->mddev->recovery_cp = sec;
Paul Clements9b1d1da2006-10-03 01:15:49 -07001559 }
1560}
1561
NeilBrown32a76272005-06-21 17:17:14 -07001562/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001563 * flush out any pending updates
1564 */
1565void bitmap_flush(mddev_t *mddev)
1566{
1567 struct bitmap *bitmap = mddev->bitmap;
NeilBrown42a04b52009-12-14 12:49:53 +11001568 long sleep;
NeilBrown6b8b3e82005-08-04 12:53:35 -07001569
1570 if (!bitmap) /* there was no bitmap */
1571 return;
1572
1573 /* run the daemon_work three time to ensure everything is flushed
1574 * that can be
1575 */
NeilBrown1b04be92009-12-14 12:49:53 +11001576 sleep = mddev->bitmap_info.daemon_sleep * 2;
NeilBrown42a04b52009-12-14 12:49:53 +11001577 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001578 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001579 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001580 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001581 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001582 bitmap_daemon_work(mddev);
NeilBrown6b8b3e82005-08-04 12:53:35 -07001583 bitmap_update_sb(bitmap);
1584}
1585
1586/*
NeilBrown32a76272005-06-21 17:17:14 -07001587 * free memory that was allocated
1588 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001589static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001590{
1591 unsigned long k, pages;
1592 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001593
1594 if (!bitmap) /* there was no bitmap */
1595 return;
1596
NeilBrown32a76272005-06-21 17:17:14 -07001597 /* release the bitmap file and kill the daemon */
1598 bitmap_file_put(bitmap);
1599
1600 bp = bitmap->bp;
1601 pages = bitmap->pages;
1602
1603 /* free all allocated memory */
1604
NeilBrown32a76272005-06-21 17:17:14 -07001605 if (bp) /* deallocate the page memory */
1606 for (k = 0; k < pages; k++)
1607 if (bp[k].map && !bp[k].hijacked)
1608 kfree(bp[k].map);
1609 kfree(bp);
1610 kfree(bitmap);
1611}
NeilBrownaa5cbd12009-12-14 12:49:46 +11001612
NeilBrown3178b0d2005-09-09 16:23:50 -07001613void bitmap_destroy(mddev_t *mddev)
1614{
1615 struct bitmap *bitmap = mddev->bitmap;
1616
1617 if (!bitmap) /* there was no bitmap */
1618 return;
1619
NeilBrownc3d97142009-12-14 12:49:52 +11001620 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrown3178b0d2005-09-09 16:23:50 -07001621 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownc3d97142009-12-14 12:49:52 +11001622 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrownb15c2e52006-01-06 00:20:16 -08001623 if (mddev->thread)
1624 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001625
NeilBrownece5cff2009-12-14 12:49:56 +11001626 if (bitmap->sysfs_can_clear)
1627 sysfs_put(bitmap->sysfs_can_clear);
1628
NeilBrown3178b0d2005-09-09 16:23:50 -07001629 bitmap_free(bitmap);
1630}
NeilBrown32a76272005-06-21 17:17:14 -07001631
1632/*
1633 * initialize the bitmap structure
1634 * if this returns an error, bitmap_destroy must be called to do clean up
1635 */
1636int bitmap_create(mddev_t *mddev)
1637{
1638 struct bitmap *bitmap;
NeilBrown1f593902009-04-20 11:50:24 +10001639 sector_t blocks = mddev->resync_max_sectors;
NeilBrown32a76272005-06-21 17:17:14 -07001640 unsigned long chunks;
1641 unsigned long pages;
NeilBrownc3d97142009-12-14 12:49:52 +11001642 struct file *file = mddev->bitmap_info.file;
NeilBrown32a76272005-06-21 17:17:14 -07001643 int err;
NeilBrown6a079972005-09-09 16:23:44 -07001644 sector_t start;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001645 struct sysfs_dirent *bm = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001646
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001647 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001648
NeilBrownc3d97142009-12-14 12:49:52 +11001649 if (!file && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001650 return 0;
1651
NeilBrownc3d97142009-12-14 12:49:52 +11001652 BUG_ON(file && mddev->bitmap_info.offset);
NeilBrowna654b9d82005-06-21 17:17:27 -07001653
NeilBrown9ffae0c2006-01-06 00:20:32 -08001654 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001655 if (!bitmap)
1656 return -ENOMEM;
1657
NeilBrown32a76272005-06-21 17:17:14 -07001658 spin_lock_init(&bitmap->lock);
NeilBrownce25c312006-06-26 00:27:49 -07001659 atomic_set(&bitmap->pending_writes, 0);
1660 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001661 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrowne5551902010-03-31 11:21:44 +11001662 init_waitqueue_head(&bitmap->behind_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001663
NeilBrown32a76272005-06-21 17:17:14 -07001664 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001665
NeilBrown5ff5aff2010-06-01 19:37:32 +10001666 if (mddev->kobj.sd)
1667 bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
NeilBrownece5cff2009-12-14 12:49:56 +11001668 if (bm) {
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07001669 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
NeilBrownece5cff2009-12-14 12:49:56 +11001670 sysfs_put(bm);
1671 } else
1672 bitmap->sysfs_can_clear = NULL;
1673
NeilBrown32a76272005-06-21 17:17:14 -07001674 bitmap->file = file;
NeilBrownce25c312006-06-26 00:27:49 -07001675 if (file) {
1676 get_file(file);
NeilBrownae8fa282009-10-16 15:56:01 +11001677 /* As future accesses to this file will use bmap,
1678 * and bypass the page cache, we must sync the file
1679 * first.
1680 */
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001681 vfs_fsync(file, 1);
NeilBrownce25c312006-06-26 00:27:49 -07001682 }
NeilBrown42a04b52009-12-14 12:49:53 +11001683 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
NeilBrownece5cff2009-12-14 12:49:56 +11001684 if (!mddev->bitmap_info.external)
1685 err = bitmap_read_sb(bitmap);
1686 else {
1687 err = 0;
1688 if (mddev->bitmap_info.chunksize == 0 ||
1689 mddev->bitmap_info.daemon_sleep == 0)
1690 /* chunksize and time_base need to be
1691 * set first. */
1692 err = -EINVAL;
1693 }
NeilBrown32a76272005-06-21 17:17:14 -07001694 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001695 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001696
NeilBrown624ce4f2009-12-14 12:49:56 +11001697 bitmap->daemon_lastrun = jiffies;
NeilBrown42a04b52009-12-14 12:49:53 +11001698 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
NeilBrown32a76272005-06-21 17:17:14 -07001699
1700 /* now that chunksize and chunkshift are set, we can use these macros */
NeilBrownac2f40b2010-06-01 19:37:31 +10001701 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
NeilBrown1f593902009-04-20 11:50:24 +10001702 CHUNK_BLOCK_SHIFT(bitmap);
NeilBrownac2f40b2010-06-01 19:37:31 +10001703 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
NeilBrown32a76272005-06-21 17:17:14 -07001704
1705 BUG_ON(!pages);
1706
1707 bitmap->chunks = chunks;
1708 bitmap->pages = pages;
1709 bitmap->missing_pages = pages;
1710 bitmap->counter_bits = COUNTER_BITS;
1711
1712 bitmap->syncchunk = ~0UL;
1713
Olaf Hering44456d32005-07-27 11:45:17 -07001714#ifdef INJECT_FATAL_FAULT_1
NeilBrown32a76272005-06-21 17:17:14 -07001715 bitmap->bp = NULL;
1716#else
NeilBrown9ffae0c2006-01-06 00:20:32 -08001717 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001718#endif
NeilBrown3178b0d2005-09-09 16:23:50 -07001719 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001720 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001721 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001722
NeilBrown32a76272005-06-21 17:17:14 -07001723 /* now that we have some pages available, initialize the in-memory
1724 * bitmap from the on-disk bitmap */
NeilBrown6a079972005-09-09 16:23:44 -07001725 start = 0;
1726 if (mddev->degraded == 0
1727 || bitmap->events_cleared == mddev->events)
1728 /* no need to keep dirty bits to optimise a re-add of a missing device */
1729 start = mddev->recovery_cp;
1730 err = bitmap_init_from_disk(bitmap, start);
NeilBrown193f1c92005-08-04 12:53:33 -07001731
NeilBrown32a76272005-06-21 17:17:14 -07001732 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001733 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001734
1735 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1736 pages, bmname(bitmap));
1737
NeilBrown3178b0d2005-09-09 16:23:50 -07001738 mddev->bitmap = bitmap;
1739
NeilBrown1b04be92009-12-14 12:49:53 +11001740 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
NeilBrown9cd30fd2009-12-14 12:49:54 +11001741 md_wakeup_thread(mddev->thread);
NeilBrownb15c2e52006-01-06 00:20:16 -08001742
NeilBrown4ad13662007-07-17 04:06:13 -07001743 bitmap_update_sb(bitmap);
1744
1745 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
NeilBrown3178b0d2005-09-09 16:23:50 -07001746
1747 error:
1748 bitmap_free(bitmap);
1749 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001750}
1751
NeilBrown43a70502009-12-14 12:49:55 +11001752static ssize_t
1753location_show(mddev_t *mddev, char *page)
1754{
1755 ssize_t len;
NeilBrownac2f40b2010-06-01 19:37:31 +10001756 if (mddev->bitmap_info.file)
NeilBrown43a70502009-12-14 12:49:55 +11001757 len = sprintf(page, "file");
NeilBrownac2f40b2010-06-01 19:37:31 +10001758 else if (mddev->bitmap_info.offset)
NeilBrown43a70502009-12-14 12:49:55 +11001759 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001760 else
NeilBrown43a70502009-12-14 12:49:55 +11001761 len = sprintf(page, "none");
1762 len += sprintf(page+len, "\n");
1763 return len;
1764}
1765
1766static ssize_t
1767location_store(mddev_t *mddev, const char *buf, size_t len)
1768{
1769
1770 if (mddev->pers) {
1771 if (!mddev->pers->quiesce)
1772 return -EBUSY;
1773 if (mddev->recovery || mddev->sync_thread)
1774 return -EBUSY;
1775 }
1776
1777 if (mddev->bitmap || mddev->bitmap_info.file ||
1778 mddev->bitmap_info.offset) {
1779 /* bitmap already configured. Only option is to clear it */
1780 if (strncmp(buf, "none", 4) != 0)
1781 return -EBUSY;
1782 if (mddev->pers) {
1783 mddev->pers->quiesce(mddev, 1);
1784 bitmap_destroy(mddev);
1785 mddev->pers->quiesce(mddev, 0);
1786 }
1787 mddev->bitmap_info.offset = 0;
1788 if (mddev->bitmap_info.file) {
1789 struct file *f = mddev->bitmap_info.file;
1790 mddev->bitmap_info.file = NULL;
1791 restore_bitmap_write_access(f);
1792 fput(f);
1793 }
1794 } else {
1795 /* No bitmap, OK to set a location */
1796 long long offset;
1797 if (strncmp(buf, "none", 4) == 0)
1798 /* nothing to be done */;
1799 else if (strncmp(buf, "file:", 5) == 0) {
1800 /* Not supported yet */
1801 return -EINVAL;
1802 } else {
1803 int rv;
1804 if (buf[0] == '+')
1805 rv = strict_strtoll(buf+1, 10, &offset);
1806 else
1807 rv = strict_strtoll(buf, 10, &offset);
1808 if (rv)
1809 return rv;
1810 if (offset == 0)
1811 return -EINVAL;
NeilBrownece5cff2009-12-14 12:49:56 +11001812 if (mddev->bitmap_info.external == 0 &&
1813 mddev->major_version == 0 &&
NeilBrown43a70502009-12-14 12:49:55 +11001814 offset != mddev->bitmap_info.default_offset)
1815 return -EINVAL;
1816 mddev->bitmap_info.offset = offset;
1817 if (mddev->pers) {
1818 mddev->pers->quiesce(mddev, 1);
1819 rv = bitmap_create(mddev);
1820 if (rv) {
1821 bitmap_destroy(mddev);
1822 mddev->bitmap_info.offset = 0;
1823 }
1824 mddev->pers->quiesce(mddev, 0);
1825 if (rv)
1826 return rv;
1827 }
1828 }
1829 }
1830 if (!mddev->external) {
1831 /* Ensure new bitmap info is stored in
1832 * metadata promptly.
1833 */
1834 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1835 md_wakeup_thread(mddev->thread);
1836 }
1837 return len;
1838}
1839
1840static struct md_sysfs_entry bitmap_location =
1841__ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
1842
1843static ssize_t
1844timeout_show(mddev_t *mddev, char *page)
1845{
1846 ssize_t len;
1847 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
1848 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
NeilBrownac2f40b2010-06-01 19:37:31 +10001849
NeilBrown43a70502009-12-14 12:49:55 +11001850 len = sprintf(page, "%lu", secs);
1851 if (jifs)
1852 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
1853 len += sprintf(page+len, "\n");
1854 return len;
1855}
1856
1857static ssize_t
1858timeout_store(mddev_t *mddev, const char *buf, size_t len)
1859{
1860 /* timeout can be set at any time */
1861 unsigned long timeout;
1862 int rv = strict_strtoul_scaled(buf, &timeout, 4);
1863 if (rv)
1864 return rv;
1865
1866 /* just to make sure we don't overflow... */
1867 if (timeout >= LONG_MAX / HZ)
1868 return -EINVAL;
1869
1870 timeout = timeout * HZ / 10000;
1871
1872 if (timeout >= MAX_SCHEDULE_TIMEOUT)
1873 timeout = MAX_SCHEDULE_TIMEOUT-1;
1874 if (timeout < 1)
1875 timeout = 1;
1876 mddev->bitmap_info.daemon_sleep = timeout;
1877 if (mddev->thread) {
1878 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1879 * the bitmap is all clean and we don't need to
1880 * adjust the timeout right now
1881 */
1882 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
1883 mddev->thread->timeout = timeout;
1884 md_wakeup_thread(mddev->thread);
1885 }
1886 }
1887 return len;
1888}
1889
1890static struct md_sysfs_entry bitmap_timeout =
1891__ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
1892
1893static ssize_t
1894backlog_show(mddev_t *mddev, char *page)
1895{
1896 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
1897}
1898
1899static ssize_t
1900backlog_store(mddev_t *mddev, const char *buf, size_t len)
1901{
1902 unsigned long backlog;
1903 int rv = strict_strtoul(buf, 10, &backlog);
1904 if (rv)
1905 return rv;
1906 if (backlog > COUNTER_MAX)
1907 return -EINVAL;
1908 mddev->bitmap_info.max_write_behind = backlog;
1909 return len;
1910}
1911
1912static struct md_sysfs_entry bitmap_backlog =
1913__ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
1914
1915static ssize_t
1916chunksize_show(mddev_t *mddev, char *page)
1917{
1918 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
1919}
1920
1921static ssize_t
1922chunksize_store(mddev_t *mddev, const char *buf, size_t len)
1923{
1924 /* Can only be changed when no bitmap is active */
1925 int rv;
1926 unsigned long csize;
1927 if (mddev->bitmap)
1928 return -EBUSY;
1929 rv = strict_strtoul(buf, 10, &csize);
1930 if (rv)
1931 return rv;
1932 if (csize < 512 ||
1933 !is_power_of_2(csize))
1934 return -EINVAL;
1935 mddev->bitmap_info.chunksize = csize;
1936 return len;
1937}
1938
1939static struct md_sysfs_entry bitmap_chunksize =
1940__ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
1941
NeilBrownece5cff2009-12-14 12:49:56 +11001942static ssize_t metadata_show(mddev_t *mddev, char *page)
1943{
1944 return sprintf(page, "%s\n", (mddev->bitmap_info.external
1945 ? "external" : "internal"));
1946}
1947
1948static ssize_t metadata_store(mddev_t *mddev, const char *buf, size_t len)
1949{
1950 if (mddev->bitmap ||
1951 mddev->bitmap_info.file ||
1952 mddev->bitmap_info.offset)
1953 return -EBUSY;
1954 if (strncmp(buf, "external", 8) == 0)
1955 mddev->bitmap_info.external = 1;
1956 else if (strncmp(buf, "internal", 8) == 0)
1957 mddev->bitmap_info.external = 0;
1958 else
1959 return -EINVAL;
1960 return len;
1961}
1962
1963static struct md_sysfs_entry bitmap_metadata =
1964__ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
1965
1966static ssize_t can_clear_show(mddev_t *mddev, char *page)
1967{
1968 int len;
1969 if (mddev->bitmap)
1970 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
1971 "false" : "true"));
1972 else
1973 len = sprintf(page, "\n");
1974 return len;
1975}
1976
1977static ssize_t can_clear_store(mddev_t *mddev, const char *buf, size_t len)
1978{
1979 if (mddev->bitmap == NULL)
1980 return -ENOENT;
1981 if (strncmp(buf, "false", 5) == 0)
1982 mddev->bitmap->need_sync = 1;
1983 else if (strncmp(buf, "true", 4) == 0) {
1984 if (mddev->degraded)
1985 return -EBUSY;
1986 mddev->bitmap->need_sync = 0;
1987 } else
1988 return -EINVAL;
1989 return len;
1990}
1991
1992static struct md_sysfs_entry bitmap_can_clear =
1993__ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
1994
Paul Clements696fcd52010-03-08 16:02:37 +11001995static ssize_t
1996behind_writes_used_show(mddev_t *mddev, char *page)
1997{
1998 if (mddev->bitmap == NULL)
1999 return sprintf(page, "0\n");
2000 return sprintf(page, "%lu\n",
2001 mddev->bitmap->behind_writes_used);
2002}
2003
2004static ssize_t
2005behind_writes_used_reset(mddev_t *mddev, const char *buf, size_t len)
2006{
2007 if (mddev->bitmap)
2008 mddev->bitmap->behind_writes_used = 0;
2009 return len;
2010}
2011
2012static struct md_sysfs_entry max_backlog_used =
2013__ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2014 behind_writes_used_show, behind_writes_used_reset);
2015
NeilBrown43a70502009-12-14 12:49:55 +11002016static struct attribute *md_bitmap_attrs[] = {
2017 &bitmap_location.attr,
2018 &bitmap_timeout.attr,
2019 &bitmap_backlog.attr,
2020 &bitmap_chunksize.attr,
NeilBrownece5cff2009-12-14 12:49:56 +11002021 &bitmap_metadata.attr,
2022 &bitmap_can_clear.attr,
Paul Clements696fcd52010-03-08 16:02:37 +11002023 &max_backlog_used.attr,
NeilBrown43a70502009-12-14 12:49:55 +11002024 NULL
2025};
2026struct attribute_group md_bitmap_group = {
2027 .name = "bitmap",
2028 .attrs = md_bitmap_attrs,
2029};
2030