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 (!in_group_p(attr
->ia_gid
) && attr
->ia_gid
!= inode
->i_gid
) &&
42 /* Make sure a caller can chmod. */
43 if (ia_valid
& ATTR_MODE
) {
44 if ((current
->fsuid
!= inode
->i_uid
) && !capable(CAP_FOWNER
))
46 /* Also check the setgid bit! */
47 if (!in_group_p((ia_valid
& ATTR_GID
) ? attr
->ia_gid
:
48 inode
->i_gid
) && !capable(CAP_FSETID
))
49 attr
->ia_mode
&= ~S_ISGID
;
52 /* Check for setting the inode time. */
53 if (ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
)) {
54 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_FOWNER
))
63 EXPORT_SYMBOL(inode_change_ok
);
65 int inode_setattr(struct inode
* inode
, struct iattr
* attr
)
67 unsigned int ia_valid
= attr
->ia_valid
;
70 if (ia_valid
& ATTR_SIZE
) {
71 if (attr
->ia_size
!= i_size_read(inode
)) {
72 error
= vmtruncate(inode
, attr
->ia_size
);
73 if (error
|| (ia_valid
== ATTR_SIZE
))
77 * We skipped the truncate but must still update
80 ia_valid
|= ATTR_MTIME
|ATTR_CTIME
;
84 if (ia_valid
& ATTR_UID
)
85 inode
->i_uid
= attr
->ia_uid
;
86 if (ia_valid
& ATTR_GID
)
87 inode
->i_gid
= attr
->ia_gid
;
88 if (ia_valid
& ATTR_ATIME
)
89 inode
->i_atime
= attr
->ia_atime
;
90 if (ia_valid
& ATTR_MTIME
)
91 inode
->i_mtime
= attr
->ia_mtime
;
92 if (ia_valid
& ATTR_CTIME
)
93 inode
->i_ctime
= attr
->ia_ctime
;
94 if (ia_valid
& ATTR_MODE
) {
95 umode_t mode
= attr
->ia_mode
;
97 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
101 mark_inode_dirty(inode
);
106 EXPORT_SYMBOL(inode_setattr
);
108 int setattr_mask(unsigned int ia_valid
)
110 unsigned long dn_mask
= 0;
112 if (ia_valid
& ATTR_UID
)
113 dn_mask
|= DN_ATTRIB
;
114 if (ia_valid
& ATTR_GID
)
115 dn_mask
|= DN_ATTRIB
;
116 if (ia_valid
& ATTR_SIZE
)
117 dn_mask
|= DN_MODIFY
;
118 /* both times implies a utime(s) call */
119 if ((ia_valid
& (ATTR_ATIME
|ATTR_MTIME
)) == (ATTR_ATIME
|ATTR_MTIME
))
120 dn_mask
|= DN_ATTRIB
;
121 else if (ia_valid
& ATTR_ATIME
)
122 dn_mask
|= DN_ACCESS
;
123 else if (ia_valid
& ATTR_MTIME
)
124 dn_mask
|= DN_MODIFY
;
125 if (ia_valid
& ATTR_MODE
)
126 dn_mask
|= DN_ATTRIB
;
130 int notify_change(struct dentry
* dentry
, struct iattr
* attr
)
132 struct inode
*inode
= dentry
->d_inode
;
133 mode_t mode
= inode
->i_mode
;
135 struct timespec now
= CURRENT_TIME
;
136 unsigned int ia_valid
= attr
->ia_valid
;
141 attr
->ia_ctime
= now
;
142 if (!(ia_valid
& ATTR_ATIME_SET
))
143 attr
->ia_atime
= now
;
144 if (!(ia_valid
& ATTR_MTIME_SET
))
145 attr
->ia_mtime
= now
;
146 if (ia_valid
& ATTR_KILL_SUID
) {
147 attr
->ia_valid
&= ~ATTR_KILL_SUID
;
148 if (mode
& S_ISUID
) {
149 if (!(ia_valid
& ATTR_MODE
)) {
150 ia_valid
= attr
->ia_valid
|= ATTR_MODE
;
151 attr
->ia_mode
= inode
->i_mode
;
153 attr
->ia_mode
&= ~S_ISUID
;
156 if (ia_valid
& ATTR_KILL_SGID
) {
157 attr
->ia_valid
&= ~ ATTR_KILL_SGID
;
158 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
159 if (!(ia_valid
& ATTR_MODE
)) {
160 ia_valid
= attr
->ia_valid
|= ATTR_MODE
;
161 attr
->ia_mode
= inode
->i_mode
;
163 attr
->ia_mode
&= ~S_ISGID
;
169 if (inode
->i_op
&& inode
->i_op
->setattr
) {
170 error
= security_inode_setattr(dentry
, attr
);
172 error
= inode
->i_op
->setattr(dentry
, attr
);
174 error
= inode_change_ok(inode
, attr
);
176 error
= security_inode_setattr(dentry
, attr
);
178 if ((ia_valid
& ATTR_UID
&& attr
->ia_uid
!= inode
->i_uid
) ||
179 (ia_valid
& ATTR_GID
&& attr
->ia_gid
!= inode
->i_gid
))
180 error
= DQUOT_TRANSFER(inode
, attr
) ? -EDQUOT
: 0;
182 error
= inode_setattr(inode
, attr
);
186 unsigned long dn_mask
= setattr_mask(ia_valid
);
188 dnotify_parent(dentry
, dn_mask
);
193 EXPORT_SYMBOL(notify_change
);