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