blob: 1be314837d3141d61810891e308af26361a6b472 [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 Whitehouse40b78a32009-08-26 18:41:32 +010030static 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)
David Teiglandb3b94fa2006-01-16 16:50:04 +000033{
Steven Whitehouse40b78a32009-08-26 18:41:32 +010034 char *data;
35 unsigned int len;
David Teiglandb3b94fa2006-01-16 16:50:04 +000036 int error;
37
Steven Whitehouse40b78a32009-08-26 18:41:32 +010038 el->el_bh = NULL;
39
Steven Whitehouse3767ac22008-11-03 14:28:42 +000040 if (!ip->i_eattr)
David Teiglandb3b94fa2006-01-16 16:50:04 +000041 return 0;
42
Steven Whitehouse40b78a32009-08-26 18:41:32 +010043 error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, el);
David Teiglandb3b94fa2006-01-16 16:50:04 +000044 if (error)
45 return error;
46 if (!el->el_ea)
47 return 0;
48 if (!GFS2_EA_DATA_LEN(el->el_ea))
49 goto out;
50
Steven Whitehouse40b78a32009-08-26 18:41:32 +010051 len = GFS2_EA_DATA_LEN(el->el_ea);
52 data = kmalloc(len, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +000053 error = -ENOMEM;
Steven Whitehouse40b78a32009-08-26 18:41:32 +010054 if (!data)
David Teiglandb3b94fa2006-01-16 16:50:04 +000055 goto out;
56
Steven Whitehouse40b78a32009-08-26 18:41:32 +010057 error = gfs2_ea_get_copy(ip, el, data, len);
58 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +000059 goto out_kfree;
Steven Whitehouse40b78a32009-08-26 18:41:32 +010060 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +000061
62 if (acl) {
Steven Whitehouse40b78a32009-08-26 18:41:32 +010063 *acl = posix_acl_from_xattr(data, len);
David Teiglandb3b94fa2006-01-16 16:50:04 +000064 if (IS_ERR(*acl))
65 error = PTR_ERR(*acl);
66 }
67
Steven Whitehousea91ea692006-09-04 12:04:26 -040068out_kfree:
Steven Whitehouse40b78a32009-08-26 18:41:32 +010069 if (error || !datap) {
70 kfree(data);
71 } else {
72 *datap = data;
73 *lenp = len;
David Teiglandb3b94fa2006-01-16 16:50:04 +000074 }
Steven Whitehousea91ea692006-09-04 12:04:26 -040075out:
David Teiglandb3b94fa2006-01-16 16:50:04 +000076 return error;
77}
78
79/**
Steven Whitehouse77386e12006-11-29 10:41:49 -050080 * gfs2_check_acl - Check an ACL to see if we're allowed to do something
David Teiglandb3b94fa2006-01-16 16:50:04 +000081 * @inode: the file we want to do something to
82 * @mask: what we want to do
83 *
84 * Returns: errno
85 */
86
Steven Whitehouse77386e12006-11-29 10:41:49 -050087int gfs2_check_acl(struct inode *inode, int mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +000088{
Steven Whitehouse40b78a32009-08-26 18:41:32 +010089 struct gfs2_ea_location el;
David Teiglandb3b94fa2006-01-16 16:50:04 +000090 struct posix_acl *acl = NULL;
91 int error;
92
Steven Whitehouse40b78a32009-08-26 18:41:32 +010093 error = acl_get(GFS2_I(inode), GFS2_POSIX_ACL_ACCESS, &acl, &el, NULL, NULL);
94 brelse(el.el_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +000095 if (error)
96 return error;
97
98 if (acl) {
99 error = posix_acl_permission(inode, acl, mask);
100 posix_acl_release(acl);
101 return error;
102 }
103
104 return -EAGAIN;
105}
106
Steven Whitehouse69dca422009-09-29 12:40:19 +0100107static int gfs2_set_mode(struct inode *inode, mode_t mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108{
Steven Whitehouse69dca422009-09-29 12:40:19 +0100109 int error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110
Steven Whitehouse69dca422009-09-29 12:40:19 +0100111 if (mode != inode->i_mode) {
112 struct iattr iattr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000113
Steven Whitehouse69dca422009-09-29 12:40:19 +0100114 iattr.ia_valid = ATTR_MODE;
115 iattr.ia_mode = mode;
116
117 error = gfs2_setattr_simple(GFS2_I(inode), &iattr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118 }
119
Steven Whitehouse69dca422009-09-29 12:40:19 +0100120 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121}
122
123int gfs2_acl_create(struct gfs2_inode *dip, struct gfs2_inode *ip)
124{
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100125 struct gfs2_ea_location el;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400126 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127 struct posix_acl *acl = NULL, *clone;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500128 mode_t mode = ip->i_inode.i_mode;
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100129 char *data = NULL;
130 unsigned int len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000131 int error;
132
133 if (!sdp->sd_args.ar_posix_acl)
134 return 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500135 if (S_ISLNK(ip->i_inode.i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000136 return 0;
137
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100138 error = acl_get(dip, GFS2_POSIX_ACL_DEFAULT, &acl, &el, &data, &len);
139 brelse(el.el_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140 if (error)
141 return error;
142 if (!acl) {
Al Viroce3b0f82009-03-29 19:08:22 -0400143 mode &= ~current_umask();
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500144 if (mode != ip->i_inode.i_mode)
Steven Whitehouse69dca422009-09-29 12:40:19 +0100145 error = gfs2_set_mode(&ip->i_inode, mode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146 return error;
147 }
148
Josef Bacik16c5f062008-04-09 09:33:41 -0400149 clone = posix_acl_clone(acl, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150 error = -ENOMEM;
151 if (!clone)
152 goto out;
153 posix_acl_release(acl);
154 acl = clone;
155
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500156 if (S_ISDIR(ip->i_inode.i_mode)) {
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100157 error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS,
158 GFS2_POSIX_ACL_DEFAULT, data, len, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000159 if (error)
160 goto out;
161 }
162
163 error = posix_acl_create_masq(acl, &mode);
164 if (error < 0)
165 goto out;
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100166 if (error == 0)
167 goto munge;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000168
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100169 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);
172 if (error)
173 goto out;
174munge:
Steven Whitehouse69dca422009-09-29 12:40:19 +0100175 error = gfs2_set_mode(&ip->i_inode, mode);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400176out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177 posix_acl_release(acl);
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100178 kfree(data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000179 return error;
180}
181
182int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
183{
184 struct posix_acl *acl = NULL, *clone;
185 struct gfs2_ea_location el;
186 char *data;
187 unsigned int len;
188 int error;
189
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100190 error = acl_get(ip, GFS2_POSIX_ACL_ACCESS, &acl, &el, &data, &len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191 if (error)
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100192 goto out_brelse;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000193 if (!acl)
194 return gfs2_setattr_simple(ip, attr);
195
Josef Bacik16c5f062008-04-09 09:33:41 -0400196 clone = posix_acl_clone(acl, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000197 error = -ENOMEM;
198 if (!clone)
199 goto out;
200 posix_acl_release(acl);
201 acl = clone;
202
203 error = posix_acl_chmod_masq(acl, attr->ia_mode);
204 if (!error) {
205 posix_acl_to_xattr(acl, data, len);
206 error = gfs2_ea_acl_chmod(ip, &el, attr, data);
207 }
208
Steven Whitehousea91ea692006-09-04 12:04:26 -0400209out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000210 posix_acl_release(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000211 kfree(data);
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100212out_brelse:
213 brelse(el.el_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000214 return error;
215}
216
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100217static int gfs2_acl_type(const char *name)
218{
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;
223 return -EINVAL;
224}
225
226static int gfs2_xattr_system_get(struct inode *inode, const char *name,
227 void *buffer, size_t size)
228{
229 int type;
230
231 type = gfs2_acl_type(name);
232 if (type < 0)
233 return type;
234
235 return gfs2_xattr_get(inode, GFS2_EATYPE_SYS, name, buffer, size);
236}
237
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100238
239static int gfs2_xattr_system_set(struct inode *inode, const char *name,
240 const void *value, size_t size, int flags)
241{
242 struct gfs2_sbd *sdp = GFS2_SB(inode);
243 struct posix_acl *acl = NULL;
244 int error = 0, type;
245
246 if (!sdp->sd_args.ar_posix_acl)
247 return -EOPNOTSUPP;
248
249 type = gfs2_acl_type(name);
250 if (type < 0)
251 return type;
252 if (flags & XATTR_CREATE)
253 return -EINVAL;
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))
257 return -EPERM;
258 if (S_ISLNK(inode->i_mode))
259 return -EOPNOTSUPP;
260
261 if (!value)
262 goto set_acl;
263
264 acl = posix_acl_from_xattr(value, size);
265 if (!acl) {
266 /*
267 * acl_set_file(3) may request that we set default ACLs with
268 * zero length -- defend (gracefully) against that here.
269 */
270 goto out;
271 }
272 if (IS_ERR(acl)) {
273 error = PTR_ERR(acl);
274 goto out;
275 }
276
277 error = posix_acl_valid(acl);
278 if (error)
279 goto out_release;
280
281 error = -EINVAL;
282 if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
283 goto out_release;
284
285 if (type == ACL_TYPE_ACCESS) {
286 mode_t mode = inode->i_mode;
287 error = posix_acl_equiv_mode(acl, &mode);
288
289 if (error <= 0) {
290 posix_acl_release(acl);
291 acl = NULL;
292
293 if (error < 0)
294 return error;
295 }
296
297 error = gfs2_set_mode(inode, mode);
298 if (error)
299 goto out_release;
300 }
301
302set_acl:
303 error = gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, value, size, 0);
304out_release:
305 posix_acl_release(acl);
306out:
307 return error;
308}
309
310struct xattr_handler gfs2_xattr_system_handler = {
311 .prefix = XATTR_SYSTEM_PREFIX,
312 .get = gfs2_xattr_system_get,
313 .set = gfs2_xattr_system_set,
314};
315