1 #ifdef CONFIG_BCACHEFS_POSIX_ACL
6 #include <linux/posix_acl.h>
7 #include <linux/posix_acl_xattr.h>
8 #include <linux/sched.h>
9 #include <linux/slab.h>
16 * Convert from filesystem to in-memory representation.
18 static struct posix_acl *bch2_acl_from_disk(const void *value, size_t size)
20 const char *end = (char *)value + size;
22 struct posix_acl *acl;
26 if (size < sizeof(bch_acl_header))
27 return ERR_PTR(-EINVAL);
28 if (((bch_acl_header *)value)->a_version !=
29 cpu_to_le32(BCH_ACL_VERSION))
30 return ERR_PTR(-EINVAL);
31 value = (char *)value + sizeof(bch_acl_header);
32 count = bch2_acl_count(size);
34 return ERR_PTR(-EINVAL);
37 acl = posix_acl_alloc(count, GFP_KERNEL);
39 return ERR_PTR(-ENOMEM);
40 for (n = 0; n < count; n++) {
41 bch_acl_entry *entry =
42 (bch_acl_entry *)value;
43 if ((char *)value + sizeof(bch_acl_entry_short) > end)
45 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
46 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
47 switch (acl->a_entries[n].e_tag) {
52 value = (char *)value +
53 sizeof(bch_acl_entry_short);
57 value = (char *)value + sizeof(bch_acl_entry);
58 if ((char *)value > end)
60 acl->a_entries[n].e_uid =
61 make_kuid(&init_user_ns,
62 le32_to_cpu(entry->e_id));
65 value = (char *)value + sizeof(bch_acl_entry);
66 if ((char *)value > end)
68 acl->a_entries[n].e_gid =
69 make_kgid(&init_user_ns,
70 le32_to_cpu(entry->e_id));
82 posix_acl_release(acl);
83 return ERR_PTR(-EINVAL);
87 * Convert from in-memory to filesystem representation.
89 static void *bch2_acl_to_disk(const struct posix_acl *acl, size_t *size)
91 bch_acl_header *ext_acl;
95 *size = bch2_acl_size(acl->a_count);
96 ext_acl = kmalloc(sizeof(bch_acl_header) + acl->a_count *
97 sizeof(bch_acl_entry), GFP_KERNEL);
99 return ERR_PTR(-ENOMEM);
100 ext_acl->a_version = cpu_to_le32(BCH_ACL_VERSION);
101 e = (char *)ext_acl + sizeof(bch_acl_header);
102 for (n = 0; n < acl->a_count; n++) {
103 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
104 bch_acl_entry *entry = (bch_acl_entry *)e;
106 entry->e_tag = cpu_to_le16(acl_e->e_tag);
107 entry->e_perm = cpu_to_le16(acl_e->e_perm);
108 switch (acl_e->e_tag) {
110 entry->e_id = cpu_to_le32(
111 from_kuid(&init_user_ns, acl_e->e_uid));
112 e += sizeof(bch_acl_entry);
115 entry->e_id = cpu_to_le32(
116 from_kgid(&init_user_ns, acl_e->e_gid));
117 e += sizeof(bch_acl_entry);
124 e += sizeof(bch_acl_entry_short);
131 return (char *)ext_acl;
135 return ERR_PTR(-EINVAL);
138 struct posix_acl *bch2_get_acl(struct inode *vinode, int type)
140 struct bch_inode_info *inode = to_bch_ei(vinode);
141 struct bch_fs *c = inode->v.i_sb->s_fs_info;
144 struct posix_acl *acl;
148 case ACL_TYPE_ACCESS:
149 name_index = BCH_XATTR_INDEX_POSIX_ACL_ACCESS;
151 case ACL_TYPE_DEFAULT:
152 name_index = BCH_XATTR_INDEX_POSIX_ACL_DEFAULT;
157 ret = bch2_xattr_get(c, inode, "", NULL, 0, name_index);
159 value = kmalloc(ret, GFP_KERNEL);
161 return ERR_PTR(-ENOMEM);
162 ret = bch2_xattr_get(c, inode, "", value,
166 acl = bch2_acl_from_disk(value, ret);
167 else if (ret == -ENODATA || ret == -ENOSYS)
174 set_cached_acl(&inode->v, type, acl);
179 int __bch2_set_acl(struct inode *vinode, struct posix_acl *acl, int type)
181 struct bch_inode_info *inode = to_bch_ei(vinode);
182 struct bch_fs *c = inode->v.i_sb->s_fs_info;
189 case ACL_TYPE_ACCESS:
190 name_index = BCH_XATTR_INDEX_POSIX_ACL_ACCESS;
192 case ACL_TYPE_DEFAULT:
193 name_index = BCH_XATTR_INDEX_POSIX_ACL_DEFAULT;
194 if (!S_ISDIR(inode->v.i_mode))
195 return acl ? -EACCES : 0;
203 value = bch2_acl_to_disk(acl, &size);
205 return (int)PTR_ERR(value);
208 ret = bch2_xattr_set(c, inode, "", value, size, 0, name_index);
215 set_cached_acl(&inode->v, type, acl);
220 int bch2_set_acl(struct inode *vinode, struct posix_acl *acl, int type)
222 struct bch_inode_info *inode = to_bch_ei(vinode);
223 struct bch_fs *c = inode->v.i_sb->s_fs_info;
224 umode_t mode = inode->v.i_mode;
227 if (type == ACL_TYPE_ACCESS && acl) {
228 ret = posix_acl_update_mode(&inode->v, &mode, &acl);
233 ret = __bch2_set_acl(vinode, acl, type);
237 if (mode != inode->v.i_mode) {
238 mutex_lock(&inode->ei_update_lock);
239 inode->v.i_mode = mode;
240 inode->v.i_ctime = current_time(&inode->v);
242 ret = bch2_write_inode(c, inode);
243 mutex_unlock(&inode->ei_update_lock);
249 #endif /* CONFIG_BCACHEFS_POSIX_ACL */