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 | |
40b78a32 SW |
30 | static int acl_get(struct gfs2_inode *ip, const char *name, |
31 | struct posix_acl **acl, struct gfs2_ea_location *el, | |
32 | char **datap, unsigned int *lenp) | |
b3b94faa | 33 | { |
40b78a32 SW |
34 | char *data; |
35 | unsigned int len; | |
b3b94faa DT |
36 | int error; |
37 | ||
40b78a32 SW |
38 | el->el_bh = NULL; |
39 | ||
3767ac21 | 40 | if (!ip->i_eattr) |
b3b94faa DT |
41 | return 0; |
42 | ||
40b78a32 | 43 | error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, el); |
b3b94faa DT |
44 | if (error) |
45 | return error; | |
46 | if (!el->el_ea) | |
47 | return 0; | |
48 | if (!GFS2_EA_DATA_LEN(el->el_ea)) | |
49 | goto out; | |
50 | ||
40b78a32 SW |
51 | len = GFS2_EA_DATA_LEN(el->el_ea); |
52 | data = kmalloc(len, GFP_NOFS); | |
b3b94faa | 53 | error = -ENOMEM; |
40b78a32 | 54 | if (!data) |
b3b94faa DT |
55 | goto out; |
56 | ||
40b78a32 SW |
57 | error = gfs2_ea_get_copy(ip, el, data, len); |
58 | if (error < 0) | |
b3b94faa | 59 | goto out_kfree; |
40b78a32 | 60 | error = 0; |
b3b94faa DT |
61 | |
62 | if (acl) { | |
40b78a32 | 63 | *acl = posix_acl_from_xattr(data, len); |
b3b94faa DT |
64 | if (IS_ERR(*acl)) |
65 | error = PTR_ERR(*acl); | |
66 | } | |
67 | ||
a91ea69f | 68 | out_kfree: |
40b78a32 SW |
69 | if (error || !datap) { |
70 | kfree(data); | |
71 | } else { | |
72 | *datap = data; | |
73 | *lenp = len; | |
b3b94faa | 74 | } |
a91ea69f | 75 | out: |
b3b94faa DT |
76 | return error; |
77 | } | |
78 | ||
79 | /** | |
77386e1f | 80 | * gfs2_check_acl - Check an ACL to see if we're allowed to do something |
b3b94faa DT |
81 | * @inode: the file we want to do something to |
82 | * @mask: what we want to do | |
83 | * | |
84 | * Returns: errno | |
85 | */ | |
86 | ||
77386e1f | 87 | int gfs2_check_acl(struct inode *inode, int mask) |
b3b94faa | 88 | { |
40b78a32 | 89 | struct gfs2_ea_location el; |
b3b94faa DT |
90 | struct posix_acl *acl = NULL; |
91 | int error; | |
92 | ||
40b78a32 SW |
93 | error = acl_get(GFS2_I(inode), GFS2_POSIX_ACL_ACCESS, &acl, &el, NULL, NULL); |
94 | brelse(el.el_bh); | |
b3b94faa DT |
95 | if (error) |
96 | return error; | |
97 | ||
98 | if (acl) { | |
99 | error = posix_acl_permission(inode, acl, mask); | |
100 | posix_acl_release(acl); | |
101 | return error; | |
102 | } | |
103 | ||
104 | return -EAGAIN; | |
105 | } | |
106 | ||
b3b94faa DT |
107 | static int munge_mode(struct gfs2_inode *ip, mode_t mode) |
108 | { | |
feaa7bba | 109 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
110 | struct buffer_head *dibh; |
111 | int error; | |
112 | ||
113 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); | |
114 | if (error) | |
115 | return error; | |
116 | ||
117 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
118 | if (!error) { | |
119 | gfs2_assert_withdraw(sdp, | |
b60623c2 SW |
120 | (ip->i_inode.i_mode & S_IFMT) == (mode & S_IFMT)); |
121 | ip->i_inode.i_mode = mode; | |
d4e9c4c3 | 122 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
539e5d6b | 123 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa DT |
124 | brelse(dibh); |
125 | } | |
126 | ||
127 | gfs2_trans_end(sdp); | |
128 | ||
129 | return 0; | |
130 | } | |
131 | ||
132 | int gfs2_acl_create(struct gfs2_inode *dip, struct gfs2_inode *ip) | |
133 | { | |
40b78a32 | 134 | struct gfs2_ea_location el; |
feaa7bba | 135 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
b3b94faa | 136 | struct posix_acl *acl = NULL, *clone; |
b60623c2 | 137 | mode_t mode = ip->i_inode.i_mode; |
40b78a32 SW |
138 | char *data = NULL; |
139 | unsigned int len; | |
b3b94faa DT |
140 | int error; |
141 | ||
142 | if (!sdp->sd_args.ar_posix_acl) | |
143 | return 0; | |
b60623c2 | 144 | if (S_ISLNK(ip->i_inode.i_mode)) |
b3b94faa DT |
145 | return 0; |
146 | ||
40b78a32 SW |
147 | error = acl_get(dip, GFS2_POSIX_ACL_DEFAULT, &acl, &el, &data, &len); |
148 | brelse(el.el_bh); | |
b3b94faa DT |
149 | if (error) |
150 | return error; | |
151 | if (!acl) { | |
ce3b0f8d | 152 | mode &= ~current_umask(); |
b60623c2 | 153 | if (mode != ip->i_inode.i_mode) |
b3b94faa DT |
154 | error = munge_mode(ip, mode); |
155 | return error; | |
156 | } | |
157 | ||
16c5f06f | 158 | clone = posix_acl_clone(acl, GFP_NOFS); |
b3b94faa DT |
159 | error = -ENOMEM; |
160 | if (!clone) | |
161 | goto out; | |
162 | posix_acl_release(acl); | |
163 | acl = clone; | |
164 | ||
b60623c2 | 165 | if (S_ISDIR(ip->i_inode.i_mode)) { |
40b78a32 SW |
166 | error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS, |
167 | GFS2_POSIX_ACL_DEFAULT, data, len, 0); | |
b3b94faa DT |
168 | if (error) |
169 | goto out; | |
170 | } | |
171 | ||
172 | error = posix_acl_create_masq(acl, &mode); | |
173 | if (error < 0) | |
174 | goto out; | |
40b78a32 SW |
175 | if (error == 0) |
176 | goto munge; | |
b3b94faa | 177 | |
40b78a32 SW |
178 | posix_acl_to_xattr(acl, data, len); |
179 | error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS, | |
180 | GFS2_POSIX_ACL_ACCESS, data, len, 0); | |
181 | if (error) | |
182 | goto out; | |
183 | munge: | |
184 | error = munge_mode(ip, mode); | |
a91ea69f | 185 | out: |
b3b94faa | 186 | posix_acl_release(acl); |
40b78a32 | 187 | kfree(data); |
b3b94faa DT |
188 | return error; |
189 | } | |
190 | ||
191 | int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr) | |
192 | { | |
193 | struct posix_acl *acl = NULL, *clone; | |
194 | struct gfs2_ea_location el; | |
195 | char *data; | |
196 | unsigned int len; | |
197 | int error; | |
198 | ||
40b78a32 | 199 | error = acl_get(ip, GFS2_POSIX_ACL_ACCESS, &acl, &el, &data, &len); |
b3b94faa | 200 | if (error) |
40b78a32 | 201 | goto out_brelse; |
b3b94faa DT |
202 | if (!acl) |
203 | return gfs2_setattr_simple(ip, attr); | |
204 | ||
16c5f06f | 205 | clone = posix_acl_clone(acl, GFP_NOFS); |
b3b94faa DT |
206 | error = -ENOMEM; |
207 | if (!clone) | |
208 | goto out; | |
209 | posix_acl_release(acl); | |
210 | acl = clone; | |
211 | ||
212 | error = posix_acl_chmod_masq(acl, attr->ia_mode); | |
213 | if (!error) { | |
214 | posix_acl_to_xattr(acl, data, len); | |
215 | error = gfs2_ea_acl_chmod(ip, &el, attr, data); | |
216 | } | |
217 | ||
a91ea69f | 218 | out: |
b3b94faa | 219 | posix_acl_release(acl); |
b3b94faa | 220 | kfree(data); |
40b78a32 SW |
221 | out_brelse: |
222 | brelse(el.el_bh); | |
b3b94faa DT |
223 | return error; |
224 | } | |
225 | ||
2646a1f6 SW |
226 | static int gfs2_acl_type(const char *name) |
227 | { | |
228 | if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0) | |
229 | return ACL_TYPE_ACCESS; | |
230 | if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0) | |
231 | return ACL_TYPE_DEFAULT; | |
232 | return -EINVAL; | |
233 | } | |
234 | ||
235 | static int gfs2_xattr_system_get(struct inode *inode, const char *name, | |
236 | void *buffer, size_t size) | |
237 | { | |
238 | int type; | |
239 | ||
240 | type = gfs2_acl_type(name); | |
241 | if (type < 0) | |
242 | return type; | |
243 | ||
244 | return gfs2_xattr_get(inode, GFS2_EATYPE_SYS, name, buffer, size); | |
245 | } | |
246 | ||
247 | static int gfs2_set_mode(struct inode *inode, mode_t mode) | |
248 | { | |
249 | int error = 0; | |
250 | ||
251 | if (mode != inode->i_mode) { | |
252 | struct iattr iattr; | |
253 | ||
254 | iattr.ia_valid = ATTR_MODE; | |
255 | iattr.ia_mode = mode; | |
256 | ||
257 | error = gfs2_setattr_simple(GFS2_I(inode), &iattr); | |
258 | } | |
259 | ||
260 | return error; | |
261 | } | |
262 | ||
263 | static int gfs2_xattr_system_set(struct inode *inode, const char *name, | |
264 | const void *value, size_t size, int flags) | |
265 | { | |
266 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
267 | struct posix_acl *acl = NULL; | |
268 | int error = 0, type; | |
269 | ||
270 | if (!sdp->sd_args.ar_posix_acl) | |
271 | return -EOPNOTSUPP; | |
272 | ||
273 | type = gfs2_acl_type(name); | |
274 | if (type < 0) | |
275 | return type; | |
276 | if (flags & XATTR_CREATE) | |
277 | return -EINVAL; | |
278 | if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) | |
279 | return value ? -EACCES : 0; | |
280 | if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER)) | |
281 | return -EPERM; | |
282 | if (S_ISLNK(inode->i_mode)) | |
283 | return -EOPNOTSUPP; | |
284 | ||
285 | if (!value) | |
286 | goto set_acl; | |
287 | ||
288 | acl = posix_acl_from_xattr(value, size); | |
289 | if (!acl) { | |
290 | /* | |
291 | * acl_set_file(3) may request that we set default ACLs with | |
292 | * zero length -- defend (gracefully) against that here. | |
293 | */ | |
294 | goto out; | |
295 | } | |
296 | if (IS_ERR(acl)) { | |
297 | error = PTR_ERR(acl); | |
298 | goto out; | |
299 | } | |
300 | ||
301 | error = posix_acl_valid(acl); | |
302 | if (error) | |
303 | goto out_release; | |
304 | ||
305 | error = -EINVAL; | |
306 | if (acl->a_count > GFS2_ACL_MAX_ENTRIES) | |
307 | goto out_release; | |
308 | ||
309 | if (type == ACL_TYPE_ACCESS) { | |
310 | mode_t mode = inode->i_mode; | |
311 | error = posix_acl_equiv_mode(acl, &mode); | |
312 | ||
313 | if (error <= 0) { | |
314 | posix_acl_release(acl); | |
315 | acl = NULL; | |
316 | ||
317 | if (error < 0) | |
318 | return error; | |
319 | } | |
320 | ||
321 | error = gfs2_set_mode(inode, mode); | |
322 | if (error) | |
323 | goto out_release; | |
324 | } | |
325 | ||
326 | set_acl: | |
327 | error = gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, value, size, 0); | |
328 | out_release: | |
329 | posix_acl_release(acl); | |
330 | out: | |
331 | return error; | |
332 | } | |
333 | ||
334 | struct xattr_handler gfs2_xattr_system_handler = { | |
335 | .prefix = XATTR_SYSTEM_PREFIX, | |
336 | .get = gfs2_xattr_system_get, | |
337 | .set = gfs2_xattr_system_set, | |
338 | }; | |
339 |