blob: 2524807ee0703643867e7e04c803d10b1aab9182 [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;
Christoph Hellwige01580b2013-12-20 05:16:52 -080094
Jan Kara07393102016-09-19 17:39:09 +020095 error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
96 if (error)
Christoph Hellwige01580b2013-12-20 05:16:52 -080097 return error;
Jan Kara07393102016-09-19 17:39:09 +020098 if (mode != inode->i_mode)
Bob Peterson733dbc12014-03-19 11:51:28 -040099 mark_inode_dirty(inode);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100100 }
101
Christoph Hellwige01580b2013-12-20 05:16:52 -0800102 if (acl) {
103 len = posix_acl_to_xattr(&init_user_ns, acl, NULL, 0);
104 if (len == 0)
105 return 0;
106 data = kmalloc(len, GFP_NOFS);
107 if (data == NULL)
108 return -ENOMEM;
109 error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
110 if (error < 0)
111 goto out;
112 } else {
113 data = NULL;
114 len = 0;
Steven Whitehouse106381b2009-09-29 16:26:23 +0100115 }
Christoph Hellwige01580b2013-12-20 05:16:52 -0800116
117 error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
118 if (error)
119 goto out;
Andreas Gruenbacher932e4682015-02-13 12:16:37 -0600120 set_cached_acl(inode, type, acl);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100121out:
Christoph Hellwige01580b2013-12-20 05:16:52 -0800122 kfree(data);
Steven Whitehouse2646a1f2009-10-02 11:50:54 +0100123 return error;
124}
Al Viro1a39ba92016-05-13 03:59:17 +0200125
126int gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
127{
128 struct gfs2_inode *ip = GFS2_I(inode);
129 struct gfs2_holder gh;
130 bool need_unlock = false;
131 int ret;
132
133 ret = gfs2_rsqa_alloc(ip);
134 if (ret)
135 return ret;
136
137 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
138 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
139 if (ret)
140 return ret;
141 need_unlock = true;
142 }
143 ret = __gfs2_set_acl(inode, acl, type);
144 if (need_unlock)
145 gfs2_glock_dq_uninit(&gh);
146 return ret;
147}