1 // SPDX-License-Identifier: GPL-2.0
2 #ifdef CONFIG_BCACHEFS_POSIX_ACL
7 #include <linux/posix_acl.h>
8 #include <linux/posix_acl_xattr.h>
9 #include <linux/sched.h>
10 #include <linux/slab.h>
16 static inline size_t bch2_acl_size(unsigned nr_short, unsigned nr_long)
18 return sizeof(bch_acl_header) +
19 sizeof(bch_acl_entry_short) * nr_short +
20 sizeof(bch_acl_entry) * nr_long;
23 static inline int acl_to_xattr_type(int type)
27 return KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS;
28 case ACL_TYPE_DEFAULT:
29 return KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT;
36 * Convert from filesystem to in-memory representation.
38 static struct posix_acl *bch2_acl_from_disk(const void *value, size_t size)
40 const void *p, *end = value + size;
41 struct posix_acl *acl;
42 struct posix_acl_entry *out;
47 if (size < sizeof(bch_acl_header))
49 if (((bch_acl_header *)value)->a_version !=
50 cpu_to_le32(BCH_ACL_VERSION))
53 p = value + sizeof(bch_acl_header);
55 const bch_acl_entry *entry = p;
57 if (p + sizeof(bch_acl_entry_short) > end)
60 switch (le16_to_cpu(entry->e_tag)) {
65 p += sizeof(bch_acl_entry_short);
69 p += sizeof(bch_acl_entry);
84 acl = posix_acl_alloc(count, GFP_KERNEL);
86 return ERR_PTR(-ENOMEM);
90 p = value + sizeof(bch_acl_header);
92 const bch_acl_entry *in = p;
94 out->e_tag = le16_to_cpu(in->e_tag);
95 out->e_perm = le16_to_cpu(in->e_perm);
102 p += sizeof(bch_acl_entry_short);
105 out->e_uid = make_kuid(&init_user_ns,
106 le32_to_cpu(in->e_id));
107 p += sizeof(bch_acl_entry);
110 out->e_gid = make_kgid(&init_user_ns,
111 le32_to_cpu(in->e_id));
112 p += sizeof(bch_acl_entry);
119 BUG_ON(out != acl->a_entries + acl->a_count);
123 pr_err("invalid acl entry");
124 return ERR_PTR(-EINVAL);
127 #define acl_for_each_entry(acl, acl_e) \
128 for (acl_e = acl->a_entries; \
129 acl_e < acl->a_entries + acl->a_count; \
133 * Convert from in-memory to filesystem representation.
135 static struct bkey_i_xattr *
136 bch2_acl_to_xattr(struct btree_trans *trans,
137 const struct posix_acl *acl,
140 struct bkey_i_xattr *xattr;
141 bch_acl_header *acl_header;
142 const struct posix_acl_entry *acl_e;
144 unsigned nr_short = 0, nr_long = 0, acl_len, u64s;
146 acl_for_each_entry(acl, acl_e) {
147 switch (acl_e->e_tag) {
159 return ERR_PTR(-EINVAL);
163 acl_len = bch2_acl_size(nr_short, nr_long);
164 u64s = BKEY_U64s + xattr_val_u64s(0, acl_len);
167 return ERR_PTR(-E2BIG);
169 xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
173 bkey_xattr_init(&xattr->k_i);
174 xattr->k.u64s = u64s;
175 xattr->v.x_type = acl_to_xattr_type(type);
176 xattr->v.x_name_len = 0,
177 xattr->v.x_val_len = cpu_to_le16(acl_len);
179 acl_header = xattr_val(&xattr->v);
180 acl_header->a_version = cpu_to_le32(BCH_ACL_VERSION);
182 outptr = (void *) acl_header + sizeof(*acl_header);
184 acl_for_each_entry(acl, acl_e) {
185 bch_acl_entry *entry = outptr;
187 entry->e_tag = cpu_to_le16(acl_e->e_tag);
188 entry->e_perm = cpu_to_le16(acl_e->e_perm);
189 switch (acl_e->e_tag) {
191 entry->e_id = cpu_to_le32(
192 from_kuid(&init_user_ns, acl_e->e_uid));
193 outptr += sizeof(bch_acl_entry);
196 entry->e_id = cpu_to_le32(
197 from_kgid(&init_user_ns, acl_e->e_gid));
198 outptr += sizeof(bch_acl_entry);
205 outptr += sizeof(bch_acl_entry_short);
210 BUG_ON(outptr != xattr_val(&xattr->v) + acl_len);
215 struct posix_acl *bch2_get_acl(struct inode *vinode, int type)
217 struct bch_inode_info *inode = to_bch_ei(vinode);
218 struct bch_fs *c = inode->v.i_sb->s_fs_info;
219 struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
220 struct btree_trans trans;
221 struct btree_iter *iter;
222 struct bkey_s_c_xattr xattr;
223 struct posix_acl *acl = NULL;
225 bch2_trans_init(&trans, c, 0, 0);
227 bch2_trans_begin(&trans);
229 iter = bch2_hash_lookup(&trans, bch2_xattr_hash_desc,
230 &hash, inode->v.i_ino,
231 &X_SEARCH(acl_to_xattr_type(type), "", 0),
234 if (PTR_ERR(iter) == -EINTR)
237 if (PTR_ERR(iter) != -ENOENT)
238 acl = ERR_CAST(iter);
242 xattr = bkey_s_c_to_xattr(bch2_btree_iter_peek_slot(iter));
243 acl = bch2_acl_from_disk(xattr_val(xattr.v),
244 le16_to_cpu(xattr.v->x_val_len));
247 set_cached_acl(&inode->v, type, acl);
248 bch2_trans_iter_put(&trans, iter);
250 bch2_trans_exit(&trans);
254 int bch2_set_acl_trans(struct btree_trans *trans,
255 struct bch_inode_unpacked *inode_u,
256 const struct bch_hash_info *hash_info,
257 struct posix_acl *acl, int type)
261 if (type == ACL_TYPE_DEFAULT &&
262 !S_ISDIR(inode_u->bi_mode))
263 return acl ? -EACCES : 0;
266 struct bkey_i_xattr *xattr =
267 bch2_acl_to_xattr(trans, acl, type);
269 return PTR_ERR(xattr);
271 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
272 inode_u->bi_inum, &xattr->k_i, 0);
274 struct xattr_search_key search =
275 X_SEARCH(acl_to_xattr_type(type), "", 0);
277 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc, hash_info,
278 inode_u->bi_inum, &search);
281 return ret == -ENOENT ? 0 : ret;
284 int bch2_set_acl(struct user_namespace *mnt_userns,
285 struct inode *vinode, struct posix_acl *_acl, int type)
287 struct bch_inode_info *inode = to_bch_ei(vinode);
288 struct bch_fs *c = inode->v.i_sb->s_fs_info;
289 struct btree_trans trans;
290 struct btree_iter *inode_iter;
291 struct bch_inode_unpacked inode_u;
292 struct bch_hash_info hash_info;
293 struct posix_acl *acl;
297 mutex_lock(&inode->ei_update_lock);
298 bch2_trans_init(&trans, c, 0, 0);
300 bch2_trans_begin(&trans);
303 inode_iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
305 ret = PTR_ERR_OR_ZERO(inode_iter);
309 mode = inode_u.bi_mode;
311 if (type == ACL_TYPE_ACCESS) {
312 ret = posix_acl_update_mode(mnt_userns, &inode->v, &mode, &acl);
317 hash_info = bch2_hash_info_init(c, &inode_u);
319 ret = bch2_set_acl_trans(&trans, &inode_u, &hash_info, acl, type);
323 inode_u.bi_ctime = bch2_current_time(c);
324 inode_u.bi_mode = mode;
326 ret = bch2_inode_write(&trans, inode_iter, &inode_u) ?:
327 bch2_trans_commit(&trans, NULL,
328 &inode->ei_journal_seq,
329 BTREE_INSERT_NOUNLOCK);
331 bch2_trans_iter_put(&trans, inode_iter);
338 bch2_inode_update_after_write(c, inode, &inode_u,
339 ATTR_CTIME|ATTR_MODE);
341 set_cached_acl(&inode->v, type, acl);
343 bch2_trans_exit(&trans);
344 mutex_unlock(&inode->ei_update_lock);
349 int bch2_acl_chmod(struct btree_trans *trans,
350 struct bch_inode_unpacked *inode,
352 struct posix_acl **new_acl)
354 struct bch_hash_info hash_info = bch2_hash_info_init(trans->c, inode);
355 struct btree_iter *iter;
356 struct bkey_s_c_xattr xattr;
357 struct bkey_i_xattr *new;
358 struct posix_acl *acl;
361 iter = bch2_hash_lookup(trans, bch2_xattr_hash_desc,
362 &hash_info, inode->bi_inum,
363 &X_SEARCH(KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS, "", 0),
365 ret = PTR_ERR_OR_ZERO(iter);
367 return ret == -ENOENT ? 0 : ret;
369 xattr = bkey_s_c_to_xattr(bch2_btree_iter_peek_slot(iter));
370 acl = bch2_acl_from_disk(xattr_val(xattr.v),
371 le16_to_cpu(xattr.v->x_val_len));
372 ret = PTR_ERR_OR_ZERO(acl);
373 if (IS_ERR_OR_NULL(acl))
376 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
380 new = bch2_acl_to_xattr(trans, acl, ACL_TYPE_ACCESS);
386 new->k.p = iter->pos;
387 ret = bch2_trans_update(trans, iter, &new->k_i, 0);
391 bch2_trans_iter_put(trans, iter);
392 if (!IS_ERR_OR_NULL(acl))
397 #endif /* CONFIG_BCACHEFS_POSIX_ACL */