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