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, 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>
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 * Hold write_lock for obj.
179 int __mdd_acl_init(const struct lu_env *env, struct mdd_object *obj,
180 struct lu_buf *buf, __u32 *mode, struct thandle *handle)
182 posix_acl_xattr_header *head;
183 posix_acl_xattr_entry *entry;
190 head = (posix_acl_xattr_header *)(buf->lb_buf);
191 entry = head->a_entries;
192 entry_count = (buf->lb_len - sizeof(head->a_version)) /
193 sizeof(posix_acl_xattr_entry);
194 if (entry_count <= 0)
197 if (S_ISDIR(*mode)) {
198 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_DEFAULT, 0,
199 handle, BYPASS_CAPA);
204 rc = lustre_posix_acl_create_masq(entry, mode, entry_count);
208 /* part of ACL went into i_mode */
210 struct mdd_thread_info *info = mdd_env_info(env);
211 struct lu_attr *pattr = &info->mti_pattr;
213 /* mode was initialized within object creation,
214 * so we need explict ->attr_set() to update it */
215 pattr->la_valid = LA_MODE;
216 pattr->la_mode = *mode;
217 rc2 = mdo_attr_set(env, obj, pattr, handle, BYPASS_CAPA);
223 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_ACL_ACCESS, 0,
224 handle, BYPASS_CAPA);
230 * Hold read_lock for obj.
232 static int mdd_check_acl(const struct lu_env *env, struct mdd_object *obj,
233 struct lu_attr *la, int mask)
235 #ifdef CONFIG_FS_POSIX_ACL
236 struct lu_ucred *uc = lu_ucred_assert(env);
237 posix_acl_xattr_header *head;
238 posix_acl_xattr_entry *entry;
244 buf = mdd_buf_get(env, mdd_env_info(env)->mti_xattr_buf,
245 sizeof(mdd_env_info(env)->mti_xattr_buf));
246 rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_ACL_ACCESS,
247 mdd_object_capa(env, obj));
249 RETURN(rc ? : -EACCES);
252 head = (posix_acl_xattr_header *)(buf->lb_buf);
253 entry = head->a_entries;
254 entry_count = (buf->lb_len - sizeof(head->a_version)) /
255 sizeof(posix_acl_xattr_entry);
257 rc = lustre_posix_acl_permission(uc, la, mask, entry, entry_count);
265 int __mdd_permission_internal(const struct lu_env *env, struct mdd_object *obj,
266 struct lu_attr *la, int mask, int role)
268 struct lu_ucred *uc = lu_ucred(env);
276 /* These means unnecessary for permission check */
277 if ((uc == NULL) || (uc->uc_valid == UCRED_INIT))
280 /* Invalid user credit */
281 if (uc->uc_valid == UCRED_INVALID)
285 * Nobody gets write access to an immutable file.
287 if ((mask & MAY_WRITE) && mdd_is_immutable(obj))
291 la = &mdd_env_info(env)->mti_la;
292 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
298 if (uc->uc_fsuid == la->la_uid) {
301 if (mode & S_IRWXG) {
303 mdd_read_lock(env, obj, role);
304 rc = mdd_check_acl(env, obj, la, mask);
306 mdd_read_unlock(env, obj);
308 goto check_capabilities;
309 else if ((rc != -EAGAIN) && (rc != -EOPNOTSUPP) &&
313 if (lustre_in_group_p(uc, la->la_gid))
317 if (((mode & mask & S_IRWXO) == mask))
321 if (!(mask & MAY_EXEC) ||
322 (la->la_mode & S_IXUGO) || S_ISDIR(la->la_mode))
323 if (md_capable(uc, CFS_CAP_DAC_OVERRIDE))
326 if ((mask == MAY_READ) ||
327 (S_ISDIR(la->la_mode) && !(mask & MAY_WRITE)))
328 if (md_capable(uc, CFS_CAP_DAC_READ_SEARCH))
334 int mdd_permission(const struct lu_env *env,
335 struct md_object *pobj, struct md_object *cobj,
336 struct md_attr *ma, int mask)
338 struct mdd_object *mdd_pobj, *mdd_cobj;
339 struct lu_ucred *uc = NULL;
340 struct lu_attr *la = &mdd_env_info(env)->mti_cattr;
341 int check_create, check_link;
343 int check_rename_src, check_rename_tar;
344 int check_vtx_part, check_vtx_full;
350 mdd_cobj = md2mdd_obj(cobj);
352 rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
356 /* For cross_open case, the "mask" is open flags,
357 * so convert it to permission mask first.
358 * XXX: MDS_OPEN_CROSS must be NOT equal to permission mask MAY_*. */
359 if (unlikely(mask & MDS_OPEN_CROSS))
360 mask = accmode(env, la, mask & ~MDS_OPEN_CROSS);
362 check_create = mask & MAY_CREATE;
363 check_link = mask & MAY_LINK;
364 check_unlink = mask & MAY_UNLINK;
365 check_rename_src = mask & MAY_RENAME_SRC;
366 check_rename_tar = mask & MAY_RENAME_TAR;
367 check_vtx_part = mask & MAY_VTX_PART;
368 check_vtx_full = mask & MAY_VTX_FULL;
369 check_rgetfacl = mask & MAY_RGETFACL;
371 mask &= ~(MAY_CREATE | MAY_LINK |
373 MAY_RENAME_SRC | MAY_RENAME_TAR |
374 MAY_VTX_PART | MAY_VTX_FULL |
377 rc = mdd_permission_internal_locked(env, mdd_cobj, NULL, mask,
380 if (!rc && (check_create || check_link))
381 rc = mdd_may_create(env, mdd_cobj, NULL, 1, check_link);
383 if (!rc && check_unlink)
384 rc = mdd_may_unlink(env, mdd_cobj, la);
386 if (!rc && (check_rename_src || check_rename_tar)) {
388 mdd_pobj = md2mdd_obj(pobj);
389 rc = mdd_may_delete(env, mdd_pobj, mdd_cobj, la, NULL, 1,
393 if (!rc && (check_vtx_part || check_vtx_full)) {
394 uc = lu_ucred_assert(env);
396 la = &mdd_env_info(env)->mti_la;
397 rc = mdd_la_get(env, mdd_cobj, la, BYPASS_CAPA);
402 if (!(la->la_mode & S_ISVTX) || (la->la_uid == uc->uc_fsuid) ||
403 (check_vtx_full && (ma->ma_attr.la_valid & LA_UID) &&
404 (ma->ma_attr.la_uid == uc->uc_fsuid))) {
405 ma->ma_attr_flags |= MDS_VTX_BYPASS;
407 ma->ma_attr_flags &= ~MDS_VTX_BYPASS;
413 if (unlikely(!rc && check_rgetfacl)) {
415 uc = lu_ucred_assert(env);
417 if (la->la_uid != uc->uc_fsuid &&
418 !md_capable(uc, CFS_CAP_FOWNER))
425 int mdd_capa_get(const struct lu_env *env, struct md_object *obj,
426 struct lustre_capa *capa, int renewal)
428 struct mdd_object *mdd_obj = md2mdd_obj(obj);
433 oc = mdo_capa_get(env, mdd_obj, renewal ? capa : NULL,
437 } else if (likely(oc != NULL)) {