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);
175 static inline void iset_acl(struct inode *inode, struct posix_acl **i_acl,
176 struct posix_acl *acl)
178 spin_lock(&inode->i_lock);
179 if (*i_acl != ERR_PTR(-ENODATA))
180 posix_acl_release(*i_acl);
181 *i_acl = posix_acl_dup(acl);
182 spin_unlock(&inode->i_lock);
185 static inline struct posix_acl *iget_acl(struct inode *inode,
186 struct posix_acl **i_acl)
188 struct posix_acl *acl = ERR_PTR(-ENODATA);
190 spin_lock(&inode->i_lock);
191 if (*i_acl != ERR_PTR(-ENODATA))
192 acl = posix_acl_dup(*i_acl);
193 spin_unlock(&inode->i_lock);
199 * Inode operation get_posix_acl().
201 * inode->i_mutex: down
202 * BKL held [before 2.5.x]
204 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
207 struct posix_acl *acl, **p_acl;
210 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
213 case ACL_TYPE_ACCESS:
214 name = POSIX_ACL_XATTR_ACCESS;
215 p_acl = &reiserfs_i->i_acl_access;
217 case ACL_TYPE_DEFAULT:
218 name = POSIX_ACL_XATTR_DEFAULT;
219 p_acl = &reiserfs_i->i_acl_default;
222 return ERR_PTR(-EINVAL);
225 acl = iget_acl(inode, p_acl);
226 if (acl && !IS_ERR(acl))
228 else if (PTR_ERR(acl) == -ENODATA)
231 size = reiserfs_xattr_get(inode, name, NULL, 0);
233 if (size == -ENODATA || size == -ENOSYS) {
234 *p_acl = ERR_PTR(-ENODATA);
237 return ERR_PTR(size);
240 value = kmalloc(size, GFP_NOFS);
242 return ERR_PTR(-ENOMEM);
244 retval = reiserfs_xattr_get(inode, name, value, size);
245 if (retval == -ENODATA || retval == -ENOSYS) {
246 /* This shouldn't actually happen as it should have
247 been caught above.. but just in case */
249 *p_acl = ERR_PTR(-ENODATA);
250 } else if (retval < 0) {
251 acl = ERR_PTR(retval);
253 acl = posix_acl_from_disk(value, retval);
255 iset_acl(inode, p_acl, acl);
263 * Inode operation set_posix_acl().
265 * inode->i_mutex: down
266 * BKL held [before 2.5.x]
269 reiserfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
273 struct posix_acl **p_acl;
276 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
278 if (S_ISLNK(inode->i_mode))
282 case ACL_TYPE_ACCESS:
283 name = POSIX_ACL_XATTR_ACCESS;
284 p_acl = &reiserfs_i->i_acl_access;
286 mode_t mode = inode->i_mode;
287 error = posix_acl_equiv_mode(acl, &mode);
291 inode->i_mode = mode;
297 case ACL_TYPE_DEFAULT:
298 name = POSIX_ACL_XATTR_DEFAULT;
299 p_acl = &reiserfs_i->i_acl_default;
300 if (!S_ISDIR(inode->i_mode))
301 return acl ? -EACCES : 0;
308 value = posix_acl_to_disk(acl, &size);
310 return (int)PTR_ERR(value);
313 error = __reiserfs_xattr_set(inode, name, value, size, 0);
316 * Ensure that the inode gets dirtied if we're only using
317 * the mode bits and an old ACL didn't exist. We don't need
318 * to check if the inode is hashed here since we won't get
319 * called by reiserfs_inherit_default_acl().
321 if (error == -ENODATA) {
323 if (type == ACL_TYPE_ACCESS) {
324 inode->i_ctime = CURRENT_TIME_SEC;
325 mark_inode_dirty(inode);
332 iset_acl(inode, p_acl, acl);
337 /* dir->i_mutex: locked,
338 * inode is new and not released into the wild yet */
340 reiserfs_inherit_default_acl(struct inode *dir, struct dentry *dentry,
343 struct posix_acl *acl;
346 /* ACLs only get applied to files and directories */
347 if (S_ISLNK(inode->i_mode))
350 /* ACLs can only be used on "new" objects, so if it's an old object
351 * there is nothing to inherit from */
352 if (get_inode_sd_version(dir) == STAT_DATA_V1)
355 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
356 * would be useless since permissions are ignored, and a pain because
357 * it introduces locking cycles */
358 if (IS_PRIVATE(dir)) {
359 inode->i_flags |= S_PRIVATE;
363 acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
365 if (PTR_ERR(acl) == -ENODATA)
371 struct posix_acl *acl_copy;
372 mode_t mode = inode->i_mode;
375 /* Copy the default ACL to the default ACL of a new directory */
376 if (S_ISDIR(inode->i_mode)) {
377 err = reiserfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
382 /* Now we reconcile the new ACL and the mode,
383 potentially modifying both */
384 acl_copy = posix_acl_clone(acl, GFP_NOFS);
390 need_acl = posix_acl_create_masq(acl_copy, &mode);
392 if (mode != inode->i_mode) {
393 inode->i_mode = mode;
396 /* If we need an ACL.. */
399 reiserfs_set_acl(inode, ACL_TYPE_ACCESS,
406 posix_acl_release(acl_copy);
408 posix_acl_release(acl);
411 /* no ACL, apply umask */
412 inode->i_mode &= ~current->fs->umask;
418 /* Looks up and caches the result of the default ACL.
419 * We do this so that we don't need to carry the xattr_sem into
420 * reiserfs_new_inode if we don't need to */
421 int reiserfs_cache_default_acl(struct inode *inode)
424 if (reiserfs_posixacl(inode->i_sb) && !IS_PRIVATE(inode)) {
425 struct posix_acl *acl;
426 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
427 ret = (acl && !IS_ERR(acl));
429 posix_acl_release(acl);
435 int reiserfs_acl_chmod(struct inode *inode)
437 struct posix_acl *acl, *clone;
440 if (S_ISLNK(inode->i_mode))
443 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
444 !reiserfs_posixacl(inode->i_sb)) {
448 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
453 clone = posix_acl_clone(acl, GFP_NOFS);
454 posix_acl_release(acl);
457 error = posix_acl_chmod_masq(clone, inode->i_mode);
459 error = reiserfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
460 posix_acl_release(clone);
465 posix_acl_access_get(struct inode *inode, const char *name,
466 void *buffer, size_t size)
468 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
470 return xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
474 posix_acl_access_set(struct inode *inode, const char *name,
475 const void *value, size_t size, int flags)
477 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
479 return xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
482 static size_t posix_acl_access_list(struct inode *inode, char *list,
483 size_t list_size, const char *name,
486 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
487 if (!reiserfs_posixacl(inode->i_sb))
489 if (list && size <= list_size)
490 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
494 struct xattr_handler reiserfs_posix_acl_access_handler = {
495 .prefix = POSIX_ACL_XATTR_ACCESS,
496 .get = posix_acl_access_get,
497 .set = posix_acl_access_set,
498 .list = posix_acl_access_list,
502 posix_acl_default_get(struct inode *inode, const char *name,
503 void *buffer, size_t size)
505 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
507 return xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
511 posix_acl_default_set(struct inode *inode, const char *name,
512 const void *value, size_t size, int flags)
514 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
516 return xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
519 static size_t posix_acl_default_list(struct inode *inode, char *list,
520 size_t list_size, const char *name,
523 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
524 if (!reiserfs_posixacl(inode->i_sb))
526 if (list && size <= list_size)
527 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
531 struct xattr_handler reiserfs_posix_acl_default_handler = {
532 .prefix = POSIX_ACL_XATTR_DEFAULT,
533 .get = posix_acl_default_get,
534 .set = posix_acl_default_set,
535 .list = posix_acl_default_list,