mm-only debug patch...
[mmotm.git] / fs / attr.c
blob370642a99db52e0fe1a4711212a18e286b47a805
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/capability.h>
13 #include <linux/fsnotify.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(const 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 (!is_owner_or_cap(inode))
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 | ATTR_TIMES_SET)) {
55 if (!is_owner_or_cap(inode))
56 goto error;
58 fine:
59 retval = 0;
60 error:
61 return retval;
63 EXPORT_SYMBOL(inode_change_ok);
65 /**
66 * inode_newsize_ok - may this inode be truncated to a given size
67 * @inode: the inode to be truncated
68 * @offset: the new size to assign to the inode
69 * @Returns: 0 on success, -ve errno on failure
71 * inode_newsize_ok will check filesystem limits and ulimits to check that the
72 * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
73 * when necessary. Caller must not proceed with inode size change if failure is
74 * returned. @inode must be a file (not directory), with appropriate
75 * permissions to allow truncate (inode_newsize_ok does NOT check these
76 * conditions).
78 * inode_newsize_ok must be called with i_mutex held.
80 int inode_newsize_ok(const struct inode *inode, loff_t offset)
82 if (inode->i_size < offset) {
83 unsigned long limit;
85 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
86 if (limit != RLIM_INFINITY && offset > limit)
87 goto out_sig;
88 if (offset > inode->i_sb->s_maxbytes)
89 goto out_big;
90 } else {
92 * truncation of in-use swapfiles is disallowed - it would
93 * cause subsequent swapout to scribble on the now-freed
94 * blocks.
96 if (IS_SWAPFILE(inode))
97 return -ETXTBSY;
100 return 0;
101 out_sig:
102 send_sig(SIGXFSZ, current, 0);
103 out_big:
104 return -EFBIG;
106 EXPORT_SYMBOL(inode_newsize_ok);
108 int inode_setattr(struct inode * inode, struct iattr * attr)
110 unsigned int ia_valid = attr->ia_valid;
112 if (ia_valid & ATTR_SIZE &&
113 attr->ia_size != i_size_read(inode)) {
114 int error = vmtruncate(inode, attr->ia_size);
115 if (error)
116 return error;
119 if (ia_valid & ATTR_UID)
120 inode->i_uid = attr->ia_uid;
121 if (ia_valid & ATTR_GID)
122 inode->i_gid = attr->ia_gid;
123 if (ia_valid & ATTR_ATIME)
124 inode->i_atime = timespec_trunc(attr->ia_atime,
125 inode->i_sb->s_time_gran);
126 if (ia_valid & ATTR_MTIME)
127 inode->i_mtime = timespec_trunc(attr->ia_mtime,
128 inode->i_sb->s_time_gran);
129 if (ia_valid & ATTR_CTIME)
130 inode->i_ctime = timespec_trunc(attr->ia_ctime,
131 inode->i_sb->s_time_gran);
132 if (ia_valid & ATTR_MODE) {
133 umode_t mode = attr->ia_mode;
135 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
136 mode &= ~S_ISGID;
137 inode->i_mode = mode;
139 mark_inode_dirty(inode);
141 return 0;
143 EXPORT_SYMBOL(inode_setattr);
145 int notify_change(struct dentry * dentry, struct iattr * attr)
147 struct inode *inode = dentry->d_inode;
148 mode_t mode = inode->i_mode;
149 int error;
150 struct timespec now;
151 unsigned int ia_valid = attr->ia_valid;
153 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
154 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
155 return -EPERM;
158 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));
160 now = current_fs_time(inode->i_sb);
162 attr->ia_ctime = now;
163 if (!(ia_valid & ATTR_ATIME_SET))
164 attr->ia_atime = now;
165 if (!(ia_valid & ATTR_MTIME_SET))
166 attr->ia_mtime = now;
167 if (ia_valid & ATTR_KILL_PRIV) {
168 attr->ia_valid &= ~ATTR_KILL_PRIV;
169 ia_valid &= ~ATTR_KILL_PRIV;
170 error = security_inode_need_killpriv(dentry);
171 if (error > 0)
172 error = security_inode_killpriv(dentry);
173 if (error)
174 return error;
178 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
179 * that the function has the ability to reinterpret a mode change
180 * that's due to these bits. This adds an implicit restriction that
181 * no function will ever call notify_change with both ATTR_MODE and
182 * ATTR_KILL_S*ID set.
184 if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
185 (ia_valid & ATTR_MODE))
186 BUG();
188 if (ia_valid & ATTR_KILL_SUID) {
189 if (mode & S_ISUID) {
190 ia_valid = attr->ia_valid |= ATTR_MODE;
191 attr->ia_mode = (inode->i_mode & ~S_ISUID);
194 if (ia_valid & ATTR_KILL_SGID) {
195 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
196 if (!(ia_valid & ATTR_MODE)) {
197 ia_valid = attr->ia_valid |= ATTR_MODE;
198 attr->ia_mode = inode->i_mode;
200 attr->ia_mode &= ~S_ISGID;
203 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
204 return 0;
206 error = security_inode_setattr(dentry, attr);
207 if (error)
208 return error;
210 if (ia_valid & ATTR_SIZE)
211 down_write(&dentry->d_inode->i_alloc_sem);
213 if (inode->i_op && inode->i_op->setattr) {
214 error = inode->i_op->setattr(dentry, attr);
215 } else {
216 error = inode_change_ok(inode, attr);
217 if (!error) {
218 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
219 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid))
220 error = vfs_dq_transfer(inode, attr) ?
221 -EDQUOT : 0;
222 if (!error)
223 error = inode_setattr(inode, attr);
227 if (ia_valid & ATTR_SIZE)
228 up_write(&dentry->d_inode->i_alloc_sem);
230 if (!error)
231 fsnotify_change(dentry, ia_valid);
233 return error;
236 EXPORT_SYMBOL(notify_change);