Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ceph/acl.c |
| 3 | * |
| 4 | * Copyright (C) 2013 Guangliang Zhao, <lucienchao@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public |
| 8 | * License v2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public |
| 16 | * License along with this program; if not, write to the |
| 17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 | * Boston, MA 021110-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/ceph/ceph_debug.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/xattr.h> |
| 25 | #include <linux/posix_acl_xattr.h> |
| 26 | #include <linux/posix_acl.h> |
| 27 | #include <linux/sched.h> |
| 28 | #include <linux/slab.h> |
| 29 | |
| 30 | #include "super.h" |
| 31 | |
| 32 | static inline void ceph_set_cached_acl(struct inode *inode, |
| 33 | int type, struct posix_acl *acl) |
| 34 | { |
| 35 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 36 | |
| 37 | spin_lock(&ci->i_ceph_lock); |
| 38 | if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0)) |
| 39 | set_cached_acl(inode, type, acl); |
| 40 | spin_unlock(&ci->i_ceph_lock); |
| 41 | } |
| 42 | |
| 43 | static inline struct posix_acl *ceph_get_cached_acl(struct inode *inode, |
| 44 | int type) |
| 45 | { |
| 46 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 47 | struct posix_acl *acl = ACL_NOT_CACHED; |
| 48 | |
| 49 | spin_lock(&ci->i_ceph_lock); |
| 50 | if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0)) |
| 51 | acl = get_cached_acl(inode, type); |
| 52 | spin_unlock(&ci->i_ceph_lock); |
| 53 | |
| 54 | return acl; |
| 55 | } |
| 56 | |
| 57 | void ceph_forget_all_cached_acls(struct inode *inode) |
| 58 | { |
| 59 | forget_all_cached_acls(inode); |
| 60 | } |
| 61 | |
| 62 | struct posix_acl *ceph_get_acl(struct inode *inode, int type) |
| 63 | { |
| 64 | int size; |
| 65 | const char *name; |
| 66 | char *value = NULL; |
| 67 | struct posix_acl *acl; |
| 68 | |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 69 | switch (type) { |
| 70 | case ACL_TYPE_ACCESS: |
| 71 | name = POSIX_ACL_XATTR_ACCESS; |
| 72 | break; |
| 73 | case ACL_TYPE_DEFAULT: |
| 74 | name = POSIX_ACL_XATTR_DEFAULT; |
| 75 | break; |
| 76 | default: |
| 77 | BUG(); |
| 78 | } |
| 79 | |
| 80 | size = __ceph_getxattr(inode, name, "", 0); |
| 81 | if (size > 0) { |
| 82 | value = kzalloc(size, GFP_NOFS); |
| 83 | if (!value) |
| 84 | return ERR_PTR(-ENOMEM); |
| 85 | size = __ceph_getxattr(inode, name, value, size); |
| 86 | } |
| 87 | |
| 88 | if (size > 0) |
| 89 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
| 90 | else if (size == -ERANGE || size == -ENODATA || size == 0) |
| 91 | acl = NULL; |
| 92 | else |
| 93 | acl = ERR_PTR(-EIO); |
| 94 | |
| 95 | kfree(value); |
| 96 | |
| 97 | if (!IS_ERR(acl)) |
| 98 | ceph_set_cached_acl(inode, type, acl); |
| 99 | |
| 100 | return acl; |
| 101 | } |
| 102 | |
Sage Weil | 72466d0 | 2014-01-29 06:22:25 -0800 | [diff] [blame] | 103 | int ceph_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 104 | { |
| 105 | int ret = 0, size = 0; |
| 106 | const char *name = NULL; |
| 107 | char *value = NULL; |
| 108 | struct iattr newattrs; |
| 109 | umode_t new_mode = inode->i_mode, old_mode = inode->i_mode; |
Sage Weil | 77516dc | 2014-01-31 07:25:25 -0800 | [diff] [blame] | 110 | struct dentry *dentry; |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 111 | |
| 112 | if (acl) { |
| 113 | ret = posix_acl_valid(acl); |
| 114 | if (ret < 0) |
| 115 | goto out; |
| 116 | } |
| 117 | |
| 118 | switch (type) { |
| 119 | case ACL_TYPE_ACCESS: |
| 120 | name = POSIX_ACL_XATTR_ACCESS; |
| 121 | if (acl) { |
| 122 | ret = posix_acl_equiv_mode(acl, &new_mode); |
| 123 | if (ret < 0) |
| 124 | goto out; |
| 125 | if (ret == 0) |
| 126 | acl = NULL; |
| 127 | } |
| 128 | break; |
| 129 | case ACL_TYPE_DEFAULT: |
| 130 | if (!S_ISDIR(inode->i_mode)) { |
| 131 | ret = acl ? -EINVAL : 0; |
| 132 | goto out; |
| 133 | } |
| 134 | name = POSIX_ACL_XATTR_DEFAULT; |
| 135 | break; |
| 136 | default: |
| 137 | ret = -EINVAL; |
| 138 | goto out; |
| 139 | } |
| 140 | |
| 141 | if (acl) { |
| 142 | size = posix_acl_xattr_size(acl->a_count); |
| 143 | value = kmalloc(size, GFP_NOFS); |
| 144 | if (!value) { |
| 145 | ret = -ENOMEM; |
| 146 | goto out; |
| 147 | } |
| 148 | |
| 149 | ret = posix_acl_to_xattr(&init_user_ns, acl, value, size); |
| 150 | if (ret < 0) |
| 151 | goto out_free; |
| 152 | } |
| 153 | |
Sage Weil | 77516dc | 2014-01-31 07:25:25 -0800 | [diff] [blame] | 154 | dentry = d_find_alias(inode); |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 155 | if (new_mode != old_mode) { |
| 156 | newattrs.ia_mode = new_mode; |
| 157 | newattrs.ia_valid = ATTR_MODE; |
| 158 | ret = ceph_setattr(dentry, &newattrs); |
| 159 | if (ret) |
Sage Weil | 77516dc | 2014-01-31 07:25:25 -0800 | [diff] [blame] | 160 | goto out_dput; |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 161 | } |
| 162 | |
Yan, Zheng | 7a92d64 | 2014-02-11 13:08:51 +0800 | [diff] [blame^] | 163 | ret = __ceph_setxattr(dentry, name, value, size, 0); |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 164 | if (ret) { |
| 165 | if (new_mode != old_mode) { |
| 166 | newattrs.ia_mode = old_mode; |
| 167 | newattrs.ia_valid = ATTR_MODE; |
| 168 | ceph_setattr(dentry, &newattrs); |
| 169 | } |
Sage Weil | 77516dc | 2014-01-31 07:25:25 -0800 | [diff] [blame] | 170 | goto out_dput; |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | ceph_set_cached_acl(inode, type, acl); |
| 174 | |
Sage Weil | 77516dc | 2014-01-31 07:25:25 -0800 | [diff] [blame] | 175 | out_dput: |
| 176 | dput(dentry); |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 177 | out_free: |
| 178 | kfree(value); |
| 179 | out: |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | int ceph_init_acl(struct dentry *dentry, struct inode *inode, struct inode *dir) |
| 184 | { |
Christoph Hellwig | 7585823 | 2014-01-30 08:56:36 -0800 | [diff] [blame] | 185 | struct posix_acl *default_acl, *acl; |
| 186 | int error; |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 187 | |
Christoph Hellwig | 7585823 | 2014-01-30 08:56:36 -0800 | [diff] [blame] | 188 | error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); |
| 189 | if (error) |
| 190 | return error; |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 191 | |
Christoph Hellwig | 7585823 | 2014-01-30 08:56:36 -0800 | [diff] [blame] | 192 | if (!default_acl && !acl) |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 193 | cache_no_acl(inode); |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 194 | |
Christoph Hellwig | 7585823 | 2014-01-30 08:56:36 -0800 | [diff] [blame] | 195 | if (default_acl) { |
| 196 | error = ceph_set_acl(inode, default_acl, ACL_TYPE_DEFAULT); |
| 197 | posix_acl_release(default_acl); |
| 198 | } |
| 199 | if (acl) { |
| 200 | if (!error) |
| 201 | error = ceph_set_acl(inode, acl, ACL_TYPE_ACCESS); |
| 202 | posix_acl_release(acl); |
| 203 | } |
| 204 | return error; |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 205 | } |