Skip to contentSkip to conversation

fs: add set_flags wrapper

PATCH New Dmitry Monakhov 1 participant 3 messages Dec 14, 2010

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 fs: add set_flags wrapper

Direct f_flags manipulation may be not suitable for some filesytems. Let's introduce new ->set_flags() callback for that purpose. This callback is responsible for flags check, so ->check_flags() no longer needed.

---
 fs/bad_inode.c     |    4 ++--
 fs/fcntl.c         |   14 +++++++-------
 fs/nfs/file.c      |   12 ++++++++----
 include/linux/fs.h |    2 +-
 4 files changed, 18 insertions(+), 14 deletions(-)
fs/bad_inode.c +2 -2
index f024d8a..97933cd 100644
@@ -120,7 +120,7 @@ static unsigned long bad_file_get_unmapped_area(struct file *file,
 	return -EIO;
 }
 
-static int bad_file_check_flags(int flags)
+static int bad_file_set_flags(struct file *file, int flags)
 {
 	return -EIO;
 }
@@ -165,7 +165,7 @@ static const struct file_operations bad_file_ops =
 	.lock		= bad_file_lock,
 	.sendpage	= bad_file_sendpage,
 	.get_unmapped_area = bad_file_get_unmapped_area,
-	.check_flags	= bad_file_check_flags,
+	.set_flags	= bad_file_set_flags,
 	.flock		= bad_file_flock,
 	.splice_write	= bad_file_splice_write,
 	.splice_read	= bad_file_splice_read,
fs/fcntl.c +7 -7
index f8cc34f..76b77ac 100644
@@ -173,10 +173,6 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
 				return -EINVAL;
 	}
 
-	if (filp->f_op && filp->f_op->check_flags)
-		error = filp->f_op->check_flags(arg);
-	if (error)
-		return error;
 
 	/*
 	 * ->fasync() is responsible for setting the FASYNC bit.
@@ -189,10 +185,14 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
 		if (error > 0)
 			error = 0;
 	}
-	spin_lock(&filp->f_lock);
-	filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
-	spin_unlock(&filp->f_lock);
 
+	if (filp->f_op && filp->f_op->set_flags)
+		error = filp->f_op->set_flags(filp, arg);
+	else {
+		spin_lock(&filp->f_lock);
+		filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
+		spin_unlock(&filp->f_lock);
+	}
  out:
 	return error;
 }
fs/nfs/file.c +8 -4
index 05bf3c0..32ac63a 100644
@@ -55,7 +55,7 @@ static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov,
 				unsigned long nr_segs, loff_t pos);
 static int  nfs_file_flush(struct file *, fl_owner_t id);
 static int  nfs_file_fsync(struct file *, int datasync);
-static int nfs_check_flags(int flags);
+static int nfs_set_flags(struct file *file, int flags);
 static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl);
 static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl);
 static int nfs_setlease(struct file *file, long arg, struct file_lock **fl);
@@ -77,7 +77,7 @@ const struct file_operations nfs_file_operations = {
 	.flock		= nfs_flock,
 	.splice_read	= nfs_file_splice_read,
 	.splice_write	= nfs_file_splice_write,
-	.check_flags	= nfs_check_flags,
+	.set_flags	= nfs_set_flags,
 	.setlease	= nfs_setlease,
 };
 
@@ -104,11 +104,15 @@ const struct inode_operations nfs3_file_inode_operations = {
 # define IS_SWAPFILE(inode)	(0)
 #endif
 
-static int nfs_check_flags(int flags)
+#define NFS_FL_MASK (O_NONBLOCK | O_NDELAY | O_NOATIME)
+static int nfs_set_flags(struct file * filp, int flags)
 {
 	if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
 		return -EINVAL;
 
+	spin_lock(&filp->f_lock);
+	filp->f_flags = (flags & NFS_FL_MASK) | (filp->f_flags & ~NFS_FL_MASK);
+	spin_unlock(&filp->f_lock);
 	return 0;
 }
 
@@ -125,7 +129,7 @@ nfs_file_open(struct inode *inode, struct file *filp)
 			filp->f_path.dentry->d_name.name);
 
 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
-	res = nfs_check_flags(filp->f_flags);
+	res = nfs_set_flags(filp, filp->f_flags);
 	if (res)
 		return res;
 
include/linux/fs.h +1 -1
index 6708fef..b176d11 100644
@@ -1517,7 +1517,7 @@ struct file_operations {
 	int (*lock) (struct file *, int, struct file_lock *);
 	ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
 	unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
-	int (*check_flags)(int);
+	int (*set_flags)(struct file *, int);
 	int (*flock) (struct file *, int, struct file_lock *);
 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
 	ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
Signature · 1 line
1.7.2.3
#1 lore raw

PATCH 2/3 fs: add fadvise file_operation

sys_fadvise result in direct f_mode modification, which may be not suitable for some unusual filesytems where file mode invariant is more complex. In order to support such filesystems we have to delegate fadvise logic to filesystem layer.

---
 include/linux/fadvise.h |    3 ++
 include/linux/fs.h      |    1 +
 mm/fadvise.c            |   56 +++++++++++++++++++++++++++++++---------------
 3 files changed, 42 insertions(+), 18 deletions(-)
include/linux/fadvise.h +3 -0
index e8e7471..8389378 100644
@@ -18,4 +18,7 @@
 #define POSIX_FADV_NOREUSE	5 /* Data will be accessed once.  */
 #endif
 
+#ifdef __KERNEL__
+extern int generic_fadvise(struct file *file, loff_t off, loff_t len, int adv);
+#endif
 #endif	/* FADVISE_H_INCLUDED */
include/linux/fs.h +1 -0
index b176d11..63785d3 100644
@@ -1522,6 +1522,7 @@ struct file_operations {
 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
 	ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
 	int (*setlease)(struct file *, long, struct file_lock **);
+	int (*fadvise)(struct file* file, loff_t offset, loff_t len, int advice);
 };
 
 struct inode_operations {
mm/fadvise.c +38 -18

90 lines not on this page · raw message

Signature · 1 line
1.7.2.3
#2 lore raw

PATCH 3/3 ecryptfs: add fadvise/set_flags calbacks

In reply to Dmitry Monakhov
---
 fs/ecryptfs/file.c |   67 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 67 insertions(+), 0 deletions(-)
fs/ecryptfs/file.c +67 -0
index 91da029..24149ec 100644
@@ -31,6 +31,7 @@
 #include <linux/security.h>
 #include <linux/compat.h>
 #include <linux/fs_stack.h>
+#include <linux/fadvise.h>
 #include "ecryptfs_kernel.h"
 
 /**
@@ -317,6 +318,68 @@ ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 }
 #endif
 
+static int
+ecryptfs_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
+{
+	struct file *lower_file = NULL;
+	long rc = 0;
+
+	if (ecryptfs_file_to_private(file))
+		lower_file = ecryptfs_file_to_lower(file);
+
+	if (!lower_file || !lower_file->f_op)
+		return rc;
+
+	if (lower_file->f_op && lower_file->f_op->fadvise)
+		rc = lower_file->f_op->fadvise(lower_file, offset, len, advice);
+	else
+		rc = generic_fadvise(lower_file, offset, len, advice);
+	if (!rc)
+		generic_fadvise(file, offset, len, advice);
+
+	return rc;
+}
+
+#define ECRYPTFS_FL_MASK (O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
+static int ecryptfs_set_flags(struct file *file, int flags)
+{
+	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
+	struct dentry *ecryptfs_dentry = file->f_path.dentry;
+	struct file *lower_file = NULL;
+	int rc = 0;
+	mount_crypt_stat = &ecryptfs_superblock_to_private(
+		ecryptfs_dentry->d_sb)->mount_crypt_stat;
+	if ((mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
+	    && (flags & O_APPEND)) {
+		printk(KERN_WARNING "Mount has encrypted view enabled; "
+		       "files may only be read\n");
+		rc = -EPERM;
+		goto out;
+	}
+	if (ecryptfs_file_to_private(file))
+		lower_file = ecryptfs_file_to_lower(file);
+	if (!lower_file)
+		goto out;
+
+	spin_lock(&file->f_lock);
+	if (lower_file->f_op && lower_file->f_op->set_flags)
+		rc = lower_file->f_op->set_flags(lower_file, flags);
+	else {
+		spin_lock(&lower_file->f_lock);
+		lower_file->f_flags = (flags & ECRYPTFS_FL_MASK) |
+			(lower_file->f_flags & ~ECRYPTFS_FL_MASK);
+		spin_unlock(&lower_file->f_lock);
+	}
+	if (!rc)
+		file->f_flags = (flags & ECRYPTFS_FL_MASK) |
+			(file->f_flags & ~ECRYPTFS_FL_MASK);
+	spin_unlock(&file->f_lock);
+
+out:
+	return rc;
+	
+
+}
 const struct file_operations ecryptfs_dir_fops = {
 	.readdir = ecryptfs_readdir,
 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
@@ -330,6 +393,8 @@ const struct file_operations ecryptfs_dir_fops = {
 	.fasync = ecryptfs_fasync,
 	.splice_read = generic_file_splice_read,
 	.llseek = default_llseek,
+	.set_flags = ecryptfs_set_flags,
+	.fadvise = ecryptfs_fadvise,
 };
 
 const struct file_operations ecryptfs_main_fops = {
@@ -350,4 +415,6 @@ const struct file_operations ecryptfs_main_fops = {
 	.fsync = ecryptfs_fsync,
 	.fasync = ecryptfs_fasync,
 	.splice_read = generic_file_splice_read,
+	.set_flags = ecryptfs_set_flags,
+	.fadvise = ecryptfs_fadvise,
 };
Signature · 1 line
1.7.2.3