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