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