ocfs2: do not overwrite error codes in ocfs2_init_acl
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ocfs2 / acl.c
blob5a2177833210bb5e6176cddad3a7abb64830babf
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * acl.c
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
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>
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.
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.
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
27 #define MLOG_MASK_PREFIX ML_INODE
28 #include <cluster/masklog.h>
30 #include "ocfs2.h"
31 #include "alloc.h"
32 #include "dlmglue.h"
33 #include "file.h"
34 #include "inode.h"
35 #include "journal.h"
36 #include "ocfs2_fs.h"
38 #include "xattr.h"
39 #include "acl.h"
42 * Convert from xattr value to acl struct.
44 static struct posix_acl *ocfs2_acl_from_xattr(const void *value, size_t size)
46 int n, count;
47 struct posix_acl *acl;
49 if (!value)
50 return NULL;
51 if (size < sizeof(struct posix_acl_entry))
52 return ERR_PTR(-EINVAL);
54 count = size / sizeof(struct posix_acl_entry);
55 if (count < 0)
56 return ERR_PTR(-EINVAL);
57 if (count == 0)
58 return NULL;
60 acl = posix_acl_alloc(count, GFP_NOFS);
61 if (!acl)
62 return ERR_PTR(-ENOMEM);
63 for (n = 0; n < count; n++) {
64 struct ocfs2_acl_entry *entry =
65 (struct ocfs2_acl_entry *)value;
67 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
68 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
69 acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
70 value += sizeof(struct posix_acl_entry);
73 return acl;
77 * Convert acl struct to xattr value.
79 static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size)
81 struct ocfs2_acl_entry *entry = NULL;
82 char *ocfs2_acl;
83 size_t n;
85 *size = acl->a_count * sizeof(struct posix_acl_entry);
87 ocfs2_acl = kmalloc(*size, GFP_NOFS);
88 if (!ocfs2_acl)
89 return ERR_PTR(-ENOMEM);
91 entry = (struct ocfs2_acl_entry *)ocfs2_acl;
92 for (n = 0; n < acl->a_count; n++, entry++) {
93 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
94 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
95 entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
97 return ocfs2_acl;
100 static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
101 int type,
102 struct buffer_head *di_bh)
104 int name_index;
105 char *value = NULL;
106 struct posix_acl *acl;
107 int retval;
109 switch (type) {
110 case ACL_TYPE_ACCESS:
111 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
112 break;
113 case ACL_TYPE_DEFAULT:
114 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
115 break;
116 default:
117 return ERR_PTR(-EINVAL);
120 retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
121 if (retval > 0) {
122 value = kmalloc(retval, GFP_NOFS);
123 if (!value)
124 return ERR_PTR(-ENOMEM);
125 retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
126 "", value, retval);
129 if (retval > 0)
130 acl = ocfs2_acl_from_xattr(value, retval);
131 else if (retval == -ENODATA || retval == 0)
132 acl = NULL;
133 else
134 acl = ERR_PTR(retval);
136 kfree(value);
138 return acl;
143 * Get posix acl.
145 static struct posix_acl *ocfs2_get_acl(struct inode *inode, int type)
147 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
148 struct buffer_head *di_bh = NULL;
149 struct posix_acl *acl;
150 int ret;
152 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
153 return NULL;
155 ret = ocfs2_inode_lock(inode, &di_bh, 0);
156 if (ret < 0) {
157 mlog_errno(ret);
158 acl = ERR_PTR(ret);
159 return acl;
162 acl = ocfs2_get_acl_nolock(inode, type, di_bh);
164 ocfs2_inode_unlock(inode, 0);
166 brelse(di_bh);
168 return acl;
172 * Helper function to set i_mode in memory and disk. Some call paths
173 * will not have di_bh or a journal handle to pass, in which case it
174 * will create it's own.
176 static int ocfs2_acl_set_mode(struct inode *inode, struct buffer_head *di_bh,
177 handle_t *handle, umode_t new_mode)
179 int ret, commit_handle = 0;
180 struct ocfs2_dinode *di;
182 if (di_bh == NULL) {
183 ret = ocfs2_read_inode_block(inode, &di_bh);
184 if (ret) {
185 mlog_errno(ret);
186 goto out;
188 } else
189 get_bh(di_bh);
191 if (handle == NULL) {
192 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
193 OCFS2_INODE_UPDATE_CREDITS);
194 if (IS_ERR(handle)) {
195 ret = PTR_ERR(handle);
196 mlog_errno(ret);
197 goto out_brelse;
200 commit_handle = 1;
203 di = (struct ocfs2_dinode *)di_bh->b_data;
204 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
205 OCFS2_JOURNAL_ACCESS_WRITE);
206 if (ret) {
207 mlog_errno(ret);
208 goto out_commit;
211 inode->i_mode = new_mode;
212 di->i_mode = cpu_to_le16(inode->i_mode);
214 ocfs2_journal_dirty(handle, di_bh);
216 out_commit:
217 if (commit_handle)
218 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
219 out_brelse:
220 brelse(di_bh);
221 out:
222 return ret;
226 * Set the access or default ACL of an inode.
228 static int ocfs2_set_acl(handle_t *handle,
229 struct inode *inode,
230 struct buffer_head *di_bh,
231 int type,
232 struct posix_acl *acl,
233 struct ocfs2_alloc_context *meta_ac,
234 struct ocfs2_alloc_context *data_ac)
236 int name_index;
237 void *value = NULL;
238 size_t size = 0;
239 int ret;
241 if (S_ISLNK(inode->i_mode))
242 return -EOPNOTSUPP;
244 switch (type) {
245 case ACL_TYPE_ACCESS:
246 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
247 if (acl) {
248 mode_t mode = inode->i_mode;
249 ret = posix_acl_equiv_mode(acl, &mode);
250 if (ret < 0)
251 return ret;
252 else {
253 if (ret == 0)
254 acl = NULL;
256 ret = ocfs2_acl_set_mode(inode, di_bh,
257 handle, mode);
258 if (ret)
259 return ret;
263 break;
264 case ACL_TYPE_DEFAULT:
265 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
266 if (!S_ISDIR(inode->i_mode))
267 return acl ? -EACCES : 0;
268 break;
269 default:
270 return -EINVAL;
273 if (acl) {
274 value = ocfs2_acl_to_xattr(acl, &size);
275 if (IS_ERR(value))
276 return (int)PTR_ERR(value);
279 if (handle)
280 ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
281 "", value, size, 0,
282 meta_ac, data_ac);
283 else
284 ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
286 kfree(value);
288 return ret;
291 int ocfs2_check_acl(struct inode *inode, int mask)
293 struct posix_acl *acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
295 if (IS_ERR(acl))
296 return PTR_ERR(acl);
297 if (acl) {
298 int ret = posix_acl_permission(inode, acl, mask);
299 posix_acl_release(acl);
300 return ret;
303 return -EAGAIN;
306 int ocfs2_acl_chmod(struct inode *inode)
308 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
309 struct posix_acl *acl, *clone;
310 int ret;
312 if (S_ISLNK(inode->i_mode))
313 return -EOPNOTSUPP;
315 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
316 return 0;
318 acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
319 if (IS_ERR(acl) || !acl)
320 return PTR_ERR(acl);
321 clone = posix_acl_clone(acl, GFP_KERNEL);
322 posix_acl_release(acl);
323 if (!clone)
324 return -ENOMEM;
325 ret = posix_acl_chmod_masq(clone, inode->i_mode);
326 if (!ret)
327 ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
328 clone, NULL, NULL);
329 posix_acl_release(clone);
330 return ret;
334 * Initialize the ACLs of a new inode. If parent directory has default ACL,
335 * then clone to new inode. Called from ocfs2_mknod.
337 int ocfs2_init_acl(handle_t *handle,
338 struct inode *inode,
339 struct inode *dir,
340 struct buffer_head *di_bh,
341 struct buffer_head *dir_bh,
342 struct ocfs2_alloc_context *meta_ac,
343 struct ocfs2_alloc_context *data_ac)
345 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
346 struct posix_acl *acl = NULL;
347 int ret = 0, ret2;
348 mode_t mode;
350 if (!S_ISLNK(inode->i_mode)) {
351 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
352 acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
353 dir_bh);
354 if (IS_ERR(acl))
355 return PTR_ERR(acl);
357 if (!acl) {
358 mode = inode->i_mode & ~current_umask();
359 ret = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
360 if (ret) {
361 mlog_errno(ret);
362 goto cleanup;
366 if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
367 struct posix_acl *clone;
369 if (S_ISDIR(inode->i_mode)) {
370 ret = ocfs2_set_acl(handle, inode, di_bh,
371 ACL_TYPE_DEFAULT, acl,
372 meta_ac, data_ac);
373 if (ret)
374 goto cleanup;
376 clone = posix_acl_clone(acl, GFP_NOFS);
377 ret = -ENOMEM;
378 if (!clone)
379 goto cleanup;
381 mode = inode->i_mode;
382 ret = posix_acl_create_masq(clone, &mode);
383 if (ret >= 0) {
384 ret2 = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
385 if (ret2) {
386 mlog_errno(ret2);
387 ret = ret2;
388 goto cleanup;
390 if (ret > 0) {
391 ret = ocfs2_set_acl(handle, inode,
392 di_bh, ACL_TYPE_ACCESS,
393 clone, meta_ac, data_ac);
396 posix_acl_release(clone);
398 cleanup:
399 posix_acl_release(acl);
400 return ret;
403 static size_t ocfs2_xattr_list_acl_access(struct dentry *dentry,
404 char *list,
405 size_t list_len,
406 const char *name,
407 size_t name_len,
408 int type)
410 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
411 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
413 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
414 return 0;
416 if (list && size <= list_len)
417 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
418 return size;
421 static size_t ocfs2_xattr_list_acl_default(struct dentry *dentry,
422 char *list,
423 size_t list_len,
424 const char *name,
425 size_t name_len,
426 int type)
428 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
429 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
431 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
432 return 0;
434 if (list && size <= list_len)
435 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
436 return size;
439 static int ocfs2_xattr_get_acl(struct dentry *dentry, const char *name,
440 void *buffer, size_t size, int type)
442 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
443 struct posix_acl *acl;
444 int ret;
446 if (strcmp(name, "") != 0)
447 return -EINVAL;
448 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
449 return -EOPNOTSUPP;
451 acl = ocfs2_get_acl(dentry->d_inode, type);
452 if (IS_ERR(acl))
453 return PTR_ERR(acl);
454 if (acl == NULL)
455 return -ENODATA;
456 ret = posix_acl_to_xattr(acl, buffer, size);
457 posix_acl_release(acl);
459 return ret;
462 static int ocfs2_xattr_set_acl(struct dentry *dentry, const char *name,
463 const void *value, size_t size, int flags, int type)
465 struct inode *inode = dentry->d_inode;
466 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
467 struct posix_acl *acl;
468 int ret = 0;
470 if (strcmp(name, "") != 0)
471 return -EINVAL;
472 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
473 return -EOPNOTSUPP;
475 if (!is_owner_or_cap(inode))
476 return -EPERM;
478 if (value) {
479 acl = posix_acl_from_xattr(value, size);
480 if (IS_ERR(acl))
481 return PTR_ERR(acl);
482 else if (acl) {
483 ret = posix_acl_valid(acl);
484 if (ret)
485 goto cleanup;
487 } else
488 acl = NULL;
490 ret = ocfs2_set_acl(NULL, inode, NULL, type, acl, NULL, NULL);
492 cleanup:
493 posix_acl_release(acl);
494 return ret;
497 const struct xattr_handler ocfs2_xattr_acl_access_handler = {
498 .prefix = POSIX_ACL_XATTR_ACCESS,
499 .flags = ACL_TYPE_ACCESS,
500 .list = ocfs2_xattr_list_acl_access,
501 .get = ocfs2_xattr_get_acl,
502 .set = ocfs2_xattr_set_acl,
505 const struct xattr_handler ocfs2_xattr_acl_default_handler = {
506 .prefix = POSIX_ACL_XATTR_DEFAULT,
507 .flags = ACL_TYPE_DEFAULT,
508 .list = ocfs2_xattr_list_acl_default,
509 .get = ocfs2_xattr_get_acl,
510 .set = ocfs2_xattr_set_acl,