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_valid(long nsec)
44 if (nsec == UTIME_OMIT || nsec == UTIME_NOW)
47 return nsec >= 0 && nsec <= 999999999;
50 /* If times==NULL, set access and modification to current time,
51 * must be owner or have write permission.
52 * Else, update from *times, must be owner or super user.
54 long do_utimes(int dfd, char __user *filename, struct timespec *times, int flags)
58 struct dentry *dentry;
60 struct iattr newattrs;
61 struct file *f = NULL;
64 if (times && (!nsec_valid(times[0].tv_nsec) ||
65 !nsec_valid(times[1].tv_nsec))) {
69 if (flags & ~AT_SYMLINK_NOFOLLOW)
72 if (filename == NULL && dfd != AT_FDCWD) {
74 if (flags & AT_SYMLINK_NOFOLLOW)
81 dentry = f->f_path.dentry;
83 error = __user_walk_fd(dfd, filename, (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW, &nd);
87 dentry = nd.path.dentry;
90 inode = dentry->d_inode;
96 /* Don't worry, the checks are done in inode_change_ok() */
97 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
100 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
103 if (times[0].tv_nsec == UTIME_OMIT)
104 newattrs.ia_valid &= ~ATTR_ATIME;
105 else if (times[0].tv_nsec != UTIME_NOW) {
106 newattrs.ia_atime.tv_sec = times[0].tv_sec;
107 newattrs.ia_atime.tv_nsec = times[0].tv_nsec;
108 newattrs.ia_valid |= ATTR_ATIME_SET;
111 if (times[1].tv_nsec == UTIME_OMIT)
112 newattrs.ia_valid &= ~ATTR_MTIME;
113 else if (times[1].tv_nsec != UTIME_NOW) {
114 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
115 newattrs.ia_mtime.tv_nsec = times[1].tv_nsec;
116 newattrs.ia_valid |= ATTR_MTIME_SET;
120 if (IS_IMMUTABLE(inode))
123 if (!is_owner_or_cap(inode)) {
125 if (!(f->f_mode & FMODE_WRITE))
128 error = vfs_permission(&nd, MAY_WRITE);
134 mutex_lock(&inode->i_mutex);
135 error = notify_change(dentry, &newattrs);
136 mutex_unlock(&inode->i_mutex);
146 asmlinkage long sys_utimensat(int dfd, char __user *filename, struct timespec __user *utimes, int flags)
148 struct timespec tstimes[2];
151 if (copy_from_user(&tstimes, utimes, sizeof(tstimes)))
153 if ((tstimes[0].tv_nsec == UTIME_OMIT ||
154 tstimes[0].tv_nsec == UTIME_NOW) &&
155 tstimes[0].tv_sec != 0)
157 if ((tstimes[1].tv_nsec == UTIME_OMIT ||
158 tstimes[1].tv_nsec == UTIME_NOW) &&
159 tstimes[1].tv_sec != 0)
162 /* Nothing to do, we must not even check the path. */
163 if (tstimes[0].tv_nsec == UTIME_OMIT &&
164 tstimes[1].tv_nsec == UTIME_OMIT)
168 return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
171 asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
173 struct timeval times[2];
174 struct timespec tstimes[2];
177 if (copy_from_user(×, utimes, sizeof(times)))
180 /* This test is needed to catch all invalid values. If we
181 would test only in do_utimes we would miss those invalid
182 values truncated by the multiplication with 1000. Note
183 that we also catch UTIME_{NOW,OMIT} here which are only
184 valid for utimensat. */
185 if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
186 times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
189 tstimes[0].tv_sec = times[0].tv_sec;
190 tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
191 tstimes[1].tv_sec = times[1].tv_sec;
192 tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
195 return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
198 asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
200 return sys_futimesat(AT_FDCWD, filename, utimes);