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 2008 Sun Microsystems, Inc. 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 #include <linux/jbd.h>
51 #include <obd_class.h>
52 #include <lustre_ver.h>
53 #include <obd_support.h>
54 #include <lprocfs_status.h>
56 #include <linux/ldiskfs_fs.h>
57 #include <lustre_mds.h>
58 #include <lustre/lustre_idl.h>
60 #include "mdd_internal.h"
62 #ifdef CONFIG_FS_POSIX_ACL
65 * Get default acl EA only.
66 * Hold read_lock for mdd_obj.
68 int mdd_def_acl_get(const struct lu_env *env, struct mdd_object *mdd_obj,
75 if (ma->ma_valid & MA_ACL_DEF)
78 buf = mdd_buf_get(env, ma->ma_acl, ma->ma_acl_size);
79 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_ACL_DEFAULT,
83 ma->ma_valid |= MA_ACL_DEF;
85 } else if ((rc == -EOPNOTSUPP) || (rc == -ENODATA)) {
92 * Hold write_lock for o.
94 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
95 struct thandle *handle)
98 posix_acl_xattr_header *head;
99 posix_acl_xattr_entry *entry;
105 buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf,
106 sizeof(mdd_env_info(env)->mti_xattr_buf));
108 rc = mdo_xattr_get(env, o, buf, XATTR_NAME_ACL_ACCESS, BYPASS_CAPA);
109 if ((rc == -EOPNOTSUPP) || (rc == -ENODATA))
115 head = (posix_acl_xattr_header *)(buf->lb_buf);
116 entry = head->a_entries;
117 entry_count = (buf->lb_len - sizeof(head->a_version)) /
118 sizeof(posix_acl_xattr_entry);
119 if (entry_count <= 0)
122 rc = lustre_posix_acl_chmod_masq(entry, mode, entry_count);
126 rc = mdo_xattr_set(env, o, buf, XATTR_NAME_ACL_ACCESS,
127 0, handle, BYPASS_CAPA);
132 * Hold write_lock for obj.
134 int __mdd_acl_init(const struct lu_env *env, struct mdd_object *obj,
135 struct lu_buf *buf, __u32 *mode, struct thandle *handle)
137 posix_acl_xattr_header *head;
138 posix_acl_xattr_entry *entry;
144 head = (posix_acl_xattr_header *)(buf->lb_buf);
145 entry = head->a_entries;
146 entry_count = (buf->lb_len - sizeof(head->a_version)) /
147 sizeof(posix_acl_xattr_entry);
148 if (entry_count <= 0)
151 if (S_ISDIR(*mode)) {
152 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_DEFAULT, 0,
153 handle, BYPASS_CAPA);
158 rc = lustre_posix_acl_create_masq(entry, mode, entry_count);
162 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_ACCESS, 0, handle,
169 * Hold read_lock for obj.
171 static int mdd_check_acl(const struct lu_env *env, struct mdd_object *obj,
172 struct lu_attr *la, int mask)
174 #ifdef CONFIG_FS_POSIX_ACL
175 struct md_ucred *uc = md_ucred(env);
176 posix_acl_xattr_header *head;
177 posix_acl_xattr_entry *entry;
183 buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf,
184 sizeof(mdd_env_info(env)->mti_xattr_buf));
185 rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_ACL_ACCESS,
186 mdd_object_capa(env, obj));
188 RETURN(rc ? : -EACCES);
191 head = (posix_acl_xattr_header *)(buf->lb_buf);
192 entry = head->a_entries;
193 entry_count = (buf->lb_len - sizeof(head->a_version)) /
194 sizeof(posix_acl_xattr_entry);
196 rc = lustre_posix_acl_permission(uc, la, mask, entry, entry_count);
204 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
205 struct lu_attr *la, int mask, int role)
207 struct md_ucred *uc = md_ucred(env);
215 /* These means unnecessary for permission check */
216 if ((uc == NULL) || (uc->mu_valid == UCRED_INIT))
219 /* Invalid user credit */
220 if (uc->mu_valid == UCRED_INVALID)
224 * Nobody gets write access to an immutable file.
226 if ((mask & MAY_WRITE) && mdd_is_immutable(obj))
230 la = &mdd_env_info(env)->mti_la;
231 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
237 if (uc->mu_fsuid == la->la_uid) {
240 if (mode & S_IRWXG) {
242 mdd_read_lock(env, obj, role);
243 rc = mdd_check_acl(env, obj, la, mask);
245 mdd_read_unlock(env, obj);
247 goto check_capabilities;
248 else if ((rc != -EAGAIN) && (rc != -EOPNOTSUPP) &&
252 if (lustre_in_group_p(uc, la->la_gid))
256 if (((mode & mask & S_IRWXO) == mask))
260 if (!(mask & MAY_EXEC) ||
261 (la->la_mode & S_IXUGO) || S_ISDIR(la->la_mode))
262 if (mdd_capable(uc, CFS_CAP_DAC_OVERRIDE))
265 if ((mask == MAY_READ) ||
266 (S_ISDIR(la->la_mode) && !(mask & MAY_WRITE)))
267 if (mdd_capable(uc, CFS_CAP_DAC_READ_SEARCH))
273 int mdd_permission(const struct lu_env *env,
274 struct md_object *pobj, struct md_object *cobj,
275 struct md_attr *ma, int mask)
277 struct mdd_object *mdd_pobj, *mdd_cobj;
278 struct md_ucred *uc = NULL;
279 struct lu_attr *la = NULL;
280 int check_create, check_link;
282 int check_rename_src, check_rename_tar;
283 int check_vtx_part, check_vtx_full;
289 mdd_cobj = md2mdd_obj(cobj);
291 /* For cross_open case, the "mask" is open flags,
292 * so convert it to permission mask first.
293 * XXX: MDS_OPEN_CROSS must be NOT equal to permission mask MAY_*. */
294 if (unlikely(mask & MDS_OPEN_CROSS)) {
295 la = &mdd_env_info(env)->mti_la;
296 rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
300 mask = accmode(env, la, mask & ~MDS_OPEN_CROSS);
303 check_create = mask & MAY_CREATE;
304 check_link = mask & MAY_LINK;
305 check_unlink = mask & MAY_UNLINK;
306 check_rename_src = mask & MAY_RENAME_SRC;
307 check_rename_tar = mask & MAY_RENAME_TAR;
308 check_vtx_part = mask & MAY_VTX_PART;
309 check_vtx_full = mask & MAY_VTX_FULL;
310 check_rgetfacl = mask & MAY_RGETFACL;
312 mask &= ~(MAY_CREATE | MAY_LINK |
314 MAY_RENAME_SRC | MAY_RENAME_TAR |
315 MAY_VTX_PART | MAY_VTX_FULL |
318 rc = mdd_permission_internal_locked(env, mdd_cobj, NULL, mask,
321 if (!rc && (check_create || check_link))
322 rc = mdd_may_create(env, mdd_cobj, NULL, 1, check_link);
324 if (!rc && check_unlink) {
326 rc = mdd_may_unlink(env, mdd_cobj, ma);
329 if (!rc && (check_rename_src || check_rename_tar)) {
332 mdd_pobj = md2mdd_obj(pobj);
333 rc = mdd_may_delete(env, mdd_pobj, mdd_cobj, ma, 1,
337 if (!rc && (check_vtx_part || check_vtx_full)) {
341 la = &mdd_env_info(env)->mti_la;
342 rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
347 if (!(la->la_mode & S_ISVTX) || (la->la_uid == uc->mu_fsuid) ||
348 (check_vtx_full && (ma->ma_attr.la_valid & LA_UID) &&
349 (ma->ma_attr.la_uid == uc->mu_fsuid))) {
350 ma->ma_attr_flags |= MDS_VTX_BYPASS;
352 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
358 if (unlikely(!rc && check_rgetfacl)) {
363 la = &mdd_env_info(env)->mti_la;
364 rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
369 if (la->la_uid != uc->mu_fsuid &&
370 !mdd_capable(uc, CFS_CAP_FOWNER))
377 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
378 struct lustre_capa *capa, int renewal)
380 struct mdd_object *mdd_obj = md2mdd_obj(obj);
385 oc = mdo_capa_get(env, mdd_obj, renewal ? capa : NULL,
389 } else if (likely(oc != NULL)) {