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>
40 #define DEBUG_SUBSYSTEM S_MDS
42 #include <linux/module.h>
43 #include <linux/kernel.h>
45 #include <linux/kmod.h>
46 #include <linux/string.h>
47 #include <linux/stat.h>
48 #include <linux/errno.h>
49 #include <linux/version.h>
50 #include <linux/unistd.h>
51 #include <asm/uaccess.h>
53 #include <linux/stat.h>
54 #include <asm/uaccess.h>
55 #include <linux/slab.h>
57 #include <libcfs/libcfs.h>
58 #include <libcfs/lucache.h>
60 #include <obd_class.h>
61 #include <obd_support.h>
62 #include <lustre_net.h>
63 #include <lustre_import.h>
64 #include <lustre_dlm.h>
65 #include <lustre_sec.h>
66 #include <lustre_lib.h>
68 #include "mdt_internal.h"
70 #define mdt_init_sec_none(reply, exp) \
72 reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT | \
73 OBD_CONNECT_RMT_CLIENT_FORCE | \
74 OBD_CONNECT_MDS_CAPA | \
75 OBD_CONNECT_OSS_CAPA); \
76 cfs_spin_lock(&exp->exp_lock); \
77 exp->exp_connect_flags = reply->ocd_connect_flags; \
78 cfs_spin_unlock(&exp->exp_lock); \
81 int mdt_init_sec_level(struct mdt_thread_info *info)
83 struct mdt_device *mdt = info->mti_mdt;
84 struct ptlrpc_request *req = mdt_info_req(info);
85 char *client = libcfs_nid2str(req->rq_peer.nid);
86 struct obd_export *exp = req->rq_export;
87 struct obd_device *obd = exp->exp_obd;
88 struct obd_connect_data *data, *reply;
92 data = req_capsule_client_get(info->mti_pill, &RMF_CONNECT_DATA);
93 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
94 if (data == NULL || reply == NULL)
97 /* connection from MDT is always trusted */
98 if (req->rq_auth_usr_mdt) {
99 mdt_init_sec_none(reply, exp);
103 /* no GSS support case */
104 if (!req->rq_auth_gss) {
105 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
106 CWARN("client %s -> target %s does not user GSS, "
107 "can not run under security level %d.\n",
108 client, obd->obd_name, mdt->mdt_sec_level);
111 mdt_init_sec_none(reply, exp);
116 /* old version case */
117 if (unlikely(!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) ||
118 !(data->ocd_connect_flags & OBD_CONNECT_MDS_CAPA) ||
119 !(data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA))) {
120 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
121 CWARN("client %s -> target %s uses old version, "
122 "can not run under security level %d.\n",
123 client, obd->obd_name, mdt->mdt_sec_level);
126 CWARN("client %s -> target %s uses old version, "
127 "run under security level %d.\n",
128 client, obd->obd_name, mdt->mdt_sec_level);
129 mdt_init_sec_none(reply, exp);
134 remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT_FORCE;
136 if (!req->rq_auth_remote)
137 CDEBUG(D_SEC, "client (local realm) %s -> target %s "
138 "asked to be remote.\n", client, obd->obd_name);
139 } else if (req->rq_auth_remote) {
141 CDEBUG(D_SEC, "client (remote realm) %s -> target %s is set "
142 "as remote by default.\n", client, obd->obd_name);
146 if (!mdt->mdt_opts.mo_oss_capa) {
147 CDEBUG(D_SEC, "client %s -> target %s is set as remote,"
148 " but OSS capabilities are not enabled: %d.\n",
149 client, obd->obd_name, mdt->mdt_opts.mo_oss_capa);
153 if (req->rq_auth_uid == INVALID_UID) {
154 CDEBUG(D_SEC, "client %s -> target %s: user is not "
155 "authenticated!\n", client, obd->obd_name);
160 switch (mdt->mdt_sec_level) {
161 case LUSTRE_SEC_NONE:
163 mdt_init_sec_none(reply, exp);
166 CDEBUG(D_SEC, "client %s -> target %s is set as remote, "
167 "can not run under security level %d.\n",
168 client, obd->obd_name, mdt->mdt_sec_level);
171 case LUSTRE_SEC_REMOTE:
173 mdt_init_sec_none(reply, exp);
177 reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |
178 OBD_CONNECT_RMT_CLIENT_FORCE);
179 if (!mdt->mdt_opts.mo_mds_capa)
180 reply->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
181 if (!mdt->mdt_opts.mo_oss_capa)
182 reply->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
184 cfs_spin_lock(&exp->exp_lock);
185 exp->exp_connect_flags = reply->ocd_connect_flags;
186 cfs_spin_unlock(&exp->exp_lock);
196 int mdt_init_idmap(struct mdt_thread_info *info)
198 struct ptlrpc_request *req = mdt_info_req(info);
199 struct mdt_export_data *med = mdt_req2med(req);
200 struct obd_export *exp = req->rq_export;
201 char *client = libcfs_nid2str(req->rq_peer.nid);
202 struct obd_device *obd = exp->exp_obd;
206 if (exp_connect_rmtclient(exp)) {
207 cfs_mutex_lock(&med->med_idmap_mutex);
209 med->med_idmap = lustre_idmap_init();
210 cfs_mutex_unlock(&med->med_idmap_mutex);
212 if (IS_ERR(med->med_idmap)) {
213 long err = PTR_ERR(med->med_idmap);
215 med->med_idmap = NULL;
216 CERROR("client %s -> target %s "
217 "failed to init idmap [%ld]!\n",
218 client, obd->obd_name, err);
220 } else if (!med->med_idmap) {
221 CERROR("client %s -> target %s "
222 "failed to init(2) idmap!\n",
223 client, obd->obd_name);
227 CDEBUG(D_SEC, "client %s -> target %s is remote.\n",
228 client, obd->obd_name);
229 /* NB, MDS_CONNECT establish root idmap too! */
230 rc = mdt_handle_idmap(info);
235 void mdt_cleanup_idmap(struct mdt_export_data *med)
237 cfs_mutex_lock(&med->med_idmap_mutex);
238 if (med->med_idmap != NULL) {
239 lustre_idmap_fini(med->med_idmap);
240 med->med_idmap = NULL;
242 cfs_mutex_unlock(&med->med_idmap_mutex);
245 static inline void mdt_revoke_export_locks(struct obd_export *exp)
247 /* don't revoke locks during recovery */
248 if (exp->exp_obd->obd_recovering)
251 ldlm_revoke_export_locks(exp);
254 int mdt_handle_idmap(struct mdt_thread_info *info)
256 struct ptlrpc_request *req = mdt_info_req(info);
257 struct mdt_device *mdt = info->mti_mdt;
258 struct mdt_export_data *med;
259 struct ptlrpc_user_desc *pud = req->rq_user_desc;
260 struct md_identity *identity;
268 med = mdt_req2med(req);
269 if (!exp_connect_rmtclient(info->mti_exp))
272 opc = lustre_msg_get_opc(req->rq_reqmsg);
273 /* Bypass other opc */
274 if ((opc != SEC_CTX_INIT) && (opc != SEC_CTX_INIT_CONT) &&
275 (opc != SEC_CTX_FINI) && (opc != MDS_CONNECT))
278 LASSERT(med->med_idmap);
280 if (unlikely(!pud)) {
281 CDEBUG(D_SEC, "remote client must run with rq_user_desc "
286 if (req->rq_auth_mapped_uid == INVALID_UID) {
287 CDEBUG(D_SEC, "invalid authorized mapped uid, please check "
288 "/etc/lustre/idmap.conf!\n");
292 if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
293 CDEBUG(D_SEC, "remote client must run with identity_get "
298 identity = mdt_identity_get(mdt->mdt_identity_cache,
299 req->rq_auth_mapped_uid);
300 if (IS_ERR(identity)) {
301 CDEBUG(D_SEC, "can't get mdt identity(%u), no mapping added\n",
302 req->rq_auth_mapped_uid);
308 case SEC_CTX_INIT_CONT:
310 rc = lustre_idmap_add(med->med_idmap,
311 pud->pud_uid, identity->mi_uid,
312 pud->pud_gid, identity->mi_gid);
315 rc = lustre_idmap_del(med->med_idmap,
316 pud->pud_uid, identity->mi_uid,
317 pud->pud_gid, identity->mi_gid);
321 mdt_identity_put(mdt->mdt_identity_cache, identity);
328 case SEC_CTX_INIT_CONT:
330 mdt_revoke_export_locks(req->rq_export);
337 int ptlrpc_user_desc_do_idmap(struct ptlrpc_request *req,
338 struct ptlrpc_user_desc *pud)
340 struct mdt_export_data *med = mdt_req2med(req);
341 struct lustre_idmap_table *idmap = med->med_idmap;
345 /* Only remote client need desc_to_idmap. */
346 if (!exp_connect_rmtclient(req->rq_export))
349 uid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_uid);
350 if (uid == CFS_IDMAP_NOTFOUND) {
351 CDEBUG(D_SEC, "no mapping for uid %u\n", pud->pud_uid);
355 if (pud->pud_uid == pud->pud_fsuid) {
358 fsuid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_fsuid);
359 if (fsuid == CFS_IDMAP_NOTFOUND) {
360 CDEBUG(D_SEC, "no mapping for fsuid %u\n",
366 gid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_gid);
367 if (gid == CFS_IDMAP_NOTFOUND) {
368 CDEBUG(D_SEC, "no mapping for gid %u\n", pud->pud_gid);
372 if (pud->pud_gid == pud->pud_fsgid) {
375 fsgid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_fsgid);
376 if (fsgid == CFS_IDMAP_NOTFOUND) {
377 CDEBUG(D_SEC, "no mapping for fsgid %u\n",
385 pud->pud_fsuid = fsuid;
386 pud->pud_fsgid = fsgid;
394 void mdt_body_reverse_idmap(struct mdt_thread_info *info, struct mdt_body *body)
396 struct ptlrpc_request *req = mdt_info_req(info);
397 struct md_ucred *uc = mdt_ucred(info);
398 struct mdt_export_data *med = mdt_req2med(req);
399 struct lustre_idmap_table *idmap = med->med_idmap;
401 if (!exp_connect_rmtclient(info->mti_exp))
404 if (body->valid & OBD_MD_FLUID) {
405 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 1, body->uid);
407 if (uid == CFS_IDMAP_NOTFOUND) {
409 if (body->valid & OBD_MD_FLMODE)
410 body->mode = (body->mode & ~S_IRWXU) |
411 ((body->mode & S_IRWXO) << 6);
417 if (body->valid & OBD_MD_FLGID) {
418 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 1, body->gid);
420 if (gid == CFS_IDMAP_NOTFOUND) {
422 if (body->valid & OBD_MD_FLMODE)
423 body->mode = (body->mode & ~S_IRWXG) |
424 ((body->mode & S_IRWXO) << 3);
431 /* Do not ignore root_squash for non-setattr case. */
432 int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
434 struct ptlrpc_request *req = mdt_info_req(info);
435 struct md_ucred *uc = mdt_ucred(info);
436 struct lu_attr *attr = &info->mti_attr.ma_attr;
437 struct mdt_export_data *med = mdt_req2med(req);
438 struct lustre_idmap_table *idmap = med->med_idmap;
440 if ((uc->mu_valid != UCRED_OLD) && (uc->mu_valid != UCRED_NEW))
443 if (op != REINT_SETATTR) {
444 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
445 attr->la_uid = uc->mu_fsuid;
446 /* for S_ISGID, inherit gid from his parent, such work will be
447 * done in cmm/mdd layer, here set all cases as uc->mu_fsgid. */
448 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
449 attr->la_gid = uc->mu_fsgid;
450 } else if (exp_connect_rmtclient(info->mti_exp)) {
451 /* NB: -1 case will be handled by mdt_fix_attr() later. */
452 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1)) {
453 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 0,
456 if (uid == CFS_IDMAP_NOTFOUND) {
457 CDEBUG(D_SEC, "Deny chown to uid %u\n",
464 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1)) {
465 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 0,
468 if (gid == CFS_IDMAP_NOTFOUND) {
469 CDEBUG(D_SEC, "Deny chown to gid %u\n",