MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / attr.c
blob8acb28910d09f661f86343c14596a4ae9b387d26
1 /*
2 * linux/fs/attr.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * changes by Thomas Schoebel-Theuer
6 */
8 #include <linux/module.h>
9 #include <linux/time.h>
10 #include <linux/mm.h>
11 #include <linux/string.h>
12 #include <linux/smp_lock.h>
13 #include <linux/dnotify.h>
14 #include <linux/fcntl.h>
15 #include <linux/quotaops.h>
16 #include <linux/security.h>
18 /* Taken over from the old code... */
20 /* POSIX UID/GID verification for setting inode attributes. */
21 int inode_change_ok(struct inode *inode, struct iattr *attr)
23 int retval = -EPERM;
24 unsigned int ia_valid = attr->ia_valid;
26 /* If force is set do it anyway. */
27 if (ia_valid & ATTR_FORCE)
28 goto fine;
30 /* Make sure a caller can chown. */
31 if ((ia_valid & ATTR_UID) &&
32 (current->fsuid != inode->i_uid ||
33 attr->ia_uid != inode->i_uid) && !capable(CAP_CHOWN))
34 goto error;
36 /* Make sure caller can chgrp. */
37 if ((ia_valid & ATTR_GID) &&
38 (current->fsuid != inode->i_uid ||
39 (!in_group_p(attr->ia_gid) && attr->ia_gid != inode->i_gid)) &&
40 !capable(CAP_CHOWN))
41 goto error;
43 /* Make sure a caller can chmod. */
44 if (ia_valid & ATTR_MODE) {
45 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
46 goto error;
47 /* Also check the setgid bit! */
48 if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
49 inode->i_gid) && !capable(CAP_FSETID))
50 attr->ia_mode &= ~S_ISGID;
53 /* Check for setting the inode time. */
54 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
55 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
56 goto error;
58 fine:
59 retval = 0;
60 error:
61 return retval;
64 EXPORT_SYMBOL(inode_change_ok);
66 int inode_setattr(struct inode * inode, struct iattr * attr)
68 unsigned int ia_valid = attr->ia_valid;
69 int error = 0;
71 if (ia_valid & ATTR_SIZE) {
72 if (attr->ia_size != i_size_read(inode)) {
73 error = vmtruncate(inode, attr->ia_size);
74 if (error || (ia_valid == ATTR_SIZE))
75 goto out;
76 } else {
78 * We skipped the truncate but must still update
79 * timestamps
81 ia_valid |= ATTR_MTIME|ATTR_CTIME;
85 if (ia_valid & ATTR_UID)
86 inode->i_uid = attr->ia_uid;
87 if (ia_valid & ATTR_GID)
88 inode->i_gid = attr->ia_gid;
89 if (ia_valid & ATTR_ATIME)
90 inode->i_atime = attr->ia_atime;
91 if (ia_valid & ATTR_MTIME)
92 inode->i_mtime = attr->ia_mtime;
93 if (ia_valid & ATTR_CTIME)
94 inode->i_ctime = attr->ia_ctime;
95 if (ia_valid & ATTR_MODE) {
96 umode_t mode = attr->ia_mode;
98 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
99 mode &= ~S_ISGID;
100 inode->i_mode = mode;
102 mark_inode_dirty(inode);
103 out:
104 return error;
107 EXPORT_SYMBOL(inode_setattr);
109 int setattr_mask(unsigned int ia_valid)
111 unsigned long dn_mask = 0;
113 if (ia_valid & ATTR_UID)
114 dn_mask |= DN_ATTRIB;
115 if (ia_valid & ATTR_GID)
116 dn_mask |= DN_ATTRIB;
117 if (ia_valid & ATTR_SIZE)
118 dn_mask |= DN_MODIFY;
119 /* both times implies a utime(s) call */
120 if ((ia_valid & (ATTR_ATIME|ATTR_MTIME)) == (ATTR_ATIME|ATTR_MTIME))
121 dn_mask |= DN_ATTRIB;
122 else if (ia_valid & ATTR_ATIME)
123 dn_mask |= DN_ACCESS;
124 else if (ia_valid & ATTR_MTIME)
125 dn_mask |= DN_MODIFY;
126 if (ia_valid & ATTR_MODE)
127 dn_mask |= DN_ATTRIB;
128 return dn_mask;
131 int notify_change(struct dentry * dentry, struct iattr * attr)
133 struct inode *inode = dentry->d_inode;
134 mode_t mode = inode->i_mode;
135 int error;
136 struct timespec now = CURRENT_TIME;
137 unsigned int ia_valid = attr->ia_valid;
139 if (!inode)
140 BUG();
142 attr->ia_ctime = now;
143 if (!(ia_valid & ATTR_ATIME_SET))
144 attr->ia_atime = now;
145 if (!(ia_valid & ATTR_MTIME_SET))
146 attr->ia_mtime = now;
147 if (ia_valid & ATTR_KILL_SUID) {
148 attr->ia_valid &= ~ATTR_KILL_SUID;
149 if (mode & S_ISUID) {
150 if (!(ia_valid & ATTR_MODE)) {
151 ia_valid = attr->ia_valid |= ATTR_MODE;
152 attr->ia_mode = inode->i_mode;
154 attr->ia_mode &= ~S_ISUID;
157 if (ia_valid & ATTR_KILL_SGID) {
158 attr->ia_valid &= ~ ATTR_KILL_SGID;
159 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
160 if (!(ia_valid & ATTR_MODE)) {
161 ia_valid = attr->ia_valid |= ATTR_MODE;
162 attr->ia_mode = inode->i_mode;
164 attr->ia_mode &= ~S_ISGID;
167 if (!attr->ia_valid)
168 return 0;
170 if (inode->i_op && inode->i_op->setattr) {
171 error = security_inode_setattr(dentry, attr);
172 if (!error)
173 error = inode->i_op->setattr(dentry, attr);
174 } else {
175 error = inode_change_ok(inode, attr);
176 if (!error)
177 error = security_inode_setattr(dentry, attr);
178 if (!error) {
179 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
180 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid))
181 error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0;
182 if (!error)
183 error = inode_setattr(inode, attr);
186 if (!error) {
187 unsigned long dn_mask = setattr_mask(ia_valid);
188 if (dn_mask)
189 dnotify_parent(dentry, dn_mask);
191 return error;
194 EXPORT_SYMBOL(notify_change);