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 int acl_get(struct gfs2_inode *ip, const char *name,
31 struct posix_acl **acl, struct gfs2_ea_location *el,
32 char **datap, unsigned int *lenp)
43 error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, el);
48 if (!GFS2_EA_DATA_LEN(el->el_ea))
51 len = GFS2_EA_DATA_LEN(el->el_ea);
52 data = kmalloc(len, GFP_NOFS);
57 error = gfs2_ea_get_copy(ip, el, data, len);
63 *acl = posix_acl_from_xattr(data, len);
65 error = PTR_ERR(*acl);
69 if (error || !datap) {
80 * gfs2_check_acl - Check an ACL to see if we're allowed to do something
81 * @inode: the file we want to do something to
82 * @mask: what we want to do
87 int gfs2_check_acl(struct inode *inode, int mask)
89 struct gfs2_ea_location el;
90 struct posix_acl *acl = NULL;
93 error = acl_get(GFS2_I(inode), GFS2_POSIX_ACL_ACCESS, &acl, &el, NULL, NULL);
99 error = posix_acl_permission(inode, acl, mask);
100 posix_acl_release(acl);
107 static int gfs2_set_mode(struct inode *inode, mode_t mode)
111 if (mode != inode->i_mode) {
114 iattr.ia_valid = ATTR_MODE;
115 iattr.ia_mode = mode;
117 error = gfs2_setattr_simple(GFS2_I(inode), &iattr);
123 int gfs2_acl_create(struct gfs2_inode *dip, struct gfs2_inode *ip)
125 struct gfs2_ea_location el;
126 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
127 struct posix_acl *acl = NULL, *clone;
128 mode_t mode = ip->i_inode.i_mode;
133 if (!sdp->sd_args.ar_posix_acl)
135 if (S_ISLNK(ip->i_inode.i_mode))
138 error = acl_get(dip, GFS2_POSIX_ACL_DEFAULT, &acl, &el, &data, &len);
143 mode &= ~current_umask();
144 if (mode != ip->i_inode.i_mode)
145 error = gfs2_set_mode(&ip->i_inode, mode);
149 clone = posix_acl_clone(acl, GFP_NOFS);
153 posix_acl_release(acl);
156 if (S_ISDIR(ip->i_inode.i_mode)) {
157 error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS,
158 GFS2_POSIX_ACL_DEFAULT, data, len, 0);
163 error = posix_acl_create_masq(acl, &mode);
169 posix_acl_to_xattr(acl, data, len);
170 error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS,
171 GFS2_POSIX_ACL_ACCESS, data, len, 0);
175 error = gfs2_set_mode(&ip->i_inode, mode);
177 posix_acl_release(acl);
182 int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
184 struct posix_acl *acl = NULL, *clone;
185 struct gfs2_ea_location el;
190 error = acl_get(ip, GFS2_POSIX_ACL_ACCESS, &acl, &el, &data, &len);
194 return gfs2_setattr_simple(ip, attr);
196 clone = posix_acl_clone(acl, GFP_NOFS);
200 posix_acl_release(acl);
203 error = posix_acl_chmod_masq(acl, attr->ia_mode);
205 posix_acl_to_xattr(acl, data, len);
206 error = gfs2_ea_acl_chmod(ip, &el, attr, data);
210 posix_acl_release(acl);
217 static int gfs2_acl_type(const char *name)
219 if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0)
220 return ACL_TYPE_ACCESS;
221 if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0)
222 return ACL_TYPE_DEFAULT;
226 static int gfs2_xattr_system_get(struct inode *inode, const char *name,
227 void *buffer, size_t size)
231 type = gfs2_acl_type(name);
235 return gfs2_xattr_get(inode, GFS2_EATYPE_SYS, name, buffer, size);
239 static int gfs2_xattr_system_set(struct inode *inode, const char *name,
240 const void *value, size_t size, int flags)
242 struct gfs2_sbd *sdp = GFS2_SB(inode);
243 struct posix_acl *acl = NULL;
246 if (!sdp->sd_args.ar_posix_acl)
249 type = gfs2_acl_type(name);
252 if (flags & XATTR_CREATE)
254 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
255 return value ? -EACCES : 0;
256 if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
258 if (S_ISLNK(inode->i_mode))
264 acl = posix_acl_from_xattr(value, size);
267 * acl_set_file(3) may request that we set default ACLs with
268 * zero length -- defend (gracefully) against that here.
273 error = PTR_ERR(acl);
277 error = posix_acl_valid(acl);
282 if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
285 if (type == ACL_TYPE_ACCESS) {
286 mode_t mode = inode->i_mode;
287 error = posix_acl_equiv_mode(acl, &mode);
290 posix_acl_release(acl);
297 error = gfs2_set_mode(inode, mode);
303 error = gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, value, size, 0);
305 posix_acl_release(acl);
310 struct xattr_handler gfs2_xattr_system_handler = {
311 .prefix = XATTR_SYSTEM_PREFIX,
312 .get = gfs2_xattr_system_get,
313 .set = gfs2_xattr_system_set,