4 * Copyright (C) 2013 Guangliang Zhao, <lucienchao@gmail.com>
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.
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.
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.
21 #include <linux/ceph/ceph_debug.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>
32 static inline void ceph_set_cached_acl(struct inode *inode,
33 int type, struct posix_acl *acl)
35 struct ceph_inode_info *ci = ceph_inode(inode);
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);
43 static inline struct posix_acl *ceph_get_cached_acl(struct inode *inode,
46 struct ceph_inode_info *ci = ceph_inode(inode);
47 struct posix_acl *acl = ACL_NOT_CACHED;
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);
57 void ceph_forget_all_cached_acls(struct inode *inode)
59 forget_all_cached_acls(inode);
62 struct posix_acl *ceph_get_acl(struct inode *inode, int type)
67 struct posix_acl *acl;
71 name = POSIX_ACL_XATTR_ACCESS;
73 case ACL_TYPE_DEFAULT:
74 name = POSIX_ACL_XATTR_DEFAULT;
80 size = __ceph_getxattr(inode, name, "", 0);
82 value = kzalloc(size, GFP_NOFS);
84 return ERR_PTR(-ENOMEM);
85 size = __ceph_getxattr(inode, name, value, size);
89 acl = posix_acl_from_xattr(&init_user_ns, value, size);
90 else if (size == -ERANGE || size == -ENODATA || size == 0)
98 ceph_set_cached_acl(inode, type, acl);
103 int ceph_set_acl(struct inode *inode, struct posix_acl *acl, int type)
105 int ret = 0, size = 0;
106 const char *name = NULL;
108 struct iattr newattrs;
109 umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
110 struct dentry *dentry;
113 ret = posix_acl_valid(acl);
119 case ACL_TYPE_ACCESS:
120 name = POSIX_ACL_XATTR_ACCESS;
122 ret = posix_acl_equiv_mode(acl, &new_mode);
129 case ACL_TYPE_DEFAULT:
130 if (!S_ISDIR(inode->i_mode)) {
131 ret = acl ? -EINVAL : 0;
134 name = POSIX_ACL_XATTR_DEFAULT;
142 size = posix_acl_xattr_size(acl->a_count);
143 value = kmalloc(size, GFP_NOFS);
149 ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
154 dentry = d_find_alias(inode);
155 if (new_mode != old_mode) {
156 newattrs.ia_mode = new_mode;
157 newattrs.ia_valid = ATTR_MODE;
158 ret = ceph_setattr(dentry, &newattrs);
164 ret = __ceph_setxattr(dentry, name, value, size, 0);
166 ret = __ceph_removexattr(dentry, name);
169 if (new_mode != old_mode) {
170 newattrs.ia_mode = old_mode;
171 newattrs.ia_valid = ATTR_MODE;
172 ceph_setattr(dentry, &newattrs);
177 ceph_set_cached_acl(inode, type, acl);
187 int ceph_init_acl(struct dentry *dentry, struct inode *inode, struct inode *dir)
189 struct posix_acl *default_acl, *acl;
192 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
196 if (!default_acl && !acl)
200 error = ceph_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
201 posix_acl_release(default_acl);
205 error = ceph_set_acl(inode, acl, ACL_TYPE_ACCESS);
206 posix_acl_release(acl);