Commit | Line | Data |
---|---|---|
b3b94faa DT |
1 | /* |
2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
3a8a9a10 | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
b3b94faa DT |
4 | * |
5 | * This copyrighted material is made available to anyone wishing to use, | |
6 | * modify, copy, or redistribute it subject to the terms and conditions | |
e9fc2aa0 | 7 | * of the GNU General Public License version 2. |
b3b94faa DT |
8 | */ |
9 | ||
10 | #include <linux/sched.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/spinlock.h> | |
13 | #include <linux/completion.h> | |
14 | #include <linux/buffer_head.h> | |
2646a1f6 | 15 | #include <linux/xattr.h> |
b3b94faa DT |
16 | #include <linux/posix_acl.h> |
17 | #include <linux/posix_acl_xattr.h> | |
5c676f6d | 18 | #include <linux/gfs2_ondisk.h> |
b3b94faa DT |
19 | |
20 | #include "gfs2.h" | |
5c676f6d | 21 | #include "incore.h" |
b3b94faa | 22 | #include "acl.h" |
307cf6e6 | 23 | #include "xattr.h" |
b3b94faa DT |
24 | #include "glock.h" |
25 | #include "inode.h" | |
26 | #include "meta_io.h" | |
27 | #include "trans.h" | |
5c676f6d | 28 | #include "util.h" |
b3b94faa | 29 | |
479c427d | 30 | static const char *gfs2_acl_name(int type) |
b3b94faa | 31 | { |
479c427d SW |
32 | switch (type) { |
33 | case ACL_TYPE_ACCESS: | |
34 | return GFS2_POSIX_ACL_ACCESS; | |
35 | case ACL_TYPE_DEFAULT: | |
36 | return GFS2_POSIX_ACL_DEFAULT; | |
37 | } | |
38 | return NULL; | |
39 | } | |
b3b94faa | 40 | |
479c427d SW |
41 | static struct posix_acl *gfs2_acl_get(struct gfs2_inode *ip, int type) |
42 | { | |
43 | struct posix_acl *acl; | |
44 | const char *name; | |
45 | char *data; | |
46 | int len; | |
40b78a32 | 47 | |
3767ac21 | 48 | if (!ip->i_eattr) |
479c427d | 49 | return NULL; |
b3b94faa | 50 | |
106381bf SW |
51 | acl = get_cached_acl(&ip->i_inode, type); |
52 | if (acl != ACL_NOT_CACHED) | |
53 | return acl; | |
54 | ||
479c427d SW |
55 | name = gfs2_acl_name(type); |
56 | if (name == NULL) | |
57 | return ERR_PTR(-EINVAL); | |
b3b94faa | 58 | |
479c427d SW |
59 | len = gfs2_xattr_acl_get(ip, name, &data); |
60 | if (len < 0) | |
61 | return ERR_PTR(len); | |
62 | if (len == 0) | |
63 | return NULL; | |
b3b94faa | 64 | |
479c427d SW |
65 | acl = posix_acl_from_xattr(data, len); |
66 | kfree(data); | |
67 | return acl; | |
b3b94faa DT |
68 | } |
69 | ||
70 | /** | |
77386e1f | 71 | * gfs2_check_acl - Check an ACL to see if we're allowed to do something |
b3b94faa DT |
72 | * @inode: the file we want to do something to |
73 | * @mask: what we want to do | |
74 | * | |
75 | * Returns: errno | |
76 | */ | |
77 | ||
b74c79e9 | 78 | int gfs2_check_acl(struct inode *inode, int mask, unsigned int flags) |
b3b94faa | 79 | { |
479c427d | 80 | struct posix_acl *acl; |
b3b94faa DT |
81 | int error; |
82 | ||
75d5cfbe SW |
83 | if (flags & IPERM_FLAG_RCU) { |
84 | if (!negative_cached_acl(inode, ACL_TYPE_ACCESS)) | |
85 | return -ECHILD; | |
86 | return -EAGAIN; | |
87 | } | |
b74c79e9 | 88 | |
479c427d SW |
89 | acl = gfs2_acl_get(GFS2_I(inode), ACL_TYPE_ACCESS); |
90 | if (IS_ERR(acl)) | |
91 | return PTR_ERR(acl); | |
b3b94faa DT |
92 | |
93 | if (acl) { | |
94 | error = posix_acl_permission(inode, acl, mask); | |
95 | posix_acl_release(acl); | |
96 | return error; | |
97 | } | |
98 | ||
99 | return -EAGAIN; | |
100 | } | |
101 | ||
69dca424 | 102 | static int gfs2_set_mode(struct inode *inode, mode_t mode) |
b3b94faa | 103 | { |
69dca424 | 104 | int error = 0; |
b3b94faa | 105 | |
69dca424 SW |
106 | if (mode != inode->i_mode) { |
107 | struct iattr iattr; | |
b3b94faa | 108 | |
69dca424 SW |
109 | iattr.ia_valid = ATTR_MODE; |
110 | iattr.ia_mode = mode; | |
b3b94faa | 111 | |
69dca424 SW |
112 | error = gfs2_setattr_simple(GFS2_I(inode), &iattr); |
113 | } | |
b3b94faa | 114 | |
69dca424 | 115 | return error; |
b3b94faa DT |
116 | } |
117 | ||
479c427d | 118 | static int gfs2_acl_set(struct inode *inode, int type, struct posix_acl *acl) |
b3b94faa | 119 | { |
b3b94faa | 120 | int error; |
479c427d SW |
121 | int len; |
122 | char *data; | |
123 | const char *name = gfs2_acl_name(type); | |
124 | ||
125 | BUG_ON(name == NULL); | |
126 | len = posix_acl_to_xattr(acl, NULL, 0); | |
127 | if (len == 0) | |
128 | return 0; | |
129 | data = kmalloc(len, GFP_NOFS); | |
130 | if (data == NULL) | |
131 | return -ENOMEM; | |
132 | error = posix_acl_to_xattr(acl, data, len); | |
133 | if (error < 0) | |
134 | goto out; | |
431547b3 | 135 | error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS); |
106381bf SW |
136 | if (!error) |
137 | set_cached_acl(inode, type, acl); | |
479c427d SW |
138 | out: |
139 | kfree(data); | |
140 | return error; | |
141 | } | |
142 | ||
143 | int gfs2_acl_create(struct gfs2_inode *dip, struct inode *inode) | |
144 | { | |
145 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); | |
146 | struct posix_acl *acl, *clone; | |
147 | mode_t mode = inode->i_mode; | |
148 | int error = 0; | |
b3b94faa DT |
149 | |
150 | if (!sdp->sd_args.ar_posix_acl) | |
151 | return 0; | |
479c427d | 152 | if (S_ISLNK(inode->i_mode)) |
b3b94faa DT |
153 | return 0; |
154 | ||
479c427d SW |
155 | acl = gfs2_acl_get(dip, ACL_TYPE_DEFAULT); |
156 | if (IS_ERR(acl)) | |
157 | return PTR_ERR(acl); | |
b3b94faa | 158 | if (!acl) { |
ce3b0f8d | 159 | mode &= ~current_umask(); |
479c427d SW |
160 | if (mode != inode->i_mode) |
161 | error = gfs2_set_mode(inode, mode); | |
b3b94faa DT |
162 | return error; |
163 | } | |
164 | ||
479c427d SW |
165 | if (S_ISDIR(inode->i_mode)) { |
166 | error = gfs2_acl_set(inode, ACL_TYPE_DEFAULT, acl); | |
167 | if (error) | |
168 | goto out; | |
169 | } | |
170 | ||
16c5f06f | 171 | clone = posix_acl_clone(acl, GFP_NOFS); |
b3b94faa DT |
172 | error = -ENOMEM; |
173 | if (!clone) | |
174 | goto out; | |
175 | posix_acl_release(acl); | |
176 | acl = clone; | |
177 | ||
b3b94faa DT |
178 | error = posix_acl_create_masq(acl, &mode); |
179 | if (error < 0) | |
180 | goto out; | |
40b78a32 SW |
181 | if (error == 0) |
182 | goto munge; | |
b3b94faa | 183 | |
479c427d | 184 | error = gfs2_acl_set(inode, ACL_TYPE_ACCESS, acl); |
40b78a32 SW |
185 | if (error) |
186 | goto out; | |
187 | munge: | |
479c427d | 188 | error = gfs2_set_mode(inode, mode); |
a91ea69f | 189 | out: |
b3b94faa | 190 | posix_acl_release(acl); |
b3b94faa DT |
191 | return error; |
192 | } | |
193 | ||
194 | int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr) | |
195 | { | |
479c427d | 196 | struct posix_acl *acl, *clone; |
b3b94faa DT |
197 | char *data; |
198 | unsigned int len; | |
199 | int error; | |
200 | ||
479c427d SW |
201 | acl = gfs2_acl_get(ip, ACL_TYPE_ACCESS); |
202 | if (IS_ERR(acl)) | |
203 | return PTR_ERR(acl); | |
b3b94faa DT |
204 | if (!acl) |
205 | return gfs2_setattr_simple(ip, attr); | |
206 | ||
16c5f06f | 207 | clone = posix_acl_clone(acl, GFP_NOFS); |
b3b94faa DT |
208 | error = -ENOMEM; |
209 | if (!clone) | |
210 | goto out; | |
211 | posix_acl_release(acl); | |
212 | acl = clone; | |
213 | ||
214 | error = posix_acl_chmod_masq(acl, attr->ia_mode); | |
215 | if (!error) { | |
479c427d SW |
216 | len = posix_acl_to_xattr(acl, NULL, 0); |
217 | data = kmalloc(len, GFP_NOFS); | |
218 | error = -ENOMEM; | |
219 | if (data == NULL) | |
220 | goto out; | |
b3b94faa | 221 | posix_acl_to_xattr(acl, data, len); |
479c427d SW |
222 | error = gfs2_xattr_acl_chmod(ip, attr, data); |
223 | kfree(data); | |
106381bf | 224 | set_cached_acl(&ip->i_inode, ACL_TYPE_ACCESS, acl); |
b3b94faa DT |
225 | } |
226 | ||
a91ea69f | 227 | out: |
b3b94faa | 228 | posix_acl_release(acl); |
b3b94faa DT |
229 | return error; |
230 | } | |
231 | ||
2646a1f6 SW |
232 | static int gfs2_acl_type(const char *name) |
233 | { | |
234 | if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0) | |
235 | return ACL_TYPE_ACCESS; | |
236 | if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0) | |
237 | return ACL_TYPE_DEFAULT; | |
238 | return -EINVAL; | |
239 | } | |
240 | ||
431547b3 CH |
241 | static int gfs2_xattr_system_get(struct dentry *dentry, const char *name, |
242 | void *buffer, size_t size, int xtype) | |
2646a1f6 | 243 | { |
431547b3 | 244 | struct inode *inode = dentry->d_inode; |
f72f2d2e | 245 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
106381bf | 246 | struct posix_acl *acl; |
2646a1f6 | 247 | int type; |
106381bf | 248 | int error; |
2646a1f6 | 249 | |
f72f2d2e SW |
250 | if (!sdp->sd_args.ar_posix_acl) |
251 | return -EOPNOTSUPP; | |
252 | ||
2646a1f6 SW |
253 | type = gfs2_acl_type(name); |
254 | if (type < 0) | |
255 | return type; | |
256 | ||
106381bf SW |
257 | acl = gfs2_acl_get(GFS2_I(inode), type); |
258 | if (IS_ERR(acl)) | |
259 | return PTR_ERR(acl); | |
260 | if (acl == NULL) | |
261 | return -ENODATA; | |
2646a1f6 | 262 | |
106381bf SW |
263 | error = posix_acl_to_xattr(acl, buffer, size); |
264 | posix_acl_release(acl); | |
265 | ||
266 | return error; | |
267 | } | |
2646a1f6 | 268 | |
431547b3 CH |
269 | static int gfs2_xattr_system_set(struct dentry *dentry, const char *name, |
270 | const void *value, size_t size, int flags, | |
271 | int xtype) | |
2646a1f6 | 272 | { |
431547b3 | 273 | struct inode *inode = dentry->d_inode; |
2646a1f6 SW |
274 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
275 | struct posix_acl *acl = NULL; | |
276 | int error = 0, type; | |
277 | ||
278 | if (!sdp->sd_args.ar_posix_acl) | |
279 | return -EOPNOTSUPP; | |
280 | ||
281 | type = gfs2_acl_type(name); | |
282 | if (type < 0) | |
283 | return type; | |
284 | if (flags & XATTR_CREATE) | |
285 | return -EINVAL; | |
286 | if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) | |
287 | return value ? -EACCES : 0; | |
288 | if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER)) | |
289 | return -EPERM; | |
290 | if (S_ISLNK(inode->i_mode)) | |
291 | return -EOPNOTSUPP; | |
292 | ||
293 | if (!value) | |
294 | goto set_acl; | |
295 | ||
296 | acl = posix_acl_from_xattr(value, size); | |
297 | if (!acl) { | |
298 | /* | |
299 | * acl_set_file(3) may request that we set default ACLs with | |
300 | * zero length -- defend (gracefully) against that here. | |
301 | */ | |
302 | goto out; | |
303 | } | |
304 | if (IS_ERR(acl)) { | |
305 | error = PTR_ERR(acl); | |
306 | goto out; | |
307 | } | |
308 | ||
309 | error = posix_acl_valid(acl); | |
310 | if (error) | |
311 | goto out_release; | |
312 | ||
313 | error = -EINVAL; | |
314 | if (acl->a_count > GFS2_ACL_MAX_ENTRIES) | |
315 | goto out_release; | |
316 | ||
317 | if (type == ACL_TYPE_ACCESS) { | |
318 | mode_t mode = inode->i_mode; | |
319 | error = posix_acl_equiv_mode(acl, &mode); | |
320 | ||
321 | if (error <= 0) { | |
322 | posix_acl_release(acl); | |
323 | acl = NULL; | |
324 | ||
325 | if (error < 0) | |
326 | return error; | |
327 | } | |
328 | ||
329 | error = gfs2_set_mode(inode, mode); | |
330 | if (error) | |
331 | goto out_release; | |
332 | } | |
333 | ||
334 | set_acl: | |
431547b3 | 335 | error = __gfs2_xattr_set(inode, name, value, size, 0, GFS2_EATYPE_SYS); |
106381bf SW |
336 | if (!error) { |
337 | if (acl) | |
338 | set_cached_acl(inode, type, acl); | |
339 | else | |
340 | forget_cached_acl(inode, type); | |
341 | } | |
2646a1f6 SW |
342 | out_release: |
343 | posix_acl_release(acl); | |
344 | out: | |
345 | return error; | |
346 | } | |
347 | ||
b7bb0a12 | 348 | const struct xattr_handler gfs2_xattr_system_handler = { |
2646a1f6 | 349 | .prefix = XATTR_SYSTEM_PREFIX, |
431547b3 | 350 | .flags = GFS2_EATYPE_SYS, |
2646a1f6 SW |
351 | .get = gfs2_xattr_system_get, |
352 | .set = gfs2_xattr_system_set, | |
353 | }; | |
354 |