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