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