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.
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>
30 static const char *gfs2_acl_name(int type)
34 return GFS2_POSIX_ACL_ACCESS;
35 case ACL_TYPE_DEFAULT:
36 return GFS2_POSIX_ACL_DEFAULT;
41 static struct posix_acl *gfs2_acl_get(struct gfs2_inode *ip, int type)
43 struct posix_acl *acl;
51 name = gfs2_acl_name(type);
53 return ERR_PTR(-EINVAL);
55 len = gfs2_xattr_acl_get(ip, name, &data);
61 acl = posix_acl_from_xattr(data, len);
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
74 int gfs2_check_acl(struct inode *inode, int mask)
76 struct posix_acl *acl;
79 acl = gfs2_acl_get(GFS2_I(inode), ACL_TYPE_ACCESS);
84 error = posix_acl_permission(inode, acl, mask);
85 posix_acl_release(acl);
92 static int gfs2_set_mode(struct inode *inode, mode_t mode)
96 if (mode != inode->i_mode) {
99 iattr.ia_valid = ATTR_MODE;
100 iattr.ia_mode = mode;
102 error = gfs2_setattr_simple(GFS2_I(inode), &iattr);
108 static int gfs2_acl_set(struct inode *inode, int type, struct posix_acl *acl)
113 const char *name = gfs2_acl_name(type);
115 BUG_ON(name == NULL);
116 len = posix_acl_to_xattr(acl, NULL, 0);
119 data = kmalloc(len, GFP_NOFS);
122 error = posix_acl_to_xattr(acl, data, len);
125 error = gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, data, len, 0);
131 int gfs2_acl_create(struct gfs2_inode *dip, struct inode *inode)
133 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
134 struct posix_acl *acl, *clone;
135 mode_t mode = inode->i_mode;
138 if (!sdp->sd_args.ar_posix_acl)
140 if (S_ISLNK(inode->i_mode))
143 acl = gfs2_acl_get(dip, ACL_TYPE_DEFAULT);
147 mode &= ~current_umask();
148 if (mode != inode->i_mode)
149 error = gfs2_set_mode(inode, mode);
153 if (S_ISDIR(inode->i_mode)) {
154 error = gfs2_acl_set(inode, ACL_TYPE_DEFAULT, acl);
159 clone = posix_acl_clone(acl, GFP_NOFS);
163 posix_acl_release(acl);
166 error = posix_acl_create_masq(acl, &mode);
172 error = gfs2_acl_set(inode, ACL_TYPE_ACCESS, acl);
176 error = gfs2_set_mode(inode, mode);
178 posix_acl_release(acl);
182 int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
184 struct posix_acl *acl, *clone;
189 acl = gfs2_acl_get(ip, ACL_TYPE_ACCESS);
193 return gfs2_setattr_simple(ip, attr);
195 clone = posix_acl_clone(acl, GFP_NOFS);
199 posix_acl_release(acl);
202 error = posix_acl_chmod_masq(acl, attr->ia_mode);
204 len = posix_acl_to_xattr(acl, NULL, 0);
205 data = kmalloc(len, GFP_NOFS);
209 posix_acl_to_xattr(acl, data, len);
210 error = gfs2_xattr_acl_chmod(ip, attr, data);
215 posix_acl_release(acl);
219 static int gfs2_acl_type(const char *name)
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;
228 static int gfs2_xattr_system_get(struct inode *inode, const char *name,
229 void *buffer, size_t size)
233 type = gfs2_acl_type(name);
237 return gfs2_xattr_get(inode, GFS2_EATYPE_SYS, name, buffer, size);
241 static int gfs2_xattr_system_set(struct inode *inode, const char *name,
242 const void *value, size_t size, int flags)
244 struct gfs2_sbd *sdp = GFS2_SB(inode);
245 struct posix_acl *acl = NULL;
248 if (!sdp->sd_args.ar_posix_acl)
251 type = gfs2_acl_type(name);
254 if (flags & XATTR_CREATE)
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))
260 if (S_ISLNK(inode->i_mode))
266 acl = posix_acl_from_xattr(value, size);
269 * acl_set_file(3) may request that we set default ACLs with
270 * zero length -- defend (gracefully) against that here.
275 error = PTR_ERR(acl);
279 error = posix_acl_valid(acl);
284 if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
287 if (type == ACL_TYPE_ACCESS) {
288 mode_t mode = inode->i_mode;
289 error = posix_acl_equiv_mode(acl, &mode);
292 posix_acl_release(acl);
299 error = gfs2_set_mode(inode, mode);
305 error = gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, value, size, 0);
307 posix_acl_release(acl);
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,