blob: 51edb9f9507c409269dd8722a6c49303ef926f84 [file] [log] [blame]
Ulrich Drepper1c710c82007-05-08 00:33:25 -07001#include <linux/file.h>
Dave Hansen74f9fdf2008-02-15 14:37:42 -08002#include <linux/mount.h>
Alexey Dobriyan82b05472006-09-30 23:27:22 -07003#include <linux/namei.h>
Alexey Dobriyan82b05472006-09-30 23:27:22 -07004#include <linux/utime.h>
Adrian Bunk12c2ab52008-02-06 01:36:47 -08005#include <linux/syscalls.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -08006#include <linux/uaccess.h>
Al Viro1a060ba2017-04-08 18:04:59 -04007#include <linux/compat.h>
Alexey Dobriyan82b05472006-09-30 23:27:22 -07008#include <asm/unistd.h>
9
10#ifdef __ARCH_WANT_SYS_UTIME
11
12/*
13 * sys_utime() can be implemented in user-level using sys_utimes().
14 * Is this for backwards compatibility? If so, why not move it
15 * into the appropriate arch directory (for those architectures that
16 * need it).
17 */
18
19/* If times==NULL, set access and modification to current time,
20 * must be owner or have write permission.
21 * Else, update from *times, must be owner or super user.
22 */
Heiko Carstens003d7ab2009-01-14 14:14:21 +010023SYSCALL_DEFINE2(utime, char __user *, filename, struct utimbuf __user *, times)
Alexey Dobriyan82b05472006-09-30 23:27:22 -070024{
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -070025 struct timespec64 tv[2];
Alexey Dobriyan82b05472006-09-30 23:27:22 -070026
Alexey Dobriyan82b05472006-09-30 23:27:22 -070027 if (times) {
Ulrich Drepper1c710c82007-05-08 00:33:25 -070028 if (get_user(tv[0].tv_sec, &times->actime) ||
29 get_user(tv[1].tv_sec, &times->modtime))
30 return -EFAULT;
31 tv[0].tv_nsec = 0;
32 tv[1].tv_nsec = 0;
Alexey Dobriyan82b05472006-09-30 23:27:22 -070033 }
Ulrich Drepper1c710c82007-05-08 00:33:25 -070034 return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
Alexey Dobriyan82b05472006-09-30 23:27:22 -070035}
36
37#endif
38
Miklos Szeredi043f46f2007-10-16 23:27:07 -070039static bool nsec_valid(long nsec)
40{
Michael Kerrisk4cca9222008-06-09 21:16:08 -070041 if (nsec == UTIME_OMIT || nsec == UTIME_NOW)
Miklos Szeredi043f46f2007-10-16 23:27:07 -070042 return true;
43
44 return nsec >= 0 && nsec <= 999999999;
45}
46
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -070047static int utimes_common(const struct path *path, struct timespec64 *times)
Alexey Dobriyan82b05472006-09-30 23:27:22 -070048{
49 int error;
Alexey Dobriyan82b05472006-09-30 23:27:22 -070050 struct iattr newattrs;
Miklos Szeredie9b76fe2008-07-01 15:01:27 +020051 struct inode *inode = path->dentry->d_inode;
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -040052 struct inode *delegated_inode = NULL;
Alexey Dobriyan82b05472006-09-30 23:27:22 -070053
Miklos Szeredie9b76fe2008-07-01 15:01:27 +020054 error = mnt_want_write(path->mnt);
Dave Hansen74f9fdf2008-02-15 14:37:42 -080055 if (error)
Miklos Szeredie9b76fe2008-07-01 15:01:27 +020056 goto out;
Alexey Dobriyan82b05472006-09-30 23:27:22 -070057
Michael Kerrisk12fd0d32008-06-09 21:16:07 -070058 if (times && times[0].tv_nsec == UTIME_NOW &&
59 times[1].tv_nsec == UTIME_NOW)
60 times = NULL;
61
Alexey Dobriyan82b05472006-09-30 23:27:22 -070062 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
63 if (times) {
Ulrich Drepper1c710c82007-05-08 00:33:25 -070064 if (times[0].tv_nsec == UTIME_OMIT)
65 newattrs.ia_valid &= ~ATTR_ATIME;
66 else if (times[0].tv_nsec != UTIME_NOW) {
67 newattrs.ia_atime.tv_sec = times[0].tv_sec;
68 newattrs.ia_atime.tv_nsec = times[0].tv_nsec;
69 newattrs.ia_valid |= ATTR_ATIME_SET;
70 }
71
72 if (times[1].tv_nsec == UTIME_OMIT)
73 newattrs.ia_valid &= ~ATTR_MTIME;
74 else if (times[1].tv_nsec != UTIME_NOW) {
75 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
76 newattrs.ia_mtime.tv_nsec = times[1].tv_nsec;
77 newattrs.ia_valid |= ATTR_MTIME_SET;
78 }
Michael Kerrisk4cca9222008-06-09 21:16:08 -070079 /*
Jan Kara31051c82016-05-26 16:55:18 +020080 * Tell setattr_prepare(), that this is an explicit time
Miklos Szeredi9767d742008-07-01 15:01:26 +020081 * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET
82 * were used.
Michael Kerrisk4cca9222008-06-09 21:16:08 -070083 */
Miklos Szeredi9767d742008-07-01 15:01:26 +020084 newattrs.ia_valid |= ATTR_TIMES_SET;
Michael Kerrisk4cca9222008-06-09 21:16:08 -070085 } else {
Miklos Szeredif2b20f62016-09-16 12:44:20 +020086 newattrs.ia_valid |= ATTR_TOUCH;
Alexey Dobriyan82b05472006-09-30 23:27:22 -070087 }
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -040088retry_deleg:
Al Viro59551022016-01-22 15:40:57 -050089 inode_lock(inode);
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -040090 error = notify_change(path->dentry, &newattrs, &delegated_inode);
Al Viro59551022016-01-22 15:40:57 -050091 inode_unlock(inode);
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -040092 if (delegated_inode) {
93 error = break_deleg_wait(&delegated_inode);
94 if (!error)
95 goto retry_deleg;
96 }
Miklos Szeredie9b76fe2008-07-01 15:01:27 +020097
Miklos Szeredie9b76fe2008-07-01 15:01:27 +020098 mnt_drop_write(path->mnt);
99out:
100 return error;
101}
102
103/*
104 * do_utimes - change times on filename or file descriptor
105 * @dfd: open file descriptor, -1 or AT_FDCWD
106 * @filename: path name or NULL
107 * @times: new times or NULL
108 * @flags: zero or more flags (only AT_SYMLINK_NOFOLLOW for the moment)
109 *
110 * If filename is NULL and dfd refers to an open file, then operate on
111 * the file. Otherwise look up filename, possibly using dfd as a
112 * starting point.
113 *
114 * If times==NULL, set access and modification to current time,
115 * must be owner or have write permission.
116 * Else, update from *times, must be owner or super user.
117 */
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700118long do_utimes(int dfd, const char __user *filename, struct timespec64 *times,
David Howellsc7887322010-08-11 11:26:22 +0100119 int flags)
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200120{
121 int error = -EINVAL;
122
123 if (times && (!nsec_valid(times[0].tv_nsec) ||
124 !nsec_valid(times[1].tv_nsec))) {
125 goto out;
126 }
127
128 if (flags & ~AT_SYMLINK_NOFOLLOW)
129 goto out;
130
131 if (filename == NULL && dfd != AT_FDCWD) {
Al Viro2903ff02012-08-28 12:52:22 -0400132 struct fd f;
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200133
134 if (flags & AT_SYMLINK_NOFOLLOW)
135 goto out;
136
Al Viro2903ff02012-08-28 12:52:22 -0400137 f = fdget(dfd);
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200138 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400139 if (!f.file)
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200140 goto out;
141
Al Viro2903ff02012-08-28 12:52:22 -0400142 error = utimes_common(&f.file->f_path, times);
143 fdput(f);
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200144 } else {
Al Viro2d8f3032008-07-22 09:59:21 -0400145 struct path path;
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200146 int lookup_flags = 0;
147
148 if (!(flags & AT_SYMLINK_NOFOLLOW))
149 lookup_flags |= LOOKUP_FOLLOW;
Jeff Laytona69201d2012-12-11 12:10:14 -0500150retry:
Al Viro2d8f3032008-07-22 09:59:21 -0400151 error = user_path_at(dfd, filename, lookup_flags, &path);
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200152 if (error)
153 goto out;
154
Al Viro2d8f3032008-07-22 09:59:21 -0400155 error = utimes_common(&path, times);
156 path_put(&path);
Jeff Laytona69201d2012-12-11 12:10:14 -0500157 if (retry_estale(error, lookup_flags)) {
158 lookup_flags |= LOOKUP_REVAL;
159 goto retry;
160 }
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200161 }
162
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700163out:
164 return error;
165}
166
David Howellsc7887322010-08-11 11:26:22 +0100167SYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename,
Heiko Carstens6559eed82009-01-14 14:14:32 +0100168 struct timespec __user *, utimes, int, flags)
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700169{
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700170 struct timespec64 tstimes[2];
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700171
172 if (utimes) {
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700173 if ((get_timespec64(&tstimes[0], &utimes[0]) ||
174 get_timespec64(&tstimes[1], &utimes[1])))
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700175 return -EFAULT;
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700176
177 /* Nothing to do, we must not even check the path. */
178 if (tstimes[0].tv_nsec == UTIME_OMIT &&
179 tstimes[1].tv_nsec == UTIME_OMIT)
180 return 0;
181 }
182
183 return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
184}
185
David Howellsc7887322010-08-11 11:26:22 +0100186SYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename,
Heiko Carstens6559eed82009-01-14 14:14:32 +0100187 struct timeval __user *, utimes)
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700188{
189 struct timeval times[2];
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700190 struct timespec64 tstimes[2];
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700191
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700192 if (utimes) {
193 if (copy_from_user(&times, utimes, sizeof(times)))
194 return -EFAULT;
195
196 /* This test is needed to catch all invalid values. If we
197 would test only in do_utimes we would miss those invalid
198 values truncated by the multiplication with 1000. Note
199 that we also catch UTIME_{NOW,OMIT} here which are only
200 valid for utimensat. */
201 if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
202 times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
203 return -EINVAL;
204
205 tstimes[0].tv_sec = times[0].tv_sec;
206 tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
207 tstimes[1].tv_sec = times[1].tv_sec;
208 tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
209 }
210
211 return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700212}
213
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100214SYSCALL_DEFINE2(utimes, char __user *, filename,
215 struct timeval __user *, utimes)
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700216{
217 return sys_futimesat(AT_FDCWD, filename, utimes);
218}
Al Viro1a060ba2017-04-08 18:04:59 -0400219
220#ifdef CONFIG_COMPAT
221/*
222 * Not all architectures have sys_utime, so implement this in terms
223 * of sys_utimes.
224 */
225COMPAT_SYSCALL_DEFINE2(utime, const char __user *, filename,
226 struct compat_utimbuf __user *, t)
227{
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700228 struct timespec64 tv[2];
Al Viro1a060ba2017-04-08 18:04:59 -0400229
230 if (t) {
231 if (get_user(tv[0].tv_sec, &t->actime) ||
232 get_user(tv[1].tv_sec, &t->modtime))
233 return -EFAULT;
234 tv[0].tv_nsec = 0;
235 tv[1].tv_nsec = 0;
236 }
237 return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
238}
239
240COMPAT_SYSCALL_DEFINE4(utimensat, unsigned int, dfd, const char __user *, filename, struct compat_timespec __user *, t, int, flags)
241{
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700242 struct timespec64 tv[2];
Al Viro1a060ba2017-04-08 18:04:59 -0400243
244 if (t) {
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700245 if (compat_get_timespec64(&tv[0], &t[0]) ||
246 compat_get_timespec64(&tv[1], &t[1]))
Al Viro1a060ba2017-04-08 18:04:59 -0400247 return -EFAULT;
248
249 if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
250 return 0;
251 }
252 return do_utimes(dfd, filename, t ? tv : NULL, flags);
253}
254
255COMPAT_SYSCALL_DEFINE3(futimesat, unsigned int, dfd, const char __user *, filename, struct compat_timeval __user *, t)
256{
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700257 struct timespec64 tv[2];
Al Viro1a060ba2017-04-08 18:04:59 -0400258
259 if (t) {
260 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
261 get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
262 get_user(tv[1].tv_sec, &t[1].tv_sec) ||
263 get_user(tv[1].tv_nsec, &t[1].tv_usec))
264 return -EFAULT;
265 if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
266 tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
267 return -EINVAL;
268 tv[0].tv_nsec *= 1000;
269 tv[1].tv_nsec *= 1000;
270 }
271 return do_utimes(dfd, filename, t ? tv : NULL, 0);
272}
273
274COMPAT_SYSCALL_DEFINE2(utimes, const char __user *, filename, struct compat_timeval __user *, t)
275{
276 return compat_sys_futimesat(AT_FDCWD, filename, t);
277}
278#endif