]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/reiserfs/xattr_acl.c
[PATCH] capable/capability.h (fs/)
[mirror_ubuntu-artful-kernel.git] / fs / reiserfs / xattr_acl.c
1 #include <linux/capability.h>
2 #include <linux/fs.h>
3 #include <linux/posix_acl.h>
4 #include <linux/reiserfs_fs.h>
5 #include <linux/errno.h>
6 #include <linux/pagemap.h>
7 #include <linux/xattr.h>
8 #include <linux/posix_acl_xattr.h>
9 #include <linux/reiserfs_xattr.h>
10 #include <linux/reiserfs_acl.h>
11 #include <asm/uaccess.h>
12
13 static int reiserfs_set_acl(struct inode *inode, int type,
14 struct posix_acl *acl);
15
16 static int
17 xattr_set_acl(struct inode *inode, int type, const void *value, size_t size)
18 {
19 struct posix_acl *acl;
20 int error;
21
22 if (!reiserfs_posixacl(inode->i_sb))
23 return -EOPNOTSUPP;
24 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
25 return -EPERM;
26
27 if (value) {
28 acl = posix_acl_from_xattr(value, size);
29 if (IS_ERR(acl)) {
30 return PTR_ERR(acl);
31 } else if (acl) {
32 error = posix_acl_valid(acl);
33 if (error)
34 goto release_and_out;
35 }
36 } else
37 acl = NULL;
38
39 error = reiserfs_set_acl(inode, type, acl);
40
41 release_and_out:
42 posix_acl_release(acl);
43 return error;
44 }
45
46 static int
47 xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
48 {
49 struct posix_acl *acl;
50 int error;
51
52 if (!reiserfs_posixacl(inode->i_sb))
53 return -EOPNOTSUPP;
54
55 acl = reiserfs_get_acl(inode, type);
56 if (IS_ERR(acl))
57 return PTR_ERR(acl);
58 if (acl == NULL)
59 return -ENODATA;
60 error = posix_acl_to_xattr(acl, buffer, size);
61 posix_acl_release(acl);
62
63 return error;
64 }
65
66 /*
67 * Convert from filesystem to in-memory representation.
68 */
69 static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
70 {
71 const char *end = (char *)value + size;
72 int n, count;
73 struct posix_acl *acl;
74
75 if (!value)
76 return NULL;
77 if (size < sizeof(reiserfs_acl_header))
78 return ERR_PTR(-EINVAL);
79 if (((reiserfs_acl_header *) value)->a_version !=
80 cpu_to_le32(REISERFS_ACL_VERSION))
81 return ERR_PTR(-EINVAL);
82 value = (char *)value + sizeof(reiserfs_acl_header);
83 count = reiserfs_acl_count(size);
84 if (count < 0)
85 return ERR_PTR(-EINVAL);
86 if (count == 0)
87 return NULL;
88 acl = posix_acl_alloc(count, GFP_NOFS);
89 if (!acl)
90 return ERR_PTR(-ENOMEM);
91 for (n = 0; n < count; n++) {
92 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
93 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
94 goto fail;
95 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
96 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
97 switch (acl->a_entries[n].e_tag) {
98 case ACL_USER_OBJ:
99 case ACL_GROUP_OBJ:
100 case ACL_MASK:
101 case ACL_OTHER:
102 value = (char *)value +
103 sizeof(reiserfs_acl_entry_short);
104 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
105 break;
106
107 case ACL_USER:
108 case ACL_GROUP:
109 value = (char *)value + sizeof(reiserfs_acl_entry);
110 if ((char *)value > end)
111 goto fail;
112 acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
113 break;
114
115 default:
116 goto fail;
117 }
118 }
119 if (value != end)
120 goto fail;
121 return acl;
122
123 fail:
124 posix_acl_release(acl);
125 return ERR_PTR(-EINVAL);
126 }
127
128 /*
129 * Convert from in-memory to filesystem representation.
130 */
131 static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
132 {
133 reiserfs_acl_header *ext_acl;
134 char *e;
135 int n;
136
137 *size = reiserfs_acl_size(acl->a_count);
138 ext_acl = (reiserfs_acl_header *) kmalloc(sizeof(reiserfs_acl_header) +
139 acl->a_count *
140 sizeof(reiserfs_acl_entry),
141 GFP_NOFS);
142 if (!ext_acl)
143 return ERR_PTR(-ENOMEM);
144 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
145 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
146 for (n = 0; n < acl->a_count; n++) {
147 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
148 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
149 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
150 switch (acl->a_entries[n].e_tag) {
151 case ACL_USER:
152 case ACL_GROUP:
153 entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
154 e += sizeof(reiserfs_acl_entry);
155 break;
156
157 case ACL_USER_OBJ:
158 case ACL_GROUP_OBJ:
159 case ACL_MASK:
160 case ACL_OTHER:
161 e += sizeof(reiserfs_acl_entry_short);
162 break;
163
164 default:
165 goto fail;
166 }
167 }
168 return (char *)ext_acl;
169
170 fail:
171 kfree(ext_acl);
172 return ERR_PTR(-EINVAL);
173 }
174
175 /*
176 * Inode operation get_posix_acl().
177 *
178 * inode->i_mutex: down
179 * BKL held [before 2.5.x]
180 */
181 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
182 {
183 char *name, *value;
184 struct posix_acl *acl, **p_acl;
185 size_t size;
186 int retval;
187 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
188
189 switch (type) {
190 case ACL_TYPE_ACCESS:
191 name = POSIX_ACL_XATTR_ACCESS;
192 p_acl = &reiserfs_i->i_acl_access;
193 break;
194 case ACL_TYPE_DEFAULT:
195 name = POSIX_ACL_XATTR_DEFAULT;
196 p_acl = &reiserfs_i->i_acl_default;
197 break;
198 default:
199 return ERR_PTR(-EINVAL);
200 }
201
202 if (IS_ERR(*p_acl)) {
203 if (PTR_ERR(*p_acl) == -ENODATA)
204 return NULL;
205 } else if (*p_acl != NULL)
206 return posix_acl_dup(*p_acl);
207
208 size = reiserfs_xattr_get(inode, name, NULL, 0);
209 if ((int)size < 0) {
210 if (size == -ENODATA || size == -ENOSYS) {
211 *p_acl = ERR_PTR(-ENODATA);
212 return NULL;
213 }
214 return ERR_PTR(size);
215 }
216
217 value = kmalloc(size, GFP_NOFS);
218 if (!value)
219 return ERR_PTR(-ENOMEM);
220
221 retval = reiserfs_xattr_get(inode, name, value, size);
222 if (retval == -ENODATA || retval == -ENOSYS) {
223 /* This shouldn't actually happen as it should have
224 been caught above.. but just in case */
225 acl = NULL;
226 *p_acl = ERR_PTR(-ENODATA);
227 } else if (retval < 0) {
228 acl = ERR_PTR(retval);
229 } else {
230 acl = posix_acl_from_disk(value, retval);
231 *p_acl = posix_acl_dup(acl);
232 }
233
234 kfree(value);
235 return acl;
236 }
237
238 /*
239 * Inode operation set_posix_acl().
240 *
241 * inode->i_mutex: down
242 * BKL held [before 2.5.x]
243 */
244 static int
245 reiserfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
246 {
247 char *name;
248 void *value = NULL;
249 struct posix_acl **p_acl;
250 size_t size;
251 int error;
252 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
253
254 if (S_ISLNK(inode->i_mode))
255 return -EOPNOTSUPP;
256
257 switch (type) {
258 case ACL_TYPE_ACCESS:
259 name = POSIX_ACL_XATTR_ACCESS;
260 p_acl = &reiserfs_i->i_acl_access;
261 if (acl) {
262 mode_t mode = inode->i_mode;
263 error = posix_acl_equiv_mode(acl, &mode);
264 if (error < 0)
265 return error;
266 else {
267 inode->i_mode = mode;
268 if (error == 0)
269 acl = NULL;
270 }
271 }
272 break;
273 case ACL_TYPE_DEFAULT:
274 name = POSIX_ACL_XATTR_DEFAULT;
275 p_acl = &reiserfs_i->i_acl_default;
276 if (!S_ISDIR(inode->i_mode))
277 return acl ? -EACCES : 0;
278 break;
279 default:
280 return -EINVAL;
281 }
282
283 if (acl) {
284 value = posix_acl_to_disk(acl, &size);
285 if (IS_ERR(value))
286 return (int)PTR_ERR(value);
287 error = reiserfs_xattr_set(inode, name, value, size, 0);
288 } else {
289 error = reiserfs_xattr_del(inode, name);
290 if (error == -ENODATA) {
291 /* This may seem odd here, but it means that the ACL was set
292 * with a value representable with mode bits. If there was
293 * an ACL before, reiserfs_xattr_del already dirtied the inode.
294 */
295 mark_inode_dirty(inode);
296 error = 0;
297 }
298 }
299
300 kfree(value);
301
302 if (!error) {
303 /* Release the old one */
304 if (!IS_ERR(*p_acl) && *p_acl)
305 posix_acl_release(*p_acl);
306
307 if (acl == NULL)
308 *p_acl = ERR_PTR(-ENODATA);
309 else
310 *p_acl = posix_acl_dup(acl);
311 }
312
313 return error;
314 }
315
316 /* dir->i_mutex: locked,
317 * inode is new and not released into the wild yet */
318 int
319 reiserfs_inherit_default_acl(struct inode *dir, struct dentry *dentry,
320 struct inode *inode)
321 {
322 struct posix_acl *acl;
323 int err = 0;
324
325 /* ACLs only get applied to files and directories */
326 if (S_ISLNK(inode->i_mode))
327 return 0;
328
329 /* ACLs can only be used on "new" objects, so if it's an old object
330 * there is nothing to inherit from */
331 if (get_inode_sd_version(dir) == STAT_DATA_V1)
332 goto apply_umask;
333
334 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
335 * would be useless since permissions are ignored, and a pain because
336 * it introduces locking cycles */
337 if (is_reiserfs_priv_object(dir)) {
338 reiserfs_mark_inode_private(inode);
339 goto apply_umask;
340 }
341
342 acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
343 if (IS_ERR(acl)) {
344 if (PTR_ERR(acl) == -ENODATA)
345 goto apply_umask;
346 return PTR_ERR(acl);
347 }
348
349 if (acl) {
350 struct posix_acl *acl_copy;
351 mode_t mode = inode->i_mode;
352 int need_acl;
353
354 /* Copy the default ACL to the default ACL of a new directory */
355 if (S_ISDIR(inode->i_mode)) {
356 err = reiserfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
357 if (err)
358 goto cleanup;
359 }
360
361 /* Now we reconcile the new ACL and the mode,
362 potentially modifying both */
363 acl_copy = posix_acl_clone(acl, GFP_NOFS);
364 if (!acl_copy) {
365 err = -ENOMEM;
366 goto cleanup;
367 }
368
369 need_acl = posix_acl_create_masq(acl_copy, &mode);
370 if (need_acl >= 0) {
371 if (mode != inode->i_mode) {
372 inode->i_mode = mode;
373 }
374
375 /* If we need an ACL.. */
376 if (need_acl > 0) {
377 err =
378 reiserfs_set_acl(inode, ACL_TYPE_ACCESS,
379 acl_copy);
380 if (err)
381 goto cleanup_copy;
382 }
383 }
384 cleanup_copy:
385 posix_acl_release(acl_copy);
386 cleanup:
387 posix_acl_release(acl);
388 } else {
389 apply_umask:
390 /* no ACL, apply umask */
391 inode->i_mode &= ~current->fs->umask;
392 }
393
394 return err;
395 }
396
397 /* Looks up and caches the result of the default ACL.
398 * We do this so that we don't need to carry the xattr_sem into
399 * reiserfs_new_inode if we don't need to */
400 int reiserfs_cache_default_acl(struct inode *inode)
401 {
402 int ret = 0;
403 if (reiserfs_posixacl(inode->i_sb) && !is_reiserfs_priv_object(inode)) {
404 struct posix_acl *acl;
405 reiserfs_read_lock_xattr_i(inode);
406 reiserfs_read_lock_xattrs(inode->i_sb);
407 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
408 reiserfs_read_unlock_xattrs(inode->i_sb);
409 reiserfs_read_unlock_xattr_i(inode);
410 ret = acl ? 1 : 0;
411 posix_acl_release(acl);
412 }
413
414 return ret;
415 }
416
417 int reiserfs_acl_chmod(struct inode *inode)
418 {
419 struct posix_acl *acl, *clone;
420 int error;
421
422 if (S_ISLNK(inode->i_mode))
423 return -EOPNOTSUPP;
424
425 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
426 !reiserfs_posixacl(inode->i_sb)) {
427 return 0;
428 }
429
430 reiserfs_read_lock_xattrs(inode->i_sb);
431 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
432 reiserfs_read_unlock_xattrs(inode->i_sb);
433 if (!acl)
434 return 0;
435 if (IS_ERR(acl))
436 return PTR_ERR(acl);
437 clone = posix_acl_clone(acl, GFP_NOFS);
438 posix_acl_release(acl);
439 if (!clone)
440 return -ENOMEM;
441 error = posix_acl_chmod_masq(clone, inode->i_mode);
442 if (!error) {
443 int lock = !has_xattr_dir(inode);
444 reiserfs_write_lock_xattr_i(inode);
445 if (lock)
446 reiserfs_write_lock_xattrs(inode->i_sb);
447 else
448 reiserfs_read_lock_xattrs(inode->i_sb);
449 error = reiserfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
450 if (lock)
451 reiserfs_write_unlock_xattrs(inode->i_sb);
452 else
453 reiserfs_read_unlock_xattrs(inode->i_sb);
454 reiserfs_write_unlock_xattr_i(inode);
455 }
456 posix_acl_release(clone);
457 return error;
458 }
459
460 static int
461 posix_acl_access_get(struct inode *inode, const char *name,
462 void *buffer, size_t size)
463 {
464 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
465 return -EINVAL;
466 return xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
467 }
468
469 static int
470 posix_acl_access_set(struct inode *inode, const char *name,
471 const void *value, size_t size, int flags)
472 {
473 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
474 return -EINVAL;
475 return xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
476 }
477
478 static int posix_acl_access_del(struct inode *inode, const char *name)
479 {
480 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
481 struct posix_acl **acl = &reiserfs_i->i_acl_access;
482 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
483 return -EINVAL;
484 if (!IS_ERR(*acl) && *acl) {
485 posix_acl_release(*acl);
486 *acl = ERR_PTR(-ENODATA);
487 }
488
489 return 0;
490 }
491
492 static int
493 posix_acl_access_list(struct inode *inode, const char *name, int namelen,
494 char *out)
495 {
496 int len = namelen;
497 if (!reiserfs_posixacl(inode->i_sb))
498 return 0;
499 if (out)
500 memcpy(out, name, len);
501
502 return len;
503 }
504
505 struct reiserfs_xattr_handler posix_acl_access_handler = {
506 .prefix = POSIX_ACL_XATTR_ACCESS,
507 .get = posix_acl_access_get,
508 .set = posix_acl_access_set,
509 .del = posix_acl_access_del,
510 .list = posix_acl_access_list,
511 };
512
513 static int
514 posix_acl_default_get(struct inode *inode, const char *name,
515 void *buffer, size_t size)
516 {
517 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
518 return -EINVAL;
519 return xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
520 }
521
522 static int
523 posix_acl_default_set(struct inode *inode, const char *name,
524 const void *value, size_t size, int flags)
525 {
526 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
527 return -EINVAL;
528 return xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
529 }
530
531 static int posix_acl_default_del(struct inode *inode, const char *name)
532 {
533 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
534 struct posix_acl **acl = &reiserfs_i->i_acl_default;
535 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
536 return -EINVAL;
537 if (!IS_ERR(*acl) && *acl) {
538 posix_acl_release(*acl);
539 *acl = ERR_PTR(-ENODATA);
540 }
541
542 return 0;
543 }
544
545 static int
546 posix_acl_default_list(struct inode *inode, const char *name, int namelen,
547 char *out)
548 {
549 int len = namelen;
550 if (!reiserfs_posixacl(inode->i_sb))
551 return 0;
552 if (out)
553 memcpy(out, name, len);
554
555 return len;
556 }
557
558 struct reiserfs_xattr_handler posix_acl_default_handler = {
559 .prefix = POSIX_ACL_XATTR_DEFAULT,
560 .get = posix_acl_default_get,
561 .set = posix_acl_default_set,
562 .del = posix_acl_default_del,
563 .list = posix_acl_default_list,
564 };