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;
69 if (!IS_POSIXACL(inode))
72 acl = ceph_get_cached_acl(inode, type);
73 if (acl != ACL_NOT_CACHED)
78 name = POSIX_ACL_XATTR_ACCESS;
80 case ACL_TYPE_DEFAULT:
81 name = POSIX_ACL_XATTR_DEFAULT;
87 size = __ceph_getxattr(inode, name, "", 0);
89 value = kzalloc(size, GFP_NOFS);
91 return ERR_PTR(-ENOMEM);
92 size = __ceph_getxattr(inode, name, value, size);
96 acl = posix_acl_from_xattr(&init_user_ns, value, size);
97 else if (size == -ERANGE || size == -ENODATA || size == 0)
105 ceph_set_cached_acl(inode, type, acl);
110 static int ceph_set_acl(struct dentry *dentry, struct inode *inode,
111 struct posix_acl *acl, int type)
113 int ret = 0, size = 0;
114 const char *name = NULL;
116 struct iattr newattrs;
117 umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
120 ret = posix_acl_valid(acl);
126 case ACL_TYPE_ACCESS:
127 name = POSIX_ACL_XATTR_ACCESS;
129 ret = posix_acl_equiv_mode(acl, &new_mode);
136 case ACL_TYPE_DEFAULT:
137 if (!S_ISDIR(inode->i_mode)) {
138 ret = acl ? -EINVAL : 0;
141 name = POSIX_ACL_XATTR_DEFAULT;
149 size = posix_acl_xattr_size(acl->a_count);
150 value = kmalloc(size, GFP_NOFS);
156 ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
161 if (new_mode != old_mode) {
162 newattrs.ia_mode = new_mode;
163 newattrs.ia_valid = ATTR_MODE;
164 ret = ceph_setattr(dentry, &newattrs);
170 ret = __ceph_setxattr(dentry, name, value, size, 0);
172 ret = __ceph_removexattr(dentry, name);
175 if (new_mode != old_mode) {
176 newattrs.ia_mode = old_mode;
177 newattrs.ia_valid = ATTR_MODE;
178 ceph_setattr(dentry, &newattrs);
183 ceph_set_cached_acl(inode, type, acl);
191 int ceph_init_acl(struct dentry *dentry, struct inode *inode, struct inode *dir)
193 struct posix_acl *acl = NULL;
196 if (!S_ISLNK(inode->i_mode)) {
197 if (IS_POSIXACL(dir)) {
198 acl = ceph_get_acl(dir, ACL_TYPE_DEFAULT);
206 inode->i_mode &= ~current_umask();
209 if (IS_POSIXACL(dir) && acl) {
210 if (S_ISDIR(inode->i_mode)) {
211 ret = ceph_set_acl(dentry, inode, acl,
216 ret = __posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
220 ret = ceph_set_acl(dentry, inode, acl, ACL_TYPE_ACCESS);
228 posix_acl_release(acl);