blob: f69ac0af5496cd456bf6df4d0c932747b6767779 [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 Whitehouse018a01c2011-11-23 13:31:51 +000041struct posix_acl *gfs2_get_acl(struct inode *inode, int type)
Steven Whitehouse479c4272009-10-02 12:00:00 +010042{
Steven Whitehouse018a01c2011-11-23 13:31:51 +000043 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse479c4272009-10-02 12:00:00 +010044 struct posix_acl *acl;
45 const char *name;
46 char *data;
47 int len;
Steven Whitehouse40b78a32009-08-26 18:41:32 +010048
Steven Whitehouse3767ac22008-11-03 14:28:42 +000049 if (!ip->i_eattr)
Steven Whitehouse479c4272009-10-02 12:00:00 +010050 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000051
Steven Whitehouse106381b2009-09-29 16:26:23 +010052 acl = get_cached_acl(&ip->i_inode, type);
53 if (acl != ACL_NOT_CACHED)
54 return acl;
55
Steven Whitehouse479c4272009-10-02 12:00:00 +010056 name = gfs2_acl_name(type);
57 if (name == NULL)
58 return ERR_PTR(-EINVAL);
David Teiglandb3b94fa2006-01-16 16:50:04 +000059
Steven Whitehouse479c4272009-10-02 12:00:00 +010060 len = gfs2_xattr_acl_get(ip, name, &data);
61 if (len < 0)
62 return ERR_PTR(len);
63 if (len == 0)
64 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000065
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -070066 acl = posix_acl_from_xattr(&init_user_ns, data, len);
Steven Whitehouse479c4272009-10-02 12:00:00 +010067 kfree(data);
68 return acl;
David Teiglandb3b94fa2006-01-16 16:50:04 +000069}
70
Al Virod3fb6122011-07-23 18:37:50 -040071static int gfs2_set_mode(struct inode *inode, umode_t mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +000072{
Steven Whitehouse69dca422009-09-29 12:40:19 +010073 int error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +000074
Steven Whitehouse69dca422009-09-29 12:40:19 +010075 if (mode != inode->i_mode) {
Steven Whitehousef9425ad2012-05-04 14:33:06 +010076 inode->i_mode = mode;
77 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000078 }
79
Steven Whitehouse69dca422009-09-29 12:40:19 +010080 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +000081}
82
Steven Whitehouse479c4272009-10-02 12:00:00 +010083static int gfs2_acl_set(struct inode *inode, int type, struct posix_acl *acl)
David Teiglandb3b94fa2006-01-16 16:50:04 +000084{
David Teiglandb3b94fa2006-01-16 16:50:04 +000085 int error;
Steven Whitehouse479c4272009-10-02 12:00:00 +010086 int len;
87 char *data;
88 const char *name = gfs2_acl_name(type);
89
90 BUG_ON(name == NULL);
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -070091 len = posix_acl_to_xattr(&init_user_ns, acl, NULL, 0);
Steven Whitehouse479c4272009-10-02 12:00:00 +010092 if (len == 0)
93 return 0;
94 data = kmalloc(len, GFP_NOFS);
95 if (data == NULL)
96 return -ENOMEM;
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -070097 error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
Steven Whitehouse479c4272009-10-02 12:00:00 +010098 if (error < 0)
99 goto out;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000100 error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
Steven Whitehouse106381b2009-09-29 16:26:23 +0100101 if (!error)
102 set_cached_acl(inode, type, acl);
Steven Whitehouse479c4272009-10-02 12:00:00 +0100103out:
104 kfree(data);
105 return error;
106}
107
108int gfs2_acl_create(struct gfs2_inode *dip, struct inode *inode)
109{
110 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Al Viro826cae22011-07-23 03:10:32 -0400111 struct posix_acl *acl;
Al Virod3fb6122011-07-23 18:37:50 -0400112 umode_t mode = inode->i_mode;
Steven Whitehouse479c4272009-10-02 12:00:00 +0100113 int error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000114
115 if (!sdp->sd_args.ar_posix_acl)
116 return 0;
Steven Whitehouse479c4272009-10-02 12:00:00 +0100117 if (S_ISLNK(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118 return 0;
119
Steven Whitehouse018a01c2011-11-23 13:31:51 +0000120 acl = gfs2_get_acl(&dip->i_inode, ACL_TYPE_DEFAULT);
Steven Whitehouse479c4272009-10-02 12:00:00 +0100121 if (IS_ERR(acl))
122 return PTR_ERR(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123 if (!acl) {
Al Viroce3b0f82009-03-29 19:08:22 -0400124 mode &= ~current_umask();
Steven Whitehousef9425ad2012-05-04 14:33:06 +0100125 return gfs2_set_mode(inode, mode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000126 }
127
Steven Whitehouse479c4272009-10-02 12:00:00 +0100128 if (S_ISDIR(inode->i_mode)) {
129 error = gfs2_acl_set(inode, ACL_TYPE_DEFAULT, acl);
130 if (error)
131 goto out;
132 }
133
Al Viro826cae22011-07-23 03:10:32 -0400134 error = posix_acl_create(&acl, GFP_NOFS, &mode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000135 if (error < 0)
Al Viro826cae22011-07-23 03:10:32 -0400136 return error;
137
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100138 if (error == 0)
139 goto munge;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140
Steven Whitehouse479c4272009-10-02 12:00:00 +0100141 error = gfs2_acl_set(inode, ACL_TYPE_ACCESS, acl);
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100142 if (error)
143 goto out;
144munge:
Steven Whitehouse479c4272009-10-02 12:00:00 +0100145 error = gfs2_set_mode(inode, mode);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400146out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 posix_acl_release(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000148 return error;
149}
150
151int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
152{
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100153 struct inode *inode = &ip->i_inode;
Al Virobc26ab52011-07-23 00:18:02 -0400154 struct posix_acl *acl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000155 char *data;
156 unsigned int len;
157 int error;
158
Steven Whitehouse018a01c2011-11-23 13:31:51 +0000159 acl = gfs2_get_acl(&ip->i_inode, ACL_TYPE_ACCESS);
Steven Whitehouse479c4272009-10-02 12:00:00 +0100160 if (IS_ERR(acl))
161 return PTR_ERR(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162 if (!acl)
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100163 return gfs2_setattr_simple(inode, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164
Al Virobc26ab52011-07-23 00:18:02 -0400165 error = posix_acl_chmod(&acl, GFP_NOFS, attr->ia_mode);
166 if (error)
167 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000168
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -0700169 len = posix_acl_to_xattr(&init_user_ns, acl, NULL, 0);
Al Virobc26ab52011-07-23 00:18:02 -0400170 data = kmalloc(len, GFP_NOFS);
171 error = -ENOMEM;
172 if (data == NULL)
173 goto out;
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -0700174 posix_acl_to_xattr(&init_user_ns, acl, data, len);
Al Virobc26ab52011-07-23 00:18:02 -0400175 error = gfs2_xattr_acl_chmod(ip, attr, data);
176 kfree(data);
177 set_cached_acl(&ip->i_inode, ACL_TYPE_ACCESS, acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000178
Steven Whitehousea91ea692006-09-04 12:04:26 -0400179out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000180 posix_acl_release(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000181 return error;
182}
183
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100184static int gfs2_acl_type(const char *name)
185{
186 if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0)
187 return ACL_TYPE_ACCESS;
188 if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0)
189 return ACL_TYPE_DEFAULT;
190 return -EINVAL;
191}
192
Christoph Hellwig431547b2009-11-13 09:52:56 +0000193static int gfs2_xattr_system_get(struct dentry *dentry, const char *name,
194 void *buffer, size_t size, int xtype)
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100195{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000196 struct inode *inode = dentry->d_inode;
Steven Whitehousef72f2d22010-05-21 16:12:27 +0100197 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse106381b2009-09-29 16:26:23 +0100198 struct posix_acl *acl;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100199 int type;
Steven Whitehouse106381b2009-09-29 16:26:23 +0100200 int error;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100201
Steven Whitehousef72f2d22010-05-21 16:12:27 +0100202 if (!sdp->sd_args.ar_posix_acl)
203 return -EOPNOTSUPP;
204
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100205 type = gfs2_acl_type(name);
206 if (type < 0)
207 return type;
208
Steven Whitehouse018a01c2011-11-23 13:31:51 +0000209 acl = gfs2_get_acl(inode, type);
Steven Whitehouse106381b2009-09-29 16:26:23 +0100210 if (IS_ERR(acl))
211 return PTR_ERR(acl);
212 if (acl == NULL)
213 return -ENODATA;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100214
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -0700215 error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
Steven Whitehouse106381b2009-09-29 16:26:23 +0100216 posix_acl_release(acl);
217
218 return error;
219}
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100220
Christoph Hellwig431547b2009-11-13 09:52:56 +0000221static int gfs2_xattr_system_set(struct dentry *dentry, const char *name,
222 const void *value, size_t size, int flags,
223 int xtype)
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100224{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000225 struct inode *inode = dentry->d_inode;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100226 struct gfs2_sbd *sdp = GFS2_SB(inode);
227 struct posix_acl *acl = NULL;
228 int error = 0, type;
229
230 if (!sdp->sd_args.ar_posix_acl)
231 return -EOPNOTSUPP;
232
233 type = gfs2_acl_type(name);
234 if (type < 0)
235 return type;
236 if (flags & XATTR_CREATE)
237 return -EINVAL;
238 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
239 return value ? -EACCES : 0;
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800240 if (!uid_eq(current_fsuid(), inode->i_uid) && !capable(CAP_FOWNER))
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100241 return -EPERM;
242 if (S_ISLNK(inode->i_mode))
243 return -EOPNOTSUPP;
244
245 if (!value)
246 goto set_acl;
247
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -0700248 acl = posix_acl_from_xattr(&init_user_ns, value, size);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100249 if (!acl) {
250 /*
251 * acl_set_file(3) may request that we set default ACLs with
252 * zero length -- defend (gracefully) against that here.
253 */
254 goto out;
255 }
256 if (IS_ERR(acl)) {
257 error = PTR_ERR(acl);
258 goto out;
259 }
260
261 error = posix_acl_valid(acl);
262 if (error)
263 goto out_release;
264
265 error = -EINVAL;
266 if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
267 goto out_release;
268
269 if (type == ACL_TYPE_ACCESS) {
Al Virod6952122011-07-23 18:56:36 -0400270 umode_t mode = inode->i_mode;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100271 error = posix_acl_equiv_mode(acl, &mode);
272
273 if (error <= 0) {
274 posix_acl_release(acl);
275 acl = NULL;
276
277 if (error < 0)
278 return error;
279 }
280
281 error = gfs2_set_mode(inode, mode);
282 if (error)
283 goto out_release;
284 }
285
286set_acl:
Christoph Hellwig431547b2009-11-13 09:52:56 +0000287 error = __gfs2_xattr_set(inode, name, value, size, 0, GFS2_EATYPE_SYS);
Steven Whitehouse106381b2009-09-29 16:26:23 +0100288 if (!error) {
289 if (acl)
290 set_cached_acl(inode, type, acl);
291 else
292 forget_cached_acl(inode, type);
293 }
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100294out_release:
295 posix_acl_release(acl);
296out:
297 return error;
298}
299
Stephen Hemmingerb7bb0a12010-05-13 17:53:23 -0700300const struct xattr_handler gfs2_xattr_system_handler = {
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100301 .prefix = XATTR_SYSTEM_PREFIX,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000302 .flags = GFS2_EATYPE_SYS,
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100303 .get = gfs2_xattr_system_get,
304 .set = gfs2_xattr_system_set,
305};
306