1 #include <linux/capability.h>
3 #include <linux/posix_acl.h>
5 #include <linux/errno.h>
6 #include <linux/pagemap.h>
7 #include <linux/xattr.h>
8 #include <linux/slab.h>
9 #include <linux/posix_acl_xattr.h>
12 #include <asm/uaccess.h>
14 static int reiserfs_set_acl(struct reiserfs_transaction_handle *th,
15 struct inode *inode, int type,
16 struct posix_acl *acl);
19 posix_acl_set(struct dentry *dentry, const char *name, const void *value,
20 size_t size, int flags, int type)
22 struct inode *inode = dentry->d_inode;
23 struct posix_acl *acl;
25 struct reiserfs_transaction_handle th;
26 size_t jcreate_blocks;
27 if (!reiserfs_posixacl(inode->i_sb))
29 if (!inode_owner_or_capable(inode))
33 acl = posix_acl_from_xattr(value, size);
37 error = posix_acl_valid(acl);
44 /* Pessimism: We can't assume that anything from the xattr root up
45 * has been created. */
47 jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
48 reiserfs_xattr_nblocks(inode, size) * 2;
50 reiserfs_write_lock(inode->i_sb);
51 error = journal_begin(&th, inode->i_sb, jcreate_blocks);
53 error = reiserfs_set_acl(&th, inode, type, acl);
54 error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
58 reiserfs_write_unlock(inode->i_sb);
61 posix_acl_release(acl);
66 posix_acl_get(struct dentry *dentry, const char *name, void *buffer,
67 size_t size, int type)
69 struct posix_acl *acl;
72 if (!reiserfs_posixacl(dentry->d_sb))
75 acl = reiserfs_get_acl(dentry->d_inode, type);
80 error = posix_acl_to_xattr(acl, buffer, size);
81 posix_acl_release(acl);
87 * Convert from filesystem to in-memory representation.
89 static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
91 const char *end = (char *)value + size;
93 struct posix_acl *acl;
97 if (size < sizeof(reiserfs_acl_header))
98 return ERR_PTR(-EINVAL);
99 if (((reiserfs_acl_header *) value)->a_version !=
100 cpu_to_le32(REISERFS_ACL_VERSION))
101 return ERR_PTR(-EINVAL);
102 value = (char *)value + sizeof(reiserfs_acl_header);
103 count = reiserfs_acl_count(size);
105 return ERR_PTR(-EINVAL);
108 acl = posix_acl_alloc(count, GFP_NOFS);
110 return ERR_PTR(-ENOMEM);
111 for (n = 0; n < count; n++) {
112 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
113 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
115 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
116 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
117 switch (acl->a_entries[n].e_tag) {
122 value = (char *)value +
123 sizeof(reiserfs_acl_entry_short);
124 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
129 value = (char *)value + sizeof(reiserfs_acl_entry);
130 if ((char *)value > end)
132 acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
144 posix_acl_release(acl);
145 return ERR_PTR(-EINVAL);
149 * Convert from in-memory to filesystem representation.
151 static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
153 reiserfs_acl_header *ext_acl;
157 *size = reiserfs_acl_size(acl->a_count);
158 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
160 sizeof(reiserfs_acl_entry),
163 return ERR_PTR(-ENOMEM);
164 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
165 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
166 for (n = 0; n < acl->a_count; n++) {
167 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
168 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
169 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
170 switch (acl->a_entries[n].e_tag) {
173 entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
174 e += sizeof(reiserfs_acl_entry);
181 e += sizeof(reiserfs_acl_entry_short);
188 return (char *)ext_acl;
192 return ERR_PTR(-EINVAL);
196 * Inode operation get_posix_acl().
198 * inode->i_mutex: down
199 * BKL held [before 2.5.x]
201 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
204 struct posix_acl *acl;
208 acl = get_cached_acl(inode, type);
209 if (acl != ACL_NOT_CACHED)
213 case ACL_TYPE_ACCESS:
214 name = POSIX_ACL_XATTR_ACCESS;
216 case ACL_TYPE_DEFAULT:
217 name = POSIX_ACL_XATTR_DEFAULT;
223 size = reiserfs_xattr_get(inode, name, NULL, 0);
225 if (size == -ENODATA || size == -ENOSYS) {
226 set_cached_acl(inode, type, NULL);
229 return ERR_PTR(size);
232 value = kmalloc(size, GFP_NOFS);
234 return ERR_PTR(-ENOMEM);
236 retval = reiserfs_xattr_get(inode, name, value, size);
237 if (retval == -ENODATA || retval == -ENOSYS) {
238 /* This shouldn't actually happen as it should have
239 been caught above.. but just in case */
241 } else if (retval < 0) {
242 acl = ERR_PTR(retval);
244 acl = posix_acl_from_disk(value, retval);
247 set_cached_acl(inode, type, acl);
254 * Inode operation set_posix_acl().
256 * inode->i_mutex: down
257 * BKL held [before 2.5.x]
260 reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
261 int type, struct posix_acl *acl)
268 if (S_ISLNK(inode->i_mode))
272 case ACL_TYPE_ACCESS:
273 name = POSIX_ACL_XATTR_ACCESS;
275 error = posix_acl_equiv_mode(acl, &inode->i_mode);
284 case ACL_TYPE_DEFAULT:
285 name = POSIX_ACL_XATTR_DEFAULT;
286 if (!S_ISDIR(inode->i_mode))
287 return acl ? -EACCES : 0;
294 value = posix_acl_to_disk(acl, &size);
296 return (int)PTR_ERR(value);
299 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
302 * Ensure that the inode gets dirtied if we're only using
303 * the mode bits and an old ACL didn't exist. We don't need
304 * to check if the inode is hashed here since we won't get
305 * called by reiserfs_inherit_default_acl().
307 if (error == -ENODATA) {
309 if (type == ACL_TYPE_ACCESS) {
310 inode->i_ctime = CURRENT_TIME_SEC;
311 mark_inode_dirty(inode);
318 set_cached_acl(inode, type, acl);
323 /* dir->i_mutex: locked,
324 * inode is new and not released into the wild yet */
326 reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
327 struct inode *dir, struct dentry *dentry,
330 struct posix_acl *acl;
333 /* ACLs only get applied to files and directories */
334 if (S_ISLNK(inode->i_mode))
337 /* ACLs can only be used on "new" objects, so if it's an old object
338 * there is nothing to inherit from */
339 if (get_inode_sd_version(dir) == STAT_DATA_V1)
342 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
343 * would be useless since permissions are ignored, and a pain because
344 * it introduces locking cycles */
345 if (IS_PRIVATE(dir)) {
346 inode->i_flags |= S_PRIVATE;
350 acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
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(th, inode, ACL_TYPE_DEFAULT,
363 /* Now we reconcile the new ACL and the mode,
364 potentially modifying both */
365 err = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
369 /* If we need an ACL.. */
371 err = reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS, acl);
373 posix_acl_release(acl);
376 /* no ACL, apply umask */
377 inode->i_mode &= ~current_umask();
383 /* This is used to cache the default acl before a new object is created.
384 * The biggest reason for this is to get an idea of how many blocks will
385 * actually be required for the create operation if we must inherit an ACL.
386 * An ACL write can add up to 3 object creations and an additional file write
387 * so we'd prefer not to reserve that many blocks in the journal if we can.
388 * It also has the advantage of not loading the ACL with a transaction open,
389 * this may seem silly, but if the owner of the directory is doing the
390 * creation, the ACL may not be loaded since the permissions wouldn't require
392 * We return the number of blocks required for the transaction.
394 int reiserfs_cache_default_acl(struct inode *inode)
396 struct posix_acl *acl;
399 if (IS_PRIVATE(inode))
402 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
404 if (acl && !IS_ERR(acl)) {
405 int size = reiserfs_acl_size(acl->a_count);
407 /* Other xattrs can be created during inode creation. We don't
408 * want to claim too many blocks, so we check to see if we
409 * we need to create the tree to the xattrs, and then we
410 * just want two files. */
411 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
412 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
414 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
416 /* We need to account for writes + bitmaps for two files */
417 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
418 posix_acl_release(acl);
424 int reiserfs_acl_chmod(struct inode *inode)
426 struct reiserfs_transaction_handle th;
427 struct posix_acl *acl;
432 if (S_ISLNK(inode->i_mode))
435 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
436 !reiserfs_posixacl(inode->i_sb)) {
440 reiserfs_write_unlock(inode->i_sb);
441 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
442 reiserfs_write_lock(inode->i_sb);
447 error = posix_acl_chmod(&acl, GFP_NOFS, inode->i_mode);
451 size = reiserfs_xattr_nblocks(inode, reiserfs_acl_size(acl->a_count));
452 depth = reiserfs_write_lock_once(inode->i_sb);
453 error = journal_begin(&th, inode->i_sb, size * 2);
456 error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS, acl);
457 error2 = journal_end(&th, inode->i_sb, size * 2);
461 reiserfs_write_unlock_once(inode->i_sb, depth);
462 posix_acl_release(acl);
466 static size_t posix_acl_access_list(struct dentry *dentry, char *list,
467 size_t list_size, const char *name,
468 size_t name_len, int type)
470 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
471 if (!reiserfs_posixacl(dentry->d_sb))
473 if (list && size <= list_size)
474 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
478 const struct xattr_handler reiserfs_posix_acl_access_handler = {
479 .prefix = POSIX_ACL_XATTR_ACCESS,
480 .flags = ACL_TYPE_ACCESS,
481 .get = posix_acl_get,
482 .set = posix_acl_set,
483 .list = posix_acl_access_list,
486 static size_t posix_acl_default_list(struct dentry *dentry, char *list,
487 size_t list_size, const char *name,
488 size_t name_len, int type)
490 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
491 if (!reiserfs_posixacl(dentry->d_sb))
493 if (list && size <= list_size)
494 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
498 const struct xattr_handler reiserfs_posix_acl_default_handler = {
499 .prefix = POSIX_ACL_XATTR_DEFAULT,
500 .flags = ACL_TYPE_DEFAULT,
501 .get = posix_acl_get,
502 .set = posix_acl_set,
503 .list = posix_acl_default_list,