]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/reiserfs/xattr_acl.c
Merge tag 'riscv-for-linus-5.14-mw0' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / fs / reiserfs / xattr_acl.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
16f7e0fe 2#include <linux/capability.h>
1da177e4
LT
3#include <linux/fs.h>
4#include <linux/posix_acl.h>
f466c6fd 5#include "reiserfs.h"
1da177e4
LT
6#include <linux/errno.h>
7#include <linux/pagemap.h>
8#include <linux/xattr.h>
5a0e3ad6 9#include <linux/slab.h>
9a59f452 10#include <linux/posix_acl_xattr.h>
c45ac888 11#include "xattr.h"
a3063ab8 12#include "acl.h"
17093991 13#include <linux/uaccess.h>
1da177e4 14
47f70d08 15static int __reiserfs_set_acl(struct reiserfs_transaction_handle *th,
0ab2621e 16 struct inode *inode, int type,
bd4c625c 17 struct posix_acl *acl);
1da177e4 18
47f70d08
CH
19
20int
549c7297
CB
21reiserfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
22 struct posix_acl *acl, int type)
1da177e4 23{
0ab2621e
JM
24 int error, error2;
25 struct reiserfs_transaction_handle th;
26 size_t jcreate_blocks;
47f70d08 27 int size = acl ? posix_acl_xattr_size(acl->a_count) : 0;
fcea8aed
EF
28 int update_mode = 0;
29 umode_t mode = inode->i_mode;
1da177e4 30
098297b2
JM
31 /*
32 * Pessimism: We can't assume that anything from the xattr root up
33 * has been created.
34 */
0ab2621e
JM
35
36 jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
37 reiserfs_xattr_nblocks(inode, size) * 2;
38
39 reiserfs_write_lock(inode->i_sb);
40 error = journal_begin(&th, inode->i_sb, jcreate_blocks);
4c05141d 41 reiserfs_write_unlock(inode->i_sb);
0ab2621e 42 if (error == 0) {
6883cd7f 43 if (type == ACL_TYPE_ACCESS && acl) {
e65ce2a5
CB
44 error = posix_acl_update_mode(&init_user_ns, inode,
45 &mode, &acl);
6883cd7f
JK
46 if (error)
47 goto unlock;
fcea8aed 48 update_mode = 1;
6883cd7f 49 }
47f70d08 50 error = __reiserfs_set_acl(&th, inode, type, acl);
fcea8aed
EF
51 if (!error && update_mode)
52 inode->i_mode = mode;
6883cd7f 53unlock:
4c05141d 54 reiserfs_write_lock(inode->i_sb);
58d85426 55 error2 = journal_end(&th);
4c05141d 56 reiserfs_write_unlock(inode->i_sb);
0ab2621e
JM
57 if (error2)
58 error = error2;
59 }
1da177e4 60
1da177e4
LT
61 return error;
62}
63
1da177e4
LT
64/*
65 * Convert from filesystem to in-memory representation.
66 */
9dad943a 67static struct posix_acl *reiserfs_posix_acl_from_disk(const void *value, size_t size)
1da177e4
LT
68{
69 const char *end = (char *)value + size;
70 int n, count;
71 struct posix_acl *acl;
72
73 if (!value)
74 return NULL;
75 if (size < sizeof(reiserfs_acl_header))
bd4c625c
LT
76 return ERR_PTR(-EINVAL);
77 if (((reiserfs_acl_header *) value)->a_version !=
1da177e4
LT
78 cpu_to_le32(REISERFS_ACL_VERSION))
79 return ERR_PTR(-EINVAL);
80 value = (char *)value + sizeof(reiserfs_acl_header);
81 count = reiserfs_acl_count(size);
82 if (count < 0)
83 return ERR_PTR(-EINVAL);
84 if (count == 0)
85 return NULL;
86 acl = posix_acl_alloc(count, GFP_NOFS);
87 if (!acl)
88 return ERR_PTR(-ENOMEM);
bd4c625c
LT
89 for (n = 0; n < count; n++) {
90 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
1da177e4
LT
91 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
92 goto fail;
bd4c625c 93 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
1da177e4 94 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
bd4c625c
LT
95 switch (acl->a_entries[n].e_tag) {
96 case ACL_USER_OBJ:
97 case ACL_GROUP_OBJ:
98 case ACL_MASK:
99 case ACL_OTHER:
100 value = (char *)value +
101 sizeof(reiserfs_acl_entry_short);
bd4c625c
LT
102 break;
103
104 case ACL_USER:
df814654
EB
105 value = (char *)value + sizeof(reiserfs_acl_entry);
106 if ((char *)value > end)
107 goto fail;
108 acl->a_entries[n].e_uid =
109 make_kuid(&init_user_ns,
110 le32_to_cpu(entry->e_id));
111 break;
bd4c625c
LT
112 case ACL_GROUP:
113 value = (char *)value + sizeof(reiserfs_acl_entry);
114 if ((char *)value > end)
1da177e4 115 goto fail;
df814654
EB
116 acl->a_entries[n].e_gid =
117 make_kgid(&init_user_ns,
118 le32_to_cpu(entry->e_id));
bd4c625c
LT
119 break;
120
121 default:
122 goto fail;
1da177e4
LT
123 }
124 }
125 if (value != end)
126 goto fail;
127 return acl;
128
cf776a7a 129fail:
1da177e4
LT
130 posix_acl_release(acl);
131 return ERR_PTR(-EINVAL);
132}
133
134/*
135 * Convert from in-memory to filesystem representation.
136 */
9dad943a 137static void *reiserfs_posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
1da177e4
LT
138{
139 reiserfs_acl_header *ext_acl;
140 char *e;
141 int n;
142
143 *size = reiserfs_acl_size(acl->a_count);
5cbded58 144 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
bd4c625c
LT
145 acl->a_count *
146 sizeof(reiserfs_acl_entry),
147 GFP_NOFS);
1da177e4
LT
148 if (!ext_acl)
149 return ERR_PTR(-ENOMEM);
150 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
151 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
bd4c625c 152 for (n = 0; n < acl->a_count; n++) {
df814654 153 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
bd4c625c
LT
154 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
155 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
1da177e4 156 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
bd4c625c
LT
157 switch (acl->a_entries[n].e_tag) {
158 case ACL_USER:
df814654
EB
159 entry->e_id = cpu_to_le32(
160 from_kuid(&init_user_ns, acl_e->e_uid));
161 e += sizeof(reiserfs_acl_entry);
162 break;
bd4c625c 163 case ACL_GROUP:
df814654
EB
164 entry->e_id = cpu_to_le32(
165 from_kgid(&init_user_ns, acl_e->e_gid));
bd4c625c
LT
166 e += sizeof(reiserfs_acl_entry);
167 break;
168
169 case ACL_USER_OBJ:
170 case ACL_GROUP_OBJ:
171 case ACL_MASK:
172 case ACL_OTHER:
173 e += sizeof(reiserfs_acl_entry_short);
174 break;
175
176 default:
177 goto fail;
1da177e4
LT
178 }
179 }
180 return (char *)ext_acl;
181
cf776a7a 182fail:
1da177e4
LT
183 kfree(ext_acl);
184 return ERR_PTR(-EINVAL);
185}
186
187/*
188 * Inode operation get_posix_acl().
189 *
1b1dcc1b 190 * inode->i_mutex: down
1da177e4
LT
191 * BKL held [before 2.5.x]
192 */
bd4c625c 193struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
1da177e4
LT
194{
195 char *name, *value;
073aaa1b 196 struct posix_acl *acl;
3cdc409c 197 int size;
1da177e4 198 int retval;
bd4c625c
LT
199
200 switch (type) {
201 case ACL_TYPE_ACCESS:
97d79299 202 name = XATTR_NAME_POSIX_ACL_ACCESS;
bd4c625c
LT
203 break;
204 case ACL_TYPE_DEFAULT:
97d79299 205 name = XATTR_NAME_POSIX_ACL_DEFAULT;
bd4c625c
LT
206 break;
207 default:
073aaa1b 208 BUG();
bd4c625c
LT
209 }
210
bd4c625c 211 size = reiserfs_xattr_get(inode, name, NULL, 0);
3cdc409c 212 if (size < 0) {
b8a7a3a6 213 if (size == -ENODATA || size == -ENOSYS)
bd4c625c 214 return NULL;
bd4c625c
LT
215 return ERR_PTR(size);
216 }
1da177e4 217
bd4c625c
LT
218 value = kmalloc(size, GFP_NOFS);
219 if (!value)
220 return ERR_PTR(-ENOMEM);
1da177e4
LT
221
222 retval = reiserfs_xattr_get(inode, name, value, size);
223 if (retval == -ENODATA || retval == -ENOSYS) {
098297b2
JM
224 /*
225 * This shouldn't actually happen as it should have
226 * been caught above.. but just in case
227 */
1da177e4 228 acl = NULL;
bd4c625c 229 } else if (retval < 0) {
1da177e4
LT
230 acl = ERR_PTR(retval);
231 } else {
9dad943a 232 acl = reiserfs_posix_acl_from_disk(value, retval);
bd4c625c 233 }
1da177e4
LT
234
235 kfree(value);
236 return acl;
237}
238
239/*
240 * Inode operation set_posix_acl().
241 *
1b1dcc1b 242 * inode->i_mutex: down
1da177e4
LT
243 * BKL held [before 2.5.x]
244 */
245static int
47f70d08 246__reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
0ab2621e 247 int type, struct posix_acl *acl)
1da177e4 248{
bd4c625c 249 char *name;
1da177e4 250 void *value = NULL;
48b32a35 251 size_t size = 0;
1da177e4 252 int error;
1da177e4 253
bd4c625c
LT
254 switch (type) {
255 case ACL_TYPE_ACCESS:
97d79299 256 name = XATTR_NAME_POSIX_ACL_ACCESS;
bd4c625c
LT
257 break;
258 case ACL_TYPE_DEFAULT:
97d79299 259 name = XATTR_NAME_POSIX_ACL_DEFAULT;
bd4c625c
LT
260 if (!S_ISDIR(inode->i_mode))
261 return acl ? -EACCES : 0;
262 break;
263 default:
264 return -EINVAL;
265 }
266
267 if (acl) {
9dad943a 268 value = reiserfs_posix_acl_to_disk(acl, &size);
bd4c625c
LT
269 if (IS_ERR(value))
270 return (int)PTR_ERR(value);
48b32a35
JM
271 }
272
0ab2621e 273 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
48b32a35
JM
274
275 /*
276 * Ensure that the inode gets dirtied if we're only using
277 * the mode bits and an old ACL didn't exist. We don't need
278 * to check if the inode is hashed here since we won't get
279 * called by reiserfs_inherit_default_acl().
280 */
281 if (error == -ENODATA) {
282 error = 0;
283 if (type == ACL_TYPE_ACCESS) {
02027d42 284 inode->i_ctime = current_time(inode);
bd4c625c 285 mark_inode_dirty(inode);
bd4c625c
LT
286 }
287 }
1da177e4 288
833d304b 289 kfree(value);
1da177e4 290
d984561b 291 if (!error)
073aaa1b 292 set_cached_acl(inode, type, acl);
1da177e4
LT
293
294 return error;
295}
296
098297b2
JM
297/*
298 * dir->i_mutex: locked,
299 * inode is new and not released into the wild yet
300 */
1da177e4 301int
0ab2621e
JM
302reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
303 struct inode *dir, struct dentry *dentry,
bd4c625c 304 struct inode *inode)
1da177e4 305{
47f70d08 306 struct posix_acl *default_acl, *acl;
bd4c625c
LT
307 int err = 0;
308
309 /* ACLs only get applied to files and directories */
310 if (S_ISLNK(inode->i_mode))
311 return 0;
312
098297b2
JM
313 /*
314 * ACLs can only be used on "new" objects, so if it's an old object
315 * there is nothing to inherit from
316 */
bd4c625c
LT
317 if (get_inode_sd_version(dir) == STAT_DATA_V1)
318 goto apply_umask;
319
098297b2
JM
320 /*
321 * Don't apply ACLs to objects in the .reiserfs_priv tree.. This
bd4c625c 322 * would be useless since permissions are ignored, and a pain because
098297b2
JM
323 * it introduces locking cycles
324 */
60e4cf67 325 if (IS_PRIVATE(inode))
bd4c625c 326 goto apply_umask;
bd4c625c 327
47f70d08
CH
328 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
329 if (err)
330 return err;
bd4c625c 331
47f70d08
CH
332 if (default_acl) {
333 err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
334 default_acl);
335 posix_acl_release(default_acl);
336 }
bd4c625c 337 if (acl) {
47f70d08
CH
338 if (!err)
339 err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS,
340 acl);
bd4c625c 341 posix_acl_release(acl);
bd4c625c
LT
342 }
343
344 return err;
47f70d08 345
cf776a7a 346apply_umask:
47f70d08
CH
347 /* no ACL, apply umask */
348 inode->i_mode &= ~current_umask();
349 return err;
1da177e4
LT
350}
351
0ab2621e
JM
352/* This is used to cache the default acl before a new object is created.
353 * The biggest reason for this is to get an idea of how many blocks will
354 * actually be required for the create operation if we must inherit an ACL.
355 * An ACL write can add up to 3 object creations and an additional file write
356 * so we'd prefer not to reserve that many blocks in the journal if we can.
357 * It also has the advantage of not loading the ACL with a transaction open,
358 * this may seem silly, but if the owner of the directory is doing the
359 * creation, the ACL may not be loaded since the permissions wouldn't require
360 * it.
361 * We return the number of blocks required for the transaction.
362 */
bd4c625c 363int reiserfs_cache_default_acl(struct inode *inode)
1da177e4 364{
0ab2621e
JM
365 struct posix_acl *acl;
366 int nblocks = 0;
367
368 if (IS_PRIVATE(inode))
369 return 0;
370
7dffc87f 371 acl = get_acl(inode, ACL_TYPE_DEFAULT);
0ab2621e
JM
372
373 if (acl && !IS_ERR(acl)) {
374 int size = reiserfs_acl_size(acl->a_count);
375
376 /* Other xattrs can be created during inode creation. We don't
377 * want to claim too many blocks, so we check to see if we
9436fb4d 378 * need to create the tree to the xattrs, and then we
0ab2621e
JM
379 * just want two files. */
380 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
381 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
382
383 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
384
385 /* We need to account for writes + bitmaps for two files */
386 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
387 posix_acl_release(acl);
bd4c625c
LT
388 }
389
0ab2621e 390 return nblocks;
1da177e4
LT
391}
392
4c05141d
JM
393/*
394 * Called under i_mutex
395 */
bd4c625c 396int reiserfs_acl_chmod(struct inode *inode)
1da177e4 397{
4a857011
JM
398 if (IS_PRIVATE(inode))
399 return 0;
bd4c625c 400 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
47f70d08 401 !reiserfs_posixacl(inode->i_sb))
bd4c625c 402 return 0;
1da177e4 403
e65ce2a5 404 return posix_acl_chmod(&init_user_ns, inode, inode->i_mode);
1da177e4 405}