]>
Commit | Line | Data |
---|---|---|
32a7627c N |
1 | /* |
2 | * bitmap.c two-level bitmap (C) Peter T. Breuer ([email protected]) 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 | |
32a7627c N |
10 | */ |
11 | ||
12 | /* | |
13 | * Still to do: | |
14 | * | |
15 | * flush after percent set rather than just time based. (maybe both). | |
32a7627c N |
16 | */ |
17 | ||
bff61975 | 18 | #include <linux/blkdev.h> |
32a7627c | 19 | #include <linux/module.h> |
32a7627c N |
20 | #include <linux/errno.h> |
21 | #include <linux/slab.h> | |
22 | #include <linux/init.h> | |
32a7627c N |
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> | |
43b2e5d8 | 29 | #include "md.h" |
ef740c37 | 30 | #include "bitmap.h" |
32a7627c | 31 | |
ac2f40be | 32 | static inline char *bmname(struct bitmap *bitmap) |
32a7627c N |
33 | { |
34 | return bitmap->mddev ? mdname(bitmap->mddev) : "mdX"; | |
35 | } | |
36 | ||
32a7627c N |
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 | ||
ac2f40be | 44 | page = kzalloc(PAGE_SIZE, GFP_NOIO); |
32a7627c N |
45 | if (!page) |
46 | printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap)); | |
47 | else | |
36a4e1fe N |
48 | pr_debug("%s: bitmap_alloc_page: allocated page at %p\n", |
49 | bmname(bitmap), page); | |
32a7627c N |
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 | { | |
36a4e1fe | 58 | pr_debug("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page); |
32a7627c N |
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 | */ | |
ac2f40be N |
72 | static int bitmap_checkpage(struct bitmap *bitmap, |
73 | unsigned long page, int create) | |
ee305ace N |
74 | __releases(bitmap->lock) |
75 | __acquires(bitmap->lock) | |
32a7627c N |
76 | { |
77 | unsigned char *mappage; | |
78 | ||
79 | if (page >= bitmap->pages) { | |
1187cf0a N |
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 | */ | |
32a7627c N |
84 | return -EINVAL; |
85 | } | |
86 | ||
32a7627c N |
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 | ||
32a7627c N |
96 | /* this page has not been allocated yet */ |
97 | ||
ac2f40be N |
98 | spin_unlock_irq(&bitmap->lock); |
99 | mappage = bitmap_alloc_page(bitmap); | |
100 | spin_lock_irq(&bitmap->lock); | |
101 | ||
102 | if (mappage == NULL) { | |
36a4e1fe N |
103 | pr_debug("%s: bitmap map page allocation failed, hijacking\n", |
104 | bmname(bitmap)); | |
32a7627c N |
105 | /* failed - set the hijacked flag so that we can use the |
106 | * pointer as a counter */ | |
32a7627c N |
107 | if (!bitmap->bp[page].map) |
108 | bitmap->bp[page].hijacked = 1; | |
ac2f40be N |
109 | } else if (bitmap->bp[page].map || |
110 | bitmap->bp[page].hijacked) { | |
32a7627c N |
111 | /* somebody beat us to getting the page */ |
112 | bitmap_free_page(bitmap, mappage); | |
113 | return 0; | |
ac2f40be | 114 | } else { |
32a7627c | 115 | |
ac2f40be | 116 | /* no page was in place and we have one, so install it */ |
32a7627c | 117 | |
ac2f40be N |
118 | bitmap->bp[page].map = mappage; |
119 | bitmap->missing_pages--; | |
120 | } | |
32a7627c N |
121 | return 0; |
122 | } | |
123 | ||
32a7627c N |
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 */ | |
858119e1 | 127 | static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page) |
32a7627c N |
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; | |
ac2f40be N |
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); | |
32a7627c | 145 | } |
32a7627c N |
146 | } |
147 | ||
32a7627c N |
148 | /* |
149 | * bitmap file handling - read and write the bitmap file and its superblock | |
150 | */ | |
151 | ||
32a7627c N |
152 | /* |
153 | * basic page I/O operations | |
154 | */ | |
155 | ||
a654b9d8 | 156 | /* IO operations when bitmap is stored near all superblocks */ |
fd01b88c | 157 | static struct page *read_sb_page(struct mddev *mddev, loff_t offset, |
a2ed9615 N |
158 | struct page *page, |
159 | unsigned long index, int size) | |
a654b9d8 N |
160 | { |
161 | /* choose a good rdev and read the page from there */ | |
162 | ||
3cb03002 | 163 | struct md_rdev *rdev; |
a654b9d8 | 164 | sector_t target; |
ac2f40be | 165 | int did_alloc = 0; |
a654b9d8 | 166 | |
ac2f40be | 167 | if (!page) { |
a2ed9615 | 168 | page = alloc_page(GFP_KERNEL); |
ac2f40be N |
169 | if (!page) |
170 | return ERR_PTR(-ENOMEM); | |
171 | did_alloc = 1; | |
172 | } | |
a654b9d8 | 173 | |
159ec1fc | 174 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
b2d444d7 N |
175 | if (! test_bit(In_sync, &rdev->flags) |
176 | || test_bit(Faulty, &rdev->flags)) | |
ab904d63 N |
177 | continue; |
178 | ||
ccebd4c4 | 179 | target = offset + index * (PAGE_SIZE/512); |
a654b9d8 | 180 | |
2b193363 | 181 | if (sync_page_io(rdev, target, |
e1defc4f | 182 | roundup(size, bdev_logical_block_size(rdev->bdev)), |
ccebd4c4 | 183 | page, READ, true)) { |
ab904d63 | 184 | page->index = index; |
ce25c31b N |
185 | attach_page_buffers(page, NULL); /* so that free_buffer will |
186 | * quietly no-op */ | |
ab904d63 N |
187 | return page; |
188 | } | |
189 | } | |
ac2f40be N |
190 | if (did_alloc) |
191 | put_page(page); | |
ab904d63 | 192 | return ERR_PTR(-EIO); |
a654b9d8 | 193 | |
a654b9d8 N |
194 | } |
195 | ||
fd01b88c | 196 | static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev) |
b2d2c4ce N |
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) { | |
3cb03002 | 217 | rdev = list_entry(pos, struct md_rdev, same_set); |
b2d2c4ce | 218 | if (rdev->raid_disk >= 0 && |
b2d2c4ce N |
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 | ||
ab6085c7 | 230 | static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait) |
a654b9d8 | 231 | { |
3cb03002 | 232 | struct md_rdev *rdev = NULL; |
a6ff7e08 | 233 | struct block_device *bdev; |
fd01b88c | 234 | struct mddev *mddev = bitmap->mddev; |
a654b9d8 | 235 | |
b2d2c4ce | 236 | while ((rdev = next_active_rdev(rdev, mddev)) != NULL) { |
ac2f40be N |
237 | int size = PAGE_SIZE; |
238 | loff_t offset = mddev->bitmap_info.offset; | |
a6ff7e08 JB |
239 | |
240 | bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev; | |
241 | ||
ac2f40be N |
242 | if (page->index == bitmap->file_pages-1) |
243 | size = roundup(bitmap->last_page_size, | |
a6ff7e08 | 244 | bdev_logical_block_size(bdev)); |
ac2f40be N |
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); | |
b2d2c4ce | 285 | } |
a654b9d8 N |
286 | |
287 | if (wait) | |
a9701a30 | 288 | md_super_wait(mddev); |
a654b9d8 | 289 | return 0; |
4b80991c N |
290 | |
291 | bad_alignment: | |
4b80991c | 292 | return -EINVAL; |
a654b9d8 N |
293 | } |
294 | ||
4ad13663 | 295 | static void bitmap_file_kick(struct bitmap *bitmap); |
32a7627c | 296 | /* |
a654b9d8 | 297 | * write out a page to a file |
32a7627c | 298 | */ |
4ad13663 | 299 | static void write_page(struct bitmap *bitmap, struct page *page, int wait) |
32a7627c | 300 | { |
d785a06a | 301 | struct buffer_head *bh; |
32a7627c | 302 | |
f0d76d70 N |
303 | if (bitmap->file == NULL) { |
304 | switch (write_sb_page(bitmap, page, wait)) { | |
305 | case -EINVAL: | |
306 | bitmap->flags |= BITMAP_WRITE_ERROR; | |
f0d76d70 | 307 | } |
4ad13663 | 308 | } else { |
a654b9d8 | 309 | |
4ad13663 | 310 | bh = page_buffers(page); |
c708443c | 311 | |
4ad13663 N |
312 | while (bh && bh->b_blocknr) { |
313 | atomic_inc(&bitmap->pending_writes); | |
314 | set_buffer_locked(bh); | |
315 | set_buffer_mapped(bh); | |
721a9602 | 316 | submit_bh(WRITE | REQ_SYNC, bh); |
4ad13663 N |
317 | bh = bh->b_this_page; |
318 | } | |
d785a06a | 319 | |
ac2f40be | 320 | if (wait) |
4ad13663 N |
321 | wait_event(bitmap->write_wait, |
322 | atomic_read(&bitmap->pending_writes)==0); | |
8a5e9cf1 | 323 | } |
4ad13663 N |
324 | if (bitmap->flags & BITMAP_WRITE_ERROR) |
325 | bitmap_file_kick(bitmap); | |
d785a06a N |
326 | } |
327 | ||
328 | static void end_bitmap_write(struct buffer_head *bh, int uptodate) | |
329 | { | |
330 | struct bitmap *bitmap = bh->b_private; | |
331 | unsigned long flags; | |
32a7627c | 332 | |
d785a06a N |
333 | if (!uptodate) { |
334 | spin_lock_irqsave(&bitmap->lock, flags); | |
335 | bitmap->flags |= BITMAP_WRITE_ERROR; | |
336 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
32a7627c | 337 | } |
d785a06a N |
338 | if (atomic_dec_and_test(&bitmap->pending_writes)) |
339 | wake_up(&bitmap->write_wait); | |
340 | } | |
32a7627c | 341 | |
d785a06a N |
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); | |
77ad4bc7 | 353 | |
d785a06a N |
354 | while (bh) { |
355 | struct buffer_head *next = bh->b_this_page; | |
356 | free_buffer_head(bh); | |
357 | bh = next; | |
77ad4bc7 | 358 | } |
d785a06a N |
359 | __clear_page_buffers(page); |
360 | put_page(page); | |
32a7627c N |
361 | } |
362 | ||
d785a06a N |
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 | */ | |
32a7627c | 370 | static struct page *read_page(struct file *file, unsigned long index, |
d785a06a N |
371 | struct bitmap *bitmap, |
372 | unsigned long count) | |
32a7627c | 373 | { |
32a7627c | 374 | struct page *page = NULL; |
c649bb9c | 375 | struct inode *inode = file->f_path.dentry->d_inode; |
d785a06a N |
376 | struct buffer_head *bh; |
377 | sector_t block; | |
32a7627c | 378 | |
36a4e1fe N |
379 | pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE, |
380 | (unsigned long long)index << PAGE_SHIFT); | |
32a7627c | 381 | |
d785a06a N |
382 | page = alloc_page(GFP_KERNEL); |
383 | if (!page) | |
384 | page = ERR_PTR(-ENOMEM); | |
32a7627c N |
385 | if (IS_ERR(page)) |
386 | goto out; | |
d785a06a | 387 | |
d785a06a N |
388 | bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0); |
389 | if (!bh) { | |
2d1f3b5d | 390 | put_page(page); |
d785a06a | 391 | page = ERR_PTR(-ENOMEM); |
32a7627c N |
392 | goto out; |
393 | } | |
d785a06a N |
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); | |
412 | ||
413 | bh->b_end_io = end_bitmap_write; | |
414 | bh->b_private = bitmap; | |
ce25c31b N |
415 | atomic_inc(&bitmap->pending_writes); |
416 | set_buffer_locked(bh); | |
417 | set_buffer_mapped(bh); | |
418 | submit_bh(READ, bh); | |
d785a06a N |
419 | } |
420 | block++; | |
421 | bh = bh->b_this_page; | |
422 | } | |
d785a06a | 423 | page->index = index; |
ce25c31b N |
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 | } | |
32a7627c N |
431 | out: |
432 | if (IS_ERR(page)) | |
ac2f40be | 433 | printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n", |
2d1f3b5d N |
434 | (int)PAGE_SIZE, |
435 | (unsigned long long)index << PAGE_SHIFT, | |
32a7627c N |
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 */ | |
4ad13663 | 445 | void bitmap_update_sb(struct bitmap *bitmap) |
32a7627c N |
446 | { |
447 | bitmap_super_t *sb; | |
448 | unsigned long flags; | |
449 | ||
450 | if (!bitmap || !bitmap->mddev) /* no bitmap for this array */ | |
4ad13663 | 451 | return; |
ece5cff0 N |
452 | if (bitmap->mddev->bitmap_info.external) |
453 | return; | |
32a7627c N |
454 | spin_lock_irqsave(&bitmap->lock, flags); |
455 | if (!bitmap->sb_page) { /* no superblock */ | |
456 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
4ad13663 | 457 | return; |
32a7627c | 458 | } |
32a7627c | 459 | spin_unlock_irqrestore(&bitmap->lock, flags); |
7b92813c | 460 | sb = kmap_atomic(bitmap->sb_page, KM_USER0); |
32a7627c | 461 | sb->events = cpu_to_le64(bitmap->mddev->events); |
8258c532 | 462 | if (bitmap->mddev->events < bitmap->events_cleared) |
a0da84f3 NB |
463 | /* rocking back to read-only */ |
464 | bitmap->events_cleared = bitmap->mddev->events; | |
8258c532 N |
465 | sb->events_cleared = cpu_to_le64(bitmap->events_cleared); |
466 | sb->state = cpu_to_le32(bitmap->flags); | |
43a70507 N |
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); | |
ea03aff9 | 470 | kunmap_atomic(sb, KM_USER0); |
4ad13663 | 471 | write_page(bitmap, bitmap->sb_page, 1); |
32a7627c N |
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; | |
7b92813c | 481 | sb = kmap_atomic(bitmap->sb_page, KM_USER0); |
32a7627c | 482 | printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap)); |
a2cff26a N |
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", | |
32a7627c N |
486 | *(__u32 *)(sb->uuid+0), |
487 | *(__u32 *)(sb->uuid+4), | |
488 | *(__u32 *)(sb->uuid+8), | |
489 | *(__u32 *)(sb->uuid+12)); | |
a2cff26a | 490 | printk(KERN_DEBUG " events: %llu\n", |
32a7627c | 491 | (unsigned long long) le64_to_cpu(sb->events)); |
a2cff26a | 492 | printk(KERN_DEBUG "events cleared: %llu\n", |
32a7627c | 493 | (unsigned long long) le64_to_cpu(sb->events_cleared)); |
a2cff26a N |
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); | |
4b6d287f | 499 | printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind)); |
ea03aff9 | 500 | kunmap_atomic(sb, KM_USER0); |
32a7627c N |
501 | } |
502 | ||
9c81075f JB |
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 | ||
32a7627c N |
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; | |
4b6d287f | 584 | unsigned long chunksize, daemon_sleep, write_behind; |
32a7627c N |
585 | unsigned long long events; |
586 | int err = -EINVAL; | |
587 | ||
588 | /* page 0 is the superblock, read it... */ | |
f49d5e62 N |
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 { | |
42a04b50 N |
595 | bitmap->sb_page = read_sb_page(bitmap->mddev, |
596 | bitmap->mddev->bitmap_info.offset, | |
a2ed9615 N |
597 | NULL, |
598 | 0, sizeof(bitmap_super_t)); | |
a654b9d8 | 599 | } |
32a7627c N |
600 | if (IS_ERR(bitmap->sb_page)) { |
601 | err = PTR_ERR(bitmap->sb_page); | |
602 | bitmap->sb_page = NULL; | |
603 | return err; | |
604 | } | |
605 | ||
7b92813c | 606 | sb = kmap_atomic(bitmap->sb_page, KM_USER0); |
32a7627c | 607 | |
32a7627c | 608 | chunksize = le32_to_cpu(sb->chunksize); |
1b04be96 | 609 | daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ; |
4b6d287f | 610 | write_behind = le32_to_cpu(sb->write_behind); |
32a7627c N |
611 | |
612 | /* verify that the bitmap-specific fields are valid */ | |
613 | if (sb->magic != cpu_to_le32(BITMAP_MAGIC)) | |
614 | reason = "bad magic"; | |
bd926c63 N |
615 | else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO || |
616 | le32_to_cpu(sb->version) > BITMAP_MAJOR_HI) | |
32a7627c | 617 | reason = "unrecognized superblock version"; |
1187cf0a | 618 | else if (chunksize < 512) |
7dd5d34c | 619 | reason = "bitmap chunksize too small"; |
d744540c | 620 | else if (!is_power_of_2(chunksize)) |
32a7627c | 621 | reason = "bitmap chunksize not a power of 2"; |
1b04be96 | 622 | else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT) |
7dd5d34c | 623 | reason = "daemon sleep period out of range"; |
4b6d287f N |
624 | else if (write_behind > COUNTER_MAX) |
625 | reason = "write-behind limit out of range (0 - 16383)"; | |
32a7627c N |
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); | |
4f2e639a | 652 | sb->state |= cpu_to_le32(BITMAP_STALE); |
32a7627c N |
653 | } |
654 | success: | |
655 | /* assign fields using values from superblock */ | |
42a04b50 N |
656 | bitmap->mddev->bitmap_info.chunksize = chunksize; |
657 | bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep; | |
42a04b50 | 658 | bitmap->mddev->bitmap_info.max_write_behind = write_behind; |
4f2e639a | 659 | bitmap->flags |= le32_to_cpu(sb->state); |
bd926c63 N |
660 | if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN) |
661 | bitmap->flags |= BITMAP_HOSTENDIAN; | |
32a7627c | 662 | bitmap->events_cleared = le64_to_cpu(sb->events_cleared); |
8258c532 | 663 | if (bitmap->flags & BITMAP_STALE) |
6a07997f | 664 | bitmap->events_cleared = bitmap->mddev->events; |
32a7627c N |
665 | err = 0; |
666 | out: | |
ea03aff9 | 667 | kunmap_atomic(sb, KM_USER0); |
32a7627c N |
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 | ||
4ad13663 N |
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) | |
32a7627c N |
681 | { |
682 | bitmap_super_t *sb; | |
683 | unsigned long flags; | |
4ad13663 | 684 | int old; |
32a7627c N |
685 | |
686 | spin_lock_irqsave(&bitmap->lock, flags); | |
7e317655 | 687 | if (!bitmap->sb_page) { /* can't set the state */ |
32a7627c | 688 | spin_unlock_irqrestore(&bitmap->lock, flags); |
4ad13663 | 689 | return 0; |
32a7627c | 690 | } |
32a7627c | 691 | spin_unlock_irqrestore(&bitmap->lock, flags); |
7b92813c | 692 | sb = kmap_atomic(bitmap->sb_page, KM_USER0); |
4ad13663 | 693 | old = le32_to_cpu(sb->state) & bits; |
32a7627c | 694 | switch (op) { |
ac2f40be N |
695 | case MASK_SET: |
696 | sb->state |= cpu_to_le32(bits); | |
8258c532 | 697 | bitmap->flags |= bits; |
ac2f40be N |
698 | break; |
699 | case MASK_UNSET: | |
700 | sb->state &= cpu_to_le32(~bits); | |
8258c532 | 701 | bitmap->flags &= ~bits; |
ac2f40be N |
702 | break; |
703 | default: | |
704 | BUG(); | |
32a7627c | 705 | } |
ea03aff9 | 706 | kunmap_atomic(sb, KM_USER0); |
4ad13663 | 707 | return old; |
32a7627c N |
708 | } |
709 | ||
710 | /* | |
711 | * general bitmap file operations | |
712 | */ | |
713 | ||
ece5cff0 N |
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 | */ | |
32a7627c | 720 | /* calculate the index of the page that contains this bit */ |
ece5cff0 | 721 | static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk) |
32a7627c | 722 | { |
ece5cff0 N |
723 | if (!bitmap->mddev->bitmap_info.external) |
724 | chunk += sizeof(bitmap_super_t) << 3; | |
725 | return chunk >> PAGE_BIT_SHIFT; | |
32a7627c N |
726 | } |
727 | ||
728 | /* calculate the (bit) offset of this bit within a page */ | |
ece5cff0 | 729 | static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk) |
32a7627c | 730 | { |
ece5cff0 N |
731 | if (!bitmap->mddev->bitmap_info.external) |
732 | chunk += sizeof(bitmap_super_t) << 3; | |
733 | return chunk & (PAGE_BITS - 1); | |
32a7627c N |
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, | |
3520fa4d | 744 | unsigned long chunk) |
32a7627c | 745 | { |
ac2f40be N |
746 | if (file_page_index(bitmap, chunk) >= bitmap->file_pages) |
747 | return NULL; | |
ece5cff0 N |
748 | return bitmap->filemap[file_page_index(bitmap, chunk) |
749 | - file_page_index(bitmap, 0)]; | |
32a7627c N |
750 | } |
751 | ||
32a7627c N |
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--) | |
ece5cff0 | 771 | if (map[pages] != sb_page) /* 0 is sb_page, release it below */ |
d785a06a | 772 | free_buffers(map[pages]); |
32a7627c N |
773 | kfree(map); |
774 | kfree(attr); | |
775 | ||
d785a06a N |
776 | if (sb_page) |
777 | free_buffers(sb_page); | |
32a7627c N |
778 | } |
779 | ||
780 | static void bitmap_file_put(struct bitmap *bitmap) | |
781 | { | |
782 | struct file *file; | |
32a7627c N |
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 | ||
d785a06a N |
790 | if (file) |
791 | wait_event(bitmap->write_wait, | |
792 | atomic_read(&bitmap->pending_writes)==0); | |
32a7627c N |
793 | bitmap_file_unmap(bitmap); |
794 | ||
d785a06a | 795 | if (file) { |
c649bb9c | 796 | struct inode *inode = file->f_path.dentry->d_inode; |
fc0ecff6 | 797 | invalidate_mapping_pages(inode->i_mapping, 0, -1); |
32a7627c | 798 | fput(file); |
d785a06a | 799 | } |
32a7627c N |
800 | } |
801 | ||
32a7627c N |
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 | ||
4ad13663 N |
811 | if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) { |
812 | bitmap_update_sb(bitmap); | |
32a7627c | 813 | |
4ad13663 N |
814 | if (bitmap->file) { |
815 | path = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
816 | if (path) | |
6bcfd601 CH |
817 | ptr = d_path(&bitmap->file->f_path, path, |
818 | PAGE_SIZE); | |
819 | ||
4ad13663 N |
820 | printk(KERN_ALERT |
821 | "%s: kicking failed bitmap file %s from array!\n", | |
6bcfd601 | 822 | bmname(bitmap), IS_ERR(ptr) ? "" : ptr); |
32a7627c | 823 | |
4ad13663 N |
824 | kfree(path); |
825 | } else | |
826 | printk(KERN_ALERT | |
827 | "%s: disabling internal bitmap due to errors\n", | |
828 | bmname(bitmap)); | |
a654b9d8 | 829 | } |
32a7627c N |
830 | |
831 | bitmap_file_put(bitmap); | |
832 | ||
833 | return; | |
834 | } | |
835 | ||
836 | enum bitmap_page_attr { | |
ac2f40be | 837 | BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */ |
5a537df4 N |
838 | BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned. |
839 | * i.e. counter is 1 or 2. */ | |
ac2f40be | 840 | BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */ |
32a7627c N |
841 | }; |
842 | ||
843 | static inline void set_page_attr(struct bitmap *bitmap, struct page *page, | |
844 | enum bitmap_page_attr attr) | |
845 | { | |
3520fa4d | 846 | __set_bit((page->index<<2) + attr, bitmap->filemap_attr); |
32a7627c N |
847 | } |
848 | ||
849 | static inline void clear_page_attr(struct bitmap *bitmap, struct page *page, | |
850 | enum bitmap_page_attr attr) | |
851 | { | |
3520fa4d | 852 | __clear_bit((page->index<<2) + attr, bitmap->filemap_attr); |
32a7627c N |
853 | } |
854 | ||
ec7a3197 N |
855 | static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page, |
856 | enum bitmap_page_attr attr) | |
32a7627c | 857 | { |
3520fa4d | 858 | return test_bit((page->index<<2) + attr, bitmap->filemap_attr); |
32a7627c N |
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; | |
3520fa4d | 871 | struct page *page; |
32a7627c N |
872 | void *kaddr; |
873 | unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap); | |
874 | ||
3520fa4d JB |
875 | if (!bitmap->filemap) |
876 | return; | |
32a7627c | 877 | |
3520fa4d JB |
878 | page = filemap_get_page(bitmap, chunk); |
879 | if (!page) | |
880 | return; | |
881 | bit = file_page_offset(bitmap, chunk); | |
32a7627c | 882 | |
3520fa4d JB |
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); | |
36a4e1fe | 890 | pr_debug("set file bit %lu page %lu\n", bit, page->index); |
32a7627c N |
891 | /* record page number so it gets flushed to disk when unplug occurs */ |
892 | set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY); | |
32a7627c N |
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 */ | |
4ad13663 | 898 | void bitmap_unplug(struct bitmap *bitmap) |
32a7627c | 899 | { |
ec7a3197 N |
900 | unsigned long i, flags; |
901 | int dirty, need_write; | |
32a7627c N |
902 | struct page *page; |
903 | int wait = 0; | |
904 | ||
905 | if (!bitmap) | |
4ad13663 | 906 | return; |
32a7627c N |
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); | |
a654b9d8 | 912 | if (!bitmap->filemap) { |
32a7627c | 913 | spin_unlock_irqrestore(&bitmap->lock, flags); |
4ad13663 | 914 | return; |
32a7627c N |
915 | } |
916 | page = bitmap->filemap[i]; | |
ec7a3197 N |
917 | dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY); |
918 | need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE); | |
32a7627c N |
919 | clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY); |
920 | clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE); | |
ec7a3197 | 921 | if (dirty) |
32a7627c N |
922 | wait = 1; |
923 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
924 | ||
ac2f40be | 925 | if (dirty || need_write) |
4ad13663 | 926 | write_page(bitmap, page, 0); |
32a7627c N |
927 | } |
928 | if (wait) { /* if any writes were performed, we need to wait on them */ | |
0b79ccf0 | 929 | if (bitmap->file) |
d785a06a N |
930 | wait_event(bitmap->write_wait, |
931 | atomic_read(&bitmap->pending_writes)==0); | |
0b79ccf0 | 932 | else |
a9701a30 | 933 | md_super_wait(bitmap->mddev); |
32a7627c | 934 | } |
d785a06a N |
935 | if (bitmap->flags & BITMAP_WRITE_ERROR) |
936 | bitmap_file_kick(bitmap); | |
32a7627c | 937 | } |
ac2f40be | 938 | EXPORT_SYMBOL(bitmap_unplug); |
32a7627c | 939 | |
6a07997f | 940 | static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed); |
32a7627c N |
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. | |
6a07997f N |
948 | * |
949 | * We ignore all bits for sectors that end earlier than 'start'. | |
950 | * This is used when reading an out-of-date bitmap... | |
32a7627c | 951 | */ |
6a07997f | 952 | static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start) |
32a7627c N |
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; | |
d785a06a | 958 | unsigned long bytes, offset; |
32a7627c N |
959 | int outofdate; |
960 | int ret = -ENOSPC; | |
ea03aff9 | 961 | void *paddr; |
32a7627c N |
962 | |
963 | chunks = bitmap->chunks; | |
964 | file = bitmap->file; | |
965 | ||
42a04b50 | 966 | BUG_ON(!file && !bitmap->mddev->bitmap_info.offset); |
32a7627c | 967 | |
32a7627c | 968 | outofdate = bitmap->flags & BITMAP_STALE; |
32a7627c N |
969 | if (outofdate) |
970 | printk(KERN_INFO "%s: bitmap file is out of date, doing full " | |
971 | "recovery\n", bmname(bitmap)); | |
972 | ||
e384e585 | 973 | bytes = DIV_ROUND_UP(bitmap->chunks, 8); |
ece5cff0 N |
974 | if (!bitmap->mddev->bitmap_info.external) |
975 | bytes += sizeof(bitmap_super_t); | |
bc7f77de | 976 | |
e384e585 | 977 | num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE); |
bc7f77de | 978 | |
ece5cff0 | 979 | if (file && i_size_read(file->f_mapping->host) < bytes) { |
32a7627c N |
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), | |
ece5cff0 | 983 | bytes); |
4ad13663 | 984 | goto err; |
32a7627c | 985 | } |
bc7f77de N |
986 | |
987 | ret = -ENOMEM; | |
988 | ||
32a7627c | 989 | bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL); |
bc7f77de | 990 | if (!bitmap->filemap) |
4ad13663 | 991 | goto err; |
32a7627c | 992 | |
e16b68b6 N |
993 | /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */ |
994 | bitmap->filemap_attr = kzalloc( | |
ac2f40be | 995 | roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)), |
e16b68b6 | 996 | GFP_KERNEL); |
bc7f77de | 997 | if (!bitmap->filemap_attr) |
4ad13663 | 998 | goto err; |
32a7627c | 999 | |
32a7627c N |
1000 | oldindex = ~0L; |
1001 | ||
1002 | for (i = 0; i < chunks; i++) { | |
bd926c63 | 1003 | int b; |
ece5cff0 N |
1004 | index = file_page_index(bitmap, i); |
1005 | bit = file_page_offset(bitmap, i); | |
32a7627c | 1006 | if (index != oldindex) { /* this is a new page, read it in */ |
d785a06a | 1007 | int count; |
32a7627c | 1008 | /* unmap the old page, we're done with it */ |
d785a06a | 1009 | if (index == num_pages-1) |
ece5cff0 | 1010 | count = bytes - index * PAGE_SIZE; |
d785a06a N |
1011 | else |
1012 | count = PAGE_SIZE; | |
ece5cff0 | 1013 | if (index == 0 && bitmap->sb_page) { |
32a7627c N |
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); | |
53845270 | 1021 | if (!file) |
5c04f551 VK |
1022 | page = read_sb_page( |
1023 | bitmap->mddev, | |
1024 | bitmap->mddev->bitmap_info.offset, | |
1025 | page, | |
1026 | index, count); | |
a654b9d8 | 1027 | } else if (file) { |
d785a06a | 1028 | page = read_page(file, index, bitmap, count); |
a654b9d8 N |
1029 | offset = 0; |
1030 | } else { | |
42a04b50 N |
1031 | page = read_sb_page(bitmap->mddev, |
1032 | bitmap->mddev->bitmap_info.offset, | |
a2ed9615 N |
1033 | NULL, |
1034 | index, count); | |
32a7627c N |
1035 | offset = 0; |
1036 | } | |
a654b9d8 N |
1037 | if (IS_ERR(page)) { /* read error */ |
1038 | ret = PTR_ERR(page); | |
4ad13663 | 1039 | goto err; |
a654b9d8 N |
1040 | } |
1041 | ||
32a7627c N |
1042 | oldindex = index; |
1043 | oldpage = page; | |
32a7627c | 1044 | |
b74fd282 N |
1045 | bitmap->filemap[bitmap->file_pages++] = page; |
1046 | bitmap->last_page_size = count; | |
1047 | ||
32a7627c N |
1048 | if (outofdate) { |
1049 | /* | |
1050 | * if bitmap is out of date, dirty the | |
ac2f40be | 1051 | * whole page and write it out |
32a7627c | 1052 | */ |
ea03aff9 N |
1053 | paddr = kmap_atomic(page, KM_USER0); |
1054 | memset(paddr + offset, 0xff, | |
6a07997f | 1055 | PAGE_SIZE - offset); |
ea03aff9 | 1056 | kunmap_atomic(paddr, KM_USER0); |
4ad13663 N |
1057 | write_page(bitmap, page, 1); |
1058 | ||
1059 | ret = -EIO; | |
b74fd282 | 1060 | if (bitmap->flags & BITMAP_WRITE_ERROR) |
4ad13663 | 1061 | goto err; |
32a7627c | 1062 | } |
32a7627c | 1063 | } |
ea03aff9 | 1064 | paddr = kmap_atomic(page, KM_USER0); |
bd926c63 | 1065 | if (bitmap->flags & BITMAP_HOSTENDIAN) |
ea03aff9 | 1066 | b = test_bit(bit, paddr); |
bd926c63 | 1067 | else |
6b33aff3 | 1068 | b = test_bit_le(bit, paddr); |
ea03aff9 | 1069 | kunmap_atomic(paddr, KM_USER0); |
bd926c63 | 1070 | if (b) { |
32a7627c | 1071 | /* if the disk bit is set, set the memory bit */ |
db305e50 N |
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); | |
32a7627c N |
1077 | bit_cnt++; |
1078 | } | |
32a7627c N |
1079 | } |
1080 | ||
ac2f40be | 1081 | /* everything went OK */ |
32a7627c N |
1082 | ret = 0; |
1083 | bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET); | |
1084 | ||
32a7627c N |
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 | ||
32a7627c | 1090 | printk(KERN_INFO "%s: bitmap initialized from disk: " |
9c81075f JB |
1091 | "read %lu/%lu pages, set %lu of %lu bits\n", |
1092 | bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, chunks); | |
4ad13663 N |
1093 | |
1094 | return 0; | |
32a7627c | 1095 | |
4ad13663 N |
1096 | err: |
1097 | printk(KERN_INFO "%s: bitmap initialisation failed: %d\n", | |
1098 | bmname(bitmap), ret); | |
32a7627c N |
1099 | return ret; |
1100 | } | |
1101 | ||
a654b9d8 N |
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 | */ | |
ec7a3197 | 1107 | int i; |
a654b9d8 | 1108 | |
7c8f4247 | 1109 | spin_lock_irq(&bitmap->lock); |
ac2f40be | 1110 | for (i = 0; i < bitmap->file_pages; i++) |
ec7a3197 N |
1111 | set_page_attr(bitmap, bitmap->filemap[i], |
1112 | BITMAP_PAGE_NEEDWRITE); | |
2585f3ef | 1113 | bitmap->allclean = 0; |
7c8f4247 | 1114 | spin_unlock_irq(&bitmap->lock); |
a654b9d8 N |
1115 | } |
1116 | ||
32a7627c N |
1117 | static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc) |
1118 | { | |
1119 | sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap); | |
1120 | unsigned long page = chunk >> PAGE_COUNTER_SHIFT; | |
1121 | bitmap->bp[page].count += inc; | |
32a7627c N |
1122 | bitmap_checkfree(bitmap, page); |
1123 | } | |
1124 | static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap, | |
57dab0bd | 1125 | sector_t offset, sector_t *blocks, |
32a7627c N |
1126 | int create); |
1127 | ||
1128 | /* | |
1129 | * bitmap daemon -- periodically wakes up to clean bits and flush pages | |
1130 | * out to disk | |
1131 | */ | |
1132 | ||
fd01b88c | 1133 | void bitmap_daemon_work(struct mddev *mddev) |
32a7627c | 1134 | { |
aa5cbd10 | 1135 | struct bitmap *bitmap; |
aa3163f8 | 1136 | unsigned long j; |
32a7627c N |
1137 | unsigned long flags; |
1138 | struct page *page = NULL, *lastpage = NULL; | |
57dab0bd | 1139 | sector_t blocks; |
ea03aff9 | 1140 | void *paddr; |
32a7627c | 1141 | |
aa5cbd10 N |
1142 | /* Use a mutex to guard daemon_work against |
1143 | * bitmap_destroy. | |
1144 | */ | |
c3d9714e | 1145 | mutex_lock(&mddev->bitmap_info.mutex); |
aa5cbd10 N |
1146 | bitmap = mddev->bitmap; |
1147 | if (bitmap == NULL) { | |
c3d9714e | 1148 | mutex_unlock(&mddev->bitmap_info.mutex); |
4ad13663 | 1149 | return; |
aa5cbd10 | 1150 | } |
42a04b50 | 1151 | if (time_before(jiffies, bitmap->daemon_lastrun |
2e61ebbc | 1152 | + mddev->bitmap_info.daemon_sleep)) |
7be3dfec N |
1153 | goto done; |
1154 | ||
32a7627c | 1155 | bitmap->daemon_lastrun = jiffies; |
8311c29d | 1156 | if (bitmap->allclean) { |
2e61ebbc | 1157 | mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT; |
aa5cbd10 | 1158 | goto done; |
8311c29d N |
1159 | } |
1160 | bitmap->allclean = 1; | |
32a7627c | 1161 | |
be512691 | 1162 | spin_lock_irqsave(&bitmap->lock, flags); |
32a7627c N |
1163 | for (j = 0; j < bitmap->chunks; j++) { |
1164 | bitmap_counter_t *bmc; | |
3520fa4d JB |
1165 | if (!bitmap->filemap) |
1166 | /* error or shutdown */ | |
1167 | break; | |
1168 | ||
1169 | page = filemap_get_page(bitmap, j); | |
32a7627c N |
1170 | |
1171 | if (page != lastpage) { | |
aa3163f8 | 1172 | /* skip this page unless it's marked as needing cleaning */ |
5a537df4 | 1173 | if (!test_page_attr(bitmap, page, BITMAP_PAGE_PENDING)) { |
ec7a3197 N |
1174 | int need_write = test_page_attr(bitmap, page, |
1175 | BITMAP_PAGE_NEEDWRITE); | |
a647e4bc | 1176 | if (need_write) |
aa3163f8 | 1177 | clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE); |
a647e4bc | 1178 | |
aa3163f8 | 1179 | spin_unlock_irqrestore(&bitmap->lock, flags); |
2585f3ef | 1180 | if (need_write) |
4ad13663 | 1181 | write_page(bitmap, page, 0); |
be512691 N |
1182 | spin_lock_irqsave(&bitmap->lock, flags); |
1183 | j |= (PAGE_BITS - 1); | |
aa3163f8 N |
1184 | continue; |
1185 | } | |
1186 | ||
32a7627c | 1187 | /* grab the new page, sync and release the old */ |
32a7627c | 1188 | if (lastpage != NULL) { |
2585f3ef N |
1189 | if (test_page_attr(bitmap, lastpage, |
1190 | BITMAP_PAGE_NEEDWRITE)) { | |
1191 | clear_page_attr(bitmap, lastpage, | |
1192 | BITMAP_PAGE_NEEDWRITE); | |
32a7627c | 1193 | spin_unlock_irqrestore(&bitmap->lock, flags); |
4ad13663 | 1194 | write_page(bitmap, lastpage, 0); |
32a7627c | 1195 | } else { |
2585f3ef N |
1196 | set_page_attr(bitmap, lastpage, |
1197 | BITMAP_PAGE_NEEDWRITE); | |
1198 | bitmap->allclean = 0; | |
32a7627c N |
1199 | spin_unlock_irqrestore(&bitmap->lock, flags); |
1200 | } | |
32a7627c N |
1201 | } else |
1202 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1203 | lastpage = page; | |
a0da84f3 NB |
1204 | |
1205 | /* We are possibly going to clear some bits, so make | |
1206 | * sure that events_cleared is up-to-date. | |
1207 | */ | |
ece5cff0 | 1208 | if (bitmap->need_sync && |
2e61ebbc | 1209 | mddev->bitmap_info.external == 0) { |
a0da84f3 NB |
1210 | bitmap_super_t *sb; |
1211 | bitmap->need_sync = 0; | |
1212 | sb = kmap_atomic(bitmap->sb_page, KM_USER0); | |
1213 | sb->events_cleared = | |
1214 | cpu_to_le64(bitmap->events_cleared); | |
1215 | kunmap_atomic(sb, KM_USER0); | |
1216 | write_page(bitmap, bitmap->sb_page, 1); | |
1217 | } | |
32a7627c | 1218 | spin_lock_irqsave(&bitmap->lock, flags); |
ece5cff0 | 1219 | if (!bitmap->need_sync) |
5a537df4 | 1220 | clear_page_attr(bitmap, page, BITMAP_PAGE_PENDING); |
2585f3ef N |
1221 | else |
1222 | bitmap->allclean = 0; | |
32a7627c | 1223 | } |
db305e50 N |
1224 | bmc = bitmap_get_counter(bitmap, |
1225 | (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap), | |
1226 | &blocks, 0); | |
5a537df4 N |
1227 | if (!bmc) |
1228 | j |= PAGE_COUNTER_MASK; | |
1229 | else if (*bmc) { | |
5a537df4 | 1230 | if (*bmc == 1 && !bitmap->need_sync) { |
32a7627c N |
1231 | /* we can clear the bit */ |
1232 | *bmc = 0; | |
db305e50 N |
1233 | bitmap_count_page(bitmap, |
1234 | (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap), | |
32a7627c N |
1235 | -1); |
1236 | ||
1237 | /* clear the bit */ | |
3520fa4d JB |
1238 | paddr = kmap_atomic(page, KM_USER0); |
1239 | if (bitmap->flags & BITMAP_HOSTENDIAN) | |
1240 | clear_bit(file_page_offset(bitmap, j), | |
1241 | paddr); | |
1242 | else | |
1243 | __clear_bit_le( | |
5a537df4 N |
1244 | file_page_offset(bitmap, |
1245 | j), | |
1246 | paddr); | |
3520fa4d | 1247 | kunmap_atomic(paddr, KM_USER0); |
5a537df4 N |
1248 | } else if (*bmc <= 2) { |
1249 | *bmc = 1; /* maybe clear the bit next time */ | |
1250 | set_page_attr(bitmap, page, BITMAP_PAGE_PENDING); | |
2585f3ef | 1251 | bitmap->allclean = 0; |
32a7627c | 1252 | } |
5a537df4 | 1253 | } |
32a7627c | 1254 | } |
be512691 | 1255 | spin_unlock_irqrestore(&bitmap->lock, flags); |
32a7627c N |
1256 | |
1257 | /* now sync the final page */ | |
3520fa4d | 1258 | if (lastpage != NULL) { |
32a7627c | 1259 | spin_lock_irqsave(&bitmap->lock, flags); |
ec7a3197 | 1260 | if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) { |
32a7627c N |
1261 | clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); |
1262 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
3520fa4d | 1263 | write_page(bitmap, lastpage, 0); |
32a7627c N |
1264 | } else { |
1265 | set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); | |
2585f3ef | 1266 | bitmap->allclean = 0; |
32a7627c N |
1267 | spin_unlock_irqrestore(&bitmap->lock, flags); |
1268 | } | |
32a7627c N |
1269 | } |
1270 | ||
7be3dfec | 1271 | done: |
8311c29d | 1272 | if (bitmap->allclean == 0) |
2e61ebbc N |
1273 | mddev->thread->timeout = |
1274 | mddev->bitmap_info.daemon_sleep; | |
c3d9714e | 1275 | mutex_unlock(&mddev->bitmap_info.mutex); |
32a7627c N |
1276 | } |
1277 | ||
32a7627c | 1278 | static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap, |
57dab0bd | 1279 | sector_t offset, sector_t *blocks, |
32a7627c | 1280 | int create) |
ee305ace N |
1281 | __releases(bitmap->lock) |
1282 | __acquires(bitmap->lock) | |
32a7627c N |
1283 | { |
1284 | /* If 'create', we might release the lock and reclaim it. | |
1285 | * The lock must have been taken with interrupts enabled. | |
1286 | * If !create, we don't release the lock. | |
1287 | */ | |
1288 | sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap); | |
1289 | unsigned long page = chunk >> PAGE_COUNTER_SHIFT; | |
1290 | unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT; | |
1291 | sector_t csize; | |
ef425673 | 1292 | int err; |
32a7627c | 1293 | |
ef425673 N |
1294 | err = bitmap_checkpage(bitmap, page, create); |
1295 | ||
1296 | if (bitmap->bp[page].hijacked || | |
1297 | bitmap->bp[page].map == NULL) | |
1298 | csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) + | |
1299 | PAGE_COUNTER_SHIFT - 1); | |
1300 | else | |
32a7627c | 1301 | csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap)); |
ef425673 N |
1302 | *blocks = csize - (offset & (csize - 1)); |
1303 | ||
1304 | if (err < 0) | |
32a7627c | 1305 | return NULL; |
ef425673 | 1306 | |
32a7627c N |
1307 | /* now locked ... */ |
1308 | ||
1309 | if (bitmap->bp[page].hijacked) { /* hijacked pointer */ | |
1310 | /* should we use the first or second counter field | |
1311 | * of the hijacked pointer? */ | |
1312 | int hi = (pageoff > PAGE_COUNTER_MASK); | |
32a7627c N |
1313 | return &((bitmap_counter_t *) |
1314 | &bitmap->bp[page].map)[hi]; | |
ef425673 | 1315 | } else /* page is allocated */ |
32a7627c N |
1316 | return (bitmap_counter_t *) |
1317 | &(bitmap->bp[page].map[pageoff]); | |
32a7627c N |
1318 | } |
1319 | ||
4b6d287f | 1320 | int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind) |
32a7627c | 1321 | { |
ac2f40be N |
1322 | if (!bitmap) |
1323 | return 0; | |
4b6d287f N |
1324 | |
1325 | if (behind) { | |
696fcd53 | 1326 | int bw; |
4b6d287f | 1327 | atomic_inc(&bitmap->behind_writes); |
696fcd53 PC |
1328 | bw = atomic_read(&bitmap->behind_writes); |
1329 | if (bw > bitmap->behind_writes_used) | |
1330 | bitmap->behind_writes_used = bw; | |
1331 | ||
36a4e1fe N |
1332 | pr_debug("inc write-behind count %d/%lu\n", |
1333 | bw, bitmap->mddev->bitmap_info.max_write_behind); | |
4b6d287f N |
1334 | } |
1335 | ||
32a7627c | 1336 | while (sectors) { |
57dab0bd | 1337 | sector_t blocks; |
32a7627c N |
1338 | bitmap_counter_t *bmc; |
1339 | ||
1340 | spin_lock_irq(&bitmap->lock); | |
1341 | bmc = bitmap_get_counter(bitmap, offset, &blocks, 1); | |
1342 | if (!bmc) { | |
1343 | spin_unlock_irq(&bitmap->lock); | |
1344 | return 0; | |
1345 | } | |
1346 | ||
27d5ea04 | 1347 | if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) { |
da6e1a32 NB |
1348 | DEFINE_WAIT(__wait); |
1349 | /* note that it is safe to do the prepare_to_wait | |
1350 | * after the test as long as we do it before dropping | |
1351 | * the spinlock. | |
1352 | */ | |
1353 | prepare_to_wait(&bitmap->overflow_wait, &__wait, | |
1354 | TASK_UNINTERRUPTIBLE); | |
1355 | spin_unlock_irq(&bitmap->lock); | |
7eaceacc | 1356 | io_schedule(); |
da6e1a32 NB |
1357 | finish_wait(&bitmap->overflow_wait, &__wait); |
1358 | continue; | |
1359 | } | |
1360 | ||
ac2f40be | 1361 | switch (*bmc) { |
32a7627c N |
1362 | case 0: |
1363 | bitmap_file_set_bit(bitmap, offset); | |
ac2f40be | 1364 | bitmap_count_page(bitmap, offset, 1); |
32a7627c N |
1365 | /* fall through */ |
1366 | case 1: | |
1367 | *bmc = 2; | |
1368 | } | |
da6e1a32 | 1369 | |
32a7627c N |
1370 | (*bmc)++; |
1371 | ||
1372 | spin_unlock_irq(&bitmap->lock); | |
1373 | ||
1374 | offset += blocks; | |
1375 | if (sectors > blocks) | |
1376 | sectors -= blocks; | |
ac2f40be N |
1377 | else |
1378 | sectors = 0; | |
32a7627c N |
1379 | } |
1380 | return 0; | |
1381 | } | |
ac2f40be | 1382 | EXPORT_SYMBOL(bitmap_startwrite); |
32a7627c N |
1383 | |
1384 | void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, | |
4b6d287f | 1385 | int success, int behind) |
32a7627c | 1386 | { |
ac2f40be N |
1387 | if (!bitmap) |
1388 | return; | |
4b6d287f | 1389 | if (behind) { |
e555190d N |
1390 | if (atomic_dec_and_test(&bitmap->behind_writes)) |
1391 | wake_up(&bitmap->behind_wait); | |
36a4e1fe N |
1392 | pr_debug("dec write-behind count %d/%lu\n", |
1393 | atomic_read(&bitmap->behind_writes), | |
1394 | bitmap->mddev->bitmap_info.max_write_behind); | |
4b6d287f N |
1395 | } |
1396 | ||
32a7627c | 1397 | while (sectors) { |
57dab0bd | 1398 | sector_t blocks; |
32a7627c N |
1399 | unsigned long flags; |
1400 | bitmap_counter_t *bmc; | |
1401 | ||
1402 | spin_lock_irqsave(&bitmap->lock, flags); | |
1403 | bmc = bitmap_get_counter(bitmap, offset, &blocks, 0); | |
1404 | if (!bmc) { | |
1405 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1406 | return; | |
1407 | } | |
1408 | ||
961902c0 | 1409 | if (success && !bitmap->mddev->degraded && |
a0da84f3 NB |
1410 | bitmap->events_cleared < bitmap->mddev->events) { |
1411 | bitmap->events_cleared = bitmap->mddev->events; | |
1412 | bitmap->need_sync = 1; | |
5ff5afff | 1413 | sysfs_notify_dirent_safe(bitmap->sysfs_can_clear); |
a0da84f3 NB |
1414 | } |
1415 | ||
27d5ea04 | 1416 | if (!success && !NEEDED(*bmc)) |
32a7627c N |
1417 | *bmc |= NEEDED_MASK; |
1418 | ||
27d5ea04 | 1419 | if (COUNTER(*bmc) == COUNTER_MAX) |
da6e1a32 NB |
1420 | wake_up(&bitmap->overflow_wait); |
1421 | ||
32a7627c | 1422 | (*bmc)--; |
2585f3ef | 1423 | if (*bmc <= 2) { |
32a7627c | 1424 | set_page_attr(bitmap, |
e384e585 N |
1425 | filemap_get_page( |
1426 | bitmap, | |
1427 | offset >> CHUNK_BLOCK_SHIFT(bitmap)), | |
5a537df4 | 1428 | BITMAP_PAGE_PENDING); |
2585f3ef N |
1429 | bitmap->allclean = 0; |
1430 | } | |
32a7627c N |
1431 | spin_unlock_irqrestore(&bitmap->lock, flags); |
1432 | offset += blocks; | |
1433 | if (sectors > blocks) | |
1434 | sectors -= blocks; | |
ac2f40be N |
1435 | else |
1436 | sectors = 0; | |
32a7627c N |
1437 | } |
1438 | } | |
ac2f40be | 1439 | EXPORT_SYMBOL(bitmap_endwrite); |
32a7627c | 1440 | |
57dab0bd | 1441 | static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, |
1187cf0a | 1442 | int degraded) |
32a7627c N |
1443 | { |
1444 | bitmap_counter_t *bmc; | |
1445 | int rv; | |
1446 | if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */ | |
1447 | *blocks = 1024; | |
1448 | return 1; /* always resync if no bitmap */ | |
1449 | } | |
1450 | spin_lock_irq(&bitmap->lock); | |
1451 | bmc = bitmap_get_counter(bitmap, offset, blocks, 0); | |
1452 | rv = 0; | |
1453 | if (bmc) { | |
1454 | /* locked */ | |
1455 | if (RESYNC(*bmc)) | |
1456 | rv = 1; | |
1457 | else if (NEEDED(*bmc)) { | |
1458 | rv = 1; | |
6a806c51 N |
1459 | if (!degraded) { /* don't set/clear bits if degraded */ |
1460 | *bmc |= RESYNC_MASK; | |
1461 | *bmc &= ~NEEDED_MASK; | |
1462 | } | |
32a7627c N |
1463 | } |
1464 | } | |
1465 | spin_unlock_irq(&bitmap->lock); | |
1466 | return rv; | |
1467 | } | |
1468 | ||
57dab0bd | 1469 | int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, |
1187cf0a N |
1470 | int degraded) |
1471 | { | |
1472 | /* bitmap_start_sync must always report on multiples of whole | |
1473 | * pages, otherwise resync (which is very PAGE_SIZE based) will | |
1474 | * get confused. | |
1475 | * So call __bitmap_start_sync repeatedly (if needed) until | |
1476 | * At least PAGE_SIZE>>9 blocks are covered. | |
1477 | * Return the 'or' of the result. | |
1478 | */ | |
1479 | int rv = 0; | |
57dab0bd | 1480 | sector_t blocks1; |
1187cf0a N |
1481 | |
1482 | *blocks = 0; | |
1483 | while (*blocks < (PAGE_SIZE>>9)) { | |
1484 | rv |= __bitmap_start_sync(bitmap, offset, | |
1485 | &blocks1, degraded); | |
1486 | offset += blocks1; | |
1487 | *blocks += blocks1; | |
1488 | } | |
1489 | return rv; | |
1490 | } | |
ac2f40be | 1491 | EXPORT_SYMBOL(bitmap_start_sync); |
1187cf0a | 1492 | |
57dab0bd | 1493 | void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted) |
32a7627c N |
1494 | { |
1495 | bitmap_counter_t *bmc; | |
1496 | unsigned long flags; | |
ac2f40be N |
1497 | |
1498 | if (bitmap == NULL) { | |
32a7627c N |
1499 | *blocks = 1024; |
1500 | return; | |
1501 | } | |
1502 | spin_lock_irqsave(&bitmap->lock, flags); | |
1503 | bmc = bitmap_get_counter(bitmap, offset, blocks, 0); | |
1504 | if (bmc == NULL) | |
1505 | goto unlock; | |
1506 | /* locked */ | |
32a7627c N |
1507 | if (RESYNC(*bmc)) { |
1508 | *bmc &= ~RESYNC_MASK; | |
1509 | ||
1510 | if (!NEEDED(*bmc) && aborted) | |
1511 | *bmc |= NEEDED_MASK; | |
1512 | else { | |
2585f3ef | 1513 | if (*bmc <= 2) { |
32a7627c N |
1514 | set_page_attr(bitmap, |
1515 | filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)), | |
5a537df4 | 1516 | BITMAP_PAGE_PENDING); |
2585f3ef N |
1517 | bitmap->allclean = 0; |
1518 | } | |
32a7627c N |
1519 | } |
1520 | } | |
1521 | unlock: | |
1522 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1523 | } | |
ac2f40be | 1524 | EXPORT_SYMBOL(bitmap_end_sync); |
32a7627c N |
1525 | |
1526 | void bitmap_close_sync(struct bitmap *bitmap) | |
1527 | { | |
1528 | /* Sync has finished, and any bitmap chunks that weren't synced | |
1529 | * properly have been aborted. It remains to us to clear the | |
1530 | * RESYNC bit wherever it is still on | |
1531 | */ | |
1532 | sector_t sector = 0; | |
57dab0bd | 1533 | sector_t blocks; |
b47490c9 N |
1534 | if (!bitmap) |
1535 | return; | |
32a7627c N |
1536 | while (sector < bitmap->mddev->resync_max_sectors) { |
1537 | bitmap_end_sync(bitmap, sector, &blocks, 0); | |
b47490c9 N |
1538 | sector += blocks; |
1539 | } | |
1540 | } | |
ac2f40be | 1541 | EXPORT_SYMBOL(bitmap_close_sync); |
b47490c9 N |
1542 | |
1543 | void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector) | |
1544 | { | |
1545 | sector_t s = 0; | |
57dab0bd | 1546 | sector_t blocks; |
b47490c9 N |
1547 | |
1548 | if (!bitmap) | |
1549 | return; | |
1550 | if (sector == 0) { | |
1551 | bitmap->last_end_sync = jiffies; | |
1552 | return; | |
1553 | } | |
1554 | if (time_before(jiffies, (bitmap->last_end_sync | |
1b04be96 | 1555 | + bitmap->mddev->bitmap_info.daemon_sleep))) |
b47490c9 N |
1556 | return; |
1557 | wait_event(bitmap->mddev->recovery_wait, | |
1558 | atomic_read(&bitmap->mddev->recovery_active) == 0); | |
1559 | ||
75d3da43 | 1560 | bitmap->mddev->curr_resync_completed = sector; |
070dc6dd | 1561 | set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags); |
b47490c9 N |
1562 | sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1); |
1563 | s = 0; | |
1564 | while (s < sector && s < bitmap->mddev->resync_max_sectors) { | |
1565 | bitmap_end_sync(bitmap, s, &blocks, 0); | |
1566 | s += blocks; | |
32a7627c | 1567 | } |
b47490c9 | 1568 | bitmap->last_end_sync = jiffies; |
acb180b0 | 1569 | sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed"); |
32a7627c | 1570 | } |
ac2f40be | 1571 | EXPORT_SYMBOL(bitmap_cond_end_sync); |
32a7627c | 1572 | |
6a07997f | 1573 | static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed) |
32a7627c N |
1574 | { |
1575 | /* For each chunk covered by any of these sectors, set the | |
193f1c93 | 1576 | * counter to 1 and set resync_needed. They should all |
32a7627c N |
1577 | * be 0 at this point |
1578 | */ | |
193f1c93 | 1579 | |
57dab0bd | 1580 | sector_t secs; |
193f1c93 N |
1581 | bitmap_counter_t *bmc; |
1582 | spin_lock_irq(&bitmap->lock); | |
1583 | bmc = bitmap_get_counter(bitmap, offset, &secs, 1); | |
1584 | if (!bmc) { | |
32a7627c | 1585 | spin_unlock_irq(&bitmap->lock); |
193f1c93 | 1586 | return; |
32a7627c | 1587 | } |
ac2f40be | 1588 | if (!*bmc) { |
193f1c93 | 1589 | struct page *page; |
915c420d | 1590 | *bmc = 2 | (needed ? NEEDED_MASK : 0); |
193f1c93 N |
1591 | bitmap_count_page(bitmap, offset, 1); |
1592 | page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)); | |
5a537df4 | 1593 | set_page_attr(bitmap, page, BITMAP_PAGE_PENDING); |
2585f3ef | 1594 | bitmap->allclean = 0; |
193f1c93 N |
1595 | } |
1596 | spin_unlock_irq(&bitmap->lock); | |
32a7627c N |
1597 | } |
1598 | ||
9b1d1dac PC |
1599 | /* dirty the memory and file bits for bitmap chunks "s" to "e" */ |
1600 | void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e) | |
1601 | { | |
1602 | unsigned long chunk; | |
1603 | ||
1604 | for (chunk = s; chunk <= e; chunk++) { | |
db305e50 | 1605 | sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap); |
9b1d1dac | 1606 | bitmap_set_memory_bits(bitmap, sec, 1); |
7c8f4247 | 1607 | spin_lock_irq(&bitmap->lock); |
9b1d1dac | 1608 | bitmap_file_set_bit(bitmap, sec); |
7c8f4247 | 1609 | spin_unlock_irq(&bitmap->lock); |
ffa23322 N |
1610 | if (sec < bitmap->mddev->recovery_cp) |
1611 | /* We are asserting that the array is dirty, | |
1612 | * so move the recovery_cp address back so | |
1613 | * that it is obvious that it is dirty | |
1614 | */ | |
1615 | bitmap->mddev->recovery_cp = sec; | |
9b1d1dac PC |
1616 | } |
1617 | } | |
1618 | ||
6b8b3e8a N |
1619 | /* |
1620 | * flush out any pending updates | |
1621 | */ | |
fd01b88c | 1622 | void bitmap_flush(struct mddev *mddev) |
6b8b3e8a N |
1623 | { |
1624 | struct bitmap *bitmap = mddev->bitmap; | |
42a04b50 | 1625 | long sleep; |
6b8b3e8a N |
1626 | |
1627 | if (!bitmap) /* there was no bitmap */ | |
1628 | return; | |
1629 | ||
1630 | /* run the daemon_work three time to ensure everything is flushed | |
1631 | * that can be | |
1632 | */ | |
1b04be96 | 1633 | sleep = mddev->bitmap_info.daemon_sleep * 2; |
42a04b50 | 1634 | bitmap->daemon_lastrun -= sleep; |
aa5cbd10 | 1635 | bitmap_daemon_work(mddev); |
42a04b50 | 1636 | bitmap->daemon_lastrun -= sleep; |
aa5cbd10 | 1637 | bitmap_daemon_work(mddev); |
42a04b50 | 1638 | bitmap->daemon_lastrun -= sleep; |
aa5cbd10 | 1639 | bitmap_daemon_work(mddev); |
6b8b3e8a N |
1640 | bitmap_update_sb(bitmap); |
1641 | } | |
1642 | ||
32a7627c N |
1643 | /* |
1644 | * free memory that was allocated | |
1645 | */ | |
3178b0db | 1646 | static void bitmap_free(struct bitmap *bitmap) |
32a7627c N |
1647 | { |
1648 | unsigned long k, pages; | |
1649 | struct bitmap_page *bp; | |
32a7627c N |
1650 | |
1651 | if (!bitmap) /* there was no bitmap */ | |
1652 | return; | |
1653 | ||
32a7627c N |
1654 | /* release the bitmap file and kill the daemon */ |
1655 | bitmap_file_put(bitmap); | |
1656 | ||
1657 | bp = bitmap->bp; | |
1658 | pages = bitmap->pages; | |
1659 | ||
1660 | /* free all allocated memory */ | |
1661 | ||
32a7627c N |
1662 | if (bp) /* deallocate the page memory */ |
1663 | for (k = 0; k < pages; k++) | |
1664 | if (bp[k].map && !bp[k].hijacked) | |
1665 | kfree(bp[k].map); | |
1666 | kfree(bp); | |
1667 | kfree(bitmap); | |
1668 | } | |
aa5cbd10 | 1669 | |
fd01b88c | 1670 | void bitmap_destroy(struct mddev *mddev) |
3178b0db N |
1671 | { |
1672 | struct bitmap *bitmap = mddev->bitmap; | |
1673 | ||
1674 | if (!bitmap) /* there was no bitmap */ | |
1675 | return; | |
1676 | ||
c3d9714e | 1677 | mutex_lock(&mddev->bitmap_info.mutex); |
3178b0db | 1678 | mddev->bitmap = NULL; /* disconnect from the md device */ |
c3d9714e | 1679 | mutex_unlock(&mddev->bitmap_info.mutex); |
b15c2e57 N |
1680 | if (mddev->thread) |
1681 | mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT; | |
3178b0db | 1682 | |
ece5cff0 N |
1683 | if (bitmap->sysfs_can_clear) |
1684 | sysfs_put(bitmap->sysfs_can_clear); | |
1685 | ||
3178b0db N |
1686 | bitmap_free(bitmap); |
1687 | } | |
32a7627c N |
1688 | |
1689 | /* | |
1690 | * initialize the bitmap structure | |
1691 | * if this returns an error, bitmap_destroy must be called to do clean up | |
1692 | */ | |
fd01b88c | 1693 | int bitmap_create(struct mddev *mddev) |
32a7627c N |
1694 | { |
1695 | struct bitmap *bitmap; | |
1f593903 | 1696 | sector_t blocks = mddev->resync_max_sectors; |
32a7627c N |
1697 | unsigned long chunks; |
1698 | unsigned long pages; | |
c3d9714e | 1699 | struct file *file = mddev->bitmap_info.file; |
32a7627c | 1700 | int err; |
5ff5afff | 1701 | struct sysfs_dirent *bm = NULL; |
32a7627c | 1702 | |
5f6e3c83 | 1703 | BUILD_BUG_ON(sizeof(bitmap_super_t) != 256); |
32a7627c | 1704 | |
e384e585 | 1705 | if (!file |
3520fa4d | 1706 | && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */ |
32a7627c N |
1707 | return 0; |
1708 | ||
c3d9714e | 1709 | BUG_ON(file && mddev->bitmap_info.offset); |
a654b9d8 | 1710 | |
9ffae0cf | 1711 | bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL); |
32a7627c N |
1712 | if (!bitmap) |
1713 | return -ENOMEM; | |
1714 | ||
32a7627c | 1715 | spin_lock_init(&bitmap->lock); |
ce25c31b N |
1716 | atomic_set(&bitmap->pending_writes, 0); |
1717 | init_waitqueue_head(&bitmap->write_wait); | |
da6e1a32 | 1718 | init_waitqueue_head(&bitmap->overflow_wait); |
e555190d | 1719 | init_waitqueue_head(&bitmap->behind_wait); |
ce25c31b | 1720 | |
32a7627c | 1721 | bitmap->mddev = mddev; |
32a7627c | 1722 | |
5ff5afff N |
1723 | if (mddev->kobj.sd) |
1724 | bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap"); | |
ece5cff0 | 1725 | if (bm) { |
3ff195b0 | 1726 | bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear"); |
ece5cff0 N |
1727 | sysfs_put(bm); |
1728 | } else | |
1729 | bitmap->sysfs_can_clear = NULL; | |
1730 | ||
32a7627c | 1731 | bitmap->file = file; |
ce25c31b N |
1732 | if (file) { |
1733 | get_file(file); | |
ae8fa283 N |
1734 | /* As future accesses to this file will use bmap, |
1735 | * and bypass the page cache, we must sync the file | |
1736 | * first. | |
1737 | */ | |
8018ab05 | 1738 | vfs_fsync(file, 1); |
ce25c31b | 1739 | } |
42a04b50 | 1740 | /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */ |
9c81075f JB |
1741 | if (!mddev->bitmap_info.external) { |
1742 | /* | |
1743 | * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is | |
1744 | * instructing us to create a new on-disk bitmap instance. | |
1745 | */ | |
1746 | if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags)) | |
1747 | err = bitmap_new_disk_sb(bitmap); | |
1748 | else | |
1749 | err = bitmap_read_sb(bitmap); | |
1750 | } else { | |
ece5cff0 N |
1751 | err = 0; |
1752 | if (mddev->bitmap_info.chunksize == 0 || | |
1753 | mddev->bitmap_info.daemon_sleep == 0) | |
1754 | /* chunksize and time_base need to be | |
1755 | * set first. */ | |
1756 | err = -EINVAL; | |
1757 | } | |
32a7627c | 1758 | if (err) |
3178b0db | 1759 | goto error; |
32a7627c | 1760 | |
624ce4f5 | 1761 | bitmap->daemon_lastrun = jiffies; |
42a04b50 | 1762 | bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize); |
32a7627c N |
1763 | |
1764 | /* now that chunksize and chunkshift are set, we can use these macros */ | |
ac2f40be | 1765 | chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >> |
1f593903 | 1766 | CHUNK_BLOCK_SHIFT(bitmap); |
ac2f40be | 1767 | pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO; |
32a7627c N |
1768 | |
1769 | BUG_ON(!pages); | |
1770 | ||
1771 | bitmap->chunks = chunks; | |
1772 | bitmap->pages = pages; | |
1773 | bitmap->missing_pages = pages; | |
32a7627c | 1774 | |
9ffae0cf | 1775 | bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL); |
29d3247e | 1776 | |
3178b0db | 1777 | err = -ENOMEM; |
32a7627c | 1778 | if (!bitmap->bp) |
3178b0db | 1779 | goto error; |
32a7627c | 1780 | |
69e51b44 N |
1781 | printk(KERN_INFO "created bitmap (%lu pages) for device %s\n", |
1782 | pages, bmname(bitmap)); | |
1783 | ||
1784 | mddev->bitmap = bitmap; | |
1785 | ||
1786 | ||
1787 | return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0; | |
1788 | ||
1789 | error: | |
1790 | bitmap_free(bitmap); | |
1791 | return err; | |
1792 | } | |
1793 | ||
fd01b88c | 1794 | int bitmap_load(struct mddev *mddev) |
69e51b44 N |
1795 | { |
1796 | int err = 0; | |
3520fa4d | 1797 | sector_t start = 0; |
69e51b44 N |
1798 | sector_t sector = 0; |
1799 | struct bitmap *bitmap = mddev->bitmap; | |
1800 | ||
1801 | if (!bitmap) | |
1802 | goto out; | |
1803 | ||
1804 | /* Clear out old bitmap info first: Either there is none, or we | |
1805 | * are resuming after someone else has possibly changed things, | |
1806 | * so we should forget old cached info. | |
1807 | * All chunks should be clean, but some might need_sync. | |
1808 | */ | |
1809 | while (sector < mddev->resync_max_sectors) { | |
57dab0bd | 1810 | sector_t blocks; |
69e51b44 N |
1811 | bitmap_start_sync(bitmap, sector, &blocks, 0); |
1812 | sector += blocks; | |
1813 | } | |
1814 | bitmap_close_sync(bitmap); | |
1815 | ||
3520fa4d JB |
1816 | if (mddev->degraded == 0 |
1817 | || bitmap->events_cleared == mddev->events) | |
1818 | /* no need to keep dirty bits to optimise a | |
1819 | * re-add of a missing device */ | |
1820 | start = mddev->recovery_cp; | |
1821 | ||
1822 | err = bitmap_init_from_disk(bitmap, start); | |
1823 | ||
32a7627c | 1824 | if (err) |
69e51b44 | 1825 | goto out; |
3178b0db | 1826 | |
1b04be96 | 1827 | mddev->thread->timeout = mddev->bitmap_info.daemon_sleep; |
9cd30fdc | 1828 | md_wakeup_thread(mddev->thread); |
b15c2e57 | 1829 | |
4ad13663 N |
1830 | bitmap_update_sb(bitmap); |
1831 | ||
69e51b44 N |
1832 | if (bitmap->flags & BITMAP_WRITE_ERROR) |
1833 | err = -EIO; | |
1834 | out: | |
3178b0db | 1835 | return err; |
32a7627c | 1836 | } |
69e51b44 | 1837 | EXPORT_SYMBOL_GPL(bitmap_load); |
32a7627c | 1838 | |
43a70507 | 1839 | static ssize_t |
fd01b88c | 1840 | location_show(struct mddev *mddev, char *page) |
43a70507 N |
1841 | { |
1842 | ssize_t len; | |
ac2f40be | 1843 | if (mddev->bitmap_info.file) |
43a70507 | 1844 | len = sprintf(page, "file"); |
ac2f40be | 1845 | else if (mddev->bitmap_info.offset) |
43a70507 | 1846 | len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset); |
ac2f40be | 1847 | else |
43a70507 N |
1848 | len = sprintf(page, "none"); |
1849 | len += sprintf(page+len, "\n"); | |
1850 | return len; | |
1851 | } | |
1852 | ||
1853 | static ssize_t | |
fd01b88c | 1854 | location_store(struct mddev *mddev, const char *buf, size_t len) |
43a70507 N |
1855 | { |
1856 | ||
1857 | if (mddev->pers) { | |
1858 | if (!mddev->pers->quiesce) | |
1859 | return -EBUSY; | |
1860 | if (mddev->recovery || mddev->sync_thread) | |
1861 | return -EBUSY; | |
1862 | } | |
1863 | ||
1864 | if (mddev->bitmap || mddev->bitmap_info.file || | |
1865 | mddev->bitmap_info.offset) { | |
1866 | /* bitmap already configured. Only option is to clear it */ | |
1867 | if (strncmp(buf, "none", 4) != 0) | |
1868 | return -EBUSY; | |
1869 | if (mddev->pers) { | |
1870 | mddev->pers->quiesce(mddev, 1); | |
1871 | bitmap_destroy(mddev); | |
1872 | mddev->pers->quiesce(mddev, 0); | |
1873 | } | |
1874 | mddev->bitmap_info.offset = 0; | |
1875 | if (mddev->bitmap_info.file) { | |
1876 | struct file *f = mddev->bitmap_info.file; | |
1877 | mddev->bitmap_info.file = NULL; | |
1878 | restore_bitmap_write_access(f); | |
1879 | fput(f); | |
1880 | } | |
1881 | } else { | |
1882 | /* No bitmap, OK to set a location */ | |
1883 | long long offset; | |
1884 | if (strncmp(buf, "none", 4) == 0) | |
1885 | /* nothing to be done */; | |
1886 | else if (strncmp(buf, "file:", 5) == 0) { | |
1887 | /* Not supported yet */ | |
1888 | return -EINVAL; | |
1889 | } else { | |
1890 | int rv; | |
1891 | if (buf[0] == '+') | |
1892 | rv = strict_strtoll(buf+1, 10, &offset); | |
1893 | else | |
1894 | rv = strict_strtoll(buf, 10, &offset); | |
1895 | if (rv) | |
1896 | return rv; | |
1897 | if (offset == 0) | |
1898 | return -EINVAL; | |
ece5cff0 N |
1899 | if (mddev->bitmap_info.external == 0 && |
1900 | mddev->major_version == 0 && | |
43a70507 N |
1901 | offset != mddev->bitmap_info.default_offset) |
1902 | return -EINVAL; | |
1903 | mddev->bitmap_info.offset = offset; | |
1904 | if (mddev->pers) { | |
1905 | mddev->pers->quiesce(mddev, 1); | |
1906 | rv = bitmap_create(mddev); | |
1907 | if (rv) { | |
1908 | bitmap_destroy(mddev); | |
1909 | mddev->bitmap_info.offset = 0; | |
1910 | } | |
1911 | mddev->pers->quiesce(mddev, 0); | |
1912 | if (rv) | |
1913 | return rv; | |
1914 | } | |
1915 | } | |
1916 | } | |
1917 | if (!mddev->external) { | |
1918 | /* Ensure new bitmap info is stored in | |
1919 | * metadata promptly. | |
1920 | */ | |
1921 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
1922 | md_wakeup_thread(mddev->thread); | |
1923 | } | |
1924 | return len; | |
1925 | } | |
1926 | ||
1927 | static struct md_sysfs_entry bitmap_location = | |
1928 | __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store); | |
1929 | ||
1930 | static ssize_t | |
fd01b88c | 1931 | timeout_show(struct mddev *mddev, char *page) |
43a70507 N |
1932 | { |
1933 | ssize_t len; | |
1934 | unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ; | |
1935 | unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ; | |
ac2f40be | 1936 | |
43a70507 N |
1937 | len = sprintf(page, "%lu", secs); |
1938 | if (jifs) | |
1939 | len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs)); | |
1940 | len += sprintf(page+len, "\n"); | |
1941 | return len; | |
1942 | } | |
1943 | ||
1944 | static ssize_t | |
fd01b88c | 1945 | timeout_store(struct mddev *mddev, const char *buf, size_t len) |
43a70507 N |
1946 | { |
1947 | /* timeout can be set at any time */ | |
1948 | unsigned long timeout; | |
1949 | int rv = strict_strtoul_scaled(buf, &timeout, 4); | |
1950 | if (rv) | |
1951 | return rv; | |
1952 | ||
1953 | /* just to make sure we don't overflow... */ | |
1954 | if (timeout >= LONG_MAX / HZ) | |
1955 | return -EINVAL; | |
1956 | ||
1957 | timeout = timeout * HZ / 10000; | |
1958 | ||
1959 | if (timeout >= MAX_SCHEDULE_TIMEOUT) | |
1960 | timeout = MAX_SCHEDULE_TIMEOUT-1; | |
1961 | if (timeout < 1) | |
1962 | timeout = 1; | |
1963 | mddev->bitmap_info.daemon_sleep = timeout; | |
1964 | if (mddev->thread) { | |
1965 | /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then | |
1966 | * the bitmap is all clean and we don't need to | |
1967 | * adjust the timeout right now | |
1968 | */ | |
1969 | if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) { | |
1970 | mddev->thread->timeout = timeout; | |
1971 | md_wakeup_thread(mddev->thread); | |
1972 | } | |
1973 | } | |
1974 | return len; | |
1975 | } | |
1976 | ||
1977 | static struct md_sysfs_entry bitmap_timeout = | |
1978 | __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store); | |
1979 | ||
1980 | static ssize_t | |
fd01b88c | 1981 | backlog_show(struct mddev *mddev, char *page) |
43a70507 N |
1982 | { |
1983 | return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind); | |
1984 | } | |
1985 | ||
1986 | static ssize_t | |
fd01b88c | 1987 | backlog_store(struct mddev *mddev, const char *buf, size_t len) |
43a70507 N |
1988 | { |
1989 | unsigned long backlog; | |
1990 | int rv = strict_strtoul(buf, 10, &backlog); | |
1991 | if (rv) | |
1992 | return rv; | |
1993 | if (backlog > COUNTER_MAX) | |
1994 | return -EINVAL; | |
1995 | mddev->bitmap_info.max_write_behind = backlog; | |
1996 | return len; | |
1997 | } | |
1998 | ||
1999 | static struct md_sysfs_entry bitmap_backlog = | |
2000 | __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store); | |
2001 | ||
2002 | static ssize_t | |
fd01b88c | 2003 | chunksize_show(struct mddev *mddev, char *page) |
43a70507 N |
2004 | { |
2005 | return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize); | |
2006 | } | |
2007 | ||
2008 | static ssize_t | |
fd01b88c | 2009 | chunksize_store(struct mddev *mddev, const char *buf, size_t len) |
43a70507 N |
2010 | { |
2011 | /* Can only be changed when no bitmap is active */ | |
2012 | int rv; | |
2013 | unsigned long csize; | |
2014 | if (mddev->bitmap) | |
2015 | return -EBUSY; | |
2016 | rv = strict_strtoul(buf, 10, &csize); | |
2017 | if (rv) | |
2018 | return rv; | |
2019 | if (csize < 512 || | |
2020 | !is_power_of_2(csize)) | |
2021 | return -EINVAL; | |
2022 | mddev->bitmap_info.chunksize = csize; | |
2023 | return len; | |
2024 | } | |
2025 | ||
2026 | static struct md_sysfs_entry bitmap_chunksize = | |
2027 | __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store); | |
2028 | ||
fd01b88c | 2029 | static ssize_t metadata_show(struct mddev *mddev, char *page) |
ece5cff0 N |
2030 | { |
2031 | return sprintf(page, "%s\n", (mddev->bitmap_info.external | |
2032 | ? "external" : "internal")); | |
2033 | } | |
2034 | ||
fd01b88c | 2035 | static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len) |
ece5cff0 N |
2036 | { |
2037 | if (mddev->bitmap || | |
2038 | mddev->bitmap_info.file || | |
2039 | mddev->bitmap_info.offset) | |
2040 | return -EBUSY; | |
2041 | if (strncmp(buf, "external", 8) == 0) | |
2042 | mddev->bitmap_info.external = 1; | |
2043 | else if (strncmp(buf, "internal", 8) == 0) | |
2044 | mddev->bitmap_info.external = 0; | |
2045 | else | |
2046 | return -EINVAL; | |
2047 | return len; | |
2048 | } | |
2049 | ||
2050 | static struct md_sysfs_entry bitmap_metadata = | |
2051 | __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store); | |
2052 | ||
fd01b88c | 2053 | static ssize_t can_clear_show(struct mddev *mddev, char *page) |
ece5cff0 N |
2054 | { |
2055 | int len; | |
2056 | if (mddev->bitmap) | |
2057 | len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ? | |
2058 | "false" : "true")); | |
2059 | else | |
2060 | len = sprintf(page, "\n"); | |
2061 | return len; | |
2062 | } | |
2063 | ||
fd01b88c | 2064 | static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len) |
ece5cff0 N |
2065 | { |
2066 | if (mddev->bitmap == NULL) | |
2067 | return -ENOENT; | |
2068 | if (strncmp(buf, "false", 5) == 0) | |
2069 | mddev->bitmap->need_sync = 1; | |
2070 | else if (strncmp(buf, "true", 4) == 0) { | |
2071 | if (mddev->degraded) | |
2072 | return -EBUSY; | |
2073 | mddev->bitmap->need_sync = 0; | |
2074 | } else | |
2075 | return -EINVAL; | |
2076 | return len; | |
2077 | } | |
2078 | ||
2079 | static struct md_sysfs_entry bitmap_can_clear = | |
2080 | __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store); | |
2081 | ||
696fcd53 | 2082 | static ssize_t |
fd01b88c | 2083 | behind_writes_used_show(struct mddev *mddev, char *page) |
696fcd53 PC |
2084 | { |
2085 | if (mddev->bitmap == NULL) | |
2086 | return sprintf(page, "0\n"); | |
2087 | return sprintf(page, "%lu\n", | |
2088 | mddev->bitmap->behind_writes_used); | |
2089 | } | |
2090 | ||
2091 | static ssize_t | |
fd01b88c | 2092 | behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len) |
696fcd53 PC |
2093 | { |
2094 | if (mddev->bitmap) | |
2095 | mddev->bitmap->behind_writes_used = 0; | |
2096 | return len; | |
2097 | } | |
2098 | ||
2099 | static struct md_sysfs_entry max_backlog_used = | |
2100 | __ATTR(max_backlog_used, S_IRUGO | S_IWUSR, | |
2101 | behind_writes_used_show, behind_writes_used_reset); | |
2102 | ||
43a70507 N |
2103 | static struct attribute *md_bitmap_attrs[] = { |
2104 | &bitmap_location.attr, | |
2105 | &bitmap_timeout.attr, | |
2106 | &bitmap_backlog.attr, | |
2107 | &bitmap_chunksize.attr, | |
ece5cff0 N |
2108 | &bitmap_metadata.attr, |
2109 | &bitmap_can_clear.attr, | |
696fcd53 | 2110 | &max_backlog_used.attr, |
43a70507 N |
2111 | NULL |
2112 | }; | |
2113 | struct attribute_group md_bitmap_group = { | |
2114 | .name = "bitmap", | |
2115 | .attrs = md_bitmap_attrs, | |
2116 | }; | |
2117 |