blob: f6777f1672600e37c8a501f137a054bc07f56669 [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>
15#include <linux/posix_acl.h>
16#include <linux/posix_acl_xattr.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050017#include <linux/gfs2_ondisk.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000018
19#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050020#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000021#include "acl.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "eattr.h"
23#include "glock.h"
24#include "inode.h"
25#include "meta_io.h"
26#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050027#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000028
29#define ACL_ACCESS 1
30#define ACL_DEFAULT 0
31
32int gfs2_acl_validate_set(struct gfs2_inode *ip, int access,
Steven Whitehouse40b78a32009-08-26 18:41:32 +010033 struct gfs2_ea_request *er, int *remove, mode_t *mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +000034{
35 struct posix_acl *acl;
36 int error;
37
38 error = gfs2_acl_validate_remove(ip, access);
39 if (error)
40 return error;
41
42 if (!er->er_data)
43 return -EINVAL;
44
45 acl = posix_acl_from_xattr(er->er_data, er->er_data_len);
46 if (IS_ERR(acl))
47 return PTR_ERR(acl);
48 if (!acl) {
49 *remove = 1;
50 return 0;
51 }
52
53 error = posix_acl_valid(acl);
54 if (error)
55 goto out;
56
57 if (access) {
58 error = posix_acl_equiv_mode(acl, mode);
59 if (!error)
60 *remove = 1;
61 else if (error > 0)
62 error = 0;
63 }
64
Steven Whitehousea91ea692006-09-04 12:04:26 -040065out:
David Teiglandb3b94fa2006-01-16 16:50:04 +000066 posix_acl_release(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +000067 return error;
68}
69
70int gfs2_acl_validate_remove(struct gfs2_inode *ip, int access)
71{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040072 if (!GFS2_SB(&ip->i_inode)->sd_args.ar_posix_acl)
David Teiglandb3b94fa2006-01-16 16:50:04 +000073 return -EOPNOTSUPP;
Satyam Sharma3bd858a2007-07-17 15:00:08 +053074 if (!is_owner_or_cap(&ip->i_inode))
David Teiglandb3b94fa2006-01-16 16:50:04 +000075 return -EPERM;
Steven Whitehouseb60623c2006-11-01 12:22:46 -050076 if (S_ISLNK(ip->i_inode.i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +000077 return -EOPNOTSUPP;
Steven Whitehouseb60623c2006-11-01 12:22:46 -050078 if (!access && !S_ISDIR(ip->i_inode.i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +000079 return -EACCES;
80
81 return 0;
82}
83
Steven Whitehouse40b78a32009-08-26 18:41:32 +010084static int acl_get(struct gfs2_inode *ip, const char *name,
85 struct posix_acl **acl, struct gfs2_ea_location *el,
86 char **datap, unsigned int *lenp)
David Teiglandb3b94fa2006-01-16 16:50:04 +000087{
Steven Whitehouse40b78a32009-08-26 18:41:32 +010088 char *data;
89 unsigned int len;
David Teiglandb3b94fa2006-01-16 16:50:04 +000090 int error;
91
Steven Whitehouse40b78a32009-08-26 18:41:32 +010092 el->el_bh = NULL;
93
Steven Whitehouse3767ac22008-11-03 14:28:42 +000094 if (!ip->i_eattr)
David Teiglandb3b94fa2006-01-16 16:50:04 +000095 return 0;
96
Steven Whitehouse40b78a32009-08-26 18:41:32 +010097 error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, el);
David Teiglandb3b94fa2006-01-16 16:50:04 +000098 if (error)
99 return error;
100 if (!el->el_ea)
101 return 0;
102 if (!GFS2_EA_DATA_LEN(el->el_ea))
103 goto out;
104
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100105 len = GFS2_EA_DATA_LEN(el->el_ea);
106 data = kmalloc(len, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000107 error = -ENOMEM;
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100108 if (!data)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109 goto out;
110
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100111 error = gfs2_ea_get_copy(ip, el, data, len);
112 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000113 goto out_kfree;
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100114 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115
116 if (acl) {
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100117 *acl = posix_acl_from_xattr(data, len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118 if (IS_ERR(*acl))
119 error = PTR_ERR(*acl);
120 }
121
Steven Whitehousea91ea692006-09-04 12:04:26 -0400122out_kfree:
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100123 if (error || !datap) {
124 kfree(data);
125 } else {
126 *datap = data;
127 *lenp = len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000128 }
Steven Whitehousea91ea692006-09-04 12:04:26 -0400129out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000130 return error;
131}
132
133/**
Steven Whitehouse77386e12006-11-29 10:41:49 -0500134 * gfs2_check_acl - Check an ACL to see if we're allowed to do something
David Teiglandb3b94fa2006-01-16 16:50:04 +0000135 * @inode: the file we want to do something to
136 * @mask: what we want to do
137 *
138 * Returns: errno
139 */
140
Steven Whitehouse77386e12006-11-29 10:41:49 -0500141int gfs2_check_acl(struct inode *inode, int mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000142{
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100143 struct gfs2_ea_location el;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000144 struct posix_acl *acl = NULL;
145 int error;
146
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100147 error = acl_get(GFS2_I(inode), GFS2_POSIX_ACL_ACCESS, &acl, &el, NULL, NULL);
148 brelse(el.el_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000149 if (error)
150 return error;
151
152 if (acl) {
153 error = posix_acl_permission(inode, acl, mask);
154 posix_acl_release(acl);
155 return error;
156 }
157
158 return -EAGAIN;
159}
160
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161static int munge_mode(struct gfs2_inode *ip, mode_t mode)
162{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400163 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164 struct buffer_head *dibh;
165 int error;
166
167 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
168 if (error)
169 return error;
170
171 error = gfs2_meta_inode_buffer(ip, &dibh);
172 if (!error) {
173 gfs2_assert_withdraw(sdp,
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500174 (ip->i_inode.i_mode & S_IFMT) == (mode & S_IFMT));
175 ip->i_inode.i_mode = mode;
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000176 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500177 gfs2_dinode_out(ip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000178 brelse(dibh);
179 }
180
181 gfs2_trans_end(sdp);
182
183 return 0;
184}
185
186int gfs2_acl_create(struct gfs2_inode *dip, struct gfs2_inode *ip)
187{
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100188 struct gfs2_ea_location el;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400189 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190 struct posix_acl *acl = NULL, *clone;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500191 mode_t mode = ip->i_inode.i_mode;
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100192 char *data = NULL;
193 unsigned int len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000194 int error;
195
196 if (!sdp->sd_args.ar_posix_acl)
197 return 0;
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500198 if (S_ISLNK(ip->i_inode.i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000199 return 0;
200
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100201 error = acl_get(dip, GFS2_POSIX_ACL_DEFAULT, &acl, &el, &data, &len);
202 brelse(el.el_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203 if (error)
204 return error;
205 if (!acl) {
Al Viroce3b0f82009-03-29 19:08:22 -0400206 mode &= ~current_umask();
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500207 if (mode != ip->i_inode.i_mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208 error = munge_mode(ip, mode);
209 return error;
210 }
211
Josef Bacik16c5f062008-04-09 09:33:41 -0400212 clone = posix_acl_clone(acl, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213 error = -ENOMEM;
214 if (!clone)
215 goto out;
216 posix_acl_release(acl);
217 acl = clone;
218
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500219 if (S_ISDIR(ip->i_inode.i_mode)) {
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100220 error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS,
221 GFS2_POSIX_ACL_DEFAULT, data, len, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000222 if (error)
223 goto out;
224 }
225
226 error = posix_acl_create_masq(acl, &mode);
227 if (error < 0)
228 goto out;
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100229 if (error == 0)
230 goto munge;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000231
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100232 posix_acl_to_xattr(acl, data, len);
233 error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS,
234 GFS2_POSIX_ACL_ACCESS, data, len, 0);
235 if (error)
236 goto out;
237munge:
238 error = munge_mode(ip, mode);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400239out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240 posix_acl_release(acl);
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100241 kfree(data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242 return error;
243}
244
245int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
246{
247 struct posix_acl *acl = NULL, *clone;
248 struct gfs2_ea_location el;
249 char *data;
250 unsigned int len;
251 int error;
252
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100253 error = acl_get(ip, GFS2_POSIX_ACL_ACCESS, &acl, &el, &data, &len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254 if (error)
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100255 goto out_brelse;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256 if (!acl)
257 return gfs2_setattr_simple(ip, attr);
258
Josef Bacik16c5f062008-04-09 09:33:41 -0400259 clone = posix_acl_clone(acl, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260 error = -ENOMEM;
261 if (!clone)
262 goto out;
263 posix_acl_release(acl);
264 acl = clone;
265
266 error = posix_acl_chmod_masq(acl, attr->ia_mode);
267 if (!error) {
268 posix_acl_to_xattr(acl, data, len);
269 error = gfs2_ea_acl_chmod(ip, &el, attr, data);
270 }
271
Steven Whitehousea91ea692006-09-04 12:04:26 -0400272out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000273 posix_acl_release(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274 kfree(data);
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100275out_brelse:
276 brelse(el.el_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277 return error;
278}
279