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