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