blob: 7b3143064af1130d22c14f66c8236785093c545a [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 Whitehouse479c4272009-10-02 12:00:00 +010052 name = gfs2_acl_name(type);
53 if (name == NULL)
54 return ERR_PTR(-EINVAL);
David Teiglandb3b94fa2006-01-16 16:50:04 +000055
Steven Whitehouse479c4272009-10-02 12:00:00 +010056 len = gfs2_xattr_acl_get(ip, name, &data);
57 if (len < 0)
58 return ERR_PTR(len);
59 if (len == 0)
60 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000061
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -070062 acl = posix_acl_from_xattr(&init_user_ns, data, len);
Steven Whitehouse479c4272009-10-02 12:00:00 +010063 kfree(data);
64 return acl;
David Teiglandb3b94fa2006-01-16 16:50:04 +000065}
66
Christoph Hellwige01580b2013-12-20 05:16:52 -080067int gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +000068{
David Teiglandb3b94fa2006-01-16 16:50:04 +000069 int error;
Steven Whitehouse479c4272009-10-02 12:00:00 +010070 int len;
71 char *data;
72 const char *name = gfs2_acl_name(type);
73
74 BUG_ON(name == NULL);
Steven Whitehouse479c4272009-10-02 12:00:00 +010075
Andrew Elble27870202015-02-09 12:53:04 -050076 if (acl && acl->a_count > GFS2_ACL_MAX_ENTRIES(GFS2_SB(inode)))
Jie Liuf2113eb2014-03-04 11:28:39 +080077 return -E2BIG;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +010078
79 if (type == ACL_TYPE_ACCESS) {
Al Virod6952122011-07-23 18:56:36 -040080 umode_t mode = inode->i_mode;
Christoph Hellwige01580b2013-12-20 05:16:52 -080081
Steven Whitehouse2646a1f2009-10-02 11:50:54 +010082 error = posix_acl_equiv_mode(acl, &mode);
Christoph Hellwige01580b2013-12-20 05:16:52 -080083 if (error < 0)
84 return error;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +010085
Christoph Hellwige01580b2013-12-20 05:16:52 -080086 if (error == 0)
Steven Whitehouse2646a1f2009-10-02 11:50:54 +010087 acl = NULL;
88
Bob Peterson733dbc12014-03-19 11:51:28 -040089 if (mode != inode->i_mode) {
90 inode->i_mode = mode;
91 mark_inode_dirty(inode);
92 }
Steven Whitehouse2646a1f2009-10-02 11:50:54 +010093 }
94
Christoph Hellwige01580b2013-12-20 05:16:52 -080095 if (acl) {
96 len = posix_acl_to_xattr(&init_user_ns, acl, NULL, 0);
97 if (len == 0)
98 return 0;
99 data = kmalloc(len, GFP_NOFS);
100 if (data == NULL)
101 return -ENOMEM;
102 error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
103 if (error < 0)
104 goto out;
105 } else {
106 data = NULL;
107 len = 0;
Steven Whitehouse106381b2009-09-29 16:26:23 +0100108 }
Christoph Hellwige01580b2013-12-20 05:16:52 -0800109
110 error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
111 if (error)
112 goto out;
113
114 if (acl)
115 set_cached_acl(inode, type, acl);
116 else
117 forget_cached_acl(inode, type);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100118out:
Christoph Hellwige01580b2013-12-20 05:16:52 -0800119 kfree(data);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100120 return error;
121}