2 #include <linux/posix_acl.h>
3 #include <linux/reiserfs_fs.h>
4 #include <linux/errno.h>
5 #include <linux/pagemap.h>
6 #include <linux/xattr.h>
7 #include <linux/posix_acl_xattr.h>
8 #include <linux/reiserfs_xattr.h>
9 #include <linux/reiserfs_acl.h>
10 #include <asm/uaccess.h>
12 static int reiserfs_set_acl(struct inode *inode, int type,
13 struct posix_acl *acl);
16 xattr_set_acl(struct inode *inode, int type, const void *value, size_t size)
18 struct posix_acl *acl;
21 if (!reiserfs_posixacl(inode->i_sb))
23 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
27 acl = posix_acl_from_xattr(value, size);
31 error = posix_acl_valid(acl);
38 error = reiserfs_set_acl(inode, type, acl);
41 posix_acl_release(acl);
46 xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
48 struct posix_acl *acl;
51 if (!reiserfs_posixacl(inode->i_sb))
54 acl = reiserfs_get_acl(inode, type);
59 error = posix_acl_to_xattr(acl, buffer, size);
60 posix_acl_release(acl);
66 * Convert from filesystem to in-memory representation.
68 static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
70 const char *end = (char *)value + size;
72 struct posix_acl *acl;
76 if (size < sizeof(reiserfs_acl_header))
77 return ERR_PTR(-EINVAL);
78 if (((reiserfs_acl_header *) value)->a_version !=
79 cpu_to_le32(REISERFS_ACL_VERSION))
80 return ERR_PTR(-EINVAL);
81 value = (char *)value + sizeof(reiserfs_acl_header);
82 count = reiserfs_acl_count(size);
84 return ERR_PTR(-EINVAL);
87 acl = posix_acl_alloc(count, GFP_NOFS);
89 return ERR_PTR(-ENOMEM);
90 for (n = 0; n < count; n++) {
91 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
92 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
94 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
95 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
96 switch (acl->a_entries[n].e_tag) {
101 value = (char *)value +
102 sizeof(reiserfs_acl_entry_short);
103 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
108 value = (char *)value + sizeof(reiserfs_acl_entry);
109 if ((char *)value > end)
111 acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
123 posix_acl_release(acl);
124 return ERR_PTR(-EINVAL);
128 * Convert from in-memory to filesystem representation.
130 static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
132 reiserfs_acl_header *ext_acl;
136 *size = reiserfs_acl_size(acl->a_count);
137 ext_acl = (reiserfs_acl_header *) kmalloc(sizeof(reiserfs_acl_header) +
139 sizeof(reiserfs_acl_entry),
142 return ERR_PTR(-ENOMEM);
143 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
144 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
145 for (n = 0; n < acl->a_count; n++) {
146 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
147 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
148 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
149 switch (acl->a_entries[n].e_tag) {
152 entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
153 e += sizeof(reiserfs_acl_entry);
160 e += sizeof(reiserfs_acl_entry_short);
167 return (char *)ext_acl;
171 return ERR_PTR(-EINVAL);
175 * Inode operation get_posix_acl().
178 * BKL held [before 2.5.x]
180 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
183 struct posix_acl *acl, **p_acl;
186 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
189 case ACL_TYPE_ACCESS:
190 name = POSIX_ACL_XATTR_ACCESS;
191 p_acl = &reiserfs_i->i_acl_access;
193 case ACL_TYPE_DEFAULT:
194 name = POSIX_ACL_XATTR_DEFAULT;
195 p_acl = &reiserfs_i->i_acl_default;
198 return ERR_PTR(-EINVAL);
201 if (IS_ERR(*p_acl)) {
202 if (PTR_ERR(*p_acl) == -ENODATA)
204 } else if (*p_acl != NULL)
205 return posix_acl_dup(*p_acl);
207 size = reiserfs_xattr_get(inode, name, NULL, 0);
209 if (size == -ENODATA || size == -ENOSYS) {
210 *p_acl = ERR_PTR(-ENODATA);
213 return ERR_PTR(size);
216 value = kmalloc(size, GFP_NOFS);
218 return ERR_PTR(-ENOMEM);
220 retval = reiserfs_xattr_get(inode, name, value, size);
221 if (retval == -ENODATA || retval == -ENOSYS) {
222 /* This shouldn't actually happen as it should have
223 been caught above.. but just in case */
225 *p_acl = ERR_PTR(-ENODATA);
226 } else if (retval < 0) {
227 acl = ERR_PTR(retval);
229 acl = posix_acl_from_disk(value, retval);
230 *p_acl = posix_acl_dup(acl);
238 * Inode operation set_posix_acl().
241 * BKL held [before 2.5.x]
244 reiserfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
248 struct posix_acl **p_acl;
251 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
253 if (S_ISLNK(inode->i_mode))
257 case ACL_TYPE_ACCESS:
258 name = POSIX_ACL_XATTR_ACCESS;
259 p_acl = &reiserfs_i->i_acl_access;
261 mode_t mode = inode->i_mode;
262 error = posix_acl_equiv_mode(acl, &mode);
266 inode->i_mode = mode;
272 case ACL_TYPE_DEFAULT:
273 name = POSIX_ACL_XATTR_DEFAULT;
274 p_acl = &reiserfs_i->i_acl_default;
275 if (!S_ISDIR(inode->i_mode))
276 return acl ? -EACCES : 0;
283 value = posix_acl_to_disk(acl, &size);
285 return (int)PTR_ERR(value);
286 error = reiserfs_xattr_set(inode, name, value, size, 0);
288 error = reiserfs_xattr_del(inode, name);
289 if (error == -ENODATA) {
290 /* This may seem odd here, but it means that the ACL was set
291 * with a value representable with mode bits. If there was
292 * an ACL before, reiserfs_xattr_del already dirtied the inode.
294 mark_inode_dirty(inode);
302 /* Release the old one */
303 if (!IS_ERR(*p_acl) && *p_acl)
304 posix_acl_release(*p_acl);
307 *p_acl = ERR_PTR(-ENODATA);
309 *p_acl = posix_acl_dup(acl);
316 * inode is new and not released into the wild yet */
318 reiserfs_inherit_default_acl(struct inode *dir, struct dentry *dentry,
321 struct posix_acl *acl;
324 /* ACLs only get applied to files and directories */
325 if (S_ISLNK(inode->i_mode))
328 /* ACLs can only be used on "new" objects, so if it's an old object
329 * there is nothing to inherit from */
330 if (get_inode_sd_version(dir) == STAT_DATA_V1)
333 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
334 * would be useless since permissions are ignored, and a pain because
335 * it introduces locking cycles */
336 if (is_reiserfs_priv_object(dir)) {
337 reiserfs_mark_inode_private(inode);
341 acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
343 if (PTR_ERR(acl) == -ENODATA)
349 struct posix_acl *acl_copy;
350 mode_t mode = inode->i_mode;
353 /* Copy the default ACL to the default ACL of a new directory */
354 if (S_ISDIR(inode->i_mode)) {
355 err = reiserfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
360 /* Now we reconcile the new ACL and the mode,
361 potentially modifying both */
362 acl_copy = posix_acl_clone(acl, GFP_NOFS);
368 need_acl = posix_acl_create_masq(acl_copy, &mode);
370 if (mode != inode->i_mode) {
371 inode->i_mode = mode;
374 /* If we need an ACL.. */
377 reiserfs_set_acl(inode, ACL_TYPE_ACCESS,
384 posix_acl_release(acl_copy);
386 posix_acl_release(acl);
389 /* no ACL, apply umask */
390 inode->i_mode &= ~current->fs->umask;
396 /* Looks up and caches the result of the default ACL.
397 * We do this so that we don't need to carry the xattr_sem into
398 * reiserfs_new_inode if we don't need to */
399 int reiserfs_cache_default_acl(struct inode *inode)
402 if (reiserfs_posixacl(inode->i_sb) && !is_reiserfs_priv_object(inode)) {
403 struct posix_acl *acl;
404 reiserfs_read_lock_xattr_i(inode);
405 reiserfs_read_lock_xattrs(inode->i_sb);
406 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
407 reiserfs_read_unlock_xattrs(inode->i_sb);
408 reiserfs_read_unlock_xattr_i(inode);
410 posix_acl_release(acl);
416 int reiserfs_acl_chmod(struct inode *inode)
418 struct posix_acl *acl, *clone;
421 if (S_ISLNK(inode->i_mode))
424 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
425 !reiserfs_posixacl(inode->i_sb)) {
429 reiserfs_read_lock_xattrs(inode->i_sb);
430 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
431 reiserfs_read_unlock_xattrs(inode->i_sb);
436 clone = posix_acl_clone(acl, GFP_NOFS);
437 posix_acl_release(acl);
440 error = posix_acl_chmod_masq(clone, inode->i_mode);
442 int lock = !has_xattr_dir(inode);
443 reiserfs_write_lock_xattr_i(inode);
445 reiserfs_write_lock_xattrs(inode->i_sb);
447 reiserfs_read_lock_xattrs(inode->i_sb);
448 error = reiserfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
450 reiserfs_write_unlock_xattrs(inode->i_sb);
452 reiserfs_read_unlock_xattrs(inode->i_sb);
453 reiserfs_write_unlock_xattr_i(inode);
455 posix_acl_release(clone);
460 posix_acl_access_get(struct inode *inode, const char *name,
461 void *buffer, size_t size)
463 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
465 return xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
469 posix_acl_access_set(struct inode *inode, const char *name,
470 const void *value, size_t size, int flags)
472 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
474 return xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
477 static int posix_acl_access_del(struct inode *inode, const char *name)
479 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
480 struct posix_acl **acl = &reiserfs_i->i_acl_access;
481 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
483 if (!IS_ERR(*acl) && *acl) {
484 posix_acl_release(*acl);
485 *acl = ERR_PTR(-ENODATA);
492 posix_acl_access_list(struct inode *inode, const char *name, int namelen,
496 if (!reiserfs_posixacl(inode->i_sb))
499 memcpy(out, name, len);
504 struct reiserfs_xattr_handler posix_acl_access_handler = {
505 .prefix = POSIX_ACL_XATTR_ACCESS,
506 .get = posix_acl_access_get,
507 .set = posix_acl_access_set,
508 .del = posix_acl_access_del,
509 .list = posix_acl_access_list,
513 posix_acl_default_get(struct inode *inode, const char *name,
514 void *buffer, size_t size)
516 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
518 return xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
522 posix_acl_default_set(struct inode *inode, const char *name,
523 const void *value, size_t size, int flags)
525 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
527 return xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
530 static int posix_acl_default_del(struct inode *inode, const char *name)
532 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
533 struct posix_acl **acl = &reiserfs_i->i_acl_default;
534 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
536 if (!IS_ERR(*acl) && *acl) {
537 posix_acl_release(*acl);
538 *acl = ERR_PTR(-ENODATA);
545 posix_acl_default_list(struct inode *inode, const char *name, int namelen,
549 if (!reiserfs_posixacl(inode->i_sb))
552 memcpy(out, name, len);
557 struct reiserfs_xattr_handler posix_acl_default_handler = {
558 .prefix = POSIX_ACL_XATTR_DEFAULT,
559 .get = posix_acl_default_get,
560 .set = posix_acl_default_set,
561 .del = posix_acl_default_del,
562 .list = posix_acl_default_list,