blob: 734d1f05d8236b3160861221f86d4e9a1bb77555 [file] [log] [blame]
Thomas Gleixner7336d0e2019-05-31 01:09:56 -07001// SPDX-License-Identifier: GPL-2.0-only
David Teiglandb3b94fa2006-01-16 16:50:04 +00002/*
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04004 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00005 */
6
7#include <linux/sched.h>
8#include <linux/slab.h>
9#include <linux/spinlock.h>
10#include <linux/completion.h>
11#include <linux/buffer_head.h>
Steven Whitehouse2646a1f2009-10-02 11:50:54 +010012#include <linux/xattr.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000013#include <linux/posix_acl.h>
14#include <linux/posix_acl_xattr.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050015#include <linux/gfs2_ondisk.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000016
17#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050018#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000019#include "acl.h"
Steven Whitehouse307cf6e2009-08-26 18:51:04 +010020#include "xattr.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000021#include "glock.h"
22#include "inode.h"
23#include "meta_io.h"
Bob Petersond5807122020-03-06 10:18:44 -060024#include "quota.h"
Al Viro1a39ba92016-05-13 03:59:17 +020025#include "rgrp.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000026#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050027#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000028
Steven Whitehouse479c4272009-10-02 12:00:00 +010029static const char *gfs2_acl_name(int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +000030{
Steven Whitehouse479c4272009-10-02 12:00:00 +010031 switch (type) {
32 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010033 return XATTR_POSIX_ACL_ACCESS;
Steven Whitehouse479c4272009-10-02 12:00:00 +010034 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010035 return XATTR_POSIX_ACL_DEFAULT;
Steven Whitehouse479c4272009-10-02 12:00:00 +010036 }
37 return NULL;
38}
David Teiglandb3b94fa2006-01-16 16:50:04 +000039
Al Viro1a39ba92016-05-13 03:59:17 +020040static struct posix_acl *__gfs2_get_acl(struct inode *inode, int type)
Steven Whitehouse479c4272009-10-02 12:00:00 +010041{
Steven Whitehouse018a01c2011-11-23 13:31:51 +000042 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse479c4272009-10-02 12:00:00 +010043 struct posix_acl *acl;
44 const char *name;
45 char *data;
46 int len;
Steven Whitehouse40b78a32009-08-26 18:41:32 +010047
Steven Whitehouse3767ac22008-11-03 14:28:42 +000048 if (!ip->i_eattr)
Steven Whitehouse479c4272009-10-02 12:00:00 +010049 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000050
Steven Whitehouse479c4272009-10-02 12:00:00 +010051 name = gfs2_acl_name(type);
Steven Whitehouse479c4272009-10-02 12:00:00 +010052 len = gfs2_xattr_acl_get(ip, name, &data);
Al Viro1a39ba92016-05-13 03:59:17 +020053 if (len <= 0)
Steven Whitehouse479c4272009-10-02 12:00:00 +010054 return ERR_PTR(len);
Eric W. Biederman5f3a4a282012-09-10 20:17:44 -070055 acl = posix_acl_from_xattr(&init_user_ns, data, len);
Steven Whitehouse479c4272009-10-02 12:00:00 +010056 kfree(data);
57 return acl;
David Teiglandb3b94fa2006-01-16 16:50:04 +000058}
59
Miklos Szeredi0cad6242021-08-18 22:08:24 +020060struct posix_acl *gfs2_get_acl(struct inode *inode, int type, bool rcu)
Al Viro1a39ba92016-05-13 03:59:17 +020061{
62 struct gfs2_inode *ip = GFS2_I(inode);
63 struct gfs2_holder gh;
64 bool need_unlock = false;
65 struct posix_acl *acl;
66
Miklos Szeredi0cad6242021-08-18 22:08:24 +020067 if (rcu)
68 return ERR_PTR(-ECHILD);
69
Al Viro1a39ba92016-05-13 03:59:17 +020070 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
71 int ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
72 LM_FLAG_ANY, &gh);
73 if (ret)
74 return ERR_PTR(ret);
75 need_unlock = true;
76 }
77 acl = __gfs2_get_acl(inode, type);
78 if (need_unlock)
79 gfs2_glock_dq_uninit(&gh);
80 return acl;
81}
82
83int __gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +000084{
David Teiglandb3b94fa2006-01-16 16:50:04 +000085 int error;
Chengguang Xu910f3d52018-06-22 09:51:43 +080086 size_t len;
Steven Whitehouse479c4272009-10-02 12:00:00 +010087 char *data;
88 const char *name = gfs2_acl_name(type);
89
Christoph Hellwige01580b2013-12-20 05:16:52 -080090 if (acl) {
Chengguang Xu910f3d52018-06-22 09:51:43 +080091 len = posix_acl_xattr_size(acl->a_count);
Christoph Hellwige01580b2013-12-20 05:16:52 -080092 data = kmalloc(len, GFP_NOFS);
93 if (data == NULL)
94 return -ENOMEM;
95 error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
96 if (error < 0)
97 goto out;
98 } else {
99 data = NULL;
100 len = 0;
Steven Whitehouse106381b2009-09-29 16:26:23 +0100101 }
Christoph Hellwige01580b2013-12-20 05:16:52 -0800102
103 error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
104 if (error)
105 goto out;
Andreas Gruenbacher932e4682015-02-13 12:16:37 -0600106 set_cached_acl(inode, type, acl);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100107out:
Christoph Hellwige01580b2013-12-20 05:16:52 -0800108 kfree(data);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100109 return error;
110}
Al Viro1a39ba92016-05-13 03:59:17 +0200111
Christian Brauner549c7292021-01-21 14:19:43 +0100112int gfs2_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
113 struct posix_acl *acl, int type)
Al Viro1a39ba92016-05-13 03:59:17 +0200114{
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
Bob Peterson2fba46a2020-02-27 12:47:53 -0600124 ret = gfs2_qa_get(ip);
Al Viro1a39ba92016-05-13 03:59:17 +0200125 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)
Bob Peterson2fba46a2020-02-27 12:47:53 -0600131 goto out;
Al Viro1a39ba92016-05-13 03:59:17 +0200132 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) {
Christian Braunere65ce2a2021-01-21 14:19:27 +0100137 ret = posix_acl_update_mode(&init_user_ns, 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);
Bob Peterson2fba46a2020-02-27 12:47:53 -0600151out:
152 gfs2_qa_put(ip);
Al Viro1a39ba92016-05-13 03:59:17 +0200153 return ret;
154}