blob: 230eb0f005b69118f80b85f1b470b6460438fc3d [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
Steven Whitehouse479c4272009-10-02 12:00:00 +010066 acl = posix_acl_from_xattr(data, len);
67 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) {
76 struct iattr iattr;
David Teiglandb3b94fa2006-01-16 16:50:04 +000077
Steven Whitehouse69dca422009-09-29 12:40:19 +010078 iattr.ia_valid = ATTR_MODE;
79 iattr.ia_mode = mode;
80
Steven Whitehouseab9bbda2011-08-15 14:20:36 +010081 error = gfs2_setattr_simple(inode, &iattr);
David Teiglandb3b94fa2006-01-16 16:50:04 +000082 }
83
Steven Whitehouse69dca422009-09-29 12:40:19 +010084 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +000085}
86
Steven Whitehouse479c4272009-10-02 12:00:00 +010087static int gfs2_acl_set(struct inode *inode, int type, struct posix_acl *acl)
David Teiglandb3b94fa2006-01-16 16:50:04 +000088{
David Teiglandb3b94fa2006-01-16 16:50:04 +000089 int error;
Steven Whitehouse479c4272009-10-02 12:00:00 +010090 int len;
91 char *data;
92 const char *name = gfs2_acl_name(type);
93
94 BUG_ON(name == NULL);
95 len = posix_acl_to_xattr(acl, NULL, 0);
96 if (len == 0)
97 return 0;
98 data = kmalloc(len, GFP_NOFS);
99 if (data == NULL)
100 return -ENOMEM;
101 error = posix_acl_to_xattr(acl, data, len);
102 if (error < 0)
103 goto out;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000104 error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
Steven Whitehouse106381b2009-09-29 16:26:23 +0100105 if (!error)
106 set_cached_acl(inode, type, acl);
Steven Whitehouse479c4272009-10-02 12:00:00 +0100107out:
108 kfree(data);
109 return error;
110}
111
112int gfs2_acl_create(struct gfs2_inode *dip, struct inode *inode)
113{
114 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Al Viro826cae22011-07-23 03:10:32 -0400115 struct posix_acl *acl;
Al Virod3fb6122011-07-23 18:37:50 -0400116 umode_t mode = inode->i_mode;
Steven Whitehouse479c4272009-10-02 12:00:00 +0100117 int error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118
119 if (!sdp->sd_args.ar_posix_acl)
120 return 0;
Steven Whitehouse479c4272009-10-02 12:00:00 +0100121 if (S_ISLNK(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122 return 0;
123
Steven Whitehouse018a01c2011-11-23 13:31:51 +0000124 acl = gfs2_get_acl(&dip->i_inode, ACL_TYPE_DEFAULT);
Steven Whitehouse479c4272009-10-02 12:00:00 +0100125 if (IS_ERR(acl))
126 return PTR_ERR(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127 if (!acl) {
Al Viroce3b0f82009-03-29 19:08:22 -0400128 mode &= ~current_umask();
Steven Whitehouse479c4272009-10-02 12:00:00 +0100129 if (mode != inode->i_mode)
130 error = gfs2_set_mode(inode, mode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000131 return error;
132 }
133
Steven Whitehouse479c4272009-10-02 12:00:00 +0100134 if (S_ISDIR(inode->i_mode)) {
135 error = gfs2_acl_set(inode, ACL_TYPE_DEFAULT, acl);
136 if (error)
137 goto out;
138 }
139
Al Viro826cae22011-07-23 03:10:32 -0400140 error = posix_acl_create(&acl, GFP_NOFS, &mode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141 if (error < 0)
Al Viro826cae22011-07-23 03:10:32 -0400142 return error;
143
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100144 if (error == 0)
145 goto munge;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146
Steven Whitehouse479c4272009-10-02 12:00:00 +0100147 error = gfs2_acl_set(inode, ACL_TYPE_ACCESS, acl);
Steven Whitehouse40b78a32009-08-26 18:41:32 +0100148 if (error)
149 goto out;
150munge:
Steven Whitehouse479c4272009-10-02 12:00:00 +0100151 error = gfs2_set_mode(inode, mode);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400152out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000153 posix_acl_release(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154 return error;
155}
156
157int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
158{
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100159 struct inode *inode = &ip->i_inode;
Al Virobc26ab52011-07-23 00:18:02 -0400160 struct posix_acl *acl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161 char *data;
162 unsigned int len;
163 int error;
164
Steven Whitehouse018a01c2011-11-23 13:31:51 +0000165 acl = gfs2_get_acl(&ip->i_inode, ACL_TYPE_ACCESS);
Steven Whitehouse479c4272009-10-02 12:00:00 +0100166 if (IS_ERR(acl))
167 return PTR_ERR(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000168 if (!acl)
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100169 return gfs2_setattr_simple(inode, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000170
Al Virobc26ab52011-07-23 00:18:02 -0400171 error = posix_acl_chmod(&acl, GFP_NOFS, attr->ia_mode);
172 if (error)
173 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000174
Al Virobc26ab52011-07-23 00:18:02 -0400175 len = posix_acl_to_xattr(acl, NULL, 0);
176 data = kmalloc(len, GFP_NOFS);
177 error = -ENOMEM;
178 if (data == NULL)
179 goto out;
180 posix_acl_to_xattr(acl, data, len);
181 error = gfs2_xattr_acl_chmod(ip, attr, data);
182 kfree(data);
183 set_cached_acl(&ip->i_inode, ACL_TYPE_ACCESS, acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000184
Steven Whitehousea91ea692006-09-04 12:04:26 -0400185out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000186 posix_acl_release(acl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000187 return error;
188}
189
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100190static int gfs2_acl_type(const char *name)
191{
192 if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0)
193 return ACL_TYPE_ACCESS;
194 if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0)
195 return ACL_TYPE_DEFAULT;
196 return -EINVAL;
197}
198
Christoph Hellwig431547b2009-11-13 09:52:56 +0000199static int gfs2_xattr_system_get(struct dentry *dentry, const char *name,
200 void *buffer, size_t size, int xtype)
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100201{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000202 struct inode *inode = dentry->d_inode;
Steven Whitehousef72f2d22010-05-21 16:12:27 +0100203 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse106381b2009-09-29 16:26:23 +0100204 struct posix_acl *acl;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100205 int type;
Steven Whitehouse106381b2009-09-29 16:26:23 +0100206 int error;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100207
Steven Whitehousef72f2d22010-05-21 16:12:27 +0100208 if (!sdp->sd_args.ar_posix_acl)
209 return -EOPNOTSUPP;
210
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100211 type = gfs2_acl_type(name);
212 if (type < 0)
213 return type;
214
Steven Whitehouse018a01c2011-11-23 13:31:51 +0000215 acl = gfs2_get_acl(inode, type);
Steven Whitehouse106381b2009-09-29 16:26:23 +0100216 if (IS_ERR(acl))
217 return PTR_ERR(acl);
218 if (acl == NULL)
219 return -ENODATA;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100220
Steven Whitehouse106381b2009-09-29 16:26:23 +0100221 error = posix_acl_to_xattr(acl, buffer, size);
222 posix_acl_release(acl);
223
224 return error;
225}
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100226
Christoph Hellwig431547b2009-11-13 09:52:56 +0000227static int gfs2_xattr_system_set(struct dentry *dentry, const char *name,
228 const void *value, size_t size, int flags,
229 int xtype)
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100230{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000231 struct inode *inode = dentry->d_inode;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100232 struct gfs2_sbd *sdp = GFS2_SB(inode);
233 struct posix_acl *acl = NULL;
234 int error = 0, type;
235
236 if (!sdp->sd_args.ar_posix_acl)
237 return -EOPNOTSUPP;
238
239 type = gfs2_acl_type(name);
240 if (type < 0)
241 return type;
242 if (flags & XATTR_CREATE)
243 return -EINVAL;
244 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
245 return value ? -EACCES : 0;
246 if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
247 return -EPERM;
248 if (S_ISLNK(inode->i_mode))
249 return -EOPNOTSUPP;
250
251 if (!value)
252 goto set_acl;
253
254 acl = posix_acl_from_xattr(value, size);
255 if (!acl) {
256 /*
257 * acl_set_file(3) may request that we set default ACLs with
258 * zero length -- defend (gracefully) against that here.
259 */
260 goto out;
261 }
262 if (IS_ERR(acl)) {
263 error = PTR_ERR(acl);
264 goto out;
265 }
266
267 error = posix_acl_valid(acl);
268 if (error)
269 goto out_release;
270
271 error = -EINVAL;
272 if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
273 goto out_release;
274
275 if (type == ACL_TYPE_ACCESS) {
Al Virod6952122011-07-23 18:56:36 -0400276 umode_t mode = inode->i_mode;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100277 error = posix_acl_equiv_mode(acl, &mode);
278
279 if (error <= 0) {
280 posix_acl_release(acl);
281 acl = NULL;
282
283 if (error < 0)
284 return error;
285 }
286
287 error = gfs2_set_mode(inode, mode);
288 if (error)
289 goto out_release;
290 }
291
292set_acl:
Christoph Hellwig431547b2009-11-13 09:52:56 +0000293 error = __gfs2_xattr_set(inode, name, value, size, 0, GFS2_EATYPE_SYS);
Steven Whitehouse106381b2009-09-29 16:26:23 +0100294 if (!error) {
295 if (acl)
296 set_cached_acl(inode, type, acl);
297 else
298 forget_cached_acl(inode, type);
299 }
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100300out_release:
301 posix_acl_release(acl);
302out:
303 return error;
304}
305
Stephen Hemmingerb7bb0a12010-05-13 17:53:23 -0700306const struct xattr_handler gfs2_xattr_system_handler = {
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100307 .prefix = XATTR_SYSTEM_PREFIX,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000308 .flags = GFS2_EATYPE_SYS,
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100309 .get = gfs2_xattr_system_get,
310 .set = gfs2_xattr_system_set,
311};
312