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