2195 lines · 57.2 KB · C
RawDownload
/* * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003 * * bitmap_create  - sets up the bitmap structure * bitmap_destroy - destroys the bitmap structure * * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.: * - added disk storage for bitmap * - changes to allow various bitmap chunk sizes */ /* * Still to do: * * flush after percent set rather than just time based. (maybe both). */ #include <linux/blkdev.h>#include <linux/module.h>#include <linux/errno.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/timer.h>#include <linux/sched.h>#include <linux/list.h>#include <linux/file.h>#include <linux/mount.h>#include <linux/buffer_head.h>#include "md.h"#include "bitmap.h" #include <linux/dm-dirty-log.h>/* debug macros */ #define DEBUG 0 #if DEBUG/* these are for debugging purposes only! */ /* define one and only one of these */#define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */#define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/#define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */#define INJECT_FAULTS_4 0 /* undef */#define INJECT_FAULTS_5 0 /* undef */#define INJECT_FAULTS_6 0 /* if these are defined, the driver will fail! debug only */#define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */#define INJECT_FATAL_FAULT_2 0 /* undef */#define INJECT_FATAL_FAULT_3 0 /* undef */#endif #ifndef PRINTK#  if DEBUG > 0#    define PRINTK(x...) printk(KERN_DEBUG x)#  else#    define PRINTK(x...)#  endif#endif static inline char *bmname(struct bitmap *bitmap){	return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";} /* * just a placeholder - calls kmalloc for bitmap pages */static unsigned char *bitmap_alloc_page(struct bitmap *bitmap){	unsigned char *page; #ifdef INJECT_FAULTS_1	page = NULL;#else	page = kzalloc(PAGE_SIZE, GFP_NOIO);#endif	if (!page)		printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));	else		PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",			bmname(bitmap), page);	return page;} /* * for now just a placeholder -- just calls kfree for bitmap pages */static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page){	PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);	kfree(page);} /* * check a page and, if necessary, allocate it (or hijack it if the alloc fails) * * 1) check to see if this page is allocated, if it's not then try to alloc * 2) if the alloc fails, set the page's hijacked flag so we'll use the *    page pointer directly as a counter * * if we find our page, we increment the page's refcount so that it stays * allocated while we're using it */static int bitmap_checkpage(struct bitmap *bitmap,			    unsigned long page, int create)__releases(bitmap->lock)__acquires(bitmap->lock){	unsigned char *mappage; 	if (page >= bitmap->pages) {		/* This can happen if bitmap_start_sync goes beyond		 * End-of-device while looking for a whole page.		 * It is harmless.		 */		return -EINVAL;	} 	if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */		return 0; 	if (bitmap->bp[page].map) /* page is already allocated, just return */		return 0; 	if (!create)		return -ENOENT; 	/* this page has not been allocated yet */ 	spin_unlock_irq(&bitmap->lock);	mappage = bitmap_alloc_page(bitmap);	spin_lock_irq(&bitmap->lock); 	if (mappage == NULL) {		PRINTK("%s: bitmap map page allocation failed, hijacking\n",			bmname(bitmap));		/* failed - set the hijacked flag so that we can use the		 * pointer as a counter */		if (!bitmap->bp[page].map)			bitmap->bp[page].hijacked = 1;	} else if (bitmap->bp[page].map ||		   bitmap->bp[page].hijacked) {		/* somebody beat us to getting the page */		bitmap_free_page(bitmap, mappage);		return 0;	} else { 		/* no page was in place and we have one, so install it */ 		bitmap->bp[page].map = mappage;		bitmap->missing_pages--;	}	return 0;} /* if page is completely empty, put it back on the free list, or dealloc it *//* if page was hijacked, unmark the flag so it might get alloced next time *//* Note: lock should be held when calling this */static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page){	char *ptr; 	if (bitmap->bp[page].count) /* page is still busy */		return; 	/* page is no longer in use, it can be released */ 	if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */		bitmap->bp[page].hijacked = 0;		bitmap->bp[page].map = NULL;	} else {		/* normal case, free the page */		ptr = bitmap->bp[page].map;		bitmap->bp[page].map = NULL;		bitmap->missing_pages++;		bitmap_free_page(bitmap, ptr);	}} /* * bitmap file handling - read and write the bitmap file and its superblock */ /* * basic page I/O operations */ /* IO operations when bitmap is stored near all superblocks */static struct page *read_sb_page(mddev_t *mddev, loff_t offset,				 struct page *page,				 unsigned long index, int size){	/* choose a good rdev and read the page from there */ 	mdk_rdev_t *rdev;	sector_t target;	int did_alloc = 0; 	if (!page) {		page = alloc_page(GFP_KERNEL);		if (!page)			return ERR_PTR(-ENOMEM);		did_alloc = 1;	} 	list_for_each_entry(rdev, &mddev->disks, same_set) {		if (! test_bit(In_sync, &rdev->flags)		    || test_bit(Faulty, &rdev->flags))			continue; 		target = offset + index * (PAGE_SIZE/512); 		if (sync_page_io(rdev, target,				 roundup(size, bdev_logical_block_size(rdev->bdev)),				 page, READ, true)) {			page->index = index;			attach_page_buffers(page, NULL); /* so that free_buffer will							  * quietly no-op */			return page;		}	}	if (did_alloc)		put_page(page);	return ERR_PTR(-EIO); } static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev){	/* Iterate the disks of an mddev, using rcu to protect access to the	 * linked list, and raising the refcount of devices we return to ensure	 * they don't disappear while in use.	 * As devices are only added or removed when raid_disk is < 0 and	 * nr_pending is 0 and In_sync is clear, the entries we return will	 * still be in the same position on the list when we re-enter	 * list_for_each_continue_rcu.	 */	struct list_head *pos;	rcu_read_lock();	if (rdev == NULL)		/* start at the beginning */		pos = &mddev->disks;	else {		/* release the previous rdev and start from there. */		rdev_dec_pending(rdev, mddev);		pos = &rdev->same_set;	}	list_for_each_continue_rcu(pos, &mddev->disks) {		rdev = list_entry(pos, mdk_rdev_t, same_set);		if (rdev->raid_disk >= 0 &&		    !test_bit(Faulty, &rdev->flags)) {			/* this is a usable devices */			atomic_inc(&rdev->nr_pending);			rcu_read_unlock();			return rdev;		}	}	rcu_read_unlock();	return NULL;} static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait){	mdk_rdev_t *rdev = NULL;	struct block_device *bdev;	mddev_t *mddev = bitmap->mddev; 	while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {		int size = PAGE_SIZE;		loff_t offset = mddev->bitmap_info.offset; 		bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev; 		if (page->index == bitmap->file_pages-1)			size = roundup(bitmap->last_page_size,				       bdev_logical_block_size(bdev));		/* Just make sure we aren't corrupting data or		 * metadata		 */		if (mddev->external) {			/* Bitmap could be anywhere. */			if (rdev->sb_start + offset + (page->index						       * (PAGE_SIZE/512))			    > rdev->data_offset			    &&			    rdev->sb_start + offset			    < (rdev->data_offset + mddev->dev_sectors			     + (PAGE_SIZE/512)))				goto bad_alignment;		} else if (offset < 0) {			/* DATA  BITMAP METADATA  */			if (offset			    + (long)(page->index * (PAGE_SIZE/512))			    + size/512 > 0)				/* bitmap runs in to metadata */				goto bad_alignment;			if (rdev->data_offset + mddev->dev_sectors			    > rdev->sb_start + offset)				/* data runs in to bitmap */				goto bad_alignment;		} else if (rdev->sb_start < rdev->data_offset) {			/* METADATA BITMAP DATA */			if (rdev->sb_start			    + offset			    + page->index*(PAGE_SIZE/512) + size/512			    > rdev->data_offset)				/* bitmap runs in to data */				goto bad_alignment;		} else {			/* DATA METADATA BITMAP - no problems */		}		md_super_write(mddev, rdev,			       rdev->sb_start + offset			       + page->index * (PAGE_SIZE/512),			       size,			       page);	} 	if (wait)		md_super_wait(mddev);	return 0;  bad_alignment:	return -EINVAL;} static void bitmap_file_kick(struct bitmap *bitmap);/* * write out a page to a file */static void write_page(struct bitmap *bitmap, struct page *page, int wait){	struct buffer_head *bh; 	if (bitmap->file == NULL) {		switch (write_sb_page(bitmap, page, wait)) {		case -EINVAL:			bitmap->flags |= BITMAP_WRITE_ERROR;		}	} else { 		bh = page_buffers(page); 		while (bh && bh->b_blocknr) {			atomic_inc(&bitmap->pending_writes);			set_buffer_locked(bh);			set_buffer_mapped(bh);			submit_bh(WRITE | REQ_SYNC, bh);			bh = bh->b_this_page;		} 		if (wait)			wait_event(bitmap->write_wait,				   atomic_read(&bitmap->pending_writes)==0);	}	if (bitmap->flags & BITMAP_WRITE_ERROR)		bitmap_file_kick(bitmap);} static void end_bitmap_write(struct buffer_head *bh, int uptodate){	struct bitmap *bitmap = bh->b_private;	unsigned long flags; 	if (!uptodate) {		spin_lock_irqsave(&bitmap->lock, flags);		bitmap->flags |= BITMAP_WRITE_ERROR;		spin_unlock_irqrestore(&bitmap->lock, flags);	}	if (atomic_dec_and_test(&bitmap->pending_writes))		wake_up(&bitmap->write_wait);} /* copied from buffer.c */static void__clear_page_buffers(struct page *page){	ClearPagePrivate(page);	set_page_private(page, 0);	page_cache_release(page);}static void free_buffers(struct page *page){	struct buffer_head *bh = page_buffers(page); 	while (bh) {		struct buffer_head *next = bh->b_this_page;		free_buffer_head(bh);		bh = next;	}	__clear_page_buffers(page);	put_page(page);} /* read a page from a file. * We both read the page, and attach buffers to the page to record the * address of each block (using bmap).  These addresses will be used * to write the block later, completely bypassing the filesystem. * This usage is similar to how swap files are handled, and allows us * to write to a file with no concerns of memory allocation failing. */static struct page *read_page(struct file *file, unsigned long index,			      struct bitmap *bitmap,			      unsigned long count){	struct page *page = NULL;	struct inode *inode = file->f_path.dentry->d_inode;	struct buffer_head *bh;	sector_t block; 	PRINTK("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,			(unsigned long long)index << PAGE_SHIFT); 	page = alloc_page(GFP_KERNEL);	if (!page)		page = ERR_PTR(-ENOMEM);	if (IS_ERR(page))		goto out; 	bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);	if (!bh) {		put_page(page);		page = ERR_PTR(-ENOMEM);		goto out;	}	attach_page_buffers(page, bh);	block = index << (PAGE_SHIFT - inode->i_blkbits);	while (bh) {		if (count == 0)			bh->b_blocknr = 0;		else {			bh->b_blocknr = bmap(inode, block);			if (bh->b_blocknr == 0) {				/* Cannot use this file! */				free_buffers(page);				page = ERR_PTR(-EINVAL);				goto out;			}			bh->b_bdev = inode->i_sb->s_bdev;			if (count < (1<<inode->i_blkbits))				count = 0;			else				count -= (1<<inode->i_blkbits); 			bh->b_end_io = end_bitmap_write;			bh->b_private = bitmap;			atomic_inc(&bitmap->pending_writes);			set_buffer_locked(bh);			set_buffer_mapped(bh);			submit_bh(READ, bh);		}		block++;		bh = bh->b_this_page;	}	page->index = index; 	wait_event(bitmap->write_wait,		   atomic_read(&bitmap->pending_writes)==0);	if (bitmap->flags & BITMAP_WRITE_ERROR) {		free_buffers(page);		page = ERR_PTR(-EIO);	}out:	if (IS_ERR(page))		printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n",			(int)PAGE_SIZE,			(unsigned long long)index << PAGE_SHIFT,			PTR_ERR(page));	return page;} /* * bitmap file superblock operations */ /* update the event counter and sync the superblock to disk */void bitmap_update_sb(struct bitmap *bitmap){	bitmap_super_t *sb;	unsigned long flags; 	if (!bitmap || !bitmap->mddev) /* no bitmap for this array */		return;	if (bitmap->mddev->bitmap_info.external)		return;	spin_lock_irqsave(&bitmap->lock, flags);	if (!bitmap->sb_page) { /* no superblock */		spin_unlock_irqrestore(&bitmap->lock, flags);		return;	}	spin_unlock_irqrestore(&bitmap->lock, flags);	sb = kmap_atomic(bitmap->sb_page, KM_USER0);	sb->events = cpu_to_le64(bitmap->mddev->events);	if (bitmap->mddev->events < bitmap->events_cleared)		/* rocking back to read-only */		bitmap->events_cleared = bitmap->mddev->events;	sb->events_cleared = cpu_to_le64(bitmap->events_cleared);	sb->state = cpu_to_le32(bitmap->flags);

Lines 1–500 of 2195.