NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003 |
| 3 | * |
| 4 | * bitmap_create - sets up the bitmap structure |
| 5 | * bitmap_destroy - destroys the bitmap structure |
| 6 | * |
| 7 | * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.: |
| 8 | * - added disk storage for bitmap |
| 9 | * - changes to allow various bitmap chunk sizes |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * Still to do: |
| 14 | * |
| 15 | * flush after percent set rather than just time based. (maybe both). |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 18 | #include <linux/blkdev.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 19 | #include <linux/module.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 20 | #include <linux/errno.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/init.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 23 | #include <linux/timer.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/list.h> |
| 26 | #include <linux/file.h> |
| 27 | #include <linux/mount.h> |
| 28 | #include <linux/buffer_head.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 29 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 30 | #include "bitmap.h" |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 31 | |
| 32 | /* debug macros */ |
| 33 | |
| 34 | #define DEBUG 0 |
| 35 | |
| 36 | #if DEBUG |
| 37 | /* these are for debugging purposes only! */ |
| 38 | |
| 39 | /* define one and only one of these */ |
| 40 | #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */ |
| 41 | #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/ |
| 42 | #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */ |
| 43 | #define INJECT_FAULTS_4 0 /* undef */ |
| 44 | #define INJECT_FAULTS_5 0 /* undef */ |
| 45 | #define INJECT_FAULTS_6 0 |
| 46 | |
| 47 | /* if these are defined, the driver will fail! debug only */ |
| 48 | #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */ |
| 49 | #define INJECT_FATAL_FAULT_2 0 /* undef */ |
| 50 | #define INJECT_FATAL_FAULT_3 0 /* undef */ |
| 51 | #endif |
| 52 | |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 53 | static inline char *bmname(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 54 | { |
| 55 | return bitmap->mddev ? mdname(bitmap->mddev) : "mdX"; |
| 56 | } |
| 57 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 58 | /* |
| 59 | * just a placeholder - calls kmalloc for bitmap pages |
| 60 | */ |
| 61 | static unsigned char *bitmap_alloc_page(struct bitmap *bitmap) |
| 62 | { |
| 63 | unsigned char *page; |
| 64 | |
Olaf Hering | 44456d3 | 2005-07-27 11:45:17 -0700 | [diff] [blame] | 65 | #ifdef INJECT_FAULTS_1 |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 66 | page = NULL; |
| 67 | #else |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 68 | page = kzalloc(PAGE_SIZE, GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 69 | #endif |
| 70 | if (!page) |
| 71 | printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap)); |
| 72 | else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 73 | pr_debug("%s: bitmap_alloc_page: allocated page at %p\n", |
| 74 | bmname(bitmap), page); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 75 | return page; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * for now just a placeholder -- just calls kfree for bitmap pages |
| 80 | */ |
| 81 | static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page) |
| 82 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 83 | pr_debug("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 84 | kfree(page); |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * check a page and, if necessary, allocate it (or hijack it if the alloc fails) |
| 89 | * |
| 90 | * 1) check to see if this page is allocated, if it's not then try to alloc |
| 91 | * 2) if the alloc fails, set the page's hijacked flag so we'll use the |
| 92 | * page pointer directly as a counter |
| 93 | * |
| 94 | * if we find our page, we increment the page's refcount so that it stays |
| 95 | * allocated while we're using it |
| 96 | */ |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 97 | static int bitmap_checkpage(struct bitmap *bitmap, |
| 98 | unsigned long page, int create) |
NeilBrown | ee305ac | 2009-09-23 18:06:44 +1000 | [diff] [blame] | 99 | __releases(bitmap->lock) |
| 100 | __acquires(bitmap->lock) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 101 | { |
| 102 | unsigned char *mappage; |
| 103 | |
| 104 | if (page >= bitmap->pages) { |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 105 | /* This can happen if bitmap_start_sync goes beyond |
| 106 | * End-of-device while looking for a whole page. |
| 107 | * It is harmless. |
| 108 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 109 | return -EINVAL; |
| 110 | } |
| 111 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 112 | if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */ |
| 113 | return 0; |
| 114 | |
| 115 | if (bitmap->bp[page].map) /* page is already allocated, just return */ |
| 116 | return 0; |
| 117 | |
| 118 | if (!create) |
| 119 | return -ENOENT; |
| 120 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 121 | /* this page has not been allocated yet */ |
| 122 | |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 123 | spin_unlock_irq(&bitmap->lock); |
| 124 | mappage = bitmap_alloc_page(bitmap); |
| 125 | spin_lock_irq(&bitmap->lock); |
| 126 | |
| 127 | if (mappage == NULL) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 128 | pr_debug("%s: bitmap map page allocation failed, hijacking\n", |
| 129 | bmname(bitmap)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 130 | /* failed - set the hijacked flag so that we can use the |
| 131 | * pointer as a counter */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 132 | if (!bitmap->bp[page].map) |
| 133 | bitmap->bp[page].hijacked = 1; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 134 | } else if (bitmap->bp[page].map || |
| 135 | bitmap->bp[page].hijacked) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 136 | /* somebody beat us to getting the page */ |
| 137 | bitmap_free_page(bitmap, mappage); |
| 138 | return 0; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 139 | } else { |
| 140 | |
| 141 | /* no page was in place and we have one, so install it */ |
| 142 | |
| 143 | bitmap->bp[page].map = mappage; |
| 144 | bitmap->missing_pages--; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 145 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 149 | /* if page is completely empty, put it back on the free list, or dealloc it */ |
| 150 | /* if page was hijacked, unmark the flag so it might get alloced next time */ |
| 151 | /* Note: lock should be held when calling this */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 152 | static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 153 | { |
| 154 | char *ptr; |
| 155 | |
| 156 | if (bitmap->bp[page].count) /* page is still busy */ |
| 157 | return; |
| 158 | |
| 159 | /* page is no longer in use, it can be released */ |
| 160 | |
| 161 | if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */ |
| 162 | bitmap->bp[page].hijacked = 0; |
| 163 | bitmap->bp[page].map = NULL; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 164 | } else { |
| 165 | /* normal case, free the page */ |
| 166 | ptr = bitmap->bp[page].map; |
| 167 | bitmap->bp[page].map = NULL; |
| 168 | bitmap->missing_pages++; |
| 169 | bitmap_free_page(bitmap, ptr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 170 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 171 | } |
| 172 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 173 | /* |
| 174 | * bitmap file handling - read and write the bitmap file and its superblock |
| 175 | */ |
| 176 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 177 | /* |
| 178 | * basic page I/O operations |
| 179 | */ |
| 180 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 181 | /* IO operations when bitmap is stored near all superblocks */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 182 | static struct page *read_sb_page(struct mddev *mddev, loff_t offset, |
NeilBrown | a2ed961 | 2008-12-19 16:25:01 +1100 | [diff] [blame] | 183 | struct page *page, |
| 184 | unsigned long index, int size) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 185 | { |
| 186 | /* choose a good rdev and read the page from there */ |
| 187 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 188 | struct md_rdev *rdev; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 189 | sector_t target; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 190 | int did_alloc = 0; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 191 | |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 192 | if (!page) { |
NeilBrown | a2ed961 | 2008-12-19 16:25:01 +1100 | [diff] [blame] | 193 | page = alloc_page(GFP_KERNEL); |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 194 | if (!page) |
| 195 | return ERR_PTR(-ENOMEM); |
| 196 | did_alloc = 1; |
| 197 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 198 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 199 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 200 | if (! test_bit(In_sync, &rdev->flags) |
| 201 | || test_bit(Faulty, &rdev->flags)) |
NeilBrown | ab904d6 | 2005-09-09 16:23:52 -0700 | [diff] [blame] | 202 | continue; |
| 203 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 204 | target = offset + index * (PAGE_SIZE/512); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 205 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 206 | if (sync_page_io(rdev, target, |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 207 | roundup(size, bdev_logical_block_size(rdev->bdev)), |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 208 | page, READ, true)) { |
NeilBrown | ab904d6 | 2005-09-09 16:23:52 -0700 | [diff] [blame] | 209 | page->index = index; |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 210 | attach_page_buffers(page, NULL); /* so that free_buffer will |
| 211 | * quietly no-op */ |
NeilBrown | ab904d6 | 2005-09-09 16:23:52 -0700 | [diff] [blame] | 212 | return page; |
| 213 | } |
| 214 | } |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 215 | if (did_alloc) |
| 216 | put_page(page); |
NeilBrown | ab904d6 | 2005-09-09 16:23:52 -0700 | [diff] [blame] | 217 | return ERR_PTR(-EIO); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 218 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 219 | } |
| 220 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 221 | 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] | 222 | { |
| 223 | /* Iterate the disks of an mddev, using rcu to protect access to the |
| 224 | * linked list, and raising the refcount of devices we return to ensure |
| 225 | * they don't disappear while in use. |
| 226 | * As devices are only added or removed when raid_disk is < 0 and |
| 227 | * nr_pending is 0 and In_sync is clear, the entries we return will |
| 228 | * still be in the same position on the list when we re-enter |
| 229 | * list_for_each_continue_rcu. |
| 230 | */ |
| 231 | struct list_head *pos; |
| 232 | rcu_read_lock(); |
| 233 | if (rdev == NULL) |
| 234 | /* start at the beginning */ |
| 235 | pos = &mddev->disks; |
| 236 | else { |
| 237 | /* release the previous rdev and start from there. */ |
| 238 | rdev_dec_pending(rdev, mddev); |
| 239 | pos = &rdev->same_set; |
| 240 | } |
| 241 | list_for_each_continue_rcu(pos, &mddev->disks) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 242 | rdev = list_entry(pos, struct md_rdev, same_set); |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 243 | if (rdev->raid_disk >= 0 && |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 244 | !test_bit(Faulty, &rdev->flags)) { |
| 245 | /* this is a usable devices */ |
| 246 | atomic_inc(&rdev->nr_pending); |
| 247 | rcu_read_unlock(); |
| 248 | return rdev; |
| 249 | } |
| 250 | } |
| 251 | rcu_read_unlock(); |
| 252 | return NULL; |
| 253 | } |
| 254 | |
NeilBrown | ab6085c | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 255 | static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 256 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 257 | struct md_rdev *rdev = NULL; |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 258 | struct block_device *bdev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 259 | struct mddev *mddev = bitmap->mddev; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 260 | |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 261 | while ((rdev = next_active_rdev(rdev, mddev)) != NULL) { |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 262 | int size = PAGE_SIZE; |
| 263 | loff_t offset = mddev->bitmap_info.offset; |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 264 | |
| 265 | bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev; |
| 266 | |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 267 | if (page->index == bitmap->file_pages-1) |
| 268 | size = roundup(bitmap->last_page_size, |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 269 | bdev_logical_block_size(bdev)); |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 270 | /* Just make sure we aren't corrupting data or |
| 271 | * metadata |
| 272 | */ |
| 273 | if (mddev->external) { |
| 274 | /* Bitmap could be anywhere. */ |
| 275 | if (rdev->sb_start + offset + (page->index |
| 276 | * (PAGE_SIZE/512)) |
| 277 | > rdev->data_offset |
| 278 | && |
| 279 | rdev->sb_start + offset |
| 280 | < (rdev->data_offset + mddev->dev_sectors |
| 281 | + (PAGE_SIZE/512))) |
| 282 | goto bad_alignment; |
| 283 | } else if (offset < 0) { |
| 284 | /* DATA BITMAP METADATA */ |
| 285 | if (offset |
| 286 | + (long)(page->index * (PAGE_SIZE/512)) |
| 287 | + size/512 > 0) |
| 288 | /* bitmap runs in to metadata */ |
| 289 | goto bad_alignment; |
| 290 | if (rdev->data_offset + mddev->dev_sectors |
| 291 | > rdev->sb_start + offset) |
| 292 | /* data runs in to bitmap */ |
| 293 | goto bad_alignment; |
| 294 | } else if (rdev->sb_start < rdev->data_offset) { |
| 295 | /* METADATA BITMAP DATA */ |
| 296 | if (rdev->sb_start |
| 297 | + offset |
| 298 | + page->index*(PAGE_SIZE/512) + size/512 |
| 299 | > rdev->data_offset) |
| 300 | /* bitmap runs in to data */ |
| 301 | goto bad_alignment; |
| 302 | } else { |
| 303 | /* DATA METADATA BITMAP - no problems */ |
| 304 | } |
| 305 | md_super_write(mddev, rdev, |
| 306 | rdev->sb_start + offset |
| 307 | + page->index * (PAGE_SIZE/512), |
| 308 | size, |
| 309 | page); |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 310 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 311 | |
| 312 | if (wait) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 313 | md_super_wait(mddev); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 314 | return 0; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 315 | |
| 316 | bad_alignment: |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 317 | return -EINVAL; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 318 | } |
| 319 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 320 | static void bitmap_file_kick(struct bitmap *bitmap); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 321 | /* |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 322 | * write out a page to a file |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 323 | */ |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 324 | static void write_page(struct bitmap *bitmap, struct page *page, int wait) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 325 | { |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 326 | struct buffer_head *bh; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 327 | |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 328 | if (bitmap->file == NULL) { |
| 329 | switch (write_sb_page(bitmap, page, wait)) { |
| 330 | case -EINVAL: |
| 331 | bitmap->flags |= BITMAP_WRITE_ERROR; |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 332 | } |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 333 | } else { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 334 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 335 | bh = page_buffers(page); |
NeilBrown | c708443 | 2006-01-06 00:20:45 -0800 | [diff] [blame] | 336 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 337 | while (bh && bh->b_blocknr) { |
| 338 | atomic_inc(&bitmap->pending_writes); |
| 339 | set_buffer_locked(bh); |
| 340 | set_buffer_mapped(bh); |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 341 | submit_bh(WRITE | REQ_SYNC, bh); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 342 | bh = bh->b_this_page; |
| 343 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 344 | |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 345 | if (wait) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 346 | wait_event(bitmap->write_wait, |
| 347 | atomic_read(&bitmap->pending_writes)==0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 348 | } |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 349 | if (bitmap->flags & BITMAP_WRITE_ERROR) |
| 350 | bitmap_file_kick(bitmap); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 351 | } |
| 352 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 353 | static void end_bitmap_write(struct buffer_head *bh, int uptodate) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 354 | { |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 355 | struct bitmap *bitmap = bh->b_private; |
| 356 | unsigned long flags; |
| 357 | |
| 358 | if (!uptodate) { |
| 359 | spin_lock_irqsave(&bitmap->lock, flags); |
| 360 | bitmap->flags |= BITMAP_WRITE_ERROR; |
| 361 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 362 | } |
| 363 | if (atomic_dec_and_test(&bitmap->pending_writes)) |
| 364 | wake_up(&bitmap->write_wait); |
| 365 | } |
| 366 | |
| 367 | /* copied from buffer.c */ |
| 368 | static void |
| 369 | __clear_page_buffers(struct page *page) |
| 370 | { |
| 371 | ClearPagePrivate(page); |
| 372 | set_page_private(page, 0); |
| 373 | page_cache_release(page); |
| 374 | } |
| 375 | static void free_buffers(struct page *page) |
| 376 | { |
| 377 | struct buffer_head *bh = page_buffers(page); |
| 378 | |
| 379 | while (bh) { |
| 380 | struct buffer_head *next = bh->b_this_page; |
| 381 | free_buffer_head(bh); |
| 382 | bh = next; |
| 383 | } |
| 384 | __clear_page_buffers(page); |
| 385 | put_page(page); |
| 386 | } |
| 387 | |
| 388 | /* read a page from a file. |
| 389 | * We both read the page, and attach buffers to the page to record the |
| 390 | * address of each block (using bmap). These addresses will be used |
| 391 | * to write the block later, completely bypassing the filesystem. |
| 392 | * This usage is similar to how swap files are handled, and allows us |
| 393 | * to write to a file with no concerns of memory allocation failing. |
| 394 | */ |
| 395 | static struct page *read_page(struct file *file, unsigned long index, |
| 396 | struct bitmap *bitmap, |
| 397 | unsigned long count) |
| 398 | { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 399 | struct page *page = NULL; |
Josef Sipek | c649bb9 | 2006-12-08 02:37:19 -0800 | [diff] [blame] | 400 | struct inode *inode = file->f_path.dentry->d_inode; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 401 | struct buffer_head *bh; |
| 402 | sector_t block; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 403 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 404 | pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE, |
| 405 | (unsigned long long)index << PAGE_SHIFT); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 406 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 407 | page = alloc_page(GFP_KERNEL); |
| 408 | if (!page) |
| 409 | page = ERR_PTR(-ENOMEM); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 410 | if (IS_ERR(page)) |
| 411 | goto out; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 412 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 413 | bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0); |
| 414 | if (!bh) { |
| 415 | put_page(page); |
| 416 | page = ERR_PTR(-ENOMEM); |
| 417 | goto out; |
| 418 | } |
| 419 | attach_page_buffers(page, bh); |
| 420 | block = index << (PAGE_SHIFT - inode->i_blkbits); |
| 421 | while (bh) { |
| 422 | if (count == 0) |
| 423 | bh->b_blocknr = 0; |
| 424 | else { |
| 425 | bh->b_blocknr = bmap(inode, block); |
| 426 | if (bh->b_blocknr == 0) { |
| 427 | /* Cannot use this file! */ |
| 428 | free_buffers(page); |
| 429 | page = ERR_PTR(-EINVAL); |
| 430 | goto out; |
| 431 | } |
| 432 | bh->b_bdev = inode->i_sb->s_bdev; |
| 433 | if (count < (1<<inode->i_blkbits)) |
| 434 | count = 0; |
| 435 | else |
| 436 | count -= (1<<inode->i_blkbits); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 437 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 438 | bh->b_end_io = end_bitmap_write; |
| 439 | bh->b_private = bitmap; |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 440 | atomic_inc(&bitmap->pending_writes); |
| 441 | set_buffer_locked(bh); |
| 442 | set_buffer_mapped(bh); |
| 443 | submit_bh(READ, bh); |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 444 | } |
| 445 | block++; |
| 446 | bh = bh->b_this_page; |
| 447 | } |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 448 | page->index = index; |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 449 | |
| 450 | wait_event(bitmap->write_wait, |
| 451 | atomic_read(&bitmap->pending_writes)==0); |
| 452 | if (bitmap->flags & BITMAP_WRITE_ERROR) { |
| 453 | free_buffers(page); |
| 454 | page = ERR_PTR(-EIO); |
| 455 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 456 | out: |
| 457 | if (IS_ERR(page)) |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 458 | printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n", |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 459 | (int)PAGE_SIZE, |
| 460 | (unsigned long long)index << PAGE_SHIFT, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 461 | PTR_ERR(page)); |
| 462 | return page; |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * bitmap file superblock operations |
| 467 | */ |
| 468 | |
| 469 | /* update the event counter and sync the superblock to disk */ |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 470 | void bitmap_update_sb(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 471 | { |
| 472 | bitmap_super_t *sb; |
| 473 | unsigned long flags; |
| 474 | |
| 475 | if (!bitmap || !bitmap->mddev) /* no bitmap for this array */ |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 476 | return; |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 477 | if (bitmap->mddev->bitmap_info.external) |
| 478 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 479 | spin_lock_irqsave(&bitmap->lock, flags); |
| 480 | if (!bitmap->sb_page) { /* no superblock */ |
| 481 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 482 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 483 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 484 | spin_unlock_irqrestore(&bitmap->lock, flags); |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 485 | sb = kmap_atomic(bitmap->sb_page, KM_USER0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 486 | sb->events = cpu_to_le64(bitmap->mddev->events); |
NeilBrown | 8258c53 | 2011-05-11 14:26:30 +1000 | [diff] [blame] | 487 | if (bitmap->mddev->events < bitmap->events_cleared) |
Neil Brown | a0da84f | 2008-06-28 08:31:22 +1000 | [diff] [blame] | 488 | /* rocking back to read-only */ |
| 489 | bitmap->events_cleared = bitmap->mddev->events; |
NeilBrown | 8258c53 | 2011-05-11 14:26:30 +1000 | [diff] [blame] | 490 | sb->events_cleared = cpu_to_le64(bitmap->events_cleared); |
| 491 | sb->state = cpu_to_le32(bitmap->flags); |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 492 | /* Just in case these have been changed via sysfs: */ |
| 493 | sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ); |
| 494 | sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind); |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 495 | kunmap_atomic(sb, KM_USER0); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 496 | write_page(bitmap, bitmap->sb_page, 1); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /* print out the bitmap file superblock */ |
| 500 | void bitmap_print_sb(struct bitmap *bitmap) |
| 501 | { |
| 502 | bitmap_super_t *sb; |
| 503 | |
| 504 | if (!bitmap || !bitmap->sb_page) |
| 505 | return; |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 506 | sb = kmap_atomic(bitmap->sb_page, KM_USER0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 507 | printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap)); |
NeilBrown | a2cff26 | 2005-06-21 17:17:20 -0700 | [diff] [blame] | 508 | printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic)); |
| 509 | printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version)); |
| 510 | printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n", |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 511 | *(__u32 *)(sb->uuid+0), |
| 512 | *(__u32 *)(sb->uuid+4), |
| 513 | *(__u32 *)(sb->uuid+8), |
| 514 | *(__u32 *)(sb->uuid+12)); |
NeilBrown | a2cff26 | 2005-06-21 17:17:20 -0700 | [diff] [blame] | 515 | printk(KERN_DEBUG " events: %llu\n", |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 516 | (unsigned long long) le64_to_cpu(sb->events)); |
NeilBrown | a2cff26 | 2005-06-21 17:17:20 -0700 | [diff] [blame] | 517 | printk(KERN_DEBUG "events cleared: %llu\n", |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 518 | (unsigned long long) le64_to_cpu(sb->events_cleared)); |
NeilBrown | a2cff26 | 2005-06-21 17:17:20 -0700 | [diff] [blame] | 519 | printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state)); |
| 520 | printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize)); |
| 521 | printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep)); |
| 522 | printk(KERN_DEBUG " sync size: %llu KB\n", |
| 523 | (unsigned long long)le64_to_cpu(sb->sync_size)/2); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 524 | printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind)); |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 525 | kunmap_atomic(sb, KM_USER0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 528 | /* |
| 529 | * bitmap_new_disk_sb |
| 530 | * @bitmap |
| 531 | * |
| 532 | * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb |
| 533 | * reads and verifies the on-disk bitmap superblock and populates bitmap_info. |
| 534 | * This function verifies 'bitmap_info' and populates the on-disk bitmap |
| 535 | * structure, which is to be written to disk. |
| 536 | * |
| 537 | * Returns: 0 on success, -Exxx on error |
| 538 | */ |
| 539 | static int bitmap_new_disk_sb(struct bitmap *bitmap) |
| 540 | { |
| 541 | bitmap_super_t *sb; |
| 542 | unsigned long chunksize, daemon_sleep, write_behind; |
| 543 | int err = -EINVAL; |
| 544 | |
| 545 | bitmap->sb_page = alloc_page(GFP_KERNEL); |
| 546 | if (IS_ERR(bitmap->sb_page)) { |
| 547 | err = PTR_ERR(bitmap->sb_page); |
| 548 | bitmap->sb_page = NULL; |
| 549 | return err; |
| 550 | } |
| 551 | bitmap->sb_page->index = 0; |
| 552 | |
| 553 | sb = kmap_atomic(bitmap->sb_page, KM_USER0); |
| 554 | |
| 555 | sb->magic = cpu_to_le32(BITMAP_MAGIC); |
| 556 | sb->version = cpu_to_le32(BITMAP_MAJOR_HI); |
| 557 | |
| 558 | chunksize = bitmap->mddev->bitmap_info.chunksize; |
| 559 | BUG_ON(!chunksize); |
| 560 | if (!is_power_of_2(chunksize)) { |
| 561 | kunmap_atomic(sb, KM_USER0); |
| 562 | printk(KERN_ERR "bitmap chunksize not a power of 2\n"); |
| 563 | return -EINVAL; |
| 564 | } |
| 565 | sb->chunksize = cpu_to_le32(chunksize); |
| 566 | |
| 567 | daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep; |
| 568 | if (!daemon_sleep || |
| 569 | (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) { |
| 570 | printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n"); |
| 571 | daemon_sleep = 5 * HZ; |
| 572 | } |
| 573 | sb->daemon_sleep = cpu_to_le32(daemon_sleep); |
| 574 | bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep; |
| 575 | |
| 576 | /* |
| 577 | * FIXME: write_behind for RAID1. If not specified, what |
| 578 | * is a good choice? We choose COUNTER_MAX / 2 arbitrarily. |
| 579 | */ |
| 580 | write_behind = bitmap->mddev->bitmap_info.max_write_behind; |
| 581 | if (write_behind > COUNTER_MAX) |
| 582 | write_behind = COUNTER_MAX / 2; |
| 583 | sb->write_behind = cpu_to_le32(write_behind); |
| 584 | bitmap->mddev->bitmap_info.max_write_behind = write_behind; |
| 585 | |
| 586 | /* keep the array size field of the bitmap superblock up to date */ |
| 587 | sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors); |
| 588 | |
| 589 | memcpy(sb->uuid, bitmap->mddev->uuid, 16); |
| 590 | |
| 591 | bitmap->flags |= BITMAP_STALE; |
| 592 | sb->state |= cpu_to_le32(BITMAP_STALE); |
| 593 | bitmap->events_cleared = bitmap->mddev->events; |
| 594 | sb->events_cleared = cpu_to_le64(bitmap->mddev->events); |
| 595 | |
| 596 | bitmap->flags |= BITMAP_HOSTENDIAN; |
| 597 | sb->version = cpu_to_le32(BITMAP_MAJOR_HOSTENDIAN); |
| 598 | |
| 599 | kunmap_atomic(sb, KM_USER0); |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 604 | /* read the superblock from the bitmap file and initialize some bitmap fields */ |
| 605 | static int bitmap_read_sb(struct bitmap *bitmap) |
| 606 | { |
| 607 | char *reason = NULL; |
| 608 | bitmap_super_t *sb; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 609 | unsigned long chunksize, daemon_sleep, write_behind; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 610 | unsigned long long events; |
| 611 | int err = -EINVAL; |
| 612 | |
| 613 | /* page 0 is the superblock, read it... */ |
NeilBrown | f49d5e6 | 2007-01-26 00:57:03 -0800 | [diff] [blame] | 614 | if (bitmap->file) { |
| 615 | loff_t isize = i_size_read(bitmap->file->f_mapping->host); |
| 616 | int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize; |
| 617 | |
| 618 | bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes); |
| 619 | } else { |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 620 | bitmap->sb_page = read_sb_page(bitmap->mddev, |
| 621 | bitmap->mddev->bitmap_info.offset, |
NeilBrown | a2ed961 | 2008-12-19 16:25:01 +1100 | [diff] [blame] | 622 | NULL, |
| 623 | 0, sizeof(bitmap_super_t)); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 624 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 625 | if (IS_ERR(bitmap->sb_page)) { |
| 626 | err = PTR_ERR(bitmap->sb_page); |
| 627 | bitmap->sb_page = NULL; |
| 628 | return err; |
| 629 | } |
| 630 | |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 631 | sb = kmap_atomic(bitmap->sb_page, KM_USER0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 632 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 633 | chunksize = le32_to_cpu(sb->chunksize); |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 634 | daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 635 | write_behind = le32_to_cpu(sb->write_behind); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 636 | |
| 637 | /* verify that the bitmap-specific fields are valid */ |
| 638 | if (sb->magic != cpu_to_le32(BITMAP_MAGIC)) |
| 639 | reason = "bad magic"; |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 640 | else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO || |
| 641 | le32_to_cpu(sb->version) > BITMAP_MAJOR_HI) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 642 | reason = "unrecognized superblock version"; |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 643 | else if (chunksize < 512) |
NeilBrown | 7dd5d34 | 2006-01-06 00:20:39 -0800 | [diff] [blame] | 644 | reason = "bitmap chunksize too small"; |
Jonathan Brassow | d744540 | 2011-06-08 18:01:10 -0500 | [diff] [blame] | 645 | else if (!is_power_of_2(chunksize)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 646 | reason = "bitmap chunksize not a power of 2"; |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 647 | else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT) |
NeilBrown | 7dd5d34 | 2006-01-06 00:20:39 -0800 | [diff] [blame] | 648 | reason = "daemon sleep period out of range"; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 649 | else if (write_behind > COUNTER_MAX) |
| 650 | reason = "write-behind limit out of range (0 - 16383)"; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 651 | if (reason) { |
| 652 | printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n", |
| 653 | bmname(bitmap), reason); |
| 654 | goto out; |
| 655 | } |
| 656 | |
| 657 | /* keep the array size field of the bitmap superblock up to date */ |
| 658 | sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors); |
| 659 | |
| 660 | if (!bitmap->mddev->persistent) |
| 661 | goto success; |
| 662 | |
| 663 | /* |
| 664 | * if we have a persistent array superblock, compare the |
| 665 | * bitmap's UUID and event counter to the mddev's |
| 666 | */ |
| 667 | if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) { |
| 668 | printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n", |
| 669 | bmname(bitmap)); |
| 670 | goto out; |
| 671 | } |
| 672 | events = le64_to_cpu(sb->events); |
| 673 | if (events < bitmap->mddev->events) { |
| 674 | printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) " |
| 675 | "-- forcing full recovery\n", bmname(bitmap), events, |
| 676 | (unsigned long long) bitmap->mddev->events); |
NeilBrown | 4f2e639 | 2006-10-21 10:24:09 -0700 | [diff] [blame] | 677 | sb->state |= cpu_to_le32(BITMAP_STALE); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 678 | } |
| 679 | success: |
| 680 | /* assign fields using values from superblock */ |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 681 | bitmap->mddev->bitmap_info.chunksize = chunksize; |
| 682 | bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 683 | bitmap->mddev->bitmap_info.max_write_behind = write_behind; |
NeilBrown | 4f2e639 | 2006-10-21 10:24:09 -0700 | [diff] [blame] | 684 | bitmap->flags |= le32_to_cpu(sb->state); |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 685 | if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN) |
| 686 | bitmap->flags |= BITMAP_HOSTENDIAN; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 687 | bitmap->events_cleared = le64_to_cpu(sb->events_cleared); |
NeilBrown | 8258c53 | 2011-05-11 14:26:30 +1000 | [diff] [blame] | 688 | if (bitmap->flags & BITMAP_STALE) |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 689 | bitmap->events_cleared = bitmap->mddev->events; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 690 | err = 0; |
| 691 | out: |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 692 | kunmap_atomic(sb, KM_USER0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 693 | if (err) |
| 694 | bitmap_print_sb(bitmap); |
| 695 | return err; |
| 696 | } |
| 697 | |
| 698 | enum bitmap_mask_op { |
| 699 | MASK_SET, |
| 700 | MASK_UNSET |
| 701 | }; |
| 702 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 703 | /* record the state of the bitmap in the superblock. Return the old value */ |
| 704 | static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits, |
| 705 | enum bitmap_mask_op op) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 706 | { |
| 707 | bitmap_super_t *sb; |
| 708 | unsigned long flags; |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 709 | int old; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 710 | |
| 711 | spin_lock_irqsave(&bitmap->lock, flags); |
Adrian Bunk | 7e31765 | 2006-03-25 03:07:51 -0800 | [diff] [blame] | 712 | if (!bitmap->sb_page) { /* can't set the state */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 713 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 714 | return 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 715 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 716 | spin_unlock_irqrestore(&bitmap->lock, flags); |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 717 | sb = kmap_atomic(bitmap->sb_page, KM_USER0); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 718 | old = le32_to_cpu(sb->state) & bits; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 719 | switch (op) { |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 720 | case MASK_SET: |
| 721 | sb->state |= cpu_to_le32(bits); |
NeilBrown | 8258c53 | 2011-05-11 14:26:30 +1000 | [diff] [blame] | 722 | bitmap->flags |= bits; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 723 | break; |
| 724 | case MASK_UNSET: |
| 725 | sb->state &= cpu_to_le32(~bits); |
NeilBrown | 8258c53 | 2011-05-11 14:26:30 +1000 | [diff] [blame] | 726 | bitmap->flags &= ~bits; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 727 | break; |
| 728 | default: |
| 729 | BUG(); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 730 | } |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 731 | kunmap_atomic(sb, KM_USER0); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 732 | return old; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | /* |
| 736 | * general bitmap file operations |
| 737 | */ |
| 738 | |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 739 | /* |
| 740 | * on-disk bitmap: |
| 741 | * |
| 742 | * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap |
| 743 | * file a page at a time. There's a superblock at the start of the file. |
| 744 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 745 | /* calculate the index of the page that contains this bit */ |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 746 | static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 747 | { |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 748 | if (!bitmap->mddev->bitmap_info.external) |
| 749 | chunk += sizeof(bitmap_super_t) << 3; |
| 750 | return chunk >> PAGE_BIT_SHIFT; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /* calculate the (bit) offset of this bit within a page */ |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 754 | static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 755 | { |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 756 | if (!bitmap->mddev->bitmap_info.external) |
| 757 | chunk += sizeof(bitmap_super_t) << 3; |
| 758 | return chunk & (PAGE_BITS - 1); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /* |
| 762 | * return a pointer to the page in the filemap that contains the given bit |
| 763 | * |
| 764 | * this lookup is complicated by the fact that the bitmap sb might be exactly |
| 765 | * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page |
| 766 | * 0 or page 1 |
| 767 | */ |
| 768 | static inline struct page *filemap_get_page(struct bitmap *bitmap, |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 769 | unsigned long chunk) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 770 | { |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 771 | if (file_page_index(bitmap, chunk) >= bitmap->file_pages) |
| 772 | return NULL; |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 773 | return bitmap->filemap[file_page_index(bitmap, chunk) |
| 774 | - file_page_index(bitmap, 0)]; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 775 | } |
| 776 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 777 | static void bitmap_file_unmap(struct bitmap *bitmap) |
| 778 | { |
| 779 | struct page **map, *sb_page; |
| 780 | unsigned long *attr; |
| 781 | int pages; |
| 782 | unsigned long flags; |
| 783 | |
| 784 | spin_lock_irqsave(&bitmap->lock, flags); |
| 785 | map = bitmap->filemap; |
| 786 | bitmap->filemap = NULL; |
| 787 | attr = bitmap->filemap_attr; |
| 788 | bitmap->filemap_attr = NULL; |
| 789 | pages = bitmap->file_pages; |
| 790 | bitmap->file_pages = 0; |
| 791 | sb_page = bitmap->sb_page; |
| 792 | bitmap->sb_page = NULL; |
| 793 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 794 | |
| 795 | while (pages--) |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 796 | if (map[pages] != sb_page) /* 0 is sb_page, release it below */ |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 797 | free_buffers(map[pages]); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 798 | kfree(map); |
| 799 | kfree(attr); |
| 800 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 801 | if (sb_page) |
| 802 | free_buffers(sb_page); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | static void bitmap_file_put(struct bitmap *bitmap) |
| 806 | { |
| 807 | struct file *file; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 808 | unsigned long flags; |
| 809 | |
| 810 | spin_lock_irqsave(&bitmap->lock, flags); |
| 811 | file = bitmap->file; |
| 812 | bitmap->file = NULL; |
| 813 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 814 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 815 | if (file) |
| 816 | wait_event(bitmap->write_wait, |
| 817 | atomic_read(&bitmap->pending_writes)==0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 818 | bitmap_file_unmap(bitmap); |
| 819 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 820 | if (file) { |
Josef Sipek | c649bb9 | 2006-12-08 02:37:19 -0800 | [diff] [blame] | 821 | struct inode *inode = file->f_path.dentry->d_inode; |
Andrew Morton | fc0ecff | 2007-02-10 01:45:39 -0800 | [diff] [blame] | 822 | invalidate_mapping_pages(inode->i_mapping, 0, -1); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 823 | fput(file); |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 824 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 825 | } |
| 826 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 827 | /* |
| 828 | * bitmap_file_kick - if an error occurs while manipulating the bitmap file |
| 829 | * then it is no longer reliable, so we stop using it and we mark the file |
| 830 | * as failed in the superblock |
| 831 | */ |
| 832 | static void bitmap_file_kick(struct bitmap *bitmap) |
| 833 | { |
| 834 | char *path, *ptr = NULL; |
| 835 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 836 | if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) { |
| 837 | bitmap_update_sb(bitmap); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 838 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 839 | if (bitmap->file) { |
| 840 | path = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 841 | if (path) |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 842 | ptr = d_path(&bitmap->file->f_path, path, |
| 843 | PAGE_SIZE); |
| 844 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 845 | printk(KERN_ALERT |
| 846 | "%s: kicking failed bitmap file %s from array!\n", |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 847 | bmname(bitmap), IS_ERR(ptr) ? "" : ptr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 848 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 849 | kfree(path); |
| 850 | } else |
| 851 | printk(KERN_ALERT |
| 852 | "%s: disabling internal bitmap due to errors\n", |
| 853 | bmname(bitmap)); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 854 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 855 | |
| 856 | bitmap_file_put(bitmap); |
| 857 | |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | enum bitmap_page_attr { |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 862 | BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */ |
NeilBrown | 5a537df | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 863 | BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned. |
| 864 | * i.e. counter is 1 or 2. */ |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 865 | BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 866 | }; |
| 867 | |
| 868 | static inline void set_page_attr(struct bitmap *bitmap, struct page *page, |
| 869 | enum bitmap_page_attr attr) |
| 870 | { |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 871 | __set_bit((page->index<<2) + attr, bitmap->filemap_attr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | static inline void clear_page_attr(struct bitmap *bitmap, struct page *page, |
| 875 | enum bitmap_page_attr attr) |
| 876 | { |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 877 | __clear_bit((page->index<<2) + attr, bitmap->filemap_attr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 878 | } |
| 879 | |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 880 | static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page, |
| 881 | enum bitmap_page_attr attr) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 882 | { |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 883 | return test_bit((page->index<<2) + attr, bitmap->filemap_attr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /* |
| 887 | * bitmap_file_set_bit -- called before performing a write to the md device |
| 888 | * to set (and eventually sync) a particular bit in the bitmap file |
| 889 | * |
| 890 | * we set the bit immediately, then we record the page number so that |
| 891 | * when an unplug occurs, we can flush the dirty pages out to disk |
| 892 | */ |
| 893 | static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block) |
| 894 | { |
| 895 | unsigned long bit; |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 896 | struct page *page; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 897 | void *kaddr; |
| 898 | unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap); |
| 899 | |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 900 | if (!bitmap->filemap) |
| 901 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 902 | |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 903 | page = filemap_get_page(bitmap, chunk); |
| 904 | if (!page) |
| 905 | return; |
| 906 | bit = file_page_offset(bitmap, chunk); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 907 | |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 908 | /* set the bit */ |
| 909 | kaddr = kmap_atomic(page, KM_USER0); |
| 910 | if (bitmap->flags & BITMAP_HOSTENDIAN) |
| 911 | set_bit(bit, kaddr); |
| 912 | else |
| 913 | __set_bit_le(bit, kaddr); |
| 914 | kunmap_atomic(kaddr, KM_USER0); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 915 | pr_debug("set file bit %lu page %lu\n", bit, page->index); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 916 | /* record page number so it gets flushed to disk when unplug occurs */ |
| 917 | set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | /* this gets called when the md device is ready to unplug its underlying |
| 921 | * (slave) device queues -- before we let any writes go down, we need to |
| 922 | * sync the dirty pages of the bitmap file to disk */ |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 923 | void bitmap_unplug(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 924 | { |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 925 | unsigned long i, flags; |
| 926 | int dirty, need_write; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 927 | struct page *page; |
| 928 | int wait = 0; |
| 929 | |
| 930 | if (!bitmap) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 931 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 932 | |
| 933 | /* look at each page to see if there are any set bits that need to be |
| 934 | * flushed out to disk */ |
| 935 | for (i = 0; i < bitmap->file_pages; i++) { |
| 936 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 937 | if (!bitmap->filemap) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 938 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 939 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 940 | } |
| 941 | page = bitmap->filemap[i]; |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 942 | dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY); |
| 943 | need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 944 | clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY); |
| 945 | clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE); |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 946 | if (dirty) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 947 | wait = 1; |
| 948 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 949 | |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 950 | if (dirty || need_write) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 951 | write_page(bitmap, page, 0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 952 | } |
| 953 | if (wait) { /* if any writes were performed, we need to wait on them */ |
NeilBrown | 0b79ccf | 2006-06-26 00:27:44 -0700 | [diff] [blame] | 954 | if (bitmap->file) |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 955 | wait_event(bitmap->write_wait, |
| 956 | atomic_read(&bitmap->pending_writes)==0); |
NeilBrown | 0b79ccf | 2006-06-26 00:27:44 -0700 | [diff] [blame] | 957 | else |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 958 | md_super_wait(bitmap->mddev); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 959 | } |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 960 | if (bitmap->flags & BITMAP_WRITE_ERROR) |
| 961 | bitmap_file_kick(bitmap); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 962 | } |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 963 | EXPORT_SYMBOL(bitmap_unplug); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 964 | |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 965 | static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 966 | /* * bitmap_init_from_disk -- called at bitmap_create time to initialize |
| 967 | * the in-memory bitmap from the on-disk bitmap -- also, sets up the |
| 968 | * memory mapping of the bitmap file |
| 969 | * Special cases: |
| 970 | * if there's no bitmap file, or if the bitmap file had been |
| 971 | * previously kicked from the array, we mark all the bits as |
| 972 | * 1's in order to cause a full resync. |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 973 | * |
| 974 | * We ignore all bits for sectors that end earlier than 'start'. |
| 975 | * This is used when reading an out-of-date bitmap... |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 976 | */ |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 977 | static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 978 | { |
| 979 | unsigned long i, chunks, index, oldindex, bit; |
| 980 | struct page *page = NULL, *oldpage = NULL; |
| 981 | unsigned long num_pages, bit_cnt = 0; |
| 982 | struct file *file; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 983 | unsigned long bytes, offset; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 984 | int outofdate; |
| 985 | int ret = -ENOSPC; |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 986 | void *paddr; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 987 | |
| 988 | chunks = bitmap->chunks; |
| 989 | file = bitmap->file; |
| 990 | |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 991 | BUG_ON(!file && !bitmap->mddev->bitmap_info.offset); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 992 | |
Olaf Hering | 44456d3 | 2005-07-27 11:45:17 -0700 | [diff] [blame] | 993 | #ifdef INJECT_FAULTS_3 |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 994 | outofdate = 1; |
| 995 | #else |
| 996 | outofdate = bitmap->flags & BITMAP_STALE; |
| 997 | #endif |
| 998 | if (outofdate) |
| 999 | printk(KERN_INFO "%s: bitmap file is out of date, doing full " |
| 1000 | "recovery\n", bmname(bitmap)); |
| 1001 | |
NeilBrown | e384e58 | 2010-06-01 19:37:34 +1000 | [diff] [blame] | 1002 | bytes = DIV_ROUND_UP(bitmap->chunks, 8); |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1003 | if (!bitmap->mddev->bitmap_info.external) |
| 1004 | bytes += sizeof(bitmap_super_t); |
NeilBrown | bc7f77d | 2005-06-21 17:17:17 -0700 | [diff] [blame] | 1005 | |
NeilBrown | e384e58 | 2010-06-01 19:37:34 +1000 | [diff] [blame] | 1006 | num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE); |
NeilBrown | bc7f77d | 2005-06-21 17:17:17 -0700 | [diff] [blame] | 1007 | |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1008 | if (file && i_size_read(file->f_mapping->host) < bytes) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1009 | printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n", |
| 1010 | bmname(bitmap), |
| 1011 | (unsigned long) i_size_read(file->f_mapping->host), |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1012 | bytes); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1013 | goto err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1014 | } |
NeilBrown | bc7f77d | 2005-06-21 17:17:17 -0700 | [diff] [blame] | 1015 | |
| 1016 | ret = -ENOMEM; |
| 1017 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1018 | bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL); |
NeilBrown | bc7f77d | 2005-06-21 17:17:17 -0700 | [diff] [blame] | 1019 | if (!bitmap->filemap) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1020 | goto err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1021 | |
NeilBrown | e16b68b | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 1022 | /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */ |
| 1023 | bitmap->filemap_attr = kzalloc( |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1024 | roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)), |
NeilBrown | e16b68b | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 1025 | GFP_KERNEL); |
NeilBrown | bc7f77d | 2005-06-21 17:17:17 -0700 | [diff] [blame] | 1026 | if (!bitmap->filemap_attr) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1027 | goto err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1028 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1029 | oldindex = ~0L; |
| 1030 | |
| 1031 | for (i = 0; i < chunks; i++) { |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 1032 | int b; |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1033 | index = file_page_index(bitmap, i); |
| 1034 | bit = file_page_offset(bitmap, i); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1035 | if (index != oldindex) { /* this is a new page, read it in */ |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 1036 | int count; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1037 | /* unmap the old page, we're done with it */ |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 1038 | if (index == num_pages-1) |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1039 | count = bytes - index * PAGE_SIZE; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 1040 | else |
| 1041 | count = PAGE_SIZE; |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1042 | if (index == 0 && bitmap->sb_page) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1043 | /* |
| 1044 | * if we're here then the superblock page |
| 1045 | * contains some bits (PAGE_SIZE != sizeof sb) |
| 1046 | * we've already read it in, so just use it |
| 1047 | */ |
| 1048 | page = bitmap->sb_page; |
| 1049 | offset = sizeof(bitmap_super_t); |
NeilBrown | 5384527 | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 1050 | if (!file) |
Vasiliy Kulikov | 5c04f55 | 2010-10-01 14:18:12 -0700 | [diff] [blame] | 1051 | page = read_sb_page( |
| 1052 | bitmap->mddev, |
| 1053 | bitmap->mddev->bitmap_info.offset, |
| 1054 | page, |
| 1055 | index, count); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1056 | } else if (file) { |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 1057 | page = read_page(file, index, bitmap, count); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1058 | offset = 0; |
| 1059 | } else { |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1060 | page = read_sb_page(bitmap->mddev, |
| 1061 | bitmap->mddev->bitmap_info.offset, |
NeilBrown | a2ed961 | 2008-12-19 16:25:01 +1100 | [diff] [blame] | 1062 | NULL, |
| 1063 | index, count); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1064 | offset = 0; |
| 1065 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1066 | if (IS_ERR(page)) { /* read error */ |
| 1067 | ret = PTR_ERR(page); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1068 | goto err; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1071 | oldindex = index; |
| 1072 | oldpage = page; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1073 | |
NeilBrown | b74fd28 | 2009-05-07 12:47:19 +1000 | [diff] [blame] | 1074 | bitmap->filemap[bitmap->file_pages++] = page; |
| 1075 | bitmap->last_page_size = count; |
| 1076 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1077 | if (outofdate) { |
| 1078 | /* |
| 1079 | * if bitmap is out of date, dirty the |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1080 | * whole page and write it out |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1081 | */ |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1082 | paddr = kmap_atomic(page, KM_USER0); |
| 1083 | memset(paddr + offset, 0xff, |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 1084 | PAGE_SIZE - offset); |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1085 | kunmap_atomic(paddr, KM_USER0); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1086 | write_page(bitmap, page, 1); |
| 1087 | |
| 1088 | ret = -EIO; |
NeilBrown | b74fd28 | 2009-05-07 12:47:19 +1000 | [diff] [blame] | 1089 | if (bitmap->flags & BITMAP_WRITE_ERROR) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1090 | goto err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1091 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1092 | } |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1093 | paddr = kmap_atomic(page, KM_USER0); |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 1094 | if (bitmap->flags & BITMAP_HOSTENDIAN) |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1095 | b = test_bit(bit, paddr); |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 1096 | else |
Akinobu Mita | 6b33aff | 2011-03-23 16:42:13 -0700 | [diff] [blame] | 1097 | b = test_bit_le(bit, paddr); |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1098 | kunmap_atomic(paddr, KM_USER0); |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 1099 | if (b) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1100 | /* if the disk bit is set, set the memory bit */ |
NeilBrown | db305e5 | 2009-05-07 12:49:06 +1000 | [diff] [blame] | 1101 | int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) |
| 1102 | >= start); |
| 1103 | bitmap_set_memory_bits(bitmap, |
| 1104 | (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap), |
| 1105 | needed); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1106 | bit_cnt++; |
| 1107 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1110 | /* everything went OK */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1111 | ret = 0; |
| 1112 | bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET); |
| 1113 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1114 | if (bit_cnt) { /* Kick recovery if any bits were set */ |
| 1115 | set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery); |
| 1116 | md_wakeup_thread(bitmap->mddev->thread); |
| 1117 | } |
| 1118 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1119 | printk(KERN_INFO "%s: bitmap initialized from disk: " |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 1120 | "read %lu/%lu pages, set %lu of %lu bits\n", |
| 1121 | bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, chunks); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1122 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1123 | return 0; |
| 1124 | |
| 1125 | err: |
| 1126 | printk(KERN_INFO "%s: bitmap initialisation failed: %d\n", |
| 1127 | bmname(bitmap), ret); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1128 | return ret; |
| 1129 | } |
| 1130 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1131 | void bitmap_write_all(struct bitmap *bitmap) |
| 1132 | { |
| 1133 | /* We don't actually write all bitmap blocks here, |
| 1134 | * just flag them as needing to be written |
| 1135 | */ |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 1136 | int i; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1137 | |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1138 | for (i = 0; i < bitmap->file_pages; i++) |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 1139 | set_page_attr(bitmap, bitmap->filemap[i], |
| 1140 | BITMAP_PAGE_NEEDWRITE); |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1141 | bitmap->allclean = 0; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1144 | static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc) |
| 1145 | { |
| 1146 | sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap); |
| 1147 | unsigned long page = chunk >> PAGE_COUNTER_SHIFT; |
| 1148 | bitmap->bp[page].count += inc; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1149 | bitmap_checkfree(bitmap, page); |
| 1150 | } |
| 1151 | static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap, |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1152 | sector_t offset, sector_t *blocks, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1153 | int create); |
| 1154 | |
| 1155 | /* |
| 1156 | * bitmap daemon -- periodically wakes up to clean bits and flush pages |
| 1157 | * out to disk |
| 1158 | */ |
| 1159 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1160 | void bitmap_daemon_work(struct mddev *mddev) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1161 | { |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1162 | struct bitmap *bitmap; |
NeilBrown | aa3163f | 2005-06-21 17:17:22 -0700 | [diff] [blame] | 1163 | unsigned long j; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1164 | unsigned long flags; |
| 1165 | struct page *page = NULL, *lastpage = NULL; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1166 | sector_t blocks; |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1167 | void *paddr; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1168 | |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1169 | /* Use a mutex to guard daemon_work against |
| 1170 | * bitmap_destroy. |
| 1171 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1172 | mutex_lock(&mddev->bitmap_info.mutex); |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1173 | bitmap = mddev->bitmap; |
| 1174 | if (bitmap == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1175 | mutex_unlock(&mddev->bitmap_info.mutex); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1176 | return; |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1177 | } |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1178 | if (time_before(jiffies, bitmap->daemon_lastrun |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1179 | + bitmap->mddev->bitmap_info.daemon_sleep)) |
NeilBrown | 7be3dfe | 2008-03-10 11:43:48 -0700 | [diff] [blame] | 1180 | goto done; |
| 1181 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1182 | bitmap->daemon_lastrun = jiffies; |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1183 | if (bitmap->allclean) { |
| 1184 | bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1185 | goto done; |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1186 | } |
| 1187 | bitmap->allclean = 1; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1188 | |
NeilBrown | be51269 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 1189 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1190 | for (j = 0; j < bitmap->chunks; j++) { |
| 1191 | bitmap_counter_t *bmc; |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 1192 | if (!bitmap->filemap) |
| 1193 | /* error or shutdown */ |
| 1194 | break; |
| 1195 | |
| 1196 | page = filemap_get_page(bitmap, j); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1197 | |
| 1198 | if (page != lastpage) { |
NeilBrown | aa3163f | 2005-06-21 17:17:22 -0700 | [diff] [blame] | 1199 | /* skip this page unless it's marked as needing cleaning */ |
NeilBrown | 5a537df | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1200 | if (!test_page_attr(bitmap, page, BITMAP_PAGE_PENDING)) { |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 1201 | int need_write = test_page_attr(bitmap, page, |
| 1202 | BITMAP_PAGE_NEEDWRITE); |
NeilBrown | a647e4b | 2006-06-26 00:27:46 -0700 | [diff] [blame] | 1203 | if (need_write) |
NeilBrown | aa3163f | 2005-06-21 17:17:22 -0700 | [diff] [blame] | 1204 | clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE); |
NeilBrown | a647e4b | 2006-06-26 00:27:46 -0700 | [diff] [blame] | 1205 | |
NeilBrown | aa3163f | 2005-06-21 17:17:22 -0700 | [diff] [blame] | 1206 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1207 | if (need_write) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1208 | write_page(bitmap, page, 0); |
NeilBrown | be51269 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 1209 | spin_lock_irqsave(&bitmap->lock, flags); |
| 1210 | j |= (PAGE_BITS - 1); |
NeilBrown | aa3163f | 2005-06-21 17:17:22 -0700 | [diff] [blame] | 1211 | continue; |
| 1212 | } |
| 1213 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1214 | /* grab the new page, sync and release the old */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1215 | if (lastpage != NULL) { |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1216 | if (test_page_attr(bitmap, lastpage, |
| 1217 | BITMAP_PAGE_NEEDWRITE)) { |
| 1218 | clear_page_attr(bitmap, lastpage, |
| 1219 | BITMAP_PAGE_NEEDWRITE); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1220 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1221 | write_page(bitmap, lastpage, 0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1222 | } else { |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1223 | set_page_attr(bitmap, lastpage, |
| 1224 | BITMAP_PAGE_NEEDWRITE); |
| 1225 | bitmap->allclean = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1226 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 1227 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1228 | } else |
| 1229 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 1230 | lastpage = page; |
Neil Brown | a0da84f | 2008-06-28 08:31:22 +1000 | [diff] [blame] | 1231 | |
| 1232 | /* We are possibly going to clear some bits, so make |
| 1233 | * sure that events_cleared is up-to-date. |
| 1234 | */ |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1235 | if (bitmap->need_sync && |
| 1236 | bitmap->mddev->bitmap_info.external == 0) { |
Neil Brown | a0da84f | 2008-06-28 08:31:22 +1000 | [diff] [blame] | 1237 | bitmap_super_t *sb; |
| 1238 | bitmap->need_sync = 0; |
| 1239 | sb = kmap_atomic(bitmap->sb_page, KM_USER0); |
| 1240 | sb->events_cleared = |
| 1241 | cpu_to_le64(bitmap->events_cleared); |
| 1242 | kunmap_atomic(sb, KM_USER0); |
| 1243 | write_page(bitmap, bitmap->sb_page, 1); |
| 1244 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1245 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1246 | if (!bitmap->need_sync) |
NeilBrown | 5a537df | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1247 | clear_page_attr(bitmap, page, BITMAP_PAGE_PENDING); |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1248 | else |
| 1249 | bitmap->allclean = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1250 | } |
NeilBrown | db305e5 | 2009-05-07 12:49:06 +1000 | [diff] [blame] | 1251 | bmc = bitmap_get_counter(bitmap, |
| 1252 | (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap), |
| 1253 | &blocks, 0); |
NeilBrown | 5a537df | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1254 | if (!bmc) |
| 1255 | j |= PAGE_COUNTER_MASK; |
| 1256 | else if (*bmc) { |
NeilBrown | 5a537df | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1257 | if (*bmc == 1 && !bitmap->need_sync) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1258 | /* we can clear the bit */ |
| 1259 | *bmc = 0; |
NeilBrown | db305e5 | 2009-05-07 12:49:06 +1000 | [diff] [blame] | 1260 | bitmap_count_page(bitmap, |
| 1261 | (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap), |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1262 | -1); |
| 1263 | |
| 1264 | /* clear the bit */ |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 1265 | paddr = kmap_atomic(page, KM_USER0); |
| 1266 | if (bitmap->flags & BITMAP_HOSTENDIAN) |
| 1267 | clear_bit(file_page_offset(bitmap, j), |
| 1268 | paddr); |
| 1269 | else |
| 1270 | __clear_bit_le( |
NeilBrown | 5a537df | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1271 | file_page_offset(bitmap, |
| 1272 | j), |
| 1273 | paddr); |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 1274 | kunmap_atomic(paddr, KM_USER0); |
NeilBrown | 5a537df | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1275 | } else if (*bmc <= 2) { |
| 1276 | *bmc = 1; /* maybe clear the bit next time */ |
| 1277 | set_page_attr(bitmap, page, BITMAP_PAGE_PENDING); |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1278 | bitmap->allclean = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1279 | } |
NeilBrown | 5a537df | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1280 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1281 | } |
NeilBrown | be51269 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 1282 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1283 | |
| 1284 | /* now sync the final page */ |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 1285 | if (lastpage != NULL) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1286 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 1287 | if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1288 | clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); |
| 1289 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 1290 | write_page(bitmap, lastpage, 0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1291 | } else { |
| 1292 | set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1293 | bitmap->allclean = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1294 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 1295 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
NeilBrown | 7be3dfe | 2008-03-10 11:43:48 -0700 | [diff] [blame] | 1298 | done: |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1299 | if (bitmap->allclean == 0) |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1300 | bitmap->mddev->thread->timeout = |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1301 | bitmap->mddev->bitmap_info.daemon_sleep; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1302 | mutex_unlock(&mddev->bitmap_info.mutex); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1305 | static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap, |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1306 | sector_t offset, sector_t *blocks, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1307 | int create) |
NeilBrown | ee305ac | 2009-09-23 18:06:44 +1000 | [diff] [blame] | 1308 | __releases(bitmap->lock) |
| 1309 | __acquires(bitmap->lock) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1310 | { |
| 1311 | /* If 'create', we might release the lock and reclaim it. |
| 1312 | * The lock must have been taken with interrupts enabled. |
| 1313 | * If !create, we don't release the lock. |
| 1314 | */ |
| 1315 | sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap); |
| 1316 | unsigned long page = chunk >> PAGE_COUNTER_SHIFT; |
| 1317 | unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT; |
| 1318 | sector_t csize; |
NeilBrown | ef42567 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 1319 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1320 | |
NeilBrown | ef42567 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 1321 | err = bitmap_checkpage(bitmap, page, create); |
| 1322 | |
| 1323 | if (bitmap->bp[page].hijacked || |
| 1324 | bitmap->bp[page].map == NULL) |
| 1325 | csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) + |
| 1326 | PAGE_COUNTER_SHIFT - 1); |
| 1327 | else |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1328 | csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap)); |
NeilBrown | ef42567 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 1329 | *blocks = csize - (offset & (csize - 1)); |
| 1330 | |
| 1331 | if (err < 0) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1332 | return NULL; |
NeilBrown | ef42567 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 1333 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1334 | /* now locked ... */ |
| 1335 | |
| 1336 | if (bitmap->bp[page].hijacked) { /* hijacked pointer */ |
| 1337 | /* should we use the first or second counter field |
| 1338 | * of the hijacked pointer? */ |
| 1339 | int hi = (pageoff > PAGE_COUNTER_MASK); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1340 | return &((bitmap_counter_t *) |
| 1341 | &bitmap->bp[page].map)[hi]; |
NeilBrown | ef42567 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 1342 | } else /* page is allocated */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1343 | return (bitmap_counter_t *) |
| 1344 | &(bitmap->bp[page].map[pageoff]); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1347 | int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1348 | { |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1349 | if (!bitmap) |
| 1350 | return 0; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1351 | |
| 1352 | if (behind) { |
Paul Clements | 696fcd5 | 2010-03-08 16:02:37 +1100 | [diff] [blame] | 1353 | int bw; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1354 | atomic_inc(&bitmap->behind_writes); |
Paul Clements | 696fcd5 | 2010-03-08 16:02:37 +1100 | [diff] [blame] | 1355 | bw = atomic_read(&bitmap->behind_writes); |
| 1356 | if (bw > bitmap->behind_writes_used) |
| 1357 | bitmap->behind_writes_used = bw; |
| 1358 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 1359 | pr_debug("inc write-behind count %d/%lu\n", |
| 1360 | bw, bitmap->mddev->bitmap_info.max_write_behind); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1363 | while (sectors) { |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1364 | sector_t blocks; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1365 | bitmap_counter_t *bmc; |
| 1366 | |
| 1367 | spin_lock_irq(&bitmap->lock); |
| 1368 | bmc = bitmap_get_counter(bitmap, offset, &blocks, 1); |
| 1369 | if (!bmc) { |
| 1370 | spin_unlock_irq(&bitmap->lock); |
| 1371 | return 0; |
| 1372 | } |
| 1373 | |
Namhyung Kim | 27d5ea0 | 2011-06-09 11:42:57 +1000 | [diff] [blame] | 1374 | if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) { |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1375 | DEFINE_WAIT(__wait); |
| 1376 | /* note that it is safe to do the prepare_to_wait |
| 1377 | * after the test as long as we do it before dropping |
| 1378 | * the spinlock. |
| 1379 | */ |
| 1380 | prepare_to_wait(&bitmap->overflow_wait, &__wait, |
| 1381 | TASK_UNINTERRUPTIBLE); |
| 1382 | spin_unlock_irq(&bitmap->lock); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 1383 | io_schedule(); |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1384 | finish_wait(&bitmap->overflow_wait, &__wait); |
| 1385 | continue; |
| 1386 | } |
| 1387 | |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1388 | switch (*bmc) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1389 | case 0: |
| 1390 | bitmap_file_set_bit(bitmap, offset); |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1391 | bitmap_count_page(bitmap, offset, 1); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1392 | /* fall through */ |
| 1393 | case 1: |
| 1394 | *bmc = 2; |
| 1395 | } |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1396 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1397 | (*bmc)++; |
| 1398 | |
| 1399 | spin_unlock_irq(&bitmap->lock); |
| 1400 | |
| 1401 | offset += blocks; |
| 1402 | if (sectors > blocks) |
| 1403 | sectors -= blocks; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1404 | else |
| 1405 | sectors = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1406 | } |
| 1407 | return 0; |
| 1408 | } |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1409 | EXPORT_SYMBOL(bitmap_startwrite); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1410 | |
| 1411 | void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1412 | int success, int behind) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1413 | { |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1414 | if (!bitmap) |
| 1415 | return; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1416 | if (behind) { |
NeilBrown | e555190 | 2010-03-31 11:21:44 +1100 | [diff] [blame] | 1417 | if (atomic_dec_and_test(&bitmap->behind_writes)) |
| 1418 | wake_up(&bitmap->behind_wait); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 1419 | pr_debug("dec write-behind count %d/%lu\n", |
| 1420 | atomic_read(&bitmap->behind_writes), |
| 1421 | bitmap->mddev->bitmap_info.max_write_behind); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1422 | } |
NeilBrown | d0a4bb4 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1423 | if (bitmap->mddev->degraded) |
| 1424 | /* Never clear bits or update events_cleared when degraded */ |
| 1425 | success = 0; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1426 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1427 | while (sectors) { |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1428 | sector_t blocks; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1429 | unsigned long flags; |
| 1430 | bitmap_counter_t *bmc; |
| 1431 | |
| 1432 | spin_lock_irqsave(&bitmap->lock, flags); |
| 1433 | bmc = bitmap_get_counter(bitmap, offset, &blocks, 0); |
| 1434 | if (!bmc) { |
| 1435 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 1436 | return; |
| 1437 | } |
| 1438 | |
Neil Brown | a0da84f | 2008-06-28 08:31:22 +1000 | [diff] [blame] | 1439 | if (success && |
| 1440 | bitmap->events_cleared < bitmap->mddev->events) { |
| 1441 | bitmap->events_cleared = bitmap->mddev->events; |
| 1442 | bitmap->need_sync = 1; |
NeilBrown | 5ff5aff | 2010-06-01 19:37:32 +1000 | [diff] [blame] | 1443 | sysfs_notify_dirent_safe(bitmap->sysfs_can_clear); |
Neil Brown | a0da84f | 2008-06-28 08:31:22 +1000 | [diff] [blame] | 1444 | } |
| 1445 | |
Namhyung Kim | 27d5ea0 | 2011-06-09 11:42:57 +1000 | [diff] [blame] | 1446 | if (!success && !NEEDED(*bmc)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1447 | *bmc |= NEEDED_MASK; |
| 1448 | |
Namhyung Kim | 27d5ea0 | 2011-06-09 11:42:57 +1000 | [diff] [blame] | 1449 | if (COUNTER(*bmc) == COUNTER_MAX) |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1450 | wake_up(&bitmap->overflow_wait); |
| 1451 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1452 | (*bmc)--; |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1453 | if (*bmc <= 2) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1454 | set_page_attr(bitmap, |
NeilBrown | e384e58 | 2010-06-01 19:37:34 +1000 | [diff] [blame] | 1455 | filemap_get_page( |
| 1456 | bitmap, |
| 1457 | offset >> CHUNK_BLOCK_SHIFT(bitmap)), |
NeilBrown | 5a537df | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1458 | BITMAP_PAGE_PENDING); |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1459 | bitmap->allclean = 0; |
| 1460 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1461 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 1462 | offset += blocks; |
| 1463 | if (sectors > blocks) |
| 1464 | sectors -= blocks; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1465 | else |
| 1466 | sectors = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1467 | } |
| 1468 | } |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1469 | EXPORT_SYMBOL(bitmap_endwrite); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1470 | |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1471 | 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] | 1472 | int degraded) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1473 | { |
| 1474 | bitmap_counter_t *bmc; |
| 1475 | int rv; |
| 1476 | if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */ |
| 1477 | *blocks = 1024; |
| 1478 | return 1; /* always resync if no bitmap */ |
| 1479 | } |
| 1480 | spin_lock_irq(&bitmap->lock); |
| 1481 | bmc = bitmap_get_counter(bitmap, offset, blocks, 0); |
| 1482 | rv = 0; |
| 1483 | if (bmc) { |
| 1484 | /* locked */ |
| 1485 | if (RESYNC(*bmc)) |
| 1486 | rv = 1; |
| 1487 | else if (NEEDED(*bmc)) { |
| 1488 | rv = 1; |
NeilBrown | 6a806c5 | 2005-07-15 03:56:35 -0700 | [diff] [blame] | 1489 | if (!degraded) { /* don't set/clear bits if degraded */ |
| 1490 | *bmc |= RESYNC_MASK; |
| 1491 | *bmc &= ~NEEDED_MASK; |
| 1492 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | spin_unlock_irq(&bitmap->lock); |
| 1496 | return rv; |
| 1497 | } |
| 1498 | |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1499 | int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1500 | int degraded) |
| 1501 | { |
| 1502 | /* bitmap_start_sync must always report on multiples of whole |
| 1503 | * pages, otherwise resync (which is very PAGE_SIZE based) will |
| 1504 | * get confused. |
| 1505 | * So call __bitmap_start_sync repeatedly (if needed) until |
| 1506 | * At least PAGE_SIZE>>9 blocks are covered. |
| 1507 | * Return the 'or' of the result. |
| 1508 | */ |
| 1509 | int rv = 0; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1510 | sector_t blocks1; |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1511 | |
| 1512 | *blocks = 0; |
| 1513 | while (*blocks < (PAGE_SIZE>>9)) { |
| 1514 | rv |= __bitmap_start_sync(bitmap, offset, |
| 1515 | &blocks1, degraded); |
| 1516 | offset += blocks1; |
| 1517 | *blocks += blocks1; |
| 1518 | } |
| 1519 | return rv; |
| 1520 | } |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1521 | EXPORT_SYMBOL(bitmap_start_sync); |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1522 | |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1523 | void 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] | 1524 | { |
| 1525 | bitmap_counter_t *bmc; |
| 1526 | unsigned long flags; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1527 | |
| 1528 | if (bitmap == NULL) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1529 | *blocks = 1024; |
| 1530 | return; |
| 1531 | } |
| 1532 | spin_lock_irqsave(&bitmap->lock, flags); |
| 1533 | bmc = bitmap_get_counter(bitmap, offset, blocks, 0); |
| 1534 | if (bmc == NULL) |
| 1535 | goto unlock; |
| 1536 | /* locked */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1537 | if (RESYNC(*bmc)) { |
| 1538 | *bmc &= ~RESYNC_MASK; |
| 1539 | |
| 1540 | if (!NEEDED(*bmc) && aborted) |
| 1541 | *bmc |= NEEDED_MASK; |
| 1542 | else { |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1543 | if (*bmc <= 2) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1544 | set_page_attr(bitmap, |
| 1545 | filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)), |
NeilBrown | 5a537df | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1546 | BITMAP_PAGE_PENDING); |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1547 | bitmap->allclean = 0; |
| 1548 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | unlock: |
| 1552 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 1553 | } |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1554 | EXPORT_SYMBOL(bitmap_end_sync); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1555 | |
| 1556 | void bitmap_close_sync(struct bitmap *bitmap) |
| 1557 | { |
| 1558 | /* Sync has finished, and any bitmap chunks that weren't synced |
| 1559 | * properly have been aborted. It remains to us to clear the |
| 1560 | * RESYNC bit wherever it is still on |
| 1561 | */ |
| 1562 | sector_t sector = 0; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1563 | sector_t blocks; |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1564 | if (!bitmap) |
| 1565 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1566 | while (sector < bitmap->mddev->resync_max_sectors) { |
| 1567 | bitmap_end_sync(bitmap, sector, &blocks, 0); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1568 | sector += blocks; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1569 | } |
| 1570 | } |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1571 | EXPORT_SYMBOL(bitmap_close_sync); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1572 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1573 | void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector) |
| 1574 | { |
| 1575 | sector_t s = 0; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1576 | sector_t blocks; |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1577 | |
| 1578 | if (!bitmap) |
| 1579 | return; |
| 1580 | if (sector == 0) { |
| 1581 | bitmap->last_end_sync = jiffies; |
| 1582 | return; |
| 1583 | } |
| 1584 | if (time_before(jiffies, (bitmap->last_end_sync |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1585 | + bitmap->mddev->bitmap_info.daemon_sleep))) |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1586 | return; |
| 1587 | wait_event(bitmap->mddev->recovery_wait, |
| 1588 | atomic_read(&bitmap->mddev->recovery_active) == 0); |
| 1589 | |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 1590 | bitmap->mddev->curr_resync_completed = sector; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 1591 | set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1592 | sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1); |
| 1593 | s = 0; |
| 1594 | while (s < sector && s < bitmap->mddev->resync_max_sectors) { |
| 1595 | bitmap_end_sync(bitmap, s, &blocks, 0); |
| 1596 | s += blocks; |
| 1597 | } |
| 1598 | bitmap->last_end_sync = jiffies; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 1599 | sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed"); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1600 | } |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1601 | EXPORT_SYMBOL(bitmap_cond_end_sync); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1602 | |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 1603 | static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1604 | { |
| 1605 | /* For each chunk covered by any of these sectors, set the |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1606 | * counter to 1 and set resync_needed. They should all |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1607 | * be 0 at this point |
| 1608 | */ |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1609 | |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1610 | sector_t secs; |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1611 | bitmap_counter_t *bmc; |
| 1612 | spin_lock_irq(&bitmap->lock); |
| 1613 | bmc = bitmap_get_counter(bitmap, offset, &secs, 1); |
| 1614 | if (!bmc) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1615 | spin_unlock_irq(&bitmap->lock); |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1616 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1617 | } |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1618 | if (!*bmc) { |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1619 | struct page *page; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1620 | *bmc = 1 | (needed ? NEEDED_MASK : 0); |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1621 | bitmap_count_page(bitmap, offset, 1); |
| 1622 | page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)); |
NeilBrown | 5a537df | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1623 | set_page_attr(bitmap, page, BITMAP_PAGE_PENDING); |
NeilBrown | 2585f3e | 2011-09-21 15:37:46 +1000 | [diff] [blame] | 1624 | bitmap->allclean = 0; |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1625 | } |
| 1626 | spin_unlock_irq(&bitmap->lock); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 1629 | /* dirty the memory and file bits for bitmap chunks "s" to "e" */ |
| 1630 | void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e) |
| 1631 | { |
| 1632 | unsigned long chunk; |
| 1633 | |
| 1634 | for (chunk = s; chunk <= e; chunk++) { |
NeilBrown | db305e5 | 2009-05-07 12:49:06 +1000 | [diff] [blame] | 1635 | sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 1636 | bitmap_set_memory_bits(bitmap, sec, 1); |
| 1637 | bitmap_file_set_bit(bitmap, sec); |
NeilBrown | ffa2332 | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1638 | if (sec < bitmap->mddev->recovery_cp) |
| 1639 | /* We are asserting that the array is dirty, |
| 1640 | * so move the recovery_cp address back so |
| 1641 | * that it is obvious that it is dirty |
| 1642 | */ |
| 1643 | bitmap->mddev->recovery_cp = sec; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 1644 | } |
| 1645 | } |
| 1646 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1647 | /* |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 1648 | * flush out any pending updates |
| 1649 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1650 | void bitmap_flush(struct mddev *mddev) |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 1651 | { |
| 1652 | struct bitmap *bitmap = mddev->bitmap; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1653 | long sleep; |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 1654 | |
| 1655 | if (!bitmap) /* there was no bitmap */ |
| 1656 | return; |
| 1657 | |
| 1658 | /* run the daemon_work three time to ensure everything is flushed |
| 1659 | * that can be |
| 1660 | */ |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1661 | sleep = mddev->bitmap_info.daemon_sleep * 2; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1662 | bitmap->daemon_lastrun -= sleep; |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1663 | bitmap_daemon_work(mddev); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1664 | bitmap->daemon_lastrun -= sleep; |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1665 | bitmap_daemon_work(mddev); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1666 | bitmap->daemon_lastrun -= sleep; |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1667 | bitmap_daemon_work(mddev); |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 1668 | bitmap_update_sb(bitmap); |
| 1669 | } |
| 1670 | |
| 1671 | /* |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1672 | * free memory that was allocated |
| 1673 | */ |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1674 | static void bitmap_free(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1675 | { |
| 1676 | unsigned long k, pages; |
| 1677 | struct bitmap_page *bp; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1678 | |
| 1679 | if (!bitmap) /* there was no bitmap */ |
| 1680 | return; |
| 1681 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1682 | /* release the bitmap file and kill the daemon */ |
| 1683 | bitmap_file_put(bitmap); |
| 1684 | |
| 1685 | bp = bitmap->bp; |
| 1686 | pages = bitmap->pages; |
| 1687 | |
| 1688 | /* free all allocated memory */ |
| 1689 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1690 | if (bp) /* deallocate the page memory */ |
| 1691 | for (k = 0; k < pages; k++) |
| 1692 | if (bp[k].map && !bp[k].hijacked) |
| 1693 | kfree(bp[k].map); |
| 1694 | kfree(bp); |
| 1695 | kfree(bitmap); |
| 1696 | } |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1697 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1698 | void bitmap_destroy(struct mddev *mddev) |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1699 | { |
| 1700 | struct bitmap *bitmap = mddev->bitmap; |
| 1701 | |
| 1702 | if (!bitmap) /* there was no bitmap */ |
| 1703 | return; |
| 1704 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1705 | mutex_lock(&mddev->bitmap_info.mutex); |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1706 | mddev->bitmap = NULL; /* disconnect from the md device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1707 | mutex_unlock(&mddev->bitmap_info.mutex); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1708 | if (mddev->thread) |
| 1709 | mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1710 | |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1711 | if (bitmap->sysfs_can_clear) |
| 1712 | sysfs_put(bitmap->sysfs_can_clear); |
| 1713 | |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1714 | bitmap_free(bitmap); |
| 1715 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1716 | |
| 1717 | /* |
| 1718 | * initialize the bitmap structure |
| 1719 | * if this returns an error, bitmap_destroy must be called to do clean up |
| 1720 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1721 | int bitmap_create(struct mddev *mddev) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1722 | { |
| 1723 | struct bitmap *bitmap; |
NeilBrown | 1f59390 | 2009-04-20 11:50:24 +1000 | [diff] [blame] | 1724 | sector_t blocks = mddev->resync_max_sectors; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1725 | unsigned long chunks; |
| 1726 | unsigned long pages; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1727 | struct file *file = mddev->bitmap_info.file; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1728 | int err; |
NeilBrown | 5ff5aff | 2010-06-01 19:37:32 +1000 | [diff] [blame] | 1729 | struct sysfs_dirent *bm = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1730 | |
Alexey Dobriyan | 5f6e3c83 | 2006-10-11 01:22:26 -0700 | [diff] [blame] | 1731 | BUILD_BUG_ON(sizeof(bitmap_super_t) != 256); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1732 | |
NeilBrown | e384e58 | 2010-06-01 19:37:34 +1000 | [diff] [blame] | 1733 | if (!file |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 1734 | && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1735 | return 0; |
| 1736 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1737 | BUG_ON(file && mddev->bitmap_info.offset); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1738 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 1739 | bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1740 | if (!bitmap) |
| 1741 | return -ENOMEM; |
| 1742 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1743 | spin_lock_init(&bitmap->lock); |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 1744 | atomic_set(&bitmap->pending_writes, 0); |
| 1745 | init_waitqueue_head(&bitmap->write_wait); |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1746 | init_waitqueue_head(&bitmap->overflow_wait); |
NeilBrown | e555190 | 2010-03-31 11:21:44 +1100 | [diff] [blame] | 1747 | init_waitqueue_head(&bitmap->behind_wait); |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 1748 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1749 | bitmap->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1750 | |
NeilBrown | 5ff5aff | 2010-06-01 19:37:32 +1000 | [diff] [blame] | 1751 | if (mddev->kobj.sd) |
| 1752 | bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap"); |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1753 | if (bm) { |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 1754 | bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear"); |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1755 | sysfs_put(bm); |
| 1756 | } else |
| 1757 | bitmap->sysfs_can_clear = NULL; |
| 1758 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1759 | bitmap->file = file; |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 1760 | if (file) { |
| 1761 | get_file(file); |
NeilBrown | ae8fa28 | 2009-10-16 15:56:01 +1100 | [diff] [blame] | 1762 | /* As future accesses to this file will use bmap, |
| 1763 | * and bypass the page cache, we must sync the file |
| 1764 | * first. |
| 1765 | */ |
Christoph Hellwig | 8018ab0 | 2010-03-22 17:32:25 +0100 | [diff] [blame] | 1766 | vfs_fsync(file, 1); |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 1767 | } |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1768 | /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */ |
Jonathan Brassow | 9c81075 | 2011-06-08 17:59:30 -0500 | [diff] [blame] | 1769 | if (!mddev->bitmap_info.external) { |
| 1770 | /* |
| 1771 | * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is |
| 1772 | * instructing us to create a new on-disk bitmap instance. |
| 1773 | */ |
| 1774 | if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags)) |
| 1775 | err = bitmap_new_disk_sb(bitmap); |
| 1776 | else |
| 1777 | err = bitmap_read_sb(bitmap); |
| 1778 | } else { |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1779 | err = 0; |
| 1780 | if (mddev->bitmap_info.chunksize == 0 || |
| 1781 | mddev->bitmap_info.daemon_sleep == 0) |
| 1782 | /* chunksize and time_base need to be |
| 1783 | * set first. */ |
| 1784 | err = -EINVAL; |
| 1785 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1786 | if (err) |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1787 | goto error; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1788 | |
NeilBrown | 624ce4f | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1789 | bitmap->daemon_lastrun = jiffies; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1790 | bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1791 | |
| 1792 | /* now that chunksize and chunkshift are set, we can use these macros */ |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1793 | chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >> |
NeilBrown | 1f59390 | 2009-04-20 11:50:24 +1000 | [diff] [blame] | 1794 | CHUNK_BLOCK_SHIFT(bitmap); |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1795 | pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1796 | |
| 1797 | BUG_ON(!pages); |
| 1798 | |
| 1799 | bitmap->chunks = chunks; |
| 1800 | bitmap->pages = pages; |
| 1801 | bitmap->missing_pages = pages; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1802 | |
Olaf Hering | 44456d3 | 2005-07-27 11:45:17 -0700 | [diff] [blame] | 1803 | #ifdef INJECT_FATAL_FAULT_1 |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1804 | bitmap->bp = NULL; |
| 1805 | #else |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 1806 | bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1807 | #endif |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1808 | err = -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1809 | if (!bitmap->bp) |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1810 | goto error; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1811 | |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1812 | printk(KERN_INFO "created bitmap (%lu pages) for device %s\n", |
| 1813 | pages, bmname(bitmap)); |
| 1814 | |
| 1815 | mddev->bitmap = bitmap; |
| 1816 | |
| 1817 | |
| 1818 | return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0; |
| 1819 | |
| 1820 | error: |
| 1821 | bitmap_free(bitmap); |
| 1822 | return err; |
| 1823 | } |
| 1824 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1825 | int bitmap_load(struct mddev *mddev) |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1826 | { |
| 1827 | int err = 0; |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 1828 | sector_t start = 0; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1829 | sector_t sector = 0; |
| 1830 | struct bitmap *bitmap = mddev->bitmap; |
| 1831 | |
| 1832 | if (!bitmap) |
| 1833 | goto out; |
| 1834 | |
| 1835 | /* Clear out old bitmap info first: Either there is none, or we |
| 1836 | * are resuming after someone else has possibly changed things, |
| 1837 | * so we should forget old cached info. |
| 1838 | * All chunks should be clean, but some might need_sync. |
| 1839 | */ |
| 1840 | while (sector < mddev->resync_max_sectors) { |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1841 | sector_t blocks; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1842 | bitmap_start_sync(bitmap, sector, &blocks, 0); |
| 1843 | sector += blocks; |
| 1844 | } |
| 1845 | bitmap_close_sync(bitmap); |
| 1846 | |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 1847 | if (mddev->degraded == 0 |
| 1848 | || bitmap->events_cleared == mddev->events) |
| 1849 | /* no need to keep dirty bits to optimise a |
| 1850 | * re-add of a missing device */ |
| 1851 | start = mddev->recovery_cp; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1852 | |
Jonathan Brassow | 3520fa4 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 1853 | err = bitmap_init_from_disk(bitmap, start); |
| 1854 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1855 | if (err) |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1856 | goto out; |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1857 | |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1858 | mddev->thread->timeout = mddev->bitmap_info.daemon_sleep; |
NeilBrown | 9cd30fd | 2009-12-14 12:49:54 +1100 | [diff] [blame] | 1859 | md_wakeup_thread(mddev->thread); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1860 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1861 | bitmap_update_sb(bitmap); |
| 1862 | |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1863 | if (bitmap->flags & BITMAP_WRITE_ERROR) |
| 1864 | err = -EIO; |
| 1865 | out: |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1866 | return err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1867 | } |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 1868 | EXPORT_SYMBOL_GPL(bitmap_load); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1869 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 1870 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1871 | location_show(struct mddev *mddev, char *page) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 1872 | { |
| 1873 | ssize_t len; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1874 | if (mddev->bitmap_info.file) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 1875 | len = sprintf(page, "file"); |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1876 | else if (mddev->bitmap_info.offset) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 1877 | len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset); |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1878 | else |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 1879 | len = sprintf(page, "none"); |
| 1880 | len += sprintf(page+len, "\n"); |
| 1881 | return len; |
| 1882 | } |
| 1883 | |
| 1884 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1885 | location_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 1886 | { |
| 1887 | |
| 1888 | if (mddev->pers) { |
| 1889 | if (!mddev->pers->quiesce) |
| 1890 | return -EBUSY; |
| 1891 | if (mddev->recovery || mddev->sync_thread) |
| 1892 | return -EBUSY; |
| 1893 | } |
| 1894 | |
| 1895 | if (mddev->bitmap || mddev->bitmap_info.file || |
| 1896 | mddev->bitmap_info.offset) { |
| 1897 | /* bitmap already configured. Only option is to clear it */ |
| 1898 | if (strncmp(buf, "none", 4) != 0) |
| 1899 | return -EBUSY; |
| 1900 | if (mddev->pers) { |
| 1901 | mddev->pers->quiesce(mddev, 1); |
| 1902 | bitmap_destroy(mddev); |
| 1903 | mddev->pers->quiesce(mddev, 0); |
| 1904 | } |
| 1905 | mddev->bitmap_info.offset = 0; |
| 1906 | if (mddev->bitmap_info.file) { |
| 1907 | struct file *f = mddev->bitmap_info.file; |
| 1908 | mddev->bitmap_info.file = NULL; |
| 1909 | restore_bitmap_write_access(f); |
| 1910 | fput(f); |
| 1911 | } |
| 1912 | } else { |
| 1913 | /* No bitmap, OK to set a location */ |
| 1914 | long long offset; |
| 1915 | if (strncmp(buf, "none", 4) == 0) |
| 1916 | /* nothing to be done */; |
| 1917 | else if (strncmp(buf, "file:", 5) == 0) { |
| 1918 | /* Not supported yet */ |
| 1919 | return -EINVAL; |
| 1920 | } else { |
| 1921 | int rv; |
| 1922 | if (buf[0] == '+') |
| 1923 | rv = strict_strtoll(buf+1, 10, &offset); |
| 1924 | else |
| 1925 | rv = strict_strtoll(buf, 10, &offset); |
| 1926 | if (rv) |
| 1927 | return rv; |
| 1928 | if (offset == 0) |
| 1929 | return -EINVAL; |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 1930 | if (mddev->bitmap_info.external == 0 && |
| 1931 | mddev->major_version == 0 && |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 1932 | offset != mddev->bitmap_info.default_offset) |
| 1933 | return -EINVAL; |
| 1934 | mddev->bitmap_info.offset = offset; |
| 1935 | if (mddev->pers) { |
| 1936 | mddev->pers->quiesce(mddev, 1); |
| 1937 | rv = bitmap_create(mddev); |
| 1938 | if (rv) { |
| 1939 | bitmap_destroy(mddev); |
| 1940 | mddev->bitmap_info.offset = 0; |
| 1941 | } |
| 1942 | mddev->pers->quiesce(mddev, 0); |
| 1943 | if (rv) |
| 1944 | return rv; |
| 1945 | } |
| 1946 | } |
| 1947 | } |
| 1948 | if (!mddev->external) { |
| 1949 | /* Ensure new bitmap info is stored in |
| 1950 | * metadata promptly. |
| 1951 | */ |
| 1952 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 1953 | md_wakeup_thread(mddev->thread); |
| 1954 | } |
| 1955 | return len; |
| 1956 | } |
| 1957 | |
| 1958 | static struct md_sysfs_entry bitmap_location = |
| 1959 | __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store); |
| 1960 | |
| 1961 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1962 | timeout_show(struct mddev *mddev, char *page) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 1963 | { |
| 1964 | ssize_t len; |
| 1965 | unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ; |
| 1966 | unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ; |
NeilBrown | ac2f40b | 2010-06-01 19:37:31 +1000 | [diff] [blame] | 1967 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 1968 | len = sprintf(page, "%lu", secs); |
| 1969 | if (jifs) |
| 1970 | len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs)); |
| 1971 | len += sprintf(page+len, "\n"); |
| 1972 | return len; |
| 1973 | } |
| 1974 | |
| 1975 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1976 | timeout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 1977 | { |
| 1978 | /* timeout can be set at any time */ |
| 1979 | unsigned long timeout; |
| 1980 | int rv = strict_strtoul_scaled(buf, &timeout, 4); |
| 1981 | if (rv) |
| 1982 | return rv; |
| 1983 | |
| 1984 | /* just to make sure we don't overflow... */ |
| 1985 | if (timeout >= LONG_MAX / HZ) |
| 1986 | return -EINVAL; |
| 1987 | |
| 1988 | timeout = timeout * HZ / 10000; |
| 1989 | |
| 1990 | if (timeout >= MAX_SCHEDULE_TIMEOUT) |
| 1991 | timeout = MAX_SCHEDULE_TIMEOUT-1; |
| 1992 | if (timeout < 1) |
| 1993 | timeout = 1; |
| 1994 | mddev->bitmap_info.daemon_sleep = timeout; |
| 1995 | if (mddev->thread) { |
| 1996 | /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then |
| 1997 | * the bitmap is all clean and we don't need to |
| 1998 | * adjust the timeout right now |
| 1999 | */ |
| 2000 | if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) { |
| 2001 | mddev->thread->timeout = timeout; |
| 2002 | md_wakeup_thread(mddev->thread); |
| 2003 | } |
| 2004 | } |
| 2005 | return len; |
| 2006 | } |
| 2007 | |
| 2008 | static struct md_sysfs_entry bitmap_timeout = |
| 2009 | __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store); |
| 2010 | |
| 2011 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2012 | backlog_show(struct mddev *mddev, char *page) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2013 | { |
| 2014 | return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind); |
| 2015 | } |
| 2016 | |
| 2017 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2018 | backlog_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2019 | { |
| 2020 | unsigned long backlog; |
| 2021 | int rv = strict_strtoul(buf, 10, &backlog); |
| 2022 | if (rv) |
| 2023 | return rv; |
| 2024 | if (backlog > COUNTER_MAX) |
| 2025 | return -EINVAL; |
| 2026 | mddev->bitmap_info.max_write_behind = backlog; |
| 2027 | return len; |
| 2028 | } |
| 2029 | |
| 2030 | static struct md_sysfs_entry bitmap_backlog = |
| 2031 | __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store); |
| 2032 | |
| 2033 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2034 | chunksize_show(struct mddev *mddev, char *page) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2035 | { |
| 2036 | return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize); |
| 2037 | } |
| 2038 | |
| 2039 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2040 | chunksize_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2041 | { |
| 2042 | /* Can only be changed when no bitmap is active */ |
| 2043 | int rv; |
| 2044 | unsigned long csize; |
| 2045 | if (mddev->bitmap) |
| 2046 | return -EBUSY; |
| 2047 | rv = strict_strtoul(buf, 10, &csize); |
| 2048 | if (rv) |
| 2049 | return rv; |
| 2050 | if (csize < 512 || |
| 2051 | !is_power_of_2(csize)) |
| 2052 | return -EINVAL; |
| 2053 | mddev->bitmap_info.chunksize = csize; |
| 2054 | return len; |
| 2055 | } |
| 2056 | |
| 2057 | static struct md_sysfs_entry bitmap_chunksize = |
| 2058 | __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store); |
| 2059 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2060 | static ssize_t metadata_show(struct mddev *mddev, char *page) |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 2061 | { |
| 2062 | return sprintf(page, "%s\n", (mddev->bitmap_info.external |
| 2063 | ? "external" : "internal")); |
| 2064 | } |
| 2065 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2066 | 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] | 2067 | { |
| 2068 | if (mddev->bitmap || |
| 2069 | mddev->bitmap_info.file || |
| 2070 | mddev->bitmap_info.offset) |
| 2071 | return -EBUSY; |
| 2072 | if (strncmp(buf, "external", 8) == 0) |
| 2073 | mddev->bitmap_info.external = 1; |
| 2074 | else if (strncmp(buf, "internal", 8) == 0) |
| 2075 | mddev->bitmap_info.external = 0; |
| 2076 | else |
| 2077 | return -EINVAL; |
| 2078 | return len; |
| 2079 | } |
| 2080 | |
| 2081 | static struct md_sysfs_entry bitmap_metadata = |
| 2082 | __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
| 2083 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2084 | static ssize_t can_clear_show(struct mddev *mddev, char *page) |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 2085 | { |
| 2086 | int len; |
| 2087 | if (mddev->bitmap) |
| 2088 | len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ? |
| 2089 | "false" : "true")); |
| 2090 | else |
| 2091 | len = sprintf(page, "\n"); |
| 2092 | return len; |
| 2093 | } |
| 2094 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2095 | 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] | 2096 | { |
| 2097 | if (mddev->bitmap == NULL) |
| 2098 | return -ENOENT; |
| 2099 | if (strncmp(buf, "false", 5) == 0) |
| 2100 | mddev->bitmap->need_sync = 1; |
| 2101 | else if (strncmp(buf, "true", 4) == 0) { |
| 2102 | if (mddev->degraded) |
| 2103 | return -EBUSY; |
| 2104 | mddev->bitmap->need_sync = 0; |
| 2105 | } else |
| 2106 | return -EINVAL; |
| 2107 | return len; |
| 2108 | } |
| 2109 | |
| 2110 | static struct md_sysfs_entry bitmap_can_clear = |
| 2111 | __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store); |
| 2112 | |
Paul Clements | 696fcd5 | 2010-03-08 16:02:37 +1100 | [diff] [blame] | 2113 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2114 | behind_writes_used_show(struct mddev *mddev, char *page) |
Paul Clements | 696fcd5 | 2010-03-08 16:02:37 +1100 | [diff] [blame] | 2115 | { |
| 2116 | if (mddev->bitmap == NULL) |
| 2117 | return sprintf(page, "0\n"); |
| 2118 | return sprintf(page, "%lu\n", |
| 2119 | mddev->bitmap->behind_writes_used); |
| 2120 | } |
| 2121 | |
| 2122 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2123 | 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] | 2124 | { |
| 2125 | if (mddev->bitmap) |
| 2126 | mddev->bitmap->behind_writes_used = 0; |
| 2127 | return len; |
| 2128 | } |
| 2129 | |
| 2130 | static struct md_sysfs_entry max_backlog_used = |
| 2131 | __ATTR(max_backlog_used, S_IRUGO | S_IWUSR, |
| 2132 | behind_writes_used_show, behind_writes_used_reset); |
| 2133 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2134 | static struct attribute *md_bitmap_attrs[] = { |
| 2135 | &bitmap_location.attr, |
| 2136 | &bitmap_timeout.attr, |
| 2137 | &bitmap_backlog.attr, |
| 2138 | &bitmap_chunksize.attr, |
NeilBrown | ece5cff | 2009-12-14 12:49:56 +1100 | [diff] [blame] | 2139 | &bitmap_metadata.attr, |
| 2140 | &bitmap_can_clear.attr, |
Paul Clements | 696fcd5 | 2010-03-08 16:02:37 +1100 | [diff] [blame] | 2141 | &max_backlog_used.attr, |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2142 | NULL |
| 2143 | }; |
| 2144 | struct attribute_group md_bitmap_group = { |
| 2145 | .name = "bitmap", |
| 2146 | .attrs = md_bitmap_attrs, |
| 2147 | }; |
| 2148 | |