blob: 6fa5a66f806339130f4c1684a79d6a90de4b822c [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. Biederman5f3a4a22012-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
Andrew Elble27870202015-02-09 12:53:04 -050089 if (acl && acl->a_count > GFS2_ACL_MAX_ENTRIES(GFS2_SB(inode)))
Jie Liuf2113eb2014-03-04 11:28:39 +080090 return -E2BIG;
Steven Whitehouse2646a1f2009-10-02 11:50:54 +010091
92 if (type == ACL_TYPE_ACCESS) {
Al Virod6952122011-07-23 18:56:36 -040093 umode_t mode = inode->i_mode;
Mark Salyzyne5f31bd2017-01-23 12:56:41 -080094 struct posix_acl *old_acl = acl;
Jan Kara07393102016-09-19 17:39:09 +020095 error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
Mark Salyzyne5f31bd2017-01-23 12:56:41 -080096
97 if (!acl)
98 posix_acl_release(old_acl);
Jan Kara07393102016-09-19 17:39:09 +020099 if (error)
Christoph Hellwige01580b2013-12-20 05:16:52 -0800100 return error;
Jan Kara07393102016-09-19 17:39:09 +0200101 if (mode != inode->i_mode)
Bob Peterson733dbc12014-03-19 11:51:28 -0400102 mark_inode_dirty(inode);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100103 }
104
Christoph Hellwige01580b2013-12-20 05:16:52 -0800105 if (acl) {
106 len = posix_acl_to_xattr(&init_user_ns, acl, NULL, 0);
107 if (len == 0)
108 return 0;
109 data = kmalloc(len, GFP_NOFS);
110 if (data == NULL)
111 return -ENOMEM;
112 error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
113 if (error < 0)
114 goto out;
115 } else {
116 data = NULL;
117 len = 0;
Steven Whitehouse106381b2009-09-29 16:26:23 +0100118 }
Christoph Hellwige01580b2013-12-20 05:16:52 -0800119
120 error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
121 if (error)
122 goto out;
Andreas Gruenbacher932e4682015-02-13 12:16:37 -0600123 set_cached_acl(inode, type, acl);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100124out:
Christoph Hellwige01580b2013-12-20 05:16:52 -0800125 kfree(data);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100126 return error;
127}
Al Viro1a39ba92016-05-13 03:59:17 +0200128
129int gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
130{
131 struct gfs2_inode *ip = GFS2_I(inode);
132 struct gfs2_holder gh;
133 bool need_unlock = false;
134 int ret;
135
136 ret = gfs2_rsqa_alloc(ip);
137 if (ret)
138 return ret;
139
140 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
141 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
142 if (ret)
143 return ret;
144 need_unlock = true;
145 }
146 ret = __gfs2_set_acl(inode, acl, type);
147 if (need_unlock)
148 gfs2_glock_dq_uninit(&gh);
149 return ret;
150}