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.
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>
42 * Convert from xattr value to acl struct.
44 static struct posix_acl *ocfs2_acl_from_xattr(const void *value, size_t size)
47 struct posix_acl *acl;
51 if (size < sizeof(struct posix_acl_entry))
52 return ERR_PTR(-EINVAL);
54 count = size / sizeof(struct posix_acl_entry);
56 return ERR_PTR(-EINVAL);
60 acl = posix_acl_alloc(count, GFP_NOFS);
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);
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;
85 *size = acl->a_count * sizeof(struct posix_acl_entry);
87 ocfs2_acl = kmalloc(*size, GFP_NOFS);
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);
100 static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
102 struct buffer_head *di_bh)
106 struct posix_acl *acl;
110 case ACL_TYPE_ACCESS:
111 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
113 case ACL_TYPE_DEFAULT:
114 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
117 return ERR_PTR(-EINVAL);
120 retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
122 value = kmalloc(retval, GFP_NOFS);
124 return ERR_PTR(-ENOMEM);
125 retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
130 acl = ocfs2_acl_from_xattr(value, retval);
131 else if (retval == -ENODATA || retval == 0)
134 acl = ERR_PTR(retval);
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;
152 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
155 ret = ocfs2_inode_lock(inode, &di_bh, 0);
162 acl = ocfs2_get_acl_nolock(inode, type, di_bh);
164 ocfs2_inode_unlock(inode, 0);
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;
183 ret = ocfs2_read_inode_block(inode, &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);
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);
211 inode->i_mode = new_mode;
212 inode->i_ctime = CURRENT_TIME;
213 di->i_mode = cpu_to_le16(inode->i_mode);
214 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
215 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
217 ocfs2_journal_dirty(handle, di_bh);
221 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
229 * Set the access or default ACL of an inode.
231 static int ocfs2_set_acl(handle_t *handle,
233 struct buffer_head *di_bh,
235 struct posix_acl *acl,
236 struct ocfs2_alloc_context *meta_ac,
237 struct ocfs2_alloc_context *data_ac)
244 if (S_ISLNK(inode->i_mode))
248 case ACL_TYPE_ACCESS:
249 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
251 mode_t mode = inode->i_mode;
252 ret = posix_acl_equiv_mode(acl, &mode);
259 ret = ocfs2_acl_set_mode(inode, di_bh,
267 case ACL_TYPE_DEFAULT:
268 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
269 if (!S_ISDIR(inode->i_mode))
270 return acl ? -EACCES : 0;
277 value = ocfs2_acl_to_xattr(acl, &size);
279 return (int)PTR_ERR(value);
283 ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
287 ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
294 int ocfs2_check_acl(struct inode *inode, int mask, unsigned int flags)
296 struct ocfs2_super *osb;
297 struct buffer_head *di_bh = NULL;
298 struct posix_acl *acl;
301 if (flags & IPERM_FLAG_RCU)
304 osb = OCFS2_SB(inode->i_sb);
305 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
308 ret = ocfs2_read_inode_block(inode, &di_bh);
314 acl = ocfs2_get_acl_nolock(inode, ACL_TYPE_ACCESS, di_bh);
319 mlog_errno(PTR_ERR(acl));
323 ret = posix_acl_permission(inode, acl, mask);
324 posix_acl_release(acl);
331 int ocfs2_acl_chmod(struct inode *inode)
333 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
334 struct posix_acl *acl, *clone;
337 if (S_ISLNK(inode->i_mode))
340 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
343 acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
344 if (IS_ERR(acl) || !acl)
346 clone = posix_acl_clone(acl, GFP_KERNEL);
347 posix_acl_release(acl);
350 ret = posix_acl_chmod_masq(clone, inode->i_mode);
352 ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
354 posix_acl_release(clone);
359 * Initialize the ACLs of a new inode. If parent directory has default ACL,
360 * then clone to new inode. Called from ocfs2_mknod.
362 int ocfs2_init_acl(handle_t *handle,
365 struct buffer_head *di_bh,
366 struct buffer_head *dir_bh,
367 struct ocfs2_alloc_context *meta_ac,
368 struct ocfs2_alloc_context *data_ac)
370 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
371 struct posix_acl *acl = NULL;
375 if (!S_ISLNK(inode->i_mode)) {
376 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
377 acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
383 mode = inode->i_mode & ~current_umask();
384 ret = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
391 if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
392 struct posix_acl *clone;
394 if (S_ISDIR(inode->i_mode)) {
395 ret = ocfs2_set_acl(handle, inode, di_bh,
396 ACL_TYPE_DEFAULT, acl,
401 clone = posix_acl_clone(acl, GFP_NOFS);
406 mode = inode->i_mode;
407 ret = posix_acl_create_masq(clone, &mode);
409 ret2 = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
416 ret = ocfs2_set_acl(handle, inode,
417 di_bh, ACL_TYPE_ACCESS,
418 clone, meta_ac, data_ac);
421 posix_acl_release(clone);
424 posix_acl_release(acl);
428 static size_t ocfs2_xattr_list_acl_access(struct dentry *dentry,
435 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
436 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
438 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
441 if (list && size <= list_len)
442 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
446 static size_t ocfs2_xattr_list_acl_default(struct dentry *dentry,
453 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
454 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
456 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
459 if (list && size <= list_len)
460 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
464 static int ocfs2_xattr_get_acl(struct dentry *dentry, const char *name,
465 void *buffer, size_t size, int type)
467 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
468 struct posix_acl *acl;
471 if (strcmp(name, "") != 0)
473 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
476 acl = ocfs2_get_acl(dentry->d_inode, type);
481 ret = posix_acl_to_xattr(acl, buffer, size);
482 posix_acl_release(acl);
487 static int ocfs2_xattr_set_acl(struct dentry *dentry, const char *name,
488 const void *value, size_t size, int flags, int type)
490 struct inode *inode = dentry->d_inode;
491 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
492 struct posix_acl *acl;
495 if (strcmp(name, "") != 0)
497 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
500 if (!is_owner_or_cap(inode))
504 acl = posix_acl_from_xattr(value, size);
508 ret = posix_acl_valid(acl);
515 ret = ocfs2_set_acl(NULL, inode, NULL, type, acl, NULL, NULL);
518 posix_acl_release(acl);
522 const struct xattr_handler ocfs2_xattr_acl_access_handler = {
523 .prefix = POSIX_ACL_XATTR_ACCESS,
524 .flags = ACL_TYPE_ACCESS,
525 .list = ocfs2_xattr_list_acl_access,
526 .get = ocfs2_xattr_get_acl,
527 .set = ocfs2_xattr_set_acl,
530 const struct xattr_handler ocfs2_xattr_acl_default_handler = {
531 .prefix = POSIX_ACL_XATTR_DEFAULT,
532 .flags = ACL_TYPE_DEFAULT,
533 .list = ocfs2_xattr_list_acl_default,
534 .get = ocfs2_xattr_get_acl,
535 .set = ocfs2_xattr_set_acl,