Skip to content

Commit

Permalink
ocfs2: convert to fileattr
Browse files Browse the repository at this point in the history
Use the fileattr API to let the VFS handle locking, permission checking and
conversion.

Signed-off-by: Miklos Szeredi <[email protected]>
Cc: Joel Becker <[email protected]>
  • Loading branch information
Miklos Szeredi committed Apr 12, 2021
1 parent 7c7c436 commit 2b5f52c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 48 deletions.
2 changes: 2 additions & 0 deletions fs/ocfs2/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -2645,6 +2645,8 @@ const struct inode_operations ocfs2_file_iops = {
.fiemap = ocfs2_fiemap,
.get_acl = ocfs2_iop_get_acl,
.set_acl = ocfs2_iop_set_acl,
.fileattr_get = ocfs2_fileattr_get,
.fileattr_set = ocfs2_fileattr_set,
};

const struct inode_operations ocfs2_special_file_iops = {
Expand Down
59 changes: 19 additions & 40 deletions fs/ocfs2/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <linux/mount.h>
#include <linux/blkdev.h>
#include <linux/compat.h>
#include <linux/fileattr.h>

#include <cluster/masklog.h>

Expand Down Expand Up @@ -61,8 +62,10 @@ static inline int o2info_coherent(struct ocfs2_info_request *req)
return (!(req->ir_flags & OCFS2_INFO_FL_NON_COHERENT));
}

static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
int ocfs2_fileattr_get(struct dentry *dentry, struct fileattr *fa)
{
struct inode *inode = d_inode(dentry);
unsigned int flags;
int status;

status = ocfs2_inode_lock(inode, NULL, 0);
Expand All @@ -71,43 +74,46 @@ static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
return status;
}
ocfs2_get_inode_flags(OCFS2_I(inode));
*flags = OCFS2_I(inode)->ip_attr;
flags = OCFS2_I(inode)->ip_attr;
ocfs2_inode_unlock(inode, 0);

fileattr_fill_flags(fa, flags & OCFS2_FL_VISIBLE);

return status;
}

static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
unsigned mask)
int ocfs2_fileattr_set(struct user_namespace *mnt_userns,
struct dentry *dentry, struct fileattr *fa)
{
struct inode *inode = d_inode(dentry);
unsigned int flags = fa->flags;
struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
handle_t *handle = NULL;
struct buffer_head *bh = NULL;
unsigned oldflags;
int status;

inode_lock(inode);
if (fileattr_has_fsx(fa))
return -EOPNOTSUPP;

status = ocfs2_inode_lock(inode, &bh, 1);
if (status < 0) {
mlog_errno(status);
goto bail;
}

status = -EACCES;
if (!inode_owner_or_capable(&init_user_ns, inode))
goto bail_unlock;

if (!S_ISDIR(inode->i_mode))
flags &= ~OCFS2_DIRSYNC_FL;

oldflags = ocfs2_inode->ip_attr;
flags = flags & mask;
flags |= oldflags & ~mask;
flags = flags & OCFS2_FL_MODIFIABLE;
flags |= oldflags & ~OCFS2_FL_MODIFIABLE;

status = vfs_ioc_setflags_prepare(inode, oldflags, flags);
if (status)
/* Check already done by VFS, but repeat with ocfs lock */
status = -EPERM;
if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL) &&
!capable(CAP_LINUX_IMMUTABLE))
goto bail_unlock;

handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Expand All @@ -129,8 +135,6 @@ static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
bail_unlock:
ocfs2_inode_unlock(inode, 1);
bail:
inode_unlock(inode);

brelse(bh);

return status;
Expand Down Expand Up @@ -836,7 +840,6 @@ static int ocfs2_info_handle(struct inode *inode, struct ocfs2_info *info,
long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct inode *inode = file_inode(filp);
unsigned int flags;
int new_clusters;
int status;
struct ocfs2_space_resv sr;
Expand All @@ -849,24 +852,6 @@ long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
void __user *argp = (void __user *)arg;

switch (cmd) {
case OCFS2_IOC_GETFLAGS:
status = ocfs2_get_inode_attr(inode, &flags);
if (status < 0)
return status;

flags &= OCFS2_FL_VISIBLE;
return put_user(flags, (int __user *) arg);
case OCFS2_IOC_SETFLAGS:
if (get_user(flags, (int __user *) arg))
return -EFAULT;

status = mnt_want_write_file(filp);
if (status)
return status;
status = ocfs2_set_inode_attr(inode, flags,
OCFS2_FL_MODIFIABLE);
mnt_drop_write_file(filp);
return status;
case OCFS2_IOC_RESVSP:
case OCFS2_IOC_RESVSP64:
case OCFS2_IOC_UNRESVSP:
Expand Down Expand Up @@ -959,12 +944,6 @@ long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
void __user *argp = (void __user *)arg;

switch (cmd) {
case OCFS2_IOC32_GETFLAGS:
cmd = OCFS2_IOC_GETFLAGS;
break;
case OCFS2_IOC32_SETFLAGS:
cmd = OCFS2_IOC_SETFLAGS;
break;
case OCFS2_IOC_RESVSP:
case OCFS2_IOC_RESVSP64:
case OCFS2_IOC_UNRESVSP:
Expand Down
3 changes: 3 additions & 0 deletions fs/ocfs2/ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#ifndef OCFS2_IOCTL_PROTO_H
#define OCFS2_IOCTL_PROTO_H

int ocfs2_fileattr_get(struct dentry *dentry, struct fileattr *fa);
int ocfs2_fileattr_set(struct user_namespace *mnt_userns,
struct dentry *dentry, struct fileattr *fa);
long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg);

Expand Down
3 changes: 3 additions & 0 deletions fs/ocfs2/namei.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "xattr.h"
#include "acl.h"
#include "ocfs2_trace.h"
#include "ioctl.h"

#include "buffer_head_io.h"

Expand Down Expand Up @@ -2918,4 +2919,6 @@ const struct inode_operations ocfs2_dir_iops = {
.fiemap = ocfs2_fiemap,
.get_acl = ocfs2_iop_get_acl,
.set_acl = ocfs2_iop_set_acl,
.fileattr_get = ocfs2_fileattr_get,
.fileattr_set = ocfs2_fileattr_set,
};
8 changes: 0 additions & 8 deletions fs/ocfs2/ocfs2_ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@
#ifndef OCFS2_IOCTL_H
#define OCFS2_IOCTL_H

/*
* ioctl commands
*/
#define OCFS2_IOC_GETFLAGS FS_IOC_GETFLAGS
#define OCFS2_IOC_SETFLAGS FS_IOC_SETFLAGS
#define OCFS2_IOC32_GETFLAGS FS_IOC32_GETFLAGS
#define OCFS2_IOC32_SETFLAGS FS_IOC32_SETFLAGS

/*
* Space reservation / allocation / free ioctls and argument structure
* are designed to be compatible with XFS.
Expand Down

0 comments on commit 2b5f52c

Please sign in to comment.