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/posix_acl.h>
16 #include <linux/posix_acl_xattr.h>
17 #include <linux/gfs2_ondisk.h>
33 int gfs2_acl_validate_set(struct gfs2_inode
*ip
, int access
,
34 struct gfs2_ea_request
*er
,
35 int *remove
, mode_t
*mode
)
37 struct posix_acl
*acl
;
40 error
= gfs2_acl_validate_remove(ip
, access
);
47 acl
= posix_acl_from_xattr(er
->er_data
, er
->er_data_len
);
55 error
= posix_acl_valid(acl
);
60 error
= posix_acl_equiv_mode(acl
, mode
);
68 posix_acl_release(acl
);
72 int gfs2_acl_validate_remove(struct gfs2_inode
*ip
, int access
)
74 if (!GFS2_SB(&ip
->i_inode
)->sd_args
.ar_posix_acl
)
76 if (!is_owner_or_cap(&ip
->i_inode
))
78 if (S_ISLNK(ip
->i_inode
.i_mode
))
80 if (!access
&& !S_ISDIR(ip
->i_inode
.i_mode
))
86 static int acl_get(struct gfs2_inode
*ip
, int access
, struct posix_acl
**acl
,
87 struct gfs2_ea_location
*el
, char **data
, unsigned int *len
)
89 struct gfs2_ea_request er
;
90 struct gfs2_ea_location el_this
;
96 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
98 er
.er_name
= GFS2_POSIX_ACL_ACCESS
;
99 er
.er_name_len
= GFS2_POSIX_ACL_ACCESS_LEN
;
101 er
.er_name
= GFS2_POSIX_ACL_DEFAULT
;
102 er
.er_name_len
= GFS2_POSIX_ACL_DEFAULT_LEN
;
104 er
.er_type
= GFS2_EATYPE_SYS
;
109 error
= gfs2_ea_find(ip
, &er
, el
);
114 if (!GFS2_EA_DATA_LEN(el
->el_ea
))
117 er
.er_data_len
= GFS2_EA_DATA_LEN(el
->el_ea
);
118 er
.er_data
= kmalloc(er
.er_data_len
, GFP_NOFS
);
123 error
= gfs2_ea_get_copy(ip
, el
, er
.er_data
);
128 *acl
= posix_acl_from_xattr(er
.er_data
, er
.er_data_len
);
130 error
= PTR_ERR(*acl
);
138 *len
= er
.er_data_len
;
141 if (error
|| el
== &el_this
)
147 * gfs2_check_acl - Check an ACL to see if we're allowed to do something
148 * @inode: the file we want to do something to
149 * @mask: what we want to do
154 int gfs2_check_acl(struct inode
*inode
, int mask
)
156 struct posix_acl
*acl
= NULL
;
159 error
= acl_get(GFS2_I(inode
), ACL_ACCESS
, &acl
, NULL
, NULL
, NULL
);
164 error
= posix_acl_permission(inode
, acl
, mask
);
165 posix_acl_release(acl
);
172 static int munge_mode(struct gfs2_inode
*ip
, mode_t mode
)
174 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
175 struct buffer_head
*dibh
;
178 error
= gfs2_trans_begin(sdp
, RES_DINODE
, 0);
182 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
184 gfs2_assert_withdraw(sdp
,
185 (ip
->i_inode
.i_mode
& S_IFMT
) == (mode
& S_IFMT
));
186 ip
->i_inode
.i_mode
= mode
;
187 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
188 gfs2_dinode_out(ip
, dibh
->b_data
);
197 int gfs2_acl_create(struct gfs2_inode
*dip
, struct gfs2_inode
*ip
)
199 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
200 struct posix_acl
*acl
= NULL
, *clone
;
201 struct gfs2_ea_request er
;
202 mode_t mode
= ip
->i_inode
.i_mode
;
205 if (!sdp
->sd_args
.ar_posix_acl
)
207 if (S_ISLNK(ip
->i_inode
.i_mode
))
210 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
211 er
.er_type
= GFS2_EATYPE_SYS
;
213 error
= acl_get(dip
, ACL_DEFAULT
, &acl
, NULL
,
214 &er
.er_data
, &er
.er_data_len
);
218 mode
&= ~current
->fs
->umask
;
219 if (mode
!= ip
->i_inode
.i_mode
)
220 error
= munge_mode(ip
, mode
);
224 clone
= posix_acl_clone(acl
, GFP_NOFS
);
228 posix_acl_release(acl
);
231 if (S_ISDIR(ip
->i_inode
.i_mode
)) {
232 er
.er_name
= GFS2_POSIX_ACL_DEFAULT
;
233 er
.er_name_len
= GFS2_POSIX_ACL_DEFAULT_LEN
;
234 error
= gfs2_system_eaops
.eo_set(ip
, &er
);
239 error
= posix_acl_create_masq(acl
, &mode
);
243 er
.er_name
= GFS2_POSIX_ACL_ACCESS
;
244 er
.er_name_len
= GFS2_POSIX_ACL_ACCESS_LEN
;
245 posix_acl_to_xattr(acl
, er
.er_data
, er
.er_data_len
);
247 er
.er_flags
= GFS2_ERF_MODE
;
248 error
= gfs2_system_eaops
.eo_set(ip
, &er
);
252 munge_mode(ip
, mode
);
255 posix_acl_release(acl
);
260 int gfs2_acl_chmod(struct gfs2_inode
*ip
, struct iattr
*attr
)
262 struct posix_acl
*acl
= NULL
, *clone
;
263 struct gfs2_ea_location el
;
268 error
= acl_get(ip
, ACL_ACCESS
, &acl
, &el
, &data
, &len
);
272 return gfs2_setattr_simple(ip
, attr
);
274 clone
= posix_acl_clone(acl
, GFP_NOFS
);
278 posix_acl_release(acl
);
281 error
= posix_acl_chmod_masq(acl
, attr
->ia_mode
);
283 posix_acl_to_xattr(acl
, data
, len
);
284 error
= gfs2_ea_acl_chmod(ip
, &el
, attr
, data
);
288 posix_acl_release(acl
);