blob: bd0fce964c411e657093f6acd481328d94dc5090 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
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>
Steven Whitehouse2646a1f2009-10-02 11:50:54 +010015#include <linux/xattr.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000016#include <linux/posix_acl.h>
17#include <linux/posix_acl_xattr.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050018#include <linux/gfs2_ondisk.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000019
20#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "acl.h"
Steven Whitehouse307cf6e2009-08-26 18:51:04 +010023#include "xattr.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#include "glock.h"
25#include "inode.h"
26#include "meta_io.h"
27#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050028#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029
Steven Whitehouse479c4272009-10-02 12:00:00 +010030static const char *gfs2_acl_name(int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +000031{
Steven Whitehouse479c4272009-10-02 12:00:00 +010032 switch (type) {
33 case ACL_TYPE_ACCESS:
34 return GFS2_POSIX_ACL_ACCESS;
35 case ACL_TYPE_DEFAULT:
36 return GFS2_POSIX_ACL_DEFAULT;
37 }
38 return NULL;
39}
David Teiglandb3b94fa2006-01-16 16:50:04 +000040
Steven Whitehouse479c4272009-10-02 12:00:00 +010041static struct posix_acl *gfs2_acl_get(struct gfs2_inode *ip, int type)
42{
43 struct posix_acl *acl;
44 const char *name;
45 char *data;
46 int len;
Steven Whitehouse40b78a32009-08-26 18:41:32 +010047
Steven Whitehouse3767ac22008-11-03 14:28:42 +000048 if (!ip->i_eattr)
Steven Whitehouse479c4272009-10-02 12:00:00 +010049 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000050
Steven Whitehouse479c4272009-10-02 12:00:00 +010051 name = gfs2_acl_name(type);
52 if (name == NULL)
53 return ERR_PTR(-EINVAL);
David Teiglandb3b94fa2006-01-16 16:50:04 +000054
Steven Whitehouse479c4272009-10-02 12:00:00 +010055 len = gfs2_xattr_acl_get(ip, name, &data);
56 if (len < 0)
57 return ERR_PTR(len);
58 if (len == 0)
59 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000060
Steven Whitehouse479c4272009-10-02 12:00:00 +010061 acl = posix_acl_from_xattr(data, len);
62 kfree(data);
63 return acl;
David Teiglandb3b94fa2006-01-16 16:50:04 +000064}
65
66/**
Steven Whitehouse77386e12006-11-29 10:41:49 -050067 * gfs2_check_acl - Check an ACL to see if we're allowed to do something
David Teiglandb3b94fa2006-01-16 16:50:04 +000068 * @inode: the file we want to do something to
69 * @mask: what we want to do
70 *
71 * Returns: errno
72 */
73
Steven Whitehouse77386e12006-11-29 10:41:49 -050074int gfs2_check_acl(struct inode *inode, int mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +000075{
Steven Whitehouse479c4272009-10-02 12:00:00 +010076 struct posix_acl *acl;
David Teiglandb3b94fa2006-01-16 16:50:04 +000077 int error;
78
Steven Whitehouse479c4272009-10-02 12:00:00 +010079 acl = gfs2_acl_get(GFS2_I(inode), ACL_TYPE_ACCESS);
80 if (IS_ERR(acl))
81 return PTR_ERR(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +000082
83 if (acl) {
84 error = posix_acl_permission(inode, acl, mask);
85 posix_acl_release(acl);
86 return error;
87 }
88
89 return -EAGAIN;
90}
91
Steven Whitehouse69dca422009-09-29 12:40:19 +010092static int gfs2_set_mode(struct inode *inode, mode_t mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +000093{
Steven Whitehouse69dca422009-09-29 12:40:19 +010094 int error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +000095
Steven Whitehouse69dca422009-09-29 12:40:19 +010096 if (mode != inode->i_mode) {
97 struct iattr iattr;
David Teiglandb3b94fa2006-01-16 16:50:04 +000098
Steven Whitehouse69dca422009-09-29 12:40:19 +010099 iattr.ia_valid = ATTR_MODE;
100 iattr.ia_mode = mode;
101
102 error = gfs2_setattr_simple(GFS2_I(inode), &iattr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000103 }
104
Steven Whitehouse69dca422009-09-29 12:40:19 +0100105 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000106}
107
Steven Whitehouse479c4272009-10-02 12:00:00 +0100108static int gfs2_acl_set(struct inode *inode, int type, struct posix_acl *acl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110 int error;
Steven Whitehouse479c4272009-10-02 12:00:00 +0100111 int len;
112 char *data;
113 const char *name = gfs2_acl_name(type);
114
115 BUG_ON(name == NULL);
116 len = posix_acl_to_xattr(acl, NULL, 0);
117 if (len == 0)
118 return 0;
119 data = kmalloc(len, GFP_NOFS);
120 if (data == NULL)
121 return -ENOMEM;
122 error = posix_acl_to_xattr(acl, data, len);
123 if (error < 0)
124 goto out;
125 error = gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, data, len, 0);
126out:
127 kfree(data);
128 return error;
129}
130
131int gfs2_acl_create(struct gfs2_inode *dip, struct inode *inode)
132{
133 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
134 struct posix_acl *acl, *clone;
135 mode_t mode = inode->i_mode;
136 int error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137
138 if (!sdp->sd_args.ar_posix_acl)
139 return 0;
Steven Whitehouse479c4272009-10-02 12:00:00 +0100140 if (S_ISLNK(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141 return 0;
142
Steven Whitehouse479c4272009-10-02 12:00:00 +0100143 acl = gfs2_acl_get(dip, ACL_TYPE_DEFAULT);
144 if (IS_ERR(acl))
145 return PTR_ERR(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146 if (!acl) {
Al Viroce3b0f82009-03-29 19:08:22 -0400147 mode &= ~current_umask();
Steven Whitehouse479c4272009-10-02 12:00:00 +0100148 if (mode != inode->i_mode)
149 error = gfs2_set_mode(inode, mode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150 return error;
151 }
152
Steven Whitehouse479c4272009-10-02 12:00:00 +0100153 if (S_ISDIR(inode->i_mode)) {
154 error = gfs2_acl_set(inode, ACL_TYPE_DEFAULT, acl);
155 if (error)
156 goto out;
157 }
158
Josef Bacik16c5f062008-04-09 09:33:41 -0400159 clone = posix_acl_clone(acl, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160 error = -ENOMEM;
161 if (!clone)
162 goto out;
163 posix_acl_release(acl);
164 acl = clone;
165
David Teiglandb3b94fa2006-01-16 16:50:04 +0000166 error = posix_acl_create_masq(acl, &mode);
167 if (error < 0)
168 goto out;
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100169 if (error == 0)
170 goto munge;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171
Steven Whitehouse479c4272009-10-02 12:00:00 +0100172 error = gfs2_acl_set(inode, ACL_TYPE_ACCESS, acl);
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100173 if (error)
174 goto out;
175munge:
Steven Whitehouse479c4272009-10-02 12:00:00 +0100176 error = gfs2_set_mode(inode, mode);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400177out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000178 posix_acl_release(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000179 return error;
180}
181
182int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
183{
Steven Whitehouse479c4272009-10-02 12:00:00 +0100184 struct posix_acl *acl, *clone;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185 char *data;
186 unsigned int len;
187 int error;
188
Steven Whitehouse479c4272009-10-02 12:00:00 +0100189 acl = gfs2_acl_get(ip, ACL_TYPE_ACCESS);
190 if (IS_ERR(acl))
191 return PTR_ERR(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000192 if (!acl)
193 return gfs2_setattr_simple(ip, attr);
194
Josef Bacik16c5f062008-04-09 09:33:41 -0400195 clone = posix_acl_clone(acl, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000196 error = -ENOMEM;
197 if (!clone)
198 goto out;
199 posix_acl_release(acl);
200 acl = clone;
201
202 error = posix_acl_chmod_masq(acl, attr->ia_mode);
203 if (!error) {
Steven Whitehouse479c4272009-10-02 12:00:00 +0100204 len = posix_acl_to_xattr(acl, NULL, 0);
205 data = kmalloc(len, GFP_NOFS);
206 error = -ENOMEM;
207 if (data == NULL)
208 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000209 posix_acl_to_xattr(acl, data, len);
Steven Whitehouse479c4272009-10-02 12:00:00 +0100210 error = gfs2_xattr_acl_chmod(ip, attr, data);
211 kfree(data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212 }
213
Steven Whitehousea91ea692006-09-04 12:04:26 -0400214out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215 posix_acl_release(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216 return error;
217}
218
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100219static int gfs2_acl_type(const char *name)
220{
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;
225 return -EINVAL;
226}
227
228static int gfs2_xattr_system_get(struct inode *inode, const char *name,
229 void *buffer, size_t size)
230{
231 int type;
232
233 type = gfs2_acl_type(name);
234 if (type < 0)
235 return type;
236
237 return gfs2_xattr_get(inode, GFS2_EATYPE_SYS, name, buffer, size);
238}
239
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100240
241static int gfs2_xattr_system_set(struct inode *inode, const char *name,
242 const void *value, size_t size, int flags)
243{
244 struct gfs2_sbd *sdp = GFS2_SB(inode);
245 struct posix_acl *acl = NULL;
246 int error = 0, type;
247
248 if (!sdp->sd_args.ar_posix_acl)
249 return -EOPNOTSUPP;
250
251 type = gfs2_acl_type(name);
252 if (type < 0)
253 return type;
254 if (flags & XATTR_CREATE)
255 return -EINVAL;
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))
259 return -EPERM;
260 if (S_ISLNK(inode->i_mode))
261 return -EOPNOTSUPP;
262
263 if (!value)
264 goto set_acl;
265
266 acl = posix_acl_from_xattr(value, size);
267 if (!acl) {
268 /*
269 * acl_set_file(3) may request that we set default ACLs with
270 * zero length -- defend (gracefully) against that here.
271 */
272 goto out;
273 }
274 if (IS_ERR(acl)) {
275 error = PTR_ERR(acl);
276 goto out;
277 }
278
279 error = posix_acl_valid(acl);
280 if (error)
281 goto out_release;
282
283 error = -EINVAL;
284 if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
285 goto out_release;
286
287 if (type == ACL_TYPE_ACCESS) {
288 mode_t mode = inode->i_mode;
289 error = posix_acl_equiv_mode(acl, &mode);
290
291 if (error <= 0) {
292 posix_acl_release(acl);
293 acl = NULL;
294
295 if (error < 0)
296 return error;
297 }
298
299 error = gfs2_set_mode(inode, mode);
300 if (error)
301 goto out_release;
302 }
303
304set_acl:
305 error = gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, value, size, 0);
306out_release:
307 posix_acl_release(acl);
308out:
309 return error;
310}
311
312struct xattr_handler gfs2_xattr_system_handler = {
313 .prefix = XATTR_SYSTEM_PREFIX,
314 .get = gfs2_xattr_system_get,
315 .set = gfs2_xattr_system_set,
316};
317