blob: cbc07155b1a0a0e4a3d10fb47f00c7ed1cdfef8f [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 Whitehouse106381b2009-09-29 16:26:23 +010051 acl = get_cached_acl(&ip->i_inode, type);
52 if (acl != ACL_NOT_CACHED)
53 return acl;
54
Steven Whitehouse479c4272009-10-02 12:00:00 +010055 name = gfs2_acl_name(type);
56 if (name == NULL)
57 return ERR_PTR(-EINVAL);
David Teiglandb3b94fa2006-01-16 16:50:04 +000058
Steven Whitehouse479c4272009-10-02 12:00:00 +010059 len = gfs2_xattr_acl_get(ip, name, &data);
60 if (len < 0)
61 return ERR_PTR(len);
62 if (len == 0)
63 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000064
Steven Whitehouse479c4272009-10-02 12:00:00 +010065 acl = posix_acl_from_xattr(data, len);
66 kfree(data);
67 return acl;
David Teiglandb3b94fa2006-01-16 16:50:04 +000068}
69
70/**
Steven Whitehouse77386e12006-11-29 10:41:49 -050071 * gfs2_check_acl - Check an ACL to see if we're allowed to do something
David Teiglandb3b94fa2006-01-16 16:50:04 +000072 * @inode: the file we want to do something to
73 * @mask: what we want to do
74 *
75 * Returns: errno
76 */
77
Nick Pigginb74c79e2011-01-07 17:49:58 +110078int gfs2_check_acl(struct inode *inode, int mask, unsigned int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +000079{
Steven Whitehouse479c4272009-10-02 12:00:00 +010080 struct posix_acl *acl;
David Teiglandb3b94fa2006-01-16 16:50:04 +000081 int error;
82
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +000083 if (flags & IPERM_FLAG_RCU) {
84 if (!negative_cached_acl(inode, ACL_TYPE_ACCESS))
85 return -ECHILD;
86 return -EAGAIN;
87 }
Nick Pigginb74c79e2011-01-07 17:49:58 +110088
Steven Whitehouse479c4272009-10-02 12:00:00 +010089 acl = gfs2_acl_get(GFS2_I(inode), ACL_TYPE_ACCESS);
90 if (IS_ERR(acl))
91 return PTR_ERR(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +000092
93 if (acl) {
94 error = posix_acl_permission(inode, acl, mask);
95 posix_acl_release(acl);
96 return error;
97 }
98
99 return -EAGAIN;
100}
101
Steven Whitehouse69dca422009-09-29 12:40:19 +0100102static int gfs2_set_mode(struct inode *inode, mode_t mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000103{
Steven Whitehouse69dca422009-09-29 12:40:19 +0100104 int error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000105
Steven Whitehouse69dca422009-09-29 12:40:19 +0100106 if (mode != inode->i_mode) {
107 struct iattr iattr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108
Steven Whitehouse69dca422009-09-29 12:40:19 +0100109 iattr.ia_valid = ATTR_MODE;
110 iattr.ia_mode = mode;
111
112 error = gfs2_setattr_simple(GFS2_I(inode), &iattr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000113 }
114
Steven Whitehouse69dca422009-09-29 12:40:19 +0100115 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000116}
117
Steven Whitehouse479c4272009-10-02 12:00:00 +0100118static int gfs2_acl_set(struct inode *inode, int type, struct posix_acl *acl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000120 int error;
Steven Whitehouse479c4272009-10-02 12:00:00 +0100121 int len;
122 char *data;
123 const char *name = gfs2_acl_name(type);
124
125 BUG_ON(name == NULL);
126 len = posix_acl_to_xattr(acl, NULL, 0);
127 if (len == 0)
128 return 0;
129 data = kmalloc(len, GFP_NOFS);
130 if (data == NULL)
131 return -ENOMEM;
132 error = posix_acl_to_xattr(acl, data, len);
133 if (error < 0)
134 goto out;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000135 error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
Steven Whitehouse106381b2009-09-29 16:26:23 +0100136 if (!error)
137 set_cached_acl(inode, type, acl);
Steven Whitehouse479c4272009-10-02 12:00:00 +0100138out:
139 kfree(data);
140 return error;
141}
142
143int gfs2_acl_create(struct gfs2_inode *dip, struct inode *inode)
144{
145 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
146 struct posix_acl *acl, *clone;
147 mode_t mode = inode->i_mode;
148 int error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000149
150 if (!sdp->sd_args.ar_posix_acl)
151 return 0;
Steven Whitehouse479c4272009-10-02 12:00:00 +0100152 if (S_ISLNK(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000153 return 0;
154
Steven Whitehouse479c4272009-10-02 12:00:00 +0100155 acl = gfs2_acl_get(dip, ACL_TYPE_DEFAULT);
156 if (IS_ERR(acl))
157 return PTR_ERR(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158 if (!acl) {
Al Viroce3b0f82009-03-29 19:08:22 -0400159 mode &= ~current_umask();
Steven Whitehouse479c4272009-10-02 12:00:00 +0100160 if (mode != inode->i_mode)
161 error = gfs2_set_mode(inode, mode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162 return error;
163 }
164
Steven Whitehouse479c4272009-10-02 12:00:00 +0100165 if (S_ISDIR(inode->i_mode)) {
166 error = gfs2_acl_set(inode, ACL_TYPE_DEFAULT, acl);
167 if (error)
168 goto out;
169 }
170
Josef Bacik16c5f062008-04-09 09:33:41 -0400171 clone = posix_acl_clone(acl, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 error = -ENOMEM;
173 if (!clone)
174 goto out;
175 posix_acl_release(acl);
176 acl = clone;
177
David Teiglandb3b94fa2006-01-16 16:50:04 +0000178 error = posix_acl_create_masq(acl, &mode);
179 if (error < 0)
180 goto out;
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100181 if (error == 0)
182 goto munge;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000183
Steven Whitehouse479c4272009-10-02 12:00:00 +0100184 error = gfs2_acl_set(inode, ACL_TYPE_ACCESS, acl);
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100185 if (error)
186 goto out;
187munge:
Steven Whitehouse479c4272009-10-02 12:00:00 +0100188 error = gfs2_set_mode(inode, mode);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400189out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190 posix_acl_release(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191 return error;
192}
193
194int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
195{
Steven Whitehouse479c4272009-10-02 12:00:00 +0100196 struct posix_acl *acl, *clone;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000197 char *data;
198 unsigned int len;
199 int error;
200
Steven Whitehouse479c4272009-10-02 12:00:00 +0100201 acl = gfs2_acl_get(ip, ACL_TYPE_ACCESS);
202 if (IS_ERR(acl))
203 return PTR_ERR(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204 if (!acl)
205 return gfs2_setattr_simple(ip, attr);
206
Josef Bacik16c5f062008-04-09 09:33:41 -0400207 clone = posix_acl_clone(acl, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208 error = -ENOMEM;
209 if (!clone)
210 goto out;
211 posix_acl_release(acl);
212 acl = clone;
213
214 error = posix_acl_chmod_masq(acl, attr->ia_mode);
215 if (!error) {
Steven Whitehouse479c4272009-10-02 12:00:00 +0100216 len = posix_acl_to_xattr(acl, NULL, 0);
217 data = kmalloc(len, GFP_NOFS);
218 error = -ENOMEM;
219 if (data == NULL)
220 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000221 posix_acl_to_xattr(acl, data, len);
Steven Whitehouse479c4272009-10-02 12:00:00 +0100222 error = gfs2_xattr_acl_chmod(ip, attr, data);
223 kfree(data);
Steven Whitehouse106381b2009-09-29 16:26:23 +0100224 set_cached_acl(&ip->i_inode, ACL_TYPE_ACCESS, acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000225 }
226
Steven Whitehousea91ea692006-09-04 12:04:26 -0400227out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000228 posix_acl_release(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000229 return error;
230}
231
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100232static int gfs2_acl_type(const char *name)
233{
234 if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0)
235 return ACL_TYPE_ACCESS;
236 if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0)
237 return ACL_TYPE_DEFAULT;
238 return -EINVAL;
239}
240
Christoph Hellwig431547b2009-11-13 09:52:56 +0000241static int gfs2_xattr_system_get(struct dentry *dentry, const char *name,
242 void *buffer, size_t size, int xtype)
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100243{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000244 struct inode *inode = dentry->d_inode;
Steven Whitehousef72f2d22010-05-21 16:12:27 +0100245 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse106381b2009-09-29 16:26:23 +0100246 struct posix_acl *acl;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100247 int type;
Steven Whitehouse106381b2009-09-29 16:26:23 +0100248 int error;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100249
Steven Whitehousef72f2d22010-05-21 16:12:27 +0100250 if (!sdp->sd_args.ar_posix_acl)
251 return -EOPNOTSUPP;
252
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100253 type = gfs2_acl_type(name);
254 if (type < 0)
255 return type;
256
Steven Whitehouse106381b2009-09-29 16:26:23 +0100257 acl = gfs2_acl_get(GFS2_I(inode), type);
258 if (IS_ERR(acl))
259 return PTR_ERR(acl);
260 if (acl == NULL)
261 return -ENODATA;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100262
Steven Whitehouse106381b2009-09-29 16:26:23 +0100263 error = posix_acl_to_xattr(acl, buffer, size);
264 posix_acl_release(acl);
265
266 return error;
267}
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100268
Christoph Hellwig431547b2009-11-13 09:52:56 +0000269static int gfs2_xattr_system_set(struct dentry *dentry, const char *name,
270 const void *value, size_t size, int flags,
271 int xtype)
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100272{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000273 struct inode *inode = dentry->d_inode;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100274 struct gfs2_sbd *sdp = GFS2_SB(inode);
275 struct posix_acl *acl = NULL;
276 int error = 0, type;
277
278 if (!sdp->sd_args.ar_posix_acl)
279 return -EOPNOTSUPP;
280
281 type = gfs2_acl_type(name);
282 if (type < 0)
283 return type;
284 if (flags & XATTR_CREATE)
285 return -EINVAL;
286 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
287 return value ? -EACCES : 0;
288 if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
289 return -EPERM;
290 if (S_ISLNK(inode->i_mode))
291 return -EOPNOTSUPP;
292
293 if (!value)
294 goto set_acl;
295
296 acl = posix_acl_from_xattr(value, size);
297 if (!acl) {
298 /*
299 * acl_set_file(3) may request that we set default ACLs with
300 * zero length -- defend (gracefully) against that here.
301 */
302 goto out;
303 }
304 if (IS_ERR(acl)) {
305 error = PTR_ERR(acl);
306 goto out;
307 }
308
309 error = posix_acl_valid(acl);
310 if (error)
311 goto out_release;
312
313 error = -EINVAL;
314 if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
315 goto out_release;
316
317 if (type == ACL_TYPE_ACCESS) {
318 mode_t mode = inode->i_mode;
319 error = posix_acl_equiv_mode(acl, &mode);
320
321 if (error <= 0) {
322 posix_acl_release(acl);
323 acl = NULL;
324
325 if (error < 0)
326 return error;
327 }
328
329 error = gfs2_set_mode(inode, mode);
330 if (error)
331 goto out_release;
332 }
333
334set_acl:
Christoph Hellwig431547b2009-11-13 09:52:56 +0000335 error = __gfs2_xattr_set(inode, name, value, size, 0, GFS2_EATYPE_SYS);
Steven Whitehouse106381b2009-09-29 16:26:23 +0100336 if (!error) {
337 if (acl)
338 set_cached_acl(inode, type, acl);
339 else
340 forget_cached_acl(inode, type);
341 }
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100342out_release:
343 posix_acl_release(acl);
344out:
345 return error;
346}
347
Stephen Hemmingerb7bb0a12010-05-13 17:53:23 -0700348const struct xattr_handler gfs2_xattr_system_handler = {
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100349 .prefix = XATTR_SYSTEM_PREFIX,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000350 .flags = GFS2_EATYPE_SYS,
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100351 .get = gfs2_xattr_system_get,
352 .set = gfs2_xattr_system_set,
353};
354