1 #include <linux/capability.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>
13 static int reiserfs_set_acl(struct inode *inode, int type,
14 struct posix_acl *acl);
17 xattr_set_acl(struct inode *inode, int type, const void *value, size_t size)
19 struct posix_acl *acl;
22 if (!reiserfs_posixacl(inode->i_sb))
24 if (!is_owner_or_cap(inode))
28 acl = posix_acl_from_xattr(value, size);
32 error = posix_acl_valid(acl);
39 error = reiserfs_set_acl(inode, type, acl);
42 posix_acl_release(acl);
47 xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
49 struct posix_acl *acl;
52 if (!reiserfs_posixacl(inode->i_sb))
55 acl = reiserfs_get_acl(inode, type);
60 error = posix_acl_to_xattr(acl, buffer, size);
61 posix_acl_release(acl);
67 * Convert from filesystem to in-memory representation.
69 static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
71 const char *end = (char *)value + size;
73 struct posix_acl *acl;
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);
85 return ERR_PTR(-EINVAL);
88 acl = posix_acl_alloc(count, GFP_NOFS);
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)
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) {
102 value = (char *)value +
103 sizeof(reiserfs_acl_entry_short);
104 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
109 value = (char *)value + sizeof(reiserfs_acl_entry);
110 if ((char *)value > end)
112 acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
124 posix_acl_release(acl);
125 return ERR_PTR(-EINVAL);
129 * Convert from in-memory to filesystem representation.
131 static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
133 reiserfs_acl_header *ext_acl;
137 *size = reiserfs_acl_size(acl->a_count);
138 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
140 sizeof(reiserfs_acl_entry),
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) {
153 entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
154 e += sizeof(reiserfs_acl_entry);
161 e += sizeof(reiserfs_acl_entry_short);
168 return (char *)ext_acl;
172 return ERR_PTR(-EINVAL);
176 * Inode operation get_posix_acl().
178 * inode->i_mutex: down
179 * BKL held [before 2.5.x]
181 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
184 struct posix_acl *acl, **p_acl;
187 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
190 case ACL_TYPE_ACCESS:
191 name = POSIX_ACL_XATTR_ACCESS;
192 p_acl = &reiserfs_i->i_acl_access;
194 case ACL_TYPE_DEFAULT:
195 name = POSIX_ACL_XATTR_DEFAULT;
196 p_acl = &reiserfs_i->i_acl_default;
199 return ERR_PTR(-EINVAL);
202 if (IS_ERR(*p_acl)) {
203 if (PTR_ERR(*p_acl) == -ENODATA)
205 } else if (*p_acl != NULL)
206 return posix_acl_dup(*p_acl);
208 size = reiserfs_xattr_get(inode, name, NULL, 0);
210 if (size == -ENODATA || size == -ENOSYS) {
211 *p_acl = ERR_PTR(-ENODATA);
214 return ERR_PTR(size);
217 value = kmalloc(size, GFP_NOFS);
219 return ERR_PTR(-ENOMEM);
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 */
226 *p_acl = ERR_PTR(-ENODATA);
227 } else if (retval < 0) {
228 acl = ERR_PTR(retval);
230 acl = posix_acl_from_disk(value, retval);
232 *p_acl = posix_acl_dup(acl);
240 * Inode operation set_posix_acl().
242 * inode->i_mutex: down
243 * BKL held [before 2.5.x]
246 reiserfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
250 struct posix_acl **p_acl;
253 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
255 if (S_ISLNK(inode->i_mode))
259 case ACL_TYPE_ACCESS:
260 name = POSIX_ACL_XATTR_ACCESS;
261 p_acl = &reiserfs_i->i_acl_access;
263 mode_t mode = inode->i_mode;
264 error = posix_acl_equiv_mode(acl, &mode);
268 inode->i_mode = mode;
274 case ACL_TYPE_DEFAULT:
275 name = POSIX_ACL_XATTR_DEFAULT;
276 p_acl = &reiserfs_i->i_acl_default;
277 if (!S_ISDIR(inode->i_mode))
278 return acl ? -EACCES : 0;
285 value = posix_acl_to_disk(acl, &size);
287 return (int)PTR_ERR(value);
288 error = reiserfs_xattr_set(inode, name, value, size, 0);
290 error = reiserfs_xattr_del(inode, name);
291 if (error == -ENODATA) {
292 /* This may seem odd here, but it means that the ACL was set
293 * with a value representable with mode bits. If there was
294 * an ACL before, reiserfs_xattr_del already dirtied the inode.
296 mark_inode_dirty(inode);
304 /* Release the old one */
305 if (!IS_ERR(*p_acl) && *p_acl)
306 posix_acl_release(*p_acl);
309 *p_acl = ERR_PTR(-ENODATA);
311 *p_acl = posix_acl_dup(acl);
317 /* dir->i_mutex: locked,
318 * inode is new and not released into the wild yet */
320 reiserfs_inherit_default_acl(struct inode *dir, struct dentry *dentry,
323 struct posix_acl *acl;
326 /* ACLs only get applied to files and directories */
327 if (S_ISLNK(inode->i_mode))
330 /* ACLs can only be used on "new" objects, so if it's an old object
331 * there is nothing to inherit from */
332 if (get_inode_sd_version(dir) == STAT_DATA_V1)
335 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
336 * would be useless since permissions are ignored, and a pain because
337 * it introduces locking cycles */
338 if (is_reiserfs_priv_object(dir)) {
339 reiserfs_mark_inode_private(inode);
343 acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
345 if (PTR_ERR(acl) == -ENODATA)
351 struct posix_acl *acl_copy;
352 mode_t mode = inode->i_mode;
355 /* Copy the default ACL to the default ACL of a new directory */
356 if (S_ISDIR(inode->i_mode)) {
357 err = reiserfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
362 /* Now we reconcile the new ACL and the mode,
363 potentially modifying both */
364 acl_copy = posix_acl_clone(acl, GFP_NOFS);
370 need_acl = posix_acl_create_masq(acl_copy, &mode);
372 if (mode != inode->i_mode) {
373 inode->i_mode = mode;
376 /* If we need an ACL.. */
379 reiserfs_set_acl(inode, ACL_TYPE_ACCESS,
386 posix_acl_release(acl_copy);
388 posix_acl_release(acl);
391 /* no ACL, apply umask */
392 inode->i_mode &= ~current->fs->umask;
398 /* Looks up and caches the result of the default ACL.
399 * We do this so that we don't need to carry the xattr_sem into
400 * reiserfs_new_inode if we don't need to */
401 int reiserfs_cache_default_acl(struct inode *inode)
404 if (reiserfs_posixacl(inode->i_sb) && !is_reiserfs_priv_object(inode)) {
405 struct posix_acl *acl;
406 reiserfs_read_lock_xattr_i(inode);
407 reiserfs_read_lock_xattrs(inode->i_sb);
408 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
409 reiserfs_read_unlock_xattrs(inode->i_sb);
410 reiserfs_read_unlock_xattr_i(inode);
411 ret = (acl && !IS_ERR(acl));
413 posix_acl_release(acl);
419 int reiserfs_acl_chmod(struct inode *inode)
421 struct posix_acl *acl, *clone;
424 if (S_ISLNK(inode->i_mode))
427 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
428 !reiserfs_posixacl(inode->i_sb)) {
432 reiserfs_read_lock_xattrs(inode->i_sb);
433 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
434 reiserfs_read_unlock_xattrs(inode->i_sb);
439 clone = posix_acl_clone(acl, GFP_NOFS);
440 posix_acl_release(acl);
443 error = posix_acl_chmod_masq(clone, inode->i_mode);
445 int lock = !has_xattr_dir(inode);
446 reiserfs_write_lock_xattr_i(inode);
448 reiserfs_write_lock_xattrs(inode->i_sb);
450 reiserfs_read_lock_xattrs(inode->i_sb);
451 error = reiserfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
453 reiserfs_write_unlock_xattrs(inode->i_sb);
455 reiserfs_read_unlock_xattrs(inode->i_sb);
456 reiserfs_write_unlock_xattr_i(inode);
458 posix_acl_release(clone);
463 posix_acl_access_get(struct inode *inode, const char *name,
464 void *buffer, size_t size)
466 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
468 return xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
472 posix_acl_access_set(struct inode *inode, const char *name,
473 const void *value, size_t size, int flags)
475 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
477 return xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
480 static int posix_acl_access_del(struct inode *inode, const char *name)
482 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
483 struct posix_acl **acl = &reiserfs_i->i_acl_access;
484 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
486 if (!IS_ERR(*acl) && *acl) {
487 posix_acl_release(*acl);
488 *acl = ERR_PTR(-ENODATA);
495 posix_acl_access_list(struct inode *inode, const char *name, int namelen,
499 if (!reiserfs_posixacl(inode->i_sb))
502 memcpy(out, name, len);
507 struct reiserfs_xattr_handler posix_acl_access_handler = {
508 .prefix = POSIX_ACL_XATTR_ACCESS,
509 .get = posix_acl_access_get,
510 .set = posix_acl_access_set,
511 .del = posix_acl_access_del,
512 .list = posix_acl_access_list,
516 posix_acl_default_get(struct inode *inode, const char *name,
517 void *buffer, size_t size)
519 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
521 return xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
525 posix_acl_default_set(struct inode *inode, const char *name,
526 const void *value, size_t size, int flags)
528 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
530 return xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
533 static int posix_acl_default_del(struct inode *inode, const char *name)
535 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
536 struct posix_acl **acl = &reiserfs_i->i_acl_default;
537 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
539 if (!IS_ERR(*acl) && *acl) {
540 posix_acl_release(*acl);
541 *acl = ERR_PTR(-ENODATA);
548 posix_acl_default_list(struct inode *inode, const char *name, int namelen,
552 if (!reiserfs_posixacl(inode->i_sb))
555 memcpy(out, name, len);
560 struct reiserfs_xattr_handler posix_acl_default_handler = {
561 .prefix = POSIX_ACL_XATTR_DEFAULT,
562 .get = posix_acl_default_get,
563 .set = posix_acl_default_set,
564 .del = posix_acl_default_del,
565 .list = posix_acl_default_list,