Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2 | /* |
| 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 |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * Still to do: |
| 15 | * |
| 16 | * flush after percent set rather than just time based. (maybe both). |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 19 | #include <linux/blkdev.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 20 | #include <linux/module.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 21 | #include <linux/errno.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/init.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 24 | #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> |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 30 | #include <linux/seq_file.h> |
NeilBrown | 581dbd9 | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 31 | #include <trace/events/block.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 32 | #include "md.h" |
Mike Snitzer | 935fe09 | 2017-10-10 17:02:41 -0400 | [diff] [blame] | 33 | #include "md-bitmap.h" |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 34 | |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 35 | static inline char *bmname(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 36 | { |
| 37 | return bitmap->mddev ? mdname(bitmap->mddev) : "mdX"; |
| 38 | } |
| 39 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 40 | /* |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 41 | * 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 50 | static int md_bitmap_checkpage(struct bitmap_counts *bitmap, |
| 51 | unsigned long page, int create, int no_hijack) |
NeilBrown | ee305ac | 2009-09-23 18:06:44 +1000 | [diff] [blame] | 52 | __releases(bitmap->lock) |
| 53 | __acquires(bitmap->lock) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 54 | { |
| 55 | unsigned char *mappage; |
| 56 | |
| 57 | if (page >= bitmap->pages) { |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 58 | /* This can happen if bitmap_start_sync goes beyond |
| 59 | * End-of-device while looking for a whole page. |
| 60 | * It is harmless. |
| 61 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 62 | return -EINVAL; |
| 63 | } |
| 64 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 65 | 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 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 74 | /* this page has not been allocated yet */ |
| 75 | |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 76 | spin_unlock_irq(&bitmap->lock); |
NeilBrown | d959014 | 2015-02-02 17:08:03 +1100 | [diff] [blame] | 77 | /* 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(); |
NeilBrown | 792a1d4 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 90 | mappage = kzalloc(PAGE_SIZE, GFP_NOIO); |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 91 | spin_lock_irq(&bitmap->lock); |
| 92 | |
| 93 | if (mappage == NULL) { |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 94 | pr_debug("md/bitmap: map page allocation failed, hijacking\n"); |
Guoqing Jiang | c9d6503 | 2016-05-02 11:50:11 -0400 | [diff] [blame] | 95 | /* We don't support hijack for cluster raid */ |
| 96 | if (no_hijack) |
| 97 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 98 | /* failed - set the hijacked flag so that we can use the |
| 99 | * pointer as a counter */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 100 | if (!bitmap->bp[page].map) |
| 101 | bitmap->bp[page].hijacked = 1; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 102 | } else if (bitmap->bp[page].map || |
| 103 | bitmap->bp[page].hijacked) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 104 | /* somebody beat us to getting the page */ |
NeilBrown | 792a1d4 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 105 | kfree(mappage); |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 106 | } 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--; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 112 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 116 | /* 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 119 | static void md_bitmap_checkfree(struct bitmap_counts *bitmap, unsigned long page) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 120 | { |
| 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; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 131 | } else { |
| 132 | /* normal case, free the page */ |
| 133 | ptr = bitmap->bp[page].map; |
| 134 | bitmap->bp[page].map = NULL; |
| 135 | bitmap->missing_pages++; |
NeilBrown | 792a1d4 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 136 | kfree(ptr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 137 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 138 | } |
| 139 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 140 | /* |
| 141 | * bitmap file handling - read and write the bitmap file and its superblock |
| 142 | */ |
| 143 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 144 | /* |
| 145 | * basic page I/O operations |
| 146 | */ |
| 147 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 148 | /* IO operations when bitmap is stored near all superblocks */ |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 149 | static int read_sb_page(struct mddev *mddev, loff_t offset, |
| 150 | struct page *page, |
| 151 | unsigned long index, int size) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 152 | { |
| 153 | /* choose a good rdev and read the page from there */ |
| 154 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 155 | struct md_rdev *rdev; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 156 | sector_t target; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 157 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 158 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 159 | if (! test_bit(In_sync, &rdev->flags) |
Guoqing Jiang | 4aaf7694 | 2017-07-04 11:20:30 +0800 | [diff] [blame] | 160 | || test_bit(Faulty, &rdev->flags) |
| 161 | || test_bit(Bitmap_sync, &rdev->flags)) |
NeilBrown | ab904d6 | 2005-09-09 16:23:52 -0700 | [diff] [blame] | 162 | continue; |
| 163 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 164 | target = offset + index * (PAGE_SIZE/512); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 165 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 166 | if (sync_page_io(rdev, target, |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 167 | roundup(size, bdev_logical_block_size(rdev->bdev)), |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 168 | page, REQ_OP_READ, 0, true)) { |
NeilBrown | ab904d6 | 2005-09-09 16:23:52 -0700 | [diff] [blame] | 169 | page->index = index; |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 170 | return 0; |
NeilBrown | ab904d6 | 2005-09-09 16:23:52 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 173 | return -EIO; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 174 | } |
| 175 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 176 | static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev) |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 177 | { |
| 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 Wang | fd17748 | 2012-10-11 13:43:21 +1100 | [diff] [blame] | 184 | * list_for_each_entry_continue_rcu. |
NeilBrown | 8532e34 | 2015-05-20 15:05:09 +1000 | [diff] [blame] | 185 | * |
| 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. |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 190 | */ |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 191 | rcu_read_lock(); |
| 192 | if (rdev == NULL) |
| 193 | /* start at the beginning */ |
NeilBrown | 8532e34 | 2015-05-20 15:05:09 +1000 | [diff] [blame] | 194 | rdev = list_entry(&mddev->disks, struct md_rdev, same_set); |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 195 | else { |
| 196 | /* release the previous rdev and start from there. */ |
| 197 | rdev_dec_pending(rdev, mddev); |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 198 | } |
Michael Wang | fd17748 | 2012-10-11 13:43:21 +1100 | [diff] [blame] | 199 | list_for_each_entry_continue_rcu(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 200 | if (rdev->raid_disk >= 0 && |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 201 | !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 | |
NeilBrown | ab6085c | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 212 | static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 213 | { |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 214 | struct md_rdev *rdev; |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 215 | struct block_device *bdev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 216 | struct mddev *mddev = bitmap->mddev; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 217 | struct bitmap_storage *store = &bitmap->storage; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 218 | |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 219 | restart: |
| 220 | rdev = NULL; |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 221 | while ((rdev = next_active_rdev(rdev, mddev)) != NULL) { |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 222 | int size = PAGE_SIZE; |
| 223 | loff_t offset = mddev->bitmap_info.offset; |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 224 | |
| 225 | bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev; |
| 226 | |
NeilBrown | 9b1215c | 2012-05-22 13:55:11 +1000 | [diff] [blame] | 227 | 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 Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 232 | bdev_logical_block_size(bdev)); |
NeilBrown | 9b1215c | 2012-05-22 13:55:11 +1000 | [diff] [blame] | 233 | } |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 234 | /* 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); |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 274 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 275 | |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 276 | if (wait && md_super_wait(mddev) < 0) |
| 277 | goto restart; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 278 | return 0; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 279 | |
| 280 | bad_alignment: |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 281 | return -EINVAL; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 284 | static void md_bitmap_file_kick(struct bitmap *bitmap); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 285 | /* |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 286 | * write out a page to a file |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 287 | */ |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 288 | static void write_page(struct bitmap *bitmap, struct page *page, int wait) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 289 | { |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 290 | struct buffer_head *bh; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 291 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 292 | if (bitmap->storage.file == NULL) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 293 | switch (write_sb_page(bitmap, page, wait)) { |
| 294 | case -EINVAL: |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 295 | set_bit(BITMAP_WRITE_ERROR, &bitmap->flags); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 296 | } |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 297 | } else { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 298 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 299 | bh = page_buffers(page); |
NeilBrown | c708443 | 2006-01-06 00:20:45 -0800 | [diff] [blame] | 300 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 301 | while (bh && bh->b_blocknr) { |
| 302 | atomic_inc(&bitmap->pending_writes); |
| 303 | set_buffer_locked(bh); |
| 304 | set_buffer_mapped(bh); |
Mike Christie | 2a222ca | 2016-06-05 14:31:43 -0500 | [diff] [blame] | 305 | submit_bh(REQ_OP_WRITE, REQ_SYNC, bh); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 306 | bh = bh->b_this_page; |
| 307 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 308 | |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 309 | if (wait) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 310 | wait_event(bitmap->write_wait, |
| 311 | atomic_read(&bitmap->pending_writes)==0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 312 | } |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 313 | if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 314 | md_bitmap_file_kick(bitmap); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 315 | } |
| 316 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 317 | static void end_bitmap_write(struct buffer_head *bh, int uptodate) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 318 | { |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 319 | struct bitmap *bitmap = bh->b_private; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 320 | |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 321 | if (!uptodate) |
| 322 | set_bit(BITMAP_WRITE_ERROR, &bitmap->flags); |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 323 | if (atomic_dec_and_test(&bitmap->pending_writes)) |
| 324 | wake_up(&bitmap->write_wait); |
| 325 | } |
| 326 | |
| 327 | /* copied from buffer.c */ |
| 328 | static void |
| 329 | __clear_page_buffers(struct page *page) |
| 330 | { |
| 331 | ClearPagePrivate(page); |
| 332 | set_page_private(page, 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 333 | put_page(page); |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 334 | } |
| 335 | static void free_buffers(struct page *page) |
| 336 | { |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 337 | struct buffer_head *bh; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 338 | |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 339 | if (!PagePrivate(page)) |
| 340 | return; |
| 341 | |
| 342 | bh = page_buffers(page); |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 343 | 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 | */ |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 359 | static int read_page(struct file *file, unsigned long index, |
| 360 | struct bitmap *bitmap, |
| 361 | unsigned long count, |
| 362 | struct page *page) |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 363 | { |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 364 | int ret = 0; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 365 | struct inode *inode = file_inode(file); |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 366 | struct buffer_head *bh; |
Carlos Maiolino | 30460e1 | 2020-01-09 14:30:41 +0100 | [diff] [blame] | 367 | sector_t block, blk_cur; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 368 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 369 | pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE, |
| 370 | (unsigned long long)index << PAGE_SHIFT); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 371 | |
Jens Axboe | 640ab98 | 2017-09-27 05:40:16 -0600 | [diff] [blame] | 372 | bh = alloc_page_buffers(page, 1<<inode->i_blkbits, false); |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 373 | if (!bh) { |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 374 | ret = -ENOMEM; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 375 | goto out; |
| 376 | } |
| 377 | attach_page_buffers(page, bh); |
Carlos Maiolino | 30460e1 | 2020-01-09 14:30:41 +0100 | [diff] [blame] | 378 | blk_cur = index << (PAGE_SHIFT - inode->i_blkbits); |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 379 | while (bh) { |
Carlos Maiolino | 30460e1 | 2020-01-09 14:30:41 +0100 | [diff] [blame] | 380 | block = blk_cur; |
| 381 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 382 | if (count == 0) |
| 383 | bh->b_blocknr = 0; |
| 384 | else { |
Carlos Maiolino | 30460e1 | 2020-01-09 14:30:41 +0100 | [diff] [blame] | 385 | ret = bmap(inode, &block); |
| 386 | if (ret || !block) { |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 387 | ret = -EINVAL; |
Carlos Maiolino | 30460e1 | 2020-01-09 14:30:41 +0100 | [diff] [blame] | 388 | bh->b_blocknr = 0; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 389 | goto out; |
| 390 | } |
Carlos Maiolino | 30460e1 | 2020-01-09 14:30:41 +0100 | [diff] [blame] | 391 | |
| 392 | bh->b_blocknr = block; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 393 | 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); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 398 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 399 | bh->b_end_io = end_bitmap_write; |
| 400 | bh->b_private = bitmap; |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 401 | atomic_inc(&bitmap->pending_writes); |
| 402 | set_buffer_locked(bh); |
| 403 | set_buffer_mapped(bh); |
Mike Christie | 2a222ca | 2016-06-05 14:31:43 -0500 | [diff] [blame] | 404 | submit_bh(REQ_OP_READ, 0, bh); |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 405 | } |
Carlos Maiolino | 30460e1 | 2020-01-09 14:30:41 +0100 | [diff] [blame] | 406 | blk_cur++; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 407 | bh = bh->b_this_page; |
| 408 | } |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 409 | page->index = index; |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 410 | |
| 411 | wait_event(bitmap->write_wait, |
| 412 | atomic_read(&bitmap->pending_writes)==0); |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 413 | if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 414 | ret = -EIO; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 415 | out: |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 416 | if (ret) |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 417 | pr_err("md: bitmap read error: (%dB @ %llu): %d\n", |
| 418 | (int)PAGE_SIZE, |
| 419 | (unsigned long long)index << PAGE_SHIFT, |
| 420 | ret); |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 421 | return ret; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /* |
| 425 | * bitmap file superblock operations |
| 426 | */ |
| 427 | |
NeilBrown | 85c9ccd | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 428 | /* |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 429 | * md_bitmap_wait_writes() should be called before writing any bitmap |
NeilBrown | 85c9ccd | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 430 | * blocks, to ensure previous writes, particularly from |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 431 | * md_bitmap_daemon_work(), have completed. |
NeilBrown | 85c9ccd | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 432 | */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 433 | static void md_bitmap_wait_writes(struct bitmap *bitmap) |
NeilBrown | 85c9ccd | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 434 | { |
| 435 | if (bitmap->storage.file) |
| 436 | wait_event(bitmap->write_wait, |
| 437 | atomic_read(&bitmap->pending_writes)==0); |
| 438 | else |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 439 | /* 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 | */ |
NeilBrown | 85c9ccd | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 446 | md_super_wait(bitmap->mddev); |
| 447 | } |
| 448 | |
| 449 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 450 | /* update the event counter and sync the superblock to disk */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 451 | void md_bitmap_update_sb(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 452 | { |
| 453 | bitmap_super_t *sb; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 454 | |
| 455 | if (!bitmap || !bitmap->mddev) /* no bitmap for this array */ |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 456 | return; |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 457 | if (bitmap->mddev->bitmap_info.external) |
| 458 | return; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 459 | if (!bitmap->storage.sb_page) /* no superblock */ |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 460 | return; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 461 | sb = kmap_atomic(bitmap->storage.sb_page); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 462 | sb->events = cpu_to_le64(bitmap->mddev->events); |
NeilBrown | 8258c53 | 2011-05-11 14:26:30 +1000 | [diff] [blame] | 463 | if (bitmap->mddev->events < bitmap->events_cleared) |
Neil Brown | a0da84f | 2008-06-28 08:31:22 +1000 | [diff] [blame] | 464 | /* rocking back to read-only */ |
| 465 | bitmap->events_cleared = bitmap->mddev->events; |
NeilBrown | 8258c53 | 2011-05-11 14:26:30 +1000 | [diff] [blame] | 466 | sb->events_cleared = cpu_to_le64(bitmap->events_cleared); |
Hou Tao | 97f0eb9 | 2017-11-06 10:11:25 +0800 | [diff] [blame] | 467 | /* |
| 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)); |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 472 | /* 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); |
NeilBrown | b81a040 | 2012-05-22 13:55:26 +1000 | [diff] [blame] | 475 | /* 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 Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 478 | sb->nodes = cpu_to_le32(bitmap->mddev->bitmap_info.nodes); |
NeilBrown | 1dff2b8 | 2012-05-22 13:55:34 +1000 | [diff] [blame] | 479 | sb->sectors_reserved = cpu_to_le32(bitmap->mddev-> |
| 480 | bitmap_info.space); |
Cong Wang | b2f46e6 | 2011-11-28 13:25:44 +0800 | [diff] [blame] | 481 | kunmap_atomic(sb); |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 482 | write_page(bitmap, bitmap->storage.sb_page, 1); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 483 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 484 | EXPORT_SYMBOL(md_bitmap_update_sb); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 485 | |
| 486 | /* print out the bitmap file superblock */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 487 | void md_bitmap_print_sb(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 488 | { |
| 489 | bitmap_super_t *sb; |
| 490 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 491 | if (!bitmap || !bitmap->storage.sb_page) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 492 | return; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 493 | sb = kmap_atomic(bitmap->storage.sb_page); |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 494 | 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 Hellwig | c35403f | 2019-04-04 18:56:11 +0200 | [diff] [blame] | 498 | 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))); |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 502 | 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 Wang | b2f46e6 | 2011-11-28 13:25:44 +0800 | [diff] [blame] | 512 | kunmap_atomic(sb); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 515 | /* |
| 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 526 | static int md_bitmap_new_disk_sb(struct bitmap *bitmap) |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 527 | { |
| 528 | bitmap_super_t *sb; |
| 529 | unsigned long chunksize, daemon_sleep, write_behind; |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 530 | |
Goldwyn Rodrigues | d3b178a | 2015-07-22 12:09:17 -0500 | [diff] [blame] | 531 | bitmap->storage.sb_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
Jianpeng Ma | 582e2e0 | 2012-10-11 13:45:36 +1100 | [diff] [blame] | 532 | if (bitmap->storage.sb_page == NULL) |
| 533 | return -ENOMEM; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 534 | bitmap->storage.sb_page->index = 0; |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 535 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 536 | sb = kmap_atomic(bitmap->storage.sb_page); |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 537 | |
| 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 Wang | b2f46e6 | 2011-11-28 13:25:44 +0800 | [diff] [blame] | 544 | kunmap_atomic(sb); |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 545 | pr_warn("bitmap chunksize not a power of 2\n"); |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 546 | return -EINVAL; |
| 547 | } |
| 548 | sb->chunksize = cpu_to_le32(chunksize); |
| 549 | |
| 550 | daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep; |
Eric Engestrom | c97e060 | 2016-03-07 12:01:05 +0000 | [diff] [blame] | 551 | if (!daemon_sleep || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) { |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 552 | pr_debug("Choosing daemon_sleep default (5 sec)\n"); |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 553 | 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 | |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 573 | set_bit(BITMAP_STALE, &bitmap->flags); |
NeilBrown | 84e9234 | 2012-05-22 13:55:14 +1000 | [diff] [blame] | 574 | sb->state = cpu_to_le32(bitmap->flags); |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 575 | bitmap->events_cleared = bitmap->mddev->events; |
| 576 | sb->events_cleared = cpu_to_le64(bitmap->mddev->events); |
Goldwyn Rodrigues | d3b178a | 2015-07-22 12:09:17 -0500 | [diff] [blame] | 577 | bitmap->mddev->bitmap_info.nodes = 0; |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 578 | |
Cong Wang | b2f46e6 | 2011-11-28 13:25:44 +0800 | [diff] [blame] | 579 | kunmap_atomic(sb); |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 584 | /* read the superblock from the bitmap file and initialize some bitmap fields */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 585 | static int md_bitmap_read_sb(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 586 | { |
| 587 | char *reason = NULL; |
| 588 | bitmap_super_t *sb; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 589 | unsigned long chunksize, daemon_sleep, write_behind; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 590 | unsigned long long events; |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 591 | int nodes = 0; |
NeilBrown | 1dff2b8 | 2012-05-22 13:55:34 +1000 | [diff] [blame] | 592 | unsigned long sectors_reserved = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 593 | int err = -EINVAL; |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 594 | struct page *sb_page; |
Goldwyn Rodrigues | 33e38ac | 2015-07-01 12:19:56 +1000 | [diff] [blame] | 595 | loff_t offset = bitmap->mddev->bitmap_info.offset; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 596 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 597 | if (!bitmap->storage.file && !bitmap->mddev->bitmap_info.offset) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 598 | chunksize = 128 * 1024 * 1024; |
| 599 | daemon_sleep = 5 * HZ; |
| 600 | write_behind = 0; |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 601 | set_bit(BITMAP_STALE, &bitmap->flags); |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 602 | err = 0; |
| 603 | goto out_no_sb; |
| 604 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 605 | /* page 0 is the superblock, read it... */ |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 606 | sb_page = alloc_page(GFP_KERNEL); |
| 607 | if (!sb_page) |
| 608 | return -ENOMEM; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 609 | bitmap->storage.sb_page = sb_page; |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 610 | |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 611 | re_read: |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 612 | /* If cluster_slot is set, the cluster is setup */ |
| 613 | if (bitmap->cluster_slot >= 0) { |
Stephen Rothwell | 3b0e6aa | 2015-03-03 13:35:31 +1100 | [diff] [blame] | 614 | sector_t bm_blocks = bitmap->mddev->resync_max_sectors; |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 615 | |
Stephen Rothwell | 3b0e6aa | 2015-03-03 13:35:31 +1100 | [diff] [blame] | 616 | sector_div(bm_blocks, |
| 617 | bitmap->mddev->bitmap_info.chunksize >> 9); |
Goldwyn Rodrigues | 124eb76 | 2015-03-24 11:29:05 -0500 | [diff] [blame] | 618 | /* bits to bytes */ |
| 619 | bm_blocks = ((bm_blocks+7) >> 3) + sizeof(bitmap_super_t); |
| 620 | /* to 4k blocks */ |
NeilBrown | 935f3d4 | 2015-03-02 17:02:29 +1100 | [diff] [blame] | 621 | bm_blocks = DIV_ROUND_UP_SECTOR_T(bm_blocks, 4096); |
Goldwyn Rodrigues | 33e38ac | 2015-07-01 12:19:56 +1000 | [diff] [blame] | 622 | offset = bitmap->mddev->bitmap_info.offset + (bitmap->cluster_slot * (bm_blocks << 3)); |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 623 | pr_debug("%s:%d bm slot: %d offset: %llu\n", __func__, __LINE__, |
Goldwyn Rodrigues | 33e38ac | 2015-07-01 12:19:56 +1000 | [diff] [blame] | 624 | bitmap->cluster_slot, offset); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 625 | } |
| 626 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 627 | if (bitmap->storage.file) { |
| 628 | loff_t isize = i_size_read(bitmap->storage.file->f_mapping->host); |
NeilBrown | f49d5e6 | 2007-01-26 00:57:03 -0800 | [diff] [blame] | 629 | int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize; |
| 630 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 631 | err = read_page(bitmap->storage.file, 0, |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 632 | bitmap, bytes, sb_page); |
NeilBrown | f49d5e6 | 2007-01-26 00:57:03 -0800 | [diff] [blame] | 633 | } else { |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 634 | err = read_sb_page(bitmap->mddev, |
Goldwyn Rodrigues | 33e38ac | 2015-07-01 12:19:56 +1000 | [diff] [blame] | 635 | offset, |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 636 | sb_page, |
Shaohua Li | 938b533 | 2017-10-16 19:03:44 -0700 | [diff] [blame] | 637 | 0, sizeof(bitmap_super_t)); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 638 | } |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 639 | if (err) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 640 | return err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 641 | |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 642 | err = -EINVAL; |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 643 | sb = kmap_atomic(sb_page); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 644 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 645 | chunksize = le32_to_cpu(sb->chunksize); |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 646 | daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 647 | write_behind = le32_to_cpu(sb->write_behind); |
NeilBrown | 1dff2b8 | 2012-05-22 13:55:34 +1000 | [diff] [blame] | 648 | sectors_reserved = le32_to_cpu(sb->sectors_reserved); |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 649 | /* Setup nodes/clustername only if bitmap version is |
| 650 | * cluster-compatible |
Goldwyn Rodrigues | d3b178a | 2015-07-22 12:09:17 -0500 | [diff] [blame] | 651 | */ |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 652 | if (sb->version == cpu_to_le32(BITMAP_MAJOR_CLUSTERED)) { |
Goldwyn Rodrigues | d3b178a | 2015-07-22 12:09:17 -0500 | [diff] [blame] | 653 | nodes = le32_to_cpu(sb->nodes); |
| 654 | strlcpy(bitmap->mddev->bitmap_info.cluster_name, |
| 655 | sb->cluster_name, 64); |
| 656 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 657 | |
| 658 | /* verify that the bitmap-specific fields are valid */ |
| 659 | if (sb->magic != cpu_to_le32(BITMAP_MAGIC)) |
| 660 | reason = "bad magic"; |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 661 | else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO || |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 662 | le32_to_cpu(sb->version) > BITMAP_MAJOR_CLUSTERED) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 663 | reason = "unrecognized superblock version"; |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 664 | else if (chunksize < 512) |
NeilBrown | 7dd5d34 | 2006-01-06 00:20:39 -0800 | [diff] [blame] | 665 | reason = "bitmap chunksize too small"; |
Jonathan Brassow | d744540 | 2011-06-08 18:01:10 -0500 | [diff] [blame] | 666 | else if (!is_power_of_2(chunksize)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 667 | reason = "bitmap chunksize not a power of 2"; |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 668 | else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT) |
NeilBrown | 7dd5d34 | 2006-01-06 00:20:39 -0800 | [diff] [blame] | 669 | reason = "daemon sleep period out of range"; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 670 | else if (write_behind > COUNTER_MAX) |
| 671 | reason = "write-behind limit out of range (0 - 16383)"; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 672 | if (reason) { |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 673 | pr_warn("%s: invalid bitmap file superblock: %s\n", |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 674 | 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 | |
NeilBrown | 278c1ca | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 681 | 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)) { |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 687 | pr_warn("%s: bitmap superblock UUID mismatch\n", |
| 688 | bmname(bitmap)); |
NeilBrown | 278c1ca | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 689 | goto out; |
| 690 | } |
| 691 | events = le64_to_cpu(sb->events); |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 692 | if (!nodes && (events < bitmap->mddev->events)) { |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 693 | 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); |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 696 | set_bit(BITMAP_STALE, &bitmap->flags); |
NeilBrown | 278c1ca | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 697 | } |
| 698 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 699 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 700 | /* assign fields using values from superblock */ |
NeilBrown | 4f2e639 | 2006-10-21 10:24:09 -0700 | [diff] [blame] | 701 | bitmap->flags |= le32_to_cpu(sb->state); |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 702 | if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN) |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 703 | set_bit(BITMAP_HOSTENDIAN, &bitmap->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 704 | bitmap->events_cleared = le64_to_cpu(sb->events_cleared); |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 705 | strlcpy(bitmap->mddev->bitmap_info.cluster_name, sb->cluster_name, 64); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 706 | err = 0; |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 707 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 708 | out: |
Cong Wang | b2f46e6 | 2011-11-28 13:25:44 +0800 | [diff] [blame] | 709 | kunmap_atomic(sb); |
Zhilong Liu | 3560741e | 2017-03-15 16:14:53 +0800 | [diff] [blame] | 710 | /* Assigning chunksize is required for "re_read" */ |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 711 | bitmap->mddev->bitmap_info.chunksize = chunksize; |
Goldwyn Rodrigues | f735727 | 2015-07-22 12:09:16 -0500 | [diff] [blame] | 712 | if (err == 0 && nodes && (bitmap->cluster_slot < 0)) { |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 713 | err = md_setup_cluster(bitmap->mddev, nodes); |
| 714 | if (err) { |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 715 | pr_warn("%s: Could not setup cluster service (%d)\n", |
| 716 | bmname(bitmap), err); |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 717 | goto out_no_sb; |
| 718 | } |
| 719 | bitmap->cluster_slot = md_cluster_ops->slot_number(bitmap->mddev); |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 720 | goto re_read; |
| 721 | } |
| 722 | |
| 723 | |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 724 | out_no_sb: |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 725 | if (test_bit(BITMAP_STALE, &bitmap->flags)) |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 726 | 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 Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 730 | bitmap->mddev->bitmap_info.nodes = nodes; |
NeilBrown | 1dff2b8 | 2012-05-22 13:55:34 +1000 | [diff] [blame] | 731 | if (bitmap->mddev->bitmap_info.space == 0 || |
| 732 | bitmap->mddev->bitmap_info.space > sectors_reserved) |
| 733 | bitmap->mddev->bitmap_info.space = sectors_reserved; |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 734 | if (err) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 735 | md_bitmap_print_sb(bitmap); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 736 | if (bitmap->cluster_slot < 0) |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 737 | md_cluster_stop(bitmap->mddev); |
| 738 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 739 | return err; |
| 740 | } |
| 741 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 742 | /* |
| 743 | * general bitmap file operations |
| 744 | */ |
| 745 | |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 746 | /* |
| 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 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 752 | /* calculate the index of the page that contains this bit */ |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 753 | static inline unsigned long file_page_index(struct bitmap_storage *store, |
| 754 | unsigned long chunk) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 755 | { |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 756 | if (store->sb_page) |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 757 | chunk += sizeof(bitmap_super_t) << 3; |
| 758 | return chunk >> PAGE_BIT_SHIFT; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /* calculate the (bit) offset of this bit within a page */ |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 762 | static inline unsigned long file_page_offset(struct bitmap_storage *store, |
| 763 | unsigned long chunk) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 764 | { |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 765 | if (store->sb_page) |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 766 | chunk += sizeof(bitmap_super_t) << 3; |
| 767 | return chunk & (PAGE_BITS - 1); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /* |
| 771 | * return a pointer to the page in the filemap that contains the given bit |
| 772 | * |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 773 | */ |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 774 | static inline struct page *filemap_get_page(struct bitmap_storage *store, |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 775 | unsigned long chunk) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 776 | { |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 777 | if (file_page_index(store, chunk) >= store->file_pages) |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 778 | return NULL; |
NeilBrown | f2e06c5 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 779 | return store->filemap[file_page_index(store, chunk)]; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 782 | static int md_bitmap_storage_alloc(struct bitmap_storage *store, |
| 783 | unsigned long chunks, int with_super, |
| 784 | int slot_number) |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 785 | { |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 786 | int pnum, offset = 0; |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 787 | 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 Jiang | 7f86ffe | 2016-05-02 11:50:13 -0400 | [diff] [blame] | 795 | offset = slot_number * num_pages; |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 796 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 797 | store->filemap = kmalloc_array(num_pages, sizeof(struct page *), |
| 798 | GFP_KERNEL); |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 799 | if (!store->filemap) |
| 800 | return -ENOMEM; |
| 801 | |
| 802 | if (with_super && !store->sb_page) { |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 803 | store->sb_page = alloc_page(GFP_KERNEL|__GFP_ZERO); |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 804 | if (store->sb_page == NULL) |
| 805 | return -ENOMEM; |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 806 | } |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 807 | |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 808 | pnum = 0; |
| 809 | if (store->sb_page) { |
| 810 | store->filemap[0] = store->sb_page; |
| 811 | pnum = 1; |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 812 | store->sb_page->index = offset; |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 813 | } |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 814 | |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 815 | for ( ; pnum < num_pages; pnum++) { |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 816 | store->filemap[pnum] = alloc_page(GFP_KERNEL|__GFP_ZERO); |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 817 | if (!store->filemap[pnum]) { |
| 818 | store->file_pages = pnum; |
| 819 | return -ENOMEM; |
| 820 | } |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 821 | store->filemap[pnum]->index = pnum + offset; |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 822 | } |
| 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 838 | static void md_bitmap_file_unmap(struct bitmap_storage *store) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 839 | { |
| 840 | struct page **map, *sb_page; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 841 | int pages; |
NeilBrown | fae7d32 | 2012-05-22 13:55:21 +1000 | [diff] [blame] | 842 | struct file *file; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 843 | |
NeilBrown | fae7d32 | 2012-05-22 13:55:21 +1000 | [diff] [blame] | 844 | file = store->file; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 845 | map = store->filemap; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 846 | pages = store->file_pages; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 847 | sb_page = store->sb_page; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 848 | |
| 849 | while (pages--) |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 850 | if (map[pages] != sb_page) /* 0 is sb_page, release it below */ |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 851 | free_buffers(map[pages]); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 852 | kfree(map); |
NeilBrown | fae7d32 | 2012-05-22 13:55:21 +1000 | [diff] [blame] | 853 | kfree(store->filemap_attr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 854 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 855 | if (sb_page) |
| 856 | free_buffers(sb_page); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 857 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 858 | if (file) { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 859 | struct inode *inode = file_inode(file); |
Andrew Morton | fc0ecff | 2007-02-10 01:45:39 -0800 | [diff] [blame] | 860 | invalidate_mapping_pages(inode->i_mapping, 0, -1); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 861 | fput(file); |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 862 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 863 | } |
| 864 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 865 | /* |
| 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 870 | static void md_bitmap_file_kick(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 871 | { |
| 872 | char *path, *ptr = NULL; |
| 873 | |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 874 | if (!test_and_set_bit(BITMAP_STALE, &bitmap->flags)) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 875 | md_bitmap_update_sb(bitmap); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 876 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 877 | if (bitmap->storage.file) { |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 878 | path = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 879 | if (path) |
Miklos Szeredi | 9bf39ab | 2015-06-19 10:29:13 +0200 | [diff] [blame] | 880 | ptr = file_path(bitmap->storage.file, |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 881 | path, PAGE_SIZE); |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 882 | |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 883 | pr_warn("%s: kicking failed bitmap file %s from array!\n", |
| 884 | bmname(bitmap), IS_ERR(ptr) ? "" : ptr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 885 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 886 | kfree(path); |
| 887 | } else |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 888 | pr_warn("%s: disabling internal bitmap due to errors\n", |
| 889 | bmname(bitmap)); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 890 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | enum bitmap_page_attr { |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 894 | BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */ |
NeilBrown | 5a537df | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 895 | BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned. |
| 896 | * i.e. counter is 1 or 2. */ |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 897 | BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 898 | }; |
| 899 | |
NeilBrown | d189122 | 2012-05-22 13:55:09 +1000 | [diff] [blame] | 900 | static inline void set_page_attr(struct bitmap *bitmap, int pnum, |
| 901 | enum bitmap_page_attr attr) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 902 | { |
NeilBrown | bdfd114 | 2012-05-22 13:55:22 +1000 | [diff] [blame] | 903 | set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 904 | } |
| 905 | |
NeilBrown | d189122 | 2012-05-22 13:55:09 +1000 | [diff] [blame] | 906 | static inline void clear_page_attr(struct bitmap *bitmap, int pnum, |
| 907 | enum bitmap_page_attr attr) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 908 | { |
NeilBrown | bdfd114 | 2012-05-22 13:55:22 +1000 | [diff] [blame] | 909 | clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 910 | } |
| 911 | |
NeilBrown | bdfd114 | 2012-05-22 13:55:22 +1000 | [diff] [blame] | 912 | static inline int test_page_attr(struct bitmap *bitmap, int pnum, |
| 913 | enum bitmap_page_attr attr) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 914 | { |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 915 | return test_bit((pnum<<2) + attr, bitmap->storage.filemap_attr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 916 | } |
| 917 | |
NeilBrown | bdfd114 | 2012-05-22 13:55:22 +1000 | [diff] [blame] | 918 | static 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 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 924 | /* |
| 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 931 | static void md_bitmap_file_set_bit(struct bitmap *bitmap, sector_t block) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 932 | { |
| 933 | unsigned long bit; |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 934 | struct page *page; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 935 | void *kaddr; |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 936 | unsigned long chunk = block >> bitmap->counts.chunkshift; |
Guoqing Jiang | 23cea66a | 2016-05-02 11:50:14 -0400 | [diff] [blame] | 937 | 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; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 942 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 943 | page = filemap_get_page(&bitmap->storage, chunk); |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 944 | if (!page) |
| 945 | return; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 946 | bit = file_page_offset(&bitmap->storage, chunk); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 947 | |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 948 | /* set the bit */ |
Cong Wang | b2f46e6 | 2011-11-28 13:25:44 +0800 | [diff] [blame] | 949 | kaddr = kmap_atomic(page); |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 950 | if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 951 | set_bit(bit, kaddr); |
| 952 | else |
Akinobu Mita | 3f810b6 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 953 | set_bit_le(bit, kaddr); |
Cong Wang | b2f46e6 | 2011-11-28 13:25:44 +0800 | [diff] [blame] | 954 | kunmap_atomic(kaddr); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 955 | pr_debug("set file bit %lu page %lu\n", bit, page->index); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 956 | /* record page number so it gets flushed to disk when unplug occurs */ |
Guoqing Jiang | 23cea66a | 2016-05-02 11:50:14 -0400 | [diff] [blame] | 957 | set_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_DIRTY); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 960 | static void md_bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block) |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 961 | { |
| 962 | unsigned long bit; |
| 963 | struct page *page; |
| 964 | void *paddr; |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 965 | unsigned long chunk = block >> bitmap->counts.chunkshift; |
Guoqing Jiang | 23cea66a | 2016-05-02 11:50:14 -0400 | [diff] [blame] | 966 | 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; |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 971 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 972 | page = filemap_get_page(&bitmap->storage, chunk); |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 973 | if (!page) |
| 974 | return; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 975 | bit = file_page_offset(&bitmap->storage, chunk); |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 976 | paddr = kmap_atomic(page); |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 977 | if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 978 | clear_bit(bit, paddr); |
| 979 | else |
Akinobu Mita | 3f810b6 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 980 | clear_bit_le(bit, paddr); |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 981 | kunmap_atomic(paddr); |
Guoqing Jiang | 23cea66a | 2016-05-02 11:50:14 -0400 | [diff] [blame] | 982 | if (!test_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_NEEDWRITE)) { |
| 983 | set_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_PENDING); |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 984 | bitmap->allclean = 0; |
| 985 | } |
| 986 | } |
| 987 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 988 | static int md_bitmap_file_test_bit(struct bitmap *bitmap, sector_t block) |
Goldwyn Rodrigues | 11dd35d | 2014-06-07 00:36:26 -0500 | [diff] [blame] | 989 | { |
| 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 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1010 | /* 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1013 | void md_bitmap_unplug(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1014 | { |
NeilBrown | 7466712 | 2012-05-22 13:55:19 +1000 | [diff] [blame] | 1015 | unsigned long i; |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 1016 | int dirty, need_write; |
NeilBrown | 85c9ccd | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 1017 | int writing = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1018 | |
NeilBrown | 62f82fa | 2012-05-22 13:55:21 +1000 | [diff] [blame] | 1019 | if (!bitmap || !bitmap->storage.filemap || |
| 1020 | test_bit(BITMAP_STALE, &bitmap->flags)) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1021 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1022 | |
| 1023 | /* look at each page to see if there are any set bits that need to be |
| 1024 | * flushed out to disk */ |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1025 | for (i = 0; i < bitmap->storage.file_pages; i++) { |
NeilBrown | bdfd114 | 2012-05-22 13:55:22 +1000 | [diff] [blame] | 1026 | 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) { |
NeilBrown | 581dbd9 | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 1030 | if (!writing) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1031 | md_bitmap_wait_writes(bitmap); |
NeilBrown | 581dbd9 | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 1032 | if (bitmap->mddev->queue) |
| 1033 | blk_add_trace_msg(bitmap->mddev->queue, |
| 1034 | "md bitmap_unplug"); |
| 1035 | } |
NeilBrown | d189122 | 2012-05-22 13:55:09 +1000 | [diff] [blame] | 1036 | clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING); |
NeilBrown | bdfd114 | 2012-05-22 13:55:22 +1000 | [diff] [blame] | 1037 | write_page(bitmap, bitmap->storage.filemap[i], 0); |
NeilBrown | 85c9ccd | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 1038 | writing = 1; |
NeilBrown | bdfd114 | 2012-05-22 13:55:22 +1000 | [diff] [blame] | 1039 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1040 | } |
NeilBrown | 85c9ccd | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 1041 | if (writing) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1042 | md_bitmap_wait_writes(bitmap); |
NeilBrown | 4b5060d | 2014-09-09 14:13:51 +1000 | [diff] [blame] | 1043 | |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 1044 | if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1045 | md_bitmap_file_kick(bitmap); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1046 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1047 | EXPORT_SYMBOL(md_bitmap_unplug); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1048 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1049 | static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1050 | /* * 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. |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 1057 | * |
| 1058 | * We ignore all bits for sectors that end earlier than 'start'. |
| 1059 | * This is used when reading an out-of-date bitmap... |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1060 | */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1061 | static int md_bitmap_init_from_disk(struct bitmap *bitmap, sector_t start) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1062 | { |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 1063 | unsigned long i, chunks, index, oldindex, bit, node_offset = 0; |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 1064 | struct page *page = NULL; |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 1065 | unsigned long bit_cnt = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1066 | struct file *file; |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 1067 | unsigned long offset; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1068 | int outofdate; |
| 1069 | int ret = -ENOSPC; |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1070 | void *paddr; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1071 | struct bitmap_storage *store = &bitmap->storage; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1072 | |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1073 | chunks = bitmap->counts.chunks; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1074 | file = store->file; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1075 | |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 1076 | if (!file && !bitmap->mddev->bitmap_info.offset) { |
| 1077 | /* No permanent bitmap - fill with '1s'. */ |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1078 | store->filemap = NULL; |
| 1079 | store->file_pages = 0; |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 1080 | for (i = 0; i < chunks ; i++) { |
| 1081 | /* if the disk bit is set, set the memory bit */ |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1082 | int needed = ((sector_t)(i+1) << (bitmap->counts.chunkshift) |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 1083 | >= start); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1084 | md_bitmap_set_memory_bits(bitmap, |
| 1085 | (sector_t)i << bitmap->counts.chunkshift, |
| 1086 | needed); |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 1087 | } |
| 1088 | return 0; |
| 1089 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1090 | |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 1091 | outofdate = test_bit(BITMAP_STALE, &bitmap->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1092 | if (outofdate) |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1093 | pr_warn("%s: bitmap file is out of date, doing full recovery\n", bmname(bitmap)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1094 | |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 1095 | if (file && i_size_read(file->f_mapping->host) < store->bytes) { |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1096 | 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); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1100 | goto err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1101 | } |
NeilBrown | bc7f77d | 2005-06-21 17:17:17 -0700 | [diff] [blame] | 1102 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1103 | oldindex = ~0L; |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 1104 | offset = 0; |
| 1105 | if (!bitmap->mddev->bitmap_info.external) |
| 1106 | offset = sizeof(bitmap_super_t); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1107 | |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 1108 | if (mddev_is_clustered(bitmap->mddev)) |
| 1109 | node_offset = bitmap->cluster_slot * (DIV_ROUND_UP(store->bytes, PAGE_SIZE)); |
| 1110 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1111 | for (i = 0; i < chunks; i++) { |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 1112 | int b; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1113 | index = file_page_index(&bitmap->storage, i); |
| 1114 | bit = file_page_offset(&bitmap->storage, i); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1115 | if (index != oldindex) { /* this is a new page, read it in */ |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 1116 | int count; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1117 | /* unmap the old page, we're done with it */ |
NeilBrown | d1244cb | 2012-05-22 13:55:12 +1000 | [diff] [blame] | 1118 | if (index == store->file_pages-1) |
| 1119 | count = store->bytes - index * PAGE_SIZE; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 1120 | else |
| 1121 | count = PAGE_SIZE; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1122 | page = store->filemap[index]; |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 1123 | 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 Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 1131 | index + node_offset, count); |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 1132 | |
| 1133 | if (ret) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1134 | goto err; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1135 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1136 | oldindex = index; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1137 | |
| 1138 | if (outofdate) { |
| 1139 | /* |
| 1140 | * if bitmap is out of date, dirty the |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1141 | * whole page and write it out |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1142 | */ |
Cong Wang | b2f46e6 | 2011-11-28 13:25:44 +0800 | [diff] [blame] | 1143 | paddr = kmap_atomic(page); |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1144 | memset(paddr + offset, 0xff, |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 1145 | PAGE_SIZE - offset); |
Cong Wang | b2f46e6 | 2011-11-28 13:25:44 +0800 | [diff] [blame] | 1146 | kunmap_atomic(paddr); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1147 | write_page(bitmap, page, 1); |
| 1148 | |
| 1149 | ret = -EIO; |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 1150 | if (test_bit(BITMAP_WRITE_ERROR, |
| 1151 | &bitmap->flags)) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1152 | goto err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1153 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1154 | } |
Cong Wang | b2f46e6 | 2011-11-28 13:25:44 +0800 | [diff] [blame] | 1155 | paddr = kmap_atomic(page); |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 1156 | if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1157 | b = test_bit(bit, paddr); |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 1158 | else |
Akinobu Mita | 6b33aff | 2011-03-23 16:42:13 -0700 | [diff] [blame] | 1159 | b = test_bit_le(bit, paddr); |
Cong Wang | b2f46e6 | 2011-11-28 13:25:44 +0800 | [diff] [blame] | 1160 | kunmap_atomic(paddr); |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 1161 | if (b) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1162 | /* if the disk bit is set, set the memory bit */ |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1163 | int needed = ((sector_t)(i+1) << bitmap->counts.chunkshift |
NeilBrown | db305e5 | 2009-05-07 12:49:06 +1000 | [diff] [blame] | 1164 | >= start); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1165 | md_bitmap_set_memory_bits(bitmap, |
| 1166 | (sector_t)i << bitmap->counts.chunkshift, |
| 1167 | needed); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1168 | bit_cnt++; |
| 1169 | } |
NeilBrown | 27581e5 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 1170 | offset = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1173 | 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); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1176 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1177 | return 0; |
| 1178 | |
| 1179 | err: |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1180 | pr_warn("%s: bitmap initialisation failed: %d\n", |
| 1181 | bmname(bitmap), ret); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1182 | return ret; |
| 1183 | } |
| 1184 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1185 | void md_bitmap_write_all(struct bitmap *bitmap) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1186 | { |
| 1187 | /* We don't actually write all bitmap blocks here, |
| 1188 | * just flag them as needing to be written |
| 1189 | */ |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 1190 | int i; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1191 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1192 | if (!bitmap || !bitmap->storage.filemap) |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 1193 | return; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1194 | if (bitmap->storage.file) |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 1195 | /* Only one copy, so nothing needed */ |
| 1196 | return; |
| 1197 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1198 | for (i = 0; i < bitmap->storage.file_pages; i++) |
NeilBrown | d189122 | 2012-05-22 13:55:09 +1000 | [diff] [blame] | 1199 | set_page_attr(bitmap, i, |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 1200 | BITMAP_PAGE_NEEDWRITE); |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1201 | bitmap->allclean = 0; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1204 | static void md_bitmap_count_page(struct bitmap_counts *bitmap, |
| 1205 | sector_t offset, int inc) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1206 | { |
NeilBrown | 61a0d80 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1207 | sector_t chunk = offset >> bitmap->chunkshift; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1208 | unsigned long page = chunk >> PAGE_COUNTER_SHIFT; |
| 1209 | bitmap->bp[page].count += inc; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1210 | md_bitmap_checkfree(bitmap, page); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1211 | } |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1212 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1213 | static void md_bitmap_set_pending(struct bitmap_counts *bitmap, sector_t offset) |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1214 | { |
| 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1223 | static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap, |
| 1224 | sector_t offset, sector_t *blocks, |
| 1225 | int create); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1226 | |
| 1227 | /* |
| 1228 | * bitmap daemon -- periodically wakes up to clean bits and flush pages |
| 1229 | * out to disk |
| 1230 | */ |
| 1231 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1232 | void md_bitmap_daemon_work(struct mddev *mddev) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1233 | { |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1234 | struct bitmap *bitmap; |
NeilBrown | aa3163f | 2005-06-21 17:17:22 -0700 | [diff] [blame] | 1235 | unsigned long j; |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1236 | unsigned long nextpage; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1237 | sector_t blocks; |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1238 | struct bitmap_counts *counts; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1239 | |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1240 | /* Use a mutex to guard daemon_work against |
| 1241 | * bitmap_destroy. |
| 1242 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1243 | mutex_lock(&mddev->bitmap_info.mutex); |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1244 | bitmap = mddev->bitmap; |
| 1245 | if (bitmap == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1246 | mutex_unlock(&mddev->bitmap_info.mutex); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1247 | return; |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1248 | } |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1249 | if (time_before(jiffies, bitmap->daemon_lastrun |
NeilBrown | 2e61ebb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1250 | + mddev->bitmap_info.daemon_sleep)) |
NeilBrown | 7be3dfe | 2008-03-10 11:43:48 -0700 | [diff] [blame] | 1251 | goto done; |
| 1252 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1253 | bitmap->daemon_lastrun = jiffies; |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1254 | if (bitmap->allclean) { |
NeilBrown | 2e61ebb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1255 | mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1256 | goto done; |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1257 | } |
| 1258 | bitmap->allclean = 1; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1259 | |
NeilBrown | 581dbd9 | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 1260 | if (bitmap->mddev->queue) |
| 1261 | blk_add_trace_msg(bitmap->mddev->queue, |
| 1262 | "md bitmap_daemon_work"); |
| 1263 | |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1264 | /* 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 | */ |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1268 | for (j = 0; j < bitmap->storage.file_pages; j++) |
NeilBrown | bdfd114 | 2012-05-22 13:55:22 +1000 | [diff] [blame] | 1269 | if (test_and_clear_page_attr(bitmap, j, |
| 1270 | BITMAP_PAGE_PENDING)) |
NeilBrown | d189122 | 2012-05-22 13:55:09 +1000 | [diff] [blame] | 1271 | set_page_attr(bitmap, j, |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1272 | BITMAP_PAGE_NEEDWRITE); |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1273 | |
| 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; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1280 | if (bitmap->storage.filemap) { |
| 1281 | sb = kmap_atomic(bitmap->storage.sb_page); |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 1282 | sb->events_cleared = |
| 1283 | cpu_to_le64(bitmap->events_cleared); |
| 1284 | kunmap_atomic(sb); |
NeilBrown | d189122 | 2012-05-22 13:55:09 +1000 | [diff] [blame] | 1285 | set_page_attr(bitmap, 0, |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 1286 | BITMAP_PAGE_NEEDWRITE); |
| 1287 | } |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1288 | } |
| 1289 | /* Now look at the bitmap counters and if any are '2' or '1', |
| 1290 | * decrement and handle accordingly. |
| 1291 | */ |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1292 | counts = &bitmap->counts; |
| 1293 | spin_lock_irq(&counts->lock); |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1294 | nextpage = 0; |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1295 | for (j = 0; j < counts->chunks; j++) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1296 | bitmap_counter_t *bmc; |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1297 | sector_t block = (sector_t)j << counts->chunkshift; |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 1298 | |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1299 | if (j == nextpage) { |
| 1300 | nextpage += PAGE_COUNTER_RATIO; |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1301 | if (!counts->bp[j >> PAGE_COUNTER_SHIFT].pending) { |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1302 | j |= PAGE_COUNTER_MASK; |
NeilBrown | aa3163f | 2005-06-21 17:17:22 -0700 | [diff] [blame] | 1303 | continue; |
| 1304 | } |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1305 | counts->bp[j >> PAGE_COUNTER_SHIFT].pending = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1306 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1307 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1308 | bmc = md_bitmap_get_counter(counts, block, &blocks, 0); |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1309 | if (!bmc) { |
| 1310 | j |= PAGE_COUNTER_MASK; |
| 1311 | continue; |
| 1312 | } |
| 1313 | if (*bmc == 1 && !bitmap->need_sync) { |
| 1314 | /* We can clear the bit */ |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1315 | *bmc = 0; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1316 | md_bitmap_count_page(counts, block, -1); |
| 1317 | md_bitmap_file_clear_bit(bitmap, block); |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1318 | } else if (*bmc && *bmc <= 2) { |
| 1319 | *bmc = 1; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1320 | md_bitmap_set_pending(counts, block); |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1321 | bitmap->allclean = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1322 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1323 | } |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1324 | spin_unlock_irq(&counts->lock); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1325 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1326 | md_bitmap_wait_writes(bitmap); |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1327 | /* 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 | */ |
NeilBrown | 62f82fa | 2012-05-22 13:55:21 +1000 | [diff] [blame] | 1335 | for (j = 0; |
| 1336 | j < bitmap->storage.file_pages |
| 1337 | && !test_bit(BITMAP_STALE, &bitmap->flags); |
| 1338 | j++) { |
NeilBrown | d189122 | 2012-05-22 13:55:09 +1000 | [diff] [blame] | 1339 | if (test_page_attr(bitmap, j, |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1340 | BITMAP_PAGE_DIRTY)) |
| 1341 | /* bitmap_unplug will handle the rest */ |
| 1342 | break; |
Zhiqiang Liu | 5518049 | 2019-12-07 11:00:08 +0800 | [diff] [blame] | 1343 | if (bitmap->storage.filemap && |
| 1344 | test_and_clear_page_attr(bitmap, j, |
NeilBrown | bdfd114 | 2012-05-22 13:55:22 +1000 | [diff] [blame] | 1345 | BITMAP_PAGE_NEEDWRITE)) { |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1346 | write_page(bitmap, bitmap->storage.filemap[j], 0); |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1347 | } |
| 1348 | } |
NeilBrown | bf07bb7 | 2012-05-22 13:55:06 +1000 | [diff] [blame] | 1349 | |
NeilBrown | 7be3dfe | 2008-03-10 11:43:48 -0700 | [diff] [blame] | 1350 | done: |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1351 | if (bitmap->allclean == 0) |
NeilBrown | 2e61ebb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1352 | mddev->thread->timeout = |
| 1353 | mddev->bitmap_info.daemon_sleep; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1354 | mutex_unlock(&mddev->bitmap_info.mutex); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1357 | static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap, |
| 1358 | sector_t offset, sector_t *blocks, |
| 1359 | int create) |
NeilBrown | ee305ac | 2009-09-23 18:06:44 +1000 | [diff] [blame] | 1360 | __releases(bitmap->lock) |
| 1361 | __acquires(bitmap->lock) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1362 | { |
| 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 | */ |
NeilBrown | 61a0d80 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1367 | sector_t chunk = offset >> bitmap->chunkshift; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1368 | unsigned long page = chunk >> PAGE_COUNTER_SHIFT; |
| 1369 | unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT; |
| 1370 | sector_t csize; |
NeilBrown | ef42567 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 1371 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1372 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1373 | err = md_bitmap_checkpage(bitmap, page, create, 0); |
NeilBrown | ef42567 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 1374 | |
| 1375 | if (bitmap->bp[page].hijacked || |
| 1376 | bitmap->bp[page].map == NULL) |
NeilBrown | 61a0d80 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1377 | csize = ((sector_t)1) << (bitmap->chunkshift + |
NeilBrown | ef42567 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 1378 | PAGE_COUNTER_SHIFT - 1); |
| 1379 | else |
NeilBrown | 61a0d80 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1380 | csize = ((sector_t)1) << bitmap->chunkshift; |
NeilBrown | ef42567 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 1381 | *blocks = csize - (offset & (csize - 1)); |
| 1382 | |
| 1383 | if (err < 0) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1384 | return NULL; |
NeilBrown | ef42567 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 1385 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1386 | /* 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); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1392 | return &((bitmap_counter_t *) |
| 1393 | &bitmap->bp[page].map)[hi]; |
NeilBrown | ef42567 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 1394 | } else /* page is allocated */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1395 | return (bitmap_counter_t *) |
| 1396 | &(bitmap->bp[page].map[pageoff]); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1399 | int md_bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1400 | { |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1401 | if (!bitmap) |
| 1402 | return 0; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1403 | |
| 1404 | if (behind) { |
Paul Clements | 696fcd5 | 2010-03-08 16:02:37 +1100 | [diff] [blame] | 1405 | int bw; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1406 | atomic_inc(&bitmap->behind_writes); |
Paul Clements | 696fcd5 | 2010-03-08 16:02:37 +1100 | [diff] [blame] | 1407 | bw = atomic_read(&bitmap->behind_writes); |
| 1408 | if (bw > bitmap->behind_writes_used) |
| 1409 | bitmap->behind_writes_used = bw; |
| 1410 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 1411 | pr_debug("inc write-behind count %d/%lu\n", |
| 1412 | bw, bitmap->mddev->bitmap_info.max_write_behind); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1415 | while (sectors) { |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1416 | sector_t blocks; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1417 | bitmap_counter_t *bmc; |
| 1418 | |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1419 | spin_lock_irq(&bitmap->counts.lock); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1420 | bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 1); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1421 | if (!bmc) { |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1422 | spin_unlock_irq(&bitmap->counts.lock); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1423 | return 0; |
| 1424 | } |
| 1425 | |
Namhyung Kim | 27d5ea0 | 2011-06-09 11:42:57 +1000 | [diff] [blame] | 1426 | if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) { |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1427 | 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); |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1434 | spin_unlock_irq(&bitmap->counts.lock); |
NeilBrown | f54a9d0 | 2012-08-02 08:33:20 +1000 | [diff] [blame] | 1435 | schedule(); |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1436 | finish_wait(&bitmap->overflow_wait, &__wait); |
| 1437 | continue; |
| 1438 | } |
| 1439 | |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1440 | switch (*bmc) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1441 | case 0: |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1442 | md_bitmap_file_set_bit(bitmap, offset); |
| 1443 | md_bitmap_count_page(&bitmap->counts, offset, 1); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1444 | /* fall through */ |
| 1445 | case 1: |
| 1446 | *bmc = 2; |
| 1447 | } |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1448 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1449 | (*bmc)++; |
| 1450 | |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1451 | spin_unlock_irq(&bitmap->counts.lock); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1452 | |
| 1453 | offset += blocks; |
| 1454 | if (sectors > blocks) |
| 1455 | sectors -= blocks; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1456 | else |
| 1457 | sectors = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1458 | } |
| 1459 | return 0; |
| 1460 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1461 | EXPORT_SYMBOL(md_bitmap_startwrite); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1462 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1463 | void md_bitmap_endwrite(struct bitmap *bitmap, sector_t offset, |
| 1464 | unsigned long sectors, int success, int behind) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1465 | { |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1466 | if (!bitmap) |
| 1467 | return; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1468 | if (behind) { |
NeilBrown | e555190 | 2010-03-31 11:21:44 +1100 | [diff] [blame] | 1469 | if (atomic_dec_and_test(&bitmap->behind_writes)) |
| 1470 | wake_up(&bitmap->behind_wait); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 1471 | pr_debug("dec write-behind count %d/%lu\n", |
| 1472 | atomic_read(&bitmap->behind_writes), |
| 1473 | bitmap->mddev->bitmap_info.max_write_behind); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1476 | while (sectors) { |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1477 | sector_t blocks; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1478 | unsigned long flags; |
| 1479 | bitmap_counter_t *bmc; |
| 1480 | |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1481 | spin_lock_irqsave(&bitmap->counts.lock, flags); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1482 | bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1483 | if (!bmc) { |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1484 | spin_unlock_irqrestore(&bitmap->counts.lock, flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1485 | return; |
| 1486 | } |
| 1487 | |
NeilBrown | 961902c | 2011-12-23 09:57:48 +1100 | [diff] [blame] | 1488 | if (success && !bitmap->mddev->degraded && |
Neil Brown | a0da84f | 2008-06-28 08:31:22 +1000 | [diff] [blame] | 1489 | bitmap->events_cleared < bitmap->mddev->events) { |
| 1490 | bitmap->events_cleared = bitmap->mddev->events; |
| 1491 | bitmap->need_sync = 1; |
NeilBrown | 5ff5aff | 2010-06-01 19:37:32 +1000 | [diff] [blame] | 1492 | sysfs_notify_dirent_safe(bitmap->sysfs_can_clear); |
Neil Brown | a0da84f | 2008-06-28 08:31:22 +1000 | [diff] [blame] | 1493 | } |
| 1494 | |
Namhyung Kim | 27d5ea0 | 2011-06-09 11:42:57 +1000 | [diff] [blame] | 1495 | if (!success && !NEEDED(*bmc)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1496 | *bmc |= NEEDED_MASK; |
| 1497 | |
Namhyung Kim | 27d5ea0 | 2011-06-09 11:42:57 +1000 | [diff] [blame] | 1498 | if (COUNTER(*bmc) == COUNTER_MAX) |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1499 | wake_up(&bitmap->overflow_wait); |
| 1500 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1501 | (*bmc)--; |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1502 | if (*bmc <= 2) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1503 | md_bitmap_set_pending(&bitmap->counts, offset); |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1504 | bitmap->allclean = 0; |
| 1505 | } |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1506 | spin_unlock_irqrestore(&bitmap->counts.lock, flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1507 | offset += blocks; |
| 1508 | if (sectors > blocks) |
| 1509 | sectors -= blocks; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1510 | else |
| 1511 | sectors = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1512 | } |
| 1513 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1514 | EXPORT_SYMBOL(md_bitmap_endwrite); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1515 | |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1516 | static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1517 | int degraded) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1518 | { |
| 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 | } |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1525 | spin_lock_irq(&bitmap->counts.lock); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1526 | bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1527 | rv = 0; |
| 1528 | if (bmc) { |
| 1529 | /* locked */ |
| 1530 | if (RESYNC(*bmc)) |
| 1531 | rv = 1; |
| 1532 | else if (NEEDED(*bmc)) { |
| 1533 | rv = 1; |
NeilBrown | 6a806c5 | 2005-07-15 03:56:35 -0700 | [diff] [blame] | 1534 | if (!degraded) { /* don't set/clear bits if degraded */ |
| 1535 | *bmc |= RESYNC_MASK; |
| 1536 | *bmc &= ~NEEDED_MASK; |
| 1537 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1538 | } |
| 1539 | } |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1540 | spin_unlock_irq(&bitmap->counts.lock); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1541 | return rv; |
| 1542 | } |
| 1543 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1544 | int md_bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, |
| 1545 | int degraded) |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1546 | { |
| 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; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1555 | sector_t blocks1; |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1556 | |
| 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1566 | EXPORT_SYMBOL(md_bitmap_start_sync); |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1567 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1568 | void md_bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1569 | { |
| 1570 | bitmap_counter_t *bmc; |
| 1571 | unsigned long flags; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1572 | |
| 1573 | if (bitmap == NULL) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1574 | *blocks = 1024; |
| 1575 | return; |
| 1576 | } |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1577 | spin_lock_irqsave(&bitmap->counts.lock, flags); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1578 | bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1579 | if (bmc == NULL) |
| 1580 | goto unlock; |
| 1581 | /* locked */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1582 | if (RESYNC(*bmc)) { |
| 1583 | *bmc &= ~RESYNC_MASK; |
| 1584 | |
| 1585 | if (!NEEDED(*bmc) && aborted) |
| 1586 | *bmc |= NEEDED_MASK; |
| 1587 | else { |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1588 | if (*bmc <= 2) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1589 | md_bitmap_set_pending(&bitmap->counts, offset); |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1590 | bitmap->allclean = 0; |
| 1591 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1592 | } |
| 1593 | } |
| 1594 | unlock: |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1595 | spin_unlock_irqrestore(&bitmap->counts.lock, flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1596 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1597 | EXPORT_SYMBOL(md_bitmap_end_sync); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1598 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1599 | void md_bitmap_close_sync(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1600 | { |
| 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; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1606 | sector_t blocks; |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1607 | if (!bitmap) |
| 1608 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1609 | while (sector < bitmap->mddev->resync_max_sectors) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1610 | md_bitmap_end_sync(bitmap, sector, &blocks, 0); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1611 | sector += blocks; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1612 | } |
| 1613 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1614 | EXPORT_SYMBOL(md_bitmap_close_sync); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1615 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1616 | void md_bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector, bool force) |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1617 | { |
| 1618 | sector_t s = 0; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1619 | sector_t blocks; |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1620 | |
| 1621 | if (!bitmap) |
| 1622 | return; |
| 1623 | if (sector == 0) { |
| 1624 | bitmap->last_end_sync = jiffies; |
| 1625 | return; |
| 1626 | } |
Goldwyn Rodrigues | c40f341 | 2015-08-19 08:14:42 +1000 | [diff] [blame] | 1627 | if (!force && time_before(jiffies, (bitmap->last_end_sync |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1628 | + bitmap->mddev->bitmap_info.daemon_sleep))) |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1629 | return; |
| 1630 | wait_event(bitmap->mddev->recovery_wait, |
| 1631 | atomic_read(&bitmap->mddev->recovery_active) == 0); |
| 1632 | |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 1633 | bitmap->mddev->curr_resync_completed = sector; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1634 | set_bit(MD_SB_CHANGE_CLEAN, &bitmap->mddev->sb_flags); |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1635 | sector &= ~((1ULL << bitmap->counts.chunkshift) - 1); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1636 | s = 0; |
| 1637 | while (s < sector && s < bitmap->mddev->resync_max_sectors) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1638 | md_bitmap_end_sync(bitmap, s, &blocks, 0); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1639 | s += blocks; |
| 1640 | } |
| 1641 | bitmap->last_end_sync = jiffies; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 1642 | sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed"); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1643 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1644 | EXPORT_SYMBOL(md_bitmap_cond_end_sync); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1645 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1646 | void md_bitmap_sync_with_cluster(struct mddev *mddev, |
Guoqing Jiang | 18c9ff7 | 2016-05-02 11:50:12 -0400 | [diff] [blame] | 1647 | 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1654 | md_bitmap_end_sync(bitmap, sector, &blocks, 0); |
Guoqing Jiang | 18c9ff7 | 2016-05-02 11:50:12 -0400 | [diff] [blame] | 1655 | 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1660 | md_bitmap_start_sync(bitmap, sector, &blocks, 0); |
Guoqing Jiang | 18c9ff7 | 2016-05-02 11:50:12 -0400 | [diff] [blame] | 1661 | sector += blocks; |
| 1662 | } |
| 1663 | WARN((blocks > new_hi) && old_hi, "alignment is not correct for hi\n"); |
| 1664 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1665 | EXPORT_SYMBOL(md_bitmap_sync_with_cluster); |
Guoqing Jiang | 18c9ff7 | 2016-05-02 11:50:12 -0400 | [diff] [blame] | 1666 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1667 | static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1668 | { |
| 1669 | /* For each chunk covered by any of these sectors, set the |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 1670 | * counter to 2 and possibly set resync_needed. They should all |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1671 | * be 0 at this point |
| 1672 | */ |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1673 | |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1674 | sector_t secs; |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1675 | bitmap_counter_t *bmc; |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1676 | spin_lock_irq(&bitmap->counts.lock); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1677 | bmc = md_bitmap_get_counter(&bitmap->counts, offset, &secs, 1); |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1678 | if (!bmc) { |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1679 | spin_unlock_irq(&bitmap->counts.lock); |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1680 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1681 | } |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1682 | if (!*bmc) { |
Goldwyn Rodrigues | 11dd35d | 2014-06-07 00:36:26 -0500 | [diff] [blame] | 1683 | *bmc = 2; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1684 | md_bitmap_count_page(&bitmap->counts, offset, 1); |
| 1685 | md_bitmap_set_pending(&bitmap->counts, offset); |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1686 | bitmap->allclean = 0; |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1687 | } |
Goldwyn Rodrigues | 11dd35d | 2014-06-07 00:36:26 -0500 | [diff] [blame] | 1688 | if (needed) |
| 1689 | *bmc |= NEEDED_MASK; |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1690 | spin_unlock_irq(&bitmap->counts.lock); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1691 | } |
| 1692 | |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 1693 | /* dirty the memory and file bits for bitmap chunks "s" to "e" */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1694 | void md_bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 1695 | { |
| 1696 | unsigned long chunk; |
| 1697 | |
| 1698 | for (chunk = s; chunk <= e; chunk++) { |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1699 | sector_t sec = (sector_t)chunk << bitmap->counts.chunkshift; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1700 | md_bitmap_set_memory_bits(bitmap, sec, 1); |
| 1701 | md_bitmap_file_set_bit(bitmap, sec); |
NeilBrown | ffa2332 | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1702 | 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 Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 1708 | } |
| 1709 | } |
| 1710 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1711 | /* |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 1712 | * flush out any pending updates |
| 1713 | */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1714 | void md_bitmap_flush(struct mddev *mddev) |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 1715 | { |
| 1716 | struct bitmap *bitmap = mddev->bitmap; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1717 | long sleep; |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 1718 | |
| 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 | */ |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1725 | sleep = mddev->bitmap_info.daemon_sleep * 2; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1726 | bitmap->daemon_lastrun -= sleep; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1727 | md_bitmap_daemon_work(mddev); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1728 | bitmap->daemon_lastrun -= sleep; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1729 | md_bitmap_daemon_work(mddev); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1730 | bitmap->daemon_lastrun -= sleep; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1731 | md_bitmap_daemon_work(mddev); |
| 1732 | md_bitmap_update_sb(bitmap); |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | /* |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1736 | * free memory that was allocated |
| 1737 | */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1738 | void md_bitmap_free(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1739 | { |
| 1740 | unsigned long k, pages; |
| 1741 | struct bitmap_page *bp; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1742 | |
| 1743 | if (!bitmap) /* there was no bitmap */ |
| 1744 | return; |
| 1745 | |
Guoqing Jiang | f9a67b1 | 2016-04-01 17:08:49 +0800 | [diff] [blame] | 1746 | if (bitmap->sysfs_can_clear) |
| 1747 | sysfs_put(bitmap->sysfs_can_clear); |
| 1748 | |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 1749 | if (mddev_is_clustered(bitmap->mddev) && bitmap->mddev->cluster_info && |
| 1750 | bitmap->cluster_slot == md_cluster_ops->slot_number(bitmap->mddev)) |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 1751 | md_cluster_stop(bitmap->mddev); |
| 1752 | |
NeilBrown | fae7d32 | 2012-05-22 13:55:21 +1000 | [diff] [blame] | 1753 | /* 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1758 | md_bitmap_file_unmap(&bitmap->storage); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1759 | |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1760 | bp = bitmap->counts.bp; |
| 1761 | pages = bitmap->counts.pages; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1762 | |
| 1763 | /* free all allocated memory */ |
| 1764 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1765 | 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1772 | EXPORT_SYMBOL(md_bitmap_free); |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1773 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1774 | void md_bitmap_wait_behind_writes(struct mddev *mddev) |
Guoqing Jiang | 48df498 | 2017-03-14 09:40:20 +0800 | [diff] [blame] | 1775 | { |
| 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1788 | void md_bitmap_destroy(struct mddev *mddev) |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1789 | { |
| 1790 | struct bitmap *bitmap = mddev->bitmap; |
| 1791 | |
| 1792 | if (!bitmap) /* there was no bitmap */ |
| 1793 | return; |
| 1794 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1795 | md_bitmap_wait_behind_writes(mddev); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 1796 | if (!mddev->serialize_policy) |
| 1797 | mddev_destroy_serial_pool(mddev, NULL, true); |
Guoqing Jiang | 48df498 | 2017-03-14 09:40:20 +0800 | [diff] [blame] | 1798 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1799 | mutex_lock(&mddev->bitmap_info.mutex); |
NeilBrown | 978a7a4 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 1800 | spin_lock(&mddev->lock); |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1801 | mddev->bitmap = NULL; /* disconnect from the md device */ |
NeilBrown | 978a7a4 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 1802 | spin_unlock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1803 | mutex_unlock(&mddev->bitmap_info.mutex); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1804 | if (mddev->thread) |
| 1805 | mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1806 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1807 | md_bitmap_free(bitmap); |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1808 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1809 | |
| 1810 | /* |
| 1811 | * initialize the bitmap structure |
| 1812 | * if this returns an error, bitmap_destroy must be called to do clean up |
Guoqing Jiang | f9a67b1 | 2016-04-01 17:08:49 +0800 | [diff] [blame] | 1813 | * once mddev->bitmap is set |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1814 | */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1815 | struct bitmap *md_bitmap_create(struct mddev *mddev, int slot) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1816 | { |
| 1817 | struct bitmap *bitmap; |
NeilBrown | 1f59390 | 2009-04-20 11:50:24 +1000 | [diff] [blame] | 1818 | sector_t blocks = mddev->resync_max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1819 | struct file *file = mddev->bitmap_info.file; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1820 | int err; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 1821 | struct kernfs_node *bm = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1822 | |
Alexey Dobriyan | 5f6e3c83 | 2006-10-11 01:22:26 -0700 | [diff] [blame] | 1823 | BUILD_BUG_ON(sizeof(bitmap_super_t) != 256); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1824 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1825 | BUG_ON(file && mddev->bitmap_info.offset); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1826 | |
NeilBrown | 230b55f | 2017-10-17 14:24:09 +1100 | [diff] [blame] | 1827 | 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 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 1833 | bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1834 | if (!bitmap) |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 1835 | return ERR_PTR(-ENOMEM); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1836 | |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 1837 | spin_lock_init(&bitmap->counts.lock); |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 1838 | atomic_set(&bitmap->pending_writes, 0); |
| 1839 | init_waitqueue_head(&bitmap->write_wait); |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1840 | init_waitqueue_head(&bitmap->overflow_wait); |
NeilBrown | e555190 | 2010-03-31 11:21:44 +1100 | [diff] [blame] | 1841 | init_waitqueue_head(&bitmap->behind_wait); |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 1842 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1843 | bitmap->mddev = mddev; |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 1844 | bitmap->cluster_slot = slot; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1845 | |
NeilBrown | 5ff5aff | 2010-06-01 19:37:32 +1000 | [diff] [blame] | 1846 | if (mddev->kobj.sd) |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 1847 | bm = sysfs_get_dirent(mddev->kobj.sd, "bitmap"); |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1848 | if (bm) { |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 1849 | bitmap->sysfs_can_clear = sysfs_get_dirent(bm, "can_clear"); |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1850 | sysfs_put(bm); |
| 1851 | } else |
| 1852 | bitmap->sysfs_can_clear = NULL; |
| 1853 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1854 | bitmap->storage.file = file; |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 1855 | if (file) { |
| 1856 | get_file(file); |
NeilBrown | ae8fa28 | 2009-10-16 15:56:01 +1100 | [diff] [blame] | 1857 | /* As future accesses to this file will use bmap, |
| 1858 | * and bypass the page cache, we must sync the file |
| 1859 | * first. |
| 1860 | */ |
Christoph Hellwig | 8018ab0 | 2010-03-22 17:32:25 +0100 | [diff] [blame] | 1861 | vfs_fsync(file, 1); |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 1862 | } |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1863 | /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */ |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 1864 | 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1870 | err = md_bitmap_new_disk_sb(bitmap); |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 1871 | else |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1872 | err = md_bitmap_read_sb(bitmap); |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 1873 | } else { |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1874 | 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 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1881 | if (err) |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1882 | goto error; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1883 | |
NeilBrown | 624ce4f | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1884 | bitmap->daemon_lastrun = jiffies; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1885 | err = md_bitmap_resize(bitmap, blocks, mddev->bitmap_info.chunksize, 1); |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 1886 | if (err) |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1887 | goto error; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1888 | |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1889 | pr_debug("created bitmap (%lu pages) for device %s\n", |
| 1890 | bitmap->counts.pages, bmname(bitmap)); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1891 | |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 1892 | err = test_bit(BITMAP_WRITE_ERROR, &bitmap->flags) ? -EIO : 0; |
| 1893 | if (err) |
| 1894 | goto error; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1895 | |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 1896 | return bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1897 | error: |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1898 | md_bitmap_free(bitmap); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 1899 | return ERR_PTR(err); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1900 | } |
| 1901 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1902 | int md_bitmap_load(struct mddev *mddev) |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1903 | { |
| 1904 | int err = 0; |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 1905 | sector_t start = 0; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1906 | sector_t sector = 0; |
| 1907 | struct bitmap *bitmap = mddev->bitmap; |
Guoqing Jiang | 617b194 | 2019-06-14 17:10:38 +0800 | [diff] [blame] | 1908 | struct md_rdev *rdev; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1909 | |
| 1910 | if (!bitmap) |
| 1911 | goto out; |
| 1912 | |
Guoqing Jiang | 617b194 | 2019-06-14 17:10:38 +0800 | [diff] [blame] | 1913 | rdev_for_each(rdev, mddev) |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 1914 | mddev_create_serial_pool(mddev, rdev, true); |
Guoqing Jiang | 617b194 | 2019-06-14 17:10:38 +0800 | [diff] [blame] | 1915 | |
Guoqing Jiang | 51e453a | 2016-05-04 02:17:09 -0400 | [diff] [blame] | 1916 | if (mddev_is_clustered(mddev)) |
| 1917 | md_cluster_ops->load_bitmaps(mddev, mddev->bitmap_info.nodes); |
| 1918 | |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1919 | /* 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) { |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1925 | sector_t blocks; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1926 | md_bitmap_start_sync(bitmap, sector, &blocks, 0); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1927 | sector += blocks; |
| 1928 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1929 | md_bitmap_close_sync(bitmap); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1930 | |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 1931 | 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; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1936 | |
NeilBrown | afbaa90 | 2012-04-12 16:05:06 +1000 | [diff] [blame] | 1937 | mutex_lock(&mddev->bitmap_info.mutex); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1938 | err = md_bitmap_init_from_disk(bitmap, start); |
NeilBrown | afbaa90 | 2012-04-12 16:05:06 +1000 | [diff] [blame] | 1939 | mutex_unlock(&mddev->bitmap_info.mutex); |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 1940 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1941 | if (err) |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1942 | goto out; |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 1943 | clear_bit(BITMAP_STALE, &bitmap->flags); |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 1944 | |
| 1945 | /* Kick recovery in case any bits were set */ |
| 1946 | set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery); |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1947 | |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1948 | mddev->thread->timeout = mddev->bitmap_info.daemon_sleep; |
NeilBrown | 9cd30fd | 2009-12-14 12:49:54 +1100 | [diff] [blame] | 1949 | md_wakeup_thread(mddev->thread); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1950 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1951 | md_bitmap_update_sb(bitmap); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1952 | |
NeilBrown | b405fe9 | 2012-05-22 13:55:15 +1000 | [diff] [blame] | 1953 | if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1954 | err = -EIO; |
| 1955 | out: |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1956 | return err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1957 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1958 | EXPORT_SYMBOL_GPL(md_bitmap_load); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1959 | |
Guoqing Jiang | b98938d | 2017-03-01 16:42:39 +0800 | [diff] [blame] | 1960 | struct bitmap *get_bitmap_from_slot(struct mddev *mddev, int slot) |
| 1961 | { |
| 1962 | int rv = 0; |
| 1963 | struct bitmap *bitmap; |
| 1964 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1965 | bitmap = md_bitmap_create(mddev, slot); |
Guoqing Jiang | b98938d | 2017-03-01 16:42:39 +0800 | [diff] [blame] | 1966 | if (IS_ERR(bitmap)) { |
| 1967 | rv = PTR_ERR(bitmap); |
| 1968 | return ERR_PTR(rv); |
| 1969 | } |
| 1970 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1971 | rv = md_bitmap_init_from_disk(bitmap, 0); |
Guoqing Jiang | b98938d | 2017-03-01 16:42:39 +0800 | [diff] [blame] | 1972 | if (rv) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1973 | md_bitmap_free(bitmap); |
Guoqing Jiang | b98938d | 2017-03-01 16:42:39 +0800 | [diff] [blame] | 1974 | return ERR_PTR(rv); |
| 1975 | } |
| 1976 | |
| 1977 | return bitmap; |
| 1978 | } |
| 1979 | EXPORT_SYMBOL(get_bitmap_from_slot); |
| 1980 | |
Goldwyn Rodrigues | 11dd35d | 2014-06-07 00:36:26 -0500 | [diff] [blame] | 1981 | /* Loads the bitmap associated with slot and copies the resync information |
| 1982 | * to our bitmap |
| 1983 | */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1984 | int md_bitmap_copy_from_slot(struct mddev *mddev, int slot, |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 1985 | sector_t *low, sector_t *high, bool clear_bits) |
Goldwyn Rodrigues | 11dd35d | 2014-06-07 00:36:26 -0500 | [diff] [blame] | 1986 | { |
| 1987 | int rv = 0, i, j; |
| 1988 | sector_t block, lo = 0, hi = 0; |
| 1989 | struct bitmap_counts *counts; |
Guoqing Jiang | b98938d | 2017-03-01 16:42:39 +0800 | [diff] [blame] | 1990 | struct bitmap *bitmap; |
Goldwyn Rodrigues | 11dd35d | 2014-06-07 00:36:26 -0500 | [diff] [blame] | 1991 | |
Guoqing Jiang | b98938d | 2017-03-01 16:42:39 +0800 | [diff] [blame] | 1992 | 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 Rodrigues | 11dd35d | 2014-06-07 00:36:26 -0500 | [diff] [blame] | 1997 | |
| 1998 | counts = &bitmap->counts; |
| 1999 | for (j = 0; j < counts->chunks; j++) { |
| 2000 | block = (sector_t)j << counts->chunkshift; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2001 | if (md_bitmap_file_test_bit(bitmap, block)) { |
Goldwyn Rodrigues | 11dd35d | 2014-06-07 00:36:26 -0500 | [diff] [blame] | 2002 | if (!lo) |
| 2003 | lo = block; |
| 2004 | hi = block; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2005 | 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 Rodrigues | 11dd35d | 2014-06-07 00:36:26 -0500 | [diff] [blame] | 2008 | } |
| 2009 | } |
| 2010 | |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 2011 | if (clear_bits) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2012 | md_bitmap_update_sb(bitmap); |
Guoqing Jiang | c84400c | 2016-05-02 11:50:15 -0400 | [diff] [blame] | 2013 | /* BITMAP_PAGE_PENDING is set, but bitmap_unplug needs |
| 2014 | * BITMAP_PAGE_DIRTY or _NEEDWRITE to write ... */ |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 2015 | for (i = 0; i < bitmap->storage.file_pages; i++) |
Guoqing Jiang | c84400c | 2016-05-02 11:50:15 -0400 | [diff] [blame] | 2016 | if (test_page_attr(bitmap, i, BITMAP_PAGE_PENDING)) |
| 2017 | set_page_attr(bitmap, i, BITMAP_PAGE_NEEDWRITE); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2018 | md_bitmap_unplug(bitmap); |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 2019 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2020 | md_bitmap_unplug(mddev->bitmap); |
Goldwyn Rodrigues | 11dd35d | 2014-06-07 00:36:26 -0500 | [diff] [blame] | 2021 | *low = lo; |
| 2022 | *high = hi; |
Guoqing Jiang | b98938d | 2017-03-01 16:42:39 +0800 | [diff] [blame] | 2023 | |
Goldwyn Rodrigues | 11dd35d | 2014-06-07 00:36:26 -0500 | [diff] [blame] | 2024 | return rv; |
| 2025 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2026 | EXPORT_SYMBOL_GPL(md_bitmap_copy_from_slot); |
Goldwyn Rodrigues | 11dd35d | 2014-06-07 00:36:26 -0500 | [diff] [blame] | 2027 | |
| 2028 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2029 | void md_bitmap_status(struct seq_file *seq, struct bitmap *bitmap) |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 2030 | { |
| 2031 | unsigned long chunk_kb; |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 2032 | struct bitmap_counts *counts; |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 2033 | |
| 2034 | if (!bitmap) |
| 2035 | return; |
| 2036 | |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 2037 | counts = &bitmap->counts; |
| 2038 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 2039 | chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10; |
| 2040 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 2041 | "%lu%s chunk", |
NeilBrown | 40cffcc | 2012-05-22 13:55:24 +1000 | [diff] [blame] | 2042 | counts->pages - counts->missing_pages, |
| 2043 | counts->pages, |
| 2044 | (counts->pages - counts->missing_pages) |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 2045 | << (PAGE_SHIFT - 10), |
| 2046 | chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize, |
| 2047 | chunk_kb ? "KB" : "B"); |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 2048 | if (bitmap->storage.file) { |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 2049 | seq_printf(seq, ", file: "); |
Miklos Szeredi | 2726d56 | 2015-06-19 10:30:28 +0200 | [diff] [blame] | 2050 | seq_file_path(seq, bitmap->storage.file, " \t\n"); |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | seq_printf(seq, "\n"); |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 2054 | } |
| 2055 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2056 | int md_bitmap_resize(struct bitmap *bitmap, sector_t blocks, |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2057 | 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 | |
NeilBrown | e8a27f8 | 2017-08-31 10:23:25 +1000 | [diff] [blame] | 2079 | if (bitmap->storage.file && !init) { |
| 2080 | pr_info("md: cannot resize file-based bitmap\n"); |
| 2081 | return -EINVAL; |
| 2082 | } |
| 2083 | |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2084 | 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2117 | 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 Jiang | cbb3873 | 2016-10-31 10:19:00 +0800 | [diff] [blame] | 2121 | if (ret) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2122 | md_bitmap_file_unmap(&store); |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2123 | goto err; |
Guoqing Jiang | cbb3873 | 2016-10-31 10:19:00 +0800 | [diff] [blame] | 2124 | } |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2125 | |
| 2126 | pages = DIV_ROUND_UP(chunks, PAGE_COUNTER_RATIO); |
| 2127 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 2128 | new_bp = kcalloc(pages, sizeof(*new_bp), GFP_KERNEL); |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2129 | ret = -ENOMEM; |
| 2130 | if (!new_bp) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2131 | md_bitmap_file_unmap(&store); |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2132 | 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 Li | 938b533 | 2017-10-16 19:03:44 -0700 | [diff] [blame] | 2144 | sizeof(bitmap_super_t)); |
Guoqing Jiang | fadcbd2 | 2019-09-26 13:53:50 +0200 | [diff] [blame] | 2145 | spin_lock_irq(&bitmap->counts.lock); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2146 | md_bitmap_file_unmap(&bitmap->storage); |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2147 | 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 Jiang | c9d6503 | 2016-05-02 11:50:11 -0400 | [diff] [blame] | 2161 | /* 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2165 | ret = md_bitmap_checkpage(&bitmap->counts, page, 1, 1); |
Guoqing Jiang | c9d6503 | 2016-05-02 11:50:11 -0400 | [diff] [blame] | 2166 | if (ret) { |
| 2167 | unsigned long k; |
| 2168 | |
| 2169 | /* deallocate the page memory */ |
| 2170 | for (k = 0; k < page; k++) { |
kbuild test robot | bc47e84 | 2016-05-02 11:50:16 -0400 | [diff] [blame] | 2171 | kfree(new_bp[k].map); |
Guoqing Jiang | c9d6503 | 2016-05-02 11:50:11 -0400 | [diff] [blame] | 2172 | } |
Zdenek Kabelac | 0868b99 | 2017-11-08 13:44:56 +0100 | [diff] [blame] | 2173 | kfree(new_bp); |
Guoqing Jiang | c9d6503 | 2016-05-02 11:50:11 -0400 | [diff] [blame] | 2174 | |
| 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; |
NeilBrown | ec0cc22 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2184 | pr_warn("Could not pre-allocate in-memory bitmap for cluster raid\n"); |
Guoqing Jiang | c9d6503 | 2016-05-02 11:50:11 -0400 | [diff] [blame] | 2185 | break; |
| 2186 | } else |
| 2187 | bitmap->counts.bp[page].count += 1; |
| 2188 | } |
| 2189 | } |
| 2190 | |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2191 | for (block = 0; block < blocks; ) { |
| 2192 | bitmap_counter_t *bmc_old, *bmc_new; |
| 2193 | int set; |
| 2194 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2195 | bmc_old = md_bitmap_get_counter(&old_counts, block, &old_blocks, 0); |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2196 | set = bmc_old && NEEDED(*bmc_old); |
| 2197 | |
| 2198 | if (set) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2199 | bmc_new = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1); |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2200 | 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2206 | md_bitmap_file_set_bit(bitmap, block); |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2207 | start += 1 << chunkshift; |
| 2208 | } |
| 2209 | *bmc_new = 2; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2210 | md_bitmap_count_page(&bitmap->counts, block, 1); |
| 2211 | md_bitmap_set_pending(&bitmap->counts, block); |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2212 | } |
| 2213 | *bmc_new |= NEEDED_MASK; |
| 2214 | if (new_blocks < old_blocks) |
| 2215 | old_blocks = new_blocks; |
| 2216 | } |
| 2217 | block += old_blocks; |
| 2218 | } |
| 2219 | |
Zdenek Kabelac | 0868b99 | 2017-11-08 13:44:56 +0100 | [diff] [blame] | 2220 | 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 | |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2228 | if (!init) { |
| 2229 | int i; |
| 2230 | while (block < (chunks << chunkshift)) { |
| 2231 | bitmap_counter_t *bmc; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2232 | bmc = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1); |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2233 | 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2239 | md_bitmap_count_page(&bitmap->counts, block, 1); |
| 2240 | md_bitmap_set_pending(&bitmap->counts, block); |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2241 | } |
| 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 Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2251 | md_bitmap_unplug(bitmap); |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2252 | bitmap->mddev->pers->quiesce(bitmap->mddev, 0); |
| 2253 | } |
| 2254 | ret = 0; |
| 2255 | err: |
| 2256 | return ret; |
| 2257 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2258 | EXPORT_SYMBOL_GPL(md_bitmap_resize); |
NeilBrown | d60b479 | 2012-05-22 13:55:25 +1000 | [diff] [blame] | 2259 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2260 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2261 | location_show(struct mddev *mddev, char *page) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2262 | { |
| 2263 | ssize_t len; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 2264 | if (mddev->bitmap_info.file) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2265 | len = sprintf(page, "file"); |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 2266 | else if (mddev->bitmap_info.offset) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2267 | len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset); |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 2268 | else |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2269 | len = sprintf(page, "none"); |
| 2270 | len += sprintf(page+len, "\n"); |
| 2271 | return len; |
| 2272 | } |
| 2273 | |
| 2274 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2275 | location_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2276 | { |
Shaohua Li | d9dd26b | 2016-07-30 10:05:31 -0700 | [diff] [blame] | 2277 | int rv; |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2278 | |
Shaohua Li | d9dd26b | 2016-07-30 10:05:31 -0700 | [diff] [blame] | 2279 | rv = mddev_lock(mddev); |
| 2280 | if (rv) |
| 2281 | return rv; |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2282 | if (mddev->pers) { |
Shaohua Li | d9dd26b | 2016-07-30 10:05:31 -0700 | [diff] [blame] | 2283 | 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 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2291 | } |
| 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 Li | d9dd26b | 2016-07-30 10:05:31 -0700 | [diff] [blame] | 2296 | if (strncmp(buf, "none", 4) != 0) { |
| 2297 | rv = -EBUSY; |
| 2298 | goto out; |
| 2299 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2300 | if (mddev->pers) { |
Jack Wang | f8f83d8 | 2018-10-08 17:24:03 +0200 | [diff] [blame] | 2301 | mddev_suspend(mddev); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2302 | md_bitmap_destroy(mddev); |
Jack Wang | f8f83d8 | 2018-10-08 17:24:03 +0200 | [diff] [blame] | 2303 | mddev_resume(mddev); |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2304 | } |
| 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; |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2309 | 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 Li | d9dd26b | 2016-07-30 10:05:31 -0700 | [diff] [blame] | 2318 | rv = -EINVAL; |
| 2319 | goto out; |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2320 | } else { |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2321 | if (buf[0] == '+') |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2322 | rv = kstrtoll(buf+1, 10, &offset); |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2323 | else |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2324 | rv = kstrtoll(buf, 10, &offset); |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2325 | if (rv) |
Shaohua Li | d9dd26b | 2016-07-30 10:05:31 -0700 | [diff] [blame] | 2326 | goto out; |
| 2327 | if (offset == 0) { |
| 2328 | rv = -EINVAL; |
| 2329 | goto out; |
| 2330 | } |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 2331 | if (mddev->bitmap_info.external == 0 && |
| 2332 | mddev->major_version == 0 && |
Shaohua Li | d9dd26b | 2016-07-30 10:05:31 -0700 | [diff] [blame] | 2333 | offset != mddev->bitmap_info.default_offset) { |
| 2334 | rv = -EINVAL; |
| 2335 | goto out; |
| 2336 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2337 | mddev->bitmap_info.offset = offset; |
| 2338 | if (mddev->pers) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 2339 | struct bitmap *bitmap; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2340 | bitmap = md_bitmap_create(mddev, -1); |
Jack Wang | f8f83d8 | 2018-10-08 17:24:03 +0200 | [diff] [blame] | 2341 | mddev_suspend(mddev); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 2342 | if (IS_ERR(bitmap)) |
| 2343 | rv = PTR_ERR(bitmap); |
| 2344 | else { |
| 2345 | mddev->bitmap = bitmap; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2346 | rv = md_bitmap_load(mddev); |
Guoqing Jiang | f9a67b1 | 2016-04-01 17:08:49 +0800 | [diff] [blame] | 2347 | if (rv) |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 2348 | mddev->bitmap_info.offset = 0; |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2349 | } |
Guoqing Jiang | f9a67b1 | 2016-04-01 17:08:49 +0800 | [diff] [blame] | 2350 | if (rv) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2351 | md_bitmap_destroy(mddev); |
Jack Wang | f8f83d8 | 2018-10-08 17:24:03 +0200 | [diff] [blame] | 2352 | mddev_resume(mddev); |
Shaohua Li | d9dd26b | 2016-07-30 10:05:31 -0700 | [diff] [blame] | 2353 | goto out; |
Guoqing Jiang | f9a67b1 | 2016-04-01 17:08:49 +0800 | [diff] [blame] | 2354 | } |
Jack Wang | f8f83d8 | 2018-10-08 17:24:03 +0200 | [diff] [blame] | 2355 | mddev_resume(mddev); |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2356 | } |
| 2357 | } |
| 2358 | } |
| 2359 | if (!mddev->external) { |
| 2360 | /* Ensure new bitmap info is stored in |
| 2361 | * metadata promptly. |
| 2362 | */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2363 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2364 | md_wakeup_thread(mddev->thread); |
| 2365 | } |
Shaohua Li | d9dd26b | 2016-07-30 10:05:31 -0700 | [diff] [blame] | 2366 | rv = 0; |
| 2367 | out: |
| 2368 | mddev_unlock(mddev); |
| 2369 | if (rv) |
| 2370 | return rv; |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2371 | return len; |
| 2372 | } |
| 2373 | |
| 2374 | static struct md_sysfs_entry bitmap_location = |
| 2375 | __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store); |
| 2376 | |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 2377 | /* '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 | */ |
| 2381 | static ssize_t |
| 2382 | space_show(struct mddev *mddev, char *page) |
| 2383 | { |
| 2384 | return sprintf(page, "%lu\n", mddev->bitmap_info.space); |
| 2385 | } |
| 2386 | |
| 2387 | static ssize_t |
| 2388 | space_store(struct mddev *mddev, const char *buf, size_t len) |
| 2389 | { |
| 2390 | unsigned long sectors; |
| 2391 | int rv; |
| 2392 | |
| 2393 | rv = kstrtoul(buf, 10, §ors); |
| 2394 | if (rv) |
| 2395 | return rv; |
| 2396 | |
| 2397 | if (sectors == 0) |
| 2398 | return -EINVAL; |
| 2399 | |
| 2400 | if (mddev->bitmap && |
NeilBrown | 9b1215c | 2012-05-22 13:55:11 +1000 | [diff] [blame] | 2401 | sectors < (mddev->bitmap->storage.bytes + 511) >> 9) |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 2402 | 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 | |
| 2411 | static struct md_sysfs_entry bitmap_space = |
| 2412 | __ATTR(space, S_IRUGO|S_IWUSR, space_show, space_store); |
| 2413 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2414 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2415 | timeout_show(struct mddev *mddev, char *page) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2416 | { |
| 2417 | ssize_t len; |
| 2418 | unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ; |
| 2419 | unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 2420 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2421 | 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 | |
| 2428 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2429 | timeout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2430 | { |
| 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 | |
| 2461 | static struct md_sysfs_entry bitmap_timeout = |
| 2462 | __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store); |
| 2463 | |
| 2464 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2465 | backlog_show(struct mddev *mddev, char *page) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2466 | { |
| 2467 | return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind); |
| 2468 | } |
| 2469 | |
| 2470 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2471 | backlog_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2472 | { |
| 2473 | unsigned long backlog; |
Guoqing Jiang | 10c92fc | 2019-06-14 17:10:37 +0800 | [diff] [blame] | 2474 | unsigned long old_mwb = mddev->bitmap_info.max_write_behind; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2475 | int rv = kstrtoul(buf, 10, &backlog); |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2476 | if (rv) |
| 2477 | return rv; |
| 2478 | if (backlog > COUNTER_MAX) |
| 2479 | return -EINVAL; |
| 2480 | mddev->bitmap_info.max_write_behind = backlog; |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 2481 | if (!backlog && mddev->serial_info_pool) { |
| 2482 | /* serial_info_pool is not needed if backlog is zero */ |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 2483 | if (!mddev->serialize_policy) |
| 2484 | mddev_destroy_serial_pool(mddev, NULL, false); |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 2485 | } else if (backlog && !mddev->serial_info_pool) { |
| 2486 | /* serial_info_pool is needed since backlog is not zero */ |
Guoqing Jiang | 10c92fc | 2019-06-14 17:10:37 +0800 | [diff] [blame] | 2487 | struct md_rdev *rdev; |
| 2488 | |
| 2489 | rdev_for_each(rdev, mddev) |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 2490 | mddev_create_serial_pool(mddev, rdev, false); |
Guoqing Jiang | 10c92fc | 2019-06-14 17:10:37 +0800 | [diff] [blame] | 2491 | } |
| 2492 | if (old_mwb != backlog) |
| 2493 | md_bitmap_update_sb(mddev->bitmap); |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2494 | return len; |
| 2495 | } |
| 2496 | |
| 2497 | static struct md_sysfs_entry bitmap_backlog = |
| 2498 | __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store); |
| 2499 | |
| 2500 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2501 | chunksize_show(struct mddev *mddev, char *page) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2502 | { |
| 2503 | return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize); |
| 2504 | } |
| 2505 | |
| 2506 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2507 | chunksize_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2508 | { |
| 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 Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2514 | rv = kstrtoul(buf, 10, &csize); |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2515 | 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 | |
| 2524 | static struct md_sysfs_entry bitmap_chunksize = |
| 2525 | __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store); |
| 2526 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2527 | static ssize_t metadata_show(struct mddev *mddev, char *page) |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 2528 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 2529 | if (mddev_is_clustered(mddev)) |
| 2530 | return sprintf(page, "clustered\n"); |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 2531 | return sprintf(page, "%s\n", (mddev->bitmap_info.external |
| 2532 | ? "external" : "internal")); |
| 2533 | } |
| 2534 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2535 | static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 2536 | { |
| 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 Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 2543 | else if ((strncmp(buf, "internal", 8) == 0) || |
| 2544 | (strncmp(buf, "clustered", 9) == 0)) |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 2545 | mddev->bitmap_info.external = 0; |
| 2546 | else |
| 2547 | return -EINVAL; |
| 2548 | return len; |
| 2549 | } |
| 2550 | |
| 2551 | static struct md_sysfs_entry bitmap_metadata = |
| 2552 | __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
| 2553 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2554 | static ssize_t can_clear_show(struct mddev *mddev, char *page) |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 2555 | { |
| 2556 | int len; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2557 | spin_lock(&mddev->lock); |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 2558 | if (mddev->bitmap) |
| 2559 | len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ? |
| 2560 | "false" : "true")); |
| 2561 | else |
| 2562 | len = sprintf(page, "\n"); |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2563 | spin_unlock(&mddev->lock); |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 2564 | return len; |
| 2565 | } |
| 2566 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2567 | static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 2568 | { |
| 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 | |
| 2582 | static struct md_sysfs_entry bitmap_can_clear = |
| 2583 | __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store); |
| 2584 | |
Paul Clements | 696fcd5 | 2010-03-08 16:02:37 +1100 | [diff] [blame] | 2585 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2586 | behind_writes_used_show(struct mddev *mddev, char *page) |
Paul Clements | 696fcd5 | 2010-03-08 16:02:37 +1100 | [diff] [blame] | 2587 | { |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2588 | ssize_t ret; |
| 2589 | spin_lock(&mddev->lock); |
Paul Clements | 696fcd5 | 2010-03-08 16:02:37 +1100 | [diff] [blame] | 2590 | if (mddev->bitmap == NULL) |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2591 | 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 Clements | 696fcd5 | 2010-03-08 16:02:37 +1100 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2600 | behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 696fcd5 | 2010-03-08 16:02:37 +1100 | [diff] [blame] | 2601 | { |
| 2602 | if (mddev->bitmap) |
| 2603 | mddev->bitmap->behind_writes_used = 0; |
| 2604 | return len; |
| 2605 | } |
| 2606 | |
| 2607 | static 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 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2611 | static struct attribute *md_bitmap_attrs[] = { |
| 2612 | &bitmap_location.attr, |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 2613 | &bitmap_space.attr, |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2614 | &bitmap_timeout.attr, |
| 2615 | &bitmap_backlog.attr, |
| 2616 | &bitmap_chunksize.attr, |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 2617 | &bitmap_metadata.attr, |
| 2618 | &bitmap_can_clear.attr, |
Paul Clements | 696fcd5 | 2010-03-08 16:02:37 +1100 | [diff] [blame] | 2619 | &max_backlog_used.attr, |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2620 | NULL |
| 2621 | }; |
| 2622 | struct attribute_group md_bitmap_group = { |
| 2623 | .name = "bitmap", |
| 2624 | .attrs = md_bitmap_attrs, |
| 2625 | }; |
| 2626 | |