diff options
author | Tiger Yang <tiger.yang@oracle.com> | 2008-11-13 22:17:04 -0500 |
---|---|---|
committer | Mark Fasheh <mfasheh@suse.com> | 2009-01-05 11:34:20 -0500 |
commit | 929fb014e041c6572c5e8c3686f1e32742b5b953 (patch) | |
tree | 149328875787dab63ec873cfa06364f4a23ac06d /fs/ocfs2/acl.c | |
parent | 4e3e9d027f63488e676bf7700ec515a192e54f69 (diff) |
ocfs2: add POSIX ACL API
This patch adds POSIX ACL(access control lists) APIs in ocfs2. We convert
struct posix_acl to many ocfs2_acl_entry and regard them as an extended
attribute entry.
Signed-off-by: Tiger Yang <tiger.yang@oracle.com>
Signed-off-by: Mark Fasheh <mfasheh@suse.com>
Diffstat (limited to 'fs/ocfs2/acl.c')
-rw-r--r-- | fs/ocfs2/acl.c | 378 |
1 files changed, 378 insertions, 0 deletions
diff --git a/fs/ocfs2/acl.c b/fs/ocfs2/acl.c new file mode 100644 index 000000000000..62d0faad600b --- /dev/null +++ b/fs/ocfs2/acl.c | |||
@@ -0,0 +1,378 @@ | |||
1 | /* -*- mode: c; c-basic-offset: 8; -*- | ||
2 | * vim: noexpandtab sw=8 ts=8 sts=0: | ||
3 | * | ||
4 | * acl.c | ||
5 | * | ||
6 | * Copyright (C) 2004, 2008 Oracle. All rights reserved. | ||
7 | * | ||
8 | * CREDITS: | ||
9 | * Lots of code in this file is copy from linux/fs/ext3/acl.c. | ||
10 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public | ||
14 | * License version 2 as published by the Free Software Foundation. | ||
15 | * | ||
16 | * This program is distributed in the hope that it will be useful, | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
19 | * General Public License for more details. | ||
20 | */ | ||
21 | |||
22 | #include <linux/init.h> | ||
23 | #include <linux/module.h> | ||
24 | #include <linux/string.h> | ||
25 | |||
26 | #define MLOG_MASK_PREFIX ML_INODE | ||
27 | #include <cluster/masklog.h> | ||
28 | |||
29 | #include "ocfs2.h" | ||
30 | #include "alloc.h" | ||
31 | #include "dlmglue.h" | ||
32 | #include "file.h" | ||
33 | #include "ocfs2_fs.h" | ||
34 | |||
35 | #include "xattr.h" | ||
36 | #include "acl.h" | ||
37 | |||
38 | /* | ||
39 | * Convert from xattr value to acl struct. | ||
40 | */ | ||
41 | static struct posix_acl *ocfs2_acl_from_xattr(const void *value, size_t size) | ||
42 | { | ||
43 | int n, count; | ||
44 | struct posix_acl *acl; | ||
45 | |||
46 | if (!value) | ||
47 | return NULL; | ||
48 | if (size < sizeof(struct posix_acl_entry)) | ||
49 | return ERR_PTR(-EINVAL); | ||
50 | |||
51 | count = size / sizeof(struct posix_acl_entry); | ||
52 | if (count < 0) | ||
53 | return ERR_PTR(-EINVAL); | ||
54 | if (count == 0) | ||
55 | return NULL; | ||
56 | |||
57 | acl = posix_acl_alloc(count, GFP_NOFS); | ||
58 | if (!acl) | ||
59 | return ERR_PTR(-ENOMEM); | ||
60 | for (n = 0; n < count; n++) { | ||
61 | struct ocfs2_acl_entry *entry = | ||
62 | (struct ocfs2_acl_entry *)value; | ||
63 | |||
64 | acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag); | ||
65 | acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm); | ||
66 | acl->a_entries[n].e_id = le32_to_cpu(entry->e_id); | ||
67 | value += sizeof(struct posix_acl_entry); | ||
68 | |||
69 | } | ||
70 | return acl; | ||
71 | } | ||
72 | |||
73 | /* | ||
74 | * Convert acl struct to xattr value. | ||
75 | */ | ||
76 | static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size) | ||
77 | { | ||
78 | struct ocfs2_acl_entry *entry = NULL; | ||
79 | char *ocfs2_acl; | ||
80 | size_t n; | ||
81 | |||
82 | *size = acl->a_count * sizeof(struct posix_acl_entry); | ||
83 | |||
84 | ocfs2_acl = kmalloc(*size, GFP_NOFS); | ||
85 | if (!ocfs2_acl) | ||
86 | return ERR_PTR(-ENOMEM); | ||
87 | |||
88 | entry = (struct ocfs2_acl_entry *)ocfs2_acl; | ||
89 | for (n = 0; n < acl->a_count; n++, entry++) { | ||
90 | entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag); | ||
91 | entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm); | ||
92 | entry->e_id = cpu_to_le32(acl->a_entries[n].e_id); | ||
93 | } | ||
94 | return ocfs2_acl; | ||
95 | } | ||
96 | |||
97 | static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode, | ||
98 | int type, | ||
99 | struct buffer_head *di_bh) | ||
100 | { | ||
101 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
102 | int name_index; | ||
103 | char *value = NULL; | ||
104 | struct posix_acl *acl; | ||
105 | int retval; | ||
106 | |||
107 | if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL)) | ||
108 | return NULL; | ||
109 | |||
110 | switch (type) { | ||
111 | case ACL_TYPE_ACCESS: | ||
112 | name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS; | ||
113 | break; | ||
114 | case ACL_TYPE_DEFAULT: | ||
115 | name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT; | ||
116 | break; | ||
117 | default: | ||
118 | return ERR_PTR(-EINVAL); | ||
119 | } | ||
120 | |||
121 | retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0); | ||
122 | if (retval > 0) { | ||
123 | value = kmalloc(retval, GFP_NOFS); | ||
124 | if (!value) | ||
125 | return ERR_PTR(-ENOMEM); | ||
126 | retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, | ||
127 | "", value, retval); | ||
128 | } | ||
129 | |||
130 | if (retval > 0) | ||
131 | acl = ocfs2_acl_from_xattr(value, retval); | ||
132 | else if (retval == -ENODATA || retval == 0) | ||
133 | acl = NULL; | ||
134 | else | ||
135 | acl = ERR_PTR(retval); | ||
136 | |||
137 | kfree(value); | ||
138 | |||
139 | return acl; | ||
140 | } | ||
141 | |||
142 | |||
143 | /* | ||
144 | * Get posix acl. | ||
145 | */ | ||
146 | static struct posix_acl *ocfs2_get_acl(struct inode *inode, int type) | ||
147 | { | ||
148 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
149 | struct buffer_head *di_bh = NULL; | ||
150 | struct posix_acl *acl; | ||
151 | int ret; | ||
152 | |||
153 | if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL)) | ||
154 | return NULL; | ||
155 | |||
156 | ret = ocfs2_inode_lock(inode, &di_bh, 0); | ||
157 | if (ret < 0) { | ||
158 | mlog_errno(ret); | ||
159 | acl = ERR_PTR(ret); | ||
160 | return acl; | ||
161 | } | ||
162 | |||
163 | acl = ocfs2_get_acl_nolock(inode, type, di_bh); | ||
164 | |||
165 | ocfs2_inode_unlock(inode, 0); | ||
166 | |||
167 | brelse(di_bh); | ||
168 | |||
169 | return acl; | ||
170 | } | ||
171 | |||
172 | /* | ||
173 | * Set the access or default ACL of an inode. | ||
174 | */ | ||
175 | static int ocfs2_set_acl(handle_t *handle, | ||
176 | struct inode *inode, | ||
177 | struct buffer_head *di_bh, | ||
178 | int type, | ||
179 | struct posix_acl *acl, | ||
180 | struct ocfs2_alloc_context *meta_ac, | ||
181 | struct ocfs2_alloc_context *data_ac) | ||
182 | { | ||
183 | int name_index; | ||
184 | void *value = NULL; | ||
185 | size_t size = 0; | ||
186 | int ret; | ||
187 | |||
188 | if (S_ISLNK(inode->i_mode)) | ||
189 | return -EOPNOTSUPP; | ||
190 | |||
191 | switch (type) { | ||
192 | case ACL_TYPE_ACCESS: | ||
193 | name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS; | ||
194 | if (acl) { | ||
195 | mode_t mode = inode->i_mode; | ||
196 | ret = posix_acl_equiv_mode(acl, &mode); | ||
197 | if (ret < 0) | ||
198 | return ret; | ||
199 | else { | ||
200 | inode->i_mode = mode; | ||
201 | if (ret == 0) | ||
202 | acl = NULL; | ||
203 | } | ||
204 | } | ||
205 | break; | ||
206 | case ACL_TYPE_DEFAULT: | ||
207 | name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT; | ||
208 | if (!S_ISDIR(inode->i_mode)) | ||
209 | return acl ? -EACCES : 0; | ||
210 | break; | ||
211 | default: | ||
212 | return -EINVAL; | ||
213 | } | ||
214 | |||
215 | if (acl) { | ||
216 | value = ocfs2_acl_to_xattr(acl, &size); | ||
217 | if (IS_ERR(value)) | ||
218 | return (int)PTR_ERR(value); | ||
219 | } | ||
220 | |||
221 | if (handle) | ||
222 | ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index, | ||
223 | "", value, size, 0, | ||
224 | meta_ac, data_ac); | ||
225 | else | ||
226 | ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0); | ||
227 | |||
228 | kfree(value); | ||
229 | |||
230 | return ret; | ||
231 | } | ||
232 | |||
233 | static size_t ocfs2_xattr_list_acl_access(struct inode *inode, | ||
234 | char *list, | ||
235 | size_t list_len, | ||
236 | const char *name, | ||
237 | size_t name_len) | ||
238 | { | ||
239 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
240 | const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS); | ||
241 | |||
242 | if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL)) | ||
243 | return 0; | ||
244 | |||
245 | if (list && size <= list_len) | ||
246 | memcpy(list, POSIX_ACL_XATTR_ACCESS, size); | ||
247 | return size; | ||
248 | } | ||
249 | |||
250 | static size_t ocfs2_xattr_list_acl_default(struct inode *inode, | ||
251 | char *list, | ||
252 | size_t list_len, | ||
253 | const char *name, | ||
254 | size_t name_len) | ||
255 | { | ||
256 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
257 | const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT); | ||
258 | |||
259 | if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL)) | ||
260 | return 0; | ||
261 | |||
262 | if (list && size <= list_len) | ||
263 | memcpy(list, POSIX_ACL_XATTR_DEFAULT, size); | ||
264 | return size; | ||
265 | } | ||
266 | |||
267 | static int ocfs2_xattr_get_acl(struct inode *inode, | ||
268 | int type, | ||
269 | void *buffer, | ||
270 | size_t size) | ||
271 | { | ||
272 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
273 | struct posix_acl *acl; | ||
274 | int ret; | ||
275 | |||
276 | if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL)) | ||
277 | return -EOPNOTSUPP; | ||
278 | |||
279 | acl = ocfs2_get_acl(inode, type); | ||
280 | if (IS_ERR(acl)) | ||
281 | return PTR_ERR(acl); | ||
282 | if (acl == NULL) | ||
283 | return -ENODATA; | ||
284 | ret = posix_acl_to_xattr(acl, buffer, size); | ||
285 | posix_acl_release(acl); | ||
286 | |||
287 | return ret; | ||
288 | } | ||
289 | |||
290 | static int ocfs2_xattr_get_acl_access(struct inode *inode, | ||
291 | const char *name, | ||
292 | void *buffer, | ||
293 | size_t size) | ||
294 | { | ||
295 | if (strcmp(name, "") != 0) | ||
296 | return -EINVAL; | ||
297 | return ocfs2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size); | ||
298 | } | ||
299 | |||
300 | static int ocfs2_xattr_get_acl_default(struct inode *inode, | ||
301 | const char *name, | ||
302 | void *buffer, | ||
303 | size_t size) | ||
304 | { | ||
305 | if (strcmp(name, "") != 0) | ||
306 | return -EINVAL; | ||
307 | return ocfs2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size); | ||
308 | } | ||
309 | |||
310 | static int ocfs2_xattr_set_acl(struct inode *inode, | ||
311 | int type, | ||
312 | const void *value, | ||
313 | size_t size) | ||
314 | { | ||
315 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
316 | struct posix_acl *acl; | ||
317 | int ret = 0; | ||
318 | |||
319 | if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL)) | ||
320 | return -EOPNOTSUPP; | ||
321 | |||
322 | if (!is_owner_or_cap(inode)) | ||
323 | return -EPERM; | ||
324 | |||
325 | if (value) { | ||
326 | acl = posix_acl_from_xattr(value, size); | ||
327 | if (IS_ERR(acl)) | ||
328 | return PTR_ERR(acl); | ||
329 | else if (acl) { | ||
330 | ret = posix_acl_valid(acl); | ||
331 | if (ret) | ||
332 | goto cleanup; | ||
333 | } | ||
334 | } else | ||
335 | acl = NULL; | ||
336 | |||
337 | ret = ocfs2_set_acl(NULL, inode, NULL, type, acl, NULL, NULL); | ||
338 | |||
339 | cleanup: | ||
340 | posix_acl_release(acl); | ||
341 | return ret; | ||
342 | } | ||
343 | |||
344 | static int ocfs2_xattr_set_acl_access(struct inode *inode, | ||
345 | const char *name, | ||
346 | const void *value, | ||
347 | size_t size, | ||
348 | int flags) | ||
349 | { | ||
350 | if (strcmp(name, "") != 0) | ||
351 | return -EINVAL; | ||
352 | return ocfs2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size); | ||
353 | } | ||
354 | |||
355 | static int ocfs2_xattr_set_acl_default(struct inode *inode, | ||
356 | const char *name, | ||
357 | const void *value, | ||
358 | size_t size, | ||
359 | int flags) | ||
360 | { | ||
361 | if (strcmp(name, "") != 0) | ||
362 | return -EINVAL; | ||
363 | return ocfs2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size); | ||
364 | } | ||
365 | |||
366 | struct xattr_handler ocfs2_xattr_acl_access_handler = { | ||
367 | .prefix = POSIX_ACL_XATTR_ACCESS, | ||
368 | .list = ocfs2_xattr_list_acl_access, | ||
369 | .get = ocfs2_xattr_get_acl_access, | ||
370 | .set = ocfs2_xattr_set_acl_access, | ||
371 | }; | ||
372 | |||
373 | struct xattr_handler ocfs2_xattr_acl_default_handler = { | ||
374 | .prefix = POSIX_ACL_XATTR_DEFAULT, | ||
375 | .list = ocfs2_xattr_list_acl_default, | ||
376 | .get = ocfs2_xattr_get_acl_default, | ||
377 | .set = ocfs2_xattr_set_acl_default, | ||
378 | }; | ||