2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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 v.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/posix_acl.h>
16 #include <linux/posix_acl_xattr.h>
17 #include <asm/semaphore.h>
18 #include <linux/gfs2_ondisk.h>
21 #include "lm_interface.h"
35 int gfs2_acl_validate_set(struct gfs2_inode *ip, int access,
36 struct gfs2_ea_request *er,
37 int *remove, mode_t *mode)
39 struct posix_acl *acl;
42 error = gfs2_acl_validate_remove(ip, access);
49 acl = posix_acl_from_xattr(er->er_data, er->er_data_len);
57 error = posix_acl_valid(acl);
62 error = posix_acl_equiv_mode(acl, mode);
70 posix_acl_release(acl);
75 int gfs2_acl_validate_remove(struct gfs2_inode *ip, int access)
77 if (!ip->i_sbd->sd_args.ar_posix_acl)
79 if (current->fsuid != ip->i_di.di_uid && !capable(CAP_FOWNER))
81 if (S_ISLNK(ip->i_di.di_mode))
83 if (!access && !S_ISDIR(ip->i_di.di_mode))
89 static int acl_get(struct gfs2_inode *ip, int access, struct posix_acl **acl,
90 struct gfs2_ea_location *el, char **data, unsigned int *len)
92 struct gfs2_ea_request er;
93 struct gfs2_ea_location el_this;
96 if (!ip->i_di.di_eattr)
99 memset(&er, 0, sizeof(struct gfs2_ea_request));
101 er.er_name = GFS2_POSIX_ACL_ACCESS;
102 er.er_name_len = GFS2_POSIX_ACL_ACCESS_LEN;
104 er.er_name = GFS2_POSIX_ACL_DEFAULT;
105 er.er_name_len = GFS2_POSIX_ACL_DEFAULT_LEN;
107 er.er_type = GFS2_EATYPE_SYS;
112 error = gfs2_ea_find(ip, &er, el);
117 if (!GFS2_EA_DATA_LEN(el->el_ea))
120 er.er_data_len = GFS2_EA_DATA_LEN(el->el_ea);
121 er.er_data = kmalloc(er.er_data_len, GFP_KERNEL);
126 error = gfs2_ea_get_copy(ip, el, er.er_data);
131 *acl = posix_acl_from_xattr(er.er_data, er.er_data_len);
133 error = PTR_ERR(*acl);
141 *len = er.er_data_len;
145 if (error || el == &el_this)
152 * gfs2_check_acl_locked - Check an ACL to see if we're allowed to do something
153 * @inode: the file we want to do something to
154 * @mask: what we want to do
159 int gfs2_check_acl_locked(struct inode *inode, int mask)
161 struct posix_acl *acl = NULL;
164 error = acl_get(inode->u.generic_ip, ACL_ACCESS, &acl, NULL, NULL, NULL);
169 error = posix_acl_permission(inode, acl, mask);
170 posix_acl_release(acl);
177 int gfs2_check_acl(struct inode *inode, int mask)
179 struct gfs2_inode *ip = inode->u.generic_ip;
180 struct gfs2_holder i_gh;
183 error = gfs2_glock_nq_init(ip->i_gl,
184 LM_ST_SHARED, LM_FLAG_ANY,
187 error = gfs2_check_acl_locked(inode, mask);
188 gfs2_glock_dq_uninit(&i_gh);
194 static int munge_mode(struct gfs2_inode *ip, mode_t mode)
196 struct gfs2_sbd *sdp = ip->i_sbd;
197 struct buffer_head *dibh;
200 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
204 error = gfs2_meta_inode_buffer(ip, &dibh);
206 gfs2_assert_withdraw(sdp,
207 (ip->i_di.di_mode & S_IFMT) == (mode & S_IFMT));
208 ip->i_di.di_mode = mode;
209 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
210 gfs2_dinode_out(&ip->i_di, dibh->b_data);
219 int gfs2_acl_create(struct gfs2_inode *dip, struct gfs2_inode *ip)
221 struct gfs2_sbd *sdp = dip->i_sbd;
222 struct posix_acl *acl = NULL, *clone;
223 struct gfs2_ea_request er;
224 mode_t mode = ip->i_di.di_mode;
227 if (!sdp->sd_args.ar_posix_acl)
229 if (S_ISLNK(ip->i_di.di_mode))
232 memset(&er, 0, sizeof(struct gfs2_ea_request));
233 er.er_type = GFS2_EATYPE_SYS;
235 error = acl_get(dip, ACL_DEFAULT, &acl, NULL,
236 &er.er_data, &er.er_data_len);
240 mode &= ~current->fs->umask;
241 if (mode != ip->i_di.di_mode)
242 error = munge_mode(ip, mode);
246 clone = posix_acl_clone(acl, GFP_KERNEL);
250 posix_acl_release(acl);
253 if (S_ISDIR(ip->i_di.di_mode)) {
254 er.er_name = GFS2_POSIX_ACL_DEFAULT;
255 er.er_name_len = GFS2_POSIX_ACL_DEFAULT_LEN;
256 error = gfs2_system_eaops.eo_set(ip, &er);
261 error = posix_acl_create_masq(acl, &mode);
265 er.er_name = GFS2_POSIX_ACL_ACCESS;
266 er.er_name_len = GFS2_POSIX_ACL_ACCESS_LEN;
267 posix_acl_to_xattr(acl, er.er_data, er.er_data_len);
269 er.er_flags = GFS2_ERF_MODE;
270 error = gfs2_system_eaops.eo_set(ip, &er);
274 munge_mode(ip, mode);
277 posix_acl_release(acl);
282 int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
284 struct posix_acl *acl = NULL, *clone;
285 struct gfs2_ea_location el;
290 error = acl_get(ip, ACL_ACCESS, &acl, &el, &data, &len);
294 return gfs2_setattr_simple(ip, attr);
296 clone = posix_acl_clone(acl, GFP_KERNEL);
300 posix_acl_release(acl);
303 error = posix_acl_chmod_masq(acl, attr->ia_mode);
305 posix_acl_to_xattr(acl, data, len);
306 error = gfs2_ea_acl_chmod(ip, &el, attr, data);
310 posix_acl_release(acl);