blob: ba9456685f47d761d51afae92a55328e2d54d78a [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
Al Virod3fb6122011-07-23 18:37:50 -040067static int gfs2_set_mode(struct inode *inode, umode_t mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +000068{
Steven Whitehouse69dca422009-09-29 12:40:19 +010069 int error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +000070
Steven Whitehouse69dca422009-09-29 12:40:19 +010071 if (mode != inode->i_mode) {
Steven Whitehousef9425ad2012-05-04 14:33:06 +010072 inode->i_mode = mode;
73 mark_inode_dirty(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000074 }
75
Steven Whitehouse69dca422009-09-29 12:40:19 +010076 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +000077}
78
Christoph Hellwige01580b2013-12-20 05:16:52 -080079int gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +000080{
David Teiglandb3b94fa2006-01-16 16:50:04 +000081 int error;
Steven Whitehouse479c4272009-10-02 12:00:00 +010082 int len;
83 char *data;
84 const char *name = gfs2_acl_name(type);
85
86 BUG_ON(name == NULL);
Steven Whitehouse479c4272009-10-02 12:00:00 +010087
Steven Whitehouse2646a1f2009-10-02 11:50:54 +010088 if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
Christoph Hellwige01580b2013-12-20 05:16:52 -080089 return -EINVAL;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +010090
91 if (type == ACL_TYPE_ACCESS) {
Al Virod6952122011-07-23 18:56:36 -040092 umode_t mode = inode->i_mode;
Christoph Hellwige01580b2013-12-20 05:16:52 -080093
Steven Whitehouse2646a1f2009-10-02 11:50:54 +010094 error = posix_acl_equiv_mode(acl, &mode);
Christoph Hellwige01580b2013-12-20 05:16:52 -080095 if (error < 0)
96 return error;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +010097
Christoph Hellwige01580b2013-12-20 05:16:52 -080098 if (error == 0)
Steven Whitehouse2646a1f2009-10-02 11:50:54 +010099 acl = NULL;
100
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100101 error = gfs2_set_mode(inode, mode);
102 if (error)
Christoph Hellwige01580b2013-12-20 05:16:52 -0800103 return error;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100104 }
105
Christoph Hellwige01580b2013-12-20 05:16:52 -0800106 if (acl) {
107 len = posix_acl_to_xattr(&init_user_ns, acl, NULL, 0);
108 if (len == 0)
109 return 0;
110 data = kmalloc(len, GFP_NOFS);
111 if (data == NULL)
112 return -ENOMEM;
113 error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
114 if (error < 0)
115 goto out;
116 } else {
117 data = NULL;
118 len = 0;
Steven Whitehouse106381b2009-09-29 16:26:23 +0100119 }
Christoph Hellwige01580b2013-12-20 05:16:52 -0800120
121 error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
122 if (error)
123 goto out;
124
125 if (acl)
126 set_cached_acl(inode, type, acl);
127 else
128 forget_cached_acl(inode, type);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100129out:
Christoph Hellwige01580b2013-12-20 05:16:52 -0800130 kfree(data);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100131 return error;
132}