blob: cdf36b1e9aa6f28af6f2f1ed9042a02f3f41b8f0 [file] [log] [blame]
NeilBrown32a76272005-06-21 17:17:14 -07001/*
2 * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
3 *
4 * bitmap_create - sets up the bitmap structure
5 * bitmap_destroy - destroys the bitmap structure
6 *
7 * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.:
8 * - added disk storage for bitmap
9 * - changes to allow various bitmap chunk sizes
NeilBrown32a76272005-06-21 17:17:14 -070010 */
11
12/*
13 * Still to do:
14 *
15 * flush after percent set rather than just time based. (maybe both).
NeilBrown32a76272005-06-21 17:17:14 -070016 */
17
NeilBrownbff61972009-03-31 14:33:13 +110018#include <linux/blkdev.h>
NeilBrown32a76272005-06-21 17:17:14 -070019#include <linux/module.h>
NeilBrown32a76272005-06-21 17:17:14 -070020#include <linux/errno.h>
21#include <linux/slab.h>
22#include <linux/init.h>
NeilBrown32a76272005-06-21 17:17:14 -070023#include <linux/timer.h>
24#include <linux/sched.h>
25#include <linux/list.h>
26#include <linux/file.h>
27#include <linux/mount.h>
28#include <linux/buffer_head.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110029#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110030#include "bitmap.h"
NeilBrown32a76272005-06-21 17:17:14 -070031
NeilBrownac2f40b2010-06-01 19:37:31 +100032static inline char *bmname(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -070033{
34 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
35}
36
NeilBrown32a76272005-06-21 17:17:14 -070037/*
38 * just a placeholder - calls kmalloc for bitmap pages
39 */
40static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
41{
42 unsigned char *page;
43
NeilBrownac2f40b2010-06-01 19:37:31 +100044 page = kzalloc(PAGE_SIZE, GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -070045 if (!page)
46 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
47 else
NeilBrown36a4e1f2011-10-07 14:23:17 +110048 pr_debug("%s: bitmap_alloc_page: allocated page at %p\n",
49 bmname(bitmap), page);
NeilBrown32a76272005-06-21 17:17:14 -070050 return page;
51}
52
53/*
54 * for now just a placeholder -- just calls kfree for bitmap pages
55 */
56static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
57{
NeilBrown36a4e1f2011-10-07 14:23:17 +110058 pr_debug("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
NeilBrown32a76272005-06-21 17:17:14 -070059 kfree(page);
60}
61
62/*
63 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
64 *
65 * 1) check to see if this page is allocated, if it's not then try to alloc
66 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
67 * page pointer directly as a counter
68 *
69 * if we find our page, we increment the page's refcount so that it stays
70 * allocated while we're using it
71 */
NeilBrownac2f40b2010-06-01 19:37:31 +100072static int bitmap_checkpage(struct bitmap *bitmap,
73 unsigned long page, int create)
NeilBrownee305ac2009-09-23 18:06:44 +100074__releases(bitmap->lock)
75__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -070076{
77 unsigned char *mappage;
78
79 if (page >= bitmap->pages) {
NeilBrown1187cf02009-03-31 14:27:02 +110080 /* This can happen if bitmap_start_sync goes beyond
81 * End-of-device while looking for a whole page.
82 * It is harmless.
83 */
NeilBrown32a76272005-06-21 17:17:14 -070084 return -EINVAL;
85 }
86
NeilBrown32a76272005-06-21 17:17:14 -070087 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
88 return 0;
89
90 if (bitmap->bp[page].map) /* page is already allocated, just return */
91 return 0;
92
93 if (!create)
94 return -ENOENT;
95
NeilBrown32a76272005-06-21 17:17:14 -070096 /* this page has not been allocated yet */
97
NeilBrownac2f40b2010-06-01 19:37:31 +100098 spin_unlock_irq(&bitmap->lock);
99 mappage = bitmap_alloc_page(bitmap);
100 spin_lock_irq(&bitmap->lock);
101
102 if (mappage == NULL) {
NeilBrown36a4e1f2011-10-07 14:23:17 +1100103 pr_debug("%s: bitmap map page allocation failed, hijacking\n",
104 bmname(bitmap));
NeilBrown32a76272005-06-21 17:17:14 -0700105 /* failed - set the hijacked flag so that we can use the
106 * pointer as a counter */
NeilBrown32a76272005-06-21 17:17:14 -0700107 if (!bitmap->bp[page].map)
108 bitmap->bp[page].hijacked = 1;
NeilBrownac2f40b2010-06-01 19:37:31 +1000109 } else if (bitmap->bp[page].map ||
110 bitmap->bp[page].hijacked) {
NeilBrown32a76272005-06-21 17:17:14 -0700111 /* somebody beat us to getting the page */
112 bitmap_free_page(bitmap, mappage);
113 return 0;
NeilBrownac2f40b2010-06-01 19:37:31 +1000114 } else {
115
116 /* no page was in place and we have one, so install it */
117
118 bitmap->bp[page].map = mappage;
119 bitmap->missing_pages--;
NeilBrown32a76272005-06-21 17:17:14 -0700120 }
NeilBrown32a76272005-06-21 17:17:14 -0700121 return 0;
122}
123
NeilBrown32a76272005-06-21 17:17:14 -0700124/* if page is completely empty, put it back on the free list, or dealloc it */
125/* if page was hijacked, unmark the flag so it might get alloced next time */
126/* Note: lock should be held when calling this */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800127static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
NeilBrown32a76272005-06-21 17:17:14 -0700128{
129 char *ptr;
130
131 if (bitmap->bp[page].count) /* page is still busy */
132 return;
133
134 /* page is no longer in use, it can be released */
135
136 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
137 bitmap->bp[page].hijacked = 0;
138 bitmap->bp[page].map = NULL;
NeilBrownac2f40b2010-06-01 19:37:31 +1000139 } else {
140 /* normal case, free the page */
141 ptr = bitmap->bp[page].map;
142 bitmap->bp[page].map = NULL;
143 bitmap->missing_pages++;
144 bitmap_free_page(bitmap, ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700145 }
NeilBrown32a76272005-06-21 17:17:14 -0700146}
147
NeilBrown32a76272005-06-21 17:17:14 -0700148/*
149 * bitmap file handling - read and write the bitmap file and its superblock
150 */
151
NeilBrown32a76272005-06-21 17:17:14 -0700152/*
153 * basic page I/O operations
154 */
155
NeilBrowna654b9d82005-06-21 17:17:27 -0700156/* IO operations when bitmap is stored near all superblocks */
NeilBrownfd01b882011-10-11 16:47:53 +1100157static struct page *read_sb_page(struct mddev *mddev, loff_t offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100158 struct page *page,
159 unsigned long index, int size)
NeilBrowna654b9d82005-06-21 17:17:27 -0700160{
161 /* choose a good rdev and read the page from there */
162
NeilBrown3cb03002011-10-11 16:45:26 +1100163 struct md_rdev *rdev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700164 sector_t target;
NeilBrownac2f40b2010-06-01 19:37:31 +1000165 int did_alloc = 0;
NeilBrowna654b9d82005-06-21 17:17:27 -0700166
NeilBrownac2f40b2010-06-01 19:37:31 +1000167 if (!page) {
NeilBrowna2ed9612008-12-19 16:25:01 +1100168 page = alloc_page(GFP_KERNEL);
NeilBrownac2f40b2010-06-01 19:37:31 +1000169 if (!page)
170 return ERR_PTR(-ENOMEM);
171 did_alloc = 1;
172 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700173
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100174 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800175 if (! test_bit(In_sync, &rdev->flags)
176 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700177 continue;
178
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100179 target = offset + index * (PAGE_SIZE/512);
NeilBrowna654b9d82005-06-21 17:17:27 -0700180
NeilBrown2b193362010-10-27 15:16:40 +1100181 if (sync_page_io(rdev, target,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400182 roundup(size, bdev_logical_block_size(rdev->bdev)),
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100183 page, READ, true)) {
NeilBrownab904d62005-09-09 16:23:52 -0700184 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700185 attach_page_buffers(page, NULL); /* so that free_buffer will
186 * quietly no-op */
NeilBrownab904d62005-09-09 16:23:52 -0700187 return page;
188 }
189 }
NeilBrownac2f40b2010-06-01 19:37:31 +1000190 if (did_alloc)
191 put_page(page);
NeilBrownab904d62005-09-09 16:23:52 -0700192 return ERR_PTR(-EIO);
NeilBrowna654b9d82005-06-21 17:17:27 -0700193
NeilBrowna654b9d82005-06-21 17:17:27 -0700194}
195
NeilBrownfd01b882011-10-11 16:47:53 +1100196static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev)
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000197{
198 /* Iterate the disks of an mddev, using rcu to protect access to the
199 * linked list, and raising the refcount of devices we return to ensure
200 * they don't disappear while in use.
201 * As devices are only added or removed when raid_disk is < 0 and
202 * nr_pending is 0 and In_sync is clear, the entries we return will
203 * still be in the same position on the list when we re-enter
204 * list_for_each_continue_rcu.
205 */
206 struct list_head *pos;
207 rcu_read_lock();
208 if (rdev == NULL)
209 /* start at the beginning */
210 pos = &mddev->disks;
211 else {
212 /* release the previous rdev and start from there. */
213 rdev_dec_pending(rdev, mddev);
214 pos = &rdev->same_set;
215 }
216 list_for_each_continue_rcu(pos, &mddev->disks) {
NeilBrown3cb03002011-10-11 16:45:26 +1100217 rdev = list_entry(pos, struct md_rdev, same_set);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000218 if (rdev->raid_disk >= 0 &&
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000219 !test_bit(Faulty, &rdev->flags)) {
220 /* this is a usable devices */
221 atomic_inc(&rdev->nr_pending);
222 rcu_read_unlock();
223 return rdev;
224 }
225 }
226 rcu_read_unlock();
227 return NULL;
228}
229
NeilBrownab6085c2007-05-23 13:58:10 -0700230static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrowna654b9d82005-06-21 17:17:27 -0700231{
NeilBrown3cb03002011-10-11 16:45:26 +1100232 struct md_rdev *rdev = NULL;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100233 struct block_device *bdev;
NeilBrownfd01b882011-10-11 16:47:53 +1100234 struct mddev *mddev = bitmap->mddev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700235
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000236 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000237 int size = PAGE_SIZE;
238 loff_t offset = mddev->bitmap_info.offset;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100239
240 bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
241
NeilBrownac2f40b2010-06-01 19:37:31 +1000242 if (page->index == bitmap->file_pages-1)
243 size = roundup(bitmap->last_page_size,
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100244 bdev_logical_block_size(bdev));
NeilBrownac2f40b2010-06-01 19:37:31 +1000245 /* Just make sure we aren't corrupting data or
246 * metadata
247 */
248 if (mddev->external) {
249 /* Bitmap could be anywhere. */
250 if (rdev->sb_start + offset + (page->index
251 * (PAGE_SIZE/512))
252 > rdev->data_offset
253 &&
254 rdev->sb_start + offset
255 < (rdev->data_offset + mddev->dev_sectors
256 + (PAGE_SIZE/512)))
257 goto bad_alignment;
258 } else if (offset < 0) {
259 /* DATA BITMAP METADATA */
260 if (offset
261 + (long)(page->index * (PAGE_SIZE/512))
262 + size/512 > 0)
263 /* bitmap runs in to metadata */
264 goto bad_alignment;
265 if (rdev->data_offset + mddev->dev_sectors
266 > rdev->sb_start + offset)
267 /* data runs in to bitmap */
268 goto bad_alignment;
269 } else if (rdev->sb_start < rdev->data_offset) {
270 /* METADATA BITMAP DATA */
271 if (rdev->sb_start
272 + offset
273 + page->index*(PAGE_SIZE/512) + size/512
274 > rdev->data_offset)
275 /* bitmap runs in to data */
276 goto bad_alignment;
277 } else {
278 /* DATA METADATA BITMAP - no problems */
279 }
280 md_super_write(mddev, rdev,
281 rdev->sb_start + offset
282 + page->index * (PAGE_SIZE/512),
283 size,
284 page);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000285 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700286
287 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800288 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700289 return 0;
NeilBrown4b809912008-07-21 17:05:25 +1000290
291 bad_alignment:
NeilBrown4b809912008-07-21 17:05:25 +1000292 return -EINVAL;
NeilBrowna654b9d82005-06-21 17:17:27 -0700293}
294
NeilBrown4ad13662007-07-17 04:06:13 -0700295static void bitmap_file_kick(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700296/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700297 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700298 */
NeilBrown4ad13662007-07-17 04:06:13 -0700299static void write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700300{
NeilBrownd785a062006-06-26 00:27:48 -0700301 struct buffer_head *bh;
NeilBrown32a76272005-06-21 17:17:14 -0700302
NeilBrownf0d76d72007-07-17 04:06:12 -0700303 if (bitmap->file == NULL) {
304 switch (write_sb_page(bitmap, page, wait)) {
305 case -EINVAL:
306 bitmap->flags |= BITMAP_WRITE_ERROR;
NeilBrownf0d76d72007-07-17 04:06:12 -0700307 }
NeilBrown4ad13662007-07-17 04:06:13 -0700308 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700309
NeilBrown4ad13662007-07-17 04:06:13 -0700310 bh = page_buffers(page);
NeilBrownc7084432006-01-06 00:20:45 -0800311
NeilBrown4ad13662007-07-17 04:06:13 -0700312 while (bh && bh->b_blocknr) {
313 atomic_inc(&bitmap->pending_writes);
314 set_buffer_locked(bh);
315 set_buffer_mapped(bh);
Jens Axboe721a9602011-03-09 11:56:30 +0100316 submit_bh(WRITE | REQ_SYNC, bh);
NeilBrown4ad13662007-07-17 04:06:13 -0700317 bh = bh->b_this_page;
318 }
NeilBrown32a76272005-06-21 17:17:14 -0700319
NeilBrownac2f40b2010-06-01 19:37:31 +1000320 if (wait)
NeilBrown4ad13662007-07-17 04:06:13 -0700321 wait_event(bitmap->write_wait,
322 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700323 }
NeilBrown4ad13662007-07-17 04:06:13 -0700324 if (bitmap->flags & BITMAP_WRITE_ERROR)
325 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700326}
327
NeilBrownd785a062006-06-26 00:27:48 -0700328static void end_bitmap_write(struct buffer_head *bh, int uptodate)
NeilBrown32a76272005-06-21 17:17:14 -0700329{
NeilBrownd785a062006-06-26 00:27:48 -0700330 struct bitmap *bitmap = bh->b_private;
331 unsigned long flags;
332
333 if (!uptodate) {
334 spin_lock_irqsave(&bitmap->lock, flags);
335 bitmap->flags |= BITMAP_WRITE_ERROR;
336 spin_unlock_irqrestore(&bitmap->lock, flags);
337 }
338 if (atomic_dec_and_test(&bitmap->pending_writes))
339 wake_up(&bitmap->write_wait);
340}
341
342/* copied from buffer.c */
343static void
344__clear_page_buffers(struct page *page)
345{
346 ClearPagePrivate(page);
347 set_page_private(page, 0);
348 page_cache_release(page);
349}
350static void free_buffers(struct page *page)
351{
352 struct buffer_head *bh = page_buffers(page);
353
354 while (bh) {
355 struct buffer_head *next = bh->b_this_page;
356 free_buffer_head(bh);
357 bh = next;
358 }
359 __clear_page_buffers(page);
360 put_page(page);
361}
362
363/* read a page from a file.
364 * We both read the page, and attach buffers to the page to record the
365 * address of each block (using bmap). These addresses will be used
366 * to write the block later, completely bypassing the filesystem.
367 * This usage is similar to how swap files are handled, and allows us
368 * to write to a file with no concerns of memory allocation failing.
369 */
370static struct page *read_page(struct file *file, unsigned long index,
371 struct bitmap *bitmap,
372 unsigned long count)
373{
NeilBrown32a76272005-06-21 17:17:14 -0700374 struct page *page = NULL;
Josef Sipekc649bb92006-12-08 02:37:19 -0800375 struct inode *inode = file->f_path.dentry->d_inode;
NeilBrownd785a062006-06-26 00:27:48 -0700376 struct buffer_head *bh;
377 sector_t block;
NeilBrown32a76272005-06-21 17:17:14 -0700378
NeilBrown36a4e1f2011-10-07 14:23:17 +1100379 pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
380 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700381
NeilBrownd785a062006-06-26 00:27:48 -0700382 page = alloc_page(GFP_KERNEL);
383 if (!page)
384 page = ERR_PTR(-ENOMEM);
NeilBrown32a76272005-06-21 17:17:14 -0700385 if (IS_ERR(page))
386 goto out;
NeilBrownd785a062006-06-26 00:27:48 -0700387
NeilBrownd785a062006-06-26 00:27:48 -0700388 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
389 if (!bh) {
390 put_page(page);
391 page = ERR_PTR(-ENOMEM);
392 goto out;
393 }
394 attach_page_buffers(page, bh);
395 block = index << (PAGE_SHIFT - inode->i_blkbits);
396 while (bh) {
397 if (count == 0)
398 bh->b_blocknr = 0;
399 else {
400 bh->b_blocknr = bmap(inode, block);
401 if (bh->b_blocknr == 0) {
402 /* Cannot use this file! */
403 free_buffers(page);
404 page = ERR_PTR(-EINVAL);
405 goto out;
406 }
407 bh->b_bdev = inode->i_sb->s_bdev;
408 if (count < (1<<inode->i_blkbits))
409 count = 0;
410 else
411 count -= (1<<inode->i_blkbits);
NeilBrown32a76272005-06-21 17:17:14 -0700412
NeilBrownd785a062006-06-26 00:27:48 -0700413 bh->b_end_io = end_bitmap_write;
414 bh->b_private = bitmap;
NeilBrownce25c312006-06-26 00:27:49 -0700415 atomic_inc(&bitmap->pending_writes);
416 set_buffer_locked(bh);
417 set_buffer_mapped(bh);
418 submit_bh(READ, bh);
NeilBrownd785a062006-06-26 00:27:48 -0700419 }
420 block++;
421 bh = bh->b_this_page;
422 }
NeilBrownd785a062006-06-26 00:27:48 -0700423 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700424
425 wait_event(bitmap->write_wait,
426 atomic_read(&bitmap->pending_writes)==0);
427 if (bitmap->flags & BITMAP_WRITE_ERROR) {
428 free_buffers(page);
429 page = ERR_PTR(-EIO);
430 }
NeilBrown32a76272005-06-21 17:17:14 -0700431out:
432 if (IS_ERR(page))
NeilBrownac2f40b2010-06-01 19:37:31 +1000433 printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800434 (int)PAGE_SIZE,
435 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700436 PTR_ERR(page));
437 return page;
438}
439
440/*
441 * bitmap file superblock operations
442 */
443
444/* update the event counter and sync the superblock to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700445void bitmap_update_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700446{
447 bitmap_super_t *sb;
448 unsigned long flags;
449
450 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
NeilBrown4ad13662007-07-17 04:06:13 -0700451 return;
NeilBrownece5cff2009-12-14 12:49:56 +1100452 if (bitmap->mddev->bitmap_info.external)
453 return;
NeilBrown32a76272005-06-21 17:17:14 -0700454 spin_lock_irqsave(&bitmap->lock, flags);
455 if (!bitmap->sb_page) { /* no superblock */
456 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700457 return;
NeilBrown32a76272005-06-21 17:17:14 -0700458 }
NeilBrown32a76272005-06-21 17:17:14 -0700459 spin_unlock_irqrestore(&bitmap->lock, flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100460 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700461 sb->events = cpu_to_le64(bitmap->mddev->events);
NeilBrown8258c532011-05-11 14:26:30 +1000462 if (bitmap->mddev->events < bitmap->events_cleared)
Neil Browna0da84f2008-06-28 08:31:22 +1000463 /* rocking back to read-only */
464 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown8258c532011-05-11 14:26:30 +1000465 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
466 sb->state = cpu_to_le32(bitmap->flags);
NeilBrown43a70502009-12-14 12:49:55 +1100467 /* Just in case these have been changed via sysfs: */
468 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
469 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
NeilBrownea03aff2006-01-06 00:20:34 -0800470 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700471 write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700472}
473
474/* print out the bitmap file superblock */
475void bitmap_print_sb(struct bitmap *bitmap)
476{
477 bitmap_super_t *sb;
478
479 if (!bitmap || !bitmap->sb_page)
480 return;
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100481 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700482 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700483 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
484 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
485 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700486 *(__u32 *)(sb->uuid+0),
487 *(__u32 *)(sb->uuid+4),
488 *(__u32 *)(sb->uuid+8),
489 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700490 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700491 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700492 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700493 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700494 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
495 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
496 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
497 printk(KERN_DEBUG " sync size: %llu KB\n",
498 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700499 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800500 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700501}
502
Jonathan Brassow9c810752011-06-08 17:59:30 -0500503/*
504 * bitmap_new_disk_sb
505 * @bitmap
506 *
507 * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
508 * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
509 * This function verifies 'bitmap_info' and populates the on-disk bitmap
510 * structure, which is to be written to disk.
511 *
512 * Returns: 0 on success, -Exxx on error
513 */
514static int bitmap_new_disk_sb(struct bitmap *bitmap)
515{
516 bitmap_super_t *sb;
517 unsigned long chunksize, daemon_sleep, write_behind;
518 int err = -EINVAL;
519
520 bitmap->sb_page = alloc_page(GFP_KERNEL);
521 if (IS_ERR(bitmap->sb_page)) {
522 err = PTR_ERR(bitmap->sb_page);
523 bitmap->sb_page = NULL;
524 return err;
525 }
526 bitmap->sb_page->index = 0;
527
528 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
529
530 sb->magic = cpu_to_le32(BITMAP_MAGIC);
531 sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
532
533 chunksize = bitmap->mddev->bitmap_info.chunksize;
534 BUG_ON(!chunksize);
535 if (!is_power_of_2(chunksize)) {
536 kunmap_atomic(sb, KM_USER0);
537 printk(KERN_ERR "bitmap chunksize not a power of 2\n");
538 return -EINVAL;
539 }
540 sb->chunksize = cpu_to_le32(chunksize);
541
542 daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
543 if (!daemon_sleep ||
544 (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
545 printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n");
546 daemon_sleep = 5 * HZ;
547 }
548 sb->daemon_sleep = cpu_to_le32(daemon_sleep);
549 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
550
551 /*
552 * FIXME: write_behind for RAID1. If not specified, what
553 * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
554 */
555 write_behind = bitmap->mddev->bitmap_info.max_write_behind;
556 if (write_behind > COUNTER_MAX)
557 write_behind = COUNTER_MAX / 2;
558 sb->write_behind = cpu_to_le32(write_behind);
559 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
560
561 /* keep the array size field of the bitmap superblock up to date */
562 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
563
564 memcpy(sb->uuid, bitmap->mddev->uuid, 16);
565
566 bitmap->flags |= BITMAP_STALE;
567 sb->state |= cpu_to_le32(BITMAP_STALE);
568 bitmap->events_cleared = bitmap->mddev->events;
569 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
570
571 bitmap->flags |= BITMAP_HOSTENDIAN;
572 sb->version = cpu_to_le32(BITMAP_MAJOR_HOSTENDIAN);
573
574 kunmap_atomic(sb, KM_USER0);
575
576 return 0;
577}
578
NeilBrown32a76272005-06-21 17:17:14 -0700579/* read the superblock from the bitmap file and initialize some bitmap fields */
580static int bitmap_read_sb(struct bitmap *bitmap)
581{
582 char *reason = NULL;
583 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700584 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700585 unsigned long long events;
586 int err = -EINVAL;
587
588 /* page 0 is the superblock, read it... */
NeilBrownf49d5e62007-01-26 00:57:03 -0800589 if (bitmap->file) {
590 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
591 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
592
593 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
594 } else {
NeilBrown42a04b52009-12-14 12:49:53 +1100595 bitmap->sb_page = read_sb_page(bitmap->mddev,
596 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100597 NULL,
598 0, sizeof(bitmap_super_t));
NeilBrowna654b9d82005-06-21 17:17:27 -0700599 }
NeilBrown32a76272005-06-21 17:17:14 -0700600 if (IS_ERR(bitmap->sb_page)) {
601 err = PTR_ERR(bitmap->sb_page);
602 bitmap->sb_page = NULL;
603 return err;
604 }
605
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100606 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700607
NeilBrown32a76272005-06-21 17:17:14 -0700608 chunksize = le32_to_cpu(sb->chunksize);
NeilBrown1b04be92009-12-14 12:49:53 +1100609 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
NeilBrown4b6d2872005-09-09 16:23:47 -0700610 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700611
612 /* verify that the bitmap-specific fields are valid */
613 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
614 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800615 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
616 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700617 reason = "unrecognized superblock version";
NeilBrown1187cf02009-03-31 14:27:02 +1100618 else if (chunksize < 512)
NeilBrown7dd5d342006-01-06 00:20:39 -0800619 reason = "bitmap chunksize too small";
Jonathan Brassowd7445402011-06-08 18:01:10 -0500620 else if (!is_power_of_2(chunksize))
NeilBrown32a76272005-06-21 17:17:14 -0700621 reason = "bitmap chunksize not a power of 2";
NeilBrown1b04be92009-12-14 12:49:53 +1100622 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
NeilBrown7dd5d342006-01-06 00:20:39 -0800623 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700624 else if (write_behind > COUNTER_MAX)
625 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700626 if (reason) {
627 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
628 bmname(bitmap), reason);
629 goto out;
630 }
631
632 /* keep the array size field of the bitmap superblock up to date */
633 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
634
635 if (!bitmap->mddev->persistent)
636 goto success;
637
638 /*
639 * if we have a persistent array superblock, compare the
640 * bitmap's UUID and event counter to the mddev's
641 */
642 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
643 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
644 bmname(bitmap));
645 goto out;
646 }
647 events = le64_to_cpu(sb->events);
648 if (events < bitmap->mddev->events) {
649 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
650 "-- forcing full recovery\n", bmname(bitmap), events,
651 (unsigned long long) bitmap->mddev->events);
NeilBrown4f2e6392006-10-21 10:24:09 -0700652 sb->state |= cpu_to_le32(BITMAP_STALE);
NeilBrown32a76272005-06-21 17:17:14 -0700653 }
654success:
655 /* assign fields using values from superblock */
NeilBrown42a04b52009-12-14 12:49:53 +1100656 bitmap->mddev->bitmap_info.chunksize = chunksize;
657 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
NeilBrown42a04b52009-12-14 12:49:53 +1100658 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
NeilBrown4f2e6392006-10-21 10:24:09 -0700659 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800660 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
661 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700662 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown8258c532011-05-11 14:26:30 +1000663 if (bitmap->flags & BITMAP_STALE)
NeilBrown6a079972005-09-09 16:23:44 -0700664 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700665 err = 0;
666out:
NeilBrownea03aff2006-01-06 00:20:34 -0800667 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700668 if (err)
669 bitmap_print_sb(bitmap);
670 return err;
671}
672
673enum bitmap_mask_op {
674 MASK_SET,
675 MASK_UNSET
676};
677
NeilBrown4ad13662007-07-17 04:06:13 -0700678/* record the state of the bitmap in the superblock. Return the old value */
679static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
680 enum bitmap_mask_op op)
NeilBrown32a76272005-06-21 17:17:14 -0700681{
682 bitmap_super_t *sb;
683 unsigned long flags;
NeilBrown4ad13662007-07-17 04:06:13 -0700684 int old;
NeilBrown32a76272005-06-21 17:17:14 -0700685
686 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800687 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700688 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700689 return 0;
NeilBrown32a76272005-06-21 17:17:14 -0700690 }
NeilBrown32a76272005-06-21 17:17:14 -0700691 spin_unlock_irqrestore(&bitmap->lock, flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100692 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700693 old = le32_to_cpu(sb->state) & bits;
NeilBrown32a76272005-06-21 17:17:14 -0700694 switch (op) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000695 case MASK_SET:
696 sb->state |= cpu_to_le32(bits);
NeilBrown8258c532011-05-11 14:26:30 +1000697 bitmap->flags |= bits;
NeilBrownac2f40b2010-06-01 19:37:31 +1000698 break;
699 case MASK_UNSET:
700 sb->state &= cpu_to_le32(~bits);
NeilBrown8258c532011-05-11 14:26:30 +1000701 bitmap->flags &= ~bits;
NeilBrownac2f40b2010-06-01 19:37:31 +1000702 break;
703 default:
704 BUG();
NeilBrown32a76272005-06-21 17:17:14 -0700705 }
NeilBrownea03aff2006-01-06 00:20:34 -0800706 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700707 return old;
NeilBrown32a76272005-06-21 17:17:14 -0700708}
709
710/*
711 * general bitmap file operations
712 */
713
NeilBrownece5cff2009-12-14 12:49:56 +1100714/*
715 * on-disk bitmap:
716 *
717 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
718 * file a page at a time. There's a superblock at the start of the file.
719 */
NeilBrown32a76272005-06-21 17:17:14 -0700720/* calculate the index of the page that contains this bit */
NeilBrownece5cff2009-12-14 12:49:56 +1100721static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700722{
NeilBrownece5cff2009-12-14 12:49:56 +1100723 if (!bitmap->mddev->bitmap_info.external)
724 chunk += sizeof(bitmap_super_t) << 3;
725 return chunk >> PAGE_BIT_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700726}
727
728/* calculate the (bit) offset of this bit within a page */
NeilBrownece5cff2009-12-14 12:49:56 +1100729static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700730{
NeilBrownece5cff2009-12-14 12:49:56 +1100731 if (!bitmap->mddev->bitmap_info.external)
732 chunk += sizeof(bitmap_super_t) << 3;
733 return chunk & (PAGE_BITS - 1);
NeilBrown32a76272005-06-21 17:17:14 -0700734}
735
736/*
737 * return a pointer to the page in the filemap that contains the given bit
738 *
739 * this lookup is complicated by the fact that the bitmap sb might be exactly
740 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
741 * 0 or page 1
742 */
743static inline struct page *filemap_get_page(struct bitmap *bitmap,
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000744 unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700745{
NeilBrownac2f40b2010-06-01 19:37:31 +1000746 if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
747 return NULL;
NeilBrownece5cff2009-12-14 12:49:56 +1100748 return bitmap->filemap[file_page_index(bitmap, chunk)
749 - file_page_index(bitmap, 0)];
NeilBrown32a76272005-06-21 17:17:14 -0700750}
751
NeilBrown32a76272005-06-21 17:17:14 -0700752static void bitmap_file_unmap(struct bitmap *bitmap)
753{
754 struct page **map, *sb_page;
755 unsigned long *attr;
756 int pages;
757 unsigned long flags;
758
759 spin_lock_irqsave(&bitmap->lock, flags);
760 map = bitmap->filemap;
761 bitmap->filemap = NULL;
762 attr = bitmap->filemap_attr;
763 bitmap->filemap_attr = NULL;
764 pages = bitmap->file_pages;
765 bitmap->file_pages = 0;
766 sb_page = bitmap->sb_page;
767 bitmap->sb_page = NULL;
768 spin_unlock_irqrestore(&bitmap->lock, flags);
769
770 while (pages--)
NeilBrownece5cff2009-12-14 12:49:56 +1100771 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700772 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700773 kfree(map);
774 kfree(attr);
775
NeilBrownd785a062006-06-26 00:27:48 -0700776 if (sb_page)
777 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700778}
779
780static void bitmap_file_put(struct bitmap *bitmap)
781{
782 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700783 unsigned long flags;
784
785 spin_lock_irqsave(&bitmap->lock, flags);
786 file = bitmap->file;
787 bitmap->file = NULL;
788 spin_unlock_irqrestore(&bitmap->lock, flags);
789
NeilBrownd785a062006-06-26 00:27:48 -0700790 if (file)
791 wait_event(bitmap->write_wait,
792 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700793 bitmap_file_unmap(bitmap);
794
NeilBrownd785a062006-06-26 00:27:48 -0700795 if (file) {
Josef Sipekc649bb92006-12-08 02:37:19 -0800796 struct inode *inode = file->f_path.dentry->d_inode;
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800797 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700798 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700799 }
NeilBrown32a76272005-06-21 17:17:14 -0700800}
801
NeilBrown32a76272005-06-21 17:17:14 -0700802/*
803 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
804 * then it is no longer reliable, so we stop using it and we mark the file
805 * as failed in the superblock
806 */
807static void bitmap_file_kick(struct bitmap *bitmap)
808{
809 char *path, *ptr = NULL;
810
NeilBrown4ad13662007-07-17 04:06:13 -0700811 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
812 bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700813
NeilBrown4ad13662007-07-17 04:06:13 -0700814 if (bitmap->file) {
815 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
816 if (path)
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700817 ptr = d_path(&bitmap->file->f_path, path,
818 PAGE_SIZE);
819
NeilBrown4ad13662007-07-17 04:06:13 -0700820 printk(KERN_ALERT
821 "%s: kicking failed bitmap file %s from array!\n",
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700822 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700823
NeilBrown4ad13662007-07-17 04:06:13 -0700824 kfree(path);
825 } else
826 printk(KERN_ALERT
827 "%s: disabling internal bitmap due to errors\n",
828 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700829 }
NeilBrown32a76272005-06-21 17:17:14 -0700830
831 bitmap_file_put(bitmap);
832
833 return;
834}
835
836enum bitmap_page_attr {
NeilBrownac2f40b2010-06-01 19:37:31 +1000837 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
NeilBrown5a537df2011-09-21 15:37:46 +1000838 BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
839 * i.e. counter is 1 or 2. */
NeilBrownac2f40b2010-06-01 19:37:31 +1000840 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
NeilBrown32a76272005-06-21 17:17:14 -0700841};
842
843static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
844 enum bitmap_page_attr attr)
845{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000846 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700847}
848
849static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
850 enum bitmap_page_attr attr)
851{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000852 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700853}
854
NeilBrownec7a3192006-06-26 00:27:45 -0700855static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
856 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700857{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000858 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700859}
860
861/*
862 * bitmap_file_set_bit -- called before performing a write to the md device
863 * to set (and eventually sync) a particular bit in the bitmap file
864 *
865 * we set the bit immediately, then we record the page number so that
866 * when an unplug occurs, we can flush the dirty pages out to disk
867 */
868static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
869{
870 unsigned long bit;
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000871 struct page *page;
NeilBrown32a76272005-06-21 17:17:14 -0700872 void *kaddr;
873 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
874
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000875 if (!bitmap->filemap)
876 return;
NeilBrown32a76272005-06-21 17:17:14 -0700877
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000878 page = filemap_get_page(bitmap, chunk);
879 if (!page)
880 return;
881 bit = file_page_offset(bitmap, chunk);
NeilBrown32a76272005-06-21 17:17:14 -0700882
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000883 /* set the bit */
884 kaddr = kmap_atomic(page, KM_USER0);
885 if (bitmap->flags & BITMAP_HOSTENDIAN)
886 set_bit(bit, kaddr);
887 else
888 __set_bit_le(bit, kaddr);
889 kunmap_atomic(kaddr, KM_USER0);
NeilBrown36a4e1f2011-10-07 14:23:17 +1100890 pr_debug("set file bit %lu page %lu\n", bit, page->index);
NeilBrown32a76272005-06-21 17:17:14 -0700891 /* record page number so it gets flushed to disk when unplug occurs */
892 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
NeilBrown32a76272005-06-21 17:17:14 -0700893}
894
895/* this gets called when the md device is ready to unplug its underlying
896 * (slave) device queues -- before we let any writes go down, we need to
897 * sync the dirty pages of the bitmap file to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700898void bitmap_unplug(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700899{
NeilBrownec7a3192006-06-26 00:27:45 -0700900 unsigned long i, flags;
901 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700902 struct page *page;
903 int wait = 0;
904
905 if (!bitmap)
NeilBrown4ad13662007-07-17 04:06:13 -0700906 return;
NeilBrown32a76272005-06-21 17:17:14 -0700907
908 /* look at each page to see if there are any set bits that need to be
909 * flushed out to disk */
910 for (i = 0; i < bitmap->file_pages; i++) {
911 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700912 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700913 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700914 return;
NeilBrown32a76272005-06-21 17:17:14 -0700915 }
916 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700917 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
918 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700919 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
920 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700921 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700922 wait = 1;
923 spin_unlock_irqrestore(&bitmap->lock, flags);
924
NeilBrownac2f40b2010-06-01 19:37:31 +1000925 if (dirty || need_write)
NeilBrown4ad13662007-07-17 04:06:13 -0700926 write_page(bitmap, page, 0);
NeilBrown32a76272005-06-21 17:17:14 -0700927 }
928 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700929 if (bitmap->file)
NeilBrownd785a062006-06-26 00:27:48 -0700930 wait_event(bitmap->write_wait,
931 atomic_read(&bitmap->pending_writes)==0);
NeilBrown0b79ccf2006-06-26 00:27:44 -0700932 else
NeilBrowna9701a32005-11-08 21:39:34 -0800933 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700934 }
NeilBrownd785a062006-06-26 00:27:48 -0700935 if (bitmap->flags & BITMAP_WRITE_ERROR)
936 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700937}
NeilBrownac2f40b2010-06-01 19:37:31 +1000938EXPORT_SYMBOL(bitmap_unplug);
NeilBrown32a76272005-06-21 17:17:14 -0700939
NeilBrown6a079972005-09-09 16:23:44 -0700940static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700941/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
942 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
943 * memory mapping of the bitmap file
944 * Special cases:
945 * if there's no bitmap file, or if the bitmap file had been
946 * previously kicked from the array, we mark all the bits as
947 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700948 *
949 * We ignore all bits for sectors that end earlier than 'start'.
950 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700951 */
NeilBrown6a079972005-09-09 16:23:44 -0700952static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700953{
954 unsigned long i, chunks, index, oldindex, bit;
955 struct page *page = NULL, *oldpage = NULL;
956 unsigned long num_pages, bit_cnt = 0;
957 struct file *file;
NeilBrownd785a062006-06-26 00:27:48 -0700958 unsigned long bytes, offset;
NeilBrown32a76272005-06-21 17:17:14 -0700959 int outofdate;
960 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800961 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700962
963 chunks = bitmap->chunks;
964 file = bitmap->file;
965
NeilBrown42a04b52009-12-14 12:49:53 +1100966 BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
NeilBrown32a76272005-06-21 17:17:14 -0700967
NeilBrown32a76272005-06-21 17:17:14 -0700968 outofdate = bitmap->flags & BITMAP_STALE;
NeilBrown32a76272005-06-21 17:17:14 -0700969 if (outofdate)
970 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
971 "recovery\n", bmname(bitmap));
972
NeilBrowne384e582010-06-01 19:37:34 +1000973 bytes = DIV_ROUND_UP(bitmap->chunks, 8);
NeilBrownece5cff2009-12-14 12:49:56 +1100974 if (!bitmap->mddev->bitmap_info.external)
975 bytes += sizeof(bitmap_super_t);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700976
NeilBrowne384e582010-06-01 19:37:34 +1000977 num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700978
NeilBrownece5cff2009-12-14 12:49:56 +1100979 if (file && i_size_read(file->f_mapping->host) < bytes) {
NeilBrown32a76272005-06-21 17:17:14 -0700980 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
981 bmname(bitmap),
982 (unsigned long) i_size_read(file->f_mapping->host),
NeilBrownece5cff2009-12-14 12:49:56 +1100983 bytes);
NeilBrown4ad13662007-07-17 04:06:13 -0700984 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700985 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700986
987 ret = -ENOMEM;
988
NeilBrown32a76272005-06-21 17:17:14 -0700989 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700990 if (!bitmap->filemap)
NeilBrown4ad13662007-07-17 04:06:13 -0700991 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700992
NeilBrowne16b68b2006-06-26 00:27:45 -0700993 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
994 bitmap->filemap_attr = kzalloc(
NeilBrownac2f40b2010-06-01 19:37:31 +1000995 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
NeilBrowne16b68b2006-06-26 00:27:45 -0700996 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -0700997 if (!bitmap->filemap_attr)
NeilBrown4ad13662007-07-17 04:06:13 -0700998 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700999
NeilBrown32a76272005-06-21 17:17:14 -07001000 oldindex = ~0L;
1001
1002 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -08001003 int b;
NeilBrownece5cff2009-12-14 12:49:56 +11001004 index = file_page_index(bitmap, i);
1005 bit = file_page_offset(bitmap, i);
NeilBrown32a76272005-06-21 17:17:14 -07001006 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -07001007 int count;
NeilBrown32a76272005-06-21 17:17:14 -07001008 /* unmap the old page, we're done with it */
NeilBrownd785a062006-06-26 00:27:48 -07001009 if (index == num_pages-1)
NeilBrownece5cff2009-12-14 12:49:56 +11001010 count = bytes - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -07001011 else
1012 count = PAGE_SIZE;
NeilBrownece5cff2009-12-14 12:49:56 +11001013 if (index == 0 && bitmap->sb_page) {
NeilBrown32a76272005-06-21 17:17:14 -07001014 /*
1015 * if we're here then the superblock page
1016 * contains some bits (PAGE_SIZE != sizeof sb)
1017 * we've already read it in, so just use it
1018 */
1019 page = bitmap->sb_page;
1020 offset = sizeof(bitmap_super_t);
NeilBrown53845272009-01-09 08:31:05 +11001021 if (!file)
Vasiliy Kulikov5c04f552010-10-01 14:18:12 -07001022 page = read_sb_page(
1023 bitmap->mddev,
1024 bitmap->mddev->bitmap_info.offset,
1025 page,
1026 index, count);
NeilBrowna654b9d82005-06-21 17:17:27 -07001027 } else if (file) {
NeilBrownd785a062006-06-26 00:27:48 -07001028 page = read_page(file, index, bitmap, count);
NeilBrowna654b9d82005-06-21 17:17:27 -07001029 offset = 0;
1030 } else {
NeilBrown42a04b52009-12-14 12:49:53 +11001031 page = read_sb_page(bitmap->mddev,
1032 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +11001033 NULL,
1034 index, count);
NeilBrown32a76272005-06-21 17:17:14 -07001035 offset = 0;
1036 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001037 if (IS_ERR(page)) { /* read error */
1038 ret = PTR_ERR(page);
NeilBrown4ad13662007-07-17 04:06:13 -07001039 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -07001040 }
1041
NeilBrown32a76272005-06-21 17:17:14 -07001042 oldindex = index;
1043 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -07001044
NeilBrownb74fd282009-05-07 12:47:19 +10001045 bitmap->filemap[bitmap->file_pages++] = page;
1046 bitmap->last_page_size = count;
1047
NeilBrown32a76272005-06-21 17:17:14 -07001048 if (outofdate) {
1049 /*
1050 * if bitmap is out of date, dirty the
NeilBrownac2f40b2010-06-01 19:37:31 +10001051 * whole page and write it out
NeilBrown32a76272005-06-21 17:17:14 -07001052 */
NeilBrownea03aff2006-01-06 00:20:34 -08001053 paddr = kmap_atomic(page, KM_USER0);
1054 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -07001055 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -08001056 kunmap_atomic(paddr, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -07001057 write_page(bitmap, page, 1);
1058
1059 ret = -EIO;
NeilBrownb74fd282009-05-07 12:47:19 +10001060 if (bitmap->flags & BITMAP_WRITE_ERROR)
NeilBrown4ad13662007-07-17 04:06:13 -07001061 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001062 }
NeilBrown32a76272005-06-21 17:17:14 -07001063 }
NeilBrownea03aff2006-01-06 00:20:34 -08001064 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001065 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001066 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001067 else
Akinobu Mita6b33aff2011-03-23 16:42:13 -07001068 b = test_bit_le(bit, paddr);
NeilBrownea03aff2006-01-06 00:20:34 -08001069 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001070 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -07001071 /* if the disk bit is set, set the memory bit */
NeilBrowndb305e52009-05-07 12:49:06 +10001072 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1073 >= start);
1074 bitmap_set_memory_bits(bitmap,
1075 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1076 needed);
NeilBrown32a76272005-06-21 17:17:14 -07001077 bit_cnt++;
1078 }
NeilBrown32a76272005-06-21 17:17:14 -07001079 }
1080
NeilBrownac2f40b2010-06-01 19:37:31 +10001081 /* everything went OK */
NeilBrown32a76272005-06-21 17:17:14 -07001082 ret = 0;
1083 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1084
NeilBrown32a76272005-06-21 17:17:14 -07001085 if (bit_cnt) { /* Kick recovery if any bits were set */
1086 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1087 md_wakeup_thread(bitmap->mddev->thread);
1088 }
1089
NeilBrown32a76272005-06-21 17:17:14 -07001090 printk(KERN_INFO "%s: bitmap initialized from disk: "
Jonathan Brassow9c810752011-06-08 17:59:30 -05001091 "read %lu/%lu pages, set %lu of %lu bits\n",
1092 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, chunks);
NeilBrown32a76272005-06-21 17:17:14 -07001093
NeilBrown4ad13662007-07-17 04:06:13 -07001094 return 0;
1095
1096 err:
1097 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1098 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001099 return ret;
1100}
1101
NeilBrowna654b9d82005-06-21 17:17:27 -07001102void bitmap_write_all(struct bitmap *bitmap)
1103{
1104 /* We don't actually write all bitmap blocks here,
1105 * just flag them as needing to be written
1106 */
NeilBrownec7a3192006-06-26 00:27:45 -07001107 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001108
NeilBrown7c8f4242011-11-23 10:18:52 +11001109 spin_lock_irq(&bitmap->lock);
NeilBrownac2f40b2010-06-01 19:37:31 +10001110 for (i = 0; i < bitmap->file_pages; i++)
NeilBrownec7a3192006-06-26 00:27:45 -07001111 set_page_attr(bitmap, bitmap->filemap[i],
1112 BITMAP_PAGE_NEEDWRITE);
NeilBrown2585f3e2011-09-21 15:37:46 +10001113 bitmap->allclean = 0;
NeilBrown7c8f4242011-11-23 10:18:52 +11001114 spin_unlock_irq(&bitmap->lock);
NeilBrowna654b9d82005-06-21 17:17:27 -07001115}
1116
NeilBrown32a76272005-06-21 17:17:14 -07001117static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1118{
1119 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1120 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1121 bitmap->bp[page].count += inc;
NeilBrown32a76272005-06-21 17:17:14 -07001122 bitmap_checkfree(bitmap, page);
1123}
1124static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001125 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001126 int create);
1127
1128/*
1129 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1130 * out to disk
1131 */
1132
NeilBrownfd01b882011-10-11 16:47:53 +11001133void bitmap_daemon_work(struct mddev *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001134{
NeilBrownaa5cbd12009-12-14 12:49:46 +11001135 struct bitmap *bitmap;
NeilBrownaa3163f2005-06-21 17:17:22 -07001136 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001137 unsigned long flags;
1138 struct page *page = NULL, *lastpage = NULL;
NeilBrown57dab0b2010-10-19 10:03:39 +11001139 sector_t blocks;
NeilBrownea03aff2006-01-06 00:20:34 -08001140 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001141
NeilBrownaa5cbd12009-12-14 12:49:46 +11001142 /* Use a mutex to guard daemon_work against
1143 * bitmap_destroy.
1144 */
NeilBrownc3d97142009-12-14 12:49:52 +11001145 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrownaa5cbd12009-12-14 12:49:46 +11001146 bitmap = mddev->bitmap;
1147 if (bitmap == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001148 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown4ad13662007-07-17 04:06:13 -07001149 return;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001150 }
NeilBrown42a04b52009-12-14 12:49:53 +11001151 if (time_before(jiffies, bitmap->daemon_lastrun
NeilBrown2e61ebb2011-12-23 10:17:50 +11001152 + mddev->bitmap_info.daemon_sleep))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001153 goto done;
1154
NeilBrown32a76272005-06-21 17:17:14 -07001155 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001156 if (bitmap->allclean) {
NeilBrown2e61ebb2011-12-23 10:17:50 +11001157 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001158 goto done;
NeilBrown8311c292008-03-04 14:29:30 -08001159 }
1160 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001161
NeilBrownbe512692009-05-26 09:41:17 +10001162 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001163 for (j = 0; j < bitmap->chunks; j++) {
1164 bitmap_counter_t *bmc;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001165 if (!bitmap->filemap)
1166 /* error or shutdown */
1167 break;
1168
1169 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001170
1171 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001172 /* skip this page unless it's marked as needing cleaning */
NeilBrown5a537df2011-09-21 15:37:46 +10001173 if (!test_page_attr(bitmap, page, BITMAP_PAGE_PENDING)) {
NeilBrownec7a3192006-06-26 00:27:45 -07001174 int need_write = test_page_attr(bitmap, page,
1175 BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001176 if (need_write)
NeilBrownaa3163f2005-06-21 17:17:22 -07001177 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001178
NeilBrownaa3163f2005-06-21 17:17:22 -07001179 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown2585f3e2011-09-21 15:37:46 +10001180 if (need_write)
NeilBrown4ad13662007-07-17 04:06:13 -07001181 write_page(bitmap, page, 0);
NeilBrownbe512692009-05-26 09:41:17 +10001182 spin_lock_irqsave(&bitmap->lock, flags);
1183 j |= (PAGE_BITS - 1);
NeilBrownaa3163f2005-06-21 17:17:22 -07001184 continue;
1185 }
1186
NeilBrown32a76272005-06-21 17:17:14 -07001187 /* grab the new page, sync and release the old */
NeilBrown32a76272005-06-21 17:17:14 -07001188 if (lastpage != NULL) {
NeilBrown2585f3e2011-09-21 15:37:46 +10001189 if (test_page_attr(bitmap, lastpage,
1190 BITMAP_PAGE_NEEDWRITE)) {
1191 clear_page_attr(bitmap, lastpage,
1192 BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -07001193 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001194 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001195 } else {
NeilBrown2585f3e2011-09-21 15:37:46 +10001196 set_page_attr(bitmap, lastpage,
1197 BITMAP_PAGE_NEEDWRITE);
1198 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001199 spin_unlock_irqrestore(&bitmap->lock, flags);
1200 }
NeilBrown32a76272005-06-21 17:17:14 -07001201 } else
1202 spin_unlock_irqrestore(&bitmap->lock, flags);
1203 lastpage = page;
Neil Browna0da84f2008-06-28 08:31:22 +10001204
1205 /* We are possibly going to clear some bits, so make
1206 * sure that events_cleared is up-to-date.
1207 */
NeilBrownece5cff2009-12-14 12:49:56 +11001208 if (bitmap->need_sync &&
NeilBrown2e61ebb2011-12-23 10:17:50 +11001209 mddev->bitmap_info.external == 0) {
Neil Browna0da84f2008-06-28 08:31:22 +10001210 bitmap_super_t *sb;
1211 bitmap->need_sync = 0;
1212 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1213 sb->events_cleared =
1214 cpu_to_le64(bitmap->events_cleared);
1215 kunmap_atomic(sb, KM_USER0);
1216 write_page(bitmap, bitmap->sb_page, 1);
1217 }
NeilBrown32a76272005-06-21 17:17:14 -07001218 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownece5cff2009-12-14 12:49:56 +11001219 if (!bitmap->need_sync)
NeilBrown5a537df2011-09-21 15:37:46 +10001220 clear_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001221 else
1222 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001223 }
NeilBrowndb305e52009-05-07 12:49:06 +10001224 bmc = bitmap_get_counter(bitmap,
1225 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1226 &blocks, 0);
NeilBrown5a537df2011-09-21 15:37:46 +10001227 if (!bmc)
1228 j |= PAGE_COUNTER_MASK;
1229 else if (*bmc) {
NeilBrown5a537df2011-09-21 15:37:46 +10001230 if (*bmc == 1 && !bitmap->need_sync) {
NeilBrown32a76272005-06-21 17:17:14 -07001231 /* we can clear the bit */
1232 *bmc = 0;
NeilBrowndb305e52009-05-07 12:49:06 +10001233 bitmap_count_page(bitmap,
1234 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
NeilBrown32a76272005-06-21 17:17:14 -07001235 -1);
1236
1237 /* clear the bit */
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001238 paddr = kmap_atomic(page, KM_USER0);
1239 if (bitmap->flags & BITMAP_HOSTENDIAN)
1240 clear_bit(file_page_offset(bitmap, j),
1241 paddr);
1242 else
1243 __clear_bit_le(
NeilBrown5a537df2011-09-21 15:37:46 +10001244 file_page_offset(bitmap,
1245 j),
1246 paddr);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001247 kunmap_atomic(paddr, KM_USER0);
NeilBrown5a537df2011-09-21 15:37:46 +10001248 } else if (*bmc <= 2) {
1249 *bmc = 1; /* maybe clear the bit next time */
1250 set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001251 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001252 }
NeilBrown5a537df2011-09-21 15:37:46 +10001253 }
NeilBrown32a76272005-06-21 17:17:14 -07001254 }
NeilBrownbe512692009-05-26 09:41:17 +10001255 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001256
1257 /* now sync the final page */
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001258 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001259 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001260 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001261 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1262 spin_unlock_irqrestore(&bitmap->lock, flags);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001263 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001264 } else {
1265 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
NeilBrown2585f3e2011-09-21 15:37:46 +10001266 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001267 spin_unlock_irqrestore(&bitmap->lock, flags);
1268 }
NeilBrown32a76272005-06-21 17:17:14 -07001269 }
1270
NeilBrown7be3dfe2008-03-10 11:43:48 -07001271 done:
NeilBrown8311c292008-03-04 14:29:30 -08001272 if (bitmap->allclean == 0)
NeilBrown2e61ebb2011-12-23 10:17:50 +11001273 mddev->thread->timeout =
1274 mddev->bitmap_info.daemon_sleep;
NeilBrownc3d97142009-12-14 12:49:52 +11001275 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown32a76272005-06-21 17:17:14 -07001276}
1277
NeilBrown32a76272005-06-21 17:17:14 -07001278static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001279 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001280 int create)
NeilBrownee305ac2009-09-23 18:06:44 +10001281__releases(bitmap->lock)
1282__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -07001283{
1284 /* If 'create', we might release the lock and reclaim it.
1285 * The lock must have been taken with interrupts enabled.
1286 * If !create, we don't release the lock.
1287 */
1288 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1289 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1290 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1291 sector_t csize;
NeilBrownef425672010-06-01 19:37:33 +10001292 int err;
NeilBrown32a76272005-06-21 17:17:14 -07001293
NeilBrownef425672010-06-01 19:37:33 +10001294 err = bitmap_checkpage(bitmap, page, create);
1295
1296 if (bitmap->bp[page].hijacked ||
1297 bitmap->bp[page].map == NULL)
1298 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1299 PAGE_COUNTER_SHIFT - 1);
1300 else
NeilBrown32a76272005-06-21 17:17:14 -07001301 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
NeilBrownef425672010-06-01 19:37:33 +10001302 *blocks = csize - (offset & (csize - 1));
1303
1304 if (err < 0)
NeilBrown32a76272005-06-21 17:17:14 -07001305 return NULL;
NeilBrownef425672010-06-01 19:37:33 +10001306
NeilBrown32a76272005-06-21 17:17:14 -07001307 /* now locked ... */
1308
1309 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1310 /* should we use the first or second counter field
1311 * of the hijacked pointer? */
1312 int hi = (pageoff > PAGE_COUNTER_MASK);
NeilBrown32a76272005-06-21 17:17:14 -07001313 return &((bitmap_counter_t *)
1314 &bitmap->bp[page].map)[hi];
NeilBrownef425672010-06-01 19:37:33 +10001315 } else /* page is allocated */
NeilBrown32a76272005-06-21 17:17:14 -07001316 return (bitmap_counter_t *)
1317 &(bitmap->bp[page].map[pageoff]);
NeilBrown32a76272005-06-21 17:17:14 -07001318}
1319
NeilBrown4b6d2872005-09-09 16:23:47 -07001320int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001321{
NeilBrownac2f40b2010-06-01 19:37:31 +10001322 if (!bitmap)
1323 return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001324
1325 if (behind) {
Paul Clements696fcd52010-03-08 16:02:37 +11001326 int bw;
NeilBrown4b6d2872005-09-09 16:23:47 -07001327 atomic_inc(&bitmap->behind_writes);
Paul Clements696fcd52010-03-08 16:02:37 +11001328 bw = atomic_read(&bitmap->behind_writes);
1329 if (bw > bitmap->behind_writes_used)
1330 bitmap->behind_writes_used = bw;
1331
NeilBrown36a4e1f2011-10-07 14:23:17 +11001332 pr_debug("inc write-behind count %d/%lu\n",
1333 bw, bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001334 }
1335
NeilBrown32a76272005-06-21 17:17:14 -07001336 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001337 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001338 bitmap_counter_t *bmc;
1339
1340 spin_lock_irq(&bitmap->lock);
1341 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1342 if (!bmc) {
1343 spin_unlock_irq(&bitmap->lock);
1344 return 0;
1345 }
1346
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001347 if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
Neil Brownda6e1a32007-02-08 14:20:37 -08001348 DEFINE_WAIT(__wait);
1349 /* note that it is safe to do the prepare_to_wait
1350 * after the test as long as we do it before dropping
1351 * the spinlock.
1352 */
1353 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1354 TASK_UNINTERRUPTIBLE);
1355 spin_unlock_irq(&bitmap->lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001356 io_schedule();
Neil Brownda6e1a32007-02-08 14:20:37 -08001357 finish_wait(&bitmap->overflow_wait, &__wait);
1358 continue;
1359 }
1360
NeilBrownac2f40b2010-06-01 19:37:31 +10001361 switch (*bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001362 case 0:
1363 bitmap_file_set_bit(bitmap, offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001364 bitmap_count_page(bitmap, offset, 1);
NeilBrown32a76272005-06-21 17:17:14 -07001365 /* fall through */
1366 case 1:
1367 *bmc = 2;
1368 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001369
NeilBrown32a76272005-06-21 17:17:14 -07001370 (*bmc)++;
1371
1372 spin_unlock_irq(&bitmap->lock);
1373
1374 offset += blocks;
1375 if (sectors > blocks)
1376 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001377 else
1378 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001379 }
1380 return 0;
1381}
NeilBrownac2f40b2010-06-01 19:37:31 +10001382EXPORT_SYMBOL(bitmap_startwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001383
1384void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001385 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001386{
NeilBrownac2f40b2010-06-01 19:37:31 +10001387 if (!bitmap)
1388 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001389 if (behind) {
NeilBrowne5551902010-03-31 11:21:44 +11001390 if (atomic_dec_and_test(&bitmap->behind_writes))
1391 wake_up(&bitmap->behind_wait);
NeilBrown36a4e1f2011-10-07 14:23:17 +11001392 pr_debug("dec write-behind count %d/%lu\n",
1393 atomic_read(&bitmap->behind_writes),
1394 bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001395 }
1396
NeilBrown32a76272005-06-21 17:17:14 -07001397 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001398 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001399 unsigned long flags;
1400 bitmap_counter_t *bmc;
1401
1402 spin_lock_irqsave(&bitmap->lock, flags);
1403 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1404 if (!bmc) {
1405 spin_unlock_irqrestore(&bitmap->lock, flags);
1406 return;
1407 }
1408
NeilBrown961902c2011-12-23 09:57:48 +11001409 if (success && !bitmap->mddev->degraded &&
Neil Browna0da84f2008-06-28 08:31:22 +10001410 bitmap->events_cleared < bitmap->mddev->events) {
1411 bitmap->events_cleared = bitmap->mddev->events;
1412 bitmap->need_sync = 1;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001413 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
Neil Browna0da84f2008-06-28 08:31:22 +10001414 }
1415
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001416 if (!success && !NEEDED(*bmc))
NeilBrown32a76272005-06-21 17:17:14 -07001417 *bmc |= NEEDED_MASK;
1418
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001419 if (COUNTER(*bmc) == COUNTER_MAX)
Neil Brownda6e1a32007-02-08 14:20:37 -08001420 wake_up(&bitmap->overflow_wait);
1421
NeilBrown32a76272005-06-21 17:17:14 -07001422 (*bmc)--;
NeilBrown2585f3e2011-09-21 15:37:46 +10001423 if (*bmc <= 2) {
NeilBrown32a76272005-06-21 17:17:14 -07001424 set_page_attr(bitmap,
NeilBrowne384e582010-06-01 19:37:34 +10001425 filemap_get_page(
1426 bitmap,
1427 offset >> CHUNK_BLOCK_SHIFT(bitmap)),
NeilBrown5a537df2011-09-21 15:37:46 +10001428 BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001429 bitmap->allclean = 0;
1430 }
NeilBrown32a76272005-06-21 17:17:14 -07001431 spin_unlock_irqrestore(&bitmap->lock, flags);
1432 offset += blocks;
1433 if (sectors > blocks)
1434 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001435 else
1436 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001437 }
1438}
NeilBrownac2f40b2010-06-01 19:37:31 +10001439EXPORT_SYMBOL(bitmap_endwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001440
NeilBrown57dab0b2010-10-19 10:03:39 +11001441static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001442 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001443{
1444 bitmap_counter_t *bmc;
1445 int rv;
1446 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1447 *blocks = 1024;
1448 return 1; /* always resync if no bitmap */
1449 }
1450 spin_lock_irq(&bitmap->lock);
1451 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1452 rv = 0;
1453 if (bmc) {
1454 /* locked */
1455 if (RESYNC(*bmc))
1456 rv = 1;
1457 else if (NEEDED(*bmc)) {
1458 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001459 if (!degraded) { /* don't set/clear bits if degraded */
1460 *bmc |= RESYNC_MASK;
1461 *bmc &= ~NEEDED_MASK;
1462 }
NeilBrown32a76272005-06-21 17:17:14 -07001463 }
1464 }
1465 spin_unlock_irq(&bitmap->lock);
1466 return rv;
1467}
1468
NeilBrown57dab0b2010-10-19 10:03:39 +11001469int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001470 int degraded)
1471{
1472 /* bitmap_start_sync must always report on multiples of whole
1473 * pages, otherwise resync (which is very PAGE_SIZE based) will
1474 * get confused.
1475 * So call __bitmap_start_sync repeatedly (if needed) until
1476 * At least PAGE_SIZE>>9 blocks are covered.
1477 * Return the 'or' of the result.
1478 */
1479 int rv = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001480 sector_t blocks1;
NeilBrown1187cf02009-03-31 14:27:02 +11001481
1482 *blocks = 0;
1483 while (*blocks < (PAGE_SIZE>>9)) {
1484 rv |= __bitmap_start_sync(bitmap, offset,
1485 &blocks1, degraded);
1486 offset += blocks1;
1487 *blocks += blocks1;
1488 }
1489 return rv;
1490}
NeilBrownac2f40b2010-06-01 19:37:31 +10001491EXPORT_SYMBOL(bitmap_start_sync);
NeilBrown1187cf02009-03-31 14:27:02 +11001492
NeilBrown57dab0b2010-10-19 10:03:39 +11001493void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
NeilBrown32a76272005-06-21 17:17:14 -07001494{
1495 bitmap_counter_t *bmc;
1496 unsigned long flags;
NeilBrownac2f40b2010-06-01 19:37:31 +10001497
1498 if (bitmap == NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001499 *blocks = 1024;
1500 return;
1501 }
1502 spin_lock_irqsave(&bitmap->lock, flags);
1503 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1504 if (bmc == NULL)
1505 goto unlock;
1506 /* locked */
NeilBrown32a76272005-06-21 17:17:14 -07001507 if (RESYNC(*bmc)) {
1508 *bmc &= ~RESYNC_MASK;
1509
1510 if (!NEEDED(*bmc) && aborted)
1511 *bmc |= NEEDED_MASK;
1512 else {
NeilBrown2585f3e2011-09-21 15:37:46 +10001513 if (*bmc <= 2) {
NeilBrown32a76272005-06-21 17:17:14 -07001514 set_page_attr(bitmap,
1515 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
NeilBrown5a537df2011-09-21 15:37:46 +10001516 BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001517 bitmap->allclean = 0;
1518 }
NeilBrown32a76272005-06-21 17:17:14 -07001519 }
1520 }
1521 unlock:
1522 spin_unlock_irqrestore(&bitmap->lock, flags);
1523}
NeilBrownac2f40b2010-06-01 19:37:31 +10001524EXPORT_SYMBOL(bitmap_end_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001525
1526void bitmap_close_sync(struct bitmap *bitmap)
1527{
1528 /* Sync has finished, and any bitmap chunks that weren't synced
1529 * properly have been aborted. It remains to us to clear the
1530 * RESYNC bit wherever it is still on
1531 */
1532 sector_t sector = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001533 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001534 if (!bitmap)
1535 return;
NeilBrown32a76272005-06-21 17:17:14 -07001536 while (sector < bitmap->mddev->resync_max_sectors) {
1537 bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001538 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001539 }
1540}
NeilBrownac2f40b2010-06-01 19:37:31 +10001541EXPORT_SYMBOL(bitmap_close_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001542
NeilBrownb47490c2008-02-06 01:39:50 -08001543void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1544{
1545 sector_t s = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001546 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001547
1548 if (!bitmap)
1549 return;
1550 if (sector == 0) {
1551 bitmap->last_end_sync = jiffies;
1552 return;
1553 }
1554 if (time_before(jiffies, (bitmap->last_end_sync
NeilBrown1b04be92009-12-14 12:49:53 +11001555 + bitmap->mddev->bitmap_info.daemon_sleep)))
NeilBrownb47490c2008-02-06 01:39:50 -08001556 return;
1557 wait_event(bitmap->mddev->recovery_wait,
1558 atomic_read(&bitmap->mddev->recovery_active) == 0);
1559
NeilBrown75d3da42011-01-14 09:14:34 +11001560 bitmap->mddev->curr_resync_completed = sector;
NeilBrown070dc6d2010-08-30 17:33:34 +10001561 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
NeilBrownb47490c2008-02-06 01:39:50 -08001562 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1563 s = 0;
1564 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1565 bitmap_end_sync(bitmap, s, &blocks, 0);
1566 s += blocks;
1567 }
1568 bitmap->last_end_sync = jiffies;
NeilBrownacb180b2009-04-14 16:28:34 +10001569 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
NeilBrownb47490c2008-02-06 01:39:50 -08001570}
NeilBrownac2f40b2010-06-01 19:37:31 +10001571EXPORT_SYMBOL(bitmap_cond_end_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08001572
NeilBrown6a079972005-09-09 16:23:44 -07001573static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001574{
1575 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001576 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001577 * be 0 at this point
1578 */
NeilBrown193f1c92005-08-04 12:53:33 -07001579
NeilBrown57dab0b2010-10-19 10:03:39 +11001580 sector_t secs;
NeilBrown193f1c92005-08-04 12:53:33 -07001581 bitmap_counter_t *bmc;
1582 spin_lock_irq(&bitmap->lock);
1583 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1584 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001585 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001586 return;
NeilBrown32a76272005-06-21 17:17:14 -07001587 }
NeilBrownac2f40b2010-06-01 19:37:31 +10001588 if (!*bmc) {
NeilBrown193f1c92005-08-04 12:53:33 -07001589 struct page *page;
NeilBrown915c4202011-12-23 10:17:51 +11001590 *bmc = 2 | (needed ? NEEDED_MASK : 0);
NeilBrown193f1c92005-08-04 12:53:33 -07001591 bitmap_count_page(bitmap, offset, 1);
1592 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
NeilBrown5a537df2011-09-21 15:37:46 +10001593 set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001594 bitmap->allclean = 0;
NeilBrown193f1c92005-08-04 12:53:33 -07001595 }
1596 spin_unlock_irq(&bitmap->lock);
NeilBrown32a76272005-06-21 17:17:14 -07001597}
1598
Paul Clements9b1d1da2006-10-03 01:15:49 -07001599/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1600void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1601{
1602 unsigned long chunk;
1603
1604 for (chunk = s; chunk <= e; chunk++) {
NeilBrowndb305e52009-05-07 12:49:06 +10001605 sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001606 bitmap_set_memory_bits(bitmap, sec, 1);
NeilBrown7c8f4242011-11-23 10:18:52 +11001607 spin_lock_irq(&bitmap->lock);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001608 bitmap_file_set_bit(bitmap, sec);
NeilBrown7c8f4242011-11-23 10:18:52 +11001609 spin_unlock_irq(&bitmap->lock);
NeilBrownffa23322009-12-14 12:49:56 +11001610 if (sec < bitmap->mddev->recovery_cp)
1611 /* We are asserting that the array is dirty,
1612 * so move the recovery_cp address back so
1613 * that it is obvious that it is dirty
1614 */
1615 bitmap->mddev->recovery_cp = sec;
Paul Clements9b1d1da2006-10-03 01:15:49 -07001616 }
1617}
1618
NeilBrown32a76272005-06-21 17:17:14 -07001619/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001620 * flush out any pending updates
1621 */
NeilBrownfd01b882011-10-11 16:47:53 +11001622void bitmap_flush(struct mddev *mddev)
NeilBrown6b8b3e82005-08-04 12:53:35 -07001623{
1624 struct bitmap *bitmap = mddev->bitmap;
NeilBrown42a04b52009-12-14 12:49:53 +11001625 long sleep;
NeilBrown6b8b3e82005-08-04 12:53:35 -07001626
1627 if (!bitmap) /* there was no bitmap */
1628 return;
1629
1630 /* run the daemon_work three time to ensure everything is flushed
1631 * that can be
1632 */
NeilBrown1b04be92009-12-14 12:49:53 +11001633 sleep = mddev->bitmap_info.daemon_sleep * 2;
NeilBrown42a04b52009-12-14 12:49:53 +11001634 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001635 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001636 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001637 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001638 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001639 bitmap_daemon_work(mddev);
NeilBrown6b8b3e82005-08-04 12:53:35 -07001640 bitmap_update_sb(bitmap);
1641}
1642
1643/*
NeilBrown32a76272005-06-21 17:17:14 -07001644 * free memory that was allocated
1645 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001646static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001647{
1648 unsigned long k, pages;
1649 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001650
1651 if (!bitmap) /* there was no bitmap */
1652 return;
1653
NeilBrown32a76272005-06-21 17:17:14 -07001654 /* release the bitmap file and kill the daemon */
1655 bitmap_file_put(bitmap);
1656
1657 bp = bitmap->bp;
1658 pages = bitmap->pages;
1659
1660 /* free all allocated memory */
1661
NeilBrown32a76272005-06-21 17:17:14 -07001662 if (bp) /* deallocate the page memory */
1663 for (k = 0; k < pages; k++)
1664 if (bp[k].map && !bp[k].hijacked)
1665 kfree(bp[k].map);
1666 kfree(bp);
1667 kfree(bitmap);
1668}
NeilBrownaa5cbd12009-12-14 12:49:46 +11001669
NeilBrownfd01b882011-10-11 16:47:53 +11001670void bitmap_destroy(struct mddev *mddev)
NeilBrown3178b0d2005-09-09 16:23:50 -07001671{
1672 struct bitmap *bitmap = mddev->bitmap;
1673
1674 if (!bitmap) /* there was no bitmap */
1675 return;
1676
NeilBrownc3d97142009-12-14 12:49:52 +11001677 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrown3178b0d2005-09-09 16:23:50 -07001678 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownc3d97142009-12-14 12:49:52 +11001679 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrownb15c2e52006-01-06 00:20:16 -08001680 if (mddev->thread)
1681 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001682
NeilBrownece5cff2009-12-14 12:49:56 +11001683 if (bitmap->sysfs_can_clear)
1684 sysfs_put(bitmap->sysfs_can_clear);
1685
NeilBrown3178b0d2005-09-09 16:23:50 -07001686 bitmap_free(bitmap);
1687}
NeilBrown32a76272005-06-21 17:17:14 -07001688
1689/*
1690 * initialize the bitmap structure
1691 * if this returns an error, bitmap_destroy must be called to do clean up
1692 */
NeilBrownfd01b882011-10-11 16:47:53 +11001693int bitmap_create(struct mddev *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001694{
1695 struct bitmap *bitmap;
NeilBrown1f593902009-04-20 11:50:24 +10001696 sector_t blocks = mddev->resync_max_sectors;
NeilBrown32a76272005-06-21 17:17:14 -07001697 unsigned long chunks;
1698 unsigned long pages;
NeilBrownc3d97142009-12-14 12:49:52 +11001699 struct file *file = mddev->bitmap_info.file;
NeilBrown32a76272005-06-21 17:17:14 -07001700 int err;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001701 struct sysfs_dirent *bm = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001702
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001703 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001704
NeilBrowne384e582010-06-01 19:37:34 +10001705 if (!file
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001706 && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001707 return 0;
1708
NeilBrownc3d97142009-12-14 12:49:52 +11001709 BUG_ON(file && mddev->bitmap_info.offset);
NeilBrowna654b9d82005-06-21 17:17:27 -07001710
NeilBrown9ffae0c2006-01-06 00:20:32 -08001711 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001712 if (!bitmap)
1713 return -ENOMEM;
1714
NeilBrown32a76272005-06-21 17:17:14 -07001715 spin_lock_init(&bitmap->lock);
NeilBrownce25c312006-06-26 00:27:49 -07001716 atomic_set(&bitmap->pending_writes, 0);
1717 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001718 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrowne5551902010-03-31 11:21:44 +11001719 init_waitqueue_head(&bitmap->behind_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001720
NeilBrown32a76272005-06-21 17:17:14 -07001721 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001722
NeilBrown5ff5aff2010-06-01 19:37:32 +10001723 if (mddev->kobj.sd)
1724 bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
NeilBrownece5cff2009-12-14 12:49:56 +11001725 if (bm) {
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07001726 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
NeilBrownece5cff2009-12-14 12:49:56 +11001727 sysfs_put(bm);
1728 } else
1729 bitmap->sysfs_can_clear = NULL;
1730
NeilBrown32a76272005-06-21 17:17:14 -07001731 bitmap->file = file;
NeilBrownce25c312006-06-26 00:27:49 -07001732 if (file) {
1733 get_file(file);
NeilBrownae8fa282009-10-16 15:56:01 +11001734 /* As future accesses to this file will use bmap,
1735 * and bypass the page cache, we must sync the file
1736 * first.
1737 */
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001738 vfs_fsync(file, 1);
NeilBrownce25c312006-06-26 00:27:49 -07001739 }
NeilBrown42a04b52009-12-14 12:49:53 +11001740 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
Jonathan Brassow9c810752011-06-08 17:59:30 -05001741 if (!mddev->bitmap_info.external) {
1742 /*
1743 * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
1744 * instructing us to create a new on-disk bitmap instance.
1745 */
1746 if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
1747 err = bitmap_new_disk_sb(bitmap);
1748 else
1749 err = bitmap_read_sb(bitmap);
1750 } else {
NeilBrownece5cff2009-12-14 12:49:56 +11001751 err = 0;
1752 if (mddev->bitmap_info.chunksize == 0 ||
1753 mddev->bitmap_info.daemon_sleep == 0)
1754 /* chunksize and time_base need to be
1755 * set first. */
1756 err = -EINVAL;
1757 }
NeilBrown32a76272005-06-21 17:17:14 -07001758 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001759 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001760
NeilBrown624ce4f2009-12-14 12:49:56 +11001761 bitmap->daemon_lastrun = jiffies;
NeilBrown42a04b52009-12-14 12:49:53 +11001762 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
NeilBrown32a76272005-06-21 17:17:14 -07001763
1764 /* now that chunksize and chunkshift are set, we can use these macros */
NeilBrownac2f40b2010-06-01 19:37:31 +10001765 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
NeilBrown1f593902009-04-20 11:50:24 +10001766 CHUNK_BLOCK_SHIFT(bitmap);
NeilBrownac2f40b2010-06-01 19:37:31 +10001767 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
NeilBrown32a76272005-06-21 17:17:14 -07001768
1769 BUG_ON(!pages);
1770
1771 bitmap->chunks = chunks;
1772 bitmap->pages = pages;
1773 bitmap->missing_pages = pages;
NeilBrown32a76272005-06-21 17:17:14 -07001774
NeilBrown9ffae0c2006-01-06 00:20:32 -08001775 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown29d32472011-10-11 16:49:56 +11001776
NeilBrown3178b0d2005-09-09 16:23:50 -07001777 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001778 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001779 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001780
NeilBrown69e51b42010-06-01 19:37:35 +10001781 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1782 pages, bmname(bitmap));
1783
1784 mddev->bitmap = bitmap;
1785
1786
1787 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
1788
1789 error:
1790 bitmap_free(bitmap);
1791 return err;
1792}
1793
NeilBrownfd01b882011-10-11 16:47:53 +11001794int bitmap_load(struct mddev *mddev)
NeilBrown69e51b42010-06-01 19:37:35 +10001795{
1796 int err = 0;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001797 sector_t start = 0;
NeilBrown69e51b42010-06-01 19:37:35 +10001798 sector_t sector = 0;
1799 struct bitmap *bitmap = mddev->bitmap;
1800
1801 if (!bitmap)
1802 goto out;
1803
1804 /* Clear out old bitmap info first: Either there is none, or we
1805 * are resuming after someone else has possibly changed things,
1806 * so we should forget old cached info.
1807 * All chunks should be clean, but some might need_sync.
1808 */
1809 while (sector < mddev->resync_max_sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001810 sector_t blocks;
NeilBrown69e51b42010-06-01 19:37:35 +10001811 bitmap_start_sync(bitmap, sector, &blocks, 0);
1812 sector += blocks;
1813 }
1814 bitmap_close_sync(bitmap);
1815
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001816 if (mddev->degraded == 0
1817 || bitmap->events_cleared == mddev->events)
1818 /* no need to keep dirty bits to optimise a
1819 * re-add of a missing device */
1820 start = mddev->recovery_cp;
NeilBrown69e51b42010-06-01 19:37:35 +10001821
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001822 err = bitmap_init_from_disk(bitmap, start);
1823
NeilBrown32a76272005-06-21 17:17:14 -07001824 if (err)
NeilBrown69e51b42010-06-01 19:37:35 +10001825 goto out;
NeilBrown3178b0d2005-09-09 16:23:50 -07001826
NeilBrown1b04be92009-12-14 12:49:53 +11001827 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
NeilBrown9cd30fd2009-12-14 12:49:54 +11001828 md_wakeup_thread(mddev->thread);
NeilBrownb15c2e52006-01-06 00:20:16 -08001829
NeilBrown4ad13662007-07-17 04:06:13 -07001830 bitmap_update_sb(bitmap);
1831
NeilBrown69e51b42010-06-01 19:37:35 +10001832 if (bitmap->flags & BITMAP_WRITE_ERROR)
1833 err = -EIO;
1834out:
NeilBrown3178b0d2005-09-09 16:23:50 -07001835 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001836}
NeilBrown69e51b42010-06-01 19:37:35 +10001837EXPORT_SYMBOL_GPL(bitmap_load);
NeilBrown32a76272005-06-21 17:17:14 -07001838
NeilBrown43a70502009-12-14 12:49:55 +11001839static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001840location_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11001841{
1842 ssize_t len;
NeilBrownac2f40b2010-06-01 19:37:31 +10001843 if (mddev->bitmap_info.file)
NeilBrown43a70502009-12-14 12:49:55 +11001844 len = sprintf(page, "file");
NeilBrownac2f40b2010-06-01 19:37:31 +10001845 else if (mddev->bitmap_info.offset)
NeilBrown43a70502009-12-14 12:49:55 +11001846 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001847 else
NeilBrown43a70502009-12-14 12:49:55 +11001848 len = sprintf(page, "none");
1849 len += sprintf(page+len, "\n");
1850 return len;
1851}
1852
1853static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001854location_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11001855{
1856
1857 if (mddev->pers) {
1858 if (!mddev->pers->quiesce)
1859 return -EBUSY;
1860 if (mddev->recovery || mddev->sync_thread)
1861 return -EBUSY;
1862 }
1863
1864 if (mddev->bitmap || mddev->bitmap_info.file ||
1865 mddev->bitmap_info.offset) {
1866 /* bitmap already configured. Only option is to clear it */
1867 if (strncmp(buf, "none", 4) != 0)
1868 return -EBUSY;
1869 if (mddev->pers) {
1870 mddev->pers->quiesce(mddev, 1);
1871 bitmap_destroy(mddev);
1872 mddev->pers->quiesce(mddev, 0);
1873 }
1874 mddev->bitmap_info.offset = 0;
1875 if (mddev->bitmap_info.file) {
1876 struct file *f = mddev->bitmap_info.file;
1877 mddev->bitmap_info.file = NULL;
1878 restore_bitmap_write_access(f);
1879 fput(f);
1880 }
1881 } else {
1882 /* No bitmap, OK to set a location */
1883 long long offset;
1884 if (strncmp(buf, "none", 4) == 0)
1885 /* nothing to be done */;
1886 else if (strncmp(buf, "file:", 5) == 0) {
1887 /* Not supported yet */
1888 return -EINVAL;
1889 } else {
1890 int rv;
1891 if (buf[0] == '+')
1892 rv = strict_strtoll(buf+1, 10, &offset);
1893 else
1894 rv = strict_strtoll(buf, 10, &offset);
1895 if (rv)
1896 return rv;
1897 if (offset == 0)
1898 return -EINVAL;
NeilBrownece5cff2009-12-14 12:49:56 +11001899 if (mddev->bitmap_info.external == 0 &&
1900 mddev->major_version == 0 &&
NeilBrown43a70502009-12-14 12:49:55 +11001901 offset != mddev->bitmap_info.default_offset)
1902 return -EINVAL;
1903 mddev->bitmap_info.offset = offset;
1904 if (mddev->pers) {
1905 mddev->pers->quiesce(mddev, 1);
1906 rv = bitmap_create(mddev);
1907 if (rv) {
1908 bitmap_destroy(mddev);
1909 mddev->bitmap_info.offset = 0;
1910 }
1911 mddev->pers->quiesce(mddev, 0);
1912 if (rv)
1913 return rv;
1914 }
1915 }
1916 }
1917 if (!mddev->external) {
1918 /* Ensure new bitmap info is stored in
1919 * metadata promptly.
1920 */
1921 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1922 md_wakeup_thread(mddev->thread);
1923 }
1924 return len;
1925}
1926
1927static struct md_sysfs_entry bitmap_location =
1928__ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
1929
1930static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001931timeout_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11001932{
1933 ssize_t len;
1934 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
1935 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
NeilBrownac2f40b2010-06-01 19:37:31 +10001936
NeilBrown43a70502009-12-14 12:49:55 +11001937 len = sprintf(page, "%lu", secs);
1938 if (jifs)
1939 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
1940 len += sprintf(page+len, "\n");
1941 return len;
1942}
1943
1944static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001945timeout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11001946{
1947 /* timeout can be set at any time */
1948 unsigned long timeout;
1949 int rv = strict_strtoul_scaled(buf, &timeout, 4);
1950 if (rv)
1951 return rv;
1952
1953 /* just to make sure we don't overflow... */
1954 if (timeout >= LONG_MAX / HZ)
1955 return -EINVAL;
1956
1957 timeout = timeout * HZ / 10000;
1958
1959 if (timeout >= MAX_SCHEDULE_TIMEOUT)
1960 timeout = MAX_SCHEDULE_TIMEOUT-1;
1961 if (timeout < 1)
1962 timeout = 1;
1963 mddev->bitmap_info.daemon_sleep = timeout;
1964 if (mddev->thread) {
1965 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1966 * the bitmap is all clean and we don't need to
1967 * adjust the timeout right now
1968 */
1969 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
1970 mddev->thread->timeout = timeout;
1971 md_wakeup_thread(mddev->thread);
1972 }
1973 }
1974 return len;
1975}
1976
1977static struct md_sysfs_entry bitmap_timeout =
1978__ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
1979
1980static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001981backlog_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11001982{
1983 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
1984}
1985
1986static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001987backlog_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11001988{
1989 unsigned long backlog;
1990 int rv = strict_strtoul(buf, 10, &backlog);
1991 if (rv)
1992 return rv;
1993 if (backlog > COUNTER_MAX)
1994 return -EINVAL;
1995 mddev->bitmap_info.max_write_behind = backlog;
1996 return len;
1997}
1998
1999static struct md_sysfs_entry bitmap_backlog =
2000__ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
2001
2002static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002003chunksize_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002004{
2005 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
2006}
2007
2008static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002009chunksize_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002010{
2011 /* Can only be changed when no bitmap is active */
2012 int rv;
2013 unsigned long csize;
2014 if (mddev->bitmap)
2015 return -EBUSY;
2016 rv = strict_strtoul(buf, 10, &csize);
2017 if (rv)
2018 return rv;
2019 if (csize < 512 ||
2020 !is_power_of_2(csize))
2021 return -EINVAL;
2022 mddev->bitmap_info.chunksize = csize;
2023 return len;
2024}
2025
2026static struct md_sysfs_entry bitmap_chunksize =
2027__ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
2028
NeilBrownfd01b882011-10-11 16:47:53 +11002029static ssize_t metadata_show(struct mddev *mddev, char *page)
NeilBrownece5cff2009-12-14 12:49:56 +11002030{
2031 return sprintf(page, "%s\n", (mddev->bitmap_info.external
2032 ? "external" : "internal"));
2033}
2034
NeilBrownfd01b882011-10-11 16:47:53 +11002035static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownece5cff2009-12-14 12:49:56 +11002036{
2037 if (mddev->bitmap ||
2038 mddev->bitmap_info.file ||
2039 mddev->bitmap_info.offset)
2040 return -EBUSY;
2041 if (strncmp(buf, "external", 8) == 0)
2042 mddev->bitmap_info.external = 1;
2043 else if (strncmp(buf, "internal", 8) == 0)
2044 mddev->bitmap_info.external = 0;
2045 else
2046 return -EINVAL;
2047 return len;
2048}
2049
2050static struct md_sysfs_entry bitmap_metadata =
2051__ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2052
NeilBrownfd01b882011-10-11 16:47:53 +11002053static ssize_t can_clear_show(struct mddev *mddev, char *page)
NeilBrownece5cff2009-12-14 12:49:56 +11002054{
2055 int len;
2056 if (mddev->bitmap)
2057 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
2058 "false" : "true"));
2059 else
2060 len = sprintf(page, "\n");
2061 return len;
2062}
2063
NeilBrownfd01b882011-10-11 16:47:53 +11002064static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownece5cff2009-12-14 12:49:56 +11002065{
2066 if (mddev->bitmap == NULL)
2067 return -ENOENT;
2068 if (strncmp(buf, "false", 5) == 0)
2069 mddev->bitmap->need_sync = 1;
2070 else if (strncmp(buf, "true", 4) == 0) {
2071 if (mddev->degraded)
2072 return -EBUSY;
2073 mddev->bitmap->need_sync = 0;
2074 } else
2075 return -EINVAL;
2076 return len;
2077}
2078
2079static struct md_sysfs_entry bitmap_can_clear =
2080__ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
2081
Paul Clements696fcd52010-03-08 16:02:37 +11002082static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002083behind_writes_used_show(struct mddev *mddev, char *page)
Paul Clements696fcd52010-03-08 16:02:37 +11002084{
2085 if (mddev->bitmap == NULL)
2086 return sprintf(page, "0\n");
2087 return sprintf(page, "%lu\n",
2088 mddev->bitmap->behind_writes_used);
2089}
2090
2091static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002092behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
Paul Clements696fcd52010-03-08 16:02:37 +11002093{
2094 if (mddev->bitmap)
2095 mddev->bitmap->behind_writes_used = 0;
2096 return len;
2097}
2098
2099static struct md_sysfs_entry max_backlog_used =
2100__ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2101 behind_writes_used_show, behind_writes_used_reset);
2102
NeilBrown43a70502009-12-14 12:49:55 +11002103static struct attribute *md_bitmap_attrs[] = {
2104 &bitmap_location.attr,
2105 &bitmap_timeout.attr,
2106 &bitmap_backlog.attr,
2107 &bitmap_chunksize.attr,
NeilBrownece5cff2009-12-14 12:49:56 +11002108 &bitmap_metadata.attr,
2109 &bitmap_can_clear.attr,
Paul Clements696fcd52010-03-08 16:02:37 +11002110 &max_backlog_used.attr,
NeilBrown43a70502009-12-14 12:49:55 +11002111 NULL
2112};
2113struct attribute_group md_bitmap_group = {
2114 .name = "bitmap",
2115 .attrs = md_bitmap_attrs,
2116};
2117