1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
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/string.h>
26 #define MLOG_MASK_PREFIX ML_INODE
27 #include <cluster/masklog.h>
39 * Convert from xattr value to acl struct.
41 static struct posix_acl *ocfs2_acl_from_xattr(const void *value, size_t size)
44 struct posix_acl *acl;
48 if (size < sizeof(struct posix_acl_entry))
49 return ERR_PTR(-EINVAL);
51 count = size / sizeof(struct posix_acl_entry);
53 return ERR_PTR(-EINVAL);
57 acl = posix_acl_alloc(count, GFP_NOFS);
59 return ERR_PTR(-ENOMEM);
60 for (n = 0; n < count; n++) {
61 struct ocfs2_acl_entry *entry =
62 (struct ocfs2_acl_entry *)value;
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);
74 * Convert acl struct to xattr value.
76 static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size)
78 struct ocfs2_acl_entry *entry = NULL;
82 *size = acl->a_count * sizeof(struct posix_acl_entry);
84 ocfs2_acl = kmalloc(*size, GFP_NOFS);
86 return ERR_PTR(-ENOMEM);
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);
97 static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
99 struct buffer_head *di_bh)
103 struct posix_acl *acl;
107 case ACL_TYPE_ACCESS:
108 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
110 case ACL_TYPE_DEFAULT:
111 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
114 return ERR_PTR(-EINVAL);
117 retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
119 value = kmalloc(retval, GFP_NOFS);
121 return ERR_PTR(-ENOMEM);
122 retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
127 acl = ocfs2_acl_from_xattr(value, retval);
128 else if (retval == -ENODATA || retval == 0)
131 acl = ERR_PTR(retval);
142 static struct posix_acl *ocfs2_get_acl(struct inode *inode, int type)
144 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
145 struct buffer_head *di_bh = NULL;
146 struct posix_acl *acl;
149 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
152 ret = ocfs2_inode_lock(inode, &di_bh, 0);
159 acl = ocfs2_get_acl_nolock(inode, type, di_bh);
161 ocfs2_inode_unlock(inode, 0);
169 * Set the access or default ACL of an inode.
171 static int ocfs2_set_acl(handle_t *handle,
173 struct buffer_head *di_bh,
175 struct posix_acl *acl,
176 struct ocfs2_alloc_context *meta_ac,
177 struct ocfs2_alloc_context *data_ac)
184 if (S_ISLNK(inode->i_mode))
188 case ACL_TYPE_ACCESS:
189 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
191 mode_t mode = inode->i_mode;
192 ret = posix_acl_equiv_mode(acl, &mode);
196 inode->i_mode = mode;
202 case ACL_TYPE_DEFAULT:
203 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
204 if (!S_ISDIR(inode->i_mode))
205 return acl ? -EACCES : 0;
212 value = ocfs2_acl_to_xattr(acl, &size);
214 return (int)PTR_ERR(value);
218 ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
222 ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
229 int ocfs2_check_acl(struct inode *inode, int mask)
231 struct posix_acl *acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
236 int ret = posix_acl_permission(inode, acl, mask);
237 posix_acl_release(acl);
244 int ocfs2_acl_chmod(struct inode *inode)
246 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
247 struct posix_acl *acl, *clone;
250 if (S_ISLNK(inode->i_mode))
253 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
256 acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
257 if (IS_ERR(acl) || !acl)
259 clone = posix_acl_clone(acl, GFP_KERNEL);
260 posix_acl_release(acl);
263 ret = posix_acl_chmod_masq(clone, inode->i_mode);
265 ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
267 posix_acl_release(clone);
272 * Initialize the ACLs of a new inode. If parent directory has default ACL,
273 * then clone to new inode. Called from ocfs2_mknod.
275 int ocfs2_init_acl(handle_t *handle,
278 struct buffer_head *di_bh,
279 struct buffer_head *dir_bh,
280 struct ocfs2_alloc_context *meta_ac,
281 struct ocfs2_alloc_context *data_ac)
283 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
284 struct posix_acl *acl = NULL;
287 if (!S_ISLNK(inode->i_mode)) {
288 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
289 acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
295 inode->i_mode &= ~current_umask();
297 if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
298 struct posix_acl *clone;
301 if (S_ISDIR(inode->i_mode)) {
302 ret = ocfs2_set_acl(handle, inode, di_bh,
303 ACL_TYPE_DEFAULT, acl,
308 clone = posix_acl_clone(acl, GFP_NOFS);
313 mode = inode->i_mode;
314 ret = posix_acl_create_masq(clone, &mode);
316 inode->i_mode = mode;
318 ret = ocfs2_set_acl(handle, inode,
319 di_bh, ACL_TYPE_ACCESS,
320 clone, meta_ac, data_ac);
323 posix_acl_release(clone);
326 posix_acl_release(acl);
330 static size_t ocfs2_xattr_list_acl_access(struct dentry *dentry,
337 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
338 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
340 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
343 if (list && size <= list_len)
344 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
348 static size_t ocfs2_xattr_list_acl_default(struct dentry *dentry,
355 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
356 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
358 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
361 if (list && size <= list_len)
362 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
366 static int ocfs2_xattr_get_acl(struct dentry *dentry, const char *name,
367 void *buffer, size_t size, int type)
369 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
370 struct posix_acl *acl;
373 if (strcmp(name, "") != 0)
375 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
378 acl = ocfs2_get_acl(dentry->d_inode, type);
383 ret = posix_acl_to_xattr(acl, buffer, size);
384 posix_acl_release(acl);
389 static int ocfs2_xattr_set_acl(struct dentry *dentry, const char *name,
390 const void *value, size_t size, int flags, int type)
392 struct inode *inode = dentry->d_inode;
393 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
394 struct posix_acl *acl;
397 if (strcmp(name, "") != 0)
399 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
402 if (!is_owner_or_cap(inode))
406 acl = posix_acl_from_xattr(value, size);
410 ret = posix_acl_valid(acl);
417 ret = ocfs2_set_acl(NULL, inode, NULL, type, acl, NULL, NULL);
420 posix_acl_release(acl);
424 struct xattr_handler ocfs2_xattr_acl_access_handler = {
425 .prefix = POSIX_ACL_XATTR_ACCESS,
426 .flags = ACL_TYPE_ACCESS,
427 .list = ocfs2_xattr_list_acl_access,
428 .get = ocfs2_xattr_get_acl,
429 .set = ocfs2_xattr_set_acl,
432 struct xattr_handler ocfs2_xattr_acl_default_handler = {
433 .prefix = POSIX_ACL_XATTR_DEFAULT,
434 .flags = ACL_TYPE_DEFAULT,
435 .list = ocfs2_xattr_list_acl_default,
436 .get = ocfs2_xattr_get_acl,
437 .set = ocfs2_xattr_set_acl,