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