bd0fce964c411e657093f6acd481328d94dc5090
[safe/jmp/linux-2.6] / fs / gfs2 / acl.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4  *
5  * This copyrighted material is made available to anyone wishing to use,
6  * modify, copy, or redistribute it subject to the terms and conditions
7  * of the GNU General Public License version 2.
8  */
9
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/xattr.h>
16 #include <linux/posix_acl.h>
17 #include <linux/posix_acl_xattr.h>
18 #include <linux/gfs2_ondisk.h>
19
20 #include "gfs2.h"
21 #include "incore.h"
22 #include "acl.h"
23 #include "xattr.h"
24 #include "glock.h"
25 #include "inode.h"
26 #include "meta_io.h"
27 #include "trans.h"
28 #include "util.h"
29
30 static const char *gfs2_acl_name(int type)
31 {
32         switch (type) {
33         case ACL_TYPE_ACCESS:
34                 return GFS2_POSIX_ACL_ACCESS;
35         case ACL_TYPE_DEFAULT:
36                 return GFS2_POSIX_ACL_DEFAULT;
37         }
38         return NULL;
39 }
40
41 static struct posix_acl *gfs2_acl_get(struct gfs2_inode *ip, int type)
42 {
43         struct posix_acl *acl;
44         const char *name;
45         char *data;
46         int len;
47
48         if (!ip->i_eattr)
49                 return NULL;
50
51         name = gfs2_acl_name(type);
52         if (name == NULL)
53                 return ERR_PTR(-EINVAL);
54
55         len = gfs2_xattr_acl_get(ip, name, &data);
56         if (len < 0)
57                 return ERR_PTR(len);
58         if (len == 0)
59                 return NULL;
60
61         acl = posix_acl_from_xattr(data, len);
62         kfree(data);
63         return acl;
64 }
65
66 /**
67  * gfs2_check_acl - Check an ACL to see if we're allowed to do something
68  * @inode: the file we want to do something to
69  * @mask: what we want to do
70  *
71  * Returns: errno
72  */
73
74 int gfs2_check_acl(struct inode *inode, int mask)
75 {
76         struct posix_acl *acl;
77         int error;
78
79         acl = gfs2_acl_get(GFS2_I(inode), ACL_TYPE_ACCESS);
80         if (IS_ERR(acl))
81                 return PTR_ERR(acl);
82
83         if (acl) {
84                 error = posix_acl_permission(inode, acl, mask);
85                 posix_acl_release(acl);
86                 return error;
87         }
88
89         return -EAGAIN;
90 }
91
92 static int gfs2_set_mode(struct inode *inode, mode_t mode)
93 {
94         int error = 0;
95
96         if (mode != inode->i_mode) {
97                 struct iattr iattr;
98
99                 iattr.ia_valid = ATTR_MODE;
100                 iattr.ia_mode = mode;
101
102                 error = gfs2_setattr_simple(GFS2_I(inode), &iattr);
103         }
104
105         return error;
106 }
107
108 static int gfs2_acl_set(struct inode *inode, int type, struct posix_acl *acl)
109 {
110         int error;
111         int len;
112         char *data;
113         const char *name = gfs2_acl_name(type);
114
115         BUG_ON(name == NULL);
116         len = posix_acl_to_xattr(acl, NULL, 0);
117         if (len == 0)
118                 return 0;
119         data = kmalloc(len, GFP_NOFS);
120         if (data == NULL)
121                 return -ENOMEM;
122         error = posix_acl_to_xattr(acl, data, len);
123         if (error < 0)
124                 goto out;
125         error = gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, data, len, 0);
126 out:
127         kfree(data);
128         return error;
129 }
130
131 int gfs2_acl_create(struct gfs2_inode *dip, struct inode *inode)
132 {
133         struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
134         struct posix_acl *acl, *clone;
135         mode_t mode = inode->i_mode;
136         int error = 0;
137
138         if (!sdp->sd_args.ar_posix_acl)
139                 return 0;
140         if (S_ISLNK(inode->i_mode))
141                 return 0;
142
143         acl = gfs2_acl_get(dip, ACL_TYPE_DEFAULT);
144         if (IS_ERR(acl))
145                 return PTR_ERR(acl);
146         if (!acl) {
147                 mode &= ~current_umask();
148                 if (mode != inode->i_mode)
149                         error = gfs2_set_mode(inode, mode);
150                 return error;
151         }
152
153         if (S_ISDIR(inode->i_mode)) {
154                 error = gfs2_acl_set(inode, ACL_TYPE_DEFAULT, acl);
155                 if (error)
156                         goto out;
157         }
158
159         clone = posix_acl_clone(acl, GFP_NOFS);
160         error = -ENOMEM;
161         if (!clone)
162                 goto out;
163         posix_acl_release(acl);
164         acl = clone;
165
166         error = posix_acl_create_masq(acl, &mode);
167         if (error < 0)
168                 goto out;
169         if (error == 0)
170                 goto munge;
171
172         error = gfs2_acl_set(inode, ACL_TYPE_ACCESS, acl);
173         if (error)
174                 goto out;
175 munge:
176         error = gfs2_set_mode(inode, mode);
177 out:
178         posix_acl_release(acl);
179         return error;
180 }
181
182 int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
183 {
184         struct posix_acl *acl, *clone;
185         char *data;
186         unsigned int len;
187         int error;
188
189         acl = gfs2_acl_get(ip, ACL_TYPE_ACCESS);
190         if (IS_ERR(acl))
191                 return PTR_ERR(acl);
192         if (!acl)
193                 return gfs2_setattr_simple(ip, attr);
194
195         clone = posix_acl_clone(acl, GFP_NOFS);
196         error = -ENOMEM;
197         if (!clone)
198                 goto out;
199         posix_acl_release(acl);
200         acl = clone;
201
202         error = posix_acl_chmod_masq(acl, attr->ia_mode);
203         if (!error) {
204                 len = posix_acl_to_xattr(acl, NULL, 0);
205                 data = kmalloc(len, GFP_NOFS);
206                 error = -ENOMEM;
207                 if (data == NULL)
208                         goto out;
209                 posix_acl_to_xattr(acl, data, len);
210                 error = gfs2_xattr_acl_chmod(ip, attr, data);
211                 kfree(data);
212         }
213
214 out:
215         posix_acl_release(acl);
216         return error;
217 }
218
219 static int gfs2_acl_type(const char *name)
220 {
221         if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0)
222                 return ACL_TYPE_ACCESS;
223         if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0)
224                 return ACL_TYPE_DEFAULT;
225         return -EINVAL;
226 }
227
228 static int gfs2_xattr_system_get(struct inode *inode, const char *name,
229                                  void *buffer, size_t size)
230 {
231         int type;
232
233         type = gfs2_acl_type(name);
234         if (type < 0)
235                 return type;
236
237         return gfs2_xattr_get(inode, GFS2_EATYPE_SYS, name, buffer, size);
238 }
239
240
241 static int gfs2_xattr_system_set(struct inode *inode, const char *name,
242                                  const void *value, size_t size, int flags)
243 {
244         struct gfs2_sbd *sdp = GFS2_SB(inode);
245         struct posix_acl *acl = NULL;
246         int error = 0, type;
247
248         if (!sdp->sd_args.ar_posix_acl)
249                 return -EOPNOTSUPP;
250
251         type = gfs2_acl_type(name);
252         if (type < 0)
253                 return type;
254         if (flags & XATTR_CREATE)
255                 return -EINVAL;
256         if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
257                 return value ? -EACCES : 0;
258         if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
259                 return -EPERM;
260         if (S_ISLNK(inode->i_mode))
261                 return -EOPNOTSUPP;
262
263         if (!value)
264                 goto set_acl;
265
266         acl = posix_acl_from_xattr(value, size);
267         if (!acl) {
268                 /*
269                  * acl_set_file(3) may request that we set default ACLs with
270                  * zero length -- defend (gracefully) against that here.
271                  */
272                 goto out;
273         }
274         if (IS_ERR(acl)) {
275                 error = PTR_ERR(acl);
276                 goto out;
277         }
278
279         error = posix_acl_valid(acl);
280         if (error)
281                 goto out_release;
282
283         error = -EINVAL;
284         if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
285                 goto out_release;
286
287         if (type == ACL_TYPE_ACCESS) {
288                 mode_t mode = inode->i_mode;
289                 error = posix_acl_equiv_mode(acl, &mode);
290
291                 if (error <= 0) {
292                         posix_acl_release(acl);
293                         acl = NULL;
294
295                         if (error < 0)
296                                 return error;
297                 }
298
299                 error = gfs2_set_mode(inode, mode);
300                 if (error)
301                         goto out_release;
302         }
303
304 set_acl:
305         error = gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, value, size, 0);
306 out_release:
307         posix_acl_release(acl);
308 out:
309         return error;
310 }
311
312 struct xattr_handler gfs2_xattr_system_handler = {
313         .prefix = XATTR_SYSTEM_PREFIX,
314         .get    = gfs2_xattr_system_get,
315         .set    = gfs2_xattr_system_set,
316 };
317