From: Miklos Szeredi <miklos@szeredi.hu>
To: akpm@linux-foundation.org
Cc: hch@infradead.org, viro@ZenIV.linux.org.uk,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
Ulrich Drepper <drepper@redhat.com>
Subject: [patch 13/24] vfs: utimes cleanup
Date: Tue, 06 May 2008 11:13:40 +0200 [thread overview]
Message-ID: <20080506091422.705393283@szeredi.hu> (raw)
In-Reply-To: <20080506091327.259950960@szeredi.hu>
[-- Attachment #1: utimes_cleanup.patch --]
[-- Type: text/plain, Size: 7745 bytes --]
From: Miklos Szeredi <mszeredi@suse.cz>
Untange the mess that is do_utimes()
Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
CC: Ulrich Drepper <drepper@redhat.com>
---
fs/compat.c | 4 -
fs/utimes.c | 197 +++++++++++++++++++++++++++++++++++++-----------------------
2 files changed, 124 insertions(+), 77 deletions(-)
Index: linux-2.6/fs/utimes.c
===================================================================
--- linux-2.6.orig/fs/utimes.c 2008-05-06 11:04:36.000000000 +0200
+++ linux-2.6/fs/utimes.c 2008-05-06 11:04:37.000000000 +0200
@@ -53,54 +53,10 @@ static bool nsec_valid(long nsec)
return nsec >= 0 && nsec <= 999999999;
}
-/* If times==NULL, set access and modification to current time,
- * must be owner or have write permission.
- * Else, update from *times, must be owner or super user.
- */
-long do_utimes(int dfd, char __user *filename, struct timespec *times, int flags)
+static int utimes_common(struct path *path, struct timespec *times)
{
int error;
- struct nameidata nd;
- struct dentry *dentry;
- struct inode *inode;
struct iattr newattrs;
- struct file *f = NULL;
- struct vfsmount *mnt;
-
- error = -EINVAL;
- if (times && (!nsec_valid(times[0].tv_nsec) ||
- !nsec_valid(times[1].tv_nsec))) {
- goto out;
- }
-
- if (flags & ~AT_SYMLINK_NOFOLLOW)
- goto out;
-
- if (filename == NULL && dfd != AT_FDCWD) {
- error = -EINVAL;
- if (flags & AT_SYMLINK_NOFOLLOW)
- goto out;
-
- error = -EBADF;
- f = fget(dfd);
- if (!f)
- goto out;
- dentry = f->f_path.dentry;
- mnt = f->f_path.mnt;
- } else {
- error = __user_walk_fd(dfd, filename, (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW, &nd);
- if (error)
- goto out;
-
- dentry = nd.path.dentry;
- mnt = nd.path.mnt;
- }
-
- inode = dentry->d_inode;
-
- error = mnt_want_write(mnt);
- if (error)
- goto dput_and_out;
/* Don't worry, the checks are done in inode_change_ok() */
newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
@@ -121,41 +77,132 @@ long do_utimes(int dfd, char __user *fil
newattrs.ia_valid |= ATTR_MTIME_SET;
}
}
+ mutex_lock(&path->dentry->d_inode->i_mutex);
+ error = mnt_want_write(path->mnt);
+ if (!error) {
+ error = notify_change(path->dentry, &newattrs);
+ mnt_drop_write(path->mnt);
+ }
+ mutex_unlock(&path->dentry->d_inode->i_mutex);
+
+ return error;
+}
+
+/*
+ * If times is NULL or both times are either UTIME_OMIT or UTIME_NOW, then
+ * need to check permissions, because inode_change_ok() won't do it.
+ */
+static bool utimes_need_permission(struct timespec *times)
+{
+ return !times || (nsec_special(times[0].tv_nsec) &&
+ nsec_special(times[1].tv_nsec));
+}
+
+static int do_futimes(int fd, struct timespec *times)
+{
+ int error;
+ struct file *file = fget(fd);
+
+ if (!file)
+ return -EBADF;
+
+ if (utimes_need_permission(times)) {
+ struct inode *inode = file->f_path.dentry->d_inode;
+
+ error = -EACCES;
+ if (!is_owner_or_cap(inode) && !(file->f_mode & FMODE_WRITE))
+ goto out_fput;
+ }
+ error = utimes_common(&file->f_path, times);
+
+ out_fput:
+ fput(file);
+
+ return error;
+}
+
+
+/*
+ * do_futimesat - change times on filename
+ * @dfd: open file descriptor, -1 or AT_FDCWD
+ * @filename: path name
+ * @times: new times or NULL
+ * @flags: zero or more flags (only AT_SYMLINK_NOFOLLOW for the moment)
+ *
+ * Caller must verify the values in times (if not NULL)
+ *
+ * If times==NULL, set access and modification to current time,
+ * must be owner or have write permission.
+ * Else, update from *times, must be owner or super user.
+ */
+int do_utimes(int dfd, char __user *filename, struct timespec *times, int flags)
+{
+ int error;
+ struct nameidata nd;
+ int lookup_flags;
+
+ if (flags & ~AT_SYMLINK_NOFOLLOW)
+ return -EINVAL;
+
+ lookup_flags = (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
+ error = __user_walk_fd(dfd, filename, lookup_flags, &nd);
+ if (error)
+ return error;
+
+
+ if (utimes_need_permission(times)) {
+ struct inode *inode = nd.path.dentry->d_inode;
- /*
- * If times is NULL or both times are either UTIME_OMIT or
- * UTIME_NOW, then need to check permissions, because
- * inode_change_ok() won't do it.
- */
- if (!times || (nsec_special(times[0].tv_nsec) &&
- nsec_special(times[1].tv_nsec))) {
error = -EACCES;
- if (!f && IS_IMMUTABLE(inode))
- goto mnt_drop_write_and_out;
+ if (IS_IMMUTABLE(inode))
+ goto out_path_put;
if (!is_owner_or_cap(inode)) {
- if (f) {
- if (!(f->f_mode & FMODE_WRITE))
- goto mnt_drop_write_and_out;
- } else {
- error = vfs_permission(&nd, MAY_WRITE);
- if (error)
- goto mnt_drop_write_and_out;
- }
+ error = vfs_permission(&nd, MAY_WRITE);
+ if (error)
+ goto out_path_put;
}
}
- mutex_lock(&inode->i_mutex);
- error = notify_change(dentry, &newattrs);
- mutex_unlock(&inode->i_mutex);
-mnt_drop_write_and_out:
- mnt_drop_write(mnt);
-dput_and_out:
- if (f)
- fput(f);
- else
- path_put(&nd.path);
-out:
+ error = utimes_common(&nd.path, times);
+
+ out_path_put:
+ path_put(&nd.path);
+
return error;
+
+}
+
+/*
+ * do_futimesat - change times on filename or file descriptor
+ * @dfd: open file descriptor, -1 or AT_FDCWD
+ * @filename: path name or NULL
+ * @times: new times or NULL
+ * @flags: zero or more flags (only AT_SYMLINK_NOFOLLOW for the moment)
+ *
+ * If filename is NULL and dfd defers to an open file, then operate on
+ * the file. Otherwise look up filename, possibly using dfd as a
+ * starting point.
+ *
+ * If times==NULL, set access and modification to current time,
+ * must be owner or have write permission.
+ * Else, update from *times, must be owner or super user.
+ */
+int do_futimesat(int dfd, char __user *filename, struct timespec *times,
+ int flags)
+{
+ if (times && (!nsec_valid(times[0].tv_nsec) ||
+ !nsec_valid(times[1].tv_nsec))) {
+ return -EINVAL;
+ }
+
+ if (filename == NULL && dfd != AT_FDCWD) {
+ if (flags)
+ return -EINVAL;
+
+ return do_futimes(dfd, times);
+ } else {
+ return do_utimes(dfd, filename, times, flags);
+ }
}
asmlinkage long sys_utimensat(int dfd, char __user *filename, struct timespec __user *utimes, int flags)
@@ -180,7 +227,7 @@ asmlinkage long sys_utimensat(int dfd, c
return 0;
}
- return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
+ return do_futimesat(dfd, filename, utimes ? tstimes : NULL, flags);
}
asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
@@ -207,7 +254,7 @@ asmlinkage long sys_futimesat(int dfd, c
tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
}
- return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
+ return do_futimesat(dfd, filename, utimes ? tstimes : NULL, 0);
}
asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
Index: linux-2.6/fs/compat.c
===================================================================
--- linux-2.6.orig/fs/compat.c 2008-05-06 11:04:28.000000000 +0200
+++ linux-2.6/fs/compat.c 2008-05-06 11:04:37.000000000 +0200
@@ -110,7 +110,7 @@ asmlinkage long compat_sys_utimensat(uns
if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
return 0;
}
- return do_utimes(dfd, filename, t ? tv : NULL, flags);
+ return do_futimesat(dfd, filename, t ? tv : NULL, flags);
}
asmlinkage long compat_sys_futimesat(unsigned int dfd, char __user *filename, struct compat_timeval __user *t)
@@ -129,7 +129,7 @@ asmlinkage long compat_sys_futimesat(uns
tv[0].tv_nsec *= 1000;
tv[1].tv_nsec *= 1000;
}
- return do_utimes(dfd, filename, t ? tv : NULL, 0);
+ return do_futimesat(dfd, filename, t ? tv : NULL, 0);
}
asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
--
next prev parent reply other threads:[~2008-05-06 9:20 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-05-06 9:13 [patch 00/24] vfs: fixes and cleanups + add helpers to check r/o bind mounts Miklos Szeredi
2008-05-06 9:13 ` [patch 01/24] nfsd: clean up mnt_want_write calls Miklos Szeredi
2008-05-06 9:13 ` [patch 02/24] cgroup: dont call vfs_mkdir Miklos Szeredi
2008-05-06 9:13 ` [patch 03/24] reiserfs: dont call vfs_rmdir Miklos Szeredi
2008-05-06 9:13 ` [patch 04/24] reiserfs: dont call notify_change Miklos Szeredi
2008-05-08 16:49 ` Christoph Hellwig
2008-05-06 9:13 ` [patch 05/24] sysfs: " Miklos Szeredi
2008-05-08 16:50 ` Christoph Hellwig
2008-05-06 9:13 ` [patch 06/24] hpfs: " Miklos Szeredi
2008-05-08 0:41 ` Mikulas Patocka
2008-05-08 16:52 ` Christoph Hellwig
2008-05-09 1:59 ` Mikulas Patocka
2008-05-06 9:13 ` [patch 07/24] fat: " Miklos Szeredi
2008-05-08 16:57 ` Christoph Hellwig
2008-05-08 18:51 ` Miklos Szeredi
2008-05-08 19:42 ` OGAWA Hirofumi
2008-05-08 23:45 ` James Morris
2008-05-06 9:13 ` [patch 08/24] vfs: immutable inode checking cleanup Miklos Szeredi
2008-05-15 7:23 ` Al Viro
2008-05-15 7:39 ` Miklos Szeredi
2008-05-06 9:13 ` [patch 09/24] vfs: truncate: dont check immutable twice Miklos Szeredi
2008-05-06 9:13 ` [patch 10/24] vfs: truncate: append-only checking cleanup Miklos Szeredi
2008-05-15 7:49 ` Al Viro
2008-05-15 7:58 ` Miklos Szeredi
2008-05-06 9:13 ` [patch 11/24] vfs: create file_truncate() helper Miklos Szeredi
2008-05-06 9:13 ` [patch 12/24] vfs: utimes immutable fix Miklos Szeredi
2008-05-06 9:13 ` Miklos Szeredi [this message]
2008-05-06 10:03 ` [patch 13/24] vfs: utimes cleanup Miklos Szeredi
2008-05-15 7:39 ` Al Viro
2008-05-15 7:45 ` Miklos Szeredi
2008-05-06 9:13 ` [patch 14/24] vfs: splice remove_suid() cleanup Miklos Szeredi
2008-05-06 9:13 ` [patch 15/24] vfs: add path_create() and path_mknod() Miklos Szeredi
2008-05-06 9:13 ` [patch 16/24] vfs: add path_mkdir() Miklos Szeredi
2008-05-06 9:13 ` [patch 17/24] vfs: add path_rmdir() Miklos Szeredi
2008-05-06 9:13 ` [patch 18/24] vfs: add path_unlink() Miklos Szeredi
2008-05-06 9:13 ` [patch 19/24] vfs: add path_symlink() Miklos Szeredi
2008-05-06 9:13 ` [patch 20/24] vfs: add path_link() Miklos Szeredi
2008-05-06 9:13 ` [patch 21/24] vfs: add path_rename() Miklos Szeredi
2008-05-06 9:13 ` [patch 22/24] vfs: add path_setattr() Miklos Szeredi
2008-05-06 9:13 ` [patch 23/24] vfs: add path_setxattr() Miklos Szeredi
2008-05-06 9:13 ` [patch 24/24] vfs: add path_removexattr() Miklos Szeredi
2008-05-06 9:16 ` [patch 00/24] vfs: fixes and cleanups + add helpers to check r/o bind mounts Christoph Hellwig
2008-05-06 10:33 ` Miklos Szeredi
2008-05-14 14:55 ` Pavel Machek
2008-05-07 11:57 ` Miklos Szeredi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080506091422.705393283@szeredi.hu \
--to=miklos@szeredi.hu \
--cc=akpm@linux-foundation.org \
--cc=drepper@redhat.com \
--cc=hch@infradead.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=viro@ZenIV.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®