blob: b952bd45bd6a321861278842b203e3eb4e22dcc9 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
NeilBrown32a76272005-06-21 17:17:14 -07002/*
3 * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
4 *
5 * bitmap_create - sets up the bitmap structure
6 * bitmap_destroy - destroys the bitmap structure
7 *
8 * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.:
9 * - added disk storage for bitmap
10 * - changes to allow various bitmap chunk sizes
NeilBrown32a76272005-06-21 17:17:14 -070011 */
12
13/*
14 * Still to do:
15 *
16 * flush after percent set rather than just time based. (maybe both).
NeilBrown32a76272005-06-21 17:17:14 -070017 */
18
NeilBrownbff61972009-03-31 14:33:13 +110019#include <linux/blkdev.h>
NeilBrown32a76272005-06-21 17:17:14 -070020#include <linux/module.h>
NeilBrown32a76272005-06-21 17:17:14 -070021#include <linux/errno.h>
22#include <linux/slab.h>
23#include <linux/init.h>
NeilBrown32a76272005-06-21 17:17:14 -070024#include <linux/timer.h>
25#include <linux/sched.h>
26#include <linux/list.h>
27#include <linux/file.h>
28#include <linux/mount.h>
29#include <linux/buffer_head.h>
NeilBrown57148962012-03-19 12:46:40 +110030#include <linux/seq_file.h>
NeilBrown581dbd92016-11-14 16:30:21 +110031#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110032#include "md.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040033#include "md-bitmap.h"
NeilBrown32a76272005-06-21 17:17:14 -070034
NeilBrownac2f40b2010-06-01 19:37:31 +100035static inline char *bmname(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -070036{
37 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
38}
39
NeilBrown32a76272005-06-21 17:17:14 -070040/*
NeilBrown32a76272005-06-21 17:17:14 -070041 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
42 *
43 * 1) check to see if this page is allocated, if it's not then try to alloc
44 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
45 * page pointer directly as a counter
46 *
47 * if we find our page, we increment the page's refcount so that it stays
48 * allocated while we're using it
49 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -070050static int md_bitmap_checkpage(struct bitmap_counts *bitmap,
51 unsigned long page, int create, int no_hijack)
NeilBrownee305ac2009-09-23 18:06:44 +100052__releases(bitmap->lock)
53__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -070054{
55 unsigned char *mappage;
56
57 if (page >= bitmap->pages) {
NeilBrown1187cf02009-03-31 14:27:02 +110058 /* This can happen if bitmap_start_sync goes beyond
59 * End-of-device while looking for a whole page.
60 * It is harmless.
61 */
NeilBrown32a76272005-06-21 17:17:14 -070062 return -EINVAL;
63 }
64
NeilBrown32a76272005-06-21 17:17:14 -070065 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
66 return 0;
67
68 if (bitmap->bp[page].map) /* page is already allocated, just return */
69 return 0;
70
71 if (!create)
72 return -ENOENT;
73
NeilBrown32a76272005-06-21 17:17:14 -070074 /* this page has not been allocated yet */
75
NeilBrownac2f40b2010-06-01 19:37:31 +100076 spin_unlock_irq(&bitmap->lock);
NeilBrownd9590142015-02-02 17:08:03 +110077 /* It is possible that this is being called inside a
78 * prepare_to_wait/finish_wait loop from raid5c:make_request().
79 * In general it is not permitted to sleep in that context as it
80 * can cause the loop to spin freely.
81 * That doesn't apply here as we can only reach this point
82 * once with any loop.
83 * When this function completes, either bp[page].map or
84 * bp[page].hijacked. In either case, this function will
85 * abort before getting to this point again. So there is
86 * no risk of a free-spin, and so it is safe to assert
87 * that sleeping here is allowed.
88 */
89 sched_annotate_sleep();
NeilBrown792a1d42012-03-19 12:46:41 +110090 mappage = kzalloc(PAGE_SIZE, GFP_NOIO);
NeilBrownac2f40b2010-06-01 19:37:31 +100091 spin_lock_irq(&bitmap->lock);
92
93 if (mappage == NULL) {
NeilBrown40cffcc2012-05-22 13:55:24 +100094 pr_debug("md/bitmap: map page allocation failed, hijacking\n");
Guoqing Jiangc9d65032016-05-02 11:50:11 -040095 /* We don't support hijack for cluster raid */
96 if (no_hijack)
97 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -070098 /* failed - set the hijacked flag so that we can use the
99 * pointer as a counter */
NeilBrown32a76272005-06-21 17:17:14 -0700100 if (!bitmap->bp[page].map)
101 bitmap->bp[page].hijacked = 1;
NeilBrownac2f40b2010-06-01 19:37:31 +1000102 } else if (bitmap->bp[page].map ||
103 bitmap->bp[page].hijacked) {
NeilBrown32a76272005-06-21 17:17:14 -0700104 /* somebody beat us to getting the page */
NeilBrown792a1d42012-03-19 12:46:41 +1100105 kfree(mappage);
NeilBrownac2f40b2010-06-01 19:37:31 +1000106 } else {
107
108 /* no page was in place and we have one, so install it */
109
110 bitmap->bp[page].map = mappage;
111 bitmap->missing_pages--;
NeilBrown32a76272005-06-21 17:17:14 -0700112 }
NeilBrown32a76272005-06-21 17:17:14 -0700113 return 0;
114}
115
NeilBrown32a76272005-06-21 17:17:14 -0700116/* if page is completely empty, put it back on the free list, or dealloc it */
117/* if page was hijacked, unmark the flag so it might get alloced next time */
118/* Note: lock should be held when calling this */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700119static void md_bitmap_checkfree(struct bitmap_counts *bitmap, unsigned long page)
NeilBrown32a76272005-06-21 17:17:14 -0700120{
121 char *ptr;
122
123 if (bitmap->bp[page].count) /* page is still busy */
124 return;
125
126 /* page is no longer in use, it can be released */
127
128 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
129 bitmap->bp[page].hijacked = 0;
130 bitmap->bp[page].map = NULL;
NeilBrownac2f40b2010-06-01 19:37:31 +1000131 } else {
132 /* normal case, free the page */
133 ptr = bitmap->bp[page].map;
134 bitmap->bp[page].map = NULL;
135 bitmap->missing_pages++;
NeilBrown792a1d42012-03-19 12:46:41 +1100136 kfree(ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700137 }
NeilBrown32a76272005-06-21 17:17:14 -0700138}
139
NeilBrown32a76272005-06-21 17:17:14 -0700140/*
141 * bitmap file handling - read and write the bitmap file and its superblock
142 */
143
NeilBrown32a76272005-06-21 17:17:14 -0700144/*
145 * basic page I/O operations
146 */
147
NeilBrowna654b9d82005-06-21 17:17:27 -0700148/* IO operations when bitmap is stored near all superblocks */
NeilBrown27581e52012-05-22 13:55:08 +1000149static int read_sb_page(struct mddev *mddev, loff_t offset,
150 struct page *page,
151 unsigned long index, int size)
NeilBrowna654b9d82005-06-21 17:17:27 -0700152{
153 /* choose a good rdev and read the page from there */
154
NeilBrown3cb03002011-10-11 16:45:26 +1100155 struct md_rdev *rdev;
NeilBrowna654b9d82005-06-21 17:17:27 -0700156 sector_t target;
NeilBrowna654b9d82005-06-21 17:17:27 -0700157
NeilBrowndafb20f2012-03-19 12:46:39 +1100158 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800159 if (! test_bit(In_sync, &rdev->flags)
Guoqing Jiang4aaf76942017-07-04 11:20:30 +0800160 || test_bit(Faulty, &rdev->flags)
161 || test_bit(Bitmap_sync, &rdev->flags))
NeilBrownab904d62005-09-09 16:23:52 -0700162 continue;
163
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100164 target = offset + index * (PAGE_SIZE/512);
NeilBrowna654b9d82005-06-21 17:17:27 -0700165
NeilBrown2b193362010-10-27 15:16:40 +1100166 if (sync_page_io(rdev, target,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400167 roundup(size, bdev_logical_block_size(rdev->bdev)),
Mike Christie796a5cf2016-06-05 14:32:07 -0500168 page, REQ_OP_READ, 0, true)) {
NeilBrownab904d62005-09-09 16:23:52 -0700169 page->index = index;
NeilBrown27581e52012-05-22 13:55:08 +1000170 return 0;
NeilBrownab904d62005-09-09 16:23:52 -0700171 }
172 }
NeilBrown27581e52012-05-22 13:55:08 +1000173 return -EIO;
NeilBrowna654b9d82005-06-21 17:17:27 -0700174}
175
NeilBrownfd01b882011-10-11 16:47:53 +1100176static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev)
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000177{
178 /* Iterate the disks of an mddev, using rcu to protect access to the
179 * linked list, and raising the refcount of devices we return to ensure
180 * they don't disappear while in use.
181 * As devices are only added or removed when raid_disk is < 0 and
182 * nr_pending is 0 and In_sync is clear, the entries we return will
183 * still be in the same position on the list when we re-enter
Michael Wangfd177482012-10-11 13:43:21 +1100184 * list_for_each_entry_continue_rcu.
NeilBrown8532e342015-05-20 15:05:09 +1000185 *
186 * Note that if entered with 'rdev == NULL' to start at the
187 * beginning, we temporarily assign 'rdev' to an address which
188 * isn't really an rdev, but which can be used by
189 * list_for_each_entry_continue_rcu() to find the first entry.
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000190 */
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000191 rcu_read_lock();
192 if (rdev == NULL)
193 /* start at the beginning */
NeilBrown8532e342015-05-20 15:05:09 +1000194 rdev = list_entry(&mddev->disks, struct md_rdev, same_set);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000195 else {
196 /* release the previous rdev and start from there. */
197 rdev_dec_pending(rdev, mddev);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000198 }
Michael Wangfd177482012-10-11 13:43:21 +1100199 list_for_each_entry_continue_rcu(rdev, &mddev->disks, same_set) {
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000200 if (rdev->raid_disk >= 0 &&
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000201 !test_bit(Faulty, &rdev->flags)) {
202 /* this is a usable devices */
203 atomic_inc(&rdev->nr_pending);
204 rcu_read_unlock();
205 return rdev;
206 }
207 }
208 rcu_read_unlock();
209 return NULL;
210}
211
NeilBrownab6085c2007-05-23 13:58:10 -0700212static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrowna654b9d82005-06-21 17:17:27 -0700213{
NeilBrown46533ff2016-11-18 16:16:11 +1100214 struct md_rdev *rdev;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100215 struct block_device *bdev;
NeilBrownfd01b882011-10-11 16:47:53 +1100216 struct mddev *mddev = bitmap->mddev;
NeilBrown1ec885c2012-05-22 13:55:10 +1000217 struct bitmap_storage *store = &bitmap->storage;
NeilBrowna654b9d82005-06-21 17:17:27 -0700218
NeilBrown46533ff2016-11-18 16:16:11 +1100219restart:
220 rdev = NULL;
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000221 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
NeilBrownac2f40b2010-06-01 19:37:31 +1000222 int size = PAGE_SIZE;
223 loff_t offset = mddev->bitmap_info.offset;
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100224
225 bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
226
NeilBrown9b1215c2012-05-22 13:55:11 +1000227 if (page->index == store->file_pages-1) {
228 int last_page_size = store->bytes & (PAGE_SIZE-1);
229 if (last_page_size == 0)
230 last_page_size = PAGE_SIZE;
231 size = roundup(last_page_size,
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100232 bdev_logical_block_size(bdev));
NeilBrown9b1215c2012-05-22 13:55:11 +1000233 }
NeilBrownac2f40b2010-06-01 19:37:31 +1000234 /* Just make sure we aren't corrupting data or
235 * metadata
236 */
237 if (mddev->external) {
238 /* Bitmap could be anywhere. */
239 if (rdev->sb_start + offset + (page->index
240 * (PAGE_SIZE/512))
241 > rdev->data_offset
242 &&
243 rdev->sb_start + offset
244 < (rdev->data_offset + mddev->dev_sectors
245 + (PAGE_SIZE/512)))
246 goto bad_alignment;
247 } else if (offset < 0) {
248 /* DATA BITMAP METADATA */
249 if (offset
250 + (long)(page->index * (PAGE_SIZE/512))
251 + size/512 > 0)
252 /* bitmap runs in to metadata */
253 goto bad_alignment;
254 if (rdev->data_offset + mddev->dev_sectors
255 > rdev->sb_start + offset)
256 /* data runs in to bitmap */
257 goto bad_alignment;
258 } else if (rdev->sb_start < rdev->data_offset) {
259 /* METADATA BITMAP DATA */
260 if (rdev->sb_start
261 + offset
262 + page->index*(PAGE_SIZE/512) + size/512
263 > rdev->data_offset)
264 /* bitmap runs in to data */
265 goto bad_alignment;
266 } else {
267 /* DATA METADATA BITMAP - no problems */
268 }
269 md_super_write(mddev, rdev,
270 rdev->sb_start + offset
271 + page->index * (PAGE_SIZE/512),
272 size,
273 page);
NeilBrownb2d2c4c2008-09-01 12:48:13 +1000274 }
NeilBrowna654b9d82005-06-21 17:17:27 -0700275
NeilBrown46533ff2016-11-18 16:16:11 +1100276 if (wait && md_super_wait(mddev) < 0)
277 goto restart;
NeilBrowna654b9d82005-06-21 17:17:27 -0700278 return 0;
NeilBrown4b809912008-07-21 17:05:25 +1000279
280 bad_alignment:
NeilBrown4b809912008-07-21 17:05:25 +1000281 return -EINVAL;
NeilBrowna654b9d82005-06-21 17:17:27 -0700282}
283
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700284static void md_bitmap_file_kick(struct bitmap *bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700285/*
NeilBrowna654b9d82005-06-21 17:17:27 -0700286 * write out a page to a file
NeilBrown32a76272005-06-21 17:17:14 -0700287 */
NeilBrown4ad13662007-07-17 04:06:13 -0700288static void write_page(struct bitmap *bitmap, struct page *page, int wait)
NeilBrown32a76272005-06-21 17:17:14 -0700289{
NeilBrownd785a062006-06-26 00:27:48 -0700290 struct buffer_head *bh;
NeilBrown32a76272005-06-21 17:17:14 -0700291
NeilBrown1ec885c2012-05-22 13:55:10 +1000292 if (bitmap->storage.file == NULL) {
NeilBrownf0d76d72007-07-17 04:06:12 -0700293 switch (write_sb_page(bitmap, page, wait)) {
294 case -EINVAL:
NeilBrownb405fe92012-05-22 13:55:15 +1000295 set_bit(BITMAP_WRITE_ERROR, &bitmap->flags);
NeilBrownf0d76d72007-07-17 04:06:12 -0700296 }
NeilBrown4ad13662007-07-17 04:06:13 -0700297 } else {
NeilBrowna654b9d82005-06-21 17:17:27 -0700298
NeilBrown4ad13662007-07-17 04:06:13 -0700299 bh = page_buffers(page);
NeilBrownc7084432006-01-06 00:20:45 -0800300
NeilBrown4ad13662007-07-17 04:06:13 -0700301 while (bh && bh->b_blocknr) {
302 atomic_inc(&bitmap->pending_writes);
303 set_buffer_locked(bh);
304 set_buffer_mapped(bh);
Mike Christie2a222ca2016-06-05 14:31:43 -0500305 submit_bh(REQ_OP_WRITE, REQ_SYNC, bh);
NeilBrown4ad13662007-07-17 04:06:13 -0700306 bh = bh->b_this_page;
307 }
NeilBrown32a76272005-06-21 17:17:14 -0700308
NeilBrownac2f40b2010-06-01 19:37:31 +1000309 if (wait)
NeilBrown4ad13662007-07-17 04:06:13 -0700310 wait_event(bitmap->write_wait,
311 atomic_read(&bitmap->pending_writes)==0);
NeilBrown32a76272005-06-21 17:17:14 -0700312 }
NeilBrownb405fe92012-05-22 13:55:15 +1000313 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700314 md_bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700315}
316
NeilBrownd785a062006-06-26 00:27:48 -0700317static void end_bitmap_write(struct buffer_head *bh, int uptodate)
NeilBrown32a76272005-06-21 17:17:14 -0700318{
NeilBrownd785a062006-06-26 00:27:48 -0700319 struct bitmap *bitmap = bh->b_private;
NeilBrownd785a062006-06-26 00:27:48 -0700320
NeilBrownb405fe92012-05-22 13:55:15 +1000321 if (!uptodate)
322 set_bit(BITMAP_WRITE_ERROR, &bitmap->flags);
NeilBrownd785a062006-06-26 00:27:48 -0700323 if (atomic_dec_and_test(&bitmap->pending_writes))
324 wake_up(&bitmap->write_wait);
325}
326
327/* copied from buffer.c */
328static void
329__clear_page_buffers(struct page *page)
330{
331 ClearPagePrivate(page);
332 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300333 put_page(page);
NeilBrownd785a062006-06-26 00:27:48 -0700334}
335static void free_buffers(struct page *page)
336{
NeilBrown27581e52012-05-22 13:55:08 +1000337 struct buffer_head *bh;
NeilBrownd785a062006-06-26 00:27:48 -0700338
NeilBrown27581e52012-05-22 13:55:08 +1000339 if (!PagePrivate(page))
340 return;
341
342 bh = page_buffers(page);
NeilBrownd785a062006-06-26 00:27:48 -0700343 while (bh) {
344 struct buffer_head *next = bh->b_this_page;
345 free_buffer_head(bh);
346 bh = next;
347 }
348 __clear_page_buffers(page);
349 put_page(page);
350}
351
352/* read a page from a file.
353 * We both read the page, and attach buffers to the page to record the
354 * address of each block (using bmap). These addresses will be used
355 * to write the block later, completely bypassing the filesystem.
356 * This usage is similar to how swap files are handled, and allows us
357 * to write to a file with no concerns of memory allocation failing.
358 */
NeilBrown27581e52012-05-22 13:55:08 +1000359static int read_page(struct file *file, unsigned long index,
360 struct bitmap *bitmap,
361 unsigned long count,
362 struct page *page)
NeilBrownd785a062006-06-26 00:27:48 -0700363{
NeilBrown27581e52012-05-22 13:55:08 +1000364 int ret = 0;
Al Viro496ad9a2013-01-23 17:07:38 -0500365 struct inode *inode = file_inode(file);
NeilBrownd785a062006-06-26 00:27:48 -0700366 struct buffer_head *bh;
Carlos Maiolino30460e12020-01-09 14:30:41 +0100367 sector_t block, blk_cur;
NeilBrown32a76272005-06-21 17:17:14 -0700368
NeilBrown36a4e1f2011-10-07 14:23:17 +1100369 pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
370 (unsigned long long)index << PAGE_SHIFT);
NeilBrown32a76272005-06-21 17:17:14 -0700371
Jens Axboe640ab982017-09-27 05:40:16 -0600372 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, false);
NeilBrownd785a062006-06-26 00:27:48 -0700373 if (!bh) {
NeilBrown27581e52012-05-22 13:55:08 +1000374 ret = -ENOMEM;
NeilBrownd785a062006-06-26 00:27:48 -0700375 goto out;
376 }
377 attach_page_buffers(page, bh);
Carlos Maiolino30460e12020-01-09 14:30:41 +0100378 blk_cur = index << (PAGE_SHIFT - inode->i_blkbits);
NeilBrownd785a062006-06-26 00:27:48 -0700379 while (bh) {
Carlos Maiolino30460e12020-01-09 14:30:41 +0100380 block = blk_cur;
381
NeilBrownd785a062006-06-26 00:27:48 -0700382 if (count == 0)
383 bh->b_blocknr = 0;
384 else {
Carlos Maiolino30460e12020-01-09 14:30:41 +0100385 ret = bmap(inode, &block);
386 if (ret || !block) {
NeilBrown27581e52012-05-22 13:55:08 +1000387 ret = -EINVAL;
Carlos Maiolino30460e12020-01-09 14:30:41 +0100388 bh->b_blocknr = 0;
NeilBrownd785a062006-06-26 00:27:48 -0700389 goto out;
390 }
Carlos Maiolino30460e12020-01-09 14:30:41 +0100391
392 bh->b_blocknr = block;
NeilBrownd785a062006-06-26 00:27:48 -0700393 bh->b_bdev = inode->i_sb->s_bdev;
394 if (count < (1<<inode->i_blkbits))
395 count = 0;
396 else
397 count -= (1<<inode->i_blkbits);
NeilBrown32a76272005-06-21 17:17:14 -0700398
NeilBrownd785a062006-06-26 00:27:48 -0700399 bh->b_end_io = end_bitmap_write;
400 bh->b_private = bitmap;
NeilBrownce25c312006-06-26 00:27:49 -0700401 atomic_inc(&bitmap->pending_writes);
402 set_buffer_locked(bh);
403 set_buffer_mapped(bh);
Mike Christie2a222ca2016-06-05 14:31:43 -0500404 submit_bh(REQ_OP_READ, 0, bh);
NeilBrownd785a062006-06-26 00:27:48 -0700405 }
Carlos Maiolino30460e12020-01-09 14:30:41 +0100406 blk_cur++;
NeilBrownd785a062006-06-26 00:27:48 -0700407 bh = bh->b_this_page;
408 }
NeilBrownd785a062006-06-26 00:27:48 -0700409 page->index = index;
NeilBrownce25c312006-06-26 00:27:49 -0700410
411 wait_event(bitmap->write_wait,
412 atomic_read(&bitmap->pending_writes)==0);
NeilBrownb405fe92012-05-22 13:55:15 +1000413 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
NeilBrown27581e52012-05-22 13:55:08 +1000414 ret = -EIO;
NeilBrown32a76272005-06-21 17:17:14 -0700415out:
NeilBrown27581e52012-05-22 13:55:08 +1000416 if (ret)
NeilBrownec0cc222016-11-02 14:16:49 +1100417 pr_err("md: bitmap read error: (%dB @ %llu): %d\n",
418 (int)PAGE_SIZE,
419 (unsigned long long)index << PAGE_SHIFT,
420 ret);
NeilBrown27581e52012-05-22 13:55:08 +1000421 return ret;
NeilBrown32a76272005-06-21 17:17:14 -0700422}
423
424/*
425 * bitmap file superblock operations
426 */
427
NeilBrown85c9ccd2016-11-04 16:46:03 +1100428/*
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700429 * md_bitmap_wait_writes() should be called before writing any bitmap
NeilBrown85c9ccd2016-11-04 16:46:03 +1100430 * blocks, to ensure previous writes, particularly from
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700431 * md_bitmap_daemon_work(), have completed.
NeilBrown85c9ccd2016-11-04 16:46:03 +1100432 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700433static void md_bitmap_wait_writes(struct bitmap *bitmap)
NeilBrown85c9ccd2016-11-04 16:46:03 +1100434{
435 if (bitmap->storage.file)
436 wait_event(bitmap->write_wait,
437 atomic_read(&bitmap->pending_writes)==0);
438 else
NeilBrown46533ff2016-11-18 16:16:11 +1100439 /* Note that we ignore the return value. The writes
440 * might have failed, but that would just mean that
441 * some bits which should be cleared haven't been,
442 * which is safe. The relevant bitmap blocks will
443 * probably get written again, but there is no great
444 * loss if they aren't.
445 */
NeilBrown85c9ccd2016-11-04 16:46:03 +1100446 md_super_wait(bitmap->mddev);
447}
448
449
NeilBrown32a76272005-06-21 17:17:14 -0700450/* update the event counter and sync the superblock to disk */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700451void md_bitmap_update_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700452{
453 bitmap_super_t *sb;
NeilBrown32a76272005-06-21 17:17:14 -0700454
455 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
NeilBrown4ad13662007-07-17 04:06:13 -0700456 return;
NeilBrownece5cff2009-12-14 12:49:56 +1100457 if (bitmap->mddev->bitmap_info.external)
458 return;
NeilBrown1ec885c2012-05-22 13:55:10 +1000459 if (!bitmap->storage.sb_page) /* no superblock */
NeilBrown4ad13662007-07-17 04:06:13 -0700460 return;
NeilBrown1ec885c2012-05-22 13:55:10 +1000461 sb = kmap_atomic(bitmap->storage.sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700462 sb->events = cpu_to_le64(bitmap->mddev->events);
NeilBrown8258c532011-05-11 14:26:30 +1000463 if (bitmap->mddev->events < bitmap->events_cleared)
Neil Browna0da84f2008-06-28 08:31:22 +1000464 /* rocking back to read-only */
465 bitmap->events_cleared = bitmap->mddev->events;
NeilBrown8258c532011-05-11 14:26:30 +1000466 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
Hou Tao97f0eb92017-11-06 10:11:25 +0800467 /*
468 * clear BITMAP_WRITE_ERROR bit to protect against the case that
469 * a bitmap write error occurred but the later writes succeeded.
470 */
471 sb->state = cpu_to_le32(bitmap->flags & ~BIT(BITMAP_WRITE_ERROR));
NeilBrown43a70502009-12-14 12:49:55 +1100472 /* Just in case these have been changed via sysfs: */
473 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
474 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
NeilBrownb81a0402012-05-22 13:55:26 +1000475 /* This might have been changed by a reshape */
476 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
477 sb->chunksize = cpu_to_le32(bitmap->mddev->bitmap_info.chunksize);
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500478 sb->nodes = cpu_to_le32(bitmap->mddev->bitmap_info.nodes);
NeilBrown1dff2b82012-05-22 13:55:34 +1000479 sb->sectors_reserved = cpu_to_le32(bitmap->mddev->
480 bitmap_info.space);
Cong Wangb2f46e62011-11-28 13:25:44 +0800481 kunmap_atomic(sb);
NeilBrown1ec885c2012-05-22 13:55:10 +1000482 write_page(bitmap, bitmap->storage.sb_page, 1);
NeilBrown32a76272005-06-21 17:17:14 -0700483}
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700484EXPORT_SYMBOL(md_bitmap_update_sb);
NeilBrown32a76272005-06-21 17:17:14 -0700485
486/* print out the bitmap file superblock */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700487void md_bitmap_print_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700488{
489 bitmap_super_t *sb;
490
NeilBrown1ec885c2012-05-22 13:55:10 +1000491 if (!bitmap || !bitmap->storage.sb_page)
NeilBrown32a76272005-06-21 17:17:14 -0700492 return;
NeilBrown1ec885c2012-05-22 13:55:10 +1000493 sb = kmap_atomic(bitmap->storage.sb_page);
NeilBrownec0cc222016-11-02 14:16:49 +1100494 pr_debug("%s: bitmap file superblock:\n", bmname(bitmap));
495 pr_debug(" magic: %08x\n", le32_to_cpu(sb->magic));
496 pr_debug(" version: %d\n", le32_to_cpu(sb->version));
497 pr_debug(" uuid: %08x.%08x.%08x.%08x\n",
Christoph Hellwigc35403f2019-04-04 18:56:11 +0200498 le32_to_cpu(*(__le32 *)(sb->uuid+0)),
499 le32_to_cpu(*(__le32 *)(sb->uuid+4)),
500 le32_to_cpu(*(__le32 *)(sb->uuid+8)),
501 le32_to_cpu(*(__le32 *)(sb->uuid+12)));
NeilBrownec0cc222016-11-02 14:16:49 +1100502 pr_debug(" events: %llu\n",
503 (unsigned long long) le64_to_cpu(sb->events));
504 pr_debug("events cleared: %llu\n",
505 (unsigned long long) le64_to_cpu(sb->events_cleared));
506 pr_debug(" state: %08x\n", le32_to_cpu(sb->state));
507 pr_debug(" chunksize: %d B\n", le32_to_cpu(sb->chunksize));
508 pr_debug(" daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
509 pr_debug(" sync size: %llu KB\n",
510 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
511 pr_debug("max write behind: %d\n", le32_to_cpu(sb->write_behind));
Cong Wangb2f46e62011-11-28 13:25:44 +0800512 kunmap_atomic(sb);
NeilBrown32a76272005-06-21 17:17:14 -0700513}
514
Jonathan Brassow9c810752011-06-08 17:59:30 -0500515/*
516 * bitmap_new_disk_sb
517 * @bitmap
518 *
519 * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
520 * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
521 * This function verifies 'bitmap_info' and populates the on-disk bitmap
522 * structure, which is to be written to disk.
523 *
524 * Returns: 0 on success, -Exxx on error
525 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700526static int md_bitmap_new_disk_sb(struct bitmap *bitmap)
Jonathan Brassow9c810752011-06-08 17:59:30 -0500527{
528 bitmap_super_t *sb;
529 unsigned long chunksize, daemon_sleep, write_behind;
Jonathan Brassow9c810752011-06-08 17:59:30 -0500530
Goldwyn Rodriguesd3b178a2015-07-22 12:09:17 -0500531 bitmap->storage.sb_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
Jianpeng Ma582e2e02012-10-11 13:45:36 +1100532 if (bitmap->storage.sb_page == NULL)
533 return -ENOMEM;
NeilBrown1ec885c2012-05-22 13:55:10 +1000534 bitmap->storage.sb_page->index = 0;
Jonathan Brassow9c810752011-06-08 17:59:30 -0500535
NeilBrown1ec885c2012-05-22 13:55:10 +1000536 sb = kmap_atomic(bitmap->storage.sb_page);
Jonathan Brassow9c810752011-06-08 17:59:30 -0500537
538 sb->magic = cpu_to_le32(BITMAP_MAGIC);
539 sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
540
541 chunksize = bitmap->mddev->bitmap_info.chunksize;
542 BUG_ON(!chunksize);
543 if (!is_power_of_2(chunksize)) {
Cong Wangb2f46e62011-11-28 13:25:44 +0800544 kunmap_atomic(sb);
NeilBrownec0cc222016-11-02 14:16:49 +1100545 pr_warn("bitmap chunksize not a power of 2\n");
Jonathan Brassow9c810752011-06-08 17:59:30 -0500546 return -EINVAL;
547 }
548 sb->chunksize = cpu_to_le32(chunksize);
549
550 daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
Eric Engestromc97e0602016-03-07 12:01:05 +0000551 if (!daemon_sleep || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
NeilBrownec0cc222016-11-02 14:16:49 +1100552 pr_debug("Choosing daemon_sleep default (5 sec)\n");
Jonathan Brassow9c810752011-06-08 17:59:30 -0500553 daemon_sleep = 5 * HZ;
554 }
555 sb->daemon_sleep = cpu_to_le32(daemon_sleep);
556 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
557
558 /*
559 * FIXME: write_behind for RAID1. If not specified, what
560 * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
561 */
562 write_behind = bitmap->mddev->bitmap_info.max_write_behind;
563 if (write_behind > COUNTER_MAX)
564 write_behind = COUNTER_MAX / 2;
565 sb->write_behind = cpu_to_le32(write_behind);
566 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
567
568 /* keep the array size field of the bitmap superblock up to date */
569 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
570
571 memcpy(sb->uuid, bitmap->mddev->uuid, 16);
572
NeilBrownb405fe92012-05-22 13:55:15 +1000573 set_bit(BITMAP_STALE, &bitmap->flags);
NeilBrown84e92342012-05-22 13:55:14 +1000574 sb->state = cpu_to_le32(bitmap->flags);
Jonathan Brassow9c810752011-06-08 17:59:30 -0500575 bitmap->events_cleared = bitmap->mddev->events;
576 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
Goldwyn Rodriguesd3b178a2015-07-22 12:09:17 -0500577 bitmap->mddev->bitmap_info.nodes = 0;
Jonathan Brassow9c810752011-06-08 17:59:30 -0500578
Cong Wangb2f46e62011-11-28 13:25:44 +0800579 kunmap_atomic(sb);
Jonathan Brassow9c810752011-06-08 17:59:30 -0500580
581 return 0;
582}
583
NeilBrown32a76272005-06-21 17:17:14 -0700584/* read the superblock from the bitmap file and initialize some bitmap fields */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700585static int md_bitmap_read_sb(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700586{
587 char *reason = NULL;
588 bitmap_super_t *sb;
NeilBrown4b6d2872005-09-09 16:23:47 -0700589 unsigned long chunksize, daemon_sleep, write_behind;
NeilBrown32a76272005-06-21 17:17:14 -0700590 unsigned long long events;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500591 int nodes = 0;
NeilBrown1dff2b82012-05-22 13:55:34 +1000592 unsigned long sectors_reserved = 0;
NeilBrown32a76272005-06-21 17:17:14 -0700593 int err = -EINVAL;
NeilBrown27581e52012-05-22 13:55:08 +1000594 struct page *sb_page;
Goldwyn Rodrigues33e38ac2015-07-01 12:19:56 +1000595 loff_t offset = bitmap->mddev->bitmap_info.offset;
NeilBrown32a76272005-06-21 17:17:14 -0700596
NeilBrown1ec885c2012-05-22 13:55:10 +1000597 if (!bitmap->storage.file && !bitmap->mddev->bitmap_info.offset) {
NeilBrownef99bf42012-05-22 13:55:08 +1000598 chunksize = 128 * 1024 * 1024;
599 daemon_sleep = 5 * HZ;
600 write_behind = 0;
NeilBrownb405fe92012-05-22 13:55:15 +1000601 set_bit(BITMAP_STALE, &bitmap->flags);
NeilBrownef99bf42012-05-22 13:55:08 +1000602 err = 0;
603 goto out_no_sb;
604 }
NeilBrown32a76272005-06-21 17:17:14 -0700605 /* page 0 is the superblock, read it... */
NeilBrown27581e52012-05-22 13:55:08 +1000606 sb_page = alloc_page(GFP_KERNEL);
607 if (!sb_page)
608 return -ENOMEM;
NeilBrown1ec885c2012-05-22 13:55:10 +1000609 bitmap->storage.sb_page = sb_page;
NeilBrown27581e52012-05-22 13:55:08 +1000610
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500611re_read:
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -0500612 /* If cluster_slot is set, the cluster is setup */
613 if (bitmap->cluster_slot >= 0) {
Stephen Rothwell3b0e6aa2015-03-03 13:35:31 +1100614 sector_t bm_blocks = bitmap->mddev->resync_max_sectors;
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -0500615
Stephen Rothwell3b0e6aa2015-03-03 13:35:31 +1100616 sector_div(bm_blocks,
617 bitmap->mddev->bitmap_info.chunksize >> 9);
Goldwyn Rodrigues124eb762015-03-24 11:29:05 -0500618 /* bits to bytes */
619 bm_blocks = ((bm_blocks+7) >> 3) + sizeof(bitmap_super_t);
620 /* to 4k blocks */
NeilBrown935f3d42015-03-02 17:02:29 +1100621 bm_blocks = DIV_ROUND_UP_SECTOR_T(bm_blocks, 4096);
Goldwyn Rodrigues33e38ac2015-07-01 12:19:56 +1000622 offset = bitmap->mddev->bitmap_info.offset + (bitmap->cluster_slot * (bm_blocks << 3));
NeilBrownec0cc222016-11-02 14:16:49 +1100623 pr_debug("%s:%d bm slot: %d offset: %llu\n", __func__, __LINE__,
Goldwyn Rodrigues33e38ac2015-07-01 12:19:56 +1000624 bitmap->cluster_slot, offset);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -0500625 }
626
NeilBrown1ec885c2012-05-22 13:55:10 +1000627 if (bitmap->storage.file) {
628 loff_t isize = i_size_read(bitmap->storage.file->f_mapping->host);
NeilBrownf49d5e62007-01-26 00:57:03 -0800629 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
630
NeilBrown1ec885c2012-05-22 13:55:10 +1000631 err = read_page(bitmap->storage.file, 0,
NeilBrown27581e52012-05-22 13:55:08 +1000632 bitmap, bytes, sb_page);
NeilBrownf49d5e62007-01-26 00:57:03 -0800633 } else {
NeilBrown27581e52012-05-22 13:55:08 +1000634 err = read_sb_page(bitmap->mddev,
Goldwyn Rodrigues33e38ac2015-07-01 12:19:56 +1000635 offset,
NeilBrown27581e52012-05-22 13:55:08 +1000636 sb_page,
Shaohua Li938b5332017-10-16 19:03:44 -0700637 0, sizeof(bitmap_super_t));
NeilBrowna654b9d82005-06-21 17:17:27 -0700638 }
NeilBrown27581e52012-05-22 13:55:08 +1000639 if (err)
NeilBrown32a76272005-06-21 17:17:14 -0700640 return err;
NeilBrown32a76272005-06-21 17:17:14 -0700641
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500642 err = -EINVAL;
NeilBrown27581e52012-05-22 13:55:08 +1000643 sb = kmap_atomic(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700644
NeilBrown32a76272005-06-21 17:17:14 -0700645 chunksize = le32_to_cpu(sb->chunksize);
NeilBrown1b04be92009-12-14 12:49:53 +1100646 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
NeilBrown4b6d2872005-09-09 16:23:47 -0700647 write_behind = le32_to_cpu(sb->write_behind);
NeilBrown1dff2b82012-05-22 13:55:34 +1000648 sectors_reserved = le32_to_cpu(sb->sectors_reserved);
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +1000649 /* Setup nodes/clustername only if bitmap version is
650 * cluster-compatible
Goldwyn Rodriguesd3b178a2015-07-22 12:09:17 -0500651 */
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +1000652 if (sb->version == cpu_to_le32(BITMAP_MAJOR_CLUSTERED)) {
Goldwyn Rodriguesd3b178a2015-07-22 12:09:17 -0500653 nodes = le32_to_cpu(sb->nodes);
654 strlcpy(bitmap->mddev->bitmap_info.cluster_name,
655 sb->cluster_name, 64);
656 }
NeilBrown32a76272005-06-21 17:17:14 -0700657
658 /* verify that the bitmap-specific fields are valid */
659 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
660 reason = "bad magic";
NeilBrownbd926c62005-11-08 21:39:32 -0800661 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +1000662 le32_to_cpu(sb->version) > BITMAP_MAJOR_CLUSTERED)
NeilBrown32a76272005-06-21 17:17:14 -0700663 reason = "unrecognized superblock version";
NeilBrown1187cf02009-03-31 14:27:02 +1100664 else if (chunksize < 512)
NeilBrown7dd5d342006-01-06 00:20:39 -0800665 reason = "bitmap chunksize too small";
Jonathan Brassowd7445402011-06-08 18:01:10 -0500666 else if (!is_power_of_2(chunksize))
NeilBrown32a76272005-06-21 17:17:14 -0700667 reason = "bitmap chunksize not a power of 2";
NeilBrown1b04be92009-12-14 12:49:53 +1100668 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
NeilBrown7dd5d342006-01-06 00:20:39 -0800669 reason = "daemon sleep period out of range";
NeilBrown4b6d2872005-09-09 16:23:47 -0700670 else if (write_behind > COUNTER_MAX)
671 reason = "write-behind limit out of range (0 - 16383)";
NeilBrown32a76272005-06-21 17:17:14 -0700672 if (reason) {
NeilBrownec0cc222016-11-02 14:16:49 +1100673 pr_warn("%s: invalid bitmap file superblock: %s\n",
NeilBrown32a76272005-06-21 17:17:14 -0700674 bmname(bitmap), reason);
675 goto out;
676 }
677
678 /* keep the array size field of the bitmap superblock up to date */
679 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
680
NeilBrown278c1ca2012-03-19 12:46:40 +1100681 if (bitmap->mddev->persistent) {
682 /*
683 * We have a persistent array superblock, so compare the
684 * bitmap's UUID and event counter to the mddev's
685 */
686 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
NeilBrownec0cc222016-11-02 14:16:49 +1100687 pr_warn("%s: bitmap superblock UUID mismatch\n",
688 bmname(bitmap));
NeilBrown278c1ca2012-03-19 12:46:40 +1100689 goto out;
690 }
691 events = le64_to_cpu(sb->events);
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500692 if (!nodes && (events < bitmap->mddev->events)) {
NeilBrownec0cc222016-11-02 14:16:49 +1100693 pr_warn("%s: bitmap file is out of date (%llu < %llu) -- forcing full recovery\n",
694 bmname(bitmap), events,
695 (unsigned long long) bitmap->mddev->events);
NeilBrownb405fe92012-05-22 13:55:15 +1000696 set_bit(BITMAP_STALE, &bitmap->flags);
NeilBrown278c1ca2012-03-19 12:46:40 +1100697 }
698 }
NeilBrown32a76272005-06-21 17:17:14 -0700699
NeilBrown32a76272005-06-21 17:17:14 -0700700 /* assign fields using values from superblock */
NeilBrown4f2e6392006-10-21 10:24:09 -0700701 bitmap->flags |= le32_to_cpu(sb->state);
NeilBrownbd926c62005-11-08 21:39:32 -0800702 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
NeilBrownb405fe92012-05-22 13:55:15 +1000703 set_bit(BITMAP_HOSTENDIAN, &bitmap->flags);
NeilBrown32a76272005-06-21 17:17:14 -0700704 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -0500705 strlcpy(bitmap->mddev->bitmap_info.cluster_name, sb->cluster_name, 64);
NeilBrown32a76272005-06-21 17:17:14 -0700706 err = 0;
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500707
NeilBrown32a76272005-06-21 17:17:14 -0700708out:
Cong Wangb2f46e62011-11-28 13:25:44 +0800709 kunmap_atomic(sb);
Zhilong Liu3560741e2017-03-15 16:14:53 +0800710 /* Assigning chunksize is required for "re_read" */
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -0500711 bitmap->mddev->bitmap_info.chunksize = chunksize;
Goldwyn Rodriguesf7357272015-07-22 12:09:16 -0500712 if (err == 0 && nodes && (bitmap->cluster_slot < 0)) {
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500713 err = md_setup_cluster(bitmap->mddev, nodes);
714 if (err) {
NeilBrownec0cc222016-11-02 14:16:49 +1100715 pr_warn("%s: Could not setup cluster service (%d)\n",
716 bmname(bitmap), err);
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500717 goto out_no_sb;
718 }
719 bitmap->cluster_slot = md_cluster_ops->slot_number(bitmap->mddev);
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500720 goto re_read;
721 }
722
723
NeilBrownef99bf42012-05-22 13:55:08 +1000724out_no_sb:
NeilBrownb405fe92012-05-22 13:55:15 +1000725 if (test_bit(BITMAP_STALE, &bitmap->flags))
NeilBrownef99bf42012-05-22 13:55:08 +1000726 bitmap->events_cleared = bitmap->mddev->events;
727 bitmap->mddev->bitmap_info.chunksize = chunksize;
728 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
729 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -0500730 bitmap->mddev->bitmap_info.nodes = nodes;
NeilBrown1dff2b82012-05-22 13:55:34 +1000731 if (bitmap->mddev->bitmap_info.space == 0 ||
732 bitmap->mddev->bitmap_info.space > sectors_reserved)
733 bitmap->mddev->bitmap_info.space = sectors_reserved;
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500734 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700735 md_bitmap_print_sb(bitmap);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -0500736 if (bitmap->cluster_slot < 0)
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500737 md_cluster_stop(bitmap->mddev);
738 }
NeilBrown32a76272005-06-21 17:17:14 -0700739 return err;
740}
741
NeilBrown32a76272005-06-21 17:17:14 -0700742/*
743 * general bitmap file operations
744 */
745
NeilBrownece5cff2009-12-14 12:49:56 +1100746/*
747 * on-disk bitmap:
748 *
749 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
750 * file a page at a time. There's a superblock at the start of the file.
751 */
NeilBrown32a76272005-06-21 17:17:14 -0700752/* calculate the index of the page that contains this bit */
NeilBrown1ec885c2012-05-22 13:55:10 +1000753static inline unsigned long file_page_index(struct bitmap_storage *store,
754 unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700755{
NeilBrown1ec885c2012-05-22 13:55:10 +1000756 if (store->sb_page)
NeilBrownece5cff2009-12-14 12:49:56 +1100757 chunk += sizeof(bitmap_super_t) << 3;
758 return chunk >> PAGE_BIT_SHIFT;
NeilBrown32a76272005-06-21 17:17:14 -0700759}
760
761/* calculate the (bit) offset of this bit within a page */
NeilBrown1ec885c2012-05-22 13:55:10 +1000762static inline unsigned long file_page_offset(struct bitmap_storage *store,
763 unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700764{
NeilBrown1ec885c2012-05-22 13:55:10 +1000765 if (store->sb_page)
NeilBrownece5cff2009-12-14 12:49:56 +1100766 chunk += sizeof(bitmap_super_t) << 3;
767 return chunk & (PAGE_BITS - 1);
NeilBrown32a76272005-06-21 17:17:14 -0700768}
769
770/*
771 * return a pointer to the page in the filemap that contains the given bit
772 *
NeilBrown32a76272005-06-21 17:17:14 -0700773 */
NeilBrown1ec885c2012-05-22 13:55:10 +1000774static inline struct page *filemap_get_page(struct bitmap_storage *store,
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000775 unsigned long chunk)
NeilBrown32a76272005-06-21 17:17:14 -0700776{
NeilBrown1ec885c2012-05-22 13:55:10 +1000777 if (file_page_index(store, chunk) >= store->file_pages)
NeilBrownac2f40b2010-06-01 19:37:31 +1000778 return NULL;
NeilBrownf2e06c52014-05-28 13:39:23 +1000779 return store->filemap[file_page_index(store, chunk)];
NeilBrown32a76272005-06-21 17:17:14 -0700780}
781
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700782static int md_bitmap_storage_alloc(struct bitmap_storage *store,
783 unsigned long chunks, int with_super,
784 int slot_number)
NeilBrownd1244cb2012-05-22 13:55:12 +1000785{
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500786 int pnum, offset = 0;
NeilBrownd1244cb2012-05-22 13:55:12 +1000787 unsigned long num_pages;
788 unsigned long bytes;
789
790 bytes = DIV_ROUND_UP(chunks, 8);
791 if (with_super)
792 bytes += sizeof(bitmap_super_t);
793
794 num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
Guoqing Jiang7f86ffe2016-05-02 11:50:13 -0400795 offset = slot_number * num_pages;
NeilBrownd1244cb2012-05-22 13:55:12 +1000796
Kees Cook6da2ec52018-06-12 13:55:00 -0700797 store->filemap = kmalloc_array(num_pages, sizeof(struct page *),
798 GFP_KERNEL);
NeilBrownd1244cb2012-05-22 13:55:12 +1000799 if (!store->filemap)
800 return -ENOMEM;
801
802 if (with_super && !store->sb_page) {
NeilBrownd60b4792012-05-22 13:55:25 +1000803 store->sb_page = alloc_page(GFP_KERNEL|__GFP_ZERO);
NeilBrownd1244cb2012-05-22 13:55:12 +1000804 if (store->sb_page == NULL)
805 return -ENOMEM;
NeilBrownd1244cb2012-05-22 13:55:12 +1000806 }
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500807
NeilBrownd1244cb2012-05-22 13:55:12 +1000808 pnum = 0;
809 if (store->sb_page) {
810 store->filemap[0] = store->sb_page;
811 pnum = 1;
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500812 store->sb_page->index = offset;
NeilBrownd1244cb2012-05-22 13:55:12 +1000813 }
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500814
NeilBrownd1244cb2012-05-22 13:55:12 +1000815 for ( ; pnum < num_pages; pnum++) {
NeilBrownd60b4792012-05-22 13:55:25 +1000816 store->filemap[pnum] = alloc_page(GFP_KERNEL|__GFP_ZERO);
NeilBrownd1244cb2012-05-22 13:55:12 +1000817 if (!store->filemap[pnum]) {
818 store->file_pages = pnum;
819 return -ENOMEM;
820 }
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -0500821 store->filemap[pnum]->index = pnum + offset;
NeilBrownd1244cb2012-05-22 13:55:12 +1000822 }
823 store->file_pages = pnum;
824
825 /* We need 4 bits per page, rounded up to a multiple
826 * of sizeof(unsigned long) */
827 store->filemap_attr = kzalloc(
828 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
829 GFP_KERNEL);
830 if (!store->filemap_attr)
831 return -ENOMEM;
832
833 store->bytes = bytes;
834
835 return 0;
836}
837
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700838static void md_bitmap_file_unmap(struct bitmap_storage *store)
NeilBrown32a76272005-06-21 17:17:14 -0700839{
840 struct page **map, *sb_page;
NeilBrown32a76272005-06-21 17:17:14 -0700841 int pages;
NeilBrownfae7d322012-05-22 13:55:21 +1000842 struct file *file;
NeilBrown32a76272005-06-21 17:17:14 -0700843
NeilBrownfae7d322012-05-22 13:55:21 +1000844 file = store->file;
NeilBrown1ec885c2012-05-22 13:55:10 +1000845 map = store->filemap;
NeilBrown1ec885c2012-05-22 13:55:10 +1000846 pages = store->file_pages;
NeilBrown1ec885c2012-05-22 13:55:10 +1000847 sb_page = store->sb_page;
NeilBrown32a76272005-06-21 17:17:14 -0700848
849 while (pages--)
NeilBrownece5cff2009-12-14 12:49:56 +1100850 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
NeilBrownd785a062006-06-26 00:27:48 -0700851 free_buffers(map[pages]);
NeilBrown32a76272005-06-21 17:17:14 -0700852 kfree(map);
NeilBrownfae7d322012-05-22 13:55:21 +1000853 kfree(store->filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700854
NeilBrownd785a062006-06-26 00:27:48 -0700855 if (sb_page)
856 free_buffers(sb_page);
NeilBrown32a76272005-06-21 17:17:14 -0700857
NeilBrownd785a062006-06-26 00:27:48 -0700858 if (file) {
Al Viro496ad9a2013-01-23 17:07:38 -0500859 struct inode *inode = file_inode(file);
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800860 invalidate_mapping_pages(inode->i_mapping, 0, -1);
NeilBrown32a76272005-06-21 17:17:14 -0700861 fput(file);
NeilBrownd785a062006-06-26 00:27:48 -0700862 }
NeilBrown32a76272005-06-21 17:17:14 -0700863}
864
NeilBrown32a76272005-06-21 17:17:14 -0700865/*
866 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
867 * then it is no longer reliable, so we stop using it and we mark the file
868 * as failed in the superblock
869 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700870static void md_bitmap_file_kick(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -0700871{
872 char *path, *ptr = NULL;
873
NeilBrownb405fe92012-05-22 13:55:15 +1000874 if (!test_and_set_bit(BITMAP_STALE, &bitmap->flags)) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700875 md_bitmap_update_sb(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -0700876
NeilBrown1ec885c2012-05-22 13:55:10 +1000877 if (bitmap->storage.file) {
NeilBrown4ad13662007-07-17 04:06:13 -0700878 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
879 if (path)
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +0200880 ptr = file_path(bitmap->storage.file,
NeilBrown1ec885c2012-05-22 13:55:10 +1000881 path, PAGE_SIZE);
Christoph Hellwig6bcfd602008-05-23 13:04:34 -0700882
NeilBrownec0cc222016-11-02 14:16:49 +1100883 pr_warn("%s: kicking failed bitmap file %s from array!\n",
884 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
NeilBrown32a76272005-06-21 17:17:14 -0700885
NeilBrown4ad13662007-07-17 04:06:13 -0700886 kfree(path);
887 } else
NeilBrownec0cc222016-11-02 14:16:49 +1100888 pr_warn("%s: disabling internal bitmap due to errors\n",
889 bmname(bitmap));
NeilBrowna654b9d82005-06-21 17:17:27 -0700890 }
NeilBrown32a76272005-06-21 17:17:14 -0700891}
892
893enum bitmap_page_attr {
NeilBrownac2f40b2010-06-01 19:37:31 +1000894 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
NeilBrown5a537df2011-09-21 15:37:46 +1000895 BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
896 * i.e. counter is 1 or 2. */
NeilBrownac2f40b2010-06-01 19:37:31 +1000897 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
NeilBrown32a76272005-06-21 17:17:14 -0700898};
899
NeilBrownd1891222012-05-22 13:55:09 +1000900static inline void set_page_attr(struct bitmap *bitmap, int pnum,
901 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700902{
NeilBrownbdfd1142012-05-22 13:55:22 +1000903 set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700904}
905
NeilBrownd1891222012-05-22 13:55:09 +1000906static inline void clear_page_attr(struct bitmap *bitmap, int pnum,
907 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700908{
NeilBrownbdfd1142012-05-22 13:55:22 +1000909 clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700910}
911
NeilBrownbdfd1142012-05-22 13:55:22 +1000912static inline int test_page_attr(struct bitmap *bitmap, int pnum,
913 enum bitmap_page_attr attr)
NeilBrown32a76272005-06-21 17:17:14 -0700914{
NeilBrown1ec885c2012-05-22 13:55:10 +1000915 return test_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
NeilBrown32a76272005-06-21 17:17:14 -0700916}
917
NeilBrownbdfd1142012-05-22 13:55:22 +1000918static inline int test_and_clear_page_attr(struct bitmap *bitmap, int pnum,
919 enum bitmap_page_attr attr)
920{
921 return test_and_clear_bit((pnum<<2) + attr,
922 bitmap->storage.filemap_attr);
923}
NeilBrown32a76272005-06-21 17:17:14 -0700924/*
925 * bitmap_file_set_bit -- called before performing a write to the md device
926 * to set (and eventually sync) a particular bit in the bitmap file
927 *
928 * we set the bit immediately, then we record the page number so that
929 * when an unplug occurs, we can flush the dirty pages out to disk
930 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700931static void md_bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
NeilBrown32a76272005-06-21 17:17:14 -0700932{
933 unsigned long bit;
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000934 struct page *page;
NeilBrown32a76272005-06-21 17:17:14 -0700935 void *kaddr;
NeilBrown40cffcc2012-05-22 13:55:24 +1000936 unsigned long chunk = block >> bitmap->counts.chunkshift;
Guoqing Jiang23cea66a2016-05-02 11:50:14 -0400937 struct bitmap_storage *store = &bitmap->storage;
938 unsigned long node_offset = 0;
939
940 if (mddev_is_clustered(bitmap->mddev))
941 node_offset = bitmap->cluster_slot * store->file_pages;
NeilBrown32a76272005-06-21 17:17:14 -0700942
NeilBrown1ec885c2012-05-22 13:55:10 +1000943 page = filemap_get_page(&bitmap->storage, chunk);
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000944 if (!page)
945 return;
NeilBrown1ec885c2012-05-22 13:55:10 +1000946 bit = file_page_offset(&bitmap->storage, chunk);
NeilBrown32a76272005-06-21 17:17:14 -0700947
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000948 /* set the bit */
Cong Wangb2f46e62011-11-28 13:25:44 +0800949 kaddr = kmap_atomic(page);
NeilBrownb405fe92012-05-22 13:55:15 +1000950 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
Jonathan Brassow3520fa42011-07-27 11:00:37 +1000951 set_bit(bit, kaddr);
952 else
Akinobu Mita3f810b62013-04-24 11:42:41 +1000953 set_bit_le(bit, kaddr);
Cong Wangb2f46e62011-11-28 13:25:44 +0800954 kunmap_atomic(kaddr);
NeilBrown36a4e1f2011-10-07 14:23:17 +1100955 pr_debug("set file bit %lu page %lu\n", bit, page->index);
NeilBrown32a76272005-06-21 17:17:14 -0700956 /* record page number so it gets flushed to disk when unplug occurs */
Guoqing Jiang23cea66a2016-05-02 11:50:14 -0400957 set_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_DIRTY);
NeilBrown32a76272005-06-21 17:17:14 -0700958}
959
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700960static void md_bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block)
NeilBrownef99bf42012-05-22 13:55:08 +1000961{
962 unsigned long bit;
963 struct page *page;
964 void *paddr;
NeilBrown40cffcc2012-05-22 13:55:24 +1000965 unsigned long chunk = block >> bitmap->counts.chunkshift;
Guoqing Jiang23cea66a2016-05-02 11:50:14 -0400966 struct bitmap_storage *store = &bitmap->storage;
967 unsigned long node_offset = 0;
968
969 if (mddev_is_clustered(bitmap->mddev))
970 node_offset = bitmap->cluster_slot * store->file_pages;
NeilBrownef99bf42012-05-22 13:55:08 +1000971
NeilBrown1ec885c2012-05-22 13:55:10 +1000972 page = filemap_get_page(&bitmap->storage, chunk);
NeilBrownef99bf42012-05-22 13:55:08 +1000973 if (!page)
974 return;
NeilBrown1ec885c2012-05-22 13:55:10 +1000975 bit = file_page_offset(&bitmap->storage, chunk);
NeilBrownef99bf42012-05-22 13:55:08 +1000976 paddr = kmap_atomic(page);
NeilBrownb405fe92012-05-22 13:55:15 +1000977 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
NeilBrownef99bf42012-05-22 13:55:08 +1000978 clear_bit(bit, paddr);
979 else
Akinobu Mita3f810b62013-04-24 11:42:41 +1000980 clear_bit_le(bit, paddr);
NeilBrownef99bf42012-05-22 13:55:08 +1000981 kunmap_atomic(paddr);
Guoqing Jiang23cea66a2016-05-02 11:50:14 -0400982 if (!test_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_NEEDWRITE)) {
983 set_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_PENDING);
NeilBrownef99bf42012-05-22 13:55:08 +1000984 bitmap->allclean = 0;
985 }
986}
987
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700988static int md_bitmap_file_test_bit(struct bitmap *bitmap, sector_t block)
Goldwyn Rodrigues11dd35d2014-06-07 00:36:26 -0500989{
990 unsigned long bit;
991 struct page *page;
992 void *paddr;
993 unsigned long chunk = block >> bitmap->counts.chunkshift;
994 int set = 0;
995
996 page = filemap_get_page(&bitmap->storage, chunk);
997 if (!page)
998 return -EINVAL;
999 bit = file_page_offset(&bitmap->storage, chunk);
1000 paddr = kmap_atomic(page);
1001 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
1002 set = test_bit(bit, paddr);
1003 else
1004 set = test_bit_le(bit, paddr);
1005 kunmap_atomic(paddr);
1006 return set;
1007}
1008
1009
NeilBrown32a76272005-06-21 17:17:14 -07001010/* this gets called when the md device is ready to unplug its underlying
1011 * (slave) device queues -- before we let any writes go down, we need to
1012 * sync the dirty pages of the bitmap file to disk */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001013void md_bitmap_unplug(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001014{
NeilBrown74667122012-05-22 13:55:19 +10001015 unsigned long i;
NeilBrownec7a3192006-06-26 00:27:45 -07001016 int dirty, need_write;
NeilBrown85c9ccd2016-11-04 16:46:03 +11001017 int writing = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001018
NeilBrown62f82fa2012-05-22 13:55:21 +10001019 if (!bitmap || !bitmap->storage.filemap ||
1020 test_bit(BITMAP_STALE, &bitmap->flags))
NeilBrown4ad13662007-07-17 04:06:13 -07001021 return;
NeilBrown32a76272005-06-21 17:17:14 -07001022
1023 /* look at each page to see if there are any set bits that need to be
1024 * flushed out to disk */
NeilBrown1ec885c2012-05-22 13:55:10 +10001025 for (i = 0; i < bitmap->storage.file_pages; i++) {
NeilBrownbdfd1142012-05-22 13:55:22 +10001026 dirty = test_and_clear_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
1027 need_write = test_and_clear_page_attr(bitmap, i,
1028 BITMAP_PAGE_NEEDWRITE);
1029 if (dirty || need_write) {
NeilBrown581dbd92016-11-14 16:30:21 +11001030 if (!writing) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001031 md_bitmap_wait_writes(bitmap);
NeilBrown581dbd92016-11-14 16:30:21 +11001032 if (bitmap->mddev->queue)
1033 blk_add_trace_msg(bitmap->mddev->queue,
1034 "md bitmap_unplug");
1035 }
NeilBrownd1891222012-05-22 13:55:09 +10001036 clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING);
NeilBrownbdfd1142012-05-22 13:55:22 +10001037 write_page(bitmap, bitmap->storage.filemap[i], 0);
NeilBrown85c9ccd2016-11-04 16:46:03 +11001038 writing = 1;
NeilBrownbdfd1142012-05-22 13:55:22 +10001039 }
NeilBrown32a76272005-06-21 17:17:14 -07001040 }
NeilBrown85c9ccd2016-11-04 16:46:03 +11001041 if (writing)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001042 md_bitmap_wait_writes(bitmap);
NeilBrown4b5060d2014-09-09 14:13:51 +10001043
NeilBrownb405fe92012-05-22 13:55:15 +10001044 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001045 md_bitmap_file_kick(bitmap);
NeilBrown32a76272005-06-21 17:17:14 -07001046}
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001047EXPORT_SYMBOL(md_bitmap_unplug);
NeilBrown32a76272005-06-21 17:17:14 -07001048
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001049static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
NeilBrown32a76272005-06-21 17:17:14 -07001050/* * bitmap_init_from_disk -- called at bitmap_create time to initialize
1051 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
1052 * memory mapping of the bitmap file
1053 * Special cases:
1054 * if there's no bitmap file, or if the bitmap file had been
1055 * previously kicked from the array, we mark all the bits as
1056 * 1's in order to cause a full resync.
NeilBrown6a079972005-09-09 16:23:44 -07001057 *
1058 * We ignore all bits for sectors that end earlier than 'start'.
1059 * This is used when reading an out-of-date bitmap...
NeilBrown32a76272005-06-21 17:17:14 -07001060 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001061static int md_bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
NeilBrown32a76272005-06-21 17:17:14 -07001062{
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -05001063 unsigned long i, chunks, index, oldindex, bit, node_offset = 0;
NeilBrown27581e52012-05-22 13:55:08 +10001064 struct page *page = NULL;
NeilBrownd1244cb2012-05-22 13:55:12 +10001065 unsigned long bit_cnt = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001066 struct file *file;
NeilBrownd1244cb2012-05-22 13:55:12 +10001067 unsigned long offset;
NeilBrown32a76272005-06-21 17:17:14 -07001068 int outofdate;
1069 int ret = -ENOSPC;
NeilBrownea03aff2006-01-06 00:20:34 -08001070 void *paddr;
NeilBrown1ec885c2012-05-22 13:55:10 +10001071 struct bitmap_storage *store = &bitmap->storage;
NeilBrown32a76272005-06-21 17:17:14 -07001072
NeilBrown40cffcc2012-05-22 13:55:24 +10001073 chunks = bitmap->counts.chunks;
NeilBrown1ec885c2012-05-22 13:55:10 +10001074 file = store->file;
NeilBrown32a76272005-06-21 17:17:14 -07001075
NeilBrownef99bf42012-05-22 13:55:08 +10001076 if (!file && !bitmap->mddev->bitmap_info.offset) {
1077 /* No permanent bitmap - fill with '1s'. */
NeilBrown1ec885c2012-05-22 13:55:10 +10001078 store->filemap = NULL;
1079 store->file_pages = 0;
NeilBrownef99bf42012-05-22 13:55:08 +10001080 for (i = 0; i < chunks ; i++) {
1081 /* if the disk bit is set, set the memory bit */
NeilBrown40cffcc2012-05-22 13:55:24 +10001082 int needed = ((sector_t)(i+1) << (bitmap->counts.chunkshift)
NeilBrownef99bf42012-05-22 13:55:08 +10001083 >= start);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001084 md_bitmap_set_memory_bits(bitmap,
1085 (sector_t)i << bitmap->counts.chunkshift,
1086 needed);
NeilBrownef99bf42012-05-22 13:55:08 +10001087 }
1088 return 0;
1089 }
NeilBrown32a76272005-06-21 17:17:14 -07001090
NeilBrownb405fe92012-05-22 13:55:15 +10001091 outofdate = test_bit(BITMAP_STALE, &bitmap->flags);
NeilBrown32a76272005-06-21 17:17:14 -07001092 if (outofdate)
NeilBrownec0cc222016-11-02 14:16:49 +11001093 pr_warn("%s: bitmap file is out of date, doing full recovery\n", bmname(bitmap));
NeilBrown32a76272005-06-21 17:17:14 -07001094
NeilBrownd1244cb2012-05-22 13:55:12 +10001095 if (file && i_size_read(file->f_mapping->host) < store->bytes) {
NeilBrownec0cc222016-11-02 14:16:49 +11001096 pr_warn("%s: bitmap file too short %lu < %lu\n",
1097 bmname(bitmap),
1098 (unsigned long) i_size_read(file->f_mapping->host),
1099 store->bytes);
NeilBrown4ad13662007-07-17 04:06:13 -07001100 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001101 }
NeilBrownbc7f77d2005-06-21 17:17:17 -07001102
NeilBrown32a76272005-06-21 17:17:14 -07001103 oldindex = ~0L;
NeilBrownd1244cb2012-05-22 13:55:12 +10001104 offset = 0;
1105 if (!bitmap->mddev->bitmap_info.external)
1106 offset = sizeof(bitmap_super_t);
NeilBrown32a76272005-06-21 17:17:14 -07001107
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -05001108 if (mddev_is_clustered(bitmap->mddev))
1109 node_offset = bitmap->cluster_slot * (DIV_ROUND_UP(store->bytes, PAGE_SIZE));
1110
NeilBrown32a76272005-06-21 17:17:14 -07001111 for (i = 0; i < chunks; i++) {
NeilBrownbd926c62005-11-08 21:39:32 -08001112 int b;
NeilBrown1ec885c2012-05-22 13:55:10 +10001113 index = file_page_index(&bitmap->storage, i);
1114 bit = file_page_offset(&bitmap->storage, i);
NeilBrown32a76272005-06-21 17:17:14 -07001115 if (index != oldindex) { /* this is a new page, read it in */
NeilBrownd785a062006-06-26 00:27:48 -07001116 int count;
NeilBrown32a76272005-06-21 17:17:14 -07001117 /* unmap the old page, we're done with it */
NeilBrownd1244cb2012-05-22 13:55:12 +10001118 if (index == store->file_pages-1)
1119 count = store->bytes - index * PAGE_SIZE;
NeilBrownd785a062006-06-26 00:27:48 -07001120 else
1121 count = PAGE_SIZE;
NeilBrown1ec885c2012-05-22 13:55:10 +10001122 page = store->filemap[index];
NeilBrown27581e52012-05-22 13:55:08 +10001123 if (file)
1124 ret = read_page(file, index, bitmap,
1125 count, page);
1126 else
1127 ret = read_sb_page(
1128 bitmap->mddev,
1129 bitmap->mddev->bitmap_info.offset,
1130 page,
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -05001131 index + node_offset, count);
NeilBrown27581e52012-05-22 13:55:08 +10001132
1133 if (ret)
NeilBrown4ad13662007-07-17 04:06:13 -07001134 goto err;
NeilBrowna654b9d82005-06-21 17:17:27 -07001135
NeilBrown32a76272005-06-21 17:17:14 -07001136 oldindex = index;
NeilBrown32a76272005-06-21 17:17:14 -07001137
1138 if (outofdate) {
1139 /*
1140 * if bitmap is out of date, dirty the
NeilBrownac2f40b2010-06-01 19:37:31 +10001141 * whole page and write it out
NeilBrown32a76272005-06-21 17:17:14 -07001142 */
Cong Wangb2f46e62011-11-28 13:25:44 +08001143 paddr = kmap_atomic(page);
NeilBrownea03aff2006-01-06 00:20:34 -08001144 memset(paddr + offset, 0xff,
NeilBrown6a079972005-09-09 16:23:44 -07001145 PAGE_SIZE - offset);
Cong Wangb2f46e62011-11-28 13:25:44 +08001146 kunmap_atomic(paddr);
NeilBrown4ad13662007-07-17 04:06:13 -07001147 write_page(bitmap, page, 1);
1148
1149 ret = -EIO;
NeilBrownb405fe92012-05-22 13:55:15 +10001150 if (test_bit(BITMAP_WRITE_ERROR,
1151 &bitmap->flags))
NeilBrown4ad13662007-07-17 04:06:13 -07001152 goto err;
NeilBrown32a76272005-06-21 17:17:14 -07001153 }
NeilBrown32a76272005-06-21 17:17:14 -07001154 }
Cong Wangb2f46e62011-11-28 13:25:44 +08001155 paddr = kmap_atomic(page);
NeilBrownb405fe92012-05-22 13:55:15 +10001156 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
NeilBrownea03aff2006-01-06 00:20:34 -08001157 b = test_bit(bit, paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001158 else
Akinobu Mita6b33aff2011-03-23 16:42:13 -07001159 b = test_bit_le(bit, paddr);
Cong Wangb2f46e62011-11-28 13:25:44 +08001160 kunmap_atomic(paddr);
NeilBrownbd926c62005-11-08 21:39:32 -08001161 if (b) {
NeilBrown32a76272005-06-21 17:17:14 -07001162 /* if the disk bit is set, set the memory bit */
NeilBrown40cffcc2012-05-22 13:55:24 +10001163 int needed = ((sector_t)(i+1) << bitmap->counts.chunkshift
NeilBrowndb305e52009-05-07 12:49:06 +10001164 >= start);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001165 md_bitmap_set_memory_bits(bitmap,
1166 (sector_t)i << bitmap->counts.chunkshift,
1167 needed);
NeilBrown32a76272005-06-21 17:17:14 -07001168 bit_cnt++;
1169 }
NeilBrown27581e52012-05-22 13:55:08 +10001170 offset = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001171 }
1172
NeilBrownec0cc222016-11-02 14:16:49 +11001173 pr_debug("%s: bitmap initialized from disk: read %lu pages, set %lu of %lu bits\n",
1174 bmname(bitmap), store->file_pages,
1175 bit_cnt, chunks);
NeilBrown32a76272005-06-21 17:17:14 -07001176
NeilBrown4ad13662007-07-17 04:06:13 -07001177 return 0;
1178
1179 err:
NeilBrownec0cc222016-11-02 14:16:49 +11001180 pr_warn("%s: bitmap initialisation failed: %d\n",
1181 bmname(bitmap), ret);
NeilBrown32a76272005-06-21 17:17:14 -07001182 return ret;
1183}
1184
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001185void md_bitmap_write_all(struct bitmap *bitmap)
NeilBrowna654b9d82005-06-21 17:17:27 -07001186{
1187 /* We don't actually write all bitmap blocks here,
1188 * just flag them as needing to be written
1189 */
NeilBrownec7a3192006-06-26 00:27:45 -07001190 int i;
NeilBrowna654b9d82005-06-21 17:17:27 -07001191
NeilBrown1ec885c2012-05-22 13:55:10 +10001192 if (!bitmap || !bitmap->storage.filemap)
NeilBrownef99bf42012-05-22 13:55:08 +10001193 return;
NeilBrown1ec885c2012-05-22 13:55:10 +10001194 if (bitmap->storage.file)
NeilBrownef99bf42012-05-22 13:55:08 +10001195 /* Only one copy, so nothing needed */
1196 return;
1197
NeilBrown1ec885c2012-05-22 13:55:10 +10001198 for (i = 0; i < bitmap->storage.file_pages; i++)
NeilBrownd1891222012-05-22 13:55:09 +10001199 set_page_attr(bitmap, i,
NeilBrownec7a3192006-06-26 00:27:45 -07001200 BITMAP_PAGE_NEEDWRITE);
NeilBrown2585f3e2011-09-21 15:37:46 +10001201 bitmap->allclean = 0;
NeilBrowna654b9d82005-06-21 17:17:27 -07001202}
1203
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001204static void md_bitmap_count_page(struct bitmap_counts *bitmap,
1205 sector_t offset, int inc)
NeilBrown32a76272005-06-21 17:17:14 -07001206{
NeilBrown61a0d802012-03-19 12:46:41 +11001207 sector_t chunk = offset >> bitmap->chunkshift;
NeilBrown32a76272005-06-21 17:17:14 -07001208 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1209 bitmap->bp[page].count += inc;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001210 md_bitmap_checkfree(bitmap, page);
NeilBrown32a76272005-06-21 17:17:14 -07001211}
NeilBrownbf07bb72012-05-22 13:55:06 +10001212
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001213static void md_bitmap_set_pending(struct bitmap_counts *bitmap, sector_t offset)
NeilBrownbf07bb72012-05-22 13:55:06 +10001214{
1215 sector_t chunk = offset >> bitmap->chunkshift;
1216 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1217 struct bitmap_page *bp = &bitmap->bp[page];
1218
1219 if (!bp->pending)
1220 bp->pending = 1;
1221}
1222
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001223static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap,
1224 sector_t offset, sector_t *blocks,
1225 int create);
NeilBrown32a76272005-06-21 17:17:14 -07001226
1227/*
1228 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1229 * out to disk
1230 */
1231
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001232void md_bitmap_daemon_work(struct mddev *mddev)
NeilBrown32a76272005-06-21 17:17:14 -07001233{
NeilBrownaa5cbd12009-12-14 12:49:46 +11001234 struct bitmap *bitmap;
NeilBrownaa3163f2005-06-21 17:17:22 -07001235 unsigned long j;
NeilBrownbf07bb72012-05-22 13:55:06 +10001236 unsigned long nextpage;
NeilBrown57dab0b2010-10-19 10:03:39 +11001237 sector_t blocks;
NeilBrown40cffcc2012-05-22 13:55:24 +10001238 struct bitmap_counts *counts;
NeilBrown32a76272005-06-21 17:17:14 -07001239
NeilBrownaa5cbd12009-12-14 12:49:46 +11001240 /* Use a mutex to guard daemon_work against
1241 * bitmap_destroy.
1242 */
NeilBrownc3d97142009-12-14 12:49:52 +11001243 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrownaa5cbd12009-12-14 12:49:46 +11001244 bitmap = mddev->bitmap;
1245 if (bitmap == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001246 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown4ad13662007-07-17 04:06:13 -07001247 return;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001248 }
NeilBrown42a04b52009-12-14 12:49:53 +11001249 if (time_before(jiffies, bitmap->daemon_lastrun
NeilBrown2e61ebb2011-12-23 10:17:50 +11001250 + mddev->bitmap_info.daemon_sleep))
NeilBrown7be3dfe2008-03-10 11:43:48 -07001251 goto done;
1252
NeilBrown32a76272005-06-21 17:17:14 -07001253 bitmap->daemon_lastrun = jiffies;
NeilBrown8311c292008-03-04 14:29:30 -08001254 if (bitmap->allclean) {
NeilBrown2e61ebb2011-12-23 10:17:50 +11001255 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrownaa5cbd12009-12-14 12:49:46 +11001256 goto done;
NeilBrown8311c292008-03-04 14:29:30 -08001257 }
1258 bitmap->allclean = 1;
NeilBrown32a76272005-06-21 17:17:14 -07001259
NeilBrown581dbd92016-11-14 16:30:21 +11001260 if (bitmap->mddev->queue)
1261 blk_add_trace_msg(bitmap->mddev->queue,
1262 "md bitmap_daemon_work");
1263
NeilBrownbf07bb72012-05-22 13:55:06 +10001264 /* Any file-page which is PENDING now needs to be written.
1265 * So set NEEDWRITE now, then after we make any last-minute changes
1266 * we will write it.
1267 */
NeilBrown1ec885c2012-05-22 13:55:10 +10001268 for (j = 0; j < bitmap->storage.file_pages; j++)
NeilBrownbdfd1142012-05-22 13:55:22 +10001269 if (test_and_clear_page_attr(bitmap, j,
1270 BITMAP_PAGE_PENDING))
NeilBrownd1891222012-05-22 13:55:09 +10001271 set_page_attr(bitmap, j,
NeilBrownbf07bb72012-05-22 13:55:06 +10001272 BITMAP_PAGE_NEEDWRITE);
NeilBrownbf07bb72012-05-22 13:55:06 +10001273
1274 if (bitmap->need_sync &&
1275 mddev->bitmap_info.external == 0) {
1276 /* Arrange for superblock update as well as
1277 * other changes */
1278 bitmap_super_t *sb;
1279 bitmap->need_sync = 0;
NeilBrown1ec885c2012-05-22 13:55:10 +10001280 if (bitmap->storage.filemap) {
1281 sb = kmap_atomic(bitmap->storage.sb_page);
NeilBrownef99bf42012-05-22 13:55:08 +10001282 sb->events_cleared =
1283 cpu_to_le64(bitmap->events_cleared);
1284 kunmap_atomic(sb);
NeilBrownd1891222012-05-22 13:55:09 +10001285 set_page_attr(bitmap, 0,
NeilBrownef99bf42012-05-22 13:55:08 +10001286 BITMAP_PAGE_NEEDWRITE);
1287 }
NeilBrownbf07bb72012-05-22 13:55:06 +10001288 }
1289 /* Now look at the bitmap counters and if any are '2' or '1',
1290 * decrement and handle accordingly.
1291 */
NeilBrown40cffcc2012-05-22 13:55:24 +10001292 counts = &bitmap->counts;
1293 spin_lock_irq(&counts->lock);
NeilBrownbf07bb72012-05-22 13:55:06 +10001294 nextpage = 0;
NeilBrown40cffcc2012-05-22 13:55:24 +10001295 for (j = 0; j < counts->chunks; j++) {
NeilBrown32a76272005-06-21 17:17:14 -07001296 bitmap_counter_t *bmc;
NeilBrown40cffcc2012-05-22 13:55:24 +10001297 sector_t block = (sector_t)j << counts->chunkshift;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001298
NeilBrownbf07bb72012-05-22 13:55:06 +10001299 if (j == nextpage) {
1300 nextpage += PAGE_COUNTER_RATIO;
NeilBrown40cffcc2012-05-22 13:55:24 +10001301 if (!counts->bp[j >> PAGE_COUNTER_SHIFT].pending) {
NeilBrownbf07bb72012-05-22 13:55:06 +10001302 j |= PAGE_COUNTER_MASK;
NeilBrownaa3163f2005-06-21 17:17:22 -07001303 continue;
1304 }
NeilBrown40cffcc2012-05-22 13:55:24 +10001305 counts->bp[j >> PAGE_COUNTER_SHIFT].pending = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001306 }
NeilBrown32a76272005-06-21 17:17:14 -07001307
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001308 bmc = md_bitmap_get_counter(counts, block, &blocks, 0);
NeilBrownbf07bb72012-05-22 13:55:06 +10001309 if (!bmc) {
1310 j |= PAGE_COUNTER_MASK;
1311 continue;
1312 }
1313 if (*bmc == 1 && !bitmap->need_sync) {
1314 /* We can clear the bit */
NeilBrownbf07bb72012-05-22 13:55:06 +10001315 *bmc = 0;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001316 md_bitmap_count_page(counts, block, -1);
1317 md_bitmap_file_clear_bit(bitmap, block);
NeilBrownbf07bb72012-05-22 13:55:06 +10001318 } else if (*bmc && *bmc <= 2) {
1319 *bmc = 1;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001320 md_bitmap_set_pending(counts, block);
NeilBrown2585f3e2011-09-21 15:37:46 +10001321 bitmap->allclean = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001322 }
NeilBrown32a76272005-06-21 17:17:14 -07001323 }
NeilBrown40cffcc2012-05-22 13:55:24 +10001324 spin_unlock_irq(&counts->lock);
NeilBrown32a76272005-06-21 17:17:14 -07001325
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001326 md_bitmap_wait_writes(bitmap);
NeilBrownbf07bb72012-05-22 13:55:06 +10001327 /* Now start writeout on any page in NEEDWRITE that isn't DIRTY.
1328 * DIRTY pages need to be written by bitmap_unplug so it can wait
1329 * for them.
1330 * If we find any DIRTY page we stop there and let bitmap_unplug
1331 * handle all the rest. This is important in the case where
1332 * the first blocking holds the superblock and it has been updated.
1333 * We mustn't write any other blocks before the superblock.
1334 */
NeilBrown62f82fa2012-05-22 13:55:21 +10001335 for (j = 0;
1336 j < bitmap->storage.file_pages
1337 && !test_bit(BITMAP_STALE, &bitmap->flags);
1338 j++) {
NeilBrownd1891222012-05-22 13:55:09 +10001339 if (test_page_attr(bitmap, j,
NeilBrownbf07bb72012-05-22 13:55:06 +10001340 BITMAP_PAGE_DIRTY))
1341 /* bitmap_unplug will handle the rest */
1342 break;
Zhiqiang Liu55180492019-12-07 11:00:08 +08001343 if (bitmap->storage.filemap &&
1344 test_and_clear_page_attr(bitmap, j,
NeilBrownbdfd1142012-05-22 13:55:22 +10001345 BITMAP_PAGE_NEEDWRITE)) {
NeilBrown1ec885c2012-05-22 13:55:10 +10001346 write_page(bitmap, bitmap->storage.filemap[j], 0);
NeilBrownbf07bb72012-05-22 13:55:06 +10001347 }
1348 }
NeilBrownbf07bb72012-05-22 13:55:06 +10001349
NeilBrown7be3dfe2008-03-10 11:43:48 -07001350 done:
NeilBrown8311c292008-03-04 14:29:30 -08001351 if (bitmap->allclean == 0)
NeilBrown2e61ebb2011-12-23 10:17:50 +11001352 mddev->thread->timeout =
1353 mddev->bitmap_info.daemon_sleep;
NeilBrownc3d97142009-12-14 12:49:52 +11001354 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrown32a76272005-06-21 17:17:14 -07001355}
1356
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001357static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap,
1358 sector_t offset, sector_t *blocks,
1359 int create)
NeilBrownee305ac2009-09-23 18:06:44 +10001360__releases(bitmap->lock)
1361__acquires(bitmap->lock)
NeilBrown32a76272005-06-21 17:17:14 -07001362{
1363 /* If 'create', we might release the lock and reclaim it.
1364 * The lock must have been taken with interrupts enabled.
1365 * If !create, we don't release the lock.
1366 */
NeilBrown61a0d802012-03-19 12:46:41 +11001367 sector_t chunk = offset >> bitmap->chunkshift;
NeilBrown32a76272005-06-21 17:17:14 -07001368 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1369 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1370 sector_t csize;
NeilBrownef425672010-06-01 19:37:33 +10001371 int err;
NeilBrown32a76272005-06-21 17:17:14 -07001372
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001373 err = md_bitmap_checkpage(bitmap, page, create, 0);
NeilBrownef425672010-06-01 19:37:33 +10001374
1375 if (bitmap->bp[page].hijacked ||
1376 bitmap->bp[page].map == NULL)
NeilBrown61a0d802012-03-19 12:46:41 +11001377 csize = ((sector_t)1) << (bitmap->chunkshift +
NeilBrownef425672010-06-01 19:37:33 +10001378 PAGE_COUNTER_SHIFT - 1);
1379 else
NeilBrown61a0d802012-03-19 12:46:41 +11001380 csize = ((sector_t)1) << bitmap->chunkshift;
NeilBrownef425672010-06-01 19:37:33 +10001381 *blocks = csize - (offset & (csize - 1));
1382
1383 if (err < 0)
NeilBrown32a76272005-06-21 17:17:14 -07001384 return NULL;
NeilBrownef425672010-06-01 19:37:33 +10001385
NeilBrown32a76272005-06-21 17:17:14 -07001386 /* now locked ... */
1387
1388 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1389 /* should we use the first or second counter field
1390 * of the hijacked pointer? */
1391 int hi = (pageoff > PAGE_COUNTER_MASK);
NeilBrown32a76272005-06-21 17:17:14 -07001392 return &((bitmap_counter_t *)
1393 &bitmap->bp[page].map)[hi];
NeilBrownef425672010-06-01 19:37:33 +10001394 } else /* page is allocated */
NeilBrown32a76272005-06-21 17:17:14 -07001395 return (bitmap_counter_t *)
1396 &(bitmap->bp[page].map[pageoff]);
NeilBrown32a76272005-06-21 17:17:14 -07001397}
1398
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001399int md_bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001400{
NeilBrownac2f40b2010-06-01 19:37:31 +10001401 if (!bitmap)
1402 return 0;
NeilBrown4b6d2872005-09-09 16:23:47 -07001403
1404 if (behind) {
Paul Clements696fcd52010-03-08 16:02:37 +11001405 int bw;
NeilBrown4b6d2872005-09-09 16:23:47 -07001406 atomic_inc(&bitmap->behind_writes);
Paul Clements696fcd52010-03-08 16:02:37 +11001407 bw = atomic_read(&bitmap->behind_writes);
1408 if (bw > bitmap->behind_writes_used)
1409 bitmap->behind_writes_used = bw;
1410
NeilBrown36a4e1f2011-10-07 14:23:17 +11001411 pr_debug("inc write-behind count %d/%lu\n",
1412 bw, bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001413 }
1414
NeilBrown32a76272005-06-21 17:17:14 -07001415 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001416 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001417 bitmap_counter_t *bmc;
1418
NeilBrown40cffcc2012-05-22 13:55:24 +10001419 spin_lock_irq(&bitmap->counts.lock);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001420 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 1);
NeilBrown32a76272005-06-21 17:17:14 -07001421 if (!bmc) {
NeilBrown40cffcc2012-05-22 13:55:24 +10001422 spin_unlock_irq(&bitmap->counts.lock);
NeilBrown32a76272005-06-21 17:17:14 -07001423 return 0;
1424 }
1425
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001426 if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
Neil Brownda6e1a32007-02-08 14:20:37 -08001427 DEFINE_WAIT(__wait);
1428 /* note that it is safe to do the prepare_to_wait
1429 * after the test as long as we do it before dropping
1430 * the spinlock.
1431 */
1432 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1433 TASK_UNINTERRUPTIBLE);
NeilBrown40cffcc2012-05-22 13:55:24 +10001434 spin_unlock_irq(&bitmap->counts.lock);
NeilBrownf54a9d02012-08-02 08:33:20 +10001435 schedule();
Neil Brownda6e1a32007-02-08 14:20:37 -08001436 finish_wait(&bitmap->overflow_wait, &__wait);
1437 continue;
1438 }
1439
NeilBrownac2f40b2010-06-01 19:37:31 +10001440 switch (*bmc) {
NeilBrown32a76272005-06-21 17:17:14 -07001441 case 0:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001442 md_bitmap_file_set_bit(bitmap, offset);
1443 md_bitmap_count_page(&bitmap->counts, offset, 1);
NeilBrown32a76272005-06-21 17:17:14 -07001444 /* fall through */
1445 case 1:
1446 *bmc = 2;
1447 }
Neil Brownda6e1a32007-02-08 14:20:37 -08001448
NeilBrown32a76272005-06-21 17:17:14 -07001449 (*bmc)++;
1450
NeilBrown40cffcc2012-05-22 13:55:24 +10001451 spin_unlock_irq(&bitmap->counts.lock);
NeilBrown32a76272005-06-21 17:17:14 -07001452
1453 offset += blocks;
1454 if (sectors > blocks)
1455 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001456 else
1457 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001458 }
1459 return 0;
1460}
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001461EXPORT_SYMBOL(md_bitmap_startwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001462
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001463void md_bitmap_endwrite(struct bitmap *bitmap, sector_t offset,
1464 unsigned long sectors, int success, int behind)
NeilBrown32a76272005-06-21 17:17:14 -07001465{
NeilBrownac2f40b2010-06-01 19:37:31 +10001466 if (!bitmap)
1467 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001468 if (behind) {
NeilBrowne5551902010-03-31 11:21:44 +11001469 if (atomic_dec_and_test(&bitmap->behind_writes))
1470 wake_up(&bitmap->behind_wait);
NeilBrown36a4e1f2011-10-07 14:23:17 +11001471 pr_debug("dec write-behind count %d/%lu\n",
1472 atomic_read(&bitmap->behind_writes),
1473 bitmap->mddev->bitmap_info.max_write_behind);
NeilBrown4b6d2872005-09-09 16:23:47 -07001474 }
1475
NeilBrown32a76272005-06-21 17:17:14 -07001476 while (sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001477 sector_t blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001478 unsigned long flags;
1479 bitmap_counter_t *bmc;
1480
NeilBrown40cffcc2012-05-22 13:55:24 +10001481 spin_lock_irqsave(&bitmap->counts.lock, flags);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001482 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001483 if (!bmc) {
NeilBrown40cffcc2012-05-22 13:55:24 +10001484 spin_unlock_irqrestore(&bitmap->counts.lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001485 return;
1486 }
1487
NeilBrown961902c2011-12-23 09:57:48 +11001488 if (success && !bitmap->mddev->degraded &&
Neil Browna0da84f2008-06-28 08:31:22 +10001489 bitmap->events_cleared < bitmap->mddev->events) {
1490 bitmap->events_cleared = bitmap->mddev->events;
1491 bitmap->need_sync = 1;
NeilBrown5ff5aff2010-06-01 19:37:32 +10001492 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
Neil Browna0da84f2008-06-28 08:31:22 +10001493 }
1494
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001495 if (!success && !NEEDED(*bmc))
NeilBrown32a76272005-06-21 17:17:14 -07001496 *bmc |= NEEDED_MASK;
1497
Namhyung Kim27d5ea02011-06-09 11:42:57 +10001498 if (COUNTER(*bmc) == COUNTER_MAX)
Neil Brownda6e1a32007-02-08 14:20:37 -08001499 wake_up(&bitmap->overflow_wait);
1500
NeilBrown32a76272005-06-21 17:17:14 -07001501 (*bmc)--;
NeilBrown2585f3e2011-09-21 15:37:46 +10001502 if (*bmc <= 2) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001503 md_bitmap_set_pending(&bitmap->counts, offset);
NeilBrown2585f3e2011-09-21 15:37:46 +10001504 bitmap->allclean = 0;
1505 }
NeilBrown40cffcc2012-05-22 13:55:24 +10001506 spin_unlock_irqrestore(&bitmap->counts.lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001507 offset += blocks;
1508 if (sectors > blocks)
1509 sectors -= blocks;
NeilBrownac2f40b2010-06-01 19:37:31 +10001510 else
1511 sectors = 0;
NeilBrown32a76272005-06-21 17:17:14 -07001512 }
1513}
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001514EXPORT_SYMBOL(md_bitmap_endwrite);
NeilBrown32a76272005-06-21 17:17:14 -07001515
NeilBrown57dab0b2010-10-19 10:03:39 +11001516static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
NeilBrown1187cf02009-03-31 14:27:02 +11001517 int degraded)
NeilBrown32a76272005-06-21 17:17:14 -07001518{
1519 bitmap_counter_t *bmc;
1520 int rv;
1521 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1522 *blocks = 1024;
1523 return 1; /* always resync if no bitmap */
1524 }
NeilBrown40cffcc2012-05-22 13:55:24 +10001525 spin_lock_irq(&bitmap->counts.lock);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001526 bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001527 rv = 0;
1528 if (bmc) {
1529 /* locked */
1530 if (RESYNC(*bmc))
1531 rv = 1;
1532 else if (NEEDED(*bmc)) {
1533 rv = 1;
NeilBrown6a806c52005-07-15 03:56:35 -07001534 if (!degraded) { /* don't set/clear bits if degraded */
1535 *bmc |= RESYNC_MASK;
1536 *bmc &= ~NEEDED_MASK;
1537 }
NeilBrown32a76272005-06-21 17:17:14 -07001538 }
1539 }
NeilBrown40cffcc2012-05-22 13:55:24 +10001540 spin_unlock_irq(&bitmap->counts.lock);
NeilBrown32a76272005-06-21 17:17:14 -07001541 return rv;
1542}
1543
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001544int md_bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
1545 int degraded)
NeilBrown1187cf02009-03-31 14:27:02 +11001546{
1547 /* bitmap_start_sync must always report on multiples of whole
1548 * pages, otherwise resync (which is very PAGE_SIZE based) will
1549 * get confused.
1550 * So call __bitmap_start_sync repeatedly (if needed) until
1551 * At least PAGE_SIZE>>9 blocks are covered.
1552 * Return the 'or' of the result.
1553 */
1554 int rv = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001555 sector_t blocks1;
NeilBrown1187cf02009-03-31 14:27:02 +11001556
1557 *blocks = 0;
1558 while (*blocks < (PAGE_SIZE>>9)) {
1559 rv |= __bitmap_start_sync(bitmap, offset,
1560 &blocks1, degraded);
1561 offset += blocks1;
1562 *blocks += blocks1;
1563 }
1564 return rv;
1565}
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001566EXPORT_SYMBOL(md_bitmap_start_sync);
NeilBrown1187cf02009-03-31 14:27:02 +11001567
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001568void md_bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
NeilBrown32a76272005-06-21 17:17:14 -07001569{
1570 bitmap_counter_t *bmc;
1571 unsigned long flags;
NeilBrownac2f40b2010-06-01 19:37:31 +10001572
1573 if (bitmap == NULL) {
NeilBrown32a76272005-06-21 17:17:14 -07001574 *blocks = 1024;
1575 return;
1576 }
NeilBrown40cffcc2012-05-22 13:55:24 +10001577 spin_lock_irqsave(&bitmap->counts.lock, flags);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001578 bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
NeilBrown32a76272005-06-21 17:17:14 -07001579 if (bmc == NULL)
1580 goto unlock;
1581 /* locked */
NeilBrown32a76272005-06-21 17:17:14 -07001582 if (RESYNC(*bmc)) {
1583 *bmc &= ~RESYNC_MASK;
1584
1585 if (!NEEDED(*bmc) && aborted)
1586 *bmc |= NEEDED_MASK;
1587 else {
NeilBrown2585f3e2011-09-21 15:37:46 +10001588 if (*bmc <= 2) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001589 md_bitmap_set_pending(&bitmap->counts, offset);
NeilBrown2585f3e2011-09-21 15:37:46 +10001590 bitmap->allclean = 0;
1591 }
NeilBrown32a76272005-06-21 17:17:14 -07001592 }
1593 }
1594 unlock:
NeilBrown40cffcc2012-05-22 13:55:24 +10001595 spin_unlock_irqrestore(&bitmap->counts.lock, flags);
NeilBrown32a76272005-06-21 17:17:14 -07001596}
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001597EXPORT_SYMBOL(md_bitmap_end_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001598
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001599void md_bitmap_close_sync(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001600{
1601 /* Sync has finished, and any bitmap chunks that weren't synced
1602 * properly have been aborted. It remains to us to clear the
1603 * RESYNC bit wherever it is still on
1604 */
1605 sector_t sector = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001606 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001607 if (!bitmap)
1608 return;
NeilBrown32a76272005-06-21 17:17:14 -07001609 while (sector < bitmap->mddev->resync_max_sectors) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001610 md_bitmap_end_sync(bitmap, sector, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001611 sector += blocks;
NeilBrown32a76272005-06-21 17:17:14 -07001612 }
1613}
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001614EXPORT_SYMBOL(md_bitmap_close_sync);
NeilBrown32a76272005-06-21 17:17:14 -07001615
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001616void md_bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector, bool force)
NeilBrownb47490c2008-02-06 01:39:50 -08001617{
1618 sector_t s = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11001619 sector_t blocks;
NeilBrownb47490c2008-02-06 01:39:50 -08001620
1621 if (!bitmap)
1622 return;
1623 if (sector == 0) {
1624 bitmap->last_end_sync = jiffies;
1625 return;
1626 }
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10001627 if (!force && time_before(jiffies, (bitmap->last_end_sync
NeilBrown1b04be92009-12-14 12:49:53 +11001628 + bitmap->mddev->bitmap_info.daemon_sleep)))
NeilBrownb47490c2008-02-06 01:39:50 -08001629 return;
1630 wait_event(bitmap->mddev->recovery_wait,
1631 atomic_read(&bitmap->mddev->recovery_active) == 0);
1632
NeilBrown75d3da42011-01-14 09:14:34 +11001633 bitmap->mddev->curr_resync_completed = sector;
Shaohua Li29530792016-12-08 15:48:19 -08001634 set_bit(MD_SB_CHANGE_CLEAN, &bitmap->mddev->sb_flags);
NeilBrown40cffcc2012-05-22 13:55:24 +10001635 sector &= ~((1ULL << bitmap->counts.chunkshift) - 1);
NeilBrownb47490c2008-02-06 01:39:50 -08001636 s = 0;
1637 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001638 md_bitmap_end_sync(bitmap, s, &blocks, 0);
NeilBrownb47490c2008-02-06 01:39:50 -08001639 s += blocks;
1640 }
1641 bitmap->last_end_sync = jiffies;
NeilBrownacb180b2009-04-14 16:28:34 +10001642 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
NeilBrownb47490c2008-02-06 01:39:50 -08001643}
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001644EXPORT_SYMBOL(md_bitmap_cond_end_sync);
NeilBrownb47490c2008-02-06 01:39:50 -08001645
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001646void md_bitmap_sync_with_cluster(struct mddev *mddev,
Guoqing Jiang18c9ff72016-05-02 11:50:12 -04001647 sector_t old_lo, sector_t old_hi,
1648 sector_t new_lo, sector_t new_hi)
1649{
1650 struct bitmap *bitmap = mddev->bitmap;
1651 sector_t sector, blocks = 0;
1652
1653 for (sector = old_lo; sector < new_lo; ) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001654 md_bitmap_end_sync(bitmap, sector, &blocks, 0);
Guoqing Jiang18c9ff72016-05-02 11:50:12 -04001655 sector += blocks;
1656 }
1657 WARN((blocks > new_lo) && old_lo, "alignment is not correct for lo\n");
1658
1659 for (sector = old_hi; sector < new_hi; ) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001660 md_bitmap_start_sync(bitmap, sector, &blocks, 0);
Guoqing Jiang18c9ff72016-05-02 11:50:12 -04001661 sector += blocks;
1662 }
1663 WARN((blocks > new_hi) && old_hi, "alignment is not correct for hi\n");
1664}
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001665EXPORT_SYMBOL(md_bitmap_sync_with_cluster);
Guoqing Jiang18c9ff72016-05-02 11:50:12 -04001666
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001667static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
NeilBrown32a76272005-06-21 17:17:14 -07001668{
1669 /* For each chunk covered by any of these sectors, set the
NeilBrownef99bf42012-05-22 13:55:08 +10001670 * counter to 2 and possibly set resync_needed. They should all
NeilBrown32a76272005-06-21 17:17:14 -07001671 * be 0 at this point
1672 */
NeilBrown193f1c92005-08-04 12:53:33 -07001673
NeilBrown57dab0b2010-10-19 10:03:39 +11001674 sector_t secs;
NeilBrown193f1c92005-08-04 12:53:33 -07001675 bitmap_counter_t *bmc;
NeilBrown40cffcc2012-05-22 13:55:24 +10001676 spin_lock_irq(&bitmap->counts.lock);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001677 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &secs, 1);
NeilBrown193f1c92005-08-04 12:53:33 -07001678 if (!bmc) {
NeilBrown40cffcc2012-05-22 13:55:24 +10001679 spin_unlock_irq(&bitmap->counts.lock);
NeilBrown193f1c92005-08-04 12:53:33 -07001680 return;
NeilBrown32a76272005-06-21 17:17:14 -07001681 }
NeilBrownac2f40b2010-06-01 19:37:31 +10001682 if (!*bmc) {
Goldwyn Rodrigues11dd35d2014-06-07 00:36:26 -05001683 *bmc = 2;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001684 md_bitmap_count_page(&bitmap->counts, offset, 1);
1685 md_bitmap_set_pending(&bitmap->counts, offset);
NeilBrown2585f3e2011-09-21 15:37:46 +10001686 bitmap->allclean = 0;
NeilBrown193f1c92005-08-04 12:53:33 -07001687 }
Goldwyn Rodrigues11dd35d2014-06-07 00:36:26 -05001688 if (needed)
1689 *bmc |= NEEDED_MASK;
NeilBrown40cffcc2012-05-22 13:55:24 +10001690 spin_unlock_irq(&bitmap->counts.lock);
NeilBrown32a76272005-06-21 17:17:14 -07001691}
1692
Paul Clements9b1d1da2006-10-03 01:15:49 -07001693/* dirty the memory and file bits for bitmap chunks "s" to "e" */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001694void md_bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
Paul Clements9b1d1da2006-10-03 01:15:49 -07001695{
1696 unsigned long chunk;
1697
1698 for (chunk = s; chunk <= e; chunk++) {
NeilBrown40cffcc2012-05-22 13:55:24 +10001699 sector_t sec = (sector_t)chunk << bitmap->counts.chunkshift;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001700 md_bitmap_set_memory_bits(bitmap, sec, 1);
1701 md_bitmap_file_set_bit(bitmap, sec);
NeilBrownffa23322009-12-14 12:49:56 +11001702 if (sec < bitmap->mddev->recovery_cp)
1703 /* We are asserting that the array is dirty,
1704 * so move the recovery_cp address back so
1705 * that it is obvious that it is dirty
1706 */
1707 bitmap->mddev->recovery_cp = sec;
Paul Clements9b1d1da2006-10-03 01:15:49 -07001708 }
1709}
1710
NeilBrown32a76272005-06-21 17:17:14 -07001711/*
NeilBrown6b8b3e82005-08-04 12:53:35 -07001712 * flush out any pending updates
1713 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001714void md_bitmap_flush(struct mddev *mddev)
NeilBrown6b8b3e82005-08-04 12:53:35 -07001715{
1716 struct bitmap *bitmap = mddev->bitmap;
NeilBrown42a04b52009-12-14 12:49:53 +11001717 long sleep;
NeilBrown6b8b3e82005-08-04 12:53:35 -07001718
1719 if (!bitmap) /* there was no bitmap */
1720 return;
1721
1722 /* run the daemon_work three time to ensure everything is flushed
1723 * that can be
1724 */
NeilBrown1b04be92009-12-14 12:49:53 +11001725 sleep = mddev->bitmap_info.daemon_sleep * 2;
NeilBrown42a04b52009-12-14 12:49:53 +11001726 bitmap->daemon_lastrun -= sleep;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001727 md_bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001728 bitmap->daemon_lastrun -= sleep;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001729 md_bitmap_daemon_work(mddev);
NeilBrown42a04b52009-12-14 12:49:53 +11001730 bitmap->daemon_lastrun -= sleep;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001731 md_bitmap_daemon_work(mddev);
1732 md_bitmap_update_sb(bitmap);
NeilBrown6b8b3e82005-08-04 12:53:35 -07001733}
1734
1735/*
NeilBrown32a76272005-06-21 17:17:14 -07001736 * free memory that was allocated
1737 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001738void md_bitmap_free(struct bitmap *bitmap)
NeilBrown32a76272005-06-21 17:17:14 -07001739{
1740 unsigned long k, pages;
1741 struct bitmap_page *bp;
NeilBrown32a76272005-06-21 17:17:14 -07001742
1743 if (!bitmap) /* there was no bitmap */
1744 return;
1745
Guoqing Jiangf9a67b12016-04-01 17:08:49 +08001746 if (bitmap->sysfs_can_clear)
1747 sysfs_put(bitmap->sysfs_can_clear);
1748
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05001749 if (mddev_is_clustered(bitmap->mddev) && bitmap->mddev->cluster_info &&
1750 bitmap->cluster_slot == md_cluster_ops->slot_number(bitmap->mddev))
Goldwyn Rodriguesb97e92572014-06-06 11:50:56 -05001751 md_cluster_stop(bitmap->mddev);
1752
NeilBrownfae7d322012-05-22 13:55:21 +10001753 /* Shouldn't be needed - but just in case.... */
1754 wait_event(bitmap->write_wait,
1755 atomic_read(&bitmap->pending_writes) == 0);
1756
1757 /* release the bitmap file */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001758 md_bitmap_file_unmap(&bitmap->storage);
NeilBrown32a76272005-06-21 17:17:14 -07001759
NeilBrown40cffcc2012-05-22 13:55:24 +10001760 bp = bitmap->counts.bp;
1761 pages = bitmap->counts.pages;
NeilBrown32a76272005-06-21 17:17:14 -07001762
1763 /* free all allocated memory */
1764
NeilBrown32a76272005-06-21 17:17:14 -07001765 if (bp) /* deallocate the page memory */
1766 for (k = 0; k < pages; k++)
1767 if (bp[k].map && !bp[k].hijacked)
1768 kfree(bp[k].map);
1769 kfree(bp);
1770 kfree(bitmap);
1771}
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001772EXPORT_SYMBOL(md_bitmap_free);
NeilBrownaa5cbd12009-12-14 12:49:46 +11001773
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001774void md_bitmap_wait_behind_writes(struct mddev *mddev)
Guoqing Jiang48df4982017-03-14 09:40:20 +08001775{
1776 struct bitmap *bitmap = mddev->bitmap;
1777
1778 /* wait for behind writes to complete */
1779 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
1780 pr_debug("md:%s: behind writes in progress - waiting to stop.\n",
1781 mdname(mddev));
1782 /* need to kick something here to make sure I/O goes? */
1783 wait_event(bitmap->behind_wait,
1784 atomic_read(&bitmap->behind_writes) == 0);
1785 }
1786}
1787
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001788void md_bitmap_destroy(struct mddev *mddev)
NeilBrown3178b0d2005-09-09 16:23:50 -07001789{
1790 struct bitmap *bitmap = mddev->bitmap;
1791
1792 if (!bitmap) /* there was no bitmap */
1793 return;
1794
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001795 md_bitmap_wait_behind_writes(mddev);
Guoqing Jiang69b00b52019-12-23 10:49:00 +01001796 if (!mddev->serialize_policy)
1797 mddev_destroy_serial_pool(mddev, NULL, true);
Guoqing Jiang48df4982017-03-14 09:40:20 +08001798
NeilBrownc3d97142009-12-14 12:49:52 +11001799 mutex_lock(&mddev->bitmap_info.mutex);
NeilBrown978a7a42014-12-15 12:56:58 +11001800 spin_lock(&mddev->lock);
NeilBrown3178b0d2005-09-09 16:23:50 -07001801 mddev->bitmap = NULL; /* disconnect from the md device */
NeilBrown978a7a42014-12-15 12:56:58 +11001802 spin_unlock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11001803 mutex_unlock(&mddev->bitmap_info.mutex);
NeilBrownb15c2e52006-01-06 00:20:16 -08001804 if (mddev->thread)
1805 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown3178b0d2005-09-09 16:23:50 -07001806
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001807 md_bitmap_free(bitmap);
NeilBrown3178b0d2005-09-09 16:23:50 -07001808}
NeilBrown32a76272005-06-21 17:17:14 -07001809
1810/*
1811 * initialize the bitmap structure
1812 * if this returns an error, bitmap_destroy must be called to do clean up
Guoqing Jiangf9a67b12016-04-01 17:08:49 +08001813 * once mddev->bitmap is set
NeilBrown32a76272005-06-21 17:17:14 -07001814 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001815struct bitmap *md_bitmap_create(struct mddev *mddev, int slot)
NeilBrown32a76272005-06-21 17:17:14 -07001816{
1817 struct bitmap *bitmap;
NeilBrown1f593902009-04-20 11:50:24 +10001818 sector_t blocks = mddev->resync_max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001819 struct file *file = mddev->bitmap_info.file;
NeilBrown32a76272005-06-21 17:17:14 -07001820 int err;
Tejun Heo324a56e2013-12-11 14:11:53 -05001821 struct kernfs_node *bm = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07001822
Alexey Dobriyan5f6e3c832006-10-11 01:22:26 -07001823 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
NeilBrown32a76272005-06-21 17:17:14 -07001824
NeilBrownc3d97142009-12-14 12:49:52 +11001825 BUG_ON(file && mddev->bitmap_info.offset);
NeilBrowna654b9d82005-06-21 17:17:27 -07001826
NeilBrown230b55f2017-10-17 14:24:09 +11001827 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
1828 pr_notice("md/raid:%s: array with journal cannot have bitmap\n",
1829 mdname(mddev));
1830 return ERR_PTR(-EBUSY);
1831 }
1832
NeilBrown9ffae0c2006-01-06 00:20:32 -08001833 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
NeilBrown32a76272005-06-21 17:17:14 -07001834 if (!bitmap)
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05001835 return ERR_PTR(-ENOMEM);
NeilBrown32a76272005-06-21 17:17:14 -07001836
NeilBrown40cffcc2012-05-22 13:55:24 +10001837 spin_lock_init(&bitmap->counts.lock);
NeilBrownce25c312006-06-26 00:27:49 -07001838 atomic_set(&bitmap->pending_writes, 0);
1839 init_waitqueue_head(&bitmap->write_wait);
Neil Brownda6e1a32007-02-08 14:20:37 -08001840 init_waitqueue_head(&bitmap->overflow_wait);
NeilBrowne5551902010-03-31 11:21:44 +11001841 init_waitqueue_head(&bitmap->behind_wait);
NeilBrownce25c312006-06-26 00:27:49 -07001842
NeilBrown32a76272005-06-21 17:17:14 -07001843 bitmap->mddev = mddev;
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05001844 bitmap->cluster_slot = slot;
NeilBrown32a76272005-06-21 17:17:14 -07001845
NeilBrown5ff5aff2010-06-01 19:37:32 +10001846 if (mddev->kobj.sd)
Tejun Heo388975c2013-09-11 23:19:13 -04001847 bm = sysfs_get_dirent(mddev->kobj.sd, "bitmap");
NeilBrownece5cff2009-12-14 12:49:56 +11001848 if (bm) {
Tejun Heo388975c2013-09-11 23:19:13 -04001849 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, "can_clear");
NeilBrownece5cff2009-12-14 12:49:56 +11001850 sysfs_put(bm);
1851 } else
1852 bitmap->sysfs_can_clear = NULL;
1853
NeilBrown1ec885c2012-05-22 13:55:10 +10001854 bitmap->storage.file = file;
NeilBrownce25c312006-06-26 00:27:49 -07001855 if (file) {
1856 get_file(file);
NeilBrownae8fa282009-10-16 15:56:01 +11001857 /* As future accesses to this file will use bmap,
1858 * and bypass the page cache, we must sync the file
1859 * first.
1860 */
Christoph Hellwig8018ab02010-03-22 17:32:25 +01001861 vfs_fsync(file, 1);
NeilBrownce25c312006-06-26 00:27:49 -07001862 }
NeilBrown42a04b52009-12-14 12:49:53 +11001863 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
Jonathan Brassow9c810752011-06-08 17:59:30 -05001864 if (!mddev->bitmap_info.external) {
1865 /*
1866 * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
1867 * instructing us to create a new on-disk bitmap instance.
1868 */
1869 if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001870 err = md_bitmap_new_disk_sb(bitmap);
Jonathan Brassow9c810752011-06-08 17:59:30 -05001871 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001872 err = md_bitmap_read_sb(bitmap);
Jonathan Brassow9c810752011-06-08 17:59:30 -05001873 } else {
NeilBrownece5cff2009-12-14 12:49:56 +11001874 err = 0;
1875 if (mddev->bitmap_info.chunksize == 0 ||
1876 mddev->bitmap_info.daemon_sleep == 0)
1877 /* chunksize and time_base need to be
1878 * set first. */
1879 err = -EINVAL;
1880 }
NeilBrown32a76272005-06-21 17:17:14 -07001881 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001882 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001883
NeilBrown624ce4f2009-12-14 12:49:56 +11001884 bitmap->daemon_lastrun = jiffies;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001885 err = md_bitmap_resize(bitmap, blocks, mddev->bitmap_info.chunksize, 1);
NeilBrownd60b4792012-05-22 13:55:25 +10001886 if (err)
NeilBrown3178b0d2005-09-09 16:23:50 -07001887 goto error;
NeilBrown32a76272005-06-21 17:17:14 -07001888
NeilBrownec0cc222016-11-02 14:16:49 +11001889 pr_debug("created bitmap (%lu pages) for device %s\n",
1890 bitmap->counts.pages, bmname(bitmap));
NeilBrown69e51b42010-06-01 19:37:35 +10001891
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05001892 err = test_bit(BITMAP_WRITE_ERROR, &bitmap->flags) ? -EIO : 0;
1893 if (err)
1894 goto error;
NeilBrown69e51b42010-06-01 19:37:35 +10001895
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05001896 return bitmap;
NeilBrown69e51b42010-06-01 19:37:35 +10001897 error:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001898 md_bitmap_free(bitmap);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05001899 return ERR_PTR(err);
NeilBrown69e51b42010-06-01 19:37:35 +10001900}
1901
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001902int md_bitmap_load(struct mddev *mddev)
NeilBrown69e51b42010-06-01 19:37:35 +10001903{
1904 int err = 0;
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001905 sector_t start = 0;
NeilBrown69e51b42010-06-01 19:37:35 +10001906 sector_t sector = 0;
1907 struct bitmap *bitmap = mddev->bitmap;
Guoqing Jiang617b1942019-06-14 17:10:38 +08001908 struct md_rdev *rdev;
NeilBrown69e51b42010-06-01 19:37:35 +10001909
1910 if (!bitmap)
1911 goto out;
1912
Guoqing Jiang617b1942019-06-14 17:10:38 +08001913 rdev_for_each(rdev, mddev)
Guoqing Jiang404659c2019-12-23 10:48:53 +01001914 mddev_create_serial_pool(mddev, rdev, true);
Guoqing Jiang617b1942019-06-14 17:10:38 +08001915
Guoqing Jiang51e453a2016-05-04 02:17:09 -04001916 if (mddev_is_clustered(mddev))
1917 md_cluster_ops->load_bitmaps(mddev, mddev->bitmap_info.nodes);
1918
NeilBrown69e51b42010-06-01 19:37:35 +10001919 /* Clear out old bitmap info first: Either there is none, or we
1920 * are resuming after someone else has possibly changed things,
1921 * so we should forget old cached info.
1922 * All chunks should be clean, but some might need_sync.
1923 */
1924 while (sector < mddev->resync_max_sectors) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001925 sector_t blocks;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001926 md_bitmap_start_sync(bitmap, sector, &blocks, 0);
NeilBrown69e51b42010-06-01 19:37:35 +10001927 sector += blocks;
1928 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001929 md_bitmap_close_sync(bitmap);
NeilBrown69e51b42010-06-01 19:37:35 +10001930
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001931 if (mddev->degraded == 0
1932 || bitmap->events_cleared == mddev->events)
1933 /* no need to keep dirty bits to optimise a
1934 * re-add of a missing device */
1935 start = mddev->recovery_cp;
NeilBrown69e51b42010-06-01 19:37:35 +10001936
NeilBrownafbaa902012-04-12 16:05:06 +10001937 mutex_lock(&mddev->bitmap_info.mutex);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001938 err = md_bitmap_init_from_disk(bitmap, start);
NeilBrownafbaa902012-04-12 16:05:06 +10001939 mutex_unlock(&mddev->bitmap_info.mutex);
Jonathan Brassow3520fa42011-07-27 11:00:37 +10001940
NeilBrown32a76272005-06-21 17:17:14 -07001941 if (err)
NeilBrown69e51b42010-06-01 19:37:35 +10001942 goto out;
NeilBrownb405fe92012-05-22 13:55:15 +10001943 clear_bit(BITMAP_STALE, &bitmap->flags);
NeilBrownef99bf42012-05-22 13:55:08 +10001944
1945 /* Kick recovery in case any bits were set */
1946 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
NeilBrown3178b0d2005-09-09 16:23:50 -07001947
NeilBrown1b04be92009-12-14 12:49:53 +11001948 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
NeilBrown9cd30fd2009-12-14 12:49:54 +11001949 md_wakeup_thread(mddev->thread);
NeilBrownb15c2e52006-01-06 00:20:16 -08001950
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001951 md_bitmap_update_sb(bitmap);
NeilBrown4ad13662007-07-17 04:06:13 -07001952
NeilBrownb405fe92012-05-22 13:55:15 +10001953 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
NeilBrown69e51b42010-06-01 19:37:35 +10001954 err = -EIO;
1955out:
NeilBrown3178b0d2005-09-09 16:23:50 -07001956 return err;
NeilBrown32a76272005-06-21 17:17:14 -07001957}
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001958EXPORT_SYMBOL_GPL(md_bitmap_load);
NeilBrown32a76272005-06-21 17:17:14 -07001959
Guoqing Jiangb98938d2017-03-01 16:42:39 +08001960struct bitmap *get_bitmap_from_slot(struct mddev *mddev, int slot)
1961{
1962 int rv = 0;
1963 struct bitmap *bitmap;
1964
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001965 bitmap = md_bitmap_create(mddev, slot);
Guoqing Jiangb98938d2017-03-01 16:42:39 +08001966 if (IS_ERR(bitmap)) {
1967 rv = PTR_ERR(bitmap);
1968 return ERR_PTR(rv);
1969 }
1970
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001971 rv = md_bitmap_init_from_disk(bitmap, 0);
Guoqing Jiangb98938d2017-03-01 16:42:39 +08001972 if (rv) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001973 md_bitmap_free(bitmap);
Guoqing Jiangb98938d2017-03-01 16:42:39 +08001974 return ERR_PTR(rv);
1975 }
1976
1977 return bitmap;
1978}
1979EXPORT_SYMBOL(get_bitmap_from_slot);
1980
Goldwyn Rodrigues11dd35d2014-06-07 00:36:26 -05001981/* Loads the bitmap associated with slot and copies the resync information
1982 * to our bitmap
1983 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001984int md_bitmap_copy_from_slot(struct mddev *mddev, int slot,
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05001985 sector_t *low, sector_t *high, bool clear_bits)
Goldwyn Rodrigues11dd35d2014-06-07 00:36:26 -05001986{
1987 int rv = 0, i, j;
1988 sector_t block, lo = 0, hi = 0;
1989 struct bitmap_counts *counts;
Guoqing Jiangb98938d2017-03-01 16:42:39 +08001990 struct bitmap *bitmap;
Goldwyn Rodrigues11dd35d2014-06-07 00:36:26 -05001991
Guoqing Jiangb98938d2017-03-01 16:42:39 +08001992 bitmap = get_bitmap_from_slot(mddev, slot);
1993 if (IS_ERR(bitmap)) {
1994 pr_err("%s can't get bitmap from slot %d\n", __func__, slot);
1995 return -1;
1996 }
Goldwyn Rodrigues11dd35d2014-06-07 00:36:26 -05001997
1998 counts = &bitmap->counts;
1999 for (j = 0; j < counts->chunks; j++) {
2000 block = (sector_t)j << counts->chunkshift;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002001 if (md_bitmap_file_test_bit(bitmap, block)) {
Goldwyn Rodrigues11dd35d2014-06-07 00:36:26 -05002002 if (!lo)
2003 lo = block;
2004 hi = block;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002005 md_bitmap_file_clear_bit(bitmap, block);
2006 md_bitmap_set_memory_bits(mddev->bitmap, block, 1);
2007 md_bitmap_file_set_bit(mddev->bitmap, block);
Goldwyn Rodrigues11dd35d2014-06-07 00:36:26 -05002008 }
2009 }
2010
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002011 if (clear_bits) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002012 md_bitmap_update_sb(bitmap);
Guoqing Jiangc84400c2016-05-02 11:50:15 -04002013 /* BITMAP_PAGE_PENDING is set, but bitmap_unplug needs
2014 * BITMAP_PAGE_DIRTY or _NEEDWRITE to write ... */
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002015 for (i = 0; i < bitmap->storage.file_pages; i++)
Guoqing Jiangc84400c2016-05-02 11:50:15 -04002016 if (test_page_attr(bitmap, i, BITMAP_PAGE_PENDING))
2017 set_page_attr(bitmap, i, BITMAP_PAGE_NEEDWRITE);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002018 md_bitmap_unplug(bitmap);
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002019 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002020 md_bitmap_unplug(mddev->bitmap);
Goldwyn Rodrigues11dd35d2014-06-07 00:36:26 -05002021 *low = lo;
2022 *high = hi;
Guoqing Jiangb98938d2017-03-01 16:42:39 +08002023
Goldwyn Rodrigues11dd35d2014-06-07 00:36:26 -05002024 return rv;
2025}
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002026EXPORT_SYMBOL_GPL(md_bitmap_copy_from_slot);
Goldwyn Rodrigues11dd35d2014-06-07 00:36:26 -05002027
2028
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002029void md_bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
NeilBrown57148962012-03-19 12:46:40 +11002030{
2031 unsigned long chunk_kb;
NeilBrown40cffcc2012-05-22 13:55:24 +10002032 struct bitmap_counts *counts;
NeilBrown57148962012-03-19 12:46:40 +11002033
2034 if (!bitmap)
2035 return;
2036
NeilBrown40cffcc2012-05-22 13:55:24 +10002037 counts = &bitmap->counts;
2038
NeilBrown57148962012-03-19 12:46:40 +11002039 chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
2040 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
2041 "%lu%s chunk",
NeilBrown40cffcc2012-05-22 13:55:24 +10002042 counts->pages - counts->missing_pages,
2043 counts->pages,
2044 (counts->pages - counts->missing_pages)
NeilBrown57148962012-03-19 12:46:40 +11002045 << (PAGE_SHIFT - 10),
2046 chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
2047 chunk_kb ? "KB" : "B");
NeilBrown1ec885c2012-05-22 13:55:10 +10002048 if (bitmap->storage.file) {
NeilBrown57148962012-03-19 12:46:40 +11002049 seq_printf(seq, ", file: ");
Miklos Szeredi2726d562015-06-19 10:30:28 +02002050 seq_file_path(seq, bitmap->storage.file, " \t\n");
NeilBrown57148962012-03-19 12:46:40 +11002051 }
2052
2053 seq_printf(seq, "\n");
NeilBrown57148962012-03-19 12:46:40 +11002054}
2055
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002056int md_bitmap_resize(struct bitmap *bitmap, sector_t blocks,
NeilBrownd60b4792012-05-22 13:55:25 +10002057 int chunksize, int init)
2058{
2059 /* If chunk_size is 0, choose an appropriate chunk size.
2060 * Then possibly allocate new storage space.
2061 * Then quiesce, copy bits, replace bitmap, and re-start
2062 *
2063 * This function is called both to set up the initial bitmap
2064 * and to resize the bitmap while the array is active.
2065 * If this happens as a result of the array being resized,
2066 * chunksize will be zero, and we need to choose a suitable
2067 * chunksize, otherwise we use what we are given.
2068 */
2069 struct bitmap_storage store;
2070 struct bitmap_counts old_counts;
2071 unsigned long chunks;
2072 sector_t block;
2073 sector_t old_blocks, new_blocks;
2074 int chunkshift;
2075 int ret = 0;
2076 long pages;
2077 struct bitmap_page *new_bp;
2078
NeilBrowne8a27f82017-08-31 10:23:25 +10002079 if (bitmap->storage.file && !init) {
2080 pr_info("md: cannot resize file-based bitmap\n");
2081 return -EINVAL;
2082 }
2083
NeilBrownd60b4792012-05-22 13:55:25 +10002084 if (chunksize == 0) {
2085 /* If there is enough space, leave the chunk size unchanged,
2086 * else increase by factor of two until there is enough space.
2087 */
2088 long bytes;
2089 long space = bitmap->mddev->bitmap_info.space;
2090
2091 if (space == 0) {
2092 /* We don't know how much space there is, so limit
2093 * to current size - in sectors.
2094 */
2095 bytes = DIV_ROUND_UP(bitmap->counts.chunks, 8);
2096 if (!bitmap->mddev->bitmap_info.external)
2097 bytes += sizeof(bitmap_super_t);
2098 space = DIV_ROUND_UP(bytes, 512);
2099 bitmap->mddev->bitmap_info.space = space;
2100 }
2101 chunkshift = bitmap->counts.chunkshift;
2102 chunkshift--;
2103 do {
2104 /* 'chunkshift' is shift from block size to chunk size */
2105 chunkshift++;
2106 chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
2107 bytes = DIV_ROUND_UP(chunks, 8);
2108 if (!bitmap->mddev->bitmap_info.external)
2109 bytes += sizeof(bitmap_super_t);
2110 } while (bytes > (space << 9));
2111 } else
2112 chunkshift = ffz(~chunksize) - BITMAP_BLOCK_SHIFT;
2113
2114 chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
2115 memset(&store, 0, sizeof(store));
2116 if (bitmap->mddev->bitmap_info.offset || bitmap->mddev->bitmap_info.file)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002117 ret = md_bitmap_storage_alloc(&store, chunks,
2118 !bitmap->mddev->bitmap_info.external,
2119 mddev_is_clustered(bitmap->mddev)
2120 ? bitmap->cluster_slot : 0);
Guoqing Jiangcbb38732016-10-31 10:19:00 +08002121 if (ret) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002122 md_bitmap_file_unmap(&store);
NeilBrownd60b4792012-05-22 13:55:25 +10002123 goto err;
Guoqing Jiangcbb38732016-10-31 10:19:00 +08002124 }
NeilBrownd60b4792012-05-22 13:55:25 +10002125
2126 pages = DIV_ROUND_UP(chunks, PAGE_COUNTER_RATIO);
2127
Kees Cook6396bb22018-06-12 14:03:40 -07002128 new_bp = kcalloc(pages, sizeof(*new_bp), GFP_KERNEL);
NeilBrownd60b4792012-05-22 13:55:25 +10002129 ret = -ENOMEM;
2130 if (!new_bp) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002131 md_bitmap_file_unmap(&store);
NeilBrownd60b4792012-05-22 13:55:25 +10002132 goto err;
2133 }
2134
2135 if (!init)
2136 bitmap->mddev->pers->quiesce(bitmap->mddev, 1);
2137
2138 store.file = bitmap->storage.file;
2139 bitmap->storage.file = NULL;
2140
2141 if (store.sb_page && bitmap->storage.sb_page)
2142 memcpy(page_address(store.sb_page),
2143 page_address(bitmap->storage.sb_page),
Shaohua Li938b5332017-10-16 19:03:44 -07002144 sizeof(bitmap_super_t));
Guoqing Jiangfadcbd22019-09-26 13:53:50 +02002145 spin_lock_irq(&bitmap->counts.lock);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002146 md_bitmap_file_unmap(&bitmap->storage);
NeilBrownd60b4792012-05-22 13:55:25 +10002147 bitmap->storage = store;
2148
2149 old_counts = bitmap->counts;
2150 bitmap->counts.bp = new_bp;
2151 bitmap->counts.pages = pages;
2152 bitmap->counts.missing_pages = pages;
2153 bitmap->counts.chunkshift = chunkshift;
2154 bitmap->counts.chunks = chunks;
2155 bitmap->mddev->bitmap_info.chunksize = 1 << (chunkshift +
2156 BITMAP_BLOCK_SHIFT);
2157
2158 blocks = min(old_counts.chunks << old_counts.chunkshift,
2159 chunks << chunkshift);
2160
Guoqing Jiangc9d65032016-05-02 11:50:11 -04002161 /* For cluster raid, need to pre-allocate bitmap */
2162 if (mddev_is_clustered(bitmap->mddev)) {
2163 unsigned long page;
2164 for (page = 0; page < pages; page++) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002165 ret = md_bitmap_checkpage(&bitmap->counts, page, 1, 1);
Guoqing Jiangc9d65032016-05-02 11:50:11 -04002166 if (ret) {
2167 unsigned long k;
2168
2169 /* deallocate the page memory */
2170 for (k = 0; k < page; k++) {
kbuild test robotbc47e842016-05-02 11:50:16 -04002171 kfree(new_bp[k].map);
Guoqing Jiangc9d65032016-05-02 11:50:11 -04002172 }
Zdenek Kabelac0868b992017-11-08 13:44:56 +01002173 kfree(new_bp);
Guoqing Jiangc9d65032016-05-02 11:50:11 -04002174
2175 /* restore some fields from old_counts */
2176 bitmap->counts.bp = old_counts.bp;
2177 bitmap->counts.pages = old_counts.pages;
2178 bitmap->counts.missing_pages = old_counts.pages;
2179 bitmap->counts.chunkshift = old_counts.chunkshift;
2180 bitmap->counts.chunks = old_counts.chunks;
2181 bitmap->mddev->bitmap_info.chunksize = 1 << (old_counts.chunkshift +
2182 BITMAP_BLOCK_SHIFT);
2183 blocks = old_counts.chunks << old_counts.chunkshift;
NeilBrownec0cc222016-11-02 14:16:49 +11002184 pr_warn("Could not pre-allocate in-memory bitmap for cluster raid\n");
Guoqing Jiangc9d65032016-05-02 11:50:11 -04002185 break;
2186 } else
2187 bitmap->counts.bp[page].count += 1;
2188 }
2189 }
2190
NeilBrownd60b4792012-05-22 13:55:25 +10002191 for (block = 0; block < blocks; ) {
2192 bitmap_counter_t *bmc_old, *bmc_new;
2193 int set;
2194
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002195 bmc_old = md_bitmap_get_counter(&old_counts, block, &old_blocks, 0);
NeilBrownd60b4792012-05-22 13:55:25 +10002196 set = bmc_old && NEEDED(*bmc_old);
2197
2198 if (set) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002199 bmc_new = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1);
NeilBrownd60b4792012-05-22 13:55:25 +10002200 if (*bmc_new == 0) {
2201 /* need to set on-disk bits too. */
2202 sector_t end = block + new_blocks;
2203 sector_t start = block >> chunkshift;
2204 start <<= chunkshift;
2205 while (start < end) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002206 md_bitmap_file_set_bit(bitmap, block);
NeilBrownd60b4792012-05-22 13:55:25 +10002207 start += 1 << chunkshift;
2208 }
2209 *bmc_new = 2;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002210 md_bitmap_count_page(&bitmap->counts, block, 1);
2211 md_bitmap_set_pending(&bitmap->counts, block);
NeilBrownd60b4792012-05-22 13:55:25 +10002212 }
2213 *bmc_new |= NEEDED_MASK;
2214 if (new_blocks < old_blocks)
2215 old_blocks = new_blocks;
2216 }
2217 block += old_blocks;
2218 }
2219
Zdenek Kabelac0868b992017-11-08 13:44:56 +01002220 if (bitmap->counts.bp != old_counts.bp) {
2221 unsigned long k;
2222 for (k = 0; k < old_counts.pages; k++)
2223 if (!old_counts.bp[k].hijacked)
2224 kfree(old_counts.bp[k].map);
2225 kfree(old_counts.bp);
2226 }
2227
NeilBrownd60b4792012-05-22 13:55:25 +10002228 if (!init) {
2229 int i;
2230 while (block < (chunks << chunkshift)) {
2231 bitmap_counter_t *bmc;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002232 bmc = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1);
NeilBrownd60b4792012-05-22 13:55:25 +10002233 if (bmc) {
2234 /* new space. It needs to be resynced, so
2235 * we set NEEDED_MASK.
2236 */
2237 if (*bmc == 0) {
2238 *bmc = NEEDED_MASK | 2;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002239 md_bitmap_count_page(&bitmap->counts, block, 1);
2240 md_bitmap_set_pending(&bitmap->counts, block);
NeilBrownd60b4792012-05-22 13:55:25 +10002241 }
2242 }
2243 block += new_blocks;
2244 }
2245 for (i = 0; i < bitmap->storage.file_pages; i++)
2246 set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
2247 }
2248 spin_unlock_irq(&bitmap->counts.lock);
2249
2250 if (!init) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002251 md_bitmap_unplug(bitmap);
NeilBrownd60b4792012-05-22 13:55:25 +10002252 bitmap->mddev->pers->quiesce(bitmap->mddev, 0);
2253 }
2254 ret = 0;
2255err:
2256 return ret;
2257}
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002258EXPORT_SYMBOL_GPL(md_bitmap_resize);
NeilBrownd60b4792012-05-22 13:55:25 +10002259
NeilBrown43a70502009-12-14 12:49:55 +11002260static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002261location_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002262{
2263 ssize_t len;
NeilBrownac2f40b2010-06-01 19:37:31 +10002264 if (mddev->bitmap_info.file)
NeilBrown43a70502009-12-14 12:49:55 +11002265 len = sprintf(page, "file");
NeilBrownac2f40b2010-06-01 19:37:31 +10002266 else if (mddev->bitmap_info.offset)
NeilBrown43a70502009-12-14 12:49:55 +11002267 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
NeilBrownac2f40b2010-06-01 19:37:31 +10002268 else
NeilBrown43a70502009-12-14 12:49:55 +11002269 len = sprintf(page, "none");
2270 len += sprintf(page+len, "\n");
2271 return len;
2272}
2273
2274static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002275location_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002276{
Shaohua Lid9dd26b2016-07-30 10:05:31 -07002277 int rv;
NeilBrown43a70502009-12-14 12:49:55 +11002278
Shaohua Lid9dd26b2016-07-30 10:05:31 -07002279 rv = mddev_lock(mddev);
2280 if (rv)
2281 return rv;
NeilBrown43a70502009-12-14 12:49:55 +11002282 if (mddev->pers) {
Shaohua Lid9dd26b2016-07-30 10:05:31 -07002283 if (!mddev->pers->quiesce) {
2284 rv = -EBUSY;
2285 goto out;
2286 }
2287 if (mddev->recovery || mddev->sync_thread) {
2288 rv = -EBUSY;
2289 goto out;
2290 }
NeilBrown43a70502009-12-14 12:49:55 +11002291 }
2292
2293 if (mddev->bitmap || mddev->bitmap_info.file ||
2294 mddev->bitmap_info.offset) {
2295 /* bitmap already configured. Only option is to clear it */
Shaohua Lid9dd26b2016-07-30 10:05:31 -07002296 if (strncmp(buf, "none", 4) != 0) {
2297 rv = -EBUSY;
2298 goto out;
2299 }
NeilBrown43a70502009-12-14 12:49:55 +11002300 if (mddev->pers) {
Jack Wangf8f83d82018-10-08 17:24:03 +02002301 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002302 md_bitmap_destroy(mddev);
Jack Wangf8f83d82018-10-08 17:24:03 +02002303 mddev_resume(mddev);
NeilBrown43a70502009-12-14 12:49:55 +11002304 }
2305 mddev->bitmap_info.offset = 0;
2306 if (mddev->bitmap_info.file) {
2307 struct file *f = mddev->bitmap_info.file;
2308 mddev->bitmap_info.file = NULL;
NeilBrown43a70502009-12-14 12:49:55 +11002309 fput(f);
2310 }
2311 } else {
2312 /* No bitmap, OK to set a location */
2313 long long offset;
2314 if (strncmp(buf, "none", 4) == 0)
2315 /* nothing to be done */;
2316 else if (strncmp(buf, "file:", 5) == 0) {
2317 /* Not supported yet */
Shaohua Lid9dd26b2016-07-30 10:05:31 -07002318 rv = -EINVAL;
2319 goto out;
NeilBrown43a70502009-12-14 12:49:55 +11002320 } else {
NeilBrown43a70502009-12-14 12:49:55 +11002321 if (buf[0] == '+')
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002322 rv = kstrtoll(buf+1, 10, &offset);
NeilBrown43a70502009-12-14 12:49:55 +11002323 else
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002324 rv = kstrtoll(buf, 10, &offset);
NeilBrown43a70502009-12-14 12:49:55 +11002325 if (rv)
Shaohua Lid9dd26b2016-07-30 10:05:31 -07002326 goto out;
2327 if (offset == 0) {
2328 rv = -EINVAL;
2329 goto out;
2330 }
NeilBrownece5cff2009-12-14 12:49:56 +11002331 if (mddev->bitmap_info.external == 0 &&
2332 mddev->major_version == 0 &&
Shaohua Lid9dd26b2016-07-30 10:05:31 -07002333 offset != mddev->bitmap_info.default_offset) {
2334 rv = -EINVAL;
2335 goto out;
2336 }
NeilBrown43a70502009-12-14 12:49:55 +11002337 mddev->bitmap_info.offset = offset;
2338 if (mddev->pers) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05002339 struct bitmap *bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002340 bitmap = md_bitmap_create(mddev, -1);
Jack Wangf8f83d82018-10-08 17:24:03 +02002341 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05002342 if (IS_ERR(bitmap))
2343 rv = PTR_ERR(bitmap);
2344 else {
2345 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002346 rv = md_bitmap_load(mddev);
Guoqing Jiangf9a67b12016-04-01 17:08:49 +08002347 if (rv)
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05002348 mddev->bitmap_info.offset = 0;
NeilBrown43a70502009-12-14 12:49:55 +11002349 }
Guoqing Jiangf9a67b12016-04-01 17:08:49 +08002350 if (rv) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002351 md_bitmap_destroy(mddev);
Jack Wangf8f83d82018-10-08 17:24:03 +02002352 mddev_resume(mddev);
Shaohua Lid9dd26b2016-07-30 10:05:31 -07002353 goto out;
Guoqing Jiangf9a67b12016-04-01 17:08:49 +08002354 }
Jack Wangf8f83d82018-10-08 17:24:03 +02002355 mddev_resume(mddev);
NeilBrown43a70502009-12-14 12:49:55 +11002356 }
2357 }
2358 }
2359 if (!mddev->external) {
2360 /* Ensure new bitmap info is stored in
2361 * metadata promptly.
2362 */
Shaohua Li29530792016-12-08 15:48:19 -08002363 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown43a70502009-12-14 12:49:55 +11002364 md_wakeup_thread(mddev->thread);
2365 }
Shaohua Lid9dd26b2016-07-30 10:05:31 -07002366 rv = 0;
2367out:
2368 mddev_unlock(mddev);
2369 if (rv)
2370 return rv;
NeilBrown43a70502009-12-14 12:49:55 +11002371 return len;
2372}
2373
2374static struct md_sysfs_entry bitmap_location =
2375__ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
2376
NeilBrown6409bb02012-05-22 13:55:07 +10002377/* 'bitmap/space' is the space available at 'location' for the
2378 * bitmap. This allows the kernel to know when it is safe to
2379 * resize the bitmap to match a resized array.
2380 */
2381static ssize_t
2382space_show(struct mddev *mddev, char *page)
2383{
2384 return sprintf(page, "%lu\n", mddev->bitmap_info.space);
2385}
2386
2387static ssize_t
2388space_store(struct mddev *mddev, const char *buf, size_t len)
2389{
2390 unsigned long sectors;
2391 int rv;
2392
2393 rv = kstrtoul(buf, 10, &sectors);
2394 if (rv)
2395 return rv;
2396
2397 if (sectors == 0)
2398 return -EINVAL;
2399
2400 if (mddev->bitmap &&
NeilBrown9b1215c2012-05-22 13:55:11 +10002401 sectors < (mddev->bitmap->storage.bytes + 511) >> 9)
NeilBrown6409bb02012-05-22 13:55:07 +10002402 return -EFBIG; /* Bitmap is too big for this small space */
2403
2404 /* could make sure it isn't too big, but that isn't really
2405 * needed - user-space should be careful.
2406 */
2407 mddev->bitmap_info.space = sectors;
2408 return len;
2409}
2410
2411static struct md_sysfs_entry bitmap_space =
2412__ATTR(space, S_IRUGO|S_IWUSR, space_show, space_store);
2413
NeilBrown43a70502009-12-14 12:49:55 +11002414static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002415timeout_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002416{
2417 ssize_t len;
2418 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
2419 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
NeilBrownac2f40b2010-06-01 19:37:31 +10002420
NeilBrown43a70502009-12-14 12:49:55 +11002421 len = sprintf(page, "%lu", secs);
2422 if (jifs)
2423 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
2424 len += sprintf(page+len, "\n");
2425 return len;
2426}
2427
2428static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002429timeout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002430{
2431 /* timeout can be set at any time */
2432 unsigned long timeout;
2433 int rv = strict_strtoul_scaled(buf, &timeout, 4);
2434 if (rv)
2435 return rv;
2436
2437 /* just to make sure we don't overflow... */
2438 if (timeout >= LONG_MAX / HZ)
2439 return -EINVAL;
2440
2441 timeout = timeout * HZ / 10000;
2442
2443 if (timeout >= MAX_SCHEDULE_TIMEOUT)
2444 timeout = MAX_SCHEDULE_TIMEOUT-1;
2445 if (timeout < 1)
2446 timeout = 1;
2447 mddev->bitmap_info.daemon_sleep = timeout;
2448 if (mddev->thread) {
2449 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
2450 * the bitmap is all clean and we don't need to
2451 * adjust the timeout right now
2452 */
2453 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
2454 mddev->thread->timeout = timeout;
2455 md_wakeup_thread(mddev->thread);
2456 }
2457 }
2458 return len;
2459}
2460
2461static struct md_sysfs_entry bitmap_timeout =
2462__ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
2463
2464static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002465backlog_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002466{
2467 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
2468}
2469
2470static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002471backlog_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002472{
2473 unsigned long backlog;
Guoqing Jiang10c92fc2019-06-14 17:10:37 +08002474 unsigned long old_mwb = mddev->bitmap_info.max_write_behind;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002475 int rv = kstrtoul(buf, 10, &backlog);
NeilBrown43a70502009-12-14 12:49:55 +11002476 if (rv)
2477 return rv;
2478 if (backlog > COUNTER_MAX)
2479 return -EINVAL;
2480 mddev->bitmap_info.max_write_behind = backlog;
Guoqing Jiang404659c2019-12-23 10:48:53 +01002481 if (!backlog && mddev->serial_info_pool) {
2482 /* serial_info_pool is not needed if backlog is zero */
Guoqing Jiang69b00b52019-12-23 10:49:00 +01002483 if (!mddev->serialize_policy)
2484 mddev_destroy_serial_pool(mddev, NULL, false);
Guoqing Jiang404659c2019-12-23 10:48:53 +01002485 } else if (backlog && !mddev->serial_info_pool) {
2486 /* serial_info_pool is needed since backlog is not zero */
Guoqing Jiang10c92fc2019-06-14 17:10:37 +08002487 struct md_rdev *rdev;
2488
2489 rdev_for_each(rdev, mddev)
Guoqing Jiang404659c2019-12-23 10:48:53 +01002490 mddev_create_serial_pool(mddev, rdev, false);
Guoqing Jiang10c92fc2019-06-14 17:10:37 +08002491 }
2492 if (old_mwb != backlog)
2493 md_bitmap_update_sb(mddev->bitmap);
NeilBrown43a70502009-12-14 12:49:55 +11002494 return len;
2495}
2496
2497static struct md_sysfs_entry bitmap_backlog =
2498__ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
2499
2500static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002501chunksize_show(struct mddev *mddev, char *page)
NeilBrown43a70502009-12-14 12:49:55 +11002502{
2503 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
2504}
2505
2506static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002507chunksize_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown43a70502009-12-14 12:49:55 +11002508{
2509 /* Can only be changed when no bitmap is active */
2510 int rv;
2511 unsigned long csize;
2512 if (mddev->bitmap)
2513 return -EBUSY;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002514 rv = kstrtoul(buf, 10, &csize);
NeilBrown43a70502009-12-14 12:49:55 +11002515 if (rv)
2516 return rv;
2517 if (csize < 512 ||
2518 !is_power_of_2(csize))
2519 return -EINVAL;
2520 mddev->bitmap_info.chunksize = csize;
2521 return len;
2522}
2523
2524static struct md_sysfs_entry bitmap_chunksize =
2525__ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
2526
NeilBrownfd01b882011-10-11 16:47:53 +11002527static ssize_t metadata_show(struct mddev *mddev, char *page)
NeilBrownece5cff2009-12-14 12:49:56 +11002528{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05002529 if (mddev_is_clustered(mddev))
2530 return sprintf(page, "clustered\n");
NeilBrownece5cff2009-12-14 12:49:56 +11002531 return sprintf(page, "%s\n", (mddev->bitmap_info.external
2532 ? "external" : "internal"));
2533}
2534
NeilBrownfd01b882011-10-11 16:47:53 +11002535static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownece5cff2009-12-14 12:49:56 +11002536{
2537 if (mddev->bitmap ||
2538 mddev->bitmap_info.file ||
2539 mddev->bitmap_info.offset)
2540 return -EBUSY;
2541 if (strncmp(buf, "external", 8) == 0)
2542 mddev->bitmap_info.external = 1;
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05002543 else if ((strncmp(buf, "internal", 8) == 0) ||
2544 (strncmp(buf, "clustered", 9) == 0))
NeilBrownece5cff2009-12-14 12:49:56 +11002545 mddev->bitmap_info.external = 0;
2546 else
2547 return -EINVAL;
2548 return len;
2549}
2550
2551static struct md_sysfs_entry bitmap_metadata =
2552__ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2553
NeilBrownfd01b882011-10-11 16:47:53 +11002554static ssize_t can_clear_show(struct mddev *mddev, char *page)
NeilBrownece5cff2009-12-14 12:49:56 +11002555{
2556 int len;
NeilBrownb7b17c92014-12-15 12:56:59 +11002557 spin_lock(&mddev->lock);
NeilBrownece5cff2009-12-14 12:49:56 +11002558 if (mddev->bitmap)
2559 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
2560 "false" : "true"));
2561 else
2562 len = sprintf(page, "\n");
NeilBrownb7b17c92014-12-15 12:56:59 +11002563 spin_unlock(&mddev->lock);
NeilBrownece5cff2009-12-14 12:49:56 +11002564 return len;
2565}
2566
NeilBrownfd01b882011-10-11 16:47:53 +11002567static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownece5cff2009-12-14 12:49:56 +11002568{
2569 if (mddev->bitmap == NULL)
2570 return -ENOENT;
2571 if (strncmp(buf, "false", 5) == 0)
2572 mddev->bitmap->need_sync = 1;
2573 else if (strncmp(buf, "true", 4) == 0) {
2574 if (mddev->degraded)
2575 return -EBUSY;
2576 mddev->bitmap->need_sync = 0;
2577 } else
2578 return -EINVAL;
2579 return len;
2580}
2581
2582static struct md_sysfs_entry bitmap_can_clear =
2583__ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
2584
Paul Clements696fcd52010-03-08 16:02:37 +11002585static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002586behind_writes_used_show(struct mddev *mddev, char *page)
Paul Clements696fcd52010-03-08 16:02:37 +11002587{
NeilBrownb7b17c92014-12-15 12:56:59 +11002588 ssize_t ret;
2589 spin_lock(&mddev->lock);
Paul Clements696fcd52010-03-08 16:02:37 +11002590 if (mddev->bitmap == NULL)
NeilBrownb7b17c92014-12-15 12:56:59 +11002591 ret = sprintf(page, "0\n");
2592 else
2593 ret = sprintf(page, "%lu\n",
2594 mddev->bitmap->behind_writes_used);
2595 spin_unlock(&mddev->lock);
2596 return ret;
Paul Clements696fcd52010-03-08 16:02:37 +11002597}
2598
2599static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11002600behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
Paul Clements696fcd52010-03-08 16:02:37 +11002601{
2602 if (mddev->bitmap)
2603 mddev->bitmap->behind_writes_used = 0;
2604 return len;
2605}
2606
2607static struct md_sysfs_entry max_backlog_used =
2608__ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2609 behind_writes_used_show, behind_writes_used_reset);
2610
NeilBrown43a70502009-12-14 12:49:55 +11002611static struct attribute *md_bitmap_attrs[] = {
2612 &bitmap_location.attr,
NeilBrown6409bb02012-05-22 13:55:07 +10002613 &bitmap_space.attr,
NeilBrown43a70502009-12-14 12:49:55 +11002614 &bitmap_timeout.attr,
2615 &bitmap_backlog.attr,
2616 &bitmap_chunksize.attr,
NeilBrownece5cff2009-12-14 12:49:56 +11002617 &bitmap_metadata.attr,
2618 &bitmap_can_clear.attr,
Paul Clements696fcd52010-03-08 16:02:37 +11002619 &max_backlog_used.attr,
NeilBrown43a70502009-12-14 12:49:55 +11002620 NULL
2621};
2622struct attribute_group md_bitmap_group = {
2623 .name = "bitmap",
2624 .attrs = md_bitmap_attrs,
2625};
2626