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);
20 reiserfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
23 struct reiserfs_transaction_handle th;
24 size_t jcreate_blocks;
25 int size = acl ? posix_acl_xattr_size(acl->a_count) : 0;
29 * Pessimism: We can't assume that anything from the xattr root up
33 jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
34 reiserfs_xattr_nblocks(inode, size) * 2;
36 reiserfs_write_lock(inode->i_sb);
37 error = journal_begin(&th, inode->i_sb, jcreate_blocks);
38 reiserfs_write_unlock(inode->i_sb);
40 error = __reiserfs_set_acl(&th, inode, type, acl);
41 reiserfs_write_lock(inode->i_sb);
42 error2 = journal_end(&th);
43 reiserfs_write_unlock(inode->i_sb);
52 * Convert from filesystem to in-memory representation.
54 static struct posix_acl *reiserfs_posix_acl_from_disk(const void *value, size_t size)
56 const char *end = (char *)value + size;
58 struct posix_acl *acl;
62 if (size < sizeof(reiserfs_acl_header))
63 return ERR_PTR(-EINVAL);
64 if (((reiserfs_acl_header *) value)->a_version !=
65 cpu_to_le32(REISERFS_ACL_VERSION))
66 return ERR_PTR(-EINVAL);
67 value = (char *)value + sizeof(reiserfs_acl_header);
68 count = reiserfs_acl_count(size);
70 return ERR_PTR(-EINVAL);
73 acl = posix_acl_alloc(count, GFP_NOFS);
75 return ERR_PTR(-ENOMEM);
76 for (n = 0; n < count; n++) {
77 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
78 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
80 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
81 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
82 switch (acl->a_entries[n].e_tag) {
87 value = (char *)value +
88 sizeof(reiserfs_acl_entry_short);
92 value = (char *)value + sizeof(reiserfs_acl_entry);
93 if ((char *)value > end)
95 acl->a_entries[n].e_uid =
96 make_kuid(&init_user_ns,
97 le32_to_cpu(entry->e_id));
100 value = (char *)value + sizeof(reiserfs_acl_entry);
101 if ((char *)value > end)
103 acl->a_entries[n].e_gid =
104 make_kgid(&init_user_ns,
105 le32_to_cpu(entry->e_id));
117 posix_acl_release(acl);
118 return ERR_PTR(-EINVAL);
122 * Convert from in-memory to filesystem representation.
124 static void *reiserfs_posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
126 reiserfs_acl_header *ext_acl;
130 *size = reiserfs_acl_size(acl->a_count);
131 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
133 sizeof(reiserfs_acl_entry),
136 return ERR_PTR(-ENOMEM);
137 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
138 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
139 for (n = 0; n < acl->a_count; n++) {
140 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
141 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
142 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
143 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
144 switch (acl->a_entries[n].e_tag) {
146 entry->e_id = cpu_to_le32(
147 from_kuid(&init_user_ns, acl_e->e_uid));
148 e += sizeof(reiserfs_acl_entry);
151 entry->e_id = cpu_to_le32(
152 from_kgid(&init_user_ns, acl_e->e_gid));
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().
177 * inode->i_mutex: down
178 * BKL held [before 2.5.x]
180 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
183 struct posix_acl *acl;
188 case ACL_TYPE_ACCESS:
189 name = POSIX_ACL_XATTR_ACCESS;
191 case ACL_TYPE_DEFAULT:
192 name = POSIX_ACL_XATTR_DEFAULT;
198 size = reiserfs_xattr_get(inode, name, NULL, 0);
200 if (size == -ENODATA || size == -ENOSYS) {
201 set_cached_acl(inode, type, NULL);
204 return ERR_PTR(size);
207 value = kmalloc(size, GFP_NOFS);
209 return ERR_PTR(-ENOMEM);
211 retval = reiserfs_xattr_get(inode, name, value, size);
212 if (retval == -ENODATA || retval == -ENOSYS) {
214 * This shouldn't actually happen as it should have
215 * been caught above.. but just in case
218 } else if (retval < 0) {
219 acl = ERR_PTR(retval);
221 acl = reiserfs_posix_acl_from_disk(value, retval);
224 set_cached_acl(inode, type, acl);
231 * Inode operation set_posix_acl().
233 * inode->i_mutex: down
234 * BKL held [before 2.5.x]
237 __reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
238 int type, struct posix_acl *acl)
246 case ACL_TYPE_ACCESS:
247 name = POSIX_ACL_XATTR_ACCESS;
249 error = posix_acl_equiv_mode(acl, &inode->i_mode);
258 case ACL_TYPE_DEFAULT:
259 name = POSIX_ACL_XATTR_DEFAULT;
260 if (!S_ISDIR(inode->i_mode))
261 return acl ? -EACCES : 0;
268 value = reiserfs_posix_acl_to_disk(acl, &size);
270 return (int)PTR_ERR(value);
273 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
276 * Ensure that the inode gets dirtied if we're only using
277 * the mode bits and an old ACL didn't exist. We don't need
278 * to check if the inode is hashed here since we won't get
279 * called by reiserfs_inherit_default_acl().
281 if (error == -ENODATA) {
283 if (type == ACL_TYPE_ACCESS) {
284 inode->i_ctime = CURRENT_TIME_SEC;
285 mark_inode_dirty(inode);
292 set_cached_acl(inode, type, acl);
298 * dir->i_mutex: locked,
299 * inode is new and not released into the wild yet
302 reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
303 struct inode *dir, struct dentry *dentry,
306 struct posix_acl *default_acl, *acl;
309 /* ACLs only get applied to files and directories */
310 if (S_ISLNK(inode->i_mode))
314 * ACLs can only be used on "new" objects, so if it's an old object
315 * there is nothing to inherit from
317 if (get_inode_sd_version(dir) == STAT_DATA_V1)
321 * Don't apply ACLs to objects in the .reiserfs_priv tree.. This
322 * would be useless since permissions are ignored, and a pain because
323 * it introduces locking cycles
325 if (IS_PRIVATE(dir)) {
326 inode->i_flags |= S_PRIVATE;
330 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
335 err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
337 posix_acl_release(default_acl);
341 err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS,
343 posix_acl_release(acl);
349 /* no ACL, apply umask */
350 inode->i_mode &= ~current_umask();
354 /* This is used to cache the default acl before a new object is created.
355 * The biggest reason for this is to get an idea of how many blocks will
356 * actually be required for the create operation if we must inherit an ACL.
357 * An ACL write can add up to 3 object creations and an additional file write
358 * so we'd prefer not to reserve that many blocks in the journal if we can.
359 * It also has the advantage of not loading the ACL with a transaction open,
360 * this may seem silly, but if the owner of the directory is doing the
361 * creation, the ACL may not be loaded since the permissions wouldn't require
363 * We return the number of blocks required for the transaction.
365 int reiserfs_cache_default_acl(struct inode *inode)
367 struct posix_acl *acl;
370 if (IS_PRIVATE(inode))
373 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
375 if (acl && !IS_ERR(acl)) {
376 int size = reiserfs_acl_size(acl->a_count);
378 /* Other xattrs can be created during inode creation. We don't
379 * want to claim too many blocks, so we check to see if we
380 * we need to create the tree to the xattrs, and then we
381 * just want two files. */
382 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
383 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
385 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
387 /* We need to account for writes + bitmaps for two files */
388 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
389 posix_acl_release(acl);
396 * Called under i_mutex
398 int reiserfs_acl_chmod(struct inode *inode)
400 if (IS_PRIVATE(inode))
402 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
403 !reiserfs_posixacl(inode->i_sb))
406 return posix_acl_chmod(inode, inode->i_mode);