acl.c 3.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
 *   Copyright (C) International Business Machines  Corp., 2002-2004
 *   Copyright (C) Andreas Gruenbacher, 2001
 *   Copyright (C) Linus Torvalds, 1991, 1992
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
D
Dave Kleikamp 已提交
8
 *   the Free Software Foundation; either version 2 of the License, or
L
Linus Torvalds 已提交
9
 *   (at your option) any later version.
D
Dave Kleikamp 已提交
10
 *
L
Linus Torvalds 已提交
11 12 13 14 15 16
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
D
Dave Kleikamp 已提交
17
 *   along with this program;  if not, write to the Free Software
L
Linus Torvalds 已提交
18 19 20 21
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/sched.h>
22
#include <linux/slab.h>
L
Linus Torvalds 已提交
23
#include <linux/fs.h>
24
#include <linux/posix_acl_xattr.h>
L
Linus Torvalds 已提交
25
#include "jfs_incore.h"
26
#include "jfs_txnmgr.h"
L
Linus Torvalds 已提交
27 28 29
#include "jfs_xattr.h"
#include "jfs_acl.h"

30
struct posix_acl *jfs_get_acl(struct inode *inode, int type)
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38
{
	struct posix_acl *acl;
	char *ea_name;
	int size;
	char *value = NULL;

	switch(type) {
		case ACL_TYPE_ACCESS:
39
			ea_name = XATTR_NAME_POSIX_ACL_ACCESS;
L
Linus Torvalds 已提交
40 41
			break;
		case ACL_TYPE_DEFAULT:
42
			ea_name = XATTR_NAME_POSIX_ACL_DEFAULT;
L
Linus Torvalds 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
			break;
		default:
			return ERR_PTR(-EINVAL);
	}

	size = __jfs_getxattr(inode, ea_name, NULL, 0);

	if (size > 0) {
		value = kmalloc(size, GFP_KERNEL);
		if (!value)
			return ERR_PTR(-ENOMEM);
		size = __jfs_getxattr(inode, ea_name, value, size);
	}

	if (size < 0) {
58
		if (size == -ENODATA)
L
Linus Torvalds 已提交
59
			acl = NULL;
60
		else
L
Linus Torvalds 已提交
61 62
			acl = ERR_PTR(size);
	} else {
63
		acl = posix_acl_from_xattr(&init_user_ns, value, size);
L
Linus Torvalds 已提交
64
	}
65
	kfree(value);
L
Linus Torvalds 已提交
66 67 68
	return acl;
}

69
static int __jfs_set_acl(tid_t tid, struct inode *inode, int type,
70
		       struct posix_acl *acl)
L
Linus Torvalds 已提交
71 72 73 74 75 76
{
	char *ea_name;
	int rc;
	int size = 0;
	char *value = NULL;

77 78
	switch (type) {
	case ACL_TYPE_ACCESS:
79
		ea_name = XATTR_NAME_POSIX_ACL_ACCESS;
80
		if (acl) {
81 82
			rc = posix_acl_update_mode(inode, &inode->i_mode, &acl);
			if (rc)
83 84 85 86
				return rc;
			inode->i_ctime = CURRENT_TIME;
			mark_inode_dirty(inode);
		}
87 88
		break;
	case ACL_TYPE_DEFAULT:
89
		ea_name = XATTR_NAME_POSIX_ACL_DEFAULT;
90 91 92
		break;
	default:
		return -EINVAL;
L
Linus Torvalds 已提交
93
	}
94

L
Linus Torvalds 已提交
95
	if (acl) {
96
		size = posix_acl_xattr_size(acl->a_count);
L
Linus Torvalds 已提交
97 98 99
		value = kmalloc(size, GFP_KERNEL);
		if (!value)
			return -ENOMEM;
100
		rc = posix_acl_to_xattr(&init_user_ns, acl, value, size);
L
Linus Torvalds 已提交
101 102 103
		if (rc < 0)
			goto out;
	}
104
	rc = __jfs_setxattr(tid, inode, ea_name, value, size, 0);
L
Linus Torvalds 已提交
105
out:
106
	kfree(value);
L
Linus Torvalds 已提交
107

108 109 110
	if (!rc)
		set_cached_acl(inode, type, acl);

L
Linus Torvalds 已提交
111 112 113
	return rc;
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
int jfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
{
	int rc;
	tid_t tid;

	tid = txBegin(inode->i_sb, 0);
	mutex_lock(&JFS_IP(inode)->commit_mutex);
	rc = __jfs_set_acl(tid, inode, type, acl);
	if (!rc)
		rc = txCommit(tid, 1, &inode, 0);
	txEnd(tid);
	mutex_unlock(&JFS_IP(inode)->commit_mutex);
	return rc;
}

129
int jfs_init_acl(tid_t tid, struct inode *inode, struct inode *dir)
L
Linus Torvalds 已提交
130
{
131
	struct posix_acl *default_acl, *acl;
L
Linus Torvalds 已提交
132 133
	int rc = 0;

134 135 136
	rc = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
	if (rc)
		return rc;
L
Linus Torvalds 已提交
137

138 139 140 141
	if (default_acl) {
		rc = __jfs_set_acl(tid, inode, ACL_TYPE_DEFAULT, default_acl);
		posix_acl_release(default_acl);
	}
L
Linus Torvalds 已提交
142 143

	if (acl) {
144 145
		if (!rc)
			rc = __jfs_set_acl(tid, inode, ACL_TYPE_ACCESS, acl);
L
Linus Torvalds 已提交
146
		posix_acl_release(acl);
147
	}
D
Dave Kleikamp 已提交
148

149 150
	JFS_IP(inode)->mode2 = (JFS_IP(inode)->mode2 & 0xffff0000) |
			       inode->i_mode;
L
Linus Torvalds 已提交
151 152 153

	return rc;
}