acl.c 2.6 KB
Newer Older
D
David Teigland 已提交
1 2
/*
 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3
 * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
D
David Teigland 已提交
4 5 6
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
7
 * of the GNU General Public License version 2.
D
David Teigland 已提交
8 9 10 11 12 13 14
 */

#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/buffer_head.h>
S
Steven Whitehouse 已提交
15
#include <linux/xattr.h>
D
David Teigland 已提交
16 17
#include <linux/posix_acl.h>
#include <linux/posix_acl_xattr.h>
18
#include <linux/gfs2_ondisk.h>
D
David Teigland 已提交
19 20

#include "gfs2.h"
21
#include "incore.h"
D
David Teigland 已提交
22
#include "acl.h"
23
#include "xattr.h"
D
David Teigland 已提交
24 25 26 27
#include "glock.h"
#include "inode.h"
#include "meta_io.h"
#include "trans.h"
28
#include "util.h"
D
David Teigland 已提交
29

S
Steven Whitehouse 已提交
30
static const char *gfs2_acl_name(int type)
D
David Teigland 已提交
31
{
S
Steven Whitehouse 已提交
32 33 34 35 36 37 38 39
	switch (type) {
	case ACL_TYPE_ACCESS:
		return GFS2_POSIX_ACL_ACCESS;
	case ACL_TYPE_DEFAULT:
		return GFS2_POSIX_ACL_DEFAULT;
	}
	return NULL;
}
D
David Teigland 已提交
40

41
struct posix_acl *gfs2_get_acl(struct inode *inode, int type)
S
Steven Whitehouse 已提交
42
{
43
	struct gfs2_inode *ip = GFS2_I(inode);
S
Steven Whitehouse 已提交
44 45 46 47
	struct posix_acl *acl;
	const char *name;
	char *data;
	int len;
48

49
	if (!ip->i_eattr)
S
Steven Whitehouse 已提交
50
		return NULL;
D
David Teigland 已提交
51

S
Steven Whitehouse 已提交
52 53 54
	name = gfs2_acl_name(type);
	if (name == NULL)
		return ERR_PTR(-EINVAL);
D
David Teigland 已提交
55

S
Steven Whitehouse 已提交
56 57 58 59 60
	len = gfs2_xattr_acl_get(ip, name, &data);
	if (len < 0)
		return ERR_PTR(len);
	if (len == 0)
		return NULL;
D
David Teigland 已提交
61

62
	acl = posix_acl_from_xattr(&init_user_ns, data, len);
S
Steven Whitehouse 已提交
63 64
	kfree(data);
	return acl;
D
David Teigland 已提交
65 66
}

A
Al Viro 已提交
67
static int gfs2_set_mode(struct inode *inode, umode_t mode)
D
David Teigland 已提交
68
{
69
	int error = 0;
D
David Teigland 已提交
70

71
	if (mode != inode->i_mode) {
72 73
		inode->i_mode = mode;
		mark_inode_dirty(inode);
74
	}
D
David Teigland 已提交
75

76
	return error;
D
David Teigland 已提交
77 78
}

79
int gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
D
David Teigland 已提交
80 81
{
	int error;
S
Steven Whitehouse 已提交
82 83 84 85 86
	int len;
	char *data;
	const char *name = gfs2_acl_name(type);

	BUG_ON(name == NULL);
87

S
Steven Whitehouse 已提交
88
	if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
89
		return -E2BIG;
S
Steven Whitehouse 已提交
90 91

	if (type == ACL_TYPE_ACCESS) {
92
		umode_t mode = inode->i_mode;
93

S
Steven Whitehouse 已提交
94
		error = posix_acl_equiv_mode(acl, &mode);
95 96
		if (error < 0)
			return error;
S
Steven Whitehouse 已提交
97

98
		if (error == 0)
S
Steven Whitehouse 已提交
99 100 101 102
			acl = NULL;

		error = gfs2_set_mode(inode, mode);
		if (error)
103
			return error;
S
Steven Whitehouse 已提交
104 105
	}

106 107 108 109 110 111 112 113 114 115 116 117 118
	if (acl) {
		len = posix_acl_to_xattr(&init_user_ns, acl, NULL, 0);
		if (len == 0)
			return 0;
		data = kmalloc(len, GFP_NOFS);
		if (data == NULL)
			return -ENOMEM;
		error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
		if (error < 0)
			goto out;
	} else {
		data = NULL;
		len = 0;
S
Steven Whitehouse 已提交
119
	}
120 121 122 123 124 125 126 127 128

	error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
	if (error)
		goto out;

	if (acl)
		set_cached_acl(inode, type, acl);
	else
		forget_cached_acl(inode, type);
S
Steven Whitehouse 已提交
129
out:
130
	kfree(data);
S
Steven Whitehouse 已提交
131 132
	return error;
}