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 <asm/uaccess.h>
10 #include <asm/unistd.h>
12 #ifdef __ARCH_WANT_SYS_UTIME
15 * sys_utime() can be implemented in user-level using sys_utimes().
16 * Is this for backwards compatibility? If so, why not move it
17 * into the appropriate arch directory (for those architectures that
21 /* If times==NULL, set access and modification to current time,
22 * must be owner or have write permission.
23 * Else, update from *times, must be owner or super user.
25 asmlinkage long sys_utime(char __user *filename, struct utimbuf __user *times)
27 struct timespec tv[2];
30 if (get_user(tv[0].tv_sec, ×->actime) ||
31 get_user(tv[1].tv_sec, ×->modtime))
36 return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
41 static bool nsec_valid(long nsec)
43 if (nsec == UTIME_OMIT || nsec == UTIME_NOW)
46 return nsec >= 0 && nsec <= 999999999;
49 /* If times==NULL, set access and modification to current time,
50 * must be owner or have write permission.
51 * Else, update from *times, must be owner or super user.
53 long do_utimes(int dfd, char __user *filename, struct timespec *times, int flags)
57 struct dentry *dentry;
59 struct iattr newattrs;
60 struct file *f = NULL;
63 if (times && (!nsec_valid(times[0].tv_nsec) ||
64 !nsec_valid(times[1].tv_nsec))) {
68 if (flags & ~AT_SYMLINK_NOFOLLOW)
71 if (filename == NULL && dfd != AT_FDCWD) {
73 if (flags & AT_SYMLINK_NOFOLLOW)
80 dentry = f->f_path.dentry;
82 error = __user_walk_fd(dfd, filename, (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW, &nd);
89 inode = dentry->d_inode;
95 /* Don't worry, the checks are done in inode_change_ok() */
96 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
99 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
102 if (times[0].tv_nsec == UTIME_OMIT)
103 newattrs.ia_valid &= ~ATTR_ATIME;
104 else if (times[0].tv_nsec != UTIME_NOW) {
105 newattrs.ia_atime.tv_sec = times[0].tv_sec;
106 newattrs.ia_atime.tv_nsec = times[0].tv_nsec;
107 newattrs.ia_valid |= ATTR_ATIME_SET;
110 if (times[1].tv_nsec == UTIME_OMIT)
111 newattrs.ia_valid &= ~ATTR_MTIME;
112 else if (times[1].tv_nsec != UTIME_NOW) {
113 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
114 newattrs.ia_mtime.tv_nsec = times[1].tv_nsec;
115 newattrs.ia_valid |= ATTR_MTIME_SET;
119 if (IS_IMMUTABLE(inode))
122 if (!is_owner_or_cap(inode)) {
124 if (!(f->f_mode & FMODE_WRITE))
127 error = vfs_permission(&nd, MAY_WRITE);
133 mutex_lock(&inode->i_mutex);
134 error = notify_change(dentry, &newattrs);
135 mutex_unlock(&inode->i_mutex);
145 asmlinkage long sys_utimensat(int dfd, char __user *filename, struct timespec __user *utimes, int flags)
147 struct timespec tstimes[2];
150 if (copy_from_user(&tstimes, utimes, sizeof(tstimes)))
152 if ((tstimes[0].tv_nsec == UTIME_OMIT ||
153 tstimes[0].tv_nsec == UTIME_NOW) &&
154 tstimes[0].tv_sec != 0)
156 if ((tstimes[1].tv_nsec == UTIME_OMIT ||
157 tstimes[1].tv_nsec == UTIME_NOW) &&
158 tstimes[1].tv_sec != 0)
161 /* Nothing to do, we must not even check the path. */
162 if (tstimes[0].tv_nsec == UTIME_OMIT &&
163 tstimes[1].tv_nsec == UTIME_OMIT)
167 return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
170 asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
172 struct timeval times[2];
173 struct timespec tstimes[2];
176 if (copy_from_user(×, utimes, sizeof(times)))
179 /* This test is needed to catch all invalid values. If we
180 would test only in do_utimes we would miss those invalid
181 values truncated by the multiplication with 1000. Note
182 that we also catch UTIME_{NOW,OMIT} here which are only
183 valid for utimensat. */
184 if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
185 times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
188 tstimes[0].tv_sec = times[0].tv_sec;
189 tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
190 tstimes[1].tv_sec = times[1].tv_sec;
191 tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
194 return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
197 asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
199 return sys_futimesat(AT_FDCWD, filename, utimes);