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