1 #include <linux/file.h>
2 #include <linux/mount.h>
3 #include <linux/namei.h>
4 #include <linux/utime.h>
5 #include <linux/syscalls.h>
6 #include <linux/uaccess.h>
7 #include <linux/compat.h>
8 #include <asm/unistd.h>
10 #ifdef __ARCH_WANT_SYS_UTIME
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
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.
23 SYSCALL_DEFINE2(utime
, char __user
*, filename
, struct utimbuf __user
*, times
)
25 struct timespec tv
[2];
28 if (get_user(tv
[0].tv_sec
, ×
->actime
) ||
29 get_user(tv
[1].tv_sec
, ×
->modtime
))
34 return do_utimes(AT_FDCWD
, filename
, times
? tv
: NULL
, 0);
39 static bool nsec_valid(long nsec
)
41 if (nsec
== UTIME_OMIT
|| nsec
== UTIME_NOW
)
44 return nsec
>= 0 && nsec
<= 999999999;
47 static int utimes_common(const struct path
*path
, struct timespec
*times
)
50 struct iattr newattrs
;
51 struct inode
*inode
= path
->dentry
->d_inode
;
52 struct inode
*delegated_inode
= NULL
;
54 error
= mnt_want_write(path
->mnt
);
58 if (times
&& times
[0].tv_nsec
== UTIME_NOW
&&
59 times
[1].tv_nsec
== UTIME_NOW
)
62 newattrs
.ia_valid
= ATTR_CTIME
| ATTR_MTIME
| ATTR_ATIME
;
64 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
;
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
;
80 * Tell setattr_prepare(), that this is an explicit time
81 * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET
84 newattrs
.ia_valid
|= ATTR_TIMES_SET
;
86 newattrs
.ia_valid
|= ATTR_TOUCH
;
90 error
= notify_change(path
->dentry
, &newattrs
, &delegated_inode
);
92 if (delegated_inode
) {
93 error
= break_deleg_wait(&delegated_inode
);
98 mnt_drop_write(path
->mnt
);
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)
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
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.
118 long do_utimes(int dfd
, const char __user
*filename
, struct timespec
*times
,
123 if (times
&& (!nsec_valid(times
[0].tv_nsec
) ||
124 !nsec_valid(times
[1].tv_nsec
))) {
128 if (flags
& ~AT_SYMLINK_NOFOLLOW
)
131 if (filename
== NULL
&& dfd
!= AT_FDCWD
) {
134 if (flags
& AT_SYMLINK_NOFOLLOW
)
142 error
= utimes_common(&f
.file
->f_path
, times
);
146 int lookup_flags
= 0;
148 if (!(flags
& AT_SYMLINK_NOFOLLOW
))
149 lookup_flags
|= LOOKUP_FOLLOW
;
151 error
= user_path_at(dfd
, filename
, lookup_flags
, &path
);
155 error
= utimes_common(&path
, times
);
157 if (retry_estale(error
, lookup_flags
)) {
158 lookup_flags
|= LOOKUP_REVAL
;
167 SYSCALL_DEFINE4(utimensat
, int, dfd
, const char __user
*, filename
,
168 struct timespec __user
*, utimes
, int, flags
)
170 struct timespec tstimes
[2];
173 if (copy_from_user(&tstimes
, utimes
, sizeof(tstimes
)))
176 /* Nothing to do, we must not even check the path. */
177 if (tstimes
[0].tv_nsec
== UTIME_OMIT
&&
178 tstimes
[1].tv_nsec
== UTIME_OMIT
)
182 return do_utimes(dfd
, filename
, utimes
? tstimes
: NULL
, flags
);
185 SYSCALL_DEFINE3(futimesat
, int, dfd
, const char __user
*, filename
,
186 struct timeval __user
*, utimes
)
188 struct timeval times
[2];
189 struct timespec tstimes
[2];
192 if (copy_from_user(×
, utimes
, sizeof(times
)))
195 /* This test is needed to catch all invalid values. If we
196 would test only in do_utimes we would miss those invalid
197 values truncated by the multiplication with 1000. Note
198 that we also catch UTIME_{NOW,OMIT} here which are only
199 valid for utimensat. */
200 if (times
[0].tv_usec
>= 1000000 || times
[0].tv_usec
< 0 ||
201 times
[1].tv_usec
>= 1000000 || times
[1].tv_usec
< 0)
204 tstimes
[0].tv_sec
= times
[0].tv_sec
;
205 tstimes
[0].tv_nsec
= 1000 * times
[0].tv_usec
;
206 tstimes
[1].tv_sec
= times
[1].tv_sec
;
207 tstimes
[1].tv_nsec
= 1000 * times
[1].tv_usec
;
210 return do_utimes(dfd
, filename
, utimes
? tstimes
: NULL
, 0);
213 SYSCALL_DEFINE2(utimes
, char __user
*, filename
,
214 struct timeval __user
*, utimes
)
216 return sys_futimesat(AT_FDCWD
, filename
, utimes
);
221 * Not all architectures have sys_utime, so implement this in terms
224 COMPAT_SYSCALL_DEFINE2(utime
, const char __user
*, filename
,
225 struct compat_utimbuf __user
*, t
)
227 struct timespec tv
[2];
230 if (get_user(tv
[0].tv_sec
, &t
->actime
) ||
231 get_user(tv
[1].tv_sec
, &t
->modtime
))
236 return do_utimes(AT_FDCWD
, filename
, t
? tv
: NULL
, 0);
239 COMPAT_SYSCALL_DEFINE4(utimensat
, unsigned int, dfd
, const char __user
*, filename
, struct compat_timespec __user
*, t
, int, flags
)
241 struct timespec tv
[2];
244 if (compat_get_timespec(&tv
[0], &t
[0]) ||
245 compat_get_timespec(&tv
[1], &t
[1]))
248 if (tv
[0].tv_nsec
== UTIME_OMIT
&& tv
[1].tv_nsec
== UTIME_OMIT
)
251 return do_utimes(dfd
, filename
, t
? tv
: NULL
, flags
);
254 COMPAT_SYSCALL_DEFINE3(futimesat
, unsigned int, dfd
, const char __user
*, filename
, struct compat_timeval __user
*, t
)
256 struct timespec tv
[2];
259 if (get_user(tv
[0].tv_sec
, &t
[0].tv_sec
) ||
260 get_user(tv
[0].tv_nsec
, &t
[0].tv_usec
) ||
261 get_user(tv
[1].tv_sec
, &t
[1].tv_sec
) ||
262 get_user(tv
[1].tv_nsec
, &t
[1].tv_usec
))
264 if (tv
[0].tv_nsec
>= 1000000 || tv
[0].tv_nsec
< 0 ||
265 tv
[1].tv_nsec
>= 1000000 || tv
[1].tv_nsec
< 0)
267 tv
[0].tv_nsec
*= 1000;
268 tv
[1].tv_nsec
*= 1000;
270 return do_utimes(dfd
, filename
, t
? tv
: NULL
, 0);
273 COMPAT_SYSCALL_DEFINE2(utimes
, const char __user
*, filename
, struct compat_timeval __user
*, t
)
275 return compat_sys_futimesat(AT_FDCWD
, filename
, t
);