blob: e2aacca46911333c7d7d094e60529950c8a01d05 [file] [log] [blame]
NeilBrown32a76272005-06-21 17:17:14 -07001/*
2 * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
3 *
4 * bitmap_create - sets up the bitmap structure
5 * bitmap_destroy - destroys the bitmap structure
6 *
7 * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.:
8 * - added disk storage for bitmap
9 * - changes to allow various bitmap chunk sizes
NeilBrown32a76272005-06-21 17:17:14 -070010 */
11
12/*
13 * Still to do:
14 *
15 * flush after percent set rather than just time based. (maybe both).
NeilBrown32a76272005-06-21 17:17:14 -070016 */
17
NeilBrownbff61972009-03-31 14:33:13 +110018#include <linux/blkdev.h>
NeilBrown32a76272005-06-21 17:17:14 -070019#include <linux/module.h>
NeilBrown32a76272005-06-21 17:17:14 -070020#include <linux/errno.h>
21#include <linux/slab.h>
22#include <linux/init.h>
NeilBrown32a76272005-06-21 17:17:14 -070023#include <linux/timer.h>
24#include <linux/sched.h>
25#include <linux/list.h>
26#include <linux/file.h>
27#include <linux/mount.h>
28#include <linux/buffer_head.h>
NeilBrown57148962012-03-19 12:46:40 +110029#include <linux/seq_file.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110030#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110031#include "bitmap.h"
NeilBrown32a76272005-06-21 17:17:14 -070032
NeilBrownac2f40b2010-06-01 19:37:31 +100033static inline char *bmname(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -070034{
35 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
36}
37
NeilBrown32a76272005-06-21 17:17:14 -070038/*
NeilBrown32a76272005-06-21 17:17:14 -070039 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
40 *
41 * 1) check to see if this page is allocated, if it's not then try to alloc
42 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
43 * page pointer directly as a counter
44 *
45 * if we find our page, we increment the page's refcount so that it stays
46 * allocated while we're using it
47 */
NeilBrown40cffcc2012-05-22 13:55:24 +100048static int bitmap_checkpage(struct bitmap_counts *bitmap,
NeilBrownac2f40b2010-06-01 19:37:31 +100049 unsigned long page, int create)
NeilBrownee305ac2009-09-23 18:06:44 +100050__releases(bitmap->lock)
51__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -070052{
53 unsigned char *mappage;
54
55 if (page >= bitmap->pages) {
NeilBrown1187cf02009-03-31 14:27:02 +110056 /* This can happen if bitmap_start_sync goes beyond
57 * End-of-device while looking for a whole page.
58 * It is harmless.
59 */
NeilBrown32a76272005-06-21 17:17:14 -070060 return -EINVAL;
61 }
62
NeilBrown32a76272005-06-21 17:17:14 -070063 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
64 return 0;
65
66 if (bitmap->bp[page].map) /* page is already allocated, just return */
67 return 0;
68
69 if (!create)
70 return -ENOENT;
71
NeilBrown32a76272005-06-21 17:17:14 -070072 /* this page has not been allocated yet */
73
NeilBrownac2f40b2010-06-01 19:37:31 +100074 spin_unlock_irq(&bitmap->lock);
NeilBrownd9590142015-02-02 17:08:03 +110075 /* It is possible that this is being called inside a
76 * prepare_to_wait/finish_wait loop from raid5c:make_request().
77 * In general it is not permitted to sleep in that context as it
78 * can cause the loop to spin freely.
79 * That doesn't apply here as we can only reach this point
80 * once with any loop.
81 * When this function completes, either bp[page].map or
82 * bp[page].hijacked. In either case, this function will
83 * abort before getting to this point again. So there is
84 * no risk of a free-spin, and so it is safe to assert
85 * that sleeping here is allowed.
86 */
87 sched_annotate_sleep();
NeilBrown792a1d42012-03-19 12:46:41 +110088 mappage = kzalloc(PAGE_SIZE, GFP_NOIO);
NeilBrownac2f40b2010-06-01 19:37:31 +100089 spin_lock_irq(&bitmap->lock);
90
91 if (mappage == NULL) {
NeilBrown40cffcc2012-05-22 13:55:24 +100092 pr_debug("md/bitmap: map page allocation failed, hijacking\n");
NeilBrown32a76272005-06-21 17:17:14 -070093 /* failed - set the hijacked flag so that we can use the
94 * pointer as a counter */
NeilBrown32a76272005-06-21 17:17:14 -070095 if (!bitmap->bp[page].map)
96 bitmap->bp[page].hijacked = 1;
NeilBrownac2f40b2010-06-01 19:37:31 +100097 } else if (bitmap->bp[page].map ||
98 bitmap->bp[page].hijacked) {
NeilBrown32a76272005-06-21 17:17:14 -070099 /* somebody beat us to getting the page */
NeilBrown792a1d42012-03-19 12:46:41 +1100100 kfree(mappage);
NeilBrown32a76272005-06-21 17:17:14 -0700101 return 0;
NeilBrownac2f40b2010-06-01 19:37:31 +1000102 } else {
103
104 /* no page was in place and we have one, so install it */
105
106 bitmap->bp[page].map = mappage;
107 bitmap->missing_pages--;
NeilBrown32a76272005-06-21 17:17:14 -0700108 }
NeilBrown32a76272005-06-21 17:17:14 -0700109 return 0;
110}
111
NeilBrown32a76272005-06-21 17:17:14 -0700112/* if page is completely empty, put it back on the free list, or dealloc it */
113/* if page was hijacked, unmark the flag so it might get alloced next time */
114/* Note: lock should be held when calling this */
NeilBrown40cffcc2012-05-22 13:55:24 +1000115static void bitmap_checkfree(struct bitmap_counts *bitmap, unsigned long page)
NeilBrown32a76272005-06-21 17:17:14 -0700116{
117 char *ptr;
118
119 if (bitmap->bp[page].count) /* page is still busy */
120 return;
121
122 /* page is no longer in use, it can be released */
123
124 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
125 bitmap->bp[page].hijacked = 0;
126 bitmap->bp[page].map = NULL;
NeilBrownac2f40b2010-06-01 19:37:31 +1000127 } else {
128 /* normal case, free the page */
129 ptr = bitmap->bp[page].map;
130 bitmap->bp[page].map = NULL;
131 bitmap->missing_pages++;
NeilBrown792a1d42012-03-19 12:46:41 +1100132 kfree(ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700133 }
NeilBrown32a76272005-06-21 17:17:14 -0700134}
135
NeilBrown32a76272005-06-21 17:17:14 -0700136/*
137 * bitmap file handling - read and write the bitmap file and its superblock
138 */
139
NeilBrown32a76272005-06-21 17:17:14 -0700140/*
141 * basic page I/O operations
142 */
143
NeilBrowna654b9d82005-06-21 17:17:27 -0700144/* IO operations when bitmap is stored near all superblocks */
NeilBrown27581e52012-05-22 13:55:08 +1000145static int read_sb_page(struct mddev *mddev, loff_t offset,
146 struct page *page,
147 unsigned long index, int size)
NeilBrowna654b9d82005-06-21 17:17:27 -0700148{
149 /* choose a good rdev and read the page from there */
150
NeilBrown3cb03002011-10-11 16:45:26 +1100151 struct md_rdev *rdev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700152 sector_t target;
NeilBrowna654b9d82005-06-21 17:17:27 -0700153
NeilBrowndafb20f2012-03-19 12:46:39 +1100154 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800155 if (! test_bit(In_sync, &rdev->flags)
156 || test_bit(Faulty, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700157 continue;
158
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100159 target = offset + index * (PAGE_SIZE/512);
NeilBrowna654b9d82005-06-21 17:17:27 -0700160
NeilBrown2b193362010-10-27 15:16:40 +1100161 if (sync_page_io(rdev, target,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400162 roundup(size, bdev_logical_block_size(rdev->bdev)),
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100163 page, READ, true)) {
NeilBrownab904d62005-09-09 16:23:52 -0700164 page->index = index;
NeilBrown27581e52012-05-22 13:55:08 +1000165 return 0;
NeilBrownab904d62005-09-09 16:23:52 -0700166 }
167 }
NeilBrown27581e52012-05-22 13:55:08 +1000168 return -EIO;
NeilBrowna654b9d82005-06-21 17:17:27 -0700169}
170
NeilBrownfd01b882011-10-11 16:47:53 +1100171static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev)
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000172{
173 /* Iterate the disks of an mddev, using rcu to protect access to the
174 * linked list, and raising the refcount of devices we return to ensure
175 * they don't disappear while in use.
176 * As devices are only added or removed when raid_disk is < 0 and
177 * nr_pending is 0 and In_sync is clear, the entries we return will
178 * still be in the same position on the list when we re-enter
Michael Wangfd177482012-10-11 13:43:21 +1100179 * list_for_each_entry_continue_rcu.
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000180 */
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000181 rcu_read_lock();
182 if (rdev == NULL)
183 /* start at the beginning */
Michael Wangfd177482012-10-11 13:43:21 +1100184 rdev = list_entry_rcu(&mddev->disks, struct md_rdev, same_set);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000185 else {
186 /* release the previous rdev and start from there. */
187 rdev_dec_pending(rdev, mddev);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000188 }
Michael Wangfd177482012-10-11 13:43:21 +1100189 list_for_each_entry_continue_rcu(rdev, &mddev->disks, same_set) {
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000190 if (rdev->raid_disk >= 0 &&
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000191 !test_bit(Faulty, &rdev->flags)) {
192 /* this is a usable devices */
193 atomic_inc(&rdev->nr_pending);
194 rcu_read_unlock();
195 return rdev;
196 }
197 }
198 rcu_read_unlock();
199 return NULL;
200}
201
NeilBrownab6085c2007-05-23 13:58:10 -0700202static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrowna654b9d82005-06-21 17:17:27 -0700203{
NeilBrown3cb03002011-10-11 16:45:26 +1100204 struct md_rdev *rdev = NULL;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100205 struct block_device *bdev;
NeilBrownfd01b882011-10-11 16:47:53 +1100206 struct mddev *mddev = bitmap->mddev;
NeilBrown1ec885c2012-05-22 13:55:10 +1000207 struct bitmap_storage *store = &bitmap->storage;
NeilBrowna654b9d82005-06-21 17:17:27 -0700208
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000209 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000210 int size = PAGE_SIZE;
211 loff_t offset = mddev->bitmap_info.offset;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100212
213 bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
214
NeilBrown9b1215c2012-05-22 13:55:11 +1000215 if (page->index == store->file_pages-1) {
216 int last_page_size = store->bytes & (PAGE_SIZE-1);
217 if (last_page_size == 0)
218 last_page_size = PAGE_SIZE;
219 size = roundup(last_page_size,
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100220 bdev_logical_block_size(bdev));
NeilBrown9b1215c2012-05-22 13:55:11 +1000221 }
NeilBrownac2f40b2010-06-01 19:37:31 +1000222 /* Just make sure we aren't corrupting data or
223 * metadata
224 */
225 if (mddev->external) {
226 /* Bitmap could be anywhere. */
227 if (rdev->sb_start + offset + (page->index
228 * (PAGE_SIZE/512))
229 > rdev->data_offset
230 &&
231 rdev->sb_start + offset
232 < (rdev->data_offset + mddev->dev_sectors
233 + (PAGE_SIZE/512)))
234 goto bad_alignment;
235 } else if (offset < 0) {
236 /* DATA BITMAP METADATA */
237 if (offset
238 + (long)(page->index * (PAGE_SIZE/512))
239 + size/512 > 0)
240 /* bitmap runs in to metadata */
241 goto bad_alignment;
242 if (rdev->data_offset + mddev->dev_sectors
243 > rdev->sb_start + offset)
244 /* data runs in to bitmap */
245 goto bad_alignment;
246 } else if (rdev->sb_start < rdev->data_offset) {
247 /* METADATA BITMAP DATA */
248 if (rdev->sb_start
249 + offset
250 + page->index*(PAGE_SIZE/512) + size/512
251 > rdev->data_offset)
252 /* bitmap runs in to data */
253 goto bad_alignment;
254 } else {
255 /* DATA METADATA BITMAP - no problems */
256 }
257 md_super_write(mddev, rdev,
258 rdev->sb_start + offset
259 + page->index * (PAGE_SIZE/512),
260 size,
261 page);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000262 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700263
264 if (wait)
NeilBrowna9701a32005-11-08 21:39:34 -0800265 md_super_wait(mddev);
NeilBrowna654b9d82005-06-21 17:17:27 -0700266 return 0;
NeilBrown4b809912008-07-21 17:05:25 +1000267
268 bad_alignment:
NeilBrown4b809912008-07-21 17:05:25 +1000269 return -EINVAL;
NeilBrowna654b9d82005-06-21 17:17:27 -0700270}
271
NeilBrown4ad13662007-07-17 04:06:13 -0700272static void bitmap_file_kick(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700273/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700274 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700275 */
NeilBrown4ad13662007-07-17 04:06:13 -0700276static void write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700277{
NeilBrownd785a062006-06-26 00:27:48 -0700278 struct buffer_head *bh;
NeilBrown32a76272005-06-21 17:17:14 -0700279
NeilBrown1ec885c2012-05-22 13:55:10 +1000280 if (bitmap->storage.file == NULL) {
NeilBrownf0d76d72007-07-17 04:06:12 -0700281 switch (write_sb_page(bitmap, page, wait)) {
282 case -EINVAL:
NeilBrownb405fe92012-05-22 13:55:15 +1000283 set_bit(BITMAP_WRITE_ERROR, &bitmap->flags);
NeilBrownf0d76d72007-07-17 04:06:12 -0700284 }
NeilBrown4ad13662007-07-17 04:06:13 -0700285 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700286
NeilBrown4ad13662007-07-17 04:06:13 -0700287 bh = page_buffers(page);
NeilBrownc7084432006-01-06 00:20:45 -0800288
NeilBrown4ad13662007-07-17 04:06:13 -0700289 while (bh && bh->b_blocknr) {
290 atomic_inc(&bitmap->pending_writes);
291 set_buffer_locked(bh);
292 set_buffer_mapped(bh);
Jens Axboe721a9602011-03-09 11:56:30 +0100293 submit_bh(WRITE | REQ_SYNC, bh);
NeilBrown4ad13662007-07-17 04:06:13 -0700294 bh = bh->b_this_page;
295 }
NeilBrown32a76272005-06-21 17:17:14 -0700296
NeilBrownac2f40b2010-06-01 19:37:31 +1000297 if (wait)
NeilBrown4ad13662007-07-17 04:06:13 -0700298 wait_event(bitmap->write_wait,
299 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700300 }
NeilBrownb405fe92012-05-22 13:55:15 +1000301 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
NeilBrown4ad13662007-07-17 04:06:13 -0700302 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700303}
304
NeilBrownd785a062006-06-26 00:27:48 -0700305static void end_bitmap_write(struct buffer_head *bh, int uptodate)
NeilBrown32a76272005-06-21 17:17:14 -0700306{
NeilBrownd785a062006-06-26 00:27:48 -0700307 struct bitmap *bitmap = bh->b_private;
NeilBrownd785a062006-06-26 00:27:48 -0700308
NeilBrownb405fe92012-05-22 13:55:15 +1000309 if (!uptodate)
310 set_bit(BITMAP_WRITE_ERROR, &bitmap->flags);
NeilBrownd785a062006-06-26 00:27:48 -0700311 if (atomic_dec_and_test(&bitmap->pending_writes))
312 wake_up(&bitmap->write_wait);
313}
314
315/* copied from buffer.c */
316static void
317__clear_page_buffers(struct page *page)
318{
319 ClearPagePrivate(page);
320 set_page_private(page, 0);
321 page_cache_release(page);
322}
323static void free_buffers(struct page *page)
324{
NeilBrown27581e52012-05-22 13:55:08 +1000325 struct buffer_head *bh;
NeilBrownd785a062006-06-26 00:27:48 -0700326
NeilBrown27581e52012-05-22 13:55:08 +1000327 if (!PagePrivate(page))
328 return;
329
330 bh = page_buffers(page);
NeilBrownd785a062006-06-26 00:27:48 -0700331 while (bh) {
332 struct buffer_head *next = bh->b_this_page;
333 free_buffer_head(bh);
334 bh = next;
335 }
336 __clear_page_buffers(page);
337 put_page(page);
338}
339
340/* read a page from a file.
341 * We both read the page, and attach buffers to the page to record the
342 * address of each block (using bmap). These addresses will be used
343 * to write the block later, completely bypassing the filesystem.
344 * This usage is similar to how swap files are handled, and allows us
345 * to write to a file with no concerns of memory allocation failing.
346 */
NeilBrown27581e52012-05-22 13:55:08 +1000347static int read_page(struct file *file, unsigned long index,
348 struct bitmap *bitmap,
349 unsigned long count,
350 struct page *page)
NeilBrownd785a062006-06-26 00:27:48 -0700351{
NeilBrown27581e52012-05-22 13:55:08 +1000352 int ret = 0;
Al Viro496ad9a2013-01-23 17:07:38 -0500353 struct inode *inode = file_inode(file);
NeilBrownd785a062006-06-26 00:27:48 -0700354 struct buffer_head *bh;
355 sector_t block;
NeilBrown32a76272005-06-21 17:17:14 -0700356
NeilBrown36a4e1f2011-10-07 14:23:17 +1100357 pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
358 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700359
NeilBrownd785a062006-06-26 00:27:48 -0700360 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
361 if (!bh) {
NeilBrown27581e52012-05-22 13:55:08 +1000362 ret = -ENOMEM;
NeilBrownd785a062006-06-26 00:27:48 -0700363 goto out;
364 }
365 attach_page_buffers(page, bh);
366 block = index << (PAGE_SHIFT - inode->i_blkbits);
367 while (bh) {
368 if (count == 0)
369 bh->b_blocknr = 0;
370 else {
371 bh->b_blocknr = bmap(inode, block);
372 if (bh->b_blocknr == 0) {
373 /* Cannot use this file! */
NeilBrown27581e52012-05-22 13:55:08 +1000374 ret = -EINVAL;
NeilBrownd785a062006-06-26 00:27:48 -0700375 goto out;
376 }
377 bh->b_bdev = inode->i_sb->s_bdev;
378 if (count < (1<<inode->i_blkbits))
379 count = 0;
380 else
381 count -= (1<<inode->i_blkbits);
NeilBrown32a76272005-06-21 17:17:14 -0700382
NeilBrownd785a062006-06-26 00:27:48 -0700383 bh->b_end_io = end_bitmap_write;
384 bh->b_private = bitmap;
NeilBrownce25c312006-06-26 00:27:49 -0700385 atomic_inc(&bitmap->pending_writes);
386 set_buffer_locked(bh);
387 set_buffer_mapped(bh);
388 submit_bh(READ, bh);
NeilBrownd785a062006-06-26 00:27:48 -0700389 }
390 block++;
391 bh = bh->b_this_page;
392 }
NeilBrownd785a062006-06-26 00:27:48 -0700393 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700394
395 wait_event(bitmap->write_wait,
396 atomic_read(&bitmap->pending_writes)==0);
NeilBrownb405fe92012-05-22 13:55:15 +1000397 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
NeilBrown27581e52012-05-22 13:55:08 +1000398 ret = -EIO;
NeilBrown32a76272005-06-21 17:17:14 -0700399out:
NeilBrown27581e52012-05-22 13:55:08 +1000400 if (ret)
401 printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %d\n",
NeilBrown2d1f3b52006-01-06 00:20:31 -0800402 (int)PAGE_SIZE,
403 (unsigned long long)index << PAGE_SHIFT,
NeilBrown27581e52012-05-22 13:55:08 +1000404 ret);
405 return ret;
NeilBrown32a76272005-06-21 17:17:14 -0700406}
407
408/*
409 * bitmap file superblock operations
410 */
411
412/* update the event counter and sync the superblock to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700413void bitmap_update_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700414{
415 bitmap_super_t *sb;
NeilBrown32a76272005-06-21 17:17:14 -0700416
417 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
NeilBrown4ad13662007-07-17 04:06:13 -0700418 return;
NeilBrownece5cff2009-12-14 12:49:56 +1100419 if (bitmap->mddev->bitmap_info.external)
420 return;
NeilBrown1ec885c2012-05-22 13:55:10 +1000421 if (!bitmap->storage.sb_page) /* no superblock */
NeilBrown4ad13662007-07-17 04:06:13 -0700422 return;
NeilBrown1ec885c2012-05-22 13:55:10 +1000423 sb = kmap_atomic(bitmap->storage.sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700424 sb->events = cpu_to_le64(bitmap->mddev->events);
NeilBrown8258c532011-05-11 14:26:30 +1000425 if (bitmap->mddev->events < bitmap->events_cleared)
Neil Browna0da84f2008-06-28 08:31:22 +1000426 /* rocking back to read-only */
427 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown8258c532011-05-11 14:26:30 +1000428 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
429 sb->state = cpu_to_le32(bitmap->flags);
NeilBrown43a70502009-12-14 12:49:55 +1100430 /* Just in case these have been changed via sysfs: */
431 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
432 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
NeilBrownb81a0402012-05-22 13:55:26 +1000433 /* This might have been changed by a reshape */
434 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
435 sb->chunksize = cpu_to_le32(bitmap->mddev->bitmap_info.chunksize);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500436 sb->nodes = cpu_to_le32(bitmap->mddev->bitmap_info.nodes);
NeilBrown1dff2b82012-05-22 13:55:34 +1000437 sb->sectors_reserved = cpu_to_le32(bitmap->mddev->
438 bitmap_info.space);
Cong Wangb2f46e62011-11-28 13:25:44 +0800439 kunmap_atomic(sb);
NeilBrown1ec885c2012-05-22 13:55:10 +1000440 write_page(bitmap, bitmap->storage.sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700441}
442
443/* print out the bitmap file superblock */
444void bitmap_print_sb(struct bitmap *bitmap)
445{
446 bitmap_super_t *sb;
447
NeilBrown1ec885c2012-05-22 13:55:10 +1000448 if (!bitmap || !bitmap->storage.sb_page)
NeilBrown32a76272005-06-21 17:17:14 -0700449 return;
NeilBrown1ec885c2012-05-22 13:55:10 +1000450 sb = kmap_atomic(bitmap->storage.sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700451 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
NeilBrowna2cff262005-06-21 17:17:20 -0700452 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
453 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
454 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
NeilBrown32a76272005-06-21 17:17:14 -0700455 *(__u32 *)(sb->uuid+0),
456 *(__u32 *)(sb->uuid+4),
457 *(__u32 *)(sb->uuid+8),
458 *(__u32 *)(sb->uuid+12));
NeilBrowna2cff262005-06-21 17:17:20 -0700459 printk(KERN_DEBUG " events: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700460 (unsigned long long) le64_to_cpu(sb->events));
NeilBrowna2cff262005-06-21 17:17:20 -0700461 printk(KERN_DEBUG "events cleared: %llu\n",
NeilBrown32a76272005-06-21 17:17:14 -0700462 (unsigned long long) le64_to_cpu(sb->events_cleared));
NeilBrowna2cff262005-06-21 17:17:20 -0700463 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
464 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
465 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
466 printk(KERN_DEBUG " sync size: %llu KB\n",
467 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
NeilBrown4b6d2872005-09-09 16:23:47 -0700468 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
Cong Wangb2f46e62011-11-28 13:25:44 +0800469 kunmap_atomic(sb);
NeilBrown32a76272005-06-21 17:17:14 -0700470}
471
Jonathan Brassow9c810752011-06-08 17:59:30 -0500472/*
473 * bitmap_new_disk_sb
474 * @bitmap
475 *
476 * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
477 * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
478 * This function verifies 'bitmap_info' and populates the on-disk bitmap
479 * structure, which is to be written to disk.
480 *
481 * Returns: 0 on success, -Exxx on error
482 */
483static int bitmap_new_disk_sb(struct bitmap *bitmap)
484{
485 bitmap_super_t *sb;
486 unsigned long chunksize, daemon_sleep, write_behind;
Jonathan Brassow9c810752011-06-08 17:59:30 -0500487
NeilBrown1ec885c2012-05-22 13:55:10 +1000488 bitmap->storage.sb_page = alloc_page(GFP_KERNEL);
Jianpeng Ma582e2e02012-10-11 13:45:36 +1100489 if (bitmap->storage.sb_page == NULL)
490 return -ENOMEM;
NeilBrown1ec885c2012-05-22 13:55:10 +1000491 bitmap->storage.sb_page->index = 0;
Jonathan Brassow9c810752011-06-08 17:59:30 -0500492
NeilBrown1ec885c2012-05-22 13:55:10 +1000493 sb = kmap_atomic(bitmap->storage.sb_page);
Jonathan Brassow9c810752011-06-08 17:59:30 -0500494
495 sb->magic = cpu_to_le32(BITMAP_MAGIC);
496 sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
497
498 chunksize = bitmap->mddev->bitmap_info.chunksize;
499 BUG_ON(!chunksize);
500 if (!is_power_of_2(chunksize)) {
Cong Wangb2f46e62011-11-28 13:25:44 +0800501 kunmap_atomic(sb);
Jonathan Brassow9c810752011-06-08 17:59:30 -0500502 printk(KERN_ERR "bitmap chunksize not a power of 2\n");
503 return -EINVAL;
504 }
505 sb->chunksize = cpu_to_le32(chunksize);
506
507 daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
508 if (!daemon_sleep ||
509 (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
510 printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n");
511 daemon_sleep = 5 * HZ;
512 }
513 sb->daemon_sleep = cpu_to_le32(daemon_sleep);
514 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
515
516 /*
517 * FIXME: write_behind for RAID1. If not specified, what
518 * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
519 */
520 write_behind = bitmap->mddev->bitmap_info.max_write_behind;
521 if (write_behind > COUNTER_MAX)
522 write_behind = COUNTER_MAX / 2;
523 sb->write_behind = cpu_to_le32(write_behind);
524 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
525
526 /* keep the array size field of the bitmap superblock up to date */
527 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
528
529 memcpy(sb->uuid, bitmap->mddev->uuid, 16);
530
NeilBrownb405fe92012-05-22 13:55:15 +1000531 set_bit(BITMAP_STALE, &bitmap->flags);
NeilBrown84e92342012-05-22 13:55:14 +1000532 sb->state = cpu_to_le32(bitmap->flags);
Jonathan Brassow9c810752011-06-08 17:59:30 -0500533 bitmap->events_cleared = bitmap->mddev->events;
534 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
535
Cong Wangb2f46e62011-11-28 13:25:44 +0800536 kunmap_atomic(sb);
Jonathan Brassow9c810752011-06-08 17:59:30 -0500537
538 return 0;
539}
540
NeilBrown32a76272005-06-21 17:17:14 -0700541/* read the superblock from the bitmap file and initialize some bitmap fields */
542static int bitmap_read_sb(struct bitmap *bitmap)
543{
544 char *reason = NULL;
545 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700546 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700547 unsigned long long events;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500548 int nodes = 0;
NeilBrown1dff2b82012-05-22 13:55:34 +1000549 unsigned long sectors_reserved = 0;
NeilBrown32a76272005-06-21 17:17:14 -0700550 int err = -EINVAL;
NeilBrown27581e52012-05-22 13:55:08 +1000551 struct page *sb_page;
NeilBrown32a76272005-06-21 17:17:14 -0700552
NeilBrown1ec885c2012-05-22 13:55:10 +1000553 if (!bitmap->storage.file && !bitmap->mddev->bitmap_info.offset) {
NeilBrownef99bf42012-05-22 13:55:08 +1000554 chunksize = 128 * 1024 * 1024;
555 daemon_sleep = 5 * HZ;
556 write_behind = 0;
NeilBrownb405fe92012-05-22 13:55:15 +1000557 set_bit(BITMAP_STALE, &bitmap->flags);
NeilBrownef99bf42012-05-22 13:55:08 +1000558 err = 0;
559 goto out_no_sb;
560 }
NeilBrown32a76272005-06-21 17:17:14 -0700561 /* page 0 is the superblock, read it... */
NeilBrown27581e52012-05-22 13:55:08 +1000562 sb_page = alloc_page(GFP_KERNEL);
563 if (!sb_page)
564 return -ENOMEM;
NeilBrown1ec885c2012-05-22 13:55:10 +1000565 bitmap->storage.sb_page = sb_page;
NeilBrown27581e52012-05-22 13:55:08 +1000566
NeilBrown1ec885c2012-05-22 13:55:10 +1000567 if (bitmap->storage.file) {
568 loff_t isize = i_size_read(bitmap->storage.file->f_mapping->host);
NeilBrownf49d5e62007-01-26 00:57:03 -0800569 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
570
NeilBrown1ec885c2012-05-22 13:55:10 +1000571 err = read_page(bitmap->storage.file, 0,
NeilBrown27581e52012-05-22 13:55:08 +1000572 bitmap, bytes, sb_page);
NeilBrownf49d5e62007-01-26 00:57:03 -0800573 } else {
NeilBrown27581e52012-05-22 13:55:08 +1000574 err = read_sb_page(bitmap->mddev,
575 bitmap->mddev->bitmap_info.offset,
576 sb_page,
577 0, sizeof(bitmap_super_t));
NeilBrowna654b9d82005-06-21 17:17:27 -0700578 }
NeilBrown27581e52012-05-22 13:55:08 +1000579 if (err)
NeilBrown32a76272005-06-21 17:17:14 -0700580 return err;
NeilBrown32a76272005-06-21 17:17:14 -0700581
NeilBrown27581e52012-05-22 13:55:08 +1000582 sb = kmap_atomic(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700583
NeilBrown32a76272005-06-21 17:17:14 -0700584 chunksize = le32_to_cpu(sb->chunksize);
NeilBrown1b04be92009-12-14 12:49:53 +1100585 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
NeilBrown4b6d2872005-09-09 16:23:47 -0700586 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown1dff2b82012-05-22 13:55:34 +1000587 sectors_reserved = le32_to_cpu(sb->sectors_reserved);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500588 nodes = le32_to_cpu(sb->nodes);
NeilBrown32a76272005-06-21 17:17:14 -0700589
590 /* verify that the bitmap-specific fields are valid */
591 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
592 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800593 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
594 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
NeilBrown32a76272005-06-21 17:17:14 -0700595 reason = "unrecognized superblock version";
NeilBrown1187cf02009-03-31 14:27:02 +1100596 else if (chunksize < 512)
NeilBrown7dd5d342006-01-06 00:20:39 -0800597 reason = "bitmap chunksize too small";
Jonathan Brassowd7445402011-06-08 18:01:10 -0500598 else if (!is_power_of_2(chunksize))
NeilBrown32a76272005-06-21 17:17:14 -0700599 reason = "bitmap chunksize not a power of 2";
NeilBrown1b04be92009-12-14 12:49:53 +1100600 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
NeilBrown7dd5d342006-01-06 00:20:39 -0800601 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700602 else if (write_behind > COUNTER_MAX)
603 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700604 if (reason) {
605 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
606 bmname(bitmap), reason);
607 goto out;
608 }
609
610 /* keep the array size field of the bitmap superblock up to date */
611 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
612
NeilBrown278c1ca2012-03-19 12:46:40 +1100613 if (bitmap->mddev->persistent) {
614 /*
615 * We have a persistent array superblock, so compare the
616 * bitmap's UUID and event counter to the mddev's
617 */
618 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
619 printk(KERN_INFO
620 "%s: bitmap superblock UUID mismatch\n",
621 bmname(bitmap));
622 goto out;
623 }
624 events = le64_to_cpu(sb->events);
625 if (events < bitmap->mddev->events) {
626 printk(KERN_INFO
627 "%s: bitmap file is out of date (%llu < %llu) "
628 "-- forcing full recovery\n",
629 bmname(bitmap), events,
630 (unsigned long long) bitmap->mddev->events);
NeilBrownb405fe92012-05-22 13:55:15 +1000631 set_bit(BITMAP_STALE, &bitmap->flags);
NeilBrown278c1ca2012-03-19 12:46:40 +1100632 }
633 }
NeilBrown32a76272005-06-21 17:17:14 -0700634
NeilBrown32a76272005-06-21 17:17:14 -0700635 /* assign fields using values from superblock */
NeilBrown4f2e6392006-10-21 10:24:09 -0700636 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800637 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
NeilBrownb405fe92012-05-22 13:55:15 +1000638 set_bit(BITMAP_HOSTENDIAN, &bitmap->flags);
NeilBrown32a76272005-06-21 17:17:14 -0700639 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
640 err = 0;
641out:
Cong Wangb2f46e62011-11-28 13:25:44 +0800642 kunmap_atomic(sb);
NeilBrownef99bf42012-05-22 13:55:08 +1000643out_no_sb:
NeilBrownb405fe92012-05-22 13:55:15 +1000644 if (test_bit(BITMAP_STALE, &bitmap->flags))
NeilBrownef99bf42012-05-22 13:55:08 +1000645 bitmap->events_cleared = bitmap->mddev->events;
646 bitmap->mddev->bitmap_info.chunksize = chunksize;
647 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
648 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500649 bitmap->mddev->bitmap_info.nodes = nodes;
NeilBrown1dff2b82012-05-22 13:55:34 +1000650 if (bitmap->mddev->bitmap_info.space == 0 ||
651 bitmap->mddev->bitmap_info.space > sectors_reserved)
652 bitmap->mddev->bitmap_info.space = sectors_reserved;
NeilBrown32a76272005-06-21 17:17:14 -0700653 if (err)
654 bitmap_print_sb(bitmap);
655 return err;
656}
657
NeilBrown32a76272005-06-21 17:17:14 -0700658/*
659 * general bitmap file operations
660 */
661
NeilBrownece5cff2009-12-14 12:49:56 +1100662/*
663 * on-disk bitmap:
664 *
665 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
666 * file a page at a time. There's a superblock at the start of the file.
667 */
NeilBrown32a76272005-06-21 17:17:14 -0700668/* calculate the index of the page that contains this bit */
NeilBrown1ec885c2012-05-22 13:55:10 +1000669static inline unsigned long file_page_index(struct bitmap_storage *store,
670 unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700671{
NeilBrown1ec885c2012-05-22 13:55:10 +1000672 if (store->sb_page)
NeilBrownece5cff2009-12-14 12:49:56 +1100673 chunk += sizeof(bitmap_super_t) << 3;
674 return chunk >> PAGE_BIT_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700675}
676
677/* calculate the (bit) offset of this bit within a page */
NeilBrown1ec885c2012-05-22 13:55:10 +1000678static inline unsigned long file_page_offset(struct bitmap_storage *store,
679 unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700680{
NeilBrown1ec885c2012-05-22 13:55:10 +1000681 if (store->sb_page)
NeilBrownece5cff2009-12-14 12:49:56 +1100682 chunk += sizeof(bitmap_super_t) << 3;
683 return chunk & (PAGE_BITS - 1);
NeilBrown32a76272005-06-21 17:17:14 -0700684}
685
686/*
687 * return a pointer to the page in the filemap that contains the given bit
688 *
NeilBrown32a76272005-06-21 17:17:14 -0700689 */
NeilBrown1ec885c2012-05-22 13:55:10 +1000690static inline struct page *filemap_get_page(struct bitmap_storage *store,
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000691 unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700692{
NeilBrown1ec885c2012-05-22 13:55:10 +1000693 if (file_page_index(store, chunk) >= store->file_pages)
NeilBrownac2f40b2010-06-01 19:37:31 +1000694 return NULL;
NeilBrownf2e06c52014-05-28 13:39:23 +1000695 return store->filemap[file_page_index(store, chunk)];
NeilBrown32a76272005-06-21 17:17:14 -0700696}
697
NeilBrownd1244cb2012-05-22 13:55:12 +1000698static int bitmap_storage_alloc(struct bitmap_storage *store,
699 unsigned long chunks, int with_super)
700{
701 int pnum;
702 unsigned long num_pages;
703 unsigned long bytes;
704
705 bytes = DIV_ROUND_UP(chunks, 8);
706 if (with_super)
707 bytes += sizeof(bitmap_super_t);
708
709 num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
710
711 store->filemap = kmalloc(sizeof(struct page *)
712 * num_pages, GFP_KERNEL);
713 if (!store->filemap)
714 return -ENOMEM;
715
716 if (with_super && !store->sb_page) {
NeilBrownd60b4792012-05-22 13:55:25 +1000717 store->sb_page = alloc_page(GFP_KERNEL|__GFP_ZERO);
NeilBrownd1244cb2012-05-22 13:55:12 +1000718 if (store->sb_page == NULL)
719 return -ENOMEM;
720 store->sb_page->index = 0;
721 }
722 pnum = 0;
723 if (store->sb_page) {
724 store->filemap[0] = store->sb_page;
725 pnum = 1;
726 }
727 for ( ; pnum < num_pages; pnum++) {
NeilBrownd60b4792012-05-22 13:55:25 +1000728 store->filemap[pnum] = alloc_page(GFP_KERNEL|__GFP_ZERO);
NeilBrownd1244cb2012-05-22 13:55:12 +1000729 if (!store->filemap[pnum]) {
730 store->file_pages = pnum;
731 return -ENOMEM;
732 }
733 store->filemap[pnum]->index = pnum;
734 }
735 store->file_pages = pnum;
736
737 /* We need 4 bits per page, rounded up to a multiple
738 * of sizeof(unsigned long) */
739 store->filemap_attr = kzalloc(
740 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
741 GFP_KERNEL);
742 if (!store->filemap_attr)
743 return -ENOMEM;
744
745 store->bytes = bytes;
746
747 return 0;
748}
749
NeilBrownfae7d322012-05-22 13:55:21 +1000750static void bitmap_file_unmap(struct bitmap_storage *store)
NeilBrown32a76272005-06-21 17:17:14 -0700751{
752 struct page **map, *sb_page;
NeilBrown32a76272005-06-21 17:17:14 -0700753 int pages;
NeilBrownfae7d322012-05-22 13:55:21 +1000754 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700755
NeilBrownfae7d322012-05-22 13:55:21 +1000756 file = store->file;
NeilBrown1ec885c2012-05-22 13:55:10 +1000757 map = store->filemap;
NeilBrown1ec885c2012-05-22 13:55:10 +1000758 pages = store->file_pages;
NeilBrown1ec885c2012-05-22 13:55:10 +1000759 sb_page = store->sb_page;
NeilBrown32a76272005-06-21 17:17:14 -0700760
761 while (pages--)
NeilBrownece5cff2009-12-14 12:49:56 +1100762 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700763 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700764 kfree(map);
NeilBrownfae7d322012-05-22 13:55:21 +1000765 kfree(store->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700766
NeilBrownd785a062006-06-26 00:27:48 -0700767 if (sb_page)
768 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700769
NeilBrownd785a062006-06-26 00:27:48 -0700770 if (file) {
Al Viro496ad9a2013-01-23 17:07:38 -0500771 struct inode *inode = file_inode(file);
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800772 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700773 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700774 }
NeilBrown32a76272005-06-21 17:17:14 -0700775}
776
NeilBrown32a76272005-06-21 17:17:14 -0700777/*
778 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
779 * then it is no longer reliable, so we stop using it and we mark the file
780 * as failed in the superblock
781 */
782static void bitmap_file_kick(struct bitmap *bitmap)
783{
784 char *path, *ptr = NULL;
785
NeilBrownb405fe92012-05-22 13:55:15 +1000786 if (!test_and_set_bit(BITMAP_STALE, &bitmap->flags)) {
NeilBrown4ad13662007-07-17 04:06:13 -0700787 bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700788
NeilBrown1ec885c2012-05-22 13:55:10 +1000789 if (bitmap->storage.file) {
NeilBrown4ad13662007-07-17 04:06:13 -0700790 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
791 if (path)
NeilBrown1ec885c2012-05-22 13:55:10 +1000792 ptr = d_path(&bitmap->storage.file->f_path,
793 path, PAGE_SIZE);
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700794
NeilBrown4ad13662007-07-17 04:06:13 -0700795 printk(KERN_ALERT
796 "%s: kicking failed bitmap file %s from array!\n",
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700797 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700798
NeilBrown4ad13662007-07-17 04:06:13 -0700799 kfree(path);
800 } else
801 printk(KERN_ALERT
802 "%s: disabling internal bitmap due to errors\n",
803 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700804 }
NeilBrown32a76272005-06-21 17:17:14 -0700805}
806
807enum bitmap_page_attr {
NeilBrownac2f40b2010-06-01 19:37:31 +1000808 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
NeilBrown5a537df2011-09-21 15:37:46 +1000809 BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
810 * i.e. counter is 1 or 2. */
NeilBrownac2f40b2010-06-01 19:37:31 +1000811 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
NeilBrown32a76272005-06-21 17:17:14 -0700812};
813
NeilBrownd1891222012-05-22 13:55:09 +1000814static inline void set_page_attr(struct bitmap *bitmap, int pnum,
815 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700816{
NeilBrownbdfd1142012-05-22 13:55:22 +1000817 set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700818}
819
NeilBrownd1891222012-05-22 13:55:09 +1000820static inline void clear_page_attr(struct bitmap *bitmap, int pnum,
821 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700822{
NeilBrownbdfd1142012-05-22 13:55:22 +1000823 clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700824}
825
NeilBrownbdfd1142012-05-22 13:55:22 +1000826static inline int test_page_attr(struct bitmap *bitmap, int pnum,
827 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700828{
NeilBrown1ec885c2012-05-22 13:55:10 +1000829 return test_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700830}
831
NeilBrownbdfd1142012-05-22 13:55:22 +1000832static inline int test_and_clear_page_attr(struct bitmap *bitmap, int pnum,
833 enum bitmap_page_attr attr)
834{
835 return test_and_clear_bit((pnum<<2) + attr,
836 bitmap->storage.filemap_attr);
837}
NeilBrown32a76272005-06-21 17:17:14 -0700838/*
839 * bitmap_file_set_bit -- called before performing a write to the md device
840 * to set (and eventually sync) a particular bit in the bitmap file
841 *
842 * we set the bit immediately, then we record the page number so that
843 * when an unplug occurs, we can flush the dirty pages out to disk
844 */
845static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
846{
847 unsigned long bit;
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000848 struct page *page;
NeilBrown32a76272005-06-21 17:17:14 -0700849 void *kaddr;
NeilBrown40cffcc2012-05-22 13:55:24 +1000850 unsigned long chunk = block >> bitmap->counts.chunkshift;
NeilBrown32a76272005-06-21 17:17:14 -0700851
NeilBrown1ec885c2012-05-22 13:55:10 +1000852 page = filemap_get_page(&bitmap->storage, chunk);
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000853 if (!page)
854 return;
NeilBrown1ec885c2012-05-22 13:55:10 +1000855 bit = file_page_offset(&bitmap->storage, chunk);
NeilBrown32a76272005-06-21 17:17:14 -0700856
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000857 /* set the bit */
Cong Wangb2f46e62011-11-28 13:25:44 +0800858 kaddr = kmap_atomic(page);
NeilBrownb405fe92012-05-22 13:55:15 +1000859 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000860 set_bit(bit, kaddr);
861 else
Akinobu Mita3f810b62013-04-24 11:42:41 +1000862 set_bit_le(bit, kaddr);
Cong Wangb2f46e62011-11-28 13:25:44 +0800863 kunmap_atomic(kaddr);
NeilBrown36a4e1f2011-10-07 14:23:17 +1100864 pr_debug("set file bit %lu page %lu\n", bit, page->index);
NeilBrown32a76272005-06-21 17:17:14 -0700865 /* record page number so it gets flushed to disk when unplug occurs */
NeilBrownd1891222012-05-22 13:55:09 +1000866 set_page_attr(bitmap, page->index, BITMAP_PAGE_DIRTY);
NeilBrown32a76272005-06-21 17:17:14 -0700867}
868
NeilBrownef99bf42012-05-22 13:55:08 +1000869static void bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block)
870{
871 unsigned long bit;
872 struct page *page;
873 void *paddr;
NeilBrown40cffcc2012-05-22 13:55:24 +1000874 unsigned long chunk = block >> bitmap->counts.chunkshift;
NeilBrownef99bf42012-05-22 13:55:08 +1000875
NeilBrown1ec885c2012-05-22 13:55:10 +1000876 page = filemap_get_page(&bitmap->storage, chunk);
NeilBrownef99bf42012-05-22 13:55:08 +1000877 if (!page)
878 return;
NeilBrown1ec885c2012-05-22 13:55:10 +1000879 bit = file_page_offset(&bitmap->storage, chunk);
NeilBrownef99bf42012-05-22 13:55:08 +1000880 paddr = kmap_atomic(page);
NeilBrownb405fe92012-05-22 13:55:15 +1000881 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
NeilBrownef99bf42012-05-22 13:55:08 +1000882 clear_bit(bit, paddr);
883 else
Akinobu Mita3f810b62013-04-24 11:42:41 +1000884 clear_bit_le(bit, paddr);
NeilBrownef99bf42012-05-22 13:55:08 +1000885 kunmap_atomic(paddr);
NeilBrownd1891222012-05-22 13:55:09 +1000886 if (!test_page_attr(bitmap, page->index, BITMAP_PAGE_NEEDWRITE)) {
887 set_page_attr(bitmap, page->index, BITMAP_PAGE_PENDING);
NeilBrownef99bf42012-05-22 13:55:08 +1000888 bitmap->allclean = 0;
889 }
890}
891
NeilBrown32a76272005-06-21 17:17:14 -0700892/* this gets called when the md device is ready to unplug its underlying
893 * (slave) device queues -- before we let any writes go down, we need to
894 * sync the dirty pages of the bitmap file to disk */
NeilBrown4ad13662007-07-17 04:06:13 -0700895void bitmap_unplug(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700896{
NeilBrown74667122012-05-22 13:55:19 +1000897 unsigned long i;
NeilBrownec7a3192006-06-26 00:27:45 -0700898 int dirty, need_write;
NeilBrown32a76272005-06-21 17:17:14 -0700899
NeilBrown62f82fa2012-05-22 13:55:21 +1000900 if (!bitmap || !bitmap->storage.filemap ||
901 test_bit(BITMAP_STALE, &bitmap->flags))
NeilBrown4ad13662007-07-17 04:06:13 -0700902 return;
NeilBrown32a76272005-06-21 17:17:14 -0700903
904 /* look at each page to see if there are any set bits that need to be
905 * flushed out to disk */
NeilBrown1ec885c2012-05-22 13:55:10 +1000906 for (i = 0; i < bitmap->storage.file_pages; i++) {
NeilBrownbdfd1142012-05-22 13:55:22 +1000907 if (!bitmap->storage.filemap)
NeilBrown4ad13662007-07-17 04:06:13 -0700908 return;
NeilBrownbdfd1142012-05-22 13:55:22 +1000909 dirty = test_and_clear_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
910 need_write = test_and_clear_page_attr(bitmap, i,
911 BITMAP_PAGE_NEEDWRITE);
912 if (dirty || need_write) {
NeilBrownd1891222012-05-22 13:55:09 +1000913 clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING);
NeilBrownbdfd1142012-05-22 13:55:22 +1000914 write_page(bitmap, bitmap->storage.filemap[i], 0);
915 }
NeilBrown32a76272005-06-21 17:17:14 -0700916 }
NeilBrown4b5060d2014-09-09 14:13:51 +1000917 if (bitmap->storage.file)
918 wait_event(bitmap->write_wait,
919 atomic_read(&bitmap->pending_writes)==0);
920 else
921 md_super_wait(bitmap->mddev);
922
NeilBrownb405fe92012-05-22 13:55:15 +1000923 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
NeilBrownd785a062006-06-26 00:27:48 -0700924 bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700925}
NeilBrownac2f40b2010-06-01 19:37:31 +1000926EXPORT_SYMBOL(bitmap_unplug);
NeilBrown32a76272005-06-21 17:17:14 -0700927
NeilBrown6a079972005-09-09 16:23:44 -0700928static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -0700929/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
930 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
931 * memory mapping of the bitmap file
932 * Special cases:
933 * if there's no bitmap file, or if the bitmap file had been
934 * previously kicked from the array, we mark all the bits as
935 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -0700936 *
937 * We ignore all bits for sectors that end earlier than 'start'.
938 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -0700939 */
NeilBrown6a079972005-09-09 16:23:44 -0700940static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -0700941{
942 unsigned long i, chunks, index, oldindex, bit;
NeilBrown27581e52012-05-22 13:55:08 +1000943 struct page *page = NULL;
NeilBrownd1244cb2012-05-22 13:55:12 +1000944 unsigned long bit_cnt = 0;
NeilBrown32a76272005-06-21 17:17:14 -0700945 struct file *file;
NeilBrownd1244cb2012-05-22 13:55:12 +1000946 unsigned long offset;
NeilBrown32a76272005-06-21 17:17:14 -0700947 int outofdate;
948 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -0800949 void *paddr;
NeilBrown1ec885c2012-05-22 13:55:10 +1000950 struct bitmap_storage *store = &bitmap->storage;
NeilBrown32a76272005-06-21 17:17:14 -0700951
NeilBrown40cffcc2012-05-22 13:55:24 +1000952 chunks = bitmap->counts.chunks;
NeilBrown1ec885c2012-05-22 13:55:10 +1000953 file = store->file;
NeilBrown32a76272005-06-21 17:17:14 -0700954
NeilBrownef99bf42012-05-22 13:55:08 +1000955 if (!file && !bitmap->mddev->bitmap_info.offset) {
956 /* No permanent bitmap - fill with '1s'. */
NeilBrown1ec885c2012-05-22 13:55:10 +1000957 store->filemap = NULL;
958 store->file_pages = 0;
NeilBrownef99bf42012-05-22 13:55:08 +1000959 for (i = 0; i < chunks ; i++) {
960 /* if the disk bit is set, set the memory bit */
NeilBrown40cffcc2012-05-22 13:55:24 +1000961 int needed = ((sector_t)(i+1) << (bitmap->counts.chunkshift)
NeilBrownef99bf42012-05-22 13:55:08 +1000962 >= start);
963 bitmap_set_memory_bits(bitmap,
NeilBrown40cffcc2012-05-22 13:55:24 +1000964 (sector_t)i << bitmap->counts.chunkshift,
NeilBrownef99bf42012-05-22 13:55:08 +1000965 needed);
966 }
967 return 0;
968 }
NeilBrown32a76272005-06-21 17:17:14 -0700969
NeilBrownb405fe92012-05-22 13:55:15 +1000970 outofdate = test_bit(BITMAP_STALE, &bitmap->flags);
NeilBrown32a76272005-06-21 17:17:14 -0700971 if (outofdate)
972 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
973 "recovery\n", bmname(bitmap));
974
NeilBrownd1244cb2012-05-22 13:55:12 +1000975 if (file && i_size_read(file->f_mapping->host) < store->bytes) {
NeilBrown32a76272005-06-21 17:17:14 -0700976 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
NeilBrownd1244cb2012-05-22 13:55:12 +1000977 bmname(bitmap),
978 (unsigned long) i_size_read(file->f_mapping->host),
979 store->bytes);
NeilBrown4ad13662007-07-17 04:06:13 -0700980 goto err;
NeilBrown32a76272005-06-21 17:17:14 -0700981 }
NeilBrownbc7f77d2005-06-21 17:17:17 -0700982
NeilBrown32a76272005-06-21 17:17:14 -0700983 oldindex = ~0L;
NeilBrownd1244cb2012-05-22 13:55:12 +1000984 offset = 0;
985 if (!bitmap->mddev->bitmap_info.external)
986 offset = sizeof(bitmap_super_t);
NeilBrown32a76272005-06-21 17:17:14 -0700987
988 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -0800989 int b;
NeilBrown1ec885c2012-05-22 13:55:10 +1000990 index = file_page_index(&bitmap->storage, i);
991 bit = file_page_offset(&bitmap->storage, i);
NeilBrown32a76272005-06-21 17:17:14 -0700992 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -0700993 int count;
NeilBrown32a76272005-06-21 17:17:14 -0700994 /* unmap the old page, we're done with it */
NeilBrownd1244cb2012-05-22 13:55:12 +1000995 if (index == store->file_pages-1)
996 count = store->bytes - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -0700997 else
998 count = PAGE_SIZE;
NeilBrown1ec885c2012-05-22 13:55:10 +1000999 page = store->filemap[index];
NeilBrown27581e52012-05-22 13:55:08 +10001000 if (file)
1001 ret = read_page(file, index, bitmap,
1002 count, page);
1003 else
1004 ret = read_sb_page(
1005 bitmap->mddev,
1006 bitmap->mddev->bitmap_info.offset,
1007 page,
1008 index, count);
1009
1010 if (ret)
NeilBrown4ad13662007-07-17 04:06:13 -07001011 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -07001012
NeilBrown32a76272005-06-21 17:17:14 -07001013 oldindex = index;
NeilBrown32a76272005-06-21 17:17:14 -07001014
1015 if (outofdate) {
1016 /*
1017 * if bitmap is out of date, dirty the
NeilBrownac2f40b2010-06-01 19:37:31 +10001018 * whole page and write it out
NeilBrown32a76272005-06-21 17:17:14 -07001019 */
Cong Wangb2f46e62011-11-28 13:25:44 +08001020 paddr = kmap_atomic(page);
NeilBrownea03aff2006-01-06 00:20:34 -08001021 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -07001022 PAGE_SIZE - offset);
Cong Wangb2f46e62011-11-28 13:25:44 +08001023 kunmap_atomic(paddr);
NeilBrown4ad13662007-07-17 04:06:13 -07001024 write_page(bitmap, page, 1);
1025
1026 ret = -EIO;
NeilBrownb405fe92012-05-22 13:55:15 +10001027 if (test_bit(BITMAP_WRITE_ERROR,
1028 &bitmap->flags))
NeilBrown4ad13662007-07-17 04:06:13 -07001029 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001030 }
NeilBrown32a76272005-06-21 17:17:14 -07001031 }
Cong Wangb2f46e62011-11-28 13:25:44 +08001032 paddr = kmap_atomic(page);
NeilBrownb405fe92012-05-22 13:55:15 +10001033 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
NeilBrownea03aff2006-01-06 00:20:34 -08001034 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001035 else
Akinobu Mita6b33aff2011-03-23 16:42:13 -07001036 b = test_bit_le(bit, paddr);
Cong Wangb2f46e62011-11-28 13:25:44 +08001037 kunmap_atomic(paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001038 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -07001039 /* if the disk bit is set, set the memory bit */
NeilBrown40cffcc2012-05-22 13:55:24 +10001040 int needed = ((sector_t)(i+1) << bitmap->counts.chunkshift
NeilBrowndb305e52009-05-07 12:49:06 +10001041 >= start);
1042 bitmap_set_memory_bits(bitmap,
NeilBrown40cffcc2012-05-22 13:55:24 +10001043 (sector_t)i << bitmap->counts.chunkshift,
NeilBrowndb305e52009-05-07 12:49:06 +10001044 needed);
NeilBrown32a76272005-06-21 17:17:14 -07001045 bit_cnt++;
1046 }
NeilBrown27581e52012-05-22 13:55:08 +10001047 offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001048 }
1049
NeilBrown32a76272005-06-21 17:17:14 -07001050 printk(KERN_INFO "%s: bitmap initialized from disk: "
NeilBrownd1244cb2012-05-22 13:55:12 +10001051 "read %lu pages, set %lu of %lu bits\n",
NeilBrown1ec885c2012-05-22 13:55:10 +10001052 bmname(bitmap), store->file_pages,
NeilBrownd1244cb2012-05-22 13:55:12 +10001053 bit_cnt, chunks);
NeilBrown32a76272005-06-21 17:17:14 -07001054
NeilBrown4ad13662007-07-17 04:06:13 -07001055 return 0;
1056
1057 err:
1058 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1059 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001060 return ret;
1061}
1062
NeilBrowna654b9d82005-06-21 17:17:27 -07001063void bitmap_write_all(struct bitmap *bitmap)
1064{
1065 /* We don't actually write all bitmap blocks here,
1066 * just flag them as needing to be written
1067 */
NeilBrownec7a3192006-06-26 00:27:45 -07001068 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001069
NeilBrown1ec885c2012-05-22 13:55:10 +10001070 if (!bitmap || !bitmap->storage.filemap)
NeilBrownef99bf42012-05-22 13:55:08 +10001071 return;
NeilBrown1ec885c2012-05-22 13:55:10 +10001072 if (bitmap->storage.file)
NeilBrownef99bf42012-05-22 13:55:08 +10001073 /* Only one copy, so nothing needed */
1074 return;
1075
NeilBrown1ec885c2012-05-22 13:55:10 +10001076 for (i = 0; i < bitmap->storage.file_pages; i++)
NeilBrownd1891222012-05-22 13:55:09 +10001077 set_page_attr(bitmap, i,
NeilBrownec7a3192006-06-26 00:27:45 -07001078 BITMAP_PAGE_NEEDWRITE);
NeilBrown2585f3e2011-09-21 15:37:46 +10001079 bitmap->allclean = 0;
NeilBrowna654b9d82005-06-21 17:17:27 -07001080}
1081
NeilBrown40cffcc2012-05-22 13:55:24 +10001082static void bitmap_count_page(struct bitmap_counts *bitmap,
1083 sector_t offset, int inc)
NeilBrown32a76272005-06-21 17:17:14 -07001084{
NeilBrown61a0d802012-03-19 12:46:41 +11001085 sector_t chunk = offset >> bitmap->chunkshift;
NeilBrown32a76272005-06-21 17:17:14 -07001086 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1087 bitmap->bp[page].count += inc;
NeilBrown32a76272005-06-21 17:17:14 -07001088 bitmap_checkfree(bitmap, page);
1089}
NeilBrownbf07bb72012-05-22 13:55:06 +10001090
NeilBrown40cffcc2012-05-22 13:55:24 +10001091static void bitmap_set_pending(struct bitmap_counts *bitmap, sector_t offset)
NeilBrownbf07bb72012-05-22 13:55:06 +10001092{
1093 sector_t chunk = offset >> bitmap->chunkshift;
1094 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1095 struct bitmap_page *bp = &bitmap->bp[page];
1096
1097 if (!bp->pending)
1098 bp->pending = 1;
1099}
1100
NeilBrown40cffcc2012-05-22 13:55:24 +10001101static bitmap_counter_t *bitmap_get_counter(struct bitmap_counts *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001102 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001103 int create);
1104
1105/*
1106 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1107 * out to disk
1108 */
1109
NeilBrownfd01b882011-10-11 16:47:53 +11001110void bitmap_daemon_work(struct mddev *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001111{
NeilBrownaa5cbd12009-12-14 12:49:46 +11001112 struct bitmap *bitmap;
NeilBrownaa3163f2005-06-21 17:17:22 -07001113 unsigned long j;
NeilBrownbf07bb72012-05-22 13:55:06 +10001114 unsigned long nextpage;
NeilBrown57dab0b2010-10-19 10:03:39 +11001115 sector_t blocks;
NeilBrown40cffcc2012-05-22 13:55:24 +10001116 struct bitmap_counts *counts;
NeilBrown32a76272005-06-21 17:17:14 -07001117
NeilBrownaa5cbd12009-12-14 12:49:46 +11001118 /* Use a mutex to guard daemon_work against
1119 * bitmap_destroy.
1120 */
NeilBrownc3d97142009-12-14 12:49:52 +11001121 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrownaa5cbd12009-12-14 12:49:46 +11001122 bitmap = mddev->bitmap;
1123 if (bitmap == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001124 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown4ad13662007-07-17 04:06:13 -07001125 return;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001126 }
NeilBrown42a04b52009-12-14 12:49:53 +11001127 if (time_before(jiffies, bitmap->daemon_lastrun
NeilBrown2e61ebb2011-12-23 10:17:50 +11001128 + mddev->bitmap_info.daemon_sleep))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001129 goto done;
1130
NeilBrown32a76272005-06-21 17:17:14 -07001131 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001132 if (bitmap->allclean) {
NeilBrown2e61ebb2011-12-23 10:17:50 +11001133 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001134 goto done;
NeilBrown8311c292008-03-04 14:29:30 -08001135 }
1136 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001137
NeilBrownbf07bb72012-05-22 13:55:06 +10001138 /* Any file-page which is PENDING now needs to be written.
1139 * So set NEEDWRITE now, then after we make any last-minute changes
1140 * we will write it.
1141 */
NeilBrown1ec885c2012-05-22 13:55:10 +10001142 for (j = 0; j < bitmap->storage.file_pages; j++)
NeilBrownbdfd1142012-05-22 13:55:22 +10001143 if (test_and_clear_page_attr(bitmap, j,
1144 BITMAP_PAGE_PENDING))
NeilBrownd1891222012-05-22 13:55:09 +10001145 set_page_attr(bitmap, j,
NeilBrownbf07bb72012-05-22 13:55:06 +10001146 BITMAP_PAGE_NEEDWRITE);
NeilBrownbf07bb72012-05-22 13:55:06 +10001147
1148 if (bitmap->need_sync &&
1149 mddev->bitmap_info.external == 0) {
1150 /* Arrange for superblock update as well as
1151 * other changes */
1152 bitmap_super_t *sb;
1153 bitmap->need_sync = 0;
NeilBrown1ec885c2012-05-22 13:55:10 +10001154 if (bitmap->storage.filemap) {
1155 sb = kmap_atomic(bitmap->storage.sb_page);
NeilBrownef99bf42012-05-22 13:55:08 +10001156 sb->events_cleared =
1157 cpu_to_le64(bitmap->events_cleared);
1158 kunmap_atomic(sb);
NeilBrownd1891222012-05-22 13:55:09 +10001159 set_page_attr(bitmap, 0,
NeilBrownef99bf42012-05-22 13:55:08 +10001160 BITMAP_PAGE_NEEDWRITE);
1161 }
NeilBrownbf07bb72012-05-22 13:55:06 +10001162 }
1163 /* Now look at the bitmap counters and if any are '2' or '1',
1164 * decrement and handle accordingly.
1165 */
NeilBrown40cffcc2012-05-22 13:55:24 +10001166 counts = &bitmap->counts;
1167 spin_lock_irq(&counts->lock);
NeilBrownbf07bb72012-05-22 13:55:06 +10001168 nextpage = 0;
NeilBrown40cffcc2012-05-22 13:55:24 +10001169 for (j = 0; j < counts->chunks; j++) {
NeilBrown32a76272005-06-21 17:17:14 -07001170 bitmap_counter_t *bmc;
NeilBrown40cffcc2012-05-22 13:55:24 +10001171 sector_t block = (sector_t)j << counts->chunkshift;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001172
NeilBrownbf07bb72012-05-22 13:55:06 +10001173 if (j == nextpage) {
1174 nextpage += PAGE_COUNTER_RATIO;
NeilBrown40cffcc2012-05-22 13:55:24 +10001175 if (!counts->bp[j >> PAGE_COUNTER_SHIFT].pending) {
NeilBrownbf07bb72012-05-22 13:55:06 +10001176 j |= PAGE_COUNTER_MASK;
NeilBrownaa3163f2005-06-21 17:17:22 -07001177 continue;
1178 }
NeilBrown40cffcc2012-05-22 13:55:24 +10001179 counts->bp[j >> PAGE_COUNTER_SHIFT].pending = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001180 }
NeilBrown40cffcc2012-05-22 13:55:24 +10001181 bmc = bitmap_get_counter(counts,
NeilBrownef99bf42012-05-22 13:55:08 +10001182 block,
NeilBrowndb305e52009-05-07 12:49:06 +10001183 &blocks, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001184
NeilBrownbf07bb72012-05-22 13:55:06 +10001185 if (!bmc) {
1186 j |= PAGE_COUNTER_MASK;
1187 continue;
1188 }
1189 if (*bmc == 1 && !bitmap->need_sync) {
1190 /* We can clear the bit */
NeilBrownbf07bb72012-05-22 13:55:06 +10001191 *bmc = 0;
NeilBrown40cffcc2012-05-22 13:55:24 +10001192 bitmap_count_page(counts, block, -1);
NeilBrownef99bf42012-05-22 13:55:08 +10001193 bitmap_file_clear_bit(bitmap, block);
NeilBrownbf07bb72012-05-22 13:55:06 +10001194 } else if (*bmc && *bmc <= 2) {
1195 *bmc = 1;
NeilBrown40cffcc2012-05-22 13:55:24 +10001196 bitmap_set_pending(counts, block);
NeilBrown2585f3e2011-09-21 15:37:46 +10001197 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001198 }
NeilBrown32a76272005-06-21 17:17:14 -07001199 }
NeilBrown40cffcc2012-05-22 13:55:24 +10001200 spin_unlock_irq(&counts->lock);
NeilBrown32a76272005-06-21 17:17:14 -07001201
NeilBrownbf07bb72012-05-22 13:55:06 +10001202 /* Now start writeout on any page in NEEDWRITE that isn't DIRTY.
1203 * DIRTY pages need to be written by bitmap_unplug so it can wait
1204 * for them.
1205 * If we find any DIRTY page we stop there and let bitmap_unplug
1206 * handle all the rest. This is important in the case where
1207 * the first blocking holds the superblock and it has been updated.
1208 * We mustn't write any other blocks before the superblock.
1209 */
NeilBrown62f82fa2012-05-22 13:55:21 +10001210 for (j = 0;
1211 j < bitmap->storage.file_pages
1212 && !test_bit(BITMAP_STALE, &bitmap->flags);
1213 j++) {
NeilBrownbf07bb72012-05-22 13:55:06 +10001214
NeilBrownd1891222012-05-22 13:55:09 +10001215 if (test_page_attr(bitmap, j,
NeilBrownbf07bb72012-05-22 13:55:06 +10001216 BITMAP_PAGE_DIRTY))
1217 /* bitmap_unplug will handle the rest */
1218 break;
NeilBrownbdfd1142012-05-22 13:55:22 +10001219 if (test_and_clear_page_attr(bitmap, j,
1220 BITMAP_PAGE_NEEDWRITE)) {
NeilBrown1ec885c2012-05-22 13:55:10 +10001221 write_page(bitmap, bitmap->storage.filemap[j], 0);
NeilBrownbf07bb72012-05-22 13:55:06 +10001222 }
1223 }
NeilBrownbf07bb72012-05-22 13:55:06 +10001224
NeilBrown7be3dfe2008-03-10 11:43:48 -07001225 done:
NeilBrown8311c292008-03-04 14:29:30 -08001226 if (bitmap->allclean == 0)
NeilBrown2e61ebb2011-12-23 10:17:50 +11001227 mddev->thread->timeout =
1228 mddev->bitmap_info.daemon_sleep;
NeilBrownc3d97142009-12-14 12:49:52 +11001229 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown32a76272005-06-21 17:17:14 -07001230}
1231
NeilBrown40cffcc2012-05-22 13:55:24 +10001232static bitmap_counter_t *bitmap_get_counter(struct bitmap_counts *bitmap,
NeilBrown57dab0b2010-10-19 10:03:39 +11001233 sector_t offset, sector_t *blocks,
NeilBrown32a76272005-06-21 17:17:14 -07001234 int create)
NeilBrownee305ac2009-09-23 18:06:44 +10001235__releases(bitmap->lock)
1236__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -07001237{
1238 /* If 'create', we might release the lock and reclaim it.
1239 * The lock must have been taken with interrupts enabled.
1240 * If !create, we don't release the lock.
1241 */
NeilBrown61a0d802012-03-19 12:46:41 +11001242 sector_t chunk = offset >> bitmap->chunkshift;
NeilBrown32a76272005-06-21 17:17:14 -07001243 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1244 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1245 sector_t csize;
NeilBrownef425672010-06-01 19:37:33 +10001246 int err;
NeilBrown32a76272005-06-21 17:17:14 -07001247
NeilBrownef425672010-06-01 19:37:33 +10001248 err = bitmap_checkpage(bitmap, page, create);
1249
1250 if (bitmap->bp[page].hijacked ||
1251 bitmap->bp[page].map == NULL)
NeilBrown61a0d802012-03-19 12:46:41 +11001252 csize = ((sector_t)1) << (bitmap->chunkshift +
NeilBrownef425672010-06-01 19:37:33 +10001253 PAGE_COUNTER_SHIFT - 1);
1254 else
NeilBrown61a0d802012-03-19 12:46:41 +11001255 csize = ((sector_t)1) << bitmap->chunkshift;
NeilBrownef425672010-06-01 19:37:33 +10001256 *blocks = csize - (offset & (csize - 1));
1257
1258 if (err < 0)
NeilBrown32a76272005-06-21 17:17:14 -07001259 return NULL;
NeilBrownef425672010-06-01 19:37:33 +10001260
NeilBrown32a76272005-06-21 17:17:14 -07001261 /* now locked ... */
1262
1263 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1264 /* should we use the first or second counter field
1265 * of the hijacked pointer? */
1266 int hi = (pageoff > PAGE_COUNTER_MASK);
NeilBrown32a76272005-06-21 17:17:14 -07001267 return &((bitmap_counter_t *)
1268 &bitmap->bp[page].map)[hi];
NeilBrownef425672010-06-01 19:37:33 +10001269 } else /* page is allocated */
NeilBrown32a76272005-06-21 17:17:14 -07001270 return (bitmap_counter_t *)
1271 &(bitmap->bp[page].map[pageoff]);
NeilBrown32a76272005-06-21 17:17:14 -07001272}
1273
NeilBrown4b6d2872005-09-09 16:23:47 -07001274int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001275{
NeilBrownac2f40b2010-06-01 19:37:31 +10001276 if (!bitmap)
1277 return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001278
1279 if (behind) {
Paul Clements696fcd52010-03-08 16:02:37 +11001280 int bw;
NeilBrown4b6d2872005-09-09 16:23:47 -07001281 atomic_inc(&bitmap->behind_writes);
Paul Clements696fcd52010-03-08 16:02:37 +11001282 bw = atomic_read(&bitmap->behind_writes);
1283 if (bw > bitmap->behind_writes_used)
1284 bitmap->behind_writes_used = bw;
1285
NeilBrown36a4e1f2011-10-07 14:23:17 +11001286 pr_debug("inc write-behind count %d/%lu\n",
1287 bw, bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001288 }
1289
NeilBrown32a76272005-06-21 17:17:14 -07001290 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001291 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001292 bitmap_counter_t *bmc;
1293
NeilBrown40cffcc2012-05-22 13:55:24 +10001294 spin_lock_irq(&bitmap->counts.lock);
1295 bmc = bitmap_get_counter(&bitmap->counts, offset, &blocks, 1);
NeilBrown32a76272005-06-21 17:17:14 -07001296 if (!bmc) {
NeilBrown40cffcc2012-05-22 13:55:24 +10001297 spin_unlock_irq(&bitmap->counts.lock);
NeilBrown32a76272005-06-21 17:17:14 -07001298 return 0;
1299 }
1300
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001301 if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
Neil Brownda6e1a32007-02-08 14:20:37 -08001302 DEFINE_WAIT(__wait);
1303 /* note that it is safe to do the prepare_to_wait
1304 * after the test as long as we do it before dropping
1305 * the spinlock.
1306 */
1307 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1308 TASK_UNINTERRUPTIBLE);
NeilBrown40cffcc2012-05-22 13:55:24 +10001309 spin_unlock_irq(&bitmap->counts.lock);
NeilBrownf54a9d02012-08-02 08:33:20 +10001310 schedule();
Neil Brownda6e1a32007-02-08 14:20:37 -08001311 finish_wait(&bitmap->overflow_wait, &__wait);
1312 continue;
1313 }
1314
NeilBrownac2f40b2010-06-01 19:37:31 +10001315 switch (*bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001316 case 0:
1317 bitmap_file_set_bit(bitmap, offset);
NeilBrown40cffcc2012-05-22 13:55:24 +10001318 bitmap_count_page(&bitmap->counts, offset, 1);
NeilBrown32a76272005-06-21 17:17:14 -07001319 /* fall through */
1320 case 1:
1321 *bmc = 2;
1322 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001323
NeilBrown32a76272005-06-21 17:17:14 -07001324 (*bmc)++;
1325
NeilBrown40cffcc2012-05-22 13:55:24 +10001326 spin_unlock_irq(&bitmap->counts.lock);
NeilBrown32a76272005-06-21 17:17:14 -07001327
1328 offset += blocks;
1329 if (sectors > blocks)
1330 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001331 else
1332 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001333 }
1334 return 0;
1335}
NeilBrownac2f40b2010-06-01 19:37:31 +10001336EXPORT_SYMBOL(bitmap_startwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001337
1338void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
NeilBrown4b6d2872005-09-09 16:23:47 -07001339 int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001340{
NeilBrownac2f40b2010-06-01 19:37:31 +10001341 if (!bitmap)
1342 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001343 if (behind) {
NeilBrowne5551902010-03-31 11:21:44 +11001344 if (atomic_dec_and_test(&bitmap->behind_writes))
1345 wake_up(&bitmap->behind_wait);
NeilBrown36a4e1f2011-10-07 14:23:17 +11001346 pr_debug("dec write-behind count %d/%lu\n",
1347 atomic_read(&bitmap->behind_writes),
1348 bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001349 }
1350
NeilBrown32a76272005-06-21 17:17:14 -07001351 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001352 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001353 unsigned long flags;
1354 bitmap_counter_t *bmc;
1355
NeilBrown40cffcc2012-05-22 13:55:24 +10001356 spin_lock_irqsave(&bitmap->counts.lock, flags);
1357 bmc = bitmap_get_counter(&bitmap->counts, offset, &blocks, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001358 if (!bmc) {
NeilBrown40cffcc2012-05-22 13:55:24 +10001359 spin_unlock_irqrestore(&bitmap->counts.lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001360 return;
1361 }
1362
NeilBrown961902c2011-12-23 09:57:48 +11001363 if (success && !bitmap->mddev->degraded &&
Neil Browna0da84f2008-06-28 08:31:22 +10001364 bitmap->events_cleared < bitmap->mddev->events) {
1365 bitmap->events_cleared = bitmap->mddev->events;
1366 bitmap->need_sync = 1;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001367 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
Neil Browna0da84f2008-06-28 08:31:22 +10001368 }
1369
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001370 if (!success && !NEEDED(*bmc))
NeilBrown32a76272005-06-21 17:17:14 -07001371 *bmc |= NEEDED_MASK;
1372
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001373 if (COUNTER(*bmc) == COUNTER_MAX)
Neil Brownda6e1a32007-02-08 14:20:37 -08001374 wake_up(&bitmap->overflow_wait);
1375
NeilBrown32a76272005-06-21 17:17:14 -07001376 (*bmc)--;
NeilBrown2585f3e2011-09-21 15:37:46 +10001377 if (*bmc <= 2) {
NeilBrown40cffcc2012-05-22 13:55:24 +10001378 bitmap_set_pending(&bitmap->counts, offset);
NeilBrown2585f3e2011-09-21 15:37:46 +10001379 bitmap->allclean = 0;
1380 }
NeilBrown40cffcc2012-05-22 13:55:24 +10001381 spin_unlock_irqrestore(&bitmap->counts.lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001382 offset += blocks;
1383 if (sectors > blocks)
1384 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001385 else
1386 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001387 }
1388}
NeilBrownac2f40b2010-06-01 19:37:31 +10001389EXPORT_SYMBOL(bitmap_endwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001390
NeilBrown57dab0b2010-10-19 10:03:39 +11001391static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001392 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001393{
1394 bitmap_counter_t *bmc;
1395 int rv;
1396 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1397 *blocks = 1024;
1398 return 1; /* always resync if no bitmap */
1399 }
NeilBrown40cffcc2012-05-22 13:55:24 +10001400 spin_lock_irq(&bitmap->counts.lock);
1401 bmc = bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001402 rv = 0;
1403 if (bmc) {
1404 /* locked */
1405 if (RESYNC(*bmc))
1406 rv = 1;
1407 else if (NEEDED(*bmc)) {
1408 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001409 if (!degraded) { /* don't set/clear bits if degraded */
1410 *bmc |= RESYNC_MASK;
1411 *bmc &= ~NEEDED_MASK;
1412 }
NeilBrown32a76272005-06-21 17:17:14 -07001413 }
1414 }
NeilBrown40cffcc2012-05-22 13:55:24 +10001415 spin_unlock_irq(&bitmap->counts.lock);
NeilBrown32a76272005-06-21 17:17:14 -07001416 return rv;
1417}
1418
NeilBrown57dab0b2010-10-19 10:03:39 +11001419int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001420 int degraded)
1421{
1422 /* bitmap_start_sync must always report on multiples of whole
1423 * pages, otherwise resync (which is very PAGE_SIZE based) will
1424 * get confused.
1425 * So call __bitmap_start_sync repeatedly (if needed) until
1426 * At least PAGE_SIZE>>9 blocks are covered.
1427 * Return the 'or' of the result.
1428 */
1429 int rv = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001430 sector_t blocks1;
NeilBrown1187cf02009-03-31 14:27:02 +11001431
1432 *blocks = 0;
1433 while (*blocks < (PAGE_SIZE>>9)) {
1434 rv |= __bitmap_start_sync(bitmap, offset,
1435 &blocks1, degraded);
1436 offset += blocks1;
1437 *blocks += blocks1;
1438 }
1439 return rv;
1440}
NeilBrownac2f40b2010-06-01 19:37:31 +10001441EXPORT_SYMBOL(bitmap_start_sync);
NeilBrown1187cf02009-03-31 14:27:02 +11001442
NeilBrown57dab0b2010-10-19 10:03:39 +11001443void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
NeilBrown32a76272005-06-21 17:17:14 -07001444{
1445 bitmap_counter_t *bmc;
1446 unsigned long flags;
NeilBrownac2f40b2010-06-01 19:37:31 +10001447
1448 if (bitmap == NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001449 *blocks = 1024;
1450 return;
1451 }
NeilBrown40cffcc2012-05-22 13:55:24 +10001452 spin_lock_irqsave(&bitmap->counts.lock, flags);
1453 bmc = bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001454 if (bmc == NULL)
1455 goto unlock;
1456 /* locked */
NeilBrown32a76272005-06-21 17:17:14 -07001457 if (RESYNC(*bmc)) {
1458 *bmc &= ~RESYNC_MASK;
1459
1460 if (!NEEDED(*bmc) && aborted)
1461 *bmc |= NEEDED_MASK;
1462 else {
NeilBrown2585f3e2011-09-21 15:37:46 +10001463 if (*bmc <= 2) {
NeilBrown40cffcc2012-05-22 13:55:24 +10001464 bitmap_set_pending(&bitmap->counts, offset);
NeilBrown2585f3e2011-09-21 15:37:46 +10001465 bitmap->allclean = 0;
1466 }
NeilBrown32a76272005-06-21 17:17:14 -07001467 }
1468 }
1469 unlock:
NeilBrown40cffcc2012-05-22 13:55:24 +10001470 spin_unlock_irqrestore(&bitmap->counts.lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001471}
NeilBrownac2f40b2010-06-01 19:37:31 +10001472EXPORT_SYMBOL(bitmap_end_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001473
1474void bitmap_close_sync(struct bitmap *bitmap)
1475{
1476 /* Sync has finished, and any bitmap chunks that weren't synced
1477 * properly have been aborted. It remains to us to clear the
1478 * RESYNC bit wherever it is still on
1479 */
1480 sector_t sector = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001481 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001482 if (!bitmap)
1483 return;
NeilBrown32a76272005-06-21 17:17:14 -07001484 while (sector < bitmap->mddev->resync_max_sectors) {
1485 bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001486 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001487 }
1488}
NeilBrownac2f40b2010-06-01 19:37:31 +10001489EXPORT_SYMBOL(bitmap_close_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001490
NeilBrownb47490c2008-02-06 01:39:50 -08001491void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1492{
1493 sector_t s = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001494 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001495
1496 if (!bitmap)
1497 return;
1498 if (sector == 0) {
1499 bitmap->last_end_sync = jiffies;
1500 return;
1501 }
1502 if (time_before(jiffies, (bitmap->last_end_sync
NeilBrown1b04be92009-12-14 12:49:53 +11001503 + bitmap->mddev->bitmap_info.daemon_sleep)))
NeilBrownb47490c2008-02-06 01:39:50 -08001504 return;
1505 wait_event(bitmap->mddev->recovery_wait,
1506 atomic_read(&bitmap->mddev->recovery_active) == 0);
1507
NeilBrown75d3da42011-01-14 09:14:34 +11001508 bitmap->mddev->curr_resync_completed = sector;
NeilBrown070dc6d2010-08-30 17:33:34 +10001509 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
NeilBrown40cffcc2012-05-22 13:55:24 +10001510 sector &= ~((1ULL << bitmap->counts.chunkshift) - 1);
NeilBrownb47490c2008-02-06 01:39:50 -08001511 s = 0;
1512 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1513 bitmap_end_sync(bitmap, s, &blocks, 0);
1514 s += blocks;
1515 }
1516 bitmap->last_end_sync = jiffies;
NeilBrownacb180b2009-04-14 16:28:34 +10001517 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
NeilBrownb47490c2008-02-06 01:39:50 -08001518}
NeilBrownac2f40b2010-06-01 19:37:31 +10001519EXPORT_SYMBOL(bitmap_cond_end_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08001520
NeilBrown6a079972005-09-09 16:23:44 -07001521static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001522{
1523 /* For each chunk covered by any of these sectors, set the
NeilBrownef99bf42012-05-22 13:55:08 +10001524 * counter to 2 and possibly set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001525 * be 0 at this point
1526 */
NeilBrown193f1c92005-08-04 12:53:33 -07001527
NeilBrown57dab0b2010-10-19 10:03:39 +11001528 sector_t secs;
NeilBrown193f1c92005-08-04 12:53:33 -07001529 bitmap_counter_t *bmc;
NeilBrown40cffcc2012-05-22 13:55:24 +10001530 spin_lock_irq(&bitmap->counts.lock);
1531 bmc = bitmap_get_counter(&bitmap->counts, offset, &secs, 1);
NeilBrown193f1c92005-08-04 12:53:33 -07001532 if (!bmc) {
NeilBrown40cffcc2012-05-22 13:55:24 +10001533 spin_unlock_irq(&bitmap->counts.lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001534 return;
NeilBrown32a76272005-06-21 17:17:14 -07001535 }
NeilBrownac2f40b2010-06-01 19:37:31 +10001536 if (!*bmc) {
NeilBrown915c4202011-12-23 10:17:51 +11001537 *bmc = 2 | (needed ? NEEDED_MASK : 0);
NeilBrown40cffcc2012-05-22 13:55:24 +10001538 bitmap_count_page(&bitmap->counts, offset, 1);
1539 bitmap_set_pending(&bitmap->counts, offset);
NeilBrown2585f3e2011-09-21 15:37:46 +10001540 bitmap->allclean = 0;
NeilBrown193f1c92005-08-04 12:53:33 -07001541 }
NeilBrown40cffcc2012-05-22 13:55:24 +10001542 spin_unlock_irq(&bitmap->counts.lock);
NeilBrown32a76272005-06-21 17:17:14 -07001543}
1544
Paul Clements9b1d1da2006-10-03 01:15:49 -07001545/* dirty the memory and file bits for bitmap chunks "s" to "e" */
1546void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1547{
1548 unsigned long chunk;
1549
1550 for (chunk = s; chunk <= e; chunk++) {
NeilBrown40cffcc2012-05-22 13:55:24 +10001551 sector_t sec = (sector_t)chunk << bitmap->counts.chunkshift;
Paul Clements9b1d1da2006-10-03 01:15:49 -07001552 bitmap_set_memory_bits(bitmap, sec, 1);
1553 bitmap_file_set_bit(bitmap, sec);
NeilBrownffa23322009-12-14 12:49:56 +11001554 if (sec < bitmap->mddev->recovery_cp)
1555 /* We are asserting that the array is dirty,
1556 * so move the recovery_cp address back so
1557 * that it is obvious that it is dirty
1558 */
1559 bitmap->mddev->recovery_cp = sec;
Paul Clements9b1d1da2006-10-03 01:15:49 -07001560 }
1561}
1562
NeilBrown32a76272005-06-21 17:17:14 -07001563/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001564 * flush out any pending updates
1565 */
NeilBrownfd01b882011-10-11 16:47:53 +11001566void bitmap_flush(struct mddev *mddev)
NeilBrown6b8b3e82005-08-04 12:53:35 -07001567{
1568 struct bitmap *bitmap = mddev->bitmap;
NeilBrown42a04b52009-12-14 12:49:53 +11001569 long sleep;
NeilBrown6b8b3e82005-08-04 12:53:35 -07001570
1571 if (!bitmap) /* there was no bitmap */
1572 return;
1573
1574 /* run the daemon_work three time to ensure everything is flushed
1575 * that can be
1576 */
NeilBrown1b04be92009-12-14 12:49:53 +11001577 sleep = mddev->bitmap_info.daemon_sleep * 2;
NeilBrown42a04b52009-12-14 12:49:53 +11001578 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001579 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001580 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001581 bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001582 bitmap->daemon_lastrun -= sleep;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001583 bitmap_daemon_work(mddev);
NeilBrown6b8b3e82005-08-04 12:53:35 -07001584 bitmap_update_sb(bitmap);
1585}
1586
1587/*
NeilBrown32a76272005-06-21 17:17:14 -07001588 * free memory that was allocated
1589 */
NeilBrown3178b0d2005-09-09 16:23:50 -07001590static void bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001591{
1592 unsigned long k, pages;
1593 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001594
1595 if (!bitmap) /* there was no bitmap */
1596 return;
1597
NeilBrownfae7d322012-05-22 13:55:21 +10001598 /* Shouldn't be needed - but just in case.... */
1599 wait_event(bitmap->write_wait,
1600 atomic_read(&bitmap->pending_writes) == 0);
1601
1602 /* release the bitmap file */
1603 bitmap_file_unmap(&bitmap->storage);
NeilBrown32a76272005-06-21 17:17:14 -07001604
NeilBrown40cffcc2012-05-22 13:55:24 +10001605 bp = bitmap->counts.bp;
1606 pages = bitmap->counts.pages;
NeilBrown32a76272005-06-21 17:17:14 -07001607
1608 /* free all allocated memory */
1609
NeilBrown32a76272005-06-21 17:17:14 -07001610 if (bp) /* deallocate the page memory */
1611 for (k = 0; k < pages; k++)
1612 if (bp[k].map && !bp[k].hijacked)
1613 kfree(bp[k].map);
1614 kfree(bp);
1615 kfree(bitmap);
1616}
NeilBrownaa5cbd12009-12-14 12:49:46 +11001617
NeilBrownfd01b882011-10-11 16:47:53 +11001618void bitmap_destroy(struct mddev *mddev)
NeilBrown3178b0d2005-09-09 16:23:50 -07001619{
1620 struct bitmap *bitmap = mddev->bitmap;
1621
1622 if (!bitmap) /* there was no bitmap */
1623 return;
1624
NeilBrownc3d97142009-12-14 12:49:52 +11001625 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrown978a7a42014-12-15 12:56:58 +11001626 spin_lock(&mddev->lock);
NeilBrown3178b0d2005-09-09 16:23:50 -07001627 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrown978a7a42014-12-15 12:56:58 +11001628 spin_unlock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11001629 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrownb15c2e52006-01-06 00:20:16 -08001630 if (mddev->thread)
1631 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001632
NeilBrownece5cff2009-12-14 12:49:56 +11001633 if (bitmap->sysfs_can_clear)
1634 sysfs_put(bitmap->sysfs_can_clear);
1635
NeilBrown3178b0d2005-09-09 16:23:50 -07001636 bitmap_free(bitmap);
1637}
NeilBrown32a76272005-06-21 17:17:14 -07001638
1639/*
1640 * initialize the bitmap structure
1641 * if this returns an error, bitmap_destroy must be called to do clean up
1642 */
NeilBrownfd01b882011-10-11 16:47:53 +11001643int bitmap_create(struct mddev *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001644{
1645 struct bitmap *bitmap;
NeilBrown1f593902009-04-20 11:50:24 +10001646 sector_t blocks = mddev->resync_max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001647 struct file *file = mddev->bitmap_info.file;
NeilBrown32a76272005-06-21 17:17:14 -07001648 int err;
Tejun Heo324a56e2013-12-11 14:11:53 -05001649 struct kernfs_node *bm = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001650
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001651 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001652
NeilBrownc3d97142009-12-14 12:49:52 +11001653 BUG_ON(file && mddev->bitmap_info.offset);
NeilBrowna654b9d82005-06-21 17:17:27 -07001654
NeilBrown9ffae0c2006-01-06 00:20:32 -08001655 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001656 if (!bitmap)
1657 return -ENOMEM;
1658
NeilBrown40cffcc2012-05-22 13:55:24 +10001659 spin_lock_init(&bitmap->counts.lock);
NeilBrownce25c312006-06-26 00:27:49 -07001660 atomic_set(&bitmap->pending_writes, 0);
1661 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001662 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrowne5551902010-03-31 11:21:44 +11001663 init_waitqueue_head(&bitmap->behind_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001664
NeilBrown32a76272005-06-21 17:17:14 -07001665 bitmap->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07001666
NeilBrown5ff5aff2010-06-01 19:37:32 +10001667 if (mddev->kobj.sd)
Tejun Heo388975c2013-09-11 23:19:13 -04001668 bm = sysfs_get_dirent(mddev->kobj.sd, "bitmap");
NeilBrownece5cff2009-12-14 12:49:56 +11001669 if (bm) {
Tejun Heo388975c2013-09-11 23:19:13 -04001670 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, "can_clear");
NeilBrownece5cff2009-12-14 12:49:56 +11001671 sysfs_put(bm);
1672 } else
1673 bitmap->sysfs_can_clear = NULL;
1674
NeilBrown1ec885c2012-05-22 13:55:10 +10001675 bitmap->storage.file = file;
NeilBrownce25c312006-06-26 00:27:49 -07001676 if (file) {
1677 get_file(file);
NeilBrownae8fa282009-10-16 15:56:01 +11001678 /* As future accesses to this file will use bmap,
1679 * and bypass the page cache, we must sync the file
1680 * first.
1681 */
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001682 vfs_fsync(file, 1);
NeilBrownce25c312006-06-26 00:27:49 -07001683 }
NeilBrown42a04b52009-12-14 12:49:53 +11001684 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
Jonathan Brassow9c810752011-06-08 17:59:30 -05001685 if (!mddev->bitmap_info.external) {
1686 /*
1687 * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
1688 * instructing us to create a new on-disk bitmap instance.
1689 */
1690 if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
1691 err = bitmap_new_disk_sb(bitmap);
1692 else
1693 err = bitmap_read_sb(bitmap);
1694 } else {
NeilBrownece5cff2009-12-14 12:49:56 +11001695 err = 0;
1696 if (mddev->bitmap_info.chunksize == 0 ||
1697 mddev->bitmap_info.daemon_sleep == 0)
1698 /* chunksize and time_base need to be
1699 * set first. */
1700 err = -EINVAL;
1701 }
NeilBrown32a76272005-06-21 17:17:14 -07001702 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001703 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001704
NeilBrown624ce4f2009-12-14 12:49:56 +11001705 bitmap->daemon_lastrun = jiffies;
NeilBrownd60b4792012-05-22 13:55:25 +10001706 err = bitmap_resize(bitmap, blocks, mddev->bitmap_info.chunksize, 1);
1707 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001708 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001709
NeilBrown69e51b42010-06-01 19:37:35 +10001710 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
NeilBrownd60b4792012-05-22 13:55:25 +10001711 bitmap->counts.pages, bmname(bitmap));
NeilBrown69e51b42010-06-01 19:37:35 +10001712
1713 mddev->bitmap = bitmap;
NeilBrownb405fe92012-05-22 13:55:15 +10001714 return test_bit(BITMAP_WRITE_ERROR, &bitmap->flags) ? -EIO : 0;
NeilBrown69e51b42010-06-01 19:37:35 +10001715
1716 error:
1717 bitmap_free(bitmap);
1718 return err;
1719}
1720
NeilBrownfd01b882011-10-11 16:47:53 +11001721int bitmap_load(struct mddev *mddev)
NeilBrown69e51b42010-06-01 19:37:35 +10001722{
1723 int err = 0;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001724 sector_t start = 0;
NeilBrown69e51b42010-06-01 19:37:35 +10001725 sector_t sector = 0;
1726 struct bitmap *bitmap = mddev->bitmap;
1727
1728 if (!bitmap)
1729 goto out;
1730
1731 /* Clear out old bitmap info first: Either there is none, or we
1732 * are resuming after someone else has possibly changed things,
1733 * so we should forget old cached info.
1734 * All chunks should be clean, but some might need_sync.
1735 */
1736 while (sector < mddev->resync_max_sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001737 sector_t blocks;
NeilBrown69e51b42010-06-01 19:37:35 +10001738 bitmap_start_sync(bitmap, sector, &blocks, 0);
1739 sector += blocks;
1740 }
1741 bitmap_close_sync(bitmap);
1742
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001743 if (mddev->degraded == 0
1744 || bitmap->events_cleared == mddev->events)
1745 /* no need to keep dirty bits to optimise a
1746 * re-add of a missing device */
1747 start = mddev->recovery_cp;
NeilBrown69e51b42010-06-01 19:37:35 +10001748
NeilBrownafbaa902012-04-12 16:05:06 +10001749 mutex_lock(&mddev->bitmap_info.mutex);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001750 err = bitmap_init_from_disk(bitmap, start);
NeilBrownafbaa902012-04-12 16:05:06 +10001751 mutex_unlock(&mddev->bitmap_info.mutex);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001752
NeilBrown32a76272005-06-21 17:17:14 -07001753 if (err)
NeilBrown69e51b42010-06-01 19:37:35 +10001754 goto out;
NeilBrownb405fe92012-05-22 13:55:15 +10001755 clear_bit(BITMAP_STALE, &bitmap->flags);
NeilBrownef99bf42012-05-22 13:55:08 +10001756
1757 /* Kick recovery in case any bits were set */
1758 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
NeilBrown3178b0d2005-09-09 16:23:50 -07001759
NeilBrown1b04be92009-12-14 12:49:53 +11001760 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
NeilBrown9cd30fd2009-12-14 12:49:54 +11001761 md_wakeup_thread(mddev->thread);
NeilBrownb15c2e52006-01-06 00:20:16 -08001762
NeilBrown4ad13662007-07-17 04:06:13 -07001763 bitmap_update_sb(bitmap);
1764
NeilBrownb405fe92012-05-22 13:55:15 +10001765 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
NeilBrown69e51b42010-06-01 19:37:35 +10001766 err = -EIO;
1767out:
NeilBrown3178b0d2005-09-09 16:23:50 -07001768 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001769}
NeilBrown69e51b42010-06-01 19:37:35 +10001770EXPORT_SYMBOL_GPL(bitmap_load);
NeilBrown32a76272005-06-21 17:17:14 -07001771
NeilBrown57148962012-03-19 12:46:40 +11001772void bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
1773{
1774 unsigned long chunk_kb;
NeilBrown40cffcc2012-05-22 13:55:24 +10001775 struct bitmap_counts *counts;
NeilBrown57148962012-03-19 12:46:40 +11001776
1777 if (!bitmap)
1778 return;
1779
NeilBrown40cffcc2012-05-22 13:55:24 +10001780 counts = &bitmap->counts;
1781
NeilBrown57148962012-03-19 12:46:40 +11001782 chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
1783 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
1784 "%lu%s chunk",
NeilBrown40cffcc2012-05-22 13:55:24 +10001785 counts->pages - counts->missing_pages,
1786 counts->pages,
1787 (counts->pages - counts->missing_pages)
NeilBrown57148962012-03-19 12:46:40 +11001788 << (PAGE_SHIFT - 10),
1789 chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
1790 chunk_kb ? "KB" : "B");
NeilBrown1ec885c2012-05-22 13:55:10 +10001791 if (bitmap->storage.file) {
NeilBrown57148962012-03-19 12:46:40 +11001792 seq_printf(seq, ", file: ");
NeilBrown1ec885c2012-05-22 13:55:10 +10001793 seq_path(seq, &bitmap->storage.file->f_path, " \t\n");
NeilBrown57148962012-03-19 12:46:40 +11001794 }
1795
1796 seq_printf(seq, "\n");
NeilBrown57148962012-03-19 12:46:40 +11001797}
1798
NeilBrownd60b4792012-05-22 13:55:25 +10001799int bitmap_resize(struct bitmap *bitmap, sector_t blocks,
1800 int chunksize, int init)
1801{
1802 /* If chunk_size is 0, choose an appropriate chunk size.
1803 * Then possibly allocate new storage space.
1804 * Then quiesce, copy bits, replace bitmap, and re-start
1805 *
1806 * This function is called both to set up the initial bitmap
1807 * and to resize the bitmap while the array is active.
1808 * If this happens as a result of the array being resized,
1809 * chunksize will be zero, and we need to choose a suitable
1810 * chunksize, otherwise we use what we are given.
1811 */
1812 struct bitmap_storage store;
1813 struct bitmap_counts old_counts;
1814 unsigned long chunks;
1815 sector_t block;
1816 sector_t old_blocks, new_blocks;
1817 int chunkshift;
1818 int ret = 0;
1819 long pages;
1820 struct bitmap_page *new_bp;
1821
1822 if (chunksize == 0) {
1823 /* If there is enough space, leave the chunk size unchanged,
1824 * else increase by factor of two until there is enough space.
1825 */
1826 long bytes;
1827 long space = bitmap->mddev->bitmap_info.space;
1828
1829 if (space == 0) {
1830 /* We don't know how much space there is, so limit
1831 * to current size - in sectors.
1832 */
1833 bytes = DIV_ROUND_UP(bitmap->counts.chunks, 8);
1834 if (!bitmap->mddev->bitmap_info.external)
1835 bytes += sizeof(bitmap_super_t);
1836 space = DIV_ROUND_UP(bytes, 512);
1837 bitmap->mddev->bitmap_info.space = space;
1838 }
1839 chunkshift = bitmap->counts.chunkshift;
1840 chunkshift--;
1841 do {
1842 /* 'chunkshift' is shift from block size to chunk size */
1843 chunkshift++;
1844 chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
1845 bytes = DIV_ROUND_UP(chunks, 8);
1846 if (!bitmap->mddev->bitmap_info.external)
1847 bytes += sizeof(bitmap_super_t);
1848 } while (bytes > (space << 9));
1849 } else
1850 chunkshift = ffz(~chunksize) - BITMAP_BLOCK_SHIFT;
1851
1852 chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
1853 memset(&store, 0, sizeof(store));
1854 if (bitmap->mddev->bitmap_info.offset || bitmap->mddev->bitmap_info.file)
1855 ret = bitmap_storage_alloc(&store, chunks,
1856 !bitmap->mddev->bitmap_info.external);
1857 if (ret)
1858 goto err;
1859
1860 pages = DIV_ROUND_UP(chunks, PAGE_COUNTER_RATIO);
1861
1862 new_bp = kzalloc(pages * sizeof(*new_bp), GFP_KERNEL);
1863 ret = -ENOMEM;
1864 if (!new_bp) {
1865 bitmap_file_unmap(&store);
1866 goto err;
1867 }
1868
1869 if (!init)
1870 bitmap->mddev->pers->quiesce(bitmap->mddev, 1);
1871
1872 store.file = bitmap->storage.file;
1873 bitmap->storage.file = NULL;
1874
1875 if (store.sb_page && bitmap->storage.sb_page)
1876 memcpy(page_address(store.sb_page),
1877 page_address(bitmap->storage.sb_page),
1878 sizeof(bitmap_super_t));
1879 bitmap_file_unmap(&bitmap->storage);
1880 bitmap->storage = store;
1881
1882 old_counts = bitmap->counts;
1883 bitmap->counts.bp = new_bp;
1884 bitmap->counts.pages = pages;
1885 bitmap->counts.missing_pages = pages;
1886 bitmap->counts.chunkshift = chunkshift;
1887 bitmap->counts.chunks = chunks;
1888 bitmap->mddev->bitmap_info.chunksize = 1 << (chunkshift +
1889 BITMAP_BLOCK_SHIFT);
1890
1891 blocks = min(old_counts.chunks << old_counts.chunkshift,
1892 chunks << chunkshift);
1893
1894 spin_lock_irq(&bitmap->counts.lock);
1895 for (block = 0; block < blocks; ) {
1896 bitmap_counter_t *bmc_old, *bmc_new;
1897 int set;
1898
1899 bmc_old = bitmap_get_counter(&old_counts, block,
1900 &old_blocks, 0);
1901 set = bmc_old && NEEDED(*bmc_old);
1902
1903 if (set) {
1904 bmc_new = bitmap_get_counter(&bitmap->counts, block,
1905 &new_blocks, 1);
1906 if (*bmc_new == 0) {
1907 /* need to set on-disk bits too. */
1908 sector_t end = block + new_blocks;
1909 sector_t start = block >> chunkshift;
1910 start <<= chunkshift;
1911 while (start < end) {
1912 bitmap_file_set_bit(bitmap, block);
1913 start += 1 << chunkshift;
1914 }
1915 *bmc_new = 2;
1916 bitmap_count_page(&bitmap->counts,
1917 block, 1);
1918 bitmap_set_pending(&bitmap->counts,
1919 block);
1920 }
1921 *bmc_new |= NEEDED_MASK;
1922 if (new_blocks < old_blocks)
1923 old_blocks = new_blocks;
1924 }
1925 block += old_blocks;
1926 }
1927
1928 if (!init) {
1929 int i;
1930 while (block < (chunks << chunkshift)) {
1931 bitmap_counter_t *bmc;
1932 bmc = bitmap_get_counter(&bitmap->counts, block,
1933 &new_blocks, 1);
1934 if (bmc) {
1935 /* new space. It needs to be resynced, so
1936 * we set NEEDED_MASK.
1937 */
1938 if (*bmc == 0) {
1939 *bmc = NEEDED_MASK | 2;
1940 bitmap_count_page(&bitmap->counts,
1941 block, 1);
1942 bitmap_set_pending(&bitmap->counts,
1943 block);
1944 }
1945 }
1946 block += new_blocks;
1947 }
1948 for (i = 0; i < bitmap->storage.file_pages; i++)
1949 set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
1950 }
1951 spin_unlock_irq(&bitmap->counts.lock);
1952
1953 if (!init) {
1954 bitmap_unplug(bitmap);
1955 bitmap->mddev->pers->quiesce(bitmap->mddev, 0);
1956 }
1957 ret = 0;
1958err:
1959 return ret;
1960}
1961EXPORT_SYMBOL_GPL(bitmap_resize);
1962
NeilBrown43a70502009-12-14 12:49:55 +11001963static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001964location_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11001965{
1966 ssize_t len;
NeilBrownac2f40b2010-06-01 19:37:31 +10001967 if (mddev->bitmap_info.file)
NeilBrown43a70502009-12-14 12:49:55 +11001968 len = sprintf(page, "file");
NeilBrownac2f40b2010-06-01 19:37:31 +10001969 else if (mddev->bitmap_info.offset)
NeilBrown43a70502009-12-14 12:49:55 +11001970 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10001971 else
NeilBrown43a70502009-12-14 12:49:55 +11001972 len = sprintf(page, "none");
1973 len += sprintf(page+len, "\n");
1974 return len;
1975}
1976
1977static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11001978location_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11001979{
1980
1981 if (mddev->pers) {
1982 if (!mddev->pers->quiesce)
1983 return -EBUSY;
1984 if (mddev->recovery || mddev->sync_thread)
1985 return -EBUSY;
1986 }
1987
1988 if (mddev->bitmap || mddev->bitmap_info.file ||
1989 mddev->bitmap_info.offset) {
1990 /* bitmap already configured. Only option is to clear it */
1991 if (strncmp(buf, "none", 4) != 0)
1992 return -EBUSY;
1993 if (mddev->pers) {
1994 mddev->pers->quiesce(mddev, 1);
1995 bitmap_destroy(mddev);
1996 mddev->pers->quiesce(mddev, 0);
1997 }
1998 mddev->bitmap_info.offset = 0;
1999 if (mddev->bitmap_info.file) {
2000 struct file *f = mddev->bitmap_info.file;
2001 mddev->bitmap_info.file = NULL;
NeilBrown43a70502009-12-14 12:49:55 +11002002 fput(f);
2003 }
2004 } else {
2005 /* No bitmap, OK to set a location */
2006 long long offset;
2007 if (strncmp(buf, "none", 4) == 0)
2008 /* nothing to be done */;
2009 else if (strncmp(buf, "file:", 5) == 0) {
2010 /* Not supported yet */
2011 return -EINVAL;
2012 } else {
2013 int rv;
2014 if (buf[0] == '+')
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002015 rv = kstrtoll(buf+1, 10, &offset);
NeilBrown43a70502009-12-14 12:49:55 +11002016 else
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002017 rv = kstrtoll(buf, 10, &offset);
NeilBrown43a70502009-12-14 12:49:55 +11002018 if (rv)
2019 return rv;
2020 if (offset == 0)
2021 return -EINVAL;
NeilBrownece5cff2009-12-14 12:49:56 +11002022 if (mddev->bitmap_info.external == 0 &&
2023 mddev->major_version == 0 &&
NeilBrown43a70502009-12-14 12:49:55 +11002024 offset != mddev->bitmap_info.default_offset)
2025 return -EINVAL;
2026 mddev->bitmap_info.offset = offset;
2027 if (mddev->pers) {
2028 mddev->pers->quiesce(mddev, 1);
2029 rv = bitmap_create(mddev);
NeilBrown4474ca42012-03-19 12:46:37 +11002030 if (!rv)
2031 rv = bitmap_load(mddev);
NeilBrown43a70502009-12-14 12:49:55 +11002032 if (rv) {
2033 bitmap_destroy(mddev);
2034 mddev->bitmap_info.offset = 0;
2035 }
2036 mddev->pers->quiesce(mddev, 0);
2037 if (rv)
2038 return rv;
2039 }
2040 }
2041 }
2042 if (!mddev->external) {
2043 /* Ensure new bitmap info is stored in
2044 * metadata promptly.
2045 */
2046 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2047 md_wakeup_thread(mddev->thread);
2048 }
2049 return len;
2050}
2051
2052static struct md_sysfs_entry bitmap_location =
2053__ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
2054
NeilBrown6409bb02012-05-22 13:55:07 +10002055/* 'bitmap/space' is the space available at 'location' for the
2056 * bitmap. This allows the kernel to know when it is safe to
2057 * resize the bitmap to match a resized array.
2058 */
2059static ssize_t
2060space_show(struct mddev *mddev, char *page)
2061{
2062 return sprintf(page, "%lu\n", mddev->bitmap_info.space);
2063}
2064
2065static ssize_t
2066space_store(struct mddev *mddev, const char *buf, size_t len)
2067{
2068 unsigned long sectors;
2069 int rv;
2070
2071 rv = kstrtoul(buf, 10, &sectors);
2072 if (rv)
2073 return rv;
2074
2075 if (sectors == 0)
2076 return -EINVAL;
2077
2078 if (mddev->bitmap &&
NeilBrown9b1215c2012-05-22 13:55:11 +10002079 sectors < (mddev->bitmap->storage.bytes + 511) >> 9)
NeilBrown6409bb02012-05-22 13:55:07 +10002080 return -EFBIG; /* Bitmap is too big for this small space */
2081
2082 /* could make sure it isn't too big, but that isn't really
2083 * needed - user-space should be careful.
2084 */
2085 mddev->bitmap_info.space = sectors;
2086 return len;
2087}
2088
2089static struct md_sysfs_entry bitmap_space =
2090__ATTR(space, S_IRUGO|S_IWUSR, space_show, space_store);
2091
NeilBrown43a70502009-12-14 12:49:55 +11002092static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002093timeout_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002094{
2095 ssize_t len;
2096 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
2097 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
NeilBrownac2f40b2010-06-01 19:37:31 +10002098
NeilBrown43a70502009-12-14 12:49:55 +11002099 len = sprintf(page, "%lu", secs);
2100 if (jifs)
2101 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
2102 len += sprintf(page+len, "\n");
2103 return len;
2104}
2105
2106static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002107timeout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002108{
2109 /* timeout can be set at any time */
2110 unsigned long timeout;
2111 int rv = strict_strtoul_scaled(buf, &timeout, 4);
2112 if (rv)
2113 return rv;
2114
2115 /* just to make sure we don't overflow... */
2116 if (timeout >= LONG_MAX / HZ)
2117 return -EINVAL;
2118
2119 timeout = timeout * HZ / 10000;
2120
2121 if (timeout >= MAX_SCHEDULE_TIMEOUT)
2122 timeout = MAX_SCHEDULE_TIMEOUT-1;
2123 if (timeout < 1)
2124 timeout = 1;
2125 mddev->bitmap_info.daemon_sleep = timeout;
2126 if (mddev->thread) {
2127 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
2128 * the bitmap is all clean and we don't need to
2129 * adjust the timeout right now
2130 */
2131 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
2132 mddev->thread->timeout = timeout;
2133 md_wakeup_thread(mddev->thread);
2134 }
2135 }
2136 return len;
2137}
2138
2139static struct md_sysfs_entry bitmap_timeout =
2140__ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
2141
2142static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002143backlog_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002144{
2145 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
2146}
2147
2148static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002149backlog_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002150{
2151 unsigned long backlog;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002152 int rv = kstrtoul(buf, 10, &backlog);
NeilBrown43a70502009-12-14 12:49:55 +11002153 if (rv)
2154 return rv;
2155 if (backlog > COUNTER_MAX)
2156 return -EINVAL;
2157 mddev->bitmap_info.max_write_behind = backlog;
2158 return len;
2159}
2160
2161static struct md_sysfs_entry bitmap_backlog =
2162__ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
2163
2164static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002165chunksize_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002166{
2167 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
2168}
2169
2170static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002171chunksize_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002172{
2173 /* Can only be changed when no bitmap is active */
2174 int rv;
2175 unsigned long csize;
2176 if (mddev->bitmap)
2177 return -EBUSY;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002178 rv = kstrtoul(buf, 10, &csize);
NeilBrown43a70502009-12-14 12:49:55 +11002179 if (rv)
2180 return rv;
2181 if (csize < 512 ||
2182 !is_power_of_2(csize))
2183 return -EINVAL;
2184 mddev->bitmap_info.chunksize = csize;
2185 return len;
2186}
2187
2188static struct md_sysfs_entry bitmap_chunksize =
2189__ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
2190
NeilBrownfd01b882011-10-11 16:47:53 +11002191static ssize_t metadata_show(struct mddev *mddev, char *page)
NeilBrownece5cff2009-12-14 12:49:56 +11002192{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05002193 if (mddev_is_clustered(mddev))
2194 return sprintf(page, "clustered\n");
NeilBrownece5cff2009-12-14 12:49:56 +11002195 return sprintf(page, "%s\n", (mddev->bitmap_info.external
2196 ? "external" : "internal"));
2197}
2198
NeilBrownfd01b882011-10-11 16:47:53 +11002199static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownece5cff2009-12-14 12:49:56 +11002200{
2201 if (mddev->bitmap ||
2202 mddev->bitmap_info.file ||
2203 mddev->bitmap_info.offset)
2204 return -EBUSY;
2205 if (strncmp(buf, "external", 8) == 0)
2206 mddev->bitmap_info.external = 1;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05002207 else if ((strncmp(buf, "internal", 8) == 0) ||
2208 (strncmp(buf, "clustered", 9) == 0))
NeilBrownece5cff2009-12-14 12:49:56 +11002209 mddev->bitmap_info.external = 0;
2210 else
2211 return -EINVAL;
2212 return len;
2213}
2214
2215static struct md_sysfs_entry bitmap_metadata =
2216__ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2217
NeilBrownfd01b882011-10-11 16:47:53 +11002218static ssize_t can_clear_show(struct mddev *mddev, char *page)
NeilBrownece5cff2009-12-14 12:49:56 +11002219{
2220 int len;
NeilBrownb7b17c92014-12-15 12:56:59 +11002221 spin_lock(&mddev->lock);
NeilBrownece5cff2009-12-14 12:49:56 +11002222 if (mddev->bitmap)
2223 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
2224 "false" : "true"));
2225 else
2226 len = sprintf(page, "\n");
NeilBrownb7b17c92014-12-15 12:56:59 +11002227 spin_unlock(&mddev->lock);
NeilBrownece5cff2009-12-14 12:49:56 +11002228 return len;
2229}
2230
NeilBrownfd01b882011-10-11 16:47:53 +11002231static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownece5cff2009-12-14 12:49:56 +11002232{
2233 if (mddev->bitmap == NULL)
2234 return -ENOENT;
2235 if (strncmp(buf, "false", 5) == 0)
2236 mddev->bitmap->need_sync = 1;
2237 else if (strncmp(buf, "true", 4) == 0) {
2238 if (mddev->degraded)
2239 return -EBUSY;
2240 mddev->bitmap->need_sync = 0;
2241 } else
2242 return -EINVAL;
2243 return len;
2244}
2245
2246static struct md_sysfs_entry bitmap_can_clear =
2247__ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
2248
Paul Clements696fcd52010-03-08 16:02:37 +11002249static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002250behind_writes_used_show(struct mddev *mddev, char *page)
Paul Clements696fcd52010-03-08 16:02:37 +11002251{
NeilBrownb7b17c92014-12-15 12:56:59 +11002252 ssize_t ret;
2253 spin_lock(&mddev->lock);
Paul Clements696fcd52010-03-08 16:02:37 +11002254 if (mddev->bitmap == NULL)
NeilBrownb7b17c92014-12-15 12:56:59 +11002255 ret = sprintf(page, "0\n");
2256 else
2257 ret = sprintf(page, "%lu\n",
2258 mddev->bitmap->behind_writes_used);
2259 spin_unlock(&mddev->lock);
2260 return ret;
Paul Clements696fcd52010-03-08 16:02:37 +11002261}
2262
2263static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002264behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
Paul Clements696fcd52010-03-08 16:02:37 +11002265{
2266 if (mddev->bitmap)
2267 mddev->bitmap->behind_writes_used = 0;
2268 return len;
2269}
2270
2271static struct md_sysfs_entry max_backlog_used =
2272__ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2273 behind_writes_used_show, behind_writes_used_reset);
2274
NeilBrown43a70502009-12-14 12:49:55 +11002275static struct attribute *md_bitmap_attrs[] = {
2276 &bitmap_location.attr,
NeilBrown6409bb02012-05-22 13:55:07 +10002277 &bitmap_space.attr,
NeilBrown43a70502009-12-14 12:49:55 +11002278 &bitmap_timeout.attr,
2279 &bitmap_backlog.attr,
2280 &bitmap_chunksize.attr,
NeilBrownece5cff2009-12-14 12:49:56 +11002281 &bitmap_metadata.attr,
2282 &bitmap_can_clear.attr,
Paul Clements696fcd52010-03-08 16:02:37 +11002283 &max_backlog_used.attr,
NeilBrown43a70502009-12-14 12:49:55 +11002284 NULL
2285};
2286struct attribute_group md_bitmap_group = {
2287 .name = "bitmap",
2288 .attrs = md_bitmap_attrs,
2289};
2290