1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_permission.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: fangyong@clusterfs.com
41 * Author: lsy@clusterfs.com
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <linux/module.h>
49 #ifdef HAVE_EXT4_LDISKFS
50 #include <ldiskfs/ldiskfs_jbd2.h>
52 #include <linux/jbd.h>
55 #include <obd_class.h>
56 #include <lustre_ver.h>
57 #include <obd_support.h>
58 #include <lprocfs_status.h>
60 #ifdef HAVE_EXT4_LDISKFS
61 #include <ldiskfs/ldiskfs.h>
63 #include <linux/ldiskfs_fs.h>
65 #include <lustre_mds.h>
66 #include <lustre/lustre_idl.h>
68 #include "mdd_internal.h"
70 #ifdef CONFIG_FS_POSIX_ACL
73 * Get default acl EA only.
74 * Hold read_lock for mdd_obj.
76 int mdd_def_acl_get(const struct lu_env *env, struct mdd_object *mdd_obj,
83 if (ma->ma_valid & MA_ACL_DEF)
86 buf = mdd_buf_get(env, ma->ma_acl, ma->ma_acl_size);
87 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_ACL_DEFAULT,
91 ma->ma_valid |= MA_ACL_DEF;
93 } else if ((rc == -EOPNOTSUPP) || (rc == -ENODATA)) {
100 * Hold write_lock for o.
102 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
103 struct thandle *handle)
106 posix_acl_xattr_header *head;
107 posix_acl_xattr_entry *entry;
113 buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf,
114 sizeof(mdd_env_info(env)->mti_xattr_buf));
116 rc = mdo_xattr_get(env, o, buf, XATTR_NAME_ACL_ACCESS, BYPASS_CAPA);
117 if ((rc == -EOPNOTSUPP) || (rc == -ENODATA))
123 head = (posix_acl_xattr_header *)(buf->lb_buf);
124 entry = head->a_entries;
125 entry_count = (buf->lb_len - sizeof(head->a_version)) /
126 sizeof(posix_acl_xattr_entry);
127 if (entry_count <= 0)
130 rc = lustre_posix_acl_chmod_masq(entry, mode, entry_count);
134 rc = mdo_xattr_set(env, o, buf, XATTR_NAME_ACL_ACCESS,
135 0, handle, BYPASS_CAPA);
140 * Hold write_lock for obj.
142 int __mdd_acl_init(const struct lu_env *env, struct mdd_object *obj,
143 struct lu_buf *buf, __u32 *mode, struct thandle *handle)
145 posix_acl_xattr_header *head;
146 posix_acl_xattr_entry *entry;
152 head = (posix_acl_xattr_header *)(buf->lb_buf);
153 entry = head->a_entries;
154 entry_count = (buf->lb_len - sizeof(head->a_version)) /
155 sizeof(posix_acl_xattr_entry);
156 if (entry_count <= 0)
159 if (S_ISDIR(*mode)) {
160 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_DEFAULT, 0,
161 handle, BYPASS_CAPA);
166 rc = lustre_posix_acl_create_masq(entry, mode, entry_count);
170 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_ACCESS, 0, handle,
177 * Hold read_lock for obj.
179 static int mdd_check_acl(const struct lu_env *env, struct mdd_object *obj,
180 struct lu_attr *la, int mask)
182 #ifdef CONFIG_FS_POSIX_ACL
183 struct md_ucred *uc = md_ucred(env);
184 posix_acl_xattr_header *head;
185 posix_acl_xattr_entry *entry;
191 buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf,
192 sizeof(mdd_env_info(env)->mti_xattr_buf));
193 rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_ACL_ACCESS,
194 mdd_object_capa(env, obj));
196 RETURN(rc ? : -EACCES);
199 head = (posix_acl_xattr_header *)(buf->lb_buf);
200 entry = head->a_entries;
201 entry_count = (buf->lb_len - sizeof(head->a_version)) /
202 sizeof(posix_acl_xattr_entry);
204 rc = lustre_posix_acl_permission(uc, la, mask, entry, entry_count);
212 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
213 struct lu_attr *la, int mask, int role)
215 struct md_ucred *uc = md_ucred(env);
223 /* These means unnecessary for permission check */
224 if ((uc == NULL) || (uc->mu_valid == UCRED_INIT))
227 /* Invalid user credit */
228 if (uc->mu_valid == UCRED_INVALID)
232 * Nobody gets write access to an immutable file.
234 if ((mask & MAY_WRITE) && mdd_is_immutable(obj))
238 la = &mdd_env_info(env)->mti_la;
239 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
245 if (uc->mu_fsuid == la->la_uid) {
248 if (mode & S_IRWXG) {
250 mdd_read_lock(env, obj, role);
251 rc = mdd_check_acl(env, obj, la, mask);
253 mdd_read_unlock(env, obj);
255 goto check_capabilities;
256 else if ((rc != -EAGAIN) && (rc != -EOPNOTSUPP) &&
260 if (lustre_in_group_p(uc, la->la_gid))
264 if (((mode & mask & S_IRWXO) == mask))
268 if (!(mask & MAY_EXEC) ||
269 (la->la_mode & S_IXUGO) || S_ISDIR(la->la_mode))
270 if (mdd_capable(uc, CFS_CAP_DAC_OVERRIDE))
273 if ((mask == MAY_READ) ||
274 (S_ISDIR(la->la_mode) && !(mask & MAY_WRITE)))
275 if (mdd_capable(uc, CFS_CAP_DAC_READ_SEARCH))
281 int mdd_permission(const struct lu_env *env,
282 struct md_object *pobj, struct md_object *cobj,
283 struct md_attr *ma, int mask)
285 struct mdd_object *mdd_pobj, *mdd_cobj;
286 struct md_ucred *uc = NULL;
287 struct lu_attr *la = NULL;
288 int check_create, check_link;
290 int check_rename_src, check_rename_tar;
291 int check_vtx_part, check_vtx_full;
297 mdd_cobj = md2mdd_obj(cobj);
299 /* For cross_open case, the "mask" is open flags,
300 * so convert it to permission mask first.
301 * XXX: MDS_OPEN_CROSS must be NOT equal to permission mask MAY_*. */
302 if (unlikely(mask & MDS_OPEN_CROSS)) {
303 la = &mdd_env_info(env)->mti_la;
304 rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
308 mask = accmode(env, la, mask & ~MDS_OPEN_CROSS);
311 check_create = mask & MAY_CREATE;
312 check_link = mask & MAY_LINK;
313 check_unlink = mask & MAY_UNLINK;
314 check_rename_src = mask & MAY_RENAME_SRC;
315 check_rename_tar = mask & MAY_RENAME_TAR;
316 check_vtx_part = mask & MAY_VTX_PART;
317 check_vtx_full = mask & MAY_VTX_FULL;
318 check_rgetfacl = mask & MAY_RGETFACL;
320 mask &= ~(MAY_CREATE | MAY_LINK |
322 MAY_RENAME_SRC | MAY_RENAME_TAR |
323 MAY_VTX_PART | MAY_VTX_FULL |
326 rc = mdd_permission_internal_locked(env, mdd_cobj, NULL, mask,
329 if (!rc && (check_create || check_link))
330 rc = mdd_may_create(env, mdd_cobj, NULL, 1, check_link);
332 if (!rc && check_unlink) {
334 rc = mdd_may_unlink(env, mdd_cobj, ma);
337 if (!rc && (check_rename_src || check_rename_tar)) {
340 mdd_pobj = md2mdd_obj(pobj);
341 rc = mdd_may_delete(env, mdd_pobj, mdd_cobj, ma, 1,
345 if (!rc && (check_vtx_part || check_vtx_full)) {
349 la = &mdd_env_info(env)->mti_la;
350 rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
355 if (!(la->la_mode & S_ISVTX) || (la->la_uid == uc->mu_fsuid) ||
356 (check_vtx_full && (ma->ma_attr.la_valid & LA_UID) &&
357 (ma->ma_attr.la_uid == uc->mu_fsuid))) {
358 ma->ma_attr_flags |= MDS_VTX_BYPASS;
360 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
366 if (unlikely(!rc && check_rgetfacl)) {
371 la = &mdd_env_info(env)->mti_la;
372 rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
377 if (la->la_uid != uc->mu_fsuid &&
378 !mdd_capable(uc, CFS_CAP_FOWNER))
385 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
386 struct lustre_capa *capa, int renewal)
388 struct mdd_object *mdd_obj = md2mdd_obj(obj);
393 oc = mdo_capa_get(env, mdd_obj, renewal ? capa : NULL,
397 } else if (likely(oc != NULL)) {