blob: 324a198e8be74119dcc339f390e297bf373031fb [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/*
NeilBrown32a76272005-06-21 17:17:14 -070039 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
40 *
41 * 1) check to see if this page is allocated, if it's not then try to alloc
42 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
43 * page pointer directly as a counter
44 *
45 * if we find our page, we increment the page's refcount so that it stays
46 * allocated while we're using it
47 */
NeilBrownac2f40b2010-06-01 19:37:31 +100048static int bitmap_checkpage(struct bitmap *bitmap,
49 unsigned long page, int create)
NeilBrownee305ac2009-09-23 18:06:44 +100050__releases(bitmap->lock)
51__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -070052{
53 unsigned char *mappage;
54
55 if (page >= bitmap->pages) {
NeilBrown1187cf02009-03-31 14:27:02 +110056 /* This can happen if bitmap_start_sync goes beyond
57 * End-of-device while looking for a whole page.
58 * It is harmless.
59 */
NeilBrown32a76272005-06-21 17:17:14 -070060 return -EINVAL;
61 }
62
NeilBrown32a76272005-06-21 17:17:14 -070063 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
64 return 0;
65
66 if (bitmap->bp[page].map) /* page is already allocated, just return */
67 return 0;
68
69 if (!create)
70 return -ENOENT;
71
NeilBrown32a76272005-06-21 17:17:14 -070072 /* this page has not been allocated yet */
73
NeilBrownac2f40b2010-06-01 19:37:31 +100074 spin_unlock_irq(&bitmap->lock);
NeilBrown792a1d42012-03-19 12:46:41 +110075 mappage = kzalloc(PAGE_SIZE, GFP_NOIO);
NeilBrownac2f40b2010-06-01 19:37:31 +100076 spin_lock_irq(&bitmap->lock);
77
78 if (mappage == NULL) {
NeilBrown36a4e1f2011-10-07 14:23:17 +110079 pr_debug("%s: bitmap map page allocation failed, hijacking\n",
80 bmname(bitmap));
NeilBrown32a76272005-06-21 17:17:14 -070081 /* failed - set the hijacked flag so that we can use the
82 * pointer as a counter */
NeilBrown32a76272005-06-21 17:17:14 -070083 if (!bitmap->bp[page].map)
84 bitmap->bp[page].hijacked = 1;
NeilBrownac2f40b2010-06-01 19:37:31 +100085 } else if (bitmap->bp[page].map ||
86 bitmap->bp[page].hijacked) {
NeilBrown32a76272005-06-21 17:17:14 -070087 /* somebody beat us to getting the page */
NeilBrown792a1d42012-03-19 12:46:41 +110088 kfree(mappage);
NeilBrown32a76272005-06-21 17:17:14 -070089 return 0;
NeilBrownac2f40b2010-06-01 19:37:31 +100090 } else {
91
92 /* no page was in place and we have one, so install it */
93
94 bitmap->bp[page].map = mappage;
95 bitmap->missing_pages--;
NeilBrown32a76272005-06-21 17:17:14 -070096 }
NeilBrown32a76272005-06-21 17:17:14 -070097 return 0;
98}
99
NeilBrown32a76272005-06-21 17:17:14 -0700100/* if page is completely empty, put it back on the free list, or dealloc it */
101/* if page was hijacked, unmark the flag so it might get alloced next time */
102/* Note: lock should be held when calling this */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800103static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
NeilBrown32a76272005-06-21 17:17:14 -0700104{
105 char *ptr;
106
107 if (bitmap->bp[page].count) /* page is still busy */
108 return;
109
110 /* page is no longer in use, it can be released */
111
112 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
113 bitmap->bp[page].hijacked = 0;
114 bitmap->bp[page].map = NULL;
NeilBrownac2f40b2010-06-01 19:37:31 +1000115 } else {
116 /* normal case, free the page */
117 ptr = bitmap->bp[page].map;
118 bitmap->bp[page].map = NULL;
119 bitmap->missing_pages++;
NeilBrown792a1d42012-03-19 12:46:41 +1100120 kfree(ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700121 }
NeilBrown32a76272005-06-21 17:17:14 -0700122}
123
NeilBrown32a76272005-06-21 17:17:14 -0700124/*
125 * bitmap file handling - read and write the bitmap file and its superblock
126 */
127
NeilBrown32a76272005-06-21 17:17:14 -0700128/*
129 * basic page I/O operations
130 */
131
NeilBrowna654b9d82005-06-21 17:17:27 -0700132/* IO operations when bitmap is stored near all superblocks */
NeilBrown27581e52012-05-22 13:55:08 +1000133static int read_sb_page(struct mddev *mddev, loff_t offset,
134 struct page *page,
135 unsigned long index, int size)
NeilBrowna654b9d82005-06-21 17:17:27 -0700136{
137 /* choose a good rdev and read the page from there */
138
NeilBrown3cb03002011-10-11 16:45:26 +1100139 struct md_rdev *rdev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700140 sector_t target;
NeilBrowna654b9d82005-06-21 17:17:27 -0700141
NeilBrowndafb20f2012-03-19 12:46:39 +1100142 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800143 if (! test_bit(In_sync, &rdev->flags)
144 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700145 continue;
146
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100147 target = offset + index * (PAGE_SIZE/512);
NeilBrowna654b9d82005-06-21 17:17:27 -0700148
NeilBrown2b193362010-10-27 15:16:40 +1100149 if (sync_page_io(rdev, target,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400150 roundup(size, bdev_logical_block_size(rdev->bdev)),
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100151 page, READ, true)) {
NeilBrownab904d62005-09-09 16:23:52 -0700152 page->index = index;
NeilBrown27581e52012-05-22 13:55:08 +1000153 return 0;
NeilBrownab904d62005-09-09 16:23:52 -0700154 }
155 }
NeilBrown27581e52012-05-22 13:55:08 +1000156 return -EIO;
NeilBrowna654b9d82005-06-21 17:17:27 -0700157}
158
NeilBrownfd01b882011-10-11 16:47:53 +1100159static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev)
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000160{
161 /* Iterate the disks of an mddev, using rcu to protect access to the
162 * linked list, and raising the refcount of devices we return to ensure
163 * they don't disappear while in use.
164 * As devices are only added or removed when raid_disk is < 0 and
165 * nr_pending is 0 and In_sync is clear, the entries we return will
166 * still be in the same position on the list when we re-enter
167 * list_for_each_continue_rcu.
168 */
169 struct list_head *pos;
170 rcu_read_lock();
171 if (rdev == NULL)
172 /* start at the beginning */
173 pos = &mddev->disks;
174 else {
175 /* release the previous rdev and start from there. */
176 rdev_dec_pending(rdev, mddev);
177 pos = &rdev->same_set;
178 }
179 list_for_each_continue_rcu(pos, &mddev->disks) {
NeilBrown3cb03002011-10-11 16:45:26 +1100180 rdev = list_entry(pos, struct md_rdev, same_set);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000181 if (rdev->raid_disk >= 0 &&
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000182 !test_bit(Faulty, &rdev->flags)) {
183 /* this is a usable devices */
184 atomic_inc(&rdev->nr_pending);
185 rcu_read_unlock();
186 return rdev;
187 }
188 }
189 rcu_read_unlock();
190 return NULL;
191}
192
NeilBrownab6085c2007-05-23 13:58:10 -0700193static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrowna654b9d82005-06-21 17:17:27 -0700194{
NeilBrown3cb03002011-10-11 16:45:26 +1100195 struct md_rdev *rdev = NULL;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100196 struct block_device *bdev;
NeilBrownfd01b882011-10-11 16:47:53 +1100197 struct mddev *mddev = bitmap->mddev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700198
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000199 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000200 int size = PAGE_SIZE;
201 loff_t offset = mddev->bitmap_info.offset;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100202
203 bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
204
NeilBrownac2f40b2010-06-01 19:37:31 +1000205 if (page->index == bitmap->file_pages-1)
206 size = roundup(bitmap->last_page_size,
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100207 bdev_logical_block_size(bdev));
NeilBrownac2f40b2010-06-01 19:37:31 +1000208 /* Just make sure we aren't corrupting data or
209 * metadata
210 */
211 if (mddev->external) {
212 /* Bitmap could be anywhere. */
213 if (rdev->sb_start + offset + (page->index
214 * (PAGE_SIZE/512))
215 > rdev->data_offset
216 &&
217 rdev->sb_start + offset
218 < (rdev->data_offset + mddev->dev_sectors
219 + (PAGE_SIZE/512)))
220 goto bad_alignment;
221 } else if (offset < 0) {
222 /* DATA BITMAP METADATA */
223 if (offset
224 + (long)(page->index * (PAGE_SIZE/512))
225 + size/512 > 0)
226 /* bitmap runs in to metadata */
227 goto bad_alignment;
228 if (rdev->data_offset + mddev->dev_sectors
229 > rdev->sb_start + offset)
230 /* data runs in to bitmap */
231 goto bad_alignment;
232 } else if (rdev->sb_start < rdev->data_offset) {
233 /* METADATA BITMAP DATA */
234 if (rdev->sb_start
235 + offset
236 + page->index*(PAGE_SIZE/512) + size/512
237 > rdev->data_offset)
238 /* bitmap runs in to data */
239 goto bad_alignment;
240 } else {
241 /* DATA METADATA BITMAP - no problems */
242 }
243 md_super_write(mddev, rdev,
244 rdev->sb_start + offset
245 + page->index * (PAGE_SIZE/512),
246 size,
247 page);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000248 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700249
250 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800251 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700252 return 0;
NeilBrown4b809912008-07-21 17:05:25 +1000253
254 bad_alignment:
NeilBrown4b809912008-07-21 17:05:25 +1000255 return -EINVAL;
NeilBrowna654b9d82005-06-21 17:17:27 -0700256}
257
NeilBrown4ad13662007-07-17 04:06:13 -0700258static void bitmap_file_kick(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700259/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700260 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700261 */
NeilBrown4ad13662007-07-17 04:06:13 -0700262static void write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700263{
NeilBrownd785a062006-06-26 00:27:48 -0700264 struct buffer_head *bh;
NeilBrown32a76272005-06-21 17:17:14 -0700265
NeilBrownf0d76d72007-07-17 04:06:12 -0700266 if (bitmap->file == NULL) {
267 switch (write_sb_page(bitmap, page, wait)) {
268 case -EINVAL:
269 bitmap->flags |= BITMAP_WRITE_ERROR;
NeilBrownf0d76d72007-07-17 04:06:12 -0700270 }
NeilBrown4ad13662007-07-17 04:06:13 -0700271 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700272
NeilBrown4ad13662007-07-17 04:06:13 -0700273 bh = page_buffers(page);
NeilBrownc7084432006-01-06 00:20:45 -0800274
NeilBrown4ad13662007-07-17 04:06:13 -0700275 while (bh && bh->b_blocknr) {
276 atomic_inc(&bitmap->pending_writes);
277 set_buffer_locked(bh);
278 set_buffer_mapped(bh);
Jens Axboe721a9602011-03-09 11:56:30 +0100279 submit_bh(WRITE | REQ_SYNC, bh);
NeilBrown4ad13662007-07-17 04:06:13 -0700280 bh = bh->b_this_page;
281 }
NeilBrown32a76272005-06-21 17:17:14 -0700282
NeilBrownac2f40b2010-06-01 19:37:31 +1000283 if (wait)
NeilBrown4ad13662007-07-17 04:06:13 -0700284 wait_event(bitmap->write_wait,
285 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700286 }
NeilBrown4ad13662007-07-17 04:06:13 -0700287 if (bitmap->flags & BITMAP_WRITE_ERROR)
288 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700289}
290
NeilBrownd785a062006-06-26 00:27:48 -0700291static void end_bitmap_write(struct buffer_head *bh, int uptodate)
NeilBrown32a76272005-06-21 17:17:14 -0700292{
NeilBrownd785a062006-06-26 00:27:48 -0700293 struct bitmap *bitmap = bh->b_private;
294 unsigned long flags;
295
296 if (!uptodate) {
297 spin_lock_irqsave(&bitmap->lock, flags);
298 bitmap->flags |= BITMAP_WRITE_ERROR;
299 spin_unlock_irqrestore(&bitmap->lock, flags);
300 }
301 if (atomic_dec_and_test(&bitmap->pending_writes))
302 wake_up(&bitmap->write_wait);
303}
304
305/* copied from buffer.c */
306static void
307__clear_page_buffers(struct page *page)
308{
309 ClearPagePrivate(page);
310 set_page_private(page, 0);
311 page_cache_release(page);
312}
313static void free_buffers(struct page *page)
314{
NeilBrown27581e52012-05-22 13:55:08 +1000315 struct buffer_head *bh;
NeilBrownd785a062006-06-26 00:27:48 -0700316
NeilBrown27581e52012-05-22 13:55:08 +1000317 if (!PagePrivate(page))
318 return;
319
320 bh = page_buffers(page);
NeilBrownd785a062006-06-26 00:27:48 -0700321 while (bh) {
322 struct buffer_head *next = bh->b_this_page;
323 free_buffer_head(bh);
324 bh = next;
325 }
326 __clear_page_buffers(page);
327 put_page(page);
328}
329
330/* read a page from a file.
331 * We both read the page, and attach buffers to the page to record the
332 * address of each block (using bmap). These addresses will be used
333 * to write the block later, completely bypassing the filesystem.
334 * This usage is similar to how swap files are handled, and allows us
335 * to write to a file with no concerns of memory allocation failing.
336 */
NeilBrown27581e52012-05-22 13:55:08 +1000337static int read_page(struct file *file, unsigned long index,
338 struct bitmap *bitmap,
339 unsigned long count,
340 struct page *page)
NeilBrownd785a062006-06-26 00:27:48 -0700341{
NeilBrown27581e52012-05-22 13:55:08 +1000342 int ret = 0;
Josef Sipekc649bb92006-12-08 02:37:19 -0800343 struct inode *inode = file->f_path.dentry->d_inode;
NeilBrownd785a062006-06-26 00:27:48 -0700344 struct buffer_head *bh;
345 sector_t block;
NeilBrown32a76272005-06-21 17:17:14 -0700346
NeilBrown36a4e1f2011-10-07 14:23:17 +1100347 pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
348 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700349
NeilBrownd785a062006-06-26 00:27:48 -0700350 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
351 if (!bh) {
NeilBrown27581e52012-05-22 13:55:08 +1000352 ret = -ENOMEM;
NeilBrownd785a062006-06-26 00:27:48 -0700353 goto out;
354 }
355 attach_page_buffers(page, bh);
356 block = index << (PAGE_SHIFT - inode->i_blkbits);
357 while (bh) {
358 if (count == 0)
359 bh->b_blocknr = 0;
360 else {
361 bh->b_blocknr = bmap(inode, block);
362 if (bh->b_blocknr == 0) {
363 /* Cannot use this file! */
NeilBrown27581e52012-05-22 13:55:08 +1000364 ret = -EINVAL;
NeilBrownd785a062006-06-26 00:27:48 -0700365 goto out;
366 }
367 bh->b_bdev = inode->i_sb->s_bdev;
368 if (count < (1<<inode->i_blkbits))
369 count = 0;
370 else
371 count -= (1<<inode->i_blkbits);
NeilBrown32a76272005-06-21 17:17:14 -0700372
NeilBrownd785a062006-06-26 00:27:48 -0700373 bh->b_end_io = end_bitmap_write;
374 bh->b_private = bitmap;
NeilBrownce25c312006-06-26 00:27:49 -0700375 atomic_inc(&bitmap->pending_writes);
376 set_buffer_locked(bh);
377 set_buffer_mapped(bh);
378 submit_bh(READ, bh);
NeilBrownd785a062006-06-26 00:27:48 -0700379 }
380 block++;
381 bh = bh->b_this_page;
382 }
NeilBrownd785a062006-06-26 00:27:48 -0700383 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700384
385 wait_event(bitmap->write_wait,
386 atomic_read(&bitmap->pending_writes)==0);
NeilBrown27581e52012-05-22 13:55:08 +1000387 if (bitmap->flags & BITMAP_WRITE_ERROR)
388 ret = -EIO;
NeilBrown32a76272005-06-21 17:17:14 -0700389out:
NeilBrown27581e52012-05-22 13:55:08 +1000390 if (ret)
391 printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %d\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800392 (int)PAGE_SIZE,
393 (unsigned long long)index << PAGE_SHIFT,
NeilBrown27581e52012-05-22 13:55:08 +1000394 ret);
395 return ret;
NeilBrown32a76272005-06-21 17:17:14 -0700396}
397
398/*
399 * bitmap file superblock operations
400 */
401
402/* update the event counter and sync the superblock to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700403void bitmap_update_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700404{
405 bitmap_super_t *sb;
NeilBrown32a76272005-06-21 17:17:14 -0700406
407 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
NeilBrown4ad13662007-07-17 04:06:13 -0700408 return;
NeilBrownece5cff2009-12-14 12:49:56 +1100409 if (bitmap->mddev->bitmap_info.external)
410 return;
NeilBrown5a6c8242012-03-19 12:46:40 +1100411 if (!bitmap->sb_page) /* no superblock */
NeilBrown4ad13662007-07-17 04:06:13 -0700412 return;
Cong Wangb2f46e62011-11-28 13:25:44 +0800413 sb = kmap_atomic(bitmap->sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700414 sb->events = cpu_to_le64(bitmap->mddev->events);
NeilBrown8258c532011-05-11 14:26:30 +1000415 if (bitmap->mddev->events < bitmap->events_cleared)
Neil Browna0da84f2008-06-28 08:31:22 +1000416 /* rocking back to read-only */
417 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown8258c532011-05-11 14:26:30 +1000418 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
419 sb->state = cpu_to_le32(bitmap->flags);
NeilBrown43a70502009-12-14 12:49:55 +1100420 /* Just in case these have been changed via sysfs: */
421 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
422 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
Cong Wangb2f46e62011-11-28 13:25:44 +0800423 kunmap_atomic(sb);
NeilBrown4ad13662007-07-17 04:06:13 -0700424 write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700425}
426
427/* print out the bitmap file superblock */
428void bitmap_print_sb(struct bitmap *bitmap)
429{
430 bitmap_super_t *sb;
431
432 if (!bitmap || !bitmap->sb_page)
433 return;
Cong Wangb2f46e62011-11-28 13:25:44 +0800434 sb = kmap_atomic(bitmap->sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700435 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700436 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
437 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
438 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700439 *(__u32 *)(sb->uuid+0),
440 *(__u32 *)(sb->uuid+4),
441 *(__u32 *)(sb->uuid+8),
442 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700443 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700444 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700445 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700446 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700447 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
448 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
449 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
450 printk(KERN_DEBUG " sync size: %llu KB\n",
451 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700452 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
Cong Wangb2f46e62011-11-28 13:25:44 +0800453 kunmap_atomic(sb);
NeilBrown32a76272005-06-21 17:17:14 -0700454}
455
Jonathan Brassow9c810752011-06-08 17:59:30 -0500456/*
457 * bitmap_new_disk_sb
458 * @bitmap
459 *
460 * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
461 * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
462 * This function verifies 'bitmap_info' and populates the on-disk bitmap
463 * structure, which is to be written to disk.
464 *
465 * Returns: 0 on success, -Exxx on error
466 */
467static int bitmap_new_disk_sb(struct bitmap *bitmap)
468{
469 bitmap_super_t *sb;
470 unsigned long chunksize, daemon_sleep, write_behind;
471 int err = -EINVAL;
472
473 bitmap->sb_page = alloc_page(GFP_KERNEL);
474 if (IS_ERR(bitmap->sb_page)) {
475 err = PTR_ERR(bitmap->sb_page);
476 bitmap->sb_page = NULL;
477 return err;
478 }
479 bitmap->sb_page->index = 0;
480
Cong Wangb2f46e62011-11-28 13:25:44 +0800481 sb = kmap_atomic(bitmap->sb_page);
Jonathan Brassow9c810752011-06-08 17:59:30 -0500482
483 sb->magic = cpu_to_le32(BITMAP_MAGIC);
484 sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
485
486 chunksize = bitmap->mddev->bitmap_info.chunksize;
487 BUG_ON(!chunksize);
488 if (!is_power_of_2(chunksize)) {
Cong Wangb2f46e62011-11-28 13:25:44 +0800489 kunmap_atomic(sb);
Jonathan Brassow9c810752011-06-08 17:59:30 -0500490 printk(KERN_ERR "bitmap chunksize not a power of 2\n");
491 return -EINVAL;
492 }
493 sb->chunksize = cpu_to_le32(chunksize);
494
495 daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
496 if (!daemon_sleep ||
497 (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
498 printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n");
499 daemon_sleep = 5 * HZ;
500 }
501 sb->daemon_sleep = cpu_to_le32(daemon_sleep);
502 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
503
504 /*
505 * FIXME: write_behind for RAID1. If not specified, what
506 * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
507 */
508 write_behind = bitmap->mddev->bitmap_info.max_write_behind;
509 if (write_behind > COUNTER_MAX)
510 write_behind = COUNTER_MAX / 2;
511 sb->write_behind = cpu_to_le32(write_behind);
512 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
513
514 /* keep the array size field of the bitmap superblock up to date */
515 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
516
517 memcpy(sb->uuid, bitmap->mddev->uuid, 16);
518
519 bitmap->flags |= BITMAP_STALE;
520 sb->state |= cpu_to_le32(BITMAP_STALE);
521 bitmap->events_cleared = bitmap->mddev->events;
522 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
523
Cong Wangb2f46e62011-11-28 13:25:44 +0800524 kunmap_atomic(sb);
Jonathan Brassow9c810752011-06-08 17:59:30 -0500525
526 return 0;
527}
528
NeilBrown32a76272005-06-21 17:17:14 -0700529/* read the superblock from the bitmap file and initialize some bitmap fields */
530static int bitmap_read_sb(struct bitmap *bitmap)
531{
532 char *reason = NULL;
533 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700534 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700535 unsigned long long events;
536 int err = -EINVAL;
NeilBrown27581e52012-05-22 13:55:08 +1000537 struct page *sb_page;
NeilBrown32a76272005-06-21 17:17:14 -0700538
NeilBrownef99bf42012-05-22 13:55:08 +1000539 if (!bitmap->file && !bitmap->mddev->bitmap_info.offset) {
540 chunksize = 128 * 1024 * 1024;
541 daemon_sleep = 5 * HZ;
542 write_behind = 0;
543 bitmap->flags = BITMAP_STALE;
544 err = 0;
545 goto out_no_sb;
546 }
NeilBrown32a76272005-06-21 17:17:14 -0700547 /* page 0 is the superblock, read it... */
NeilBrown27581e52012-05-22 13:55:08 +1000548 sb_page = alloc_page(GFP_KERNEL);
549 if (!sb_page)
550 return -ENOMEM;
551 bitmap->sb_page = sb_page;
552
NeilBrownf49d5e62007-01-26 00:57:03 -0800553 if (bitmap->file) {
554 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
555 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
556
NeilBrown27581e52012-05-22 13:55:08 +1000557 err = read_page(bitmap->file, 0,
558 bitmap, bytes, sb_page);
NeilBrownf49d5e62007-01-26 00:57:03 -0800559 } else {
NeilBrown27581e52012-05-22 13:55:08 +1000560 err = read_sb_page(bitmap->mddev,
561 bitmap->mddev->bitmap_info.offset,
562 sb_page,
563 0, sizeof(bitmap_super_t));
NeilBrowna654b9d82005-06-21 17:17:27 -0700564 }
NeilBrown27581e52012-05-22 13:55:08 +1000565 if (err)
NeilBrown32a76272005-06-21 17:17:14 -0700566 return err;
NeilBrown32a76272005-06-21 17:17:14 -0700567
NeilBrown27581e52012-05-22 13:55:08 +1000568 sb = kmap_atomic(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700569
NeilBrown32a76272005-06-21 17:17:14 -0700570 chunksize = le32_to_cpu(sb->chunksize);
NeilBrown1b04be92009-12-14 12:49:53 +1100571 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
NeilBrown4b6d2872005-09-09 16:23:47 -0700572 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700573
574 /* verify that the bitmap-specific fields are valid */
575 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
576 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800577 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
578 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700579 reason = "unrecognized superblock version";
NeilBrown1187cf02009-03-31 14:27:02 +1100580 else if (chunksize < 512)
NeilBrown7dd5d342006-01-06 00:20:39 -0800581 reason = "bitmap chunksize too small";
Jonathan Brassowd7445402011-06-08 18:01:10 -0500582 else if (!is_power_of_2(chunksize))
NeilBrown32a76272005-06-21 17:17:14 -0700583 reason = "bitmap chunksize not a power of 2";
NeilBrown1b04be92009-12-14 12:49:53 +1100584 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
NeilBrown7dd5d342006-01-06 00:20:39 -0800585 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700586 else if (write_behind > COUNTER_MAX)
587 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700588 if (reason) {
589 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
590 bmname(bitmap), reason);
591 goto out;
592 }
593
594 /* keep the array size field of the bitmap superblock up to date */
595 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
596
NeilBrown278c1ca2012-03-19 12:46:40 +1100597 if (bitmap->mddev->persistent) {
598 /*
599 * We have a persistent array superblock, so compare the
600 * bitmap's UUID and event counter to the mddev's
601 */
602 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
603 printk(KERN_INFO
604 "%s: bitmap superblock UUID mismatch\n",
605 bmname(bitmap));
606 goto out;
607 }
608 events = le64_to_cpu(sb->events);
609 if (events < bitmap->mddev->events) {
610 printk(KERN_INFO
611 "%s: bitmap file is out of date (%llu < %llu) "
612 "-- forcing full recovery\n",
613 bmname(bitmap), events,
614 (unsigned long long) bitmap->mddev->events);
615 sb->state |= cpu_to_le32(BITMAP_STALE);
616 }
617 }
NeilBrown32a76272005-06-21 17:17:14 -0700618
NeilBrown32a76272005-06-21 17:17:14 -0700619 /* assign fields using values from superblock */
NeilBrown4f2e6392006-10-21 10:24:09 -0700620 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800621 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
622 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700623 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
624 err = 0;
625out:
Cong Wangb2f46e62011-11-28 13:25:44 +0800626 kunmap_atomic(sb);
NeilBrownef99bf42012-05-22 13:55:08 +1000627out_no_sb:
628 if (bitmap->flags & BITMAP_STALE)
629 bitmap->events_cleared = bitmap->mddev->events;
630 bitmap->mddev->bitmap_info.chunksize = chunksize;
631 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
632 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700633 if (err)
634 bitmap_print_sb(bitmap);
635 return err;
636}
637
638enum bitmap_mask_op {
639 MASK_SET,
640 MASK_UNSET
641};
642
NeilBrown4ad13662007-07-17 04:06:13 -0700643/* record the state of the bitmap in the superblock. Return the old value */
644static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
645 enum bitmap_mask_op op)
NeilBrown32a76272005-06-21 17:17:14 -0700646{
647 bitmap_super_t *sb;
NeilBrown4ad13662007-07-17 04:06:13 -0700648 int old;
NeilBrown32a76272005-06-21 17:17:14 -0700649
NeilBrown5a6c8242012-03-19 12:46:40 +1100650 if (!bitmap->sb_page) /* can't set the state */
NeilBrown4ad13662007-07-17 04:06:13 -0700651 return 0;
Cong Wangb2f46e62011-11-28 13:25:44 +0800652 sb = kmap_atomic(bitmap->sb_page);
NeilBrown4ad13662007-07-17 04:06:13 -0700653 old = le32_to_cpu(sb->state) & bits;
NeilBrown32a76272005-06-21 17:17:14 -0700654 switch (op) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000655 case MASK_SET:
656 sb->state |= cpu_to_le32(bits);
NeilBrown8258c532011-05-11 14:26:30 +1000657 bitmap->flags |= bits;
NeilBrownac2f40b2010-06-01 19:37:31 +1000658 break;
659 case MASK_UNSET:
660 sb->state &= cpu_to_le32(~bits);
NeilBrown8258c532011-05-11 14:26:30 +1000661 bitmap->flags &= ~bits;
NeilBrownac2f40b2010-06-01 19:37:31 +1000662 break;
663 default:
664 BUG();
NeilBrown32a76272005-06-21 17:17:14 -0700665 }
Cong Wangb2f46e62011-11-28 13:25:44 +0800666 kunmap_atomic(sb);
NeilBrown4ad13662007-07-17 04:06:13 -0700667 return old;
NeilBrown32a76272005-06-21 17:17:14 -0700668}
669
670/*
671 * general bitmap file operations
672 */
673
NeilBrownece5cff2009-12-14 12:49:56 +1100674/*
675 * on-disk bitmap:
676 *
677 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
678 * file a page at a time. There's a superblock at the start of the file.
679 */
NeilBrown32a76272005-06-21 17:17:14 -0700680/* calculate the index of the page that contains this bit */
NeilBrownece5cff2009-12-14 12:49:56 +1100681static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700682{
NeilBrownece5cff2009-12-14 12:49:56 +1100683 if (!bitmap->mddev->bitmap_info.external)
684 chunk += sizeof(bitmap_super_t) << 3;
685 return chunk >> PAGE_BIT_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700686}
687
688/* calculate the (bit) offset of this bit within a page */
NeilBrownece5cff2009-12-14 12:49:56 +1100689static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700690{
NeilBrownece5cff2009-12-14 12:49:56 +1100691 if (!bitmap->mddev->bitmap_info.external)
692 chunk += sizeof(bitmap_super_t) << 3;
693 return chunk & (PAGE_BITS - 1);
NeilBrown32a76272005-06-21 17:17:14 -0700694}
695
696/*
697 * return a pointer to the page in the filemap that contains the given bit
698 *
699 * this lookup is complicated by the fact that the bitmap sb might be exactly
700 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
701 * 0 or page 1
702 */
703static inline struct page *filemap_get_page(struct bitmap *bitmap,
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000704 unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700705{
NeilBrownac2f40b2010-06-01 19:37:31 +1000706 if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
707 return NULL;
NeilBrownece5cff2009-12-14 12:49:56 +1100708 return bitmap->filemap[file_page_index(bitmap, chunk)
709 - file_page_index(bitmap, 0)];
NeilBrown32a76272005-06-21 17:17:14 -0700710}
711
NeilBrown32a76272005-06-21 17:17:14 -0700712static void bitmap_file_unmap(struct bitmap *bitmap)
713{
714 struct page **map, *sb_page;
715 unsigned long *attr;
716 int pages;
717 unsigned long flags;
718
719 spin_lock_irqsave(&bitmap->lock, flags);
720 map = bitmap->filemap;
721 bitmap->filemap = NULL;
722 attr = bitmap->filemap_attr;
723 bitmap->filemap_attr = NULL;
724 pages = bitmap->file_pages;
725 bitmap->file_pages = 0;
726 sb_page = bitmap->sb_page;
727 bitmap->sb_page = NULL;
728 spin_unlock_irqrestore(&bitmap->lock, flags);
729
730 while (pages--)
NeilBrownece5cff2009-12-14 12:49:56 +1100731 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700732 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700733 kfree(map);
734 kfree(attr);
735
NeilBrownd785a062006-06-26 00:27:48 -0700736 if (sb_page)
737 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700738}
739
740static void bitmap_file_put(struct bitmap *bitmap)
741{
742 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700743 unsigned long flags;
744
745 spin_lock_irqsave(&bitmap->lock, flags);
746 file = bitmap->file;
747 bitmap->file = NULL;
748 spin_unlock_irqrestore(&bitmap->lock, flags);
749
NeilBrownd785a062006-06-26 00:27:48 -0700750 if (file)
751 wait_event(bitmap->write_wait,
752 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700753 bitmap_file_unmap(bitmap);
754
NeilBrownd785a062006-06-26 00:27:48 -0700755 if (file) {
Josef Sipekc649bb92006-12-08 02:37:19 -0800756 struct inode *inode = file->f_path.dentry->d_inode;
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800757 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700758 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700759 }
NeilBrown32a76272005-06-21 17:17:14 -0700760}
761
NeilBrown32a76272005-06-21 17:17:14 -0700762/*
763 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
764 * then it is no longer reliable, so we stop using it and we mark the file
765 * as failed in the superblock
766 */
767static void bitmap_file_kick(struct bitmap *bitmap)
768{
769 char *path, *ptr = NULL;
770
NeilBrown4ad13662007-07-17 04:06:13 -0700771 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
772 bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700773
NeilBrown4ad13662007-07-17 04:06:13 -0700774 if (bitmap->file) {
775 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
776 if (path)
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700777 ptr = d_path(&bitmap->file->f_path, path,
778 PAGE_SIZE);
779
NeilBrown4ad13662007-07-17 04:06:13 -0700780 printk(KERN_ALERT
781 "%s: kicking failed bitmap file %s from array!\n",
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700782 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700783
NeilBrown4ad13662007-07-17 04:06:13 -0700784 kfree(path);
785 } else
786 printk(KERN_ALERT
787 "%s: disabling internal bitmap due to errors\n",
788 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700789 }
NeilBrown32a76272005-06-21 17:17:14 -0700790
791 bitmap_file_put(bitmap);
792
793 return;
794}
795
796enum bitmap_page_attr {
NeilBrownac2f40b2010-06-01 19:37:31 +1000797 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
NeilBrown5a537df2011-09-21 15:37:46 +1000798 BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
799 * i.e. counter is 1 or 2. */
NeilBrownac2f40b2010-06-01 19:37:31 +1000800 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
NeilBrown32a76272005-06-21 17:17:14 -0700801};
802
803static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
804 enum bitmap_page_attr attr)
805{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000806 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700807}
808
809static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
810 enum bitmap_page_attr attr)
811{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000812 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700813}
814
NeilBrownec7a3192006-06-26 00:27:45 -0700815static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
816 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700817{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000818 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700819}
820
821/*
822 * bitmap_file_set_bit -- called before performing a write to the md device
823 * to set (and eventually sync) a particular bit in the bitmap file
824 *
825 * we set the bit immediately, then we record the page number so that
826 * when an unplug occurs, we can flush the dirty pages out to disk
827 */
828static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
829{
830 unsigned long bit;
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000831 struct page *page;
NeilBrown32a76272005-06-21 17:17:14 -0700832 void *kaddr;
NeilBrown61a0d802012-03-19 12:46:41 +1100833 unsigned long chunk = block >> bitmap->chunkshift;
NeilBrown32a76272005-06-21 17:17:14 -0700834
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000835 page = filemap_get_page(bitmap, chunk);
836 if (!page)
837 return;
838 bit = file_page_offset(bitmap, chunk);
NeilBrown32a76272005-06-21 17:17:14 -0700839
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000840 /* set the bit */
Cong Wangb2f46e62011-11-28 13:25:44 +0800841 kaddr = kmap_atomic(page);
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000842 if (bitmap->flags & BITMAP_HOSTENDIAN)
843 set_bit(bit, kaddr);
844 else
845 __set_bit_le(bit, kaddr);
Cong Wangb2f46e62011-11-28 13:25:44 +0800846 kunmap_atomic(kaddr);
NeilBrown36a4e1f2011-10-07 14:23:17 +1100847 pr_debug("set file bit %lu page %lu\n", bit, page->index);
NeilBrown32a76272005-06-21 17:17:14 -0700848 /* record page number so it gets flushed to disk when unplug occurs */
849 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
NeilBrown32a76272005-06-21 17:17:14 -0700850}
851
NeilBrownef99bf42012-05-22 13:55:08 +1000852static void bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block)
853{
854 unsigned long bit;
855 struct page *page;
856 void *paddr;
857 unsigned long chunk = block >> bitmap->chunkshift;
858
859 page = filemap_get_page(bitmap, chunk);
860 if (!page)
861 return;
862 bit = file_page_offset(bitmap, chunk);
863 paddr = kmap_atomic(page);
864 if (bitmap->flags & BITMAP_HOSTENDIAN)
865 clear_bit(bit, paddr);
866 else
867 __clear_bit_le(bit, paddr);
868 kunmap_atomic(paddr);
869 if (!test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE)) {
870 set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
871 bitmap->allclean = 0;
872 }
873}
874
NeilBrown32a76272005-06-21 17:17:14 -0700875/* this gets called when the md device is ready to unplug its underlying
876 * (slave) device queues -- before we let any writes go down, we need to
877 * sync the dirty pages of the bitmap file to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700878void bitmap_unplug(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700879{
NeilBrownec7a3192006-06-26 00:27:45 -0700880 unsigned long i, flags;
881 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700882 struct page *page;
883 int wait = 0;
884
NeilBrownef99bf42012-05-22 13:55:08 +1000885 if (!bitmap || !bitmap->filemap)
NeilBrown4ad13662007-07-17 04:06:13 -0700886 return;
NeilBrown32a76272005-06-21 17:17:14 -0700887
888 /* look at each page to see if there are any set bits that need to be
889 * flushed out to disk */
890 for (i = 0; i < bitmap->file_pages; i++) {
891 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700892 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700893 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700894 return;
NeilBrown32a76272005-06-21 17:17:14 -0700895 }
896 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700897 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
898 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700899 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
900 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownbf07bb72012-05-22 13:55:06 +1000901 if (dirty || need_write)
902 clear_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrownec7a3192006-06-26 00:27:45 -0700903 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700904 wait = 1;
905 spin_unlock_irqrestore(&bitmap->lock, flags);
906
NeilBrownac2f40b2010-06-01 19:37:31 +1000907 if (dirty || need_write)
NeilBrown4ad13662007-07-17 04:06:13 -0700908 write_page(bitmap, page, 0);
NeilBrown32a76272005-06-21 17:17:14 -0700909 }
910 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700911 if (bitmap->file)
NeilBrownd785a062006-06-26 00:27:48 -0700912 wait_event(bitmap->write_wait,
913 atomic_read(&bitmap->pending_writes)==0);
NeilBrown0b79ccf2006-06-26 00:27:44 -0700914 else
NeilBrowna9701a32005-11-08 21:39:34 -0800915 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700916 }
NeilBrownd785a062006-06-26 00:27:48 -0700917 if (bitmap->flags & BITMAP_WRITE_ERROR)
918 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700919}
NeilBrownac2f40b2010-06-01 19:37:31 +1000920EXPORT_SYMBOL(bitmap_unplug);
NeilBrown32a76272005-06-21 17:17:14 -0700921
NeilBrown6a079972005-09-09 16:23:44 -0700922static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700923/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
924 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
925 * memory mapping of the bitmap file
926 * Special cases:
927 * if there's no bitmap file, or if the bitmap file had been
928 * previously kicked from the array, we mark all the bits as
929 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700930 *
931 * We ignore all bits for sectors that end earlier than 'start'.
932 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700933 */
NeilBrown6a079972005-09-09 16:23:44 -0700934static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700935{
936 unsigned long i, chunks, index, oldindex, bit;
NeilBrown27581e52012-05-22 13:55:08 +1000937 int pnum;
938 struct page *page = NULL;
NeilBrown32a76272005-06-21 17:17:14 -0700939 unsigned long num_pages, bit_cnt = 0;
940 struct file *file;
NeilBrownd785a062006-06-26 00:27:48 -0700941 unsigned long bytes, offset;
NeilBrown32a76272005-06-21 17:17:14 -0700942 int outofdate;
943 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800944 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700945
946 chunks = bitmap->chunks;
947 file = bitmap->file;
948
NeilBrownef99bf42012-05-22 13:55:08 +1000949 if (!file && !bitmap->mddev->bitmap_info.offset) {
950 /* No permanent bitmap - fill with '1s'. */
951 bitmap->filemap = NULL;
952 bitmap->file_pages = 0;
953 for (i = 0; i < chunks ; i++) {
954 /* if the disk bit is set, set the memory bit */
955 int needed = ((sector_t)(i+1) << (bitmap->chunkshift)
956 >= start);
957 bitmap_set_memory_bits(bitmap,
958 (sector_t)i << bitmap->chunkshift,
959 needed);
960 }
961 return 0;
962 }
NeilBrown32a76272005-06-21 17:17:14 -0700963
NeilBrown32a76272005-06-21 17:17:14 -0700964 outofdate = bitmap->flags & BITMAP_STALE;
NeilBrown32a76272005-06-21 17:17:14 -0700965 if (outofdate)
966 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
967 "recovery\n", bmname(bitmap));
968
NeilBrowne384e582010-06-01 19:37:34 +1000969 bytes = DIV_ROUND_UP(bitmap->chunks, 8);
NeilBrownece5cff2009-12-14 12:49:56 +1100970 if (!bitmap->mddev->bitmap_info.external)
971 bytes += sizeof(bitmap_super_t);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700972
NeilBrowne384e582010-06-01 19:37:34 +1000973 num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700974
NeilBrownece5cff2009-12-14 12:49:56 +1100975 if (file && i_size_read(file->f_mapping->host) < bytes) {
NeilBrown32a76272005-06-21 17:17:14 -0700976 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
977 bmname(bitmap),
978 (unsigned long) i_size_read(file->f_mapping->host),
NeilBrownece5cff2009-12-14 12:49:56 +1100979 bytes);
NeilBrown4ad13662007-07-17 04:06:13 -0700980 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700981 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700982
983 ret = -ENOMEM;
984
NeilBrown32a76272005-06-21 17:17:14 -0700985 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700986 if (!bitmap->filemap)
NeilBrown4ad13662007-07-17 04:06:13 -0700987 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700988
NeilBrown27581e52012-05-22 13:55:08 +1000989 pnum = 0;
990 offset = 0;
991 if (bitmap->sb_page) {
992 bitmap->filemap[0] = bitmap->sb_page;
993 pnum = 1;
994 offset = sizeof(bitmap_super_t);
995 }
996 for ( ; pnum < num_pages; pnum++) {
997 bitmap->filemap[pnum] = alloc_page(GFP_KERNEL);
998 if (!bitmap->filemap[pnum]) {
999 bitmap->file_pages = pnum;
1000 goto err;
1001 }
1002 }
1003 bitmap->file_pages = pnum;
1004
NeilBrowne16b68b2006-06-26 00:27:45 -07001005 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
1006 bitmap->filemap_attr = kzalloc(
NeilBrownac2f40b2010-06-01 19:37:31 +10001007 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
NeilBrowne16b68b2006-06-26 00:27:45 -07001008 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -07001009 if (!bitmap->filemap_attr)
NeilBrown4ad13662007-07-17 04:06:13 -07001010 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001011
NeilBrown32a76272005-06-21 17:17:14 -07001012 oldindex = ~0L;
1013
1014 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -08001015 int b;
NeilBrownece5cff2009-12-14 12:49:56 +11001016 index = file_page_index(bitmap, i);
1017 bit = file_page_offset(bitmap, i);
NeilBrown32a76272005-06-21 17:17:14 -07001018 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -07001019 int count;
NeilBrown32a76272005-06-21 17:17:14 -07001020 /* unmap the old page, we're done with it */
NeilBrownd785a062006-06-26 00:27:48 -07001021 if (index == num_pages-1)
NeilBrownece5cff2009-12-14 12:49:56 +11001022 count = bytes - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -07001023 else
1024 count = PAGE_SIZE;
NeilBrown27581e52012-05-22 13:55:08 +10001025 page = bitmap->filemap[index];
1026 if (file)
1027 ret = read_page(file, index, bitmap,
1028 count, page);
1029 else
1030 ret = read_sb_page(
1031 bitmap->mddev,
1032 bitmap->mddev->bitmap_info.offset,
1033 page,
1034 index, count);
1035
1036 if (ret)
NeilBrown4ad13662007-07-17 04:06:13 -07001037 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -07001038
NeilBrown32a76272005-06-21 17:17:14 -07001039 oldindex = index;
NeilBrown32a76272005-06-21 17:17:14 -07001040
NeilBrownb74fd282009-05-07 12:47:19 +10001041 bitmap->last_page_size = count;
1042
NeilBrown32a76272005-06-21 17:17:14 -07001043 if (outofdate) {
1044 /*
1045 * if bitmap is out of date, dirty the
NeilBrownac2f40b2010-06-01 19:37:31 +10001046 * whole page and write it out
NeilBrown32a76272005-06-21 17:17:14 -07001047 */
Cong Wangb2f46e62011-11-28 13:25:44 +08001048 paddr = kmap_atomic(page);
NeilBrownea03aff2006-01-06 00:20:34 -08001049 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -07001050 PAGE_SIZE - offset);
Cong Wangb2f46e62011-11-28 13:25:44 +08001051 kunmap_atomic(paddr);
NeilBrown4ad13662007-07-17 04:06:13 -07001052 write_page(bitmap, page, 1);
1053
1054 ret = -EIO;
NeilBrownb74fd282009-05-07 12:47:19 +10001055 if (bitmap->flags & BITMAP_WRITE_ERROR)
NeilBrown4ad13662007-07-17 04:06:13 -07001056 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001057 }
NeilBrown32a76272005-06-21 17:17:14 -07001058 }
Cong Wangb2f46e62011-11-28 13:25:44 +08001059 paddr = kmap_atomic(page);
NeilBrownbd926c62005-11-08 21:39:32 -08001060 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001061 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001062 else
Akinobu Mita6b33aff2011-03-23 16:42:13 -07001063 b = test_bit_le(bit, paddr);
Cong Wangb2f46e62011-11-28 13:25:44 +08001064 kunmap_atomic(paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001065 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -07001066 /* if the disk bit is set, set the memory bit */
NeilBrown61a0d802012-03-19 12:46:41 +11001067 int needed = ((sector_t)(i+1) << bitmap->chunkshift
NeilBrowndb305e52009-05-07 12:49:06 +10001068 >= start);
1069 bitmap_set_memory_bits(bitmap,
NeilBrown61a0d802012-03-19 12:46:41 +11001070 (sector_t)i << bitmap->chunkshift,
NeilBrowndb305e52009-05-07 12:49:06 +10001071 needed);
NeilBrown32a76272005-06-21 17:17:14 -07001072 bit_cnt++;
1073 }
NeilBrown27581e52012-05-22 13:55:08 +10001074 offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001075 }
1076
NeilBrown32a76272005-06-21 17:17:14 -07001077 printk(KERN_INFO "%s: bitmap initialized from disk: "
Jonathan Brassow9c810752011-06-08 17:59:30 -05001078 "read %lu/%lu pages, set %lu of %lu bits\n",
1079 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, chunks);
NeilBrown32a76272005-06-21 17:17:14 -07001080
NeilBrown4ad13662007-07-17 04:06:13 -07001081 return 0;
1082
1083 err:
1084 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1085 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001086 return ret;
1087}
1088
NeilBrowna654b9d82005-06-21 17:17:27 -07001089void bitmap_write_all(struct bitmap *bitmap)
1090{
1091 /* We don't actually write all bitmap blocks here,
1092 * just flag them as needing to be written
1093 */
NeilBrownec7a3192006-06-26 00:27:45 -07001094 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001095
NeilBrownef99bf42012-05-22 13:55:08 +10001096 if (!bitmap || !bitmap->filemap)
1097 return;
1098 if (bitmap->file)
1099 /* Only one copy, so nothing needed */
1100 return;
1101
NeilBrown7c8f4242011-11-23 10:18:52 +11001102 spin_lock_irq(&bitmap->lock);
NeilBrownac2f40b2010-06-01 19:37:31 +10001103 for (i = 0; i < bitmap->file_pages; i++)
NeilBrownec7a3192006-06-26 00:27:45 -07001104 set_page_attr(bitmap, bitmap->filemap[i],
1105 BITMAP_PAGE_NEEDWRITE);
NeilBrown2585f3e2011-09-21 15:37:46 +10001106 bitmap->allclean = 0;
NeilBrown7c8f4242011-11-23 10:18:52 +11001107 spin_unlock_irq(&bitmap->lock);
NeilBrowna654b9d82005-06-21 17:17:27 -07001108}
1109
NeilBrown32a76272005-06-21 17:17:14 -07001110static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1111{
NeilBrown61a0d802012-03-19 12:46:41 +11001112 sector_t chunk = offset >> bitmap->chunkshift;
NeilBrown32a76272005-06-21 17:17:14 -07001113 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1114 bitmap->bp[page].count += inc;
NeilBrown32a76272005-06-21 17:17:14 -07001115 bitmap_checkfree(bitmap, page);
1116}
NeilBrownbf07bb72012-05-22 13:55:06 +10001117
1118static void bitmap_set_pending(struct bitmap *bitmap, sector_t offset)
1119{
1120 sector_t chunk = offset >> bitmap->chunkshift;
1121 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1122 struct bitmap_page *bp = &bitmap->bp[page];
1123
1124 if (!bp->pending)
1125 bp->pending = 1;
1126}
1127
NeilBrown32a76272005-06-21 17:17:14 -07001128static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001129 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001130 int create);
1131
1132/*
1133 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1134 * out to disk
1135 */
1136
NeilBrownfd01b882011-10-11 16:47:53 +11001137void bitmap_daemon_work(struct mddev *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001138{
NeilBrownaa5cbd12009-12-14 12:49:46 +11001139 struct bitmap *bitmap;
NeilBrownaa3163f2005-06-21 17:17:22 -07001140 unsigned long j;
NeilBrownbf07bb72012-05-22 13:55:06 +10001141 unsigned long nextpage;
NeilBrown32a76272005-06-21 17:17:14 -07001142 unsigned long flags;
NeilBrown57dab0b2010-10-19 10:03:39 +11001143 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001144
NeilBrownaa5cbd12009-12-14 12:49:46 +11001145 /* Use a mutex to guard daemon_work against
1146 * bitmap_destroy.
1147 */
NeilBrownc3d97142009-12-14 12:49:52 +11001148 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrownaa5cbd12009-12-14 12:49:46 +11001149 bitmap = mddev->bitmap;
1150 if (bitmap == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001151 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown4ad13662007-07-17 04:06:13 -07001152 return;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001153 }
NeilBrown42a04b52009-12-14 12:49:53 +11001154 if (time_before(jiffies, bitmap->daemon_lastrun
NeilBrown2e61ebb2011-12-23 10:17:50 +11001155 + mddev->bitmap_info.daemon_sleep))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001156 goto done;
1157
NeilBrown32a76272005-06-21 17:17:14 -07001158 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001159 if (bitmap->allclean) {
NeilBrown2e61ebb2011-12-23 10:17:50 +11001160 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001161 goto done;
NeilBrown8311c292008-03-04 14:29:30 -08001162 }
1163 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001164
NeilBrownbf07bb72012-05-22 13:55:06 +10001165 /* Any file-page which is PENDING now needs to be written.
1166 * So set NEEDWRITE now, then after we make any last-minute changes
1167 * we will write it.
1168 */
NeilBrownbe512692009-05-26 09:41:17 +10001169 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownbf07bb72012-05-22 13:55:06 +10001170 for (j = 0; j < bitmap->file_pages; j++)
1171 if (test_page_attr(bitmap, bitmap->filemap[j],
1172 BITMAP_PAGE_PENDING)) {
1173 set_page_attr(bitmap, bitmap->filemap[j],
1174 BITMAP_PAGE_NEEDWRITE);
1175 clear_page_attr(bitmap, bitmap->filemap[j],
1176 BITMAP_PAGE_PENDING);
1177 }
1178
1179 if (bitmap->need_sync &&
1180 mddev->bitmap_info.external == 0) {
1181 /* Arrange for superblock update as well as
1182 * other changes */
1183 bitmap_super_t *sb;
1184 bitmap->need_sync = 0;
NeilBrownef99bf42012-05-22 13:55:08 +10001185 if (bitmap->filemap) {
1186 sb = kmap_atomic(bitmap->sb_page);
1187 sb->events_cleared =
1188 cpu_to_le64(bitmap->events_cleared);
1189 kunmap_atomic(sb);
1190 set_page_attr(bitmap, bitmap->sb_page,
1191 BITMAP_PAGE_NEEDWRITE);
1192 }
NeilBrownbf07bb72012-05-22 13:55:06 +10001193 }
1194 /* Now look at the bitmap counters and if any are '2' or '1',
1195 * decrement and handle accordingly.
1196 */
1197 nextpage = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001198 for (j = 0; j < bitmap->chunks; j++) {
1199 bitmap_counter_t *bmc;
NeilBrownef99bf42012-05-22 13:55:08 +10001200 sector_t block = (sector_t)j << bitmap->chunkshift;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001201
NeilBrownbf07bb72012-05-22 13:55:06 +10001202 if (j == nextpage) {
1203 nextpage += PAGE_COUNTER_RATIO;
1204 if (!bitmap->bp[j >> PAGE_COUNTER_SHIFT].pending) {
1205 j |= PAGE_COUNTER_MASK;
NeilBrownaa3163f2005-06-21 17:17:22 -07001206 continue;
1207 }
NeilBrownbf07bb72012-05-22 13:55:06 +10001208 bitmap->bp[j >> PAGE_COUNTER_SHIFT].pending = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001209 }
NeilBrowndb305e52009-05-07 12:49:06 +10001210 bmc = bitmap_get_counter(bitmap,
NeilBrownef99bf42012-05-22 13:55:08 +10001211 block,
NeilBrowndb305e52009-05-07 12:49:06 +10001212 &blocks, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001213
NeilBrownbf07bb72012-05-22 13:55:06 +10001214 if (!bmc) {
1215 j |= PAGE_COUNTER_MASK;
1216 continue;
1217 }
1218 if (*bmc == 1 && !bitmap->need_sync) {
1219 /* We can clear the bit */
NeilBrownbf07bb72012-05-22 13:55:06 +10001220 *bmc = 0;
NeilBrownef99bf42012-05-22 13:55:08 +10001221 bitmap_count_page(bitmap, block, -1);
1222 bitmap_file_clear_bit(bitmap, block);
NeilBrownbf07bb72012-05-22 13:55:06 +10001223 } else if (*bmc && *bmc <= 2) {
1224 *bmc = 1;
NeilBrownef99bf42012-05-22 13:55:08 +10001225 bitmap_set_pending(bitmap, block);
NeilBrown2585f3e2011-09-21 15:37:46 +10001226 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001227 }
NeilBrown32a76272005-06-21 17:17:14 -07001228 }
1229
NeilBrownbf07bb72012-05-22 13:55:06 +10001230 /* Now start writeout on any page in NEEDWRITE that isn't DIRTY.
1231 * DIRTY pages need to be written by bitmap_unplug so it can wait
1232 * for them.
1233 * If we find any DIRTY page we stop there and let bitmap_unplug
1234 * handle all the rest. This is important in the case where
1235 * the first blocking holds the superblock and it has been updated.
1236 * We mustn't write any other blocks before the superblock.
1237 */
1238 for (j = 0; j < bitmap->file_pages; j++) {
1239 struct page *page = bitmap->filemap[j];
1240
1241 if (test_page_attr(bitmap, page,
1242 BITMAP_PAGE_DIRTY))
1243 /* bitmap_unplug will handle the rest */
1244 break;
1245 if (test_page_attr(bitmap, page,
1246 BITMAP_PAGE_NEEDWRITE)) {
1247 clear_page_attr(bitmap, page,
1248 BITMAP_PAGE_NEEDWRITE);
1249 spin_unlock_irqrestore(&bitmap->lock, flags);
1250 write_page(bitmap, page, 0);
1251 spin_lock_irqsave(&bitmap->lock, flags);
1252 if (!bitmap->filemap)
1253 break;
1254 }
1255 }
NeilBrownbf07bb72012-05-22 13:55:06 +10001256 spin_unlock_irqrestore(&bitmap->lock, flags);
1257
NeilBrown7be3dfe2008-03-10 11:43:48 -07001258 done:
NeilBrown8311c292008-03-04 14:29:30 -08001259 if (bitmap->allclean == 0)
NeilBrown2e61ebb2011-12-23 10:17:50 +11001260 mddev->thread->timeout =
1261 mddev->bitmap_info.daemon_sleep;
NeilBrownc3d97142009-12-14 12:49:52 +11001262 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown32a76272005-06-21 17:17:14 -07001263}
1264
NeilBrown32a76272005-06-21 17:17:14 -07001265static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001266 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001267 int create)
NeilBrownee305ac2009-09-23 18:06:44 +10001268__releases(bitmap->lock)
1269__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -07001270{
1271 /* If 'create', we might release the lock and reclaim it.
1272 * The lock must have been taken with interrupts enabled.
1273 * If !create, we don't release the lock.
1274 */
NeilBrown61a0d802012-03-19 12:46:41 +11001275 sector_t chunk = offset >> bitmap->chunkshift;
NeilBrown32a76272005-06-21 17:17:14 -07001276 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1277 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1278 sector_t csize;
NeilBrownef425672010-06-01 19:37:33 +10001279 int err;
NeilBrown32a76272005-06-21 17:17:14 -07001280
NeilBrownef425672010-06-01 19:37:33 +10001281 err = bitmap_checkpage(bitmap, page, create);
1282
1283 if (bitmap->bp[page].hijacked ||
1284 bitmap->bp[page].map == NULL)
NeilBrown61a0d802012-03-19 12:46:41 +11001285 csize = ((sector_t)1) << (bitmap->chunkshift +
NeilBrownef425672010-06-01 19:37:33 +10001286 PAGE_COUNTER_SHIFT - 1);
1287 else
NeilBrown61a0d802012-03-19 12:46:41 +11001288 csize = ((sector_t)1) << bitmap->chunkshift;
NeilBrownef425672010-06-01 19:37:33 +10001289 *blocks = csize - (offset & (csize - 1));
1290
1291 if (err < 0)
NeilBrown32a76272005-06-21 17:17:14 -07001292 return NULL;
NeilBrownef425672010-06-01 19:37:33 +10001293
NeilBrown32a76272005-06-21 17:17:14 -07001294 /* now locked ... */
1295
1296 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1297 /* should we use the first or second counter field
1298 * of the hijacked pointer? */
1299 int hi = (pageoff > PAGE_COUNTER_MASK);
NeilBrown32a76272005-06-21 17:17:14 -07001300 return &((bitmap_counter_t *)
1301 &bitmap->bp[page].map)[hi];
NeilBrownef425672010-06-01 19:37:33 +10001302 } else /* page is allocated */
NeilBrown32a76272005-06-21 17:17:14 -07001303 return (bitmap_counter_t *)
1304 &(bitmap->bp[page].map[pageoff]);
NeilBrown32a76272005-06-21 17:17:14 -07001305}
1306
NeilBrown4b6d2872005-09-09 16:23:47 -07001307int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001308{
NeilBrownac2f40b2010-06-01 19:37:31 +10001309 if (!bitmap)
1310 return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001311
1312 if (behind) {
Paul Clements696fcd52010-03-08 16:02:37 +11001313 int bw;
NeilBrown4b6d2872005-09-09 16:23:47 -07001314 atomic_inc(&bitmap->behind_writes);
Paul Clements696fcd52010-03-08 16:02:37 +11001315 bw = atomic_read(&bitmap->behind_writes);
1316 if (bw > bitmap->behind_writes_used)
1317 bitmap->behind_writes_used = bw;
1318
NeilBrown36a4e1f2011-10-07 14:23:17 +11001319 pr_debug("inc write-behind count %d/%lu\n",
1320 bw, bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001321 }
1322
NeilBrown32a76272005-06-21 17:17:14 -07001323 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001324 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001325 bitmap_counter_t *bmc;
1326
1327 spin_lock_irq(&bitmap->lock);
1328 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1329 if (!bmc) {
1330 spin_unlock_irq(&bitmap->lock);
1331 return 0;
1332 }
1333
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001334 if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
Neil Brownda6e1a32007-02-08 14:20:37 -08001335 DEFINE_WAIT(__wait);
1336 /* note that it is safe to do the prepare_to_wait
1337 * after the test as long as we do it before dropping
1338 * the spinlock.
1339 */
1340 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1341 TASK_UNINTERRUPTIBLE);
1342 spin_unlock_irq(&bitmap->lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001343 io_schedule();
Neil Brownda6e1a32007-02-08 14:20:37 -08001344 finish_wait(&bitmap->overflow_wait, &__wait);
1345 continue;
1346 }
1347
NeilBrownac2f40b2010-06-01 19:37:31 +10001348 switch (*bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001349 case 0:
1350 bitmap_file_set_bit(bitmap, offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001351 bitmap_count_page(bitmap, offset, 1);
NeilBrown32a76272005-06-21 17:17:14 -07001352 /* fall through */
1353 case 1:
1354 *bmc = 2;
1355 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001356
NeilBrown32a76272005-06-21 17:17:14 -07001357 (*bmc)++;
1358
1359 spin_unlock_irq(&bitmap->lock);
1360
1361 offset += blocks;
1362 if (sectors > blocks)
1363 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001364 else
1365 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001366 }
1367 return 0;
1368}
NeilBrownac2f40b2010-06-01 19:37:31 +10001369EXPORT_SYMBOL(bitmap_startwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001370
1371void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001372 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001373{
NeilBrownac2f40b2010-06-01 19:37:31 +10001374 if (!bitmap)
1375 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001376 if (behind) {
NeilBrowne5551902010-03-31 11:21:44 +11001377 if (atomic_dec_and_test(&bitmap->behind_writes))
1378 wake_up(&bitmap->behind_wait);
NeilBrown36a4e1f2011-10-07 14:23:17 +11001379 pr_debug("dec write-behind count %d/%lu\n",
1380 atomic_read(&bitmap->behind_writes),
1381 bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001382 }
1383
NeilBrown32a76272005-06-21 17:17:14 -07001384 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001385 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001386 unsigned long flags;
1387 bitmap_counter_t *bmc;
1388
1389 spin_lock_irqsave(&bitmap->lock, flags);
1390 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1391 if (!bmc) {
1392 spin_unlock_irqrestore(&bitmap->lock, flags);
1393 return;
1394 }
1395
NeilBrown961902c2011-12-23 09:57:48 +11001396 if (success && !bitmap->mddev->degraded &&
Neil Browna0da84f2008-06-28 08:31:22 +10001397 bitmap->events_cleared < bitmap->mddev->events) {
1398 bitmap->events_cleared = bitmap->mddev->events;
1399 bitmap->need_sync = 1;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001400 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
Neil Browna0da84f2008-06-28 08:31:22 +10001401 }
1402
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001403 if (!success && !NEEDED(*bmc))
NeilBrown32a76272005-06-21 17:17:14 -07001404 *bmc |= NEEDED_MASK;
1405
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001406 if (COUNTER(*bmc) == COUNTER_MAX)
Neil Brownda6e1a32007-02-08 14:20:37 -08001407 wake_up(&bitmap->overflow_wait);
1408
NeilBrown32a76272005-06-21 17:17:14 -07001409 (*bmc)--;
NeilBrown2585f3e2011-09-21 15:37:46 +10001410 if (*bmc <= 2) {
NeilBrownbf07bb72012-05-22 13:55:06 +10001411 bitmap_set_pending(bitmap, offset);
NeilBrown2585f3e2011-09-21 15:37:46 +10001412 bitmap->allclean = 0;
1413 }
NeilBrown32a76272005-06-21 17:17:14 -07001414 spin_unlock_irqrestore(&bitmap->lock, flags);
1415 offset += blocks;
1416 if (sectors > blocks)
1417 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001418 else
1419 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001420 }
1421}
NeilBrownac2f40b2010-06-01 19:37:31 +10001422EXPORT_SYMBOL(bitmap_endwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001423
NeilBrown57dab0b2010-10-19 10:03:39 +11001424static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001425 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001426{
1427 bitmap_counter_t *bmc;
1428 int rv;
1429 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1430 *blocks = 1024;
1431 return 1; /* always resync if no bitmap */
1432 }
1433 spin_lock_irq(&bitmap->lock);
1434 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1435 rv = 0;
1436 if (bmc) {
1437 /* locked */
1438 if (RESYNC(*bmc))
1439 rv = 1;
1440 else if (NEEDED(*bmc)) {
1441 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001442 if (!degraded) { /* don't set/clear bits if degraded */
1443 *bmc |= RESYNC_MASK;
1444 *bmc &= ~NEEDED_MASK;
1445 }
NeilBrown32a76272005-06-21 17:17:14 -07001446 }
1447 }
1448 spin_unlock_irq(&bitmap->lock);
1449 return rv;
1450}
1451
NeilBrown57dab0b2010-10-19 10:03:39 +11001452int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001453 int degraded)
1454{
1455 /* bitmap_start_sync must always report on multiples of whole
1456 * pages, otherwise resync (which is very PAGE_SIZE based) will
1457 * get confused.
1458 * So call __bitmap_start_sync repeatedly (if needed) until
1459 * At least PAGE_SIZE>>9 blocks are covered.
1460 * Return the 'or' of the result.
1461 */
1462 int rv = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001463 sector_t blocks1;
NeilBrown1187cf02009-03-31 14:27:02 +11001464
1465 *blocks = 0;
1466 while (*blocks < (PAGE_SIZE>>9)) {
1467 rv |= __bitmap_start_sync(bitmap, offset,
1468 &blocks1, degraded);
1469 offset += blocks1;
1470 *blocks += blocks1;
1471 }
1472 return rv;
1473}
NeilBrownac2f40b2010-06-01 19:37:31 +10001474EXPORT_SYMBOL(bitmap_start_sync);
NeilBrown1187cf02009-03-31 14:27:02 +11001475
NeilBrown57dab0b2010-10-19 10:03:39 +11001476void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
NeilBrown32a76272005-06-21 17:17:14 -07001477{
1478 bitmap_counter_t *bmc;
1479 unsigned long flags;
NeilBrownac2f40b2010-06-01 19:37:31 +10001480
1481 if (bitmap == NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001482 *blocks = 1024;
1483 return;
1484 }
1485 spin_lock_irqsave(&bitmap->lock, flags);
1486 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1487 if (bmc == NULL)
1488 goto unlock;
1489 /* locked */
NeilBrown32a76272005-06-21 17:17:14 -07001490 if (RESYNC(*bmc)) {
1491 *bmc &= ~RESYNC_MASK;
1492
1493 if (!NEEDED(*bmc) && aborted)
1494 *bmc |= NEEDED_MASK;
1495 else {
NeilBrown2585f3e2011-09-21 15:37:46 +10001496 if (*bmc <= 2) {
NeilBrownbf07bb72012-05-22 13:55:06 +10001497 bitmap_set_pending(bitmap, offset);
NeilBrown2585f3e2011-09-21 15:37:46 +10001498 bitmap->allclean = 0;
1499 }
NeilBrown32a76272005-06-21 17:17:14 -07001500 }
1501 }
1502 unlock:
1503 spin_unlock_irqrestore(&bitmap->lock, flags);
1504}
NeilBrownac2f40b2010-06-01 19:37:31 +10001505EXPORT_SYMBOL(bitmap_end_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001506
1507void bitmap_close_sync(struct bitmap *bitmap)
1508{
1509 /* Sync has finished, and any bitmap chunks that weren't synced
1510 * properly have been aborted. It remains to us to clear the
1511 * RESYNC bit wherever it is still on
1512 */
1513 sector_t sector = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001514 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001515 if (!bitmap)
1516 return;
NeilBrown32a76272005-06-21 17:17:14 -07001517 while (sector < bitmap->mddev->resync_max_sectors) {
1518 bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001519 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001520 }
1521}
NeilBrownac2f40b2010-06-01 19:37:31 +10001522EXPORT_SYMBOL(bitmap_close_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001523
NeilBrownb47490c2008-02-06 01:39:50 -08001524void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1525{
1526 sector_t s = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001527 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001528
1529 if (!bitmap)
1530 return;
1531 if (sector == 0) {
1532 bitmap->last_end_sync = jiffies;
1533 return;
1534 }
1535 if (time_before(jiffies, (bitmap->last_end_sync
NeilBrown1b04be92009-12-14 12:49:53 +11001536 + bitmap->mddev->bitmap_info.daemon_sleep)))
NeilBrownb47490c2008-02-06 01:39:50 -08001537 return;
1538 wait_event(bitmap->mddev->recovery_wait,
1539 atomic_read(&bitmap->mddev->recovery_active) == 0);
1540
NeilBrown75d3da42011-01-14 09:14:34 +11001541 bitmap->mddev->curr_resync_completed = sector;
NeilBrown070dc6d2010-08-30 17:33:34 +10001542 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
NeilBrown61a0d802012-03-19 12:46:41 +11001543 sector &= ~((1ULL << bitmap->chunkshift) - 1);
NeilBrownb47490c2008-02-06 01:39:50 -08001544 s = 0;
1545 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1546 bitmap_end_sync(bitmap, s, &blocks, 0);
1547 s += blocks;
1548 }
1549 bitmap->last_end_sync = jiffies;
NeilBrownacb180b2009-04-14 16:28:34 +10001550 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
NeilBrownb47490c2008-02-06 01:39:50 -08001551}
NeilBrownac2f40b2010-06-01 19:37:31 +10001552EXPORT_SYMBOL(bitmap_cond_end_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08001553
NeilBrown6a079972005-09-09 16:23:44 -07001554static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001555{
1556 /* For each chunk covered by any of these sectors, set the
NeilBrownef99bf42012-05-22 13:55:08 +10001557 * counter to 2 and possibly set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001558 * be 0 at this point
1559 */
NeilBrown193f1c92005-08-04 12:53:33 -07001560
NeilBrown57dab0b2010-10-19 10:03:39 +11001561 sector_t secs;
NeilBrown193f1c92005-08-04 12:53:33 -07001562 bitmap_counter_t *bmc;
1563 spin_lock_irq(&bitmap->lock);
1564 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1565 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001566 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001567 return;
NeilBrown32a76272005-06-21 17:17:14 -07001568 }
NeilBrownac2f40b2010-06-01 19:37:31 +10001569 if (!*bmc) {
NeilBrown915c4202011-12-23 10:17:51 +11001570 *bmc = 2 | (needed ? NEEDED_MASK : 0);
NeilBrown193f1c92005-08-04 12:53:33 -07001571 bitmap_count_page(bitmap, offset, 1);
NeilBrownbf07bb72012-05-22 13:55:06 +10001572 bitmap_set_pending(bitmap, offset);
NeilBrown2585f3e2011-09-21 15:37:46 +10001573 bitmap->allclean = 0;
NeilBrown193f1c92005-08-04 12:53:33 -07001574 }
1575 spin_unlock_irq(&bitmap->lock);
NeilBrown32a76272005-06-21 17:17:14 -07001576}
1577
Paul Clements9b1d1da2006-10-03 01:15:49 -07001578/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1579void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1580{
1581 unsigned long chunk;
1582
1583 for (chunk = s; chunk <= e; chunk++) {
NeilBrown61a0d802012-03-19 12:46:41 +11001584 sector_t sec = (sector_t)chunk << bitmap->chunkshift;
Paul Clements9b1d1da2006-10-03 01:15:49 -07001585 bitmap_set_memory_bits(bitmap, sec, 1);
NeilBrown7c8f4242011-11-23 10:18:52 +11001586 spin_lock_irq(&bitmap->lock);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001587 bitmap_file_set_bit(bitmap, sec);
NeilBrown7c8f4242011-11-23 10:18:52 +11001588 spin_unlock_irq(&bitmap->lock);
NeilBrownffa23322009-12-14 12:49:56 +11001589 if (sec < bitmap->mddev->recovery_cp)
1590 /* We are asserting that the array is dirty,
1591 * so move the recovery_cp address back so
1592 * that it is obvious that it is dirty
1593 */
1594 bitmap->mddev->recovery_cp = sec;
Paul Clements9b1d1da2006-10-03 01:15:49 -07001595 }
1596}
1597
NeilBrown32a76272005-06-21 17:17:14 -07001598/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001599 * flush out any pending updates
1600 */
NeilBrownfd01b882011-10-11 16:47:53 +11001601void bitmap_flush(struct mddev *mddev)
NeilBrown6b8b3e82005-08-04 12:53:35 -07001602{
1603 struct bitmap *bitmap = mddev->bitmap;
NeilBrown42a04b52009-12-14 12:49:53 +11001604 long sleep;
NeilBrown6b8b3e82005-08-04 12:53:35 -07001605
1606 if (!bitmap) /* there was no bitmap */
1607 return;
1608
1609 /* run the daemon_work three time to ensure everything is flushed
1610 * that can be
1611 */
NeilBrown1b04be92009-12-14 12:49:53 +11001612 sleep = mddev->bitmap_info.daemon_sleep * 2;
NeilBrown42a04b52009-12-14 12:49:53 +11001613 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001614 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001615 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001616 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001617 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001618 bitmap_daemon_work(mddev);
NeilBrown6b8b3e82005-08-04 12:53:35 -07001619 bitmap_update_sb(bitmap);
1620}
1621
1622/*
NeilBrown32a76272005-06-21 17:17:14 -07001623 * free memory that was allocated
1624 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001625static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001626{
1627 unsigned long k, pages;
1628 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001629
1630 if (!bitmap) /* there was no bitmap */
1631 return;
1632
NeilBrown32a76272005-06-21 17:17:14 -07001633 /* release the bitmap file and kill the daemon */
1634 bitmap_file_put(bitmap);
1635
1636 bp = bitmap->bp;
1637 pages = bitmap->pages;
1638
1639 /* free all allocated memory */
1640
NeilBrown32a76272005-06-21 17:17:14 -07001641 if (bp) /* deallocate the page memory */
1642 for (k = 0; k < pages; k++)
1643 if (bp[k].map && !bp[k].hijacked)
1644 kfree(bp[k].map);
1645 kfree(bp);
1646 kfree(bitmap);
1647}
NeilBrownaa5cbd12009-12-14 12:49:46 +11001648
NeilBrownfd01b882011-10-11 16:47:53 +11001649void bitmap_destroy(struct mddev *mddev)
NeilBrown3178b0d2005-09-09 16:23:50 -07001650{
1651 struct bitmap *bitmap = mddev->bitmap;
1652
1653 if (!bitmap) /* there was no bitmap */
1654 return;
1655
NeilBrownc3d97142009-12-14 12:49:52 +11001656 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrown3178b0d2005-09-09 16:23:50 -07001657 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownc3d97142009-12-14 12:49:52 +11001658 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrownb15c2e52006-01-06 00:20:16 -08001659 if (mddev->thread)
1660 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001661
NeilBrownece5cff2009-12-14 12:49:56 +11001662 if (bitmap->sysfs_can_clear)
1663 sysfs_put(bitmap->sysfs_can_clear);
1664
NeilBrown3178b0d2005-09-09 16:23:50 -07001665 bitmap_free(bitmap);
1666}
NeilBrown32a76272005-06-21 17:17:14 -07001667
1668/*
1669 * initialize the bitmap structure
1670 * if this returns an error, bitmap_destroy must be called to do clean up
1671 */
NeilBrownfd01b882011-10-11 16:47:53 +11001672int bitmap_create(struct mddev *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001673{
1674 struct bitmap *bitmap;
NeilBrown1f593902009-04-20 11:50:24 +10001675 sector_t blocks = mddev->resync_max_sectors;
NeilBrown32a76272005-06-21 17:17:14 -07001676 unsigned long chunks;
1677 unsigned long pages;
NeilBrownc3d97142009-12-14 12:49:52 +11001678 struct file *file = mddev->bitmap_info.file;
NeilBrown32a76272005-06-21 17:17:14 -07001679 int err;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001680 struct sysfs_dirent *bm = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001681
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001682 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001683
NeilBrownc3d97142009-12-14 12:49:52 +11001684 BUG_ON(file && mddev->bitmap_info.offset);
NeilBrowna654b9d82005-06-21 17:17:27 -07001685
NeilBrown9ffae0c2006-01-06 00:20:32 -08001686 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001687 if (!bitmap)
1688 return -ENOMEM;
1689
NeilBrown32a76272005-06-21 17:17:14 -07001690 spin_lock_init(&bitmap->lock);
NeilBrownce25c312006-06-26 00:27:49 -07001691 atomic_set(&bitmap->pending_writes, 0);
1692 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001693 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrowne5551902010-03-31 11:21:44 +11001694 init_waitqueue_head(&bitmap->behind_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001695
NeilBrown32a76272005-06-21 17:17:14 -07001696 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001697
NeilBrown5ff5aff2010-06-01 19:37:32 +10001698 if (mddev->kobj.sd)
1699 bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
NeilBrownece5cff2009-12-14 12:49:56 +11001700 if (bm) {
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07001701 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
NeilBrownece5cff2009-12-14 12:49:56 +11001702 sysfs_put(bm);
1703 } else
1704 bitmap->sysfs_can_clear = NULL;
1705
NeilBrown32a76272005-06-21 17:17:14 -07001706 bitmap->file = file;
NeilBrownce25c312006-06-26 00:27:49 -07001707 if (file) {
1708 get_file(file);
NeilBrownae8fa282009-10-16 15:56:01 +11001709 /* As future accesses to this file will use bmap,
1710 * and bypass the page cache, we must sync the file
1711 * first.
1712 */
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001713 vfs_fsync(file, 1);
NeilBrownce25c312006-06-26 00:27:49 -07001714 }
NeilBrown42a04b52009-12-14 12:49:53 +11001715 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
Jonathan Brassow9c810752011-06-08 17:59:30 -05001716 if (!mddev->bitmap_info.external) {
1717 /*
1718 * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
1719 * instructing us to create a new on-disk bitmap instance.
1720 */
1721 if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
1722 err = bitmap_new_disk_sb(bitmap);
1723 else
1724 err = bitmap_read_sb(bitmap);
1725 } else {
NeilBrownece5cff2009-12-14 12:49:56 +11001726 err = 0;
1727 if (mddev->bitmap_info.chunksize == 0 ||
1728 mddev->bitmap_info.daemon_sleep == 0)
1729 /* chunksize and time_base need to be
1730 * set first. */
1731 err = -EINVAL;
1732 }
NeilBrown32a76272005-06-21 17:17:14 -07001733 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001734 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001735
NeilBrown624ce4f2009-12-14 12:49:56 +11001736 bitmap->daemon_lastrun = jiffies;
NeilBrown61a0d802012-03-19 12:46:41 +11001737 bitmap->chunkshift = (ffz(~mddev->bitmap_info.chunksize)
1738 - BITMAP_BLOCK_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -07001739
NeilBrownb16b1b62012-05-04 17:03:18 +10001740 chunks = (blocks + (1 << bitmap->chunkshift) - 1) >>
NeilBrown61a0d802012-03-19 12:46:41 +11001741 bitmap->chunkshift;
NeilBrownac2f40b2010-06-01 19:37:31 +10001742 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
NeilBrown32a76272005-06-21 17:17:14 -07001743
1744 BUG_ON(!pages);
1745
1746 bitmap->chunks = chunks;
1747 bitmap->pages = pages;
1748 bitmap->missing_pages = pages;
NeilBrown32a76272005-06-21 17:17:14 -07001749
NeilBrown9ffae0c2006-01-06 00:20:32 -08001750 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown29d32472011-10-11 16:49:56 +11001751
NeilBrown3178b0d2005-09-09 16:23:50 -07001752 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001753 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001754 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001755
NeilBrown69e51b42010-06-01 19:37:35 +10001756 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1757 pages, bmname(bitmap));
1758
1759 mddev->bitmap = bitmap;
1760
1761
1762 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
1763
1764 error:
1765 bitmap_free(bitmap);
1766 return err;
1767}
1768
NeilBrownfd01b882011-10-11 16:47:53 +11001769int bitmap_load(struct mddev *mddev)
NeilBrown69e51b42010-06-01 19:37:35 +10001770{
1771 int err = 0;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001772 sector_t start = 0;
NeilBrown69e51b42010-06-01 19:37:35 +10001773 sector_t sector = 0;
1774 struct bitmap *bitmap = mddev->bitmap;
1775
1776 if (!bitmap)
1777 goto out;
1778
1779 /* Clear out old bitmap info first: Either there is none, or we
1780 * are resuming after someone else has possibly changed things,
1781 * so we should forget old cached info.
1782 * All chunks should be clean, but some might need_sync.
1783 */
1784 while (sector < mddev->resync_max_sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001785 sector_t blocks;
NeilBrown69e51b42010-06-01 19:37:35 +10001786 bitmap_start_sync(bitmap, sector, &blocks, 0);
1787 sector += blocks;
1788 }
1789 bitmap_close_sync(bitmap);
1790
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001791 if (mddev->degraded == 0
1792 || bitmap->events_cleared == mddev->events)
1793 /* no need to keep dirty bits to optimise a
1794 * re-add of a missing device */
1795 start = mddev->recovery_cp;
NeilBrown69e51b42010-06-01 19:37:35 +10001796
NeilBrownafbaa902012-04-12 16:05:06 +10001797 mutex_lock(&mddev->bitmap_info.mutex);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001798 err = bitmap_init_from_disk(bitmap, start);
NeilBrownafbaa902012-04-12 16:05:06 +10001799 mutex_unlock(&mddev->bitmap_info.mutex);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001800
NeilBrown32a76272005-06-21 17:17:14 -07001801 if (err)
NeilBrown69e51b42010-06-01 19:37:35 +10001802 goto out;
NeilBrownef99bf42012-05-22 13:55:08 +10001803 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1804
1805 /* Kick recovery in case any bits were set */
1806 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
NeilBrown3178b0d2005-09-09 16:23:50 -07001807
NeilBrown1b04be92009-12-14 12:49:53 +11001808 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
NeilBrown9cd30fd2009-12-14 12:49:54 +11001809 md_wakeup_thread(mddev->thread);
NeilBrownb15c2e52006-01-06 00:20:16 -08001810
NeilBrown4ad13662007-07-17 04:06:13 -07001811 bitmap_update_sb(bitmap);
1812
NeilBrown69e51b42010-06-01 19:37:35 +10001813 if (bitmap->flags & BITMAP_WRITE_ERROR)
1814 err = -EIO;
1815out:
NeilBrown3178b0d2005-09-09 16:23:50 -07001816 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001817}
NeilBrown69e51b42010-06-01 19:37:35 +10001818EXPORT_SYMBOL_GPL(bitmap_load);
NeilBrown32a76272005-06-21 17:17:14 -07001819
NeilBrown57148962012-03-19 12:46:40 +11001820void bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
1821{
1822 unsigned long chunk_kb;
1823 unsigned long flags;
1824
1825 if (!bitmap)
1826 return;
1827
1828 spin_lock_irqsave(&bitmap->lock, flags);
1829 chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
1830 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
1831 "%lu%s chunk",
1832 bitmap->pages - bitmap->missing_pages,
1833 bitmap->pages,
1834 (bitmap->pages - bitmap->missing_pages)
1835 << (PAGE_SHIFT - 10),
1836 chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
1837 chunk_kb ? "KB" : "B");
1838 if (bitmap->file) {
1839 seq_printf(seq, ", file: ");
1840 seq_path(seq, &bitmap->file->f_path, " \t\n");
1841 }
1842
1843 seq_printf(seq, "\n");
1844 spin_unlock_irqrestore(&bitmap->lock, flags);
1845}
1846
NeilBrown43a70502009-12-14 12:49:55 +11001847static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001848location_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11001849{
1850 ssize_t len;
NeilBrownac2f40b2010-06-01 19:37:31 +10001851 if (mddev->bitmap_info.file)
NeilBrown43a70502009-12-14 12:49:55 +11001852 len = sprintf(page, "file");
NeilBrownac2f40b2010-06-01 19:37:31 +10001853 else if (mddev->bitmap_info.offset)
NeilBrown43a70502009-12-14 12:49:55 +11001854 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001855 else
NeilBrown43a70502009-12-14 12:49:55 +11001856 len = sprintf(page, "none");
1857 len += sprintf(page+len, "\n");
1858 return len;
1859}
1860
1861static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001862location_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11001863{
1864
1865 if (mddev->pers) {
1866 if (!mddev->pers->quiesce)
1867 return -EBUSY;
1868 if (mddev->recovery || mddev->sync_thread)
1869 return -EBUSY;
1870 }
1871
1872 if (mddev->bitmap || mddev->bitmap_info.file ||
1873 mddev->bitmap_info.offset) {
1874 /* bitmap already configured. Only option is to clear it */
1875 if (strncmp(buf, "none", 4) != 0)
1876 return -EBUSY;
1877 if (mddev->pers) {
1878 mddev->pers->quiesce(mddev, 1);
1879 bitmap_destroy(mddev);
1880 mddev->pers->quiesce(mddev, 0);
1881 }
1882 mddev->bitmap_info.offset = 0;
1883 if (mddev->bitmap_info.file) {
1884 struct file *f = mddev->bitmap_info.file;
1885 mddev->bitmap_info.file = NULL;
1886 restore_bitmap_write_access(f);
1887 fput(f);
1888 }
1889 } else {
1890 /* No bitmap, OK to set a location */
1891 long long offset;
1892 if (strncmp(buf, "none", 4) == 0)
1893 /* nothing to be done */;
1894 else if (strncmp(buf, "file:", 5) == 0) {
1895 /* Not supported yet */
1896 return -EINVAL;
1897 } else {
1898 int rv;
1899 if (buf[0] == '+')
1900 rv = strict_strtoll(buf+1, 10, &offset);
1901 else
1902 rv = strict_strtoll(buf, 10, &offset);
1903 if (rv)
1904 return rv;
1905 if (offset == 0)
1906 return -EINVAL;
NeilBrownece5cff2009-12-14 12:49:56 +11001907 if (mddev->bitmap_info.external == 0 &&
1908 mddev->major_version == 0 &&
NeilBrown43a70502009-12-14 12:49:55 +11001909 offset != mddev->bitmap_info.default_offset)
1910 return -EINVAL;
1911 mddev->bitmap_info.offset = offset;
1912 if (mddev->pers) {
1913 mddev->pers->quiesce(mddev, 1);
1914 rv = bitmap_create(mddev);
NeilBrown4474ca42012-03-19 12:46:37 +11001915 if (!rv)
1916 rv = bitmap_load(mddev);
NeilBrown43a70502009-12-14 12:49:55 +11001917 if (rv) {
1918 bitmap_destroy(mddev);
1919 mddev->bitmap_info.offset = 0;
1920 }
1921 mddev->pers->quiesce(mddev, 0);
1922 if (rv)
1923 return rv;
1924 }
1925 }
1926 }
1927 if (!mddev->external) {
1928 /* Ensure new bitmap info is stored in
1929 * metadata promptly.
1930 */
1931 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1932 md_wakeup_thread(mddev->thread);
1933 }
1934 return len;
1935}
1936
1937static struct md_sysfs_entry bitmap_location =
1938__ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
1939
NeilBrown6409bb02012-05-22 13:55:07 +10001940/* 'bitmap/space' is the space available at 'location' for the
1941 * bitmap. This allows the kernel to know when it is safe to
1942 * resize the bitmap to match a resized array.
1943 */
1944static ssize_t
1945space_show(struct mddev *mddev, char *page)
1946{
1947 return sprintf(page, "%lu\n", mddev->bitmap_info.space);
1948}
1949
1950static ssize_t
1951space_store(struct mddev *mddev, const char *buf, size_t len)
1952{
1953 unsigned long sectors;
1954 int rv;
1955
1956 rv = kstrtoul(buf, 10, &sectors);
1957 if (rv)
1958 return rv;
1959
1960 if (sectors == 0)
1961 return -EINVAL;
1962
1963 if (mddev->bitmap &&
1964 sectors < ((mddev->bitmap->file_pages - 1) * PAGE_SIZE
1965 + mddev->bitmap->last_page_size + 511) >> 9)
1966 return -EFBIG; /* Bitmap is too big for this small space */
1967
1968 /* could make sure it isn't too big, but that isn't really
1969 * needed - user-space should be careful.
1970 */
1971 mddev->bitmap_info.space = sectors;
1972 return len;
1973}
1974
1975static struct md_sysfs_entry bitmap_space =
1976__ATTR(space, S_IRUGO|S_IWUSR, space_show, space_store);
1977
NeilBrown43a70502009-12-14 12:49:55 +11001978static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001979timeout_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11001980{
1981 ssize_t len;
1982 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
1983 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
NeilBrownac2f40b2010-06-01 19:37:31 +10001984
NeilBrown43a70502009-12-14 12:49:55 +11001985 len = sprintf(page, "%lu", secs);
1986 if (jifs)
1987 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
1988 len += sprintf(page+len, "\n");
1989 return len;
1990}
1991
1992static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001993timeout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11001994{
1995 /* timeout can be set at any time */
1996 unsigned long timeout;
1997 int rv = strict_strtoul_scaled(buf, &timeout, 4);
1998 if (rv)
1999 return rv;
2000
2001 /* just to make sure we don't overflow... */
2002 if (timeout >= LONG_MAX / HZ)
2003 return -EINVAL;
2004
2005 timeout = timeout * HZ / 10000;
2006
2007 if (timeout >= MAX_SCHEDULE_TIMEOUT)
2008 timeout = MAX_SCHEDULE_TIMEOUT-1;
2009 if (timeout < 1)
2010 timeout = 1;
2011 mddev->bitmap_info.daemon_sleep = timeout;
2012 if (mddev->thread) {
2013 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
2014 * the bitmap is all clean and we don't need to
2015 * adjust the timeout right now
2016 */
2017 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
2018 mddev->thread->timeout = timeout;
2019 md_wakeup_thread(mddev->thread);
2020 }
2021 }
2022 return len;
2023}
2024
2025static struct md_sysfs_entry bitmap_timeout =
2026__ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
2027
2028static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002029backlog_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002030{
2031 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
2032}
2033
2034static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002035backlog_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002036{
2037 unsigned long backlog;
2038 int rv = strict_strtoul(buf, 10, &backlog);
2039 if (rv)
2040 return rv;
2041 if (backlog > COUNTER_MAX)
2042 return -EINVAL;
2043 mddev->bitmap_info.max_write_behind = backlog;
2044 return len;
2045}
2046
2047static struct md_sysfs_entry bitmap_backlog =
2048__ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
2049
2050static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002051chunksize_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002052{
2053 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
2054}
2055
2056static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002057chunksize_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002058{
2059 /* Can only be changed when no bitmap is active */
2060 int rv;
2061 unsigned long csize;
2062 if (mddev->bitmap)
2063 return -EBUSY;
2064 rv = strict_strtoul(buf, 10, &csize);
2065 if (rv)
2066 return rv;
2067 if (csize < 512 ||
2068 !is_power_of_2(csize))
2069 return -EINVAL;
2070 mddev->bitmap_info.chunksize = csize;
2071 return len;
2072}
2073
2074static struct md_sysfs_entry bitmap_chunksize =
2075__ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
2076
NeilBrownfd01b882011-10-11 16:47:53 +11002077static ssize_t metadata_show(struct mddev *mddev, char *page)
NeilBrownece5cff2009-12-14 12:49:56 +11002078{
2079 return sprintf(page, "%s\n", (mddev->bitmap_info.external
2080 ? "external" : "internal"));
2081}
2082
NeilBrownfd01b882011-10-11 16:47:53 +11002083static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownece5cff2009-12-14 12:49:56 +11002084{
2085 if (mddev->bitmap ||
2086 mddev->bitmap_info.file ||
2087 mddev->bitmap_info.offset)
2088 return -EBUSY;
2089 if (strncmp(buf, "external", 8) == 0)
2090 mddev->bitmap_info.external = 1;
2091 else if (strncmp(buf, "internal", 8) == 0)
2092 mddev->bitmap_info.external = 0;
2093 else
2094 return -EINVAL;
2095 return len;
2096}
2097
2098static struct md_sysfs_entry bitmap_metadata =
2099__ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2100
NeilBrownfd01b882011-10-11 16:47:53 +11002101static ssize_t can_clear_show(struct mddev *mddev, char *page)
NeilBrownece5cff2009-12-14 12:49:56 +11002102{
2103 int len;
2104 if (mddev->bitmap)
2105 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
2106 "false" : "true"));
2107 else
2108 len = sprintf(page, "\n");
2109 return len;
2110}
2111
NeilBrownfd01b882011-10-11 16:47:53 +11002112static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownece5cff2009-12-14 12:49:56 +11002113{
2114 if (mddev->bitmap == NULL)
2115 return -ENOENT;
2116 if (strncmp(buf, "false", 5) == 0)
2117 mddev->bitmap->need_sync = 1;
2118 else if (strncmp(buf, "true", 4) == 0) {
2119 if (mddev->degraded)
2120 return -EBUSY;
2121 mddev->bitmap->need_sync = 0;
2122 } else
2123 return -EINVAL;
2124 return len;
2125}
2126
2127static struct md_sysfs_entry bitmap_can_clear =
2128__ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
2129
Paul Clements696fcd52010-03-08 16:02:37 +11002130static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002131behind_writes_used_show(struct mddev *mddev, char *page)
Paul Clements696fcd52010-03-08 16:02:37 +11002132{
2133 if (mddev->bitmap == NULL)
2134 return sprintf(page, "0\n");
2135 return sprintf(page, "%lu\n",
2136 mddev->bitmap->behind_writes_used);
2137}
2138
2139static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002140behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
Paul Clements696fcd52010-03-08 16:02:37 +11002141{
2142 if (mddev->bitmap)
2143 mddev->bitmap->behind_writes_used = 0;
2144 return len;
2145}
2146
2147static struct md_sysfs_entry max_backlog_used =
2148__ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2149 behind_writes_used_show, behind_writes_used_reset);
2150
NeilBrown43a70502009-12-14 12:49:55 +11002151static struct attribute *md_bitmap_attrs[] = {
2152 &bitmap_location.attr,
NeilBrown6409bb02012-05-22 13:55:07 +10002153 &bitmap_space.attr,
NeilBrown43a70502009-12-14 12:49:55 +11002154 &bitmap_timeout.attr,
2155 &bitmap_backlog.attr,
2156 &bitmap_chunksize.attr,
NeilBrownece5cff2009-12-14 12:49:56 +11002157 &bitmap_metadata.attr,
2158 &bitmap_can_clear.attr,
Paul Clements696fcd52010-03-08 16:02:37 +11002159 &max_backlog_used.attr,
NeilBrown43a70502009-12-14 12:49:55 +11002160 NULL
2161};
2162struct attribute_group md_bitmap_group = {
2163 .name = "bitmap",
2164 .attrs = md_bitmap_attrs,
2165};
2166