[PATCH] const struct tty_operations
[linux-2.6/linux-mips.git] / fs / utimes.c
blob1bcd852fc4a9509199bd334c510a788ca387c153
1 #include <linux/compiler.h>
2 #include <linux/fs.h>
3 #include <linux/linkage.h>
4 #include <linux/namei.h>
5 #include <linux/utime.h>
6 #include <asm/uaccess.h>
7 #include <asm/unistd.h>
9 #ifdef __ARCH_WANT_SYS_UTIME
12 * sys_utime() can be implemented in user-level using sys_utimes().
13 * Is this for backwards compatibility? If so, why not move it
14 * into the appropriate arch directory (for those architectures that
15 * need it).
18 /* If times==NULL, set access and modification to current time,
19 * must be owner or have write permission.
20 * Else, update from *times, must be owner or super user.
22 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
24 int error;
25 struct nameidata nd;
26 struct inode * inode;
27 struct iattr newattrs;
29 error = user_path_walk(filename, &nd);
30 if (error)
31 goto out;
32 inode = nd.dentry->d_inode;
34 error = -EROFS;
35 if (IS_RDONLY(inode))
36 goto dput_and_out;
38 /* Don't worry, the checks are done in inode_change_ok() */
39 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
40 if (times) {
41 error = -EPERM;
42 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
43 goto dput_and_out;
45 error = get_user(newattrs.ia_atime.tv_sec, &times->actime);
46 newattrs.ia_atime.tv_nsec = 0;
47 if (!error)
48 error = get_user(newattrs.ia_mtime.tv_sec, &times->modtime);
49 newattrs.ia_mtime.tv_nsec = 0;
50 if (error)
51 goto dput_and_out;
53 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
54 } else {
55 error = -EACCES;
56 if (IS_IMMUTABLE(inode))
57 goto dput_and_out;
59 if (current->fsuid != inode->i_uid &&
60 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
61 goto dput_and_out;
63 mutex_lock(&inode->i_mutex);
64 error = notify_change(nd.dentry, &newattrs);
65 mutex_unlock(&inode->i_mutex);
66 dput_and_out:
67 path_release(&nd);
68 out:
69 return error;
72 #endif
74 /* If times==NULL, set access and modification to current time,
75 * must be owner or have write permission.
76 * Else, update from *times, must be owner or super user.
78 long do_utimes(int dfd, char __user *filename, struct timeval *times)
80 int error;
81 struct nameidata nd;
82 struct inode * inode;
83 struct iattr newattrs;
85 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
87 if (error)
88 goto out;
89 inode = nd.dentry->d_inode;
91 error = -EROFS;
92 if (IS_RDONLY(inode))
93 goto dput_and_out;
95 /* Don't worry, the checks are done in inode_change_ok() */
96 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
97 if (times) {
98 error = -EPERM;
99 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
100 goto dput_and_out;
102 newattrs.ia_atime.tv_sec = times[0].tv_sec;
103 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
104 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
105 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
106 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
107 } else {
108 error = -EACCES;
109 if (IS_IMMUTABLE(inode))
110 goto dput_and_out;
112 if (current->fsuid != inode->i_uid &&
113 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
114 goto dput_and_out;
116 mutex_lock(&inode->i_mutex);
117 error = notify_change(nd.dentry, &newattrs);
118 mutex_unlock(&inode->i_mutex);
119 dput_and_out:
120 path_release(&nd);
121 out:
122 return error;
125 asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
127 struct timeval times[2];
129 if (utimes && copy_from_user(&times, utimes, sizeof(times)))
130 return -EFAULT;
131 return do_utimes(dfd, filename, utimes ? times : NULL);
134 asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
136 return sys_futimesat(AT_FDCWD, filename, utimes);