Blame view

fs/jfs/acl.c 4.56 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
63f83c9fc   Dave Kleikamp   JFS: White space ...
8
   *   the Free Software Foundation; either version 2 of the License, or
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
   *   (at your option) any later version.
63f83c9fc   Dave Kleikamp   JFS: White space ...
10
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
63f83c9fc   Dave Kleikamp   JFS: White space ...
17
   *   along with this program;  if not, write to the Free Software
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
21
   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   */
  
  #include <linux/sched.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
22
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
  #include <linux/fs.h>
9a59f452a   Christoph Hellwig   [PATCH] remove <l...
24
  #include <linux/posix_acl_xattr.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
  #include "jfs_incore.h"
4f4b401bf   Dave Kleikamp   JFS: allow extend...
26
  #include "jfs_txnmgr.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
30
31
32
33
  #include "jfs_xattr.h"
  #include "jfs_acl.h"
  
  static struct posix_acl *jfs_get_acl(struct inode *inode, int type)
  {
  	struct posix_acl *acl;
  	char *ea_name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
  	int size;
  	char *value = NULL;
073aaa1b1   Al Viro   helpers for acl c...
36
37
38
  	acl = get_cached_acl(inode, type);
  	if (acl != ACL_NOT_CACHED)
  		return acl;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
  	switch(type) {
  		case ACL_TYPE_ACCESS:
9a59f452a   Christoph Hellwig   [PATCH] remove <l...
41
  			ea_name = POSIX_ACL_XATTR_ACCESS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
43
  			break;
  		case ACL_TYPE_DEFAULT:
9a59f452a   Christoph Hellwig   [PATCH] remove <l...
44
  			ea_name = POSIX_ACL_XATTR_DEFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
  			break;
  		default:
  			return ERR_PTR(-EINVAL);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
51
52
53
54
55
56
57
58
  	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) {
073aaa1b1   Al Viro   helpers for acl c...
59
  		if (size == -ENODATA)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
  			acl = NULL;
073aaa1b1   Al Viro   helpers for acl c...
61
  		else
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
64
  			acl = ERR_PTR(size);
  	} else {
  		acl = posix_acl_from_xattr(value, size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
  	}
259692bd5   Jesper Juhl   JFS: Remove redun...
66
  	kfree(value);
4a19fb11a   Stefan Bader   jfs: Fix early re...
67
  	if (!IS_ERR(acl))
073aaa1b1   Al Viro   helpers for acl c...
68
  		set_cached_acl(inode, type, acl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
  	return acl;
  }
4f4b401bf   Dave Kleikamp   JFS: allow extend...
71
72
  static int jfs_set_acl(tid_t tid, struct inode *inode, int type,
  		       struct posix_acl *acl)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
74
  {
  	char *ea_name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
76
77
78
79
80
81
82
83
  	int rc;
  	int size = 0;
  	char *value = NULL;
  
  	if (S_ISLNK(inode->i_mode))
  		return -EOPNOTSUPP;
  
  	switch(type) {
  		case ACL_TYPE_ACCESS:
9a59f452a   Christoph Hellwig   [PATCH] remove <l...
84
  			ea_name = POSIX_ACL_XATTR_ACCESS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
86
  			break;
  		case ACL_TYPE_DEFAULT:
9a59f452a   Christoph Hellwig   [PATCH] remove <l...
87
  			ea_name = POSIX_ACL_XATTR_DEFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
89
90
91
92
93
94
  			if (!S_ISDIR(inode->i_mode))
  				return acl ? -EACCES : 0;
  			break;
  		default:
  			return -EINVAL;
  	}
  	if (acl) {
9a59f452a   Christoph Hellwig   [PATCH] remove <l...
95
  		size = posix_acl_xattr_size(acl->a_count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
97
98
99
100
101
102
  		value = kmalloc(size, GFP_KERNEL);
  		if (!value)
  			return -ENOMEM;
  		rc = posix_acl_to_xattr(acl, value, size);
  		if (rc < 0)
  			goto out;
  	}
4f4b401bf   Dave Kleikamp   JFS: allow extend...
103
  	rc = __jfs_setxattr(tid, inode, ea_name, value, size, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
  out:
259692bd5   Jesper Juhl   JFS: Remove redun...
105
  	kfree(value);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106

073aaa1b1   Al Viro   helpers for acl c...
107
108
  	if (!rc)
  		set_cached_acl(inode, type, acl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
  	return rc;
  }
18f4c6447   Linus Torvalds   jffs2/jfs/xfs: sw...
111
  int jfs_check_acl(struct inode *inode, int mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
  {
d5bb68add   Al Viro   another race fix ...
113
114
115
116
117
118
  	struct posix_acl *acl = jfs_get_acl(inode, ACL_TYPE_ACCESS);
  
  	if (IS_ERR(acl))
  		return PTR_ERR(acl);
  	if (acl) {
  		int error = posix_acl_permission(inode, acl, mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
  		posix_acl_release(acl);
d5bb68add   Al Viro   another race fix ...
120
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
  	return -EAGAIN;
  }
4f4b401bf   Dave Kleikamp   JFS: allow extend...
124
  int jfs_init_acl(tid_t tid, struct inode *inode, struct inode *dir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  {
  	struct posix_acl *acl = NULL;
  	struct posix_acl *clone;
  	mode_t mode;
  	int rc = 0;
  
  	if (S_ISLNK(inode->i_mode))
  		return 0;
  
  	acl = jfs_get_acl(dir, ACL_TYPE_DEFAULT);
  	if (IS_ERR(acl))
  		return PTR_ERR(acl);
  
  	if (acl) {
  		if (S_ISDIR(inode->i_mode)) {
4f4b401bf   Dave Kleikamp   JFS: allow extend...
140
  			rc = jfs_set_acl(tid, inode, ACL_TYPE_DEFAULT, acl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
142
143
144
145
146
147
148
149
150
151
152
153
  			if (rc)
  				goto cleanup;
  		}
  		clone = posix_acl_clone(acl, GFP_KERNEL);
  		if (!clone) {
  			rc = -ENOMEM;
  			goto cleanup;
  		}
  		mode = inode->i_mode;
  		rc = posix_acl_create_masq(clone, &mode);
  		if (rc >= 0) {
  			inode->i_mode = mode;
  			if (rc > 0)
4f4b401bf   Dave Kleikamp   JFS: allow extend...
154
155
  				rc = jfs_set_acl(tid, inode, ACL_TYPE_ACCESS,
  						 clone);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
159
160
  		}
  		posix_acl_release(clone);
  cleanup:
  		posix_acl_release(acl);
  	} else
ce3b0f8d5   Al Viro   New helper - curr...
161
  		inode->i_mode &= ~current_umask();
63f83c9fc   Dave Kleikamp   JFS: White space ...
162

69eb66d7d   Dave Kleikamp   JFS: add uid, gid...
163
164
  	JFS_IP(inode)->mode2 = (JFS_IP(inode)->mode2 & 0xffff0000) |
  			       inode->i_mode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
166
167
  
  	return rc;
  }
759bfee65   Christoph Hellwig   dquot: move dquot...
168
  int jfs_acl_chmod(struct inode *inode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  {
  	struct posix_acl *acl, *clone;
  	int rc;
  
  	if (S_ISLNK(inode->i_mode))
  		return -EOPNOTSUPP;
  
  	acl = jfs_get_acl(inode, ACL_TYPE_ACCESS);
  	if (IS_ERR(acl) || !acl)
  		return PTR_ERR(acl);
  
  	clone = posix_acl_clone(acl, GFP_KERNEL);
  	posix_acl_release(acl);
  	if (!clone)
  		return -ENOMEM;
  
  	rc = posix_acl_chmod_masq(clone, inode->i_mode);
4f4b401bf   Dave Kleikamp   JFS: allow extend...
186
187
  	if (!rc) {
  		tid_t tid = txBegin(inode->i_sb, 0);
1de87444f   Ingo Molnar   JFS: semaphore to...
188
  		mutex_lock(&JFS_IP(inode)->commit_mutex);
4f4b401bf   Dave Kleikamp   JFS: allow extend...
189
190
191
192
  		rc = jfs_set_acl(tid, inode, ACL_TYPE_ACCESS, clone);
  		if (!rc)
  			rc = txCommit(tid, 1, &inode, 0);
  		txEnd(tid);
1de87444f   Ingo Molnar   JFS: semaphore to...
193
  		mutex_unlock(&JFS_IP(inode)->commit_mutex);
4f4b401bf   Dave Kleikamp   JFS: allow extend...
194
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
195
196
197
198
  
  	posix_acl_release(clone);
  	return rc;
  }