blob: fcf3c9465fd894f924a9510dcf59640e70c4c905 [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>
NeilBrown57148962012-03-19 12:46:40 +110029#include <linux/seq_file.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110030#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110031#include "bitmap.h"
NeilBrown32a76272005-06-21 17:17:14 -070032
NeilBrownac2f40b2010-06-01 19:37:31 +100033static inline char *bmname(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -070034{
35 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
36}
37
NeilBrown32a76272005-06-21 17:17:14 -070038/*
39 * just a placeholder - calls kmalloc for bitmap pages
40 */
41static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
42{
43 unsigned char *page;
44
NeilBrownac2f40b2010-06-01 19:37:31 +100045 page = kzalloc(PAGE_SIZE, GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -070046 if (!page)
47 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
48 else
NeilBrown36a4e1f2011-10-07 14:23:17 +110049 pr_debug("%s: bitmap_alloc_page: allocated page at %p\n",
50 bmname(bitmap), page);
NeilBrown32a76272005-06-21 17:17:14 -070051 return page;
52}
53
54/*
55 * for now just a placeholder -- just calls kfree for bitmap pages
56 */
57static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
58{
NeilBrown36a4e1f2011-10-07 14:23:17 +110059 pr_debug("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
NeilBrown32a76272005-06-21 17:17:14 -070060 kfree(page);
61}
62
63/*
64 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
65 *
66 * 1) check to see if this page is allocated, if it's not then try to alloc
67 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
68 * page pointer directly as a counter
69 *
70 * if we find our page, we increment the page's refcount so that it stays
71 * allocated while we're using it
72 */
NeilBrownac2f40b2010-06-01 19:37:31 +100073static int bitmap_checkpage(struct bitmap *bitmap,
74 unsigned long page, int create)
NeilBrownee305ac2009-09-23 18:06:44 +100075__releases(bitmap->lock)
76__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -070077{
78 unsigned char *mappage;
79
80 if (page >= bitmap->pages) {
NeilBrown1187cf02009-03-31 14:27:02 +110081 /* This can happen if bitmap_start_sync goes beyond
82 * End-of-device while looking for a whole page.
83 * It is harmless.
84 */
NeilBrown32a76272005-06-21 17:17:14 -070085 return -EINVAL;
86 }
87
NeilBrown32a76272005-06-21 17:17:14 -070088 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
89 return 0;
90
91 if (bitmap->bp[page].map) /* page is already allocated, just return */
92 return 0;
93
94 if (!create)
95 return -ENOENT;
96
NeilBrown32a76272005-06-21 17:17:14 -070097 /* this page has not been allocated yet */
98
NeilBrownac2f40b2010-06-01 19:37:31 +100099 spin_unlock_irq(&bitmap->lock);
100 mappage = bitmap_alloc_page(bitmap);
101 spin_lock_irq(&bitmap->lock);
102
103 if (mappage == NULL) {
NeilBrown36a4e1f2011-10-07 14:23:17 +1100104 pr_debug("%s: bitmap map page allocation failed, hijacking\n",
105 bmname(bitmap));
NeilBrown32a76272005-06-21 17:17:14 -0700106 /* failed - set the hijacked flag so that we can use the
107 * pointer as a counter */
NeilBrown32a76272005-06-21 17:17:14 -0700108 if (!bitmap->bp[page].map)
109 bitmap->bp[page].hijacked = 1;
NeilBrownac2f40b2010-06-01 19:37:31 +1000110 } else if (bitmap->bp[page].map ||
111 bitmap->bp[page].hijacked) {
NeilBrown32a76272005-06-21 17:17:14 -0700112 /* somebody beat us to getting the page */
113 bitmap_free_page(bitmap, mappage);
114 return 0;
NeilBrownac2f40b2010-06-01 19:37:31 +1000115 } else {
116
117 /* no page was in place and we have one, so install it */
118
119 bitmap->bp[page].map = mappage;
120 bitmap->missing_pages--;
NeilBrown32a76272005-06-21 17:17:14 -0700121 }
NeilBrown32a76272005-06-21 17:17:14 -0700122 return 0;
123}
124
NeilBrown32a76272005-06-21 17:17:14 -0700125/* if page is completely empty, put it back on the free list, or dealloc it */
126/* if page was hijacked, unmark the flag so it might get alloced next time */
127/* Note: lock should be held when calling this */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800128static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
NeilBrown32a76272005-06-21 17:17:14 -0700129{
130 char *ptr;
131
132 if (bitmap->bp[page].count) /* page is still busy */
133 return;
134
135 /* page is no longer in use, it can be released */
136
137 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
138 bitmap->bp[page].hijacked = 0;
139 bitmap->bp[page].map = NULL;
NeilBrownac2f40b2010-06-01 19:37:31 +1000140 } else {
141 /* normal case, free the page */
142 ptr = bitmap->bp[page].map;
143 bitmap->bp[page].map = NULL;
144 bitmap->missing_pages++;
145 bitmap_free_page(bitmap, ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700146 }
NeilBrown32a76272005-06-21 17:17:14 -0700147}
148
NeilBrown32a76272005-06-21 17:17:14 -0700149/*
150 * bitmap file handling - read and write the bitmap file and its superblock
151 */
152
NeilBrown32a76272005-06-21 17:17:14 -0700153/*
154 * basic page I/O operations
155 */
156
NeilBrowna654b9d82005-06-21 17:17:27 -0700157/* IO operations when bitmap is stored near all superblocks */
NeilBrownfd01b882011-10-11 16:47:53 +1100158static struct page *read_sb_page(struct mddev *mddev, loff_t offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100159 struct page *page,
160 unsigned long index, int size)
NeilBrowna654b9d82005-06-21 17:17:27 -0700161{
162 /* choose a good rdev and read the page from there */
163
NeilBrown3cb03002011-10-11 16:45:26 +1100164 struct md_rdev *rdev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700165 sector_t target;
NeilBrownac2f40b2010-06-01 19:37:31 +1000166 int did_alloc = 0;
NeilBrowna654b9d82005-06-21 17:17:27 -0700167
NeilBrownac2f40b2010-06-01 19:37:31 +1000168 if (!page) {
NeilBrowna2ed9612008-12-19 16:25:01 +1100169 page = alloc_page(GFP_KERNEL);
NeilBrownac2f40b2010-06-01 19:37:31 +1000170 if (!page)
171 return ERR_PTR(-ENOMEM);
172 did_alloc = 1;
173 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700174
NeilBrowndafb20f2012-03-19 12:46:39 +1100175 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800176 if (! test_bit(In_sync, &rdev->flags)
177 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700178 continue;
179
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100180 target = offset + index * (PAGE_SIZE/512);
NeilBrowna654b9d82005-06-21 17:17:27 -0700181
NeilBrown2b193362010-10-27 15:16:40 +1100182 if (sync_page_io(rdev, target,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400183 roundup(size, bdev_logical_block_size(rdev->bdev)),
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100184 page, READ, true)) {
NeilBrownab904d62005-09-09 16:23:52 -0700185 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700186 attach_page_buffers(page, NULL); /* so that free_buffer will
187 * quietly no-op */
NeilBrownab904d62005-09-09 16:23:52 -0700188 return page;
189 }
190 }
NeilBrownac2f40b2010-06-01 19:37:31 +1000191 if (did_alloc)
192 put_page(page);
NeilBrownab904d62005-09-09 16:23:52 -0700193 return ERR_PTR(-EIO);
NeilBrowna654b9d82005-06-21 17:17:27 -0700194
NeilBrowna654b9d82005-06-21 17:17:27 -0700195}
196
NeilBrownfd01b882011-10-11 16:47:53 +1100197static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev)
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000198{
199 /* Iterate the disks of an mddev, using rcu to protect access to the
200 * linked list, and raising the refcount of devices we return to ensure
201 * they don't disappear while in use.
202 * As devices are only added or removed when raid_disk is < 0 and
203 * nr_pending is 0 and In_sync is clear, the entries we return will
204 * still be in the same position on the list when we re-enter
205 * list_for_each_continue_rcu.
206 */
207 struct list_head *pos;
208 rcu_read_lock();
209 if (rdev == NULL)
210 /* start at the beginning */
211 pos = &mddev->disks;
212 else {
213 /* release the previous rdev and start from there. */
214 rdev_dec_pending(rdev, mddev);
215 pos = &rdev->same_set;
216 }
217 list_for_each_continue_rcu(pos, &mddev->disks) {
NeilBrown3cb03002011-10-11 16:45:26 +1100218 rdev = list_entry(pos, struct md_rdev, same_set);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000219 if (rdev->raid_disk >= 0 &&
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000220 !test_bit(Faulty, &rdev->flags)) {
221 /* this is a usable devices */
222 atomic_inc(&rdev->nr_pending);
223 rcu_read_unlock();
224 return rdev;
225 }
226 }
227 rcu_read_unlock();
228 return NULL;
229}
230
NeilBrownab6085c2007-05-23 13:58:10 -0700231static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrowna654b9d82005-06-21 17:17:27 -0700232{
NeilBrown3cb03002011-10-11 16:45:26 +1100233 struct md_rdev *rdev = NULL;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100234 struct block_device *bdev;
NeilBrownfd01b882011-10-11 16:47:53 +1100235 struct mddev *mddev = bitmap->mddev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700236
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000237 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000238 int size = PAGE_SIZE;
239 loff_t offset = mddev->bitmap_info.offset;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100240
241 bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
242
NeilBrownac2f40b2010-06-01 19:37:31 +1000243 if (page->index == bitmap->file_pages-1)
244 size = roundup(bitmap->last_page_size,
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100245 bdev_logical_block_size(bdev));
NeilBrownac2f40b2010-06-01 19:37:31 +1000246 /* Just make sure we aren't corrupting data or
247 * metadata
248 */
249 if (mddev->external) {
250 /* Bitmap could be anywhere. */
251 if (rdev->sb_start + offset + (page->index
252 * (PAGE_SIZE/512))
253 > rdev->data_offset
254 &&
255 rdev->sb_start + offset
256 < (rdev->data_offset + mddev->dev_sectors
257 + (PAGE_SIZE/512)))
258 goto bad_alignment;
259 } else if (offset < 0) {
260 /* DATA BITMAP METADATA */
261 if (offset
262 + (long)(page->index * (PAGE_SIZE/512))
263 + size/512 > 0)
264 /* bitmap runs in to metadata */
265 goto bad_alignment;
266 if (rdev->data_offset + mddev->dev_sectors
267 > rdev->sb_start + offset)
268 /* data runs in to bitmap */
269 goto bad_alignment;
270 } else if (rdev->sb_start < rdev->data_offset) {
271 /* METADATA BITMAP DATA */
272 if (rdev->sb_start
273 + offset
274 + page->index*(PAGE_SIZE/512) + size/512
275 > rdev->data_offset)
276 /* bitmap runs in to data */
277 goto bad_alignment;
278 } else {
279 /* DATA METADATA BITMAP - no problems */
280 }
281 md_super_write(mddev, rdev,
282 rdev->sb_start + offset
283 + page->index * (PAGE_SIZE/512),
284 size,
285 page);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000286 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700287
288 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800289 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700290 return 0;
NeilBrown4b809912008-07-21 17:05:25 +1000291
292 bad_alignment:
NeilBrown4b809912008-07-21 17:05:25 +1000293 return -EINVAL;
NeilBrowna654b9d82005-06-21 17:17:27 -0700294}
295
NeilBrown4ad13662007-07-17 04:06:13 -0700296static void bitmap_file_kick(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700297/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700298 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700299 */
NeilBrown4ad13662007-07-17 04:06:13 -0700300static void write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700301{
NeilBrownd785a062006-06-26 00:27:48 -0700302 struct buffer_head *bh;
NeilBrown32a76272005-06-21 17:17:14 -0700303
NeilBrownf0d76d72007-07-17 04:06:12 -0700304 if (bitmap->file == NULL) {
305 switch (write_sb_page(bitmap, page, wait)) {
306 case -EINVAL:
307 bitmap->flags |= BITMAP_WRITE_ERROR;
NeilBrownf0d76d72007-07-17 04:06:12 -0700308 }
NeilBrown4ad13662007-07-17 04:06:13 -0700309 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700310
NeilBrown4ad13662007-07-17 04:06:13 -0700311 bh = page_buffers(page);
NeilBrownc7084432006-01-06 00:20:45 -0800312
NeilBrown4ad13662007-07-17 04:06:13 -0700313 while (bh && bh->b_blocknr) {
314 atomic_inc(&bitmap->pending_writes);
315 set_buffer_locked(bh);
316 set_buffer_mapped(bh);
Jens Axboe721a9602011-03-09 11:56:30 +0100317 submit_bh(WRITE | REQ_SYNC, bh);
NeilBrown4ad13662007-07-17 04:06:13 -0700318 bh = bh->b_this_page;
319 }
NeilBrown32a76272005-06-21 17:17:14 -0700320
NeilBrownac2f40b2010-06-01 19:37:31 +1000321 if (wait)
NeilBrown4ad13662007-07-17 04:06:13 -0700322 wait_event(bitmap->write_wait,
323 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700324 }
NeilBrown4ad13662007-07-17 04:06:13 -0700325 if (bitmap->flags & BITMAP_WRITE_ERROR)
326 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700327}
328
NeilBrownd785a062006-06-26 00:27:48 -0700329static void end_bitmap_write(struct buffer_head *bh, int uptodate)
NeilBrown32a76272005-06-21 17:17:14 -0700330{
NeilBrownd785a062006-06-26 00:27:48 -0700331 struct bitmap *bitmap = bh->b_private;
332 unsigned long flags;
333
334 if (!uptodate) {
335 spin_lock_irqsave(&bitmap->lock, flags);
336 bitmap->flags |= BITMAP_WRITE_ERROR;
337 spin_unlock_irqrestore(&bitmap->lock, flags);
338 }
339 if (atomic_dec_and_test(&bitmap->pending_writes))
340 wake_up(&bitmap->write_wait);
341}
342
343/* copied from buffer.c */
344static void
345__clear_page_buffers(struct page *page)
346{
347 ClearPagePrivate(page);
348 set_page_private(page, 0);
349 page_cache_release(page);
350}
351static void free_buffers(struct page *page)
352{
353 struct buffer_head *bh = page_buffers(page);
354
355 while (bh) {
356 struct buffer_head *next = bh->b_this_page;
357 free_buffer_head(bh);
358 bh = next;
359 }
360 __clear_page_buffers(page);
361 put_page(page);
362}
363
364/* read a page from a file.
365 * We both read the page, and attach buffers to the page to record the
366 * address of each block (using bmap). These addresses will be used
367 * to write the block later, completely bypassing the filesystem.
368 * This usage is similar to how swap files are handled, and allows us
369 * to write to a file with no concerns of memory allocation failing.
370 */
371static struct page *read_page(struct file *file, unsigned long index,
372 struct bitmap *bitmap,
373 unsigned long count)
374{
NeilBrown32a76272005-06-21 17:17:14 -0700375 struct page *page = NULL;
Josef Sipekc649bb92006-12-08 02:37:19 -0800376 struct inode *inode = file->f_path.dentry->d_inode;
NeilBrownd785a062006-06-26 00:27:48 -0700377 struct buffer_head *bh;
378 sector_t block;
NeilBrown32a76272005-06-21 17:17:14 -0700379
NeilBrown36a4e1f2011-10-07 14:23:17 +1100380 pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
381 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700382
NeilBrownd785a062006-06-26 00:27:48 -0700383 page = alloc_page(GFP_KERNEL);
384 if (!page)
385 page = ERR_PTR(-ENOMEM);
NeilBrown32a76272005-06-21 17:17:14 -0700386 if (IS_ERR(page))
387 goto out;
NeilBrownd785a062006-06-26 00:27:48 -0700388
NeilBrownd785a062006-06-26 00:27:48 -0700389 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
390 if (!bh) {
391 put_page(page);
392 page = ERR_PTR(-ENOMEM);
393 goto out;
394 }
395 attach_page_buffers(page, bh);
396 block = index << (PAGE_SHIFT - inode->i_blkbits);
397 while (bh) {
398 if (count == 0)
399 bh->b_blocknr = 0;
400 else {
401 bh->b_blocknr = bmap(inode, block);
402 if (bh->b_blocknr == 0) {
403 /* Cannot use this file! */
404 free_buffers(page);
405 page = ERR_PTR(-EINVAL);
406 goto out;
407 }
408 bh->b_bdev = inode->i_sb->s_bdev;
409 if (count < (1<<inode->i_blkbits))
410 count = 0;
411 else
412 count -= (1<<inode->i_blkbits);
NeilBrown32a76272005-06-21 17:17:14 -0700413
NeilBrownd785a062006-06-26 00:27:48 -0700414 bh->b_end_io = end_bitmap_write;
415 bh->b_private = bitmap;
NeilBrownce25c312006-06-26 00:27:49 -0700416 atomic_inc(&bitmap->pending_writes);
417 set_buffer_locked(bh);
418 set_buffer_mapped(bh);
419 submit_bh(READ, bh);
NeilBrownd785a062006-06-26 00:27:48 -0700420 }
421 block++;
422 bh = bh->b_this_page;
423 }
NeilBrownd785a062006-06-26 00:27:48 -0700424 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700425
426 wait_event(bitmap->write_wait,
427 atomic_read(&bitmap->pending_writes)==0);
428 if (bitmap->flags & BITMAP_WRITE_ERROR) {
429 free_buffers(page);
430 page = ERR_PTR(-EIO);
431 }
NeilBrown32a76272005-06-21 17:17:14 -0700432out:
433 if (IS_ERR(page))
NeilBrownac2f40b2010-06-01 19:37:31 +1000434 printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800435 (int)PAGE_SIZE,
436 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700437 PTR_ERR(page));
438 return page;
439}
440
441/*
442 * bitmap file superblock operations
443 */
444
445/* update the event counter and sync the superblock to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700446void bitmap_update_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700447{
448 bitmap_super_t *sb;
449 unsigned long flags;
450
451 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
NeilBrown4ad13662007-07-17 04:06:13 -0700452 return;
NeilBrownece5cff2009-12-14 12:49:56 +1100453 if (bitmap->mddev->bitmap_info.external)
454 return;
NeilBrown32a76272005-06-21 17:17:14 -0700455 spin_lock_irqsave(&bitmap->lock, flags);
456 if (!bitmap->sb_page) { /* no superblock */
457 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700458 return;
NeilBrown32a76272005-06-21 17:17:14 -0700459 }
NeilBrown32a76272005-06-21 17:17:14 -0700460 spin_unlock_irqrestore(&bitmap->lock, flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100461 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700462 sb->events = cpu_to_le64(bitmap->mddev->events);
NeilBrown8258c532011-05-11 14:26:30 +1000463 if (bitmap->mddev->events < bitmap->events_cleared)
Neil Browna0da84f2008-06-28 08:31:22 +1000464 /* rocking back to read-only */
465 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown8258c532011-05-11 14:26:30 +1000466 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
467 sb->state = cpu_to_le32(bitmap->flags);
NeilBrown43a70502009-12-14 12:49:55 +1100468 /* Just in case these have been changed via sysfs: */
469 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
470 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
NeilBrownea03aff2006-01-06 00:20:34 -0800471 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700472 write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700473}
474
475/* print out the bitmap file superblock */
476void bitmap_print_sb(struct bitmap *bitmap)
477{
478 bitmap_super_t *sb;
479
480 if (!bitmap || !bitmap->sb_page)
481 return;
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100482 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700483 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700484 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
485 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
486 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700487 *(__u32 *)(sb->uuid+0),
488 *(__u32 *)(sb->uuid+4),
489 *(__u32 *)(sb->uuid+8),
490 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700491 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700492 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700493 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700494 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700495 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
496 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
497 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
498 printk(KERN_DEBUG " sync size: %llu KB\n",
499 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700500 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800501 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700502}
503
Jonathan Brassow9c810752011-06-08 17:59:30 -0500504/*
505 * bitmap_new_disk_sb
506 * @bitmap
507 *
508 * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
509 * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
510 * This function verifies 'bitmap_info' and populates the on-disk bitmap
511 * structure, which is to be written to disk.
512 *
513 * Returns: 0 on success, -Exxx on error
514 */
515static int bitmap_new_disk_sb(struct bitmap *bitmap)
516{
517 bitmap_super_t *sb;
518 unsigned long chunksize, daemon_sleep, write_behind;
519 int err = -EINVAL;
520
521 bitmap->sb_page = alloc_page(GFP_KERNEL);
522 if (IS_ERR(bitmap->sb_page)) {
523 err = PTR_ERR(bitmap->sb_page);
524 bitmap->sb_page = NULL;
525 return err;
526 }
527 bitmap->sb_page->index = 0;
528
529 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
530
531 sb->magic = cpu_to_le32(BITMAP_MAGIC);
532 sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
533
534 chunksize = bitmap->mddev->bitmap_info.chunksize;
535 BUG_ON(!chunksize);
536 if (!is_power_of_2(chunksize)) {
537 kunmap_atomic(sb, KM_USER0);
538 printk(KERN_ERR "bitmap chunksize not a power of 2\n");
539 return -EINVAL;
540 }
541 sb->chunksize = cpu_to_le32(chunksize);
542
543 daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
544 if (!daemon_sleep ||
545 (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
546 printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n");
547 daemon_sleep = 5 * HZ;
548 }
549 sb->daemon_sleep = cpu_to_le32(daemon_sleep);
550 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
551
552 /*
553 * FIXME: write_behind for RAID1. If not specified, what
554 * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
555 */
556 write_behind = bitmap->mddev->bitmap_info.max_write_behind;
557 if (write_behind > COUNTER_MAX)
558 write_behind = COUNTER_MAX / 2;
559 sb->write_behind = cpu_to_le32(write_behind);
560 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
561
562 /* keep the array size field of the bitmap superblock up to date */
563 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
564
565 memcpy(sb->uuid, bitmap->mddev->uuid, 16);
566
567 bitmap->flags |= BITMAP_STALE;
568 sb->state |= cpu_to_le32(BITMAP_STALE);
569 bitmap->events_cleared = bitmap->mddev->events;
570 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
571
572 bitmap->flags |= BITMAP_HOSTENDIAN;
573 sb->version = cpu_to_le32(BITMAP_MAJOR_HOSTENDIAN);
574
575 kunmap_atomic(sb, KM_USER0);
576
577 return 0;
578}
579
NeilBrown32a76272005-06-21 17:17:14 -0700580/* read the superblock from the bitmap file and initialize some bitmap fields */
581static int bitmap_read_sb(struct bitmap *bitmap)
582{
583 char *reason = NULL;
584 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700585 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700586 unsigned long long events;
587 int err = -EINVAL;
588
589 /* page 0 is the superblock, read it... */
NeilBrownf49d5e62007-01-26 00:57:03 -0800590 if (bitmap->file) {
591 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
592 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
593
594 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
595 } else {
NeilBrown42a04b52009-12-14 12:49:53 +1100596 bitmap->sb_page = read_sb_page(bitmap->mddev,
597 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100598 NULL,
599 0, sizeof(bitmap_super_t));
NeilBrowna654b9d82005-06-21 17:17:27 -0700600 }
NeilBrown32a76272005-06-21 17:17:14 -0700601 if (IS_ERR(bitmap->sb_page)) {
602 err = PTR_ERR(bitmap->sb_page);
603 bitmap->sb_page = NULL;
604 return err;
605 }
606
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100607 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700608
NeilBrown32a76272005-06-21 17:17:14 -0700609 chunksize = le32_to_cpu(sb->chunksize);
NeilBrown1b04be92009-12-14 12:49:53 +1100610 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
NeilBrown4b6d2872005-09-09 16:23:47 -0700611 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700612
613 /* verify that the bitmap-specific fields are valid */
614 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
615 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800616 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
617 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700618 reason = "unrecognized superblock version";
NeilBrown1187cf02009-03-31 14:27:02 +1100619 else if (chunksize < 512)
NeilBrown7dd5d342006-01-06 00:20:39 -0800620 reason = "bitmap chunksize too small";
Jonathan Brassowd7445402011-06-08 18:01:10 -0500621 else if (!is_power_of_2(chunksize))
NeilBrown32a76272005-06-21 17:17:14 -0700622 reason = "bitmap chunksize not a power of 2";
NeilBrown1b04be92009-12-14 12:49:53 +1100623 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
NeilBrown7dd5d342006-01-06 00:20:39 -0800624 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700625 else if (write_behind > COUNTER_MAX)
626 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700627 if (reason) {
628 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
629 bmname(bitmap), reason);
630 goto out;
631 }
632
633 /* keep the array size field of the bitmap superblock up to date */
634 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
635
NeilBrown278c1ca2012-03-19 12:46:40 +1100636 if (bitmap->mddev->persistent) {
637 /*
638 * We have a persistent array superblock, so compare the
639 * bitmap's UUID and event counter to the mddev's
640 */
641 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
642 printk(KERN_INFO
643 "%s: bitmap superblock UUID mismatch\n",
644 bmname(bitmap));
645 goto out;
646 }
647 events = le64_to_cpu(sb->events);
648 if (events < bitmap->mddev->events) {
649 printk(KERN_INFO
650 "%s: bitmap file is out of date (%llu < %llu) "
651 "-- forcing full recovery\n",
652 bmname(bitmap), events,
653 (unsigned long long) bitmap->mddev->events);
654 sb->state |= cpu_to_le32(BITMAP_STALE);
655 }
656 }
NeilBrown32a76272005-06-21 17:17:14 -0700657
NeilBrown32a76272005-06-21 17:17:14 -0700658 /* assign fields using values from superblock */
NeilBrown42a04b52009-12-14 12:49:53 +1100659 bitmap->mddev->bitmap_info.chunksize = chunksize;
660 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
NeilBrown42a04b52009-12-14 12:49:53 +1100661 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
NeilBrown4f2e6392006-10-21 10:24:09 -0700662 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800663 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
664 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700665 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown8258c532011-05-11 14:26:30 +1000666 if (bitmap->flags & BITMAP_STALE)
NeilBrown6a079972005-09-09 16:23:44 -0700667 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700668 err = 0;
669out:
NeilBrownea03aff2006-01-06 00:20:34 -0800670 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700671 if (err)
672 bitmap_print_sb(bitmap);
673 return err;
674}
675
676enum bitmap_mask_op {
677 MASK_SET,
678 MASK_UNSET
679};
680
NeilBrown4ad13662007-07-17 04:06:13 -0700681/* record the state of the bitmap in the superblock. Return the old value */
682static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
683 enum bitmap_mask_op op)
NeilBrown32a76272005-06-21 17:17:14 -0700684{
685 bitmap_super_t *sb;
686 unsigned long flags;
NeilBrown4ad13662007-07-17 04:06:13 -0700687 int old;
NeilBrown32a76272005-06-21 17:17:14 -0700688
689 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800690 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700691 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700692 return 0;
NeilBrown32a76272005-06-21 17:17:14 -0700693 }
NeilBrown32a76272005-06-21 17:17:14 -0700694 spin_unlock_irqrestore(&bitmap->lock, flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100695 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700696 old = le32_to_cpu(sb->state) & bits;
NeilBrown32a76272005-06-21 17:17:14 -0700697 switch (op) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000698 case MASK_SET:
699 sb->state |= cpu_to_le32(bits);
NeilBrown8258c532011-05-11 14:26:30 +1000700 bitmap->flags |= bits;
NeilBrownac2f40b2010-06-01 19:37:31 +1000701 break;
702 case MASK_UNSET:
703 sb->state &= cpu_to_le32(~bits);
NeilBrown8258c532011-05-11 14:26:30 +1000704 bitmap->flags &= ~bits;
NeilBrownac2f40b2010-06-01 19:37:31 +1000705 break;
706 default:
707 BUG();
NeilBrown32a76272005-06-21 17:17:14 -0700708 }
NeilBrownea03aff2006-01-06 00:20:34 -0800709 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700710 return old;
NeilBrown32a76272005-06-21 17:17:14 -0700711}
712
713/*
714 * general bitmap file operations
715 */
716
NeilBrownece5cff2009-12-14 12:49:56 +1100717/*
718 * on-disk bitmap:
719 *
720 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
721 * file a page at a time. There's a superblock at the start of the file.
722 */
NeilBrown32a76272005-06-21 17:17:14 -0700723/* calculate the index of the page that contains this bit */
NeilBrownece5cff2009-12-14 12:49:56 +1100724static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700725{
NeilBrownece5cff2009-12-14 12:49:56 +1100726 if (!bitmap->mddev->bitmap_info.external)
727 chunk += sizeof(bitmap_super_t) << 3;
728 return chunk >> PAGE_BIT_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700729}
730
731/* calculate the (bit) offset of this bit within a page */
NeilBrownece5cff2009-12-14 12:49:56 +1100732static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700733{
NeilBrownece5cff2009-12-14 12:49:56 +1100734 if (!bitmap->mddev->bitmap_info.external)
735 chunk += sizeof(bitmap_super_t) << 3;
736 return chunk & (PAGE_BITS - 1);
NeilBrown32a76272005-06-21 17:17:14 -0700737}
738
739/*
740 * return a pointer to the page in the filemap that contains the given bit
741 *
742 * this lookup is complicated by the fact that the bitmap sb might be exactly
743 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
744 * 0 or page 1
745 */
746static inline struct page *filemap_get_page(struct bitmap *bitmap,
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000747 unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700748{
NeilBrownac2f40b2010-06-01 19:37:31 +1000749 if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
750 return NULL;
NeilBrownece5cff2009-12-14 12:49:56 +1100751 return bitmap->filemap[file_page_index(bitmap, chunk)
752 - file_page_index(bitmap, 0)];
NeilBrown32a76272005-06-21 17:17:14 -0700753}
754
NeilBrown32a76272005-06-21 17:17:14 -0700755static void bitmap_file_unmap(struct bitmap *bitmap)
756{
757 struct page **map, *sb_page;
758 unsigned long *attr;
759 int pages;
760 unsigned long flags;
761
762 spin_lock_irqsave(&bitmap->lock, flags);
763 map = bitmap->filemap;
764 bitmap->filemap = NULL;
765 attr = bitmap->filemap_attr;
766 bitmap->filemap_attr = NULL;
767 pages = bitmap->file_pages;
768 bitmap->file_pages = 0;
769 sb_page = bitmap->sb_page;
770 bitmap->sb_page = NULL;
771 spin_unlock_irqrestore(&bitmap->lock, flags);
772
773 while (pages--)
NeilBrownece5cff2009-12-14 12:49:56 +1100774 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700775 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700776 kfree(map);
777 kfree(attr);
778
NeilBrownd785a062006-06-26 00:27:48 -0700779 if (sb_page)
780 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700781}
782
783static void bitmap_file_put(struct bitmap *bitmap)
784{
785 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700786 unsigned long flags;
787
788 spin_lock_irqsave(&bitmap->lock, flags);
789 file = bitmap->file;
790 bitmap->file = NULL;
791 spin_unlock_irqrestore(&bitmap->lock, flags);
792
NeilBrownd785a062006-06-26 00:27:48 -0700793 if (file)
794 wait_event(bitmap->write_wait,
795 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700796 bitmap_file_unmap(bitmap);
797
NeilBrownd785a062006-06-26 00:27:48 -0700798 if (file) {
Josef Sipekc649bb92006-12-08 02:37:19 -0800799 struct inode *inode = file->f_path.dentry->d_inode;
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800800 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700801 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700802 }
NeilBrown32a76272005-06-21 17:17:14 -0700803}
804
NeilBrown32a76272005-06-21 17:17:14 -0700805/*
806 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
807 * then it is no longer reliable, so we stop using it and we mark the file
808 * as failed in the superblock
809 */
810static void bitmap_file_kick(struct bitmap *bitmap)
811{
812 char *path, *ptr = NULL;
813
NeilBrown4ad13662007-07-17 04:06:13 -0700814 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
815 bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700816
NeilBrown4ad13662007-07-17 04:06:13 -0700817 if (bitmap->file) {
818 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
819 if (path)
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700820 ptr = d_path(&bitmap->file->f_path, path,
821 PAGE_SIZE);
822
NeilBrown4ad13662007-07-17 04:06:13 -0700823 printk(KERN_ALERT
824 "%s: kicking failed bitmap file %s from array!\n",
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700825 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700826
NeilBrown4ad13662007-07-17 04:06:13 -0700827 kfree(path);
828 } else
829 printk(KERN_ALERT
830 "%s: disabling internal bitmap due to errors\n",
831 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700832 }
NeilBrown32a76272005-06-21 17:17:14 -0700833
834 bitmap_file_put(bitmap);
835
836 return;
837}
838
839enum bitmap_page_attr {
NeilBrownac2f40b2010-06-01 19:37:31 +1000840 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
NeilBrown5a537df2011-09-21 15:37:46 +1000841 BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
842 * i.e. counter is 1 or 2. */
NeilBrownac2f40b2010-06-01 19:37:31 +1000843 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
NeilBrown32a76272005-06-21 17:17:14 -0700844};
845
846static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
847 enum bitmap_page_attr attr)
848{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000849 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700850}
851
852static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
853 enum bitmap_page_attr attr)
854{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000855 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700856}
857
NeilBrownec7a3192006-06-26 00:27:45 -0700858static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
859 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700860{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000861 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700862}
863
864/*
865 * bitmap_file_set_bit -- called before performing a write to the md device
866 * to set (and eventually sync) a particular bit in the bitmap file
867 *
868 * we set the bit immediately, then we record the page number so that
869 * when an unplug occurs, we can flush the dirty pages out to disk
870 */
871static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
872{
873 unsigned long bit;
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000874 struct page *page;
NeilBrown32a76272005-06-21 17:17:14 -0700875 void *kaddr;
876 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
877
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000878 if (!bitmap->filemap)
879 return;
NeilBrown32a76272005-06-21 17:17:14 -0700880
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000881 page = filemap_get_page(bitmap, chunk);
882 if (!page)
883 return;
884 bit = file_page_offset(bitmap, chunk);
NeilBrown32a76272005-06-21 17:17:14 -0700885
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000886 /* set the bit */
887 kaddr = kmap_atomic(page, KM_USER0);
888 if (bitmap->flags & BITMAP_HOSTENDIAN)
889 set_bit(bit, kaddr);
890 else
891 __set_bit_le(bit, kaddr);
892 kunmap_atomic(kaddr, KM_USER0);
NeilBrown36a4e1f2011-10-07 14:23:17 +1100893 pr_debug("set file bit %lu page %lu\n", bit, page->index);
NeilBrown32a76272005-06-21 17:17:14 -0700894 /* record page number so it gets flushed to disk when unplug occurs */
895 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
NeilBrown32a76272005-06-21 17:17:14 -0700896}
897
898/* this gets called when the md device is ready to unplug its underlying
899 * (slave) device queues -- before we let any writes go down, we need to
900 * sync the dirty pages of the bitmap file to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700901void bitmap_unplug(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700902{
NeilBrownec7a3192006-06-26 00:27:45 -0700903 unsigned long i, flags;
904 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700905 struct page *page;
906 int wait = 0;
907
908 if (!bitmap)
NeilBrown4ad13662007-07-17 04:06:13 -0700909 return;
NeilBrown32a76272005-06-21 17:17:14 -0700910
911 /* look at each page to see if there are any set bits that need to be
912 * flushed out to disk */
913 for (i = 0; i < bitmap->file_pages; i++) {
914 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700915 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700916 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700917 return;
NeilBrown32a76272005-06-21 17:17:14 -0700918 }
919 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700920 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
921 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700922 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
923 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700924 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700925 wait = 1;
926 spin_unlock_irqrestore(&bitmap->lock, flags);
927
NeilBrownac2f40b2010-06-01 19:37:31 +1000928 if (dirty || need_write)
NeilBrown4ad13662007-07-17 04:06:13 -0700929 write_page(bitmap, page, 0);
NeilBrown32a76272005-06-21 17:17:14 -0700930 }
931 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700932 if (bitmap->file)
NeilBrownd785a062006-06-26 00:27:48 -0700933 wait_event(bitmap->write_wait,
934 atomic_read(&bitmap->pending_writes)==0);
NeilBrown0b79ccf2006-06-26 00:27:44 -0700935 else
NeilBrowna9701a32005-11-08 21:39:34 -0800936 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700937 }
NeilBrownd785a062006-06-26 00:27:48 -0700938 if (bitmap->flags & BITMAP_WRITE_ERROR)
939 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700940}
NeilBrownac2f40b2010-06-01 19:37:31 +1000941EXPORT_SYMBOL(bitmap_unplug);
NeilBrown32a76272005-06-21 17:17:14 -0700942
NeilBrown6a079972005-09-09 16:23:44 -0700943static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700944/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
945 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
946 * memory mapping of the bitmap file
947 * Special cases:
948 * if there's no bitmap file, or if the bitmap file had been
949 * previously kicked from the array, we mark all the bits as
950 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700951 *
952 * We ignore all bits for sectors that end earlier than 'start'.
953 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700954 */
NeilBrown6a079972005-09-09 16:23:44 -0700955static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700956{
957 unsigned long i, chunks, index, oldindex, bit;
958 struct page *page = NULL, *oldpage = NULL;
959 unsigned long num_pages, bit_cnt = 0;
960 struct file *file;
NeilBrownd785a062006-06-26 00:27:48 -0700961 unsigned long bytes, offset;
NeilBrown32a76272005-06-21 17:17:14 -0700962 int outofdate;
963 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800964 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700965
966 chunks = bitmap->chunks;
967 file = bitmap->file;
968
NeilBrown42a04b52009-12-14 12:49:53 +1100969 BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
NeilBrown32a76272005-06-21 17:17:14 -0700970
NeilBrown32a76272005-06-21 17:17:14 -0700971 outofdate = bitmap->flags & BITMAP_STALE;
NeilBrown32a76272005-06-21 17:17:14 -0700972 if (outofdate)
973 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
974 "recovery\n", bmname(bitmap));
975
NeilBrowne384e582010-06-01 19:37:34 +1000976 bytes = DIV_ROUND_UP(bitmap->chunks, 8);
NeilBrownece5cff2009-12-14 12:49:56 +1100977 if (!bitmap->mddev->bitmap_info.external)
978 bytes += sizeof(bitmap_super_t);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700979
NeilBrowne384e582010-06-01 19:37:34 +1000980 num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700981
NeilBrownece5cff2009-12-14 12:49:56 +1100982 if (file && i_size_read(file->f_mapping->host) < bytes) {
NeilBrown32a76272005-06-21 17:17:14 -0700983 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
984 bmname(bitmap),
985 (unsigned long) i_size_read(file->f_mapping->host),
NeilBrownece5cff2009-12-14 12:49:56 +1100986 bytes);
NeilBrown4ad13662007-07-17 04:06:13 -0700987 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700988 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700989
990 ret = -ENOMEM;
991
NeilBrown32a76272005-06-21 17:17:14 -0700992 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700993 if (!bitmap->filemap)
NeilBrown4ad13662007-07-17 04:06:13 -0700994 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700995
NeilBrowne16b68b2006-06-26 00:27:45 -0700996 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
997 bitmap->filemap_attr = kzalloc(
NeilBrownac2f40b2010-06-01 19:37:31 +1000998 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
NeilBrowne16b68b2006-06-26 00:27:45 -0700999 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -07001000 if (!bitmap->filemap_attr)
NeilBrown4ad13662007-07-17 04:06:13 -07001001 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001002
NeilBrown32a76272005-06-21 17:17:14 -07001003 oldindex = ~0L;
1004
1005 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -08001006 int b;
NeilBrownece5cff2009-12-14 12:49:56 +11001007 index = file_page_index(bitmap, i);
1008 bit = file_page_offset(bitmap, i);
NeilBrown32a76272005-06-21 17:17:14 -07001009 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -07001010 int count;
NeilBrown32a76272005-06-21 17:17:14 -07001011 /* unmap the old page, we're done with it */
NeilBrownd785a062006-06-26 00:27:48 -07001012 if (index == num_pages-1)
NeilBrownece5cff2009-12-14 12:49:56 +11001013 count = bytes - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -07001014 else
1015 count = PAGE_SIZE;
NeilBrownece5cff2009-12-14 12:49:56 +11001016 if (index == 0 && bitmap->sb_page) {
NeilBrown32a76272005-06-21 17:17:14 -07001017 /*
1018 * if we're here then the superblock page
1019 * contains some bits (PAGE_SIZE != sizeof sb)
1020 * we've already read it in, so just use it
1021 */
1022 page = bitmap->sb_page;
1023 offset = sizeof(bitmap_super_t);
NeilBrown53845272009-01-09 08:31:05 +11001024 if (!file)
Vasiliy Kulikov5c04f552010-10-01 14:18:12 -07001025 page = read_sb_page(
1026 bitmap->mddev,
1027 bitmap->mddev->bitmap_info.offset,
1028 page,
1029 index, count);
NeilBrowna654b9d82005-06-21 17:17:27 -07001030 } else if (file) {
NeilBrownd785a062006-06-26 00:27:48 -07001031 page = read_page(file, index, bitmap, count);
NeilBrowna654b9d82005-06-21 17:17:27 -07001032 offset = 0;
1033 } else {
NeilBrown42a04b52009-12-14 12:49:53 +11001034 page = read_sb_page(bitmap->mddev,
1035 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +11001036 NULL,
1037 index, count);
NeilBrown32a76272005-06-21 17:17:14 -07001038 offset = 0;
1039 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001040 if (IS_ERR(page)) { /* read error */
1041 ret = PTR_ERR(page);
NeilBrown4ad13662007-07-17 04:06:13 -07001042 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -07001043 }
1044
NeilBrown32a76272005-06-21 17:17:14 -07001045 oldindex = index;
1046 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -07001047
NeilBrownb74fd282009-05-07 12:47:19 +10001048 bitmap->filemap[bitmap->file_pages++] = page;
1049 bitmap->last_page_size = count;
1050
NeilBrown32a76272005-06-21 17:17:14 -07001051 if (outofdate) {
1052 /*
1053 * if bitmap is out of date, dirty the
NeilBrownac2f40b2010-06-01 19:37:31 +10001054 * whole page and write it out
NeilBrown32a76272005-06-21 17:17:14 -07001055 */
NeilBrownea03aff2006-01-06 00:20:34 -08001056 paddr = kmap_atomic(page, KM_USER0);
1057 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -07001058 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -08001059 kunmap_atomic(paddr, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -07001060 write_page(bitmap, page, 1);
1061
1062 ret = -EIO;
NeilBrownb74fd282009-05-07 12:47:19 +10001063 if (bitmap->flags & BITMAP_WRITE_ERROR)
NeilBrown4ad13662007-07-17 04:06:13 -07001064 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001065 }
NeilBrown32a76272005-06-21 17:17:14 -07001066 }
NeilBrownea03aff2006-01-06 00:20:34 -08001067 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001068 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001069 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001070 else
Akinobu Mita6b33aff2011-03-23 16:42:13 -07001071 b = test_bit_le(bit, paddr);
NeilBrownea03aff2006-01-06 00:20:34 -08001072 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001073 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -07001074 /* if the disk bit is set, set the memory bit */
NeilBrowndb305e52009-05-07 12:49:06 +10001075 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1076 >= start);
1077 bitmap_set_memory_bits(bitmap,
1078 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1079 needed);
NeilBrown32a76272005-06-21 17:17:14 -07001080 bit_cnt++;
1081 }
NeilBrown32a76272005-06-21 17:17:14 -07001082 }
1083
NeilBrownac2f40b2010-06-01 19:37:31 +10001084 /* everything went OK */
NeilBrown32a76272005-06-21 17:17:14 -07001085 ret = 0;
1086 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1087
NeilBrown32a76272005-06-21 17:17:14 -07001088 if (bit_cnt) { /* Kick recovery if any bits were set */
1089 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1090 md_wakeup_thread(bitmap->mddev->thread);
1091 }
1092
NeilBrown32a76272005-06-21 17:17:14 -07001093 printk(KERN_INFO "%s: bitmap initialized from disk: "
Jonathan Brassow9c810752011-06-08 17:59:30 -05001094 "read %lu/%lu pages, set %lu of %lu bits\n",
1095 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, chunks);
NeilBrown32a76272005-06-21 17:17:14 -07001096
NeilBrown4ad13662007-07-17 04:06:13 -07001097 return 0;
1098
1099 err:
1100 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1101 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001102 return ret;
1103}
1104
NeilBrowna654b9d82005-06-21 17:17:27 -07001105void bitmap_write_all(struct bitmap *bitmap)
1106{
1107 /* We don't actually write all bitmap blocks here,
1108 * just flag them as needing to be written
1109 */
NeilBrownec7a3192006-06-26 00:27:45 -07001110 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001111
NeilBrown7c8f4242011-11-23 10:18:52 +11001112 spin_lock_irq(&bitmap->lock);
NeilBrownac2f40b2010-06-01 19:37:31 +10001113 for (i = 0; i < bitmap->file_pages; i++)
NeilBrownec7a3192006-06-26 00:27:45 -07001114 set_page_attr(bitmap, bitmap->filemap[i],
1115 BITMAP_PAGE_NEEDWRITE);
NeilBrown2585f3e2011-09-21 15:37:46 +10001116 bitmap->allclean = 0;
NeilBrown7c8f4242011-11-23 10:18:52 +11001117 spin_unlock_irq(&bitmap->lock);
NeilBrowna654b9d82005-06-21 17:17:27 -07001118}
1119
NeilBrown32a76272005-06-21 17:17:14 -07001120static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1121{
1122 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1123 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1124 bitmap->bp[page].count += inc;
NeilBrown32a76272005-06-21 17:17:14 -07001125 bitmap_checkfree(bitmap, page);
1126}
1127static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001128 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001129 int create);
1130
1131/*
1132 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1133 * out to disk
1134 */
1135
NeilBrownfd01b882011-10-11 16:47:53 +11001136void bitmap_daemon_work(struct mddev *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001137{
NeilBrownaa5cbd12009-12-14 12:49:46 +11001138 struct bitmap *bitmap;
NeilBrownaa3163f2005-06-21 17:17:22 -07001139 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001140 unsigned long flags;
1141 struct page *page = NULL, *lastpage = NULL;
NeilBrown57dab0b2010-10-19 10:03:39 +11001142 sector_t blocks;
NeilBrownea03aff2006-01-06 00:20:34 -08001143 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001144
NeilBrownaa5cbd12009-12-14 12:49:46 +11001145 /* Use a mutex to guard daemon_work against
1146 * bitmap_destroy.
1147 */
NeilBrownc3d97142009-12-14 12:49:52 +11001148 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrownaa5cbd12009-12-14 12:49:46 +11001149 bitmap = mddev->bitmap;
1150 if (bitmap == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001151 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown4ad13662007-07-17 04:06:13 -07001152 return;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001153 }
NeilBrown42a04b52009-12-14 12:49:53 +11001154 if (time_before(jiffies, bitmap->daemon_lastrun
NeilBrown2e61ebb2011-12-23 10:17:50 +11001155 + mddev->bitmap_info.daemon_sleep))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001156 goto done;
1157
NeilBrown32a76272005-06-21 17:17:14 -07001158 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001159 if (bitmap->allclean) {
NeilBrown2e61ebb2011-12-23 10:17:50 +11001160 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001161 goto done;
NeilBrown8311c292008-03-04 14:29:30 -08001162 }
1163 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001164
NeilBrownbe512692009-05-26 09:41:17 +10001165 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001166 for (j = 0; j < bitmap->chunks; j++) {
1167 bitmap_counter_t *bmc;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001168 if (!bitmap->filemap)
1169 /* error or shutdown */
1170 break;
1171
1172 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001173
1174 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001175 /* skip this page unless it's marked as needing cleaning */
NeilBrown5a537df2011-09-21 15:37:46 +10001176 if (!test_page_attr(bitmap, page, BITMAP_PAGE_PENDING)) {
NeilBrownec7a3192006-06-26 00:27:45 -07001177 int need_write = test_page_attr(bitmap, page,
1178 BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001179 if (need_write)
NeilBrownaa3163f2005-06-21 17:17:22 -07001180 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001181
NeilBrownaa3163f2005-06-21 17:17:22 -07001182 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown2585f3e2011-09-21 15:37:46 +10001183 if (need_write)
NeilBrown4ad13662007-07-17 04:06:13 -07001184 write_page(bitmap, page, 0);
NeilBrownbe512692009-05-26 09:41:17 +10001185 spin_lock_irqsave(&bitmap->lock, flags);
1186 j |= (PAGE_BITS - 1);
NeilBrownaa3163f2005-06-21 17:17:22 -07001187 continue;
1188 }
1189
NeilBrown32a76272005-06-21 17:17:14 -07001190 /* grab the new page, sync and release the old */
NeilBrown32a76272005-06-21 17:17:14 -07001191 if (lastpage != NULL) {
NeilBrown2585f3e2011-09-21 15:37:46 +10001192 if (test_page_attr(bitmap, lastpage,
1193 BITMAP_PAGE_NEEDWRITE)) {
1194 clear_page_attr(bitmap, lastpage,
1195 BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -07001196 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001197 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001198 } else {
NeilBrown2585f3e2011-09-21 15:37:46 +10001199 set_page_attr(bitmap, lastpage,
1200 BITMAP_PAGE_NEEDWRITE);
1201 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001202 spin_unlock_irqrestore(&bitmap->lock, flags);
1203 }
NeilBrown32a76272005-06-21 17:17:14 -07001204 } else
1205 spin_unlock_irqrestore(&bitmap->lock, flags);
1206 lastpage = page;
Neil Browna0da84f2008-06-28 08:31:22 +10001207
1208 /* We are possibly going to clear some bits, so make
1209 * sure that events_cleared is up-to-date.
1210 */
NeilBrownece5cff2009-12-14 12:49:56 +11001211 if (bitmap->need_sync &&
NeilBrown2e61ebb2011-12-23 10:17:50 +11001212 mddev->bitmap_info.external == 0) {
Neil Browna0da84f2008-06-28 08:31:22 +10001213 bitmap_super_t *sb;
1214 bitmap->need_sync = 0;
1215 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1216 sb->events_cleared =
1217 cpu_to_le64(bitmap->events_cleared);
1218 kunmap_atomic(sb, KM_USER0);
1219 write_page(bitmap, bitmap->sb_page, 1);
1220 }
NeilBrown32a76272005-06-21 17:17:14 -07001221 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownece5cff2009-12-14 12:49:56 +11001222 if (!bitmap->need_sync)
NeilBrown5a537df2011-09-21 15:37:46 +10001223 clear_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001224 else
1225 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001226 }
NeilBrowndb305e52009-05-07 12:49:06 +10001227 bmc = bitmap_get_counter(bitmap,
1228 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1229 &blocks, 0);
NeilBrown5a537df2011-09-21 15:37:46 +10001230 if (!bmc)
1231 j |= PAGE_COUNTER_MASK;
1232 else if (*bmc) {
NeilBrown5a537df2011-09-21 15:37:46 +10001233 if (*bmc == 1 && !bitmap->need_sync) {
NeilBrown32a76272005-06-21 17:17:14 -07001234 /* we can clear the bit */
1235 *bmc = 0;
NeilBrowndb305e52009-05-07 12:49:06 +10001236 bitmap_count_page(bitmap,
1237 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
NeilBrown32a76272005-06-21 17:17:14 -07001238 -1);
1239
1240 /* clear the bit */
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001241 paddr = kmap_atomic(page, KM_USER0);
1242 if (bitmap->flags & BITMAP_HOSTENDIAN)
1243 clear_bit(file_page_offset(bitmap, j),
1244 paddr);
1245 else
1246 __clear_bit_le(
NeilBrown5a537df2011-09-21 15:37:46 +10001247 file_page_offset(bitmap,
1248 j),
1249 paddr);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001250 kunmap_atomic(paddr, KM_USER0);
NeilBrown5a537df2011-09-21 15:37:46 +10001251 } else if (*bmc <= 2) {
1252 *bmc = 1; /* maybe clear the bit next time */
1253 set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001254 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001255 }
NeilBrown5a537df2011-09-21 15:37:46 +10001256 }
NeilBrown32a76272005-06-21 17:17:14 -07001257 }
NeilBrownbe512692009-05-26 09:41:17 +10001258 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001259
1260 /* now sync the final page */
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001261 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001262 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001263 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001264 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1265 spin_unlock_irqrestore(&bitmap->lock, flags);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001266 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001267 } else {
1268 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
NeilBrown2585f3e2011-09-21 15:37:46 +10001269 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001270 spin_unlock_irqrestore(&bitmap->lock, flags);
1271 }
NeilBrown32a76272005-06-21 17:17:14 -07001272 }
1273
NeilBrown7be3dfe2008-03-10 11:43:48 -07001274 done:
NeilBrown8311c292008-03-04 14:29:30 -08001275 if (bitmap->allclean == 0)
NeilBrown2e61ebb2011-12-23 10:17:50 +11001276 mddev->thread->timeout =
1277 mddev->bitmap_info.daemon_sleep;
NeilBrownc3d97142009-12-14 12:49:52 +11001278 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown32a76272005-06-21 17:17:14 -07001279}
1280
NeilBrown32a76272005-06-21 17:17:14 -07001281static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001282 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001283 int create)
NeilBrownee305ac2009-09-23 18:06:44 +10001284__releases(bitmap->lock)
1285__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -07001286{
1287 /* If 'create', we might release the lock and reclaim it.
1288 * The lock must have been taken with interrupts enabled.
1289 * If !create, we don't release the lock.
1290 */
1291 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1292 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1293 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1294 sector_t csize;
NeilBrownef425672010-06-01 19:37:33 +10001295 int err;
NeilBrown32a76272005-06-21 17:17:14 -07001296
NeilBrownef425672010-06-01 19:37:33 +10001297 err = bitmap_checkpage(bitmap, page, create);
1298
1299 if (bitmap->bp[page].hijacked ||
1300 bitmap->bp[page].map == NULL)
1301 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1302 PAGE_COUNTER_SHIFT - 1);
1303 else
NeilBrown32a76272005-06-21 17:17:14 -07001304 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
NeilBrownef425672010-06-01 19:37:33 +10001305 *blocks = csize - (offset & (csize - 1));
1306
1307 if (err < 0)
NeilBrown32a76272005-06-21 17:17:14 -07001308 return NULL;
NeilBrownef425672010-06-01 19:37:33 +10001309
NeilBrown32a76272005-06-21 17:17:14 -07001310 /* now locked ... */
1311
1312 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1313 /* should we use the first or second counter field
1314 * of the hijacked pointer? */
1315 int hi = (pageoff > PAGE_COUNTER_MASK);
NeilBrown32a76272005-06-21 17:17:14 -07001316 return &((bitmap_counter_t *)
1317 &bitmap->bp[page].map)[hi];
NeilBrownef425672010-06-01 19:37:33 +10001318 } else /* page is allocated */
NeilBrown32a76272005-06-21 17:17:14 -07001319 return (bitmap_counter_t *)
1320 &(bitmap->bp[page].map[pageoff]);
NeilBrown32a76272005-06-21 17:17:14 -07001321}
1322
NeilBrown4b6d2872005-09-09 16:23:47 -07001323int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001324{
NeilBrownac2f40b2010-06-01 19:37:31 +10001325 if (!bitmap)
1326 return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001327
1328 if (behind) {
Paul Clements696fcd52010-03-08 16:02:37 +11001329 int bw;
NeilBrown4b6d2872005-09-09 16:23:47 -07001330 atomic_inc(&bitmap->behind_writes);
Paul Clements696fcd52010-03-08 16:02:37 +11001331 bw = atomic_read(&bitmap->behind_writes);
1332 if (bw > bitmap->behind_writes_used)
1333 bitmap->behind_writes_used = bw;
1334
NeilBrown36a4e1f2011-10-07 14:23:17 +11001335 pr_debug("inc write-behind count %d/%lu\n",
1336 bw, bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001337 }
1338
NeilBrown32a76272005-06-21 17:17:14 -07001339 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001340 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001341 bitmap_counter_t *bmc;
1342
1343 spin_lock_irq(&bitmap->lock);
1344 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1345 if (!bmc) {
1346 spin_unlock_irq(&bitmap->lock);
1347 return 0;
1348 }
1349
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001350 if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
Neil Brownda6e1a32007-02-08 14:20:37 -08001351 DEFINE_WAIT(__wait);
1352 /* note that it is safe to do the prepare_to_wait
1353 * after the test as long as we do it before dropping
1354 * the spinlock.
1355 */
1356 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1357 TASK_UNINTERRUPTIBLE);
1358 spin_unlock_irq(&bitmap->lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001359 io_schedule();
Neil Brownda6e1a32007-02-08 14:20:37 -08001360 finish_wait(&bitmap->overflow_wait, &__wait);
1361 continue;
1362 }
1363
NeilBrownac2f40b2010-06-01 19:37:31 +10001364 switch (*bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001365 case 0:
1366 bitmap_file_set_bit(bitmap, offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001367 bitmap_count_page(bitmap, offset, 1);
NeilBrown32a76272005-06-21 17:17:14 -07001368 /* fall through */
1369 case 1:
1370 *bmc = 2;
1371 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001372
NeilBrown32a76272005-06-21 17:17:14 -07001373 (*bmc)++;
1374
1375 spin_unlock_irq(&bitmap->lock);
1376
1377 offset += blocks;
1378 if (sectors > blocks)
1379 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001380 else
1381 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001382 }
1383 return 0;
1384}
NeilBrownac2f40b2010-06-01 19:37:31 +10001385EXPORT_SYMBOL(bitmap_startwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001386
1387void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001388 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001389{
NeilBrownac2f40b2010-06-01 19:37:31 +10001390 if (!bitmap)
1391 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001392 if (behind) {
NeilBrowne5551902010-03-31 11:21:44 +11001393 if (atomic_dec_and_test(&bitmap->behind_writes))
1394 wake_up(&bitmap->behind_wait);
NeilBrown36a4e1f2011-10-07 14:23:17 +11001395 pr_debug("dec write-behind count %d/%lu\n",
1396 atomic_read(&bitmap->behind_writes),
1397 bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001398 }
1399
NeilBrown32a76272005-06-21 17:17:14 -07001400 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001401 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001402 unsigned long flags;
1403 bitmap_counter_t *bmc;
1404
1405 spin_lock_irqsave(&bitmap->lock, flags);
1406 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1407 if (!bmc) {
1408 spin_unlock_irqrestore(&bitmap->lock, flags);
1409 return;
1410 }
1411
NeilBrown961902c2011-12-23 09:57:48 +11001412 if (success && !bitmap->mddev->degraded &&
Neil Browna0da84f2008-06-28 08:31:22 +10001413 bitmap->events_cleared < bitmap->mddev->events) {
1414 bitmap->events_cleared = bitmap->mddev->events;
1415 bitmap->need_sync = 1;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001416 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
Neil Browna0da84f2008-06-28 08:31:22 +10001417 }
1418
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001419 if (!success && !NEEDED(*bmc))
NeilBrown32a76272005-06-21 17:17:14 -07001420 *bmc |= NEEDED_MASK;
1421
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001422 if (COUNTER(*bmc) == COUNTER_MAX)
Neil Brownda6e1a32007-02-08 14:20:37 -08001423 wake_up(&bitmap->overflow_wait);
1424
NeilBrown32a76272005-06-21 17:17:14 -07001425 (*bmc)--;
NeilBrown2585f3e2011-09-21 15:37:46 +10001426 if (*bmc <= 2) {
NeilBrown32a76272005-06-21 17:17:14 -07001427 set_page_attr(bitmap,
NeilBrowne384e582010-06-01 19:37:34 +10001428 filemap_get_page(
1429 bitmap,
1430 offset >> CHUNK_BLOCK_SHIFT(bitmap)),
NeilBrown5a537df2011-09-21 15:37:46 +10001431 BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001432 bitmap->allclean = 0;
1433 }
NeilBrown32a76272005-06-21 17:17:14 -07001434 spin_unlock_irqrestore(&bitmap->lock, flags);
1435 offset += blocks;
1436 if (sectors > blocks)
1437 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001438 else
1439 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001440 }
1441}
NeilBrownac2f40b2010-06-01 19:37:31 +10001442EXPORT_SYMBOL(bitmap_endwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001443
NeilBrown57dab0b2010-10-19 10:03:39 +11001444static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001445 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001446{
1447 bitmap_counter_t *bmc;
1448 int rv;
1449 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1450 *blocks = 1024;
1451 return 1; /* always resync if no bitmap */
1452 }
1453 spin_lock_irq(&bitmap->lock);
1454 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1455 rv = 0;
1456 if (bmc) {
1457 /* locked */
1458 if (RESYNC(*bmc))
1459 rv = 1;
1460 else if (NEEDED(*bmc)) {
1461 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001462 if (!degraded) { /* don't set/clear bits if degraded */
1463 *bmc |= RESYNC_MASK;
1464 *bmc &= ~NEEDED_MASK;
1465 }
NeilBrown32a76272005-06-21 17:17:14 -07001466 }
1467 }
1468 spin_unlock_irq(&bitmap->lock);
1469 return rv;
1470}
1471
NeilBrown57dab0b2010-10-19 10:03:39 +11001472int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001473 int degraded)
1474{
1475 /* bitmap_start_sync must always report on multiples of whole
1476 * pages, otherwise resync (which is very PAGE_SIZE based) will
1477 * get confused.
1478 * So call __bitmap_start_sync repeatedly (if needed) until
1479 * At least PAGE_SIZE>>9 blocks are covered.
1480 * Return the 'or' of the result.
1481 */
1482 int rv = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001483 sector_t blocks1;
NeilBrown1187cf02009-03-31 14:27:02 +11001484
1485 *blocks = 0;
1486 while (*blocks < (PAGE_SIZE>>9)) {
1487 rv |= __bitmap_start_sync(bitmap, offset,
1488 &blocks1, degraded);
1489 offset += blocks1;
1490 *blocks += blocks1;
1491 }
1492 return rv;
1493}
NeilBrownac2f40b2010-06-01 19:37:31 +10001494EXPORT_SYMBOL(bitmap_start_sync);
NeilBrown1187cf02009-03-31 14:27:02 +11001495
NeilBrown57dab0b2010-10-19 10:03:39 +11001496void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
NeilBrown32a76272005-06-21 17:17:14 -07001497{
1498 bitmap_counter_t *bmc;
1499 unsigned long flags;
NeilBrownac2f40b2010-06-01 19:37:31 +10001500
1501 if (bitmap == NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001502 *blocks = 1024;
1503 return;
1504 }
1505 spin_lock_irqsave(&bitmap->lock, flags);
1506 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1507 if (bmc == NULL)
1508 goto unlock;
1509 /* locked */
NeilBrown32a76272005-06-21 17:17:14 -07001510 if (RESYNC(*bmc)) {
1511 *bmc &= ~RESYNC_MASK;
1512
1513 if (!NEEDED(*bmc) && aborted)
1514 *bmc |= NEEDED_MASK;
1515 else {
NeilBrown2585f3e2011-09-21 15:37:46 +10001516 if (*bmc <= 2) {
NeilBrown32a76272005-06-21 17:17:14 -07001517 set_page_attr(bitmap,
1518 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
NeilBrown5a537df2011-09-21 15:37:46 +10001519 BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001520 bitmap->allclean = 0;
1521 }
NeilBrown32a76272005-06-21 17:17:14 -07001522 }
1523 }
1524 unlock:
1525 spin_unlock_irqrestore(&bitmap->lock, flags);
1526}
NeilBrownac2f40b2010-06-01 19:37:31 +10001527EXPORT_SYMBOL(bitmap_end_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001528
1529void bitmap_close_sync(struct bitmap *bitmap)
1530{
1531 /* Sync has finished, and any bitmap chunks that weren't synced
1532 * properly have been aborted. It remains to us to clear the
1533 * RESYNC bit wherever it is still on
1534 */
1535 sector_t sector = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001536 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001537 if (!bitmap)
1538 return;
NeilBrown32a76272005-06-21 17:17:14 -07001539 while (sector < bitmap->mddev->resync_max_sectors) {
1540 bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001541 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001542 }
1543}
NeilBrownac2f40b2010-06-01 19:37:31 +10001544EXPORT_SYMBOL(bitmap_close_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001545
NeilBrownb47490c2008-02-06 01:39:50 -08001546void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1547{
1548 sector_t s = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001549 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001550
1551 if (!bitmap)
1552 return;
1553 if (sector == 0) {
1554 bitmap->last_end_sync = jiffies;
1555 return;
1556 }
1557 if (time_before(jiffies, (bitmap->last_end_sync
NeilBrown1b04be92009-12-14 12:49:53 +11001558 + bitmap->mddev->bitmap_info.daemon_sleep)))
NeilBrownb47490c2008-02-06 01:39:50 -08001559 return;
1560 wait_event(bitmap->mddev->recovery_wait,
1561 atomic_read(&bitmap->mddev->recovery_active) == 0);
1562
NeilBrown75d3da42011-01-14 09:14:34 +11001563 bitmap->mddev->curr_resync_completed = sector;
NeilBrown070dc6d2010-08-30 17:33:34 +10001564 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
NeilBrownb47490c2008-02-06 01:39:50 -08001565 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1566 s = 0;
1567 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1568 bitmap_end_sync(bitmap, s, &blocks, 0);
1569 s += blocks;
1570 }
1571 bitmap->last_end_sync = jiffies;
NeilBrownacb180b2009-04-14 16:28:34 +10001572 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
NeilBrownb47490c2008-02-06 01:39:50 -08001573}
NeilBrownac2f40b2010-06-01 19:37:31 +10001574EXPORT_SYMBOL(bitmap_cond_end_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08001575
NeilBrown6a079972005-09-09 16:23:44 -07001576static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001577{
1578 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001579 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001580 * be 0 at this point
1581 */
NeilBrown193f1c92005-08-04 12:53:33 -07001582
NeilBrown57dab0b2010-10-19 10:03:39 +11001583 sector_t secs;
NeilBrown193f1c92005-08-04 12:53:33 -07001584 bitmap_counter_t *bmc;
1585 spin_lock_irq(&bitmap->lock);
1586 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1587 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001588 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001589 return;
NeilBrown32a76272005-06-21 17:17:14 -07001590 }
NeilBrownac2f40b2010-06-01 19:37:31 +10001591 if (!*bmc) {
NeilBrown193f1c92005-08-04 12:53:33 -07001592 struct page *page;
NeilBrown915c4202011-12-23 10:17:51 +11001593 *bmc = 2 | (needed ? NEEDED_MASK : 0);
NeilBrown193f1c92005-08-04 12:53:33 -07001594 bitmap_count_page(bitmap, offset, 1);
1595 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
NeilBrown5a537df2011-09-21 15:37:46 +10001596 set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001597 bitmap->allclean = 0;
NeilBrown193f1c92005-08-04 12:53:33 -07001598 }
1599 spin_unlock_irq(&bitmap->lock);
NeilBrown32a76272005-06-21 17:17:14 -07001600}
1601
Paul Clements9b1d1da2006-10-03 01:15:49 -07001602/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1603void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1604{
1605 unsigned long chunk;
1606
1607 for (chunk = s; chunk <= e; chunk++) {
NeilBrowndb305e52009-05-07 12:49:06 +10001608 sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001609 bitmap_set_memory_bits(bitmap, sec, 1);
NeilBrown7c8f4242011-11-23 10:18:52 +11001610 spin_lock_irq(&bitmap->lock);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001611 bitmap_file_set_bit(bitmap, sec);
NeilBrown7c8f4242011-11-23 10:18:52 +11001612 spin_unlock_irq(&bitmap->lock);
NeilBrownffa23322009-12-14 12:49:56 +11001613 if (sec < bitmap->mddev->recovery_cp)
1614 /* We are asserting that the array is dirty,
1615 * so move the recovery_cp address back so
1616 * that it is obvious that it is dirty
1617 */
1618 bitmap->mddev->recovery_cp = sec;
Paul Clements9b1d1da2006-10-03 01:15:49 -07001619 }
1620}
1621
NeilBrown32a76272005-06-21 17:17:14 -07001622/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001623 * flush out any pending updates
1624 */
NeilBrownfd01b882011-10-11 16:47:53 +11001625void bitmap_flush(struct mddev *mddev)
NeilBrown6b8b3e82005-08-04 12:53:35 -07001626{
1627 struct bitmap *bitmap = mddev->bitmap;
NeilBrown42a04b52009-12-14 12:49:53 +11001628 long sleep;
NeilBrown6b8b3e82005-08-04 12:53:35 -07001629
1630 if (!bitmap) /* there was no bitmap */
1631 return;
1632
1633 /* run the daemon_work three time to ensure everything is flushed
1634 * that can be
1635 */
NeilBrown1b04be92009-12-14 12:49:53 +11001636 sleep = mddev->bitmap_info.daemon_sleep * 2;
NeilBrown42a04b52009-12-14 12:49:53 +11001637 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001638 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001639 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001640 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001641 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001642 bitmap_daemon_work(mddev);
NeilBrown6b8b3e82005-08-04 12:53:35 -07001643 bitmap_update_sb(bitmap);
1644}
1645
1646/*
NeilBrown32a76272005-06-21 17:17:14 -07001647 * free memory that was allocated
1648 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001649static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001650{
1651 unsigned long k, pages;
1652 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001653
1654 if (!bitmap) /* there was no bitmap */
1655 return;
1656
NeilBrown32a76272005-06-21 17:17:14 -07001657 /* release the bitmap file and kill the daemon */
1658 bitmap_file_put(bitmap);
1659
1660 bp = bitmap->bp;
1661 pages = bitmap->pages;
1662
1663 /* free all allocated memory */
1664
NeilBrown32a76272005-06-21 17:17:14 -07001665 if (bp) /* deallocate the page memory */
1666 for (k = 0; k < pages; k++)
1667 if (bp[k].map && !bp[k].hijacked)
1668 kfree(bp[k].map);
1669 kfree(bp);
1670 kfree(bitmap);
1671}
NeilBrownaa5cbd12009-12-14 12:49:46 +11001672
NeilBrownfd01b882011-10-11 16:47:53 +11001673void bitmap_destroy(struct mddev *mddev)
NeilBrown3178b0d2005-09-09 16:23:50 -07001674{
1675 struct bitmap *bitmap = mddev->bitmap;
1676
1677 if (!bitmap) /* there was no bitmap */
1678 return;
1679
NeilBrownc3d97142009-12-14 12:49:52 +11001680 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrown3178b0d2005-09-09 16:23:50 -07001681 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownc3d97142009-12-14 12:49:52 +11001682 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrownb15c2e52006-01-06 00:20:16 -08001683 if (mddev->thread)
1684 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001685
NeilBrownece5cff2009-12-14 12:49:56 +11001686 if (bitmap->sysfs_can_clear)
1687 sysfs_put(bitmap->sysfs_can_clear);
1688
NeilBrown3178b0d2005-09-09 16:23:50 -07001689 bitmap_free(bitmap);
1690}
NeilBrown32a76272005-06-21 17:17:14 -07001691
1692/*
1693 * initialize the bitmap structure
1694 * if this returns an error, bitmap_destroy must be called to do clean up
1695 */
NeilBrownfd01b882011-10-11 16:47:53 +11001696int bitmap_create(struct mddev *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001697{
1698 struct bitmap *bitmap;
NeilBrown1f593902009-04-20 11:50:24 +10001699 sector_t blocks = mddev->resync_max_sectors;
NeilBrown32a76272005-06-21 17:17:14 -07001700 unsigned long chunks;
1701 unsigned long pages;
NeilBrownc3d97142009-12-14 12:49:52 +11001702 struct file *file = mddev->bitmap_info.file;
NeilBrown32a76272005-06-21 17:17:14 -07001703 int err;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001704 struct sysfs_dirent *bm = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001705
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001706 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001707
NeilBrowne384e582010-06-01 19:37:34 +10001708 if (!file
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001709 && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001710 return 0;
1711
NeilBrownc3d97142009-12-14 12:49:52 +11001712 BUG_ON(file && mddev->bitmap_info.offset);
NeilBrowna654b9d82005-06-21 17:17:27 -07001713
NeilBrown9ffae0c2006-01-06 00:20:32 -08001714 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001715 if (!bitmap)
1716 return -ENOMEM;
1717
NeilBrown32a76272005-06-21 17:17:14 -07001718 spin_lock_init(&bitmap->lock);
NeilBrownce25c312006-06-26 00:27:49 -07001719 atomic_set(&bitmap->pending_writes, 0);
1720 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001721 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrowne5551902010-03-31 11:21:44 +11001722 init_waitqueue_head(&bitmap->behind_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001723
NeilBrown32a76272005-06-21 17:17:14 -07001724 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001725
NeilBrown5ff5aff2010-06-01 19:37:32 +10001726 if (mddev->kobj.sd)
1727 bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
NeilBrownece5cff2009-12-14 12:49:56 +11001728 if (bm) {
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07001729 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
NeilBrownece5cff2009-12-14 12:49:56 +11001730 sysfs_put(bm);
1731 } else
1732 bitmap->sysfs_can_clear = NULL;
1733
NeilBrown32a76272005-06-21 17:17:14 -07001734 bitmap->file = file;
NeilBrownce25c312006-06-26 00:27:49 -07001735 if (file) {
1736 get_file(file);
NeilBrownae8fa282009-10-16 15:56:01 +11001737 /* As future accesses to this file will use bmap,
1738 * and bypass the page cache, we must sync the file
1739 * first.
1740 */
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001741 vfs_fsync(file, 1);
NeilBrownce25c312006-06-26 00:27:49 -07001742 }
NeilBrown42a04b52009-12-14 12:49:53 +11001743 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
Jonathan Brassow9c810752011-06-08 17:59:30 -05001744 if (!mddev->bitmap_info.external) {
1745 /*
1746 * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
1747 * instructing us to create a new on-disk bitmap instance.
1748 */
1749 if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
1750 err = bitmap_new_disk_sb(bitmap);
1751 else
1752 err = bitmap_read_sb(bitmap);
1753 } else {
NeilBrownece5cff2009-12-14 12:49:56 +11001754 err = 0;
1755 if (mddev->bitmap_info.chunksize == 0 ||
1756 mddev->bitmap_info.daemon_sleep == 0)
1757 /* chunksize and time_base need to be
1758 * set first. */
1759 err = -EINVAL;
1760 }
NeilBrown32a76272005-06-21 17:17:14 -07001761 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001762 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001763
NeilBrown624ce4f2009-12-14 12:49:56 +11001764 bitmap->daemon_lastrun = jiffies;
NeilBrown42a04b52009-12-14 12:49:53 +11001765 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
NeilBrown32a76272005-06-21 17:17:14 -07001766
1767 /* now that chunksize and chunkshift are set, we can use these macros */
NeilBrownac2f40b2010-06-01 19:37:31 +10001768 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
NeilBrown1f593902009-04-20 11:50:24 +10001769 CHUNK_BLOCK_SHIFT(bitmap);
NeilBrownac2f40b2010-06-01 19:37:31 +10001770 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
NeilBrown32a76272005-06-21 17:17:14 -07001771
1772 BUG_ON(!pages);
1773
1774 bitmap->chunks = chunks;
1775 bitmap->pages = pages;
1776 bitmap->missing_pages = pages;
NeilBrown32a76272005-06-21 17:17:14 -07001777
NeilBrown9ffae0c2006-01-06 00:20:32 -08001778 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown29d32472011-10-11 16:49:56 +11001779
NeilBrown3178b0d2005-09-09 16:23:50 -07001780 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001781 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001782 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001783
NeilBrown69e51b42010-06-01 19:37:35 +10001784 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1785 pages, bmname(bitmap));
1786
1787 mddev->bitmap = bitmap;
1788
1789
1790 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
1791
1792 error:
1793 bitmap_free(bitmap);
1794 return err;
1795}
1796
NeilBrownfd01b882011-10-11 16:47:53 +11001797int bitmap_load(struct mddev *mddev)
NeilBrown69e51b42010-06-01 19:37:35 +10001798{
1799 int err = 0;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001800 sector_t start = 0;
NeilBrown69e51b42010-06-01 19:37:35 +10001801 sector_t sector = 0;
1802 struct bitmap *bitmap = mddev->bitmap;
1803
1804 if (!bitmap)
1805 goto out;
1806
1807 /* Clear out old bitmap info first: Either there is none, or we
1808 * are resuming after someone else has possibly changed things,
1809 * so we should forget old cached info.
1810 * All chunks should be clean, but some might need_sync.
1811 */
1812 while (sector < mddev->resync_max_sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001813 sector_t blocks;
NeilBrown69e51b42010-06-01 19:37:35 +10001814 bitmap_start_sync(bitmap, sector, &blocks, 0);
1815 sector += blocks;
1816 }
1817 bitmap_close_sync(bitmap);
1818
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001819 if (mddev->degraded == 0
1820 || bitmap->events_cleared == mddev->events)
1821 /* no need to keep dirty bits to optimise a
1822 * re-add of a missing device */
1823 start = mddev->recovery_cp;
NeilBrown69e51b42010-06-01 19:37:35 +10001824
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001825 err = bitmap_init_from_disk(bitmap, start);
1826
NeilBrown32a76272005-06-21 17:17:14 -07001827 if (err)
NeilBrown69e51b42010-06-01 19:37:35 +10001828 goto out;
NeilBrown3178b0d2005-09-09 16:23:50 -07001829
NeilBrown1b04be92009-12-14 12:49:53 +11001830 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
NeilBrown9cd30fd2009-12-14 12:49:54 +11001831 md_wakeup_thread(mddev->thread);
NeilBrownb15c2e52006-01-06 00:20:16 -08001832
NeilBrown4ad13662007-07-17 04:06:13 -07001833 bitmap_update_sb(bitmap);
1834
NeilBrown69e51b42010-06-01 19:37:35 +10001835 if (bitmap->flags & BITMAP_WRITE_ERROR)
1836 err = -EIO;
1837out:
NeilBrown3178b0d2005-09-09 16:23:50 -07001838 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001839}
NeilBrown69e51b42010-06-01 19:37:35 +10001840EXPORT_SYMBOL_GPL(bitmap_load);
NeilBrown32a76272005-06-21 17:17:14 -07001841
NeilBrown57148962012-03-19 12:46:40 +11001842void bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
1843{
1844 unsigned long chunk_kb;
1845 unsigned long flags;
1846
1847 if (!bitmap)
1848 return;
1849
1850 spin_lock_irqsave(&bitmap->lock, flags);
1851 chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
1852 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
1853 "%lu%s chunk",
1854 bitmap->pages - bitmap->missing_pages,
1855 bitmap->pages,
1856 (bitmap->pages - bitmap->missing_pages)
1857 << (PAGE_SHIFT - 10),
1858 chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
1859 chunk_kb ? "KB" : "B");
1860 if (bitmap->file) {
1861 seq_printf(seq, ", file: ");
1862 seq_path(seq, &bitmap->file->f_path, " \t\n");
1863 }
1864
1865 seq_printf(seq, "\n");
1866 spin_unlock_irqrestore(&bitmap->lock, flags);
1867}
1868
NeilBrown43a70502009-12-14 12:49:55 +11001869static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001870location_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11001871{
1872 ssize_t len;
NeilBrownac2f40b2010-06-01 19:37:31 +10001873 if (mddev->bitmap_info.file)
NeilBrown43a70502009-12-14 12:49:55 +11001874 len = sprintf(page, "file");
NeilBrownac2f40b2010-06-01 19:37:31 +10001875 else if (mddev->bitmap_info.offset)
NeilBrown43a70502009-12-14 12:49:55 +11001876 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001877 else
NeilBrown43a70502009-12-14 12:49:55 +11001878 len = sprintf(page, "none");
1879 len += sprintf(page+len, "\n");
1880 return len;
1881}
1882
1883static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001884location_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11001885{
1886
1887 if (mddev->pers) {
1888 if (!mddev->pers->quiesce)
1889 return -EBUSY;
1890 if (mddev->recovery || mddev->sync_thread)
1891 return -EBUSY;
1892 }
1893
1894 if (mddev->bitmap || mddev->bitmap_info.file ||
1895 mddev->bitmap_info.offset) {
1896 /* bitmap already configured. Only option is to clear it */
1897 if (strncmp(buf, "none", 4) != 0)
1898 return -EBUSY;
1899 if (mddev->pers) {
1900 mddev->pers->quiesce(mddev, 1);
1901 bitmap_destroy(mddev);
1902 mddev->pers->quiesce(mddev, 0);
1903 }
1904 mddev->bitmap_info.offset = 0;
1905 if (mddev->bitmap_info.file) {
1906 struct file *f = mddev->bitmap_info.file;
1907 mddev->bitmap_info.file = NULL;
1908 restore_bitmap_write_access(f);
1909 fput(f);
1910 }
1911 } else {
1912 /* No bitmap, OK to set a location */
1913 long long offset;
1914 if (strncmp(buf, "none", 4) == 0)
1915 /* nothing to be done */;
1916 else if (strncmp(buf, "file:", 5) == 0) {
1917 /* Not supported yet */
1918 return -EINVAL;
1919 } else {
1920 int rv;
1921 if (buf[0] == '+')
1922 rv = strict_strtoll(buf+1, 10, &offset);
1923 else
1924 rv = strict_strtoll(buf, 10, &offset);
1925 if (rv)
1926 return rv;
1927 if (offset == 0)
1928 return -EINVAL;
NeilBrownece5cff2009-12-14 12:49:56 +11001929 if (mddev->bitmap_info.external == 0 &&
1930 mddev->major_version == 0 &&
NeilBrown43a70502009-12-14 12:49:55 +11001931 offset != mddev->bitmap_info.default_offset)
1932 return -EINVAL;
1933 mddev->bitmap_info.offset = offset;
1934 if (mddev->pers) {
1935 mddev->pers->quiesce(mddev, 1);
1936 rv = bitmap_create(mddev);
NeilBrown4474ca42012-03-19 12:46:37 +11001937 if (!rv)
1938 rv = bitmap_load(mddev);
NeilBrown43a70502009-12-14 12:49:55 +11001939 if (rv) {
1940 bitmap_destroy(mddev);
1941 mddev->bitmap_info.offset = 0;
1942 }
1943 mddev->pers->quiesce(mddev, 0);
1944 if (rv)
1945 return rv;
1946 }
1947 }
1948 }
1949 if (!mddev->external) {
1950 /* Ensure new bitmap info is stored in
1951 * metadata promptly.
1952 */
1953 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1954 md_wakeup_thread(mddev->thread);
1955 }
1956 return len;
1957}
1958
1959static struct md_sysfs_entry bitmap_location =
1960__ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
1961
1962static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001963timeout_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11001964{
1965 ssize_t len;
1966 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
1967 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
NeilBrownac2f40b2010-06-01 19:37:31 +10001968
NeilBrown43a70502009-12-14 12:49:55 +11001969 len = sprintf(page, "%lu", secs);
1970 if (jifs)
1971 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
1972 len += sprintf(page+len, "\n");
1973 return len;
1974}
1975
1976static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001977timeout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11001978{
1979 /* timeout can be set at any time */
1980 unsigned long timeout;
1981 int rv = strict_strtoul_scaled(buf, &timeout, 4);
1982 if (rv)
1983 return rv;
1984
1985 /* just to make sure we don't overflow... */
1986 if (timeout >= LONG_MAX / HZ)
1987 return -EINVAL;
1988
1989 timeout = timeout * HZ / 10000;
1990
1991 if (timeout >= MAX_SCHEDULE_TIMEOUT)
1992 timeout = MAX_SCHEDULE_TIMEOUT-1;
1993 if (timeout < 1)
1994 timeout = 1;
1995 mddev->bitmap_info.daemon_sleep = timeout;
1996 if (mddev->thread) {
1997 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1998 * the bitmap is all clean and we don't need to
1999 * adjust the timeout right now
2000 */
2001 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
2002 mddev->thread->timeout = timeout;
2003 md_wakeup_thread(mddev->thread);
2004 }
2005 }
2006 return len;
2007}
2008
2009static struct md_sysfs_entry bitmap_timeout =
2010__ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
2011
2012static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002013backlog_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002014{
2015 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
2016}
2017
2018static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002019backlog_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002020{
2021 unsigned long backlog;
2022 int rv = strict_strtoul(buf, 10, &backlog);
2023 if (rv)
2024 return rv;
2025 if (backlog > COUNTER_MAX)
2026 return -EINVAL;
2027 mddev->bitmap_info.max_write_behind = backlog;
2028 return len;
2029}
2030
2031static struct md_sysfs_entry bitmap_backlog =
2032__ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
2033
2034static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002035chunksize_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002036{
2037 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
2038}
2039
2040static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002041chunksize_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002042{
2043 /* Can only be changed when no bitmap is active */
2044 int rv;
2045 unsigned long csize;
2046 if (mddev->bitmap)
2047 return -EBUSY;
2048 rv = strict_strtoul(buf, 10, &csize);
2049 if (rv)
2050 return rv;
2051 if (csize < 512 ||
2052 !is_power_of_2(csize))
2053 return -EINVAL;
2054 mddev->bitmap_info.chunksize = csize;
2055 return len;
2056}
2057
2058static struct md_sysfs_entry bitmap_chunksize =
2059__ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
2060
NeilBrownfd01b882011-10-11 16:47:53 +11002061static ssize_t metadata_show(struct mddev *mddev, char *page)
NeilBrownece5cff2009-12-14 12:49:56 +11002062{
2063 return sprintf(page, "%s\n", (mddev->bitmap_info.external
2064 ? "external" : "internal"));
2065}
2066
NeilBrownfd01b882011-10-11 16:47:53 +11002067static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownece5cff2009-12-14 12:49:56 +11002068{
2069 if (mddev->bitmap ||
2070 mddev->bitmap_info.file ||
2071 mddev->bitmap_info.offset)
2072 return -EBUSY;
2073 if (strncmp(buf, "external", 8) == 0)
2074 mddev->bitmap_info.external = 1;
2075 else if (strncmp(buf, "internal", 8) == 0)
2076 mddev->bitmap_info.external = 0;
2077 else
2078 return -EINVAL;
2079 return len;
2080}
2081
2082static struct md_sysfs_entry bitmap_metadata =
2083__ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2084
NeilBrownfd01b882011-10-11 16:47:53 +11002085static ssize_t can_clear_show(struct mddev *mddev, char *page)
NeilBrownece5cff2009-12-14 12:49:56 +11002086{
2087 int len;
2088 if (mddev->bitmap)
2089 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
2090 "false" : "true"));
2091 else
2092 len = sprintf(page, "\n");
2093 return len;
2094}
2095
NeilBrownfd01b882011-10-11 16:47:53 +11002096static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownece5cff2009-12-14 12:49:56 +11002097{
2098 if (mddev->bitmap == NULL)
2099 return -ENOENT;
2100 if (strncmp(buf, "false", 5) == 0)
2101 mddev->bitmap->need_sync = 1;
2102 else if (strncmp(buf, "true", 4) == 0) {
2103 if (mddev->degraded)
2104 return -EBUSY;
2105 mddev->bitmap->need_sync = 0;
2106 } else
2107 return -EINVAL;
2108 return len;
2109}
2110
2111static struct md_sysfs_entry bitmap_can_clear =
2112__ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
2113
Paul Clements696fcd52010-03-08 16:02:37 +11002114static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002115behind_writes_used_show(struct mddev *mddev, char *page)
Paul Clements696fcd52010-03-08 16:02:37 +11002116{
2117 if (mddev->bitmap == NULL)
2118 return sprintf(page, "0\n");
2119 return sprintf(page, "%lu\n",
2120 mddev->bitmap->behind_writes_used);
2121}
2122
2123static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002124behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
Paul Clements696fcd52010-03-08 16:02:37 +11002125{
2126 if (mddev->bitmap)
2127 mddev->bitmap->behind_writes_used = 0;
2128 return len;
2129}
2130
2131static struct md_sysfs_entry max_backlog_used =
2132__ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2133 behind_writes_used_show, behind_writes_used_reset);
2134
NeilBrown43a70502009-12-14 12:49:55 +11002135static struct attribute *md_bitmap_attrs[] = {
2136 &bitmap_location.attr,
2137 &bitmap_timeout.attr,
2138 &bitmap_backlog.attr,
2139 &bitmap_chunksize.attr,
NeilBrownece5cff2009-12-14 12:49:56 +11002140 &bitmap_metadata.attr,
2141 &bitmap_can_clear.attr,
Paul Clements696fcd52010-03-08 16:02:37 +11002142 &max_backlog_used.attr,
NeilBrown43a70502009-12-14 12:49:55 +11002143 NULL
2144};
2145struct attribute_group md_bitmap_group = {
2146 .name = "bitmap",
2147 .attrs = md_bitmap_attrs,
2148};
2149