1 #include <linux/compiler.h>
2 #include <linux/file.h>
4 #include <linux/linkage.h>
5 #include <linux/namei.h>
6 #include <linux/sched.h>
7 #include <linux/stat.h>
8 #include <linux/utime.h>
9 #include <linux/syscalls.h>
10 #include <asm/uaccess.h>
11 #include <asm/unistd.h>
13 #ifdef __ARCH_WANT_SYS_UTIME
16 * sys_utime() can be implemented in user-level using sys_utimes().
17 * Is this for backwards compatibility? If so, why not move it
18 * into the appropriate arch directory (for those architectures that
22 /* If times==NULL, set access and modification to current time,
23 * must be owner or have write permission.
24 * Else, update from *times, must be owner or super user.
26 asmlinkage
long sys_utime(char __user
*filename
, struct utimbuf __user
*times
)
28 struct timespec tv
[2];
31 if (get_user(tv
[0].tv_sec
, ×
->actime
) ||
32 get_user(tv
[1].tv_sec
, ×
->modtime
))
37 return do_utimes(AT_FDCWD
, filename
, times
? tv
: NULL
, 0);
42 static bool nsec_special(long nsec
)
44 return nsec
== UTIME_OMIT
|| nsec
== UTIME_NOW
;
47 static bool nsec_valid(long nsec
)
49 if (nsec_special(nsec
))
52 return nsec
>= 0 && nsec
<= 999999999;
55 /* If times==NULL, set access and modification to current time,
56 * must be owner or have write permission.
57 * Else, update from *times, must be owner or super user.
59 long do_utimes(int dfd
, char __user
*filename
, struct timespec
*times
, int flags
)
63 struct dentry
*dentry
;
65 struct iattr newattrs
;
66 struct file
*f
= NULL
;
69 if (times
&& (!nsec_valid(times
[0].tv_nsec
) ||
70 !nsec_valid(times
[1].tv_nsec
))) {
74 if (flags
& ~AT_SYMLINK_NOFOLLOW
)
77 if (filename
== NULL
&& dfd
!= AT_FDCWD
) {
79 if (flags
& AT_SYMLINK_NOFOLLOW
)
86 dentry
= f
->f_path
.dentry
;
88 error
= __user_walk_fd(dfd
, filename
, (flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : LOOKUP_FOLLOW
, &nd
);
92 dentry
= nd
.path
.dentry
;
95 inode
= dentry
->d_inode
;
101 /* Don't worry, the checks are done in inode_change_ok() */
102 newattrs
.ia_valid
= ATTR_CTIME
| ATTR_MTIME
| ATTR_ATIME
;
105 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
108 if (times
[0].tv_nsec
== UTIME_OMIT
)
109 newattrs
.ia_valid
&= ~ATTR_ATIME
;
110 else if (times
[0].tv_nsec
!= UTIME_NOW
) {
111 newattrs
.ia_atime
.tv_sec
= times
[0].tv_sec
;
112 newattrs
.ia_atime
.tv_nsec
= times
[0].tv_nsec
;
113 newattrs
.ia_valid
|= ATTR_ATIME_SET
;
116 if (times
[1].tv_nsec
== UTIME_OMIT
)
117 newattrs
.ia_valid
&= ~ATTR_MTIME
;
118 else if (times
[1].tv_nsec
!= UTIME_NOW
) {
119 newattrs
.ia_mtime
.tv_sec
= times
[1].tv_sec
;
120 newattrs
.ia_mtime
.tv_nsec
= times
[1].tv_nsec
;
121 newattrs
.ia_valid
|= ATTR_MTIME_SET
;
126 * If times is NULL or both times are either UTIME_OMIT or
127 * UTIME_NOW, then need to check permissions, because
128 * inode_change_ok() won't do it.
130 if (!times
|| (nsec_special(times
[0].tv_nsec
) &&
131 nsec_special(times
[1].tv_nsec
))) {
133 if (IS_IMMUTABLE(inode
))
136 if (!is_owner_or_cap(inode
)) {
138 if (!(f
->f_mode
& FMODE_WRITE
))
141 error
= vfs_permission(&nd
, MAY_WRITE
);
147 mutex_lock(&inode
->i_mutex
);
148 error
= notify_change(dentry
, &newattrs
);
149 mutex_unlock(&inode
->i_mutex
);
159 asmlinkage
long sys_utimensat(int dfd
, char __user
*filename
, struct timespec __user
*utimes
, int flags
)
161 struct timespec tstimes
[2];
164 if (copy_from_user(&tstimes
, utimes
, sizeof(tstimes
)))
166 if ((tstimes
[0].tv_nsec
== UTIME_OMIT
||
167 tstimes
[0].tv_nsec
== UTIME_NOW
) &&
168 tstimes
[0].tv_sec
!= 0)
170 if ((tstimes
[1].tv_nsec
== UTIME_OMIT
||
171 tstimes
[1].tv_nsec
== UTIME_NOW
) &&
172 tstimes
[1].tv_sec
!= 0)
175 /* Nothing to do, we must not even check the path. */
176 if (tstimes
[0].tv_nsec
== UTIME_OMIT
&&
177 tstimes
[1].tv_nsec
== UTIME_OMIT
)
181 return do_utimes(dfd
, filename
, utimes
? tstimes
: NULL
, flags
);
184 asmlinkage
long sys_futimesat(int dfd
, char __user
*filename
, struct timeval __user
*utimes
)
186 struct timeval times
[2];
187 struct timespec tstimes
[2];
190 if (copy_from_user(×
, utimes
, sizeof(times
)))
193 /* This test is needed to catch all invalid values. If we
194 would test only in do_utimes we would miss those invalid
195 values truncated by the multiplication with 1000. Note
196 that we also catch UTIME_{NOW,OMIT} here which are only
197 valid for utimensat. */
198 if (times
[0].tv_usec
>= 1000000 || times
[0].tv_usec
< 0 ||
199 times
[1].tv_usec
>= 1000000 || times
[1].tv_usec
< 0)
202 tstimes
[0].tv_sec
= times
[0].tv_sec
;
203 tstimes
[0].tv_nsec
= 1000 * times
[0].tv_usec
;
204 tstimes
[1].tv_sec
= times
[1].tv_sec
;
205 tstimes
[1].tv_nsec
= 1000 * times
[1].tv_usec
;
208 return do_utimes(dfd
, filename
, utimes
? tstimes
: NULL
, 0);
211 asmlinkage
long sys_utimes(char __user
*filename
, struct timeval __user
*utimes
)
213 return sys_futimesat(AT_FDCWD
, filename
, utimes
);