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 xattr_set_acl(struct inode *inode, int type, const void *value, size_t size)
20 struct posix_acl *acl;
22 struct reiserfs_transaction_handle th;
23 size_t jcreate_blocks;
24 if (!reiserfs_posixacl(inode->i_sb))
26 if (!is_owner_or_cap(inode))
30 acl = posix_acl_from_xattr(value, size);
34 error = posix_acl_valid(acl);
41 /* Pessimism: We can't assume that anything from the xattr root up
42 * has been created. */
44 jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
45 reiserfs_xattr_nblocks(inode, size) * 2;
47 reiserfs_write_lock(inode->i_sb);
48 error = journal_begin(&th, inode->i_sb, jcreate_blocks);
50 error = reiserfs_set_acl(&th, inode, type, acl);
51 error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
55 reiserfs_write_unlock(inode->i_sb);
58 posix_acl_release(acl);
63 xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
65 struct posix_acl *acl;
68 if (!reiserfs_posixacl(inode->i_sb))
71 acl = reiserfs_get_acl(inode, type);
76 error = posix_acl_to_xattr(acl, buffer, size);
77 posix_acl_release(acl);
83 * Convert from filesystem to in-memory representation.
85 static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
87 const char *end = (char *)value + size;
89 struct posix_acl *acl;
93 if (size < sizeof(reiserfs_acl_header))
94 return ERR_PTR(-EINVAL);
95 if (((reiserfs_acl_header *) value)->a_version !=
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);
101 return ERR_PTR(-EINVAL);
104 acl = posix_acl_alloc(count, GFP_NOFS);
106 return ERR_PTR(-ENOMEM);
107 for (n = 0; n < count; n++) {
108 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
109 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
111 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
112 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
113 switch (acl->a_entries[n].e_tag) {
118 value = (char *)value +
119 sizeof(reiserfs_acl_entry_short);
120 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
125 value = (char *)value + sizeof(reiserfs_acl_entry);
126 if ((char *)value > end)
128 acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
140 posix_acl_release(acl);
141 return ERR_PTR(-EINVAL);
145 * Convert from in-memory to filesystem representation.
147 static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
149 reiserfs_acl_header *ext_acl;
153 *size = reiserfs_acl_size(acl->a_count);
154 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
156 sizeof(reiserfs_acl_entry),
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);
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);
165 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
166 switch (acl->a_entries[n].e_tag) {
169 entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
170 e += sizeof(reiserfs_acl_entry);
177 e += sizeof(reiserfs_acl_entry_short);
184 return (char *)ext_acl;
188 return ERR_PTR(-EINVAL);
191 static inline void iset_acl(struct inode *inode, struct posix_acl **i_acl,
192 struct posix_acl *acl)
194 spin_lock(&inode->i_lock);
195 if (*i_acl != ACL_NOT_CACHED)
196 posix_acl_release(*i_acl);
197 *i_acl = posix_acl_dup(acl);
198 spin_unlock(&inode->i_lock);
201 static inline struct posix_acl *iget_acl(struct inode *inode,
202 struct posix_acl **i_acl)
204 struct posix_acl *acl = ACL_NOT_CACHED;
206 spin_lock(&inode->i_lock);
207 if (*i_acl != ACL_NOT_CACHED)
208 acl = posix_acl_dup(*i_acl);
209 spin_unlock(&inode->i_lock);
215 * Inode operation get_posix_acl().
217 * inode->i_mutex: down
218 * BKL held [before 2.5.x]
220 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
223 struct posix_acl *acl, **p_acl;
226 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
229 case ACL_TYPE_ACCESS:
230 name = POSIX_ACL_XATTR_ACCESS;
231 p_acl = &reiserfs_i->i_acl_access;
233 case ACL_TYPE_DEFAULT:
234 name = POSIX_ACL_XATTR_DEFAULT;
235 p_acl = &reiserfs_i->i_acl_default;
238 return ERR_PTR(-EINVAL);
241 acl = iget_acl(inode, p_acl);
242 if (acl != ACL_NOT_CACHED)
245 size = reiserfs_xattr_get(inode, name, NULL, 0);
247 if (size == -ENODATA || size == -ENOSYS) {
251 return ERR_PTR(size);
254 value = kmalloc(size, GFP_NOFS);
256 return ERR_PTR(-ENOMEM);
258 retval = reiserfs_xattr_get(inode, name, value, size);
259 if (retval == -ENODATA || retval == -ENOSYS) {
260 /* This shouldn't actually happen as it should have
261 been caught above.. but just in case */
264 } else if (retval < 0) {
265 acl = ERR_PTR(retval);
267 acl = posix_acl_from_disk(value, retval);
269 iset_acl(inode, p_acl, acl);
277 * Inode operation set_posix_acl().
279 * inode->i_mutex: down
280 * BKL held [before 2.5.x]
283 reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
284 int type, struct posix_acl *acl)
288 struct posix_acl **p_acl;
291 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
293 if (S_ISLNK(inode->i_mode))
297 case ACL_TYPE_ACCESS:
298 name = POSIX_ACL_XATTR_ACCESS;
299 p_acl = &reiserfs_i->i_acl_access;
301 mode_t mode = inode->i_mode;
302 error = posix_acl_equiv_mode(acl, &mode);
306 inode->i_mode = mode;
312 case ACL_TYPE_DEFAULT:
313 name = POSIX_ACL_XATTR_DEFAULT;
314 p_acl = &reiserfs_i->i_acl_default;
315 if (!S_ISDIR(inode->i_mode))
316 return acl ? -EACCES : 0;
323 value = posix_acl_to_disk(acl, &size);
325 return (int)PTR_ERR(value);
328 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
331 * Ensure that the inode gets dirtied if we're only using
332 * the mode bits and an old ACL didn't exist. We don't need
333 * to check if the inode is hashed here since we won't get
334 * called by reiserfs_inherit_default_acl().
336 if (error == -ENODATA) {
338 if (type == ACL_TYPE_ACCESS) {
339 inode->i_ctime = CURRENT_TIME_SEC;
340 mark_inode_dirty(inode);
347 iset_acl(inode, p_acl, acl);
352 /* dir->i_mutex: locked,
353 * inode is new and not released into the wild yet */
355 reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
356 struct inode *dir, struct dentry *dentry,
359 struct posix_acl *acl;
362 /* ACLs only get applied to files and directories */
363 if (S_ISLNK(inode->i_mode))
366 /* ACLs can only be used on "new" objects, so if it's an old object
367 * there is nothing to inherit from */
368 if (get_inode_sd_version(dir) == STAT_DATA_V1)
371 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
372 * would be useless since permissions are ignored, and a pain because
373 * it introduces locking cycles */
374 if (IS_PRIVATE(dir)) {
375 inode->i_flags |= S_PRIVATE;
379 acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
384 struct posix_acl *acl_copy;
385 mode_t mode = inode->i_mode;
388 /* Copy the default ACL to the default ACL of a new directory */
389 if (S_ISDIR(inode->i_mode)) {
390 err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
396 /* Now we reconcile the new ACL and the mode,
397 potentially modifying both */
398 acl_copy = posix_acl_clone(acl, GFP_NOFS);
404 need_acl = posix_acl_create_masq(acl_copy, &mode);
406 if (mode != inode->i_mode) {
407 inode->i_mode = mode;
410 /* If we need an ACL.. */
412 err = reiserfs_set_acl(th, inode,
420 posix_acl_release(acl_copy);
422 posix_acl_release(acl);
425 /* no ACL, apply umask */
426 inode->i_mode &= ~current_umask();
432 /* This is used to cache the default acl before a new object is created.
433 * The biggest reason for this is to get an idea of how many blocks will
434 * actually be required for the create operation if we must inherit an ACL.
435 * An ACL write can add up to 3 object creations and an additional file write
436 * so we'd prefer not to reserve that many blocks in the journal if we can.
437 * It also has the advantage of not loading the ACL with a transaction open,
438 * this may seem silly, but if the owner of the directory is doing the
439 * creation, the ACL may not be loaded since the permissions wouldn't require
441 * We return the number of blocks required for the transaction.
443 int reiserfs_cache_default_acl(struct inode *inode)
445 struct posix_acl *acl;
448 if (IS_PRIVATE(inode))
451 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
453 if (acl && !IS_ERR(acl)) {
454 int size = reiserfs_acl_size(acl->a_count);
456 /* Other xattrs can be created during inode creation. We don't
457 * want to claim too many blocks, so we check to see if we
458 * we need to create the tree to the xattrs, and then we
459 * just want two files. */
460 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
461 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
463 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
465 /* We need to account for writes + bitmaps for two files */
466 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
467 posix_acl_release(acl);
473 int reiserfs_acl_chmod(struct inode *inode)
475 struct posix_acl *acl, *clone;
478 if (S_ISLNK(inode->i_mode))
481 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
482 !reiserfs_posixacl(inode->i_sb)) {
486 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
491 clone = posix_acl_clone(acl, GFP_NOFS);
492 posix_acl_release(acl);
495 error = posix_acl_chmod_masq(clone, inode->i_mode);
497 struct reiserfs_transaction_handle th;
498 size_t size = reiserfs_xattr_nblocks(inode,
499 reiserfs_acl_size(clone->a_count));
500 reiserfs_write_lock(inode->i_sb);
501 error = journal_begin(&th, inode->i_sb, size * 2);
504 error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS,
506 error2 = journal_end(&th, inode->i_sb, size * 2);
510 reiserfs_write_unlock(inode->i_sb);
512 posix_acl_release(clone);
517 posix_acl_access_get(struct inode *inode, const char *name,
518 void *buffer, size_t size)
520 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
522 return xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
526 posix_acl_access_set(struct inode *inode, const char *name,
527 const void *value, size_t size, int flags)
529 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
531 return xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
534 static size_t posix_acl_access_list(struct inode *inode, char *list,
535 size_t list_size, const char *name,
538 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
539 if (!reiserfs_posixacl(inode->i_sb))
541 if (list && size <= list_size)
542 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
546 struct xattr_handler reiserfs_posix_acl_access_handler = {
547 .prefix = POSIX_ACL_XATTR_ACCESS,
548 .get = posix_acl_access_get,
549 .set = posix_acl_access_set,
550 .list = posix_acl_access_list,
554 posix_acl_default_get(struct inode *inode, const char *name,
555 void *buffer, size_t size)
557 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
559 return xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
563 posix_acl_default_set(struct inode *inode, const char *name,
564 const void *value, size_t size, int flags)
566 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
568 return xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
571 static size_t posix_acl_default_list(struct inode *inode, char *list,
572 size_t list_size, const char *name,
575 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
576 if (!reiserfs_posixacl(inode->i_sb))
578 if (list && size <= list_size)
579 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
583 struct xattr_handler reiserfs_posix_acl_default_handler = {
584 .prefix = POSIX_ACL_XATTR_DEFAULT,
585 .get = posix_acl_default_get,
586 .set = posix_acl_default_set,
587 .list = posix_acl_default_list,