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
)
101 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
104 struct posix_acl
*acl
;
107 if (!(osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
))
111 case ACL_TYPE_ACCESS
:
112 name_index
= OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
;
114 case ACL_TYPE_DEFAULT
:
115 name_index
= OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
;
118 return ERR_PTR(-EINVAL
);
121 retval
= ocfs2_xattr_get_nolock(inode
, di_bh
, name_index
, "", NULL
, 0);
123 value
= kmalloc(retval
, GFP_NOFS
);
125 return ERR_PTR(-ENOMEM
);
126 retval
= ocfs2_xattr_get_nolock(inode
, di_bh
, name_index
,
131 acl
= ocfs2_acl_from_xattr(value
, retval
);
132 else if (retval
== -ENODATA
|| retval
== 0)
135 acl
= ERR_PTR(retval
);
146 static struct posix_acl
*ocfs2_get_acl(struct inode
*inode
, int type
)
148 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
149 struct buffer_head
*di_bh
= NULL
;
150 struct posix_acl
*acl
;
153 if (!(osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
))
156 ret
= ocfs2_inode_lock(inode
, &di_bh
, 0);
163 acl
= ocfs2_get_acl_nolock(inode
, type
, di_bh
);
165 ocfs2_inode_unlock(inode
, 0);
173 * Set the access or default ACL of an inode.
175 static int ocfs2_set_acl(handle_t
*handle
,
177 struct buffer_head
*di_bh
,
179 struct posix_acl
*acl
,
180 struct ocfs2_alloc_context
*meta_ac
,
181 struct ocfs2_alloc_context
*data_ac
)
188 if (S_ISLNK(inode
->i_mode
))
192 case ACL_TYPE_ACCESS
:
193 name_index
= OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
;
195 mode_t mode
= inode
->i_mode
;
196 ret
= posix_acl_equiv_mode(acl
, &mode
);
200 inode
->i_mode
= mode
;
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;
216 value
= ocfs2_acl_to_xattr(acl
, &size
);
218 return (int)PTR_ERR(value
);
222 ret
= ocfs2_xattr_set_handle(handle
, inode
, di_bh
, name_index
,
226 ret
= ocfs2_xattr_set(inode
, name_index
, "", value
, size
, 0);
233 int ocfs2_check_acl(struct inode
*inode
, int mask
)
235 struct posix_acl
*acl
= ocfs2_get_acl(inode
, ACL_TYPE_ACCESS
);
240 int ret
= posix_acl_permission(inode
, acl
, mask
);
241 posix_acl_release(acl
);
248 static size_t ocfs2_xattr_list_acl_access(struct inode
*inode
,
254 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
255 const size_t size
= sizeof(POSIX_ACL_XATTR_ACCESS
);
257 if (!(osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
))
260 if (list
&& size
<= list_len
)
261 memcpy(list
, POSIX_ACL_XATTR_ACCESS
, size
);
265 static size_t ocfs2_xattr_list_acl_default(struct inode
*inode
,
271 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
272 const size_t size
= sizeof(POSIX_ACL_XATTR_DEFAULT
);
274 if (!(osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
))
277 if (list
&& size
<= list_len
)
278 memcpy(list
, POSIX_ACL_XATTR_DEFAULT
, size
);
282 static int ocfs2_xattr_get_acl(struct inode
*inode
,
287 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
288 struct posix_acl
*acl
;
291 if (!(osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
))
294 acl
= ocfs2_get_acl(inode
, type
);
299 ret
= posix_acl_to_xattr(acl
, buffer
, size
);
300 posix_acl_release(acl
);
305 static int ocfs2_xattr_get_acl_access(struct inode
*inode
,
310 if (strcmp(name
, "") != 0)
312 return ocfs2_xattr_get_acl(inode
, ACL_TYPE_ACCESS
, buffer
, size
);
315 static int ocfs2_xattr_get_acl_default(struct inode
*inode
,
320 if (strcmp(name
, "") != 0)
322 return ocfs2_xattr_get_acl(inode
, ACL_TYPE_DEFAULT
, buffer
, size
);
325 static int ocfs2_xattr_set_acl(struct inode
*inode
,
330 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
331 struct posix_acl
*acl
;
334 if (!(osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
))
337 if (!is_owner_or_cap(inode
))
341 acl
= posix_acl_from_xattr(value
, size
);
345 ret
= posix_acl_valid(acl
);
352 ret
= ocfs2_set_acl(NULL
, inode
, NULL
, type
, acl
, NULL
, NULL
);
355 posix_acl_release(acl
);
359 static int ocfs2_xattr_set_acl_access(struct inode
*inode
,
365 if (strcmp(name
, "") != 0)
367 return ocfs2_xattr_set_acl(inode
, ACL_TYPE_ACCESS
, value
, size
);
370 static int ocfs2_xattr_set_acl_default(struct inode
*inode
,
376 if (strcmp(name
, "") != 0)
378 return ocfs2_xattr_set_acl(inode
, ACL_TYPE_DEFAULT
, value
, size
);
381 struct xattr_handler ocfs2_xattr_acl_access_handler
= {
382 .prefix
= POSIX_ACL_XATTR_ACCESS
,
383 .list
= ocfs2_xattr_list_acl_access
,
384 .get
= ocfs2_xattr_get_acl_access
,
385 .set
= ocfs2_xattr_set_acl_access
,
388 struct xattr_handler ocfs2_xattr_acl_default_handler
= {
389 .prefix
= POSIX_ACL_XATTR_DEFAULT
,
390 .list
= ocfs2_xattr_list_acl_default
,
391 .get
= ocfs2_xattr_get_acl_default
,
392 .set
= ocfs2_xattr_set_acl_default
,