2007-09-21 12:34:38

by Miklos Szeredi

[permalink] [raw]
Subject: [patch 4/5] VFS: allow filesystems to implement atomic open+truncate

From: Miklos Szeredi <[email protected]>

Add a new attribute flag ATTR_OPEN, with the meaning: "truncation was
initiated by open() due to the O_TRUNC flag".

This way filesystems wanting to implement truncation within their
->open() method can ignore such truncate requests.

This is a quick & dirty hack, but it comes for free.

Signed-off-by: Miklos Szeredi <[email protected]>
---

Index: linux/fs/namei.c
===================================================================
--- linux.orig/fs/namei.c 2007-09-21 13:44:53.000000000 +0200
+++ linux/fs/namei.c 2007-09-21 13:45:14.000000000 +0200
@@ -1656,8 +1656,10 @@ int may_open(struct nameidata *nd, int a
error = locks_verify_locked(inode);
if (!error) {
DQUOT_INIT(inode);
-
- error = do_truncate(dentry, 0, ATTR_MTIME|ATTR_CTIME, NULL);
+
+ error = do_truncate(dentry, 0,
+ ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
+ NULL);
}
put_write_access(inode);
if (error)
Index: linux/include/linux/fs.h
===================================================================
--- linux.orig/include/linux/fs.h 2007-09-21 13:45:11.000000000 +0200
+++ linux/include/linux/fs.h 2007-09-21 13:45:14.000000000 +0200
@@ -337,6 +337,7 @@ typedef void (dio_iodone_t)(struct kiocb
#define ATTR_KILL_SGID 4096
#define ATTR_FILE 8192
#define ATTR_KILL_PRIV 16384
+#define ATTR_OPEN 32768 /* Truncating from open(O_TRUNC) */

/*
* This is the Inode Attributes structure, used for notify_change(). It
@@ -1526,7 +1527,7 @@ static inline int break_lease(struct ino

/* fs/open.c */

-extern int do_truncate(struct dentry *, loff_t start, unsigned int time_attrs,
+extern int do_truncate(struct dentry *, loff_t start, unsigned int attrs,
struct file *filp);
extern long do_sys_open(int fdf, const char __user *filename, int flags,
int mode);
Index: linux/fs/open.c
===================================================================
--- linux.orig/fs/open.c 2007-09-21 13:45:04.000000000 +0200
+++ linux/fs/open.c 2007-09-21 13:45:14.000000000 +0200
@@ -194,7 +194,7 @@ out:
return error;
}

-int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
+int do_truncate(struct dentry *dentry, loff_t length, unsigned int attrs,
struct file *filp)
{
int err;
@@ -205,7 +205,7 @@ int do_truncate(struct dentry *dentry, l
return -EINVAL;

newattrs.ia_size = length;
- newattrs.ia_valid = ATTR_SIZE | time_attrs;
+ newattrs.ia_valid = ATTR_SIZE | attrs;
if (filp) {
newattrs.ia_file = filp;
newattrs.ia_valid |= ATTR_FILE;

--


2007-09-21 12:44:29

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [patch 4/5] VFS: allow filesystems to implement atomic open+truncate

On Fri, Sep 21, 2007 at 02:23:47PM +0200, Miklos Szeredi wrote:
> From: Miklos Szeredi <[email protected]>
>
> Add a new attribute flag ATTR_OPEN, with the meaning: "truncation was
> initiated by open() due to the O_TRUNC flag".
>
> This way filesystems wanting to implement truncation within their
> ->open() method can ignore such truncate requests.
>
> This is a quick & dirty hack, but it comes for free.

Fine with me as it doesn't cause any active harm, but expect this to
go away once the nfs intent mess is cleaned up and we'll get a real
method for this kind of thing.

2007-09-21 13:01:55

by Miklos Szeredi

[permalink] [raw]
Subject: Re: [patch 4/5] VFS: allow filesystems to implement atomic open+truncate

> > From: Miklos Szeredi <[email protected]>
> >
> > Add a new attribute flag ATTR_OPEN, with the meaning: "truncation was
> > initiated by open() due to the O_TRUNC flag".
> >
> > This way filesystems wanting to implement truncation within their
> > ->open() method can ignore such truncate requests.
> >
> > This is a quick & dirty hack, but it comes for free.
>
> Fine with me as it doesn't cause any active harm, but expect this to
> go away once the nfs intent mess is cleaned up and we'll get a real
> method for this kind of thing.

Sure.

Miklos

2007-09-21 18:47:22

by Andreas Dilger

[permalink] [raw]
Subject: Re: [patch 4/5] VFS: allow filesystems to implement atomic open+truncate

On Sep 21, 2007 14:23 +0200, Miklos Szeredi wrote:
> Add a new attribute flag ATTR_OPEN, with the meaning: "truncation was
> initiated by open() due to the O_TRUNC flag".
>
> This way filesystems wanting to implement truncation within their
> ->open() method can ignore such truncate requests.

This is actually something we've needed to do in Lustre for a while also.
We called it ATTR_FROM_OPEN, but I don't really mind ATTR_OPEN either -
the less patching we need to do the better.

Cheers, Andreas
--
Andreas Dilger
Principal Software Engineer
Cluster File Systems, Inc.