ns proc: Add support for the uts namespace
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / gfs2 / acl.c
blobcbc07155b1a0a0e4a3d10fb47f00c7ed1cdfef8f
1 /*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
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 */
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>
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"
30 static const char *gfs2_acl_name(int type)
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;
38 return NULL;
41 static struct posix_acl *gfs2_acl_get(struct gfs2_inode *ip, int type)
43 struct posix_acl *acl;
44 const char *name;
45 char *data;
46 int len;
48 if (!ip->i_eattr)
49 return NULL;
51 acl = get_cached_acl(&ip->i_inode, type);
52 if (acl != ACL_NOT_CACHED)
53 return acl;
55 name = gfs2_acl_name(type);
56 if (name == NULL)
57 return ERR_PTR(-EINVAL);
59 len = gfs2_xattr_acl_get(ip, name, &data);
60 if (len < 0)
61 return ERR_PTR(len);
62 if (len == 0)
63 return NULL;
65 acl = posix_acl_from_xattr(data, len);
66 kfree(data);
67 return acl;
70 /**
71 * gfs2_check_acl - Check an ACL to see if we're allowed to do something
72 * @inode: the file we want to do something to
73 * @mask: what we want to do
75 * Returns: errno
78 int gfs2_check_acl(struct inode *inode, int mask, unsigned int flags)
80 struct posix_acl *acl;
81 int error;
83 if (flags & IPERM_FLAG_RCU) {
84 if (!negative_cached_acl(inode, ACL_TYPE_ACCESS))
85 return -ECHILD;
86 return -EAGAIN;
89 acl = gfs2_acl_get(GFS2_I(inode), ACL_TYPE_ACCESS);
90 if (IS_ERR(acl))
91 return PTR_ERR(acl);
93 if (acl) {
94 error = posix_acl_permission(inode, acl, mask);
95 posix_acl_release(acl);
96 return error;
99 return -EAGAIN;
102 static int gfs2_set_mode(struct inode *inode, mode_t mode)
104 int error = 0;
106 if (mode != inode->i_mode) {
107 struct iattr iattr;
109 iattr.ia_valid = ATTR_MODE;
110 iattr.ia_mode = mode;
112 error = gfs2_setattr_simple(GFS2_I(inode), &iattr);
115 return error;
118 static int gfs2_acl_set(struct inode *inode, int type, struct posix_acl *acl)
120 int error;
121 int len;
122 char *data;
123 const char *name = gfs2_acl_name(type);
125 BUG_ON(name == NULL);
126 len = posix_acl_to_xattr(acl, NULL, 0);
127 if (len == 0)
128 return 0;
129 data = kmalloc(len, GFP_NOFS);
130 if (data == NULL)
131 return -ENOMEM;
132 error = posix_acl_to_xattr(acl, data, len);
133 if (error < 0)
134 goto out;
135 error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
136 if (!error)
137 set_cached_acl(inode, type, acl);
138 out:
139 kfree(data);
140 return error;
143 int gfs2_acl_create(struct gfs2_inode *dip, struct inode *inode)
145 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
146 struct posix_acl *acl, *clone;
147 mode_t mode = inode->i_mode;
148 int error = 0;
150 if (!sdp->sd_args.ar_posix_acl)
151 return 0;
152 if (S_ISLNK(inode->i_mode))
153 return 0;
155 acl = gfs2_acl_get(dip, ACL_TYPE_DEFAULT);
156 if (IS_ERR(acl))
157 return PTR_ERR(acl);
158 if (!acl) {
159 mode &= ~current_umask();
160 if (mode != inode->i_mode)
161 error = gfs2_set_mode(inode, mode);
162 return error;
165 if (S_ISDIR(inode->i_mode)) {
166 error = gfs2_acl_set(inode, ACL_TYPE_DEFAULT, acl);
167 if (error)
168 goto out;
171 clone = posix_acl_clone(acl, GFP_NOFS);
172 error = -ENOMEM;
173 if (!clone)
174 goto out;
175 posix_acl_release(acl);
176 acl = clone;
178 error = posix_acl_create_masq(acl, &mode);
179 if (error < 0)
180 goto out;
181 if (error == 0)
182 goto munge;
184 error = gfs2_acl_set(inode, ACL_TYPE_ACCESS, acl);
185 if (error)
186 goto out;
187 munge:
188 error = gfs2_set_mode(inode, mode);
189 out:
190 posix_acl_release(acl);
191 return error;
194 int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
196 struct posix_acl *acl, *clone;
197 char *data;
198 unsigned int len;
199 int error;
201 acl = gfs2_acl_get(ip, ACL_TYPE_ACCESS);
202 if (IS_ERR(acl))
203 return PTR_ERR(acl);
204 if (!acl)
205 return gfs2_setattr_simple(ip, attr);
207 clone = posix_acl_clone(acl, GFP_NOFS);
208 error = -ENOMEM;
209 if (!clone)
210 goto out;
211 posix_acl_release(acl);
212 acl = clone;
214 error = posix_acl_chmod_masq(acl, attr->ia_mode);
215 if (!error) {
216 len = posix_acl_to_xattr(acl, NULL, 0);
217 data = kmalloc(len, GFP_NOFS);
218 error = -ENOMEM;
219 if (data == NULL)
220 goto out;
221 posix_acl_to_xattr(acl, data, len);
222 error = gfs2_xattr_acl_chmod(ip, attr, data);
223 kfree(data);
224 set_cached_acl(&ip->i_inode, ACL_TYPE_ACCESS, acl);
227 out:
228 posix_acl_release(acl);
229 return error;
232 static int gfs2_acl_type(const char *name)
234 if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0)
235 return ACL_TYPE_ACCESS;
236 if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0)
237 return ACL_TYPE_DEFAULT;
238 return -EINVAL;
241 static int gfs2_xattr_system_get(struct dentry *dentry, const char *name,
242 void *buffer, size_t size, int xtype)
244 struct inode *inode = dentry->d_inode;
245 struct gfs2_sbd *sdp = GFS2_SB(inode);
246 struct posix_acl *acl;
247 int type;
248 int error;
250 if (!sdp->sd_args.ar_posix_acl)
251 return -EOPNOTSUPP;
253 type = gfs2_acl_type(name);
254 if (type < 0)
255 return type;
257 acl = gfs2_acl_get(GFS2_I(inode), type);
258 if (IS_ERR(acl))
259 return PTR_ERR(acl);
260 if (acl == NULL)
261 return -ENODATA;
263 error = posix_acl_to_xattr(acl, buffer, size);
264 posix_acl_release(acl);
266 return error;
269 static int gfs2_xattr_system_set(struct dentry *dentry, const char *name,
270 const void *value, size_t size, int flags,
271 int xtype)
273 struct inode *inode = dentry->d_inode;
274 struct gfs2_sbd *sdp = GFS2_SB(inode);
275 struct posix_acl *acl = NULL;
276 int error = 0, type;
278 if (!sdp->sd_args.ar_posix_acl)
279 return -EOPNOTSUPP;
281 type = gfs2_acl_type(name);
282 if (type < 0)
283 return type;
284 if (flags & XATTR_CREATE)
285 return -EINVAL;
286 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
287 return value ? -EACCES : 0;
288 if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
289 return -EPERM;
290 if (S_ISLNK(inode->i_mode))
291 return -EOPNOTSUPP;
293 if (!value)
294 goto set_acl;
296 acl = posix_acl_from_xattr(value, size);
297 if (!acl) {
299 * acl_set_file(3) may request that we set default ACLs with
300 * zero length -- defend (gracefully) against that here.
302 goto out;
304 if (IS_ERR(acl)) {
305 error = PTR_ERR(acl);
306 goto out;
309 error = posix_acl_valid(acl);
310 if (error)
311 goto out_release;
313 error = -EINVAL;
314 if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
315 goto out_release;
317 if (type == ACL_TYPE_ACCESS) {
318 mode_t mode = inode->i_mode;
319 error = posix_acl_equiv_mode(acl, &mode);
321 if (error <= 0) {
322 posix_acl_release(acl);
323 acl = NULL;
325 if (error < 0)
326 return error;
329 error = gfs2_set_mode(inode, mode);
330 if (error)
331 goto out_release;
334 set_acl:
335 error = __gfs2_xattr_set(inode, name, value, size, 0, GFS2_EATYPE_SYS);
336 if (!error) {
337 if (acl)
338 set_cached_acl(inode, type, acl);
339 else
340 forget_cached_acl(inode, type);
342 out_release:
343 posix_acl_release(acl);
344 out:
345 return error;
348 const struct xattr_handler gfs2_xattr_system_handler = {
349 .prefix = XATTR_SYSTEM_PREFIX,
350 .flags = GFS2_EATYPE_SYS,
351 .get = gfs2_xattr_system_get,
352 .set = gfs2_xattr_system_set,