]>
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 | |
10 | * - added bitmap daemon (to asynchronously clear bitmap bits from disk) | |
11 | */ | |
12 | ||
13 | /* | |
14 | * Still to do: | |
15 | * | |
16 | * flush after percent set rather than just time based. (maybe both). | |
17 | * wait if count gets too high, wake when it drops to half. | |
18 | * allow bitmap to be mirrored with superblock (before or after...) | |
19 | * allow hot-add to re-instate a current device. | |
20 | * allow hot-add of bitmap after quiessing device | |
21 | */ | |
22 | ||
23 | #include <linux/module.h> | |
24 | #include <linux/version.h> | |
25 | #include <linux/errno.h> | |
26 | #include <linux/slab.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/config.h> | |
29 | #include <linux/timer.h> | |
30 | #include <linux/sched.h> | |
31 | #include <linux/list.h> | |
32 | #include <linux/file.h> | |
33 | #include <linux/mount.h> | |
34 | #include <linux/buffer_head.h> | |
35 | #include <linux/raid/md.h> | |
36 | #include <linux/raid/bitmap.h> | |
37 | ||
38 | /* debug macros */ | |
39 | ||
40 | #define DEBUG 0 | |
41 | ||
42 | #if DEBUG | |
43 | /* these are for debugging purposes only! */ | |
44 | ||
45 | /* define one and only one of these */ | |
46 | #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */ | |
47 | #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/ | |
48 | #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */ | |
49 | #define INJECT_FAULTS_4 0 /* undef */ | |
50 | #define INJECT_FAULTS_5 0 /* undef */ | |
51 | #define INJECT_FAULTS_6 0 | |
52 | ||
53 | /* if these are defined, the driver will fail! debug only */ | |
54 | #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */ | |
55 | #define INJECT_FATAL_FAULT_2 0 /* undef */ | |
56 | #define INJECT_FATAL_FAULT_3 0 /* undef */ | |
57 | #endif | |
58 | ||
59 | //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */ | |
60 | #define DPRINTK(x...) do { } while(0) | |
61 | ||
62 | #ifndef PRINTK | |
63 | # if DEBUG > 0 | |
64 | # define PRINTK(x...) printk(KERN_DEBUG x) | |
65 | # else | |
66 | # define PRINTK(x...) | |
67 | # endif | |
68 | #endif | |
69 | ||
70 | static inline char * bmname(struct bitmap *bitmap) | |
71 | { | |
72 | return bitmap->mddev ? mdname(bitmap->mddev) : "mdX"; | |
73 | } | |
74 | ||
75 | ||
76 | /* | |
77 | * test if the bitmap is active | |
78 | */ | |
79 | int bitmap_active(struct bitmap *bitmap) | |
80 | { | |
81 | unsigned long flags; | |
82 | int res = 0; | |
83 | ||
84 | if (!bitmap) | |
85 | return res; | |
86 | spin_lock_irqsave(&bitmap->lock, flags); | |
87 | res = bitmap->flags & BITMAP_ACTIVE; | |
88 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
89 | return res; | |
90 | } | |
91 | ||
92 | #define WRITE_POOL_SIZE 256 | |
93 | /* mempool for queueing pending writes on the bitmap file */ | |
94 | static void *write_pool_alloc(unsigned int gfp_flags, void *data) | |
95 | { | |
96 | return kmalloc(sizeof(struct page_list), gfp_flags); | |
97 | } | |
98 | ||
99 | static void write_pool_free(void *ptr, void *data) | |
100 | { | |
101 | kfree(ptr); | |
102 | } | |
103 | ||
104 | /* | |
105 | * just a placeholder - calls kmalloc for bitmap pages | |
106 | */ | |
107 | static unsigned char *bitmap_alloc_page(struct bitmap *bitmap) | |
108 | { | |
109 | unsigned char *page; | |
110 | ||
44456d37 | 111 | #ifdef INJECT_FAULTS_1 |
32a7627c N |
112 | page = NULL; |
113 | #else | |
114 | page = kmalloc(PAGE_SIZE, GFP_NOIO); | |
115 | #endif | |
116 | if (!page) | |
117 | printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap)); | |
118 | else | |
a654b9d8 | 119 | PRINTK("%s: bitmap_alloc_page: allocated page at %p\n", |
32a7627c N |
120 | bmname(bitmap), page); |
121 | return page; | |
122 | } | |
123 | ||
124 | /* | |
125 | * for now just a placeholder -- just calls kfree for bitmap pages | |
126 | */ | |
127 | static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page) | |
128 | { | |
129 | PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page); | |
130 | kfree(page); | |
131 | } | |
132 | ||
133 | /* | |
134 | * check a page and, if necessary, allocate it (or hijack it if the alloc fails) | |
135 | * | |
136 | * 1) check to see if this page is allocated, if it's not then try to alloc | |
137 | * 2) if the alloc fails, set the page's hijacked flag so we'll use the | |
138 | * page pointer directly as a counter | |
139 | * | |
140 | * if we find our page, we increment the page's refcount so that it stays | |
141 | * allocated while we're using it | |
142 | */ | |
143 | static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create) | |
144 | { | |
145 | unsigned char *mappage; | |
146 | ||
147 | if (page >= bitmap->pages) { | |
148 | printk(KERN_ALERT | |
149 | "%s: invalid bitmap page request: %lu (> %lu)\n", | |
150 | bmname(bitmap), page, bitmap->pages-1); | |
151 | return -EINVAL; | |
152 | } | |
153 | ||
154 | ||
155 | if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */ | |
156 | return 0; | |
157 | ||
158 | if (bitmap->bp[page].map) /* page is already allocated, just return */ | |
159 | return 0; | |
160 | ||
161 | if (!create) | |
162 | return -ENOENT; | |
163 | ||
164 | spin_unlock_irq(&bitmap->lock); | |
165 | ||
166 | /* this page has not been allocated yet */ | |
167 | ||
168 | if ((mappage = bitmap_alloc_page(bitmap)) == NULL) { | |
169 | PRINTK("%s: bitmap map page allocation failed, hijacking\n", | |
170 | bmname(bitmap)); | |
171 | /* failed - set the hijacked flag so that we can use the | |
172 | * pointer as a counter */ | |
173 | spin_lock_irq(&bitmap->lock); | |
174 | if (!bitmap->bp[page].map) | |
175 | bitmap->bp[page].hijacked = 1; | |
176 | goto out; | |
177 | } | |
178 | ||
179 | /* got a page */ | |
180 | ||
181 | spin_lock_irq(&bitmap->lock); | |
182 | ||
183 | /* recheck the page */ | |
184 | ||
185 | if (bitmap->bp[page].map || bitmap->bp[page].hijacked) { | |
186 | /* somebody beat us to getting the page */ | |
187 | bitmap_free_page(bitmap, mappage); | |
188 | return 0; | |
189 | } | |
190 | ||
191 | /* no page was in place and we have one, so install it */ | |
192 | ||
193 | memset(mappage, 0, PAGE_SIZE); | |
194 | bitmap->bp[page].map = mappage; | |
195 | bitmap->missing_pages--; | |
196 | out: | |
197 | return 0; | |
198 | } | |
199 | ||
200 | ||
201 | /* if page is completely empty, put it back on the free list, or dealloc it */ | |
202 | /* if page was hijacked, unmark the flag so it might get alloced next time */ | |
203 | /* Note: lock should be held when calling this */ | |
204 | static inline void bitmap_checkfree(struct bitmap *bitmap, unsigned long page) | |
205 | { | |
206 | char *ptr; | |
207 | ||
208 | if (bitmap->bp[page].count) /* page is still busy */ | |
209 | return; | |
210 | ||
211 | /* page is no longer in use, it can be released */ | |
212 | ||
213 | if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */ | |
214 | bitmap->bp[page].hijacked = 0; | |
215 | bitmap->bp[page].map = NULL; | |
216 | return; | |
217 | } | |
218 | ||
219 | /* normal case, free the page */ | |
220 | ||
221 | #if 0 | |
222 | /* actually ... let's not. We will probably need the page again exactly when | |
223 | * memory is tight and we are flusing to disk | |
224 | */ | |
225 | return; | |
226 | #else | |
227 | ptr = bitmap->bp[page].map; | |
228 | bitmap->bp[page].map = NULL; | |
229 | bitmap->missing_pages++; | |
230 | bitmap_free_page(bitmap, ptr); | |
231 | return; | |
232 | #endif | |
233 | } | |
234 | ||
235 | ||
236 | /* | |
237 | * bitmap file handling - read and write the bitmap file and its superblock | |
238 | */ | |
239 | ||
240 | /* copy the pathname of a file to a buffer */ | |
241 | char *file_path(struct file *file, char *buf, int count) | |
242 | { | |
243 | struct dentry *d; | |
244 | struct vfsmount *v; | |
245 | ||
246 | if (!buf) | |
247 | return NULL; | |
248 | ||
249 | d = file->f_dentry; | |
250 | v = file->f_vfsmnt; | |
251 | ||
252 | buf = d_path(d, v, buf, count); | |
253 | ||
254 | return IS_ERR(buf) ? NULL : buf; | |
255 | } | |
256 | ||
257 | /* | |
258 | * basic page I/O operations | |
259 | */ | |
260 | ||
a654b9d8 N |
261 | /* IO operations when bitmap is stored near all superblocks */ |
262 | static struct page *read_sb_page(mddev_t *mddev, long offset, unsigned long index) | |
263 | { | |
264 | /* choose a good rdev and read the page from there */ | |
265 | ||
266 | mdk_rdev_t *rdev; | |
267 | struct list_head *tmp; | |
268 | struct page *page = alloc_page(GFP_KERNEL); | |
269 | sector_t target; | |
270 | ||
271 | if (!page) | |
272 | return ERR_PTR(-ENOMEM); | |
273 | do { | |
274 | ITERATE_RDEV(mddev, rdev, tmp) | |
275 | if (rdev->in_sync && !rdev->faulty) | |
276 | goto found; | |
277 | return ERR_PTR(-EIO); | |
278 | ||
279 | found: | |
280 | target = (rdev->sb_offset << 1) + offset + index * (PAGE_SIZE/512); | |
281 | ||
282 | } while (!sync_page_io(rdev->bdev, target, PAGE_SIZE, page, READ)); | |
283 | ||
284 | page->index = index; | |
285 | return page; | |
286 | } | |
287 | ||
288 | static int write_sb_page(mddev_t *mddev, long offset, struct page *page, int wait) | |
289 | { | |
290 | mdk_rdev_t *rdev; | |
291 | struct list_head *tmp; | |
292 | ||
293 | ITERATE_RDEV(mddev, rdev, tmp) | |
294 | if (rdev->in_sync && !rdev->faulty) | |
295 | md_super_write(mddev, rdev, | |
296 | (rdev->sb_offset<<1) + offset | |
297 | + page->index * (PAGE_SIZE/512), | |
298 | PAGE_SIZE, | |
299 | page); | |
300 | ||
301 | if (wait) | |
302 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); | |
303 | return 0; | |
304 | } | |
305 | ||
32a7627c | 306 | /* |
a654b9d8 | 307 | * write out a page to a file |
32a7627c | 308 | */ |
77ad4bc7 | 309 | static int write_page(struct bitmap *bitmap, struct page *page, int wait) |
32a7627c N |
310 | { |
311 | int ret = -ENOMEM; | |
312 | ||
a654b9d8 N |
313 | if (bitmap->file == NULL) |
314 | return write_sb_page(bitmap->mddev, bitmap->offset, page, wait); | |
315 | ||
8a5e9cf1 N |
316 | if (wait) |
317 | lock_page(page); | |
318 | else { | |
319 | if (TestSetPageLocked(page)) | |
320 | return -EAGAIN; /* already locked */ | |
321 | if (PageWriteback(page)) { | |
322 | unlock_page(page); | |
323 | return -EAGAIN; | |
324 | } | |
325 | } | |
32a7627c | 326 | |
32a7627c N |
327 | ret = page->mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE); |
328 | if (!ret) | |
329 | ret = page->mapping->a_ops->commit_write(NULL, page, 0, | |
330 | PAGE_SIZE); | |
331 | if (ret) { | |
32a7627c N |
332 | unlock_page(page); |
333 | return ret; | |
334 | } | |
335 | ||
336 | set_page_dirty(page); /* force it to be written out */ | |
77ad4bc7 N |
337 | |
338 | if (!wait) { | |
339 | /* add to list to be waited for by daemon */ | |
340 | struct page_list *item = mempool_alloc(bitmap->write_pool, GFP_NOIO); | |
341 | item->page = page; | |
342 | page_cache_get(page); | |
343 | spin_lock(&bitmap->write_lock); | |
344 | list_add(&item->list, &bitmap->complete_pages); | |
345 | spin_unlock(&bitmap->write_lock); | |
346 | md_wakeup_thread(bitmap->writeback_daemon); | |
347 | } | |
32a7627c N |
348 | return write_one_page(page, wait); |
349 | } | |
350 | ||
351 | /* read a page from a file, pinning it into cache, and return bytes_read */ | |
352 | static struct page *read_page(struct file *file, unsigned long index, | |
353 | unsigned long *bytes_read) | |
354 | { | |
355 | struct inode *inode = file->f_mapping->host; | |
356 | struct page *page = NULL; | |
357 | loff_t isize = i_size_read(inode); | |
358 | unsigned long end_index = isize >> PAGE_CACHE_SHIFT; | |
359 | ||
360 | PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_CACHE_SIZE, | |
361 | (unsigned long long)index << PAGE_CACHE_SHIFT); | |
362 | ||
363 | page = read_cache_page(inode->i_mapping, index, | |
364 | (filler_t *)inode->i_mapping->a_ops->readpage, file); | |
365 | if (IS_ERR(page)) | |
366 | goto out; | |
367 | wait_on_page_locked(page); | |
368 | if (!PageUptodate(page) || PageError(page)) { | |
369 | page_cache_release(page); | |
370 | page = ERR_PTR(-EIO); | |
371 | goto out; | |
372 | } | |
373 | ||
374 | if (index > end_index) /* we have read beyond EOF */ | |
375 | *bytes_read = 0; | |
376 | else if (index == end_index) /* possible short read */ | |
377 | *bytes_read = isize & ~PAGE_CACHE_MASK; | |
378 | else | |
379 | *bytes_read = PAGE_CACHE_SIZE; /* got a full page */ | |
380 | out: | |
381 | if (IS_ERR(page)) | |
382 | printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n", | |
383 | (int)PAGE_CACHE_SIZE, | |
384 | (unsigned long long)index << PAGE_CACHE_SHIFT, | |
385 | PTR_ERR(page)); | |
386 | return page; | |
387 | } | |
388 | ||
389 | /* | |
390 | * bitmap file superblock operations | |
391 | */ | |
392 | ||
393 | /* update the event counter and sync the superblock to disk */ | |
394 | int bitmap_update_sb(struct bitmap *bitmap) | |
395 | { | |
396 | bitmap_super_t *sb; | |
397 | unsigned long flags; | |
398 | ||
399 | if (!bitmap || !bitmap->mddev) /* no bitmap for this array */ | |
400 | return 0; | |
401 | spin_lock_irqsave(&bitmap->lock, flags); | |
402 | if (!bitmap->sb_page) { /* no superblock */ | |
403 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
404 | return 0; | |
405 | } | |
32a7627c N |
406 | spin_unlock_irqrestore(&bitmap->lock, flags); |
407 | sb = (bitmap_super_t *)kmap(bitmap->sb_page); | |
408 | sb->events = cpu_to_le64(bitmap->mddev->events); | |
409 | if (!bitmap->mddev->degraded) | |
410 | sb->events_cleared = cpu_to_le64(bitmap->mddev->events); | |
411 | kunmap(bitmap->sb_page); | |
8a5e9cf1 | 412 | return write_page(bitmap, bitmap->sb_page, 1); |
32a7627c N |
413 | } |
414 | ||
415 | /* print out the bitmap file superblock */ | |
416 | void bitmap_print_sb(struct bitmap *bitmap) | |
417 | { | |
418 | bitmap_super_t *sb; | |
419 | ||
420 | if (!bitmap || !bitmap->sb_page) | |
421 | return; | |
422 | sb = (bitmap_super_t *)kmap(bitmap->sb_page); | |
423 | printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap)); | |
a2cff26a N |
424 | printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic)); |
425 | printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version)); | |
426 | printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n", | |
32a7627c N |
427 | *(__u32 *)(sb->uuid+0), |
428 | *(__u32 *)(sb->uuid+4), | |
429 | *(__u32 *)(sb->uuid+8), | |
430 | *(__u32 *)(sb->uuid+12)); | |
a2cff26a | 431 | printk(KERN_DEBUG " events: %llu\n", |
32a7627c | 432 | (unsigned long long) le64_to_cpu(sb->events)); |
a2cff26a | 433 | printk(KERN_DEBUG "events cleared: %llu\n", |
32a7627c | 434 | (unsigned long long) le64_to_cpu(sb->events_cleared)); |
a2cff26a N |
435 | printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state)); |
436 | printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize)); | |
437 | printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep)); | |
438 | printk(KERN_DEBUG " sync size: %llu KB\n", | |
439 | (unsigned long long)le64_to_cpu(sb->sync_size)/2); | |
32a7627c N |
440 | kunmap(bitmap->sb_page); |
441 | } | |
442 | ||
443 | /* read the superblock from the bitmap file and initialize some bitmap fields */ | |
444 | static int bitmap_read_sb(struct bitmap *bitmap) | |
445 | { | |
446 | char *reason = NULL; | |
447 | bitmap_super_t *sb; | |
448 | unsigned long chunksize, daemon_sleep; | |
449 | unsigned long bytes_read; | |
450 | unsigned long long events; | |
451 | int err = -EINVAL; | |
452 | ||
453 | /* page 0 is the superblock, read it... */ | |
a654b9d8 N |
454 | if (bitmap->file) |
455 | bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read); | |
456 | else { | |
457 | bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0); | |
458 | bytes_read = PAGE_SIZE; | |
459 | } | |
32a7627c N |
460 | if (IS_ERR(bitmap->sb_page)) { |
461 | err = PTR_ERR(bitmap->sb_page); | |
462 | bitmap->sb_page = NULL; | |
463 | return err; | |
464 | } | |
465 | ||
466 | sb = (bitmap_super_t *)kmap(bitmap->sb_page); | |
467 | ||
468 | if (bytes_read < sizeof(*sb)) { /* short read */ | |
469 | printk(KERN_INFO "%s: bitmap file superblock truncated\n", | |
470 | bmname(bitmap)); | |
471 | err = -ENOSPC; | |
472 | goto out; | |
473 | } | |
474 | ||
475 | chunksize = le32_to_cpu(sb->chunksize); | |
476 | daemon_sleep = le32_to_cpu(sb->daemon_sleep); | |
477 | ||
478 | /* verify that the bitmap-specific fields are valid */ | |
479 | if (sb->magic != cpu_to_le32(BITMAP_MAGIC)) | |
480 | reason = "bad magic"; | |
481 | else if (sb->version != cpu_to_le32(BITMAP_MAJOR)) | |
482 | reason = "unrecognized superblock version"; | |
483 | else if (chunksize < 512 || chunksize > (1024 * 1024 * 4)) | |
484 | reason = "bitmap chunksize out of range (512B - 4MB)"; | |
485 | else if ((1 << ffz(~chunksize)) != chunksize) | |
486 | reason = "bitmap chunksize not a power of 2"; | |
487 | else if (daemon_sleep < 1 || daemon_sleep > 15) | |
488 | reason = "daemon sleep period out of range"; | |
489 | if (reason) { | |
490 | printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n", | |
491 | bmname(bitmap), reason); | |
492 | goto out; | |
493 | } | |
494 | ||
495 | /* keep the array size field of the bitmap superblock up to date */ | |
496 | sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors); | |
497 | ||
498 | if (!bitmap->mddev->persistent) | |
499 | goto success; | |
500 | ||
501 | /* | |
502 | * if we have a persistent array superblock, compare the | |
503 | * bitmap's UUID and event counter to the mddev's | |
504 | */ | |
505 | if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) { | |
506 | printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n", | |
507 | bmname(bitmap)); | |
508 | goto out; | |
509 | } | |
510 | events = le64_to_cpu(sb->events); | |
511 | if (events < bitmap->mddev->events) { | |
512 | printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) " | |
513 | "-- forcing full recovery\n", bmname(bitmap), events, | |
514 | (unsigned long long) bitmap->mddev->events); | |
515 | sb->state |= BITMAP_STALE; | |
516 | } | |
517 | success: | |
518 | /* assign fields using values from superblock */ | |
519 | bitmap->chunksize = chunksize; | |
520 | bitmap->daemon_sleep = daemon_sleep; | |
521 | bitmap->flags |= sb->state; | |
522 | bitmap->events_cleared = le64_to_cpu(sb->events_cleared); | |
523 | err = 0; | |
524 | out: | |
525 | kunmap(bitmap->sb_page); | |
526 | if (err) | |
527 | bitmap_print_sb(bitmap); | |
528 | return err; | |
529 | } | |
530 | ||
531 | enum bitmap_mask_op { | |
532 | MASK_SET, | |
533 | MASK_UNSET | |
534 | }; | |
535 | ||
536 | /* record the state of the bitmap in the superblock */ | |
537 | static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits, | |
538 | enum bitmap_mask_op op) | |
539 | { | |
540 | bitmap_super_t *sb; | |
541 | unsigned long flags; | |
542 | ||
543 | spin_lock_irqsave(&bitmap->lock, flags); | |
544 | if (!bitmap || !bitmap->sb_page) { /* can't set the state */ | |
545 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
546 | return; | |
547 | } | |
548 | page_cache_get(bitmap->sb_page); | |
549 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
550 | sb = (bitmap_super_t *)kmap(bitmap->sb_page); | |
551 | switch (op) { | |
552 | case MASK_SET: sb->state |= bits; | |
553 | break; | |
554 | case MASK_UNSET: sb->state &= ~bits; | |
555 | break; | |
556 | default: BUG(); | |
557 | } | |
558 | kunmap(bitmap->sb_page); | |
559 | page_cache_release(bitmap->sb_page); | |
560 | } | |
561 | ||
562 | /* | |
563 | * general bitmap file operations | |
564 | */ | |
565 | ||
566 | /* calculate the index of the page that contains this bit */ | |
567 | static inline unsigned long file_page_index(unsigned long chunk) | |
568 | { | |
569 | return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT; | |
570 | } | |
571 | ||
572 | /* calculate the (bit) offset of this bit within a page */ | |
573 | static inline unsigned long file_page_offset(unsigned long chunk) | |
574 | { | |
575 | return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1); | |
576 | } | |
577 | ||
578 | /* | |
579 | * return a pointer to the page in the filemap that contains the given bit | |
580 | * | |
581 | * this lookup is complicated by the fact that the bitmap sb might be exactly | |
582 | * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page | |
583 | * 0 or page 1 | |
584 | */ | |
585 | static inline struct page *filemap_get_page(struct bitmap *bitmap, | |
586 | unsigned long chunk) | |
587 | { | |
588 | return bitmap->filemap[file_page_index(chunk) - file_page_index(0)]; | |
589 | } | |
590 | ||
591 | ||
592 | static void bitmap_file_unmap(struct bitmap *bitmap) | |
593 | { | |
594 | struct page **map, *sb_page; | |
595 | unsigned long *attr; | |
596 | int pages; | |
597 | unsigned long flags; | |
598 | ||
599 | spin_lock_irqsave(&bitmap->lock, flags); | |
600 | map = bitmap->filemap; | |
601 | bitmap->filemap = NULL; | |
602 | attr = bitmap->filemap_attr; | |
603 | bitmap->filemap_attr = NULL; | |
604 | pages = bitmap->file_pages; | |
605 | bitmap->file_pages = 0; | |
606 | sb_page = bitmap->sb_page; | |
607 | bitmap->sb_page = NULL; | |
608 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
609 | ||
610 | while (pages--) | |
611 | if (map[pages]->index != 0) /* 0 is sb_page, release it below */ | |
612 | page_cache_release(map[pages]); | |
613 | kfree(map); | |
614 | kfree(attr); | |
615 | ||
616 | if (sb_page) | |
617 | page_cache_release(sb_page); | |
618 | } | |
619 | ||
620 | static void bitmap_stop_daemons(struct bitmap *bitmap); | |
621 | ||
622 | /* dequeue the next item in a page list -- don't call from irq context */ | |
77ad4bc7 | 623 | static struct page_list *dequeue_page(struct bitmap *bitmap) |
32a7627c N |
624 | { |
625 | struct page_list *item = NULL; | |
77ad4bc7 | 626 | struct list_head *head = &bitmap->complete_pages; |
32a7627c N |
627 | |
628 | spin_lock(&bitmap->write_lock); | |
629 | if (list_empty(head)) | |
630 | goto out; | |
631 | item = list_entry(head->prev, struct page_list, list); | |
632 | list_del(head->prev); | |
633 | out: | |
634 | spin_unlock(&bitmap->write_lock); | |
635 | return item; | |
636 | } | |
637 | ||
638 | static void drain_write_queues(struct bitmap *bitmap) | |
639 | { | |
32a7627c | 640 | struct page_list *item; |
32a7627c | 641 | |
77ad4bc7 N |
642 | while ((item = dequeue_page(bitmap))) { |
643 | /* don't bother to wait */ | |
644 | page_cache_release(item->page); | |
645 | mempool_free(item, bitmap->write_pool); | |
32a7627c N |
646 | } |
647 | ||
32a7627c | 648 | wake_up(&bitmap->write_wait); |
32a7627c N |
649 | } |
650 | ||
651 | static void bitmap_file_put(struct bitmap *bitmap) | |
652 | { | |
653 | struct file *file; | |
654 | struct inode *inode; | |
655 | unsigned long flags; | |
656 | ||
657 | spin_lock_irqsave(&bitmap->lock, flags); | |
658 | file = bitmap->file; | |
659 | bitmap->file = NULL; | |
660 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
661 | ||
662 | bitmap_stop_daemons(bitmap); | |
663 | ||
664 | drain_write_queues(bitmap); | |
665 | ||
666 | bitmap_file_unmap(bitmap); | |
667 | ||
668 | if (file) { | |
669 | inode = file->f_mapping->host; | |
670 | spin_lock(&inode->i_lock); | |
671 | atomic_set(&inode->i_writecount, 1); /* allow writes again */ | |
672 | spin_unlock(&inode->i_lock); | |
673 | fput(file); | |
674 | } | |
675 | } | |
676 | ||
677 | ||
678 | /* | |
679 | * bitmap_file_kick - if an error occurs while manipulating the bitmap file | |
680 | * then it is no longer reliable, so we stop using it and we mark the file | |
681 | * as failed in the superblock | |
682 | */ | |
683 | static void bitmap_file_kick(struct bitmap *bitmap) | |
684 | { | |
685 | char *path, *ptr = NULL; | |
686 | ||
687 | bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET); | |
688 | bitmap_update_sb(bitmap); | |
689 | ||
a654b9d8 N |
690 | if (bitmap->file) { |
691 | path = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
692 | if (path) | |
693 | ptr = file_path(bitmap->file, path, PAGE_SIZE); | |
32a7627c | 694 | |
a654b9d8 N |
695 | printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n", |
696 | bmname(bitmap), ptr ? ptr : ""); | |
32a7627c | 697 | |
a654b9d8 N |
698 | kfree(path); |
699 | } | |
32a7627c N |
700 | |
701 | bitmap_file_put(bitmap); | |
702 | ||
703 | return; | |
704 | } | |
705 | ||
706 | enum bitmap_page_attr { | |
707 | BITMAP_PAGE_DIRTY = 1, // there are set bits that need to be synced | |
708 | BITMAP_PAGE_CLEAN = 2, // there are bits that might need to be cleared | |
709 | BITMAP_PAGE_NEEDWRITE=4, // there are cleared bits that need to be synced | |
710 | }; | |
711 | ||
712 | static inline void set_page_attr(struct bitmap *bitmap, struct page *page, | |
713 | enum bitmap_page_attr attr) | |
714 | { | |
715 | bitmap->filemap_attr[page->index] |= attr; | |
716 | } | |
717 | ||
718 | static inline void clear_page_attr(struct bitmap *bitmap, struct page *page, | |
719 | enum bitmap_page_attr attr) | |
720 | { | |
721 | bitmap->filemap_attr[page->index] &= ~attr; | |
722 | } | |
723 | ||
724 | static inline unsigned long get_page_attr(struct bitmap *bitmap, struct page *page) | |
725 | { | |
726 | return bitmap->filemap_attr[page->index]; | |
727 | } | |
728 | ||
729 | /* | |
730 | * bitmap_file_set_bit -- called before performing a write to the md device | |
731 | * to set (and eventually sync) a particular bit in the bitmap file | |
732 | * | |
733 | * we set the bit immediately, then we record the page number so that | |
734 | * when an unplug occurs, we can flush the dirty pages out to disk | |
735 | */ | |
736 | static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block) | |
737 | { | |
738 | unsigned long bit; | |
739 | struct page *page; | |
740 | void *kaddr; | |
741 | unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap); | |
742 | ||
a654b9d8 | 743 | if (!bitmap->filemap) { |
32a7627c N |
744 | return; |
745 | } | |
746 | ||
747 | page = filemap_get_page(bitmap, chunk); | |
748 | bit = file_page_offset(chunk); | |
749 | ||
750 | ||
751 | /* make sure the page stays cached until it gets written out */ | |
752 | if (! (get_page_attr(bitmap, page) & BITMAP_PAGE_DIRTY)) | |
753 | page_cache_get(page); | |
754 | ||
755 | /* set the bit */ | |
756 | kaddr = kmap_atomic(page, KM_USER0); | |
757 | set_bit(bit, kaddr); | |
758 | kunmap_atomic(kaddr, KM_USER0); | |
759 | PRINTK("set file bit %lu page %lu\n", bit, page->index); | |
760 | ||
761 | /* record page number so it gets flushed to disk when unplug occurs */ | |
762 | set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY); | |
763 | ||
764 | } | |
765 | ||
766 | /* this gets called when the md device is ready to unplug its underlying | |
767 | * (slave) device queues -- before we let any writes go down, we need to | |
768 | * sync the dirty pages of the bitmap file to disk */ | |
769 | int bitmap_unplug(struct bitmap *bitmap) | |
770 | { | |
771 | unsigned long i, attr, flags; | |
772 | struct page *page; | |
773 | int wait = 0; | |
8a5e9cf1 | 774 | int err; |
32a7627c N |
775 | |
776 | if (!bitmap) | |
777 | return 0; | |
778 | ||
779 | /* look at each page to see if there are any set bits that need to be | |
780 | * flushed out to disk */ | |
781 | for (i = 0; i < bitmap->file_pages; i++) { | |
782 | spin_lock_irqsave(&bitmap->lock, flags); | |
a654b9d8 | 783 | if (!bitmap->filemap) { |
32a7627c N |
784 | spin_unlock_irqrestore(&bitmap->lock, flags); |
785 | return 0; | |
786 | } | |
787 | page = bitmap->filemap[i]; | |
788 | attr = get_page_attr(bitmap, page); | |
789 | clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY); | |
790 | clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE); | |
791 | if ((attr & BITMAP_PAGE_DIRTY)) | |
792 | wait = 1; | |
793 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
794 | ||
8a5e9cf1 N |
795 | if (attr & (BITMAP_PAGE_DIRTY | BITMAP_PAGE_NEEDWRITE)) { |
796 | err = write_page(bitmap, page, 0); | |
797 | if (err == -EAGAIN) { | |
798 | if (attr & BITMAP_PAGE_DIRTY) | |
799 | err = write_page(bitmap, page, 1); | |
800 | else | |
801 | err = 0; | |
802 | } | |
803 | if (err) | |
bfb39fba | 804 | return 1; |
8a5e9cf1 | 805 | } |
32a7627c N |
806 | } |
807 | if (wait) { /* if any writes were performed, we need to wait on them */ | |
a654b9d8 N |
808 | if (bitmap->file) { |
809 | spin_lock_irq(&bitmap->write_lock); | |
810 | wait_event_lock_irq(bitmap->write_wait, | |
811 | list_empty(&bitmap->complete_pages), bitmap->write_lock, | |
812 | wake_up_process(bitmap->writeback_daemon->tsk)); | |
813 | spin_unlock_irq(&bitmap->write_lock); | |
814 | } else | |
815 | wait_event(bitmap->mddev->sb_wait, | |
816 | atomic_read(&bitmap->mddev->pending_writes)==0); | |
32a7627c N |
817 | } |
818 | return 0; | |
819 | } | |
820 | ||
193f1c93 | 821 | static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset); |
32a7627c N |
822 | /* * bitmap_init_from_disk -- called at bitmap_create time to initialize |
823 | * the in-memory bitmap from the on-disk bitmap -- also, sets up the | |
824 | * memory mapping of the bitmap file | |
825 | * Special cases: | |
826 | * if there's no bitmap file, or if the bitmap file had been | |
827 | * previously kicked from the array, we mark all the bits as | |
828 | * 1's in order to cause a full resync. | |
829 | */ | |
193f1c93 | 830 | static int bitmap_init_from_disk(struct bitmap *bitmap) |
32a7627c N |
831 | { |
832 | unsigned long i, chunks, index, oldindex, bit; | |
833 | struct page *page = NULL, *oldpage = NULL; | |
834 | unsigned long num_pages, bit_cnt = 0; | |
835 | struct file *file; | |
836 | unsigned long bytes, offset, dummy; | |
837 | int outofdate; | |
838 | int ret = -ENOSPC; | |
839 | ||
840 | chunks = bitmap->chunks; | |
841 | file = bitmap->file; | |
842 | ||
a654b9d8 | 843 | BUG_ON(!file && !bitmap->offset); |
32a7627c | 844 | |
44456d37 | 845 | #ifdef INJECT_FAULTS_3 |
32a7627c N |
846 | outofdate = 1; |
847 | #else | |
848 | outofdate = bitmap->flags & BITMAP_STALE; | |
849 | #endif | |
850 | if (outofdate) | |
851 | printk(KERN_INFO "%s: bitmap file is out of date, doing full " | |
852 | "recovery\n", bmname(bitmap)); | |
853 | ||
854 | bytes = (chunks + 7) / 8; | |
bc7f77de | 855 | |
cdbb4cc2 | 856 | num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE; |
bc7f77de | 857 | |
a654b9d8 | 858 | if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) { |
32a7627c N |
859 | printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n", |
860 | bmname(bitmap), | |
861 | (unsigned long) i_size_read(file->f_mapping->host), | |
862 | bytes + sizeof(bitmap_super_t)); | |
863 | goto out; | |
864 | } | |
bc7f77de N |
865 | |
866 | ret = -ENOMEM; | |
867 | ||
32a7627c | 868 | bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL); |
bc7f77de | 869 | if (!bitmap->filemap) |
32a7627c | 870 | goto out; |
32a7627c N |
871 | |
872 | bitmap->filemap_attr = kmalloc(sizeof(long) * num_pages, GFP_KERNEL); | |
bc7f77de | 873 | if (!bitmap->filemap_attr) |
32a7627c | 874 | goto out; |
32a7627c N |
875 | |
876 | memset(bitmap->filemap_attr, 0, sizeof(long) * num_pages); | |
877 | ||
878 | oldindex = ~0L; | |
879 | ||
880 | for (i = 0; i < chunks; i++) { | |
881 | index = file_page_index(i); | |
882 | bit = file_page_offset(i); | |
883 | if (index != oldindex) { /* this is a new page, read it in */ | |
884 | /* unmap the old page, we're done with it */ | |
885 | if (oldpage != NULL) | |
886 | kunmap(oldpage); | |
887 | if (index == 0) { | |
888 | /* | |
889 | * if we're here then the superblock page | |
890 | * contains some bits (PAGE_SIZE != sizeof sb) | |
891 | * we've already read it in, so just use it | |
892 | */ | |
893 | page = bitmap->sb_page; | |
894 | offset = sizeof(bitmap_super_t); | |
a654b9d8 | 895 | } else if (file) { |
32a7627c | 896 | page = read_page(file, index, &dummy); |
a654b9d8 N |
897 | offset = 0; |
898 | } else { | |
899 | page = read_sb_page(bitmap->mddev, bitmap->offset, index); | |
32a7627c N |
900 | offset = 0; |
901 | } | |
a654b9d8 N |
902 | if (IS_ERR(page)) { /* read error */ |
903 | ret = PTR_ERR(page); | |
904 | goto out; | |
905 | } | |
906 | ||
32a7627c N |
907 | oldindex = index; |
908 | oldpage = page; | |
909 | kmap(page); | |
910 | ||
911 | if (outofdate) { | |
912 | /* | |
913 | * if bitmap is out of date, dirty the | |
914 | * whole page and write it out | |
915 | */ | |
916 | memset(page_address(page) + offset, 0xff, | |
917 | PAGE_SIZE - offset); | |
77ad4bc7 | 918 | ret = write_page(bitmap, page, 1); |
32a7627c N |
919 | if (ret) { |
920 | kunmap(page); | |
921 | /* release, page not in filemap yet */ | |
922 | page_cache_release(page); | |
923 | goto out; | |
924 | } | |
925 | } | |
926 | ||
927 | bitmap->filemap[bitmap->file_pages++] = page; | |
928 | } | |
929 | if (test_bit(bit, page_address(page))) { | |
930 | /* if the disk bit is set, set the memory bit */ | |
193f1c93 | 931 | bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap)); |
32a7627c N |
932 | bit_cnt++; |
933 | } | |
32a7627c N |
934 | } |
935 | ||
936 | /* everything went OK */ | |
937 | ret = 0; | |
938 | bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET); | |
939 | ||
940 | if (page) /* unmap the last page */ | |
941 | kunmap(page); | |
942 | ||
943 | if (bit_cnt) { /* Kick recovery if any bits were set */ | |
944 | set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery); | |
945 | md_wakeup_thread(bitmap->mddev->thread); | |
946 | } | |
947 | ||
948 | out: | |
949 | printk(KERN_INFO "%s: bitmap initialized from disk: " | |
950 | "read %lu/%lu pages, set %lu bits, status: %d\n", | |
951 | bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret); | |
952 | ||
953 | return ret; | |
954 | } | |
955 | ||
a654b9d8 N |
956 | void bitmap_write_all(struct bitmap *bitmap) |
957 | { | |
958 | /* We don't actually write all bitmap blocks here, | |
959 | * just flag them as needing to be written | |
960 | */ | |
961 | ||
962 | unsigned long chunks = bitmap->chunks; | |
963 | unsigned long bytes = (chunks+7)/8 + sizeof(bitmap_super_t); | |
964 | unsigned long num_pages = (bytes + PAGE_SIZE-1) / PAGE_SIZE; | |
965 | while (num_pages--) | |
966 | bitmap->filemap_attr[num_pages] |= BITMAP_PAGE_NEEDWRITE; | |
967 | } | |
968 | ||
32a7627c N |
969 | |
970 | static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc) | |
971 | { | |
972 | sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap); | |
973 | unsigned long page = chunk >> PAGE_COUNTER_SHIFT; | |
974 | bitmap->bp[page].count += inc; | |
975 | /* | |
976 | if (page == 0) printk("count page 0, offset %llu: %d gives %d\n", | |
977 | (unsigned long long)offset, inc, bitmap->bp[page].count); | |
978 | */ | |
979 | bitmap_checkfree(bitmap, page); | |
980 | } | |
981 | static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap, | |
982 | sector_t offset, int *blocks, | |
983 | int create); | |
984 | ||
985 | /* | |
986 | * bitmap daemon -- periodically wakes up to clean bits and flush pages | |
987 | * out to disk | |
988 | */ | |
989 | ||
990 | int bitmap_daemon_work(struct bitmap *bitmap) | |
991 | { | |
aa3163f8 | 992 | unsigned long j; |
32a7627c N |
993 | unsigned long flags; |
994 | struct page *page = NULL, *lastpage = NULL; | |
995 | int err = 0; | |
996 | int blocks; | |
997 | int attr; | |
998 | ||
999 | if (bitmap == NULL) | |
1000 | return 0; | |
1001 | if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ)) | |
1002 | return 0; | |
1003 | bitmap->daemon_lastrun = jiffies; | |
1004 | ||
1005 | for (j = 0; j < bitmap->chunks; j++) { | |
1006 | bitmap_counter_t *bmc; | |
1007 | spin_lock_irqsave(&bitmap->lock, flags); | |
a654b9d8 | 1008 | if (!bitmap->filemap) { |
32a7627c N |
1009 | /* error or shutdown */ |
1010 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1011 | break; | |
1012 | } | |
1013 | ||
1014 | page = filemap_get_page(bitmap, j); | |
32a7627c N |
1015 | |
1016 | if (page != lastpage) { | |
aa3163f8 N |
1017 | /* skip this page unless it's marked as needing cleaning */ |
1018 | if (!((attr=get_page_attr(bitmap, page)) & BITMAP_PAGE_CLEAN)) { | |
1019 | if (attr & BITMAP_PAGE_NEEDWRITE) { | |
1020 | page_cache_get(page); | |
1021 | clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE); | |
1022 | } | |
1023 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1024 | if (attr & BITMAP_PAGE_NEEDWRITE) { | |
8a5e9cf1 N |
1025 | switch (write_page(bitmap, page, 0)) { |
1026 | case -EAGAIN: | |
1027 | set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE); | |
1028 | break; | |
1029 | case 0: | |
1030 | break; | |
1031 | default: | |
aa3163f8 | 1032 | bitmap_file_kick(bitmap); |
8a5e9cf1 | 1033 | } |
aa3163f8 N |
1034 | page_cache_release(page); |
1035 | } | |
1036 | continue; | |
1037 | } | |
1038 | ||
32a7627c N |
1039 | /* grab the new page, sync and release the old */ |
1040 | page_cache_get(page); | |
1041 | if (lastpage != NULL) { | |
1042 | if (get_page_attr(bitmap, lastpage) & BITMAP_PAGE_NEEDWRITE) { | |
1043 | clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); | |
1044 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
77ad4bc7 | 1045 | err = write_page(bitmap, lastpage, 0); |
8a5e9cf1 N |
1046 | if (err == -EAGAIN) { |
1047 | err = 0; | |
1048 | set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); | |
1049 | } | |
32a7627c N |
1050 | } else { |
1051 | set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); | |
1052 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1053 | } | |
1054 | kunmap(lastpage); | |
1055 | page_cache_release(lastpage); | |
1056 | if (err) | |
1057 | bitmap_file_kick(bitmap); | |
1058 | } else | |
1059 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1060 | lastpage = page; | |
1061 | kmap(page); | |
1062 | /* | |
1063 | printk("bitmap clean at page %lu\n", j); | |
1064 | */ | |
1065 | spin_lock_irqsave(&bitmap->lock, flags); | |
1066 | clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN); | |
1067 | } | |
1068 | bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap), | |
1069 | &blocks, 0); | |
1070 | if (bmc) { | |
1071 | /* | |
1072 | if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc); | |
1073 | */ | |
1074 | if (*bmc == 2) { | |
1075 | *bmc=1; /* maybe clear the bit next time */ | |
1076 | set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN); | |
1077 | } else if (*bmc == 1) { | |
1078 | /* we can clear the bit */ | |
1079 | *bmc = 0; | |
1080 | bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap), | |
1081 | -1); | |
1082 | ||
1083 | /* clear the bit */ | |
aa3163f8 | 1084 | clear_bit(file_page_offset(j), page_address(page)); |
32a7627c N |
1085 | } |
1086 | } | |
1087 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1088 | } | |
1089 | ||
1090 | /* now sync the final page */ | |
1091 | if (lastpage != NULL) { | |
1092 | kunmap(lastpage); | |
1093 | spin_lock_irqsave(&bitmap->lock, flags); | |
1094 | if (get_page_attr(bitmap, lastpage) &BITMAP_PAGE_NEEDWRITE) { | |
1095 | clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); | |
1096 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
77ad4bc7 | 1097 | err = write_page(bitmap, lastpage, 0); |
8a5e9cf1 N |
1098 | if (err == -EAGAIN) { |
1099 | set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); | |
1100 | err = 0; | |
1101 | } | |
32a7627c N |
1102 | } else { |
1103 | set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); | |
1104 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1105 | } | |
1106 | ||
1107 | page_cache_release(lastpage); | |
1108 | } | |
1109 | ||
1110 | return err; | |
1111 | } | |
1112 | ||
1113 | static void daemon_exit(struct bitmap *bitmap, mdk_thread_t **daemon) | |
1114 | { | |
1115 | mdk_thread_t *dmn; | |
1116 | unsigned long flags; | |
1117 | ||
1118 | /* if no one is waiting on us, we'll free the md thread struct | |
1119 | * and exit, otherwise we let the waiter clean things up */ | |
1120 | spin_lock_irqsave(&bitmap->lock, flags); | |
1121 | if ((dmn = *daemon)) { /* no one is waiting, cleanup and exit */ | |
1122 | *daemon = NULL; | |
1123 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1124 | kfree(dmn); | |
1125 | complete_and_exit(NULL, 0); /* do_exit not exported */ | |
1126 | } | |
1127 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1128 | } | |
1129 | ||
1130 | static void bitmap_writeback_daemon(mddev_t *mddev) | |
1131 | { | |
1132 | struct bitmap *bitmap = mddev->bitmap; | |
1133 | struct page *page; | |
1134 | struct page_list *item; | |
1135 | int err = 0; | |
1136 | ||
77ad4bc7 N |
1137 | if (signal_pending(current)) { |
1138 | printk(KERN_INFO | |
1139 | "%s: bitmap writeback daemon got signal, exiting...\n", | |
1140 | bmname(bitmap)); | |
1141 | err = -EINTR; | |
1142 | goto out; | |
1143 | } | |
32a7627c | 1144 | |
77ad4bc7 N |
1145 | PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap)); |
1146 | /* wait on bitmap page writebacks */ | |
1147 | while ((item = dequeue_page(bitmap))) { | |
1148 | page = item->page; | |
1149 | mempool_free(item, bitmap->write_pool); | |
1150 | PRINTK("wait on page writeback: %p\n", page); | |
1151 | wait_on_page_writeback(page); | |
1152 | PRINTK("finished page writeback: %p\n", page); | |
1153 | ||
1154 | err = PageError(page); | |
1155 | page_cache_release(page); | |
1156 | if (err) { | |
1157 | printk(KERN_WARNING "%s: bitmap file writeback " | |
1158 | "failed (page %lu): %d\n", | |
1159 | bmname(bitmap), page->index, err); | |
1160 | bitmap_file_kick(bitmap); | |
1161 | goto out; | |
32a7627c N |
1162 | } |
1163 | } | |
77ad4bc7 N |
1164 | out: |
1165 | wake_up(&bitmap->write_wait); | |
32a7627c N |
1166 | if (err) { |
1167 | printk(KERN_INFO "%s: bitmap writeback daemon exiting (%d)\n", | |
77ad4bc7 | 1168 | bmname(bitmap), err); |
32a7627c N |
1169 | daemon_exit(bitmap, &bitmap->writeback_daemon); |
1170 | } | |
32a7627c N |
1171 | } |
1172 | ||
1173 | static int bitmap_start_daemon(struct bitmap *bitmap, mdk_thread_t **ptr, | |
1174 | void (*func)(mddev_t *), char *name) | |
1175 | { | |
1176 | mdk_thread_t *daemon; | |
1177 | unsigned long flags; | |
1178 | char namebuf[32]; | |
1179 | ||
1180 | spin_lock_irqsave(&bitmap->lock, flags); | |
1181 | *ptr = NULL; | |
a654b9d8 | 1182 | |
32a7627c N |
1183 | if (!bitmap->file) /* no need for daemon if there's no backing file */ |
1184 | goto out_unlock; | |
1185 | ||
1186 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1187 | ||
44456d37 | 1188 | #ifdef INJECT_FATAL_FAULT_2 |
32a7627c N |
1189 | daemon = NULL; |
1190 | #else | |
1191 | sprintf(namebuf, "%%s_%s", name); | |
1192 | daemon = md_register_thread(func, bitmap->mddev, namebuf); | |
1193 | #endif | |
1194 | if (!daemon) { | |
1195 | printk(KERN_ERR "%s: failed to start bitmap daemon\n", | |
1196 | bmname(bitmap)); | |
1197 | return -ECHILD; | |
1198 | } | |
1199 | ||
1200 | spin_lock_irqsave(&bitmap->lock, flags); | |
1201 | *ptr = daemon; | |
1202 | ||
1203 | md_wakeup_thread(daemon); /* start it running */ | |
1204 | ||
1205 | PRINTK("%s: %s daemon (pid %d) started...\n", | |
d80a138c | 1206 | bmname(bitmap), name, daemon->tsk->pid); |
32a7627c N |
1207 | out_unlock: |
1208 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1209 | return 0; | |
1210 | } | |
1211 | ||
1212 | static int bitmap_start_daemons(struct bitmap *bitmap) | |
1213 | { | |
1214 | int err = bitmap_start_daemon(bitmap, &bitmap->writeback_daemon, | |
1215 | bitmap_writeback_daemon, "bitmap_wb"); | |
1216 | return err; | |
1217 | } | |
1218 | ||
1219 | static void bitmap_stop_daemon(struct bitmap *bitmap, mdk_thread_t **ptr) | |
1220 | { | |
1221 | mdk_thread_t *daemon; | |
1222 | unsigned long flags; | |
1223 | ||
1224 | spin_lock_irqsave(&bitmap->lock, flags); | |
1225 | daemon = *ptr; | |
1226 | *ptr = NULL; | |
1227 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1228 | if (daemon) | |
1229 | md_unregister_thread(daemon); /* destroy the thread */ | |
1230 | } | |
1231 | ||
1232 | static void bitmap_stop_daemons(struct bitmap *bitmap) | |
1233 | { | |
1234 | /* the daemons can't stop themselves... they'll just exit instead... */ | |
1235 | if (bitmap->writeback_daemon && | |
1236 | current->pid != bitmap->writeback_daemon->tsk->pid) | |
1237 | bitmap_stop_daemon(bitmap, &bitmap->writeback_daemon); | |
1238 | } | |
1239 | ||
1240 | static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap, | |
1241 | sector_t offset, int *blocks, | |
1242 | int create) | |
1243 | { | |
1244 | /* If 'create', we might release the lock and reclaim it. | |
1245 | * The lock must have been taken with interrupts enabled. | |
1246 | * If !create, we don't release the lock. | |
1247 | */ | |
1248 | sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap); | |
1249 | unsigned long page = chunk >> PAGE_COUNTER_SHIFT; | |
1250 | unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT; | |
1251 | sector_t csize; | |
1252 | ||
1253 | if (bitmap_checkpage(bitmap, page, create) < 0) { | |
1254 | csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap)); | |
1255 | *blocks = csize - (offset & (csize- 1)); | |
1256 | return NULL; | |
1257 | } | |
1258 | /* now locked ... */ | |
1259 | ||
1260 | if (bitmap->bp[page].hijacked) { /* hijacked pointer */ | |
1261 | /* should we use the first or second counter field | |
1262 | * of the hijacked pointer? */ | |
1263 | int hi = (pageoff > PAGE_COUNTER_MASK); | |
1264 | csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) + | |
1265 | PAGE_COUNTER_SHIFT - 1); | |
1266 | *blocks = csize - (offset & (csize- 1)); | |
1267 | return &((bitmap_counter_t *) | |
1268 | &bitmap->bp[page].map)[hi]; | |
1269 | } else { /* page is allocated */ | |
1270 | csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap)); | |
1271 | *blocks = csize - (offset & (csize- 1)); | |
1272 | return (bitmap_counter_t *) | |
1273 | &(bitmap->bp[page].map[pageoff]); | |
1274 | } | |
1275 | } | |
1276 | ||
1277 | int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors) | |
1278 | { | |
1279 | if (!bitmap) return 0; | |
1280 | while (sectors) { | |
1281 | int blocks; | |
1282 | bitmap_counter_t *bmc; | |
1283 | ||
1284 | spin_lock_irq(&bitmap->lock); | |
1285 | bmc = bitmap_get_counter(bitmap, offset, &blocks, 1); | |
1286 | if (!bmc) { | |
1287 | spin_unlock_irq(&bitmap->lock); | |
1288 | return 0; | |
1289 | } | |
1290 | ||
1291 | switch(*bmc) { | |
1292 | case 0: | |
1293 | bitmap_file_set_bit(bitmap, offset); | |
1294 | bitmap_count_page(bitmap,offset, 1); | |
1295 | blk_plug_device(bitmap->mddev->queue); | |
1296 | /* fall through */ | |
1297 | case 1: | |
1298 | *bmc = 2; | |
1299 | } | |
1300 | if ((*bmc & COUNTER_MAX) == COUNTER_MAX) BUG(); | |
1301 | (*bmc)++; | |
1302 | ||
1303 | spin_unlock_irq(&bitmap->lock); | |
1304 | ||
1305 | offset += blocks; | |
1306 | if (sectors > blocks) | |
1307 | sectors -= blocks; | |
1308 | else sectors = 0; | |
1309 | } | |
1310 | return 0; | |
1311 | } | |
1312 | ||
1313 | void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, | |
1314 | int success) | |
1315 | { | |
1316 | if (!bitmap) return; | |
1317 | while (sectors) { | |
1318 | int blocks; | |
1319 | unsigned long flags; | |
1320 | bitmap_counter_t *bmc; | |
1321 | ||
1322 | spin_lock_irqsave(&bitmap->lock, flags); | |
1323 | bmc = bitmap_get_counter(bitmap, offset, &blocks, 0); | |
1324 | if (!bmc) { | |
1325 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1326 | return; | |
1327 | } | |
1328 | ||
1329 | if (!success && ! (*bmc & NEEDED_MASK)) | |
1330 | *bmc |= NEEDED_MASK; | |
1331 | ||
1332 | (*bmc)--; | |
1333 | if (*bmc <= 2) { | |
1334 | set_page_attr(bitmap, | |
1335 | filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)), | |
1336 | BITMAP_PAGE_CLEAN); | |
1337 | } | |
1338 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1339 | offset += blocks; | |
1340 | if (sectors > blocks) | |
1341 | sectors -= blocks; | |
1342 | else sectors = 0; | |
1343 | } | |
1344 | } | |
1345 | ||
6a806c51 N |
1346 | int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks, |
1347 | int degraded) | |
32a7627c N |
1348 | { |
1349 | bitmap_counter_t *bmc; | |
1350 | int rv; | |
1351 | if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */ | |
1352 | *blocks = 1024; | |
1353 | return 1; /* always resync if no bitmap */ | |
1354 | } | |
1355 | spin_lock_irq(&bitmap->lock); | |
1356 | bmc = bitmap_get_counter(bitmap, offset, blocks, 0); | |
1357 | rv = 0; | |
1358 | if (bmc) { | |
1359 | /* locked */ | |
1360 | if (RESYNC(*bmc)) | |
1361 | rv = 1; | |
1362 | else if (NEEDED(*bmc)) { | |
1363 | rv = 1; | |
6a806c51 N |
1364 | if (!degraded) { /* don't set/clear bits if degraded */ |
1365 | *bmc |= RESYNC_MASK; | |
1366 | *bmc &= ~NEEDED_MASK; | |
1367 | } | |
32a7627c N |
1368 | } |
1369 | } | |
1370 | spin_unlock_irq(&bitmap->lock); | |
1371 | return rv; | |
1372 | } | |
1373 | ||
1374 | void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted) | |
1375 | { | |
1376 | bitmap_counter_t *bmc; | |
1377 | unsigned long flags; | |
1378 | /* | |
1379 | if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted); | |
1380 | */ if (bitmap == NULL) { | |
1381 | *blocks = 1024; | |
1382 | return; | |
1383 | } | |
1384 | spin_lock_irqsave(&bitmap->lock, flags); | |
1385 | bmc = bitmap_get_counter(bitmap, offset, blocks, 0); | |
1386 | if (bmc == NULL) | |
1387 | goto unlock; | |
1388 | /* locked */ | |
1389 | /* | |
1390 | if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks); | |
1391 | */ | |
1392 | if (RESYNC(*bmc)) { | |
1393 | *bmc &= ~RESYNC_MASK; | |
1394 | ||
1395 | if (!NEEDED(*bmc) && aborted) | |
1396 | *bmc |= NEEDED_MASK; | |
1397 | else { | |
1398 | if (*bmc <= 2) { | |
1399 | set_page_attr(bitmap, | |
1400 | filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)), | |
1401 | BITMAP_PAGE_CLEAN); | |
1402 | } | |
1403 | } | |
1404 | } | |
1405 | unlock: | |
1406 | spin_unlock_irqrestore(&bitmap->lock, flags); | |
1407 | } | |
1408 | ||
1409 | void bitmap_close_sync(struct bitmap *bitmap) | |
1410 | { | |
1411 | /* Sync has finished, and any bitmap chunks that weren't synced | |
1412 | * properly have been aborted. It remains to us to clear the | |
1413 | * RESYNC bit wherever it is still on | |
1414 | */ | |
1415 | sector_t sector = 0; | |
1416 | int blocks; | |
1417 | if (!bitmap) return; | |
1418 | while (sector < bitmap->mddev->resync_max_sectors) { | |
1419 | bitmap_end_sync(bitmap, sector, &blocks, 0); | |
1420 | /* | |
1421 | if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n", | |
1422 | (unsigned long long)sector, blocks); | |
1423 | */ sector += blocks; | |
1424 | } | |
1425 | } | |
1426 | ||
193f1c93 | 1427 | static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset) |
32a7627c N |
1428 | { |
1429 | /* For each chunk covered by any of these sectors, set the | |
193f1c93 | 1430 | * counter to 1 and set resync_needed. They should all |
32a7627c N |
1431 | * be 0 at this point |
1432 | */ | |
193f1c93 N |
1433 | |
1434 | int secs; | |
1435 | bitmap_counter_t *bmc; | |
1436 | spin_lock_irq(&bitmap->lock); | |
1437 | bmc = bitmap_get_counter(bitmap, offset, &secs, 1); | |
1438 | if (!bmc) { | |
32a7627c | 1439 | spin_unlock_irq(&bitmap->lock); |
193f1c93 | 1440 | return; |
32a7627c | 1441 | } |
193f1c93 N |
1442 | if (! *bmc) { |
1443 | struct page *page; | |
1444 | *bmc = 1 | NEEDED_MASK; | |
1445 | bitmap_count_page(bitmap, offset, 1); | |
1446 | page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)); | |
1447 | set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN); | |
1448 | } | |
1449 | spin_unlock_irq(&bitmap->lock); | |
1450 | ||
32a7627c N |
1451 | } |
1452 | ||
32a7627c N |
1453 | /* |
1454 | * free memory that was allocated | |
1455 | */ | |
1456 | void bitmap_destroy(mddev_t *mddev) | |
1457 | { | |
1458 | unsigned long k, pages; | |
1459 | struct bitmap_page *bp; | |
1460 | struct bitmap *bitmap = mddev->bitmap; | |
1461 | ||
1462 | if (!bitmap) /* there was no bitmap */ | |
1463 | return; | |
1464 | ||
1465 | mddev->bitmap = NULL; /* disconnect from the md device */ | |
1466 | ||
1467 | /* release the bitmap file and kill the daemon */ | |
1468 | bitmap_file_put(bitmap); | |
1469 | ||
1470 | bp = bitmap->bp; | |
1471 | pages = bitmap->pages; | |
1472 | ||
1473 | /* free all allocated memory */ | |
1474 | ||
1475 | mempool_destroy(bitmap->write_pool); | |
1476 | ||
1477 | if (bp) /* deallocate the page memory */ | |
1478 | for (k = 0; k < pages; k++) | |
1479 | if (bp[k].map && !bp[k].hijacked) | |
1480 | kfree(bp[k].map); | |
1481 | kfree(bp); | |
1482 | kfree(bitmap); | |
1483 | } | |
1484 | ||
1485 | /* | |
1486 | * initialize the bitmap structure | |
1487 | * if this returns an error, bitmap_destroy must be called to do clean up | |
1488 | */ | |
1489 | int bitmap_create(mddev_t *mddev) | |
1490 | { | |
1491 | struct bitmap *bitmap; | |
1492 | unsigned long blocks = mddev->resync_max_sectors; | |
1493 | unsigned long chunks; | |
1494 | unsigned long pages; | |
1495 | struct file *file = mddev->bitmap_file; | |
1496 | int err; | |
1497 | ||
1498 | BUG_ON(sizeof(bitmap_super_t) != 256); | |
1499 | ||
a654b9d8 | 1500 | if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */ |
32a7627c N |
1501 | return 0; |
1502 | ||
a654b9d8 N |
1503 | BUG_ON(file && mddev->bitmap_offset); |
1504 | ||
32a7627c N |
1505 | bitmap = kmalloc(sizeof(*bitmap), GFP_KERNEL); |
1506 | if (!bitmap) | |
1507 | return -ENOMEM; | |
1508 | ||
1509 | memset(bitmap, 0, sizeof(*bitmap)); | |
1510 | ||
1511 | spin_lock_init(&bitmap->lock); | |
1512 | bitmap->mddev = mddev; | |
1513 | mddev->bitmap = bitmap; | |
1514 | ||
1515 | spin_lock_init(&bitmap->write_lock); | |
32a7627c N |
1516 | INIT_LIST_HEAD(&bitmap->complete_pages); |
1517 | init_waitqueue_head(&bitmap->write_wait); | |
1518 | bitmap->write_pool = mempool_create(WRITE_POOL_SIZE, write_pool_alloc, | |
1519 | write_pool_free, NULL); | |
1520 | if (!bitmap->write_pool) | |
1521 | return -ENOMEM; | |
1522 | ||
1523 | bitmap->file = file; | |
a654b9d8 N |
1524 | bitmap->offset = mddev->bitmap_offset; |
1525 | if (file) get_file(file); | |
32a7627c N |
1526 | /* read superblock from bitmap file (this sets bitmap->chunksize) */ |
1527 | err = bitmap_read_sb(bitmap); | |
1528 | if (err) | |
1529 | return err; | |
1530 | ||
1531 | bitmap->chunkshift = find_first_bit(&bitmap->chunksize, | |
1532 | sizeof(bitmap->chunksize)); | |
1533 | ||
1534 | /* now that chunksize and chunkshift are set, we can use these macros */ | |
1535 | chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) / | |
1536 | CHUNK_BLOCK_RATIO(bitmap); | |
1537 | pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO; | |
1538 | ||
1539 | BUG_ON(!pages); | |
1540 | ||
1541 | bitmap->chunks = chunks; | |
1542 | bitmap->pages = pages; | |
1543 | bitmap->missing_pages = pages; | |
1544 | bitmap->counter_bits = COUNTER_BITS; | |
1545 | ||
1546 | bitmap->syncchunk = ~0UL; | |
1547 | ||
44456d37 | 1548 | #ifdef INJECT_FATAL_FAULT_1 |
32a7627c N |
1549 | bitmap->bp = NULL; |
1550 | #else | |
1551 | bitmap->bp = kmalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL); | |
1552 | #endif | |
1553 | if (!bitmap->bp) | |
1554 | return -ENOMEM; | |
1555 | memset(bitmap->bp, 0, pages * sizeof(*bitmap->bp)); | |
1556 | ||
1557 | bitmap->flags |= BITMAP_ACTIVE; | |
1558 | ||
1559 | /* now that we have some pages available, initialize the in-memory | |
1560 | * bitmap from the on-disk bitmap */ | |
193f1c93 N |
1561 | err = bitmap_init_from_disk(bitmap); |
1562 | ||
32a7627c N |
1563 | if (err) |
1564 | return err; | |
1565 | ||
1566 | printk(KERN_INFO "created bitmap (%lu pages) for device %s\n", | |
1567 | pages, bmname(bitmap)); | |
1568 | ||
1569 | /* kick off the bitmap daemons */ | |
1570 | err = bitmap_start_daemons(bitmap); | |
1571 | if (err) | |
1572 | return err; | |
1573 | return bitmap_update_sb(bitmap); | |
1574 | } | |
1575 | ||
1576 | /* the bitmap API -- for raid personalities */ | |
1577 | EXPORT_SYMBOL(bitmap_startwrite); | |
1578 | EXPORT_SYMBOL(bitmap_endwrite); | |
1579 | EXPORT_SYMBOL(bitmap_start_sync); | |
1580 | EXPORT_SYMBOL(bitmap_end_sync); | |
1581 | EXPORT_SYMBOL(bitmap_unplug); | |
1582 | EXPORT_SYMBOL(bitmap_close_sync); | |
1583 | EXPORT_SYMBOL(bitmap_daemon_work); |