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). |
| 16 | * wait if count gets too high, wake when it drops to half. |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 19 | #include <linux/blkdev.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 20 | #include <linux/module.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 21 | #include <linux/errno.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/init.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 24 | #include <linux/timer.h> |
| 25 | #include <linux/sched.h> |
| 26 | #include <linux/list.h> |
| 27 | #include <linux/file.h> |
| 28 | #include <linux/mount.h> |
| 29 | #include <linux/buffer_head.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 30 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 31 | #include "bitmap.h" |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 32 | |
| 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 | |
| 54 | //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */ |
| 55 | #define DPRINTK(x...) do { } while(0) |
| 56 | |
| 57 | #ifndef PRINTK |
| 58 | # if DEBUG > 0 |
| 59 | # define PRINTK(x...) printk(KERN_DEBUG x) |
| 60 | # else |
| 61 | # define PRINTK(x...) |
| 62 | # endif |
| 63 | #endif |
| 64 | |
| 65 | static inline char * bmname(struct bitmap *bitmap) |
| 66 | { |
| 67 | return bitmap->mddev ? mdname(bitmap->mddev) : "mdX"; |
| 68 | } |
| 69 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * just a placeholder - calls kmalloc for bitmap pages |
| 73 | */ |
| 74 | static unsigned char *bitmap_alloc_page(struct bitmap *bitmap) |
| 75 | { |
| 76 | unsigned char *page; |
| 77 | |
Olaf Hering | 44456d3 | 2005-07-27 11:45:17 -0700 | [diff] [blame] | 78 | #ifdef INJECT_FAULTS_1 |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 79 | page = NULL; |
| 80 | #else |
| 81 | page = kmalloc(PAGE_SIZE, GFP_NOIO); |
| 82 | #endif |
| 83 | if (!page) |
| 84 | printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap)); |
| 85 | else |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 86 | PRINTK("%s: bitmap_alloc_page: allocated page at %p\n", |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 87 | bmname(bitmap), page); |
| 88 | return page; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * for now just a placeholder -- just calls kfree for bitmap pages |
| 93 | */ |
| 94 | static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page) |
| 95 | { |
| 96 | PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page); |
| 97 | kfree(page); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * check a page and, if necessary, allocate it (or hijack it if the alloc fails) |
| 102 | * |
| 103 | * 1) check to see if this page is allocated, if it's not then try to alloc |
| 104 | * 2) if the alloc fails, set the page's hijacked flag so we'll use the |
| 105 | * page pointer directly as a counter |
| 106 | * |
| 107 | * if we find our page, we increment the page's refcount so that it stays |
| 108 | * allocated while we're using it |
| 109 | */ |
| 110 | static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create) |
NeilBrown | ee305ac | 2009-09-23 18:06:44 +1000 | [diff] [blame] | 111 | __releases(bitmap->lock) |
| 112 | __acquires(bitmap->lock) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 113 | { |
| 114 | unsigned char *mappage; |
| 115 | |
| 116 | if (page >= bitmap->pages) { |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 117 | /* This can happen if bitmap_start_sync goes beyond |
| 118 | * End-of-device while looking for a whole page. |
| 119 | * It is harmless. |
| 120 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 121 | return -EINVAL; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */ |
| 126 | return 0; |
| 127 | |
| 128 | if (bitmap->bp[page].map) /* page is already allocated, just return */ |
| 129 | return 0; |
| 130 | |
| 131 | if (!create) |
| 132 | return -ENOENT; |
| 133 | |
| 134 | spin_unlock_irq(&bitmap->lock); |
| 135 | |
| 136 | /* this page has not been allocated yet */ |
| 137 | |
| 138 | if ((mappage = bitmap_alloc_page(bitmap)) == NULL) { |
| 139 | PRINTK("%s: bitmap map page allocation failed, hijacking\n", |
| 140 | bmname(bitmap)); |
| 141 | /* failed - set the hijacked flag so that we can use the |
| 142 | * pointer as a counter */ |
| 143 | spin_lock_irq(&bitmap->lock); |
| 144 | if (!bitmap->bp[page].map) |
| 145 | bitmap->bp[page].hijacked = 1; |
| 146 | goto out; |
| 147 | } |
| 148 | |
| 149 | /* got a page */ |
| 150 | |
| 151 | spin_lock_irq(&bitmap->lock); |
| 152 | |
| 153 | /* recheck the page */ |
| 154 | |
| 155 | if (bitmap->bp[page].map || bitmap->bp[page].hijacked) { |
| 156 | /* somebody beat us to getting the page */ |
| 157 | bitmap_free_page(bitmap, mappage); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /* no page was in place and we have one, so install it */ |
| 162 | |
| 163 | memset(mappage, 0, PAGE_SIZE); |
| 164 | bitmap->bp[page].map = mappage; |
| 165 | bitmap->missing_pages--; |
| 166 | out: |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /* if page is completely empty, put it back on the free list, or dealloc it */ |
| 172 | /* if page was hijacked, unmark the flag so it might get alloced next time */ |
| 173 | /* Note: lock should be held when calling this */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 174 | static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 175 | { |
| 176 | char *ptr; |
| 177 | |
| 178 | if (bitmap->bp[page].count) /* page is still busy */ |
| 179 | return; |
| 180 | |
| 181 | /* page is no longer in use, it can be released */ |
| 182 | |
| 183 | if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */ |
| 184 | bitmap->bp[page].hijacked = 0; |
| 185 | bitmap->bp[page].map = NULL; |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | /* normal case, free the page */ |
| 190 | |
| 191 | #if 0 |
| 192 | /* actually ... let's not. We will probably need the page again exactly when |
| 193 | * memory is tight and we are flusing to disk |
| 194 | */ |
| 195 | return; |
| 196 | #else |
| 197 | ptr = bitmap->bp[page].map; |
| 198 | bitmap->bp[page].map = NULL; |
| 199 | bitmap->missing_pages++; |
| 200 | bitmap_free_page(bitmap, ptr); |
| 201 | return; |
| 202 | #endif |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /* |
| 207 | * bitmap file handling - read and write the bitmap file and its superblock |
| 208 | */ |
| 209 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 210 | /* |
| 211 | * basic page I/O operations |
| 212 | */ |
| 213 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 214 | /* IO operations when bitmap is stored near all superblocks */ |
NeilBrown | f6af949 | 2009-12-14 12:49:54 +1100 | [diff] [blame^] | 215 | static struct page *read_sb_page(mddev_t *mddev, loff_t offset, |
NeilBrown | a2ed961 | 2008-12-19 16:25:01 +1100 | [diff] [blame] | 216 | struct page *page, |
| 217 | unsigned long index, int size) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 218 | { |
| 219 | /* choose a good rdev and read the page from there */ |
| 220 | |
| 221 | mdk_rdev_t *rdev; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 222 | sector_t target; |
| 223 | |
| 224 | if (!page) |
NeilBrown | a2ed961 | 2008-12-19 16:25:01 +1100 | [diff] [blame] | 225 | page = alloc_page(GFP_KERNEL); |
| 226 | if (!page) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 227 | return ERR_PTR(-ENOMEM); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 228 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 229 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 230 | if (! test_bit(In_sync, &rdev->flags) |
| 231 | || test_bit(Faulty, &rdev->flags)) |
NeilBrown | ab904d6 | 2005-09-09 16:23:52 -0700 | [diff] [blame] | 232 | continue; |
| 233 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 234 | target = rdev->sb_start + offset + index * (PAGE_SIZE/512); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 235 | |
NeilBrown | a2ed961 | 2008-12-19 16:25:01 +1100 | [diff] [blame] | 236 | if (sync_page_io(rdev->bdev, target, |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 237 | roundup(size, bdev_logical_block_size(rdev->bdev)), |
NeilBrown | a2ed961 | 2008-12-19 16:25:01 +1100 | [diff] [blame] | 238 | page, READ)) { |
NeilBrown | ab904d6 | 2005-09-09 16:23:52 -0700 | [diff] [blame] | 239 | page->index = index; |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 240 | attach_page_buffers(page, NULL); /* so that free_buffer will |
| 241 | * quietly no-op */ |
NeilBrown | ab904d6 | 2005-09-09 16:23:52 -0700 | [diff] [blame] | 242 | return page; |
| 243 | } |
| 244 | } |
| 245 | return ERR_PTR(-EIO); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 246 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 247 | } |
| 248 | |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 249 | static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 250 | { |
| 251 | /* Iterate the disks of an mddev, using rcu to protect access to the |
| 252 | * linked list, and raising the refcount of devices we return to ensure |
| 253 | * they don't disappear while in use. |
| 254 | * As devices are only added or removed when raid_disk is < 0 and |
| 255 | * nr_pending is 0 and In_sync is clear, the entries we return will |
| 256 | * still be in the same position on the list when we re-enter |
| 257 | * list_for_each_continue_rcu. |
| 258 | */ |
| 259 | struct list_head *pos; |
| 260 | rcu_read_lock(); |
| 261 | if (rdev == NULL) |
| 262 | /* start at the beginning */ |
| 263 | pos = &mddev->disks; |
| 264 | else { |
| 265 | /* release the previous rdev and start from there. */ |
| 266 | rdev_dec_pending(rdev, mddev); |
| 267 | pos = &rdev->same_set; |
| 268 | } |
| 269 | list_for_each_continue_rcu(pos, &mddev->disks) { |
| 270 | rdev = list_entry(pos, mdk_rdev_t, same_set); |
| 271 | if (rdev->raid_disk >= 0 && |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 272 | !test_bit(Faulty, &rdev->flags)) { |
| 273 | /* this is a usable devices */ |
| 274 | atomic_inc(&rdev->nr_pending); |
| 275 | rcu_read_unlock(); |
| 276 | return rdev; |
| 277 | } |
| 278 | } |
| 279 | rcu_read_unlock(); |
| 280 | return NULL; |
| 281 | } |
| 282 | |
NeilBrown | ab6085c | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 283 | static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 284 | { |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 285 | mdk_rdev_t *rdev = NULL; |
NeilBrown | ab6085c | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 286 | mddev_t *mddev = bitmap->mddev; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 287 | |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 288 | while ((rdev = next_active_rdev(rdev, mddev)) != NULL) { |
NeilBrown | ab6085c | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 289 | int size = PAGE_SIZE; |
NeilBrown | f6af949 | 2009-12-14 12:49:54 +1100 | [diff] [blame^] | 290 | loff_t offset = mddev->bitmap_info.offset; |
NeilBrown | ab6085c | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 291 | if (page->index == bitmap->file_pages-1) |
| 292 | size = roundup(bitmap->last_page_size, |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 293 | bdev_logical_block_size(rdev->bdev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 294 | /* Just make sure we aren't corrupting data or |
| 295 | * metadata |
| 296 | */ |
NeilBrown | f6af949 | 2009-12-14 12:49:54 +1100 | [diff] [blame^] | 297 | if (mddev->external) { |
| 298 | /* Bitmap could be anywhere. */ |
| 299 | if (rdev->sb_start + offset + (page->index *(PAGE_SIZE/512)) > |
| 300 | rdev->data_offset && |
| 301 | rdev->sb_start + offset < |
| 302 | rdev->data_offset + mddev->dev_sectors + |
| 303 | (PAGE_SIZE/512)) |
| 304 | goto bad_alignment; |
| 305 | } else if (offset < 0) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 306 | /* DATA BITMAP METADATA */ |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 307 | if (offset |
NeilBrown | 85bfb4d | 2007-10-22 20:45:11 -0700 | [diff] [blame] | 308 | + (long)(page->index * (PAGE_SIZE/512)) |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 309 | + size/512 > 0) |
| 310 | /* bitmap runs in to metadata */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 311 | goto bad_alignment; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 312 | if (rdev->data_offset + mddev->dev_sectors |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 313 | > rdev->sb_start + offset) |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 314 | /* data runs in to bitmap */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 315 | goto bad_alignment; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 316 | } else if (rdev->sb_start < rdev->data_offset) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 317 | /* METADATA BITMAP DATA */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 318 | if (rdev->sb_start |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 319 | + offset |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 320 | + page->index*(PAGE_SIZE/512) + size/512 |
| 321 | > rdev->data_offset) |
| 322 | /* bitmap runs in to data */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 323 | goto bad_alignment; |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 324 | } else { |
| 325 | /* DATA METADATA BITMAP - no problems */ |
| 326 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 327 | md_super_write(mddev, rdev, |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 328 | rdev->sb_start + offset |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 329 | + page->index * (PAGE_SIZE/512), |
NeilBrown | ab6085c | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 330 | size, |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 331 | page); |
NeilBrown | b2d2c4c | 2008-09-01 12:48:13 +1000 | [diff] [blame] | 332 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 333 | |
| 334 | if (wait) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 335 | md_super_wait(mddev); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 336 | return 0; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 337 | |
| 338 | bad_alignment: |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 339 | return -EINVAL; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 340 | } |
| 341 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 342 | static void bitmap_file_kick(struct bitmap *bitmap); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 343 | /* |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 344 | * write out a page to a file |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 345 | */ |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 346 | static void write_page(struct bitmap *bitmap, struct page *page, int wait) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 347 | { |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 348 | struct buffer_head *bh; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 349 | |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 350 | if (bitmap->file == NULL) { |
| 351 | switch (write_sb_page(bitmap, page, wait)) { |
| 352 | case -EINVAL: |
| 353 | bitmap->flags |= BITMAP_WRITE_ERROR; |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 354 | } |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 355 | } else { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 356 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 357 | bh = page_buffers(page); |
NeilBrown | c708443 | 2006-01-06 00:20:45 -0800 | [diff] [blame] | 358 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 359 | while (bh && bh->b_blocknr) { |
| 360 | atomic_inc(&bitmap->pending_writes); |
| 361 | set_buffer_locked(bh); |
| 362 | set_buffer_mapped(bh); |
| 363 | submit_bh(WRITE, bh); |
| 364 | bh = bh->b_this_page; |
| 365 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 366 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 367 | if (wait) { |
| 368 | wait_event(bitmap->write_wait, |
| 369 | atomic_read(&bitmap->pending_writes)==0); |
| 370 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 371 | } |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 372 | if (bitmap->flags & BITMAP_WRITE_ERROR) |
| 373 | bitmap_file_kick(bitmap); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 374 | } |
| 375 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 376 | static void end_bitmap_write(struct buffer_head *bh, int uptodate) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 377 | { |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 378 | struct bitmap *bitmap = bh->b_private; |
| 379 | unsigned long flags; |
| 380 | |
| 381 | if (!uptodate) { |
| 382 | spin_lock_irqsave(&bitmap->lock, flags); |
| 383 | bitmap->flags |= BITMAP_WRITE_ERROR; |
| 384 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 385 | } |
| 386 | if (atomic_dec_and_test(&bitmap->pending_writes)) |
| 387 | wake_up(&bitmap->write_wait); |
| 388 | } |
| 389 | |
| 390 | /* copied from buffer.c */ |
| 391 | static void |
| 392 | __clear_page_buffers(struct page *page) |
| 393 | { |
| 394 | ClearPagePrivate(page); |
| 395 | set_page_private(page, 0); |
| 396 | page_cache_release(page); |
| 397 | } |
| 398 | static void free_buffers(struct page *page) |
| 399 | { |
| 400 | struct buffer_head *bh = page_buffers(page); |
| 401 | |
| 402 | while (bh) { |
| 403 | struct buffer_head *next = bh->b_this_page; |
| 404 | free_buffer_head(bh); |
| 405 | bh = next; |
| 406 | } |
| 407 | __clear_page_buffers(page); |
| 408 | put_page(page); |
| 409 | } |
| 410 | |
| 411 | /* read a page from a file. |
| 412 | * We both read the page, and attach buffers to the page to record the |
| 413 | * address of each block (using bmap). These addresses will be used |
| 414 | * to write the block later, completely bypassing the filesystem. |
| 415 | * This usage is similar to how swap files are handled, and allows us |
| 416 | * to write to a file with no concerns of memory allocation failing. |
| 417 | */ |
| 418 | static struct page *read_page(struct file *file, unsigned long index, |
| 419 | struct bitmap *bitmap, |
| 420 | unsigned long count) |
| 421 | { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 422 | struct page *page = NULL; |
Josef Sipek | c649bb9 | 2006-12-08 02:37:19 -0800 | [diff] [blame] | 423 | struct inode *inode = file->f_path.dentry->d_inode; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 424 | struct buffer_head *bh; |
| 425 | sector_t block; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 426 | |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 427 | PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE, |
| 428 | (unsigned long long)index << PAGE_SHIFT); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 429 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 430 | page = alloc_page(GFP_KERNEL); |
| 431 | if (!page) |
| 432 | page = ERR_PTR(-ENOMEM); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 433 | if (IS_ERR(page)) |
| 434 | goto out; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 435 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 436 | bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0); |
| 437 | if (!bh) { |
| 438 | put_page(page); |
| 439 | page = ERR_PTR(-ENOMEM); |
| 440 | goto out; |
| 441 | } |
| 442 | attach_page_buffers(page, bh); |
| 443 | block = index << (PAGE_SHIFT - inode->i_blkbits); |
| 444 | while (bh) { |
| 445 | if (count == 0) |
| 446 | bh->b_blocknr = 0; |
| 447 | else { |
| 448 | bh->b_blocknr = bmap(inode, block); |
| 449 | if (bh->b_blocknr == 0) { |
| 450 | /* Cannot use this file! */ |
| 451 | free_buffers(page); |
| 452 | page = ERR_PTR(-EINVAL); |
| 453 | goto out; |
| 454 | } |
| 455 | bh->b_bdev = inode->i_sb->s_bdev; |
| 456 | if (count < (1<<inode->i_blkbits)) |
| 457 | count = 0; |
| 458 | else |
| 459 | count -= (1<<inode->i_blkbits); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 460 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 461 | bh->b_end_io = end_bitmap_write; |
| 462 | bh->b_private = bitmap; |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 463 | atomic_inc(&bitmap->pending_writes); |
| 464 | set_buffer_locked(bh); |
| 465 | set_buffer_mapped(bh); |
| 466 | submit_bh(READ, bh); |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 467 | } |
| 468 | block++; |
| 469 | bh = bh->b_this_page; |
| 470 | } |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 471 | page->index = index; |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 472 | |
| 473 | wait_event(bitmap->write_wait, |
| 474 | atomic_read(&bitmap->pending_writes)==0); |
| 475 | if (bitmap->flags & BITMAP_WRITE_ERROR) { |
| 476 | free_buffers(page); |
| 477 | page = ERR_PTR(-EIO); |
| 478 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 479 | out: |
| 480 | if (IS_ERR(page)) |
| 481 | printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n", |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 482 | (int)PAGE_SIZE, |
| 483 | (unsigned long long)index << PAGE_SHIFT, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 484 | PTR_ERR(page)); |
| 485 | return page; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * bitmap file superblock operations |
| 490 | */ |
| 491 | |
| 492 | /* update the event counter and sync the superblock to disk */ |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 493 | void bitmap_update_sb(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 494 | { |
| 495 | bitmap_super_t *sb; |
| 496 | unsigned long flags; |
| 497 | |
| 498 | if (!bitmap || !bitmap->mddev) /* no bitmap for this array */ |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 499 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 500 | spin_lock_irqsave(&bitmap->lock, flags); |
| 501 | if (!bitmap->sb_page) { /* no superblock */ |
| 502 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 503 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 504 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 505 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 506 | sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 507 | sb->events = cpu_to_le64(bitmap->mddev->events); |
Neil Brown | a0da84f | 2008-06-28 08:31:22 +1000 | [diff] [blame] | 508 | if (bitmap->mddev->events < bitmap->events_cleared) { |
| 509 | /* rocking back to read-only */ |
| 510 | bitmap->events_cleared = bitmap->mddev->events; |
| 511 | sb->events_cleared = cpu_to_le64(bitmap->events_cleared); |
| 512 | } |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 513 | kunmap_atomic(sb, KM_USER0); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 514 | write_page(bitmap, bitmap->sb_page, 1); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /* print out the bitmap file superblock */ |
| 518 | void bitmap_print_sb(struct bitmap *bitmap) |
| 519 | { |
| 520 | bitmap_super_t *sb; |
| 521 | |
| 522 | if (!bitmap || !bitmap->sb_page) |
| 523 | return; |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 524 | sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 525 | printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap)); |
NeilBrown | a2cff26 | 2005-06-21 17:17:20 -0700 | [diff] [blame] | 526 | printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic)); |
| 527 | printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version)); |
| 528 | printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n", |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 529 | *(__u32 *)(sb->uuid+0), |
| 530 | *(__u32 *)(sb->uuid+4), |
| 531 | *(__u32 *)(sb->uuid+8), |
| 532 | *(__u32 *)(sb->uuid+12)); |
NeilBrown | a2cff26 | 2005-06-21 17:17:20 -0700 | [diff] [blame] | 533 | printk(KERN_DEBUG " events: %llu\n", |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 534 | (unsigned long long) le64_to_cpu(sb->events)); |
NeilBrown | a2cff26 | 2005-06-21 17:17:20 -0700 | [diff] [blame] | 535 | printk(KERN_DEBUG "events cleared: %llu\n", |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 536 | (unsigned long long) le64_to_cpu(sb->events_cleared)); |
NeilBrown | a2cff26 | 2005-06-21 17:17:20 -0700 | [diff] [blame] | 537 | printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state)); |
| 538 | printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize)); |
| 539 | printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep)); |
| 540 | printk(KERN_DEBUG " sync size: %llu KB\n", |
| 541 | (unsigned long long)le64_to_cpu(sb->sync_size)/2); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 542 | 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] | 543 | kunmap_atomic(sb, KM_USER0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* read the superblock from the bitmap file and initialize some bitmap fields */ |
| 547 | static int bitmap_read_sb(struct bitmap *bitmap) |
| 548 | { |
| 549 | char *reason = NULL; |
| 550 | bitmap_super_t *sb; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 551 | unsigned long chunksize, daemon_sleep, write_behind; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 552 | unsigned long long events; |
| 553 | int err = -EINVAL; |
| 554 | |
| 555 | /* page 0 is the superblock, read it... */ |
NeilBrown | f49d5e6 | 2007-01-26 00:57:03 -0800 | [diff] [blame] | 556 | if (bitmap->file) { |
| 557 | loff_t isize = i_size_read(bitmap->file->f_mapping->host); |
| 558 | int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize; |
| 559 | |
| 560 | bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes); |
| 561 | } else { |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 562 | bitmap->sb_page = read_sb_page(bitmap->mddev, |
| 563 | bitmap->mddev->bitmap_info.offset, |
NeilBrown | a2ed961 | 2008-12-19 16:25:01 +1100 | [diff] [blame] | 564 | NULL, |
| 565 | 0, sizeof(bitmap_super_t)); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 566 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 567 | if (IS_ERR(bitmap->sb_page)) { |
| 568 | err = PTR_ERR(bitmap->sb_page); |
| 569 | bitmap->sb_page = NULL; |
| 570 | return err; |
| 571 | } |
| 572 | |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 573 | sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 574 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 575 | chunksize = le32_to_cpu(sb->chunksize); |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 576 | daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 577 | write_behind = le32_to_cpu(sb->write_behind); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 578 | |
| 579 | /* verify that the bitmap-specific fields are valid */ |
| 580 | if (sb->magic != cpu_to_le32(BITMAP_MAGIC)) |
| 581 | reason = "bad magic"; |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 582 | else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO || |
| 583 | le32_to_cpu(sb->version) > BITMAP_MAJOR_HI) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 584 | reason = "unrecognized superblock version"; |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 585 | else if (chunksize < 512) |
NeilBrown | 7dd5d34 | 2006-01-06 00:20:39 -0800 | [diff] [blame] | 586 | reason = "bitmap chunksize too small"; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 587 | else if ((1 << ffz(~chunksize)) != chunksize) |
| 588 | reason = "bitmap chunksize not a power of 2"; |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 589 | else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT) |
NeilBrown | 7dd5d34 | 2006-01-06 00:20:39 -0800 | [diff] [blame] | 590 | reason = "daemon sleep period out of range"; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 591 | else if (write_behind > COUNTER_MAX) |
| 592 | reason = "write-behind limit out of range (0 - 16383)"; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 593 | if (reason) { |
| 594 | printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n", |
| 595 | bmname(bitmap), reason); |
| 596 | goto out; |
| 597 | } |
| 598 | |
| 599 | /* keep the array size field of the bitmap superblock up to date */ |
| 600 | sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors); |
| 601 | |
| 602 | if (!bitmap->mddev->persistent) |
| 603 | goto success; |
| 604 | |
| 605 | /* |
| 606 | * if we have a persistent array superblock, compare the |
| 607 | * bitmap's UUID and event counter to the mddev's |
| 608 | */ |
| 609 | if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) { |
| 610 | printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n", |
| 611 | bmname(bitmap)); |
| 612 | goto out; |
| 613 | } |
| 614 | events = le64_to_cpu(sb->events); |
| 615 | if (events < bitmap->mddev->events) { |
| 616 | printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) " |
| 617 | "-- forcing full recovery\n", bmname(bitmap), events, |
| 618 | (unsigned long long) bitmap->mddev->events); |
NeilBrown | 4f2e639 | 2006-10-21 10:24:09 -0700 | [diff] [blame] | 619 | sb->state |= cpu_to_le32(BITMAP_STALE); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 620 | } |
| 621 | success: |
| 622 | /* assign fields using values from superblock */ |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 623 | bitmap->mddev->bitmap_info.chunksize = chunksize; |
| 624 | bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep; |
NeilBrown | 585f0dd | 2005-09-09 16:23:49 -0700 | [diff] [blame] | 625 | bitmap->daemon_lastrun = jiffies; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 626 | bitmap->mddev->bitmap_info.max_write_behind = write_behind; |
NeilBrown | 4f2e639 | 2006-10-21 10:24:09 -0700 | [diff] [blame] | 627 | bitmap->flags |= le32_to_cpu(sb->state); |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 628 | if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN) |
| 629 | bitmap->flags |= BITMAP_HOSTENDIAN; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 630 | bitmap->events_cleared = le64_to_cpu(sb->events_cleared); |
NeilBrown | 4f2e639 | 2006-10-21 10:24:09 -0700 | [diff] [blame] | 631 | if (sb->state & cpu_to_le32(BITMAP_STALE)) |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 632 | bitmap->events_cleared = bitmap->mddev->events; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 633 | err = 0; |
| 634 | out: |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 635 | kunmap_atomic(sb, KM_USER0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 636 | if (err) |
| 637 | bitmap_print_sb(bitmap); |
| 638 | return err; |
| 639 | } |
| 640 | |
| 641 | enum bitmap_mask_op { |
| 642 | MASK_SET, |
| 643 | MASK_UNSET |
| 644 | }; |
| 645 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 646 | /* record the state of the bitmap in the superblock. Return the old value */ |
| 647 | static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits, |
| 648 | enum bitmap_mask_op op) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 649 | { |
| 650 | bitmap_super_t *sb; |
| 651 | unsigned long flags; |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 652 | int old; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 653 | |
| 654 | spin_lock_irqsave(&bitmap->lock, flags); |
Adrian Bunk | 7e31765 | 2006-03-25 03:07:51 -0800 | [diff] [blame] | 655 | if (!bitmap->sb_page) { /* can't set the state */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 656 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 657 | return 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 658 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 659 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 660 | sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 661 | old = le32_to_cpu(sb->state) & bits; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 662 | switch (op) { |
NeilBrown | 4f2e639 | 2006-10-21 10:24:09 -0700 | [diff] [blame] | 663 | case MASK_SET: sb->state |= cpu_to_le32(bits); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 664 | break; |
NeilBrown | 4f2e639 | 2006-10-21 10:24:09 -0700 | [diff] [blame] | 665 | case MASK_UNSET: sb->state &= cpu_to_le32(~bits); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 666 | break; |
| 667 | default: BUG(); |
| 668 | } |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 669 | kunmap_atomic(sb, KM_USER0); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 670 | return old; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* |
| 674 | * general bitmap file operations |
| 675 | */ |
| 676 | |
| 677 | /* calculate the index of the page that contains this bit */ |
| 678 | static inline unsigned long file_page_index(unsigned long chunk) |
| 679 | { |
| 680 | return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT; |
| 681 | } |
| 682 | |
| 683 | /* calculate the (bit) offset of this bit within a page */ |
| 684 | static inline unsigned long file_page_offset(unsigned long chunk) |
| 685 | { |
| 686 | return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1); |
| 687 | } |
| 688 | |
| 689 | /* |
| 690 | * return a pointer to the page in the filemap that contains the given bit |
| 691 | * |
| 692 | * this lookup is complicated by the fact that the bitmap sb might be exactly |
| 693 | * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page |
| 694 | * 0 or page 1 |
| 695 | */ |
| 696 | static inline struct page *filemap_get_page(struct bitmap *bitmap, |
| 697 | unsigned long chunk) |
| 698 | { |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 699 | if (file_page_index(chunk) >= bitmap->file_pages) return NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 700 | return bitmap->filemap[file_page_index(chunk) - file_page_index(0)]; |
| 701 | } |
| 702 | |
| 703 | |
| 704 | static void bitmap_file_unmap(struct bitmap *bitmap) |
| 705 | { |
| 706 | struct page **map, *sb_page; |
| 707 | unsigned long *attr; |
| 708 | int pages; |
| 709 | unsigned long flags; |
| 710 | |
| 711 | spin_lock_irqsave(&bitmap->lock, flags); |
| 712 | map = bitmap->filemap; |
| 713 | bitmap->filemap = NULL; |
| 714 | attr = bitmap->filemap_attr; |
| 715 | bitmap->filemap_attr = NULL; |
| 716 | pages = bitmap->file_pages; |
| 717 | bitmap->file_pages = 0; |
| 718 | sb_page = bitmap->sb_page; |
| 719 | bitmap->sb_page = NULL; |
| 720 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 721 | |
| 722 | while (pages--) |
| 723 | if (map[pages]->index != 0) /* 0 is sb_page, release it below */ |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 724 | free_buffers(map[pages]); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 725 | kfree(map); |
| 726 | kfree(attr); |
| 727 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 728 | if (sb_page) |
| 729 | free_buffers(sb_page); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | static void bitmap_file_put(struct bitmap *bitmap) |
| 733 | { |
| 734 | struct file *file; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 735 | unsigned long flags; |
| 736 | |
| 737 | spin_lock_irqsave(&bitmap->lock, flags); |
| 738 | file = bitmap->file; |
| 739 | bitmap->file = NULL; |
| 740 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 741 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 742 | if (file) |
| 743 | wait_event(bitmap->write_wait, |
| 744 | atomic_read(&bitmap->pending_writes)==0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 745 | bitmap_file_unmap(bitmap); |
| 746 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 747 | if (file) { |
Josef Sipek | c649bb9 | 2006-12-08 02:37:19 -0800 | [diff] [blame] | 748 | struct inode *inode = file->f_path.dentry->d_inode; |
Andrew Morton | fc0ecff | 2007-02-10 01:45:39 -0800 | [diff] [blame] | 749 | invalidate_mapping_pages(inode->i_mapping, 0, -1); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 750 | fput(file); |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 751 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | |
| 755 | /* |
| 756 | * bitmap_file_kick - if an error occurs while manipulating the bitmap file |
| 757 | * then it is no longer reliable, so we stop using it and we mark the file |
| 758 | * as failed in the superblock |
| 759 | */ |
| 760 | static void bitmap_file_kick(struct bitmap *bitmap) |
| 761 | { |
| 762 | char *path, *ptr = NULL; |
| 763 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 764 | if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) { |
| 765 | bitmap_update_sb(bitmap); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 766 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 767 | if (bitmap->file) { |
| 768 | path = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 769 | if (path) |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 770 | ptr = d_path(&bitmap->file->f_path, path, |
| 771 | PAGE_SIZE); |
| 772 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 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 | e16b68b | 2006-06-26 00:27:45 -0700 | [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 | e16b68b | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 799 | __set_bit((page->index<<2) + attr, bitmap->filemap_attr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | static inline void clear_page_attr(struct bitmap *bitmap, struct page *page, |
| 803 | enum bitmap_page_attr attr) |
| 804 | { |
NeilBrown | e16b68b | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 805 | __clear_bit((page->index<<2) + attr, bitmap->filemap_attr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 806 | } |
| 807 | |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 808 | static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page, |
| 809 | enum bitmap_page_attr attr) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 810 | { |
NeilBrown | e16b68b | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 811 | return test_bit((page->index<<2) + attr, bitmap->filemap_attr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /* |
| 815 | * bitmap_file_set_bit -- called before performing a write to the md device |
| 816 | * to set (and eventually sync) a particular bit in the bitmap file |
| 817 | * |
| 818 | * we set the bit immediately, then we record the page number so that |
| 819 | * when an unplug occurs, we can flush the dirty pages out to disk |
| 820 | */ |
| 821 | static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block) |
| 822 | { |
| 823 | unsigned long bit; |
| 824 | struct page *page; |
| 825 | void *kaddr; |
| 826 | unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap); |
| 827 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 828 | if (!bitmap->filemap) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 829 | return; |
| 830 | } |
| 831 | |
| 832 | page = filemap_get_page(bitmap, chunk); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 833 | if (!page) return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 834 | bit = file_page_offset(chunk); |
| 835 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 836 | /* set the bit */ |
| 837 | kaddr = kmap_atomic(page, KM_USER0); |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 838 | if (bitmap->flags & BITMAP_HOSTENDIAN) |
| 839 | set_bit(bit, kaddr); |
| 840 | else |
| 841 | ext2_set_bit(bit, kaddr); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 842 | kunmap_atomic(kaddr, KM_USER0); |
| 843 | PRINTK("set file bit %lu page %lu\n", bit, page->index); |
| 844 | |
| 845 | /* record page number so it gets flushed to disk when unplug occurs */ |
| 846 | set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY); |
| 847 | |
| 848 | } |
| 849 | |
| 850 | /* this gets called when the md device is ready to unplug its underlying |
| 851 | * (slave) device queues -- before we let any writes go down, we need to |
| 852 | * sync the dirty pages of the bitmap file to disk */ |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 853 | void bitmap_unplug(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 854 | { |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 855 | unsigned long i, flags; |
| 856 | int dirty, need_write; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 857 | struct page *page; |
| 858 | int wait = 0; |
| 859 | |
| 860 | if (!bitmap) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 861 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 862 | |
| 863 | /* look at each page to see if there are any set bits that need to be |
| 864 | * flushed out to disk */ |
| 865 | for (i = 0; i < bitmap->file_pages; i++) { |
| 866 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 867 | if (!bitmap->filemap) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 868 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 869 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 870 | } |
| 871 | page = bitmap->filemap[i]; |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 872 | dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY); |
| 873 | need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 874 | clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY); |
| 875 | clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE); |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 876 | if (dirty) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 877 | wait = 1; |
| 878 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 879 | |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 880 | if (dirty | need_write) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 881 | write_page(bitmap, page, 0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 882 | } |
| 883 | if (wait) { /* if any writes were performed, we need to wait on them */ |
NeilBrown | 0b79ccf | 2006-06-26 00:27:44 -0700 | [diff] [blame] | 884 | if (bitmap->file) |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 885 | wait_event(bitmap->write_wait, |
| 886 | atomic_read(&bitmap->pending_writes)==0); |
NeilBrown | 0b79ccf | 2006-06-26 00:27:44 -0700 | [diff] [blame] | 887 | else |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 888 | md_super_wait(bitmap->mddev); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 889 | } |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 890 | if (bitmap->flags & BITMAP_WRITE_ERROR) |
| 891 | bitmap_file_kick(bitmap); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 892 | } |
| 893 | |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 894 | 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] | 895 | /* * bitmap_init_from_disk -- called at bitmap_create time to initialize |
| 896 | * the in-memory bitmap from the on-disk bitmap -- also, sets up the |
| 897 | * memory mapping of the bitmap file |
| 898 | * Special cases: |
| 899 | * if there's no bitmap file, or if the bitmap file had been |
| 900 | * previously kicked from the array, we mark all the bits as |
| 901 | * 1's in order to cause a full resync. |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 902 | * |
| 903 | * We ignore all bits for sectors that end earlier than 'start'. |
| 904 | * This is used when reading an out-of-date bitmap... |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 905 | */ |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 906 | static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 907 | { |
| 908 | unsigned long i, chunks, index, oldindex, bit; |
| 909 | struct page *page = NULL, *oldpage = NULL; |
| 910 | unsigned long num_pages, bit_cnt = 0; |
| 911 | struct file *file; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 912 | unsigned long bytes, offset; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 913 | int outofdate; |
| 914 | int ret = -ENOSPC; |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 915 | void *paddr; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 916 | |
| 917 | chunks = bitmap->chunks; |
| 918 | file = bitmap->file; |
| 919 | |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 920 | BUG_ON(!file && !bitmap->mddev->bitmap_info.offset); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 921 | |
Olaf Hering | 44456d3 | 2005-07-27 11:45:17 -0700 | [diff] [blame] | 922 | #ifdef INJECT_FAULTS_3 |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 923 | outofdate = 1; |
| 924 | #else |
| 925 | outofdate = bitmap->flags & BITMAP_STALE; |
| 926 | #endif |
| 927 | if (outofdate) |
| 928 | printk(KERN_INFO "%s: bitmap file is out of date, doing full " |
| 929 | "recovery\n", bmname(bitmap)); |
| 930 | |
| 931 | bytes = (chunks + 7) / 8; |
NeilBrown | bc7f77d | 2005-06-21 17:17:17 -0700 | [diff] [blame] | 932 | |
NeilBrown | cdbb4cc | 2005-06-21 17:17:18 -0700 | [diff] [blame] | 933 | num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE; |
NeilBrown | bc7f77d | 2005-06-21 17:17:17 -0700 | [diff] [blame] | 934 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 935 | if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 936 | printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n", |
| 937 | bmname(bitmap), |
| 938 | (unsigned long) i_size_read(file->f_mapping->host), |
| 939 | bytes + sizeof(bitmap_super_t)); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 940 | goto err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 941 | } |
NeilBrown | bc7f77d | 2005-06-21 17:17:17 -0700 | [diff] [blame] | 942 | |
| 943 | ret = -ENOMEM; |
| 944 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 945 | bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL); |
NeilBrown | bc7f77d | 2005-06-21 17:17:17 -0700 | [diff] [blame] | 946 | if (!bitmap->filemap) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 947 | goto err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 948 | |
NeilBrown | e16b68b | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 949 | /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */ |
| 950 | bitmap->filemap_attr = kzalloc( |
Neil Brown | 505fa2c | 2007-04-11 23:28:44 -0700 | [diff] [blame] | 951 | roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)), |
NeilBrown | e16b68b | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 952 | GFP_KERNEL); |
NeilBrown | bc7f77d | 2005-06-21 17:17:17 -0700 | [diff] [blame] | 953 | if (!bitmap->filemap_attr) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 954 | goto err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 955 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 956 | oldindex = ~0L; |
| 957 | |
| 958 | for (i = 0; i < chunks; i++) { |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 959 | int b; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 960 | index = file_page_index(i); |
| 961 | bit = file_page_offset(i); |
| 962 | if (index != oldindex) { /* this is a new page, read it in */ |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 963 | int count; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 964 | /* unmap the old page, we're done with it */ |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 965 | if (index == num_pages-1) |
NeilBrown | f49d5e6 | 2007-01-26 00:57:03 -0800 | [diff] [blame] | 966 | count = bytes + sizeof(bitmap_super_t) |
| 967 | - index * PAGE_SIZE; |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 968 | else |
| 969 | count = PAGE_SIZE; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 970 | if (index == 0) { |
| 971 | /* |
| 972 | * if we're here then the superblock page |
| 973 | * contains some bits (PAGE_SIZE != sizeof sb) |
| 974 | * we've already read it in, so just use it |
| 975 | */ |
| 976 | page = bitmap->sb_page; |
| 977 | offset = sizeof(bitmap_super_t); |
NeilBrown | 5384527 | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 978 | if (!file) |
| 979 | read_sb_page(bitmap->mddev, |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 980 | bitmap->mddev->bitmap_info.offset, |
NeilBrown | 5384527 | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 981 | page, |
| 982 | index, count); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 983 | } else if (file) { |
NeilBrown | d785a06 | 2006-06-26 00:27:48 -0700 | [diff] [blame] | 984 | page = read_page(file, index, bitmap, count); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 985 | offset = 0; |
| 986 | } else { |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 987 | page = read_sb_page(bitmap->mddev, |
| 988 | bitmap->mddev->bitmap_info.offset, |
NeilBrown | a2ed961 | 2008-12-19 16:25:01 +1100 | [diff] [blame] | 989 | NULL, |
| 990 | index, count); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 991 | offset = 0; |
| 992 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 993 | if (IS_ERR(page)) { /* read error */ |
| 994 | ret = PTR_ERR(page); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 995 | goto err; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 996 | } |
| 997 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 998 | oldindex = index; |
| 999 | oldpage = page; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1000 | |
NeilBrown | b74fd28 | 2009-05-07 12:47:19 +1000 | [diff] [blame] | 1001 | bitmap->filemap[bitmap->file_pages++] = page; |
| 1002 | bitmap->last_page_size = count; |
| 1003 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1004 | if (outofdate) { |
| 1005 | /* |
| 1006 | * if bitmap is out of date, dirty the |
| 1007 | * whole page and write it out |
| 1008 | */ |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1009 | paddr = kmap_atomic(page, KM_USER0); |
| 1010 | memset(paddr + offset, 0xff, |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 1011 | PAGE_SIZE - offset); |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1012 | kunmap_atomic(paddr, KM_USER0); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1013 | write_page(bitmap, page, 1); |
| 1014 | |
| 1015 | ret = -EIO; |
NeilBrown | b74fd28 | 2009-05-07 12:47:19 +1000 | [diff] [blame] | 1016 | if (bitmap->flags & BITMAP_WRITE_ERROR) |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1017 | goto err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1018 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1019 | } |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1020 | paddr = kmap_atomic(page, KM_USER0); |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 1021 | if (bitmap->flags & BITMAP_HOSTENDIAN) |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1022 | b = test_bit(bit, paddr); |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 1023 | else |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1024 | b = ext2_test_bit(bit, paddr); |
| 1025 | kunmap_atomic(paddr, KM_USER0); |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 1026 | if (b) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1027 | /* if the disk bit is set, set the memory bit */ |
NeilBrown | db305e5 | 2009-05-07 12:49:06 +1000 | [diff] [blame] | 1028 | int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) |
| 1029 | >= start); |
| 1030 | bitmap_set_memory_bits(bitmap, |
| 1031 | (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap), |
| 1032 | needed); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1033 | bit_cnt++; |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 1034 | set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1035 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | /* everything went OK */ |
| 1039 | ret = 0; |
| 1040 | bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET); |
| 1041 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1042 | if (bit_cnt) { /* Kick recovery if any bits were set */ |
| 1043 | set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery); |
| 1044 | md_wakeup_thread(bitmap->mddev->thread); |
| 1045 | } |
| 1046 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1047 | printk(KERN_INFO "%s: bitmap initialized from disk: " |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1048 | "read %lu/%lu pages, set %lu bits\n", |
| 1049 | bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1050 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1051 | return 0; |
| 1052 | |
| 1053 | err: |
| 1054 | printk(KERN_INFO "%s: bitmap initialisation failed: %d\n", |
| 1055 | bmname(bitmap), ret); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1056 | return ret; |
| 1057 | } |
| 1058 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1059 | void bitmap_write_all(struct bitmap *bitmap) |
| 1060 | { |
| 1061 | /* We don't actually write all bitmap blocks here, |
| 1062 | * just flag them as needing to be written |
| 1063 | */ |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 1064 | int i; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1065 | |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 1066 | for (i=0; i < bitmap->file_pages; i++) |
| 1067 | set_page_attr(bitmap, bitmap->filemap[i], |
| 1068 | BITMAP_PAGE_NEEDWRITE); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1071 | |
| 1072 | static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc) |
| 1073 | { |
| 1074 | sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap); |
| 1075 | unsigned long page = chunk >> PAGE_COUNTER_SHIFT; |
| 1076 | bitmap->bp[page].count += inc; |
| 1077 | /* |
| 1078 | if (page == 0) printk("count page 0, offset %llu: %d gives %d\n", |
| 1079 | (unsigned long long)offset, inc, bitmap->bp[page].count); |
| 1080 | */ |
| 1081 | bitmap_checkfree(bitmap, page); |
| 1082 | } |
| 1083 | static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap, |
| 1084 | sector_t offset, int *blocks, |
| 1085 | int create); |
| 1086 | |
| 1087 | /* |
| 1088 | * bitmap daemon -- periodically wakes up to clean bits and flush pages |
| 1089 | * out to disk |
| 1090 | */ |
| 1091 | |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1092 | void bitmap_daemon_work(mddev_t *mddev) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1093 | { |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1094 | struct bitmap *bitmap; |
NeilBrown | aa3163f | 2005-06-21 17:17:22 -0700 | [diff] [blame] | 1095 | unsigned long j; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1096 | unsigned long flags; |
| 1097 | struct page *page = NULL, *lastpage = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1098 | int blocks; |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1099 | void *paddr; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1100 | |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1101 | /* Use a mutex to guard daemon_work against |
| 1102 | * bitmap_destroy. |
| 1103 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1104 | mutex_lock(&mddev->bitmap_info.mutex); |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1105 | bitmap = mddev->bitmap; |
| 1106 | if (bitmap == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1107 | mutex_unlock(&mddev->bitmap_info.mutex); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1108 | return; |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1109 | } |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1110 | if (time_before(jiffies, bitmap->daemon_lastrun |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1111 | + bitmap->mddev->bitmap_info.daemon_sleep)) |
NeilBrown | 7be3dfe | 2008-03-10 11:43:48 -0700 | [diff] [blame] | 1112 | goto done; |
| 1113 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1114 | bitmap->daemon_lastrun = jiffies; |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1115 | if (bitmap->allclean) { |
| 1116 | bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1117 | goto done; |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1118 | } |
| 1119 | bitmap->allclean = 1; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1120 | |
NeilBrown | be51269 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 1121 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1122 | for (j = 0; j < bitmap->chunks; j++) { |
| 1123 | bitmap_counter_t *bmc; |
NeilBrown | be51269 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 1124 | if (!bitmap->filemap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1125 | /* error or shutdown */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1126 | break; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1127 | |
| 1128 | page = filemap_get_page(bitmap, j); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1129 | |
| 1130 | if (page != lastpage) { |
NeilBrown | aa3163f | 2005-06-21 17:17:22 -0700 | [diff] [blame] | 1131 | /* skip this page unless it's marked as needing cleaning */ |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 1132 | if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) { |
| 1133 | int need_write = test_page_attr(bitmap, page, |
| 1134 | BITMAP_PAGE_NEEDWRITE); |
NeilBrown | a647e4b | 2006-06-26 00:27:46 -0700 | [diff] [blame] | 1135 | if (need_write) |
NeilBrown | aa3163f | 2005-06-21 17:17:22 -0700 | [diff] [blame] | 1136 | clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE); |
NeilBrown | a647e4b | 2006-06-26 00:27:46 -0700 | [diff] [blame] | 1137 | |
NeilBrown | aa3163f | 2005-06-21 17:17:22 -0700 | [diff] [blame] | 1138 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1139 | if (need_write) { |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1140 | write_page(bitmap, page, 0); |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1141 | bitmap->allclean = 0; |
| 1142 | } |
NeilBrown | be51269 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 1143 | spin_lock_irqsave(&bitmap->lock, flags); |
| 1144 | j |= (PAGE_BITS - 1); |
NeilBrown | aa3163f | 2005-06-21 17:17:22 -0700 | [diff] [blame] | 1145 | continue; |
| 1146 | } |
| 1147 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1148 | /* grab the new page, sync and release the old */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1149 | if (lastpage != NULL) { |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 1150 | if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1151 | clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); |
| 1152 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1153 | write_page(bitmap, lastpage, 0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1154 | } else { |
| 1155 | set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); |
| 1156 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 1157 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1158 | } else |
| 1159 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 1160 | lastpage = page; |
Neil Brown | a0da84f | 2008-06-28 08:31:22 +1000 | [diff] [blame] | 1161 | |
| 1162 | /* We are possibly going to clear some bits, so make |
| 1163 | * sure that events_cleared is up-to-date. |
| 1164 | */ |
| 1165 | if (bitmap->need_sync) { |
| 1166 | bitmap_super_t *sb; |
| 1167 | bitmap->need_sync = 0; |
| 1168 | sb = kmap_atomic(bitmap->sb_page, KM_USER0); |
| 1169 | sb->events_cleared = |
| 1170 | cpu_to_le64(bitmap->events_cleared); |
| 1171 | kunmap_atomic(sb, KM_USER0); |
| 1172 | write_page(bitmap, bitmap->sb_page, 1); |
| 1173 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1174 | spin_lock_irqsave(&bitmap->lock, flags); |
| 1175 | clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN); |
| 1176 | } |
NeilBrown | db305e5 | 2009-05-07 12:49:06 +1000 | [diff] [blame] | 1177 | bmc = bitmap_get_counter(bitmap, |
| 1178 | (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap), |
| 1179 | &blocks, 0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1180 | if (bmc) { |
| 1181 | /* |
| 1182 | if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc); |
| 1183 | */ |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1184 | if (*bmc) |
| 1185 | bitmap->allclean = 0; |
| 1186 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1187 | if (*bmc == 2) { |
| 1188 | *bmc=1; /* maybe clear the bit next time */ |
| 1189 | set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN); |
| 1190 | } else if (*bmc == 1) { |
| 1191 | /* we can clear the bit */ |
| 1192 | *bmc = 0; |
NeilBrown | db305e5 | 2009-05-07 12:49:06 +1000 | [diff] [blame] | 1193 | bitmap_count_page(bitmap, |
| 1194 | (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap), |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1195 | -1); |
| 1196 | |
| 1197 | /* clear the bit */ |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1198 | paddr = kmap_atomic(page, KM_USER0); |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 1199 | if (bitmap->flags & BITMAP_HOSTENDIAN) |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1200 | clear_bit(file_page_offset(j), paddr); |
NeilBrown | bd926c6 | 2005-11-08 21:39:32 -0800 | [diff] [blame] | 1201 | else |
NeilBrown | ea03aff | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1202 | ext2_clear_bit(file_page_offset(j), paddr); |
| 1203 | kunmap_atomic(paddr, KM_USER0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1204 | } |
NeilBrown | be51269 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 1205 | } else |
| 1206 | j |= PAGE_COUNTER_MASK; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1207 | } |
NeilBrown | be51269 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 1208 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1209 | |
| 1210 | /* now sync the final page */ |
| 1211 | if (lastpage != NULL) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1212 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | ec7a319 | 2006-06-26 00:27:45 -0700 | [diff] [blame] | 1213 | if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1214 | clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); |
| 1215 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1216 | write_page(bitmap, lastpage, 0); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1217 | } else { |
| 1218 | set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); |
| 1219 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 1220 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
NeilBrown | 7be3dfe | 2008-03-10 11:43:48 -0700 | [diff] [blame] | 1223 | done: |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1224 | if (bitmap->allclean == 0) |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1225 | bitmap->mddev->thread->timeout = |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1226 | bitmap->mddev->bitmap_info.daemon_sleep; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1227 | mutex_unlock(&mddev->bitmap_info.mutex); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1230 | static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap, |
| 1231 | sector_t offset, int *blocks, |
| 1232 | int create) |
NeilBrown | ee305ac | 2009-09-23 18:06:44 +1000 | [diff] [blame] | 1233 | __releases(bitmap->lock) |
| 1234 | __acquires(bitmap->lock) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1235 | { |
| 1236 | /* If 'create', we might release the lock and reclaim it. |
| 1237 | * The lock must have been taken with interrupts enabled. |
| 1238 | * If !create, we don't release the lock. |
| 1239 | */ |
| 1240 | sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap); |
| 1241 | unsigned long page = chunk >> PAGE_COUNTER_SHIFT; |
| 1242 | unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT; |
| 1243 | sector_t csize; |
| 1244 | |
| 1245 | if (bitmap_checkpage(bitmap, page, create) < 0) { |
| 1246 | csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap)); |
| 1247 | *blocks = csize - (offset & (csize- 1)); |
| 1248 | return NULL; |
| 1249 | } |
| 1250 | /* now locked ... */ |
| 1251 | |
| 1252 | if (bitmap->bp[page].hijacked) { /* hijacked pointer */ |
| 1253 | /* should we use the first or second counter field |
| 1254 | * of the hijacked pointer? */ |
| 1255 | int hi = (pageoff > PAGE_COUNTER_MASK); |
| 1256 | csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) + |
| 1257 | PAGE_COUNTER_SHIFT - 1); |
| 1258 | *blocks = csize - (offset & (csize- 1)); |
| 1259 | return &((bitmap_counter_t *) |
| 1260 | &bitmap->bp[page].map)[hi]; |
| 1261 | } else { /* page is allocated */ |
| 1262 | csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap)); |
| 1263 | *blocks = csize - (offset & (csize- 1)); |
| 1264 | return (bitmap_counter_t *) |
| 1265 | &(bitmap->bp[page].map[pageoff]); |
| 1266 | } |
| 1267 | } |
| 1268 | |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1269 | 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] | 1270 | { |
| 1271 | if (!bitmap) return 0; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1272 | |
| 1273 | if (behind) { |
| 1274 | atomic_inc(&bitmap->behind_writes); |
| 1275 | PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n", |
| 1276 | atomic_read(&bitmap->behind_writes), bitmap->max_write_behind); |
| 1277 | } |
| 1278 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1279 | while (sectors) { |
| 1280 | int blocks; |
| 1281 | bitmap_counter_t *bmc; |
| 1282 | |
| 1283 | spin_lock_irq(&bitmap->lock); |
| 1284 | bmc = bitmap_get_counter(bitmap, offset, &blocks, 1); |
| 1285 | if (!bmc) { |
| 1286 | spin_unlock_irq(&bitmap->lock); |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1290 | if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) { |
| 1291 | DEFINE_WAIT(__wait); |
| 1292 | /* note that it is safe to do the prepare_to_wait |
| 1293 | * after the test as long as we do it before dropping |
| 1294 | * the spinlock. |
| 1295 | */ |
| 1296 | prepare_to_wait(&bitmap->overflow_wait, &__wait, |
| 1297 | TASK_UNINTERRUPTIBLE); |
| 1298 | spin_unlock_irq(&bitmap->lock); |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 1299 | blk_unplug(bitmap->mddev->queue); |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1300 | schedule(); |
| 1301 | finish_wait(&bitmap->overflow_wait, &__wait); |
| 1302 | continue; |
| 1303 | } |
| 1304 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1305 | switch(*bmc) { |
| 1306 | case 0: |
| 1307 | bitmap_file_set_bit(bitmap, offset); |
| 1308 | bitmap_count_page(bitmap,offset, 1); |
Jens Axboe | 93769f5 | 2008-08-01 20:32:31 +0200 | [diff] [blame] | 1309 | blk_plug_device_unlocked(bitmap->mddev->queue); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1310 | /* fall through */ |
| 1311 | case 1: |
| 1312 | *bmc = 2; |
| 1313 | } |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1314 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1315 | (*bmc)++; |
| 1316 | |
| 1317 | spin_unlock_irq(&bitmap->lock); |
| 1318 | |
| 1319 | offset += blocks; |
| 1320 | if (sectors > blocks) |
| 1321 | sectors -= blocks; |
| 1322 | else sectors = 0; |
| 1323 | } |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1324 | bitmap->allclean = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1329 | int success, int behind) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1330 | { |
| 1331 | if (!bitmap) return; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1332 | if (behind) { |
| 1333 | atomic_dec(&bitmap->behind_writes); |
| 1334 | PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n", |
| 1335 | atomic_read(&bitmap->behind_writes), bitmap->max_write_behind); |
| 1336 | } |
NeilBrown | d0a4bb4 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1337 | if (bitmap->mddev->degraded) |
| 1338 | /* Never clear bits or update events_cleared when degraded */ |
| 1339 | success = 0; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1340 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1341 | while (sectors) { |
| 1342 | int blocks; |
| 1343 | unsigned long flags; |
| 1344 | bitmap_counter_t *bmc; |
| 1345 | |
| 1346 | spin_lock_irqsave(&bitmap->lock, flags); |
| 1347 | bmc = bitmap_get_counter(bitmap, offset, &blocks, 0); |
| 1348 | if (!bmc) { |
| 1349 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 1350 | return; |
| 1351 | } |
| 1352 | |
Neil Brown | a0da84f | 2008-06-28 08:31:22 +1000 | [diff] [blame] | 1353 | if (success && |
| 1354 | bitmap->events_cleared < bitmap->mddev->events) { |
| 1355 | bitmap->events_cleared = bitmap->mddev->events; |
| 1356 | bitmap->need_sync = 1; |
| 1357 | } |
| 1358 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1359 | if (!success && ! (*bmc & NEEDED_MASK)) |
| 1360 | *bmc |= NEEDED_MASK; |
| 1361 | |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1362 | if ((*bmc & COUNTER_MAX) == COUNTER_MAX) |
| 1363 | wake_up(&bitmap->overflow_wait); |
| 1364 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1365 | (*bmc)--; |
| 1366 | if (*bmc <= 2) { |
| 1367 | set_page_attr(bitmap, |
| 1368 | filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)), |
| 1369 | BITMAP_PAGE_CLEAN); |
| 1370 | } |
| 1371 | spin_unlock_irqrestore(&bitmap->lock, flags); |
| 1372 | offset += blocks; |
| 1373 | if (sectors > blocks) |
| 1374 | sectors -= blocks; |
| 1375 | else sectors = 0; |
| 1376 | } |
| 1377 | } |
| 1378 | |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1379 | static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks, |
| 1380 | int degraded) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1381 | { |
| 1382 | bitmap_counter_t *bmc; |
| 1383 | int rv; |
| 1384 | if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */ |
| 1385 | *blocks = 1024; |
| 1386 | return 1; /* always resync if no bitmap */ |
| 1387 | } |
| 1388 | spin_lock_irq(&bitmap->lock); |
| 1389 | bmc = bitmap_get_counter(bitmap, offset, blocks, 0); |
| 1390 | rv = 0; |
| 1391 | if (bmc) { |
| 1392 | /* locked */ |
| 1393 | if (RESYNC(*bmc)) |
| 1394 | rv = 1; |
| 1395 | else if (NEEDED(*bmc)) { |
| 1396 | rv = 1; |
NeilBrown | 6a806c5 | 2005-07-15 03:56:35 -0700 | [diff] [blame] | 1397 | if (!degraded) { /* don't set/clear bits if degraded */ |
| 1398 | *bmc |= RESYNC_MASK; |
| 1399 | *bmc &= ~NEEDED_MASK; |
| 1400 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1401 | } |
| 1402 | } |
| 1403 | spin_unlock_irq(&bitmap->lock); |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1404 | bitmap->allclean = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1405 | return rv; |
| 1406 | } |
| 1407 | |
NeilBrown | 1187cf0 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1408 | int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks, |
| 1409 | int degraded) |
| 1410 | { |
| 1411 | /* bitmap_start_sync must always report on multiples of whole |
| 1412 | * pages, otherwise resync (which is very PAGE_SIZE based) will |
| 1413 | * get confused. |
| 1414 | * So call __bitmap_start_sync repeatedly (if needed) until |
| 1415 | * At least PAGE_SIZE>>9 blocks are covered. |
| 1416 | * Return the 'or' of the result. |
| 1417 | */ |
| 1418 | int rv = 0; |
| 1419 | int blocks1; |
| 1420 | |
| 1421 | *blocks = 0; |
| 1422 | while (*blocks < (PAGE_SIZE>>9)) { |
| 1423 | rv |= __bitmap_start_sync(bitmap, offset, |
| 1424 | &blocks1, degraded); |
| 1425 | offset += blocks1; |
| 1426 | *blocks += blocks1; |
| 1427 | } |
| 1428 | return rv; |
| 1429 | } |
| 1430 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1431 | void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted) |
| 1432 | { |
| 1433 | bitmap_counter_t *bmc; |
| 1434 | unsigned long flags; |
| 1435 | /* |
| 1436 | if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted); |
| 1437 | */ if (bitmap == NULL) { |
| 1438 | *blocks = 1024; |
| 1439 | return; |
| 1440 | } |
| 1441 | spin_lock_irqsave(&bitmap->lock, flags); |
| 1442 | bmc = bitmap_get_counter(bitmap, offset, blocks, 0); |
| 1443 | if (bmc == NULL) |
| 1444 | goto unlock; |
| 1445 | /* locked */ |
| 1446 | /* |
| 1447 | if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks); |
| 1448 | */ |
| 1449 | if (RESYNC(*bmc)) { |
| 1450 | *bmc &= ~RESYNC_MASK; |
| 1451 | |
| 1452 | if (!NEEDED(*bmc) && aborted) |
| 1453 | *bmc |= NEEDED_MASK; |
| 1454 | else { |
| 1455 | if (*bmc <= 2) { |
| 1456 | set_page_attr(bitmap, |
| 1457 | filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)), |
| 1458 | BITMAP_PAGE_CLEAN); |
| 1459 | } |
| 1460 | } |
| 1461 | } |
| 1462 | unlock: |
| 1463 | spin_unlock_irqrestore(&bitmap->lock, flags); |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1464 | bitmap->allclean = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | void bitmap_close_sync(struct bitmap *bitmap) |
| 1468 | { |
| 1469 | /* Sync has finished, and any bitmap chunks that weren't synced |
| 1470 | * properly have been aborted. It remains to us to clear the |
| 1471 | * RESYNC bit wherever it is still on |
| 1472 | */ |
| 1473 | sector_t sector = 0; |
| 1474 | int blocks; |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1475 | if (!bitmap) |
| 1476 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1477 | while (sector < bitmap->mddev->resync_max_sectors) { |
| 1478 | bitmap_end_sync(bitmap, sector, &blocks, 0); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1479 | sector += blocks; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1480 | } |
| 1481 | } |
| 1482 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1483 | void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector) |
| 1484 | { |
| 1485 | sector_t s = 0; |
| 1486 | int blocks; |
| 1487 | |
| 1488 | if (!bitmap) |
| 1489 | return; |
| 1490 | if (sector == 0) { |
| 1491 | bitmap->last_end_sync = jiffies; |
| 1492 | return; |
| 1493 | } |
| 1494 | if (time_before(jiffies, (bitmap->last_end_sync |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1495 | + bitmap->mddev->bitmap_info.daemon_sleep))) |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1496 | return; |
| 1497 | wait_event(bitmap->mddev->recovery_wait, |
| 1498 | atomic_read(&bitmap->mddev->recovery_active) == 0); |
| 1499 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1500 | bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync; |
| 1501 | set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1502 | sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1); |
| 1503 | s = 0; |
| 1504 | while (s < sector && s < bitmap->mddev->resync_max_sectors) { |
| 1505 | bitmap_end_sync(bitmap, s, &blocks, 0); |
| 1506 | s += blocks; |
| 1507 | } |
| 1508 | bitmap->last_end_sync = jiffies; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 1509 | sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed"); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1510 | } |
| 1511 | |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 1512 | 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] | 1513 | { |
| 1514 | /* For each chunk covered by any of these sectors, set the |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1515 | * counter to 1 and set resync_needed. They should all |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1516 | * be 0 at this point |
| 1517 | */ |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1518 | |
| 1519 | int secs; |
| 1520 | bitmap_counter_t *bmc; |
| 1521 | spin_lock_irq(&bitmap->lock); |
| 1522 | bmc = bitmap_get_counter(bitmap, offset, &secs, 1); |
| 1523 | if (!bmc) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1524 | spin_unlock_irq(&bitmap->lock); |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1525 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1526 | } |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1527 | if (! *bmc) { |
| 1528 | struct page *page; |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 1529 | *bmc = 1 | (needed?NEEDED_MASK:0); |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1530 | bitmap_count_page(bitmap, offset, 1); |
| 1531 | page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)); |
| 1532 | set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN); |
| 1533 | } |
| 1534 | spin_unlock_irq(&bitmap->lock); |
NeilBrown | 8311c29 | 2008-03-04 14:29:30 -0800 | [diff] [blame] | 1535 | bitmap->allclean = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 1538 | /* dirty the memory and file bits for bitmap chunks "s" to "e" */ |
| 1539 | void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e) |
| 1540 | { |
| 1541 | unsigned long chunk; |
| 1542 | |
| 1543 | for (chunk = s; chunk <= e; chunk++) { |
NeilBrown | db305e5 | 2009-05-07 12:49:06 +1000 | [diff] [blame] | 1544 | sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 1545 | bitmap_set_memory_bits(bitmap, sec, 1); |
| 1546 | bitmap_file_set_bit(bitmap, sec); |
| 1547 | } |
| 1548 | } |
| 1549 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1550 | /* |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 1551 | * flush out any pending updates |
| 1552 | */ |
| 1553 | void bitmap_flush(mddev_t *mddev) |
| 1554 | { |
| 1555 | struct bitmap *bitmap = mddev->bitmap; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1556 | long sleep; |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 1557 | |
| 1558 | if (!bitmap) /* there was no bitmap */ |
| 1559 | return; |
| 1560 | |
| 1561 | /* run the daemon_work three time to ensure everything is flushed |
| 1562 | * that can be |
| 1563 | */ |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1564 | sleep = mddev->bitmap_info.daemon_sleep * 2; |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1565 | bitmap->daemon_lastrun -= sleep; |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1566 | bitmap_daemon_work(mddev); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1567 | bitmap->daemon_lastrun -= sleep; |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1568 | bitmap_daemon_work(mddev); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1569 | bitmap->daemon_lastrun -= sleep; |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1570 | bitmap_daemon_work(mddev); |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 1571 | bitmap_update_sb(bitmap); |
| 1572 | } |
| 1573 | |
| 1574 | /* |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1575 | * free memory that was allocated |
| 1576 | */ |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1577 | static void bitmap_free(struct bitmap *bitmap) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1578 | { |
| 1579 | unsigned long k, pages; |
| 1580 | struct bitmap_page *bp; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1581 | |
| 1582 | if (!bitmap) /* there was no bitmap */ |
| 1583 | return; |
| 1584 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1585 | /* release the bitmap file and kill the daemon */ |
| 1586 | bitmap_file_put(bitmap); |
| 1587 | |
| 1588 | bp = bitmap->bp; |
| 1589 | pages = bitmap->pages; |
| 1590 | |
| 1591 | /* free all allocated memory */ |
| 1592 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1593 | if (bp) /* deallocate the page memory */ |
| 1594 | for (k = 0; k < pages; k++) |
| 1595 | if (bp[k].map && !bp[k].hijacked) |
| 1596 | kfree(bp[k].map); |
| 1597 | kfree(bp); |
| 1598 | kfree(bitmap); |
| 1599 | } |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 1600 | |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1601 | void bitmap_destroy(mddev_t *mddev) |
| 1602 | { |
| 1603 | struct bitmap *bitmap = mddev->bitmap; |
| 1604 | |
| 1605 | if (!bitmap) /* there was no bitmap */ |
| 1606 | return; |
| 1607 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1608 | mutex_lock(&mddev->bitmap_info.mutex); |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1609 | mddev->bitmap = NULL; /* disconnect from the md device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1610 | mutex_unlock(&mddev->bitmap_info.mutex); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1611 | if (mddev->thread) |
| 1612 | mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1613 | |
| 1614 | bitmap_free(bitmap); |
| 1615 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1616 | |
| 1617 | /* |
| 1618 | * initialize the bitmap structure |
| 1619 | * if this returns an error, bitmap_destroy must be called to do clean up |
| 1620 | */ |
| 1621 | int bitmap_create(mddev_t *mddev) |
| 1622 | { |
| 1623 | struct bitmap *bitmap; |
NeilBrown | 1f59390 | 2009-04-20 11:50:24 +1000 | [diff] [blame] | 1624 | sector_t blocks = mddev->resync_max_sectors; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1625 | unsigned long chunks; |
| 1626 | unsigned long pages; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1627 | struct file *file = mddev->bitmap_info.file; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1628 | int err; |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 1629 | sector_t start; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1630 | |
Alexey Dobriyan | 5f6e3c83 | 2006-10-11 01:22:26 -0700 | [diff] [blame] | 1631 | BUILD_BUG_ON(sizeof(bitmap_super_t) != 256); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1632 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1633 | if (!file && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1634 | return 0; |
| 1635 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1636 | BUG_ON(file && mddev->bitmap_info.offset); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1637 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 1638 | bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1639 | if (!bitmap) |
| 1640 | return -ENOMEM; |
| 1641 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1642 | spin_lock_init(&bitmap->lock); |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 1643 | atomic_set(&bitmap->pending_writes, 0); |
| 1644 | init_waitqueue_head(&bitmap->write_wait); |
Neil Brown | da6e1a3 | 2007-02-08 14:20:37 -0800 | [diff] [blame] | 1645 | init_waitqueue_head(&bitmap->overflow_wait); |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 1646 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1647 | bitmap->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1648 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1649 | bitmap->file = file; |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 1650 | if (file) { |
| 1651 | get_file(file); |
NeilBrown | ae8fa28 | 2009-10-16 15:56:01 +1100 | [diff] [blame] | 1652 | /* As future accesses to this file will use bmap, |
| 1653 | * and bypass the page cache, we must sync the file |
| 1654 | * first. |
| 1655 | */ |
| 1656 | vfs_fsync(file, file->f_dentry, 1); |
NeilBrown | ce25c31 | 2006-06-26 00:27:49 -0700 | [diff] [blame] | 1657 | } |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1658 | /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1659 | err = bitmap_read_sb(bitmap); |
| 1660 | if (err) |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1661 | goto error; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1662 | |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1663 | bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1664 | |
| 1665 | /* now that chunksize and chunkshift are set, we can use these macros */ |
NeilBrown | 1f59390 | 2009-04-20 11:50:24 +1000 | [diff] [blame] | 1666 | chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >> |
| 1667 | CHUNK_BLOCK_SHIFT(bitmap); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1668 | pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO; |
| 1669 | |
| 1670 | BUG_ON(!pages); |
| 1671 | |
| 1672 | bitmap->chunks = chunks; |
| 1673 | bitmap->pages = pages; |
| 1674 | bitmap->missing_pages = pages; |
| 1675 | bitmap->counter_bits = COUNTER_BITS; |
| 1676 | |
| 1677 | bitmap->syncchunk = ~0UL; |
| 1678 | |
Olaf Hering | 44456d3 | 2005-07-27 11:45:17 -0700 | [diff] [blame] | 1679 | #ifdef INJECT_FATAL_FAULT_1 |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1680 | bitmap->bp = NULL; |
| 1681 | #else |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 1682 | bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1683 | #endif |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1684 | err = -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1685 | if (!bitmap->bp) |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1686 | goto error; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1687 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1688 | /* now that we have some pages available, initialize the in-memory |
| 1689 | * bitmap from the on-disk bitmap */ |
NeilBrown | 6a07997 | 2005-09-09 16:23:44 -0700 | [diff] [blame] | 1690 | start = 0; |
| 1691 | if (mddev->degraded == 0 |
| 1692 | || bitmap->events_cleared == mddev->events) |
| 1693 | /* no need to keep dirty bits to optimise a re-add of a missing device */ |
| 1694 | start = mddev->recovery_cp; |
| 1695 | err = bitmap_init_from_disk(bitmap, start); |
NeilBrown | 193f1c9 | 2005-08-04 12:53:33 -0700 | [diff] [blame] | 1696 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1697 | if (err) |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1698 | goto error; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1699 | |
| 1700 | printk(KERN_INFO "created bitmap (%lu pages) for device %s\n", |
| 1701 | pages, bmname(bitmap)); |
| 1702 | |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1703 | mddev->bitmap = bitmap; |
| 1704 | |
NeilBrown | 1b04be9 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 1705 | mddev->thread->timeout = mddev->bitmap_info.daemon_sleep; |
NeilBrown | 9cd30fd | 2009-12-14 12:49:54 +1100 | [diff] [blame] | 1706 | md_wakeup_thread(mddev->thread); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1707 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1708 | bitmap_update_sb(bitmap); |
| 1709 | |
| 1710 | return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0; |
NeilBrown | 3178b0d | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 1711 | |
| 1712 | error: |
| 1713 | bitmap_free(bitmap); |
| 1714 | return err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | /* the bitmap API -- for raid personalities */ |
| 1718 | EXPORT_SYMBOL(bitmap_startwrite); |
| 1719 | EXPORT_SYMBOL(bitmap_endwrite); |
| 1720 | EXPORT_SYMBOL(bitmap_start_sync); |
| 1721 | EXPORT_SYMBOL(bitmap_end_sync); |
| 1722 | EXPORT_SYMBOL(bitmap_unplug); |
| 1723 | EXPORT_SYMBOL(bitmap_close_sync); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1724 | EXPORT_SYMBOL(bitmap_cond_end_sync); |