blob: 04df18e8885fc129a1c9ec13f76665737ea71faa [file] [log] [blame]
NeilBrown32a76272005-06-21 17:17:14 -07001/*
2 * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
3 *
4 * bitmap_create - sets up the bitmap structure
5 * bitmap_destroy - destroys the bitmap structure
6 *
7 * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.:
8 * - added disk storage for bitmap
9 * - changes to allow various bitmap chunk sizes
NeilBrown32a76272005-06-21 17:17:14 -070010 */
11
12/*
13 * Still to do:
14 *
15 * flush after percent set rather than just time based. (maybe both).
NeilBrown32a76272005-06-21 17:17:14 -070016 */
17
NeilBrownbff61972009-03-31 14:33:13 +110018#include <linux/blkdev.h>
NeilBrown32a76272005-06-21 17:17:14 -070019#include <linux/module.h>
NeilBrown32a76272005-06-21 17:17:14 -070020#include <linux/errno.h>
21#include <linux/slab.h>
22#include <linux/init.h>
NeilBrown32a76272005-06-21 17:17:14 -070023#include <linux/timer.h>
24#include <linux/sched.h>
25#include <linux/list.h>
26#include <linux/file.h>
27#include <linux/mount.h>
28#include <linux/buffer_head.h>
NeilBrown57148962012-03-19 12:46:40 +110029#include <linux/seq_file.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110030#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110031#include "bitmap.h"
NeilBrown32a76272005-06-21 17:17:14 -070032
NeilBrownac2f40b2010-06-01 19:37:31 +100033static inline char *bmname(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -070034{
35 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
36}
37
NeilBrown32a76272005-06-21 17:17:14 -070038/*
39 * just a placeholder - calls kmalloc for bitmap pages
40 */
41static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
42{
43 unsigned char *page;
44
NeilBrownac2f40b2010-06-01 19:37:31 +100045 page = kzalloc(PAGE_SIZE, GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -070046 if (!page)
47 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
48 else
NeilBrown36a4e1f2011-10-07 14:23:17 +110049 pr_debug("%s: bitmap_alloc_page: allocated page at %p\n",
50 bmname(bitmap), page);
NeilBrown32a76272005-06-21 17:17:14 -070051 return page;
52}
53
54/*
55 * for now just a placeholder -- just calls kfree for bitmap pages
56 */
57static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
58{
NeilBrown36a4e1f2011-10-07 14:23:17 +110059 pr_debug("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
NeilBrown32a76272005-06-21 17:17:14 -070060 kfree(page);
61}
62
63/*
64 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
65 *
66 * 1) check to see if this page is allocated, if it's not then try to alloc
67 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
68 * page pointer directly as a counter
69 *
70 * if we find our page, we increment the page's refcount so that it stays
71 * allocated while we're using it
72 */
NeilBrownac2f40b2010-06-01 19:37:31 +100073static int bitmap_checkpage(struct bitmap *bitmap,
74 unsigned long page, int create)
NeilBrownee305ac2009-09-23 18:06:44 +100075__releases(bitmap->lock)
76__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -070077{
78 unsigned char *mappage;
79
80 if (page >= bitmap->pages) {
NeilBrown1187cf02009-03-31 14:27:02 +110081 /* This can happen if bitmap_start_sync goes beyond
82 * End-of-device while looking for a whole page.
83 * It is harmless.
84 */
NeilBrown32a76272005-06-21 17:17:14 -070085 return -EINVAL;
86 }
87
NeilBrown32a76272005-06-21 17:17:14 -070088 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
89 return 0;
90
91 if (bitmap->bp[page].map) /* page is already allocated, just return */
92 return 0;
93
94 if (!create)
95 return -ENOENT;
96
NeilBrown32a76272005-06-21 17:17:14 -070097 /* this page has not been allocated yet */
98
NeilBrownac2f40b2010-06-01 19:37:31 +100099 spin_unlock_irq(&bitmap->lock);
100 mappage = bitmap_alloc_page(bitmap);
101 spin_lock_irq(&bitmap->lock);
102
103 if (mappage == NULL) {
NeilBrown36a4e1f2011-10-07 14:23:17 +1100104 pr_debug("%s: bitmap map page allocation failed, hijacking\n",
105 bmname(bitmap));
NeilBrown32a76272005-06-21 17:17:14 -0700106 /* failed - set the hijacked flag so that we can use the
107 * pointer as a counter */
NeilBrown32a76272005-06-21 17:17:14 -0700108 if (!bitmap->bp[page].map)
109 bitmap->bp[page].hijacked = 1;
NeilBrownac2f40b2010-06-01 19:37:31 +1000110 } else if (bitmap->bp[page].map ||
111 bitmap->bp[page].hijacked) {
NeilBrown32a76272005-06-21 17:17:14 -0700112 /* somebody beat us to getting the page */
113 bitmap_free_page(bitmap, mappage);
114 return 0;
NeilBrownac2f40b2010-06-01 19:37:31 +1000115 } else {
116
117 /* no page was in place and we have one, so install it */
118
119 bitmap->bp[page].map = mappage;
120 bitmap->missing_pages--;
NeilBrown32a76272005-06-21 17:17:14 -0700121 }
NeilBrown32a76272005-06-21 17:17:14 -0700122 return 0;
123}
124
NeilBrown32a76272005-06-21 17:17:14 -0700125/* if page is completely empty, put it back on the free list, or dealloc it */
126/* if page was hijacked, unmark the flag so it might get alloced next time */
127/* Note: lock should be held when calling this */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800128static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
NeilBrown32a76272005-06-21 17:17:14 -0700129{
130 char *ptr;
131
132 if (bitmap->bp[page].count) /* page is still busy */
133 return;
134
135 /* page is no longer in use, it can be released */
136
137 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
138 bitmap->bp[page].hijacked = 0;
139 bitmap->bp[page].map = NULL;
NeilBrownac2f40b2010-06-01 19:37:31 +1000140 } else {
141 /* normal case, free the page */
142 ptr = bitmap->bp[page].map;
143 bitmap->bp[page].map = NULL;
144 bitmap->missing_pages++;
145 bitmap_free_page(bitmap, ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700146 }
NeilBrown32a76272005-06-21 17:17:14 -0700147}
148
NeilBrown32a76272005-06-21 17:17:14 -0700149/*
150 * bitmap file handling - read and write the bitmap file and its superblock
151 */
152
NeilBrown32a76272005-06-21 17:17:14 -0700153/*
154 * basic page I/O operations
155 */
156
NeilBrowna654b9d82005-06-21 17:17:27 -0700157/* IO operations when bitmap is stored near all superblocks */
NeilBrownfd01b882011-10-11 16:47:53 +1100158static struct page *read_sb_page(struct mddev *mddev, loff_t offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100159 struct page *page,
160 unsigned long index, int size)
NeilBrowna654b9d82005-06-21 17:17:27 -0700161{
162 /* choose a good rdev and read the page from there */
163
NeilBrown3cb03002011-10-11 16:45:26 +1100164 struct md_rdev *rdev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700165 sector_t target;
NeilBrownac2f40b2010-06-01 19:37:31 +1000166 int did_alloc = 0;
NeilBrowna654b9d82005-06-21 17:17:27 -0700167
NeilBrownac2f40b2010-06-01 19:37:31 +1000168 if (!page) {
NeilBrowna2ed9612008-12-19 16:25:01 +1100169 page = alloc_page(GFP_KERNEL);
NeilBrownac2f40b2010-06-01 19:37:31 +1000170 if (!page)
171 return ERR_PTR(-ENOMEM);
172 did_alloc = 1;
173 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700174
NeilBrowndafb20f2012-03-19 12:46:39 +1100175 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800176 if (! test_bit(In_sync, &rdev->flags)
177 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700178 continue;
179
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100180 target = offset + index * (PAGE_SIZE/512);
NeilBrowna654b9d82005-06-21 17:17:27 -0700181
NeilBrown2b193362010-10-27 15:16:40 +1100182 if (sync_page_io(rdev, target,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400183 roundup(size, bdev_logical_block_size(rdev->bdev)),
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100184 page, READ, true)) {
NeilBrownab904d62005-09-09 16:23:52 -0700185 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700186 attach_page_buffers(page, NULL); /* so that free_buffer will
187 * quietly no-op */
NeilBrownab904d62005-09-09 16:23:52 -0700188 return page;
189 }
190 }
NeilBrownac2f40b2010-06-01 19:37:31 +1000191 if (did_alloc)
192 put_page(page);
NeilBrownab904d62005-09-09 16:23:52 -0700193 return ERR_PTR(-EIO);
NeilBrowna654b9d82005-06-21 17:17:27 -0700194
NeilBrowna654b9d82005-06-21 17:17:27 -0700195}
196
NeilBrownfd01b882011-10-11 16:47:53 +1100197static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev)
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000198{
199 /* Iterate the disks of an mddev, using rcu to protect access to the
200 * linked list, and raising the refcount of devices we return to ensure
201 * they don't disappear while in use.
202 * As devices are only added or removed when raid_disk is < 0 and
203 * nr_pending is 0 and In_sync is clear, the entries we return will
204 * still be in the same position on the list when we re-enter
205 * list_for_each_continue_rcu.
206 */
207 struct list_head *pos;
208 rcu_read_lock();
209 if (rdev == NULL)
210 /* start at the beginning */
211 pos = &mddev->disks;
212 else {
213 /* release the previous rdev and start from there. */
214 rdev_dec_pending(rdev, mddev);
215 pos = &rdev->same_set;
216 }
217 list_for_each_continue_rcu(pos, &mddev->disks) {
NeilBrown3cb03002011-10-11 16:45:26 +1100218 rdev = list_entry(pos, struct md_rdev, same_set);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000219 if (rdev->raid_disk >= 0 &&
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000220 !test_bit(Faulty, &rdev->flags)) {
221 /* this is a usable devices */
222 atomic_inc(&rdev->nr_pending);
223 rcu_read_unlock();
224 return rdev;
225 }
226 }
227 rcu_read_unlock();
228 return NULL;
229}
230
NeilBrownab6085c2007-05-23 13:58:10 -0700231static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrowna654b9d82005-06-21 17:17:27 -0700232{
NeilBrown3cb03002011-10-11 16:45:26 +1100233 struct md_rdev *rdev = NULL;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100234 struct block_device *bdev;
NeilBrownfd01b882011-10-11 16:47:53 +1100235 struct mddev *mddev = bitmap->mddev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700236
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000237 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000238 int size = PAGE_SIZE;
239 loff_t offset = mddev->bitmap_info.offset;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100240
241 bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
242
NeilBrownac2f40b2010-06-01 19:37:31 +1000243 if (page->index == bitmap->file_pages-1)
244 size = roundup(bitmap->last_page_size,
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100245 bdev_logical_block_size(bdev));
NeilBrownac2f40b2010-06-01 19:37:31 +1000246 /* Just make sure we aren't corrupting data or
247 * metadata
248 */
249 if (mddev->external) {
250 /* Bitmap could be anywhere. */
251 if (rdev->sb_start + offset + (page->index
252 * (PAGE_SIZE/512))
253 > rdev->data_offset
254 &&
255 rdev->sb_start + offset
256 < (rdev->data_offset + mddev->dev_sectors
257 + (PAGE_SIZE/512)))
258 goto bad_alignment;
259 } else if (offset < 0) {
260 /* DATA BITMAP METADATA */
261 if (offset
262 + (long)(page->index * (PAGE_SIZE/512))
263 + size/512 > 0)
264 /* bitmap runs in to metadata */
265 goto bad_alignment;
266 if (rdev->data_offset + mddev->dev_sectors
267 > rdev->sb_start + offset)
268 /* data runs in to bitmap */
269 goto bad_alignment;
270 } else if (rdev->sb_start < rdev->data_offset) {
271 /* METADATA BITMAP DATA */
272 if (rdev->sb_start
273 + offset
274 + page->index*(PAGE_SIZE/512) + size/512
275 > rdev->data_offset)
276 /* bitmap runs in to data */
277 goto bad_alignment;
278 } else {
279 /* DATA METADATA BITMAP - no problems */
280 }
281 md_super_write(mddev, rdev,
282 rdev->sb_start + offset
283 + page->index * (PAGE_SIZE/512),
284 size,
285 page);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000286 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700287
288 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800289 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700290 return 0;
NeilBrown4b809912008-07-21 17:05:25 +1000291
292 bad_alignment:
NeilBrown4b809912008-07-21 17:05:25 +1000293 return -EINVAL;
NeilBrowna654b9d82005-06-21 17:17:27 -0700294}
295
NeilBrown4ad13662007-07-17 04:06:13 -0700296static void bitmap_file_kick(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700297/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700298 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700299 */
NeilBrown4ad13662007-07-17 04:06:13 -0700300static void write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700301{
NeilBrownd785a062006-06-26 00:27:48 -0700302 struct buffer_head *bh;
NeilBrown32a76272005-06-21 17:17:14 -0700303
NeilBrownf0d76d72007-07-17 04:06:12 -0700304 if (bitmap->file == NULL) {
305 switch (write_sb_page(bitmap, page, wait)) {
306 case -EINVAL:
307 bitmap->flags |= BITMAP_WRITE_ERROR;
NeilBrownf0d76d72007-07-17 04:06:12 -0700308 }
NeilBrown4ad13662007-07-17 04:06:13 -0700309 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700310
NeilBrown4ad13662007-07-17 04:06:13 -0700311 bh = page_buffers(page);
NeilBrownc7084432006-01-06 00:20:45 -0800312
NeilBrown4ad13662007-07-17 04:06:13 -0700313 while (bh && bh->b_blocknr) {
314 atomic_inc(&bitmap->pending_writes);
315 set_buffer_locked(bh);
316 set_buffer_mapped(bh);
Jens Axboe721a9602011-03-09 11:56:30 +0100317 submit_bh(WRITE | REQ_SYNC, bh);
NeilBrown4ad13662007-07-17 04:06:13 -0700318 bh = bh->b_this_page;
319 }
NeilBrown32a76272005-06-21 17:17:14 -0700320
NeilBrownac2f40b2010-06-01 19:37:31 +1000321 if (wait)
NeilBrown4ad13662007-07-17 04:06:13 -0700322 wait_event(bitmap->write_wait,
323 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700324 }
NeilBrown4ad13662007-07-17 04:06:13 -0700325 if (bitmap->flags & BITMAP_WRITE_ERROR)
326 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700327}
328
NeilBrownd785a062006-06-26 00:27:48 -0700329static void end_bitmap_write(struct buffer_head *bh, int uptodate)
NeilBrown32a76272005-06-21 17:17:14 -0700330{
NeilBrownd785a062006-06-26 00:27:48 -0700331 struct bitmap *bitmap = bh->b_private;
332 unsigned long flags;
333
334 if (!uptodate) {
335 spin_lock_irqsave(&bitmap->lock, flags);
336 bitmap->flags |= BITMAP_WRITE_ERROR;
337 spin_unlock_irqrestore(&bitmap->lock, flags);
338 }
339 if (atomic_dec_and_test(&bitmap->pending_writes))
340 wake_up(&bitmap->write_wait);
341}
342
343/* copied from buffer.c */
344static void
345__clear_page_buffers(struct page *page)
346{
347 ClearPagePrivate(page);
348 set_page_private(page, 0);
349 page_cache_release(page);
350}
351static void free_buffers(struct page *page)
352{
353 struct buffer_head *bh = page_buffers(page);
354
355 while (bh) {
356 struct buffer_head *next = bh->b_this_page;
357 free_buffer_head(bh);
358 bh = next;
359 }
360 __clear_page_buffers(page);
361 put_page(page);
362}
363
364/* read a page from a file.
365 * We both read the page, and attach buffers to the page to record the
366 * address of each block (using bmap). These addresses will be used
367 * to write the block later, completely bypassing the filesystem.
368 * This usage is similar to how swap files are handled, and allows us
369 * to write to a file with no concerns of memory allocation failing.
370 */
371static struct page *read_page(struct file *file, unsigned long index,
372 struct bitmap *bitmap,
373 unsigned long count)
374{
NeilBrown32a76272005-06-21 17:17:14 -0700375 struct page *page = NULL;
Josef Sipekc649bb92006-12-08 02:37:19 -0800376 struct inode *inode = file->f_path.dentry->d_inode;
NeilBrownd785a062006-06-26 00:27:48 -0700377 struct buffer_head *bh;
378 sector_t block;
NeilBrown32a76272005-06-21 17:17:14 -0700379
NeilBrown36a4e1f2011-10-07 14:23:17 +1100380 pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
381 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700382
NeilBrownd785a062006-06-26 00:27:48 -0700383 page = alloc_page(GFP_KERNEL);
384 if (!page)
385 page = ERR_PTR(-ENOMEM);
NeilBrown32a76272005-06-21 17:17:14 -0700386 if (IS_ERR(page))
387 goto out;
NeilBrownd785a062006-06-26 00:27:48 -0700388
NeilBrownd785a062006-06-26 00:27:48 -0700389 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
390 if (!bh) {
391 put_page(page);
392 page = ERR_PTR(-ENOMEM);
393 goto out;
394 }
395 attach_page_buffers(page, bh);
396 block = index << (PAGE_SHIFT - inode->i_blkbits);
397 while (bh) {
398 if (count == 0)
399 bh->b_blocknr = 0;
400 else {
401 bh->b_blocknr = bmap(inode, block);
402 if (bh->b_blocknr == 0) {
403 /* Cannot use this file! */
404 free_buffers(page);
405 page = ERR_PTR(-EINVAL);
406 goto out;
407 }
408 bh->b_bdev = inode->i_sb->s_bdev;
409 if (count < (1<<inode->i_blkbits))
410 count = 0;
411 else
412 count -= (1<<inode->i_blkbits);
NeilBrown32a76272005-06-21 17:17:14 -0700413
NeilBrownd785a062006-06-26 00:27:48 -0700414 bh->b_end_io = end_bitmap_write;
415 bh->b_private = bitmap;
NeilBrownce25c312006-06-26 00:27:49 -0700416 atomic_inc(&bitmap->pending_writes);
417 set_buffer_locked(bh);
418 set_buffer_mapped(bh);
419 submit_bh(READ, bh);
NeilBrownd785a062006-06-26 00:27:48 -0700420 }
421 block++;
422 bh = bh->b_this_page;
423 }
NeilBrownd785a062006-06-26 00:27:48 -0700424 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700425
426 wait_event(bitmap->write_wait,
427 atomic_read(&bitmap->pending_writes)==0);
428 if (bitmap->flags & BITMAP_WRITE_ERROR) {
429 free_buffers(page);
430 page = ERR_PTR(-EIO);
431 }
NeilBrown32a76272005-06-21 17:17:14 -0700432out:
433 if (IS_ERR(page))
NeilBrownac2f40b2010-06-01 19:37:31 +1000434 printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800435 (int)PAGE_SIZE,
436 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700437 PTR_ERR(page));
438 return page;
439}
440
441/*
442 * bitmap file superblock operations
443 */
444
445/* update the event counter and sync the superblock to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700446void bitmap_update_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700447{
448 bitmap_super_t *sb;
449 unsigned long flags;
450
451 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
NeilBrown4ad13662007-07-17 04:06:13 -0700452 return;
NeilBrownece5cff2009-12-14 12:49:56 +1100453 if (bitmap->mddev->bitmap_info.external)
454 return;
NeilBrown32a76272005-06-21 17:17:14 -0700455 spin_lock_irqsave(&bitmap->lock, flags);
456 if (!bitmap->sb_page) { /* no superblock */
457 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700458 return;
NeilBrown32a76272005-06-21 17:17:14 -0700459 }
NeilBrown32a76272005-06-21 17:17:14 -0700460 spin_unlock_irqrestore(&bitmap->lock, flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100461 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700462 sb->events = cpu_to_le64(bitmap->mddev->events);
NeilBrown8258c532011-05-11 14:26:30 +1000463 if (bitmap->mddev->events < bitmap->events_cleared)
Neil Browna0da84f2008-06-28 08:31:22 +1000464 /* rocking back to read-only */
465 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown8258c532011-05-11 14:26:30 +1000466 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
467 sb->state = cpu_to_le32(bitmap->flags);
NeilBrown43a70502009-12-14 12:49:55 +1100468 /* Just in case these have been changed via sysfs: */
469 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
470 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
NeilBrownea03aff2006-01-06 00:20:34 -0800471 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700472 write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700473}
474
475/* print out the bitmap file superblock */
476void bitmap_print_sb(struct bitmap *bitmap)
477{
478 bitmap_super_t *sb;
479
480 if (!bitmap || !bitmap->sb_page)
481 return;
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100482 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700483 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700484 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
485 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
486 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700487 *(__u32 *)(sb->uuid+0),
488 *(__u32 *)(sb->uuid+4),
489 *(__u32 *)(sb->uuid+8),
490 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700491 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700492 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700493 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700494 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700495 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
496 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
497 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
498 printk(KERN_DEBUG " sync size: %llu KB\n",
499 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700500 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800501 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700502}
503
Jonathan Brassow9c810752011-06-08 17:59:30 -0500504/*
505 * bitmap_new_disk_sb
506 * @bitmap
507 *
508 * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
509 * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
510 * This function verifies 'bitmap_info' and populates the on-disk bitmap
511 * structure, which is to be written to disk.
512 *
513 * Returns: 0 on success, -Exxx on error
514 */
515static int bitmap_new_disk_sb(struct bitmap *bitmap)
516{
517 bitmap_super_t *sb;
518 unsigned long chunksize, daemon_sleep, write_behind;
519 int err = -EINVAL;
520
521 bitmap->sb_page = alloc_page(GFP_KERNEL);
522 if (IS_ERR(bitmap->sb_page)) {
523 err = PTR_ERR(bitmap->sb_page);
524 bitmap->sb_page = NULL;
525 return err;
526 }
527 bitmap->sb_page->index = 0;
528
529 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
530
531 sb->magic = cpu_to_le32(BITMAP_MAGIC);
532 sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
533
534 chunksize = bitmap->mddev->bitmap_info.chunksize;
535 BUG_ON(!chunksize);
536 if (!is_power_of_2(chunksize)) {
537 kunmap_atomic(sb, KM_USER0);
538 printk(KERN_ERR "bitmap chunksize not a power of 2\n");
539 return -EINVAL;
540 }
541 sb->chunksize = cpu_to_le32(chunksize);
542
543 daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
544 if (!daemon_sleep ||
545 (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
546 printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n");
547 daemon_sleep = 5 * HZ;
548 }
549 sb->daemon_sleep = cpu_to_le32(daemon_sleep);
550 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
551
552 /*
553 * FIXME: write_behind for RAID1. If not specified, what
554 * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
555 */
556 write_behind = bitmap->mddev->bitmap_info.max_write_behind;
557 if (write_behind > COUNTER_MAX)
558 write_behind = COUNTER_MAX / 2;
559 sb->write_behind = cpu_to_le32(write_behind);
560 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
561
562 /* keep the array size field of the bitmap superblock up to date */
563 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
564
565 memcpy(sb->uuid, bitmap->mddev->uuid, 16);
566
567 bitmap->flags |= BITMAP_STALE;
568 sb->state |= cpu_to_le32(BITMAP_STALE);
569 bitmap->events_cleared = bitmap->mddev->events;
570 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
571
572 bitmap->flags |= BITMAP_HOSTENDIAN;
573 sb->version = cpu_to_le32(BITMAP_MAJOR_HOSTENDIAN);
574
575 kunmap_atomic(sb, KM_USER0);
576
577 return 0;
578}
579
NeilBrown32a76272005-06-21 17:17:14 -0700580/* read the superblock from the bitmap file and initialize some bitmap fields */
581static int bitmap_read_sb(struct bitmap *bitmap)
582{
583 char *reason = NULL;
584 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700585 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700586 unsigned long long events;
587 int err = -EINVAL;
588
589 /* page 0 is the superblock, read it... */
NeilBrownf49d5e62007-01-26 00:57:03 -0800590 if (bitmap->file) {
591 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
592 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
593
594 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
595 } else {
NeilBrown42a04b52009-12-14 12:49:53 +1100596 bitmap->sb_page = read_sb_page(bitmap->mddev,
597 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100598 NULL,
599 0, sizeof(bitmap_super_t));
NeilBrowna654b9d82005-06-21 17:17:27 -0700600 }
NeilBrown32a76272005-06-21 17:17:14 -0700601 if (IS_ERR(bitmap->sb_page)) {
602 err = PTR_ERR(bitmap->sb_page);
603 bitmap->sb_page = NULL;
604 return err;
605 }
606
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100607 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700608
NeilBrown32a76272005-06-21 17:17:14 -0700609 chunksize = le32_to_cpu(sb->chunksize);
NeilBrown1b04be92009-12-14 12:49:53 +1100610 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
NeilBrown4b6d2872005-09-09 16:23:47 -0700611 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700612
613 /* verify that the bitmap-specific fields are valid */
614 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
615 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800616 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
617 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700618 reason = "unrecognized superblock version";
NeilBrown1187cf02009-03-31 14:27:02 +1100619 else if (chunksize < 512)
NeilBrown7dd5d342006-01-06 00:20:39 -0800620 reason = "bitmap chunksize too small";
Jonathan Brassowd7445402011-06-08 18:01:10 -0500621 else if (!is_power_of_2(chunksize))
NeilBrown32a76272005-06-21 17:17:14 -0700622 reason = "bitmap chunksize not a power of 2";
NeilBrown1b04be92009-12-14 12:49:53 +1100623 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
NeilBrown7dd5d342006-01-06 00:20:39 -0800624 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700625 else if (write_behind > COUNTER_MAX)
626 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700627 if (reason) {
628 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
629 bmname(bitmap), reason);
630 goto out;
631 }
632
633 /* keep the array size field of the bitmap superblock up to date */
634 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
635
636 if (!bitmap->mddev->persistent)
637 goto success;
638
639 /*
640 * if we have a persistent array superblock, compare the
641 * bitmap's UUID and event counter to the mddev's
642 */
643 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
644 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
645 bmname(bitmap));
646 goto out;
647 }
648 events = le64_to_cpu(sb->events);
649 if (events < bitmap->mddev->events) {
650 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
651 "-- forcing full recovery\n", bmname(bitmap), events,
652 (unsigned long long) bitmap->mddev->events);
NeilBrown4f2e6392006-10-21 10:24:09 -0700653 sb->state |= cpu_to_le32(BITMAP_STALE);
NeilBrown32a76272005-06-21 17:17:14 -0700654 }
655success:
656 /* assign fields using values from superblock */
NeilBrown42a04b52009-12-14 12:49:53 +1100657 bitmap->mddev->bitmap_info.chunksize = chunksize;
658 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
NeilBrown42a04b52009-12-14 12:49:53 +1100659 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
NeilBrown4f2e6392006-10-21 10:24:09 -0700660 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800661 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
662 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700663 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown8258c532011-05-11 14:26:30 +1000664 if (bitmap->flags & BITMAP_STALE)
NeilBrown6a079972005-09-09 16:23:44 -0700665 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700666 err = 0;
667out:
NeilBrownea03aff2006-01-06 00:20:34 -0800668 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700669 if (err)
670 bitmap_print_sb(bitmap);
671 return err;
672}
673
674enum bitmap_mask_op {
675 MASK_SET,
676 MASK_UNSET
677};
678
NeilBrown4ad13662007-07-17 04:06:13 -0700679/* record the state of the bitmap in the superblock. Return the old value */
680static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
681 enum bitmap_mask_op op)
NeilBrown32a76272005-06-21 17:17:14 -0700682{
683 bitmap_super_t *sb;
684 unsigned long flags;
NeilBrown4ad13662007-07-17 04:06:13 -0700685 int old;
NeilBrown32a76272005-06-21 17:17:14 -0700686
687 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800688 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700689 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700690 return 0;
NeilBrown32a76272005-06-21 17:17:14 -0700691 }
NeilBrown32a76272005-06-21 17:17:14 -0700692 spin_unlock_irqrestore(&bitmap->lock, flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100693 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700694 old = le32_to_cpu(sb->state) & bits;
NeilBrown32a76272005-06-21 17:17:14 -0700695 switch (op) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000696 case MASK_SET:
697 sb->state |= cpu_to_le32(bits);
NeilBrown8258c532011-05-11 14:26:30 +1000698 bitmap->flags |= bits;
NeilBrownac2f40b2010-06-01 19:37:31 +1000699 break;
700 case MASK_UNSET:
701 sb->state &= cpu_to_le32(~bits);
NeilBrown8258c532011-05-11 14:26:30 +1000702 bitmap->flags &= ~bits;
NeilBrownac2f40b2010-06-01 19:37:31 +1000703 break;
704 default:
705 BUG();
NeilBrown32a76272005-06-21 17:17:14 -0700706 }
NeilBrownea03aff2006-01-06 00:20:34 -0800707 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700708 return old;
NeilBrown32a76272005-06-21 17:17:14 -0700709}
710
711/*
712 * general bitmap file operations
713 */
714
NeilBrownece5cff2009-12-14 12:49:56 +1100715/*
716 * on-disk bitmap:
717 *
718 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
719 * file a page at a time. There's a superblock at the start of the file.
720 */
NeilBrown32a76272005-06-21 17:17:14 -0700721/* calculate the index of the page that contains this bit */
NeilBrownece5cff2009-12-14 12:49:56 +1100722static inline unsigned long file_page_index(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_BIT_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700727}
728
729/* calculate the (bit) offset of this bit within a page */
NeilBrownece5cff2009-12-14 12:49:56 +1100730static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700731{
NeilBrownece5cff2009-12-14 12:49:56 +1100732 if (!bitmap->mddev->bitmap_info.external)
733 chunk += sizeof(bitmap_super_t) << 3;
734 return chunk & (PAGE_BITS - 1);
NeilBrown32a76272005-06-21 17:17:14 -0700735}
736
737/*
738 * return a pointer to the page in the filemap that contains the given bit
739 *
740 * this lookup is complicated by the fact that the bitmap sb might be exactly
741 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
742 * 0 or page 1
743 */
744static inline struct page *filemap_get_page(struct bitmap *bitmap,
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000745 unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700746{
NeilBrownac2f40b2010-06-01 19:37:31 +1000747 if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
748 return NULL;
NeilBrownece5cff2009-12-14 12:49:56 +1100749 return bitmap->filemap[file_page_index(bitmap, chunk)
750 - file_page_index(bitmap, 0)];
NeilBrown32a76272005-06-21 17:17:14 -0700751}
752
NeilBrown32a76272005-06-21 17:17:14 -0700753static void bitmap_file_unmap(struct bitmap *bitmap)
754{
755 struct page **map, *sb_page;
756 unsigned long *attr;
757 int pages;
758 unsigned long flags;
759
760 spin_lock_irqsave(&bitmap->lock, flags);
761 map = bitmap->filemap;
762 bitmap->filemap = NULL;
763 attr = bitmap->filemap_attr;
764 bitmap->filemap_attr = NULL;
765 pages = bitmap->file_pages;
766 bitmap->file_pages = 0;
767 sb_page = bitmap->sb_page;
768 bitmap->sb_page = NULL;
769 spin_unlock_irqrestore(&bitmap->lock, flags);
770
771 while (pages--)
NeilBrownece5cff2009-12-14 12:49:56 +1100772 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700773 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700774 kfree(map);
775 kfree(attr);
776
NeilBrownd785a062006-06-26 00:27:48 -0700777 if (sb_page)
778 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700779}
780
781static void bitmap_file_put(struct bitmap *bitmap)
782{
783 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700784 unsigned long flags;
785
786 spin_lock_irqsave(&bitmap->lock, flags);
787 file = bitmap->file;
788 bitmap->file = NULL;
789 spin_unlock_irqrestore(&bitmap->lock, flags);
790
NeilBrownd785a062006-06-26 00:27:48 -0700791 if (file)
792 wait_event(bitmap->write_wait,
793 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700794 bitmap_file_unmap(bitmap);
795
NeilBrownd785a062006-06-26 00:27:48 -0700796 if (file) {
Josef Sipekc649bb92006-12-08 02:37:19 -0800797 struct inode *inode = file->f_path.dentry->d_inode;
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800798 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700799 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700800 }
NeilBrown32a76272005-06-21 17:17:14 -0700801}
802
NeilBrown32a76272005-06-21 17:17:14 -0700803/*
804 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
805 * then it is no longer reliable, so we stop using it and we mark the file
806 * as failed in the superblock
807 */
808static void bitmap_file_kick(struct bitmap *bitmap)
809{
810 char *path, *ptr = NULL;
811
NeilBrown4ad13662007-07-17 04:06:13 -0700812 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
813 bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700814
NeilBrown4ad13662007-07-17 04:06:13 -0700815 if (bitmap->file) {
816 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
817 if (path)
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700818 ptr = d_path(&bitmap->file->f_path, path,
819 PAGE_SIZE);
820
NeilBrown4ad13662007-07-17 04:06:13 -0700821 printk(KERN_ALERT
822 "%s: kicking failed bitmap file %s from array!\n",
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700823 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700824
NeilBrown4ad13662007-07-17 04:06:13 -0700825 kfree(path);
826 } else
827 printk(KERN_ALERT
828 "%s: disabling internal bitmap due to errors\n",
829 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700830 }
NeilBrown32a76272005-06-21 17:17:14 -0700831
832 bitmap_file_put(bitmap);
833
834 return;
835}
836
837enum bitmap_page_attr {
NeilBrownac2f40b2010-06-01 19:37:31 +1000838 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
NeilBrown5a537df2011-09-21 15:37:46 +1000839 BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
840 * i.e. counter is 1 or 2. */
NeilBrownac2f40b2010-06-01 19:37:31 +1000841 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
NeilBrown32a76272005-06-21 17:17:14 -0700842};
843
844static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
845 enum bitmap_page_attr attr)
846{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000847 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700848}
849
850static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
851 enum bitmap_page_attr attr)
852{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000853 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700854}
855
NeilBrownec7a3192006-06-26 00:27:45 -0700856static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
857 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700858{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000859 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700860}
861
862/*
863 * bitmap_file_set_bit -- called before performing a write to the md device
864 * to set (and eventually sync) a particular bit in the bitmap file
865 *
866 * we set the bit immediately, then we record the page number so that
867 * when an unplug occurs, we can flush the dirty pages out to disk
868 */
869static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
870{
871 unsigned long bit;
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000872 struct page *page;
NeilBrown32a76272005-06-21 17:17:14 -0700873 void *kaddr;
874 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
875
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000876 if (!bitmap->filemap)
877 return;
NeilBrown32a76272005-06-21 17:17:14 -0700878
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000879 page = filemap_get_page(bitmap, chunk);
880 if (!page)
881 return;
882 bit = file_page_offset(bitmap, chunk);
NeilBrown32a76272005-06-21 17:17:14 -0700883
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000884 /* set the bit */
885 kaddr = kmap_atomic(page, KM_USER0);
886 if (bitmap->flags & BITMAP_HOSTENDIAN)
887 set_bit(bit, kaddr);
888 else
889 __set_bit_le(bit, kaddr);
890 kunmap_atomic(kaddr, KM_USER0);
NeilBrown36a4e1f2011-10-07 14:23:17 +1100891 pr_debug("set file bit %lu page %lu\n", bit, page->index);
NeilBrown32a76272005-06-21 17:17:14 -0700892 /* record page number so it gets flushed to disk when unplug occurs */
893 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
NeilBrown32a76272005-06-21 17:17:14 -0700894}
895
896/* this gets called when the md device is ready to unplug its underlying
897 * (slave) device queues -- before we let any writes go down, we need to
898 * sync the dirty pages of the bitmap file to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700899void bitmap_unplug(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700900{
NeilBrownec7a3192006-06-26 00:27:45 -0700901 unsigned long i, flags;
902 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700903 struct page *page;
904 int wait = 0;
905
906 if (!bitmap)
NeilBrown4ad13662007-07-17 04:06:13 -0700907 return;
NeilBrown32a76272005-06-21 17:17:14 -0700908
909 /* look at each page to see if there are any set bits that need to be
910 * flushed out to disk */
911 for (i = 0; i < bitmap->file_pages; i++) {
912 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700913 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700914 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700915 return;
NeilBrown32a76272005-06-21 17:17:14 -0700916 }
917 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700918 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
919 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700920 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
921 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700922 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700923 wait = 1;
924 spin_unlock_irqrestore(&bitmap->lock, flags);
925
NeilBrownac2f40b2010-06-01 19:37:31 +1000926 if (dirty || need_write)
NeilBrown4ad13662007-07-17 04:06:13 -0700927 write_page(bitmap, page, 0);
NeilBrown32a76272005-06-21 17:17:14 -0700928 }
929 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700930 if (bitmap->file)
NeilBrownd785a062006-06-26 00:27:48 -0700931 wait_event(bitmap->write_wait,
932 atomic_read(&bitmap->pending_writes)==0);
NeilBrown0b79ccf2006-06-26 00:27:44 -0700933 else
NeilBrowna9701a32005-11-08 21:39:34 -0800934 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700935 }
NeilBrownd785a062006-06-26 00:27:48 -0700936 if (bitmap->flags & BITMAP_WRITE_ERROR)
937 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700938}
NeilBrownac2f40b2010-06-01 19:37:31 +1000939EXPORT_SYMBOL(bitmap_unplug);
NeilBrown32a76272005-06-21 17:17:14 -0700940
NeilBrown6a079972005-09-09 16:23:44 -0700941static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700942/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
943 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
944 * memory mapping of the bitmap file
945 * Special cases:
946 * if there's no bitmap file, or if the bitmap file had been
947 * previously kicked from the array, we mark all the bits as
948 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700949 *
950 * We ignore all bits for sectors that end earlier than 'start'.
951 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700952 */
NeilBrown6a079972005-09-09 16:23:44 -0700953static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700954{
955 unsigned long i, chunks, index, oldindex, bit;
956 struct page *page = NULL, *oldpage = NULL;
957 unsigned long num_pages, bit_cnt = 0;
958 struct file *file;
NeilBrownd785a062006-06-26 00:27:48 -0700959 unsigned long bytes, offset;
NeilBrown32a76272005-06-21 17:17:14 -0700960 int outofdate;
961 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800962 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700963
964 chunks = bitmap->chunks;
965 file = bitmap->file;
966
NeilBrown42a04b52009-12-14 12:49:53 +1100967 BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
NeilBrown32a76272005-06-21 17:17:14 -0700968
NeilBrown32a76272005-06-21 17:17:14 -0700969 outofdate = bitmap->flags & BITMAP_STALE;
NeilBrown32a76272005-06-21 17:17:14 -0700970 if (outofdate)
971 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
972 "recovery\n", bmname(bitmap));
973
NeilBrowne384e582010-06-01 19:37:34 +1000974 bytes = DIV_ROUND_UP(bitmap->chunks, 8);
NeilBrownece5cff2009-12-14 12:49:56 +1100975 if (!bitmap->mddev->bitmap_info.external)
976 bytes += sizeof(bitmap_super_t);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700977
NeilBrowne384e582010-06-01 19:37:34 +1000978 num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700979
NeilBrownece5cff2009-12-14 12:49:56 +1100980 if (file && i_size_read(file->f_mapping->host) < bytes) {
NeilBrown32a76272005-06-21 17:17:14 -0700981 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
982 bmname(bitmap),
983 (unsigned long) i_size_read(file->f_mapping->host),
NeilBrownece5cff2009-12-14 12:49:56 +1100984 bytes);
NeilBrown4ad13662007-07-17 04:06:13 -0700985 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700986 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700987
988 ret = -ENOMEM;
989
NeilBrown32a76272005-06-21 17:17:14 -0700990 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700991 if (!bitmap->filemap)
NeilBrown4ad13662007-07-17 04:06:13 -0700992 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700993
NeilBrowne16b68b2006-06-26 00:27:45 -0700994 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
995 bitmap->filemap_attr = kzalloc(
NeilBrownac2f40b2010-06-01 19:37:31 +1000996 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
NeilBrowne16b68b2006-06-26 00:27:45 -0700997 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700998 if (!bitmap->filemap_attr)
NeilBrown4ad13662007-07-17 04:06:13 -0700999 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001000
NeilBrown32a76272005-06-21 17:17:14 -07001001 oldindex = ~0L;
1002
1003 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -08001004 int b;
NeilBrownece5cff2009-12-14 12:49:56 +11001005 index = file_page_index(bitmap, i);
1006 bit = file_page_offset(bitmap, i);
NeilBrown32a76272005-06-21 17:17:14 -07001007 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -07001008 int count;
NeilBrown32a76272005-06-21 17:17:14 -07001009 /* unmap the old page, we're done with it */
NeilBrownd785a062006-06-26 00:27:48 -07001010 if (index == num_pages-1)
NeilBrownece5cff2009-12-14 12:49:56 +11001011 count = bytes - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -07001012 else
1013 count = PAGE_SIZE;
NeilBrownece5cff2009-12-14 12:49:56 +11001014 if (index == 0 && bitmap->sb_page) {
NeilBrown32a76272005-06-21 17:17:14 -07001015 /*
1016 * if we're here then the superblock page
1017 * contains some bits (PAGE_SIZE != sizeof sb)
1018 * we've already read it in, so just use it
1019 */
1020 page = bitmap->sb_page;
1021 offset = sizeof(bitmap_super_t);
NeilBrown53845272009-01-09 08:31:05 +11001022 if (!file)
Vasiliy Kulikov5c04f552010-10-01 14:18:12 -07001023 page = read_sb_page(
1024 bitmap->mddev,
1025 bitmap->mddev->bitmap_info.offset,
1026 page,
1027 index, count);
NeilBrowna654b9d82005-06-21 17:17:27 -07001028 } else if (file) {
NeilBrownd785a062006-06-26 00:27:48 -07001029 page = read_page(file, index, bitmap, count);
NeilBrowna654b9d82005-06-21 17:17:27 -07001030 offset = 0;
1031 } else {
NeilBrown42a04b52009-12-14 12:49:53 +11001032 page = read_sb_page(bitmap->mddev,
1033 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +11001034 NULL,
1035 index, count);
NeilBrown32a76272005-06-21 17:17:14 -07001036 offset = 0;
1037 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001038 if (IS_ERR(page)) { /* read error */
1039 ret = PTR_ERR(page);
NeilBrown4ad13662007-07-17 04:06:13 -07001040 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -07001041 }
1042
NeilBrown32a76272005-06-21 17:17:14 -07001043 oldindex = index;
1044 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -07001045
NeilBrownb74fd282009-05-07 12:47:19 +10001046 bitmap->filemap[bitmap->file_pages++] = page;
1047 bitmap->last_page_size = count;
1048
NeilBrown32a76272005-06-21 17:17:14 -07001049 if (outofdate) {
1050 /*
1051 * if bitmap is out of date, dirty the
NeilBrownac2f40b2010-06-01 19:37:31 +10001052 * whole page and write it out
NeilBrown32a76272005-06-21 17:17:14 -07001053 */
NeilBrownea03aff2006-01-06 00:20:34 -08001054 paddr = kmap_atomic(page, KM_USER0);
1055 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -07001056 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -08001057 kunmap_atomic(paddr, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -07001058 write_page(bitmap, page, 1);
1059
1060 ret = -EIO;
NeilBrownb74fd282009-05-07 12:47:19 +10001061 if (bitmap->flags & BITMAP_WRITE_ERROR)
NeilBrown4ad13662007-07-17 04:06:13 -07001062 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001063 }
NeilBrown32a76272005-06-21 17:17:14 -07001064 }
NeilBrownea03aff2006-01-06 00:20:34 -08001065 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001066 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001067 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001068 else
Akinobu Mita6b33aff2011-03-23 16:42:13 -07001069 b = test_bit_le(bit, paddr);
NeilBrownea03aff2006-01-06 00:20:34 -08001070 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001071 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -07001072 /* if the disk bit is set, set the memory bit */
NeilBrowndb305e52009-05-07 12:49:06 +10001073 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1074 >= start);
1075 bitmap_set_memory_bits(bitmap,
1076 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1077 needed);
NeilBrown32a76272005-06-21 17:17:14 -07001078 bit_cnt++;
1079 }
NeilBrown32a76272005-06-21 17:17:14 -07001080 }
1081
NeilBrownac2f40b2010-06-01 19:37:31 +10001082 /* everything went OK */
NeilBrown32a76272005-06-21 17:17:14 -07001083 ret = 0;
1084 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1085
NeilBrown32a76272005-06-21 17:17:14 -07001086 if (bit_cnt) { /* Kick recovery if any bits were set */
1087 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1088 md_wakeup_thread(bitmap->mddev->thread);
1089 }
1090
NeilBrown32a76272005-06-21 17:17:14 -07001091 printk(KERN_INFO "%s: bitmap initialized from disk: "
Jonathan Brassow9c810752011-06-08 17:59:30 -05001092 "read %lu/%lu pages, set %lu of %lu bits\n",
1093 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, chunks);
NeilBrown32a76272005-06-21 17:17:14 -07001094
NeilBrown4ad13662007-07-17 04:06:13 -07001095 return 0;
1096
1097 err:
1098 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1099 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001100 return ret;
1101}
1102
NeilBrowna654b9d82005-06-21 17:17:27 -07001103void bitmap_write_all(struct bitmap *bitmap)
1104{
1105 /* We don't actually write all bitmap blocks here,
1106 * just flag them as needing to be written
1107 */
NeilBrownec7a3192006-06-26 00:27:45 -07001108 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001109
NeilBrown7c8f4242011-11-23 10:18:52 +11001110 spin_lock_irq(&bitmap->lock);
NeilBrownac2f40b2010-06-01 19:37:31 +10001111 for (i = 0; i < bitmap->file_pages; i++)
NeilBrownec7a3192006-06-26 00:27:45 -07001112 set_page_attr(bitmap, bitmap->filemap[i],
1113 BITMAP_PAGE_NEEDWRITE);
NeilBrown2585f3e2011-09-21 15:37:46 +10001114 bitmap->allclean = 0;
NeilBrown7c8f4242011-11-23 10:18:52 +11001115 spin_unlock_irq(&bitmap->lock);
NeilBrowna654b9d82005-06-21 17:17:27 -07001116}
1117
NeilBrown32a76272005-06-21 17:17:14 -07001118static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1119{
1120 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1121 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1122 bitmap->bp[page].count += inc;
NeilBrown32a76272005-06-21 17:17:14 -07001123 bitmap_checkfree(bitmap, page);
1124}
1125static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001126 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001127 int create);
1128
1129/*
1130 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1131 * out to disk
1132 */
1133
NeilBrownfd01b882011-10-11 16:47:53 +11001134void bitmap_daemon_work(struct mddev *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001135{
NeilBrownaa5cbd12009-12-14 12:49:46 +11001136 struct bitmap *bitmap;
NeilBrownaa3163f2005-06-21 17:17:22 -07001137 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001138 unsigned long flags;
1139 struct page *page = NULL, *lastpage = NULL;
NeilBrown57dab0b2010-10-19 10:03:39 +11001140 sector_t blocks;
NeilBrownea03aff2006-01-06 00:20:34 -08001141 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001142
NeilBrownaa5cbd12009-12-14 12:49:46 +11001143 /* Use a mutex to guard daemon_work against
1144 * bitmap_destroy.
1145 */
NeilBrownc3d97142009-12-14 12:49:52 +11001146 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrownaa5cbd12009-12-14 12:49:46 +11001147 bitmap = mddev->bitmap;
1148 if (bitmap == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001149 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown4ad13662007-07-17 04:06:13 -07001150 return;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001151 }
NeilBrown42a04b52009-12-14 12:49:53 +11001152 if (time_before(jiffies, bitmap->daemon_lastrun
NeilBrown2e61ebb2011-12-23 10:17:50 +11001153 + mddev->bitmap_info.daemon_sleep))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001154 goto done;
1155
NeilBrown32a76272005-06-21 17:17:14 -07001156 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001157 if (bitmap->allclean) {
NeilBrown2e61ebb2011-12-23 10:17:50 +11001158 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001159 goto done;
NeilBrown8311c292008-03-04 14:29:30 -08001160 }
1161 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001162
NeilBrownbe512692009-05-26 09:41:17 +10001163 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001164 for (j = 0; j < bitmap->chunks; j++) {
1165 bitmap_counter_t *bmc;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001166 if (!bitmap->filemap)
1167 /* error or shutdown */
1168 break;
1169
1170 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001171
1172 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001173 /* skip this page unless it's marked as needing cleaning */
NeilBrown5a537df2011-09-21 15:37:46 +10001174 if (!test_page_attr(bitmap, page, BITMAP_PAGE_PENDING)) {
NeilBrownec7a3192006-06-26 00:27:45 -07001175 int need_write = test_page_attr(bitmap, page,
1176 BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001177 if (need_write)
NeilBrownaa3163f2005-06-21 17:17:22 -07001178 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001179
NeilBrownaa3163f2005-06-21 17:17:22 -07001180 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown2585f3e2011-09-21 15:37:46 +10001181 if (need_write)
NeilBrown4ad13662007-07-17 04:06:13 -07001182 write_page(bitmap, page, 0);
NeilBrownbe512692009-05-26 09:41:17 +10001183 spin_lock_irqsave(&bitmap->lock, flags);
1184 j |= (PAGE_BITS - 1);
NeilBrownaa3163f2005-06-21 17:17:22 -07001185 continue;
1186 }
1187
NeilBrown32a76272005-06-21 17:17:14 -07001188 /* grab the new page, sync and release the old */
NeilBrown32a76272005-06-21 17:17:14 -07001189 if (lastpage != NULL) {
NeilBrown2585f3e2011-09-21 15:37:46 +10001190 if (test_page_attr(bitmap, lastpage,
1191 BITMAP_PAGE_NEEDWRITE)) {
1192 clear_page_attr(bitmap, lastpage,
1193 BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -07001194 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001195 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001196 } else {
NeilBrown2585f3e2011-09-21 15:37:46 +10001197 set_page_attr(bitmap, lastpage,
1198 BITMAP_PAGE_NEEDWRITE);
1199 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001200 spin_unlock_irqrestore(&bitmap->lock, flags);
1201 }
NeilBrown32a76272005-06-21 17:17:14 -07001202 } else
1203 spin_unlock_irqrestore(&bitmap->lock, flags);
1204 lastpage = page;
Neil Browna0da84f2008-06-28 08:31:22 +10001205
1206 /* We are possibly going to clear some bits, so make
1207 * sure that events_cleared is up-to-date.
1208 */
NeilBrownece5cff2009-12-14 12:49:56 +11001209 if (bitmap->need_sync &&
NeilBrown2e61ebb2011-12-23 10:17:50 +11001210 mddev->bitmap_info.external == 0) {
Neil Browna0da84f2008-06-28 08:31:22 +10001211 bitmap_super_t *sb;
1212 bitmap->need_sync = 0;
1213 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1214 sb->events_cleared =
1215 cpu_to_le64(bitmap->events_cleared);
1216 kunmap_atomic(sb, KM_USER0);
1217 write_page(bitmap, bitmap->sb_page, 1);
1218 }
NeilBrown32a76272005-06-21 17:17:14 -07001219 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownece5cff2009-12-14 12:49:56 +11001220 if (!bitmap->need_sync)
NeilBrown5a537df2011-09-21 15:37:46 +10001221 clear_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001222 else
1223 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001224 }
NeilBrowndb305e52009-05-07 12:49:06 +10001225 bmc = bitmap_get_counter(bitmap,
1226 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1227 &blocks, 0);
NeilBrown5a537df2011-09-21 15:37:46 +10001228 if (!bmc)
1229 j |= PAGE_COUNTER_MASK;
1230 else if (*bmc) {
NeilBrown5a537df2011-09-21 15:37:46 +10001231 if (*bmc == 1 && !bitmap->need_sync) {
NeilBrown32a76272005-06-21 17:17:14 -07001232 /* we can clear the bit */
1233 *bmc = 0;
NeilBrowndb305e52009-05-07 12:49:06 +10001234 bitmap_count_page(bitmap,
1235 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
NeilBrown32a76272005-06-21 17:17:14 -07001236 -1);
1237
1238 /* clear the bit */
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001239 paddr = kmap_atomic(page, KM_USER0);
1240 if (bitmap->flags & BITMAP_HOSTENDIAN)
1241 clear_bit(file_page_offset(bitmap, j),
1242 paddr);
1243 else
1244 __clear_bit_le(
NeilBrown5a537df2011-09-21 15:37:46 +10001245 file_page_offset(bitmap,
1246 j),
1247 paddr);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001248 kunmap_atomic(paddr, KM_USER0);
NeilBrown5a537df2011-09-21 15:37:46 +10001249 } else if (*bmc <= 2) {
1250 *bmc = 1; /* maybe clear the bit next time */
1251 set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001252 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001253 }
NeilBrown5a537df2011-09-21 15:37:46 +10001254 }
NeilBrown32a76272005-06-21 17:17:14 -07001255 }
NeilBrownbe512692009-05-26 09:41:17 +10001256 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001257
1258 /* now sync the final page */
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001259 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001260 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001261 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001262 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1263 spin_unlock_irqrestore(&bitmap->lock, flags);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001264 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001265 } else {
1266 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
NeilBrown2585f3e2011-09-21 15:37:46 +10001267 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001268 spin_unlock_irqrestore(&bitmap->lock, flags);
1269 }
NeilBrown32a76272005-06-21 17:17:14 -07001270 }
1271
NeilBrown7be3dfe2008-03-10 11:43:48 -07001272 done:
NeilBrown8311c292008-03-04 14:29:30 -08001273 if (bitmap->allclean == 0)
NeilBrown2e61ebb2011-12-23 10:17:50 +11001274 mddev->thread->timeout =
1275 mddev->bitmap_info.daemon_sleep;
NeilBrownc3d97142009-12-14 12:49:52 +11001276 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown32a76272005-06-21 17:17:14 -07001277}
1278
NeilBrown32a76272005-06-21 17:17:14 -07001279static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001280 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001281 int create)
NeilBrownee305ac2009-09-23 18:06:44 +10001282__releases(bitmap->lock)
1283__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -07001284{
1285 /* If 'create', we might release the lock and reclaim it.
1286 * The lock must have been taken with interrupts enabled.
1287 * If !create, we don't release the lock.
1288 */
1289 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1290 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1291 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1292 sector_t csize;
NeilBrownef425672010-06-01 19:37:33 +10001293 int err;
NeilBrown32a76272005-06-21 17:17:14 -07001294
NeilBrownef425672010-06-01 19:37:33 +10001295 err = bitmap_checkpage(bitmap, page, create);
1296
1297 if (bitmap->bp[page].hijacked ||
1298 bitmap->bp[page].map == NULL)
1299 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1300 PAGE_COUNTER_SHIFT - 1);
1301 else
NeilBrown32a76272005-06-21 17:17:14 -07001302 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
NeilBrownef425672010-06-01 19:37:33 +10001303 *blocks = csize - (offset & (csize - 1));
1304
1305 if (err < 0)
NeilBrown32a76272005-06-21 17:17:14 -07001306 return NULL;
NeilBrownef425672010-06-01 19:37:33 +10001307
NeilBrown32a76272005-06-21 17:17:14 -07001308 /* now locked ... */
1309
1310 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1311 /* should we use the first or second counter field
1312 * of the hijacked pointer? */
1313 int hi = (pageoff > PAGE_COUNTER_MASK);
NeilBrown32a76272005-06-21 17:17:14 -07001314 return &((bitmap_counter_t *)
1315 &bitmap->bp[page].map)[hi];
NeilBrownef425672010-06-01 19:37:33 +10001316 } else /* page is allocated */
NeilBrown32a76272005-06-21 17:17:14 -07001317 return (bitmap_counter_t *)
1318 &(bitmap->bp[page].map[pageoff]);
NeilBrown32a76272005-06-21 17:17:14 -07001319}
1320
NeilBrown4b6d2872005-09-09 16:23:47 -07001321int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001322{
NeilBrownac2f40b2010-06-01 19:37:31 +10001323 if (!bitmap)
1324 return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001325
1326 if (behind) {
Paul Clements696fcd52010-03-08 16:02:37 +11001327 int bw;
NeilBrown4b6d2872005-09-09 16:23:47 -07001328 atomic_inc(&bitmap->behind_writes);
Paul Clements696fcd52010-03-08 16:02:37 +11001329 bw = atomic_read(&bitmap->behind_writes);
1330 if (bw > bitmap->behind_writes_used)
1331 bitmap->behind_writes_used = bw;
1332
NeilBrown36a4e1f2011-10-07 14:23:17 +11001333 pr_debug("inc write-behind count %d/%lu\n",
1334 bw, bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001335 }
1336
NeilBrown32a76272005-06-21 17:17:14 -07001337 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001338 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001339 bitmap_counter_t *bmc;
1340
1341 spin_lock_irq(&bitmap->lock);
1342 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1343 if (!bmc) {
1344 spin_unlock_irq(&bitmap->lock);
1345 return 0;
1346 }
1347
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001348 if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
Neil Brownda6e1a32007-02-08 14:20:37 -08001349 DEFINE_WAIT(__wait);
1350 /* note that it is safe to do the prepare_to_wait
1351 * after the test as long as we do it before dropping
1352 * the spinlock.
1353 */
1354 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1355 TASK_UNINTERRUPTIBLE);
1356 spin_unlock_irq(&bitmap->lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001357 io_schedule();
Neil Brownda6e1a32007-02-08 14:20:37 -08001358 finish_wait(&bitmap->overflow_wait, &__wait);
1359 continue;
1360 }
1361
NeilBrownac2f40b2010-06-01 19:37:31 +10001362 switch (*bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001363 case 0:
1364 bitmap_file_set_bit(bitmap, offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001365 bitmap_count_page(bitmap, offset, 1);
NeilBrown32a76272005-06-21 17:17:14 -07001366 /* fall through */
1367 case 1:
1368 *bmc = 2;
1369 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001370
NeilBrown32a76272005-06-21 17:17:14 -07001371 (*bmc)++;
1372
1373 spin_unlock_irq(&bitmap->lock);
1374
1375 offset += blocks;
1376 if (sectors > blocks)
1377 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001378 else
1379 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001380 }
1381 return 0;
1382}
NeilBrownac2f40b2010-06-01 19:37:31 +10001383EXPORT_SYMBOL(bitmap_startwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001384
1385void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001386 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001387{
NeilBrownac2f40b2010-06-01 19:37:31 +10001388 if (!bitmap)
1389 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001390 if (behind) {
NeilBrowne5551902010-03-31 11:21:44 +11001391 if (atomic_dec_and_test(&bitmap->behind_writes))
1392 wake_up(&bitmap->behind_wait);
NeilBrown36a4e1f2011-10-07 14:23:17 +11001393 pr_debug("dec write-behind count %d/%lu\n",
1394 atomic_read(&bitmap->behind_writes),
1395 bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001396 }
1397
NeilBrown32a76272005-06-21 17:17:14 -07001398 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001399 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001400 unsigned long flags;
1401 bitmap_counter_t *bmc;
1402
1403 spin_lock_irqsave(&bitmap->lock, flags);
1404 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1405 if (!bmc) {
1406 spin_unlock_irqrestore(&bitmap->lock, flags);
1407 return;
1408 }
1409
NeilBrown961902c2011-12-23 09:57:48 +11001410 if (success && !bitmap->mddev->degraded &&
Neil Browna0da84f2008-06-28 08:31:22 +10001411 bitmap->events_cleared < bitmap->mddev->events) {
1412 bitmap->events_cleared = bitmap->mddev->events;
1413 bitmap->need_sync = 1;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001414 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
Neil Browna0da84f2008-06-28 08:31:22 +10001415 }
1416
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001417 if (!success && !NEEDED(*bmc))
NeilBrown32a76272005-06-21 17:17:14 -07001418 *bmc |= NEEDED_MASK;
1419
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001420 if (COUNTER(*bmc) == COUNTER_MAX)
Neil Brownda6e1a32007-02-08 14:20:37 -08001421 wake_up(&bitmap->overflow_wait);
1422
NeilBrown32a76272005-06-21 17:17:14 -07001423 (*bmc)--;
NeilBrown2585f3e2011-09-21 15:37:46 +10001424 if (*bmc <= 2) {
NeilBrown32a76272005-06-21 17:17:14 -07001425 set_page_attr(bitmap,
NeilBrowne384e582010-06-01 19:37:34 +10001426 filemap_get_page(
1427 bitmap,
1428 offset >> CHUNK_BLOCK_SHIFT(bitmap)),
NeilBrown5a537df2011-09-21 15:37:46 +10001429 BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001430 bitmap->allclean = 0;
1431 }
NeilBrown32a76272005-06-21 17:17:14 -07001432 spin_unlock_irqrestore(&bitmap->lock, flags);
1433 offset += blocks;
1434 if (sectors > blocks)
1435 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001436 else
1437 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001438 }
1439}
NeilBrownac2f40b2010-06-01 19:37:31 +10001440EXPORT_SYMBOL(bitmap_endwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001441
NeilBrown57dab0b2010-10-19 10:03:39 +11001442static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001443 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001444{
1445 bitmap_counter_t *bmc;
1446 int rv;
1447 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1448 *blocks = 1024;
1449 return 1; /* always resync if no bitmap */
1450 }
1451 spin_lock_irq(&bitmap->lock);
1452 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1453 rv = 0;
1454 if (bmc) {
1455 /* locked */
1456 if (RESYNC(*bmc))
1457 rv = 1;
1458 else if (NEEDED(*bmc)) {
1459 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001460 if (!degraded) { /* don't set/clear bits if degraded */
1461 *bmc |= RESYNC_MASK;
1462 *bmc &= ~NEEDED_MASK;
1463 }
NeilBrown32a76272005-06-21 17:17:14 -07001464 }
1465 }
1466 spin_unlock_irq(&bitmap->lock);
1467 return rv;
1468}
1469
NeilBrown57dab0b2010-10-19 10:03:39 +11001470int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001471 int degraded)
1472{
1473 /* bitmap_start_sync must always report on multiples of whole
1474 * pages, otherwise resync (which is very PAGE_SIZE based) will
1475 * get confused.
1476 * So call __bitmap_start_sync repeatedly (if needed) until
1477 * At least PAGE_SIZE>>9 blocks are covered.
1478 * Return the 'or' of the result.
1479 */
1480 int rv = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001481 sector_t blocks1;
NeilBrown1187cf02009-03-31 14:27:02 +11001482
1483 *blocks = 0;
1484 while (*blocks < (PAGE_SIZE>>9)) {
1485 rv |= __bitmap_start_sync(bitmap, offset,
1486 &blocks1, degraded);
1487 offset += blocks1;
1488 *blocks += blocks1;
1489 }
1490 return rv;
1491}
NeilBrownac2f40b2010-06-01 19:37:31 +10001492EXPORT_SYMBOL(bitmap_start_sync);
NeilBrown1187cf02009-03-31 14:27:02 +11001493
NeilBrown57dab0b2010-10-19 10:03:39 +11001494void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
NeilBrown32a76272005-06-21 17:17:14 -07001495{
1496 bitmap_counter_t *bmc;
1497 unsigned long flags;
NeilBrownac2f40b2010-06-01 19:37:31 +10001498
1499 if (bitmap == NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001500 *blocks = 1024;
1501 return;
1502 }
1503 spin_lock_irqsave(&bitmap->lock, flags);
1504 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1505 if (bmc == NULL)
1506 goto unlock;
1507 /* locked */
NeilBrown32a76272005-06-21 17:17:14 -07001508 if (RESYNC(*bmc)) {
1509 *bmc &= ~RESYNC_MASK;
1510
1511 if (!NEEDED(*bmc) && aborted)
1512 *bmc |= NEEDED_MASK;
1513 else {
NeilBrown2585f3e2011-09-21 15:37:46 +10001514 if (*bmc <= 2) {
NeilBrown32a76272005-06-21 17:17:14 -07001515 set_page_attr(bitmap,
1516 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
NeilBrown5a537df2011-09-21 15:37:46 +10001517 BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001518 bitmap->allclean = 0;
1519 }
NeilBrown32a76272005-06-21 17:17:14 -07001520 }
1521 }
1522 unlock:
1523 spin_unlock_irqrestore(&bitmap->lock, flags);
1524}
NeilBrownac2f40b2010-06-01 19:37:31 +10001525EXPORT_SYMBOL(bitmap_end_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001526
1527void bitmap_close_sync(struct bitmap *bitmap)
1528{
1529 /* Sync has finished, and any bitmap chunks that weren't synced
1530 * properly have been aborted. It remains to us to clear the
1531 * RESYNC bit wherever it is still on
1532 */
1533 sector_t sector = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001534 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001535 if (!bitmap)
1536 return;
NeilBrown32a76272005-06-21 17:17:14 -07001537 while (sector < bitmap->mddev->resync_max_sectors) {
1538 bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001539 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001540 }
1541}
NeilBrownac2f40b2010-06-01 19:37:31 +10001542EXPORT_SYMBOL(bitmap_close_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001543
NeilBrownb47490c2008-02-06 01:39:50 -08001544void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1545{
1546 sector_t s = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001547 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001548
1549 if (!bitmap)
1550 return;
1551 if (sector == 0) {
1552 bitmap->last_end_sync = jiffies;
1553 return;
1554 }
1555 if (time_before(jiffies, (bitmap->last_end_sync
NeilBrown1b04be92009-12-14 12:49:53 +11001556 + bitmap->mddev->bitmap_info.daemon_sleep)))
NeilBrownb47490c2008-02-06 01:39:50 -08001557 return;
1558 wait_event(bitmap->mddev->recovery_wait,
1559 atomic_read(&bitmap->mddev->recovery_active) == 0);
1560
NeilBrown75d3da42011-01-14 09:14:34 +11001561 bitmap->mddev->curr_resync_completed = sector;
NeilBrown070dc6d2010-08-30 17:33:34 +10001562 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
NeilBrownb47490c2008-02-06 01:39:50 -08001563 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1564 s = 0;
1565 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1566 bitmap_end_sync(bitmap, s, &blocks, 0);
1567 s += blocks;
1568 }
1569 bitmap->last_end_sync = jiffies;
NeilBrownacb180b2009-04-14 16:28:34 +10001570 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
NeilBrownb47490c2008-02-06 01:39:50 -08001571}
NeilBrownac2f40b2010-06-01 19:37:31 +10001572EXPORT_SYMBOL(bitmap_cond_end_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08001573
NeilBrown6a079972005-09-09 16:23:44 -07001574static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001575{
1576 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001577 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001578 * be 0 at this point
1579 */
NeilBrown193f1c92005-08-04 12:53:33 -07001580
NeilBrown57dab0b2010-10-19 10:03:39 +11001581 sector_t secs;
NeilBrown193f1c92005-08-04 12:53:33 -07001582 bitmap_counter_t *bmc;
1583 spin_lock_irq(&bitmap->lock);
1584 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1585 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001586 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001587 return;
NeilBrown32a76272005-06-21 17:17:14 -07001588 }
NeilBrownac2f40b2010-06-01 19:37:31 +10001589 if (!*bmc) {
NeilBrown193f1c92005-08-04 12:53:33 -07001590 struct page *page;
NeilBrown915c4202011-12-23 10:17:51 +11001591 *bmc = 2 | (needed ? NEEDED_MASK : 0);
NeilBrown193f1c92005-08-04 12:53:33 -07001592 bitmap_count_page(bitmap, offset, 1);
1593 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
NeilBrown5a537df2011-09-21 15:37:46 +10001594 set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001595 bitmap->allclean = 0;
NeilBrown193f1c92005-08-04 12:53:33 -07001596 }
1597 spin_unlock_irq(&bitmap->lock);
NeilBrown32a76272005-06-21 17:17:14 -07001598}
1599
Paul Clements9b1d1da2006-10-03 01:15:49 -07001600/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1601void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1602{
1603 unsigned long chunk;
1604
1605 for (chunk = s; chunk <= e; chunk++) {
NeilBrowndb305e52009-05-07 12:49:06 +10001606 sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001607 bitmap_set_memory_bits(bitmap, sec, 1);
NeilBrown7c8f4242011-11-23 10:18:52 +11001608 spin_lock_irq(&bitmap->lock);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001609 bitmap_file_set_bit(bitmap, sec);
NeilBrown7c8f4242011-11-23 10:18:52 +11001610 spin_unlock_irq(&bitmap->lock);
NeilBrownffa23322009-12-14 12:49:56 +11001611 if (sec < bitmap->mddev->recovery_cp)
1612 /* We are asserting that the array is dirty,
1613 * so move the recovery_cp address back so
1614 * that it is obvious that it is dirty
1615 */
1616 bitmap->mddev->recovery_cp = sec;
Paul Clements9b1d1da2006-10-03 01:15:49 -07001617 }
1618}
1619
NeilBrown32a76272005-06-21 17:17:14 -07001620/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001621 * flush out any pending updates
1622 */
NeilBrownfd01b882011-10-11 16:47:53 +11001623void bitmap_flush(struct mddev *mddev)
NeilBrown6b8b3e82005-08-04 12:53:35 -07001624{
1625 struct bitmap *bitmap = mddev->bitmap;
NeilBrown42a04b52009-12-14 12:49:53 +11001626 long sleep;
NeilBrown6b8b3e82005-08-04 12:53:35 -07001627
1628 if (!bitmap) /* there was no bitmap */
1629 return;
1630
1631 /* run the daemon_work three time to ensure everything is flushed
1632 * that can be
1633 */
NeilBrown1b04be92009-12-14 12:49:53 +11001634 sleep = mddev->bitmap_info.daemon_sleep * 2;
NeilBrown42a04b52009-12-14 12:49:53 +11001635 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001636 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001637 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001638 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001639 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001640 bitmap_daemon_work(mddev);
NeilBrown6b8b3e82005-08-04 12:53:35 -07001641 bitmap_update_sb(bitmap);
1642}
1643
1644/*
NeilBrown32a76272005-06-21 17:17:14 -07001645 * free memory that was allocated
1646 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001647static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001648{
1649 unsigned long k, pages;
1650 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001651
1652 if (!bitmap) /* there was no bitmap */
1653 return;
1654
NeilBrown32a76272005-06-21 17:17:14 -07001655 /* release the bitmap file and kill the daemon */
1656 bitmap_file_put(bitmap);
1657
1658 bp = bitmap->bp;
1659 pages = bitmap->pages;
1660
1661 /* free all allocated memory */
1662
NeilBrown32a76272005-06-21 17:17:14 -07001663 if (bp) /* deallocate the page memory */
1664 for (k = 0; k < pages; k++)
1665 if (bp[k].map && !bp[k].hijacked)
1666 kfree(bp[k].map);
1667 kfree(bp);
1668 kfree(bitmap);
1669}
NeilBrownaa5cbd12009-12-14 12:49:46 +11001670
NeilBrownfd01b882011-10-11 16:47:53 +11001671void bitmap_destroy(struct mddev *mddev)
NeilBrown3178b0d2005-09-09 16:23:50 -07001672{
1673 struct bitmap *bitmap = mddev->bitmap;
1674
1675 if (!bitmap) /* there was no bitmap */
1676 return;
1677
NeilBrownc3d97142009-12-14 12:49:52 +11001678 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrown3178b0d2005-09-09 16:23:50 -07001679 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownc3d97142009-12-14 12:49:52 +11001680 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrownb15c2e52006-01-06 00:20:16 -08001681 if (mddev->thread)
1682 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001683
NeilBrownece5cff2009-12-14 12:49:56 +11001684 if (bitmap->sysfs_can_clear)
1685 sysfs_put(bitmap->sysfs_can_clear);
1686
NeilBrown3178b0d2005-09-09 16:23:50 -07001687 bitmap_free(bitmap);
1688}
NeilBrown32a76272005-06-21 17:17:14 -07001689
1690/*
1691 * initialize the bitmap structure
1692 * if this returns an error, bitmap_destroy must be called to do clean up
1693 */
NeilBrownfd01b882011-10-11 16:47:53 +11001694int bitmap_create(struct mddev *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001695{
1696 struct bitmap *bitmap;
NeilBrown1f593902009-04-20 11:50:24 +10001697 sector_t blocks = mddev->resync_max_sectors;
NeilBrown32a76272005-06-21 17:17:14 -07001698 unsigned long chunks;
1699 unsigned long pages;
NeilBrownc3d97142009-12-14 12:49:52 +11001700 struct file *file = mddev->bitmap_info.file;
NeilBrown32a76272005-06-21 17:17:14 -07001701 int err;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001702 struct sysfs_dirent *bm = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001703
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001704 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001705
NeilBrowne384e582010-06-01 19:37:34 +10001706 if (!file
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001707 && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001708 return 0;
1709
NeilBrownc3d97142009-12-14 12:49:52 +11001710 BUG_ON(file && mddev->bitmap_info.offset);
NeilBrowna654b9d82005-06-21 17:17:27 -07001711
NeilBrown9ffae0c2006-01-06 00:20:32 -08001712 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001713 if (!bitmap)
1714 return -ENOMEM;
1715
NeilBrown32a76272005-06-21 17:17:14 -07001716 spin_lock_init(&bitmap->lock);
NeilBrownce25c312006-06-26 00:27:49 -07001717 atomic_set(&bitmap->pending_writes, 0);
1718 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001719 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrowne5551902010-03-31 11:21:44 +11001720 init_waitqueue_head(&bitmap->behind_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001721
NeilBrown32a76272005-06-21 17:17:14 -07001722 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001723
NeilBrown5ff5aff2010-06-01 19:37:32 +10001724 if (mddev->kobj.sd)
1725 bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
NeilBrownece5cff2009-12-14 12:49:56 +11001726 if (bm) {
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07001727 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
NeilBrownece5cff2009-12-14 12:49:56 +11001728 sysfs_put(bm);
1729 } else
1730 bitmap->sysfs_can_clear = NULL;
1731
NeilBrown32a76272005-06-21 17:17:14 -07001732 bitmap->file = file;
NeilBrownce25c312006-06-26 00:27:49 -07001733 if (file) {
1734 get_file(file);
NeilBrownae8fa282009-10-16 15:56:01 +11001735 /* As future accesses to this file will use bmap,
1736 * and bypass the page cache, we must sync the file
1737 * first.
1738 */
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001739 vfs_fsync(file, 1);
NeilBrownce25c312006-06-26 00:27:49 -07001740 }
NeilBrown42a04b52009-12-14 12:49:53 +11001741 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
Jonathan Brassow9c810752011-06-08 17:59:30 -05001742 if (!mddev->bitmap_info.external) {
1743 /*
1744 * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
1745 * instructing us to create a new on-disk bitmap instance.
1746 */
1747 if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
1748 err = bitmap_new_disk_sb(bitmap);
1749 else
1750 err = bitmap_read_sb(bitmap);
1751 } else {
NeilBrownece5cff2009-12-14 12:49:56 +11001752 err = 0;
1753 if (mddev->bitmap_info.chunksize == 0 ||
1754 mddev->bitmap_info.daemon_sleep == 0)
1755 /* chunksize and time_base need to be
1756 * set first. */
1757 err = -EINVAL;
1758 }
NeilBrown32a76272005-06-21 17:17:14 -07001759 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001760 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001761
NeilBrown624ce4f2009-12-14 12:49:56 +11001762 bitmap->daemon_lastrun = jiffies;
NeilBrown42a04b52009-12-14 12:49:53 +11001763 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
NeilBrown32a76272005-06-21 17:17:14 -07001764
1765 /* now that chunksize and chunkshift are set, we can use these macros */
NeilBrownac2f40b2010-06-01 19:37:31 +10001766 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
NeilBrown1f593902009-04-20 11:50:24 +10001767 CHUNK_BLOCK_SHIFT(bitmap);
NeilBrownac2f40b2010-06-01 19:37:31 +10001768 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
NeilBrown32a76272005-06-21 17:17:14 -07001769
1770 BUG_ON(!pages);
1771
1772 bitmap->chunks = chunks;
1773 bitmap->pages = pages;
1774 bitmap->missing_pages = pages;
NeilBrown32a76272005-06-21 17:17:14 -07001775
NeilBrown9ffae0c2006-01-06 00:20:32 -08001776 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown29d32472011-10-11 16:49:56 +11001777
NeilBrown3178b0d2005-09-09 16:23:50 -07001778 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001779 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001780 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001781
NeilBrown69e51b42010-06-01 19:37:35 +10001782 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1783 pages, bmname(bitmap));
1784
1785 mddev->bitmap = bitmap;
1786
1787
1788 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
1789
1790 error:
1791 bitmap_free(bitmap);
1792 return err;
1793}
1794
NeilBrownfd01b882011-10-11 16:47:53 +11001795int bitmap_load(struct mddev *mddev)
NeilBrown69e51b42010-06-01 19:37:35 +10001796{
1797 int err = 0;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001798 sector_t start = 0;
NeilBrown69e51b42010-06-01 19:37:35 +10001799 sector_t sector = 0;
1800 struct bitmap *bitmap = mddev->bitmap;
1801
1802 if (!bitmap)
1803 goto out;
1804
1805 /* Clear out old bitmap info first: Either there is none, or we
1806 * are resuming after someone else has possibly changed things,
1807 * so we should forget old cached info.
1808 * All chunks should be clean, but some might need_sync.
1809 */
1810 while (sector < mddev->resync_max_sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001811 sector_t blocks;
NeilBrown69e51b42010-06-01 19:37:35 +10001812 bitmap_start_sync(bitmap, sector, &blocks, 0);
1813 sector += blocks;
1814 }
1815 bitmap_close_sync(bitmap);
1816
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001817 if (mddev->degraded == 0
1818 || bitmap->events_cleared == mddev->events)
1819 /* no need to keep dirty bits to optimise a
1820 * re-add of a missing device */
1821 start = mddev->recovery_cp;
NeilBrown69e51b42010-06-01 19:37:35 +10001822
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001823 err = bitmap_init_from_disk(bitmap, start);
1824
NeilBrown32a76272005-06-21 17:17:14 -07001825 if (err)
NeilBrown69e51b42010-06-01 19:37:35 +10001826 goto out;
NeilBrown3178b0d2005-09-09 16:23:50 -07001827
NeilBrown1b04be92009-12-14 12:49:53 +11001828 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
NeilBrown9cd30fd2009-12-14 12:49:54 +11001829 md_wakeup_thread(mddev->thread);
NeilBrownb15c2e52006-01-06 00:20:16 -08001830
NeilBrown4ad13662007-07-17 04:06:13 -07001831 bitmap_update_sb(bitmap);
1832
NeilBrown69e51b42010-06-01 19:37:35 +10001833 if (bitmap->flags & BITMAP_WRITE_ERROR)
1834 err = -EIO;
1835out:
NeilBrown3178b0d2005-09-09 16:23:50 -07001836 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001837}
NeilBrown69e51b42010-06-01 19:37:35 +10001838EXPORT_SYMBOL_GPL(bitmap_load);
NeilBrown32a76272005-06-21 17:17:14 -07001839
NeilBrown57148962012-03-19 12:46:40 +11001840void bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
1841{
1842 unsigned long chunk_kb;
1843 unsigned long flags;
1844
1845 if (!bitmap)
1846 return;
1847
1848 spin_lock_irqsave(&bitmap->lock, flags);
1849 chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
1850 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
1851 "%lu%s chunk",
1852 bitmap->pages - bitmap->missing_pages,
1853 bitmap->pages,
1854 (bitmap->pages - bitmap->missing_pages)
1855 << (PAGE_SHIFT - 10),
1856 chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
1857 chunk_kb ? "KB" : "B");
1858 if (bitmap->file) {
1859 seq_printf(seq, ", file: ");
1860 seq_path(seq, &bitmap->file->f_path, " \t\n");
1861 }
1862
1863 seq_printf(seq, "\n");
1864 spin_unlock_irqrestore(&bitmap->lock, flags);
1865}
1866
NeilBrown43a70502009-12-14 12:49:55 +11001867static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001868location_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11001869{
1870 ssize_t len;
NeilBrownac2f40b2010-06-01 19:37:31 +10001871 if (mddev->bitmap_info.file)
NeilBrown43a70502009-12-14 12:49:55 +11001872 len = sprintf(page, "file");
NeilBrownac2f40b2010-06-01 19:37:31 +10001873 else if (mddev->bitmap_info.offset)
NeilBrown43a70502009-12-14 12:49:55 +11001874 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001875 else
NeilBrown43a70502009-12-14 12:49:55 +11001876 len = sprintf(page, "none");
1877 len += sprintf(page+len, "\n");
1878 return len;
1879}
1880
1881static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001882location_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11001883{
1884
1885 if (mddev->pers) {
1886 if (!mddev->pers->quiesce)
1887 return -EBUSY;
1888 if (mddev->recovery || mddev->sync_thread)
1889 return -EBUSY;
1890 }
1891
1892 if (mddev->bitmap || mddev->bitmap_info.file ||
1893 mddev->bitmap_info.offset) {
1894 /* bitmap already configured. Only option is to clear it */
1895 if (strncmp(buf, "none", 4) != 0)
1896 return -EBUSY;
1897 if (mddev->pers) {
1898 mddev->pers->quiesce(mddev, 1);
1899 bitmap_destroy(mddev);
1900 mddev->pers->quiesce(mddev, 0);
1901 }
1902 mddev->bitmap_info.offset = 0;
1903 if (mddev->bitmap_info.file) {
1904 struct file *f = mddev->bitmap_info.file;
1905 mddev->bitmap_info.file = NULL;
1906 restore_bitmap_write_access(f);
1907 fput(f);
1908 }
1909 } else {
1910 /* No bitmap, OK to set a location */
1911 long long offset;
1912 if (strncmp(buf, "none", 4) == 0)
1913 /* nothing to be done */;
1914 else if (strncmp(buf, "file:", 5) == 0) {
1915 /* Not supported yet */
1916 return -EINVAL;
1917 } else {
1918 int rv;
1919 if (buf[0] == '+')
1920 rv = strict_strtoll(buf+1, 10, &offset);
1921 else
1922 rv = strict_strtoll(buf, 10, &offset);
1923 if (rv)
1924 return rv;
1925 if (offset == 0)
1926 return -EINVAL;
NeilBrownece5cff2009-12-14 12:49:56 +11001927 if (mddev->bitmap_info.external == 0 &&
1928 mddev->major_version == 0 &&
NeilBrown43a70502009-12-14 12:49:55 +11001929 offset != mddev->bitmap_info.default_offset)
1930 return -EINVAL;
1931 mddev->bitmap_info.offset = offset;
1932 if (mddev->pers) {
1933 mddev->pers->quiesce(mddev, 1);
1934 rv = bitmap_create(mddev);
NeilBrown4474ca42012-03-19 12:46:37 +11001935 if (!rv)
1936 rv = bitmap_load(mddev);
NeilBrown43a70502009-12-14 12:49:55 +11001937 if (rv) {
1938 bitmap_destroy(mddev);
1939 mddev->bitmap_info.offset = 0;
1940 }
1941 mddev->pers->quiesce(mddev, 0);
1942 if (rv)
1943 return rv;
1944 }
1945 }
1946 }
1947 if (!mddev->external) {
1948 /* Ensure new bitmap info is stored in
1949 * metadata promptly.
1950 */
1951 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1952 md_wakeup_thread(mddev->thread);
1953 }
1954 return len;
1955}
1956
1957static struct md_sysfs_entry bitmap_location =
1958__ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
1959
1960static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001961timeout_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11001962{
1963 ssize_t len;
1964 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
1965 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
NeilBrownac2f40b2010-06-01 19:37:31 +10001966
NeilBrown43a70502009-12-14 12:49:55 +11001967 len = sprintf(page, "%lu", secs);
1968 if (jifs)
1969 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
1970 len += sprintf(page+len, "\n");
1971 return len;
1972}
1973
1974static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001975timeout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11001976{
1977 /* timeout can be set at any time */
1978 unsigned long timeout;
1979 int rv = strict_strtoul_scaled(buf, &timeout, 4);
1980 if (rv)
1981 return rv;
1982
1983 /* just to make sure we don't overflow... */
1984 if (timeout >= LONG_MAX / HZ)
1985 return -EINVAL;
1986
1987 timeout = timeout * HZ / 10000;
1988
1989 if (timeout >= MAX_SCHEDULE_TIMEOUT)
1990 timeout = MAX_SCHEDULE_TIMEOUT-1;
1991 if (timeout < 1)
1992 timeout = 1;
1993 mddev->bitmap_info.daemon_sleep = timeout;
1994 if (mddev->thread) {
1995 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1996 * the bitmap is all clean and we don't need to
1997 * adjust the timeout right now
1998 */
1999 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
2000 mddev->thread->timeout = timeout;
2001 md_wakeup_thread(mddev->thread);
2002 }
2003 }
2004 return len;
2005}
2006
2007static struct md_sysfs_entry bitmap_timeout =
2008__ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
2009
2010static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002011backlog_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002012{
2013 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
2014}
2015
2016static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002017backlog_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002018{
2019 unsigned long backlog;
2020 int rv = strict_strtoul(buf, 10, &backlog);
2021 if (rv)
2022 return rv;
2023 if (backlog > COUNTER_MAX)
2024 return -EINVAL;
2025 mddev->bitmap_info.max_write_behind = backlog;
2026 return len;
2027}
2028
2029static struct md_sysfs_entry bitmap_backlog =
2030__ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
2031
2032static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002033chunksize_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002034{
2035 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
2036}
2037
2038static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002039chunksize_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002040{
2041 /* Can only be changed when no bitmap is active */
2042 int rv;
2043 unsigned long csize;
2044 if (mddev->bitmap)
2045 return -EBUSY;
2046 rv = strict_strtoul(buf, 10, &csize);
2047 if (rv)
2048 return rv;
2049 if (csize < 512 ||
2050 !is_power_of_2(csize))
2051 return -EINVAL;
2052 mddev->bitmap_info.chunksize = csize;
2053 return len;
2054}
2055
2056static struct md_sysfs_entry bitmap_chunksize =
2057__ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
2058
NeilBrownfd01b882011-10-11 16:47:53 +11002059static ssize_t metadata_show(struct mddev *mddev, char *page)
NeilBrownece5cff2009-12-14 12:49:56 +11002060{
2061 return sprintf(page, "%s\n", (mddev->bitmap_info.external
2062 ? "external" : "internal"));
2063}
2064
NeilBrownfd01b882011-10-11 16:47:53 +11002065static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownece5cff2009-12-14 12:49:56 +11002066{
2067 if (mddev->bitmap ||
2068 mddev->bitmap_info.file ||
2069 mddev->bitmap_info.offset)
2070 return -EBUSY;
2071 if (strncmp(buf, "external", 8) == 0)
2072 mddev->bitmap_info.external = 1;
2073 else if (strncmp(buf, "internal", 8) == 0)
2074 mddev->bitmap_info.external = 0;
2075 else
2076 return -EINVAL;
2077 return len;
2078}
2079
2080static struct md_sysfs_entry bitmap_metadata =
2081__ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2082
NeilBrownfd01b882011-10-11 16:47:53 +11002083static ssize_t can_clear_show(struct mddev *mddev, char *page)
NeilBrownece5cff2009-12-14 12:49:56 +11002084{
2085 int len;
2086 if (mddev->bitmap)
2087 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
2088 "false" : "true"));
2089 else
2090 len = sprintf(page, "\n");
2091 return len;
2092}
2093
NeilBrownfd01b882011-10-11 16:47:53 +11002094static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownece5cff2009-12-14 12:49:56 +11002095{
2096 if (mddev->bitmap == NULL)
2097 return -ENOENT;
2098 if (strncmp(buf, "false", 5) == 0)
2099 mddev->bitmap->need_sync = 1;
2100 else if (strncmp(buf, "true", 4) == 0) {
2101 if (mddev->degraded)
2102 return -EBUSY;
2103 mddev->bitmap->need_sync = 0;
2104 } else
2105 return -EINVAL;
2106 return len;
2107}
2108
2109static struct md_sysfs_entry bitmap_can_clear =
2110__ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
2111
Paul Clements696fcd52010-03-08 16:02:37 +11002112static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002113behind_writes_used_show(struct mddev *mddev, char *page)
Paul Clements696fcd52010-03-08 16:02:37 +11002114{
2115 if (mddev->bitmap == NULL)
2116 return sprintf(page, "0\n");
2117 return sprintf(page, "%lu\n",
2118 mddev->bitmap->behind_writes_used);
2119}
2120
2121static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002122behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
Paul Clements696fcd52010-03-08 16:02:37 +11002123{
2124 if (mddev->bitmap)
2125 mddev->bitmap->behind_writes_used = 0;
2126 return len;
2127}
2128
2129static struct md_sysfs_entry max_backlog_used =
2130__ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2131 behind_writes_used_show, behind_writes_used_reset);
2132
NeilBrown43a70502009-12-14 12:49:55 +11002133static struct attribute *md_bitmap_attrs[] = {
2134 &bitmap_location.attr,
2135 &bitmap_timeout.attr,
2136 &bitmap_backlog.attr,
2137 &bitmap_chunksize.attr,
NeilBrownece5cff2009-12-14 12:49:56 +11002138 &bitmap_metadata.attr,
2139 &bitmap_can_clear.attr,
Paul Clements696fcd52010-03-08 16:02:37 +11002140 &max_backlog_used.attr,
NeilBrown43a70502009-12-14 12:49:55 +11002141 NULL
2142};
2143struct attribute_group md_bitmap_group = {
2144 .name = "bitmap",
2145 .attrs = md_bitmap_attrs,
2146};
2147