1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/capability.h>
4 #include <linux/posix_acl.h>
6 #include <linux/errno.h>
7 #include <linux/pagemap.h>
8 #include <linux/xattr.h>
9 #include <linux/slab.h>
10 #include <linux/posix_acl_xattr.h>
13 #include <linux/uaccess.h>
15 static int __reiserfs_set_acl(struct reiserfs_transaction_handle *th,
16 struct inode *inode, int type,
17 struct posix_acl *acl);
21 reiserfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
22 struct posix_acl *acl, int type)
25 struct reiserfs_transaction_handle th;
26 size_t jcreate_blocks;
27 int size = acl ? posix_acl_xattr_size(acl->a_count) : 0;
29 struct inode *inode = d_inode(dentry);
30 umode_t mode = inode->i_mode;
33 * Pessimism: We can't assume that anything from the xattr root up
37 jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
38 reiserfs_xattr_nblocks(inode, size) * 2;
40 reiserfs_write_lock(inode->i_sb);
41 error = journal_begin(&th, inode->i_sb, jcreate_blocks);
42 reiserfs_write_unlock(inode->i_sb);
44 if (type == ACL_TYPE_ACCESS && acl) {
45 error = posix_acl_update_mode(&nop_mnt_idmap, inode,
51 error = __reiserfs_set_acl(&th, inode, type, acl);
52 if (!error && update_mode)
55 reiserfs_write_lock(inode->i_sb);
56 error2 = journal_end(&th);
57 reiserfs_write_unlock(inode->i_sb);
66 * Convert from filesystem to in-memory representation.
68 static struct posix_acl *reiserfs_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 = (reiserfs_acl_entry *) value;
92 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
94 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
95 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
96 switch (acl->a_entries[n].e_tag) {
101 value = (char *)value +
102 sizeof(reiserfs_acl_entry_short);
106 value = (char *)value + sizeof(reiserfs_acl_entry);
107 if ((char *)value > end)
109 acl->a_entries[n].e_uid =
110 make_kuid(&init_user_ns,
111 le32_to_cpu(entry->e_id));
114 value = (char *)value + sizeof(reiserfs_acl_entry);
115 if ((char *)value > end)
117 acl->a_entries[n].e_gid =
118 make_kgid(&init_user_ns,
119 le32_to_cpu(entry->e_id));
131 posix_acl_release(acl);
132 return ERR_PTR(-EINVAL);
136 * Convert from in-memory to filesystem representation.
138 static void *reiserfs_posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
140 reiserfs_acl_header *ext_acl;
144 *size = reiserfs_acl_size(acl->a_count);
145 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
147 sizeof(reiserfs_acl_entry),
150 return ERR_PTR(-ENOMEM);
151 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
152 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
153 for (n = 0; n < acl->a_count; n++) {
154 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
155 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
156 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
157 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
158 switch (acl->a_entries[n].e_tag) {
160 entry->e_id = cpu_to_le32(
161 from_kuid(&init_user_ns, acl_e->e_uid));
162 e += sizeof(reiserfs_acl_entry);
165 entry->e_id = cpu_to_le32(
166 from_kgid(&init_user_ns, acl_e->e_gid));
167 e += sizeof(reiserfs_acl_entry);
174 e += sizeof(reiserfs_acl_entry_short);
181 return (char *)ext_acl;
185 return ERR_PTR(-EINVAL);
189 * Inode operation get_posix_acl().
191 * inode->i_mutex: down
192 * BKL held [before 2.5.x]
194 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type, bool rcu)
197 struct posix_acl *acl;
202 return ERR_PTR(-ECHILD);
205 case ACL_TYPE_ACCESS:
206 name = XATTR_NAME_POSIX_ACL_ACCESS;
208 case ACL_TYPE_DEFAULT:
209 name = XATTR_NAME_POSIX_ACL_DEFAULT;
215 size = reiserfs_xattr_get(inode, name, NULL, 0);
217 if (size == -ENODATA || size == -ENOSYS)
219 return ERR_PTR(size);
222 value = kmalloc(size, GFP_NOFS);
224 return ERR_PTR(-ENOMEM);
226 retval = reiserfs_xattr_get(inode, name, value, size);
227 if (retval == -ENODATA || retval == -ENOSYS) {
229 * This shouldn't actually happen as it should have
230 * been caught above.. but just in case
233 } else if (retval < 0) {
234 acl = ERR_PTR(retval);
236 acl = reiserfs_posix_acl_from_disk(value, retval);
244 * Inode operation set_posix_acl().
246 * inode->i_mutex: down
247 * BKL held [before 2.5.x]
250 __reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
251 int type, struct posix_acl *acl)
259 case ACL_TYPE_ACCESS:
260 name = XATTR_NAME_POSIX_ACL_ACCESS;
262 case ACL_TYPE_DEFAULT:
263 name = XATTR_NAME_POSIX_ACL_DEFAULT;
264 if (!S_ISDIR(inode->i_mode))
265 return acl ? -EACCES : 0;
272 value = reiserfs_posix_acl_to_disk(acl, &size);
274 return (int)PTR_ERR(value);
277 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
280 * Ensure that the inode gets dirtied if we're only using
281 * the mode bits and an old ACL didn't exist. We don't need
282 * to check if the inode is hashed here since we won't get
283 * called by reiserfs_inherit_default_acl().
285 if (error == -ENODATA) {
287 if (type == ACL_TYPE_ACCESS) {
288 inode_set_ctime_current(inode);
289 mark_inode_dirty(inode);
296 set_cached_acl(inode, type, acl);
302 * dir->i_mutex: locked,
303 * inode is new and not released into the wild yet
306 reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
307 struct inode *dir, struct dentry *dentry,
310 struct posix_acl *default_acl, *acl;
313 /* ACLs only get applied to files and directories */
314 if (S_ISLNK(inode->i_mode))
318 * ACLs can only be used on "new" objects, so if it's an old object
319 * there is nothing to inherit from
321 if (get_inode_sd_version(dir) == STAT_DATA_V1)
325 * Don't apply ACLs to objects in the .reiserfs_priv tree.. This
326 * would be useless since permissions are ignored, and a pain because
327 * it introduces locking cycles
329 if (IS_PRIVATE(inode))
332 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
337 err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
339 posix_acl_release(default_acl);
343 err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS,
345 posix_acl_release(acl);
351 /* no ACL, apply umask */
352 inode->i_mode &= ~current_umask();
356 /* This is used to cache the default acl before a new object is created.
357 * The biggest reason for this is to get an idea of how many blocks will
358 * actually be required for the create operation if we must inherit an ACL.
359 * An ACL write can add up to 3 object creations and an additional file write
360 * so we'd prefer not to reserve that many blocks in the journal if we can.
361 * It also has the advantage of not loading the ACL with a transaction open,
362 * this may seem silly, but if the owner of the directory is doing the
363 * creation, the ACL may not be loaded since the permissions wouldn't require
365 * We return the number of blocks required for the transaction.
367 int reiserfs_cache_default_acl(struct inode *inode)
369 struct posix_acl *acl;
372 if (IS_PRIVATE(inode))
375 acl = get_inode_acl(inode, ACL_TYPE_DEFAULT);
377 if (acl && !IS_ERR(acl)) {
378 int size = reiserfs_acl_size(acl->a_count);
380 /* Other xattrs can be created during inode creation. We don't
381 * want to claim too many blocks, so we check to see if we
382 * need to create the tree to the xattrs, and then we
383 * just want two files. */
384 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
385 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
387 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
389 /* We need to account for writes + bitmaps for two files */
390 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
391 posix_acl_release(acl);
398 * Called under i_mutex
400 int reiserfs_acl_chmod(struct dentry *dentry)
402 struct inode *inode = d_inode(dentry);
404 if (IS_PRIVATE(inode))
406 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
407 !reiserfs_posixacl(inode->i_sb))
410 return posix_acl_chmod(&nop_mnt_idmap, dentry, inode->i_mode);