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