2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
* Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
|
|
|
|
*/
|
|
|
|
|
2006-01-11 13:17:46 -07:00
|
|
|
#include <linux/capability.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <linux/fs.h>
|
2008-02-15 15:37:46 -07:00
|
|
|
#include <linux/mount.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <linux/reiserfs_fs.h>
|
|
|
|
#include <linux/time.h>
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/smp_lock.h>
|
2006-08-29 11:06:18 -07:00
|
|
|
#include <linux/compat.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
** reiserfs_ioctl - handler for ioctl for inode
|
|
|
|
** supported commands:
|
|
|
|
** 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect
|
|
|
|
** and prevent packing file (argument arg has to be non-zero)
|
|
|
|
** 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION
|
|
|
|
** 3) That's all for a while ...
|
|
|
|
*/
|
2005-07-12 20:21:28 -07:00
|
|
|
int reiserfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
unsigned int flags;
|
2008-02-15 15:37:46 -07:00
|
|
|
int err = 0;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
switch (cmd) {
|
2005-07-12 20:21:28 -07:00
|
|
|
case REISERFS_IOC_UNPACK:
|
|
|
|
if (S_ISREG(inode->i_mode)) {
|
|
|
|
if (arg)
|
|
|
|
return reiserfs_unpack(inode, filp);
|
2005-04-16 15:20:36 -07:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
return -ENOTTY;
|
2005-07-12 20:21:28 -07:00
|
|
|
/* following two cases are taken from fs/ext2/ioctl.c by Remy
|
|
|
|
Card (card@masi.ibp.fr) */
|
2005-04-16 15:20:36 -07:00
|
|
|
case REISERFS_IOC_GETFLAGS:
|
2005-07-12 20:21:28 -07:00
|
|
|
if (!reiserfs_attrs(inode->i_sb))
|
2005-06-29 15:52:28 -07:00
|
|
|
return -ENOTTY;
|
|
|
|
|
2005-07-12 20:21:28 -07:00
|
|
|
flags = REISERFS_I(inode)->i_attrs;
|
|
|
|
i_attrs_to_sd_attrs(inode, (__u16 *) & flags);
|
|
|
|
return put_user(flags, (int __user *)arg);
|
|
|
|
case REISERFS_IOC_SETFLAGS:{
|
|
|
|
if (!reiserfs_attrs(inode->i_sb))
|
|
|
|
return -ENOTTY;
|
2005-06-29 15:52:28 -07:00
|
|
|
|
2008-02-15 15:37:46 -07:00
|
|
|
err = mnt_want_write(filp->f_path.mnt);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2008-02-15 15:37:46 -07:00
|
|
|
if (!is_owner_or_cap(inode)) {
|
|
|
|
err = -EPERM;
|
|
|
|
goto setflags_out;
|
|
|
|
}
|
|
|
|
if (get_user(flags, (int __user *)arg)) {
|
|
|
|
err = -EFAULT;
|
|
|
|
goto setflags_out;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Is it quota file? Do not allow user to mess with it
|
|
|
|
*/
|
|
|
|
if (IS_NOQUOTA(inode)) {
|
|
|
|
err = -EPERM;
|
|
|
|
goto setflags_out;
|
|
|
|
}
|
2005-07-12 20:21:28 -07:00
|
|
|
if (((flags ^ REISERFS_I(inode)->
|
|
|
|
i_attrs) & (REISERFS_IMMUTABLE_FL |
|
|
|
|
REISERFS_APPEND_FL))
|
2008-02-15 15:37:46 -07:00
|
|
|
&& !capable(CAP_LINUX_IMMUTABLE)) {
|
|
|
|
err = -EPERM;
|
|
|
|
goto setflags_out;
|
|
|
|
}
|
2005-07-12 20:21:28 -07:00
|
|
|
if ((flags & REISERFS_NOTAIL_FL) &&
|
|
|
|
S_ISREG(inode->i_mode)) {
|
2005-04-16 15:20:36 -07:00
|
|
|
int result;
|
|
|
|
|
2005-07-12 20:21:28 -07:00
|
|
|
result = reiserfs_unpack(inode, filp);
|
2008-02-15 15:37:46 -07:00
|
|
|
if (result) {
|
|
|
|
err = result;
|
|
|
|
goto setflags_out;
|
|
|
|
}
|
2005-07-12 20:21:28 -07:00
|
|
|
}
|
|
|
|
sd_attrs_to_i_attrs(flags, inode);
|
|
|
|
REISERFS_I(inode)->i_attrs = flags;
|
|
|
|
inode->i_ctime = CURRENT_TIME_SEC;
|
|
|
|
mark_inode_dirty(inode);
|
2008-02-15 15:37:46 -07:00
|
|
|
setflags_out:
|
|
|
|
mnt_drop_write(filp->f_path.mnt);
|
|
|
|
return err;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
case REISERFS_IOC_GETVERSION:
|
2005-07-12 20:21:28 -07:00
|
|
|
return put_user(inode->i_generation, (int __user *)arg);
|
2005-04-16 15:20:36 -07:00
|
|
|
case REISERFS_IOC_SETVERSION:
|
2007-07-17 02:30:08 -07:00
|
|
|
if (!is_owner_or_cap(inode))
|
2005-04-16 15:20:36 -07:00
|
|
|
return -EPERM;
|
2008-02-15 15:37:46 -07:00
|
|
|
err = mnt_want_write(filp->f_path.mnt);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
if (get_user(inode->i_generation, (int __user *)arg)) {
|
|
|
|
err = -EFAULT;
|
|
|
|
goto setversion_out;
|
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
inode->i_ctime = CURRENT_TIME_SEC;
|
|
|
|
mark_inode_dirty(inode);
|
2008-02-15 15:37:46 -07:00
|
|
|
setversion_out:
|
|
|
|
mnt_drop_write(filp->f_path.mnt);
|
|
|
|
return err;
|
2005-04-16 15:20:36 -07:00
|
|
|
default:
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-29 11:06:18 -07:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
long reiserfs_compat_ioctl(struct file *file, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
2006-12-08 03:37:33 -07:00
|
|
|
struct inode *inode = file->f_path.dentry->d_inode;
|
2006-08-29 11:06:18 -07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* These are just misnamed, they actually get/put from/to user an int */
|
|
|
|
switch (cmd) {
|
|
|
|
case REISERFS_IOC32_UNPACK:
|
|
|
|
cmd = REISERFS_IOC_UNPACK;
|
|
|
|
break;
|
|
|
|
case REISERFS_IOC32_GETFLAGS:
|
|
|
|
cmd = REISERFS_IOC_GETFLAGS;
|
|
|
|
break;
|
|
|
|
case REISERFS_IOC32_SETFLAGS:
|
|
|
|
cmd = REISERFS_IOC_SETFLAGS;
|
|
|
|
break;
|
|
|
|
case REISERFS_IOC32_GETVERSION:
|
|
|
|
cmd = REISERFS_IOC_GETVERSION;
|
|
|
|
break;
|
|
|
|
case REISERFS_IOC32_SETVERSION:
|
|
|
|
cmd = REISERFS_IOC_SETVERSION;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -ENOIOCTLCMD;
|
|
|
|
}
|
reiserfs: kill-the-BKL
This patch is an attempt to remove the Bkl based locking scheme from
reiserfs and is intended.
It is a bit inspired from an old attempt by Peter Zijlstra:
http://lkml.indiana.edu/hypermail/linux/kernel/0704.2/2174.html
The bkl is heavily used in this filesystem to prevent from
concurrent write accesses on the filesystem.
Reiserfs makes a deep use of the specific properties of the Bkl:
- It can be acqquired recursively by a same task
- It is released on the schedule() calls and reacquired when schedule() returns
The two properties above are a roadmap for the reiserfs write locking so it's
very hard to simply replace it with a common mutex.
- We need a recursive-able locking unless we want to restructure several blocks
of the code.
- We need to identify the sites where the bkl was implictly relaxed
(schedule, wait, sync, etc...) so that we can in turn release and
reacquire our new lock explicitly.
Such implicit releases of the lock are often required to let other
resources producer/consumer do their job or we can suffer unexpected
starvations or deadlocks.
So the new lock that replaces the bkl here is a per superblock mutex with a
specific property: it can be acquired recursively by a same task, like the
bkl.
For such purpose, we integrate a lock owner and a lock depth field on the
superblock information structure.
The first axis on this patch is to turn reiserfs_write_(un)lock() function
into a wrapper to manage this mutex. Also some explicit calls to
lock_kernel() have been converted to reiserfs_write_lock() helpers.
The second axis is to find the important blocking sites (schedule...(),
wait_on_buffer(), sync_dirty_buffer(), etc...) and then apply an explicit
release of the write lock on these locations before blocking. Then we can
safely wait for those who can give us resources or those who need some.
Typically this is a fight between the current writer, the reiserfs workqueue
(aka the async commiter) and the pdflush threads.
The third axis is a consequence of the second. The write lock is usually
on top of a lock dependency chain which can include the journal lock, the
flush lock or the commit lock. So it's dangerous to release and trying to
reacquire the write lock while we still hold other locks.
This is fine with the bkl:
T1 T2
lock_kernel()
mutex_lock(A)
unlock_kernel()
// do something
lock_kernel()
mutex_lock(A) -> already locked by T1
schedule() (and then unlock_kernel())
lock_kernel()
mutex_unlock(A)
....
This is not fine with a mutex:
T1 T2
mutex_lock(write)
mutex_lock(A)
mutex_unlock(write)
// do something
mutex_lock(write)
mutex_lock(A) -> already locked by T1
schedule()
mutex_lock(write) -> already locked by T2
deadlock
The solution in this patch is to provide a helper which releases the write
lock and sleep a bit if we can't lock a mutex that depend on it. It's another
simulation of the bkl behaviour.
The last axis is to locate the fs callbacks that are called with the bkl held,
according to Documentation/filesystem/Locking.
Those are:
- reiserfs_remount
- reiserfs_fill_super
- reiserfs_put_super
Reiserfs didn't need to explicitly lock because of the context of these callbacks.
But now we must take care of that with the new locking.
After this patch, reiserfs suffers from a slight performance regression (for now).
On UP, a high volume write with dd reports an average of 27 MB/s instead
of 30 MB/s without the patch applied.
Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
Reviewed-by: Ingo Molnar <mingo@elte.hu>
Cc: Jeff Mahoney <jeffm@suse.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Bron Gondwana <brong@fastmail.fm>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
LKML-Reference: <1239070789-13354-1-git-send-email-fweisbec@gmail.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-04-06 19:19:49 -07:00
|
|
|
|
|
|
|
reiserfs_write_lock(inode->i_sb);
|
2006-08-29 11:06:18 -07:00
|
|
|
ret = reiserfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
|
reiserfs: kill-the-BKL
This patch is an attempt to remove the Bkl based locking scheme from
reiserfs and is intended.
It is a bit inspired from an old attempt by Peter Zijlstra:
http://lkml.indiana.edu/hypermail/linux/kernel/0704.2/2174.html
The bkl is heavily used in this filesystem to prevent from
concurrent write accesses on the filesystem.
Reiserfs makes a deep use of the specific properties of the Bkl:
- It can be acqquired recursively by a same task
- It is released on the schedule() calls and reacquired when schedule() returns
The two properties above are a roadmap for the reiserfs write locking so it's
very hard to simply replace it with a common mutex.
- We need a recursive-able locking unless we want to restructure several blocks
of the code.
- We need to identify the sites where the bkl was implictly relaxed
(schedule, wait, sync, etc...) so that we can in turn release and
reacquire our new lock explicitly.
Such implicit releases of the lock are often required to let other
resources producer/consumer do their job or we can suffer unexpected
starvations or deadlocks.
So the new lock that replaces the bkl here is a per superblock mutex with a
specific property: it can be acquired recursively by a same task, like the
bkl.
For such purpose, we integrate a lock owner and a lock depth field on the
superblock information structure.
The first axis on this patch is to turn reiserfs_write_(un)lock() function
into a wrapper to manage this mutex. Also some explicit calls to
lock_kernel() have been converted to reiserfs_write_lock() helpers.
The second axis is to find the important blocking sites (schedule...(),
wait_on_buffer(), sync_dirty_buffer(), etc...) and then apply an explicit
release of the write lock on these locations before blocking. Then we can
safely wait for those who can give us resources or those who need some.
Typically this is a fight between the current writer, the reiserfs workqueue
(aka the async commiter) and the pdflush threads.
The third axis is a consequence of the second. The write lock is usually
on top of a lock dependency chain which can include the journal lock, the
flush lock or the commit lock. So it's dangerous to release and trying to
reacquire the write lock while we still hold other locks.
This is fine with the bkl:
T1 T2
lock_kernel()
mutex_lock(A)
unlock_kernel()
// do something
lock_kernel()
mutex_lock(A) -> already locked by T1
schedule() (and then unlock_kernel())
lock_kernel()
mutex_unlock(A)
....
This is not fine with a mutex:
T1 T2
mutex_lock(write)
mutex_lock(A)
mutex_unlock(write)
// do something
mutex_lock(write)
mutex_lock(A) -> already locked by T1
schedule()
mutex_lock(write) -> already locked by T2
deadlock
The solution in this patch is to provide a helper which releases the write
lock and sleep a bit if we can't lock a mutex that depend on it. It's another
simulation of the bkl behaviour.
The last axis is to locate the fs callbacks that are called with the bkl held,
according to Documentation/filesystem/Locking.
Those are:
- reiserfs_remount
- reiserfs_fill_super
- reiserfs_put_super
Reiserfs didn't need to explicitly lock because of the context of these callbacks.
But now we must take care of that with the new locking.
After this patch, reiserfs suffers from a slight performance regression (for now).
On UP, a high volume write with dd reports an average of 27 MB/s instead
of 30 MB/s without the patch applied.
Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
Reviewed-by: Ingo Molnar <mingo@elte.hu>
Cc: Jeff Mahoney <jeffm@suse.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Bron Gondwana <brong@fastmail.fm>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
LKML-Reference: <1239070789-13354-1-git-send-email-fweisbec@gmail.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-04-06 19:19:49 -07:00
|
|
|
reiserfs_write_unlock(inode->i_sb);
|
|
|
|
|
2006-08-29 11:06:18 -07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-16 01:25:14 -07:00
|
|
|
int reiserfs_commit_write(struct file *f, struct page *page,
|
|
|
|
unsigned from, unsigned to);
|
|
|
|
int reiserfs_prepare_write(struct file *f, struct page *page,
|
|
|
|
unsigned from, unsigned to);
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
** reiserfs_unpack
|
|
|
|
** Function try to convert tail from direct item into indirect.
|
|
|
|
** It set up nopack attribute in the REISERFS_I(inode)->nopack
|
|
|
|
*/
|
2008-04-28 02:16:23 -07:00
|
|
|
int reiserfs_unpack(struct inode *inode, struct file *filp)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2005-07-12 20:21:28 -07:00
|
|
|
int retval = 0;
|
|
|
|
int index;
|
|
|
|
struct page *page;
|
|
|
|
struct address_space *mapping;
|
|
|
|
unsigned long write_from;
|
|
|
|
unsigned long blocksize = inode->i_sb->s_blocksize;
|
|
|
|
|
|
|
|
if (inode->i_size == 0) {
|
|
|
|
REISERFS_I(inode)->i_flags |= i_nopack_mask;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* ioctl already done */
|
|
|
|
if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we need to make sure nobody is changing the file size beneath
|
|
|
|
** us
|
|
|
|
*/
|
2006-01-09 16:59:24 -07:00
|
|
|
mutex_lock(&inode->i_mutex);
|
2006-08-05 12:15:08 -07:00
|
|
|
reiserfs_write_lock(inode->i_sb);
|
2005-07-12 20:21:28 -07:00
|
|
|
|
|
|
|
write_from = inode->i_size & (blocksize - 1);
|
|
|
|
/* if we are on a block boundary, we are already unpacked. */
|
|
|
|
if (write_from == 0) {
|
|
|
|
REISERFS_I(inode)->i_flags |= i_nopack_mask;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we unpack by finding the page with the tail, and calling
|
2009-03-30 11:02:44 -07:00
|
|
|
** reiserfs_prepare_write on that page. This will force a
|
2005-07-12 20:21:28 -07:00
|
|
|
** reiserfs_get_block to unpack the tail for us.
|
|
|
|
*/
|
|
|
|
index = inode->i_size >> PAGE_CACHE_SHIFT;
|
|
|
|
mapping = inode->i_mapping;
|
|
|
|
page = grab_cache_page(mapping, index);
|
|
|
|
retval = -ENOMEM;
|
|
|
|
if (!page) {
|
|
|
|
goto out;
|
|
|
|
}
|
2007-10-16 01:25:14 -07:00
|
|
|
retval = reiserfs_prepare_write(NULL, page, write_from, write_from);
|
2005-07-12 20:21:28 -07:00
|
|
|
if (retval)
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
/* conversion can change page contents, must flush */
|
|
|
|
flush_dcache_page(page);
|
2007-10-16 01:25:14 -07:00
|
|
|
retval = reiserfs_commit_write(NULL, page, write_from, write_from);
|
2005-04-16 15:20:36 -07:00
|
|
|
REISERFS_I(inode)->i_flags |= i_nopack_mask;
|
2005-07-12 20:21:28 -07:00
|
|
|
|
|
|
|
out_unlock:
|
|
|
|
unlock_page(page);
|
|
|
|
page_cache_release(page);
|
|
|
|
|
|
|
|
out:
|
2006-01-09 16:59:24 -07:00
|
|
|
mutex_unlock(&inode->i_mutex);
|
2005-07-12 20:21:28 -07:00
|
|
|
reiserfs_write_unlock(inode->i_sb);
|
|
|
|
return retval;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|