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