aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/md/bitmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/md/bitmap.c')
-rw-r--r--drivers/md/bitmap.c283
1 files changed, 135 insertions, 148 deletions
diff --git a/drivers/md/bitmap.c b/drivers/md/bitmap.c
index e40d3e4b8d27..bedd66e9e8ac 100644
--- a/drivers/md/bitmap.c
+++ b/drivers/md/bitmap.c
@@ -67,7 +67,6 @@ static inline char * bmname(struct bitmap *bitmap)
67 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX"; 67 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
68} 68}
69 69
70#define WRITE_POOL_SIZE 256
71 70
72/* 71/*
73 * just a placeholder - calls kmalloc for bitmap pages 72 * just a placeholder - calls kmalloc for bitmap pages
@@ -279,75 +278,137 @@ static int write_sb_page(mddev_t *mddev, long offset, struct page *page, int wai
279 */ 278 */
280static int write_page(struct bitmap *bitmap, struct page *page, int wait) 279static int write_page(struct bitmap *bitmap, struct page *page, int wait)
281{ 280{
282 int ret = -ENOMEM; 281 struct buffer_head *bh;
283 282
284 if (bitmap->file == NULL) 283 if (bitmap->file == NULL)
285 return write_sb_page(bitmap->mddev, bitmap->offset, page, wait); 284 return write_sb_page(bitmap->mddev, bitmap->offset, page, wait);
286 285
287 flush_dcache_page(page); /* make sure visible to anyone reading the file */ 286 bh = page_buffers(page);
288 287
289 if (wait) 288 while (bh && bh->b_blocknr) {
290 lock_page(page); 289 atomic_inc(&bitmap->pending_writes);
291 else { 290 set_buffer_locked(bh);
292 if (TestSetPageLocked(page)) 291 set_buffer_mapped(bh);
293 return -EAGAIN; /* already locked */ 292 submit_bh(WRITE, bh);
294 if (PageWriteback(page)) { 293 bh = bh->b_this_page;
295 unlock_page(page); 294 }
296 return -EAGAIN; 295
297 } 296 if (wait) {
297 wait_event(bitmap->write_wait,
298 atomic_read(&bitmap->pending_writes)==0);
299 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
298 } 300 }
301 return 0;
302}
303
304static void end_bitmap_write(struct buffer_head *bh, int uptodate)
305{
306 struct bitmap *bitmap = bh->b_private;
307 unsigned long flags;
299 308
300 ret = page->mapping->a_ops->prepare_write(bitmap->file, page, 0, PAGE_SIZE); 309 if (!uptodate) {
301 if (!ret) 310 spin_lock_irqsave(&bitmap->lock, flags);
302 ret = page->mapping->a_ops->commit_write(bitmap->file, page, 0, 311 bitmap->flags |= BITMAP_WRITE_ERROR;
303 PAGE_SIZE); 312 spin_unlock_irqrestore(&bitmap->lock, flags);
304 if (ret) {
305 unlock_page(page);
306 return ret;
307 } 313 }
314 if (atomic_dec_and_test(&bitmap->pending_writes))
315 wake_up(&bitmap->write_wait);
316}
308 317
309 set_page_dirty(page); /* force it to be written out */ 318/* copied from buffer.c */
319static void
320__clear_page_buffers(struct page *page)
321{
322 ClearPagePrivate(page);
323 set_page_private(page, 0);
324 page_cache_release(page);
325}
326static void free_buffers(struct page *page)
327{
328 struct buffer_head *bh = page_buffers(page);
310 329
311 if (!wait) { 330 while (bh) {
312 /* add to list to be waited for */ 331 struct buffer_head *next = bh->b_this_page;
313 struct page_list *item = mempool_alloc(bitmap->write_pool, GFP_NOIO); 332 free_buffer_head(bh);
314 item->page = page; 333 bh = next;
315 spin_lock(&bitmap->write_lock);
316 list_add(&item->list, &bitmap->complete_pages);
317 spin_unlock(&bitmap->write_lock);
318 } 334 }
319 return write_one_page(page, wait); 335 __clear_page_buffers(page);
336 put_page(page);
320} 337}
321 338
322/* read a page from a file, pinning it into cache, and return bytes_read */ 339/* read a page from a file.
340 * We both read the page, and attach buffers to the page to record the
341 * address of each block (using bmap). These addresses will be used
342 * to write the block later, completely bypassing the filesystem.
343 * This usage is similar to how swap files are handled, and allows us
344 * to write to a file with no concerns of memory allocation failing.
345 */
323static struct page *read_page(struct file *file, unsigned long index, 346static struct page *read_page(struct file *file, unsigned long index,
324 unsigned long *bytes_read) 347 struct bitmap *bitmap,
348 unsigned long count)
325{ 349{
326 struct inode *inode = file->f_mapping->host;
327 struct page *page = NULL; 350 struct page *page = NULL;
328 loff_t isize = i_size_read(inode); 351 struct inode *inode = file->f_dentry->d_inode;
329 unsigned long end_index = isize >> PAGE_SHIFT; 352 loff_t pos = index << PAGE_SHIFT;
353 int ret;
354 struct buffer_head *bh;
355 sector_t block;
356 mm_segment_t oldfs;
330 357
331 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE, 358 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
332 (unsigned long long)index << PAGE_SHIFT); 359 (unsigned long long)index << PAGE_SHIFT);
333 360
334 page = read_cache_page(inode->i_mapping, index, 361 page = alloc_page(GFP_KERNEL);
335 (filler_t *)inode->i_mapping->a_ops->readpage, file); 362 if (!page)
363 page = ERR_PTR(-ENOMEM);
336 if (IS_ERR(page)) 364 if (IS_ERR(page))
337 goto out; 365 goto out;
338 wait_on_page_locked(page); 366
339 if (!PageUptodate(page) || PageError(page)) { 367 oldfs = get_fs();
368 set_fs(KERNEL_DS);
369 ret = vfs_read(file, (char __user*) page_address(page), count, &pos);
370 set_fs(oldfs);
371
372 if (ret >= 0 && ret != count)
373 ret = -EIO;
374 if (ret < 0) {
375 put_page(page);
376 page = ERR_PTR(ret);
377 goto out;
378 }
379 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
380 if (!bh) {
340 put_page(page); 381 put_page(page);
341 page = ERR_PTR(-EIO); 382 page = ERR_PTR(-ENOMEM);
342 goto out; 383 goto out;
343 } 384 }
385 attach_page_buffers(page, bh);
386 block = index << (PAGE_SHIFT - inode->i_blkbits);
387 while (bh) {
388 if (count == 0)
389 bh->b_blocknr = 0;
390 else {
391 bh->b_blocknr = bmap(inode, block);
392 if (bh->b_blocknr == 0) {
393 /* Cannot use this file! */
394 free_buffers(page);
395 page = ERR_PTR(-EINVAL);
396 goto out;
397 }
398 bh->b_bdev = inode->i_sb->s_bdev;
399 if (count < (1<<inode->i_blkbits))
400 count = 0;
401 else
402 count -= (1<<inode->i_blkbits);
403
404 bh->b_end_io = end_bitmap_write;
405 bh->b_private = bitmap;
406 }
407 block++;
408 bh = bh->b_this_page;
409 }
344 410
345 if (index > end_index) /* we have read beyond EOF */ 411 page->index = index;
346 *bytes_read = 0;
347 else if (index == end_index) /* possible short read */
348 *bytes_read = isize & ~PAGE_MASK;
349 else
350 *bytes_read = PAGE_SIZE; /* got a full page */
351out: 412out:
352 if (IS_ERR(page)) 413 if (IS_ERR(page))
353 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n", 414 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
@@ -418,16 +479,14 @@ static int bitmap_read_sb(struct bitmap *bitmap)
418 char *reason = NULL; 479 char *reason = NULL;
419 bitmap_super_t *sb; 480 bitmap_super_t *sb;
420 unsigned long chunksize, daemon_sleep, write_behind; 481 unsigned long chunksize, daemon_sleep, write_behind;
421 unsigned long bytes_read;
422 unsigned long long events; 482 unsigned long long events;
423 int err = -EINVAL; 483 int err = -EINVAL;
424 484
425 /* page 0 is the superblock, read it... */ 485 /* page 0 is the superblock, read it... */
426 if (bitmap->file) 486 if (bitmap->file)
427 bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read); 487 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, PAGE_SIZE);
428 else { 488 else {
429 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0); 489 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
430 bytes_read = PAGE_SIZE;
431 } 490 }
432 if (IS_ERR(bitmap->sb_page)) { 491 if (IS_ERR(bitmap->sb_page)) {
433 err = PTR_ERR(bitmap->sb_page); 492 err = PTR_ERR(bitmap->sb_page);
@@ -437,13 +496,6 @@ static int bitmap_read_sb(struct bitmap *bitmap)
437 496
438 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0); 497 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
439 498
440 if (bytes_read < sizeof(*sb)) { /* short read */
441 printk(KERN_INFO "%s: bitmap file superblock truncated\n",
442 bmname(bitmap));
443 err = -ENOSPC;
444 goto out;
445 }
446
447 chunksize = le32_to_cpu(sb->chunksize); 499 chunksize = le32_to_cpu(sb->chunksize);
448 daemon_sleep = le32_to_cpu(sb->daemon_sleep); 500 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
449 write_behind = le32_to_cpu(sb->write_behind); 501 write_behind = le32_to_cpu(sb->write_behind);
@@ -589,37 +641,12 @@ static void bitmap_file_unmap(struct bitmap *bitmap)
589 641
590 while (pages--) 642 while (pages--)
591 if (map[pages]->index != 0) /* 0 is sb_page, release it below */ 643 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
592 put_page(map[pages]); 644 free_buffers(map[pages]);
593 kfree(map); 645 kfree(map);
594 kfree(attr); 646 kfree(attr);
595 647
596 safe_put_page(sb_page); 648 if (sb_page)
597} 649 free_buffers(sb_page);
598
599/* dequeue the next item in a page list -- don't call from irq context */
600static struct page_list *dequeue_page(struct bitmap *bitmap)
601{
602 struct page_list *item = NULL;
603 struct list_head *head = &bitmap->complete_pages;
604
605 spin_lock(&bitmap->write_lock);
606 if (list_empty(head))
607 goto out;
608 item = list_entry(head->prev, struct page_list, list);
609 list_del(head->prev);
610out:
611 spin_unlock(&bitmap->write_lock);
612 return item;
613}
614
615static void drain_write_queues(struct bitmap *bitmap)
616{
617 struct page_list *item;
618
619 while ((item = dequeue_page(bitmap))) {
620 /* don't bother to wait */
621 mempool_free(item, bitmap->write_pool);
622 }
623} 650}
624 651
625static void bitmap_file_put(struct bitmap *bitmap) 652static void bitmap_file_put(struct bitmap *bitmap)
@@ -632,12 +659,16 @@ static void bitmap_file_put(struct bitmap *bitmap)
632 bitmap->file = NULL; 659 bitmap->file = NULL;
633 spin_unlock_irqrestore(&bitmap->lock, flags); 660 spin_unlock_irqrestore(&bitmap->lock, flags);
634 661
635 drain_write_queues(bitmap); 662 if (file)
636 663 wait_event(bitmap->write_wait,
664 atomic_read(&bitmap->pending_writes)==0);
637 bitmap_file_unmap(bitmap); 665 bitmap_file_unmap(bitmap);
638 666
639 if (file) 667 if (file) {
668 struct inode *inode = file->f_dentry->d_inode;
669 invalidate_inode_pages(inode->i_mapping);
640 fput(file); 670 fput(file);
671 }
641} 672}
642 673
643 674
@@ -728,8 +759,6 @@ static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
728 759
729} 760}
730 761
731static void bitmap_writeback(struct bitmap *bitmap);
732
733/* this gets called when the md device is ready to unplug its underlying 762/* this gets called when the md device is ready to unplug its underlying
734 * (slave) device queues -- before we let any writes go down, we need to 763 * (slave) device queues -- before we let any writes go down, we need to
735 * sync the dirty pages of the bitmap file to disk */ 764 * sync the dirty pages of the bitmap file to disk */
@@ -761,24 +790,18 @@ int bitmap_unplug(struct bitmap *bitmap)
761 wait = 1; 790 wait = 1;
762 spin_unlock_irqrestore(&bitmap->lock, flags); 791 spin_unlock_irqrestore(&bitmap->lock, flags);
763 792
764 if (dirty | need_write) { 793 if (dirty | need_write)
765 err = write_page(bitmap, page, 0); 794 err = write_page(bitmap, page, 0);
766 if (err == -EAGAIN) {
767 if (dirty)
768 err = write_page(bitmap, page, 1);
769 else
770 err = 0;
771 }
772 if (err)
773 return 1;
774 }
775 } 795 }
776 if (wait) { /* if any writes were performed, we need to wait on them */ 796 if (wait) { /* if any writes were performed, we need to wait on them */
777 if (bitmap->file) 797 if (bitmap->file)
778 bitmap_writeback(bitmap); 798 wait_event(bitmap->write_wait,
799 atomic_read(&bitmap->pending_writes)==0);
779 else 800 else
780 md_super_wait(bitmap->mddev); 801 md_super_wait(bitmap->mddev);
781 } 802 }
803 if (bitmap->flags & BITMAP_WRITE_ERROR)
804 bitmap_file_kick(bitmap);
782 return 0; 805 return 0;
783} 806}
784 807
@@ -800,7 +823,7 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
800 struct page *page = NULL, *oldpage = NULL; 823 struct page *page = NULL, *oldpage = NULL;
801 unsigned long num_pages, bit_cnt = 0; 824 unsigned long num_pages, bit_cnt = 0;
802 struct file *file; 825 struct file *file;
803 unsigned long bytes, offset, dummy; 826 unsigned long bytes, offset;
804 int outofdate; 827 int outofdate;
805 int ret = -ENOSPC; 828 int ret = -ENOSPC;
806 void *paddr; 829 void *paddr;
@@ -853,7 +876,12 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
853 index = file_page_index(i); 876 index = file_page_index(i);
854 bit = file_page_offset(i); 877 bit = file_page_offset(i);
855 if (index != oldindex) { /* this is a new page, read it in */ 878 if (index != oldindex) { /* this is a new page, read it in */
879 int count;
856 /* unmap the old page, we're done with it */ 880 /* unmap the old page, we're done with it */
881 if (index == num_pages-1)
882 count = bytes - index * PAGE_SIZE;
883 else
884 count = PAGE_SIZE;
857 if (index == 0) { 885 if (index == 0) {
858 /* 886 /*
859 * if we're here then the superblock page 887 * if we're here then the superblock page
@@ -863,7 +891,7 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
863 page = bitmap->sb_page; 891 page = bitmap->sb_page;
864 offset = sizeof(bitmap_super_t); 892 offset = sizeof(bitmap_super_t);
865 } else if (file) { 893 } else if (file) {
866 page = read_page(file, index, &dummy); 894 page = read_page(file, index, bitmap, count);
867 offset = 0; 895 offset = 0;
868 } else { 896 } else {
869 page = read_sb_page(bitmap->mddev, bitmap->offset, index); 897 page = read_sb_page(bitmap->mddev, bitmap->offset, index);
@@ -999,9 +1027,6 @@ int bitmap_daemon_work(struct bitmap *bitmap)
999 spin_unlock_irqrestore(&bitmap->lock, flags); 1027 spin_unlock_irqrestore(&bitmap->lock, flags);
1000 if (need_write) { 1028 if (need_write) {
1001 switch (write_page(bitmap, page, 0)) { 1029 switch (write_page(bitmap, page, 0)) {
1002 case -EAGAIN:
1003 set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1004 break;
1005 case 0: 1030 case 0:
1006 break; 1031 break;
1007 default: 1032 default:
@@ -1017,10 +1042,6 @@ int bitmap_daemon_work(struct bitmap *bitmap)
1017 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); 1042 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1018 spin_unlock_irqrestore(&bitmap->lock, flags); 1043 spin_unlock_irqrestore(&bitmap->lock, flags);
1019 err = write_page(bitmap, lastpage, 0); 1044 err = write_page(bitmap, lastpage, 0);
1020 if (err == -EAGAIN) {
1021 err = 0;
1022 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1023 }
1024 } else { 1045 } else {
1025 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); 1046 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1026 spin_unlock_irqrestore(&bitmap->lock, flags); 1047 spin_unlock_irqrestore(&bitmap->lock, flags);
@@ -1070,10 +1091,6 @@ int bitmap_daemon_work(struct bitmap *bitmap)
1070 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); 1091 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1071 spin_unlock_irqrestore(&bitmap->lock, flags); 1092 spin_unlock_irqrestore(&bitmap->lock, flags);
1072 err = write_page(bitmap, lastpage, 0); 1093 err = write_page(bitmap, lastpage, 0);
1073 if (err == -EAGAIN) {
1074 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1075 err = 0;
1076 }
1077 } else { 1094 } else {
1078 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE); 1095 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1079 spin_unlock_irqrestore(&bitmap->lock, flags); 1096 spin_unlock_irqrestore(&bitmap->lock, flags);
@@ -1083,32 +1100,6 @@ int bitmap_daemon_work(struct bitmap *bitmap)
1083 return err; 1100 return err;
1084} 1101}
1085 1102
1086static void bitmap_writeback(struct bitmap *bitmap)
1087{
1088 struct page *page;
1089 struct page_list *item;
1090 int err = 0;
1091
1092 PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
1093 /* wait on bitmap page writebacks */
1094 while ((item = dequeue_page(bitmap))) {
1095 page = item->page;
1096 mempool_free(item, bitmap->write_pool);
1097 PRINTK("wait on page writeback: %p\n", page);
1098 wait_on_page_writeback(page);
1099 PRINTK("finished page writeback: %p\n", page);
1100
1101 err = PageError(page);
1102 if (err) {
1103 printk(KERN_WARNING "%s: bitmap file writeback "
1104 "failed (page %lu): %d\n",
1105 bmname(bitmap), page->index, err);
1106 bitmap_file_kick(bitmap);
1107 break;
1108 }
1109 }
1110}
1111
1112static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap, 1103static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1113 sector_t offset, int *blocks, 1104 sector_t offset, int *blocks,
1114 int create) 1105 int create)
@@ -1377,8 +1368,6 @@ static void bitmap_free(struct bitmap *bitmap)
1377 1368
1378 /* free all allocated memory */ 1369 /* free all allocated memory */
1379 1370
1380 mempool_destroy(bitmap->write_pool);
1381
1382 if (bp) /* deallocate the page memory */ 1371 if (bp) /* deallocate the page memory */
1383 for (k = 0; k < pages; k++) 1372 for (k = 0; k < pages; k++)
1384 if (bp[k].map && !bp[k].hijacked) 1373 if (bp[k].map && !bp[k].hijacked)
@@ -1428,17 +1417,13 @@ int bitmap_create(mddev_t *mddev)
1428 spin_lock_init(&bitmap->lock); 1417 spin_lock_init(&bitmap->lock);
1429 bitmap->mddev = mddev; 1418 bitmap->mddev = mddev;
1430 1419
1431 spin_lock_init(&bitmap->write_lock);
1432 INIT_LIST_HEAD(&bitmap->complete_pages);
1433 bitmap->write_pool = mempool_create_kmalloc_pool(WRITE_POOL_SIZE,
1434 sizeof(struct page_list));
1435 err = -ENOMEM;
1436 if (!bitmap->write_pool)
1437 goto error;
1438
1439 bitmap->file = file; 1420 bitmap->file = file;
1440 bitmap->offset = mddev->bitmap_offset; 1421 bitmap->offset = mddev->bitmap_offset;
1441 if (file) get_file(file); 1422 if (file) get_file(file);
1423
1424 /* Ensure we read fresh data */
1425 invalidate_inode_pages(file->f_dentry->d_inode->i_mapping);
1426
1442 /* read superblock from bitmap file (this sets bitmap->chunksize) */ 1427 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1443 err = bitmap_read_sb(bitmap); 1428 err = bitmap_read_sb(bitmap);
1444 if (err) 1429 if (err)
@@ -1461,6 +1446,9 @@ int bitmap_create(mddev_t *mddev)
1461 1446
1462 bitmap->syncchunk = ~0UL; 1447 bitmap->syncchunk = ~0UL;
1463 1448
1449 atomic_set(&bitmap->pending_writes, 0);
1450 init_waitqueue_head(&bitmap->write_wait);
1451
1464#ifdef INJECT_FATAL_FAULT_1 1452#ifdef INJECT_FATAL_FAULT_1
1465 bitmap->bp = NULL; 1453 bitmap->bp = NULL;
1466#else 1454#else
@@ -1503,4 +1491,3 @@ EXPORT_SYMBOL(bitmap_start_sync);
1503EXPORT_SYMBOL(bitmap_end_sync); 1491EXPORT_SYMBOL(bitmap_end_sync);
1504EXPORT_SYMBOL(bitmap_unplug); 1492EXPORT_SYMBOL(bitmap_unplug);
1505EXPORT_SYMBOL(bitmap_close_sync); 1493EXPORT_SYMBOL(bitmap_close_sync);
1506EXPORT_SYMBOL(bitmap_daemon_work);