1 #include <linux/capability.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>
8 #include <linux/posix_acl_xattr.h>
9 #include <linux/reiserfs_xattr.h>
10 #include <linux/reiserfs_acl.h>
11 #include <asm/uaccess.h>
13 static int reiserfs_set_acl(struct reiserfs_transaction_handle
*th
,
14 struct inode
*inode
, int type
,
15 struct posix_acl
*acl
);
18 posix_acl_set(struct dentry
*dentry
, const char *name
, const void *value
,
19 size_t size
, int flags
, int type
)
21 struct inode
*inode
= dentry
->d_inode
;
22 struct posix_acl
*acl
;
24 struct reiserfs_transaction_handle th
;
25 size_t jcreate_blocks
;
26 if (!reiserfs_posixacl(inode
->i_sb
))
28 if (!is_owner_or_cap(inode
))
32 acl
= posix_acl_from_xattr(value
, size
);
36 error
= posix_acl_valid(acl
);
43 /* Pessimism: We can't assume that anything from the xattr root up
44 * has been created. */
46 jcreate_blocks
= reiserfs_xattr_jcreate_nblocks(inode
) +
47 reiserfs_xattr_nblocks(inode
, size
) * 2;
49 reiserfs_write_lock(inode
->i_sb
);
50 error
= journal_begin(&th
, inode
->i_sb
, jcreate_blocks
);
52 error
= reiserfs_set_acl(&th
, inode
, type
, acl
);
53 error2
= journal_end(&th
, inode
->i_sb
, jcreate_blocks
);
57 reiserfs_write_unlock(inode
->i_sb
);
60 posix_acl_release(acl
);
65 posix_acl_get(struct dentry
*dentry
, const char *name
, void *buffer
,
66 size_t size
, int type
)
68 struct posix_acl
*acl
;
71 if (!reiserfs_posixacl(dentry
->d_sb
))
74 acl
= reiserfs_get_acl(dentry
->d_inode
, type
);
79 error
= posix_acl_to_xattr(acl
, buffer
, size
);
80 posix_acl_release(acl
);
86 * Convert from filesystem to in-memory representation.
88 static struct posix_acl
*posix_acl_from_disk(const void *value
, size_t size
)
90 const char *end
= (char *)value
+ size
;
92 struct posix_acl
*acl
;
96 if (size
< sizeof(reiserfs_acl_header
))
97 return ERR_PTR(-EINVAL
);
98 if (((reiserfs_acl_header
*) value
)->a_version
!=
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
);
104 return ERR_PTR(-EINVAL
);
107 acl
= posix_acl_alloc(count
, GFP_NOFS
);
109 return ERR_PTR(-ENOMEM
);
110 for (n
= 0; n
< count
; n
++) {
111 reiserfs_acl_entry
*entry
= (reiserfs_acl_entry
*) value
;
112 if ((char *)value
+ sizeof(reiserfs_acl_entry_short
) > end
)
114 acl
->a_entries
[n
].e_tag
= le16_to_cpu(entry
->e_tag
);
115 acl
->a_entries
[n
].e_perm
= le16_to_cpu(entry
->e_perm
);
116 switch (acl
->a_entries
[n
].e_tag
) {
121 value
= (char *)value
+
122 sizeof(reiserfs_acl_entry_short
);
123 acl
->a_entries
[n
].e_id
= ACL_UNDEFINED_ID
;
128 value
= (char *)value
+ sizeof(reiserfs_acl_entry
);
129 if ((char *)value
> end
)
131 acl
->a_entries
[n
].e_id
= le32_to_cpu(entry
->e_id
);
143 posix_acl_release(acl
);
144 return ERR_PTR(-EINVAL
);
148 * Convert from in-memory to filesystem representation.
150 static void *posix_acl_to_disk(const struct posix_acl
*acl
, size_t * size
)
152 reiserfs_acl_header
*ext_acl
;
156 *size
= reiserfs_acl_size(acl
->a_count
);
157 ext_acl
= kmalloc(sizeof(reiserfs_acl_header
) +
159 sizeof(reiserfs_acl_entry
),
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
);
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
);
168 entry
->e_perm
= cpu_to_le16(acl
->a_entries
[n
].e_perm
);
169 switch (acl
->a_entries
[n
].e_tag
) {
172 entry
->e_id
= cpu_to_le32(acl
->a_entries
[n
].e_id
);
173 e
+= sizeof(reiserfs_acl_entry
);
180 e
+= sizeof(reiserfs_acl_entry_short
);
187 return (char *)ext_acl
;
191 return ERR_PTR(-EINVAL
);
195 * Inode operation get_posix_acl().
197 * inode->i_mutex: down
198 * BKL held [before 2.5.x]
200 struct posix_acl
*reiserfs_get_acl(struct inode
*inode
, int type
)
203 struct posix_acl
*acl
;
207 acl
= get_cached_acl(inode
, type
);
208 if (acl
!= ACL_NOT_CACHED
)
212 case ACL_TYPE_ACCESS
:
213 name
= POSIX_ACL_XATTR_ACCESS
;
215 case ACL_TYPE_DEFAULT
:
216 name
= POSIX_ACL_XATTR_DEFAULT
;
222 size
= reiserfs_xattr_get(inode
, name
, NULL
, 0);
224 if (size
== -ENODATA
|| size
== -ENOSYS
) {
225 set_cached_acl(inode
, type
, NULL
);
228 return ERR_PTR(size
);
231 value
= kmalloc(size
, GFP_NOFS
);
233 return ERR_PTR(-ENOMEM
);
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 */
240 } else if (retval
< 0) {
241 acl
= ERR_PTR(retval
);
243 acl
= posix_acl_from_disk(value
, retval
);
246 set_cached_acl(inode
, type
, acl
);
253 * Inode operation set_posix_acl().
255 * inode->i_mutex: down
256 * BKL held [before 2.5.x]
259 reiserfs_set_acl(struct reiserfs_transaction_handle
*th
, struct inode
*inode
,
260 int type
, struct posix_acl
*acl
)
267 if (S_ISLNK(inode
->i_mode
))
271 case ACL_TYPE_ACCESS
:
272 name
= POSIX_ACL_XATTR_ACCESS
;
274 mode_t mode
= inode
->i_mode
;
275 error
= posix_acl_equiv_mode(acl
, &mode
);
279 inode
->i_mode
= mode
;
285 case ACL_TYPE_DEFAULT
:
286 name
= POSIX_ACL_XATTR_DEFAULT
;
287 if (!S_ISDIR(inode
->i_mode
))
288 return acl
? -EACCES
: 0;
295 value
= posix_acl_to_disk(acl
, &size
);
297 return (int)PTR_ERR(value
);
300 error
= reiserfs_xattr_set_handle(th
, inode
, name
, value
, size
, 0);
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().
308 if (error
== -ENODATA
) {
310 if (type
== ACL_TYPE_ACCESS
) {
311 inode
->i_ctime
= CURRENT_TIME_SEC
;
312 mark_inode_dirty(inode
);
319 set_cached_acl(inode
, type
, acl
);
324 /* dir->i_mutex: locked,
325 * inode is new and not released into the wild yet */
327 reiserfs_inherit_default_acl(struct reiserfs_transaction_handle
*th
,
328 struct inode
*dir
, struct dentry
*dentry
,
331 struct posix_acl
*acl
;
334 /* ACLs only get applied to files and directories */
335 if (S_ISLNK(inode
->i_mode
))
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
)
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 */
346 if (IS_PRIVATE(dir
)) {
347 inode
->i_flags
|= S_PRIVATE
;
351 acl
= reiserfs_get_acl(dir
, ACL_TYPE_DEFAULT
);
356 struct posix_acl
*acl_copy
;
357 mode_t mode
= inode
->i_mode
;
360 /* Copy the default ACL to the default ACL of a new directory */
361 if (S_ISDIR(inode
->i_mode
)) {
362 err
= reiserfs_set_acl(th
, inode
, ACL_TYPE_DEFAULT
,
368 /* Now we reconcile the new ACL and the mode,
369 potentially modifying both */
370 acl_copy
= posix_acl_clone(acl
, GFP_NOFS
);
376 need_acl
= posix_acl_create_masq(acl_copy
, &mode
);
378 if (mode
!= inode
->i_mode
) {
379 inode
->i_mode
= mode
;
382 /* If we need an ACL.. */
384 err
= reiserfs_set_acl(th
, inode
,
392 posix_acl_release(acl_copy
);
394 posix_acl_release(acl
);
397 /* no ACL, apply umask */
398 inode
->i_mode
&= ~current_umask();
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
413 * We return the number of blocks required for the transaction.
415 int reiserfs_cache_default_acl(struct inode
*inode
)
417 struct posix_acl
*acl
;
420 if (IS_PRIVATE(inode
))
423 acl
= reiserfs_get_acl(inode
, ACL_TYPE_DEFAULT
);
425 if (acl
&& !IS_ERR(acl
)) {
426 int size
= reiserfs_acl_size(acl
->a_count
);
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
);
435 REISERFS_I(inode
)->i_flags
|= i_has_xattr_dir
;
437 /* We need to account for writes + bitmaps for two files */
438 nblocks
+= reiserfs_xattr_nblocks(inode
, size
) * 4;
439 posix_acl_release(acl
);
445 int reiserfs_acl_chmod(struct inode
*inode
)
447 struct posix_acl
*acl
, *clone
;
450 if (S_ISLNK(inode
->i_mode
))
453 if (get_inode_sd_version(inode
) == STAT_DATA_V1
||
454 !reiserfs_posixacl(inode
->i_sb
)) {
458 reiserfs_write_unlock(inode
->i_sb
);
459 acl
= reiserfs_get_acl(inode
, ACL_TYPE_ACCESS
);
460 reiserfs_write_lock(inode
->i_sb
);
465 clone
= posix_acl_clone(acl
, GFP_NOFS
);
466 posix_acl_release(acl
);
469 error
= posix_acl_chmod_masq(clone
, inode
->i_mode
);
471 struct reiserfs_transaction_handle th
;
472 size_t size
= reiserfs_xattr_nblocks(inode
,
473 reiserfs_acl_size(clone
->a_count
));
474 reiserfs_write_lock(inode
->i_sb
);
475 error
= journal_begin(&th
, inode
->i_sb
, size
* 2);
478 error
= reiserfs_set_acl(&th
, inode
, ACL_TYPE_ACCESS
,
480 error2
= journal_end(&th
, inode
->i_sb
, size
* 2);
484 reiserfs_write_unlock(inode
->i_sb
);
486 posix_acl_release(clone
);
490 static size_t posix_acl_access_list(struct dentry
*dentry
, char *list
,
491 size_t list_size
, const char *name
,
492 size_t name_len
, int type
)
494 const size_t size
= sizeof(POSIX_ACL_XATTR_ACCESS
);
495 if (!reiserfs_posixacl(dentry
->d_sb
))
497 if (list
&& size
<= list_size
)
498 memcpy(list
, POSIX_ACL_XATTR_ACCESS
, size
);
502 struct xattr_handler reiserfs_posix_acl_access_handler
= {
503 .prefix
= POSIX_ACL_XATTR_ACCESS
,
504 .flags
= ACL_TYPE_ACCESS
,
505 .get
= posix_acl_get
,
506 .set
= posix_acl_set
,
507 .list
= posix_acl_access_list
,
510 static size_t posix_acl_default_list(struct dentry
*dentry
, char *list
,
511 size_t list_size
, const char *name
,
512 size_t name_len
, int type
)
514 const size_t size
= sizeof(POSIX_ACL_XATTR_DEFAULT
);
515 if (!reiserfs_posixacl(dentry
->d_sb
))
517 if (list
&& size
<= list_size
)
518 memcpy(list
, POSIX_ACL_XATTR_DEFAULT
, size
);
522 struct xattr_handler reiserfs_posix_acl_default_handler
= {
523 .prefix
= POSIX_ACL_XATTR_DEFAULT
,
524 .flags
= ACL_TYPE_DEFAULT
,
525 .get
= posix_acl_get
,
526 .set
= posix_acl_set
,
527 .list
= posix_acl_default_list
,