1836 lines · 46.4 KB · C
RawDownload
/* *  linux/fs/block_dev.c * *  Copyright (C) 1991, 1992  Linus Torvalds *  Copyright (C) 2001  Andrea Arcangeli <andrea@suse.de> SuSE */ #include <linux/init.h>#include <linux/mm.h>#include <linux/fcntl.h>#include <linux/slab.h>#include <linux/kmod.h>#include <linux/major.h>#include <linux/device_cgroup.h>#include <linux/highmem.h>#include <linux/blkdev.h>#include <linux/backing-dev.h>#include <linux/module.h>#include <linux/blkpg.h>#include <linux/magic.h>#include <linux/buffer_head.h>#include <linux/swap.h>#include <linux/pagevec.h>#include <linux/writeback.h>#include <linux/mpage.h>#include <linux/mount.h>#include <linux/uio.h>#include <linux/namei.h>#include <linux/log2.h>#include <linux/cleancache.h>#include <linux/dax.h>#include <asm/uaccess.h>#include "internal.h" struct bdev_inode {	struct block_device bdev;	struct inode vfs_inode;}; static const struct address_space_operations def_blk_aops; static inline struct bdev_inode *BDEV_I(struct inode *inode){	return container_of(inode, struct bdev_inode, vfs_inode);} struct block_device *I_BDEV(struct inode *inode){	return &BDEV_I(inode)->bdev;}EXPORT_SYMBOL(I_BDEV); static void bdev_write_inode(struct block_device *bdev){	struct inode *inode = bdev->bd_inode;	int ret; 	spin_lock(&inode->i_lock);	while (inode->i_state & I_DIRTY) {		spin_unlock(&inode->i_lock);		ret = write_inode_now(inode, true);		if (ret) {			char name[BDEVNAME_SIZE];			pr_warn_ratelimited("VFS: Dirty inode writeback failed "					    "for block device %s (err=%d).\n",					    bdevname(bdev, name), ret);		}		spin_lock(&inode->i_lock);	}	spin_unlock(&inode->i_lock);} /* Kill _all_ buffers and pagecache , dirty or not.. */void kill_bdev(struct block_device *bdev){	struct address_space *mapping = bdev->bd_inode->i_mapping; 	if (mapping->nrpages == 0 && mapping->nrshadows == 0)		return; 	invalidate_bh_lrus();	truncate_inode_pages(mapping, 0);}	EXPORT_SYMBOL(kill_bdev); /* Invalidate clean unused buffers and pagecache. */void invalidate_bdev(struct block_device *bdev){	struct address_space *mapping = bdev->bd_inode->i_mapping; 	if (mapping->nrpages == 0)		return; 	invalidate_bh_lrus();	lru_add_drain_all();	/* make sure all lru add caches are flushed */	invalidate_mapping_pages(mapping, 0, -1);	/* 99% of the time, we don't need to flush the cleancache on the bdev.	 * But, for the strange corners, lets be cautious	 */	cleancache_invalidate_inode(mapping);}EXPORT_SYMBOL(invalidate_bdev); int set_blocksize(struct block_device *bdev, int size){	/* Size must be a power of two, and between 512 and PAGE_SIZE */	if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))		return -EINVAL; 	/* Size cannot be smaller than the size supported by the device */	if (size < bdev_logical_block_size(bdev))		return -EINVAL; 	/* Don't change the size if it is same as current */	if (bdev->bd_block_size != size) {		sync_blockdev(bdev);		bdev->bd_block_size = size;		bdev->bd_inode->i_blkbits = blksize_bits(size);		kill_bdev(bdev);	}	return 0;} EXPORT_SYMBOL(set_blocksize); int sb_set_blocksize(struct super_block *sb, int size){	if (set_blocksize(sb->s_bdev, size))		return 0;	/* If we get here, we know size is power of two	 * and it's value is between 512 and PAGE_SIZE */	sb->s_blocksize = size;	sb->s_blocksize_bits = blksize_bits(size);	return sb->s_blocksize;} EXPORT_SYMBOL(sb_set_blocksize); int sb_min_blocksize(struct super_block *sb, int size){	int minsize = bdev_logical_block_size(sb->s_bdev);	if (size < minsize)		size = minsize;	return sb_set_blocksize(sb, size);} EXPORT_SYMBOL(sb_min_blocksize); static intblkdev_get_block(struct inode *inode, sector_t iblock,		struct buffer_head *bh, int create){	bh->b_bdev = I_BDEV(inode);	bh->b_blocknr = iblock;	set_buffer_mapped(bh);	return 0;} static ssize_tblkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, loff_t offset){	struct file *file = iocb->ki_filp;	struct inode *inode = file->f_mapping->host; 	if (IS_DAX(inode))		return dax_do_io(iocb, inode, iter, offset, blkdev_get_block,				NULL, DIO_SKIP_DIO_COUNT);	return __blockdev_direct_IO(iocb, inode, I_BDEV(inode), iter, offset,				    blkdev_get_block, NULL, NULL,				    DIO_SKIP_DIO_COUNT);} int __sync_blockdev(struct block_device *bdev, int wait){	if (!bdev)		return 0;	if (!wait)		return filemap_flush(bdev->bd_inode->i_mapping);	return filemap_write_and_wait(bdev->bd_inode->i_mapping);} /* * Write out and wait upon all the dirty data associated with a block * device via its mapping.  Does not take the superblock lock. */int sync_blockdev(struct block_device *bdev){	return __sync_blockdev(bdev, 1);}EXPORT_SYMBOL(sync_blockdev); /* * Write out and wait upon all dirty data associated with this * device.   Filesystem data as well as the underlying block * device.  Takes the superblock lock. */int fsync_bdev(struct block_device *bdev){	struct super_block *sb = get_super(bdev);	if (sb) {		int res = sync_filesystem(sb);		drop_super(sb);		return res;	}	return sync_blockdev(bdev);}EXPORT_SYMBOL(fsync_bdev); /** * freeze_bdev  --  lock a filesystem and force it into a consistent state * @bdev:	blockdevice to lock * * If a superblock is found on this device, we take the s_umount semaphore * on it to make sure nobody unmounts until the snapshot creation is done. * The reference counter (bd_fsfreeze_count) guarantees that only the last * unfreeze process can unfreeze the frozen filesystem actually when multiple * freeze requests arrive simultaneously. It counts up in freeze_bdev() and * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze * actually. */struct super_block *freeze_bdev(struct block_device *bdev){	struct super_block *sb;	int error = 0; 	mutex_lock(&bdev->bd_fsfreeze_mutex);	if (++bdev->bd_fsfreeze_count > 1) {		/*		 * We don't even need to grab a reference - the first call		 * to freeze_bdev grab an active reference and only the last		 * thaw_bdev drops it.		 */		sb = get_super(bdev);		drop_super(sb);		mutex_unlock(&bdev->bd_fsfreeze_mutex);		return sb;	} 	sb = get_active_super(bdev);	if (!sb)		goto out;	if (sb->s_op->freeze_super)		error = sb->s_op->freeze_super(sb);	else		error = freeze_super(sb);	if (error) {		deactivate_super(sb);		bdev->bd_fsfreeze_count--;		mutex_unlock(&bdev->bd_fsfreeze_mutex);		return ERR_PTR(error);	}	deactivate_super(sb); out:	sync_blockdev(bdev);	mutex_unlock(&bdev->bd_fsfreeze_mutex);	return sb;	/* thaw_bdev releases s->s_umount */}EXPORT_SYMBOL(freeze_bdev); /** * thaw_bdev  -- unlock filesystem * @bdev:	blockdevice to unlock * @sb:		associated superblock * * Unlocks the filesystem and marks it writeable again after freeze_bdev(). */int thaw_bdev(struct block_device *bdev, struct super_block *sb){	int error = -EINVAL; 	mutex_lock(&bdev->bd_fsfreeze_mutex);	if (!bdev->bd_fsfreeze_count)		goto out; 	error = 0;	if (--bdev->bd_fsfreeze_count > 0)		goto out; 	if (!sb)		goto out; 	if (sb->s_op->thaw_super)		error = sb->s_op->thaw_super(sb);	else		error = thaw_super(sb);	if (error) {		bdev->bd_fsfreeze_count++;		mutex_unlock(&bdev->bd_fsfreeze_mutex);		return error;	}out:	mutex_unlock(&bdev->bd_fsfreeze_mutex);	return 0;}EXPORT_SYMBOL(thaw_bdev); static int blkdev_writepage(struct page *page, struct writeback_control *wbc){	return block_write_full_page(page, blkdev_get_block, wbc);} static int blkdev_readpage(struct file * file, struct page * page){	return block_read_full_page(page, blkdev_get_block);} static int blkdev_readpages(struct file *file, struct address_space *mapping,			struct list_head *pages, unsigned nr_pages){	return mpage_readpages(mapping, pages, nr_pages, blkdev_get_block);} static int blkdev_write_begin(struct file *file, struct address_space *mapping,			loff_t pos, unsigned len, unsigned flags,			struct page **pagep, void **fsdata){	return block_write_begin(mapping, pos, len, flags, pagep,				 blkdev_get_block);} static int blkdev_write_end(struct file *file, struct address_space *mapping,			loff_t pos, unsigned len, unsigned copied,			struct page *page, void *fsdata){	int ret;	ret = block_write_end(file, mapping, pos, len, copied, page, fsdata); 	unlock_page(page);	page_cache_release(page); 	return ret;} /* * private llseek: * for a block special file file_inode(file)->i_size is zero * so we compute the size by hand (just as in block_read/write above) */static loff_t block_llseek(struct file *file, loff_t offset, int whence){	struct inode *bd_inode = file->f_mapping->host;	loff_t retval; 	mutex_lock(&bd_inode->i_mutex);	retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));	mutex_unlock(&bd_inode->i_mutex);	return retval;}	int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync){	struct inode *bd_inode = filp->f_mapping->host;	struct block_device *bdev = I_BDEV(bd_inode);	int error;		error = filemap_write_and_wait_range(filp->f_mapping, start, end);	if (error)		return error; 	/*	 * There is no need to serialise calls to blkdev_issue_flush with	 * i_mutex and doing so causes performance issues with concurrent	 * O_SYNC writers to a block device.	 */	error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);	if (error == -EOPNOTSUPP)		error = 0; 	return error;}EXPORT_SYMBOL(blkdev_fsync); /** * bdev_read_page() - Start reading a page from a block device * @bdev: The device to read the page from * @sector: The offset on the device to read the page to (need not be aligned) * @page: The page to read * * On entry, the page should be locked.  It will be unlocked when the page * has been read.  If the block driver implements rw_page synchronously, * that will be true on exit from this function, but it need not be. * * Errors returned by this function are usually "soft", eg out of memory, or * queue full; callers should try a different route to read this page rather * than propagate an error back up the stack. * * Return: negative errno if an error occurs, 0 if submission was successful. */int bdev_read_page(struct block_device *bdev, sector_t sector,			struct page *page){	const struct block_device_operations *ops = bdev->bd_disk->fops;	int result = -EOPNOTSUPP; 	if (!ops->rw_page || bdev_get_integrity(bdev))		return result; 	result = blk_queue_enter(bdev->bd_queue, GFP_KERNEL);	if (result)		return result;	result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, READ);	blk_queue_exit(bdev->bd_queue);	return result;}EXPORT_SYMBOL_GPL(bdev_read_page); /** * bdev_write_page() - Start writing a page to a block device * @bdev: The device to write the page to * @sector: The offset on the device to write the page to (need not be aligned) * @page: The page to write * @wbc: The writeback_control for the write * * On entry, the page should be locked and not currently under writeback. * On exit, if the write started successfully, the page will be unlocked and * under writeback.  If the write failed already (eg the driver failed to * queue the page to the device), the page will still be locked.  If the * caller is a ->writepage implementation, it will need to unlock the page. * * Errors returned by this function are usually "soft", eg out of memory, or * queue full; callers should try a different route to write this page rather * than propagate an error back up the stack. * * Return: negative errno if an error occurs, 0 if submission was successful. */int bdev_write_page(struct block_device *bdev, sector_t sector,			struct page *page, struct writeback_control *wbc){	int result;	int rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE;	const struct block_device_operations *ops = bdev->bd_disk->fops; 	if (!ops->rw_page || bdev_get_integrity(bdev))		return -EOPNOTSUPP;	result = blk_queue_enter(bdev->bd_queue, GFP_KERNEL);	if (result)		return result; 	set_page_writeback(page);	result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, rw);	if (result)		end_page_writeback(page);	else		unlock_page(page);	blk_queue_exit(bdev->bd_queue);	return result;}EXPORT_SYMBOL_GPL(bdev_write_page); /** * bdev_direct_access() - Get the address for directly-accessibly memory * @bdev: The device containing the memory * @sector: The offset within the device * @addr: Where to put the address of the memory * @pfn: The Page Frame Number for the memory * @size: The number of bytes requested * * If a block device is made up of directly addressable memory, this function * will tell the caller the PFN and the address of the memory.  The address * may be directly dereferenced within the kernel without the need to call * ioremap(), kmap() or similar.  The PFN is suitable for inserting into * page tables. * * Return: negative errno if an error occurs, otherwise the number of bytes * accessible at this address. */long bdev_direct_access(struct block_device *bdev, sector_t sector,			void __pmem **addr, unsigned long *pfn, long size){	long avail;	const struct block_device_operations *ops = bdev->bd_disk->fops; 	/*	 * The device driver is allowed to sleep, in order to make the	 * memory directly accessible.	 */	might_sleep(); 	if (size < 0)		return size;	if (!ops->direct_access)		return -EOPNOTSUPP;	if ((sector + DIV_ROUND_UP(size, 512)) >					part_nr_sects_read(bdev->bd_part))		return -ERANGE;	sector += get_start_sect(bdev);	if (sector % (PAGE_SIZE / 512))		return -EINVAL;	avail = ops->direct_access(bdev, sector, addr, pfn);	if (!avail)		return -ERANGE;	return min(avail, size);}EXPORT_SYMBOL_GPL(bdev_direct_access); /* * pseudo-fs */ static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);

Lines 1–500 of 1836.