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/xattr_acl.h>
8 #include <linux/reiserfs_xattr.h>
9 #include <linux/reiserfs_acl.h>
10 #include <asm/uaccess.h>
13 xattr_set_acl(struct inode *inode, int type, const void *value, size_t size)
15 struct posix_acl *acl;
18 if (!reiserfs_posixacl(inode->i_sb))
20 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
24 acl = posix_acl_from_xattr(value, size);
28 error = posix_acl_valid(acl);
35 error = reiserfs_set_acl (inode, type, acl);
38 posix_acl_release(acl);
44 xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
46 struct posix_acl *acl;
49 if (!reiserfs_posixacl(inode->i_sb))
52 acl = reiserfs_get_acl (inode, type);
57 error = posix_acl_to_xattr(acl, buffer, size);
58 posix_acl_release(acl);
65 * Convert from filesystem to in-memory representation.
67 static struct posix_acl *
68 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 =
92 (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 =
113 le32_to_cpu(entry->e_id);
125 posix_acl_release(acl);
126 return ERR_PTR(-EINVAL);
130 * Convert from in-memory to filesystem representation.
133 posix_acl_to_disk(const struct posix_acl *acl, size_t *size)
135 reiserfs_acl_header *ext_acl;
139 *size = reiserfs_acl_size(acl->a_count);
140 ext_acl = (reiserfs_acl_header *)kmalloc(sizeof(reiserfs_acl_header) +
141 acl->a_count * sizeof(reiserfs_acl_entry), GFP_NOFS);
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) {
154 cpu_to_le32(acl->a_entries[n].e_id);
155 e += sizeof(reiserfs_acl_entry);
162 e += sizeof(reiserfs_acl_entry_short);
169 return (char *)ext_acl;
173 return ERR_PTR(-EINVAL);
177 * Inode operation get_posix_acl().
180 * BKL held [before 2.5.x]
183 reiserfs_get_acl(struct inode *inode, int type)
186 struct posix_acl *acl, **p_acl;
189 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
192 case ACL_TYPE_ACCESS:
193 name = XATTR_NAME_ACL_ACCESS;
194 p_acl = &reiserfs_i->i_acl_access;
196 case ACL_TYPE_DEFAULT:
197 name = XATTR_NAME_ACL_DEFAULT;
198 p_acl = &reiserfs_i->i_acl_default;
201 return ERR_PTR (-EINVAL);
204 if (IS_ERR (*p_acl)) {
205 if (PTR_ERR (*p_acl) == -ENODATA)
207 } else if (*p_acl != NULL)
208 return posix_acl_dup (*p_acl);
210 size = reiserfs_xattr_get (inode, name, NULL, 0);
212 if (size == -ENODATA || size == -ENOSYS) {
213 *p_acl = ERR_PTR (-ENODATA);
216 return ERR_PTR (size);
219 value = kmalloc (size, GFP_NOFS);
221 return ERR_PTR (-ENOMEM);
223 retval = reiserfs_xattr_get(inode, name, value, size);
224 if (retval == -ENODATA || retval == -ENOSYS) {
225 /* This shouldn't actually happen as it should have
226 been caught above.. but just in case */
228 *p_acl = ERR_PTR (-ENODATA);
229 } else if (retval < 0) {
230 acl = ERR_PTR(retval);
232 acl = posix_acl_from_disk(value, retval);
233 *p_acl = posix_acl_dup (acl);
241 * Inode operation set_posix_acl().
244 * BKL held [before 2.5.x]
247 reiserfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
251 struct posix_acl **p_acl;
254 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
256 if (S_ISLNK(inode->i_mode))
260 case ACL_TYPE_ACCESS:
261 name = XATTR_NAME_ACL_ACCESS;
262 p_acl = &reiserfs_i->i_acl_access;
264 mode_t mode = inode->i_mode;
265 error = posix_acl_equiv_mode (acl, &mode);
269 inode->i_mode = mode;
275 case ACL_TYPE_DEFAULT:
276 name = XATTR_NAME_ACL_DEFAULT;
277 p_acl = &reiserfs_i->i_acl_default;
278 if (!S_ISDIR (inode->i_mode))
279 return acl ? -EACCES : 0;
286 value = posix_acl_to_disk(acl, &size);
288 return (int)PTR_ERR(value);
289 error = reiserfs_xattr_set(inode, name, value, size, 0);
291 error = reiserfs_xattr_del (inode, name);
292 if (error == -ENODATA)
300 /* Release the old one */
301 if (!IS_ERR (*p_acl) && *p_acl)
302 posix_acl_release (*p_acl);
305 *p_acl = ERR_PTR (-ENODATA);
307 *p_acl = posix_acl_dup (acl);
314 * inode is new and not released into the wild yet */
316 reiserfs_inherit_default_acl (struct inode *dir, struct dentry *dentry, struct inode *inode)
318 struct posix_acl *acl;
321 /* ACLs only get applied to files and directories */
322 if (S_ISLNK (inode->i_mode))
325 /* ACLs can only be used on "new" objects, so if it's an old object
326 * there is nothing to inherit from */
327 if (get_inode_sd_version (dir) == STAT_DATA_V1)
330 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
331 * would be useless since permissions are ignored, and a pain because
332 * it introduces locking cycles */
333 if (is_reiserfs_priv_object (dir)) {
334 REISERFS_I(inode)->i_flags |= i_priv_object;
338 acl = reiserfs_get_acl (dir, ACL_TYPE_DEFAULT);
340 if (PTR_ERR (acl) == -ENODATA)
342 return PTR_ERR (acl);
346 struct posix_acl *acl_copy;
347 mode_t mode = inode->i_mode;
350 /* Copy the default ACL to the default ACL of a new directory */
351 if (S_ISDIR (inode->i_mode)) {
352 err = reiserfs_set_acl (inode, ACL_TYPE_DEFAULT, acl);
357 /* Now we reconcile the new ACL and the mode,
358 potentially modifying both */
359 acl_copy = posix_acl_clone (acl, GFP_NOFS);
366 need_acl = posix_acl_create_masq (acl_copy, &mode);
368 if (mode != inode->i_mode) {
369 inode->i_mode = mode;
372 /* If we need an ACL.. */
374 err = reiserfs_set_acl (inode, ACL_TYPE_ACCESS, acl_copy);
380 posix_acl_release (acl_copy);
382 posix_acl_release (acl);
385 /* no ACL, apply umask */
386 inode->i_mode &= ~current->fs->umask;
392 /* Looks up and caches the result of the default ACL.
393 * We do this so that we don't need to carry the xattr_sem into
394 * reiserfs_new_inode if we don't need to */
396 reiserfs_cache_default_acl (struct inode *inode)
399 if (reiserfs_posixacl (inode->i_sb) &&
400 !is_reiserfs_priv_object (inode)) {
401 struct posix_acl *acl;
402 reiserfs_read_lock_xattr_i (inode);
403 reiserfs_read_lock_xattrs (inode->i_sb);
404 acl = reiserfs_get_acl (inode, ACL_TYPE_DEFAULT);
405 reiserfs_read_unlock_xattrs (inode->i_sb);
406 reiserfs_read_unlock_xattr_i (inode);
408 posix_acl_release (acl);
415 reiserfs_acl_chmod (struct inode *inode)
417 struct posix_acl *acl, *clone;
420 if (S_ISLNK(inode->i_mode))
423 if (get_inode_sd_version (inode) == STAT_DATA_V1 ||
424 !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(XATTR_NAME_ACL_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(XATTR_NAME_ACL_ACCESS)-1)
474 return xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
478 posix_acl_access_del (struct inode *inode, const char *name)
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(XATTR_NAME_ACL_ACCESS)-1)
484 if (!IS_ERR (*acl) && *acl) {
485 posix_acl_release (*acl);
486 *acl = ERR_PTR (-ENODATA);
493 posix_acl_access_list (struct inode *inode, const char *name, int namelen, char *out)
496 if (!reiserfs_posixacl (inode->i_sb))
499 memcpy (out, name, len);
504 struct reiserfs_xattr_handler posix_acl_access_handler = {
505 prefix: XATTR_NAME_ACL_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(XATTR_NAME_ACL_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(XATTR_NAME_ACL_DEFAULT)-1)
527 return xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
531 posix_acl_default_del (struct inode *inode, const char *name)
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(XATTR_NAME_ACL_DEFAULT)-1)
537 if (!IS_ERR (*acl) && *acl) {
538 posix_acl_release (*acl);
539 *acl = ERR_PTR (-ENODATA);
546 posix_acl_default_list (struct inode *inode, const char *name, int namelen, char *out)
549 if (!reiserfs_posixacl (inode->i_sb))
552 memcpy (out, name, len);
557 struct reiserfs_xattr_handler posix_acl_default_handler = {
558 prefix: XATTR_NAME_ACL_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,