Skip to content

Commit

Permalink
[PATCH] BLOCK: Move functions out of buffer code [try #6]
Browse files Browse the repository at this point in the history
Move some functions out of the buffering code that aren't strictly buffering
specific.  This is a precursor to being able to disable the block layer.

 (*) Moved some stuff out of fs/buffer.c:

     (*) The file sync and general sync stuff moved to fs/sync.c.

     (*) The superblock sync stuff moved to fs/super.c.

     (*) do_invalidatepage() moved to mm/truncate.c.

     (*) try_to_release_page() moved to mm/filemap.c.

 (*) Moved some related declarations between header files:

     (*) declarations for do_invalidatepage() and try_to_release_page() moved
     	 to linux/mm.h.

     (*) __set_page_dirty_buffers() moved to linux/buffer_head.h.

Signed-Off-By: David Howells <dhowells@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
  • Loading branch information
David Howells authored and Jens Axboe committed Sep 30, 2006
1 parent 4090959 commit cf9a2ae
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 177 deletions.
174 changes: 0 additions & 174 deletions fs/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,31 +159,6 @@ int sync_blockdev(struct block_device *bdev)
}
EXPORT_SYMBOL(sync_blockdev);

static void __fsync_super(struct super_block *sb)
{
sync_inodes_sb(sb, 0);
DQUOT_SYNC(sb);
lock_super(sb);
if (sb->s_dirt && sb->s_op->write_super)
sb->s_op->write_super(sb);
unlock_super(sb);
if (sb->s_op->sync_fs)
sb->s_op->sync_fs(sb, 1);
sync_blockdev(sb->s_bdev);
sync_inodes_sb(sb, 1);
}

/*
* Write out and wait upon all dirty data associated with this
* superblock. Filesystem data as well as the underlying block
* device. Takes the superblock lock.
*/
int fsync_super(struct super_block *sb)
{
__fsync_super(sb);
return sync_blockdev(sb->s_bdev);
}

/*
* Write out and wait upon all dirty data associated with this
* device. Filesystem data as well as the underlying block
Expand Down Expand Up @@ -259,118 +234,6 @@ void thaw_bdev(struct block_device *bdev, struct super_block *sb)
}
EXPORT_SYMBOL(thaw_bdev);

/*
* sync everything. Start out by waking pdflush, because that writes back
* all queues in parallel.
*/
static void do_sync(unsigned long wait)
{
wakeup_pdflush(0);
sync_inodes(0); /* All mappings, inodes and their blockdevs */
DQUOT_SYNC(NULL);
sync_supers(); /* Write the superblocks */
sync_filesystems(0); /* Start syncing the filesystems */
sync_filesystems(wait); /* Waitingly sync the filesystems */
sync_inodes(wait); /* Mappings, inodes and blockdevs, again. */
if (!wait)
printk("Emergency Sync complete\n");
if (unlikely(laptop_mode))
laptop_sync_completion();
}

asmlinkage long sys_sync(void)
{
do_sync(1);
return 0;
}

void emergency_sync(void)
{
pdflush_operation(do_sync, 0);
}

/*
* Generic function to fsync a file.
*
* filp may be NULL if called via the msync of a vma.
*/

int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
{
struct inode * inode = dentry->d_inode;
struct super_block * sb;
int ret, err;

/* sync the inode to buffers */
ret = write_inode_now(inode, 0);

/* sync the superblock to buffers */
sb = inode->i_sb;
lock_super(sb);
if (sb->s_op->write_super)
sb->s_op->write_super(sb);
unlock_super(sb);

/* .. finally sync the buffers to disk */
err = sync_blockdev(sb->s_bdev);
if (!ret)
ret = err;
return ret;
}

long do_fsync(struct file *file, int datasync)
{
int ret;
int err;
struct address_space *mapping = file->f_mapping;

if (!file->f_op || !file->f_op->fsync) {
/* Why? We can still call filemap_fdatawrite */
ret = -EINVAL;
goto out;
}

ret = filemap_fdatawrite(mapping);

/*
* We need to protect against concurrent writers, which could cause
* livelocks in fsync_buffers_list().
*/
mutex_lock(&mapping->host->i_mutex);
err = file->f_op->fsync(file, file->f_dentry, datasync);
if (!ret)
ret = err;
mutex_unlock(&mapping->host->i_mutex);
err = filemap_fdatawait(mapping);
if (!ret)
ret = err;
out:
return ret;
}

static long __do_fsync(unsigned int fd, int datasync)
{
struct file *file;
int ret = -EBADF;

file = fget(fd);
if (file) {
ret = do_fsync(file, datasync);
fput(file);
}
return ret;
}

asmlinkage long sys_fsync(unsigned int fd)
{
return __do_fsync(fd, 0);
}

asmlinkage long sys_fdatasync(unsigned int fd)
{
return __do_fsync(fd, 1);
}

/*
* Various filesystems appear to want __find_get_block to be non-blocking.
* But it's the page lock which protects the buffers. To get around this,
Expand Down Expand Up @@ -1550,35 +1413,6 @@ static void discard_buffer(struct buffer_head * bh)
unlock_buffer(bh);
}

/**
* try_to_release_page() - release old fs-specific metadata on a page
*
* @page: the page which the kernel is trying to free
* @gfp_mask: memory allocation flags (and I/O mode)
*
* The address_space is to try to release any data against the page
* (presumably at page->private). If the release was successful, return `1'.
* Otherwise return zero.
*
* The @gfp_mask argument specifies whether I/O may be performed to release
* this page (__GFP_IO), and whether the call may block (__GFP_WAIT).
*
* NOTE: @gfp_mask may go away, and this function may become non-blocking.
*/
int try_to_release_page(struct page *page, gfp_t gfp_mask)
{
struct address_space * const mapping = page->mapping;

BUG_ON(!PageLocked(page));
if (PageWriteback(page))
return 0;

if (mapping && mapping->a_ops->releasepage)
return mapping->a_ops->releasepage(page, gfp_mask);
return try_to_free_buffers(page);
}
EXPORT_SYMBOL(try_to_release_page);

/**
* block_invalidatepage - invalidate part of all of a buffer-backed page
*
Expand Down Expand Up @@ -1630,14 +1464,6 @@ void block_invalidatepage(struct page *page, unsigned long offset)
}
EXPORT_SYMBOL(block_invalidatepage);

void do_invalidatepage(struct page *page, unsigned long offset)
{
void (*invalidatepage)(struct page *, unsigned long);
invalidatepage = page->mapping->a_ops->invalidatepage ? :
block_invalidatepage;
(*invalidatepage)(page, offset);
}

/*
* We attach and possibly dirty the buffers atomically wrt
* __set_page_dirty_buffers() via private_lock. try_to_free_buffers
Expand Down
31 changes: 31 additions & 0 deletions fs/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,37 @@ static int grab_super(struct super_block *s) __releases(sb_lock)
return 0;
}

/*
* Write out and wait upon all dirty data associated with this
* superblock. Filesystem data as well as the underlying block
* device. Takes the superblock lock. Requires a second blkdev
* flush by the caller to complete the operation.
*/
void __fsync_super(struct super_block *sb)
{
sync_inodes_sb(sb, 0);
DQUOT_SYNC(sb);
lock_super(sb);
if (sb->s_dirt && sb->s_op->write_super)
sb->s_op->write_super(sb);
unlock_super(sb);
if (sb->s_op->sync_fs)
sb->s_op->sync_fs(sb, 1);
sync_blockdev(sb->s_bdev);
sync_inodes_sb(sb, 1);
}

/*
* Write out and wait upon all dirty data associated with this
* superblock. Filesystem data as well as the underlying block
* device. Takes the superblock lock.
*/
int fsync_super(struct super_block *sb)
{
__fsync_super(sb);
return sync_blockdev(sb->s_bdev);
}

/**
* generic_shutdown_super - common helper for ->kill_sb()
* @sb: superblock to kill
Expand Down
113 changes: 113 additions & 0 deletions fs/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,123 @@
#include <linux/syscalls.h>
#include <linux/linkage.h>
#include <linux/pagemap.h>
#include <linux/quotaops.h>
#include <linux/buffer_head.h>

#define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
SYNC_FILE_RANGE_WAIT_AFTER)

/*
* sync everything. Start out by waking pdflush, because that writes back
* all queues in parallel.
*/
static void do_sync(unsigned long wait)
{
wakeup_pdflush(0);
sync_inodes(0); /* All mappings, inodes and their blockdevs */
DQUOT_SYNC(NULL);
sync_supers(); /* Write the superblocks */
sync_filesystems(0); /* Start syncing the filesystems */
sync_filesystems(wait); /* Waitingly sync the filesystems */
sync_inodes(wait); /* Mappings, inodes and blockdevs, again. */
if (!wait)
printk("Emergency Sync complete\n");
if (unlikely(laptop_mode))
laptop_sync_completion();
}

asmlinkage long sys_sync(void)
{
do_sync(1);
return 0;
}

void emergency_sync(void)
{
pdflush_operation(do_sync, 0);
}

/*
* Generic function to fsync a file.
*
* filp may be NULL if called via the msync of a vma.
*/
int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
{
struct inode * inode = dentry->d_inode;
struct super_block * sb;
int ret, err;

/* sync the inode to buffers */
ret = write_inode_now(inode, 0);

/* sync the superblock to buffers */
sb = inode->i_sb;
lock_super(sb);
if (sb->s_op->write_super)
sb->s_op->write_super(sb);
unlock_super(sb);

/* .. finally sync the buffers to disk */
err = sync_blockdev(sb->s_bdev);
if (!ret)
ret = err;
return ret;
}

long do_fsync(struct file *file, int datasync)
{
int ret;
int err;
struct address_space *mapping = file->f_mapping;

if (!file->f_op || !file->f_op->fsync) {
/* Why? We can still call filemap_fdatawrite */
ret = -EINVAL;
goto out;
}

ret = filemap_fdatawrite(mapping);

/*
* We need to protect against concurrent writers, which could cause
* livelocks in fsync_buffers_list().
*/
mutex_lock(&mapping->host->i_mutex);
err = file->f_op->fsync(file, file->f_dentry, datasync);
if (!ret)
ret = err;
mutex_unlock(&mapping->host->i_mutex);
err = filemap_fdatawait(mapping);
if (!ret)
ret = err;
out:
return ret;
}

static long __do_fsync(unsigned int fd, int datasync)
{
struct file *file;
int ret = -EBADF;

file = fget(fd);
if (file) {
ret = do_fsync(file, datasync);
fput(file);
}
return ret;
}

asmlinkage long sys_fsync(unsigned int fd)
{
return __do_fsync(fd, 0);
}

asmlinkage long sys_fdatasync(unsigned int fd)
{
return __do_fsync(fd, 1);
}

/*
* sys_sync_file_range() permits finely controlled syncing over a segment of
* a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
Expand Down
3 changes: 1 addition & 2 deletions include/linux/buffer_head.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,7 @@ extern int buffer_heads_over_limit;
* Generic address_space_operations implementations for buffer_head-backed
* address_spaces.
*/
int try_to_release_page(struct page * page, gfp_t gfp_mask);
void block_invalidatepage(struct page *page, unsigned long offset);
void do_invalidatepage(struct page *page, unsigned long offset);
int block_write_full_page(struct page *page, get_block_t *get_block,
struct writeback_control *wbc);
int block_read_full_page(struct page*, get_block_t*);
Expand Down Expand Up @@ -302,4 +300,5 @@ static inline void lock_buffer(struct buffer_head *bh)
__lock_buffer(bh);
}

extern int __set_page_dirty_buffers(struct page *page);
#endif /* _LINUX_BUFFER_HEAD_H */
1 change: 1 addition & 0 deletions include/linux/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,7 @@ extern int __filemap_fdatawrite_range(struct address_space *mapping,
extern long do_fsync(struct file *file, int datasync);
extern void sync_supers(void);
extern void sync_filesystems(int wait);
extern void __fsync_super(struct super_block *sb);
extern void emergency_sync(void);
extern void emergency_remount(void);
extern int do_remount_sb(struct super_block *sb, int flags,
Expand Down
4 changes: 3 additions & 1 deletion include/linux/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,9 @@ int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, unsigned long
int len, int write, int force, struct page **pages, struct vm_area_struct **vmas);
void print_bad_pte(struct vm_area_struct *, pte_t, unsigned long);

int __set_page_dirty_buffers(struct page *page);
extern int try_to_release_page(struct page * page, gfp_t gfp_mask);
extern void do_invalidatepage(struct page *page, unsigned long offset);

int __set_page_dirty_nobuffers(struct page *page);
int redirty_page_for_writepage(struct writeback_control *wbc,
struct page *page);
Expand Down
Loading

0 comments on commit cf9a2ae

Please sign in to comment.