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.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/mdt/mdt_idmap.c
36 * Author: Lai Siyao <lsy@clusterfs.com>
37 * Author: Fan Yong <fanyong@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #ifndef AUTOCONF_INCLUDED
46 #include <linux/config.h>
48 #include <linux/module.h>
49 #include <linux/kernel.h>
51 #include <linux/kmod.h>
52 #include <linux/string.h>
53 #include <linux/stat.h>
54 #include <linux/errno.h>
55 #include <linux/version.h>
56 #include <linux/unistd.h>
57 #include <asm/system.h>
58 #include <asm/uaccess.h>
60 #include <linux/stat.h>
61 #include <asm/uaccess.h>
62 #include <linux/slab.h>
64 #include <libcfs/libcfs.h>
65 #include <libcfs/lucache.h>
67 #include <obd_class.h>
68 #include <obd_support.h>
69 #include <lustre_net.h>
70 #include <lustre_import.h>
71 #include <lustre_dlm.h>
72 #include <lustre_sec.h>
73 #include <lustre_lib.h>
75 #include "mdt_internal.h"
77 #define mdt_init_sec_none(reply, exp) \
79 reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT | \
80 OBD_CONNECT_RMT_CLIENT_FORCE | \
81 OBD_CONNECT_MDS_CAPA | \
82 OBD_CONNECT_OSS_CAPA); \
83 cfs_spin_lock(&exp->exp_lock); \
84 exp->exp_connect_flags = reply->ocd_connect_flags; \
85 cfs_spin_unlock(&exp->exp_lock); \
88 int mdt_init_sec_level(struct mdt_thread_info *info)
90 struct mdt_device *mdt = info->mti_mdt;
91 struct ptlrpc_request *req = mdt_info_req(info);
92 char *client = libcfs_nid2str(req->rq_peer.nid);
93 struct obd_export *exp = req->rq_export;
94 struct obd_device *obd = exp->exp_obd;
95 struct obd_connect_data *data, *reply;
99 data = req_capsule_client_get(info->mti_pill, &RMF_CONNECT_DATA);
100 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
101 if (data == NULL || reply == NULL)
104 /* connection from MDT is always trusted */
105 if (req->rq_auth_usr_mdt) {
106 mdt_init_sec_none(reply, exp);
110 /* no GSS support case */
111 if (!req->rq_auth_gss) {
112 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
113 CWARN("client %s -> target %s does not user GSS, "
114 "can not run under security level %d.\n",
115 client, obd->obd_name, mdt->mdt_sec_level);
118 mdt_init_sec_none(reply, exp);
123 /* old version case */
124 if (unlikely(!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) ||
125 !(data->ocd_connect_flags & OBD_CONNECT_MDS_CAPA) ||
126 !(data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA))) {
127 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
128 CWARN("client %s -> target %s uses old version, "
129 "can not run under security level %d.\n",
130 client, obd->obd_name, mdt->mdt_sec_level);
133 CWARN("client %s -> target %s uses old version, "
134 "run under security level %d.\n",
135 client, obd->obd_name, mdt->mdt_sec_level);
136 mdt_init_sec_none(reply, exp);
141 remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT_FORCE;
143 if (!req->rq_auth_remote)
144 CDEBUG(D_SEC, "client (local realm) %s -> target %s "
145 "asked to be remote.\n", client, obd->obd_name);
146 } else if (req->rq_auth_remote) {
148 CDEBUG(D_SEC, "client (remote realm) %s -> target %s is set "
149 "as remote by default.\n", client, obd->obd_name);
153 if (!mdt->mdt_opts.mo_oss_capa) {
154 CDEBUG(D_SEC, "client %s -> target %s is set as remote,"
155 " but OSS capabilities are not enabled: %d.\n",
156 client, obd->obd_name, mdt->mdt_opts.mo_oss_capa);
160 if (req->rq_auth_uid == INVALID_UID) {
161 CDEBUG(D_SEC, "client %s -> target %s: user is not "
162 "authenticated!\n", client, obd->obd_name);
167 switch (mdt->mdt_sec_level) {
168 case LUSTRE_SEC_NONE:
170 mdt_init_sec_none(reply, exp);
173 CDEBUG(D_SEC, "client %s -> target %s is set as remote, "
174 "can not run under security level %d.\n",
175 client, obd->obd_name, mdt->mdt_sec_level);
178 case LUSTRE_SEC_REMOTE:
180 mdt_init_sec_none(reply, exp);
184 reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |
185 OBD_CONNECT_RMT_CLIENT_FORCE);
186 if (!mdt->mdt_opts.mo_mds_capa)
187 reply->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
188 if (!mdt->mdt_opts.mo_oss_capa)
189 reply->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
191 cfs_spin_lock(&exp->exp_lock);
192 exp->exp_connect_flags = reply->ocd_connect_flags;
193 cfs_spin_unlock(&exp->exp_lock);
203 int mdt_init_idmap(struct mdt_thread_info *info)
205 struct ptlrpc_request *req = mdt_info_req(info);
206 struct mdt_export_data *med = mdt_req2med(req);
207 struct obd_export *exp = req->rq_export;
208 char *client = libcfs_nid2str(req->rq_peer.nid);
209 struct obd_device *obd = exp->exp_obd;
213 if (exp_connect_rmtclient(exp)) {
214 cfs_mutex_lock(&med->med_idmap_mutex);
216 med->med_idmap = lustre_idmap_init();
217 cfs_mutex_unlock(&med->med_idmap_mutex);
219 if (IS_ERR(med->med_idmap)) {
220 long err = PTR_ERR(med->med_idmap);
222 med->med_idmap = NULL;
223 CERROR("client %s -> target %s "
224 "failed to init idmap [%ld]!\n",
225 client, obd->obd_name, err);
227 } else if (!med->med_idmap) {
228 CERROR("client %s -> target %s "
229 "failed to init(2) idmap!\n",
230 client, obd->obd_name);
234 CDEBUG(D_SEC, "client %s -> target %s is remote.\n",
235 client, obd->obd_name);
236 /* NB, MDS_CONNECT establish root idmap too! */
237 rc = mdt_handle_idmap(info);
242 void mdt_cleanup_idmap(struct mdt_export_data *med)
244 cfs_mutex_lock(&med->med_idmap_mutex);
245 if (med->med_idmap != NULL) {
246 lustre_idmap_fini(med->med_idmap);
247 med->med_idmap = NULL;
249 cfs_mutex_unlock(&med->med_idmap_mutex);
252 static inline void mdt_revoke_export_locks(struct obd_export *exp)
254 /* don't revoke locks during recovery */
255 if (exp->exp_obd->obd_recovering)
258 ldlm_revoke_export_locks(exp);
261 int mdt_handle_idmap(struct mdt_thread_info *info)
263 struct ptlrpc_request *req = mdt_info_req(info);
264 struct mdt_device *mdt = info->mti_mdt;
265 struct mdt_export_data *med;
266 struct ptlrpc_user_desc *pud = req->rq_user_desc;
267 struct md_identity *identity;
275 med = mdt_req2med(req);
276 if (!exp_connect_rmtclient(info->mti_exp))
279 opc = lustre_msg_get_opc(req->rq_reqmsg);
280 /* Bypass other opc */
281 if ((opc != SEC_CTX_INIT) && (opc != SEC_CTX_INIT_CONT) &&
282 (opc != SEC_CTX_FINI) && (opc != MDS_CONNECT))
285 LASSERT(med->med_idmap);
287 if (unlikely(!pud)) {
288 CDEBUG(D_SEC, "remote client must run with rq_user_desc "
293 if (req->rq_auth_mapped_uid == INVALID_UID) {
294 CDEBUG(D_SEC, "invalid authorized mapped uid, please check "
295 "/etc/lustre/idmap.conf!\n");
299 if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
300 CDEBUG(D_SEC, "remote client must run with identity_get "
305 identity = mdt_identity_get(mdt->mdt_identity_cache,
306 req->rq_auth_mapped_uid);
307 if (IS_ERR(identity)) {
308 CDEBUG(D_SEC, "can't get mdt identity(%u), no mapping added\n",
309 req->rq_auth_mapped_uid);
315 case SEC_CTX_INIT_CONT:
317 rc = lustre_idmap_add(med->med_idmap,
318 pud->pud_uid, identity->mi_uid,
319 pud->pud_gid, identity->mi_gid);
322 rc = lustre_idmap_del(med->med_idmap,
323 pud->pud_uid, identity->mi_uid,
324 pud->pud_gid, identity->mi_gid);
328 mdt_identity_put(mdt->mdt_identity_cache, identity);
335 case SEC_CTX_INIT_CONT:
337 mdt_revoke_export_locks(req->rq_export);
344 int ptlrpc_user_desc_do_idmap(struct ptlrpc_request *req,
345 struct ptlrpc_user_desc *pud)
347 struct mdt_export_data *med = mdt_req2med(req);
348 struct lustre_idmap_table *idmap = med->med_idmap;
352 /* Only remote client need desc_to_idmap. */
353 if (!exp_connect_rmtclient(req->rq_export))
356 uid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_uid);
357 if (uid == CFS_IDMAP_NOTFOUND) {
358 CDEBUG(D_SEC, "no mapping for uid %u\n", pud->pud_uid);
362 if (pud->pud_uid == pud->pud_fsuid) {
365 fsuid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_fsuid);
366 if (fsuid == CFS_IDMAP_NOTFOUND) {
367 CDEBUG(D_SEC, "no mapping for fsuid %u\n",
373 gid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_gid);
374 if (gid == CFS_IDMAP_NOTFOUND) {
375 CDEBUG(D_SEC, "no mapping for gid %u\n", pud->pud_gid);
379 if (pud->pud_gid == pud->pud_fsgid) {
382 fsgid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_fsgid);
383 if (fsgid == CFS_IDMAP_NOTFOUND) {
384 CDEBUG(D_SEC, "no mapping for fsgid %u\n",
392 pud->pud_fsuid = fsuid;
393 pud->pud_fsgid = fsgid;
401 void mdt_body_reverse_idmap(struct mdt_thread_info *info, struct mdt_body *body)
403 struct ptlrpc_request *req = mdt_info_req(info);
404 struct md_ucred *uc = mdt_ucred(info);
405 struct mdt_export_data *med = mdt_req2med(req);
406 struct lustre_idmap_table *idmap = med->med_idmap;
408 if (!exp_connect_rmtclient(info->mti_exp))
411 if (body->valid & OBD_MD_FLUID) {
412 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 1, body->uid);
414 if (uid == CFS_IDMAP_NOTFOUND) {
416 if (body->valid & OBD_MD_FLMODE)
417 body->mode = (body->mode & ~S_IRWXU) |
418 ((body->mode & S_IRWXO) << 6);
424 if (body->valid & OBD_MD_FLGID) {
425 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 1, body->gid);
427 if (gid == CFS_IDMAP_NOTFOUND) {
429 if (body->valid & OBD_MD_FLMODE)
430 body->mode = (body->mode & ~S_IRWXG) |
431 ((body->mode & S_IRWXO) << 3);
438 /* Do not ignore root_squash for non-setattr case. */
439 int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
441 struct ptlrpc_request *req = mdt_info_req(info);
442 struct md_ucred *uc = mdt_ucred(info);
443 struct lu_attr *attr = &info->mti_attr.ma_attr;
444 struct mdt_export_data *med = mdt_req2med(req);
445 struct lustre_idmap_table *idmap = med->med_idmap;
447 if ((uc->mu_valid != UCRED_OLD) && (uc->mu_valid != UCRED_NEW))
450 if (op != REINT_SETATTR) {
451 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
452 attr->la_uid = uc->mu_fsuid;
453 /* for S_ISGID, inherit gid from his parent, such work will be
454 * done in cmm/mdd layer, here set all cases as uc->mu_fsgid. */
455 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
456 attr->la_gid = uc->mu_fsgid;
457 } else if (exp_connect_rmtclient(info->mti_exp)) {
458 /* NB: -1 case will be handled by mdt_fix_attr() later. */
459 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1)) {
460 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 0,
463 if (uid == CFS_IDMAP_NOTFOUND) {
464 CDEBUG(D_SEC, "Deny chown to uid %u\n",
471 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1)) {
472 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 0,
475 if (gid == CFS_IDMAP_NOTFOUND) {
476 CDEBUG(D_SEC, "Deny chown to gid %u\n",