4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2013, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/acl.c
34 * Lustre Access Control List.
36 * Author: Fan Yong <fanyong@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_SEC
40 #include <lu_object.h>
41 #include <lustre_acl.h>
42 #include <lustre_eacl.h>
43 #include <obd_support.h>
44 #ifdef HAVE_SERVER_SUPPORT
45 # include <lustre_idmap.h>
46 # include <md_object.h>
47 #endif /* HAVE_SERVER_SUPPORT */
49 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
51 static inline void lustre_posix_acl_le_to_cpu(posix_acl_xattr_entry *d,
52 posix_acl_xattr_entry *s)
54 d->e_tag = le16_to_cpu(s->e_tag);
55 d->e_perm = le16_to_cpu(s->e_perm);
56 d->e_id = le32_to_cpu(s->e_id);
60 static inline void lustre_posix_acl_cpu_to_le(posix_acl_xattr_entry *d,
61 posix_acl_xattr_entry *s)
63 d->e_tag = cpu_to_le16(s->e_tag);
64 d->e_perm = cpu_to_le16(s->e_perm);
65 d->e_id = cpu_to_le32(s->e_id);
70 * Check permission based on POSIX ACL.
72 int lustre_posix_acl_permission(struct lu_ucred *mu, const struct lu_attr *la,
73 int want, posix_acl_xattr_entry *entry,
76 posix_acl_xattr_entry *pa, *pe, *mask_obj;
77 posix_acl_xattr_entry ae, me;
83 for (pa = &entry[0], pe = &entry[count - 1]; pa <= pe; pa++) {
84 lustre_posix_acl_le_to_cpu(&ae, pa);
87 /* (May have been checked already) */
88 if (la->la_uid == mu->uc_fsuid)
92 if (ae.e_id == mu->uc_fsuid)
96 if (lustre_in_group_p(mu, la->la_gid)) {
98 if ((ae.e_perm & want) == want)
103 if (lustre_in_group_p(mu, ae.e_id)) {
105 if ((ae.e_perm & want) == want)
122 for (mask_obj = pa + 1; mask_obj <= pe; mask_obj++) {
123 lustre_posix_acl_le_to_cpu(&me, mask_obj);
124 if (me.e_tag == ACL_MASK) {
125 if ((ae.e_perm & me.e_perm & want) == want)
133 if ((ae.e_perm & want) == want)
138 EXPORT_SYMBOL(lustre_posix_acl_permission);
141 * Modify the ACL for the chmod.
143 int lustre_posix_acl_chmod_masq(posix_acl_xattr_entry *entry, u32 mode,
146 posix_acl_xattr_entry *group_obj = NULL, *mask_obj = NULL, *pa, *pe;
148 for (pa = &entry[0], pe = &entry[count - 1]; pa <= pe; pa++) {
149 switch (le16_to_cpu(pa->e_tag)) {
151 pa->e_perm = cpu_to_le16((mode & S_IRWXU) >> 6);
163 pa->e_perm = cpu_to_le16(mode & S_IRWXO);
171 mask_obj->e_perm = cpu_to_le16((mode & S_IRWXG) >> 3);
175 group_obj->e_perm = cpu_to_le16((mode & S_IRWXG) >> 3);
180 EXPORT_SYMBOL(lustre_posix_acl_chmod_masq);
183 * Returns 0 if the acl can be exactly represented in the traditional
184 * file mode permission bits, or else 1. Returns -E... on error.
187 lustre_posix_acl_equiv_mode(posix_acl_xattr_entry *entry, mode_t *mode_p,
190 posix_acl_xattr_entry *pa, *pe;
194 for (pa = &entry[0], pe = &entry[count - 1]; pa <= pe; pa++) {
195 __u16 perm = le16_to_cpu(pa->e_perm);
196 switch (le16_to_cpu(pa->e_tag)) {
198 mode |= (perm & S_IRWXO) << 6;
201 mode |= (perm & S_IRWXO) << 3;
204 mode |= perm & S_IRWXO;
207 mode = (mode & ~S_IRWXG) |
208 ((perm & S_IRWXO) << 3);
220 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
223 EXPORT_SYMBOL(lustre_posix_acl_equiv_mode);
226 * Modify acl when creating a new object.
228 int lustre_posix_acl_create_masq(posix_acl_xattr_entry *entry, u32 *pmode,
231 posix_acl_xattr_entry *group_obj = NULL, *mask_obj = NULL, *pa, *pe;
232 posix_acl_xattr_entry ae;
236 for (pa = &entry[0], pe = &entry[count - 1]; pa <= pe; pa++) {
237 lustre_posix_acl_le_to_cpu(&ae, pa);
240 ae.e_perm &= (mode >> 6) | ~(0007);
241 pa->e_perm = cpu_to_le16(ae.e_perm);
242 mode &= (ae.e_perm << 6) | ~S_IRWXU;
252 ae.e_perm &= mode | ~(0007);
253 pa->e_perm = cpu_to_le16(ae.e_perm);
254 mode &= ae.e_perm | ~(0007);
266 ae.e_perm = le16_to_cpu(mask_obj->e_perm) &
267 ((mode >> 3) | ~(0007));
268 mode &= (ae.e_perm << 3) | ~S_IRWXG;
269 mask_obj->e_perm = cpu_to_le16(ae.e_perm);
273 ae.e_perm = le16_to_cpu(group_obj->e_perm) &
274 ((mode >> 3) | ~(0007));
275 mode &= (ae.e_perm << 3) | ~S_IRWXG;
276 group_obj->e_perm = cpu_to_le16(ae.e_perm);
279 *pmode = (*pmode & ~S_IRWXUGO) | mode;
282 EXPORT_SYMBOL(lustre_posix_acl_create_masq);