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