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> |
| 23 | #include "ctree.h" |
| 24 | #include "xattr.h" |
Yan | fb4bc1e | 2008-01-17 11:59:51 -0500 | [diff] [blame^] | 25 | #ifndef is_owner_or_cap |
| 26 | #define is_owner_or_cap(inode) \ |
| 27 | ((current->fsuid == (inode)->i_uid) || capable(CAP_FOWNER)) |
| 28 | #endif |
| 29 | |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 30 | static int btrfs_xattr_set_acl(struct inode *inode, int type, |
| 31 | const void *value, size_t size) |
| 32 | { |
| 33 | int ret = 0; |
| 34 | struct posix_acl *acl; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 35 | |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 36 | if (!is_owner_or_cap(inode)) |
| 37 | return -EPERM; |
| 38 | if (value) { |
| 39 | acl = posix_acl_from_xattr(value, size); |
| 40 | if (acl == NULL) { |
| 41 | value = NULL; |
| 42 | size = 0; |
| 43 | } else if (IS_ERR(acl)) { |
| 44 | ret = PTR_ERR(acl); |
| 45 | } else { |
| 46 | ret = posix_acl_valid(acl); |
| 47 | posix_acl_release(acl); |
| 48 | } |
| 49 | if (ret) |
| 50 | return ret; |
| 51 | } |
| 52 | return btrfs_xattr_set(inode, type, "", value, size, 0); |
| 53 | } |
Josef Bacik | 1caf934 | 2007-11-19 10:18:17 -0500 | [diff] [blame] | 54 | |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 55 | static int btrfs_xattr_get_acl(struct inode *inode, int type, |
| 56 | void *value, size_t size) |
| 57 | { |
| 58 | return btrfs_xattr_get(inode, type, "", value, size); |
| 59 | } |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 60 | static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name, |
| 61 | void *value, size_t size) |
| 62 | { |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 63 | if (*name != '\0') |
| 64 | return -EINVAL; |
| 65 | return btrfs_xattr_get_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS, |
| 66 | value, size); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 67 | } |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 68 | static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name, |
| 69 | const void *value, size_t size, int flags) |
| 70 | { |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 71 | if (*name != '\0') |
| 72 | return -EINVAL; |
| 73 | return btrfs_xattr_set_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS, |
| 74 | value, size); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 75 | } |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 76 | static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name, |
| 77 | void *value, size_t size) |
| 78 | { |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 79 | if (*name != '\0') |
| 80 | return -EINVAL; |
| 81 | return btrfs_xattr_get_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT, |
| 82 | value, size); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 83 | } |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 84 | static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name, |
| 85 | const void *value, size_t size, int flags) |
| 86 | { |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 87 | if (*name != '\0') |
| 88 | return -EINVAL; |
| 89 | return btrfs_xattr_set_acl(inode, BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT, |
| 90 | value, size); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 91 | } |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 92 | struct xattr_handler btrfs_xattr_acl_default_handler = { |
| 93 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
| 94 | .list = btrfs_xattr_generic_list, |
| 95 | .get = btrfs_xattr_acl_default_get, |
| 96 | .set = btrfs_xattr_acl_default_set, |
| 97 | }; |
| 98 | |
| 99 | struct xattr_handler btrfs_xattr_acl_access_handler = { |
| 100 | .prefix = POSIX_ACL_XATTR_ACCESS, |
| 101 | .list = btrfs_xattr_generic_list, |
| 102 | .get = btrfs_xattr_acl_access_get, |
| 103 | .set = btrfs_xattr_acl_access_set, |
| 104 | }; |