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 inode *inode, struct posix_acl *acl, int type)
24 struct reiserfs_transaction_handle th;
25 size_t jcreate_blocks;
26 int size = acl ? posix_acl_xattr_size(acl->a_count) : 0;
28 umode_t mode = inode->i_mode;
31 * Pessimism: We can't assume that anything from the xattr root up
35 jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
36 reiserfs_xattr_nblocks(inode, size) * 2;
38 reiserfs_write_lock(inode->i_sb);
39 error = journal_begin(&th, inode->i_sb, jcreate_blocks);
40 reiserfs_write_unlock(inode->i_sb);
42 if (type == ACL_TYPE_ACCESS && acl) {
43 error = posix_acl_update_mode(&init_user_ns, inode,
49 error = __reiserfs_set_acl(&th, inode, type, acl);
50 if (!error && update_mode)
53 reiserfs_write_lock(inode->i_sb);
54 error2 = journal_end(&th);
55 reiserfs_write_unlock(inode->i_sb);
64 * Convert from filesystem to in-memory representation.
66 static struct posix_acl *reiserfs_posix_acl_from_disk(const void *value, size_t size)
68 const char *end = (char *)value + size;
70 struct posix_acl *acl;
74 if (size < sizeof(reiserfs_acl_header))
75 return ERR_PTR(-EINVAL);
76 if (((reiserfs_acl_header *) value)->a_version !=
77 cpu_to_le32(REISERFS_ACL_VERSION))
78 return ERR_PTR(-EINVAL);
79 value = (char *)value + sizeof(reiserfs_acl_header);
80 count = reiserfs_acl_count(size);
82 return ERR_PTR(-EINVAL);
85 acl = posix_acl_alloc(count, GFP_NOFS);
87 return ERR_PTR(-ENOMEM);
88 for (n = 0; n < count; n++) {
89 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
90 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
92 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
93 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
94 switch (acl->a_entries[n].e_tag) {
99 value = (char *)value +
100 sizeof(reiserfs_acl_entry_short);
104 value = (char *)value + sizeof(reiserfs_acl_entry);
105 if ((char *)value > end)
107 acl->a_entries[n].e_uid =
108 make_kuid(&init_user_ns,
109 le32_to_cpu(entry->e_id));
112 value = (char *)value + sizeof(reiserfs_acl_entry);
113 if ((char *)value > end)
115 acl->a_entries[n].e_gid =
116 make_kgid(&init_user_ns,
117 le32_to_cpu(entry->e_id));
129 posix_acl_release(acl);
130 return ERR_PTR(-EINVAL);
134 * Convert from in-memory to filesystem representation.
136 static void *reiserfs_posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
138 reiserfs_acl_header *ext_acl;
142 *size = reiserfs_acl_size(acl->a_count);
143 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
145 sizeof(reiserfs_acl_entry),
148 return ERR_PTR(-ENOMEM);
149 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
150 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
151 for (n = 0; n < acl->a_count; n++) {
152 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
153 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
154 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
155 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
156 switch (acl->a_entries[n].e_tag) {
158 entry->e_id = cpu_to_le32(
159 from_kuid(&init_user_ns, acl_e->e_uid));
160 e += sizeof(reiserfs_acl_entry);
163 entry->e_id = cpu_to_le32(
164 from_kgid(&init_user_ns, acl_e->e_gid));
165 e += sizeof(reiserfs_acl_entry);
172 e += sizeof(reiserfs_acl_entry_short);
179 return (char *)ext_acl;
183 return ERR_PTR(-EINVAL);
187 * Inode operation get_posix_acl().
189 * inode->i_mutex: down
190 * BKL held [before 2.5.x]
192 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
195 struct posix_acl *acl;
200 case ACL_TYPE_ACCESS:
201 name = XATTR_NAME_POSIX_ACL_ACCESS;
203 case ACL_TYPE_DEFAULT:
204 name = XATTR_NAME_POSIX_ACL_DEFAULT;
210 size = reiserfs_xattr_get(inode, name, NULL, 0);
212 if (size == -ENODATA || size == -ENOSYS)
214 return ERR_PTR(size);
217 value = kmalloc(size, GFP_NOFS);
219 return ERR_PTR(-ENOMEM);
221 retval = reiserfs_xattr_get(inode, name, value, size);
222 if (retval == -ENODATA || retval == -ENOSYS) {
224 * This shouldn't actually happen as it should have
225 * been caught above.. but just in case
228 } else if (retval < 0) {
229 acl = ERR_PTR(retval);
231 acl = reiserfs_posix_acl_from_disk(value, retval);
239 * Inode operation set_posix_acl().
241 * inode->i_mutex: down
242 * BKL held [before 2.5.x]
245 __reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
246 int type, struct posix_acl *acl)
254 case ACL_TYPE_ACCESS:
255 name = XATTR_NAME_POSIX_ACL_ACCESS;
257 case ACL_TYPE_DEFAULT:
258 name = XATTR_NAME_POSIX_ACL_DEFAULT;
259 if (!S_ISDIR(inode->i_mode))
260 return acl ? -EACCES : 0;
267 value = reiserfs_posix_acl_to_disk(acl, &size);
269 return (int)PTR_ERR(value);
272 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
275 * Ensure that the inode gets dirtied if we're only using
276 * the mode bits and an old ACL didn't exist. We don't need
277 * to check if the inode is hashed here since we won't get
278 * called by reiserfs_inherit_default_acl().
280 if (error == -ENODATA) {
282 if (type == ACL_TYPE_ACCESS) {
283 inode->i_ctime = current_time(inode);
284 mark_inode_dirty(inode);
291 set_cached_acl(inode, type, acl);
297 * dir->i_mutex: locked,
298 * inode is new and not released into the wild yet
301 reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
302 struct inode *dir, struct dentry *dentry,
305 struct posix_acl *default_acl, *acl;
308 /* ACLs only get applied to files and directories */
309 if (S_ISLNK(inode->i_mode))
313 * ACLs can only be used on "new" objects, so if it's an old object
314 * there is nothing to inherit from
316 if (get_inode_sd_version(dir) == STAT_DATA_V1)
320 * Don't apply ACLs to objects in the .reiserfs_priv tree.. This
321 * would be useless since permissions are ignored, and a pain because
322 * it introduces locking cycles
324 if (IS_PRIVATE(inode))
327 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
332 err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
334 posix_acl_release(default_acl);
338 err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS,
340 posix_acl_release(acl);
346 /* no ACL, apply umask */
347 inode->i_mode &= ~current_umask();
351 /* This is used to cache the default acl before a new object is created.
352 * The biggest reason for this is to get an idea of how many blocks will
353 * actually be required for the create operation if we must inherit an ACL.
354 * An ACL write can add up to 3 object creations and an additional file write
355 * so we'd prefer not to reserve that many blocks in the journal if we can.
356 * It also has the advantage of not loading the ACL with a transaction open,
357 * this may seem silly, but if the owner of the directory is doing the
358 * creation, the ACL may not be loaded since the permissions wouldn't require
360 * We return the number of blocks required for the transaction.
362 int reiserfs_cache_default_acl(struct inode *inode)
364 struct posix_acl *acl;
367 if (IS_PRIVATE(inode))
370 acl = get_acl(inode, ACL_TYPE_DEFAULT);
372 if (acl && !IS_ERR(acl)) {
373 int size = reiserfs_acl_size(acl->a_count);
375 /* Other xattrs can be created during inode creation. We don't
376 * want to claim too many blocks, so we check to see if we
377 * need to create the tree to the xattrs, and then we
378 * just want two files. */
379 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
380 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
382 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
384 /* We need to account for writes + bitmaps for two files */
385 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
386 posix_acl_release(acl);
393 * Called under i_mutex
395 int reiserfs_acl_chmod(struct inode *inode)
397 if (IS_PRIVATE(inode))
399 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
400 !reiserfs_posixacl(inode->i_sb))
403 return posix_acl_chmod(&init_user_ns, inode, inode->i_mode);