blob: f8dfc2269d85634aa61ebb19a761e986ea0bbc93 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/attr.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * changes by Thomas Schoebel-Theuer
6 */
7
8#include <linux/module.h>
9#include <linux/time.h>
10#include <linux/mm.h>
11#include <linux/string.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080012#include <linux/capability.h>
Robert Love0eeca282005-07-12 17:06:03 -040013#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/fcntl.h>
15#include <linux/quotaops.h>
16#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18/* Taken over from the old code... */
19
20/* POSIX UID/GID verification for setting inode attributes. */
21int inode_change_ok(struct inode *inode, struct iattr *attr)
22{
23 int retval = -EPERM;
24 unsigned int ia_valid = attr->ia_valid;
25
26 /* If force is set do it anyway. */
27 if (ia_valid & ATTR_FORCE)
28 goto fine;
29
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;
35
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;
42
43 /* Make sure a caller can chmod. */
44 if (ia_valid & ATTR_MODE) {
Satyam Sharma3bd858a2007-07-17 15:00:08 +053045 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 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;
51 }
52
53 /* Check for setting the inode time. */
54 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
Satyam Sharma3bd858a2007-07-17 15:00:08 +053055 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 goto error;
57 }
58fine:
59 retval = 0;
60error:
61 return retval;
62}
63
64EXPORT_SYMBOL(inode_change_ok);
65
66int inode_setattr(struct inode * inode, struct iattr * attr)
67{
68 unsigned int ia_valid = attr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
NeilBrown4a301312006-01-08 01:02:39 -080070 if (ia_valid & ATTR_SIZE &&
71 attr->ia_size != i_size_read(inode)) {
72 int error = vmtruncate(inode, attr->ia_size);
73 if (error)
74 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76
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;
92
93 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
94 mode &= ~S_ISGID;
95 inode->i_mode = mode;
96 }
97 mark_inode_dirty(inode);
NeilBrown4a301312006-01-08 01:02:39 -080098
99 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101EXPORT_SYMBOL(inode_setattr);
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103int notify_change(struct dentry * dentry, struct iattr * attr)
104{
105 struct inode *inode = dentry->d_inode;
106 mode_t mode;
107 int error;
108 struct timespec now;
109 unsigned int ia_valid = attr->ia_valid;
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 mode = inode->i_mode;
112 now = current_fs_time(inode->i_sb);
113
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_SUID) {
120 attr->ia_valid &= ~ATTR_KILL_SUID;
121 if (mode & S_ISUID) {
122 if (!(ia_valid & ATTR_MODE)) {
123 ia_valid = attr->ia_valid |= ATTR_MODE;
124 attr->ia_mode = inode->i_mode;
125 }
126 attr->ia_mode &= ~S_ISUID;
127 }
128 }
129 if (ia_valid & ATTR_KILL_SGID) {
130 attr->ia_valid &= ~ ATTR_KILL_SGID;
131 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
132 if (!(ia_valid & ATTR_MODE)) {
133 ia_valid = attr->ia_valid |= ATTR_MODE;
134 attr->ia_mode = inode->i_mode;
135 }
136 attr->ia_mode &= ~S_ISGID;
137 }
138 }
139 if (!attr->ia_valid)
140 return 0;
141
142 if (ia_valid & ATTR_SIZE)
143 down_write(&dentry->d_inode->i_alloc_sem);
144
145 if (inode->i_op && inode->i_op->setattr) {
146 error = security_inode_setattr(dentry, attr);
147 if (!error)
148 error = inode->i_op->setattr(dentry, attr);
149 } else {
150 error = inode_change_ok(inode, attr);
151 if (!error)
152 error = security_inode_setattr(dentry, attr);
153 if (!error) {
154 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
155 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid))
156 error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0;
157 if (!error)
158 error = inode_setattr(inode, attr);
159 }
160 }
161
162 if (ia_valid & ATTR_SIZE)
163 up_write(&dentry->d_inode->i_alloc_sem);
164
Robert Love0eeca282005-07-12 17:06:03 -0400165 if (!error)
166 fsnotify_change(dentry, ia_valid);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return error;
169}
170
171EXPORT_SYMBOL(notify_change);