import PULS_20180308
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / reiserfs / xattr_acl.c
CommitLineData
16f7e0fe 1#include <linux/capability.h>
1da177e4
LT
2#include <linux/fs.h>
3#include <linux/posix_acl.h>
f466c6fd 4#include "reiserfs.h"
1da177e4
LT
5#include <linux/errno.h>
6#include <linux/pagemap.h>
7#include <linux/xattr.h>
5a0e3ad6 8#include <linux/slab.h>
9a59f452 9#include <linux/posix_acl_xattr.h>
c45ac888 10#include "xattr.h"
a3063ab8 11#include "acl.h"
1da177e4
LT
12#include <asm/uaccess.h>
13
0ab2621e
JM
14static int reiserfs_set_acl(struct reiserfs_transaction_handle *th,
15 struct inode *inode, int type,
bd4c625c 16 struct posix_acl *acl);
1da177e4
LT
17
18static int
431547b3
CH
19posix_acl_set(struct dentry *dentry, const char *name, const void *value,
20 size_t size, int flags, int type)
1da177e4 21{
431547b3 22 struct inode *inode = dentry->d_inode;
1da177e4 23 struct posix_acl *acl;
0ab2621e
JM
24 int error, error2;
25 struct reiserfs_transaction_handle th;
26 size_t jcreate_blocks;
1da177e4
LT
27 if (!reiserfs_posixacl(inode->i_sb))
28 return -EOPNOTSUPP;
2e149670 29 if (!inode_owner_or_capable(inode))
1da177e4
LT
30 return -EPERM;
31
32 if (value) {
5f3a4a28 33 acl = posix_acl_from_xattr(&init_user_ns, value, size);
1da177e4
LT
34 if (IS_ERR(acl)) {
35 return PTR_ERR(acl);
36 } else if (acl) {
37 error = posix_acl_valid(acl);
38 if (error)
39 goto release_and_out;
40 }
41 } else
42 acl = NULL;
43
0ab2621e
JM
44 /* Pessimism: We can't assume that anything from the xattr root up
45 * has been created. */
46
47 jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
48 reiserfs_xattr_nblocks(inode, size) * 2;
49
50 reiserfs_write_lock(inode->i_sb);
51 error = journal_begin(&th, inode->i_sb, jcreate_blocks);
52 if (error == 0) {
53 error = reiserfs_set_acl(&th, inode, type, acl);
54 error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
55 if (error2)
56 error = error2;
57 }
58 reiserfs_write_unlock(inode->i_sb);
1da177e4 59
bd4c625c 60 release_and_out:
1da177e4
LT
61 posix_acl_release(acl);
62 return error;
63}
64
1da177e4 65static int
431547b3
CH
66posix_acl_get(struct dentry *dentry, const char *name, void *buffer,
67 size_t size, int type)
1da177e4
LT
68{
69 struct posix_acl *acl;
70 int error;
71
431547b3 72 if (!reiserfs_posixacl(dentry->d_sb))
1da177e4
LT
73 return -EOPNOTSUPP;
74
431547b3 75 acl = reiserfs_get_acl(dentry->d_inode, type);
1da177e4
LT
76 if (IS_ERR(acl))
77 return PTR_ERR(acl);
78 if (acl == NULL)
79 return -ENODATA;
5f3a4a28 80 error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
1da177e4
LT
81 posix_acl_release(acl);
82
83 return error;
84}
85
1da177e4
LT
86/*
87 * Convert from filesystem to in-memory representation.
88 */
bd4c625c 89static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
1da177e4
LT
90{
91 const char *end = (char *)value + size;
92 int n, count;
93 struct posix_acl *acl;
94
95 if (!value)
96 return NULL;
97 if (size < sizeof(reiserfs_acl_header))
bd4c625c
LT
98 return ERR_PTR(-EINVAL);
99 if (((reiserfs_acl_header *) value)->a_version !=
1da177e4
LT
100 cpu_to_le32(REISERFS_ACL_VERSION))
101 return ERR_PTR(-EINVAL);
102 value = (char *)value + sizeof(reiserfs_acl_header);
103 count = reiserfs_acl_count(size);
104 if (count < 0)
105 return ERR_PTR(-EINVAL);
106 if (count == 0)
107 return NULL;
108 acl = posix_acl_alloc(count, GFP_NOFS);
109 if (!acl)
110 return ERR_PTR(-ENOMEM);
bd4c625c
LT
111 for (n = 0; n < count; n++) {
112 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
1da177e4
LT
113 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
114 goto fail;
bd4c625c 115 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
1da177e4 116 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
bd4c625c
LT
117 switch (acl->a_entries[n].e_tag) {
118 case ACL_USER_OBJ:
119 case ACL_GROUP_OBJ:
120 case ACL_MASK:
121 case ACL_OTHER:
122 value = (char *)value +
123 sizeof(reiserfs_acl_entry_short);
bd4c625c
LT
124 break;
125
126 case ACL_USER:
df814654
EB
127 value = (char *)value + sizeof(reiserfs_acl_entry);
128 if ((char *)value > end)
129 goto fail;
130 acl->a_entries[n].e_uid =
131 make_kuid(&init_user_ns,
132 le32_to_cpu(entry->e_id));
133 break;
bd4c625c
LT
134 case ACL_GROUP:
135 value = (char *)value + sizeof(reiserfs_acl_entry);
136 if ((char *)value > end)
1da177e4 137 goto fail;
df814654
EB
138 acl->a_entries[n].e_gid =
139 make_kgid(&init_user_ns,
140 le32_to_cpu(entry->e_id));
bd4c625c
LT
141 break;
142
143 default:
144 goto fail;
1da177e4
LT
145 }
146 }
147 if (value != end)
148 goto fail;
149 return acl;
150
bd4c625c 151 fail:
1da177e4
LT
152 posix_acl_release(acl);
153 return ERR_PTR(-EINVAL);
154}
155
156/*
157 * Convert from in-memory to filesystem representation.
158 */
bd4c625c 159static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
1da177e4
LT
160{
161 reiserfs_acl_header *ext_acl;
162 char *e;
163 int n;
164
165 *size = reiserfs_acl_size(acl->a_count);
5cbded58 166 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
bd4c625c
LT
167 acl->a_count *
168 sizeof(reiserfs_acl_entry),
169 GFP_NOFS);
1da177e4
LT
170 if (!ext_acl)
171 return ERR_PTR(-ENOMEM);
172 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
173 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
bd4c625c 174 for (n = 0; n < acl->a_count; n++) {
df814654 175 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
bd4c625c
LT
176 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
177 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
1da177e4 178 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
bd4c625c
LT
179 switch (acl->a_entries[n].e_tag) {
180 case ACL_USER:
df814654
EB
181 entry->e_id = cpu_to_le32(
182 from_kuid(&init_user_ns, acl_e->e_uid));
183 e += sizeof(reiserfs_acl_entry);
184 break;
bd4c625c 185 case ACL_GROUP:
df814654
EB
186 entry->e_id = cpu_to_le32(
187 from_kgid(&init_user_ns, acl_e->e_gid));
bd4c625c
LT
188 e += sizeof(reiserfs_acl_entry);
189 break;
190
191 case ACL_USER_OBJ:
192 case ACL_GROUP_OBJ:
193 case ACL_MASK:
194 case ACL_OTHER:
195 e += sizeof(reiserfs_acl_entry_short);
196 break;
197
198 default:
199 goto fail;
1da177e4
LT
200 }
201 }
202 return (char *)ext_acl;
203
bd4c625c 204 fail:
1da177e4
LT
205 kfree(ext_acl);
206 return ERR_PTR(-EINVAL);
207}
208
209/*
210 * Inode operation get_posix_acl().
211 *
1b1dcc1b 212 * inode->i_mutex: down
1da177e4
LT
213 * BKL held [before 2.5.x]
214 */
bd4c625c 215struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
1da177e4
LT
216{
217 char *name, *value;
073aaa1b 218 struct posix_acl *acl;
3cdc409c 219 int size;
1da177e4 220 int retval;
bd4c625c 221
073aaa1b
AV
222 acl = get_cached_acl(inode, type);
223 if (acl != ACL_NOT_CACHED)
224 return acl;
225
bd4c625c
LT
226 switch (type) {
227 case ACL_TYPE_ACCESS:
228 name = POSIX_ACL_XATTR_ACCESS;
bd4c625c
LT
229 break;
230 case ACL_TYPE_DEFAULT:
231 name = POSIX_ACL_XATTR_DEFAULT;
bd4c625c
LT
232 break;
233 default:
073aaa1b 234 BUG();
bd4c625c
LT
235 }
236
bd4c625c 237 size = reiserfs_xattr_get(inode, name, NULL, 0);
3cdc409c 238 if (size < 0) {
bd4c625c 239 if (size == -ENODATA || size == -ENOSYS) {
073aaa1b 240 set_cached_acl(inode, type, NULL);
bd4c625c
LT
241 return NULL;
242 }
243 return ERR_PTR(size);
244 }
1da177e4 245
bd4c625c
LT
246 value = kmalloc(size, GFP_NOFS);
247 if (!value)
248 return ERR_PTR(-ENOMEM);
1da177e4
LT
249
250 retval = reiserfs_xattr_get(inode, name, value, size);
251 if (retval == -ENODATA || retval == -ENOSYS) {
252 /* This shouldn't actually happen as it should have
253 been caught above.. but just in case */
254 acl = NULL;
bd4c625c 255 } else if (retval < 0) {
1da177e4
LT
256 acl = ERR_PTR(retval);
257 } else {
258 acl = posix_acl_from_disk(value, retval);
bd4c625c 259 }
073aaa1b
AV
260 if (!IS_ERR(acl))
261 set_cached_acl(inode, type, acl);
1da177e4
LT
262
263 kfree(value);
264 return acl;
265}
266
267/*
268 * Inode operation set_posix_acl().
269 *
1b1dcc1b 270 * inode->i_mutex: down
1da177e4
LT
271 * BKL held [before 2.5.x]
272 */
273static int
0ab2621e
JM
274reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
275 int type, struct posix_acl *acl)
1da177e4 276{
bd4c625c 277 char *name;
1da177e4 278 void *value = NULL;
48b32a35 279 size_t size = 0;
1da177e4 280 int error;
1da177e4
LT
281
282 if (S_ISLNK(inode->i_mode))
283 return -EOPNOTSUPP;
284
bd4c625c
LT
285 switch (type) {
286 case ACL_TYPE_ACCESS:
287 name = POSIX_ACL_XATTR_ACCESS;
bd4c625c 288 if (acl) {
4b9e9796
S
289 error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
290 if (error)
bd4c625c 291 return error;
bd4c625c
LT
292 }
293 break;
294 case ACL_TYPE_DEFAULT:
295 name = POSIX_ACL_XATTR_DEFAULT;
bd4c625c
LT
296 if (!S_ISDIR(inode->i_mode))
297 return acl ? -EACCES : 0;
298 break;
299 default:
300 return -EINVAL;
301 }
302
303 if (acl) {
304 value = posix_acl_to_disk(acl, &size);
305 if (IS_ERR(value))
306 return (int)PTR_ERR(value);
48b32a35
JM
307 }
308
0ab2621e 309 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
48b32a35
JM
310
311 /*
312 * Ensure that the inode gets dirtied if we're only using
313 * the mode bits and an old ACL didn't exist. We don't need
314 * to check if the inode is hashed here since we won't get
315 * called by reiserfs_inherit_default_acl().
316 */
317 if (error == -ENODATA) {
318 error = 0;
319 if (type == ACL_TYPE_ACCESS) {
320 inode->i_ctime = CURRENT_TIME_SEC;
bd4c625c 321 mark_inode_dirty(inode);
bd4c625c
LT
322 }
323 }
1da177e4 324
833d304b 325 kfree(value);
1da177e4 326
d984561b 327 if (!error)
073aaa1b 328 set_cached_acl(inode, type, acl);
1da177e4
LT
329
330 return error;
331}
332
1b1dcc1b 333/* dir->i_mutex: locked,
1da177e4
LT
334 * inode is new and not released into the wild yet */
335int
0ab2621e
JM
336reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
337 struct inode *dir, struct dentry *dentry,
bd4c625c 338 struct inode *inode)
1da177e4 339{
bd4c625c
LT
340 struct posix_acl *acl;
341 int err = 0;
342
343 /* ACLs only get applied to files and directories */
344 if (S_ISLNK(inode->i_mode))
345 return 0;
346
347 /* ACLs can only be used on "new" objects, so if it's an old object
348 * there is nothing to inherit from */
349 if (get_inode_sd_version(dir) == STAT_DATA_V1)
350 goto apply_umask;
351
352 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
353 * would be useless since permissions are ignored, and a pain because
354 * it introduces locking cycles */
6dfede69
JM
355 if (IS_PRIVATE(dir)) {
356 inode->i_flags |= S_PRIVATE;
bd4c625c
LT
357 goto apply_umask;
358 }
359
360 acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
7a77b15d 361 if (IS_ERR(acl))
bd4c625c 362 return PTR_ERR(acl);
bd4c625c
LT
363
364 if (acl) {
bd4c625c
LT
365 /* Copy the default ACL to the default ACL of a new directory */
366 if (S_ISDIR(inode->i_mode)) {
0ab2621e
JM
367 err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
368 acl);
bd4c625c
LT
369 if (err)
370 goto cleanup;
371 }
372
373 /* Now we reconcile the new ACL and the mode,
374 potentially modifying both */
d3fb6120 375 err = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
826cae2f
AV
376 if (err < 0)
377 return err;
bd4c625c 378
826cae2f
AV
379 /* If we need an ACL.. */
380 if (err > 0)
381 err = reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS, acl);
bd4c625c
LT
382 cleanup:
383 posix_acl_release(acl);
384 } else {
385 apply_umask:
386 /* no ACL, apply umask */
ce3b0f8d 387 inode->i_mode &= ~current_umask();
bd4c625c
LT
388 }
389
390 return err;
1da177e4
LT
391}
392
0ab2621e
JM
393/* This is used to cache the default acl before a new object is created.
394 * The biggest reason for this is to get an idea of how many blocks will
395 * actually be required for the create operation if we must inherit an ACL.
396 * An ACL write can add up to 3 object creations and an additional file write
397 * so we'd prefer not to reserve that many blocks in the journal if we can.
398 * It also has the advantage of not loading the ACL with a transaction open,
399 * this may seem silly, but if the owner of the directory is doing the
400 * creation, the ACL may not be loaded since the permissions wouldn't require
401 * it.
402 * We return the number of blocks required for the transaction.
403 */
bd4c625c 404int reiserfs_cache_default_acl(struct inode *inode)
1da177e4 405{
0ab2621e
JM
406 struct posix_acl *acl;
407 int nblocks = 0;
408
409 if (IS_PRIVATE(inode))
410 return 0;
411
412 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
413
414 if (acl && !IS_ERR(acl)) {
415 int size = reiserfs_acl_size(acl->a_count);
416
417 /* Other xattrs can be created during inode creation. We don't
418 * want to claim too many blocks, so we check to see if we
419 * we need to create the tree to the xattrs, and then we
420 * just want two files. */
421 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
422 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
423
424 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
425
426 /* We need to account for writes + bitmaps for two files */
427 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
428 posix_acl_release(acl);
bd4c625c
LT
429 }
430
0ab2621e 431 return nblocks;
1da177e4
LT
432}
433
bd4c625c 434int reiserfs_acl_chmod(struct inode *inode)
1da177e4 435{
bc26ab5f
AV
436 struct reiserfs_transaction_handle th;
437 struct posix_acl *acl;
438 size_t size;
439 int depth;
bd4c625c 440 int error;
1da177e4 441
4a857011
JM
442 if (IS_PRIVATE(inode))
443 return 0;
444
bd4c625c
LT
445 if (S_ISLNK(inode->i_mode))
446 return -EOPNOTSUPP;
1da177e4 447
bd4c625c
LT
448 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
449 !reiserfs_posixacl(inode->i_sb)) {
450 return 0;
1da177e4
LT
451 }
452
6c287054 453 reiserfs_write_unlock(inode->i_sb);
bd4c625c 454 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
6c287054 455 reiserfs_write_lock(inode->i_sb);
bd4c625c
LT
456 if (!acl)
457 return 0;
458 if (IS_ERR(acl))
459 return PTR_ERR(acl);
bc26ab5f
AV
460 error = posix_acl_chmod(&acl, GFP_NOFS, inode->i_mode);
461 if (error)
462 return error;
463
464 size = reiserfs_xattr_nblocks(inode, reiserfs_acl_size(acl->a_count));
465 depth = reiserfs_write_lock_once(inode->i_sb);
466 error = journal_begin(&th, inode->i_sb, size * 2);
0ab2621e 467 if (!error) {
bc26ab5f
AV
468 int error2;
469 error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS, acl);
470 error2 = journal_end(&th, inode->i_sb, size * 2);
471 if (error2)
472 error = error2;
0ab2621e 473 }
bc26ab5f
AV
474 reiserfs_write_unlock_once(inode->i_sb, depth);
475 posix_acl_release(acl);
bd4c625c 476 return error;
1da177e4
LT
477}
478
431547b3 479static size_t posix_acl_access_list(struct dentry *dentry, char *list,
48b32a35 480 size_t list_size, const char *name,
431547b3 481 size_t name_len, int type)
1da177e4 482{
48b32a35 483 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
431547b3 484 if (!reiserfs_posixacl(dentry->d_sb))
bd4c625c 485 return 0;
48b32a35
JM
486 if (list && size <= list_size)
487 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
488 return size;
1da177e4
LT
489}
490
94d09a98 491const struct xattr_handler reiserfs_posix_acl_access_handler = {
9a59f452 492 .prefix = POSIX_ACL_XATTR_ACCESS,
431547b3
CH
493 .flags = ACL_TYPE_ACCESS,
494 .get = posix_acl_get,
495 .set = posix_acl_set,
1da177e4
LT
496 .list = posix_acl_access_list,
497};
498
431547b3 499static size_t posix_acl_default_list(struct dentry *dentry, char *list,
48b32a35 500 size_t list_size, const char *name,
431547b3 501 size_t name_len, int type)
1da177e4 502{
48b32a35 503 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
431547b3 504 if (!reiserfs_posixacl(dentry->d_sb))
bd4c625c 505 return 0;
48b32a35
JM
506 if (list && size <= list_size)
507 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
508 return size;
1da177e4
LT
509}
510
94d09a98 511const struct xattr_handler reiserfs_posix_acl_default_handler = {
9a59f452 512 .prefix = POSIX_ACL_XATTR_DEFAULT,
431547b3
CH
513 .flags = ACL_TYPE_DEFAULT,
514 .get = posix_acl_get,
515 .set = posix_acl_set,
1da177e4
LT
516 .list = posix_acl_default_list,
517};