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(&init_user_ns, 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);
52 reiserfs_write_unlock(inode->i_sb);
54 error = reiserfs_set_acl(&th, inode, type, acl);
55 reiserfs_write_lock(inode->i_sb);
56 error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
57 reiserfs_write_unlock(inode->i_sb);
63 posix_acl_release(acl);
68 posix_acl_get(struct dentry *dentry, const char *name, void *buffer,
69 size_t size, int type)
71 struct posix_acl *acl;
74 if (!reiserfs_posixacl(dentry->d_sb))
77 acl = reiserfs_get_acl(dentry->d_inode, type);
82 error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
83 posix_acl_release(acl);
89 * Convert from filesystem to in-memory representation.
91 static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
93 const char *end = (char *)value + size;
95 struct posix_acl *acl;
99 if (size < sizeof(reiserfs_acl_header))
100 return ERR_PTR(-EINVAL);
101 if (((reiserfs_acl_header *) value)->a_version !=
102 cpu_to_le32(REISERFS_ACL_VERSION))
103 return ERR_PTR(-EINVAL);
104 value = (char *)value + sizeof(reiserfs_acl_header);
105 count = reiserfs_acl_count(size);
107 return ERR_PTR(-EINVAL);
110 acl = posix_acl_alloc(count, GFP_NOFS);
112 return ERR_PTR(-ENOMEM);
113 for (n = 0; n < count; n++) {
114 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
115 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
117 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
118 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
119 switch (acl->a_entries[n].e_tag) {
124 value = (char *)value +
125 sizeof(reiserfs_acl_entry_short);
129 value = (char *)value + sizeof(reiserfs_acl_entry);
130 if ((char *)value > end)
132 acl->a_entries[n].e_uid =
133 make_kuid(&init_user_ns,
134 le32_to_cpu(entry->e_id));
137 value = (char *)value + sizeof(reiserfs_acl_entry);
138 if ((char *)value > end)
140 acl->a_entries[n].e_gid =
141 make_kgid(&init_user_ns,
142 le32_to_cpu(entry->e_id));
154 posix_acl_release(acl);
155 return ERR_PTR(-EINVAL);
159 * Convert from in-memory to filesystem representation.
161 static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
163 reiserfs_acl_header *ext_acl;
167 *size = reiserfs_acl_size(acl->a_count);
168 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
170 sizeof(reiserfs_acl_entry),
173 return ERR_PTR(-ENOMEM);
174 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
175 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
176 for (n = 0; n < acl->a_count; n++) {
177 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
178 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
179 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
180 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
181 switch (acl->a_entries[n].e_tag) {
183 entry->e_id = cpu_to_le32(
184 from_kuid(&init_user_ns, acl_e->e_uid));
185 e += sizeof(reiserfs_acl_entry);
188 entry->e_id = cpu_to_le32(
189 from_kgid(&init_user_ns, acl_e->e_gid));
190 e += sizeof(reiserfs_acl_entry);
197 e += sizeof(reiserfs_acl_entry_short);
204 return (char *)ext_acl;
208 return ERR_PTR(-EINVAL);
212 * Inode operation get_posix_acl().
214 * inode->i_mutex: down
215 * BKL held [before 2.5.x]
217 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
220 struct posix_acl *acl;
224 acl = get_cached_acl(inode, type);
225 if (acl != ACL_NOT_CACHED)
229 case ACL_TYPE_ACCESS:
230 name = POSIX_ACL_XATTR_ACCESS;
232 case ACL_TYPE_DEFAULT:
233 name = POSIX_ACL_XATTR_DEFAULT;
239 size = reiserfs_xattr_get(inode, name, NULL, 0);
241 if (size == -ENODATA || size == -ENOSYS) {
242 set_cached_acl(inode, type, NULL);
245 return ERR_PTR(size);
248 value = kmalloc(size, GFP_NOFS);
250 return ERR_PTR(-ENOMEM);
252 retval = reiserfs_xattr_get(inode, name, value, size);
253 if (retval == -ENODATA || retval == -ENOSYS) {
254 /* This shouldn't actually happen as it should have
255 been caught above.. but just in case */
257 } else if (retval < 0) {
258 acl = ERR_PTR(retval);
260 acl = posix_acl_from_disk(value, retval);
263 set_cached_acl(inode, type, acl);
270 * Inode operation set_posix_acl().
272 * inode->i_mutex: down
273 * BKL held [before 2.5.x]
276 reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
277 int type, struct posix_acl *acl)
284 if (S_ISLNK(inode->i_mode))
288 case ACL_TYPE_ACCESS:
289 name = POSIX_ACL_XATTR_ACCESS;
291 error = posix_acl_equiv_mode(acl, &inode->i_mode);
300 case ACL_TYPE_DEFAULT:
301 name = POSIX_ACL_XATTR_DEFAULT;
302 if (!S_ISDIR(inode->i_mode))
303 return acl ? -EACCES : 0;
310 value = posix_acl_to_disk(acl, &size);
312 return (int)PTR_ERR(value);
315 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
318 * Ensure that the inode gets dirtied if we're only using
319 * the mode bits and an old ACL didn't exist. We don't need
320 * to check if the inode is hashed here since we won't get
321 * called by reiserfs_inherit_default_acl().
323 if (error == -ENODATA) {
325 if (type == ACL_TYPE_ACCESS) {
326 inode->i_ctime = CURRENT_TIME_SEC;
327 mark_inode_dirty(inode);
334 set_cached_acl(inode, type, acl);
339 /* dir->i_mutex: locked,
340 * inode is new and not released into the wild yet */
342 reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
343 struct inode *dir, struct dentry *dentry,
346 struct posix_acl *acl;
349 /* ACLs only get applied to files and directories */
350 if (S_ISLNK(inode->i_mode))
353 /* ACLs can only be used on "new" objects, so if it's an old object
354 * there is nothing to inherit from */
355 if (get_inode_sd_version(dir) == STAT_DATA_V1)
358 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
359 * would be useless since permissions are ignored, and a pain because
360 * it introduces locking cycles */
361 if (IS_PRIVATE(dir)) {
362 inode->i_flags |= S_PRIVATE;
366 acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
371 /* Copy the default ACL to the default ACL of a new directory */
372 if (S_ISDIR(inode->i_mode)) {
373 err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
379 /* Now we reconcile the new ACL and the mode,
380 potentially modifying both */
381 err = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
385 /* If we need an ACL.. */
387 err = reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS, acl);
389 posix_acl_release(acl);
392 /* no ACL, apply umask */
393 inode->i_mode &= ~current_umask();
399 /* This is used to cache the default acl before a new object is created.
400 * The biggest reason for this is to get an idea of how many blocks will
401 * actually be required for the create operation if we must inherit an ACL.
402 * An ACL write can add up to 3 object creations and an additional file write
403 * so we'd prefer not to reserve that many blocks in the journal if we can.
404 * It also has the advantage of not loading the ACL with a transaction open,
405 * this may seem silly, but if the owner of the directory is doing the
406 * creation, the ACL may not be loaded since the permissions wouldn't require
408 * We return the number of blocks required for the transaction.
410 int reiserfs_cache_default_acl(struct inode *inode)
412 struct posix_acl *acl;
415 if (IS_PRIVATE(inode))
418 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
420 if (acl && !IS_ERR(acl)) {
421 int size = reiserfs_acl_size(acl->a_count);
423 /* Other xattrs can be created during inode creation. We don't
424 * want to claim too many blocks, so we check to see if we
425 * we need to create the tree to the xattrs, and then we
426 * just want two files. */
427 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
428 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
430 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
432 /* We need to account for writes + bitmaps for two files */
433 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
434 posix_acl_release(acl);
441 * Called under i_mutex
443 int reiserfs_acl_chmod(struct inode *inode)
445 struct reiserfs_transaction_handle th;
446 struct posix_acl *acl;
450 if (IS_PRIVATE(inode))
453 if (S_ISLNK(inode->i_mode))
456 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
457 !reiserfs_posixacl(inode->i_sb)) {
461 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
466 error = posix_acl_chmod(&acl, GFP_NOFS, inode->i_mode);
470 size = reiserfs_xattr_nblocks(inode, reiserfs_acl_size(acl->a_count));
471 reiserfs_write_lock(inode->i_sb);
472 error = journal_begin(&th, inode->i_sb, size * 2);
473 reiserfs_write_unlock(inode->i_sb);
476 error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS, acl);
477 reiserfs_write_lock(inode->i_sb);
478 error2 = journal_end(&th, inode->i_sb, size * 2);
479 reiserfs_write_unlock(inode->i_sb);
483 posix_acl_release(acl);
487 static size_t posix_acl_access_list(struct dentry *dentry, char *list,
488 size_t list_size, const char *name,
489 size_t name_len, int type)
491 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
492 if (!reiserfs_posixacl(dentry->d_sb))
494 if (list && size <= list_size)
495 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
499 const struct xattr_handler reiserfs_posix_acl_access_handler = {
500 .prefix = POSIX_ACL_XATTR_ACCESS,
501 .flags = ACL_TYPE_ACCESS,
502 .get = posix_acl_get,
503 .set = posix_acl_set,
504 .list = posix_acl_access_list,
507 static size_t posix_acl_default_list(struct dentry *dentry, char *list,
508 size_t list_size, const char *name,
509 size_t name_len, int type)
511 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
512 if (!reiserfs_posixacl(dentry->d_sb))
514 if (list && size <= list_size)
515 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
519 const struct xattr_handler reiserfs_posix_acl_default_handler = {
520 .prefix = POSIX_ACL_XATTR_DEFAULT,
521 .flags = ACL_TYPE_DEFAULT,
522 .get = posix_acl_get,
523 .set = posix_acl_set,
524 .list = posix_acl_default_list,