blob: 2168da1216475b2b9d3a9611730b77c89a3022d4 [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
David Teiglandb3b94fa2006-01-16 16:50:04 +0000107static int munge_mode(struct gfs2_inode *ip, mode_t mode)
108{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400109 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110 struct buffer_head *dibh;
111 int error;
112
113 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
114 if (error)
115 return error;
116
117 error = gfs2_meta_inode_buffer(ip, &dibh);
118 if (!error) {
119 gfs2_assert_withdraw(sdp,
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500120 (ip->i_inode.i_mode & S_IFMT) == (mode & S_IFMT));
121 ip->i_inode.i_mode = mode;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000122 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500123 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000124 brelse(dibh);
125 }
126
127 gfs2_trans_end(sdp);
128
129 return 0;
130}
131
132int gfs2_acl_create(struct gfs2_inode *dip, struct gfs2_inode *ip)
133{
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100134 struct gfs2_ea_location el;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400135 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000136 struct posix_acl *acl = NULL, *clone;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500137 mode_t mode = ip->i_inode.i_mode;
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100138 char *data = NULL;
139 unsigned int len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140 int error;
141
142 if (!sdp->sd_args.ar_posix_acl)
143 return 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500144 if (S_ISLNK(ip->i_inode.i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145 return 0;
146
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100147 error = acl_get(dip, GFS2_POSIX_ACL_DEFAULT, &acl, &el, &data, &len);
148 brelse(el.el_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000149 if (error)
150 return error;
151 if (!acl) {
Al Viroce3b0f82009-03-29 19:08:22 -0400152 mode &= ~current_umask();
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500153 if (mode != ip->i_inode.i_mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154 error = munge_mode(ip, mode);
155 return error;
156 }
157
Josef Bacik16c5f062008-04-09 09:33:41 -0400158 clone = posix_acl_clone(acl, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000159 error = -ENOMEM;
160 if (!clone)
161 goto out;
162 posix_acl_release(acl);
163 acl = clone;
164
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500165 if (S_ISDIR(ip->i_inode.i_mode)) {
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100166 error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS,
167 GFS2_POSIX_ACL_DEFAULT, data, len, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000168 if (error)
169 goto out;
170 }
171
172 error = posix_acl_create_masq(acl, &mode);
173 if (error < 0)
174 goto out;
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100175 if (error == 0)
176 goto munge;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100178 posix_acl_to_xattr(acl, data, len);
179 error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS,
180 GFS2_POSIX_ACL_ACCESS, data, len, 0);
181 if (error)
182 goto out;
183munge:
184 error = munge_mode(ip, mode);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400185out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000186 posix_acl_release(acl);
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100187 kfree(data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000188 return error;
189}
190
191int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
192{
193 struct posix_acl *acl = NULL, *clone;
194 struct gfs2_ea_location el;
195 char *data;
196 unsigned int len;
197 int error;
198
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100199 error = acl_get(ip, GFS2_POSIX_ACL_ACCESS, &acl, &el, &data, &len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000200 if (error)
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100201 goto out_brelse;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202 if (!acl)
203 return gfs2_setattr_simple(ip, attr);
204
Josef Bacik16c5f062008-04-09 09:33:41 -0400205 clone = posix_acl_clone(acl, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206 error = -ENOMEM;
207 if (!clone)
208 goto out;
209 posix_acl_release(acl);
210 acl = clone;
211
212 error = posix_acl_chmod_masq(acl, attr->ia_mode);
213 if (!error) {
214 posix_acl_to_xattr(acl, data, len);
215 error = gfs2_ea_acl_chmod(ip, &el, attr, data);
216 }
217
Steven Whitehousea91ea692006-09-04 12:04:26 -0400218out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000219 posix_acl_release(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000220 kfree(data);
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100221out_brelse:
222 brelse(el.el_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000223 return error;
224}
225
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100226static int gfs2_acl_type(const char *name)
227{
228 if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0)
229 return ACL_TYPE_ACCESS;
230 if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0)
231 return ACL_TYPE_DEFAULT;
232 return -EINVAL;
233}
234
235static int gfs2_xattr_system_get(struct inode *inode, const char *name,
236 void *buffer, size_t size)
237{
238 int type;
239
240 type = gfs2_acl_type(name);
241 if (type < 0)
242 return type;
243
244 return gfs2_xattr_get(inode, GFS2_EATYPE_SYS, name, buffer, size);
245}
246
247static int gfs2_set_mode(struct inode *inode, mode_t mode)
248{
249 int error = 0;
250
251 if (mode != inode->i_mode) {
252 struct iattr iattr;
253
254 iattr.ia_valid = ATTR_MODE;
255 iattr.ia_mode = mode;
256
257 error = gfs2_setattr_simple(GFS2_I(inode), &iattr);
258 }
259
260 return error;
261}
262
263static int gfs2_xattr_system_set(struct inode *inode, const char *name,
264 const void *value, size_t size, int flags)
265{
266 struct gfs2_sbd *sdp = GFS2_SB(inode);
267 struct posix_acl *acl = NULL;
268 int error = 0, type;
269
270 if (!sdp->sd_args.ar_posix_acl)
271 return -EOPNOTSUPP;
272
273 type = gfs2_acl_type(name);
274 if (type < 0)
275 return type;
276 if (flags & XATTR_CREATE)
277 return -EINVAL;
278 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
279 return value ? -EACCES : 0;
280 if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
281 return -EPERM;
282 if (S_ISLNK(inode->i_mode))
283 return -EOPNOTSUPP;
284
285 if (!value)
286 goto set_acl;
287
288 acl = posix_acl_from_xattr(value, size);
289 if (!acl) {
290 /*
291 * acl_set_file(3) may request that we set default ACLs with
292 * zero length -- defend (gracefully) against that here.
293 */
294 goto out;
295 }
296 if (IS_ERR(acl)) {
297 error = PTR_ERR(acl);
298 goto out;
299 }
300
301 error = posix_acl_valid(acl);
302 if (error)
303 goto out_release;
304
305 error = -EINVAL;
306 if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
307 goto out_release;
308
309 if (type == ACL_TYPE_ACCESS) {
310 mode_t mode = inode->i_mode;
311 error = posix_acl_equiv_mode(acl, &mode);
312
313 if (error <= 0) {
314 posix_acl_release(acl);
315 acl = NULL;
316
317 if (error < 0)
318 return error;
319 }
320
321 error = gfs2_set_mode(inode, mode);
322 if (error)
323 goto out_release;
324 }
325
326set_acl:
327 error = gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, value, size, 0);
328out_release:
329 posix_acl_release(acl);
330out:
331 return error;
332}
333
334struct xattr_handler gfs2_xattr_system_handler = {
335 .prefix = XATTR_SYSTEM_PREFIX,
336 .get = gfs2_xattr_system_get,
337 .set = gfs2_xattr_system_set,
338};
339