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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
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 DEBUG_SUBSYSTEM S_MDS
46 #include <obd_class.h>
47 #include <lustre_ver.h>
48 #include <lprocfs_status.h>
49 #include <lustre_mds.h>
50 #include <lustre_idmap.h>
51 #include "mdd_internal.h"
53 #ifdef CONFIG_FS_POSIX_ACL
56 * Hold write_lock for o.
58 int mdd_acl_chmod(const struct lu_env *env, struct mdd_object *o, __u32 mode,
59 struct thandle *handle)
62 posix_acl_xattr_header *head;
63 posix_acl_xattr_entry *entry;
69 buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf,
70 sizeof(mdd_env_info(env)->mti_xattr_buf));
72 rc = mdo_xattr_get(env, o, buf, XATTR_NAME_ACL_ACCESS, BYPASS_CAPA);
73 if ((rc == -EOPNOTSUPP) || (rc == -ENODATA))
79 head = (posix_acl_xattr_header *)(buf->lb_buf);
80 entry = head->a_entries;
81 entry_count = (buf->lb_len - sizeof(head->a_version)) /
82 sizeof(posix_acl_xattr_entry);
86 rc = lustre_posix_acl_chmod_masq(entry, mode, entry_count);
90 rc = mdo_xattr_set(env, o, buf, XATTR_NAME_ACL_ACCESS,
91 0, handle, BYPASS_CAPA);
95 int mdd_acl_set(const struct lu_env *env, struct mdd_object *obj,
96 const struct lu_buf *buf, int fl)
98 struct mdd_device *mdd = mdd_obj2mdd_dev(obj);
99 struct lu_attr *la = &mdd_env_info(env)->mti_la;
100 struct thandle *handle;
101 posix_acl_xattr_header *head;
102 posix_acl_xattr_entry *entry;
104 bool not_equiv, mode_change;
108 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
112 head = (posix_acl_xattr_header *)(buf->lb_buf);
113 entry = head->a_entries;
114 entry_count = (buf->lb_len - sizeof(head->a_version)) /
115 sizeof(posix_acl_xattr_entry);
116 if (entry_count <= 0)
119 LASSERT(la->la_valid & LA_MODE);
121 rc = lustre_posix_acl_equiv_mode(entry, &mode, entry_count);
125 not_equiv = (rc > 0);
126 mode_change = (mode != la->la_mode);
128 handle = mdd_trans_create(env, mdd);
130 RETURN(PTR_ERR(handle));
132 /* rc tells whether ACL can be represented by i_mode only */
134 rc = mdo_declare_xattr_set(env, obj, buf,
135 XATTR_NAME_ACL_ACCESS, fl, handle);
137 rc = mdo_declare_xattr_del(env, obj, XATTR_NAME_ACL_ACCESS,
144 rc = mdo_declare_attr_set(env, obj, la, handle);
147 rc = mdd_trans_start(env, mdd, handle);
151 mdd_write_lock(env, obj, MOR_TGT_CHILD);
152 /* whether ACL can be represented by i_mode only */
154 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_ACCESS, fl,
155 handle, mdd_object_capa(env, obj));
157 rc = mdo_xattr_del(env, obj, XATTR_NAME_ACL_ACCESS, handle,
158 mdd_object_capa(env, obj));
163 rc = mdo_attr_set(env, obj, la, handle,
164 mdd_object_capa(env, obj));
166 /* security-replated changes may require sync */
167 handle->th_sync |= !!mdd->mdd_sync_permission;
169 mdd_write_unlock(env, obj);
171 mdd_trans_stop(env, mdd, rc, handle);
177 * Fix mode and ACL according to the default ACL(buf)
178 * \retval = 0 ACL does not need to be reset.
179 * \retval = 1 ACL needs to be reset.
182 int __mdd_fix_mode_acl(const struct lu_env *env, struct lu_buf *buf,
185 posix_acl_xattr_header *head;
186 posix_acl_xattr_entry *entry;
192 head = (posix_acl_xattr_header *)(buf->lb_buf);
193 entry = head->a_entries;
194 entry_count = (buf->lb_len - sizeof(head->a_version)) /
195 sizeof(posix_acl_xattr_entry);
196 if (entry_count <= 0)
199 rc = lustre_posix_acl_create_masq(entry, mode, entry_count);
207 * Hold read_lock for obj.
209 static int mdd_check_acl(const struct lu_env *env, struct mdd_object *obj,
210 struct lu_attr *la, int mask)
212 #ifdef CONFIG_FS_POSIX_ACL
213 struct lu_ucred *uc = lu_ucred_assert(env);
214 posix_acl_xattr_header *head;
215 posix_acl_xattr_entry *entry;
221 buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf,
222 sizeof(mdd_env_info(env)->mti_xattr_buf));
223 rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_ACL_ACCESS,
224 mdd_object_capa(env, obj));
226 RETURN(rc ? : -EACCES);
229 head = (posix_acl_xattr_header *)(buf->lb_buf);
230 entry = head->a_entries;
231 entry_count = (buf->lb_len - sizeof(head->a_version)) /
232 sizeof(posix_acl_xattr_entry);
234 rc = lustre_posix_acl_permission(uc, la, mask, entry, entry_count);
242 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
243 struct lu_attr *la, int mask, int role)
245 struct lu_ucred *uc = lu_ucred(env);
253 /* These means unnecessary for permission check */
254 if ((uc == NULL) || (uc->uc_valid == UCRED_INIT))
257 /* Invalid user credit */
258 if (uc->uc_valid == UCRED_INVALID)
262 * Nobody gets write access to an immutable file.
264 if ((mask & MAY_WRITE) && mdd_is_immutable(obj))
268 la = &mdd_env_info(env)->mti_la;
269 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
275 if (uc->uc_fsuid == la->la_uid) {
278 if (mode & S_IRWXG) {
280 mdd_read_lock(env, obj, role);
281 rc = mdd_check_acl(env, obj, la, mask);
283 mdd_read_unlock(env, obj);
285 goto check_capabilities;
286 else if ((rc != -EAGAIN) && (rc != -EOPNOTSUPP) &&
290 if (lustre_in_group_p(uc, la->la_gid))
294 if (((mode & mask & S_IRWXO) == mask))
298 if (!(mask & MAY_EXEC) ||
299 (la->la_mode & S_IXUGO) || S_ISDIR(la->la_mode))
300 if (md_capable(uc, CFS_CAP_DAC_OVERRIDE))
303 if ((mask == MAY_READ) ||
304 (S_ISDIR(la->la_mode) && !(mask & MAY_WRITE)))
305 if (md_capable(uc, CFS_CAP_DAC_READ_SEARCH))
311 int mdd_permission(const struct lu_env *env,
312 struct md_object *pobj, struct md_object *cobj,
313 struct md_attr *ma, int mask)
315 struct mdd_object *mdd_pobj, *mdd_cobj;
316 struct lu_ucred *uc = NULL;
317 struct lu_attr *la = &mdd_env_info(env)->mti_cattr;
318 int check_create, check_link;
320 int check_rename_src, check_rename_tar;
321 int check_vtx_part, check_vtx_full;
327 mdd_cobj = md2mdd_obj(cobj);
329 rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
333 /* For cross_open case, the "mask" is open flags,
334 * so convert it to permission mask first.
335 * XXX: MDS_OPEN_CROSS must be NOT equal to permission mask MAY_*. */
336 if (unlikely(mask & MDS_OPEN_CROSS))
337 mask = accmode(env, la, mask & ~MDS_OPEN_CROSS);
339 check_create = mask & MAY_CREATE;
340 check_link = mask & MAY_LINK;
341 check_unlink = mask & MAY_UNLINK;
342 check_rename_src = mask & MAY_RENAME_SRC;
343 check_rename_tar = mask & MAY_RENAME_TAR;
344 check_vtx_part = mask & MAY_VTX_PART;
345 check_vtx_full = mask & MAY_VTX_FULL;
346 check_rgetfacl = mask & MAY_RGETFACL;
348 mask &= ~(MAY_CREATE | MAY_LINK |
350 MAY_RENAME_SRC | MAY_RENAME_TAR |
351 MAY_VTX_PART | MAY_VTX_FULL |
354 rc = mdd_permission_internal_locked(env, mdd_cobj, NULL, mask,
357 if (!rc && (check_create || check_link))
358 rc = mdd_may_create(env, mdd_cobj, NULL, 1, check_link);
360 if (!rc && check_unlink)
361 rc = mdd_may_unlink(env, mdd_cobj, la);
363 if (!rc && (check_rename_src || check_rename_tar)) {
365 mdd_pobj = md2mdd_obj(pobj);
366 rc = mdd_may_delete(env, mdd_pobj, mdd_cobj, la, NULL, 1,
370 if (!rc && (check_vtx_part || check_vtx_full)) {
371 uc = lu_ucred_assert(env);
373 la = &mdd_env_info(env)->mti_la;
374 rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
379 if (!(la->la_mode & S_ISVTX) || (la->la_uid == uc->uc_fsuid) ||
380 (check_vtx_full && (ma->ma_attr.la_valid & LA_UID) &&
381 (ma->ma_attr.la_uid == uc->uc_fsuid))) {
382 ma->ma_attr_flags |= MDS_VTX_BYPASS;
384 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
390 if (unlikely(!rc && check_rgetfacl)) {
392 uc = lu_ucred_assert(env);
394 if (la->la_uid != uc->uc_fsuid &&
395 !md_capable(uc, CFS_CAP_FOWNER))
402 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
403 struct lustre_capa *capa, int renewal)
405 struct mdd_object *mdd_obj = md2mdd_obj(obj);
410 oc = mdo_capa_get(env, mdd_obj, renewal ? capa : NULL,
414 } else if (likely(oc != NULL)) {