blob: 776717f1eeea440cfe5897712e23139dfa1f99f3 [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"
Al Viro1a39ba92016-05-13 03:59:17 +020027#include "rgrp.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000028#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050029#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000030
Steven Whitehouse479c4272009-10-02 12:00:00 +010031static const char *gfs2_acl_name(int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +000032{
Steven Whitehouse479c4272009-10-02 12:00:00 +010033 switch (type) {
34 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010035 return XATTR_POSIX_ACL_ACCESS;
Steven Whitehouse479c4272009-10-02 12:00:00 +010036 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010037 return XATTR_POSIX_ACL_DEFAULT;
Steven Whitehouse479c4272009-10-02 12:00:00 +010038 }
39 return NULL;
40}
David Teiglandb3b94fa2006-01-16 16:50:04 +000041
Al Viro1a39ba92016-05-13 03:59:17 +020042static struct posix_acl *__gfs2_get_acl(struct inode *inode, int type)
Steven Whitehouse479c4272009-10-02 12:00:00 +010043{
Steven Whitehouse018a01c2011-11-23 13:31:51 +000044 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse479c4272009-10-02 12:00:00 +010045 struct posix_acl *acl;
46 const char *name;
47 char *data;
48 int len;
Steven Whitehouse40b78a32009-08-26 18:41:32 +010049
Steven Whitehouse3767ac22008-11-03 14:28:42 +000050 if (!ip->i_eattr)
Steven Whitehouse479c4272009-10-02 12:00:00 +010051 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000052
Steven Whitehouse479c4272009-10-02 12:00:00 +010053 name = gfs2_acl_name(type);
Steven Whitehouse479c4272009-10-02 12:00:00 +010054 len = gfs2_xattr_acl_get(ip, name, &data);
Al Viro1a39ba92016-05-13 03:59:17 +020055 if (len <= 0)
Steven Whitehouse479c4272009-10-02 12:00:00 +010056 return ERR_PTR(len);
Eric W. Biederman5f3a4a282012-09-10 20:17:44 -070057 acl = posix_acl_from_xattr(&init_user_ns, data, len);
Steven Whitehouse479c4272009-10-02 12:00:00 +010058 kfree(data);
59 return acl;
David Teiglandb3b94fa2006-01-16 16:50:04 +000060}
61
Al Viro1a39ba92016-05-13 03:59:17 +020062struct posix_acl *gfs2_get_acl(struct inode *inode, int type)
63{
64 struct gfs2_inode *ip = GFS2_I(inode);
65 struct gfs2_holder gh;
66 bool need_unlock = false;
67 struct posix_acl *acl;
68
69 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
70 int ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
71 LM_FLAG_ANY, &gh);
72 if (ret)
73 return ERR_PTR(ret);
74 need_unlock = true;
75 }
76 acl = __gfs2_get_acl(inode, type);
77 if (need_unlock)
78 gfs2_glock_dq_uninit(&gh);
79 return acl;
80}
81
82int __gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +000083{
David Teiglandb3b94fa2006-01-16 16:50:04 +000084 int error;
Steven Whitehouse479c4272009-10-02 12:00:00 +010085 int len;
86 char *data;
87 const char *name = gfs2_acl_name(type);
88
Christoph Hellwige01580b2013-12-20 05:16:52 -080089 if (acl) {
90 len = posix_acl_to_xattr(&init_user_ns, acl, NULL, 0);
91 if (len == 0)
92 return 0;
93 data = kmalloc(len, GFP_NOFS);
94 if (data == NULL)
95 return -ENOMEM;
96 error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
97 if (error < 0)
98 goto out;
99 } else {
100 data = NULL;
101 len = 0;
Steven Whitehouse106381b2009-09-29 16:26:23 +0100102 }
Christoph Hellwige01580b2013-12-20 05:16:52 -0800103
104 error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
105 if (error)
106 goto out;
Andreas Gruenbacher932e4682015-02-13 12:16:37 -0600107 set_cached_acl(inode, type, acl);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100108out:
Christoph Hellwige01580b2013-12-20 05:16:52 -0800109 kfree(data);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100110 return error;
111}
Al Viro1a39ba92016-05-13 03:59:17 +0200112
113int gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
114{
115 struct gfs2_inode *ip = GFS2_I(inode);
116 struct gfs2_holder gh;
117 bool need_unlock = false;
118 int ret;
Ernesto A. Fernández309e8cd2017-08-31 07:53:15 -0500119 umode_t mode;
Al Viro1a39ba92016-05-13 03:59:17 +0200120
Jan Kara914cea92017-07-19 10:56:42 -0500121 if (acl && acl->a_count > GFS2_ACL_MAX_ENTRIES(GFS2_SB(inode)))
122 return -E2BIG;
123
Al Viro1a39ba92016-05-13 03:59:17 +0200124 ret = gfs2_rsqa_alloc(ip);
125 if (ret)
126 return ret;
127
128 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
129 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
130 if (ret)
131 return ret;
132 need_unlock = true;
133 }
Jan Kara914cea92017-07-19 10:56:42 -0500134
Ernesto A. Fernández309e8cd2017-08-31 07:53:15 -0500135 mode = inode->i_mode;
136 if (type == ACL_TYPE_ACCESS && acl) {
137 ret = posix_acl_update_mode(inode, &mode, &acl);
Jan Kara914cea92017-07-19 10:56:42 -0500138 if (ret)
139 goto unlock;
Jan Kara914cea92017-07-19 10:56:42 -0500140 }
141
Al Viro1a39ba92016-05-13 03:59:17 +0200142 ret = __gfs2_set_acl(inode, acl, type);
Ernesto A. Fernández309e8cd2017-08-31 07:53:15 -0500143 if (!ret && mode != inode->i_mode) {
Andreas Gruenbacherc2c4be22017-09-25 08:37:15 -0500144 inode->i_ctime = current_time(inode);
Ernesto A. Fernández309e8cd2017-08-31 07:53:15 -0500145 inode->i_mode = mode;
146 mark_inode_dirty(inode);
147 }
Jan Kara914cea92017-07-19 10:56:42 -0500148unlock:
Al Viro1a39ba92016-05-13 03:59:17 +0200149 if (need_unlock)
150 gfs2_glock_dq_uninit(&gh);
151 return ret;
152}