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