Btrfs: deal with NULL acl sent to btrfs_set_acl
[firefly-linux-kernel-4.4.55.git] / fs / btrfs / acl.c
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 <linux/posix_acl.h>
24 #include <linux/sched.h>
25
26 #include "ctree.h"
27 #include "btrfs_inode.h"
28 #include "xattr.h"
29
30 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
31
32 static struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
33 {
34         int size;
35         const char *name;
36         char *value = NULL;
37         struct posix_acl *acl;
38
39         acl = get_cached_acl(inode, type);
40         if (acl != ACL_NOT_CACHED)
41                 return acl;
42
43         switch (type) {
44         case ACL_TYPE_ACCESS:
45                 name = POSIX_ACL_XATTR_ACCESS;
46                 break;
47         case ACL_TYPE_DEFAULT:
48                 name = POSIX_ACL_XATTR_DEFAULT;
49                 break;
50         default:
51                 BUG();
52         }
53
54         size = __btrfs_getxattr(inode, name, "", 0);
55         if (size > 0) {
56                 value = kzalloc(size, GFP_NOFS);
57                 if (!value)
58                         return ERR_PTR(-ENOMEM);
59                 size = __btrfs_getxattr(inode, name, value, size);
60                 if (size > 0) {
61                         acl = posix_acl_from_xattr(value, size);
62                         set_cached_acl(inode, type, acl);
63                 }
64                 kfree(value);
65         } else if (size == -ENOENT || size == -ENODATA || size == 0) {
66                 /* FIXME, who returns -ENOENT?  I think nobody */
67                 acl = NULL;
68                 set_cached_acl(inode, type, acl);
69         } else {
70                 acl = ERR_PTR(-EIO);
71         }
72
73         return acl;
74 }
75
76 static int btrfs_xattr_get_acl(struct inode *inode, int type,
77                                void *value, size_t size)
78 {
79         struct posix_acl *acl;
80         int ret = 0;
81
82         acl = btrfs_get_acl(inode, type);
83
84         if (IS_ERR(acl))
85                 return PTR_ERR(acl);
86         if (acl == NULL)
87                 return -ENODATA;
88         ret = posix_acl_to_xattr(acl, value, size);
89         posix_acl_release(acl);
90
91         return ret;
92 }
93
94 /*
95  * Needs to be called with fs_mutex held
96  */
97 static int btrfs_set_acl(struct btrfs_trans_handle *trans,
98                          struct inode *inode, struct posix_acl *acl, int type)
99 {
100         int ret, size = 0;
101         const char *name;
102         char *value = NULL;
103         mode_t mode;
104
105         if (acl) {
106                 ret = posix_acl_valid(acl);
107                 if (ret < 0)
108                         return ret;
109                 ret = 0;
110         }
111
112         switch (type) {
113         case ACL_TYPE_ACCESS:
114                 mode = inode->i_mode;
115                 name = POSIX_ACL_XATTR_ACCESS;
116                 if (acl) {
117                         ret = posix_acl_equiv_mode(acl, &mode);
118                         if (ret < 0)
119                                 return ret;
120                         inode->i_mode = mode;
121                 }
122                 ret = 0;
123                 break;
124         case ACL_TYPE_DEFAULT:
125                 if (!S_ISDIR(inode->i_mode))
126                         return acl ? -EINVAL : 0;
127                 name = POSIX_ACL_XATTR_DEFAULT;
128                 break;
129         default:
130                 return -EINVAL;
131         }
132
133         if (acl) {
134                 size = posix_acl_xattr_size(acl->a_count);
135                 value = kmalloc(size, GFP_NOFS);
136                 if (!value) {
137                         ret = -ENOMEM;
138                         goto out;
139                 }
140
141                 ret = posix_acl_to_xattr(acl, value, size);
142                 if (ret < 0)
143                         goto out;
144         }
145
146         ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
147 out:
148         kfree(value);
149
150         if (!ret)
151                 set_cached_acl(inode, type, acl);
152
153         return ret;
154 }
155
156 static int btrfs_xattr_set_acl(struct inode *inode, int type,
157                                const void *value, size_t size)
158 {
159         int ret;
160         struct posix_acl *acl = NULL;
161
162         if (!is_owner_or_cap(inode))
163                 return -EPERM;
164
165         if (value) {
166                 acl = posix_acl_from_xattr(value, size);
167                 if (acl == NULL) {
168                         value = NULL;
169                         size = 0;
170                 } else if (IS_ERR(acl)) {
171                         return PTR_ERR(acl);
172                 }
173         }
174
175         ret = btrfs_set_acl(NULL, inode, acl, type);
176
177         posix_acl_release(acl);
178
179         return ret;
180 }
181
182
183 static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
184                                       void *value, size_t size)
185 {
186         return btrfs_xattr_get_acl(inode, ACL_TYPE_ACCESS, value, size);
187 }
188
189 static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name,
190                                       const void *value, size_t size, int flags)
191 {
192         return btrfs_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
193 }
194
195 static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
196                                        void *value, size_t size)
197 {
198         return btrfs_xattr_get_acl(inode, ACL_TYPE_DEFAULT, value, size);
199 }
200
201 static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
202                                const void *value, size_t size, int flags)
203 {
204         return btrfs_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
205 }
206
207 int btrfs_check_acl(struct inode *inode, int mask)
208 {
209         struct posix_acl *acl;
210         int error = -EAGAIN;
211
212         acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
213
214         if (IS_ERR(acl))
215                 return PTR_ERR(acl);
216         if (acl) {
217                 error = posix_acl_permission(inode, acl, mask);
218                 posix_acl_release(acl);
219         }
220
221         return error;
222 }
223
224 /*
225  * btrfs_init_acl is already generally called under fs_mutex, so the locking
226  * stuff has been fixed to work with that.  If the locking stuff changes, we
227  * need to re-evaluate the acl locking stuff.
228  */
229 int btrfs_init_acl(struct btrfs_trans_handle *trans,
230                    struct inode *inode, struct inode *dir)
231 {
232         struct posix_acl *acl = NULL;
233         int ret = 0;
234
235         /* this happens with subvols */
236         if (!dir)
237                 return 0;
238
239         if (!S_ISLNK(inode->i_mode)) {
240                 if (IS_POSIXACL(dir)) {
241                         acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
242                         if (IS_ERR(acl))
243                                 return PTR_ERR(acl);
244                 }
245
246                 if (!acl)
247                         inode->i_mode &= ~current_umask();
248         }
249
250         if (IS_POSIXACL(dir) && acl) {
251                 struct posix_acl *clone;
252                 mode_t mode;
253
254                 if (S_ISDIR(inode->i_mode)) {
255                         ret = btrfs_set_acl(trans, inode, acl,
256                                             ACL_TYPE_DEFAULT);
257                         if (ret)
258                                 goto failed;
259                 }
260                 clone = posix_acl_clone(acl, GFP_NOFS);
261                 ret = -ENOMEM;
262                 if (!clone)
263                         goto failed;
264
265                 mode = inode->i_mode;
266                 ret = posix_acl_create_masq(clone, &mode);
267                 if (ret >= 0) {
268                         inode->i_mode = mode;
269                         if (ret > 0) {
270                                 /* we need an acl */
271                                 ret = btrfs_set_acl(trans, inode, clone,
272                                                     ACL_TYPE_ACCESS);
273                         }
274                 }
275         }
276 failed:
277         posix_acl_release(acl);
278
279         return ret;
280 }
281
282 int btrfs_acl_chmod(struct inode *inode)
283 {
284         struct posix_acl *acl, *clone;
285         int ret = 0;
286
287         if (S_ISLNK(inode->i_mode))
288                 return -EOPNOTSUPP;
289
290         if (!IS_POSIXACL(inode))
291                 return 0;
292
293         acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
294         if (IS_ERR(acl) || !acl)
295                 return PTR_ERR(acl);
296
297         clone = posix_acl_clone(acl, GFP_KERNEL);
298         posix_acl_release(acl);
299         if (!clone)
300                 return -ENOMEM;
301
302         ret = posix_acl_chmod_masq(clone, inode->i_mode);
303         if (!ret)
304                 ret = btrfs_set_acl(NULL, inode, clone, ACL_TYPE_ACCESS);
305
306         posix_acl_release(clone);
307
308         return ret;
309 }
310
311 struct xattr_handler btrfs_xattr_acl_default_handler = {
312         .prefix = POSIX_ACL_XATTR_DEFAULT,
313         .get    = btrfs_xattr_acl_default_get,
314         .set    = btrfs_xattr_acl_default_set,
315 };
316
317 struct xattr_handler btrfs_xattr_acl_access_handler = {
318         .prefix = POSIX_ACL_XATTR_ACCESS,
319         .get    = btrfs_xattr_acl_access_get,
320         .set    = btrfs_xattr_acl_access_set,
321 };
322
323 #else /* CONFIG_BTRFS_FS_POSIX_ACL */
324
325 int btrfs_acl_chmod(struct inode *inode)
326 {
327         return 0;
328 }
329
330 int btrfs_init_acl(struct btrfs_trans_handle *trans,
331                    struct inode *inode, struct inode *dir)
332 {
333         return 0;
334 }
335
336 #endif /* CONFIG_BTRFS_FS_POSIX_ACL */