]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/acl.c
New upstream snapshot
[bcachefs-tools-debian] / libbcachefs / 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 inode *vinode, int type)
216 {
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;
224
225         bch2_trans_init(&trans, c, 0, 0);
226 retry:
227         bch2_trans_begin(&trans);
228
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),
232                         0);
233         if (IS_ERR(iter)) {
234                 if (PTR_ERR(iter) == -EINTR)
235                         goto retry;
236
237                 if (PTR_ERR(iter) != -ENOENT)
238                         acl = ERR_CAST(iter);
239                 goto out;
240         }
241
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));
245
246         if (!IS_ERR(acl))
247                 set_cached_acl(&inode->v, type, acl);
248         bch2_trans_iter_put(&trans, iter);
249 out:
250         bch2_trans_exit(&trans);
251         return acl;
252 }
253
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)
258 {
259         int ret;
260
261         if (type == ACL_TYPE_DEFAULT &&
262             !S_ISDIR(inode_u->bi_mode))
263                 return acl ? -EACCES : 0;
264
265         if (acl) {
266                 struct bkey_i_xattr *xattr =
267                         bch2_acl_to_xattr(trans, acl, type);
268                 if (IS_ERR(xattr))
269                         return PTR_ERR(xattr);
270
271                 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
272                                     inode_u->bi_inum, &xattr->k_i, 0);
273         } else {
274                 struct xattr_search_key search =
275                         X_SEARCH(acl_to_xattr_type(type), "", 0);
276
277                 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc, hash_info,
278                                        inode_u->bi_inum, &search);
279         }
280
281         return ret == -ENOENT ? 0 : ret;
282 }
283
284 int bch2_set_acl(struct inode *vinode, struct posix_acl *_acl, int type)
285 {
286         struct bch_inode_info *inode = to_bch_ei(vinode);
287         struct bch_fs *c = inode->v.i_sb->s_fs_info;
288         struct btree_trans trans;
289         struct btree_iter *inode_iter;
290         struct bch_inode_unpacked inode_u;
291         struct bch_hash_info hash_info;
292         struct posix_acl *acl;
293         umode_t mode;
294         int ret;
295
296         mutex_lock(&inode->ei_update_lock);
297         bch2_trans_init(&trans, c, 0, 0);
298 retry:
299         bch2_trans_begin(&trans);
300         acl = _acl;
301
302         inode_iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
303                                      BTREE_ITER_INTENT);
304         ret = PTR_ERR_OR_ZERO(inode_iter);
305         if (ret)
306                 goto btree_err;
307
308         mode = inode_u.bi_mode;
309
310         if (type == ACL_TYPE_ACCESS) {
311                 ret = posix_acl_update_mode(&inode->v, &mode, &acl);
312                 if (ret)
313                         goto btree_err;
314         }
315
316         hash_info = bch2_hash_info_init(c, &inode_u);
317
318         ret = bch2_set_acl_trans(&trans, &inode_u, &hash_info, acl, type);
319         if (ret)
320                 goto btree_err;
321
322         inode_u.bi_ctime        = bch2_current_time(c);
323         inode_u.bi_mode         = mode;
324
325         ret =   bch2_inode_write(&trans, inode_iter, &inode_u) ?:
326                 bch2_trans_commit(&trans, NULL,
327                                   &inode->ei_journal_seq,
328                                   BTREE_INSERT_NOUNLOCK);
329 btree_err:
330         bch2_trans_iter_put(&trans, inode_iter);
331
332         if (ret == -EINTR)
333                 goto retry;
334         if (unlikely(ret))
335                 goto err;
336
337         bch2_inode_update_after_write(c, inode, &inode_u,
338                                       ATTR_CTIME|ATTR_MODE);
339
340         set_cached_acl(&inode->v, type, acl);
341 err:
342         bch2_trans_exit(&trans);
343         mutex_unlock(&inode->ei_update_lock);
344
345         return ret;
346 }
347
348 int bch2_acl_chmod(struct btree_trans *trans,
349                    struct bch_inode_unpacked *inode,
350                    umode_t mode,
351                    struct posix_acl **new_acl)
352 {
353         struct bch_hash_info hash_info = bch2_hash_info_init(trans->c, inode);
354         struct btree_iter *iter;
355         struct bkey_s_c_xattr xattr;
356         struct bkey_i_xattr *new;
357         struct posix_acl *acl;
358         int ret;
359
360         iter = bch2_hash_lookup(trans, bch2_xattr_hash_desc,
361                         &hash_info, inode->bi_inum,
362                         &X_SEARCH(KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS, "", 0),
363                         BTREE_ITER_INTENT);
364         ret = PTR_ERR_OR_ZERO(iter);
365         if (ret)
366                 return ret == -ENOENT ? 0 : ret;
367
368         xattr = bkey_s_c_to_xattr(bch2_btree_iter_peek_slot(iter));
369         acl = bch2_acl_from_disk(xattr_val(xattr.v),
370                         le16_to_cpu(xattr.v->x_val_len));
371         ret = PTR_ERR_OR_ZERO(acl);
372         if (ret || !acl)
373                 goto err;
374
375         ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
376         if (ret)
377                 goto err;
378
379         new = bch2_acl_to_xattr(trans, acl, ACL_TYPE_ACCESS);
380         if (IS_ERR(new)) {
381                 ret = PTR_ERR(new);
382                 goto err;
383         }
384
385         new->k.p = iter->pos;
386         bch2_trans_update(trans, iter, &new->k_i, 0);
387         *new_acl = acl;
388         acl = NULL;
389 err:
390         bch2_trans_iter_put(trans, iter);
391         kfree(acl);
392         return ret;
393 }
394
395 #endif /* CONFIG_BCACHEFS_POSIX_ACL */