Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Red Hat. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/xattr.h> |
| 22 | #include <linux/posix_acl_xattr.h> |
Chris Mason | c1e32da | 2008-01-22 12:46:56 -0500 | [diff] [blame] | 23 | #include <linux/sched.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 24 | #include "ctree.h" |
| 25 | #include "xattr.h" |
Yan | fb4bc1e | 2008-01-17 11:59:51 -0500 | [diff] [blame] | 26 | #ifndef is_owner_or_cap |
| 27 | #define is_owner_or_cap(inode) \ |
| 28 | ((current->fsuid == (inode)->i_uid) || capable(CAP_FOWNER)) |
| 29 | #endif |
| 30 | |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 31 | static int btrfs_xattr_set_acl(struct inode *inode, int type, |
| 32 | const void *value, size_t size) |
| 33 | { |
| 34 | int ret = 0; |
| 35 | struct posix_acl *acl; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 36 | |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 37 | if (!is_owner_or_cap(inode)) |
| 38 | return -EPERM; |
| 39 | if (value) { |
| 40 | acl = posix_acl_from_xattr(value, size); |
| 41 | if (acl == NULL) { |
| 42 | value = NULL; |
| 43 | size = 0; |
| 44 | } else if (IS_ERR(acl)) { |
| 45 | ret = PTR_ERR(acl); |
| 46 | } else { |
| 47 | ret = posix_acl_valid(acl); |
| 48 | posix_acl_release(acl); |
| 49 | } |
| 50 | if (ret) |
| 51 | return ret; |
| 52 | } |
| 53 | return btrfs_xattr_set(inode, type, "", value, size, 0); |
| 54 | } |
Josef Bacik | 1caf934 | 2007-11-19 10:18:17 -0500 | [diff] [blame] | 55 | |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 56 | static int btrfs_xattr_get_acl(struct inode *inode, int type, |
| 57 | void *value, size_t size) |
| 58 | { |
| 59 | return btrfs_xattr_get(inode, type, "", value, size); |
| 60 | } |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 61 | static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name, |
| 62 | void *value, size_t size) |
| 63 | { |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 64 | if (*name != '\0') |
| 65 | return -EINVAL; |
| 66 | return btrfs_xattr_get_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS, |
| 67 | value, size); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 68 | } |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 69 | static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name, |
| 70 | const void *value, size_t size, int flags) |
| 71 | { |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 72 | if (*name != '\0') |
| 73 | return -EINVAL; |
| 74 | return btrfs_xattr_set_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS, |
| 75 | value, size); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 76 | } |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 77 | static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name, |
| 78 | void *value, size_t size) |
| 79 | { |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 80 | if (*name != '\0') |
| 81 | return -EINVAL; |
| 82 | return btrfs_xattr_get_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT, |
| 83 | value, size); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 84 | } |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 85 | static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name, |
| 86 | const void *value, size_t size, int flags) |
| 87 | { |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 88 | if (*name != '\0') |
| 89 | return -EINVAL; |
| 90 | return btrfs_xattr_set_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT, |
| 91 | value, size); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 92 | } |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 93 | struct xattr_handler btrfs_xattr_acl_default_handler = { |
| 94 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
| 95 | .list = btrfs_xattr_generic_list, |
| 96 | .get = btrfs_xattr_acl_default_get, |
| 97 | .set = btrfs_xattr_acl_default_set, |
| 98 | }; |
| 99 | |
| 100 | struct xattr_handler btrfs_xattr_acl_access_handler = { |
| 101 | .prefix = POSIX_ACL_XATTR_ACCESS, |
| 102 | .list = btrfs_xattr_generic_list, |
| 103 | .get = btrfs_xattr_acl_access_get, |
| 104 | .set = btrfs_xattr_acl_access_set, |
| 105 | }; |