]> Pileus Git - ~andy/linux/blob - fs/ceph/acl.c
ceph: fix posix ACL hooks
[~andy/linux] / fs / ceph / acl.c
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
69         if (!IS_POSIXACL(inode))
70                 return NULL;
71
72         acl = ceph_get_cached_acl(inode, type);
73         if (acl != ACL_NOT_CACHED)
74                 return acl;
75
76         switch (type) {
77         case ACL_TYPE_ACCESS:
78                 name = POSIX_ACL_XATTR_ACCESS;
79                 break;
80         case ACL_TYPE_DEFAULT:
81                 name = POSIX_ACL_XATTR_DEFAULT;
82                 break;
83         default:
84                 BUG();
85         }
86
87         size = __ceph_getxattr(inode, name, "", 0);
88         if (size > 0) {
89                 value = kzalloc(size, GFP_NOFS);
90                 if (!value)
91                         return ERR_PTR(-ENOMEM);
92                 size = __ceph_getxattr(inode, name, value, size);
93         }
94
95         if (size > 0)
96                 acl = posix_acl_from_xattr(&init_user_ns, value, size);
97         else if (size == -ERANGE || size == -ENODATA || size == 0)
98                 acl = NULL;
99         else
100                 acl = ERR_PTR(-EIO);
101
102         kfree(value);
103
104         if (!IS_ERR(acl))
105                 ceph_set_cached_acl(inode, type, acl);
106
107         return acl;
108 }
109
110 int ceph_set_acl(struct inode *inode, struct posix_acl *acl, int type)
111 {
112         int ret = 0, size = 0;
113         const char *name = NULL;
114         char *value = NULL;
115         struct iattr newattrs;
116         umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
117         struct dentry *dentry = d_find_alias(inode);
118
119         if (acl) {
120                 ret = posix_acl_valid(acl);
121                 if (ret < 0)
122                         goto out;
123         }
124
125         switch (type) {
126         case ACL_TYPE_ACCESS:
127                 name = POSIX_ACL_XATTR_ACCESS;
128                 if (acl) {
129                         ret = posix_acl_equiv_mode(acl, &new_mode);
130                         if (ret < 0)
131                                 goto out;
132                         if (ret == 0)
133                                 acl = NULL;
134                 }
135                 break;
136         case ACL_TYPE_DEFAULT:
137                 if (!S_ISDIR(inode->i_mode)) {
138                         ret = acl ? -EINVAL : 0;
139                         goto out;
140                 }
141                 name = POSIX_ACL_XATTR_DEFAULT;
142                 break;
143         default:
144                 ret = -EINVAL;
145                 goto out;
146         }
147
148         if (acl) {
149                 size = posix_acl_xattr_size(acl->a_count);
150                 value = kmalloc(size, GFP_NOFS);
151                 if (!value) {
152                         ret = -ENOMEM;
153                         goto out;
154                 }
155
156                 ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
157                 if (ret < 0)
158                         goto out_free;
159         }
160
161         if (new_mode != old_mode) {
162                 newattrs.ia_mode = new_mode;
163                 newattrs.ia_valid = ATTR_MODE;
164                 ret = ceph_setattr(dentry, &newattrs);
165                 if (ret)
166                         goto out_free;
167         }
168
169         if (value)
170                 ret = __ceph_setxattr(dentry, name, value, size, 0);
171         else
172                 ret = __ceph_removexattr(dentry, name);
173
174         if (ret) {
175                 if (new_mode != old_mode) {
176                         newattrs.ia_mode = old_mode;
177                         newattrs.ia_valid = ATTR_MODE;
178                         ceph_setattr(dentry, &newattrs);
179                 }
180                 goto out_free;
181         }
182
183         ceph_set_cached_acl(inode, type, acl);
184
185 out_free:
186         kfree(value);
187 out:
188         return ret;
189 }
190
191 int ceph_init_acl(struct dentry *dentry, struct inode *inode, struct inode *dir)
192 {
193         struct posix_acl *acl = NULL;
194         int ret = 0;
195
196         if (!S_ISLNK(inode->i_mode)) {
197                 if (IS_POSIXACL(dir)) {
198                         acl = ceph_get_acl(dir, ACL_TYPE_DEFAULT);
199                         if (IS_ERR(acl)) {
200                                 ret = PTR_ERR(acl);
201                                 goto out;
202                         }
203                 }
204
205                 if (!acl)
206                         inode->i_mode &= ~current_umask();
207         }
208
209         if (IS_POSIXACL(dir) && acl) {
210                 if (S_ISDIR(inode->i_mode)) {
211                         ret = ceph_set_acl(inode, acl, ACL_TYPE_DEFAULT);
212                         if (ret)
213                                 goto out_release;
214                 }
215                 ret = __posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
216                 if (ret < 0)
217                         goto out;
218                 else if (ret > 0)
219                         ret = ceph_set_acl(inode, acl, ACL_TYPE_ACCESS);
220                 else
221                         cache_no_acl(inode);
222         } else {
223                 cache_no_acl(inode);
224         }
225
226 out_release:
227         posix_acl_release(acl);
228 out:
229         return ret;
230 }