Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / fs / utimes.c
blob80a481ea3419443fbce79b5ebeb22a6b63fd0a65
1 #include <linux/compiler.h>
2 #include <linux/file.h>
3 #include <linux/fs.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
19 * need it).
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];
30 if (times) {
31 if (get_user(tv[0].tv_sec, &times->actime) ||
32 get_user(tv[1].tv_sec, &times->modtime))
33 return -EFAULT;
34 tv[0].tv_nsec = 0;
35 tv[1].tv_nsec = 0;
37 return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
40 #endif
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))
50 return true;
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)
61 int error;
62 struct nameidata nd;
63 struct dentry *dentry;
64 struct inode *inode;
65 struct iattr newattrs;
66 struct file *f = NULL;
68 error = -EINVAL;
69 if (times && (!nsec_valid(times[0].tv_nsec) ||
70 !nsec_valid(times[1].tv_nsec))) {
71 goto out;
74 if (flags & ~AT_SYMLINK_NOFOLLOW)
75 goto out;
77 if (filename == NULL && dfd != AT_FDCWD) {
78 error = -EINVAL;
79 if (flags & AT_SYMLINK_NOFOLLOW)
80 goto out;
82 error = -EBADF;
83 f = fget(dfd);
84 if (!f)
85 goto out;
86 dentry = f->f_path.dentry;
87 } else {
88 error = __user_walk_fd(dfd, filename, (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW, &nd);
89 if (error)
90 goto out;
92 dentry = nd.path.dentry;
95 inode = dentry->d_inode;
97 error = -EROFS;
98 if (IS_RDONLY(inode))
99 goto dput_and_out;
101 /* Don't worry, the checks are done in inode_change_ok() */
102 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
103 if (times) {
104 error = -EPERM;
105 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
106 goto dput_and_out;
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))) {
132 error = -EACCES;
133 if (IS_IMMUTABLE(inode))
134 goto dput_and_out;
136 if (!is_owner_or_cap(inode)) {
137 if (f) {
138 if (!(f->f_mode & FMODE_WRITE))
139 goto dput_and_out;
140 } else {
141 error = vfs_permission(&nd, MAY_WRITE);
142 if (error)
143 goto dput_and_out;
147 mutex_lock(&inode->i_mutex);
148 error = notify_change(dentry, &newattrs);
149 mutex_unlock(&inode->i_mutex);
150 dput_and_out:
151 if (f)
152 fput(f);
153 else
154 path_put(&nd.path);
155 out:
156 return error;
159 asmlinkage long sys_utimensat(int dfd, char __user *filename, struct timespec __user *utimes, int flags)
161 struct timespec tstimes[2];
163 if (utimes) {
164 if (copy_from_user(&tstimes, utimes, sizeof(tstimes)))
165 return -EFAULT;
166 if ((tstimes[0].tv_nsec == UTIME_OMIT ||
167 tstimes[0].tv_nsec == UTIME_NOW) &&
168 tstimes[0].tv_sec != 0)
169 return -EINVAL;
170 if ((tstimes[1].tv_nsec == UTIME_OMIT ||
171 tstimes[1].tv_nsec == UTIME_NOW) &&
172 tstimes[1].tv_sec != 0)
173 return -EINVAL;
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)
178 return 0;
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];
189 if (utimes) {
190 if (copy_from_user(&times, utimes, sizeof(times)))
191 return -EFAULT;
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)
200 return -EINVAL;
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);