blob: 54bfc274b39abe9fab10f0f1501e1d346b43138f [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
NeilBrowne384e582010-06-01 19:37:34 +100032#include <linux/dm-dirty-log.h>
NeilBrown32a76272005-06-21 17:17:14 -070033/* 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
NeilBrown32a76272005-06-21 17:17:14 -070054#ifndef PRINTK
55# if DEBUG > 0
56# define PRINTK(x...) printk(KERN_DEBUG x)
57# else
58# define PRINTK(x...)
59# endif
60#endif
61
NeilBrownac2f40b2010-06-01 19:37:31 +100062static inline char *bmname(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -070063{
64 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
65}
66
NeilBrown32a76272005-06-21 17:17:14 -070067/*
68 * just a placeholder - calls kmalloc for bitmap pages
69 */
70static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
71{
72 unsigned char *page;
73
Olaf Hering44456d32005-07-27 11:45:17 -070074#ifdef INJECT_FAULTS_1
NeilBrown32a76272005-06-21 17:17:14 -070075 page = NULL;
76#else
NeilBrownac2f40b2010-06-01 19:37:31 +100077 page = kzalloc(PAGE_SIZE, GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -070078#endif
79 if (!page)
80 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
81 else
NeilBrowna654b9d82005-06-21 17:17:27 -070082 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
NeilBrown32a76272005-06-21 17:17:14 -070083 bmname(bitmap), page);
84 return page;
85}
86
87/*
88 * for now just a placeholder -- just calls kfree for bitmap pages
89 */
90static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
91{
92 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
93 kfree(page);
94}
95
96/*
97 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
98 *
99 * 1) check to see if this page is allocated, if it's not then try to alloc
100 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
101 * page pointer directly as a counter
102 *
103 * if we find our page, we increment the page's refcount so that it stays
104 * allocated while we're using it
105 */
NeilBrownac2f40b2010-06-01 19:37:31 +1000106static int bitmap_checkpage(struct bitmap *bitmap,
107 unsigned long page, int create)
NeilBrownee305ac2009-09-23 18:06:44 +1000108__releases(bitmap->lock)
109__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -0700110{
111 unsigned char *mappage;
112
113 if (page >= bitmap->pages) {
NeilBrown1187cf02009-03-31 14:27:02 +1100114 /* This can happen if bitmap_start_sync goes beyond
115 * End-of-device while looking for a whole page.
116 * It is harmless.
117 */
NeilBrown32a76272005-06-21 17:17:14 -0700118 return -EINVAL;
119 }
120
NeilBrown32a76272005-06-21 17:17:14 -0700121 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
122 return 0;
123
124 if (bitmap->bp[page].map) /* page is already allocated, just return */
125 return 0;
126
127 if (!create)
128 return -ENOENT;
129
NeilBrown32a76272005-06-21 17:17:14 -0700130 /* this page has not been allocated yet */
131
NeilBrownac2f40b2010-06-01 19:37:31 +1000132 spin_unlock_irq(&bitmap->lock);
133 mappage = bitmap_alloc_page(bitmap);
134 spin_lock_irq(&bitmap->lock);
135
136 if (mappage == NULL) {
NeilBrown32a76272005-06-21 17:17:14 -0700137 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
138 bmname(bitmap));
139 /* failed - set the hijacked flag so that we can use the
140 * pointer as a counter */
NeilBrown32a76272005-06-21 17:17:14 -0700141 if (!bitmap->bp[page].map)
142 bitmap->bp[page].hijacked = 1;
NeilBrownac2f40b2010-06-01 19:37:31 +1000143 } else if (bitmap->bp[page].map ||
144 bitmap->bp[page].hijacked) {
NeilBrown32a76272005-06-21 17:17:14 -0700145 /* somebody beat us to getting the page */
146 bitmap_free_page(bitmap, mappage);
147 return 0;
NeilBrownac2f40b2010-06-01 19:37:31 +1000148 } else {
149
150 /* no page was in place and we have one, so install it */
151
152 bitmap->bp[page].map = mappage;
153 bitmap->missing_pages--;
NeilBrown32a76272005-06-21 17:17:14 -0700154 }
NeilBrown32a76272005-06-21 17:17:14 -0700155 return 0;
156}
157
NeilBrown32a76272005-06-21 17:17:14 -0700158/* if page is completely empty, put it back on the free list, or dealloc it */
159/* if page was hijacked, unmark the flag so it might get alloced next time */
160/* Note: lock should be held when calling this */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800161static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
NeilBrown32a76272005-06-21 17:17:14 -0700162{
163 char *ptr;
164
165 if (bitmap->bp[page].count) /* page is still busy */
166 return;
167
168 /* page is no longer in use, it can be released */
169
170 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
171 bitmap->bp[page].hijacked = 0;
172 bitmap->bp[page].map = NULL;
NeilBrownac2f40b2010-06-01 19:37:31 +1000173 } else {
174 /* normal case, free the page */
175 ptr = bitmap->bp[page].map;
176 bitmap->bp[page].map = NULL;
177 bitmap->missing_pages++;
178 bitmap_free_page(bitmap, ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700179 }
NeilBrown32a76272005-06-21 17:17:14 -0700180}
181
NeilBrown32a76272005-06-21 17:17:14 -0700182/*
183 * bitmap file handling - read and write the bitmap file and its superblock
184 */
185
NeilBrown32a76272005-06-21 17:17:14 -0700186/*
187 * basic page I/O operations
188 */
189
NeilBrowna654b9d82005-06-21 17:17:27 -0700190/* IO operations when bitmap is stored near all superblocks */
NeilBrownf6af9492009-12-14 12:49:54 +1100191static struct page *read_sb_page(mddev_t *mddev, loff_t offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100192 struct page *page,
193 unsigned long index, int size)
NeilBrowna654b9d82005-06-21 17:17:27 -0700194{
195 /* choose a good rdev and read the page from there */
196
197 mdk_rdev_t *rdev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700198 sector_t target;
NeilBrownac2f40b2010-06-01 19:37:31 +1000199 int did_alloc = 0;
NeilBrowna654b9d82005-06-21 17:17:27 -0700200
NeilBrownac2f40b2010-06-01 19:37:31 +1000201 if (!page) {
NeilBrowna2ed9612008-12-19 16:25:01 +1100202 page = alloc_page(GFP_KERNEL);
NeilBrownac2f40b2010-06-01 19:37:31 +1000203 if (!page)
204 return ERR_PTR(-ENOMEM);
205 did_alloc = 1;
206 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700207
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100208 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800209 if (! test_bit(In_sync, &rdev->flags)
210 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700211 continue;
212
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100213 target = offset + index * (PAGE_SIZE/512);
NeilBrowna654b9d82005-06-21 17:17:27 -0700214
NeilBrown2b193362010-10-27 15:16:40 +1100215 if (sync_page_io(rdev, target,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400216 roundup(size, bdev_logical_block_size(rdev->bdev)),
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100217 page, READ, true)) {
NeilBrownab904d62005-09-09 16:23:52 -0700218 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700219 attach_page_buffers(page, NULL); /* so that free_buffer will
220 * quietly no-op */
NeilBrownab904d62005-09-09 16:23:52 -0700221 return page;
222 }
223 }
NeilBrownac2f40b2010-06-01 19:37:31 +1000224 if (did_alloc)
225 put_page(page);
NeilBrownab904d62005-09-09 16:23:52 -0700226 return ERR_PTR(-EIO);
NeilBrowna654b9d82005-06-21 17:17:27 -0700227
NeilBrowna654b9d82005-06-21 17:17:27 -0700228}
229
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000230static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
231{
232 /* Iterate the disks of an mddev, using rcu to protect access to the
233 * linked list, and raising the refcount of devices we return to ensure
234 * they don't disappear while in use.
235 * As devices are only added or removed when raid_disk is < 0 and
236 * nr_pending is 0 and In_sync is clear, the entries we return will
237 * still be in the same position on the list when we re-enter
238 * list_for_each_continue_rcu.
239 */
240 struct list_head *pos;
241 rcu_read_lock();
242 if (rdev == NULL)
243 /* start at the beginning */
244 pos = &mddev->disks;
245 else {
246 /* release the previous rdev and start from there. */
247 rdev_dec_pending(rdev, mddev);
248 pos = &rdev->same_set;
249 }
250 list_for_each_continue_rcu(pos, &mddev->disks) {
251 rdev = list_entry(pos, mdk_rdev_t, same_set);
252 if (rdev->raid_disk >= 0 &&
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000253 !test_bit(Faulty, &rdev->flags)) {
254 /* this is a usable devices */
255 atomic_inc(&rdev->nr_pending);
256 rcu_read_unlock();
257 return rdev;
258 }
259 }
260 rcu_read_unlock();
261 return NULL;
262}
263
NeilBrownab6085c2007-05-23 13:58:10 -0700264static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrowna654b9d82005-06-21 17:17:27 -0700265{
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000266 mdk_rdev_t *rdev = NULL;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100267 struct block_device *bdev;
NeilBrownab6085c2007-05-23 13:58:10 -0700268 mddev_t *mddev = bitmap->mddev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700269
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000270 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000271 int size = PAGE_SIZE;
272 loff_t offset = mddev->bitmap_info.offset;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100273
274 bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
275
NeilBrownac2f40b2010-06-01 19:37:31 +1000276 if (page->index == bitmap->file_pages-1)
277 size = roundup(bitmap->last_page_size,
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100278 bdev_logical_block_size(bdev));
NeilBrownac2f40b2010-06-01 19:37:31 +1000279 /* Just make sure we aren't corrupting data or
280 * metadata
281 */
282 if (mddev->external) {
283 /* Bitmap could be anywhere. */
284 if (rdev->sb_start + offset + (page->index
285 * (PAGE_SIZE/512))
286 > rdev->data_offset
287 &&
288 rdev->sb_start + offset
289 < (rdev->data_offset + mddev->dev_sectors
290 + (PAGE_SIZE/512)))
291 goto bad_alignment;
292 } else if (offset < 0) {
293 /* DATA BITMAP METADATA */
294 if (offset
295 + (long)(page->index * (PAGE_SIZE/512))
296 + size/512 > 0)
297 /* bitmap runs in to metadata */
298 goto bad_alignment;
299 if (rdev->data_offset + mddev->dev_sectors
300 > rdev->sb_start + offset)
301 /* data runs in to bitmap */
302 goto bad_alignment;
303 } else if (rdev->sb_start < rdev->data_offset) {
304 /* METADATA BITMAP DATA */
305 if (rdev->sb_start
306 + offset
307 + page->index*(PAGE_SIZE/512) + size/512
308 > rdev->data_offset)
309 /* bitmap runs in to data */
310 goto bad_alignment;
311 } else {
312 /* DATA METADATA BITMAP - no problems */
313 }
314 md_super_write(mddev, rdev,
315 rdev->sb_start + offset
316 + page->index * (PAGE_SIZE/512),
317 size,
318 page);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000319 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700320
321 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800322 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700323 return 0;
NeilBrown4b809912008-07-21 17:05:25 +1000324
325 bad_alignment:
NeilBrown4b809912008-07-21 17:05:25 +1000326 return -EINVAL;
NeilBrowna654b9d82005-06-21 17:17:27 -0700327}
328
NeilBrown4ad13662007-07-17 04:06:13 -0700329static void bitmap_file_kick(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700330/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700331 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700332 */
NeilBrown4ad13662007-07-17 04:06:13 -0700333static void write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700334{
NeilBrownd785a062006-06-26 00:27:48 -0700335 struct buffer_head *bh;
NeilBrown32a76272005-06-21 17:17:14 -0700336
NeilBrownf0d76d72007-07-17 04:06:12 -0700337 if (bitmap->file == NULL) {
338 switch (write_sb_page(bitmap, page, wait)) {
339 case -EINVAL:
340 bitmap->flags |= BITMAP_WRITE_ERROR;
NeilBrownf0d76d72007-07-17 04:06:12 -0700341 }
NeilBrown4ad13662007-07-17 04:06:13 -0700342 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700343
NeilBrown4ad13662007-07-17 04:06:13 -0700344 bh = page_buffers(page);
NeilBrownc7084432006-01-06 00:20:45 -0800345
NeilBrown4ad13662007-07-17 04:06:13 -0700346 while (bh && bh->b_blocknr) {
347 atomic_inc(&bitmap->pending_writes);
348 set_buffer_locked(bh);
349 set_buffer_mapped(bh);
NeilBrownbe2a2652010-10-27 15:37:41 +1100350 submit_bh(WRITE | REQ_UNPLUG | REQ_SYNC, bh);
NeilBrown4ad13662007-07-17 04:06:13 -0700351 bh = bh->b_this_page;
352 }
NeilBrown32a76272005-06-21 17:17:14 -0700353
NeilBrownac2f40b2010-06-01 19:37:31 +1000354 if (wait)
NeilBrown4ad13662007-07-17 04:06:13 -0700355 wait_event(bitmap->write_wait,
356 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700357 }
NeilBrown4ad13662007-07-17 04:06:13 -0700358 if (bitmap->flags & BITMAP_WRITE_ERROR)
359 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700360}
361
NeilBrownd785a062006-06-26 00:27:48 -0700362static void end_bitmap_write(struct buffer_head *bh, int uptodate)
NeilBrown32a76272005-06-21 17:17:14 -0700363{
NeilBrownd785a062006-06-26 00:27:48 -0700364 struct bitmap *bitmap = bh->b_private;
365 unsigned long flags;
366
367 if (!uptodate) {
368 spin_lock_irqsave(&bitmap->lock, flags);
369 bitmap->flags |= BITMAP_WRITE_ERROR;
370 spin_unlock_irqrestore(&bitmap->lock, flags);
371 }
372 if (atomic_dec_and_test(&bitmap->pending_writes))
373 wake_up(&bitmap->write_wait);
374}
375
376/* copied from buffer.c */
377static void
378__clear_page_buffers(struct page *page)
379{
380 ClearPagePrivate(page);
381 set_page_private(page, 0);
382 page_cache_release(page);
383}
384static void free_buffers(struct page *page)
385{
386 struct buffer_head *bh = page_buffers(page);
387
388 while (bh) {
389 struct buffer_head *next = bh->b_this_page;
390 free_buffer_head(bh);
391 bh = next;
392 }
393 __clear_page_buffers(page);
394 put_page(page);
395}
396
397/* read a page from a file.
398 * We both read the page, and attach buffers to the page to record the
399 * address of each block (using bmap). These addresses will be used
400 * to write the block later, completely bypassing the filesystem.
401 * This usage is similar to how swap files are handled, and allows us
402 * to write to a file with no concerns of memory allocation failing.
403 */
404static struct page *read_page(struct file *file, unsigned long index,
405 struct bitmap *bitmap,
406 unsigned long count)
407{
NeilBrown32a76272005-06-21 17:17:14 -0700408 struct page *page = NULL;
Josef Sipekc649bb92006-12-08 02:37:19 -0800409 struct inode *inode = file->f_path.dentry->d_inode;
NeilBrownd785a062006-06-26 00:27:48 -0700410 struct buffer_head *bh;
411 sector_t block;
NeilBrown32a76272005-06-21 17:17:14 -0700412
NeilBrownac2f40b2010-06-01 19:37:31 +1000413 PRINTK("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
NeilBrown2d1f3b52006-01-06 00:20:31 -0800414 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700415
NeilBrownd785a062006-06-26 00:27:48 -0700416 page = alloc_page(GFP_KERNEL);
417 if (!page)
418 page = ERR_PTR(-ENOMEM);
NeilBrown32a76272005-06-21 17:17:14 -0700419 if (IS_ERR(page))
420 goto out;
NeilBrownd785a062006-06-26 00:27:48 -0700421
NeilBrownd785a062006-06-26 00:27:48 -0700422 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
423 if (!bh) {
424 put_page(page);
425 page = ERR_PTR(-ENOMEM);
426 goto out;
427 }
428 attach_page_buffers(page, bh);
429 block = index << (PAGE_SHIFT - inode->i_blkbits);
430 while (bh) {
431 if (count == 0)
432 bh->b_blocknr = 0;
433 else {
434 bh->b_blocknr = bmap(inode, block);
435 if (bh->b_blocknr == 0) {
436 /* Cannot use this file! */
437 free_buffers(page);
438 page = ERR_PTR(-EINVAL);
439 goto out;
440 }
441 bh->b_bdev = inode->i_sb->s_bdev;
442 if (count < (1<<inode->i_blkbits))
443 count = 0;
444 else
445 count -= (1<<inode->i_blkbits);
NeilBrown32a76272005-06-21 17:17:14 -0700446
NeilBrownd785a062006-06-26 00:27:48 -0700447 bh->b_end_io = end_bitmap_write;
448 bh->b_private = bitmap;
NeilBrownce25c312006-06-26 00:27:49 -0700449 atomic_inc(&bitmap->pending_writes);
450 set_buffer_locked(bh);
451 set_buffer_mapped(bh);
452 submit_bh(READ, bh);
NeilBrownd785a062006-06-26 00:27:48 -0700453 }
454 block++;
455 bh = bh->b_this_page;
456 }
NeilBrownd785a062006-06-26 00:27:48 -0700457 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700458
459 wait_event(bitmap->write_wait,
460 atomic_read(&bitmap->pending_writes)==0);
461 if (bitmap->flags & BITMAP_WRITE_ERROR) {
462 free_buffers(page);
463 page = ERR_PTR(-EIO);
464 }
NeilBrown32a76272005-06-21 17:17:14 -0700465out:
466 if (IS_ERR(page))
NeilBrownac2f40b2010-06-01 19:37:31 +1000467 printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800468 (int)PAGE_SIZE,
469 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700470 PTR_ERR(page));
471 return page;
472}
473
474/*
475 * bitmap file superblock operations
476 */
477
478/* update the event counter and sync the superblock to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700479void bitmap_update_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700480{
481 bitmap_super_t *sb;
482 unsigned long flags;
483
484 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
NeilBrown4ad13662007-07-17 04:06:13 -0700485 return;
NeilBrownece5cff2009-12-14 12:49:56 +1100486 if (bitmap->mddev->bitmap_info.external)
487 return;
NeilBrown32a76272005-06-21 17:17:14 -0700488 spin_lock_irqsave(&bitmap->lock, flags);
489 if (!bitmap->sb_page) { /* no superblock */
490 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700491 return;
NeilBrown32a76272005-06-21 17:17:14 -0700492 }
NeilBrown32a76272005-06-21 17:17:14 -0700493 spin_unlock_irqrestore(&bitmap->lock, flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100494 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700495 sb->events = cpu_to_le64(bitmap->mddev->events);
Neil Browna0da84f2008-06-28 08:31:22 +1000496 if (bitmap->mddev->events < bitmap->events_cleared) {
497 /* rocking back to read-only */
498 bitmap->events_cleared = bitmap->mddev->events;
499 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
500 }
NeilBrown43a70502009-12-14 12:49:55 +1100501 /* Just in case these have been changed via sysfs: */
502 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
503 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
NeilBrownea03aff2006-01-06 00:20:34 -0800504 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700505 write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700506}
507
508/* print out the bitmap file superblock */
509void bitmap_print_sb(struct bitmap *bitmap)
510{
511 bitmap_super_t *sb;
512
513 if (!bitmap || !bitmap->sb_page)
514 return;
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100515 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700516 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700517 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
518 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
519 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700520 *(__u32 *)(sb->uuid+0),
521 *(__u32 *)(sb->uuid+4),
522 *(__u32 *)(sb->uuid+8),
523 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700524 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700525 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700526 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700527 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700528 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
529 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
530 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
531 printk(KERN_DEBUG " sync size: %llu KB\n",
532 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700533 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800534 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700535}
536
537/* read the superblock from the bitmap file and initialize some bitmap fields */
538static int bitmap_read_sb(struct bitmap *bitmap)
539{
540 char *reason = NULL;
541 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700542 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700543 unsigned long long events;
544 int err = -EINVAL;
545
546 /* page 0 is the superblock, read it... */
NeilBrownf49d5e62007-01-26 00:57:03 -0800547 if (bitmap->file) {
548 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
549 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
550
551 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
552 } else {
NeilBrown42a04b52009-12-14 12:49:53 +1100553 bitmap->sb_page = read_sb_page(bitmap->mddev,
554 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100555 NULL,
556 0, sizeof(bitmap_super_t));
NeilBrowna654b9d82005-06-21 17:17:27 -0700557 }
NeilBrown32a76272005-06-21 17:17:14 -0700558 if (IS_ERR(bitmap->sb_page)) {
559 err = PTR_ERR(bitmap->sb_page);
560 bitmap->sb_page = NULL;
561 return err;
562 }
563
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100564 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700565
NeilBrown32a76272005-06-21 17:17:14 -0700566 chunksize = le32_to_cpu(sb->chunksize);
NeilBrown1b04be92009-12-14 12:49:53 +1100567 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
NeilBrown4b6d2872005-09-09 16:23:47 -0700568 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700569
570 /* verify that the bitmap-specific fields are valid */
571 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
572 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800573 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
574 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700575 reason = "unrecognized superblock version";
NeilBrown1187cf02009-03-31 14:27:02 +1100576 else if (chunksize < 512)
NeilBrown7dd5d342006-01-06 00:20:39 -0800577 reason = "bitmap chunksize too small";
NeilBrown32a76272005-06-21 17:17:14 -0700578 else if ((1 << ffz(~chunksize)) != chunksize)
579 reason = "bitmap chunksize not a power of 2";
NeilBrown1b04be92009-12-14 12:49:53 +1100580 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
NeilBrown7dd5d342006-01-06 00:20:39 -0800581 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700582 else if (write_behind > COUNTER_MAX)
583 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700584 if (reason) {
585 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
586 bmname(bitmap), reason);
587 goto out;
588 }
589
590 /* keep the array size field of the bitmap superblock up to date */
591 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
592
593 if (!bitmap->mddev->persistent)
594 goto success;
595
596 /*
597 * if we have a persistent array superblock, compare the
598 * bitmap's UUID and event counter to the mddev's
599 */
600 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
601 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
602 bmname(bitmap));
603 goto out;
604 }
605 events = le64_to_cpu(sb->events);
606 if (events < bitmap->mddev->events) {
607 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
608 "-- forcing full recovery\n", bmname(bitmap), events,
609 (unsigned long long) bitmap->mddev->events);
NeilBrown4f2e6392006-10-21 10:24:09 -0700610 sb->state |= cpu_to_le32(BITMAP_STALE);
NeilBrown32a76272005-06-21 17:17:14 -0700611 }
612success:
613 /* assign fields using values from superblock */
NeilBrown42a04b52009-12-14 12:49:53 +1100614 bitmap->mddev->bitmap_info.chunksize = chunksize;
615 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
NeilBrown42a04b52009-12-14 12:49:53 +1100616 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
NeilBrown4f2e6392006-10-21 10:24:09 -0700617 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800618 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
619 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700620 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown4f2e6392006-10-21 10:24:09 -0700621 if (sb->state & cpu_to_le32(BITMAP_STALE))
NeilBrown6a079972005-09-09 16:23:44 -0700622 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700623 err = 0;
624out:
NeilBrownea03aff2006-01-06 00:20:34 -0800625 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700626 if (err)
627 bitmap_print_sb(bitmap);
628 return err;
629}
630
631enum bitmap_mask_op {
632 MASK_SET,
633 MASK_UNSET
634};
635
NeilBrown4ad13662007-07-17 04:06:13 -0700636/* record the state of the bitmap in the superblock. Return the old value */
637static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
638 enum bitmap_mask_op op)
NeilBrown32a76272005-06-21 17:17:14 -0700639{
640 bitmap_super_t *sb;
641 unsigned long flags;
NeilBrown4ad13662007-07-17 04:06:13 -0700642 int old;
NeilBrown32a76272005-06-21 17:17:14 -0700643
644 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800645 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700646 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700647 return 0;
NeilBrown32a76272005-06-21 17:17:14 -0700648 }
NeilBrown32a76272005-06-21 17:17:14 -0700649 spin_unlock_irqrestore(&bitmap->lock, flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100650 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700651 old = le32_to_cpu(sb->state) & bits;
NeilBrown32a76272005-06-21 17:17:14 -0700652 switch (op) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000653 case MASK_SET:
654 sb->state |= cpu_to_le32(bits);
655 break;
656 case MASK_UNSET:
657 sb->state &= cpu_to_le32(~bits);
658 break;
659 default:
660 BUG();
NeilBrown32a76272005-06-21 17:17:14 -0700661 }
NeilBrownea03aff2006-01-06 00:20:34 -0800662 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700663 return old;
NeilBrown32a76272005-06-21 17:17:14 -0700664}
665
666/*
667 * general bitmap file operations
668 */
669
NeilBrownece5cff2009-12-14 12:49:56 +1100670/*
671 * on-disk bitmap:
672 *
673 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
674 * file a page at a time. There's a superblock at the start of the file.
675 */
NeilBrown32a76272005-06-21 17:17:14 -0700676/* calculate the index of the page that contains this bit */
NeilBrownece5cff2009-12-14 12:49:56 +1100677static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700678{
NeilBrownece5cff2009-12-14 12:49:56 +1100679 if (!bitmap->mddev->bitmap_info.external)
680 chunk += sizeof(bitmap_super_t) << 3;
681 return chunk >> PAGE_BIT_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700682}
683
684/* calculate the (bit) offset of this bit within a page */
NeilBrownece5cff2009-12-14 12:49:56 +1100685static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700686{
NeilBrownece5cff2009-12-14 12:49:56 +1100687 if (!bitmap->mddev->bitmap_info.external)
688 chunk += sizeof(bitmap_super_t) << 3;
689 return chunk & (PAGE_BITS - 1);
NeilBrown32a76272005-06-21 17:17:14 -0700690}
691
692/*
693 * return a pointer to the page in the filemap that contains the given bit
694 *
695 * this lookup is complicated by the fact that the bitmap sb might be exactly
696 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
697 * 0 or page 1
698 */
699static inline struct page *filemap_get_page(struct bitmap *bitmap,
700 unsigned long chunk)
701{
NeilBrowne384e582010-06-01 19:37:34 +1000702 if (bitmap->filemap == NULL)
703 return NULL;
NeilBrownac2f40b2010-06-01 19:37:31 +1000704 if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
705 return NULL;
NeilBrownece5cff2009-12-14 12:49:56 +1100706 return bitmap->filemap[file_page_index(bitmap, chunk)
707 - file_page_index(bitmap, 0)];
NeilBrown32a76272005-06-21 17:17:14 -0700708}
709
NeilBrown32a76272005-06-21 17:17:14 -0700710static void bitmap_file_unmap(struct bitmap *bitmap)
711{
712 struct page **map, *sb_page;
713 unsigned long *attr;
714 int pages;
715 unsigned long flags;
716
717 spin_lock_irqsave(&bitmap->lock, flags);
718 map = bitmap->filemap;
719 bitmap->filemap = NULL;
720 attr = bitmap->filemap_attr;
721 bitmap->filemap_attr = NULL;
722 pages = bitmap->file_pages;
723 bitmap->file_pages = 0;
724 sb_page = bitmap->sb_page;
725 bitmap->sb_page = NULL;
726 spin_unlock_irqrestore(&bitmap->lock, flags);
727
728 while (pages--)
NeilBrownece5cff2009-12-14 12:49:56 +1100729 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700730 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700731 kfree(map);
732 kfree(attr);
733
NeilBrownd785a062006-06-26 00:27:48 -0700734 if (sb_page)
735 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700736}
737
738static void bitmap_file_put(struct bitmap *bitmap)
739{
740 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700741 unsigned long flags;
742
743 spin_lock_irqsave(&bitmap->lock, flags);
744 file = bitmap->file;
745 bitmap->file = NULL;
746 spin_unlock_irqrestore(&bitmap->lock, flags);
747
NeilBrownd785a062006-06-26 00:27:48 -0700748 if (file)
749 wait_event(bitmap->write_wait,
750 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700751 bitmap_file_unmap(bitmap);
752
NeilBrownd785a062006-06-26 00:27:48 -0700753 if (file) {
Josef Sipekc649bb92006-12-08 02:37:19 -0800754 struct inode *inode = file->f_path.dentry->d_inode;
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800755 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700756 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700757 }
NeilBrown32a76272005-06-21 17:17:14 -0700758}
759
NeilBrown32a76272005-06-21 17:17:14 -0700760/*
761 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
762 * then it is no longer reliable, so we stop using it and we mark the file
763 * as failed in the superblock
764 */
765static void bitmap_file_kick(struct bitmap *bitmap)
766{
767 char *path, *ptr = NULL;
768
NeilBrown4ad13662007-07-17 04:06:13 -0700769 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
770 bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700771
NeilBrown4ad13662007-07-17 04:06:13 -0700772 if (bitmap->file) {
773 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
774 if (path)
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700775 ptr = d_path(&bitmap->file->f_path, path,
776 PAGE_SIZE);
777
NeilBrown4ad13662007-07-17 04:06:13 -0700778 printk(KERN_ALERT
779 "%s: kicking failed bitmap file %s from array!\n",
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700780 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700781
NeilBrown4ad13662007-07-17 04:06:13 -0700782 kfree(path);
783 } else
784 printk(KERN_ALERT
785 "%s: disabling internal bitmap due to errors\n",
786 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700787 }
NeilBrown32a76272005-06-21 17:17:14 -0700788
789 bitmap_file_put(bitmap);
790
791 return;
792}
793
794enum bitmap_page_attr {
NeilBrownac2f40b2010-06-01 19:37:31 +1000795 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
796 BITMAP_PAGE_CLEAN = 1, /* there are bits that might need to be cleared */
797 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
NeilBrown32a76272005-06-21 17:17:14 -0700798};
799
800static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
801 enum bitmap_page_attr attr)
802{
NeilBrowne384e582010-06-01 19:37:34 +1000803 if (page)
804 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
805 else
806 __set_bit(attr, &bitmap->logattrs);
NeilBrown32a76272005-06-21 17:17:14 -0700807}
808
809static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
810 enum bitmap_page_attr attr)
811{
NeilBrowne384e582010-06-01 19:37:34 +1000812 if (page)
813 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
814 else
815 __clear_bit(attr, &bitmap->logattrs);
NeilBrown32a76272005-06-21 17:17:14 -0700816}
817
NeilBrownec7a3192006-06-26 00:27:45 -0700818static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
819 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700820{
NeilBrowne384e582010-06-01 19:37:34 +1000821 if (page)
822 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
823 else
824 return test_bit(attr, &bitmap->logattrs);
NeilBrown32a76272005-06-21 17:17:14 -0700825}
826
827/*
828 * bitmap_file_set_bit -- called before performing a write to the md device
829 * to set (and eventually sync) a particular bit in the bitmap file
830 *
831 * we set the bit immediately, then we record the page number so that
832 * when an unplug occurs, we can flush the dirty pages out to disk
833 */
834static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
835{
836 unsigned long bit;
NeilBrowne384e582010-06-01 19:37:34 +1000837 struct page *page = NULL;
NeilBrown32a76272005-06-21 17:17:14 -0700838 void *kaddr;
839 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
840
NeilBrowne384e582010-06-01 19:37:34 +1000841 if (!bitmap->filemap) {
842 struct dm_dirty_log *log = bitmap->mddev->bitmap_info.log;
843 if (log)
844 log->type->mark_region(log, chunk);
845 } else {
NeilBrown32a76272005-06-21 17:17:14 -0700846
NeilBrowne384e582010-06-01 19:37:34 +1000847 page = filemap_get_page(bitmap, chunk);
848 if (!page)
849 return;
850 bit = file_page_offset(bitmap, chunk);
NeilBrown32a76272005-06-21 17:17:14 -0700851
NeilBrowne384e582010-06-01 19:37:34 +1000852 /* set the bit */
853 kaddr = kmap_atomic(page, KM_USER0);
854 if (bitmap->flags & BITMAP_HOSTENDIAN)
855 set_bit(bit, kaddr);
856 else
857 ext2_set_bit(bit, kaddr);
858 kunmap_atomic(kaddr, KM_USER0);
859 PRINTK("set file bit %lu page %lu\n", bit, page->index);
860 }
NeilBrown32a76272005-06-21 17:17:14 -0700861 /* record page number so it gets flushed to disk when unplug occurs */
862 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
NeilBrown32a76272005-06-21 17:17:14 -0700863}
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;
NeilBrowne384e582010-06-01 19:37:34 +1000877 if (!bitmap->filemap) {
878 /* Must be using a dirty_log */
879 struct dm_dirty_log *log = bitmap->mddev->bitmap_info.log;
880 dirty = test_and_clear_bit(BITMAP_PAGE_DIRTY, &bitmap->logattrs);
881 need_write = test_and_clear_bit(BITMAP_PAGE_NEEDWRITE, &bitmap->logattrs);
882 if (dirty || need_write)
883 if (log->type->flush(log))
884 bitmap->flags |= BITMAP_WRITE_ERROR;
885 goto out;
886 }
NeilBrown32a76272005-06-21 17:17:14 -0700887
888 /* look at each page to see if there are any set bits that need to be
889 * flushed out to disk */
890 for (i = 0; i < bitmap->file_pages; i++) {
891 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700892 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700893 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700894 return;
NeilBrown32a76272005-06-21 17:17:14 -0700895 }
896 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700897 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
898 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700899 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
900 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700901 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700902 wait = 1;
903 spin_unlock_irqrestore(&bitmap->lock, flags);
904
NeilBrownac2f40b2010-06-01 19:37:31 +1000905 if (dirty || need_write)
NeilBrown4ad13662007-07-17 04:06:13 -0700906 write_page(bitmap, page, 0);
NeilBrown32a76272005-06-21 17:17:14 -0700907 }
908 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700909 if (bitmap->file)
NeilBrownd785a062006-06-26 00:27:48 -0700910 wait_event(bitmap->write_wait,
911 atomic_read(&bitmap->pending_writes)==0);
NeilBrown0b79ccf2006-06-26 00:27:44 -0700912 else
NeilBrowna9701a32005-11-08 21:39:34 -0800913 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700914 }
NeilBrowne384e582010-06-01 19:37:34 +1000915out:
NeilBrownd785a062006-06-26 00:27:48 -0700916 if (bitmap->flags & BITMAP_WRITE_ERROR)
917 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700918}
NeilBrownac2f40b2010-06-01 19:37:31 +1000919EXPORT_SYMBOL(bitmap_unplug);
NeilBrown32a76272005-06-21 17:17:14 -0700920
NeilBrown6a079972005-09-09 16:23:44 -0700921static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700922/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
923 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
924 * memory mapping of the bitmap file
925 * Special cases:
926 * if there's no bitmap file, or if the bitmap file had been
927 * previously kicked from the array, we mark all the bits as
928 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700929 *
930 * We ignore all bits for sectors that end earlier than 'start'.
931 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700932 */
NeilBrown6a079972005-09-09 16:23:44 -0700933static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700934{
935 unsigned long i, chunks, index, oldindex, bit;
936 struct page *page = NULL, *oldpage = NULL;
937 unsigned long num_pages, bit_cnt = 0;
938 struct file *file;
NeilBrownd785a062006-06-26 00:27:48 -0700939 unsigned long bytes, offset;
NeilBrown32a76272005-06-21 17:17:14 -0700940 int outofdate;
941 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800942 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700943
944 chunks = bitmap->chunks;
945 file = bitmap->file;
946
NeilBrown42a04b52009-12-14 12:49:53 +1100947 BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
NeilBrown32a76272005-06-21 17:17:14 -0700948
Olaf Hering44456d32005-07-27 11:45:17 -0700949#ifdef INJECT_FAULTS_3
NeilBrown32a76272005-06-21 17:17:14 -0700950 outofdate = 1;
951#else
952 outofdate = bitmap->flags & BITMAP_STALE;
953#endif
954 if (outofdate)
955 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
956 "recovery\n", bmname(bitmap));
957
NeilBrowne384e582010-06-01 19:37:34 +1000958 bytes = DIV_ROUND_UP(bitmap->chunks, 8);
NeilBrownece5cff2009-12-14 12:49:56 +1100959 if (!bitmap->mddev->bitmap_info.external)
960 bytes += sizeof(bitmap_super_t);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700961
NeilBrowne384e582010-06-01 19:37:34 +1000962 num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700963
NeilBrownece5cff2009-12-14 12:49:56 +1100964 if (file && i_size_read(file->f_mapping->host) < bytes) {
NeilBrown32a76272005-06-21 17:17:14 -0700965 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
966 bmname(bitmap),
967 (unsigned long) i_size_read(file->f_mapping->host),
NeilBrownece5cff2009-12-14 12:49:56 +1100968 bytes);
NeilBrown4ad13662007-07-17 04:06:13 -0700969 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700970 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700971
972 ret = -ENOMEM;
973
NeilBrown32a76272005-06-21 17:17:14 -0700974 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700975 if (!bitmap->filemap)
NeilBrown4ad13662007-07-17 04:06:13 -0700976 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700977
NeilBrowne16b68b2006-06-26 00:27:45 -0700978 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
979 bitmap->filemap_attr = kzalloc(
NeilBrownac2f40b2010-06-01 19:37:31 +1000980 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
NeilBrowne16b68b2006-06-26 00:27:45 -0700981 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700982 if (!bitmap->filemap_attr)
NeilBrown4ad13662007-07-17 04:06:13 -0700983 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700984
NeilBrown32a76272005-06-21 17:17:14 -0700985 oldindex = ~0L;
986
987 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -0800988 int b;
NeilBrownece5cff2009-12-14 12:49:56 +1100989 index = file_page_index(bitmap, i);
990 bit = file_page_offset(bitmap, i);
NeilBrown32a76272005-06-21 17:17:14 -0700991 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -0700992 int count;
NeilBrown32a76272005-06-21 17:17:14 -0700993 /* unmap the old page, we're done with it */
NeilBrownd785a062006-06-26 00:27:48 -0700994 if (index == num_pages-1)
NeilBrownece5cff2009-12-14 12:49:56 +1100995 count = bytes - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -0700996 else
997 count = PAGE_SIZE;
NeilBrownece5cff2009-12-14 12:49:56 +1100998 if (index == 0 && bitmap->sb_page) {
NeilBrown32a76272005-06-21 17:17:14 -0700999 /*
1000 * if we're here then the superblock page
1001 * contains some bits (PAGE_SIZE != sizeof sb)
1002 * we've already read it in, so just use it
1003 */
1004 page = bitmap->sb_page;
1005 offset = sizeof(bitmap_super_t);
NeilBrown53845272009-01-09 08:31:05 +11001006 if (!file)
Vasiliy Kulikov5c04f552010-10-01 14:18:12 -07001007 page = read_sb_page(
1008 bitmap->mddev,
1009 bitmap->mddev->bitmap_info.offset,
1010 page,
1011 index, count);
NeilBrowna654b9d82005-06-21 17:17:27 -07001012 } else if (file) {
NeilBrownd785a062006-06-26 00:27:48 -07001013 page = read_page(file, index, bitmap, count);
NeilBrowna654b9d82005-06-21 17:17:27 -07001014 offset = 0;
1015 } else {
NeilBrown42a04b52009-12-14 12:49:53 +11001016 page = read_sb_page(bitmap->mddev,
1017 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +11001018 NULL,
1019 index, count);
NeilBrown32a76272005-06-21 17:17:14 -07001020 offset = 0;
1021 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001022 if (IS_ERR(page)) { /* read error */
1023 ret = PTR_ERR(page);
NeilBrown4ad13662007-07-17 04:06:13 -07001024 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -07001025 }
1026
NeilBrown32a76272005-06-21 17:17:14 -07001027 oldindex = index;
1028 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -07001029
NeilBrownb74fd282009-05-07 12:47:19 +10001030 bitmap->filemap[bitmap->file_pages++] = page;
1031 bitmap->last_page_size = count;
1032
NeilBrown32a76272005-06-21 17:17:14 -07001033 if (outofdate) {
1034 /*
1035 * if bitmap is out of date, dirty the
NeilBrownac2f40b2010-06-01 19:37:31 +10001036 * whole page and write it out
NeilBrown32a76272005-06-21 17:17:14 -07001037 */
NeilBrownea03aff2006-01-06 00:20:34 -08001038 paddr = kmap_atomic(page, KM_USER0);
1039 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -07001040 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -08001041 kunmap_atomic(paddr, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -07001042 write_page(bitmap, page, 1);
1043
1044 ret = -EIO;
NeilBrownb74fd282009-05-07 12:47:19 +10001045 if (bitmap->flags & BITMAP_WRITE_ERROR)
NeilBrown4ad13662007-07-17 04:06:13 -07001046 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001047 }
NeilBrown32a76272005-06-21 17:17:14 -07001048 }
NeilBrownea03aff2006-01-06 00:20:34 -08001049 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001050 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001051 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001052 else
NeilBrownea03aff2006-01-06 00:20:34 -08001053 b = ext2_test_bit(bit, paddr);
1054 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001055 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -07001056 /* if the disk bit is set, set the memory bit */
NeilBrowndb305e52009-05-07 12:49:06 +10001057 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1058 >= start);
1059 bitmap_set_memory_bits(bitmap,
1060 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1061 needed);
NeilBrown32a76272005-06-21 17:17:14 -07001062 bit_cnt++;
NeilBrown6a079972005-09-09 16:23:44 -07001063 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -07001064 }
NeilBrown32a76272005-06-21 17:17:14 -07001065 }
1066
NeilBrownac2f40b2010-06-01 19:37:31 +10001067 /* everything went OK */
NeilBrown32a76272005-06-21 17:17:14 -07001068 ret = 0;
1069 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1070
NeilBrown32a76272005-06-21 17:17:14 -07001071 if (bit_cnt) { /* Kick recovery if any bits were set */
1072 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1073 md_wakeup_thread(bitmap->mddev->thread);
1074 }
1075
NeilBrown32a76272005-06-21 17:17:14 -07001076 printk(KERN_INFO "%s: bitmap initialized from disk: "
NeilBrown4ad13662007-07-17 04:06:13 -07001077 "read %lu/%lu pages, set %lu bits\n",
1078 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
NeilBrown32a76272005-06-21 17:17:14 -07001079
NeilBrown4ad13662007-07-17 04:06:13 -07001080 return 0;
1081
1082 err:
1083 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1084 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001085 return ret;
1086}
1087
NeilBrowna654b9d82005-06-21 17:17:27 -07001088void bitmap_write_all(struct bitmap *bitmap)
1089{
1090 /* We don't actually write all bitmap blocks here,
1091 * just flag them as needing to be written
1092 */
NeilBrownec7a3192006-06-26 00:27:45 -07001093 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001094
NeilBrownac2f40b2010-06-01 19:37:31 +10001095 for (i = 0; i < bitmap->file_pages; i++)
NeilBrownec7a3192006-06-26 00:27:45 -07001096 set_page_attr(bitmap, bitmap->filemap[i],
1097 BITMAP_PAGE_NEEDWRITE);
NeilBrowna654b9d82005-06-21 17:17:27 -07001098}
1099
NeilBrown32a76272005-06-21 17:17:14 -07001100static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1101{
1102 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1103 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1104 bitmap->bp[page].count += inc;
NeilBrown32a76272005-06-21 17:17:14 -07001105 bitmap_checkfree(bitmap, page);
1106}
1107static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001108 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001109 int create);
1110
1111/*
1112 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1113 * out to disk
1114 */
1115
NeilBrownaa5cbd12009-12-14 12:49:46 +11001116void bitmap_daemon_work(mddev_t *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001117{
NeilBrownaa5cbd12009-12-14 12:49:46 +11001118 struct bitmap *bitmap;
NeilBrownaa3163f2005-06-21 17:17:22 -07001119 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001120 unsigned long flags;
1121 struct page *page = NULL, *lastpage = NULL;
NeilBrown57dab0b2010-10-19 10:03:39 +11001122 sector_t blocks;
NeilBrownea03aff2006-01-06 00:20:34 -08001123 void *paddr;
NeilBrowne384e582010-06-01 19:37:34 +10001124 struct dm_dirty_log *log = mddev->bitmap_info.log;
NeilBrown32a76272005-06-21 17:17:14 -07001125
NeilBrownaa5cbd12009-12-14 12:49:46 +11001126 /* Use a mutex to guard daemon_work against
1127 * bitmap_destroy.
1128 */
NeilBrownc3d97142009-12-14 12:49:52 +11001129 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrownaa5cbd12009-12-14 12:49:46 +11001130 bitmap = mddev->bitmap;
1131 if (bitmap == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001132 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown4ad13662007-07-17 04:06:13 -07001133 return;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001134 }
NeilBrown42a04b52009-12-14 12:49:53 +11001135 if (time_before(jiffies, bitmap->daemon_lastrun
NeilBrown1b04be92009-12-14 12:49:53 +11001136 + bitmap->mddev->bitmap_info.daemon_sleep))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001137 goto done;
1138
NeilBrown32a76272005-06-21 17:17:14 -07001139 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001140 if (bitmap->allclean) {
1141 bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001142 goto done;
NeilBrown8311c292008-03-04 14:29:30 -08001143 }
1144 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001145
NeilBrownbe512692009-05-26 09:41:17 +10001146 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001147 for (j = 0; j < bitmap->chunks; j++) {
1148 bitmap_counter_t *bmc;
NeilBrowne384e582010-06-01 19:37:34 +10001149 if (!bitmap->filemap) {
1150 if (!log)
1151 /* error or shutdown */
1152 break;
1153 } else
1154 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001155
1156 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001157 /* skip this page unless it's marked as needing cleaning */
NeilBrownec7a3192006-06-26 00:27:45 -07001158 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1159 int need_write = test_page_attr(bitmap, page,
1160 BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001161 if (need_write)
NeilBrownaa3163f2005-06-21 17:17:22 -07001162 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001163
NeilBrownaa3163f2005-06-21 17:17:22 -07001164 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown8311c292008-03-04 14:29:30 -08001165 if (need_write) {
NeilBrown4ad13662007-07-17 04:06:13 -07001166 write_page(bitmap, page, 0);
NeilBrown8311c292008-03-04 14:29:30 -08001167 bitmap->allclean = 0;
1168 }
NeilBrownbe512692009-05-26 09:41:17 +10001169 spin_lock_irqsave(&bitmap->lock, flags);
1170 j |= (PAGE_BITS - 1);
NeilBrownaa3163f2005-06-21 17:17:22 -07001171 continue;
1172 }
1173
NeilBrown32a76272005-06-21 17:17:14 -07001174 /* grab the new page, sync and release the old */
NeilBrown32a76272005-06-21 17:17:14 -07001175 if (lastpage != NULL) {
NeilBrownec7a3192006-06-26 00:27:45 -07001176 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001177 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1178 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001179 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001180 } else {
1181 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1182 spin_unlock_irqrestore(&bitmap->lock, flags);
1183 }
NeilBrown32a76272005-06-21 17:17:14 -07001184 } else
1185 spin_unlock_irqrestore(&bitmap->lock, flags);
1186 lastpage = page;
Neil Browna0da84f2008-06-28 08:31:22 +10001187
1188 /* We are possibly going to clear some bits, so make
1189 * sure that events_cleared is up-to-date.
1190 */
NeilBrownece5cff2009-12-14 12:49:56 +11001191 if (bitmap->need_sync &&
1192 bitmap->mddev->bitmap_info.external == 0) {
Neil Browna0da84f2008-06-28 08:31:22 +10001193 bitmap_super_t *sb;
1194 bitmap->need_sync = 0;
1195 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1196 sb->events_cleared =
1197 cpu_to_le64(bitmap->events_cleared);
1198 kunmap_atomic(sb, KM_USER0);
1199 write_page(bitmap, bitmap->sb_page, 1);
1200 }
NeilBrown32a76272005-06-21 17:17:14 -07001201 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownece5cff2009-12-14 12:49:56 +11001202 if (!bitmap->need_sync)
1203 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -07001204 }
NeilBrowndb305e52009-05-07 12:49:06 +10001205 bmc = bitmap_get_counter(bitmap,
1206 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1207 &blocks, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001208 if (bmc) {
NeilBrown8311c292008-03-04 14:29:30 -08001209 if (*bmc)
1210 bitmap->allclean = 0;
1211
NeilBrown32a76272005-06-21 17:17:14 -07001212 if (*bmc == 2) {
NeilBrownac2f40b2010-06-01 19:37:31 +10001213 *bmc = 1; /* maybe clear the bit next time */
NeilBrown32a76272005-06-21 17:17:14 -07001214 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
NeilBrownece5cff2009-12-14 12:49:56 +11001215 } else if (*bmc == 1 && !bitmap->need_sync) {
NeilBrown32a76272005-06-21 17:17:14 -07001216 /* we can clear the bit */
1217 *bmc = 0;
NeilBrowndb305e52009-05-07 12:49:06 +10001218 bitmap_count_page(bitmap,
1219 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
NeilBrown32a76272005-06-21 17:17:14 -07001220 -1);
1221
1222 /* clear the bit */
NeilBrowne384e582010-06-01 19:37:34 +10001223 if (page) {
1224 paddr = kmap_atomic(page, KM_USER0);
1225 if (bitmap->flags & BITMAP_HOSTENDIAN)
1226 clear_bit(file_page_offset(bitmap, j),
1227 paddr);
1228 else
1229 ext2_clear_bit(file_page_offset(bitmap, j),
1230 paddr);
1231 kunmap_atomic(paddr, KM_USER0);
1232 } else
1233 log->type->clear_region(log, j);
NeilBrown32a76272005-06-21 17:17:14 -07001234 }
NeilBrownbe512692009-05-26 09:41:17 +10001235 } else
1236 j |= PAGE_COUNTER_MASK;
NeilBrown32a76272005-06-21 17:17:14 -07001237 }
NeilBrownbe512692009-05-26 09:41:17 +10001238 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001239
1240 /* now sync the final page */
NeilBrowne384e582010-06-01 19:37:34 +10001241 if (lastpage != NULL || log != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001242 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001243 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001244 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1245 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrowne384e582010-06-01 19:37:34 +10001246 if (lastpage)
1247 write_page(bitmap, lastpage, 0);
1248 else
1249 if (log->type->flush(log))
1250 bitmap->flags |= BITMAP_WRITE_ERROR;
NeilBrown32a76272005-06-21 17:17:14 -07001251 } else {
1252 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1253 spin_unlock_irqrestore(&bitmap->lock, flags);
1254 }
NeilBrown32a76272005-06-21 17:17:14 -07001255 }
1256
NeilBrown7be3dfe2008-03-10 11:43:48 -07001257 done:
NeilBrown8311c292008-03-04 14:29:30 -08001258 if (bitmap->allclean == 0)
NeilBrownac2f40b2010-06-01 19:37:31 +10001259 bitmap->mddev->thread->timeout =
NeilBrown1b04be92009-12-14 12:49:53 +11001260 bitmap->mddev->bitmap_info.daemon_sleep;
NeilBrownc3d97142009-12-14 12:49:52 +11001261 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown32a76272005-06-21 17:17:14 -07001262}
1263
NeilBrown32a76272005-06-21 17:17:14 -07001264static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001265 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001266 int create)
NeilBrownee305ac2009-09-23 18:06:44 +10001267__releases(bitmap->lock)
1268__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -07001269{
1270 /* If 'create', we might release the lock and reclaim it.
1271 * The lock must have been taken with interrupts enabled.
1272 * If !create, we don't release the lock.
1273 */
1274 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1275 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1276 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1277 sector_t csize;
NeilBrownef425672010-06-01 19:37:33 +10001278 int err;
NeilBrown32a76272005-06-21 17:17:14 -07001279
NeilBrownef425672010-06-01 19:37:33 +10001280 err = bitmap_checkpage(bitmap, page, create);
1281
1282 if (bitmap->bp[page].hijacked ||
1283 bitmap->bp[page].map == NULL)
1284 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1285 PAGE_COUNTER_SHIFT - 1);
1286 else
NeilBrown32a76272005-06-21 17:17:14 -07001287 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
NeilBrownef425672010-06-01 19:37:33 +10001288 *blocks = csize - (offset & (csize - 1));
1289
1290 if (err < 0)
NeilBrown32a76272005-06-21 17:17:14 -07001291 return NULL;
NeilBrownef425672010-06-01 19:37:33 +10001292
NeilBrown32a76272005-06-21 17:17:14 -07001293 /* now locked ... */
1294
1295 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1296 /* should we use the first or second counter field
1297 * of the hijacked pointer? */
1298 int hi = (pageoff > PAGE_COUNTER_MASK);
NeilBrown32a76272005-06-21 17:17:14 -07001299 return &((bitmap_counter_t *)
1300 &bitmap->bp[page].map)[hi];
NeilBrownef425672010-06-01 19:37:33 +10001301 } else /* page is allocated */
NeilBrown32a76272005-06-21 17:17:14 -07001302 return (bitmap_counter_t *)
1303 &(bitmap->bp[page].map[pageoff]);
NeilBrown32a76272005-06-21 17:17:14 -07001304}
1305
NeilBrown4b6d2872005-09-09 16:23:47 -07001306int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001307{
NeilBrownac2f40b2010-06-01 19:37:31 +10001308 if (!bitmap)
1309 return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001310
1311 if (behind) {
Paul Clements696fcd52010-03-08 16:02:37 +11001312 int bw;
NeilBrown4b6d2872005-09-09 16:23:47 -07001313 atomic_inc(&bitmap->behind_writes);
Paul Clements696fcd52010-03-08 16:02:37 +11001314 bw = atomic_read(&bitmap->behind_writes);
1315 if (bw > bitmap->behind_writes_used)
1316 bitmap->behind_writes_used = bw;
1317
NeilBrown4b6d2872005-09-09 16:23:47 -07001318 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
Paul Clements696fcd52010-03-08 16:02:37 +11001319 bw, bitmap->max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001320 }
1321
NeilBrown32a76272005-06-21 17:17:14 -07001322 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001323 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001324 bitmap_counter_t *bmc;
1325
1326 spin_lock_irq(&bitmap->lock);
1327 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1328 if (!bmc) {
1329 spin_unlock_irq(&bitmap->lock);
1330 return 0;
1331 }
1332
Neil Brownda6e1a32007-02-08 14:20:37 -08001333 if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
1334 DEFINE_WAIT(__wait);
1335 /* note that it is safe to do the prepare_to_wait
1336 * after the test as long as we do it before dropping
1337 * the spinlock.
1338 */
1339 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1340 TASK_UNINTERRUPTIBLE);
1341 spin_unlock_irq(&bitmap->lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001342 io_schedule();
Neil Brownda6e1a32007-02-08 14:20:37 -08001343 finish_wait(&bitmap->overflow_wait, &__wait);
1344 continue;
1345 }
1346
NeilBrownac2f40b2010-06-01 19:37:31 +10001347 switch (*bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001348 case 0:
1349 bitmap_file_set_bit(bitmap, offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001350 bitmap_count_page(bitmap, offset, 1);
NeilBrown32a76272005-06-21 17:17:14 -07001351 /* fall through */
1352 case 1:
1353 *bmc = 2;
1354 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001355
NeilBrown32a76272005-06-21 17:17:14 -07001356 (*bmc)++;
1357
1358 spin_unlock_irq(&bitmap->lock);
1359
1360 offset += blocks;
1361 if (sectors > blocks)
1362 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001363 else
1364 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001365 }
NeilBrown8311c292008-03-04 14:29:30 -08001366 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001367 return 0;
1368}
NeilBrownac2f40b2010-06-01 19:37:31 +10001369EXPORT_SYMBOL(bitmap_startwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001370
1371void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001372 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001373{
NeilBrownac2f40b2010-06-01 19:37:31 +10001374 if (!bitmap)
1375 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001376 if (behind) {
NeilBrowne5551902010-03-31 11:21:44 +11001377 if (atomic_dec_and_test(&bitmap->behind_writes))
1378 wake_up(&bitmap->behind_wait);
NeilBrown4b6d2872005-09-09 16:23:47 -07001379 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1380 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1381 }
NeilBrownd0a4bb42009-03-31 14:27:02 +11001382 if (bitmap->mddev->degraded)
1383 /* Never clear bits or update events_cleared when degraded */
1384 success = 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001385
NeilBrown32a76272005-06-21 17:17:14 -07001386 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001387 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001388 unsigned long flags;
1389 bitmap_counter_t *bmc;
1390
1391 spin_lock_irqsave(&bitmap->lock, flags);
1392 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1393 if (!bmc) {
1394 spin_unlock_irqrestore(&bitmap->lock, flags);
1395 return;
1396 }
1397
Neil Browna0da84f2008-06-28 08:31:22 +10001398 if (success &&
1399 bitmap->events_cleared < bitmap->mddev->events) {
1400 bitmap->events_cleared = bitmap->mddev->events;
1401 bitmap->need_sync = 1;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001402 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
Neil Browna0da84f2008-06-28 08:31:22 +10001403 }
1404
NeilBrown32a76272005-06-21 17:17:14 -07001405 if (!success && ! (*bmc & NEEDED_MASK))
1406 *bmc |= NEEDED_MASK;
1407
Neil Brownda6e1a32007-02-08 14:20:37 -08001408 if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
1409 wake_up(&bitmap->overflow_wait);
1410
NeilBrown32a76272005-06-21 17:17:14 -07001411 (*bmc)--;
NeilBrownac2f40b2010-06-01 19:37:31 +10001412 if (*bmc <= 2)
NeilBrown32a76272005-06-21 17:17:14 -07001413 set_page_attr(bitmap,
NeilBrowne384e582010-06-01 19:37:34 +10001414 filemap_get_page(
1415 bitmap,
1416 offset >> CHUNK_BLOCK_SHIFT(bitmap)),
NeilBrown32a76272005-06-21 17:17:14 -07001417 BITMAP_PAGE_CLEAN);
NeilBrownac2f40b2010-06-01 19:37:31 +10001418
NeilBrown32a76272005-06-21 17:17:14 -07001419 spin_unlock_irqrestore(&bitmap->lock, flags);
1420 offset += blocks;
1421 if (sectors > blocks)
1422 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001423 else
1424 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001425 }
1426}
NeilBrownac2f40b2010-06-01 19:37:31 +10001427EXPORT_SYMBOL(bitmap_endwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001428
NeilBrown57dab0b2010-10-19 10:03:39 +11001429static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001430 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001431{
1432 bitmap_counter_t *bmc;
1433 int rv;
1434 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1435 *blocks = 1024;
1436 return 1; /* always resync if no bitmap */
1437 }
1438 spin_lock_irq(&bitmap->lock);
1439 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1440 rv = 0;
1441 if (bmc) {
1442 /* locked */
1443 if (RESYNC(*bmc))
1444 rv = 1;
1445 else if (NEEDED(*bmc)) {
1446 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001447 if (!degraded) { /* don't set/clear bits if degraded */
1448 *bmc |= RESYNC_MASK;
1449 *bmc &= ~NEEDED_MASK;
1450 }
NeilBrown32a76272005-06-21 17:17:14 -07001451 }
1452 }
1453 spin_unlock_irq(&bitmap->lock);
NeilBrown8311c292008-03-04 14:29:30 -08001454 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001455 return rv;
1456}
1457
NeilBrown57dab0b2010-10-19 10:03:39 +11001458int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001459 int degraded)
1460{
1461 /* bitmap_start_sync must always report on multiples of whole
1462 * pages, otherwise resync (which is very PAGE_SIZE based) will
1463 * get confused.
1464 * So call __bitmap_start_sync repeatedly (if needed) until
1465 * At least PAGE_SIZE>>9 blocks are covered.
1466 * Return the 'or' of the result.
1467 */
1468 int rv = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001469 sector_t blocks1;
NeilBrown1187cf02009-03-31 14:27:02 +11001470
1471 *blocks = 0;
1472 while (*blocks < (PAGE_SIZE>>9)) {
1473 rv |= __bitmap_start_sync(bitmap, offset,
1474 &blocks1, degraded);
1475 offset += blocks1;
1476 *blocks += blocks1;
1477 }
1478 return rv;
1479}
NeilBrownac2f40b2010-06-01 19:37:31 +10001480EXPORT_SYMBOL(bitmap_start_sync);
NeilBrown1187cf02009-03-31 14:27:02 +11001481
NeilBrown57dab0b2010-10-19 10:03:39 +11001482void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
NeilBrown32a76272005-06-21 17:17:14 -07001483{
1484 bitmap_counter_t *bmc;
1485 unsigned long flags;
NeilBrownac2f40b2010-06-01 19:37:31 +10001486
1487 if (bitmap == NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001488 *blocks = 1024;
1489 return;
1490 }
1491 spin_lock_irqsave(&bitmap->lock, flags);
1492 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1493 if (bmc == NULL)
1494 goto unlock;
1495 /* locked */
NeilBrown32a76272005-06-21 17:17:14 -07001496 if (RESYNC(*bmc)) {
1497 *bmc &= ~RESYNC_MASK;
1498
1499 if (!NEEDED(*bmc) && aborted)
1500 *bmc |= NEEDED_MASK;
1501 else {
NeilBrownac2f40b2010-06-01 19:37:31 +10001502 if (*bmc <= 2)
NeilBrown32a76272005-06-21 17:17:14 -07001503 set_page_attr(bitmap,
1504 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1505 BITMAP_PAGE_CLEAN);
NeilBrown32a76272005-06-21 17:17:14 -07001506 }
1507 }
1508 unlock:
1509 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown8311c292008-03-04 14:29:30 -08001510 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001511}
NeilBrownac2f40b2010-06-01 19:37:31 +10001512EXPORT_SYMBOL(bitmap_end_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001513
1514void bitmap_close_sync(struct bitmap *bitmap)
1515{
1516 /* Sync has finished, and any bitmap chunks that weren't synced
1517 * properly have been aborted. It remains to us to clear the
1518 * RESYNC bit wherever it is still on
1519 */
1520 sector_t sector = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001521 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001522 if (!bitmap)
1523 return;
NeilBrown32a76272005-06-21 17:17:14 -07001524 while (sector < bitmap->mddev->resync_max_sectors) {
1525 bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001526 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001527 }
1528}
NeilBrownac2f40b2010-06-01 19:37:31 +10001529EXPORT_SYMBOL(bitmap_close_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001530
NeilBrownb47490c2008-02-06 01:39:50 -08001531void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1532{
1533 sector_t s = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001534 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001535
1536 if (!bitmap)
1537 return;
1538 if (sector == 0) {
1539 bitmap->last_end_sync = jiffies;
1540 return;
1541 }
1542 if (time_before(jiffies, (bitmap->last_end_sync
NeilBrown1b04be92009-12-14 12:49:53 +11001543 + bitmap->mddev->bitmap_info.daemon_sleep)))
NeilBrownb47490c2008-02-06 01:39:50 -08001544 return;
1545 wait_event(bitmap->mddev->recovery_wait,
1546 atomic_read(&bitmap->mddev->recovery_active) == 0);
1547
NeilBrown75d3da42011-01-14 09:14:34 +11001548 bitmap->mddev->curr_resync_completed = sector;
NeilBrown070dc6d2010-08-30 17:33:34 +10001549 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
NeilBrownb47490c2008-02-06 01:39:50 -08001550 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1551 s = 0;
1552 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1553 bitmap_end_sync(bitmap, s, &blocks, 0);
1554 s += blocks;
1555 }
1556 bitmap->last_end_sync = jiffies;
NeilBrownacb180b2009-04-14 16:28:34 +10001557 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
NeilBrownb47490c2008-02-06 01:39:50 -08001558}
NeilBrownac2f40b2010-06-01 19:37:31 +10001559EXPORT_SYMBOL(bitmap_cond_end_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08001560
NeilBrown6a079972005-09-09 16:23:44 -07001561static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001562{
1563 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001564 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001565 * be 0 at this point
1566 */
NeilBrown193f1c92005-08-04 12:53:33 -07001567
NeilBrown57dab0b2010-10-19 10:03:39 +11001568 sector_t secs;
NeilBrown193f1c92005-08-04 12:53:33 -07001569 bitmap_counter_t *bmc;
1570 spin_lock_irq(&bitmap->lock);
1571 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1572 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001573 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001574 return;
NeilBrown32a76272005-06-21 17:17:14 -07001575 }
NeilBrownac2f40b2010-06-01 19:37:31 +10001576 if (!*bmc) {
NeilBrown193f1c92005-08-04 12:53:33 -07001577 struct page *page;
NeilBrownac2f40b2010-06-01 19:37:31 +10001578 *bmc = 1 | (needed ? NEEDED_MASK : 0);
NeilBrown193f1c92005-08-04 12:53:33 -07001579 bitmap_count_page(bitmap, offset, 1);
1580 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1581 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1582 }
1583 spin_unlock_irq(&bitmap->lock);
NeilBrown8311c292008-03-04 14:29:30 -08001584 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001585}
1586
Paul Clements9b1d1da2006-10-03 01:15:49 -07001587/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1588void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1589{
1590 unsigned long chunk;
1591
1592 for (chunk = s; chunk <= e; chunk++) {
NeilBrowndb305e52009-05-07 12:49:06 +10001593 sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001594 bitmap_set_memory_bits(bitmap, sec, 1);
1595 bitmap_file_set_bit(bitmap, sec);
NeilBrownffa23322009-12-14 12:49:56 +11001596 if (sec < bitmap->mddev->recovery_cp)
1597 /* We are asserting that the array is dirty,
1598 * so move the recovery_cp address back so
1599 * that it is obvious that it is dirty
1600 */
1601 bitmap->mddev->recovery_cp = sec;
Paul Clements9b1d1da2006-10-03 01:15:49 -07001602 }
1603}
1604
NeilBrown32a76272005-06-21 17:17:14 -07001605/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001606 * flush out any pending updates
1607 */
1608void bitmap_flush(mddev_t *mddev)
1609{
1610 struct bitmap *bitmap = mddev->bitmap;
NeilBrown42a04b52009-12-14 12:49:53 +11001611 long sleep;
NeilBrown6b8b3e82005-08-04 12:53:35 -07001612
1613 if (!bitmap) /* there was no bitmap */
1614 return;
1615
1616 /* run the daemon_work three time to ensure everything is flushed
1617 * that can be
1618 */
NeilBrown1b04be92009-12-14 12:49:53 +11001619 sleep = mddev->bitmap_info.daemon_sleep * 2;
NeilBrown42a04b52009-12-14 12:49:53 +11001620 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001621 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001622 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001623 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001624 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001625 bitmap_daemon_work(mddev);
NeilBrown6b8b3e82005-08-04 12:53:35 -07001626 bitmap_update_sb(bitmap);
1627}
1628
1629/*
NeilBrown32a76272005-06-21 17:17:14 -07001630 * free memory that was allocated
1631 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001632static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001633{
1634 unsigned long k, pages;
1635 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001636
1637 if (!bitmap) /* there was no bitmap */
1638 return;
1639
NeilBrown32a76272005-06-21 17:17:14 -07001640 /* release the bitmap file and kill the daemon */
1641 bitmap_file_put(bitmap);
1642
1643 bp = bitmap->bp;
1644 pages = bitmap->pages;
1645
1646 /* free all allocated memory */
1647
NeilBrown32a76272005-06-21 17:17:14 -07001648 if (bp) /* deallocate the page memory */
1649 for (k = 0; k < pages; k++)
1650 if (bp[k].map && !bp[k].hijacked)
1651 kfree(bp[k].map);
1652 kfree(bp);
1653 kfree(bitmap);
1654}
NeilBrownaa5cbd12009-12-14 12:49:46 +11001655
NeilBrown3178b0d2005-09-09 16:23:50 -07001656void bitmap_destroy(mddev_t *mddev)
1657{
1658 struct bitmap *bitmap = mddev->bitmap;
1659
1660 if (!bitmap) /* there was no bitmap */
1661 return;
1662
NeilBrownc3d97142009-12-14 12:49:52 +11001663 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrown3178b0d2005-09-09 16:23:50 -07001664 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownc3d97142009-12-14 12:49:52 +11001665 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrownb15c2e52006-01-06 00:20:16 -08001666 if (mddev->thread)
1667 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001668
NeilBrownece5cff2009-12-14 12:49:56 +11001669 if (bitmap->sysfs_can_clear)
1670 sysfs_put(bitmap->sysfs_can_clear);
1671
NeilBrown3178b0d2005-09-09 16:23:50 -07001672 bitmap_free(bitmap);
1673}
NeilBrown32a76272005-06-21 17:17:14 -07001674
1675/*
1676 * initialize the bitmap structure
1677 * if this returns an error, bitmap_destroy must be called to do clean up
1678 */
1679int bitmap_create(mddev_t *mddev)
1680{
1681 struct bitmap *bitmap;
NeilBrown1f593902009-04-20 11:50:24 +10001682 sector_t blocks = mddev->resync_max_sectors;
NeilBrown32a76272005-06-21 17:17:14 -07001683 unsigned long chunks;
1684 unsigned long pages;
NeilBrownc3d97142009-12-14 12:49:52 +11001685 struct file *file = mddev->bitmap_info.file;
NeilBrown32a76272005-06-21 17:17:14 -07001686 int err;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001687 struct sysfs_dirent *bm = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001688
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001689 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001690
NeilBrowne384e582010-06-01 19:37:34 +10001691 if (!file
1692 && !mddev->bitmap_info.offset
1693 && !mddev->bitmap_info.log) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001694 return 0;
1695
NeilBrownc3d97142009-12-14 12:49:52 +11001696 BUG_ON(file && mddev->bitmap_info.offset);
NeilBrowne384e582010-06-01 19:37:34 +10001697 BUG_ON(mddev->bitmap_info.offset && mddev->bitmap_info.log);
NeilBrowna654b9d82005-06-21 17:17:27 -07001698
NeilBrown9ffae0c2006-01-06 00:20:32 -08001699 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001700 if (!bitmap)
1701 return -ENOMEM;
1702
NeilBrown32a76272005-06-21 17:17:14 -07001703 spin_lock_init(&bitmap->lock);
NeilBrownce25c312006-06-26 00:27:49 -07001704 atomic_set(&bitmap->pending_writes, 0);
1705 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001706 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrowne5551902010-03-31 11:21:44 +11001707 init_waitqueue_head(&bitmap->behind_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001708
NeilBrown32a76272005-06-21 17:17:14 -07001709 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001710
NeilBrown5ff5aff2010-06-01 19:37:32 +10001711 if (mddev->kobj.sd)
1712 bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
NeilBrownece5cff2009-12-14 12:49:56 +11001713 if (bm) {
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07001714 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
NeilBrownece5cff2009-12-14 12:49:56 +11001715 sysfs_put(bm);
1716 } else
1717 bitmap->sysfs_can_clear = NULL;
1718
NeilBrown32a76272005-06-21 17:17:14 -07001719 bitmap->file = file;
NeilBrownce25c312006-06-26 00:27:49 -07001720 if (file) {
1721 get_file(file);
NeilBrownae8fa282009-10-16 15:56:01 +11001722 /* As future accesses to this file will use bmap,
1723 * and bypass the page cache, we must sync the file
1724 * first.
1725 */
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001726 vfs_fsync(file, 1);
NeilBrownce25c312006-06-26 00:27:49 -07001727 }
NeilBrown42a04b52009-12-14 12:49:53 +11001728 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
NeilBrownece5cff2009-12-14 12:49:56 +11001729 if (!mddev->bitmap_info.external)
1730 err = bitmap_read_sb(bitmap);
1731 else {
1732 err = 0;
1733 if (mddev->bitmap_info.chunksize == 0 ||
1734 mddev->bitmap_info.daemon_sleep == 0)
1735 /* chunksize and time_base need to be
1736 * set first. */
1737 err = -EINVAL;
1738 }
NeilBrown32a76272005-06-21 17:17:14 -07001739 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001740 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001741
NeilBrown624ce4f2009-12-14 12:49:56 +11001742 bitmap->daemon_lastrun = jiffies;
NeilBrown42a04b52009-12-14 12:49:53 +11001743 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
NeilBrown32a76272005-06-21 17:17:14 -07001744
1745 /* now that chunksize and chunkshift are set, we can use these macros */
NeilBrownac2f40b2010-06-01 19:37:31 +10001746 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
NeilBrown1f593902009-04-20 11:50:24 +10001747 CHUNK_BLOCK_SHIFT(bitmap);
NeilBrownac2f40b2010-06-01 19:37:31 +10001748 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
NeilBrown32a76272005-06-21 17:17:14 -07001749
1750 BUG_ON(!pages);
1751
1752 bitmap->chunks = chunks;
1753 bitmap->pages = pages;
1754 bitmap->missing_pages = pages;
1755 bitmap->counter_bits = COUNTER_BITS;
1756
1757 bitmap->syncchunk = ~0UL;
1758
Olaf Hering44456d32005-07-27 11:45:17 -07001759#ifdef INJECT_FATAL_FAULT_1
NeilBrown32a76272005-06-21 17:17:14 -07001760 bitmap->bp = NULL;
1761#else
NeilBrown9ffae0c2006-01-06 00:20:32 -08001762 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001763#endif
NeilBrown3178b0d2005-09-09 16:23:50 -07001764 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001765 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001766 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001767
NeilBrown69e51b42010-06-01 19:37:35 +10001768 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1769 pages, bmname(bitmap));
1770
1771 mddev->bitmap = bitmap;
1772
1773
1774 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
1775
1776 error:
1777 bitmap_free(bitmap);
1778 return err;
1779}
1780
1781int bitmap_load(mddev_t *mddev)
1782{
1783 int err = 0;
1784 sector_t sector = 0;
1785 struct bitmap *bitmap = mddev->bitmap;
1786
1787 if (!bitmap)
1788 goto out;
1789
1790 /* Clear out old bitmap info first: Either there is none, or we
1791 * are resuming after someone else has possibly changed things,
1792 * so we should forget old cached info.
1793 * All chunks should be clean, but some might need_sync.
1794 */
1795 while (sector < mddev->resync_max_sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001796 sector_t blocks;
NeilBrown69e51b42010-06-01 19:37:35 +10001797 bitmap_start_sync(bitmap, sector, &blocks, 0);
1798 sector += blocks;
1799 }
1800 bitmap_close_sync(bitmap);
1801
NeilBrowne384e582010-06-01 19:37:34 +10001802 if (mddev->bitmap_info.log) {
1803 unsigned long i;
1804 struct dm_dirty_log *log = mddev->bitmap_info.log;
1805 for (i = 0; i < bitmap->chunks; i++)
1806 if (!log->type->in_sync(log, i, 1))
1807 bitmap_set_memory_bits(bitmap,
1808 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1809 1);
NeilBrown69e51b42010-06-01 19:37:35 +10001810 } else {
1811 sector_t start = 0;
1812 if (mddev->degraded == 0
1813 || bitmap->events_cleared == mddev->events)
1814 /* no need to keep dirty bits to optimise a
1815 * re-add of a missing device */
1816 start = mddev->recovery_cp;
1817
NeilBrowne384e582010-06-01 19:37:34 +10001818 err = bitmap_init_from_disk(bitmap, start);
NeilBrown69e51b42010-06-01 19:37:35 +10001819 }
NeilBrown32a76272005-06-21 17:17:14 -07001820 if (err)
NeilBrown69e51b42010-06-01 19:37:35 +10001821 goto out;
NeilBrown3178b0d2005-09-09 16:23:50 -07001822
NeilBrown1b04be92009-12-14 12:49:53 +11001823 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
NeilBrown9cd30fd2009-12-14 12:49:54 +11001824 md_wakeup_thread(mddev->thread);
NeilBrownb15c2e52006-01-06 00:20:16 -08001825
NeilBrown4ad13662007-07-17 04:06:13 -07001826 bitmap_update_sb(bitmap);
1827
NeilBrown69e51b42010-06-01 19:37:35 +10001828 if (bitmap->flags & BITMAP_WRITE_ERROR)
1829 err = -EIO;
1830out:
NeilBrown3178b0d2005-09-09 16:23:50 -07001831 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001832}
NeilBrown69e51b42010-06-01 19:37:35 +10001833EXPORT_SYMBOL_GPL(bitmap_load);
NeilBrown32a76272005-06-21 17:17:14 -07001834
NeilBrown43a70502009-12-14 12:49:55 +11001835static ssize_t
1836location_show(mddev_t *mddev, char *page)
1837{
1838 ssize_t len;
NeilBrownac2f40b2010-06-01 19:37:31 +10001839 if (mddev->bitmap_info.file)
NeilBrown43a70502009-12-14 12:49:55 +11001840 len = sprintf(page, "file");
NeilBrownac2f40b2010-06-01 19:37:31 +10001841 else if (mddev->bitmap_info.offset)
NeilBrown43a70502009-12-14 12:49:55 +11001842 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001843 else
NeilBrown43a70502009-12-14 12:49:55 +11001844 len = sprintf(page, "none");
1845 len += sprintf(page+len, "\n");
1846 return len;
1847}
1848
1849static ssize_t
1850location_store(mddev_t *mddev, const char *buf, size_t len)
1851{
1852
1853 if (mddev->pers) {
1854 if (!mddev->pers->quiesce)
1855 return -EBUSY;
1856 if (mddev->recovery || mddev->sync_thread)
1857 return -EBUSY;
1858 }
1859
1860 if (mddev->bitmap || mddev->bitmap_info.file ||
1861 mddev->bitmap_info.offset) {
1862 /* bitmap already configured. Only option is to clear it */
1863 if (strncmp(buf, "none", 4) != 0)
1864 return -EBUSY;
1865 if (mddev->pers) {
1866 mddev->pers->quiesce(mddev, 1);
1867 bitmap_destroy(mddev);
1868 mddev->pers->quiesce(mddev, 0);
1869 }
1870 mddev->bitmap_info.offset = 0;
1871 if (mddev->bitmap_info.file) {
1872 struct file *f = mddev->bitmap_info.file;
1873 mddev->bitmap_info.file = NULL;
1874 restore_bitmap_write_access(f);
1875 fput(f);
1876 }
1877 } else {
1878 /* No bitmap, OK to set a location */
1879 long long offset;
1880 if (strncmp(buf, "none", 4) == 0)
1881 /* nothing to be done */;
1882 else if (strncmp(buf, "file:", 5) == 0) {
1883 /* Not supported yet */
1884 return -EINVAL;
1885 } else {
1886 int rv;
1887 if (buf[0] == '+')
1888 rv = strict_strtoll(buf+1, 10, &offset);
1889 else
1890 rv = strict_strtoll(buf, 10, &offset);
1891 if (rv)
1892 return rv;
1893 if (offset == 0)
1894 return -EINVAL;
NeilBrownece5cff2009-12-14 12:49:56 +11001895 if (mddev->bitmap_info.external == 0 &&
1896 mddev->major_version == 0 &&
NeilBrown43a70502009-12-14 12:49:55 +11001897 offset != mddev->bitmap_info.default_offset)
1898 return -EINVAL;
1899 mddev->bitmap_info.offset = offset;
1900 if (mddev->pers) {
1901 mddev->pers->quiesce(mddev, 1);
1902 rv = bitmap_create(mddev);
1903 if (rv) {
1904 bitmap_destroy(mddev);
1905 mddev->bitmap_info.offset = 0;
1906 }
1907 mddev->pers->quiesce(mddev, 0);
1908 if (rv)
1909 return rv;
1910 }
1911 }
1912 }
1913 if (!mddev->external) {
1914 /* Ensure new bitmap info is stored in
1915 * metadata promptly.
1916 */
1917 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1918 md_wakeup_thread(mddev->thread);
1919 }
1920 return len;
1921}
1922
1923static struct md_sysfs_entry bitmap_location =
1924__ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
1925
1926static ssize_t
1927timeout_show(mddev_t *mddev, char *page)
1928{
1929 ssize_t len;
1930 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
1931 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
NeilBrownac2f40b2010-06-01 19:37:31 +10001932
NeilBrown43a70502009-12-14 12:49:55 +11001933 len = sprintf(page, "%lu", secs);
1934 if (jifs)
1935 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
1936 len += sprintf(page+len, "\n");
1937 return len;
1938}
1939
1940static ssize_t
1941timeout_store(mddev_t *mddev, const char *buf, size_t len)
1942{
1943 /* timeout can be set at any time */
1944 unsigned long timeout;
1945 int rv = strict_strtoul_scaled(buf, &timeout, 4);
1946 if (rv)
1947 return rv;
1948
1949 /* just to make sure we don't overflow... */
1950 if (timeout >= LONG_MAX / HZ)
1951 return -EINVAL;
1952
1953 timeout = timeout * HZ / 10000;
1954
1955 if (timeout >= MAX_SCHEDULE_TIMEOUT)
1956 timeout = MAX_SCHEDULE_TIMEOUT-1;
1957 if (timeout < 1)
1958 timeout = 1;
1959 mddev->bitmap_info.daemon_sleep = timeout;
1960 if (mddev->thread) {
1961 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1962 * the bitmap is all clean and we don't need to
1963 * adjust the timeout right now
1964 */
1965 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
1966 mddev->thread->timeout = timeout;
1967 md_wakeup_thread(mddev->thread);
1968 }
1969 }
1970 return len;
1971}
1972
1973static struct md_sysfs_entry bitmap_timeout =
1974__ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
1975
1976static ssize_t
1977backlog_show(mddev_t *mddev, char *page)
1978{
1979 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
1980}
1981
1982static ssize_t
1983backlog_store(mddev_t *mddev, const char *buf, size_t len)
1984{
1985 unsigned long backlog;
1986 int rv = strict_strtoul(buf, 10, &backlog);
1987 if (rv)
1988 return rv;
1989 if (backlog > COUNTER_MAX)
1990 return -EINVAL;
1991 mddev->bitmap_info.max_write_behind = backlog;
1992 return len;
1993}
1994
1995static struct md_sysfs_entry bitmap_backlog =
1996__ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
1997
1998static ssize_t
1999chunksize_show(mddev_t *mddev, char *page)
2000{
2001 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
2002}
2003
2004static ssize_t
2005chunksize_store(mddev_t *mddev, const char *buf, size_t len)
2006{
2007 /* Can only be changed when no bitmap is active */
2008 int rv;
2009 unsigned long csize;
2010 if (mddev->bitmap)
2011 return -EBUSY;
2012 rv = strict_strtoul(buf, 10, &csize);
2013 if (rv)
2014 return rv;
2015 if (csize < 512 ||
2016 !is_power_of_2(csize))
2017 return -EINVAL;
2018 mddev->bitmap_info.chunksize = csize;
2019 return len;
2020}
2021
2022static struct md_sysfs_entry bitmap_chunksize =
2023__ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
2024
NeilBrownece5cff2009-12-14 12:49:56 +11002025static ssize_t metadata_show(mddev_t *mddev, char *page)
2026{
2027 return sprintf(page, "%s\n", (mddev->bitmap_info.external
2028 ? "external" : "internal"));
2029}
2030
2031static ssize_t metadata_store(mddev_t *mddev, const char *buf, size_t len)
2032{
2033 if (mddev->bitmap ||
2034 mddev->bitmap_info.file ||
2035 mddev->bitmap_info.offset)
2036 return -EBUSY;
2037 if (strncmp(buf, "external", 8) == 0)
2038 mddev->bitmap_info.external = 1;
2039 else if (strncmp(buf, "internal", 8) == 0)
2040 mddev->bitmap_info.external = 0;
2041 else
2042 return -EINVAL;
2043 return len;
2044}
2045
2046static struct md_sysfs_entry bitmap_metadata =
2047__ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2048
2049static ssize_t can_clear_show(mddev_t *mddev, char *page)
2050{
2051 int len;
2052 if (mddev->bitmap)
2053 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
2054 "false" : "true"));
2055 else
2056 len = sprintf(page, "\n");
2057 return len;
2058}
2059
2060static ssize_t can_clear_store(mddev_t *mddev, const char *buf, size_t len)
2061{
2062 if (mddev->bitmap == NULL)
2063 return -ENOENT;
2064 if (strncmp(buf, "false", 5) == 0)
2065 mddev->bitmap->need_sync = 1;
2066 else if (strncmp(buf, "true", 4) == 0) {
2067 if (mddev->degraded)
2068 return -EBUSY;
2069 mddev->bitmap->need_sync = 0;
2070 } else
2071 return -EINVAL;
2072 return len;
2073}
2074
2075static struct md_sysfs_entry bitmap_can_clear =
2076__ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
2077
Paul Clements696fcd52010-03-08 16:02:37 +11002078static ssize_t
2079behind_writes_used_show(mddev_t *mddev, char *page)
2080{
2081 if (mddev->bitmap == NULL)
2082 return sprintf(page, "0\n");
2083 return sprintf(page, "%lu\n",
2084 mddev->bitmap->behind_writes_used);
2085}
2086
2087static ssize_t
2088behind_writes_used_reset(mddev_t *mddev, const char *buf, size_t len)
2089{
2090 if (mddev->bitmap)
2091 mddev->bitmap->behind_writes_used = 0;
2092 return len;
2093}
2094
2095static struct md_sysfs_entry max_backlog_used =
2096__ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2097 behind_writes_used_show, behind_writes_used_reset);
2098
NeilBrown43a70502009-12-14 12:49:55 +11002099static struct attribute *md_bitmap_attrs[] = {
2100 &bitmap_location.attr,
2101 &bitmap_timeout.attr,
2102 &bitmap_backlog.attr,
2103 &bitmap_chunksize.attr,
NeilBrownece5cff2009-12-14 12:49:56 +11002104 &bitmap_metadata.attr,
2105 &bitmap_can_clear.attr,
Paul Clements696fcd52010-03-08 16:02:37 +11002106 &max_backlog_used.attr,
NeilBrown43a70502009-12-14 12:49:55 +11002107 NULL
2108};
2109struct attribute_group md_bitmap_group = {
2110 .name = "bitmap",
2111 .attrs = md_bitmap_attrs,
2112};
2113