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