bcachefs: Simplify hash table checks
[linux-block.git] / fs / bcachefs / acl.c
1 // SPDX-License-Identifier: GPL-2.0
2 #ifdef CONFIG_BCACHEFS_POSIX_ACL
3
4 #include "bcachefs.h"
5
6 #include <linux/fs.h>
7 #include <linux/posix_acl.h>
8 #include <linux/posix_acl_xattr.h>
9 #include <linux/sched.h>
10 #include <linux/slab.h>
11
12 #include "acl.h"
13 #include "fs.h"
14 #include "xattr.h"
15
16 static inline size_t bch2_acl_size(unsigned nr_short, unsigned nr_long)
17 {
18         return sizeof(bch_acl_header) +
19                 sizeof(bch_acl_entry_short) * nr_short +
20                 sizeof(bch_acl_entry) * nr_long;
21 }
22
23 static inline int acl_to_xattr_type(int type)
24 {
25         switch (type) {
26         case ACL_TYPE_ACCESS:
27                 return KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS;
28         case ACL_TYPE_DEFAULT:
29                 return KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT;
30         default:
31                 BUG();
32         }
33 }
34
35 /*
36  * Convert from filesystem to in-memory representation.
37  */
38 static struct posix_acl *bch2_acl_from_disk(const void *value, size_t size)
39 {
40         const void *p, *end = value + size;
41         struct posix_acl *acl;
42         struct posix_acl_entry *out;
43         unsigned count = 0;
44
45         if (!value)
46                 return NULL;
47         if (size < sizeof(bch_acl_header))
48                 goto invalid;
49         if (((bch_acl_header *)value)->a_version !=
50             cpu_to_le32(BCH_ACL_VERSION))
51                 goto invalid;
52
53         p = value + sizeof(bch_acl_header);
54         while (p < end) {
55                 const bch_acl_entry *entry = p;
56
57                 if (p + sizeof(bch_acl_entry_short) > end)
58                         goto invalid;
59
60                 switch (le16_to_cpu(entry->e_tag)) {
61                 case ACL_USER_OBJ:
62                 case ACL_GROUP_OBJ:
63                 case ACL_MASK:
64                 case ACL_OTHER:
65                         p += sizeof(bch_acl_entry_short);
66                         break;
67                 case ACL_USER:
68                 case ACL_GROUP:
69                         p += sizeof(bch_acl_entry);
70                         break;
71                 default:
72                         goto invalid;
73                 }
74
75                 count++;
76         }
77
78         if (p > end)
79                 goto invalid;
80
81         if (!count)
82                 return NULL;
83
84         acl = posix_acl_alloc(count, GFP_KERNEL);
85         if (!acl)
86                 return ERR_PTR(-ENOMEM);
87
88         out = acl->a_entries;
89
90         p = value + sizeof(bch_acl_header);
91         while (p < end) {
92                 const bch_acl_entry *in = p;
93
94                 out->e_tag  = le16_to_cpu(in->e_tag);
95                 out->e_perm = le16_to_cpu(in->e_perm);
96
97                 switch (out->e_tag) {
98                 case ACL_USER_OBJ:
99                 case ACL_GROUP_OBJ:
100                 case ACL_MASK:
101                 case ACL_OTHER:
102                         p += sizeof(bch_acl_entry_short);
103                         break;
104                 case ACL_USER:
105                         out->e_uid = make_kuid(&init_user_ns,
106                                                le32_to_cpu(in->e_id));
107                         p += sizeof(bch_acl_entry);
108                         break;
109                 case ACL_GROUP:
110                         out->e_gid = make_kgid(&init_user_ns,
111                                                le32_to_cpu(in->e_id));
112                         p += sizeof(bch_acl_entry);
113                         break;
114                 }
115
116                 out++;
117         }
118
119         BUG_ON(out != acl->a_entries + acl->a_count);
120
121         return acl;
122 invalid:
123         pr_err("invalid acl entry");
124         return ERR_PTR(-EINVAL);
125 }
126
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;     \
130              acl_e++)
131
132 /*
133  * Convert from in-memory to filesystem representation.
134  */
135 static struct bkey_i_xattr *
136 bch2_acl_to_xattr(struct btree_trans *trans,
137                   const struct posix_acl *acl,
138                   int type)
139 {
140         struct bkey_i_xattr *xattr;
141         bch_acl_header *acl_header;
142         const struct posix_acl_entry *acl_e;
143         void *outptr;
144         unsigned nr_short = 0, nr_long = 0, acl_len, u64s;
145
146         acl_for_each_entry(acl, acl_e) {
147                 switch (acl_e->e_tag) {
148                 case ACL_USER:
149                 case ACL_GROUP:
150                         nr_long++;
151                         break;
152                 case ACL_USER_OBJ:
153                 case ACL_GROUP_OBJ:
154                 case ACL_MASK:
155                 case ACL_OTHER:
156                         nr_short++;
157                         break;
158                 default:
159                         return ERR_PTR(-EINVAL);
160                 }
161         }
162
163         acl_len = bch2_acl_size(nr_short, nr_long);
164         u64s = BKEY_U64s + xattr_val_u64s(0, acl_len);
165
166         if (u64s > U8_MAX)
167                 return ERR_PTR(-E2BIG);
168
169         xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
170         if (IS_ERR(xattr))
171                 return xattr;
172
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);
178
179         acl_header = xattr_val(&xattr->v);
180         acl_header->a_version = cpu_to_le32(BCH_ACL_VERSION);
181
182         outptr = (void *) acl_header + sizeof(*acl_header);
183
184         acl_for_each_entry(acl, acl_e) {
185                 bch_acl_entry *entry = outptr;
186
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) {
190                 case ACL_USER:
191                         entry->e_id = cpu_to_le32(
192                                 from_kuid(&init_user_ns, acl_e->e_uid));
193                         outptr += sizeof(bch_acl_entry);
194                         break;
195                 case ACL_GROUP:
196                         entry->e_id = cpu_to_le32(
197                                 from_kgid(&init_user_ns, acl_e->e_gid));
198                         outptr += sizeof(bch_acl_entry);
199                         break;
200
201                 case ACL_USER_OBJ:
202                 case ACL_GROUP_OBJ:
203                 case ACL_MASK:
204                 case ACL_OTHER:
205                         outptr += sizeof(bch_acl_entry_short);
206                         break;
207                 }
208         }
209
210         BUG_ON(outptr != xattr_val(&xattr->v) + acl_len);
211
212         return xattr;
213 }
214
215 struct posix_acl *bch2_get_acl(struct mnt_idmap *idmap,
216                                struct dentry *dentry, int type)
217 {
218         struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
219         struct bch_fs *c = inode->v.i_sb->s_fs_info;
220         struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
221         struct btree_trans trans;
222         struct btree_iter *iter;
223         struct bkey_s_c_xattr xattr;
224         struct posix_acl *acl = NULL;
225
226         bch2_trans_init(&trans, c, 0, 0);
227 retry:
228         bch2_trans_begin(&trans);
229
230         iter = bch2_hash_lookup(&trans, bch2_xattr_hash_desc,
231                         &hash, inode->v.i_ino,
232                         &X_SEARCH(acl_to_xattr_type(type), "", 0),
233                         0);
234         if (IS_ERR(iter)) {
235                 if (PTR_ERR(iter) == -EINTR)
236                         goto retry;
237
238                 if (PTR_ERR(iter) != -ENOENT)
239                         acl = ERR_CAST(iter);
240                 goto out;
241         }
242
243         xattr = bkey_s_c_to_xattr(bch2_btree_iter_peek_slot(iter));
244         acl = bch2_acl_from_disk(xattr_val(xattr.v),
245                         le16_to_cpu(xattr.v->x_val_len));
246
247         if (!IS_ERR(acl))
248                 set_cached_acl(&inode->v, type, acl);
249         bch2_trans_iter_put(&trans, iter);
250 out:
251         bch2_trans_exit(&trans);
252         return acl;
253 }
254
255 int bch2_set_acl_trans(struct btree_trans *trans,
256                        struct bch_inode_unpacked *inode_u,
257                        const struct bch_hash_info *hash_info,
258                        struct posix_acl *acl, int type)
259 {
260         int ret;
261
262         if (type == ACL_TYPE_DEFAULT &&
263             !S_ISDIR(inode_u->bi_mode))
264                 return acl ? -EACCES : 0;
265
266         if (acl) {
267                 struct bkey_i_xattr *xattr =
268                         bch2_acl_to_xattr(trans, acl, type);
269                 if (IS_ERR(xattr))
270                         return PTR_ERR(xattr);
271
272                 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
273                                     inode_u->bi_inum, &xattr->k_i, 0);
274         } else {
275                 struct xattr_search_key search =
276                         X_SEARCH(acl_to_xattr_type(type), "", 0);
277
278                 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc, hash_info,
279                                        inode_u->bi_inum, &search);
280         }
281
282         return ret == -ENOENT ? 0 : ret;
283 }
284
285 int bch2_set_acl(struct mnt_idmap *idmap,
286                  struct dentry *dentry,
287                  struct posix_acl *_acl, int type)
288 {
289         struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
290         struct bch_fs *c = inode->v.i_sb->s_fs_info;
291         struct btree_trans trans;
292         struct btree_iter *inode_iter;
293         struct bch_inode_unpacked inode_u;
294         struct bch_hash_info hash_info;
295         struct posix_acl *acl;
296         umode_t mode;
297         int ret;
298
299         mutex_lock(&inode->ei_update_lock);
300         bch2_trans_init(&trans, c, 0, 0);
301 retry:
302         bch2_trans_begin(&trans);
303         acl = _acl;
304
305         inode_iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
306                                      BTREE_ITER_INTENT);
307         ret = PTR_ERR_OR_ZERO(inode_iter);
308         if (ret)
309                 goto btree_err;
310
311         mode = inode_u.bi_mode;
312
313         if (type == ACL_TYPE_ACCESS) {
314                 ret = posix_acl_update_mode(idmap, &inode->v, &mode, &acl);
315                 if (ret)
316                         goto btree_err;
317         }
318
319         hash_info = bch2_hash_info_init(c, &inode_u);
320
321         ret = bch2_set_acl_trans(&trans, &inode_u, &hash_info, acl, type);
322         if (ret)
323                 goto btree_err;
324
325         inode_u.bi_ctime        = bch2_current_time(c);
326         inode_u.bi_mode         = mode;
327
328         ret =   bch2_inode_write(&trans, inode_iter, &inode_u) ?:
329                 bch2_trans_commit(&trans, NULL,
330                                   &inode->ei_journal_seq,
331                                   BTREE_INSERT_NOUNLOCK);
332 btree_err:
333         bch2_trans_iter_put(&trans, inode_iter);
334
335         if (ret == -EINTR)
336                 goto retry;
337         if (unlikely(ret))
338                 goto err;
339
340         bch2_inode_update_after_write(c, inode, &inode_u,
341                                       ATTR_CTIME|ATTR_MODE);
342
343         set_cached_acl(&inode->v, type, acl);
344 err:
345         bch2_trans_exit(&trans);
346         mutex_unlock(&inode->ei_update_lock);
347
348         return ret;
349 }
350
351 int bch2_acl_chmod(struct btree_trans *trans,
352                    struct bch_inode_unpacked *inode,
353                    umode_t mode,
354                    struct posix_acl **new_acl)
355 {
356         struct bch_hash_info hash_info = bch2_hash_info_init(trans->c, inode);
357         struct btree_iter *iter;
358         struct bkey_s_c_xattr xattr;
359         struct bkey_i_xattr *new;
360         struct posix_acl *acl;
361         int ret;
362
363         iter = bch2_hash_lookup(trans, bch2_xattr_hash_desc,
364                         &hash_info, inode->bi_inum,
365                         &X_SEARCH(KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS, "", 0),
366                         BTREE_ITER_INTENT);
367         ret = PTR_ERR_OR_ZERO(iter);
368         if (ret)
369                 return ret == -ENOENT ? 0 : ret;
370
371         xattr = bkey_s_c_to_xattr(bch2_btree_iter_peek_slot(iter));
372         acl = bch2_acl_from_disk(xattr_val(xattr.v),
373                         le16_to_cpu(xattr.v->x_val_len));
374         ret = PTR_ERR_OR_ZERO(acl);
375         if (ret || !acl)
376                 goto err;
377
378         ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
379         if (ret)
380                 goto err;
381
382         new = bch2_acl_to_xattr(trans, acl, ACL_TYPE_ACCESS);
383         if (IS_ERR(new)) {
384                 ret = PTR_ERR(new);
385                 goto err;
386         }
387
388         new->k.p = iter->pos;
389         bch2_trans_update(trans, iter, &new->k_i, 0);
390         *new_acl = acl;
391         acl = NULL;
392 err:
393         bch2_trans_iter_put(trans, iter);
394         kfree(acl);
395         return ret;
396 }
397
398 #endif /* CONFIG_BCACHEFS_POSIX_ACL */