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 user_namespace *mnt_userns, struct inode *inode,
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 umode_t mode = inode->i_mode;
32 * Pessimism: We can't assume that anything from the xattr root up
36 jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
37 reiserfs_xattr_nblocks(inode, size) * 2;
39 reiserfs_write_lock(inode->i_sb);
40 error = journal_begin(&th, inode->i_sb, jcreate_blocks);
41 reiserfs_write_unlock(inode->i_sb);
43 if (type == ACL_TYPE_ACCESS && acl) {
44 error = posix_acl_update_mode(&init_user_ns, inode,
50 error = __reiserfs_set_acl(&th, inode, type, acl);
51 if (!error && update_mode)
54 reiserfs_write_lock(inode->i_sb);
55 error2 = journal_end(&th);
56 reiserfs_write_unlock(inode->i_sb);
65 * Convert from filesystem to in-memory representation.
67 static struct posix_acl *reiserfs_posix_acl_from_disk(const void *value, size_t size)
69 const char *end = (char *)value + size;
71 struct posix_acl *acl;
75 if (size < sizeof(reiserfs_acl_header))
76 return ERR_PTR(-EINVAL);
77 if (((reiserfs_acl_header *) value)->a_version !=
78 cpu_to_le32(REISERFS_ACL_VERSION))
79 return ERR_PTR(-EINVAL);
80 value = (char *)value + sizeof(reiserfs_acl_header);
81 count = reiserfs_acl_count(size);
83 return ERR_PTR(-EINVAL);
86 acl = posix_acl_alloc(count, GFP_NOFS);
88 return ERR_PTR(-ENOMEM);
89 for (n = 0; n < count; n++) {
90 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
91 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
93 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
94 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
95 switch (acl->a_entries[n].e_tag) {
100 value = (char *)value +
101 sizeof(reiserfs_acl_entry_short);
105 value = (char *)value + sizeof(reiserfs_acl_entry);
106 if ((char *)value > end)
108 acl->a_entries[n].e_uid =
109 make_kuid(&init_user_ns,
110 le32_to_cpu(entry->e_id));
113 value = (char *)value + sizeof(reiserfs_acl_entry);
114 if ((char *)value > end)
116 acl->a_entries[n].e_gid =
117 make_kgid(&init_user_ns,
118 le32_to_cpu(entry->e_id));
130 posix_acl_release(acl);
131 return ERR_PTR(-EINVAL);
135 * Convert from in-memory to filesystem representation.
137 static void *reiserfs_posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
139 reiserfs_acl_header *ext_acl;
143 *size = reiserfs_acl_size(acl->a_count);
144 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
146 sizeof(reiserfs_acl_entry),
149 return ERR_PTR(-ENOMEM);
150 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
151 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
152 for (n = 0; n < acl->a_count; n++) {
153 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
154 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
155 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
156 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
157 switch (acl->a_entries[n].e_tag) {
159 entry->e_id = cpu_to_le32(
160 from_kuid(&init_user_ns, acl_e->e_uid));
161 e += sizeof(reiserfs_acl_entry);
164 entry->e_id = cpu_to_le32(
165 from_kgid(&init_user_ns, acl_e->e_gid));
166 e += sizeof(reiserfs_acl_entry);
173 e += sizeof(reiserfs_acl_entry_short);
180 return (char *)ext_acl;
184 return ERR_PTR(-EINVAL);
188 * Inode operation get_posix_acl().
190 * inode->i_mutex: down
191 * BKL held [before 2.5.x]
193 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type, bool rcu)
196 struct posix_acl *acl;
201 return ERR_PTR(-ECHILD);
204 case ACL_TYPE_ACCESS:
205 name = XATTR_NAME_POSIX_ACL_ACCESS;
207 case ACL_TYPE_DEFAULT:
208 name = XATTR_NAME_POSIX_ACL_DEFAULT;
214 size = reiserfs_xattr_get(inode, name, NULL, 0);
216 if (size == -ENODATA || size == -ENOSYS)
218 return ERR_PTR(size);
221 value = kmalloc(size, GFP_NOFS);
223 return ERR_PTR(-ENOMEM);
225 retval = reiserfs_xattr_get(inode, name, value, size);
226 if (retval == -ENODATA || retval == -ENOSYS) {
228 * This shouldn't actually happen as it should have
229 * been caught above.. but just in case
232 } else if (retval < 0) {
233 acl = ERR_PTR(retval);
235 acl = reiserfs_posix_acl_from_disk(value, retval);
243 * Inode operation set_posix_acl().
245 * inode->i_mutex: down
246 * BKL held [before 2.5.x]
249 __reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
250 int type, struct posix_acl *acl)
258 case ACL_TYPE_ACCESS:
259 name = XATTR_NAME_POSIX_ACL_ACCESS;
261 case ACL_TYPE_DEFAULT:
262 name = XATTR_NAME_POSIX_ACL_DEFAULT;
263 if (!S_ISDIR(inode->i_mode))
264 return acl ? -EACCES : 0;
271 value = reiserfs_posix_acl_to_disk(acl, &size);
273 return (int)PTR_ERR(value);
276 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
279 * Ensure that the inode gets dirtied if we're only using
280 * the mode bits and an old ACL didn't exist. We don't need
281 * to check if the inode is hashed here since we won't get
282 * called by reiserfs_inherit_default_acl().
284 if (error == -ENODATA) {
286 if (type == ACL_TYPE_ACCESS) {
287 inode->i_ctime = current_time(inode);
288 mark_inode_dirty(inode);
295 set_cached_acl(inode, type, acl);
301 * dir->i_mutex: locked,
302 * inode is new and not released into the wild yet
305 reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
306 struct inode *dir, struct dentry *dentry,
309 struct posix_acl *default_acl, *acl;
312 /* ACLs only get applied to files and directories */
313 if (S_ISLNK(inode->i_mode))
317 * ACLs can only be used on "new" objects, so if it's an old object
318 * there is nothing to inherit from
320 if (get_inode_sd_version(dir) == STAT_DATA_V1)
324 * Don't apply ACLs to objects in the .reiserfs_priv tree.. This
325 * would be useless since permissions are ignored, and a pain because
326 * it introduces locking cycles
328 if (IS_PRIVATE(inode))
331 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
336 err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
338 posix_acl_release(default_acl);
342 err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS,
344 posix_acl_release(acl);
350 /* no ACL, apply umask */
351 inode->i_mode &= ~current_umask();
355 /* This is used to cache the default acl before a new object is created.
356 * The biggest reason for this is to get an idea of how many blocks will
357 * actually be required for the create operation if we must inherit an ACL.
358 * An ACL write can add up to 3 object creations and an additional file write
359 * so we'd prefer not to reserve that many blocks in the journal if we can.
360 * It also has the advantage of not loading the ACL with a transaction open,
361 * this may seem silly, but if the owner of the directory is doing the
362 * creation, the ACL may not be loaded since the permissions wouldn't require
364 * We return the number of blocks required for the transaction.
366 int reiserfs_cache_default_acl(struct inode *inode)
368 struct posix_acl *acl;
371 if (IS_PRIVATE(inode))
374 acl = get_acl(inode, ACL_TYPE_DEFAULT);
376 if (acl && !IS_ERR(acl)) {
377 int size = reiserfs_acl_size(acl->a_count);
379 /* Other xattrs can be created during inode creation. We don't
380 * want to claim too many blocks, so we check to see if we
381 * need to create the tree to the xattrs, and then we
382 * just want two files. */
383 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
384 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
386 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
388 /* We need to account for writes + bitmaps for two files */
389 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
390 posix_acl_release(acl);
397 * Called under i_mutex
399 int reiserfs_acl_chmod(struct inode *inode)
401 if (IS_PRIVATE(inode))
403 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
404 !reiserfs_posixacl(inode->i_sb))
407 return posix_acl_chmod(&init_user_ns, inode, inode->i_mode);