utimes.c 5.92 KB
Newer Older
Alexey Dobriyan's avatar
Alexey Dobriyan committed
1
#include <linux/compiler.h>
Ulrich Drepper's avatar
Ulrich Drepper committed
2
#include <linux/file.h>
Alexey Dobriyan's avatar
Alexey Dobriyan committed
3 4
#include <linux/fs.h>
#include <linux/linkage.h>
5
#include <linux/mount.h>
Alexey Dobriyan's avatar
Alexey Dobriyan committed
6
#include <linux/namei.h>
7
#include <linux/sched.h>
Ulrich Drepper's avatar
Ulrich Drepper committed
8
#include <linux/stat.h>
Alexey Dobriyan's avatar
Alexey Dobriyan committed
9
#include <linux/utime.h>
10
#include <linux/syscalls.h>
Alexey Dobriyan's avatar
Alexey Dobriyan committed
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.
 */
27
SYSCALL_DEFINE2(utime, char __user *, filename, struct utimbuf __user *, times)
Alexey Dobriyan's avatar
Alexey Dobriyan committed
28
{
Ulrich Drepper's avatar
Ulrich Drepper committed
29
	struct timespec tv[2];
Alexey Dobriyan's avatar
Alexey Dobriyan committed
30 31

	if (times) {
Ulrich Drepper's avatar
Ulrich Drepper committed
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;
Alexey Dobriyan's avatar
Alexey Dobriyan committed
37
	}
Ulrich Drepper's avatar
Ulrich Drepper committed
38
	return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
Alexey Dobriyan's avatar
Alexey Dobriyan committed
39 40 41 42
}

#endif

43 44
static bool nsec_valid(long nsec)
{
45
	if (nsec == UTIME_OMIT || nsec == UTIME_NOW)
46 47 48 49 50
		return true;

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

51
static int utimes_common(struct path *path, struct timespec *times)
Alexey Dobriyan's avatar
Alexey Dobriyan committed
52 53 54
{
	int error;
	struct iattr newattrs;
55
	struct inode *inode = path->dentry->d_inode;
56
	struct inode *delegated_inode = NULL;
Alexey Dobriyan's avatar
Alexey Dobriyan committed
57

58
	error = mnt_want_write(path->mnt);
59
	if (error)
60
		goto out;
Alexey Dobriyan's avatar
Alexey Dobriyan committed
61

62 63 64 65
	if (times && times[0].tv_nsec == UTIME_NOW &&
		     times[1].tv_nsec == UTIME_NOW)
		times = NULL;

Alexey Dobriyan's avatar
Alexey Dobriyan committed
66 67
	newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
	if (times) {
Ulrich Drepper's avatar
Ulrich Drepper committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
		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;
		}
83
		/*
84 85 86
		 * Tell inode_change_ok(), that this is an explicit time
		 * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET
		 * were used.
87
		 */
88
		newattrs.ia_valid |= ATTR_TIMES_SET;
89 90 91 92 93 94
	} else {
		/*
		 * If times is NULL (or both times are UTIME_NOW),
		 * then we need to check permissions, because
		 * inode_change_ok() won't do it.
		 */
Alexey Dobriyan's avatar
Alexey Dobriyan committed
95 96
		error = -EACCES;
                if (IS_IMMUTABLE(inode))
97
			goto mnt_drop_write_and_out;
Alexey Dobriyan's avatar
Alexey Dobriyan committed
98

99
		if (!inode_owner_or_capable(inode)) {
100
			error = inode_permission(inode, MAY_WRITE);
101 102
			if (error)
				goto mnt_drop_write_and_out;
103
		}
Alexey Dobriyan's avatar
Alexey Dobriyan committed
104
	}
105
retry_deleg:
Alexey Dobriyan's avatar
Alexey Dobriyan committed
106
	mutex_lock(&inode->i_mutex);
107
	error = notify_change(path->dentry, &newattrs, &delegated_inode);
Alexey Dobriyan's avatar
Alexey Dobriyan committed
108
	mutex_unlock(&inode->i_mutex);
109 110 111 112 113
	if (delegated_inode) {
		error = break_deleg_wait(&delegated_inode);
		if (!error)
			goto retry_deleg;
	}
114

115
mnt_drop_write_and_out:
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	mnt_drop_write(path->mnt);
out:
	return error;
}

/*
 * do_utimes - 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 refers 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.
 */
136 137
long do_utimes(int dfd, const char __user *filename, struct timespec *times,
	       int flags)
138 139 140 141 142 143 144 145 146 147 148 149
{
	int 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) {
150
		struct fd f;
151 152 153 154

		if (flags & AT_SYMLINK_NOFOLLOW)
			goto out;

155
		f = fdget(dfd);
156
		error = -EBADF;
157
		if (!f.file)
158 159
			goto out;

160 161
		error = utimes_common(&f.file->f_path, times);
		fdput(f);
162
	} else {
163
		struct path path;
164 165 166 167
		int lookup_flags = 0;

		if (!(flags & AT_SYMLINK_NOFOLLOW))
			lookup_flags |= LOOKUP_FOLLOW;
168
retry:
169
		error = user_path_at(dfd, filename, lookup_flags, &path);
170 171 172
		if (error)
			goto out;

173 174
		error = utimes_common(&path, times);
		path_put(&path);
175 176 177 178
		if (retry_estale(error, lookup_flags)) {
			lookup_flags |= LOOKUP_REVAL;
			goto retry;
		}
179 180
	}

Alexey Dobriyan's avatar
Alexey Dobriyan committed
181 182 183 184
out:
	return error;
}

185
SYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename,
186
		struct timespec __user *, utimes, int, flags)
Ulrich Drepper's avatar
Ulrich Drepper committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
{
	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);
}

203
SYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename,
204
		struct timeval __user *, utimes)
Alexey Dobriyan's avatar
Alexey Dobriyan committed
205 206
{
	struct timeval times[2];
Ulrich Drepper's avatar
Ulrich Drepper committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	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;
	}
Alexey Dobriyan's avatar
Alexey Dobriyan committed
227

Ulrich Drepper's avatar
Ulrich Drepper committed
228
	return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
Alexey Dobriyan's avatar
Alexey Dobriyan committed
229 230
}

231 232
SYSCALL_DEFINE2(utimes, char __user *, filename,
		struct timeval __user *, utimes)
Alexey Dobriyan's avatar
Alexey Dobriyan committed
233 234 235
{
	return sys_futimesat(AT_FDCWD, filename, utimes);
}