blob: cd722b83a0c95c9798a32b46ff4a4602f54e7198 [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
32/* debug macros */
33
34#define DEBUG 0
35
36#if DEBUG
37/* these are for debugging purposes only! */
38
39/* define one and only one of these */
40#define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
41#define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
42#define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
43#define INJECT_FAULTS_4 0 /* undef */
44#define INJECT_FAULTS_5 0 /* undef */
45#define INJECT_FAULTS_6 0
46
47/* if these are defined, the driver will fail! debug only */
48#define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
49#define INJECT_FATAL_FAULT_2 0 /* undef */
50#define INJECT_FATAL_FAULT_3 0 /* undef */
51#endif
52
NeilBrownac2f40b2010-06-01 19:37:31 +100053static inline char *bmname(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -070054{
55 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
56}
57
NeilBrown32a76272005-06-21 17:17:14 -070058/*
59 * just a placeholder - calls kmalloc for bitmap pages
60 */
61static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
62{
63 unsigned char *page;
64
Olaf Hering44456d32005-07-27 11:45:17 -070065#ifdef INJECT_FAULTS_1
NeilBrown32a76272005-06-21 17:17:14 -070066 page = NULL;
67#else
NeilBrownac2f40b2010-06-01 19:37:31 +100068 page = kzalloc(PAGE_SIZE, GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -070069#endif
70 if (!page)
71 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
72 else
NeilBrown36a4e1f2011-10-07 14:23:17 +110073 pr_debug("%s: bitmap_alloc_page: allocated page at %p\n",
74 bmname(bitmap), page);
NeilBrown32a76272005-06-21 17:17:14 -070075 return page;
76}
77
78/*
79 * for now just a placeholder -- just calls kfree for bitmap pages
80 */
81static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
82{
NeilBrown36a4e1f2011-10-07 14:23:17 +110083 pr_debug("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
NeilBrown32a76272005-06-21 17:17:14 -070084 kfree(page);
85}
86
87/*
88 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
89 *
90 * 1) check to see if this page is allocated, if it's not then try to alloc
91 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
92 * page pointer directly as a counter
93 *
94 * if we find our page, we increment the page's refcount so that it stays
95 * allocated while we're using it
96 */
NeilBrownac2f40b2010-06-01 19:37:31 +100097static int bitmap_checkpage(struct bitmap *bitmap,
98 unsigned long page, int create)
NeilBrownee305ac2009-09-23 18:06:44 +100099__releases(bitmap->lock)
100__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -0700101{
102 unsigned char *mappage;
103
104 if (page >= bitmap->pages) {
NeilBrown1187cf02009-03-31 14:27:02 +1100105 /* This can happen if bitmap_start_sync goes beyond
106 * End-of-device while looking for a whole page.
107 * It is harmless.
108 */
NeilBrown32a76272005-06-21 17:17:14 -0700109 return -EINVAL;
110 }
111
NeilBrown32a76272005-06-21 17:17:14 -0700112 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
113 return 0;
114
115 if (bitmap->bp[page].map) /* page is already allocated, just return */
116 return 0;
117
118 if (!create)
119 return -ENOENT;
120
NeilBrown32a76272005-06-21 17:17:14 -0700121 /* this page has not been allocated yet */
122
NeilBrownac2f40b2010-06-01 19:37:31 +1000123 spin_unlock_irq(&bitmap->lock);
124 mappage = bitmap_alloc_page(bitmap);
125 spin_lock_irq(&bitmap->lock);
126
127 if (mappage == NULL) {
NeilBrown36a4e1f2011-10-07 14:23:17 +1100128 pr_debug("%s: bitmap map page allocation failed, hijacking\n",
129 bmname(bitmap));
NeilBrown32a76272005-06-21 17:17:14 -0700130 /* failed - set the hijacked flag so that we can use the
131 * pointer as a counter */
NeilBrown32a76272005-06-21 17:17:14 -0700132 if (!bitmap->bp[page].map)
133 bitmap->bp[page].hijacked = 1;
NeilBrownac2f40b2010-06-01 19:37:31 +1000134 } else if (bitmap->bp[page].map ||
135 bitmap->bp[page].hijacked) {
NeilBrown32a76272005-06-21 17:17:14 -0700136 /* somebody beat us to getting the page */
137 bitmap_free_page(bitmap, mappage);
138 return 0;
NeilBrownac2f40b2010-06-01 19:37:31 +1000139 } else {
140
141 /* no page was in place and we have one, so install it */
142
143 bitmap->bp[page].map = mappage;
144 bitmap->missing_pages--;
NeilBrown32a76272005-06-21 17:17:14 -0700145 }
NeilBrown32a76272005-06-21 17:17:14 -0700146 return 0;
147}
148
NeilBrown32a76272005-06-21 17:17:14 -0700149/* if page is completely empty, put it back on the free list, or dealloc it */
150/* if page was hijacked, unmark the flag so it might get alloced next time */
151/* Note: lock should be held when calling this */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800152static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
NeilBrown32a76272005-06-21 17:17:14 -0700153{
154 char *ptr;
155
156 if (bitmap->bp[page].count) /* page is still busy */
157 return;
158
159 /* page is no longer in use, it can be released */
160
161 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
162 bitmap->bp[page].hijacked = 0;
163 bitmap->bp[page].map = NULL;
NeilBrownac2f40b2010-06-01 19:37:31 +1000164 } else {
165 /* normal case, free the page */
166 ptr = bitmap->bp[page].map;
167 bitmap->bp[page].map = NULL;
168 bitmap->missing_pages++;
169 bitmap_free_page(bitmap, ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700170 }
NeilBrown32a76272005-06-21 17:17:14 -0700171}
172
NeilBrown32a76272005-06-21 17:17:14 -0700173/*
174 * bitmap file handling - read and write the bitmap file and its superblock
175 */
176
NeilBrown32a76272005-06-21 17:17:14 -0700177/*
178 * basic page I/O operations
179 */
180
NeilBrowna654b9d82005-06-21 17:17:27 -0700181/* IO operations when bitmap is stored near all superblocks */
NeilBrownf6af9492009-12-14 12:49:54 +1100182static struct page *read_sb_page(mddev_t *mddev, loff_t offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100183 struct page *page,
184 unsigned long index, int size)
NeilBrowna654b9d82005-06-21 17:17:27 -0700185{
186 /* choose a good rdev and read the page from there */
187
188 mdk_rdev_t *rdev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700189 sector_t target;
NeilBrownac2f40b2010-06-01 19:37:31 +1000190 int did_alloc = 0;
NeilBrowna654b9d82005-06-21 17:17:27 -0700191
NeilBrownac2f40b2010-06-01 19:37:31 +1000192 if (!page) {
NeilBrowna2ed9612008-12-19 16:25:01 +1100193 page = alloc_page(GFP_KERNEL);
NeilBrownac2f40b2010-06-01 19:37:31 +1000194 if (!page)
195 return ERR_PTR(-ENOMEM);
196 did_alloc = 1;
197 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700198
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100199 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800200 if (! test_bit(In_sync, &rdev->flags)
201 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700202 continue;
203
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100204 target = offset + index * (PAGE_SIZE/512);
NeilBrowna654b9d82005-06-21 17:17:27 -0700205
NeilBrown2b193362010-10-27 15:16:40 +1100206 if (sync_page_io(rdev, target,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400207 roundup(size, bdev_logical_block_size(rdev->bdev)),
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100208 page, READ, true)) {
NeilBrownab904d62005-09-09 16:23:52 -0700209 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700210 attach_page_buffers(page, NULL); /* so that free_buffer will
211 * quietly no-op */
NeilBrownab904d62005-09-09 16:23:52 -0700212 return page;
213 }
214 }
NeilBrownac2f40b2010-06-01 19:37:31 +1000215 if (did_alloc)
216 put_page(page);
NeilBrownab904d62005-09-09 16:23:52 -0700217 return ERR_PTR(-EIO);
NeilBrowna654b9d82005-06-21 17:17:27 -0700218
NeilBrowna654b9d82005-06-21 17:17:27 -0700219}
220
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000221static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
222{
223 /* Iterate the disks of an mddev, using rcu to protect access to the
224 * linked list, and raising the refcount of devices we return to ensure
225 * they don't disappear while in use.
226 * As devices are only added or removed when raid_disk is < 0 and
227 * nr_pending is 0 and In_sync is clear, the entries we return will
228 * still be in the same position on the list when we re-enter
229 * list_for_each_continue_rcu.
230 */
231 struct list_head *pos;
232 rcu_read_lock();
233 if (rdev == NULL)
234 /* start at the beginning */
235 pos = &mddev->disks;
236 else {
237 /* release the previous rdev and start from there. */
238 rdev_dec_pending(rdev, mddev);
239 pos = &rdev->same_set;
240 }
241 list_for_each_continue_rcu(pos, &mddev->disks) {
242 rdev = list_entry(pos, mdk_rdev_t, same_set);
243 if (rdev->raid_disk >= 0 &&
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000244 !test_bit(Faulty, &rdev->flags)) {
245 /* this is a usable devices */
246 atomic_inc(&rdev->nr_pending);
247 rcu_read_unlock();
248 return rdev;
249 }
250 }
251 rcu_read_unlock();
252 return NULL;
253}
254
NeilBrownab6085c2007-05-23 13:58:10 -0700255static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrowna654b9d82005-06-21 17:17:27 -0700256{
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000257 mdk_rdev_t *rdev = NULL;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100258 struct block_device *bdev;
NeilBrownab6085c2007-05-23 13:58:10 -0700259 mddev_t *mddev = bitmap->mddev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700260
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000261 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000262 int size = PAGE_SIZE;
263 loff_t offset = mddev->bitmap_info.offset;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100264
265 bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
266
NeilBrownac2f40b2010-06-01 19:37:31 +1000267 if (page->index == bitmap->file_pages-1)
268 size = roundup(bitmap->last_page_size,
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100269 bdev_logical_block_size(bdev));
NeilBrownac2f40b2010-06-01 19:37:31 +1000270 /* Just make sure we aren't corrupting data or
271 * metadata
272 */
273 if (mddev->external) {
274 /* Bitmap could be anywhere. */
275 if (rdev->sb_start + offset + (page->index
276 * (PAGE_SIZE/512))
277 > rdev->data_offset
278 &&
279 rdev->sb_start + offset
280 < (rdev->data_offset + mddev->dev_sectors
281 + (PAGE_SIZE/512)))
282 goto bad_alignment;
283 } else if (offset < 0) {
284 /* DATA BITMAP METADATA */
285 if (offset
286 + (long)(page->index * (PAGE_SIZE/512))
287 + size/512 > 0)
288 /* bitmap runs in to metadata */
289 goto bad_alignment;
290 if (rdev->data_offset + mddev->dev_sectors
291 > rdev->sb_start + offset)
292 /* data runs in to bitmap */
293 goto bad_alignment;
294 } else if (rdev->sb_start < rdev->data_offset) {
295 /* METADATA BITMAP DATA */
296 if (rdev->sb_start
297 + offset
298 + page->index*(PAGE_SIZE/512) + size/512
299 > rdev->data_offset)
300 /* bitmap runs in to data */
301 goto bad_alignment;
302 } else {
303 /* DATA METADATA BITMAP - no problems */
304 }
305 md_super_write(mddev, rdev,
306 rdev->sb_start + offset
307 + page->index * (PAGE_SIZE/512),
308 size,
309 page);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000310 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700311
312 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800313 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700314 return 0;
NeilBrown4b809912008-07-21 17:05:25 +1000315
316 bad_alignment:
NeilBrown4b809912008-07-21 17:05:25 +1000317 return -EINVAL;
NeilBrowna654b9d82005-06-21 17:17:27 -0700318}
319
NeilBrown4ad13662007-07-17 04:06:13 -0700320static void bitmap_file_kick(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700321/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700322 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700323 */
NeilBrown4ad13662007-07-17 04:06:13 -0700324static void write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700325{
NeilBrownd785a062006-06-26 00:27:48 -0700326 struct buffer_head *bh;
NeilBrown32a76272005-06-21 17:17:14 -0700327
NeilBrownf0d76d72007-07-17 04:06:12 -0700328 if (bitmap->file == NULL) {
329 switch (write_sb_page(bitmap, page, wait)) {
330 case -EINVAL:
331 bitmap->flags |= BITMAP_WRITE_ERROR;
NeilBrownf0d76d72007-07-17 04:06:12 -0700332 }
NeilBrown4ad13662007-07-17 04:06:13 -0700333 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700334
NeilBrown4ad13662007-07-17 04:06:13 -0700335 bh = page_buffers(page);
NeilBrownc7084432006-01-06 00:20:45 -0800336
NeilBrown4ad13662007-07-17 04:06:13 -0700337 while (bh && bh->b_blocknr) {
338 atomic_inc(&bitmap->pending_writes);
339 set_buffer_locked(bh);
340 set_buffer_mapped(bh);
Jens Axboe721a9602011-03-09 11:56:30 +0100341 submit_bh(WRITE | REQ_SYNC, bh);
NeilBrown4ad13662007-07-17 04:06:13 -0700342 bh = bh->b_this_page;
343 }
NeilBrown32a76272005-06-21 17:17:14 -0700344
NeilBrownac2f40b2010-06-01 19:37:31 +1000345 if (wait)
NeilBrown4ad13662007-07-17 04:06:13 -0700346 wait_event(bitmap->write_wait,
347 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700348 }
NeilBrown4ad13662007-07-17 04:06:13 -0700349 if (bitmap->flags & BITMAP_WRITE_ERROR)
350 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700351}
352
NeilBrownd785a062006-06-26 00:27:48 -0700353static void end_bitmap_write(struct buffer_head *bh, int uptodate)
NeilBrown32a76272005-06-21 17:17:14 -0700354{
NeilBrownd785a062006-06-26 00:27:48 -0700355 struct bitmap *bitmap = bh->b_private;
356 unsigned long flags;
357
358 if (!uptodate) {
359 spin_lock_irqsave(&bitmap->lock, flags);
360 bitmap->flags |= BITMAP_WRITE_ERROR;
361 spin_unlock_irqrestore(&bitmap->lock, flags);
362 }
363 if (atomic_dec_and_test(&bitmap->pending_writes))
364 wake_up(&bitmap->write_wait);
365}
366
367/* copied from buffer.c */
368static void
369__clear_page_buffers(struct page *page)
370{
371 ClearPagePrivate(page);
372 set_page_private(page, 0);
373 page_cache_release(page);
374}
375static void free_buffers(struct page *page)
376{
377 struct buffer_head *bh = page_buffers(page);
378
379 while (bh) {
380 struct buffer_head *next = bh->b_this_page;
381 free_buffer_head(bh);
382 bh = next;
383 }
384 __clear_page_buffers(page);
385 put_page(page);
386}
387
388/* read a page from a file.
389 * We both read the page, and attach buffers to the page to record the
390 * address of each block (using bmap). These addresses will be used
391 * to write the block later, completely bypassing the filesystem.
392 * This usage is similar to how swap files are handled, and allows us
393 * to write to a file with no concerns of memory allocation failing.
394 */
395static struct page *read_page(struct file *file, unsigned long index,
396 struct bitmap *bitmap,
397 unsigned long count)
398{
NeilBrown32a76272005-06-21 17:17:14 -0700399 struct page *page = NULL;
Josef Sipekc649bb92006-12-08 02:37:19 -0800400 struct inode *inode = file->f_path.dentry->d_inode;
NeilBrownd785a062006-06-26 00:27:48 -0700401 struct buffer_head *bh;
402 sector_t block;
NeilBrown32a76272005-06-21 17:17:14 -0700403
NeilBrown36a4e1f2011-10-07 14:23:17 +1100404 pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
405 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700406
NeilBrownd785a062006-06-26 00:27:48 -0700407 page = alloc_page(GFP_KERNEL);
408 if (!page)
409 page = ERR_PTR(-ENOMEM);
NeilBrown32a76272005-06-21 17:17:14 -0700410 if (IS_ERR(page))
411 goto out;
NeilBrownd785a062006-06-26 00:27:48 -0700412
NeilBrownd785a062006-06-26 00:27:48 -0700413 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
414 if (!bh) {
415 put_page(page);
416 page = ERR_PTR(-ENOMEM);
417 goto out;
418 }
419 attach_page_buffers(page, bh);
420 block = index << (PAGE_SHIFT - inode->i_blkbits);
421 while (bh) {
422 if (count == 0)
423 bh->b_blocknr = 0;
424 else {
425 bh->b_blocknr = bmap(inode, block);
426 if (bh->b_blocknr == 0) {
427 /* Cannot use this file! */
428 free_buffers(page);
429 page = ERR_PTR(-EINVAL);
430 goto out;
431 }
432 bh->b_bdev = inode->i_sb->s_bdev;
433 if (count < (1<<inode->i_blkbits))
434 count = 0;
435 else
436 count -= (1<<inode->i_blkbits);
NeilBrown32a76272005-06-21 17:17:14 -0700437
NeilBrownd785a062006-06-26 00:27:48 -0700438 bh->b_end_io = end_bitmap_write;
439 bh->b_private = bitmap;
NeilBrownce25c312006-06-26 00:27:49 -0700440 atomic_inc(&bitmap->pending_writes);
441 set_buffer_locked(bh);
442 set_buffer_mapped(bh);
443 submit_bh(READ, bh);
NeilBrownd785a062006-06-26 00:27:48 -0700444 }
445 block++;
446 bh = bh->b_this_page;
447 }
NeilBrownd785a062006-06-26 00:27:48 -0700448 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700449
450 wait_event(bitmap->write_wait,
451 atomic_read(&bitmap->pending_writes)==0);
452 if (bitmap->flags & BITMAP_WRITE_ERROR) {
453 free_buffers(page);
454 page = ERR_PTR(-EIO);
455 }
NeilBrown32a76272005-06-21 17:17:14 -0700456out:
457 if (IS_ERR(page))
NeilBrownac2f40b2010-06-01 19:37:31 +1000458 printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800459 (int)PAGE_SIZE,
460 (unsigned long long)index << PAGE_SHIFT,
NeilBrown32a76272005-06-21 17:17:14 -0700461 PTR_ERR(page));
462 return page;
463}
464
465/*
466 * bitmap file superblock operations
467 */
468
469/* update the event counter and sync the superblock to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700470void bitmap_update_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700471{
472 bitmap_super_t *sb;
473 unsigned long flags;
474
475 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
NeilBrown4ad13662007-07-17 04:06:13 -0700476 return;
NeilBrownece5cff2009-12-14 12:49:56 +1100477 if (bitmap->mddev->bitmap_info.external)
478 return;
NeilBrown32a76272005-06-21 17:17:14 -0700479 spin_lock_irqsave(&bitmap->lock, flags);
480 if (!bitmap->sb_page) { /* no superblock */
481 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700482 return;
NeilBrown32a76272005-06-21 17:17:14 -0700483 }
NeilBrown32a76272005-06-21 17:17:14 -0700484 spin_unlock_irqrestore(&bitmap->lock, flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100485 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700486 sb->events = cpu_to_le64(bitmap->mddev->events);
NeilBrown8258c532011-05-11 14:26:30 +1000487 if (bitmap->mddev->events < bitmap->events_cleared)
Neil Browna0da84f2008-06-28 08:31:22 +1000488 /* rocking back to read-only */
489 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown8258c532011-05-11 14:26:30 +1000490 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
491 sb->state = cpu_to_le32(bitmap->flags);
NeilBrown43a70502009-12-14 12:49:55 +1100492 /* Just in case these have been changed via sysfs: */
493 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
494 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
NeilBrownea03aff2006-01-06 00:20:34 -0800495 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700496 write_page(bitmap, bitmap->sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700497}
498
499/* print out the bitmap file superblock */
500void bitmap_print_sb(struct bitmap *bitmap)
501{
502 bitmap_super_t *sb;
503
504 if (!bitmap || !bitmap->sb_page)
505 return;
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100506 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700507 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700508 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
509 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
510 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700511 *(__u32 *)(sb->uuid+0),
512 *(__u32 *)(sb->uuid+4),
513 *(__u32 *)(sb->uuid+8),
514 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700515 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700516 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700517 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700518 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700519 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
520 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
521 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
522 printk(KERN_DEBUG " sync size: %llu KB\n",
523 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700524 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
NeilBrownea03aff2006-01-06 00:20:34 -0800525 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700526}
527
Jonathan Brassow9c810752011-06-08 17:59:30 -0500528/*
529 * bitmap_new_disk_sb
530 * @bitmap
531 *
532 * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
533 * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
534 * This function verifies 'bitmap_info' and populates the on-disk bitmap
535 * structure, which is to be written to disk.
536 *
537 * Returns: 0 on success, -Exxx on error
538 */
539static int bitmap_new_disk_sb(struct bitmap *bitmap)
540{
541 bitmap_super_t *sb;
542 unsigned long chunksize, daemon_sleep, write_behind;
543 int err = -EINVAL;
544
545 bitmap->sb_page = alloc_page(GFP_KERNEL);
546 if (IS_ERR(bitmap->sb_page)) {
547 err = PTR_ERR(bitmap->sb_page);
548 bitmap->sb_page = NULL;
549 return err;
550 }
551 bitmap->sb_page->index = 0;
552
553 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
554
555 sb->magic = cpu_to_le32(BITMAP_MAGIC);
556 sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
557
558 chunksize = bitmap->mddev->bitmap_info.chunksize;
559 BUG_ON(!chunksize);
560 if (!is_power_of_2(chunksize)) {
561 kunmap_atomic(sb, KM_USER0);
562 printk(KERN_ERR "bitmap chunksize not a power of 2\n");
563 return -EINVAL;
564 }
565 sb->chunksize = cpu_to_le32(chunksize);
566
567 daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
568 if (!daemon_sleep ||
569 (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
570 printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n");
571 daemon_sleep = 5 * HZ;
572 }
573 sb->daemon_sleep = cpu_to_le32(daemon_sleep);
574 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
575
576 /*
577 * FIXME: write_behind for RAID1. If not specified, what
578 * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
579 */
580 write_behind = bitmap->mddev->bitmap_info.max_write_behind;
581 if (write_behind > COUNTER_MAX)
582 write_behind = COUNTER_MAX / 2;
583 sb->write_behind = cpu_to_le32(write_behind);
584 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
585
586 /* keep the array size field of the bitmap superblock up to date */
587 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
588
589 memcpy(sb->uuid, bitmap->mddev->uuid, 16);
590
591 bitmap->flags |= BITMAP_STALE;
592 sb->state |= cpu_to_le32(BITMAP_STALE);
593 bitmap->events_cleared = bitmap->mddev->events;
594 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
595
596 bitmap->flags |= BITMAP_HOSTENDIAN;
597 sb->version = cpu_to_le32(BITMAP_MAJOR_HOSTENDIAN);
598
599 kunmap_atomic(sb, KM_USER0);
600
601 return 0;
602}
603
NeilBrown32a76272005-06-21 17:17:14 -0700604/* read the superblock from the bitmap file and initialize some bitmap fields */
605static int bitmap_read_sb(struct bitmap *bitmap)
606{
607 char *reason = NULL;
608 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700609 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700610 unsigned long long events;
611 int err = -EINVAL;
612
613 /* page 0 is the superblock, read it... */
NeilBrownf49d5e62007-01-26 00:57:03 -0800614 if (bitmap->file) {
615 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
616 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
617
618 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
619 } else {
NeilBrown42a04b52009-12-14 12:49:53 +1100620 bitmap->sb_page = read_sb_page(bitmap->mddev,
621 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +1100622 NULL,
623 0, sizeof(bitmap_super_t));
NeilBrowna654b9d82005-06-21 17:17:27 -0700624 }
NeilBrown32a76272005-06-21 17:17:14 -0700625 if (IS_ERR(bitmap->sb_page)) {
626 err = PTR_ERR(bitmap->sb_page);
627 bitmap->sb_page = NULL;
628 return err;
629 }
630
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100631 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700632
NeilBrown32a76272005-06-21 17:17:14 -0700633 chunksize = le32_to_cpu(sb->chunksize);
NeilBrown1b04be92009-12-14 12:49:53 +1100634 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
NeilBrown4b6d2872005-09-09 16:23:47 -0700635 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown32a76272005-06-21 17:17:14 -0700636
637 /* verify that the bitmap-specific fields are valid */
638 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
639 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800640 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
641 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700642 reason = "unrecognized superblock version";
NeilBrown1187cf02009-03-31 14:27:02 +1100643 else if (chunksize < 512)
NeilBrown7dd5d342006-01-06 00:20:39 -0800644 reason = "bitmap chunksize too small";
Jonathan Brassowd7445402011-06-08 18:01:10 -0500645 else if (!is_power_of_2(chunksize))
NeilBrown32a76272005-06-21 17:17:14 -0700646 reason = "bitmap chunksize not a power of 2";
NeilBrown1b04be92009-12-14 12:49:53 +1100647 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
NeilBrown7dd5d342006-01-06 00:20:39 -0800648 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700649 else if (write_behind > COUNTER_MAX)
650 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700651 if (reason) {
652 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
653 bmname(bitmap), reason);
654 goto out;
655 }
656
657 /* keep the array size field of the bitmap superblock up to date */
658 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
659
660 if (!bitmap->mddev->persistent)
661 goto success;
662
663 /*
664 * if we have a persistent array superblock, compare the
665 * bitmap's UUID and event counter to the mddev's
666 */
667 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
668 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
669 bmname(bitmap));
670 goto out;
671 }
672 events = le64_to_cpu(sb->events);
673 if (events < bitmap->mddev->events) {
674 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
675 "-- forcing full recovery\n", bmname(bitmap), events,
676 (unsigned long long) bitmap->mddev->events);
NeilBrown4f2e6392006-10-21 10:24:09 -0700677 sb->state |= cpu_to_le32(BITMAP_STALE);
NeilBrown32a76272005-06-21 17:17:14 -0700678 }
679success:
680 /* assign fields using values from superblock */
NeilBrown42a04b52009-12-14 12:49:53 +1100681 bitmap->mddev->bitmap_info.chunksize = chunksize;
682 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
NeilBrown42a04b52009-12-14 12:49:53 +1100683 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
NeilBrown4f2e6392006-10-21 10:24:09 -0700684 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800685 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
686 bitmap->flags |= BITMAP_HOSTENDIAN;
NeilBrown32a76272005-06-21 17:17:14 -0700687 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
NeilBrown8258c532011-05-11 14:26:30 +1000688 if (bitmap->flags & BITMAP_STALE)
NeilBrown6a079972005-09-09 16:23:44 -0700689 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown32a76272005-06-21 17:17:14 -0700690 err = 0;
691out:
NeilBrownea03aff2006-01-06 00:20:34 -0800692 kunmap_atomic(sb, KM_USER0);
NeilBrown32a76272005-06-21 17:17:14 -0700693 if (err)
694 bitmap_print_sb(bitmap);
695 return err;
696}
697
698enum bitmap_mask_op {
699 MASK_SET,
700 MASK_UNSET
701};
702
NeilBrown4ad13662007-07-17 04:06:13 -0700703/* record the state of the bitmap in the superblock. Return the old value */
704static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
705 enum bitmap_mask_op op)
NeilBrown32a76272005-06-21 17:17:14 -0700706{
707 bitmap_super_t *sb;
708 unsigned long flags;
NeilBrown4ad13662007-07-17 04:06:13 -0700709 int old;
NeilBrown32a76272005-06-21 17:17:14 -0700710
711 spin_lock_irqsave(&bitmap->lock, flags);
Adrian Bunk7e317652006-03-25 03:07:51 -0800712 if (!bitmap->sb_page) { /* can't set the state */
NeilBrown32a76272005-06-21 17:17:14 -0700713 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700714 return 0;
NeilBrown32a76272005-06-21 17:17:14 -0700715 }
NeilBrown32a76272005-06-21 17:17:14 -0700716 spin_unlock_irqrestore(&bitmap->lock, flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100717 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700718 old = le32_to_cpu(sb->state) & bits;
NeilBrown32a76272005-06-21 17:17:14 -0700719 switch (op) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000720 case MASK_SET:
721 sb->state |= cpu_to_le32(bits);
NeilBrown8258c532011-05-11 14:26:30 +1000722 bitmap->flags |= bits;
NeilBrownac2f40b2010-06-01 19:37:31 +1000723 break;
724 case MASK_UNSET:
725 sb->state &= cpu_to_le32(~bits);
NeilBrown8258c532011-05-11 14:26:30 +1000726 bitmap->flags &= ~bits;
NeilBrownac2f40b2010-06-01 19:37:31 +1000727 break;
728 default:
729 BUG();
NeilBrown32a76272005-06-21 17:17:14 -0700730 }
NeilBrownea03aff2006-01-06 00:20:34 -0800731 kunmap_atomic(sb, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -0700732 return old;
NeilBrown32a76272005-06-21 17:17:14 -0700733}
734
735/*
736 * general bitmap file operations
737 */
738
NeilBrownece5cff2009-12-14 12:49:56 +1100739/*
740 * on-disk bitmap:
741 *
742 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
743 * file a page at a time. There's a superblock at the start of the file.
744 */
NeilBrown32a76272005-06-21 17:17:14 -0700745/* calculate the index of the page that contains this bit */
NeilBrownece5cff2009-12-14 12:49:56 +1100746static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700747{
NeilBrownece5cff2009-12-14 12:49:56 +1100748 if (!bitmap->mddev->bitmap_info.external)
749 chunk += sizeof(bitmap_super_t) << 3;
750 return chunk >> PAGE_BIT_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700751}
752
753/* calculate the (bit) offset of this bit within a page */
NeilBrownece5cff2009-12-14 12:49:56 +1100754static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700755{
NeilBrownece5cff2009-12-14 12:49:56 +1100756 if (!bitmap->mddev->bitmap_info.external)
757 chunk += sizeof(bitmap_super_t) << 3;
758 return chunk & (PAGE_BITS - 1);
NeilBrown32a76272005-06-21 17:17:14 -0700759}
760
761/*
762 * return a pointer to the page in the filemap that contains the given bit
763 *
764 * this lookup is complicated by the fact that the bitmap sb might be exactly
765 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
766 * 0 or page 1
767 */
768static inline struct page *filemap_get_page(struct bitmap *bitmap,
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000769 unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700770{
NeilBrownac2f40b2010-06-01 19:37:31 +1000771 if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
772 return NULL;
NeilBrownece5cff2009-12-14 12:49:56 +1100773 return bitmap->filemap[file_page_index(bitmap, chunk)
774 - file_page_index(bitmap, 0)];
NeilBrown32a76272005-06-21 17:17:14 -0700775}
776
NeilBrown32a76272005-06-21 17:17:14 -0700777static void bitmap_file_unmap(struct bitmap *bitmap)
778{
779 struct page **map, *sb_page;
780 unsigned long *attr;
781 int pages;
782 unsigned long flags;
783
784 spin_lock_irqsave(&bitmap->lock, flags);
785 map = bitmap->filemap;
786 bitmap->filemap = NULL;
787 attr = bitmap->filemap_attr;
788 bitmap->filemap_attr = NULL;
789 pages = bitmap->file_pages;
790 bitmap->file_pages = 0;
791 sb_page = bitmap->sb_page;
792 bitmap->sb_page = NULL;
793 spin_unlock_irqrestore(&bitmap->lock, flags);
794
795 while (pages--)
NeilBrownece5cff2009-12-14 12:49:56 +1100796 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700797 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700798 kfree(map);
799 kfree(attr);
800
NeilBrownd785a062006-06-26 00:27:48 -0700801 if (sb_page)
802 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700803}
804
805static void bitmap_file_put(struct bitmap *bitmap)
806{
807 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700808 unsigned long flags;
809
810 spin_lock_irqsave(&bitmap->lock, flags);
811 file = bitmap->file;
812 bitmap->file = NULL;
813 spin_unlock_irqrestore(&bitmap->lock, flags);
814
NeilBrownd785a062006-06-26 00:27:48 -0700815 if (file)
816 wait_event(bitmap->write_wait,
817 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700818 bitmap_file_unmap(bitmap);
819
NeilBrownd785a062006-06-26 00:27:48 -0700820 if (file) {
Josef Sipekc649bb92006-12-08 02:37:19 -0800821 struct inode *inode = file->f_path.dentry->d_inode;
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800822 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700823 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700824 }
NeilBrown32a76272005-06-21 17:17:14 -0700825}
826
NeilBrown32a76272005-06-21 17:17:14 -0700827/*
828 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
829 * then it is no longer reliable, so we stop using it and we mark the file
830 * as failed in the superblock
831 */
832static void bitmap_file_kick(struct bitmap *bitmap)
833{
834 char *path, *ptr = NULL;
835
NeilBrown4ad13662007-07-17 04:06:13 -0700836 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
837 bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700838
NeilBrown4ad13662007-07-17 04:06:13 -0700839 if (bitmap->file) {
840 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
841 if (path)
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700842 ptr = d_path(&bitmap->file->f_path, path,
843 PAGE_SIZE);
844
NeilBrown4ad13662007-07-17 04:06:13 -0700845 printk(KERN_ALERT
846 "%s: kicking failed bitmap file %s from array!\n",
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700847 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700848
NeilBrown4ad13662007-07-17 04:06:13 -0700849 kfree(path);
850 } else
851 printk(KERN_ALERT
852 "%s: disabling internal bitmap due to errors\n",
853 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700854 }
NeilBrown32a76272005-06-21 17:17:14 -0700855
856 bitmap_file_put(bitmap);
857
858 return;
859}
860
861enum bitmap_page_attr {
NeilBrownac2f40b2010-06-01 19:37:31 +1000862 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
NeilBrown5a537df2011-09-21 15:37:46 +1000863 BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
864 * i.e. counter is 1 or 2. */
NeilBrownac2f40b2010-06-01 19:37:31 +1000865 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
NeilBrown32a76272005-06-21 17:17:14 -0700866};
867
868static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
869 enum bitmap_page_attr attr)
870{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000871 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700872}
873
874static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
875 enum bitmap_page_attr attr)
876{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000877 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700878}
879
NeilBrownec7a3192006-06-26 00:27:45 -0700880static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
881 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700882{
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000883 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700884}
885
886/*
887 * bitmap_file_set_bit -- called before performing a write to the md device
888 * to set (and eventually sync) a particular bit in the bitmap file
889 *
890 * we set the bit immediately, then we record the page number so that
891 * when an unplug occurs, we can flush the dirty pages out to disk
892 */
893static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
894{
895 unsigned long bit;
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000896 struct page *page;
NeilBrown32a76272005-06-21 17:17:14 -0700897 void *kaddr;
898 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
899
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000900 if (!bitmap->filemap)
901 return;
NeilBrown32a76272005-06-21 17:17:14 -0700902
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000903 page = filemap_get_page(bitmap, chunk);
904 if (!page)
905 return;
906 bit = file_page_offset(bitmap, chunk);
NeilBrown32a76272005-06-21 17:17:14 -0700907
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000908 /* set the bit */
909 kaddr = kmap_atomic(page, KM_USER0);
910 if (bitmap->flags & BITMAP_HOSTENDIAN)
911 set_bit(bit, kaddr);
912 else
913 __set_bit_le(bit, kaddr);
914 kunmap_atomic(kaddr, KM_USER0);
NeilBrown36a4e1f2011-10-07 14:23:17 +1100915 pr_debug("set file bit %lu page %lu\n", bit, page->index);
NeilBrown32a76272005-06-21 17:17:14 -0700916 /* record page number so it gets flushed to disk when unplug occurs */
917 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
NeilBrown32a76272005-06-21 17:17:14 -0700918}
919
920/* this gets called when the md device is ready to unplug its underlying
921 * (slave) device queues -- before we let any writes go down, we need to
922 * sync the dirty pages of the bitmap file to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700923void bitmap_unplug(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700924{
NeilBrownec7a3192006-06-26 00:27:45 -0700925 unsigned long i, flags;
926 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700927 struct page *page;
928 int wait = 0;
929
930 if (!bitmap)
NeilBrown4ad13662007-07-17 04:06:13 -0700931 return;
NeilBrown32a76272005-06-21 17:17:14 -0700932
933 /* look at each page to see if there are any set bits that need to be
934 * flushed out to disk */
935 for (i = 0; i < bitmap->file_pages; i++) {
936 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrowna654b9d82005-06-21 17:17:27 -0700937 if (!bitmap->filemap) {
NeilBrown32a76272005-06-21 17:17:14 -0700938 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -0700939 return;
NeilBrown32a76272005-06-21 17:17:14 -0700940 }
941 page = bitmap->filemap[i];
NeilBrownec7a3192006-06-26 00:27:45 -0700942 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
943 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -0700944 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
945 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrownec7a3192006-06-26 00:27:45 -0700946 if (dirty)
NeilBrown32a76272005-06-21 17:17:14 -0700947 wait = 1;
948 spin_unlock_irqrestore(&bitmap->lock, flags);
949
NeilBrownac2f40b2010-06-01 19:37:31 +1000950 if (dirty || need_write)
NeilBrown4ad13662007-07-17 04:06:13 -0700951 write_page(bitmap, page, 0);
NeilBrown32a76272005-06-21 17:17:14 -0700952 }
953 if (wait) { /* if any writes were performed, we need to wait on them */
NeilBrown0b79ccf2006-06-26 00:27:44 -0700954 if (bitmap->file)
NeilBrownd785a062006-06-26 00:27:48 -0700955 wait_event(bitmap->write_wait,
956 atomic_read(&bitmap->pending_writes)==0);
NeilBrown0b79ccf2006-06-26 00:27:44 -0700957 else
NeilBrowna9701a32005-11-08 21:39:34 -0800958 md_super_wait(bitmap->mddev);
NeilBrown32a76272005-06-21 17:17:14 -0700959 }
NeilBrownd785a062006-06-26 00:27:48 -0700960 if (bitmap->flags & BITMAP_WRITE_ERROR)
961 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700962}
NeilBrownac2f40b2010-06-01 19:37:31 +1000963EXPORT_SYMBOL(bitmap_unplug);
NeilBrown32a76272005-06-21 17:17:14 -0700964
NeilBrown6a079972005-09-09 16:23:44 -0700965static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700966/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
967 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
968 * memory mapping of the bitmap file
969 * Special cases:
970 * if there's no bitmap file, or if the bitmap file had been
971 * previously kicked from the array, we mark all the bits as
972 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700973 *
974 * We ignore all bits for sectors that end earlier than 'start'.
975 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700976 */
NeilBrown6a079972005-09-09 16:23:44 -0700977static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700978{
979 unsigned long i, chunks, index, oldindex, bit;
980 struct page *page = NULL, *oldpage = NULL;
981 unsigned long num_pages, bit_cnt = 0;
982 struct file *file;
NeilBrownd785a062006-06-26 00:27:48 -0700983 unsigned long bytes, offset;
NeilBrown32a76272005-06-21 17:17:14 -0700984 int outofdate;
985 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800986 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -0700987
988 chunks = bitmap->chunks;
989 file = bitmap->file;
990
NeilBrown42a04b52009-12-14 12:49:53 +1100991 BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
NeilBrown32a76272005-06-21 17:17:14 -0700992
Olaf Hering44456d32005-07-27 11:45:17 -0700993#ifdef INJECT_FAULTS_3
NeilBrown32a76272005-06-21 17:17:14 -0700994 outofdate = 1;
995#else
996 outofdate = bitmap->flags & BITMAP_STALE;
997#endif
998 if (outofdate)
999 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
1000 "recovery\n", bmname(bitmap));
1001
NeilBrowne384e582010-06-01 19:37:34 +10001002 bytes = DIV_ROUND_UP(bitmap->chunks, 8);
NeilBrownece5cff2009-12-14 12:49:56 +11001003 if (!bitmap->mddev->bitmap_info.external)
1004 bytes += sizeof(bitmap_super_t);
NeilBrownbc7f77d2005-06-21 17:17:17 -07001005
NeilBrowne384e582010-06-01 19:37:34 +10001006 num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
NeilBrownbc7f77d2005-06-21 17:17:17 -07001007
NeilBrownece5cff2009-12-14 12:49:56 +11001008 if (file && i_size_read(file->f_mapping->host) < bytes) {
NeilBrown32a76272005-06-21 17:17:14 -07001009 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
1010 bmname(bitmap),
1011 (unsigned long) i_size_read(file->f_mapping->host),
NeilBrownece5cff2009-12-14 12:49:56 +11001012 bytes);
NeilBrown4ad13662007-07-17 04:06:13 -07001013 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001014 }
NeilBrownbc7f77d2005-06-21 17:17:17 -07001015
1016 ret = -ENOMEM;
1017
NeilBrown32a76272005-06-21 17:17:14 -07001018 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -07001019 if (!bitmap->filemap)
NeilBrown4ad13662007-07-17 04:06:13 -07001020 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001021
NeilBrowne16b68b2006-06-26 00:27:45 -07001022 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
1023 bitmap->filemap_attr = kzalloc(
NeilBrownac2f40b2010-06-01 19:37:31 +10001024 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
NeilBrowne16b68b2006-06-26 00:27:45 -07001025 GFP_KERNEL);
NeilBrownbc7f77d2005-06-21 17:17:17 -07001026 if (!bitmap->filemap_attr)
NeilBrown4ad13662007-07-17 04:06:13 -07001027 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001028
NeilBrown32a76272005-06-21 17:17:14 -07001029 oldindex = ~0L;
1030
1031 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -08001032 int b;
NeilBrownece5cff2009-12-14 12:49:56 +11001033 index = file_page_index(bitmap, i);
1034 bit = file_page_offset(bitmap, i);
NeilBrown32a76272005-06-21 17:17:14 -07001035 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -07001036 int count;
NeilBrown32a76272005-06-21 17:17:14 -07001037 /* unmap the old page, we're done with it */
NeilBrownd785a062006-06-26 00:27:48 -07001038 if (index == num_pages-1)
NeilBrownece5cff2009-12-14 12:49:56 +11001039 count = bytes - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -07001040 else
1041 count = PAGE_SIZE;
NeilBrownece5cff2009-12-14 12:49:56 +11001042 if (index == 0 && bitmap->sb_page) {
NeilBrown32a76272005-06-21 17:17:14 -07001043 /*
1044 * if we're here then the superblock page
1045 * contains some bits (PAGE_SIZE != sizeof sb)
1046 * we've already read it in, so just use it
1047 */
1048 page = bitmap->sb_page;
1049 offset = sizeof(bitmap_super_t);
NeilBrown53845272009-01-09 08:31:05 +11001050 if (!file)
Vasiliy Kulikov5c04f552010-10-01 14:18:12 -07001051 page = read_sb_page(
1052 bitmap->mddev,
1053 bitmap->mddev->bitmap_info.offset,
1054 page,
1055 index, count);
NeilBrowna654b9d82005-06-21 17:17:27 -07001056 } else if (file) {
NeilBrownd785a062006-06-26 00:27:48 -07001057 page = read_page(file, index, bitmap, count);
NeilBrowna654b9d82005-06-21 17:17:27 -07001058 offset = 0;
1059 } else {
NeilBrown42a04b52009-12-14 12:49:53 +11001060 page = read_sb_page(bitmap->mddev,
1061 bitmap->mddev->bitmap_info.offset,
NeilBrowna2ed9612008-12-19 16:25:01 +11001062 NULL,
1063 index, count);
NeilBrown32a76272005-06-21 17:17:14 -07001064 offset = 0;
1065 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001066 if (IS_ERR(page)) { /* read error */
1067 ret = PTR_ERR(page);
NeilBrown4ad13662007-07-17 04:06:13 -07001068 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -07001069 }
1070
NeilBrown32a76272005-06-21 17:17:14 -07001071 oldindex = index;
1072 oldpage = page;
NeilBrown32a76272005-06-21 17:17:14 -07001073
NeilBrownb74fd282009-05-07 12:47:19 +10001074 bitmap->filemap[bitmap->file_pages++] = page;
1075 bitmap->last_page_size = count;
1076
NeilBrown32a76272005-06-21 17:17:14 -07001077 if (outofdate) {
1078 /*
1079 * if bitmap is out of date, dirty the
NeilBrownac2f40b2010-06-01 19:37:31 +10001080 * whole page and write it out
NeilBrown32a76272005-06-21 17:17:14 -07001081 */
NeilBrownea03aff2006-01-06 00:20:34 -08001082 paddr = kmap_atomic(page, KM_USER0);
1083 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -07001084 PAGE_SIZE - offset);
NeilBrownea03aff2006-01-06 00:20:34 -08001085 kunmap_atomic(paddr, KM_USER0);
NeilBrown4ad13662007-07-17 04:06:13 -07001086 write_page(bitmap, page, 1);
1087
1088 ret = -EIO;
NeilBrownb74fd282009-05-07 12:47:19 +10001089 if (bitmap->flags & BITMAP_WRITE_ERROR)
NeilBrown4ad13662007-07-17 04:06:13 -07001090 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001091 }
NeilBrown32a76272005-06-21 17:17:14 -07001092 }
NeilBrownea03aff2006-01-06 00:20:34 -08001093 paddr = kmap_atomic(page, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001094 if (bitmap->flags & BITMAP_HOSTENDIAN)
NeilBrownea03aff2006-01-06 00:20:34 -08001095 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001096 else
Akinobu Mita6b33aff2011-03-23 16:42:13 -07001097 b = test_bit_le(bit, paddr);
NeilBrownea03aff2006-01-06 00:20:34 -08001098 kunmap_atomic(paddr, KM_USER0);
NeilBrownbd926c62005-11-08 21:39:32 -08001099 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -07001100 /* if the disk bit is set, set the memory bit */
NeilBrowndb305e52009-05-07 12:49:06 +10001101 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1102 >= start);
1103 bitmap_set_memory_bits(bitmap,
1104 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1105 needed);
NeilBrown32a76272005-06-21 17:17:14 -07001106 bit_cnt++;
1107 }
NeilBrown32a76272005-06-21 17:17:14 -07001108 }
1109
NeilBrownac2f40b2010-06-01 19:37:31 +10001110 /* everything went OK */
NeilBrown32a76272005-06-21 17:17:14 -07001111 ret = 0;
1112 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1113
NeilBrown32a76272005-06-21 17:17:14 -07001114 if (bit_cnt) { /* Kick recovery if any bits were set */
1115 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1116 md_wakeup_thread(bitmap->mddev->thread);
1117 }
1118
NeilBrown32a76272005-06-21 17:17:14 -07001119 printk(KERN_INFO "%s: bitmap initialized from disk: "
Jonathan Brassow9c810752011-06-08 17:59:30 -05001120 "read %lu/%lu pages, set %lu of %lu bits\n",
1121 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, chunks);
NeilBrown32a76272005-06-21 17:17:14 -07001122
NeilBrown4ad13662007-07-17 04:06:13 -07001123 return 0;
1124
1125 err:
1126 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1127 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001128 return ret;
1129}
1130
NeilBrowna654b9d82005-06-21 17:17:27 -07001131void bitmap_write_all(struct bitmap *bitmap)
1132{
1133 /* We don't actually write all bitmap blocks here,
1134 * just flag them as needing to be written
1135 */
NeilBrownec7a3192006-06-26 00:27:45 -07001136 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001137
NeilBrownac2f40b2010-06-01 19:37:31 +10001138 for (i = 0; i < bitmap->file_pages; i++)
NeilBrownec7a3192006-06-26 00:27:45 -07001139 set_page_attr(bitmap, bitmap->filemap[i],
1140 BITMAP_PAGE_NEEDWRITE);
NeilBrown2585f3e2011-09-21 15:37:46 +10001141 bitmap->allclean = 0;
NeilBrowna654b9d82005-06-21 17:17:27 -07001142}
1143
NeilBrown32a76272005-06-21 17:17:14 -07001144static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1145{
1146 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1147 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1148 bitmap->bp[page].count += inc;
NeilBrown32a76272005-06-21 17:17:14 -07001149 bitmap_checkfree(bitmap, page);
1150}
1151static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001152 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001153 int create);
1154
1155/*
1156 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1157 * out to disk
1158 */
1159
NeilBrownaa5cbd12009-12-14 12:49:46 +11001160void bitmap_daemon_work(mddev_t *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001161{
NeilBrownaa5cbd12009-12-14 12:49:46 +11001162 struct bitmap *bitmap;
NeilBrownaa3163f2005-06-21 17:17:22 -07001163 unsigned long j;
NeilBrown32a76272005-06-21 17:17:14 -07001164 unsigned long flags;
1165 struct page *page = NULL, *lastpage = NULL;
NeilBrown57dab0b2010-10-19 10:03:39 +11001166 sector_t blocks;
NeilBrownea03aff2006-01-06 00:20:34 -08001167 void *paddr;
NeilBrown32a76272005-06-21 17:17:14 -07001168
NeilBrownaa5cbd12009-12-14 12:49:46 +11001169 /* Use a mutex to guard daemon_work against
1170 * bitmap_destroy.
1171 */
NeilBrownc3d97142009-12-14 12:49:52 +11001172 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrownaa5cbd12009-12-14 12:49:46 +11001173 bitmap = mddev->bitmap;
1174 if (bitmap == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001175 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown4ad13662007-07-17 04:06:13 -07001176 return;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001177 }
NeilBrown42a04b52009-12-14 12:49:53 +11001178 if (time_before(jiffies, bitmap->daemon_lastrun
NeilBrown1b04be92009-12-14 12:49:53 +11001179 + bitmap->mddev->bitmap_info.daemon_sleep))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001180 goto done;
1181
NeilBrown32a76272005-06-21 17:17:14 -07001182 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001183 if (bitmap->allclean) {
1184 bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001185 goto done;
NeilBrown8311c292008-03-04 14:29:30 -08001186 }
1187 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001188
NeilBrownbe512692009-05-26 09:41:17 +10001189 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001190 for (j = 0; j < bitmap->chunks; j++) {
1191 bitmap_counter_t *bmc;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001192 if (!bitmap->filemap)
1193 /* error or shutdown */
1194 break;
1195
1196 page = filemap_get_page(bitmap, j);
NeilBrown32a76272005-06-21 17:17:14 -07001197
1198 if (page != lastpage) {
NeilBrownaa3163f2005-06-21 17:17:22 -07001199 /* skip this page unless it's marked as needing cleaning */
NeilBrown5a537df2011-09-21 15:37:46 +10001200 if (!test_page_attr(bitmap, page, BITMAP_PAGE_PENDING)) {
NeilBrownec7a3192006-06-26 00:27:45 -07001201 int need_write = test_page_attr(bitmap, page,
1202 BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001203 if (need_write)
NeilBrownaa3163f2005-06-21 17:17:22 -07001204 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
NeilBrowna647e4b2006-06-26 00:27:46 -07001205
NeilBrownaa3163f2005-06-21 17:17:22 -07001206 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown2585f3e2011-09-21 15:37:46 +10001207 if (need_write)
NeilBrown4ad13662007-07-17 04:06:13 -07001208 write_page(bitmap, page, 0);
NeilBrownbe512692009-05-26 09:41:17 +10001209 spin_lock_irqsave(&bitmap->lock, flags);
1210 j |= (PAGE_BITS - 1);
NeilBrownaa3163f2005-06-21 17:17:22 -07001211 continue;
1212 }
1213
NeilBrown32a76272005-06-21 17:17:14 -07001214 /* grab the new page, sync and release the old */
NeilBrown32a76272005-06-21 17:17:14 -07001215 if (lastpage != NULL) {
NeilBrown2585f3e2011-09-21 15:37:46 +10001216 if (test_page_attr(bitmap, lastpage,
1217 BITMAP_PAGE_NEEDWRITE)) {
1218 clear_page_attr(bitmap, lastpage,
1219 BITMAP_PAGE_NEEDWRITE);
NeilBrown32a76272005-06-21 17:17:14 -07001220 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown4ad13662007-07-17 04:06:13 -07001221 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001222 } else {
NeilBrown2585f3e2011-09-21 15:37:46 +10001223 set_page_attr(bitmap, lastpage,
1224 BITMAP_PAGE_NEEDWRITE);
1225 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001226 spin_unlock_irqrestore(&bitmap->lock, flags);
1227 }
NeilBrown32a76272005-06-21 17:17:14 -07001228 } else
1229 spin_unlock_irqrestore(&bitmap->lock, flags);
1230 lastpage = page;
Neil Browna0da84f2008-06-28 08:31:22 +10001231
1232 /* We are possibly going to clear some bits, so make
1233 * sure that events_cleared is up-to-date.
1234 */
NeilBrownece5cff2009-12-14 12:49:56 +11001235 if (bitmap->need_sync &&
1236 bitmap->mddev->bitmap_info.external == 0) {
Neil Browna0da84f2008-06-28 08:31:22 +10001237 bitmap_super_t *sb;
1238 bitmap->need_sync = 0;
1239 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1240 sb->events_cleared =
1241 cpu_to_le64(bitmap->events_cleared);
1242 kunmap_atomic(sb, KM_USER0);
1243 write_page(bitmap, bitmap->sb_page, 1);
1244 }
NeilBrown32a76272005-06-21 17:17:14 -07001245 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownece5cff2009-12-14 12:49:56 +11001246 if (!bitmap->need_sync)
NeilBrown5a537df2011-09-21 15:37:46 +10001247 clear_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001248 else
1249 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001250 }
NeilBrowndb305e52009-05-07 12:49:06 +10001251 bmc = bitmap_get_counter(bitmap,
1252 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1253 &blocks, 0);
NeilBrown5a537df2011-09-21 15:37:46 +10001254 if (!bmc)
1255 j |= PAGE_COUNTER_MASK;
1256 else if (*bmc) {
NeilBrown5a537df2011-09-21 15:37:46 +10001257 if (*bmc == 1 && !bitmap->need_sync) {
NeilBrown32a76272005-06-21 17:17:14 -07001258 /* we can clear the bit */
1259 *bmc = 0;
NeilBrowndb305e52009-05-07 12:49:06 +10001260 bitmap_count_page(bitmap,
1261 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
NeilBrown32a76272005-06-21 17:17:14 -07001262 -1);
1263
1264 /* clear the bit */
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001265 paddr = kmap_atomic(page, KM_USER0);
1266 if (bitmap->flags & BITMAP_HOSTENDIAN)
1267 clear_bit(file_page_offset(bitmap, j),
1268 paddr);
1269 else
1270 __clear_bit_le(
NeilBrown5a537df2011-09-21 15:37:46 +10001271 file_page_offset(bitmap,
1272 j),
1273 paddr);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001274 kunmap_atomic(paddr, KM_USER0);
NeilBrown5a537df2011-09-21 15:37:46 +10001275 } else if (*bmc <= 2) {
1276 *bmc = 1; /* maybe clear the bit next time */
1277 set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001278 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001279 }
NeilBrown5a537df2011-09-21 15:37:46 +10001280 }
NeilBrown32a76272005-06-21 17:17:14 -07001281 }
NeilBrownbe512692009-05-26 09:41:17 +10001282 spin_unlock_irqrestore(&bitmap->lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001283
1284 /* now sync the final page */
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001285 if (lastpage != NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001286 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrownec7a3192006-06-26 00:27:45 -07001287 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
NeilBrown32a76272005-06-21 17:17:14 -07001288 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1289 spin_unlock_irqrestore(&bitmap->lock, flags);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001290 write_page(bitmap, lastpage, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001291 } else {
1292 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
NeilBrown2585f3e2011-09-21 15:37:46 +10001293 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001294 spin_unlock_irqrestore(&bitmap->lock, flags);
1295 }
NeilBrown32a76272005-06-21 17:17:14 -07001296 }
1297
NeilBrown7be3dfe2008-03-10 11:43:48 -07001298 done:
NeilBrown8311c292008-03-04 14:29:30 -08001299 if (bitmap->allclean == 0)
NeilBrownac2f40b2010-06-01 19:37:31 +10001300 bitmap->mddev->thread->timeout =
NeilBrown1b04be92009-12-14 12:49:53 +11001301 bitmap->mddev->bitmap_info.daemon_sleep;
NeilBrownc3d97142009-12-14 12:49:52 +11001302 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown32a76272005-06-21 17:17:14 -07001303}
1304
NeilBrown32a76272005-06-21 17:17:14 -07001305static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001306 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001307 int create)
NeilBrownee305ac2009-09-23 18:06:44 +10001308__releases(bitmap->lock)
1309__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -07001310{
1311 /* If 'create', we might release the lock and reclaim it.
1312 * The lock must have been taken with interrupts enabled.
1313 * If !create, we don't release the lock.
1314 */
1315 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1316 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1317 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1318 sector_t csize;
NeilBrownef425672010-06-01 19:37:33 +10001319 int err;
NeilBrown32a76272005-06-21 17:17:14 -07001320
NeilBrownef425672010-06-01 19:37:33 +10001321 err = bitmap_checkpage(bitmap, page, create);
1322
1323 if (bitmap->bp[page].hijacked ||
1324 bitmap->bp[page].map == NULL)
1325 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1326 PAGE_COUNTER_SHIFT - 1);
1327 else
NeilBrown32a76272005-06-21 17:17:14 -07001328 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
NeilBrownef425672010-06-01 19:37:33 +10001329 *blocks = csize - (offset & (csize - 1));
1330
1331 if (err < 0)
NeilBrown32a76272005-06-21 17:17:14 -07001332 return NULL;
NeilBrownef425672010-06-01 19:37:33 +10001333
NeilBrown32a76272005-06-21 17:17:14 -07001334 /* now locked ... */
1335
1336 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1337 /* should we use the first or second counter field
1338 * of the hijacked pointer? */
1339 int hi = (pageoff > PAGE_COUNTER_MASK);
NeilBrown32a76272005-06-21 17:17:14 -07001340 return &((bitmap_counter_t *)
1341 &bitmap->bp[page].map)[hi];
NeilBrownef425672010-06-01 19:37:33 +10001342 } else /* page is allocated */
NeilBrown32a76272005-06-21 17:17:14 -07001343 return (bitmap_counter_t *)
1344 &(bitmap->bp[page].map[pageoff]);
NeilBrown32a76272005-06-21 17:17:14 -07001345}
1346
NeilBrown4b6d2872005-09-09 16:23:47 -07001347int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001348{
NeilBrownac2f40b2010-06-01 19:37:31 +10001349 if (!bitmap)
1350 return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001351
1352 if (behind) {
Paul Clements696fcd52010-03-08 16:02:37 +11001353 int bw;
NeilBrown4b6d2872005-09-09 16:23:47 -07001354 atomic_inc(&bitmap->behind_writes);
Paul Clements696fcd52010-03-08 16:02:37 +11001355 bw = atomic_read(&bitmap->behind_writes);
1356 if (bw > bitmap->behind_writes_used)
1357 bitmap->behind_writes_used = bw;
1358
NeilBrown36a4e1f2011-10-07 14:23:17 +11001359 pr_debug("inc write-behind count %d/%lu\n",
1360 bw, bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001361 }
1362
NeilBrown32a76272005-06-21 17:17:14 -07001363 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001364 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001365 bitmap_counter_t *bmc;
1366
1367 spin_lock_irq(&bitmap->lock);
1368 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1369 if (!bmc) {
1370 spin_unlock_irq(&bitmap->lock);
1371 return 0;
1372 }
1373
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001374 if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
Neil Brownda6e1a32007-02-08 14:20:37 -08001375 DEFINE_WAIT(__wait);
1376 /* note that it is safe to do the prepare_to_wait
1377 * after the test as long as we do it before dropping
1378 * the spinlock.
1379 */
1380 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1381 TASK_UNINTERRUPTIBLE);
1382 spin_unlock_irq(&bitmap->lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001383 io_schedule();
Neil Brownda6e1a32007-02-08 14:20:37 -08001384 finish_wait(&bitmap->overflow_wait, &__wait);
1385 continue;
1386 }
1387
NeilBrownac2f40b2010-06-01 19:37:31 +10001388 switch (*bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001389 case 0:
1390 bitmap_file_set_bit(bitmap, offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001391 bitmap_count_page(bitmap, offset, 1);
NeilBrown32a76272005-06-21 17:17:14 -07001392 /* fall through */
1393 case 1:
1394 *bmc = 2;
1395 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001396
NeilBrown32a76272005-06-21 17:17:14 -07001397 (*bmc)++;
1398
1399 spin_unlock_irq(&bitmap->lock);
1400
1401 offset += blocks;
1402 if (sectors > blocks)
1403 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001404 else
1405 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001406 }
1407 return 0;
1408}
NeilBrownac2f40b2010-06-01 19:37:31 +10001409EXPORT_SYMBOL(bitmap_startwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001410
1411void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001412 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001413{
NeilBrownac2f40b2010-06-01 19:37:31 +10001414 if (!bitmap)
1415 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001416 if (behind) {
NeilBrowne5551902010-03-31 11:21:44 +11001417 if (atomic_dec_and_test(&bitmap->behind_writes))
1418 wake_up(&bitmap->behind_wait);
NeilBrown36a4e1f2011-10-07 14:23:17 +11001419 pr_debug("dec write-behind count %d/%lu\n",
1420 atomic_read(&bitmap->behind_writes),
1421 bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001422 }
NeilBrownd0a4bb42009-03-31 14:27:02 +11001423 if (bitmap->mddev->degraded)
1424 /* Never clear bits or update events_cleared when degraded */
1425 success = 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001426
NeilBrown32a76272005-06-21 17:17:14 -07001427 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001428 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001429 unsigned long flags;
1430 bitmap_counter_t *bmc;
1431
1432 spin_lock_irqsave(&bitmap->lock, flags);
1433 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1434 if (!bmc) {
1435 spin_unlock_irqrestore(&bitmap->lock, flags);
1436 return;
1437 }
1438
Neil Browna0da84f2008-06-28 08:31:22 +10001439 if (success &&
1440 bitmap->events_cleared < bitmap->mddev->events) {
1441 bitmap->events_cleared = bitmap->mddev->events;
1442 bitmap->need_sync = 1;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001443 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
Neil Browna0da84f2008-06-28 08:31:22 +10001444 }
1445
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001446 if (!success && !NEEDED(*bmc))
NeilBrown32a76272005-06-21 17:17:14 -07001447 *bmc |= NEEDED_MASK;
1448
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001449 if (COUNTER(*bmc) == COUNTER_MAX)
Neil Brownda6e1a32007-02-08 14:20:37 -08001450 wake_up(&bitmap->overflow_wait);
1451
NeilBrown32a76272005-06-21 17:17:14 -07001452 (*bmc)--;
NeilBrown2585f3e2011-09-21 15:37:46 +10001453 if (*bmc <= 2) {
NeilBrown32a76272005-06-21 17:17:14 -07001454 set_page_attr(bitmap,
NeilBrowne384e582010-06-01 19:37:34 +10001455 filemap_get_page(
1456 bitmap,
1457 offset >> CHUNK_BLOCK_SHIFT(bitmap)),
NeilBrown5a537df2011-09-21 15:37:46 +10001458 BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001459 bitmap->allclean = 0;
1460 }
NeilBrown32a76272005-06-21 17:17:14 -07001461 spin_unlock_irqrestore(&bitmap->lock, flags);
1462 offset += blocks;
1463 if (sectors > blocks)
1464 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001465 else
1466 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001467 }
1468}
NeilBrownac2f40b2010-06-01 19:37:31 +10001469EXPORT_SYMBOL(bitmap_endwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001470
NeilBrown57dab0b2010-10-19 10:03:39 +11001471static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001472 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001473{
1474 bitmap_counter_t *bmc;
1475 int rv;
1476 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1477 *blocks = 1024;
1478 return 1; /* always resync if no bitmap */
1479 }
1480 spin_lock_irq(&bitmap->lock);
1481 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1482 rv = 0;
1483 if (bmc) {
1484 /* locked */
1485 if (RESYNC(*bmc))
1486 rv = 1;
1487 else if (NEEDED(*bmc)) {
1488 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001489 if (!degraded) { /* don't set/clear bits if degraded */
1490 *bmc |= RESYNC_MASK;
1491 *bmc &= ~NEEDED_MASK;
1492 }
NeilBrown32a76272005-06-21 17:17:14 -07001493 }
1494 }
1495 spin_unlock_irq(&bitmap->lock);
1496 return rv;
1497}
1498
NeilBrown57dab0b2010-10-19 10:03:39 +11001499int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001500 int degraded)
1501{
1502 /* bitmap_start_sync must always report on multiples of whole
1503 * pages, otherwise resync (which is very PAGE_SIZE based) will
1504 * get confused.
1505 * So call __bitmap_start_sync repeatedly (if needed) until
1506 * At least PAGE_SIZE>>9 blocks are covered.
1507 * Return the 'or' of the result.
1508 */
1509 int rv = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001510 sector_t blocks1;
NeilBrown1187cf02009-03-31 14:27:02 +11001511
1512 *blocks = 0;
1513 while (*blocks < (PAGE_SIZE>>9)) {
1514 rv |= __bitmap_start_sync(bitmap, offset,
1515 &blocks1, degraded);
1516 offset += blocks1;
1517 *blocks += blocks1;
1518 }
1519 return rv;
1520}
NeilBrownac2f40b2010-06-01 19:37:31 +10001521EXPORT_SYMBOL(bitmap_start_sync);
NeilBrown1187cf02009-03-31 14:27:02 +11001522
NeilBrown57dab0b2010-10-19 10:03:39 +11001523void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
NeilBrown32a76272005-06-21 17:17:14 -07001524{
1525 bitmap_counter_t *bmc;
1526 unsigned long flags;
NeilBrownac2f40b2010-06-01 19:37:31 +10001527
1528 if (bitmap == NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001529 *blocks = 1024;
1530 return;
1531 }
1532 spin_lock_irqsave(&bitmap->lock, flags);
1533 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1534 if (bmc == NULL)
1535 goto unlock;
1536 /* locked */
NeilBrown32a76272005-06-21 17:17:14 -07001537 if (RESYNC(*bmc)) {
1538 *bmc &= ~RESYNC_MASK;
1539
1540 if (!NEEDED(*bmc) && aborted)
1541 *bmc |= NEEDED_MASK;
1542 else {
NeilBrown2585f3e2011-09-21 15:37:46 +10001543 if (*bmc <= 2) {
NeilBrown32a76272005-06-21 17:17:14 -07001544 set_page_attr(bitmap,
1545 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
NeilBrown5a537df2011-09-21 15:37:46 +10001546 BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001547 bitmap->allclean = 0;
1548 }
NeilBrown32a76272005-06-21 17:17:14 -07001549 }
1550 }
1551 unlock:
1552 spin_unlock_irqrestore(&bitmap->lock, flags);
1553}
NeilBrownac2f40b2010-06-01 19:37:31 +10001554EXPORT_SYMBOL(bitmap_end_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001555
1556void bitmap_close_sync(struct bitmap *bitmap)
1557{
1558 /* Sync has finished, and any bitmap chunks that weren't synced
1559 * properly have been aborted. It remains to us to clear the
1560 * RESYNC bit wherever it is still on
1561 */
1562 sector_t sector = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001563 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001564 if (!bitmap)
1565 return;
NeilBrown32a76272005-06-21 17:17:14 -07001566 while (sector < bitmap->mddev->resync_max_sectors) {
1567 bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001568 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001569 }
1570}
NeilBrownac2f40b2010-06-01 19:37:31 +10001571EXPORT_SYMBOL(bitmap_close_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001572
NeilBrownb47490c2008-02-06 01:39:50 -08001573void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1574{
1575 sector_t s = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001576 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001577
1578 if (!bitmap)
1579 return;
1580 if (sector == 0) {
1581 bitmap->last_end_sync = jiffies;
1582 return;
1583 }
1584 if (time_before(jiffies, (bitmap->last_end_sync
NeilBrown1b04be92009-12-14 12:49:53 +11001585 + bitmap->mddev->bitmap_info.daemon_sleep)))
NeilBrownb47490c2008-02-06 01:39:50 -08001586 return;
1587 wait_event(bitmap->mddev->recovery_wait,
1588 atomic_read(&bitmap->mddev->recovery_active) == 0);
1589
NeilBrown75d3da42011-01-14 09:14:34 +11001590 bitmap->mddev->curr_resync_completed = sector;
NeilBrown070dc6d2010-08-30 17:33:34 +10001591 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
NeilBrownb47490c2008-02-06 01:39:50 -08001592 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1593 s = 0;
1594 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1595 bitmap_end_sync(bitmap, s, &blocks, 0);
1596 s += blocks;
1597 }
1598 bitmap->last_end_sync = jiffies;
NeilBrownacb180b2009-04-14 16:28:34 +10001599 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
NeilBrownb47490c2008-02-06 01:39:50 -08001600}
NeilBrownac2f40b2010-06-01 19:37:31 +10001601EXPORT_SYMBOL(bitmap_cond_end_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08001602
NeilBrown6a079972005-09-09 16:23:44 -07001603static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001604{
1605 /* For each chunk covered by any of these sectors, set the
NeilBrown193f1c92005-08-04 12:53:33 -07001606 * counter to 1 and set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001607 * be 0 at this point
1608 */
NeilBrown193f1c92005-08-04 12:53:33 -07001609
NeilBrown57dab0b2010-10-19 10:03:39 +11001610 sector_t secs;
NeilBrown193f1c92005-08-04 12:53:33 -07001611 bitmap_counter_t *bmc;
1612 spin_lock_irq(&bitmap->lock);
1613 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1614 if (!bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001615 spin_unlock_irq(&bitmap->lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001616 return;
NeilBrown32a76272005-06-21 17:17:14 -07001617 }
NeilBrownac2f40b2010-06-01 19:37:31 +10001618 if (!*bmc) {
NeilBrown193f1c92005-08-04 12:53:33 -07001619 struct page *page;
NeilBrownac2f40b2010-06-01 19:37:31 +10001620 *bmc = 1 | (needed ? NEEDED_MASK : 0);
NeilBrown193f1c92005-08-04 12:53:33 -07001621 bitmap_count_page(bitmap, offset, 1);
1622 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
NeilBrown5a537df2011-09-21 15:37:46 +10001623 set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
NeilBrown2585f3e2011-09-21 15:37:46 +10001624 bitmap->allclean = 0;
NeilBrown193f1c92005-08-04 12:53:33 -07001625 }
1626 spin_unlock_irq(&bitmap->lock);
NeilBrown32a76272005-06-21 17:17:14 -07001627}
1628
Paul Clements9b1d1da2006-10-03 01:15:49 -07001629/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1630void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1631{
1632 unsigned long chunk;
1633
1634 for (chunk = s; chunk <= e; chunk++) {
NeilBrowndb305e52009-05-07 12:49:06 +10001635 sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
Paul Clements9b1d1da2006-10-03 01:15:49 -07001636 bitmap_set_memory_bits(bitmap, sec, 1);
1637 bitmap_file_set_bit(bitmap, sec);
NeilBrownffa23322009-12-14 12:49:56 +11001638 if (sec < bitmap->mddev->recovery_cp)
1639 /* We are asserting that the array is dirty,
1640 * so move the recovery_cp address back so
1641 * that it is obvious that it is dirty
1642 */
1643 bitmap->mddev->recovery_cp = sec;
Paul Clements9b1d1da2006-10-03 01:15:49 -07001644 }
1645}
1646
NeilBrown32a76272005-06-21 17:17:14 -07001647/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001648 * flush out any pending updates
1649 */
1650void bitmap_flush(mddev_t *mddev)
1651{
1652 struct bitmap *bitmap = mddev->bitmap;
NeilBrown42a04b52009-12-14 12:49:53 +11001653 long sleep;
NeilBrown6b8b3e82005-08-04 12:53:35 -07001654
1655 if (!bitmap) /* there was no bitmap */
1656 return;
1657
1658 /* run the daemon_work three time to ensure everything is flushed
1659 * that can be
1660 */
NeilBrown1b04be92009-12-14 12:49:53 +11001661 sleep = mddev->bitmap_info.daemon_sleep * 2;
NeilBrown42a04b52009-12-14 12:49:53 +11001662 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001663 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001664 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001665 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001666 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001667 bitmap_daemon_work(mddev);
NeilBrown6b8b3e82005-08-04 12:53:35 -07001668 bitmap_update_sb(bitmap);
1669}
1670
1671/*
NeilBrown32a76272005-06-21 17:17:14 -07001672 * free memory that was allocated
1673 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001674static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001675{
1676 unsigned long k, pages;
1677 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001678
1679 if (!bitmap) /* there was no bitmap */
1680 return;
1681
NeilBrown32a76272005-06-21 17:17:14 -07001682 /* release the bitmap file and kill the daemon */
1683 bitmap_file_put(bitmap);
1684
1685 bp = bitmap->bp;
1686 pages = bitmap->pages;
1687
1688 /* free all allocated memory */
1689
NeilBrown32a76272005-06-21 17:17:14 -07001690 if (bp) /* deallocate the page memory */
1691 for (k = 0; k < pages; k++)
1692 if (bp[k].map && !bp[k].hijacked)
1693 kfree(bp[k].map);
1694 kfree(bp);
1695 kfree(bitmap);
1696}
NeilBrownaa5cbd12009-12-14 12:49:46 +11001697
NeilBrown3178b0d2005-09-09 16:23:50 -07001698void bitmap_destroy(mddev_t *mddev)
1699{
1700 struct bitmap *bitmap = mddev->bitmap;
1701
1702 if (!bitmap) /* there was no bitmap */
1703 return;
1704
NeilBrownc3d97142009-12-14 12:49:52 +11001705 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrown3178b0d2005-09-09 16:23:50 -07001706 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrownc3d97142009-12-14 12:49:52 +11001707 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrownb15c2e52006-01-06 00:20:16 -08001708 if (mddev->thread)
1709 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001710
NeilBrownece5cff2009-12-14 12:49:56 +11001711 if (bitmap->sysfs_can_clear)
1712 sysfs_put(bitmap->sysfs_can_clear);
1713
NeilBrown3178b0d2005-09-09 16:23:50 -07001714 bitmap_free(bitmap);
1715}
NeilBrown32a76272005-06-21 17:17:14 -07001716
1717/*
1718 * initialize the bitmap structure
1719 * if this returns an error, bitmap_destroy must be called to do clean up
1720 */
1721int bitmap_create(mddev_t *mddev)
1722{
1723 struct bitmap *bitmap;
NeilBrown1f593902009-04-20 11:50:24 +10001724 sector_t blocks = mddev->resync_max_sectors;
NeilBrown32a76272005-06-21 17:17:14 -07001725 unsigned long chunks;
1726 unsigned long pages;
NeilBrownc3d97142009-12-14 12:49:52 +11001727 struct file *file = mddev->bitmap_info.file;
NeilBrown32a76272005-06-21 17:17:14 -07001728 int err;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001729 struct sysfs_dirent *bm = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001730
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001731 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001732
NeilBrowne384e582010-06-01 19:37:34 +10001733 if (!file
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001734 && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
NeilBrown32a76272005-06-21 17:17:14 -07001735 return 0;
1736
NeilBrownc3d97142009-12-14 12:49:52 +11001737 BUG_ON(file && mddev->bitmap_info.offset);
NeilBrowna654b9d82005-06-21 17:17:27 -07001738
NeilBrown9ffae0c2006-01-06 00:20:32 -08001739 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001740 if (!bitmap)
1741 return -ENOMEM;
1742
NeilBrown32a76272005-06-21 17:17:14 -07001743 spin_lock_init(&bitmap->lock);
NeilBrownce25c312006-06-26 00:27:49 -07001744 atomic_set(&bitmap->pending_writes, 0);
1745 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001746 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrowne5551902010-03-31 11:21:44 +11001747 init_waitqueue_head(&bitmap->behind_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001748
NeilBrown32a76272005-06-21 17:17:14 -07001749 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001750
NeilBrown5ff5aff2010-06-01 19:37:32 +10001751 if (mddev->kobj.sd)
1752 bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
NeilBrownece5cff2009-12-14 12:49:56 +11001753 if (bm) {
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07001754 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
NeilBrownece5cff2009-12-14 12:49:56 +11001755 sysfs_put(bm);
1756 } else
1757 bitmap->sysfs_can_clear = NULL;
1758
NeilBrown32a76272005-06-21 17:17:14 -07001759 bitmap->file = file;
NeilBrownce25c312006-06-26 00:27:49 -07001760 if (file) {
1761 get_file(file);
NeilBrownae8fa282009-10-16 15:56:01 +11001762 /* As future accesses to this file will use bmap,
1763 * and bypass the page cache, we must sync the file
1764 * first.
1765 */
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001766 vfs_fsync(file, 1);
NeilBrownce25c312006-06-26 00:27:49 -07001767 }
NeilBrown42a04b52009-12-14 12:49:53 +11001768 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
Jonathan Brassow9c810752011-06-08 17:59:30 -05001769 if (!mddev->bitmap_info.external) {
1770 /*
1771 * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
1772 * instructing us to create a new on-disk bitmap instance.
1773 */
1774 if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
1775 err = bitmap_new_disk_sb(bitmap);
1776 else
1777 err = bitmap_read_sb(bitmap);
1778 } else {
NeilBrownece5cff2009-12-14 12:49:56 +11001779 err = 0;
1780 if (mddev->bitmap_info.chunksize == 0 ||
1781 mddev->bitmap_info.daemon_sleep == 0)
1782 /* chunksize and time_base need to be
1783 * set first. */
1784 err = -EINVAL;
1785 }
NeilBrown32a76272005-06-21 17:17:14 -07001786 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001787 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001788
NeilBrown624ce4f2009-12-14 12:49:56 +11001789 bitmap->daemon_lastrun = jiffies;
NeilBrown42a04b52009-12-14 12:49:53 +11001790 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
NeilBrown32a76272005-06-21 17:17:14 -07001791
1792 /* now that chunksize and chunkshift are set, we can use these macros */
NeilBrownac2f40b2010-06-01 19:37:31 +10001793 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
NeilBrown1f593902009-04-20 11:50:24 +10001794 CHUNK_BLOCK_SHIFT(bitmap);
NeilBrownac2f40b2010-06-01 19:37:31 +10001795 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
NeilBrown32a76272005-06-21 17:17:14 -07001796
1797 BUG_ON(!pages);
1798
1799 bitmap->chunks = chunks;
1800 bitmap->pages = pages;
1801 bitmap->missing_pages = pages;
NeilBrown32a76272005-06-21 17:17:14 -07001802
Olaf Hering44456d32005-07-27 11:45:17 -07001803#ifdef INJECT_FATAL_FAULT_1
NeilBrown32a76272005-06-21 17:17:14 -07001804 bitmap->bp = NULL;
1805#else
NeilBrown9ffae0c2006-01-06 00:20:32 -08001806 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001807#endif
NeilBrown3178b0d2005-09-09 16:23:50 -07001808 err = -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07001809 if (!bitmap->bp)
NeilBrown3178b0d2005-09-09 16:23:50 -07001810 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001811
NeilBrown69e51b42010-06-01 19:37:35 +10001812 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1813 pages, bmname(bitmap));
1814
1815 mddev->bitmap = bitmap;
1816
1817
1818 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
1819
1820 error:
1821 bitmap_free(bitmap);
1822 return err;
1823}
1824
1825int bitmap_load(mddev_t *mddev)
1826{
1827 int err = 0;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001828 sector_t start = 0;
NeilBrown69e51b42010-06-01 19:37:35 +10001829 sector_t sector = 0;
1830 struct bitmap *bitmap = mddev->bitmap;
1831
1832 if (!bitmap)
1833 goto out;
1834
1835 /* Clear out old bitmap info first: Either there is none, or we
1836 * are resuming after someone else has possibly changed things,
1837 * so we should forget old cached info.
1838 * All chunks should be clean, but some might need_sync.
1839 */
1840 while (sector < mddev->resync_max_sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001841 sector_t blocks;
NeilBrown69e51b42010-06-01 19:37:35 +10001842 bitmap_start_sync(bitmap, sector, &blocks, 0);
1843 sector += blocks;
1844 }
1845 bitmap_close_sync(bitmap);
1846
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001847 if (mddev->degraded == 0
1848 || bitmap->events_cleared == mddev->events)
1849 /* no need to keep dirty bits to optimise a
1850 * re-add of a missing device */
1851 start = mddev->recovery_cp;
NeilBrown69e51b42010-06-01 19:37:35 +10001852
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001853 err = bitmap_init_from_disk(bitmap, start);
1854
NeilBrown32a76272005-06-21 17:17:14 -07001855 if (err)
NeilBrown69e51b42010-06-01 19:37:35 +10001856 goto out;
NeilBrown3178b0d2005-09-09 16:23:50 -07001857
NeilBrown1b04be92009-12-14 12:49:53 +11001858 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
NeilBrown9cd30fd2009-12-14 12:49:54 +11001859 md_wakeup_thread(mddev->thread);
NeilBrownb15c2e52006-01-06 00:20:16 -08001860
NeilBrown4ad13662007-07-17 04:06:13 -07001861 bitmap_update_sb(bitmap);
1862
NeilBrown69e51b42010-06-01 19:37:35 +10001863 if (bitmap->flags & BITMAP_WRITE_ERROR)
1864 err = -EIO;
1865out:
NeilBrown3178b0d2005-09-09 16:23:50 -07001866 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001867}
NeilBrown69e51b42010-06-01 19:37:35 +10001868EXPORT_SYMBOL_GPL(bitmap_load);
NeilBrown32a76272005-06-21 17:17:14 -07001869
NeilBrown43a70502009-12-14 12:49:55 +11001870static ssize_t
1871location_show(mddev_t *mddev, char *page)
1872{
1873 ssize_t len;
NeilBrownac2f40b2010-06-01 19:37:31 +10001874 if (mddev->bitmap_info.file)
NeilBrown43a70502009-12-14 12:49:55 +11001875 len = sprintf(page, "file");
NeilBrownac2f40b2010-06-01 19:37:31 +10001876 else if (mddev->bitmap_info.offset)
NeilBrown43a70502009-12-14 12:49:55 +11001877 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001878 else
NeilBrown43a70502009-12-14 12:49:55 +11001879 len = sprintf(page, "none");
1880 len += sprintf(page+len, "\n");
1881 return len;
1882}
1883
1884static ssize_t
1885location_store(mddev_t *mddev, const char *buf, size_t len)
1886{
1887
1888 if (mddev->pers) {
1889 if (!mddev->pers->quiesce)
1890 return -EBUSY;
1891 if (mddev->recovery || mddev->sync_thread)
1892 return -EBUSY;
1893 }
1894
1895 if (mddev->bitmap || mddev->bitmap_info.file ||
1896 mddev->bitmap_info.offset) {
1897 /* bitmap already configured. Only option is to clear it */
1898 if (strncmp(buf, "none", 4) != 0)
1899 return -EBUSY;
1900 if (mddev->pers) {
1901 mddev->pers->quiesce(mddev, 1);
1902 bitmap_destroy(mddev);
1903 mddev->pers->quiesce(mddev, 0);
1904 }
1905 mddev->bitmap_info.offset = 0;
1906 if (mddev->bitmap_info.file) {
1907 struct file *f = mddev->bitmap_info.file;
1908 mddev->bitmap_info.file = NULL;
1909 restore_bitmap_write_access(f);
1910 fput(f);
1911 }
1912 } else {
1913 /* No bitmap, OK to set a location */
1914 long long offset;
1915 if (strncmp(buf, "none", 4) == 0)
1916 /* nothing to be done */;
1917 else if (strncmp(buf, "file:", 5) == 0) {
1918 /* Not supported yet */
1919 return -EINVAL;
1920 } else {
1921 int rv;
1922 if (buf[0] == '+')
1923 rv = strict_strtoll(buf+1, 10, &offset);
1924 else
1925 rv = strict_strtoll(buf, 10, &offset);
1926 if (rv)
1927 return rv;
1928 if (offset == 0)
1929 return -EINVAL;
NeilBrownece5cff2009-12-14 12:49:56 +11001930 if (mddev->bitmap_info.external == 0 &&
1931 mddev->major_version == 0 &&
NeilBrown43a70502009-12-14 12:49:55 +11001932 offset != mddev->bitmap_info.default_offset)
1933 return -EINVAL;
1934 mddev->bitmap_info.offset = offset;
1935 if (mddev->pers) {
1936 mddev->pers->quiesce(mddev, 1);
1937 rv = bitmap_create(mddev);
1938 if (rv) {
1939 bitmap_destroy(mddev);
1940 mddev->bitmap_info.offset = 0;
1941 }
1942 mddev->pers->quiesce(mddev, 0);
1943 if (rv)
1944 return rv;
1945 }
1946 }
1947 }
1948 if (!mddev->external) {
1949 /* Ensure new bitmap info is stored in
1950 * metadata promptly.
1951 */
1952 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1953 md_wakeup_thread(mddev->thread);
1954 }
1955 return len;
1956}
1957
1958static struct md_sysfs_entry bitmap_location =
1959__ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
1960
1961static ssize_t
1962timeout_show(mddev_t *mddev, char *page)
1963{
1964 ssize_t len;
1965 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
1966 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
NeilBrownac2f40b2010-06-01 19:37:31 +10001967
NeilBrown43a70502009-12-14 12:49:55 +11001968 len = sprintf(page, "%lu", secs);
1969 if (jifs)
1970 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
1971 len += sprintf(page+len, "\n");
1972 return len;
1973}
1974
1975static ssize_t
1976timeout_store(mddev_t *mddev, const char *buf, size_t len)
1977{
1978 /* timeout can be set at any time */
1979 unsigned long timeout;
1980 int rv = strict_strtoul_scaled(buf, &timeout, 4);
1981 if (rv)
1982 return rv;
1983
1984 /* just to make sure we don't overflow... */
1985 if (timeout >= LONG_MAX / HZ)
1986 return -EINVAL;
1987
1988 timeout = timeout * HZ / 10000;
1989
1990 if (timeout >= MAX_SCHEDULE_TIMEOUT)
1991 timeout = MAX_SCHEDULE_TIMEOUT-1;
1992 if (timeout < 1)
1993 timeout = 1;
1994 mddev->bitmap_info.daemon_sleep = timeout;
1995 if (mddev->thread) {
1996 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1997 * the bitmap is all clean and we don't need to
1998 * adjust the timeout right now
1999 */
2000 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
2001 mddev->thread->timeout = timeout;
2002 md_wakeup_thread(mddev->thread);
2003 }
2004 }
2005 return len;
2006}
2007
2008static struct md_sysfs_entry bitmap_timeout =
2009__ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
2010
2011static ssize_t
2012backlog_show(mddev_t *mddev, char *page)
2013{
2014 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
2015}
2016
2017static ssize_t
2018backlog_store(mddev_t *mddev, const char *buf, size_t len)
2019{
2020 unsigned long backlog;
2021 int rv = strict_strtoul(buf, 10, &backlog);
2022 if (rv)
2023 return rv;
2024 if (backlog > COUNTER_MAX)
2025 return -EINVAL;
2026 mddev->bitmap_info.max_write_behind = backlog;
2027 return len;
2028}
2029
2030static struct md_sysfs_entry bitmap_backlog =
2031__ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
2032
2033static ssize_t
2034chunksize_show(mddev_t *mddev, char *page)
2035{
2036 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
2037}
2038
2039static ssize_t
2040chunksize_store(mddev_t *mddev, const char *buf, size_t len)
2041{
2042 /* Can only be changed when no bitmap is active */
2043 int rv;
2044 unsigned long csize;
2045 if (mddev->bitmap)
2046 return -EBUSY;
2047 rv = strict_strtoul(buf, 10, &csize);
2048 if (rv)
2049 return rv;
2050 if (csize < 512 ||
2051 !is_power_of_2(csize))
2052 return -EINVAL;
2053 mddev->bitmap_info.chunksize = csize;
2054 return len;
2055}
2056
2057static struct md_sysfs_entry bitmap_chunksize =
2058__ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
2059
NeilBrownece5cff2009-12-14 12:49:56 +11002060static ssize_t metadata_show(mddev_t *mddev, char *page)
2061{
2062 return sprintf(page, "%s\n", (mddev->bitmap_info.external
2063 ? "external" : "internal"));
2064}
2065
2066static ssize_t metadata_store(mddev_t *mddev, const char *buf, size_t len)
2067{
2068 if (mddev->bitmap ||
2069 mddev->bitmap_info.file ||
2070 mddev->bitmap_info.offset)
2071 return -EBUSY;
2072 if (strncmp(buf, "external", 8) == 0)
2073 mddev->bitmap_info.external = 1;
2074 else if (strncmp(buf, "internal", 8) == 0)
2075 mddev->bitmap_info.external = 0;
2076 else
2077 return -EINVAL;
2078 return len;
2079}
2080
2081static struct md_sysfs_entry bitmap_metadata =
2082__ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2083
2084static ssize_t can_clear_show(mddev_t *mddev, char *page)
2085{
2086 int len;
2087 if (mddev->bitmap)
2088 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
2089 "false" : "true"));
2090 else
2091 len = sprintf(page, "\n");
2092 return len;
2093}
2094
2095static ssize_t can_clear_store(mddev_t *mddev, const char *buf, size_t len)
2096{
2097 if (mddev->bitmap == NULL)
2098 return -ENOENT;
2099 if (strncmp(buf, "false", 5) == 0)
2100 mddev->bitmap->need_sync = 1;
2101 else if (strncmp(buf, "true", 4) == 0) {
2102 if (mddev->degraded)
2103 return -EBUSY;
2104 mddev->bitmap->need_sync = 0;
2105 } else
2106 return -EINVAL;
2107 return len;
2108}
2109
2110static struct md_sysfs_entry bitmap_can_clear =
2111__ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
2112
Paul Clements696fcd52010-03-08 16:02:37 +11002113static ssize_t
2114behind_writes_used_show(mddev_t *mddev, char *page)
2115{
2116 if (mddev->bitmap == NULL)
2117 return sprintf(page, "0\n");
2118 return sprintf(page, "%lu\n",
2119 mddev->bitmap->behind_writes_used);
2120}
2121
2122static ssize_t
2123behind_writes_used_reset(mddev_t *mddev, const char *buf, size_t len)
2124{
2125 if (mddev->bitmap)
2126 mddev->bitmap->behind_writes_used = 0;
2127 return len;
2128}
2129
2130static struct md_sysfs_entry max_backlog_used =
2131__ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2132 behind_writes_used_show, behind_writes_used_reset);
2133
NeilBrown43a70502009-12-14 12:49:55 +11002134static struct attribute *md_bitmap_attrs[] = {
2135 &bitmap_location.attr,
2136 &bitmap_timeout.attr,
2137 &bitmap_backlog.attr,
2138 &bitmap_chunksize.attr,
NeilBrownece5cff2009-12-14 12:49:56 +11002139 &bitmap_metadata.attr,
2140 &bitmap_can_clear.attr,
Paul Clements696fcd52010-03-08 16:02:37 +11002141 &max_backlog_used.attr,
NeilBrown43a70502009-12-14 12:49:55 +11002142 NULL
2143};
2144struct attribute_group md_bitmap_group = {
2145 .name = "bitmap",
2146 .attrs = md_bitmap_attrs,
2147};
2148