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