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