blob: af5f87a493d9b6043fa8ef14a38a0f3e6784a979 [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;
Chengguang Xu910f3d52018-06-22 09:51:43 +080085 size_t len;
Steven Whitehouse479c4272009-10-02 12:00:00 +010086 char *data;
87 const char *name = gfs2_acl_name(type);
88
Christoph Hellwige01580b2013-12-20 05:16:52 -080089 if (acl) {
Chengguang Xu910f3d52018-06-22 09:51:43 +080090 len = posix_acl_xattr_size(acl->a_count);
Christoph Hellwige01580b2013-12-20 05:16:52 -080091 data = kmalloc(len, GFP_NOFS);
92 if (data == NULL)
93 return -ENOMEM;
94 error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
95 if (error < 0)
96 goto out;
97 } else {
98 data = NULL;
99 len = 0;
Steven Whitehouse106381b2009-09-29 16:26:23 +0100100 }
Christoph Hellwige01580b2013-12-20 05:16:52 -0800101
102 error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
103 if (error)
104 goto out;
Andreas Gruenbacher932e4682015-02-13 12:16:37 -0600105 set_cached_acl(inode, type, acl);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100106out:
Christoph Hellwige01580b2013-12-20 05:16:52 -0800107 kfree(data);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100108 return error;
109}
Al Viro1a39ba92016-05-13 03:59:17 +0200110
111int gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
112{
113 struct gfs2_inode *ip = GFS2_I(inode);
114 struct gfs2_holder gh;
115 bool need_unlock = false;
116 int ret;
Ernesto A. Fernández309e8cd2017-08-31 07:53:15 -0500117 umode_t mode;
Al Viro1a39ba92016-05-13 03:59:17 +0200118
Jan Kara914cea92017-07-19 10:56:42 -0500119 if (acl && acl->a_count > GFS2_ACL_MAX_ENTRIES(GFS2_SB(inode)))
120 return -E2BIG;
121
Al Viro1a39ba92016-05-13 03:59:17 +0200122 ret = gfs2_rsqa_alloc(ip);
123 if (ret)
124 return ret;
125
126 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
127 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
128 if (ret)
129 return ret;
130 need_unlock = true;
131 }
Jan Kara914cea92017-07-19 10:56:42 -0500132
Ernesto A. Fernández309e8cd2017-08-31 07:53:15 -0500133 mode = inode->i_mode;
134 if (type == ACL_TYPE_ACCESS && acl) {
135 ret = posix_acl_update_mode(inode, &mode, &acl);
Jan Kara914cea92017-07-19 10:56:42 -0500136 if (ret)
137 goto unlock;
Jan Kara914cea92017-07-19 10:56:42 -0500138 }
139
Al Viro1a39ba92016-05-13 03:59:17 +0200140 ret = __gfs2_set_acl(inode, acl, type);
Ernesto A. Fernández309e8cd2017-08-31 07:53:15 -0500141 if (!ret && mode != inode->i_mode) {
Andreas Gruenbacherc2c4be22017-09-25 08:37:15 -0500142 inode->i_ctime = current_time(inode);
Ernesto A. Fernández309e8cd2017-08-31 07:53:15 -0500143 inode->i_mode = mode;
144 mark_inode_dirty(inode);
145 }
Jan Kara914cea92017-07-19 10:56:42 -0500146unlock:
Al Viro1a39ba92016-05-13 03:59:17 +0200147 if (need_unlock)
148 gfs2_glock_dq_uninit(&gh);
149 return ret;
150}