blob: 480f7c8c29da13ee10941f5cf5e560faffbde0a6 [file] [log] [blame]
Alexey Dobriyan82b05472006-09-30 23:27:22 -07001#include <linux/compiler.h>
Ulrich Drepper1c710c82007-05-08 00:33:25 -07002#include <linux/file.h>
Alexey Dobriyan82b05472006-09-30 23:27:22 -07003#include <linux/fs.h>
4#include <linux/linkage.h>
5#include <linux/namei.h>
Al Viro914e2632006-10-18 13:55:46 -04006#include <linux/sched.h>
Ulrich Drepper1c710c82007-05-08 00:33:25 -07007#include <linux/stat.h>
Alexey Dobriyan82b05472006-09-30 23:27:22 -07008#include <linux/utime.h>
9#include <asm/uaccess.h>
10#include <asm/unistd.h>
11
12#ifdef __ARCH_WANT_SYS_UTIME
13
14/*
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
18 * need it).
19 */
20
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.
24 */
Ulrich Drepper1c710c82007-05-08 00:33:25 -070025asmlinkage long sys_utime(char __user *filename, struct utimbuf __user *times)
Alexey Dobriyan82b05472006-09-30 23:27:22 -070026{
Ulrich Drepper1c710c82007-05-08 00:33:25 -070027 struct timespec tv[2];
Alexey Dobriyan82b05472006-09-30 23:27:22 -070028
Alexey Dobriyan82b05472006-09-30 23:27:22 -070029 if (times) {
Ulrich Drepper1c710c82007-05-08 00:33:25 -070030 if (get_user(tv[0].tv_sec, &times->actime) ||
31 get_user(tv[1].tv_sec, &times->modtime))
32 return -EFAULT;
33 tv[0].tv_nsec = 0;
34 tv[1].tv_nsec = 0;
Alexey Dobriyan82b05472006-09-30 23:27:22 -070035 }
Ulrich Drepper1c710c82007-05-08 00:33:25 -070036 return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
Alexey Dobriyan82b05472006-09-30 23:27:22 -070037}
38
39#endif
40
41/* If times==NULL, set access and modification to current time,
42 * must be owner or have write permission.
43 * Else, update from *times, must be owner or super user.
44 */
Ulrich Drepper1c710c82007-05-08 00:33:25 -070045long do_utimes(int dfd, char __user *filename, struct timespec *times, int flags)
Alexey Dobriyan82b05472006-09-30 23:27:22 -070046{
47 int error;
48 struct nameidata nd;
Ulrich Drepper1c710c82007-05-08 00:33:25 -070049 struct dentry *dentry;
50 struct inode *inode;
Alexey Dobriyan82b05472006-09-30 23:27:22 -070051 struct iattr newattrs;
Ulrich Drepper1c710c82007-05-08 00:33:25 -070052 struct file *f = NULL;
Alexey Dobriyan82b05472006-09-30 23:27:22 -070053
Ulrich Drepper1c710c82007-05-08 00:33:25 -070054 error = -EINVAL;
55 if (flags & ~AT_SYMLINK_NOFOLLOW)
Alexey Dobriyan82b05472006-09-30 23:27:22 -070056 goto out;
Ulrich Drepper1c710c82007-05-08 00:33:25 -070057
58 if (filename == NULL && dfd != AT_FDCWD) {
59 error = -EINVAL;
60 if (flags & AT_SYMLINK_NOFOLLOW)
61 goto out;
62
63 error = -EBADF;
64 f = fget(dfd);
65 if (!f)
66 goto out;
67 dentry = f->f_path.dentry;
68 } else {
69 error = __user_walk_fd(dfd, filename, (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW, &nd);
70 if (error)
71 goto out;
72
73 dentry = nd.dentry;
74 }
75
76 inode = dentry->d_inode;
Alexey Dobriyan82b05472006-09-30 23:27:22 -070077
78 error = -EROFS;
79 if (IS_RDONLY(inode))
80 goto dput_and_out;
81
82 /* Don't worry, the checks are done in inode_change_ok() */
83 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
84 if (times) {
85 error = -EPERM;
86 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
87 goto dput_and_out;
88
Ulrich Drepper1c710c82007-05-08 00:33:25 -070089 if (times[0].tv_nsec == UTIME_OMIT)
90 newattrs.ia_valid &= ~ATTR_ATIME;
91 else if (times[0].tv_nsec != UTIME_NOW) {
92 newattrs.ia_atime.tv_sec = times[0].tv_sec;
93 newattrs.ia_atime.tv_nsec = times[0].tv_nsec;
94 newattrs.ia_valid |= ATTR_ATIME_SET;
95 }
96
97 if (times[1].tv_nsec == UTIME_OMIT)
98 newattrs.ia_valid &= ~ATTR_MTIME;
99 else if (times[1].tv_nsec != UTIME_NOW) {
100 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
101 newattrs.ia_mtime.tv_nsec = times[1].tv_nsec;
102 newattrs.ia_valid |= ATTR_MTIME_SET;
103 }
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700104 } else {
105 error = -EACCES;
106 if (IS_IMMUTABLE(inode))
107 goto dput_and_out;
108
109 if (current->fsuid != inode->i_uid &&
110 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
111 goto dput_and_out;
112 }
113 mutex_lock(&inode->i_mutex);
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700114 error = notify_change(dentry, &newattrs);
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700115 mutex_unlock(&inode->i_mutex);
116dput_and_out:
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700117 if (f)
118 fput(f);
119 else
120 path_release(&nd);
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700121out:
122 return error;
123}
124
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700125asmlinkage long sys_utimensat(int dfd, char __user *filename, struct timespec __user *utimes, int flags)
126{
127 struct timespec tstimes[2];
128
129 if (utimes) {
130 if (copy_from_user(&tstimes, utimes, sizeof(tstimes)))
131 return -EFAULT;
132 if ((tstimes[0].tv_nsec == UTIME_OMIT ||
133 tstimes[0].tv_nsec == UTIME_NOW) &&
134 tstimes[0].tv_sec != 0)
135 return -EINVAL;
136 if ((tstimes[1].tv_nsec == UTIME_OMIT ||
137 tstimes[1].tv_nsec == UTIME_NOW) &&
138 tstimes[1].tv_sec != 0)
139 return -EINVAL;
140
141 /* Nothing to do, we must not even check the path. */
142 if (tstimes[0].tv_nsec == UTIME_OMIT &&
143 tstimes[1].tv_nsec == UTIME_OMIT)
144 return 0;
145 }
146
147 return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
148}
149
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700150asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
151{
152 struct timeval times[2];
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700153 struct timespec tstimes[2];
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700154
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700155 if (utimes) {
156 if (copy_from_user(&times, utimes, sizeof(times)))
157 return -EFAULT;
158
159 /* This test is needed to catch all invalid values. If we
160 would test only in do_utimes we would miss those invalid
161 values truncated by the multiplication with 1000. Note
162 that we also catch UTIME_{NOW,OMIT} here which are only
163 valid for utimensat. */
164 if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
165 times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
166 return -EINVAL;
167
168 tstimes[0].tv_sec = times[0].tv_sec;
169 tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
170 tstimes[1].tv_sec = times[1].tv_sec;
171 tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
172 }
173
174 return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700175}
176
177asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
178{
179 return sys_futimesat(AT_FDCWD, filename, utimes);
180}