utimes.c 5.4 KB
Newer Older
A
Alexey Dobriyan 已提交
1
#include <linux/compiler.h>
U
Ulrich Drepper 已提交
2
#include <linux/file.h>
A
Alexey Dobriyan 已提交
3 4
#include <linux/fs.h>
#include <linux/linkage.h>
5
#include <linux/mount.h>
A
Alexey Dobriyan 已提交
6
#include <linux/namei.h>
7
#include <linux/sched.h>
U
Ulrich Drepper 已提交
8
#include <linux/stat.h>
A
Alexey Dobriyan 已提交
9
#include <linux/utime.h>
10
#include <linux/syscalls.h>
A
Alexey Dobriyan 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <asm/uaccess.h>
#include <asm/unistd.h>

#ifdef __ARCH_WANT_SYS_UTIME

/*
 * sys_utime() can be implemented in user-level using sys_utimes().
 * Is this for backwards compatibility?  If so, why not move it
 * into the appropriate arch directory (for those architectures that
 * need it).
 */

/* 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.
 */
U
Ulrich Drepper 已提交
27
asmlinkage long sys_utime(char __user *filename, struct utimbuf __user *times)
A
Alexey Dobriyan 已提交
28
{
U
Ulrich Drepper 已提交
29
	struct timespec tv[2];
A
Alexey Dobriyan 已提交
30 31

	if (times) {
U
Ulrich Drepper 已提交
32 33 34 35 36
		if (get_user(tv[0].tv_sec, &times->actime) ||
		    get_user(tv[1].tv_sec, &times->modtime))
			return -EFAULT;
		tv[0].tv_nsec = 0;
		tv[1].tv_nsec = 0;
A
Alexey Dobriyan 已提交
37
	}
U
Ulrich Drepper 已提交
38
	return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
A
Alexey Dobriyan 已提交
39 40 41 42
}

#endif

43 44 45 46 47
static bool nsec_special(long nsec)
{
	return nsec == UTIME_OMIT || nsec == UTIME_NOW;
}

48 49
static bool nsec_valid(long nsec)
{
50
	if (nsec_special(nsec))
51 52 53 54 55
		return true;

	return nsec >= 0 && nsec <= 999999999;
}

A
Alexey Dobriyan 已提交
56 57 58 59
/* 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.
 */
U
Ulrich Drepper 已提交
60
long do_utimes(int dfd, char __user *filename, struct timespec *times, int flags)
A
Alexey Dobriyan 已提交
61 62 63
{
	int error;
	struct nameidata nd;
U
Ulrich Drepper 已提交
64 65
	struct dentry *dentry;
	struct inode *inode;
A
Alexey Dobriyan 已提交
66
	struct iattr newattrs;
U
Ulrich Drepper 已提交
67
	struct file *f = NULL;
68
	struct vfsmount *mnt;
A
Alexey Dobriyan 已提交
69

U
Ulrich Drepper 已提交
70
	error = -EINVAL;
71 72 73 74 75
	if (times && (!nsec_valid(times[0].tv_nsec) ||
		      !nsec_valid(times[1].tv_nsec))) {
		goto out;
	}

U
Ulrich Drepper 已提交
76
	if (flags & ~AT_SYMLINK_NOFOLLOW)
A
Alexey Dobriyan 已提交
77
		goto out;
U
Ulrich Drepper 已提交
78 79 80 81 82 83 84 85 86 87 88

	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;
89
		mnt = f->f_path.mnt;
U
Ulrich Drepper 已提交
90 91 92 93 94
	} else {
		error = __user_walk_fd(dfd, filename, (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW, &nd);
		if (error)
			goto out;

95
		dentry = nd.path.dentry;
96
		mnt = nd.path.mnt;
U
Ulrich Drepper 已提交
97 98 99
	}

	inode = dentry->d_inode;
A
Alexey Dobriyan 已提交
100

101 102
	error = mnt_want_write(mnt);
	if (error)
A
Alexey Dobriyan 已提交
103 104
		goto dput_and_out;

105 106 107 108
	if (times && times[0].tv_nsec == UTIME_NOW &&
		     times[1].tv_nsec == UTIME_NOW)
		times = NULL;

A
Alexey Dobriyan 已提交
109 110 111 112 113
	/* Don't worry, the checks are done in inode_change_ok() */
	newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
	if (times) {
		error = -EPERM;
                if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
114
			goto mnt_drop_write_and_out;
A
Alexey Dobriyan 已提交
115

U
Ulrich Drepper 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		if (times[0].tv_nsec == UTIME_OMIT)
			newattrs.ia_valid &= ~ATTR_ATIME;
		else if (times[0].tv_nsec != UTIME_NOW) {
			newattrs.ia_atime.tv_sec = times[0].tv_sec;
			newattrs.ia_atime.tv_nsec = times[0].tv_nsec;
			newattrs.ia_valid |= ATTR_ATIME_SET;
		}

		if (times[1].tv_nsec == UTIME_OMIT)
			newattrs.ia_valid &= ~ATTR_MTIME;
		else if (times[1].tv_nsec != UTIME_NOW) {
			newattrs.ia_mtime.tv_sec = times[1].tv_sec;
			newattrs.ia_mtime.tv_nsec = times[1].tv_nsec;
			newattrs.ia_valid |= ATTR_MTIME_SET;
		}
131 132 133 134 135 136 137 138 139
	}

	/*
	 * 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))) {
A
Alexey Dobriyan 已提交
140 141
		error = -EACCES;
                if (IS_IMMUTABLE(inode))
142
			goto mnt_drop_write_and_out;
A
Alexey Dobriyan 已提交
143

144
		if (!is_owner_or_cap(inode)) {
145 146
			if (f) {
				if (!(f->f_mode & FMODE_WRITE))
147
					goto mnt_drop_write_and_out;
148 149 150
			} else {
				error = vfs_permission(&nd, MAY_WRITE);
				if (error)
151
					goto mnt_drop_write_and_out;
152 153
			}
		}
A
Alexey Dobriyan 已提交
154 155
	}
	mutex_lock(&inode->i_mutex);
U
Ulrich Drepper 已提交
156
	error = notify_change(dentry, &newattrs);
A
Alexey Dobriyan 已提交
157
	mutex_unlock(&inode->i_mutex);
158 159
mnt_drop_write_and_out:
	mnt_drop_write(mnt);
A
Alexey Dobriyan 已提交
160
dput_and_out:
U
Ulrich Drepper 已提交
161 162 163
	if (f)
		fput(f);
	else
J
Jan Blunck 已提交
164
		path_put(&nd.path);
A
Alexey Dobriyan 已提交
165 166 167 168
out:
	return error;
}

U
Ulrich Drepper 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
asmlinkage long sys_utimensat(int dfd, char __user *filename, struct timespec __user *utimes, int flags)
{
	struct timespec tstimes[2];

	if (utimes) {
		if (copy_from_user(&tstimes, utimes, sizeof(tstimes)))
			return -EFAULT;

		/* Nothing to do, we must not even check the path.  */
		if (tstimes[0].tv_nsec == UTIME_OMIT &&
		    tstimes[1].tv_nsec == UTIME_OMIT)
			return 0;
	}

	return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
}

A
Alexey Dobriyan 已提交
186 187 188
asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
{
	struct timeval times[2];
U
Ulrich Drepper 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	struct timespec tstimes[2];

	if (utimes) {
		if (copy_from_user(&times, utimes, sizeof(times)))
			return -EFAULT;

		/* This test is needed to catch all invalid values.  If we
		   would test only in do_utimes we would miss those invalid
		   values truncated by the multiplication with 1000.  Note
		   that we also catch UTIME_{NOW,OMIT} here which are only
		   valid for utimensat.  */
		if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
		    times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
			return -EINVAL;

		tstimes[0].tv_sec = times[0].tv_sec;
		tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
		tstimes[1].tv_sec = times[1].tv_sec;
		tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
	}
A
Alexey Dobriyan 已提交
209

U
Ulrich Drepper 已提交
210
	return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
A
Alexey Dobriyan 已提交
211 212 213 214 215 216
}

asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
{
	return sys_futimesat(AT_FDCWD, filename, utimes);
}