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;
227 bch2_trans_init(&trans, c, 0, 0);
229 bch2_trans_begin(&trans);
231 iter = bch2_hash_lookup(&trans, bch2_xattr_hash_desc,
232 &hash, inode->v.i_ino,
233 &X_SEARCH(acl_to_xattr_type(type), "", 0),
236 if (PTR_ERR(iter) == -EINTR)
239 if (PTR_ERR(iter) != -ENOENT)
240 acl = ERR_CAST(iter);
244 k = bch2_btree_iter_peek_slot(iter);
251 xattr = bkey_s_c_to_xattr(k);
252 acl = bch2_acl_from_disk(xattr_val(xattr.v),
253 le16_to_cpu(xattr.v->x_val_len));
256 set_cached_acl(&inode->v, type, acl);
257 bch2_trans_iter_put(&trans, iter);
259 bch2_trans_exit(&trans);
263 int bch2_set_acl_trans(struct btree_trans *trans,
264 struct bch_inode_unpacked *inode_u,
265 const struct bch_hash_info *hash_info,
266 struct posix_acl *acl, int type)
270 if (type == ACL_TYPE_DEFAULT &&
271 !S_ISDIR(inode_u->bi_mode))
272 return acl ? -EACCES : 0;
275 struct bkey_i_xattr *xattr =
276 bch2_acl_to_xattr(trans, acl, type);
278 return PTR_ERR(xattr);
280 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
281 inode_u->bi_inum, &xattr->k_i, 0);
283 struct xattr_search_key search =
284 X_SEARCH(acl_to_xattr_type(type), "", 0);
286 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc, hash_info,
287 inode_u->bi_inum, &search);
290 return ret == -ENOENT ? 0 : ret;
293 int bch2_set_acl(struct user_namespace *mnt_userns,
294 struct inode *vinode, struct posix_acl *_acl, int type)
296 struct bch_inode_info *inode = to_bch_ei(vinode);
297 struct bch_fs *c = inode->v.i_sb->s_fs_info;
298 struct btree_trans trans;
299 struct btree_iter *inode_iter;
300 struct bch_inode_unpacked inode_u;
301 struct bch_hash_info hash_info;
302 struct posix_acl *acl;
306 mutex_lock(&inode->ei_update_lock);
307 bch2_trans_init(&trans, c, 0, 0);
309 bch2_trans_begin(&trans);
312 inode_iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
314 ret = PTR_ERR_OR_ZERO(inode_iter);
318 mode = inode_u.bi_mode;
320 if (type == ACL_TYPE_ACCESS) {
321 ret = posix_acl_update_mode(mnt_userns, &inode->v, &mode, &acl);
326 hash_info = bch2_hash_info_init(c, &inode_u);
328 ret = bch2_set_acl_trans(&trans, &inode_u, &hash_info, acl, type);
332 inode_u.bi_ctime = bch2_current_time(c);
333 inode_u.bi_mode = mode;
335 ret = bch2_inode_write(&trans, inode_iter, &inode_u) ?:
336 bch2_trans_commit(&trans, NULL,
337 &inode->ei_journal_seq, 0);
339 bch2_trans_iter_put(&trans, inode_iter);
346 bch2_inode_update_after_write(c, inode, &inode_u,
347 ATTR_CTIME|ATTR_MODE);
349 set_cached_acl(&inode->v, type, acl);
351 bch2_trans_exit(&trans);
352 mutex_unlock(&inode->ei_update_lock);
357 int bch2_acl_chmod(struct btree_trans *trans,
358 struct bch_inode_unpacked *inode,
360 struct posix_acl **new_acl)
362 struct bch_hash_info hash_info = bch2_hash_info_init(trans->c, inode);
363 struct btree_iter *iter;
364 struct bkey_s_c_xattr xattr;
365 struct bkey_i_xattr *new;
366 struct posix_acl *acl;
370 iter = bch2_hash_lookup(trans, bch2_xattr_hash_desc,
371 &hash_info, inode->bi_inum,
372 &X_SEARCH(KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS, "", 0),
374 ret = PTR_ERR_OR_ZERO(iter);
376 return ret == -ENOENT ? 0 : ret;
378 k = bch2_btree_iter_peek_slot(iter);
379 xattr = bkey_s_c_to_xattr(k);
383 acl = bch2_acl_from_disk(xattr_val(xattr.v),
384 le16_to_cpu(xattr.v->x_val_len));
385 ret = PTR_ERR_OR_ZERO(acl);
386 if (IS_ERR_OR_NULL(acl))
389 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
393 new = bch2_acl_to_xattr(trans, acl, ACL_TYPE_ACCESS);
399 new->k.p = iter->pos;
400 ret = bch2_trans_update(trans, iter, &new->k_i, 0);
404 bch2_trans_iter_put(trans, iter);
405 if (!IS_ERR_OR_NULL(acl))
410 #endif /* CONFIG_BCACHEFS_POSIX_ACL */