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, bool rcu)
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 = { NULL };
222 struct bkey_s_c_xattr xattr;
223 struct posix_acl *acl = NULL;
228 return ERR_PTR(-ECHILD);
230 bch2_trans_init(&trans, c, 0, 0);
232 bch2_trans_begin(&trans);
234 ret = bch2_hash_lookup(&trans, &iter, bch2_xattr_hash_desc,
235 &hash, inode_inum(inode),
236 &X_SEARCH(acl_to_xattr_type(type), "", 0),
239 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
246 k = bch2_btree_iter_peek_slot(&iter);
253 xattr = bkey_s_c_to_xattr(k);
254 acl = bch2_acl_from_disk(xattr_val(xattr.v),
255 le16_to_cpu(xattr.v->x_val_len));
258 set_cached_acl(&inode->v, type, acl);
260 bch2_trans_iter_exit(&trans, &iter);
261 bch2_trans_exit(&trans);
265 int bch2_set_acl_trans(struct btree_trans *trans, subvol_inum inum,
266 struct bch_inode_unpacked *inode_u,
267 struct posix_acl *acl, int type)
269 struct bch_hash_info hash_info = bch2_hash_info_init(trans->c, inode_u);
272 if (type == ACL_TYPE_DEFAULT &&
273 !S_ISDIR(inode_u->bi_mode))
274 return acl ? -EACCES : 0;
277 struct bkey_i_xattr *xattr =
278 bch2_acl_to_xattr(trans, acl, type);
280 return PTR_ERR(xattr);
282 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, &hash_info,
283 inum, &xattr->k_i, 0);
285 struct xattr_search_key search =
286 X_SEARCH(acl_to_xattr_type(type), "", 0);
288 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc, &hash_info,
292 return ret == -ENOENT ? 0 : ret;
295 int bch2_set_acl(struct user_namespace *mnt_userns,
296 struct inode *vinode, struct posix_acl *_acl, int type)
298 struct bch_inode_info *inode = to_bch_ei(vinode);
299 struct bch_fs *c = inode->v.i_sb->s_fs_info;
300 struct btree_trans trans;
301 struct btree_iter inode_iter = { NULL };
302 struct bch_inode_unpacked inode_u;
303 struct posix_acl *acl;
307 mutex_lock(&inode->ei_update_lock);
308 bch2_trans_init(&trans, c, 0, 0);
310 bch2_trans_begin(&trans);
313 ret = bch2_inode_peek(&trans, &inode_iter, &inode_u, inode_inum(inode),
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 ret = bch2_set_acl_trans(&trans, inode_inum(inode), &inode_u, acl, type);
330 inode_u.bi_ctime = bch2_current_time(c);
331 inode_u.bi_mode = mode;
333 ret = bch2_inode_write(&trans, &inode_iter, &inode_u) ?:
334 bch2_trans_commit(&trans, NULL, NULL, 0);
336 bch2_trans_iter_exit(&trans, &inode_iter);
338 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
343 bch2_inode_update_after_write(&trans, inode, &inode_u,
344 ATTR_CTIME|ATTR_MODE);
346 set_cached_acl(&inode->v, type, acl);
348 bch2_trans_exit(&trans);
349 mutex_unlock(&inode->ei_update_lock);
354 int bch2_acl_chmod(struct btree_trans *trans, subvol_inum inum,
355 struct bch_inode_unpacked *inode,
357 struct posix_acl **new_acl)
359 struct bch_hash_info hash_info = bch2_hash_info_init(trans->c, inode);
360 struct btree_iter iter;
361 struct bkey_s_c_xattr xattr;
362 struct bkey_i_xattr *new;
363 struct posix_acl *acl;
367 ret = bch2_hash_lookup(trans, &iter, bch2_xattr_hash_desc,
369 &X_SEARCH(KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS, "", 0),
372 return ret == -ENOENT ? 0 : ret;
374 k = bch2_btree_iter_peek_slot(&iter);
375 xattr = bkey_s_c_to_xattr(k);
379 acl = bch2_acl_from_disk(xattr_val(xattr.v),
380 le16_to_cpu(xattr.v->x_val_len));
381 ret = PTR_ERR_OR_ZERO(acl);
382 if (IS_ERR_OR_NULL(acl))
385 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
389 new = bch2_acl_to_xattr(trans, acl, ACL_TYPE_ACCESS);
396 ret = bch2_trans_update(trans, &iter, &new->k_i, 0);
400 bch2_trans_iter_exit(trans, &iter);
401 if (!IS_ERR_OR_NULL(acl))
406 #endif /* CONFIG_BCACHEFS_POSIX_ACL */