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 btree_trans trans;
220 struct btree_iter *iter;
221 struct bkey_s_c_xattr xattr;
222 struct posix_acl *acl = NULL;
224 bch2_trans_init(&trans, c, 0, 0);
226 bch2_trans_begin(&trans);
228 iter = bch2_hash_lookup(&trans, bch2_xattr_hash_desc,
229 &inode->ei_str_hash, inode->v.i_ino,
230 &X_SEARCH(acl_to_xattr_type(type), "", 0),
233 if (PTR_ERR(iter) == -EINTR)
236 if (PTR_ERR(iter) != -ENOENT)
237 acl = ERR_CAST(iter);
241 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);
249 bch2_trans_exit(&trans);
253 int bch2_set_acl_trans(struct btree_trans *trans,
254 struct bch_inode_unpacked *inode_u,
255 const struct bch_hash_info *hash_info,
256 struct posix_acl *acl, int type)
260 if (type == ACL_TYPE_DEFAULT &&
261 !S_ISDIR(inode_u->bi_mode))
262 return acl ? -EACCES : 0;
265 struct bkey_i_xattr *xattr =
266 bch2_acl_to_xattr(trans, acl, type);
268 return PTR_ERR(xattr);
270 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
271 inode_u->bi_inum, &xattr->k_i, 0);
273 struct xattr_search_key search =
274 X_SEARCH(acl_to_xattr_type(type), "", 0);
276 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc, hash_info,
277 inode_u->bi_inum, &search);
280 return ret == -ENOENT ? 0 : ret;
283 int bch2_set_acl(struct inode *vinode, struct posix_acl *_acl, int type)
285 struct bch_inode_info *inode = to_bch_ei(vinode);
286 struct bch_fs *c = inode->v.i_sb->s_fs_info;
287 struct btree_trans trans;
288 struct btree_iter *inode_iter;
289 struct bch_inode_unpacked inode_u;
290 struct posix_acl *acl;
294 mutex_lock(&inode->ei_update_lock);
295 bch2_trans_init(&trans, c, 0, 0);
297 bch2_trans_begin(&trans);
300 inode_iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
302 ret = PTR_ERR_OR_ZERO(inode_iter);
306 mode = inode_u.bi_mode;
308 if (type == ACL_TYPE_ACCESS) {
309 ret = posix_acl_update_mode(&inode->v, &mode, &acl);
314 ret = bch2_set_acl_trans(&trans, &inode_u,
320 inode_u.bi_ctime = bch2_current_time(c);
321 inode_u.bi_mode = mode;
323 ret = bch2_inode_write(&trans, inode_iter, &inode_u) ?:
324 bch2_trans_commit(&trans, NULL,
325 &inode->ei_journal_seq,
326 BTREE_INSERT_NOUNLOCK);
333 bch2_inode_update_after_write(c, inode, &inode_u,
334 ATTR_CTIME|ATTR_MODE);
336 set_cached_acl(&inode->v, type, acl);
338 bch2_trans_exit(&trans);
339 mutex_unlock(&inode->ei_update_lock);
344 int bch2_acl_chmod(struct btree_trans *trans,
345 struct bch_inode_info *inode,
347 struct posix_acl **new_acl)
349 struct btree_iter *iter;
350 struct bkey_s_c_xattr xattr;
351 struct bkey_i_xattr *new;
352 struct posix_acl *acl;
355 iter = bch2_hash_lookup(trans, bch2_xattr_hash_desc,
356 &inode->ei_str_hash, inode->v.i_ino,
357 &X_SEARCH(KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS, "", 0),
360 return PTR_ERR(iter) != -ENOENT ? PTR_ERR(iter) : 0;
362 xattr = bkey_s_c_to_xattr(bch2_btree_iter_peek_slot(iter));
364 acl = bch2_acl_from_disk(xattr_val(xattr.v),
365 le16_to_cpu(xattr.v->x_val_len));
366 if (IS_ERR_OR_NULL(acl))
369 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
373 new = bch2_acl_to_xattr(trans, acl, ACL_TYPE_ACCESS);
379 new->k.p = iter->pos;
380 bch2_trans_update(trans, iter, &new->k_i, 0);
388 #endif /* CONFIG_BCACHEFS_POSIX_ACL */