4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * changes by Thomas Schoebel-Theuer
8 #include <linux/module.h>
9 #include <linux/time.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
)
24 unsigned int ia_valid
= attr
->ia_valid
;
26 /* If force is set do it anyway. */
27 if (ia_valid
& ATTR_FORCE
)
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
))
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
)) &&
43 /* Make sure a caller can chmod. */
44 if (ia_valid
& ATTR_MODE
) {
45 if ((current
->fsuid
!= inode
->i_uid
) && !capable(CAP_FOWNER
))
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
))
64 EXPORT_SYMBOL(inode_change_ok
);
66 int inode_setattr(struct inode
* inode
, struct iattr
* attr
)
68 unsigned int ia_valid
= attr
->ia_valid
;
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
))
78 * We skipped the truncate but must still update
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
))
100 inode
->i_mode
= mode
;
102 mark_inode_dirty(inode
);
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
;
131 int notify_change(struct dentry
* dentry
, struct iattr
* attr
)
133 struct inode
*inode
= dentry
->d_inode
;
134 mode_t mode
= inode
->i_mode
;
136 struct timespec now
= CURRENT_TIME
;
137 unsigned int ia_valid
= attr
->ia_valid
;
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
;
170 if (inode
->i_op
&& inode
->i_op
->setattr
) {
171 error
= security_inode_setattr(dentry
, attr
);
173 error
= inode
->i_op
->setattr(dentry
, attr
);
175 error
= inode_change_ok(inode
, attr
);
177 error
= security_inode_setattr(dentry
, attr
);
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;
183 error
= inode_setattr(inode
, attr
);
187 unsigned long dn_mask
= setattr_mask(ia_valid
);
189 dnotify_parent(dentry
, dn_mask
);
194 EXPORT_SYMBOL(notify_change
);