blob: e12b515bd471fe8ea344aed64b791d2560870b1b [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;
NeilBrown32a76272005-06-21 17:17:14 -0700449
450 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
NeilBrown4ad13662007-07-17 04:06:13 -0700451 return;
NeilBrownece5cff2009-12-14 12:49:56 +1100452 if (bitmap->mddev->bitmap_info.external)
453 return;
NeilBrown5a6c8242012-03-19 12:46:40 +1100454 if (!bitmap->sb_page) /* no superblock */
NeilBrown4ad13662007-07-17 04:06:13 -0700455 return;
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100456 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700457 sb->events = cpu_to_le64(bitmap->mddev->events);
NeilBrown8258c532011-05-11 14:26:30 +1000458 if (bitmap->mddev->events < bitmap->events_cleared)
Neil Browna0da84f2008-06-28 08:31:22 +1000459 /* rocking back to read-only */
460 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown8258c532011-05-11 14:26:30 +1000461 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
462 sb->state = cpu_to_le32(bitmap->flags);
NeilBrown43a70502009-12-14 12:49:55 +1100463 /* Just in case these have been changed via sysfs: */
464 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
465 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
NeilBrownea03aff2006-01-06 00:20:34 -0800466 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700467 write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700468}
469
470/* print out the bitmap file superblock */
471void bitmap_print_sb(struct bitmap *bitmap)
472{
473 bitmap_super_t *sb;
474
475 if (!bitmap || !bitmap->sb_page)
476 return;
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100477 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700478 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700479 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
480 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
481 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700482 *(__u32 *)(sb->uuid+0),
483 *(__u32 *)(sb->uuid+4),
484 *(__u32 *)(sb->uuid+8),
485 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700486 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700487 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700488 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700489 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700490 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
491 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
492 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
493 printk(KERN_DEBUG " sync size: %llu KB\n",
494 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700495 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800496 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700497}
498
Jonathan Brassow9c810752011-06-08 17:59:30 -0500499/*
500 * bitmap_new_disk_sb
501 * @bitmap
502 *
503 * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
504 * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
505 * This function verifies 'bitmap_info' and populates the on-disk bitmap
506 * structure, which is to be written to disk.
507 *
508 * Returns: 0 on success, -Exxx on error
509 */
510static int bitmap_new_disk_sb(struct bitmap *bitmap)
511{
512 bitmap_super_t *sb;
513 unsigned long chunksize, daemon_sleep, write_behind;
514 int err = -EINVAL;
515
516 bitmap->sb_page = alloc_page(GFP_KERNEL);
517 if (IS_ERR(bitmap->sb_page)) {
518 err = PTR_ERR(bitmap->sb_page);
519 bitmap->sb_page = NULL;
520 return err;
521 }
522 bitmap->sb_page->index = 0;
523
524 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
525
526 sb->magic = cpu_to_le32(BITMAP_MAGIC);
527 sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
528
529 chunksize = bitmap->mddev->bitmap_info.chunksize;
530 BUG_ON(!chunksize);
531 if (!is_power_of_2(chunksize)) {
532 kunmap_atomic(sb, KM_USER0);
533 printk(KERN_ERR "bitmap chunksize not a power of 2\n");
534 return -EINVAL;
535 }
536 sb->chunksize = cpu_to_le32(chunksize);
537
538 daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
539 if (!daemon_sleep ||
540 (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
541 printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n");
542 daemon_sleep = 5 * HZ;
543 }
544 sb->daemon_sleep = cpu_to_le32(daemon_sleep);
545 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
546
547 /*
548 * FIXME: write_behind for RAID1. If not specified, what
549 * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
550 */
551 write_behind = bitmap->mddev->bitmap_info.max_write_behind;
552 if (write_behind > COUNTER_MAX)
553 write_behind = COUNTER_MAX / 2;
554 sb->write_behind = cpu_to_le32(write_behind);
555 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
556
557 /* keep the array size field of the bitmap superblock up to date */
558 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
559
560 memcpy(sb->uuid, bitmap->mddev->uuid, 16);
561
562 bitmap->flags |= BITMAP_STALE;
563 sb->state |= cpu_to_le32(BITMAP_STALE);
564 bitmap->events_cleared = bitmap->mddev->events;
565 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
566
567 bitmap->flags |= BITMAP_HOSTENDIAN;
568 sb->version = cpu_to_le32(BITMAP_MAJOR_HOSTENDIAN);
569
570 kunmap_atomic(sb, KM_USER0);
571
572 return 0;
573}
574
NeilBrown32a76272005-06-21 17:17:14 -0700575/* read the superblock from the bitmap file and initialize some bitmap fields */
576static int bitmap_read_sb(struct bitmap *bitmap)
577{
578 char *reason = NULL;
579 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700580 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700581 unsigned long long events;
582 int err = -EINVAL;
583
584 /* page 0 is the superblock, read it... */
NeilBrownf49d5e62007-01-26 00:57:03 -0800585 if (bitmap->file) {
586 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
587 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
588
589 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
590 } else {
NeilBrown42a04b52009-12-14 12:49:53 +1100591 bitmap->sb_page = read_sb_page(bitmap->mddev,
592 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100593 NULL,
594 0, sizeof(bitmap_super_t));
NeilBrowna654b9d82005-06-21 17:17:27 -0700595 }
NeilBrown32a76272005-06-21 17:17:14 -0700596 if (IS_ERR(bitmap->sb_page)) {
597 err = PTR_ERR(bitmap->sb_page);
598 bitmap->sb_page = NULL;
599 return err;
600 }
601
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100602 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700603
NeilBrown32a76272005-06-21 17:17:14 -0700604 chunksize = le32_to_cpu(sb->chunksize);
NeilBrown1b04be92009-12-14 12:49:53 +1100605 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
NeilBrown4b6d2872005-09-09 16:23:47 -0700606 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700607
608 /* verify that the bitmap-specific fields are valid */
609 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
610 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800611 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
612 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700613 reason = "unrecognized superblock version";
NeilBrown1187cf02009-03-31 14:27:02 +1100614 else if (chunksize < 512)
NeilBrown7dd5d342006-01-06 00:20:39 -0800615 reason = "bitmap chunksize too small";
Jonathan Brassowd7445402011-06-08 18:01:10 -0500616 else if (!is_power_of_2(chunksize))
NeilBrown32a76272005-06-21 17:17:14 -0700617 reason = "bitmap chunksize not a power of 2";
NeilBrown1b04be92009-12-14 12:49:53 +1100618 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
NeilBrown7dd5d342006-01-06 00:20:39 -0800619 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700620 else if (write_behind > COUNTER_MAX)
621 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700622 if (reason) {
623 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
624 bmname(bitmap), reason);
625 goto out;
626 }
627
628 /* keep the array size field of the bitmap superblock up to date */
629 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
630
NeilBrown278c1ca2012-03-19 12:46:40 +1100631 if (bitmap->mddev->persistent) {
632 /*
633 * We have a persistent array superblock, so compare the
634 * bitmap's UUID and event counter to the mddev's
635 */
636 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
637 printk(KERN_INFO
638 "%s: bitmap superblock UUID mismatch\n",
639 bmname(bitmap));
640 goto out;
641 }
642 events = le64_to_cpu(sb->events);
643 if (events < bitmap->mddev->events) {
644 printk(KERN_INFO
645 "%s: bitmap file is out of date (%llu < %llu) "
646 "-- forcing full recovery\n",
647 bmname(bitmap), events,
648 (unsigned long long) bitmap->mddev->events);
649 sb->state |= cpu_to_le32(BITMAP_STALE);
650 }
651 }
NeilBrown32a76272005-06-21 17:17:14 -0700652
NeilBrown32a76272005-06-21 17:17:14 -0700653 /* assign fields using values from superblock */
NeilBrown42a04b52009-12-14 12:49:53 +1100654 bitmap->mddev->bitmap_info.chunksize = chunksize;
655 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
NeilBrown42a04b52009-12-14 12:49:53 +1100656 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
NeilBrown4f2e6392006-10-21 10:24:09 -0700657 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800658 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
659 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700660 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown8258c532011-05-11 14:26:30 +1000661 if (bitmap->flags & BITMAP_STALE)
NeilBrown6a079972005-09-09 16:23:44 -0700662 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700663 err = 0;
664out:
NeilBrownea03aff2006-01-06 00:20:34 -0800665 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700666 if (err)
667 bitmap_print_sb(bitmap);
668 return err;
669}
670
671enum bitmap_mask_op {
672 MASK_SET,
673 MASK_UNSET
674};
675
NeilBrown4ad13662007-07-17 04:06:13 -0700676/* record the state of the bitmap in the superblock. Return the old value */
677static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
678 enum bitmap_mask_op op)
NeilBrown32a76272005-06-21 17:17:14 -0700679{
680 bitmap_super_t *sb;
NeilBrown4ad13662007-07-17 04:06:13 -0700681 int old;
NeilBrown32a76272005-06-21 17:17:14 -0700682
NeilBrown5a6c8242012-03-19 12:46:40 +1100683 if (!bitmap->sb_page) /* can't set the state */
NeilBrown4ad13662007-07-17 04:06:13 -0700684 return 0;
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100685 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700686 old = le32_to_cpu(sb->state) & bits;
NeilBrown32a76272005-06-21 17:17:14 -0700687 switch (op) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000688 case MASK_SET:
689 sb->state |= cpu_to_le32(bits);
NeilBrown8258c532011-05-11 14:26:30 +1000690 bitmap->flags |= bits;
NeilBrownac2f40b2010-06-01 19:37:31 +1000691 break;
692 case MASK_UNSET:
693 sb->state &= cpu_to_le32(~bits);
NeilBrown8258c532011-05-11 14:26:30 +1000694 bitmap->flags &= ~bits;
NeilBrownac2f40b2010-06-01 19:37:31 +1000695 break;
696 default:
697 BUG();
NeilBrown32a76272005-06-21 17:17:14 -0700698 }
NeilBrownea03aff2006-01-06 00:20:34 -0800699 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700700 return old;
NeilBrown32a76272005-06-21 17:17:14 -0700701}
702
703/*
704 * general bitmap file operations
705 */
706
NeilBrownece5cff2009-12-14 12:49:56 +1100707/*
708 * on-disk bitmap:
709 *
710 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
711 * file a page at a time. There's a superblock at the start of the file.
712 */
NeilBrown32a76272005-06-21 17:17:14 -0700713/* calculate the index of the page that contains this bit */
NeilBrownece5cff2009-12-14 12:49:56 +1100714static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700715{
NeilBrownece5cff2009-12-14 12:49:56 +1100716 if (!bitmap->mddev->bitmap_info.external)
717 chunk += sizeof(bitmap_super_t) << 3;
718 return chunk >> PAGE_BIT_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700719}
720
721/* calculate the (bit) offset of this bit within a page */
NeilBrownece5cff2009-12-14 12:49:56 +1100722static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700723{
NeilBrownece5cff2009-12-14 12:49:56 +1100724 if (!bitmap->mddev->bitmap_info.external)
725 chunk += sizeof(bitmap_super_t) << 3;
726 return chunk & (PAGE_BITS - 1);
NeilBrown32a76272005-06-21 17:17:14 -0700727}
728
729/*
730 * return a pointer to the page in the filemap that contains the given bit
731 *
732 * this lookup is complicated by the fact that the bitmap sb might be exactly
733 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
734 * 0 or page 1
735 */
736static inline struct page *filemap_get_page(struct bitmap *bitmap,
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000737 unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700738{
NeilBrownac2f40b2010-06-01 19:37:31 +1000739 if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
740 return NULL;
NeilBrownece5cff2009-12-14 12:49:56 +1100741 return bitmap->filemap[file_page_index(bitmap, chunk)
742 - file_page_index(bitmap, 0)];
NeilBrown32a76272005-06-21 17:17:14 -0700743}
744
NeilBrown32a76272005-06-21 17:17:14 -0700745static void bitmap_file_unmap(struct bitmap *bitmap)
746{
747 struct page **map, *sb_page;
748 unsigned long *attr;
749 int pages;
750 unsigned long flags;
751
752 spin_lock_irqsave(&bitmap->lock, flags);
753 map = bitmap->filemap;
754 bitmap->filemap = NULL;
755 attr = bitmap->filemap_attr;
756 bitmap->filemap_attr = NULL;
757 pages = bitmap->file_pages;
758 bitmap->file_pages = 0;
759 sb_page = bitmap->sb_page;
760 bitmap->sb_page = NULL;
761 spin_unlock_irqrestore(&bitmap->lock, flags);
762
763 while (pages--)
NeilBrownece5cff2009-12-14 12:49:56 +1100764 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700765 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700766 kfree(map);
767 kfree(attr);
768
NeilBrownd785a062006-06-26 00:27:48 -0700769 if (sb_page)
770 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700771}
772
773static void bitmap_file_put(struct bitmap *bitmap)
774{
775 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700776 unsigned long flags;
777
778 spin_lock_irqsave(&bitmap->lock, flags);
779 file = bitmap->file;
780 bitmap->file = NULL;
781 spin_unlock_irqrestore(&bitmap->lock, flags);
782
NeilBrownd785a062006-06-26 00:27:48 -0700783 if (file)
784 wait_event(bitmap->write_wait,
785 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700786 bitmap_file_unmap(bitmap);
787
NeilBrownd785a062006-06-26 00:27:48 -0700788 if (file) {
Josef Sipekc649bb92006-12-08 02:37:19 -0800789 struct inode *inode = file->f_path.dentry->d_inode;
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800790 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700791 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700792 }
NeilBrown32a76272005-06-21 17:17:14 -0700793}
794
NeilBrown32a76272005-06-21 17:17:14 -0700795/*
796 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
797 * then it is no longer reliable, so we stop using it and we mark the file
798 * as failed in the superblock
799 */
800static void bitmap_file_kick(struct bitmap *bitmap)
801{
802 char *path, *ptr = NULL;
803
NeilBrown4ad13662007-07-17 04:06:13 -0700804 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
805 bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700806
NeilBrown4ad13662007-07-17 04:06:13 -0700807 if (bitmap->file) {
808 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
809 if (path)
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700810 ptr = d_path(&bitmap->file->f_path, path,
811 PAGE_SIZE);
812
NeilBrown4ad13662007-07-17 04:06:13 -0700813 printk(KERN_ALERT
814 "%s: kicking failed bitmap file %s from array!\n",
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700815 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700816
NeilBrown4ad13662007-07-17 04:06:13 -0700817 kfree(path);
818 } else
819 printk(KERN_ALERT
820 "%s: disabling internal bitmap due to errors\n",
821 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700822 }
NeilBrown32a76272005-06-21 17:17:14 -0700823
824 bitmap_file_put(bitmap);
825
826 return;
827}
828
829enum bitmap_page_attr {
NeilBrownac2f40b2010-06-01 19:37:31 +1000830 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
NeilBrown5a537df2011-09-21 15:37:46 +1000831 BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
832 * i.e. counter is 1 or 2. */
NeilBrownac2f40b2010-06-01 19:37:31 +1000833 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
NeilBrown32a76272005-06-21 17:17:14 -0700834};
835
836static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
837 enum bitmap_page_attr attr)
838{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000839 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700840}
841
842static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
843 enum bitmap_page_attr attr)
844{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000845 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700846}
847
NeilBrownec7a3192006-06-26 00:27:45 -0700848static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
849 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700850{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000851 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700852}
853
854/*
855 * bitmap_file_set_bit -- called before performing a write to the md device
856 * to set (and eventually sync) a particular bit in the bitmap file
857 *
858 * we set the bit immediately, then we record the page number so that
859 * when an unplug occurs, we can flush the dirty pages out to disk
860 */
861static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
862{
863 unsigned long bit;
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000864 struct page *page;
NeilBrown32a76272005-06-21 17:17:14 -0700865 void *kaddr;
866 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
867
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000868 if (!bitmap->filemap)
869 return;
NeilBrown32a76272005-06-21 17:17:14 -0700870
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000871 page = filemap_get_page(bitmap, chunk);
872 if (!page)
873 return;
874 bit = file_page_offset(bitmap, chunk);
NeilBrown32a76272005-06-21 17:17:14 -0700875
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000876 /* set the bit */
877 kaddr = kmap_atomic(page, KM_USER0);
878 if (bitmap->flags & BITMAP_HOSTENDIAN)
879 set_bit(bit, kaddr);
880 else
881 __set_bit_le(bit, kaddr);
882 kunmap_atomic(kaddr, KM_USER0);
NeilBrown36a4e1f2011-10-07 14:23:17 +1100883 pr_debug("set file bit %lu page %lu\n", bit, page->index);
NeilBrown32a76272005-06-21 17:17:14 -0700884 /* record page number so it gets flushed to disk when unplug occurs */
885 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
NeilBrown32a76272005-06-21 17:17:14 -0700886}
887
888/* this gets called when the md device is ready to unplug its underlying
889 * (slave) device queues -- before we let any writes go down, we need to
890 * sync the dirty pages of the bitmap file to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700891void bitmap_unplug(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700892{
NeilBrownec7a3192006-06-26 00:27:45 -0700893 unsigned long i, flags;
894 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700895 struct page *page;
896 int wait = 0;
897
898 if (!bitmap)
NeilBrown4ad13662007-07-17 04:06:13 -0700899 return;
NeilBrown32a76272005-06-21 17:17:14 -0700900
901 /* look at each page to see if there are any set bits that need to be
902 * flushed out to disk */
903 for (i = 0; i < bitmap->file_pages; i++) {
904 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700905 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700906 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700907 return;
NeilBrown32a76272005-06-21 17:17:14 -0700908 }
909 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700910 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
911 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700912 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
913 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700914 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700915 wait = 1;
916 spin_unlock_irqrestore(&bitmap->lock, flags);
917
NeilBrownac2f40b2010-06-01 19:37:31 +1000918 if (dirty || need_write)
NeilBrown4ad13662007-07-17 04:06:13 -0700919 write_page(bitmap, page, 0);
NeilBrown32a76272005-06-21 17:17:14 -0700920 }
921 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700922 if (bitmap->file)
NeilBrownd785a062006-06-26 00:27:48 -0700923 wait_event(bitmap->write_wait,
924 atomic_read(&bitmap->pending_writes)==0);
NeilBrown0b79ccf2006-06-26 00:27:44 -0700925 else
NeilBrowna9701a32005-11-08 21:39:34 -0800926 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700927 }
NeilBrownd785a062006-06-26 00:27:48 -0700928 if (bitmap->flags & BITMAP_WRITE_ERROR)
929 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700930}
NeilBrownac2f40b2010-06-01 19:37:31 +1000931EXPORT_SYMBOL(bitmap_unplug);
NeilBrown32a76272005-06-21 17:17:14 -0700932
NeilBrown6a079972005-09-09 16:23:44 -0700933static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700934/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
935 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
936 * memory mapping of the bitmap file
937 * Special cases:
938 * if there's no bitmap file, or if the bitmap file had been
939 * previously kicked from the array, we mark all the bits as
940 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700941 *
942 * We ignore all bits for sectors that end earlier than 'start'.
943 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700944 */
NeilBrown6a079972005-09-09 16:23:44 -0700945static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700946{
947 unsigned long i, chunks, index, oldindex, bit;
948 struct page *page = NULL, *oldpage = NULL;
949 unsigned long num_pages, bit_cnt = 0;
950 struct file *file;
NeilBrownd785a062006-06-26 00:27:48 -0700951 unsigned long bytes, offset;
NeilBrown32a76272005-06-21 17:17:14 -0700952 int outofdate;
953 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800954 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700955
956 chunks = bitmap->chunks;
957 file = bitmap->file;
958
NeilBrown42a04b52009-12-14 12:49:53 +1100959 BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
NeilBrown32a76272005-06-21 17:17:14 -0700960
NeilBrown32a76272005-06-21 17:17:14 -0700961 outofdate = bitmap->flags & BITMAP_STALE;
NeilBrown32a76272005-06-21 17:17:14 -0700962 if (outofdate)
963 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
964 "recovery\n", bmname(bitmap));
965
NeilBrowne384e582010-06-01 19:37:34 +1000966 bytes = DIV_ROUND_UP(bitmap->chunks, 8);
NeilBrownece5cff2009-12-14 12:49:56 +1100967 if (!bitmap->mddev->bitmap_info.external)
968 bytes += sizeof(bitmap_super_t);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700969
NeilBrowne384e582010-06-01 19:37:34 +1000970 num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700971
NeilBrownece5cff2009-12-14 12:49:56 +1100972 if (file && i_size_read(file->f_mapping->host) < bytes) {
NeilBrown32a76272005-06-21 17:17:14 -0700973 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
974 bmname(bitmap),
975 (unsigned long) i_size_read(file->f_mapping->host),
NeilBrownece5cff2009-12-14 12:49:56 +1100976 bytes);
NeilBrown4ad13662007-07-17 04:06:13 -0700977 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700978 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700979
980 ret = -ENOMEM;
981
NeilBrown32a76272005-06-21 17:17:14 -0700982 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700983 if (!bitmap->filemap)
NeilBrown4ad13662007-07-17 04:06:13 -0700984 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700985
NeilBrowne16b68b2006-06-26 00:27:45 -0700986 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
987 bitmap->filemap_attr = kzalloc(
NeilBrownac2f40b2010-06-01 19:37:31 +1000988 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
NeilBrowne16b68b2006-06-26 00:27:45 -0700989 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700990 if (!bitmap->filemap_attr)
NeilBrown4ad13662007-07-17 04:06:13 -0700991 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700992
NeilBrown32a76272005-06-21 17:17:14 -0700993 oldindex = ~0L;
994
995 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -0800996 int b;
NeilBrownece5cff2009-12-14 12:49:56 +1100997 index = file_page_index(bitmap, i);
998 bit = file_page_offset(bitmap, i);
NeilBrown32a76272005-06-21 17:17:14 -0700999 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -07001000 int count;
NeilBrown32a76272005-06-21 17:17:14 -07001001 /* unmap the old page, we're done with it */
NeilBrownd785a062006-06-26 00:27:48 -07001002 if (index == num_pages-1)
NeilBrownece5cff2009-12-14 12:49:56 +11001003 count = bytes - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -07001004 else
1005 count = PAGE_SIZE;
NeilBrownece5cff2009-12-14 12:49:56 +11001006 if (index == 0 && bitmap->sb_page) {
NeilBrown32a76272005-06-21 17:17:14 -07001007 /*
1008 * if we're here then the superblock page
1009 * contains some bits (PAGE_SIZE != sizeof sb)
1010 * we've already read it in, so just use it
1011 */
1012 page = bitmap->sb_page;
1013 offset = sizeof(bitmap_super_t);
NeilBrown53845272009-01-09 08:31:05 +11001014 if (!file)
Vasiliy Kulikov5c04f552010-10-01 14:18:12 -07001015 page = read_sb_page(
1016 bitmap->mddev,
1017 bitmap->mddev->bitmap_info.offset,
1018 page,
1019 index, count);
NeilBrowna654b9d82005-06-21 17:17:27 -07001020 } else if (file) {
NeilBrownd785a062006-06-26 00:27:48 -07001021 page = read_page(file, index, bitmap, count);
NeilBrowna654b9d82005-06-21 17:17:27 -07001022 offset = 0;
1023 } else {
NeilBrown42a04b52009-12-14 12:49:53 +11001024 page = read_sb_page(bitmap->mddev,
1025 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +11001026 NULL,
1027 index, count);
NeilBrown32a76272005-06-21 17:17:14 -07001028 offset = 0;
1029 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001030 if (IS_ERR(page)) { /* read error */
1031 ret = PTR_ERR(page);
NeilBrown4ad13662007-07-17 04:06:13 -07001032 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -07001033 }
1034
NeilBrown32a76272005-06-21 17:17:14 -07001035 oldindex = index;
1036 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -07001037
NeilBrownb74fd282009-05-07 12:47:19 +10001038 bitmap->filemap[bitmap->file_pages++] = page;
1039 bitmap->last_page_size = count;
1040
NeilBrown32a76272005-06-21 17:17:14 -07001041 if (outofdate) {
1042 /*
1043 * if bitmap is out of date, dirty the
NeilBrownac2f40b2010-06-01 19:37:31 +10001044 * whole page and write it out
NeilBrown32a76272005-06-21 17:17:14 -07001045 */
NeilBrownea03aff2006-01-06 00:20:34 -08001046 paddr = kmap_atomic(page, KM_USER0);
1047 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -07001048 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -08001049 kunmap_atomic(paddr, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -07001050 write_page(bitmap, page, 1);
1051
1052 ret = -EIO;
NeilBrownb74fd282009-05-07 12:47:19 +10001053 if (bitmap->flags & BITMAP_WRITE_ERROR)
NeilBrown4ad13662007-07-17 04:06:13 -07001054 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001055 }
NeilBrown32a76272005-06-21 17:17:14 -07001056 }
NeilBrownea03aff2006-01-06 00:20:34 -08001057 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001058 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001059 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001060 else
Akinobu Mita6b33aff2011-03-23 16:42:13 -07001061 b = test_bit_le(bit, paddr);
NeilBrownea03aff2006-01-06 00:20:34 -08001062 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001063 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -07001064 /* if the disk bit is set, set the memory bit */
NeilBrowndb305e52009-05-07 12:49:06 +10001065 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1066 >= start);
1067 bitmap_set_memory_bits(bitmap,
1068 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1069 needed);
NeilBrown32a76272005-06-21 17:17:14 -07001070 bit_cnt++;
1071 }
NeilBrown32a76272005-06-21 17:17:14 -07001072 }
1073
NeilBrownac2f40b2010-06-01 19:37:31 +10001074 /* everything went OK */
NeilBrown32a76272005-06-21 17:17:14 -07001075 ret = 0;
1076 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1077
NeilBrown32a76272005-06-21 17:17:14 -07001078 if (bit_cnt) { /* Kick recovery if any bits were set */
1079 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1080 md_wakeup_thread(bitmap->mddev->thread);
1081 }
1082
NeilBrown32a76272005-06-21 17:17:14 -07001083 printk(KERN_INFO "%s: bitmap initialized from disk: "
Jonathan Brassow9c810752011-06-08 17:59:30 -05001084 "read %lu/%lu pages, set %lu of %lu bits\n",
1085 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, chunks);
NeilBrown32a76272005-06-21 17:17:14 -07001086
NeilBrown4ad13662007-07-17 04:06:13 -07001087 return 0;
1088
1089 err:
1090 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1091 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001092 return ret;
1093}
1094
NeilBrowna654b9d82005-06-21 17:17:27 -07001095void bitmap_write_all(struct bitmap *bitmap)
1096{
1097 /* We don't actually write all bitmap blocks here,
1098 * just flag them as needing to be written
1099 */
NeilBrownec7a3192006-06-26 00:27:45 -07001100 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001101
NeilBrown7c8f4242011-11-23 10:18:52 +11001102 spin_lock_irq(&bitmap->lock);
NeilBrownac2f40b2010-06-01 19:37:31 +10001103 for (i = 0; i < bitmap->file_pages; i++)
NeilBrownec7a3192006-06-26 00:27:45 -07001104 set_page_attr(bitmap, bitmap->filemap[i],
1105 BITMAP_PAGE_NEEDWRITE);
NeilBrown2585f3e2011-09-21 15:37:46 +10001106 bitmap->allclean = 0;
NeilBrown7c8f4242011-11-23 10:18:52 +11001107 spin_unlock_irq(&bitmap->lock);
NeilBrowna654b9d82005-06-21 17:17:27 -07001108}
1109
NeilBrown32a76272005-06-21 17:17:14 -07001110static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1111{
1112 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1113 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1114 bitmap->bp[page].count += inc;
NeilBrown32a76272005-06-21 17:17:14 -07001115 bitmap_checkfree(bitmap, page);
1116}
1117static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001118 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001119 int create);
1120
1121/*
1122 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1123 * out to disk
1124 */
1125
NeilBrownfd01b882011-10-11 16:47:53 +11001126void bitmap_daemon_work(struct mddev *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001127{
NeilBrownaa5cbd12009-12-14 12:49:46 +11001128 struct bitmap *bitmap;
NeilBrownaa3163f2005-06-21 17:17:22 -07001129 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001130 unsigned long flags;
1131 struct page *page = NULL, *lastpage = NULL;
NeilBrown57dab0b2010-10-19 10:03:39 +11001132 sector_t blocks;
NeilBrownea03aff2006-01-06 00:20:34 -08001133 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001134
NeilBrownaa5cbd12009-12-14 12:49:46 +11001135 /* Use a mutex to guard daemon_work against
1136 * bitmap_destroy.
1137 */
NeilBrownc3d97142009-12-14 12:49:52 +11001138 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrownaa5cbd12009-12-14 12:49:46 +11001139 bitmap = mddev->bitmap;
1140 if (bitmap == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001141 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown4ad13662007-07-17 04:06:13 -07001142 return;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001143 }
NeilBrown42a04b52009-12-14 12:49:53 +11001144 if (time_before(jiffies, bitmap->daemon_lastrun
NeilBrown2e61ebb2011-12-23 10:17:50 +11001145 + mddev->bitmap_info.daemon_sleep))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001146 goto done;
1147
NeilBrown32a76272005-06-21 17:17:14 -07001148 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001149 if (bitmap->allclean) {
NeilBrown2e61ebb2011-12-23 10:17:50 +11001150 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001151 goto done;
NeilBrown8311c292008-03-04 14:29:30 -08001152 }
1153 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001154
NeilBrownbe512692009-05-26 09:41:17 +10001155 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001156 for (j = 0; j < bitmap->chunks; j++) {
1157 bitmap_counter_t *bmc;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001158 if (!bitmap->filemap)
1159 /* error or shutdown */
1160 break;
1161
1162 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001163
1164 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001165 /* skip this page unless it's marked as needing cleaning */
NeilBrown5a537df2011-09-21 15:37:46 +10001166 if (!test_page_attr(bitmap, page, BITMAP_PAGE_PENDING)) {
NeilBrownec7a3192006-06-26 00:27:45 -07001167 int need_write = test_page_attr(bitmap, page,
1168 BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001169 if (need_write)
NeilBrownaa3163f2005-06-21 17:17:22 -07001170 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001171
NeilBrownaa3163f2005-06-21 17:17:22 -07001172 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown2585f3e2011-09-21 15:37:46 +10001173 if (need_write)
NeilBrown4ad13662007-07-17 04:06:13 -07001174 write_page(bitmap, page, 0);
NeilBrownbe512692009-05-26 09:41:17 +10001175 spin_lock_irqsave(&bitmap->lock, flags);
1176 j |= (PAGE_BITS - 1);
NeilBrownaa3163f2005-06-21 17:17:22 -07001177 continue;
1178 }
1179
NeilBrown32a76272005-06-21 17:17:14 -07001180 /* grab the new page, sync and release the old */
NeilBrown32a76272005-06-21 17:17:14 -07001181 if (lastpage != NULL) {
NeilBrown2585f3e2011-09-21 15:37:46 +10001182 if (test_page_attr(bitmap, lastpage,
1183 BITMAP_PAGE_NEEDWRITE)) {
1184 clear_page_attr(bitmap, lastpage,
1185 BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -07001186 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001187 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001188 } else {
NeilBrown2585f3e2011-09-21 15:37:46 +10001189 set_page_attr(bitmap, lastpage,
1190 BITMAP_PAGE_NEEDWRITE);
1191 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001192 spin_unlock_irqrestore(&bitmap->lock, flags);
1193 }
NeilBrown32a76272005-06-21 17:17:14 -07001194 } else
1195 spin_unlock_irqrestore(&bitmap->lock, flags);
1196 lastpage = page;
Neil Browna0da84f2008-06-28 08:31:22 +10001197
1198 /* We are possibly going to clear some bits, so make
1199 * sure that events_cleared is up-to-date.
1200 */
NeilBrownece5cff2009-12-14 12:49:56 +11001201 if (bitmap->need_sync &&
NeilBrown2e61ebb2011-12-23 10:17:50 +11001202 mddev->bitmap_info.external == 0) {
Neil Browna0da84f2008-06-28 08:31:22 +10001203 bitmap_super_t *sb;
1204 bitmap->need_sync = 0;
1205 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1206 sb->events_cleared =
1207 cpu_to_le64(bitmap->events_cleared);
1208 kunmap_atomic(sb, KM_USER0);
1209 write_page(bitmap, bitmap->sb_page, 1);
1210 }
NeilBrown32a76272005-06-21 17:17:14 -07001211 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownece5cff2009-12-14 12:49:56 +11001212 if (!bitmap->need_sync)
NeilBrown5a537df2011-09-21 15:37:46 +10001213 clear_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001214 else
1215 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001216 }
NeilBrowndb305e52009-05-07 12:49:06 +10001217 bmc = bitmap_get_counter(bitmap,
1218 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1219 &blocks, 0);
NeilBrown5a537df2011-09-21 15:37:46 +10001220 if (!bmc)
1221 j |= PAGE_COUNTER_MASK;
1222 else if (*bmc) {
NeilBrown5a537df2011-09-21 15:37:46 +10001223 if (*bmc == 1 && !bitmap->need_sync) {
NeilBrown32a76272005-06-21 17:17:14 -07001224 /* we can clear the bit */
1225 *bmc = 0;
NeilBrowndb305e52009-05-07 12:49:06 +10001226 bitmap_count_page(bitmap,
1227 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
NeilBrown32a76272005-06-21 17:17:14 -07001228 -1);
1229
1230 /* clear the bit */
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001231 paddr = kmap_atomic(page, KM_USER0);
1232 if (bitmap->flags & BITMAP_HOSTENDIAN)
1233 clear_bit(file_page_offset(bitmap, j),
1234 paddr);
1235 else
1236 __clear_bit_le(
NeilBrown5a537df2011-09-21 15:37:46 +10001237 file_page_offset(bitmap,
1238 j),
1239 paddr);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001240 kunmap_atomic(paddr, KM_USER0);
NeilBrown5a537df2011-09-21 15:37:46 +10001241 } else if (*bmc <= 2) {
1242 *bmc = 1; /* maybe clear the bit next time */
1243 set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001244 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001245 }
NeilBrown5a537df2011-09-21 15:37:46 +10001246 }
NeilBrown32a76272005-06-21 17:17:14 -07001247 }
NeilBrownbe512692009-05-26 09:41:17 +10001248 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001249
1250 /* now sync the final page */
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001251 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001252 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001253 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001254 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1255 spin_unlock_irqrestore(&bitmap->lock, flags);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001256 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001257 } else {
1258 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
NeilBrown2585f3e2011-09-21 15:37:46 +10001259 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001260 spin_unlock_irqrestore(&bitmap->lock, flags);
1261 }
NeilBrown32a76272005-06-21 17:17:14 -07001262 }
1263
NeilBrown7be3dfe2008-03-10 11:43:48 -07001264 done:
NeilBrown8311c292008-03-04 14:29:30 -08001265 if (bitmap->allclean == 0)
NeilBrown2e61ebb2011-12-23 10:17:50 +11001266 mddev->thread->timeout =
1267 mddev->bitmap_info.daemon_sleep;
NeilBrownc3d97142009-12-14 12:49:52 +11001268 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown32a76272005-06-21 17:17:14 -07001269}
1270
NeilBrown32a76272005-06-21 17:17:14 -07001271static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001272 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001273 int create)
NeilBrownee305ac2009-09-23 18:06:44 +10001274__releases(bitmap->lock)
1275__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -07001276{
1277 /* If 'create', we might release the lock and reclaim it.
1278 * The lock must have been taken with interrupts enabled.
1279 * If !create, we don't release the lock.
1280 */
1281 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1282 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1283 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1284 sector_t csize;
NeilBrownef425672010-06-01 19:37:33 +10001285 int err;
NeilBrown32a76272005-06-21 17:17:14 -07001286
NeilBrownef425672010-06-01 19:37:33 +10001287 err = bitmap_checkpage(bitmap, page, create);
1288
1289 if (bitmap->bp[page].hijacked ||
1290 bitmap->bp[page].map == NULL)
1291 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1292 PAGE_COUNTER_SHIFT - 1);
1293 else
NeilBrown32a76272005-06-21 17:17:14 -07001294 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
NeilBrownef425672010-06-01 19:37:33 +10001295 *blocks = csize - (offset & (csize - 1));
1296
1297 if (err < 0)
NeilBrown32a76272005-06-21 17:17:14 -07001298 return NULL;
NeilBrownef425672010-06-01 19:37:33 +10001299
NeilBrown32a76272005-06-21 17:17:14 -07001300 /* now locked ... */
1301
1302 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1303 /* should we use the first or second counter field
1304 * of the hijacked pointer? */
1305 int hi = (pageoff > PAGE_COUNTER_MASK);
NeilBrown32a76272005-06-21 17:17:14 -07001306 return &((bitmap_counter_t *)
1307 &bitmap->bp[page].map)[hi];
NeilBrownef425672010-06-01 19:37:33 +10001308 } else /* page is allocated */
NeilBrown32a76272005-06-21 17:17:14 -07001309 return (bitmap_counter_t *)
1310 &(bitmap->bp[page].map[pageoff]);
NeilBrown32a76272005-06-21 17:17:14 -07001311}
1312
NeilBrown4b6d2872005-09-09 16:23:47 -07001313int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001314{
NeilBrownac2f40b2010-06-01 19:37:31 +10001315 if (!bitmap)
1316 return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001317
1318 if (behind) {
Paul Clements696fcd52010-03-08 16:02:37 +11001319 int bw;
NeilBrown4b6d2872005-09-09 16:23:47 -07001320 atomic_inc(&bitmap->behind_writes);
Paul Clements696fcd52010-03-08 16:02:37 +11001321 bw = atomic_read(&bitmap->behind_writes);
1322 if (bw > bitmap->behind_writes_used)
1323 bitmap->behind_writes_used = bw;
1324
NeilBrown36a4e1f2011-10-07 14:23:17 +11001325 pr_debug("inc write-behind count %d/%lu\n",
1326 bw, bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001327 }
1328
NeilBrown32a76272005-06-21 17:17:14 -07001329 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001330 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001331 bitmap_counter_t *bmc;
1332
1333 spin_lock_irq(&bitmap->lock);
1334 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1335 if (!bmc) {
1336 spin_unlock_irq(&bitmap->lock);
1337 return 0;
1338 }
1339
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001340 if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
Neil Brownda6e1a32007-02-08 14:20:37 -08001341 DEFINE_WAIT(__wait);
1342 /* note that it is safe to do the prepare_to_wait
1343 * after the test as long as we do it before dropping
1344 * the spinlock.
1345 */
1346 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1347 TASK_UNINTERRUPTIBLE);
1348 spin_unlock_irq(&bitmap->lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001349 io_schedule();
Neil Brownda6e1a32007-02-08 14:20:37 -08001350 finish_wait(&bitmap->overflow_wait, &__wait);
1351 continue;
1352 }
1353
NeilBrownac2f40b2010-06-01 19:37:31 +10001354 switch (*bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001355 case 0:
1356 bitmap_file_set_bit(bitmap, offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001357 bitmap_count_page(bitmap, offset, 1);
NeilBrown32a76272005-06-21 17:17:14 -07001358 /* fall through */
1359 case 1:
1360 *bmc = 2;
1361 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001362
NeilBrown32a76272005-06-21 17:17:14 -07001363 (*bmc)++;
1364
1365 spin_unlock_irq(&bitmap->lock);
1366
1367 offset += blocks;
1368 if (sectors > blocks)
1369 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001370 else
1371 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001372 }
1373 return 0;
1374}
NeilBrownac2f40b2010-06-01 19:37:31 +10001375EXPORT_SYMBOL(bitmap_startwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001376
1377void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001378 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001379{
NeilBrownac2f40b2010-06-01 19:37:31 +10001380 if (!bitmap)
1381 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001382 if (behind) {
NeilBrowne5551902010-03-31 11:21:44 +11001383 if (atomic_dec_and_test(&bitmap->behind_writes))
1384 wake_up(&bitmap->behind_wait);
NeilBrown36a4e1f2011-10-07 14:23:17 +11001385 pr_debug("dec write-behind count %d/%lu\n",
1386 atomic_read(&bitmap->behind_writes),
1387 bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001388 }
1389
NeilBrown32a76272005-06-21 17:17:14 -07001390 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001391 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001392 unsigned long flags;
1393 bitmap_counter_t *bmc;
1394
1395 spin_lock_irqsave(&bitmap->lock, flags);
1396 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1397 if (!bmc) {
1398 spin_unlock_irqrestore(&bitmap->lock, flags);
1399 return;
1400 }
1401
NeilBrown961902c2011-12-23 09:57:48 +11001402 if (success && !bitmap->mddev->degraded &&
Neil Browna0da84f2008-06-28 08:31:22 +10001403 bitmap->events_cleared < bitmap->mddev->events) {
1404 bitmap->events_cleared = bitmap->mddev->events;
1405 bitmap->need_sync = 1;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001406 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
Neil Browna0da84f2008-06-28 08:31:22 +10001407 }
1408
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001409 if (!success && !NEEDED(*bmc))
NeilBrown32a76272005-06-21 17:17:14 -07001410 *bmc |= NEEDED_MASK;
1411
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001412 if (COUNTER(*bmc) == COUNTER_MAX)
Neil Brownda6e1a32007-02-08 14:20:37 -08001413 wake_up(&bitmap->overflow_wait);
1414
NeilBrown32a76272005-06-21 17:17:14 -07001415 (*bmc)--;
NeilBrown2585f3e2011-09-21 15:37:46 +10001416 if (*bmc <= 2) {
NeilBrown32a76272005-06-21 17:17:14 -07001417 set_page_attr(bitmap,
NeilBrowne384e582010-06-01 19:37:34 +10001418 filemap_get_page(
1419 bitmap,
1420 offset >> CHUNK_BLOCK_SHIFT(bitmap)),
NeilBrown5a537df2011-09-21 15:37:46 +10001421 BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001422 bitmap->allclean = 0;
1423 }
NeilBrown32a76272005-06-21 17:17:14 -07001424 spin_unlock_irqrestore(&bitmap->lock, flags);
1425 offset += blocks;
1426 if (sectors > blocks)
1427 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001428 else
1429 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001430 }
1431}
NeilBrownac2f40b2010-06-01 19:37:31 +10001432EXPORT_SYMBOL(bitmap_endwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001433
NeilBrown57dab0b2010-10-19 10:03:39 +11001434static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001435 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001436{
1437 bitmap_counter_t *bmc;
1438 int rv;
1439 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1440 *blocks = 1024;
1441 return 1; /* always resync if no bitmap */
1442 }
1443 spin_lock_irq(&bitmap->lock);
1444 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1445 rv = 0;
1446 if (bmc) {
1447 /* locked */
1448 if (RESYNC(*bmc))
1449 rv = 1;
1450 else if (NEEDED(*bmc)) {
1451 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001452 if (!degraded) { /* don't set/clear bits if degraded */
1453 *bmc |= RESYNC_MASK;
1454 *bmc &= ~NEEDED_MASK;
1455 }
NeilBrown32a76272005-06-21 17:17:14 -07001456 }
1457 }
1458 spin_unlock_irq(&bitmap->lock);
1459 return rv;
1460}
1461
NeilBrown57dab0b2010-10-19 10:03:39 +11001462int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001463 int degraded)
1464{
1465 /* bitmap_start_sync must always report on multiples of whole
1466 * pages, otherwise resync (which is very PAGE_SIZE based) will
1467 * get confused.
1468 * So call __bitmap_start_sync repeatedly (if needed) until
1469 * At least PAGE_SIZE>>9 blocks are covered.
1470 * Return the 'or' of the result.
1471 */
1472 int rv = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001473 sector_t blocks1;
NeilBrown1187cf02009-03-31 14:27:02 +11001474
1475 *blocks = 0;
1476 while (*blocks < (PAGE_SIZE>>9)) {
1477 rv |= __bitmap_start_sync(bitmap, offset,
1478 &blocks1, degraded);
1479 offset += blocks1;
1480 *blocks += blocks1;
1481 }
1482 return rv;
1483}
NeilBrownac2f40b2010-06-01 19:37:31 +10001484EXPORT_SYMBOL(bitmap_start_sync);
NeilBrown1187cf02009-03-31 14:27:02 +11001485
NeilBrown57dab0b2010-10-19 10:03:39 +11001486void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
NeilBrown32a76272005-06-21 17:17:14 -07001487{
1488 bitmap_counter_t *bmc;
1489 unsigned long flags;
NeilBrownac2f40b2010-06-01 19:37:31 +10001490
1491 if (bitmap == NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001492 *blocks = 1024;
1493 return;
1494 }
1495 spin_lock_irqsave(&bitmap->lock, flags);
1496 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1497 if (bmc == NULL)
1498 goto unlock;
1499 /* locked */
NeilBrown32a76272005-06-21 17:17:14 -07001500 if (RESYNC(*bmc)) {
1501 *bmc &= ~RESYNC_MASK;
1502
1503 if (!NEEDED(*bmc) && aborted)
1504 *bmc |= NEEDED_MASK;
1505 else {
NeilBrown2585f3e2011-09-21 15:37:46 +10001506 if (*bmc <= 2) {
NeilBrown32a76272005-06-21 17:17:14 -07001507 set_page_attr(bitmap,
1508 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
NeilBrown5a537df2011-09-21 15:37:46 +10001509 BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001510 bitmap->allclean = 0;
1511 }
NeilBrown32a76272005-06-21 17:17:14 -07001512 }
1513 }
1514 unlock:
1515 spin_unlock_irqrestore(&bitmap->lock, flags);
1516}
NeilBrownac2f40b2010-06-01 19:37:31 +10001517EXPORT_SYMBOL(bitmap_end_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001518
1519void bitmap_close_sync(struct bitmap *bitmap)
1520{
1521 /* Sync has finished, and any bitmap chunks that weren't synced
1522 * properly have been aborted. It remains to us to clear the
1523 * RESYNC bit wherever it is still on
1524 */
1525 sector_t sector = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001526 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001527 if (!bitmap)
1528 return;
NeilBrown32a76272005-06-21 17:17:14 -07001529 while (sector < bitmap->mddev->resync_max_sectors) {
1530 bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001531 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001532 }
1533}
NeilBrownac2f40b2010-06-01 19:37:31 +10001534EXPORT_SYMBOL(bitmap_close_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001535
NeilBrownb47490c2008-02-06 01:39:50 -08001536void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1537{
1538 sector_t s = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001539 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001540
1541 if (!bitmap)
1542 return;
1543 if (sector == 0) {
1544 bitmap->last_end_sync = jiffies;
1545 return;
1546 }
1547 if (time_before(jiffies, (bitmap->last_end_sync
NeilBrown1b04be92009-12-14 12:49:53 +11001548 + bitmap->mddev->bitmap_info.daemon_sleep)))
NeilBrownb47490c2008-02-06 01:39:50 -08001549 return;
1550 wait_event(bitmap->mddev->recovery_wait,
1551 atomic_read(&bitmap->mddev->recovery_active) == 0);
1552
NeilBrown75d3da42011-01-14 09:14:34 +11001553 bitmap->mddev->curr_resync_completed = sector;
NeilBrown070dc6d2010-08-30 17:33:34 +10001554 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
NeilBrownb47490c2008-02-06 01:39:50 -08001555 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1556 s = 0;
1557 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1558 bitmap_end_sync(bitmap, s, &blocks, 0);
1559 s += blocks;
1560 }
1561 bitmap->last_end_sync = jiffies;
NeilBrownacb180b2009-04-14 16:28:34 +10001562 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
NeilBrownb47490c2008-02-06 01:39:50 -08001563}
NeilBrownac2f40b2010-06-01 19:37:31 +10001564EXPORT_SYMBOL(bitmap_cond_end_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08001565
NeilBrown6a079972005-09-09 16:23:44 -07001566static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001567{
1568 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001569 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001570 * be 0 at this point
1571 */
NeilBrown193f1c92005-08-04 12:53:33 -07001572
NeilBrown57dab0b2010-10-19 10:03:39 +11001573 sector_t secs;
NeilBrown193f1c92005-08-04 12:53:33 -07001574 bitmap_counter_t *bmc;
1575 spin_lock_irq(&bitmap->lock);
1576 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1577 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001578 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001579 return;
NeilBrown32a76272005-06-21 17:17:14 -07001580 }
NeilBrownac2f40b2010-06-01 19:37:31 +10001581 if (!*bmc) {
NeilBrown193f1c92005-08-04 12:53:33 -07001582 struct page *page;
NeilBrown915c4202011-12-23 10:17:51 +11001583 *bmc = 2 | (needed ? NEEDED_MASK : 0);
NeilBrown193f1c92005-08-04 12:53:33 -07001584 bitmap_count_page(bitmap, offset, 1);
1585 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
NeilBrown5a537df2011-09-21 15:37:46 +10001586 set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001587 bitmap->allclean = 0;
NeilBrown193f1c92005-08-04 12:53:33 -07001588 }
1589 spin_unlock_irq(&bitmap->lock);
NeilBrown32a76272005-06-21 17:17:14 -07001590}
1591
Paul Clements9b1d1da2006-10-03 01:15:49 -07001592/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1593void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1594{
1595 unsigned long chunk;
1596
1597 for (chunk = s; chunk <= e; chunk++) {
NeilBrowndb305e52009-05-07 12:49:06 +10001598 sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001599 bitmap_set_memory_bits(bitmap, sec, 1);
NeilBrown7c8f4242011-11-23 10:18:52 +11001600 spin_lock_irq(&bitmap->lock);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001601 bitmap_file_set_bit(bitmap, sec);
NeilBrown7c8f4242011-11-23 10:18:52 +11001602 spin_unlock_irq(&bitmap->lock);
NeilBrownffa23322009-12-14 12:49:56 +11001603 if (sec < bitmap->mddev->recovery_cp)
1604 /* We are asserting that the array is dirty,
1605 * so move the recovery_cp address back so
1606 * that it is obvious that it is dirty
1607 */
1608 bitmap->mddev->recovery_cp = sec;
Paul Clements9b1d1da2006-10-03 01:15:49 -07001609 }
1610}
1611
NeilBrown32a76272005-06-21 17:17:14 -07001612/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001613 * flush out any pending updates
1614 */
NeilBrownfd01b882011-10-11 16:47:53 +11001615void bitmap_flush(struct mddev *mddev)
NeilBrown6b8b3e82005-08-04 12:53:35 -07001616{
1617 struct bitmap *bitmap = mddev->bitmap;
NeilBrown42a04b52009-12-14 12:49:53 +11001618 long sleep;
NeilBrown6b8b3e82005-08-04 12:53:35 -07001619
1620 if (!bitmap) /* there was no bitmap */
1621 return;
1622
1623 /* run the daemon_work three time to ensure everything is flushed
1624 * that can be
1625 */
NeilBrown1b04be92009-12-14 12:49:53 +11001626 sleep = mddev->bitmap_info.daemon_sleep * 2;
NeilBrown42a04b52009-12-14 12:49:53 +11001627 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001628 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001629 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001630 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001631 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001632 bitmap_daemon_work(mddev);
NeilBrown6b8b3e82005-08-04 12:53:35 -07001633 bitmap_update_sb(bitmap);
1634}
1635
1636/*
NeilBrown32a76272005-06-21 17:17:14 -07001637 * free memory that was allocated
1638 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001639static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001640{
1641 unsigned long k, pages;
1642 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001643
1644 if (!bitmap) /* there was no bitmap */
1645 return;
1646
NeilBrown32a76272005-06-21 17:17:14 -07001647 /* release the bitmap file and kill the daemon */
1648 bitmap_file_put(bitmap);
1649
1650 bp = bitmap->bp;
1651 pages = bitmap->pages;
1652
1653 /* free all allocated memory */
1654
NeilBrown32a76272005-06-21 17:17:14 -07001655 if (bp) /* deallocate the page memory */
1656 for (k = 0; k < pages; k++)
1657 if (bp[k].map && !bp[k].hijacked)
1658 kfree(bp[k].map);
1659 kfree(bp);
1660 kfree(bitmap);
1661}
NeilBrownaa5cbd12009-12-14 12:49:46 +11001662
NeilBrownfd01b882011-10-11 16:47:53 +11001663void bitmap_destroy(struct mddev *mddev)
NeilBrown3178b0d2005-09-09 16:23:50 -07001664{
1665 struct bitmap *bitmap = mddev->bitmap;
1666
1667 if (!bitmap) /* there was no bitmap */
1668 return;
1669
NeilBrownc3d97142009-12-14 12:49:52 +11001670 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrown3178b0d2005-09-09 16:23:50 -07001671 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownc3d97142009-12-14 12:49:52 +11001672 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrownb15c2e52006-01-06 00:20:16 -08001673 if (mddev->thread)
1674 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001675
NeilBrownece5cff2009-12-14 12:49:56 +11001676 if (bitmap->sysfs_can_clear)
1677 sysfs_put(bitmap->sysfs_can_clear);
1678
NeilBrown3178b0d2005-09-09 16:23:50 -07001679 bitmap_free(bitmap);
1680}
NeilBrown32a76272005-06-21 17:17:14 -07001681
1682/*
1683 * initialize the bitmap structure
1684 * if this returns an error, bitmap_destroy must be called to do clean up
1685 */
NeilBrownfd01b882011-10-11 16:47:53 +11001686int bitmap_create(struct mddev *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001687{
1688 struct bitmap *bitmap;
NeilBrown1f593902009-04-20 11:50:24 +10001689 sector_t blocks = mddev->resync_max_sectors;
NeilBrown32a76272005-06-21 17:17:14 -07001690 unsigned long chunks;
1691 unsigned long pages;
NeilBrownc3d97142009-12-14 12:49:52 +11001692 struct file *file = mddev->bitmap_info.file;
NeilBrown32a76272005-06-21 17:17:14 -07001693 int err;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001694 struct sysfs_dirent *bm = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001695
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001696 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001697
NeilBrowne384e582010-06-01 19:37:34 +10001698 if (!file
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001699 && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001700 return 0;
1701
NeilBrownc3d97142009-12-14 12:49:52 +11001702 BUG_ON(file && mddev->bitmap_info.offset);
NeilBrowna654b9d82005-06-21 17:17:27 -07001703
NeilBrown9ffae0c2006-01-06 00:20:32 -08001704 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001705 if (!bitmap)
1706 return -ENOMEM;
1707
NeilBrown32a76272005-06-21 17:17:14 -07001708 spin_lock_init(&bitmap->lock);
NeilBrownce25c312006-06-26 00:27:49 -07001709 atomic_set(&bitmap->pending_writes, 0);
1710 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001711 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrowne5551902010-03-31 11:21:44 +11001712 init_waitqueue_head(&bitmap->behind_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001713
NeilBrown32a76272005-06-21 17:17:14 -07001714 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001715
NeilBrown5ff5aff2010-06-01 19:37:32 +10001716 if (mddev->kobj.sd)
1717 bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
NeilBrownece5cff2009-12-14 12:49:56 +11001718 if (bm) {
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07001719 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
NeilBrownece5cff2009-12-14 12:49:56 +11001720 sysfs_put(bm);
1721 } else
1722 bitmap->sysfs_can_clear = NULL;
1723
NeilBrown32a76272005-06-21 17:17:14 -07001724 bitmap->file = file;
NeilBrownce25c312006-06-26 00:27:49 -07001725 if (file) {
1726 get_file(file);
NeilBrownae8fa282009-10-16 15:56:01 +11001727 /* As future accesses to this file will use bmap,
1728 * and bypass the page cache, we must sync the file
1729 * first.
1730 */
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001731 vfs_fsync(file, 1);
NeilBrownce25c312006-06-26 00:27:49 -07001732 }
NeilBrown42a04b52009-12-14 12:49:53 +11001733 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
Jonathan Brassow9c810752011-06-08 17:59:30 -05001734 if (!mddev->bitmap_info.external) {
1735 /*
1736 * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
1737 * instructing us to create a new on-disk bitmap instance.
1738 */
1739 if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
1740 err = bitmap_new_disk_sb(bitmap);
1741 else
1742 err = bitmap_read_sb(bitmap);
1743 } else {
NeilBrownece5cff2009-12-14 12:49:56 +11001744 err = 0;
1745 if (mddev->bitmap_info.chunksize == 0 ||
1746 mddev->bitmap_info.daemon_sleep == 0)
1747 /* chunksize and time_base need to be
1748 * set first. */
1749 err = -EINVAL;
1750 }
NeilBrown32a76272005-06-21 17:17:14 -07001751 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001752 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001753
NeilBrown624ce4f2009-12-14 12:49:56 +11001754 bitmap->daemon_lastrun = jiffies;
NeilBrown42a04b52009-12-14 12:49:53 +11001755 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
NeilBrown32a76272005-06-21 17:17:14 -07001756
1757 /* now that chunksize and chunkshift are set, we can use these macros */
NeilBrownac2f40b2010-06-01 19:37:31 +10001758 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
NeilBrown1f593902009-04-20 11:50:24 +10001759 CHUNK_BLOCK_SHIFT(bitmap);
NeilBrownac2f40b2010-06-01 19:37:31 +10001760 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
NeilBrown32a76272005-06-21 17:17:14 -07001761
1762 BUG_ON(!pages);
1763
1764 bitmap->chunks = chunks;
1765 bitmap->pages = pages;
1766 bitmap->missing_pages = pages;
NeilBrown32a76272005-06-21 17:17:14 -07001767
NeilBrown9ffae0c2006-01-06 00:20:32 -08001768 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown29d32472011-10-11 16:49:56 +11001769
NeilBrown3178b0d2005-09-09 16:23:50 -07001770 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001771 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001772 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001773
NeilBrown69e51b42010-06-01 19:37:35 +10001774 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1775 pages, bmname(bitmap));
1776
1777 mddev->bitmap = bitmap;
1778
1779
1780 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
1781
1782 error:
1783 bitmap_free(bitmap);
1784 return err;
1785}
1786
NeilBrownfd01b882011-10-11 16:47:53 +11001787int bitmap_load(struct mddev *mddev)
NeilBrown69e51b42010-06-01 19:37:35 +10001788{
1789 int err = 0;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001790 sector_t start = 0;
NeilBrown69e51b42010-06-01 19:37:35 +10001791 sector_t sector = 0;
1792 struct bitmap *bitmap = mddev->bitmap;
1793
1794 if (!bitmap)
1795 goto out;
1796
1797 /* Clear out old bitmap info first: Either there is none, or we
1798 * are resuming after someone else has possibly changed things,
1799 * so we should forget old cached info.
1800 * All chunks should be clean, but some might need_sync.
1801 */
1802 while (sector < mddev->resync_max_sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001803 sector_t blocks;
NeilBrown69e51b42010-06-01 19:37:35 +10001804 bitmap_start_sync(bitmap, sector, &blocks, 0);
1805 sector += blocks;
1806 }
1807 bitmap_close_sync(bitmap);
1808
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001809 if (mddev->degraded == 0
1810 || bitmap->events_cleared == mddev->events)
1811 /* no need to keep dirty bits to optimise a
1812 * re-add of a missing device */
1813 start = mddev->recovery_cp;
NeilBrown69e51b42010-06-01 19:37:35 +10001814
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001815 err = bitmap_init_from_disk(bitmap, start);
1816
NeilBrown32a76272005-06-21 17:17:14 -07001817 if (err)
NeilBrown69e51b42010-06-01 19:37:35 +10001818 goto out;
NeilBrown3178b0d2005-09-09 16:23:50 -07001819
NeilBrown1b04be92009-12-14 12:49:53 +11001820 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
NeilBrown9cd30fd2009-12-14 12:49:54 +11001821 md_wakeup_thread(mddev->thread);
NeilBrownb15c2e52006-01-06 00:20:16 -08001822
NeilBrown4ad13662007-07-17 04:06:13 -07001823 bitmap_update_sb(bitmap);
1824
NeilBrown69e51b42010-06-01 19:37:35 +10001825 if (bitmap->flags & BITMAP_WRITE_ERROR)
1826 err = -EIO;
1827out:
NeilBrown3178b0d2005-09-09 16:23:50 -07001828 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001829}
NeilBrown69e51b42010-06-01 19:37:35 +10001830EXPORT_SYMBOL_GPL(bitmap_load);
NeilBrown32a76272005-06-21 17:17:14 -07001831
NeilBrown57148962012-03-19 12:46:40 +11001832void bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
1833{
1834 unsigned long chunk_kb;
1835 unsigned long flags;
1836
1837 if (!bitmap)
1838 return;
1839
1840 spin_lock_irqsave(&bitmap->lock, flags);
1841 chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
1842 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
1843 "%lu%s chunk",
1844 bitmap->pages - bitmap->missing_pages,
1845 bitmap->pages,
1846 (bitmap->pages - bitmap->missing_pages)
1847 << (PAGE_SHIFT - 10),
1848 chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
1849 chunk_kb ? "KB" : "B");
1850 if (bitmap->file) {
1851 seq_printf(seq, ", file: ");
1852 seq_path(seq, &bitmap->file->f_path, " \t\n");
1853 }
1854
1855 seq_printf(seq, "\n");
1856 spin_unlock_irqrestore(&bitmap->lock, flags);
1857}
1858
NeilBrown43a70502009-12-14 12:49:55 +11001859static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001860location_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11001861{
1862 ssize_t len;
NeilBrownac2f40b2010-06-01 19:37:31 +10001863 if (mddev->bitmap_info.file)
NeilBrown43a70502009-12-14 12:49:55 +11001864 len = sprintf(page, "file");
NeilBrownac2f40b2010-06-01 19:37:31 +10001865 else if (mddev->bitmap_info.offset)
NeilBrown43a70502009-12-14 12:49:55 +11001866 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001867 else
NeilBrown43a70502009-12-14 12:49:55 +11001868 len = sprintf(page, "none");
1869 len += sprintf(page+len, "\n");
1870 return len;
1871}
1872
1873static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001874location_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11001875{
1876
1877 if (mddev->pers) {
1878 if (!mddev->pers->quiesce)
1879 return -EBUSY;
1880 if (mddev->recovery || mddev->sync_thread)
1881 return -EBUSY;
1882 }
1883
1884 if (mddev->bitmap || mddev->bitmap_info.file ||
1885 mddev->bitmap_info.offset) {
1886 /* bitmap already configured. Only option is to clear it */
1887 if (strncmp(buf, "none", 4) != 0)
1888 return -EBUSY;
1889 if (mddev->pers) {
1890 mddev->pers->quiesce(mddev, 1);
1891 bitmap_destroy(mddev);
1892 mddev->pers->quiesce(mddev, 0);
1893 }
1894 mddev->bitmap_info.offset = 0;
1895 if (mddev->bitmap_info.file) {
1896 struct file *f = mddev->bitmap_info.file;
1897 mddev->bitmap_info.file = NULL;
1898 restore_bitmap_write_access(f);
1899 fput(f);
1900 }
1901 } else {
1902 /* No bitmap, OK to set a location */
1903 long long offset;
1904 if (strncmp(buf, "none", 4) == 0)
1905 /* nothing to be done */;
1906 else if (strncmp(buf, "file:", 5) == 0) {
1907 /* Not supported yet */
1908 return -EINVAL;
1909 } else {
1910 int rv;
1911 if (buf[0] == '+')
1912 rv = strict_strtoll(buf+1, 10, &offset);
1913 else
1914 rv = strict_strtoll(buf, 10, &offset);
1915 if (rv)
1916 return rv;
1917 if (offset == 0)
1918 return -EINVAL;
NeilBrownece5cff2009-12-14 12:49:56 +11001919 if (mddev->bitmap_info.external == 0 &&
1920 mddev->major_version == 0 &&
NeilBrown43a70502009-12-14 12:49:55 +11001921 offset != mddev->bitmap_info.default_offset)
1922 return -EINVAL;
1923 mddev->bitmap_info.offset = offset;
1924 if (mddev->pers) {
1925 mddev->pers->quiesce(mddev, 1);
1926 rv = bitmap_create(mddev);
NeilBrown4474ca42012-03-19 12:46:37 +11001927 if (!rv)
1928 rv = bitmap_load(mddev);
NeilBrown43a70502009-12-14 12:49:55 +11001929 if (rv) {
1930 bitmap_destroy(mddev);
1931 mddev->bitmap_info.offset = 0;
1932 }
1933 mddev->pers->quiesce(mddev, 0);
1934 if (rv)
1935 return rv;
1936 }
1937 }
1938 }
1939 if (!mddev->external) {
1940 /* Ensure new bitmap info is stored in
1941 * metadata promptly.
1942 */
1943 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1944 md_wakeup_thread(mddev->thread);
1945 }
1946 return len;
1947}
1948
1949static struct md_sysfs_entry bitmap_location =
1950__ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
1951
1952static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001953timeout_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11001954{
1955 ssize_t len;
1956 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
1957 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
NeilBrownac2f40b2010-06-01 19:37:31 +10001958
NeilBrown43a70502009-12-14 12:49:55 +11001959 len = sprintf(page, "%lu", secs);
1960 if (jifs)
1961 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
1962 len += sprintf(page+len, "\n");
1963 return len;
1964}
1965
1966static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001967timeout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11001968{
1969 /* timeout can be set at any time */
1970 unsigned long timeout;
1971 int rv = strict_strtoul_scaled(buf, &timeout, 4);
1972 if (rv)
1973 return rv;
1974
1975 /* just to make sure we don't overflow... */
1976 if (timeout >= LONG_MAX / HZ)
1977 return -EINVAL;
1978
1979 timeout = timeout * HZ / 10000;
1980
1981 if (timeout >= MAX_SCHEDULE_TIMEOUT)
1982 timeout = MAX_SCHEDULE_TIMEOUT-1;
1983 if (timeout < 1)
1984 timeout = 1;
1985 mddev->bitmap_info.daemon_sleep = timeout;
1986 if (mddev->thread) {
1987 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1988 * the bitmap is all clean and we don't need to
1989 * adjust the timeout right now
1990 */
1991 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
1992 mddev->thread->timeout = timeout;
1993 md_wakeup_thread(mddev->thread);
1994 }
1995 }
1996 return len;
1997}
1998
1999static struct md_sysfs_entry bitmap_timeout =
2000__ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
2001
2002static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002003backlog_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002004{
2005 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
2006}
2007
2008static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002009backlog_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002010{
2011 unsigned long backlog;
2012 int rv = strict_strtoul(buf, 10, &backlog);
2013 if (rv)
2014 return rv;
2015 if (backlog > COUNTER_MAX)
2016 return -EINVAL;
2017 mddev->bitmap_info.max_write_behind = backlog;
2018 return len;
2019}
2020
2021static struct md_sysfs_entry bitmap_backlog =
2022__ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
2023
2024static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002025chunksize_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002026{
2027 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
2028}
2029
2030static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002031chunksize_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002032{
2033 /* Can only be changed when no bitmap is active */
2034 int rv;
2035 unsigned long csize;
2036 if (mddev->bitmap)
2037 return -EBUSY;
2038 rv = strict_strtoul(buf, 10, &csize);
2039 if (rv)
2040 return rv;
2041 if (csize < 512 ||
2042 !is_power_of_2(csize))
2043 return -EINVAL;
2044 mddev->bitmap_info.chunksize = csize;
2045 return len;
2046}
2047
2048static struct md_sysfs_entry bitmap_chunksize =
2049__ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
2050
NeilBrownfd01b882011-10-11 16:47:53 +11002051static ssize_t metadata_show(struct mddev *mddev, char *page)
NeilBrownece5cff2009-12-14 12:49:56 +11002052{
2053 return sprintf(page, "%s\n", (mddev->bitmap_info.external
2054 ? "external" : "internal"));
2055}
2056
NeilBrownfd01b882011-10-11 16:47:53 +11002057static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownece5cff2009-12-14 12:49:56 +11002058{
2059 if (mddev->bitmap ||
2060 mddev->bitmap_info.file ||
2061 mddev->bitmap_info.offset)
2062 return -EBUSY;
2063 if (strncmp(buf, "external", 8) == 0)
2064 mddev->bitmap_info.external = 1;
2065 else if (strncmp(buf, "internal", 8) == 0)
2066 mddev->bitmap_info.external = 0;
2067 else
2068 return -EINVAL;
2069 return len;
2070}
2071
2072static struct md_sysfs_entry bitmap_metadata =
2073__ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2074
NeilBrownfd01b882011-10-11 16:47:53 +11002075static ssize_t can_clear_show(struct mddev *mddev, char *page)
NeilBrownece5cff2009-12-14 12:49:56 +11002076{
2077 int len;
2078 if (mddev->bitmap)
2079 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
2080 "false" : "true"));
2081 else
2082 len = sprintf(page, "\n");
2083 return len;
2084}
2085
NeilBrownfd01b882011-10-11 16:47:53 +11002086static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownece5cff2009-12-14 12:49:56 +11002087{
2088 if (mddev->bitmap == NULL)
2089 return -ENOENT;
2090 if (strncmp(buf, "false", 5) == 0)
2091 mddev->bitmap->need_sync = 1;
2092 else if (strncmp(buf, "true", 4) == 0) {
2093 if (mddev->degraded)
2094 return -EBUSY;
2095 mddev->bitmap->need_sync = 0;
2096 } else
2097 return -EINVAL;
2098 return len;
2099}
2100
2101static struct md_sysfs_entry bitmap_can_clear =
2102__ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
2103
Paul Clements696fcd52010-03-08 16:02:37 +11002104static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002105behind_writes_used_show(struct mddev *mddev, char *page)
Paul Clements696fcd52010-03-08 16:02:37 +11002106{
2107 if (mddev->bitmap == NULL)
2108 return sprintf(page, "0\n");
2109 return sprintf(page, "%lu\n",
2110 mddev->bitmap->behind_writes_used);
2111}
2112
2113static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002114behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
Paul Clements696fcd52010-03-08 16:02:37 +11002115{
2116 if (mddev->bitmap)
2117 mddev->bitmap->behind_writes_used = 0;
2118 return len;
2119}
2120
2121static struct md_sysfs_entry max_backlog_used =
2122__ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2123 behind_writes_used_show, behind_writes_used_reset);
2124
NeilBrown43a70502009-12-14 12:49:55 +11002125static struct attribute *md_bitmap_attrs[] = {
2126 &bitmap_location.attr,
2127 &bitmap_timeout.attr,
2128 &bitmap_backlog.attr,
2129 &bitmap_chunksize.attr,
NeilBrownece5cff2009-12-14 12:49:56 +11002130 &bitmap_metadata.attr,
2131 &bitmap_can_clear.attr,
Paul Clements696fcd52010-03-08 16:02:37 +11002132 &max_backlog_used.attr,
NeilBrown43a70502009-12-14 12:49:55 +11002133 NULL
2134};
2135struct attribute_group md_bitmap_group = {
2136 .name = "bitmap",
2137 .attrs = md_bitmap_attrs,
2138};
2139