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/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(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 (!is_owner_or_cap(inode
))
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 (!is_owner_or_cap(inode
))
64 EXPORT_SYMBOL(inode_change_ok
);
66 int inode_setattr(struct inode
* inode
, struct iattr
* attr
)
68 unsigned int ia_valid
= attr
->ia_valid
;
70 if (ia_valid
& ATTR_SIZE
&&
71 attr
->ia_size
!= i_size_read(inode
)) {
72 int error
= vmtruncate(inode
, attr
->ia_size
);
77 if (ia_valid
& ATTR_UID
)
78 inode
->i_uid
= attr
->ia_uid
;
79 if (ia_valid
& ATTR_GID
)
80 inode
->i_gid
= attr
->ia_gid
;
81 if (ia_valid
& ATTR_ATIME
)
82 inode
->i_atime
= timespec_trunc(attr
->ia_atime
,
83 inode
->i_sb
->s_time_gran
);
84 if (ia_valid
& ATTR_MTIME
)
85 inode
->i_mtime
= timespec_trunc(attr
->ia_mtime
,
86 inode
->i_sb
->s_time_gran
);
87 if (ia_valid
& ATTR_CTIME
)
88 inode
->i_ctime
= timespec_trunc(attr
->ia_ctime
,
89 inode
->i_sb
->s_time_gran
);
90 if (ia_valid
& ATTR_MODE
) {
91 umode_t mode
= attr
->ia_mode
;
93 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
97 mark_inode_dirty(inode
);
101 EXPORT_SYMBOL(inode_setattr
);
103 int notify_change(struct dentry
* dentry
, struct iattr
* attr
)
105 struct inode
*inode
= dentry
->d_inode
;
109 unsigned int ia_valid
= attr
->ia_valid
;
111 mode
= inode
->i_mode
;
112 now
= current_fs_time(inode
->i_sb
);
114 attr
->ia_ctime
= now
;
115 if (!(ia_valid
& ATTR_ATIME_SET
))
116 attr
->ia_atime
= now
;
117 if (!(ia_valid
& ATTR_MTIME_SET
))
118 attr
->ia_mtime
= now
;
119 if (ia_valid
& ATTR_KILL_PRIV
) {
120 attr
->ia_valid
&= ~ATTR_KILL_PRIV
;
121 ia_valid
&= ~ATTR_KILL_PRIV
;
122 error
= security_inode_need_killpriv(dentry
);
124 error
= security_inode_killpriv(dentry
);
128 if (ia_valid
& ATTR_KILL_SUID
) {
129 attr
->ia_valid
&= ~ATTR_KILL_SUID
;
130 if (mode
& S_ISUID
) {
131 if (!(ia_valid
& ATTR_MODE
)) {
132 ia_valid
= attr
->ia_valid
|= ATTR_MODE
;
133 attr
->ia_mode
= inode
->i_mode
;
135 attr
->ia_mode
&= ~S_ISUID
;
138 if (ia_valid
& ATTR_KILL_SGID
) {
139 attr
->ia_valid
&= ~ ATTR_KILL_SGID
;
140 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
141 if (!(ia_valid
& ATTR_MODE
)) {
142 ia_valid
= attr
->ia_valid
|= ATTR_MODE
;
143 attr
->ia_mode
= inode
->i_mode
;
145 attr
->ia_mode
&= ~S_ISGID
;
151 if (ia_valid
& ATTR_SIZE
)
152 down_write(&dentry
->d_inode
->i_alloc_sem
);
154 if (inode
->i_op
&& inode
->i_op
->setattr
) {
155 error
= security_inode_setattr(dentry
, attr
);
157 error
= inode
->i_op
->setattr(dentry
, attr
);
159 error
= inode_change_ok(inode
, attr
);
161 error
= security_inode_setattr(dentry
, attr
);
163 if ((ia_valid
& ATTR_UID
&& attr
->ia_uid
!= inode
->i_uid
) ||
164 (ia_valid
& ATTR_GID
&& attr
->ia_gid
!= inode
->i_gid
))
165 error
= DQUOT_TRANSFER(inode
, attr
) ? -EDQUOT
: 0;
167 error
= inode_setattr(inode
, attr
);
171 if (ia_valid
& ATTR_SIZE
)
172 up_write(&dentry
->d_inode
->i_alloc_sem
);
175 fsnotify_change(dentry
, ia_valid
);
180 EXPORT_SYMBOL(notify_change
);